1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.test.vcl.client;
25 
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 
29 
30 /**
31  *
32  * The class is used to handle handshake package
33  *
34  */
35 public class Handshaker implements CommunicationListener, Constant {
36 
37 	private static Logger logger = Logger.getLogger("Handshaker");
38 
39 	private CommunicationManager communicationManager = null;
40 
Handshaker(CommunicationManager communicationManager)41 	public Handshaker(CommunicationManager communicationManager) {
42 		this.communicationManager = communicationManager;
43 		this.communicationManager.addListener(this);
44 	}
45 
received(int headerType, byte[] header, byte[] data)46 	public void received(int headerType, byte[] header, byte[] data) {
47 		if (headerType == CH_Handshake) {
48 			int handshakeType = data[1] + ((data[0] & 255) << 8);
49 			switch (handshakeType) {
50 			case CH_REQUEST_HandshakeAlive:
51 				logger.log(Level.CONFIG, "Receive Handshake - CH_REQUEST_HandshakeAlive");
52 				sendHandshake(CH_RESPONSE_HandshakeAlive);
53 				break;
54 			case CH_REQUEST_ShutdownLink:
55 				logger.log(Level.CONFIG, "Receive Handshake - CH_REQUEST_ShutdownLink");
56 				sendHandshake(CH_ShutdownLink);
57 				break;
58 			case CH_ShutdownLink:
59 				logger.log(Level.CONFIG, "Receive Handshake - CH_ShutdownLink");
60 				communicationManager.stop();
61 				break;
62 			case CH_SetApplication:
63 				//String len
64 //				int len = data[2] + ((data[3] & 255) << 8);
65 //				String app = new String(data, 4, data.length - 4);
66 				logger.log(Level.CONFIG, "Receive Handshake - CH_SetApplication - app");
67 				//sendHandshake(CH_SetApplication);
68 				break;
69 			default:
70 			}
71 		}
72 	}
sendHandshake(int handshakeType)73 	public void sendHandshake(int handshakeType) {
74 		sendHandshake(handshakeType, new byte[0]);
75 	}
sendHandshake(int handshakeType, byte[] data)76 	public void sendHandshake(int handshakeType, byte[] data) {
77 		byte[] realData = new byte[data.length + 2];
78 		realData[0] = (byte) ((handshakeType >>> 8) & 0xFF);
79 		realData[1] = (byte) ((handshakeType >>> 0) & 0xFF);
80 		System.arraycopy(data, 0, realData, 2, data.length);
81 		communicationManager.sendPackage(CH_Handshake, null, realData);
82 	}
83 
start()84 	public void start() {
85 	}
86 
stop()87 	public void stop() {
88 	}
89 }
90