-
Notifications
You must be signed in to change notification settings - Fork 470
/
STOMP.java
685 lines (586 loc) · 23.9 KB
/
STOMP.java
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
package org.jgroups.protocols;
import org.jgroups.*;
import org.jgroups.annotations.*;
import org.jgroups.stack.Protocol;
import org.jgroups.util.MessageBatch;
import org.jgroups.util.UUID;
import org.jgroups.util.Util;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
/**
* Protocol which provides STOMP (http://stomp.codehaus.org/) support. Very simple implementation, with a
* one-thread-per-connection model. Use for a few hundred clients max.<p/>
* The intended use for this protocol is pub-sub with clients which handle text messages, e.g. stock updates,
* SMS messages to mobile clients, SNMP traps etc.<p/>
* Note that the full STOMP protocol has not yet been implemented, e.g. transactions are not supported.
* todo: use a thread pool to handle incoming frames and to send messages to clients
* <p/>
* todo: add PING to test health of client connections
* <p/>
* @author Bela Ban
* @since 2.11
*/
@MBean(description="Server side STOPM protocol, STOMP clients can connect to it")
public class STOMP extends Protocol implements Runnable {
/* ----------------------------------------- Properties ----------------------------------------------- */
@Property(name="bind_addr",
description="The bind address which should be used by the server socket. The following special values " +
"are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
defaultValueIPv4="0.0.0.0", defaultValueIPv6="::", writable=false)
protected InetAddress bind_addr;
@Property(description="If set, then endpoint will be set to this address")
protected String endpoint_addr;
@Property(description="Port on which the STOMP protocol listens for requests",writable=false)
protected int port=8787;
@Property(description="If set to false, then a destination of /a/b match /a/b/c, a/b/d, a/b/c/d etc")
protected boolean exact_destination_match=true;
@Property(description="If true, information such as a list of endpoints, or views, will be sent to all clients " +
"(via the INFO command). This allows for example intelligent clients to connect to " +
"a different server should a connection be closed.")
protected boolean send_info=true;
@Property(description="Forward received messages which don't have a StompHeader to clients")
protected boolean forward_non_client_generated_msgs=false;
/* --------------------------------------------- JMX ---------------------------------------------------*/
@ManagedAttribute(description="Number of client connections")
public int getNumConnections() {return connections.size();}
@ManagedAttribute(description="Number of subscriptions")
public int getNumSubscriptions() {return subscriptions.size();}
@ManagedAttribute(description="Print subscriptions")
public String getSubscriptions() {return subscriptions.keySet().toString();}
@ManagedAttribute
public String getEndpoints() {return endpoints.toString();}
/* --------------------------------------------- Fields ------------------------------------------------------ */
protected Address local_addr;
protected ServerSocket srv_sock;
@ManagedAttribute
protected String endpoint;
protected Thread acceptor;
protected final List<Connection> connections=new LinkedList<>();
protected final Map<Address,String> endpoints=new HashMap<>();
protected View view;
// Subscriptions and connections which are subscribed
protected final ConcurrentMap<String,Set<Connection>> subscriptions=Util.createConcurrentMap(20);
public enum ClientVerb {CONNECT, SEND, SUBSCRIBE, UNSUBSCRIBE, BEGIN, COMMIT, ABORT, ACK, DISCONNECT}
public enum ServerVerb {MESSAGE, RECEIPT, ERROR, CONNECTED, INFO}
public static final byte NULL_BYTE=0;
public STOMP() {
}
public void start() throws Exception {
super.start();
srv_sock=Util.createServerSocket(getSocketFactory(), "jgroups.stomp.srv_sock", bind_addr, port);
if(log.isDebugEnabled())
log.debug("server socket listening on " + srv_sock.getLocalSocketAddress());
if(acceptor == null) {
acceptor=getThreadFactory().newThread(this, "STOMP acceptor");
acceptor.setDaemon(true);
acceptor.start();
}
endpoint=endpoint_addr != null? endpoint_addr : getAddress();
}
public void stop() {
if(log.isDebugEnabled())
log.debug("closing server socket " + srv_sock.getLocalSocketAddress());
if(acceptor != null && acceptor.isAlive()) {
try {
// this will terminate thread, peer will receive SocketException (socket close)
getSocketFactory().close(srv_sock);
}
catch(Exception ex) {
}
}
synchronized(connections) {
connections.forEach(Connection::stop);
connections.clear();
}
acceptor=null;
super.stop();
}
// Acceptor loop
public void run() {
Socket client_sock;
while(acceptor != null && srv_sock != null) {
try {
client_sock=srv_sock.accept();
if(log.isTraceEnabled())
log.trace("accepted connection from " + client_sock.getInetAddress() + ':' + client_sock.getPort());
Connection conn=new Connection(client_sock);
Thread thread=getThreadFactory().newThread(conn, "STOMP client connection");
thread.setDaemon(true);
synchronized(connections) {
connections.add(conn);
}
thread.start();
conn.sendInfo();
}
catch(IOException io_ex) {
break;
}
}
acceptor=null;
}
public Object down(Event evt) {
switch(evt.getType()) {
case Event.VIEW_CHANGE:
handleView(evt.getArg());
break;
case Event.SET_LOCAL_ADDRESS:
local_addr=evt.getArg();
break;
}
return down_prot.down(evt);
}
public Object up(Event evt) {
switch(evt.getType()) {
case Event.VIEW_CHANGE:
handleView(evt.getArg());
break;
}
return up_prot.up(evt);
}
public Object up(Message msg) {
StompHeader hdr=msg.getHeader(id);
if(hdr == null) {
if(forward_non_client_generated_msgs) {
HashMap<String, String> hdrs=new HashMap<>();
hdrs.put("sender", msg.getSrc().toString());
sendToClients(hdrs, msg.getRawBuffer(), msg.getOffset(), msg.getLength());
}
return up_prot.up(msg);
}
switch(hdr.type) {
case MESSAGE:
sendToClients(hdr.headers, msg.getRawBuffer(), msg.getOffset(), msg.getLength());
break;
case ENDPOINT:
String tmp_endpoint=hdr.headers.get("endpoint");
if(tmp_endpoint != null) {
boolean update_clients;
String old_endpoint=null;
synchronized(endpoints) {
endpoints.put(msg.getSrc(), tmp_endpoint);
}
update_clients=!Objects.equals(old_endpoint, tmp_endpoint);
if(update_clients && this.send_info) {
synchronized(connections) {
for(Connection conn: connections) {
conn.writeResponse(ServerVerb.INFO, "endpoints", getAllEndpoints());
}
}
}
}
return null;
default:
throw new IllegalArgumentException("type " + hdr.type + " is not known");
}
return up_prot.up(msg);
}
public void up(MessageBatch batch) {
for(Message msg: batch) {
StompHeader hdr=msg.getHeader(id);
if(hdr != null || forward_non_client_generated_msgs) {
try {
batch.remove(msg);
up(msg);
}
catch(Throwable t) {
log.error(Util.getMessage("FailedPassingUpMessage"), t);
}
}
}
if(!batch.isEmpty())
up_prot.up(batch);
}
public static Frame readFrame(DataInputStream in) throws IOException {
String verb=Util.readLine(in);
if(verb == null)
throw new EOFException("reading verb");
if(verb.isEmpty())
return null;
verb=verb.trim();
Map<String,String> headers=new HashMap<>();
byte[] body=null;
for(;;) {
String header=Util.readLine(in);
if(header == null)
throw new EOFException("reading header");
if(header.isEmpty())
break;
int index=header.indexOf(':');
if(index != -1)
headers.put(header.substring(0, index).trim(), header.substring(index+1).trim());
}
if(headers.containsKey("content-length")) {
int length=Integer.parseInt(headers.get("content-length"));
body=new byte[length];
in.read(body, 0, body.length);
}
else {
ByteBuffer buf=ByteBuffer.allocate(500);
boolean terminate=false;
for(;;) {
int c=in.read();
if(c == -1 || c == 0)
terminate=true;
if(buf.remaining() == 0 || terminate) {
if(body == null) {
body=new byte[buf.position()];
System.arraycopy(buf.array(), buf.arrayOffset(), body, 0, buf.position());
}
else {
byte[] tmp=new byte[body.length + buf.position()];
System.arraycopy(body, 0, tmp, 0, body.length);
try {
System.arraycopy(buf.array(), buf.arrayOffset(), tmp, body.length, buf.position());
}
catch(Throwable t) {
}
body=tmp;
}
buf.rewind();
}
if(terminate)
break;
buf.put((byte)c);
}
}
return new Frame(verb, headers, body);
}
protected void handleView(View view) {
broadcastEndpoint();
List<Address> mbrs=view.getMembers();
this.view=view;
synchronized(endpoints) {
endpoints.keySet().retainAll(mbrs);
}
synchronized(connections) {
connections.forEach(Connection::sendInfo);
}
}
private String getAddress() {
InetSocketAddress saddr=(InetSocketAddress)srv_sock.getLocalSocketAddress();
InetAddress tmp=saddr.getAddress();
if(!tmp.isAnyLocalAddress())
return tmp.getHostAddress() + ":" + srv_sock.getLocalPort();
for(Util.AddressScope scope: Util.AddressScope.values()) {
try {
InetAddress addr=Util.getAddress(scope);
if(addr != null) return addr.getHostAddress() + ":" + srv_sock.getLocalPort();
}
catch(SocketException e) {
}
}
return null;
}
protected String getAllEndpoints() {
synchronized(endpoints) {
return Util.printListWithDelimiter(endpoints.values(), ",");
}
}
protected void broadcastEndpoint() {
if(endpoint != null) {
Message msg=new Message().putHeader(id, StompHeader.createHeader(StompHeader.Type.ENDPOINT, "endpoint", endpoint));
down_prot.down(msg);
}
}
private void sendToClients(Map<String,String> headers, byte[] buffer, int offset, int length) {
int len=50 + length + (ServerVerb.MESSAGE.name().length() + 2);
if(headers != null) {
for(Map.Entry<String,String> entry: headers.entrySet()) {
len+=entry.getKey().length() +2;
len+=entry.getValue().length() +2;
len+=5; // fill chars, such as ": " or "\n"
}
}
len+=(buffer != null? 20 : 0);
ByteBuffer buf=ByteBuffer.allocate(len);
StringBuilder sb=new StringBuilder(ServerVerb.MESSAGE.name()).append("\n");
if(headers != null) {
for(Map.Entry<String,String> entry: headers.entrySet())
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
if(buffer != null)
sb.append("content-length: ").append(String.valueOf(length)).append("\n");
sb.append("\n");
byte[] tmp=sb.toString().getBytes();
if(buffer != null) {
buf.put(tmp, 0, tmp.length);
buf.put(buffer, offset, length);
}
buf.put(NULL_BYTE);
final Set<Connection> target_connections=new HashSet<>();
String destination=headers != null? headers.get("destination") : null;
if(destination == null) {
synchronized(connections) {
target_connections.addAll(connections);
}
}
else {
if(!exact_destination_match) {
subscriptions.entrySet().stream().filter(entry -> entry.getKey().startsWith(destination))
.forEach(entry -> target_connections.addAll(entry.getValue()));
}
else {
Set<Connection> conns=subscriptions.get(destination);
if(conns != null)
target_connections.addAll(conns);
}
}
for(Connection conn: target_connections)
conn.writeResponse(buf.array(), buf.arrayOffset(), buf.position());
}
/**
* Class which handles a connection to a client
*/
public class Connection implements Runnable {
protected final Socket sock;
protected final DataInputStream in;
protected final DataOutputStream out;
protected final UUID session_id=UUID.randomUUID();
public Connection(Socket sock) throws IOException {
this.sock=sock;
this.in=new DataInputStream(sock.getInputStream());
this.out=new DataOutputStream(sock.getOutputStream());
}
public void stop() {
if(log.isTraceEnabled())
log.trace("closing connection to " + sock.getRemoteSocketAddress());
Util.close(in);
Util.close(out);
Util.close(sock);
}
protected void remove() {
synchronized(connections) {
connections.remove(this);
}
for(Set<Connection> conns: subscriptions.values()) {
conns.remove(this);
}
for(Iterator<Map.Entry<String,Set<Connection>>> it=subscriptions.entrySet().iterator(); it.hasNext();) {
Map.Entry<String,Set<Connection>> entry=it.next();
if(entry.getValue().isEmpty())
it.remove();
}
}
public void run() {
while(!sock.isClosed()) {
try {
Frame frame=readFrame(in);
if(frame != null) {
if(log.isTraceEnabled())
log.trace(frame);
handleFrame(frame);
}
}
catch(IOException ex) {
stop();
remove();
}
catch(Throwable t) {
log.error(Util.getMessage("FailureReadingFrame"), t);
}
}
}
protected void handleFrame(Frame frame) {
Map<String,String> headers=frame.getHeaders();
ClientVerb verb=ClientVerb.valueOf(frame.getVerb());
switch(verb) {
case CONNECT:
writeResponse(ServerVerb.CONNECTED,
"session-id", session_id.toString(),
"password-check", "none");
break;
case SEND:
if(!headers.containsKey("sender")) {
headers.put("sender", session_id.toString());
}
Message msg=new Message(null, frame.getBody());
Header hdr=StompHeader.createHeader(StompHeader.Type.MESSAGE, headers);
msg.putHeader(id, hdr);
down_prot.down(msg);
String receipt=headers.get("receipt");
if(receipt != null)
writeResponse(ServerVerb.RECEIPT, "receipt-id", receipt);
break;
case SUBSCRIBE:
String destination=headers.get("destination");
if(destination != null) {
Set<Connection> conns=subscriptions.get(destination);
if(conns == null) {
conns=new HashSet<>();
Set<Connection> tmp=subscriptions.putIfAbsent(destination, conns);
if(tmp != null)
conns=tmp;
}
conns.add(this);
}
break;
case UNSUBSCRIBE:
destination=headers.get("destination");
if(destination != null) {
Set<Connection> conns=subscriptions.get(destination);
if(conns != null && conns.remove(this) && conns.isEmpty())
subscriptions.remove(destination);
}
break;
case BEGIN:
break;
case COMMIT:
break;
case ABORT:
break;
case ACK:
break;
case DISCONNECT:
break;
default:
log.error("Verb " + frame.getVerb() + " is not handled");
break;
}
}
public void sendInfo() {
if(send_info) {
writeResponse(ServerVerb.INFO,
"local_addr", local_addr != null? local_addr.toString() : "n/a",
"view", view.toString(),
"endpoints", getAllEndpoints());
// "clients", getAllClients());
}
}
/**
* Sends back a response. The keys_and_values vararg array needs to have an even number of elements
* @param response
* @param keys_and_values
*/
private void writeResponse(ServerVerb response, String ... keys_and_values) {
String tmp=response.name();
try {
out.write(tmp.getBytes());
out.write('\n');
for(int i=0; i < keys_and_values.length; i++) {
String key=keys_and_values[i];
String val=keys_and_values[++i];
out.write((key + ": " + val + "\n").getBytes());
}
out.write("\n".getBytes());
out.write(NULL_BYTE);
out.flush();
}
catch(IOException ex) {
log.error(Util.getMessage("FailedWritingResponse") + response + ": " + ex);
}
}
private void writeResponse(byte[] response, int offset, int length) {
try {
out.write(response, offset, length);
out.flush();
}
catch(IOException ex) {
log.error(Util.getMessage("FailedWritingResponse") + ex);
}
}
}
public static class Frame {
final String verb;
final Map<String,String> headers;
final byte[] body;
public Frame(String verb, Map<String, String> headers, byte[] body) {
this.verb=verb;
this.headers=headers;
this.body=body;
}
public byte[] getBody() {
return body;
}
public Map<String, String> getHeaders() {
return headers;
}
public String getVerb() {
return verb;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append(verb).append("\n");
if(headers != null && !headers.isEmpty()) {
for(Map.Entry<String,String> entry: headers.entrySet())
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
if(body != null && body.length > 0) {
sb.append("body: ");
if(body.length < 50)
sb.append(new String(body)).append(" (").append(body.length).append(" bytes)");
else
sb.append(body.length).append(" bytes");
}
return sb.toString();
}
}
public static class StompHeader extends org.jgroups.Header {
public enum Type {MESSAGE, ENDPOINT}
protected Type type;
protected final Map<String,String> headers=new HashMap<>();
public StompHeader() {
}
public Supplier<? extends Header> create() {return StompHeader::new;}
public short getMagicId() {return 71;}
private StompHeader(Type type) {
this.type=type;
}
/**
* Creates a new header
* @param type
* @param headers Keys and values to be added to the header hashmap. Needs to be an even number
* @return
*/
public static StompHeader createHeader(Type type, String ... headers) {
StompHeader retval=new StompHeader(type);
if(headers != null) {
for(int i=0; i < headers.length; i++) {
String key=headers[i];
String value=headers[++i];
retval.headers.put(key, value);
}
}
return retval;
}
public static StompHeader createHeader(Type type, Map<String,String> headers) {
StompHeader retval=new StompHeader(type);
if(headers != null)
retval.headers.putAll(headers);
return retval;
}
public int serializedSize() {
int retval=Global.INT_SIZE *2; // type + size of hashmap
for(Map.Entry<String,String> entry: headers.entrySet()) {
retval+=entry.getKey().length() +2;
retval+=entry.getValue().length() +2;
}
return retval;
}
public void writeTo(DataOutput out) throws Exception {
out.writeInt(type.ordinal());
out.writeInt(headers.size());
for(Map.Entry<String,String> entry: headers.entrySet()) {
out.writeUTF(entry.getKey());
out.writeUTF(entry.getValue());
}
}
public void readFrom(DataInput in) throws Exception {
type=Type.values()[in.readInt()];
int size=in.readInt();
for(int i=0; i < size; i++) {
String key=in.readUTF();
String value=in.readUTF();
headers.put(key, value);
}
}
public String toString() {
StringBuilder sb=new StringBuilder(type.toString());
sb.append("headers: ").append(headers);
return sb.toString();
}
}
}