summaryrefslogtreecommitdiff
path: root/SDL_Android/SmartDeviceLinkProxyAndroid/src/com/smartdevicelink/transport/SiphonServer.java
blob: df350264b4d0774fda577c5d229d783f6eb969d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.smartdevicelink.transport;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

import com.smartdevicelink.util.*;

public class SiphonServer {
	// Prohibit use of no-arg ctor
	private SiphonServer() {}
	
	enum SiphonDataType {
		fromSmartDeviceLink,				
		fromApp,				
		appLog,
		formattedTrace,
		baselineTimeStamp,		
		traceSettings			
	}
	
	// Boolean to enable/disable the siphon
	private static Boolean m_siphonEnabled = false;
	
	// Boolean to determine if the siphon has been initialized
	private static Boolean m_siphonInitialized = false;
	
	private static Socket m_siphonSocket = null;
	private static Object m_siphonLock = new Object();
	private static ServerSocket m_listeningSocket = null;
	private static OutputStream m_siphonSocketOutputStream = null;
	private static SiphonServerThread m_siphonClientThread = null;
	
	// Initial timestamp in MS
	private static long m_startTimeStamp = 0;
	
	// SMARTDEVICELINK Trace Message Version
	private static byte m_smartDeviceLinkTraceMsgVersionNumber = 1;
	
	// Max number of ports to attempt a connection on
	private final static Integer MAX_NUMBER_OF_PORT_ATTEMPTS = 1000;
	
	// Starting port for future port attempts
	private final static short FIRST_PORT_TO_ATTEMPT_CONNECTION = 7474;

	// Boolean to determine if formatted trace is being sent
	private static Boolean m_sendingFormattedTrace = false;
	
	public static void enableSiphonServer() {
		m_siphonEnabled = true;
	}
	
	public static void disableSiphonServer() {
		m_siphonEnabled = false;
	}

	public static boolean init() {		
		// Only initialize if the siphon has not been initialized previously

		// Check here to be lean. If true, no need to smartDeviceLinkhronize
		if (m_siphonInitialized) {
			return true;
		}
		
		synchronized (m_siphonLock) {
			// To prevent a race condition, re-check m_siphonInitialized inside of smartDeviceLinkhronize block
			if (!m_siphonInitialized) {
				if (m_siphonClientThread == null) {
					// Set current time stamp 
					m_startTimeStamp = System.currentTimeMillis();
					
					// Start Siphon Thread
		            m_siphonClientThread = new SiphonServerThread();
			        m_siphonClientThread.setName("Siphon");
			        m_siphonClientThread.setDaemon(true);
			        m_siphonClientThread.start();
			        
			        m_siphonInitialized = true;
				} // end-if
			} // end-lock
		}
		
		return m_siphonInitialized;
	} // end-method

	public static void closeServer() throws IOException {
		
		if (m_siphonClientThread != null) {
			m_siphonClientThread.halt();
			m_siphonClientThread = null;
		}
		
		if (m_listeningSocket != null) {
			m_listeningSocket.close();
			m_listeningSocket = null;
		}
		
		if (m_siphonSocket != null) {
			m_siphonSocket.close();
			m_siphonSocket = null;
		}
		
		if (m_siphonSocketOutputStream != null) {
			m_siphonSocketOutputStream.close();
			m_siphonSocketOutputStream = null;
		}
	}
    
	public static Boolean sendBytesFromAPP(byte[] msgBytes, int offset, int length) {
		
		if (m_sendingFormattedTrace) {
			return false;
		}
		
		return sendSiphonData(SiphonDataType.fromApp, msgBytes, offset, length);
	} // end-method

	public static Boolean sendBytesFromSMARTDEVICELINK(byte[] msgBytes, int offset, int length) {		

		if (m_sendingFormattedTrace) {
			return false;
		}
		
		return sendSiphonData(SiphonDataType.fromSmartDeviceLink, msgBytes, offset, length);
	} // end-method
	
	public static Boolean sendSiphonLogData(String message) {
		
		if (m_sendingFormattedTrace) {
			return false;
		}
		
		if (message == null || message.length() == 0) {
			return false;
		}
				
		byte messageBytes[] = null;
		int messageLength = 0;
		
		try {
			messageBytes = message.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			return false;
		}
		
		messageLength = messageBytes.length;
		return sendSiphonData(SiphonDataType.appLog, messageBytes, 0, messageLength);
		
	}
	
	public static Boolean sendFormattedTraceMessage(String message) {
				
		if (message == null || message.length() == 0) {
			return false;
		}
		
		byte messageBytes[] = null;
		int messageLength = 0;
				
		try {
			messageBytes = message.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			return false;
		}
		
		messageLength = messageBytes.length;
		if (sendSiphonData(SiphonDataType.formattedTrace, messageBytes, 0, messageLength)) {
			m_sendingFormattedTrace = true;
			return true;
		} else {
			return false;
		}
	}
	
