-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridge.java
More file actions
369 lines (309 loc) · 9.68 KB
/
Bridge.java
File metadata and controls
369 lines (309 loc) · 9.68 KB
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
362
363
364
365
366
367
368
369
////////////////////////////////////////////////////////////////////////////////
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses>.
//
////////////////////////////////////////////////////////////////////////////////
package merapi;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import merapi.config.BridgeConfig;
import merapi.handlers.IMessageHandler;
import merapi.io.amf.AMF3Reader;
import merapi.io.amf.AMF3Writer;
import merapi.io.reader.IReader;
import merapi.io.writer.IWriter;
import merapi.messages.IMessage;
import merapi.systemexecute.handlers.SystemExecuteMessageHandler;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
/**
* The <code>Bridge</code> class is a singleton gateway to the Flex Merapi bridge.
* IMessages are exchanged between this Java bridge object and the Flex bridge object.
*
* @see merapi.messages.IMessage;
*/
public class Bridge implements Runnable
{
//--------------------------------------------------------------------------
//
// Class Constants
//
//--------------------------------------------------------------------------
public static final String CONFIG_PATH = "./config/merapi-native-config.xml";
public static final String READER_STRING = "reader";
public static final String WRITER_STRING = "writer";
public static final String PORT_STRING = "port";
//--------------------------------------------------------------------------
//
// Class Properties
//
//--------------------------------------------------------------------------
/**
* The port used to connect Merapi
*/
public static int PORT = 12345;
//--------------------------------------------------------------------------
//
// Class Methods
//
//--------------------------------------------------------------------------
/**
* The single instance of the Merapi <code>Bridge</code>
*/
public static Bridge getInstance()
{
if ( instance == null )
{
instance = new Bridge();
instance.registerHandlers();
}
return instance;
}
/**
* Opens the Merapi server socket
*/
public static void open()
{
if ( thread == null )
{
Bridge.thread = new Thread( Bridge.getInstance() );
Bridge.thread.start();
Bridge.isRunning = true;
}
}
/**
* Closes the Merapi server socket
*/
public static void close()
{
Bridge.isRunning = false;
Bridge.thread = null;
try
{
instance.__server.close();
}
catch ( IOException exception )
{
Bridge.instance.__logger.error( exception );
}
}
//--------------------------------------------------------------------------
//
// Class Variables
//
//--------------------------------------------------------------------------
private static Bridge instance = null;
private static Thread thread = null;
private static boolean isRunning = true;
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
/**
* Constructor.
*/
private Bridge()
{
super();
readConfig();
__handlers = new HashMap<String, ArrayList<IMessageHandler>>();
}
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Called by start() from the Threading API.
*/
public void run()
{
try
{
// Open the server socket
__server = new ServerSocket( PORT );
__logger.info( "Merapi started on port: " + PORT );
while( Bridge.isRunning == true )
{
// Get the first client that connects
Socket temp = __server.accept();
__client = temp;
// Instantiate a listener thread that will listen and read the input stream
// of the first client that connects and start the thread
__clientListenerThread = new BridgeListenerThread( __client.getInputStream(),
__reader );
__clientListenerThread.start();
}
}
catch ( IOException e )
{
__logger.error( e.getMessage() );
}
}
/**
* Dispatches an <code>IMessage</code> to registered listeners.
*/
public void dispatchMessage( IMessage message )
{
// Get the list of handlers registered for the event type
ArrayList<IMessageHandler> list = __handlers.get( message.getType() );
// If the list is not null and not empty notify the registered event handlers
if ( list != null && list.size() > 0 )
{
for ( IMessageHandler handler : list )
{
if ( handler != null )
{
handler.handleMessage( message );
}
}
}
}
/**
* Registers an <code>IMessageHandler</code> to be notified when messages of type
* <code>type<code> are dispatched from the <code>Bridge</code>.
*/
public void registerMessageHandler( String type, IMessageHandler handler )
{
// Get the list of handlers registered for the event type
ArrayList<IMessageHandler> list = __handlers.get( type );
// If the list is null, create a new list to add 'handler' to
if ( list == null )
{
list = new ArrayList<IMessageHandler>();
__handlers.put( type, list );
}
// Add the handler to the list
list.add( handler );
}
/**
* Sends a <code>message</code> to the Flex side of the Merapi bridge.
*/
public void sendMessage( IMessage message ) throws Exception
{
if ( __client == null || __client.isClosed() ) return;
byte[] bytes = __writer.write( message );
// Send the length of the message first
__client.getOutputStream().write( bytes.length );
// Send the message
__client.getOutputStream().write( bytes );
}
/**
* Unregisters a given handler.
*/
public void unRegisterMessageHandler( String type, IMessageHandler handler )
{
// Get the list of handlers registered for the event type
ArrayList<IMessageHandler> list = __handlers.get( type );
// If the list is not null and not empty, look for handler in the list and remove it
// if a match is found
if ( list != null && list.size() > 0 )
{
for ( IMessageHandler activeHandler : list )
{
if ( activeHandler == handler )
{
list.remove( handler );
}
}
}
}
/**
* @protected
*
* Instantiates the framework <code>IMessageHandlers</code>.
*/
protected void registerHandlers()
{
// Registers SystemExecuteHandler as the IMessageHandler of the
// SystemExecuteMessage.SYSTEM_EXECUTE message type.
new SystemExecuteMessageHandler();
}
/**
* @protected
*
* Loads the Bridge config via the BridgeConfig object.
*/
protected void readConfig()
{
try
{
BridgeConfig bridgeConfig = new BridgeConfig( CONFIG_PATH );
Properties config = bridgeConfig.getProps();
ApplicationContext ctx = bridgeConfig.getContext();
__reader = (IReader) ctx.getBean( config.getProperty( READER_STRING ) );
__writer = (IWriter) ctx.getBean( config.getProperty( WRITER_STRING ) );
PORT = Integer.parseInt( config.getProperty( PORT_STRING ) );
bridgeConfig = null;
}
catch ( Exception e )
{
__logger.error( e.getMessage() );
}
if ( __reader == null ) __reader = new AMF3Reader();
if ( __writer == null ) __writer = new AMF3Writer();
}
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
/**
* @private
*
* The socket that open connections to the Flex Merapi bridge.
*/
private ServerSocket __server = null;
/**
* @private
*
* The socket that connected to the Flex Merapi bridge.
*/
private Socket __client = null;
/**
* @private
*
* The thread that listens for messages from the Flex client socket.
*/
private BridgeListenerThread __clientListenerThread = null;
/**
* @private
*
* The <code>IWriter</code> used to serialize data sent across the bridge to Flex.
*/
private IWriter __writer = null;
/**
* @private
*
* The <code>IReader</code> used to deserialize data that comes across the bridge from Flex.
*/
private IReader __reader = null;
/**
* @private
*
* An instance of the log4j logger to handle the logging.
*/
private Logger __logger = Logger.getLogger( Bridge.class );
/**
* @private
*
* A HashMap of registered event handlers.
*/
private HashMap<String, ArrayList<IMessageHandler>> __handlers = null;
}