	private static Boolean sendSiphonData(SiphonDataType direction, byte[] msgBytes, int offset, int length) {
		byte siphonDataTypeIndicator = 0x00;
		
		long currentDateTime = System.currentTimeMillis();
		Integer deltaTimeMills = null;
			
		deltaTimeMills = (int)(currentDateTime - m_startTimeStamp);
		
		switch(direction) {
			case fromSmartDeviceLink:
				siphonDataTypeIndicator = 0x00;
				break;
			case fromApp:
				siphonDataTypeIndicator = 0x01;
				break;
			case appLog:
				siphonDataTypeIndicator = 0x02;
				break;
			case formattedTrace:
				siphonDataTypeIndicator = 0x03;
				break;
			case baselineTimeStamp:
				siphonDataTypeIndicator = 0x04;
				break;
			case traceSettings:
				siphonDataTypeIndicator = 0x05;
				break;
			default:
				siphonDataTypeIndicator = 0x00;
				break;
		}
		
		// Set high bit to indicate new format
		siphonDataTypeIndicator = (byte)((byte)0x80 | siphonDataTypeIndicator);
		
		return sendDataToSiphonSocket(siphonDataTypeIndicator, deltaTimeMills, msgBytes, offset, length);
	}
	
	private synchronized static Boolean sendDataToSiphonSocket(byte directionIndicator, Integer timeStamp, 
			byte[] msgBytes, int offset, int length) {
		if (!m_siphonEnabled) {
			return false;
		}
		
		if (msgBytes == null || length == 0) {
			return false;
		}
		
		OutputStream siphonOutputStream = null;

		synchronized (m_siphonLock) {
			siphonOutputStream = m_siphonSocketOutputStream;
		} // end-lock

		if (siphonOutputStream == null) {
			return false;
		}		
		
		try	{		
			// blobSize = length(of message) + 1(size of direction indicator)
			//				+ 1 (size of msgVersionNumber) + 4 (size of timeStamp)
			int blobSize = length + 1 + 1 + 4;
			
			siphonOutputStream.write(BitConverter.intToByteArray(blobSize));
			siphonOutputStream.write(new byte[] {directionIndicator});
			siphonOutputStream.write(new byte[] {m_smartDeviceLinkTraceMsgVersionNumber});
			siphonOutputStream.write(intToByteArray(timeStamp));
			siphonOutputStream.write(msgBytes, offset, length);
		} catch (Exception ex) {
			return false;
		} // end-catch
		
		return true;
	} // end-method
    
    private static class SiphonServerThread extends Thread {
    	
    	private Boolean isHalted = false;
    	short listenPort;
		
		public void halt() {
			isHalted = true;
		}
    	
    	private boolean findOpenSocket(short port) {
    		// Accept incoming sihpon connection from trace utility.
    		Boolean foundOpenPort = false;
    		listenPort = port;
    		
    		// Listen to accept incoming connection from SMARTDEVICELINK
    		while (!foundOpenPort) {
	    		try {
	    			m_listeningSocket = new ServerSocket(listenPort);
	    			foundOpenPort = true;
	    		} catch (BindException ex) {
	    			listenPort++;
	    			if(listenPort > port + MAX_NUMBER_OF_PORT_ATTEMPTS) {
	    				return false;
	    			}
	    		} catch (IOException e) {
	    			return false;
				}
    		}
    		
    		return foundOpenPort;
    	}
    	
    	private void startServerOnPort() throws IOException {
    		Socket newSocket = null;
    		
    		// Wait for a connection
    		newSocket = m_listeningSocket.accept(); 
            
            // If isHalted after accept() delay, return
        	if (isHalted) {
        		return;
        	}

    		synchronized (m_siphonLock) {
    			// Reset siphonSocketOutputStream
    			if (m_siphonSocketOutputStream != null) {
    				try {
    					m_siphonSocketOutputStream.close();
    				} catch (IOException e) {
    					// Do nothing
    				}
    				m_siphonSocketOutputStream = null;
    			}

    			// Reset siphonSocket
    			if (m_siphonSocket != null) {
    				try {
    					m_siphonSocket.close();
    				} catch (IOException e) {
    					// Do nothing
    				}
    				m_siphonSocket = null;
    			}

    			// Store the new socket
    			m_siphonSocket = newSocket;
    			
    			// Set Socket Options
				m_siphonSocket.setKeepAlive(true);
				
				// Get the output stream of the connection
				m_siphonSocketOutputStream = m_siphonSocket.getOutputStream();
				
	        	// Output version number to the Siphon upon connection (version number prepending to logInfo)
				DebugTool.logInfo("Siphon connected.");
    		} // end-lock
        } // end-method
    	
    	@Override
    	public void run() {
    		try	{
    			if (findOpenSocket(FIRST_PORT_TO_ATTEMPT_CONNECTION)) {
    				while (!isHalted) {
	    				startServerOnPort();
    				}
    			} 
	    	} catch (Exception ex) {
	    		// Do nothing
	    	} finally {
				if (m_listeningSocket != null) {
					try {
						m_listeningSocket.close();
					} catch (IOException e) {
						// Do nothing
					}
					m_listeningSocket = null;
				}
			}
    	}
    }
    
    private static final byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }
} // end-class