-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMQTTServer.java
More file actions
978 lines (921 loc) · 28.9 KB
/
Copy pathMQTTServer.java
File metadata and controls
978 lines (921 loc) · 28.9 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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
package javaforce.service;
import java.io.*;
import java.net.*;
import java.util.*;
import javaforce.*;
import javaforce.bus.*;
import static javaforce.MQTT.*;
/** MQTTServer (broker) service
*
* @author pquiring
*/
public class MQTTServer extends ConfigServlet {
public final static String serviceBus = "javaforce.jfmqtt";
private static MQTTServer service;
private static JBusServer busServer;
private ArrayList<ServerWorker> servers = new ArrayList<ServerWorker>();
public String getAppName() {return "MQTT";}
public String getBusName() {return serviceBus;}
public String getHelpURL() {return "https://pquiring.github.io/javaforce//projects/jfmqtt-server/docs/help.html";}
public static void serviceStart(String[] args) {
service = new MQTTServer();
service.start();
if (JF.isWindows()) {
busServer = new JBusServer(serviceBus, new JBusMethods());
busServer.connect();
}
for(String arg : args) {
switch (arg) {
case "debug": debug = true; JFLog.log("debug enabled"); break;
case "debug_auth": debug_auth = true; JFLog.log("debug auth enabled"); break;
case "debug_msg": debug_msg = true; JFLog.log("debug messages enabled");break;
}
}
}
public static void serviceStop() {
if (service != null) {
service.stop();
service = null;
}
}
public static String getLogFile() {
return JF.getLogPath() + "/jfmqtt.log";
}
public static String getConfigFile() {
return JF.getConfigPath() + "/jfmqtt.cfg";
}
private static String getKeyFile() {
return JF.getConfigPath() + "/jfmqtt.key";
}
public void setListener(MQTTEvents events) {
this.events = events;
}
public void start() {
server = new Server();
server.start();
}
public void stop() {
if (server == null) return;
server.active = false;
synchronized(lock) {
ServerWorker[] sa = servers.toArray(new ServerWorker[0]);
for(ServerWorker s : sa) {
s.close();
}
servers.clear();
}
if (forwarder != null) {
forwarder.stop();
forwarder = null;
}
synchronized(server) {
server.notify();
}
if (busServer != null) {
busServer.disconnect();
busServer = null;
}
server = null;
}
private static class Config {
public int port = 1883;
public int secure = 8883;
public String user, pass;
public String forward;
public int forward_port = 1883;
public boolean forward_secure;
public String forward_topic = wildcard_multi_level;
public String forward_user;
public String forward_pass;
}
private static String defaultConfig
= "port=1883\n"
+ "secure=8883\n"
+ "#user=username\n"
+ "#pass=password\n"
+ "#forward=host\n"
+ "#forward.port=1883 or 8883\n"
+ "#forward.secure=true\n"
+ "#forward.topic=" + wildcard_multi_level + "\n"
+ "#forward.user=username\n"
+ "#forward.pass=password\n"
;
private static Config loadConfig() {
try {
File file = new File(getConfigFile());
FileInputStream fis = new FileInputStream(file);
Properties props = new Properties();
props.load(fis);
fis.close();
Config config = new Config();
String port = props.getProperty("port");
if (port != null) {
config.port = JF.atoi(port);
if (config.port <= 0 || config.port > 65535) {
config.port = 1883;
}
}
String secure = props.getProperty("secure");
if (secure != null) {
config.secure = JF.atoi(secure);
if (config.secure <= 0 || config.secure > 65535) {
config.secure = 1883;
}
}
String user = props.getProperty("user");
if (user != null) {
config.user = user;
}
String pass = props.getProperty("pass");
if (pass != null) {
config.pass = pass;
}
String forward = props.getProperty("forward");
if (forward != null) {
config.forward = forward;
}
String forward_port = props.getProperty("forward.port");
if (forward_port != null) {
config.forward_port = JF.atoi(forward_port);
if (config.forward_port <= 0 || config.forward_port > 65535) {
config.forward_port = 1883;
}
}
String forward_secure = props.getProperty("forward.secure");
if (forward_port != null) {
config.forward_secure = forward_secure.equals("true");
}
String forward_topic = props.getProperty("forward.topic");
if (forward_topic != null) {
config.forward_topic = forward_topic;
}
String forward_user = props.getProperty("forward.user");
if (forward_user != null) {
config.forward_user = forward_user;
}
String forward_pass = props.getProperty("forward.pass");
if (forward_pass != null) {
config.forward_pass = forward_pass;
}
return config;
} catch (FileNotFoundException e) {
//create default config
try {
FileOutputStream fos = new FileOutputStream(getConfigFile());
fos.write(defaultConfig.getBytes());
fos.close();
} catch (Exception e2) {
JFLog.log(e2);
}
return new Config();
} catch (Exception e) {
JFLog.log(e);
return new Config();
}
}
private void loadKeys() {
try {
keys = new KeyMgmt();
if (new File(getKeyFile()).exists()) {
FileInputStream fis = new FileInputStream(getKeyFile());
keys.open(fis, "password");
fis.close();
} else {
JFLog.log("Warning:Server SSL Keys not generated!");
}
} catch (Exception e) {
JFLog.log(e);
}
}
public static boolean hasWildcard(String topic) {
if (topic.indexOf(wildcard_single_level_char) != -1) return true;
if (topic.indexOf(wildcard_multi_level_char) != -1) return true;
return false;
}
private static class Topic {
private String name;
private String value; //retained value
private ArrayList<Client> subs = new ArrayList<>();
private Object lock = new Object();
public static Topic[] TopicArrayType = new Topic[0];
public Topic(String name) {
this.name = name;
}
public void publish(String value, boolean retain) {
if (retain) {
this.value = value;
}
synchronized (lock) {
for(Client sub : subs.toArray(ClientArrayType)) {
try {
sub.publish(name, value, QOS_0);
} catch (Exception e) {
unsubscribe(sub);
}
}
}
}
public void subscribe(Client client) {
synchronized (lock) {
subs.add(client);
}
if (value != null) {
try {client.publish(name, value, QOS_0);} catch (Exception e) {}
}
}
public void unsubscribe(Client client) {
synchronized (lock) {
subs.remove(client);
}
}
public boolean matches(String topic) {
String[] ns = name.split("[/]");
String[] ws = topic.split("[/]");
int ni = 0;
int wi = 0;
for(;ni<ns.length;ni++) {
String n = ns[ni];
if (wi == ws.length) return false;
String w = ws[wi];
wi++;
if (w.equals(wildcard_single_level)) {
//match any section
continue;
}
if (w.equals(wildcard_multi_level)) {
//match remainder
return true;
}
if (!n.equals(w)) {
//section does not match
return false;
}
}
if (wi != ws.length) {
String w = ws[wi];
if (w.equals(wildcard_multi_level)) return true;
return false;
}
return true;
}
}
private Server server;
private Config config;
private Object lock = new Object();
private HashMap<String, Topic> topics = new HashMap<>();
private MQTTEvents events;
private KeyMgmt keys;
private MQTTForward forwarder;
private static int bufsiz = 4096;
public static boolean debug = false;
public static boolean debug_auth = false;
public static boolean debug_msg = false;
private class Server extends Thread {
public boolean active;
public void run() {
active = true;
try {
JFLog.append(getLogFile(), true);
config = loadConfig();
loadKeys();
if (config.forward != null) {
forwarder = new MQTTForward();
KeyMgmt forward_keys = null;
if (config.forward_secure) {
forward_keys = keys;
}
if (config.forward_user != null && config.forward_pass != null) {
forwarder.start(config.forward, config.forward_port, forward_keys, config.forward_user, config.forward_pass);
} else {
forwarder.start(config.forward, config.forward_port, forward_keys);
}
}
if (config.port > 0) {
ServerWorker worker = new ServerWorker(config.port, false);
worker.start();
servers.add(worker);
}
if (config.secure > 0) {
ServerWorker worker = new ServerWorker(config.secure, true);
worker.start();
servers.add(worker);
}
while(active) {
synchronized(this) {
wait();
}
}
} catch (Exception e) {
JFLog.log(e);
}
}
}
private class ServerWorker extends Thread {
private ServerSocket ss;
private int port;
private boolean secure;
public boolean worker_active;
public ServerWorker(int port, boolean secure) {
this.port = port;
this.secure = secure;
}
public void run() {
try {
if (secure) {
JFLog.log("CreateServerSocketSSL:" + port);
ss = JF.createServerSocketSSL(port, keys);
} else {
JFLog.log("CreateServerSocket:" + port);
ss = new ServerSocket(port);
}
worker_active = true;
while (worker_active) {
Socket s = ss.accept();
Client client = new Client(s);
client.start();
}
} catch (Exception e) {
JFLog.log(e);
}
}
public void close() {
worker_active = false;
if (ss != null) {
try { ss.close(); } catch (Exception e) {}
ss = null;
}
}
}
private Topic getTopic(String name) {
synchronized (lock) {
Topic topic = topics.get(name);
if (topic != null) return topic;
topic = new Topic(name);
topics.put(name, topic);
return topic;
}
}
private Topic[] getTopics(String wc) {
ArrayList<Topic> topics_sub = new ArrayList<>();
synchronized (lock) {
for(Topic topic : topics.values().toArray(Topic.TopicArrayType)) {
if (topic.matches(wc)) {
topics_sub.add(topic);
}
}
}
return topics_sub.toArray(Topic.TopicArrayType);
}
private void unsubscribeAll(Client client) {
synchronized (lock) {
Topic[] alltopics = (Topic[])topics.values().toArray(Topic.TopicArrayType);
for(Topic topic : alltopics) {
topic.unsubscribe(client);
}
}
}
/** Get variable sized length. */
private int getLength(byte[] data, int pos, int length) {
int multi = 1;
int value = 0;
int next;
do {
if (pos >= length) return -1;
next = data[pos++] & 0xff;
value += (next & 0x7f) * multi;
multi *= 0x80;
} while (next >= 0x80);
return value;
}
private int getStringLength(byte[] data, int topicPosition) {
return BE.getuint16(data, topicPosition);
}
private String getString(byte[] data, int offset, int length) {
return new String(data, offset, length);
}
private short getPacketID(byte[] data, int idPosition) {
return (short)BE.getuint16(data, idPosition);
}
private void setPacketID(byte[] data, int offset, short id) {
BE.setuint16(data, offset, id);
}
private int getLengthBytes(int length) {
//does not include the header or length bytes itself
if (length <= 0x7f) return 1; //7 bits
if (length <= 0x3fff) return 2; //14 bits
if (length <= 0x1fffff) return 3; //21 bits
if (length <= 0xfffffff) return 4; //28 bits (max length)
return -1;
}
public static final byte CMD_CONNECT = 1;
public static final byte CMD_CONNECT_ACK = 2;
public static final byte CMD_PUBLISH = 3;
public static final byte CMD_PUBLISH_ACK = 4;
public static final byte CMD_PUBLISH_REC = 5;
public static final byte CMD_PUBLISH_REL = 6;
public static final byte CMD_PUBLISH_CMP = 7;
public static final byte CMD_SUBSCRIBE = 8;
public static final byte CMD_SUBSCRIBE_ACK = 9;
public static final byte CMD_UNSUBSCRIBE = 10;
public static final byte CMD_UNSUBSCRIBE_ACK = 11;
public static final byte CMD_PING = 12;
public static final byte CMD_PONG = 13;
public static final byte CMD_DISCONNECT = 14;
public static final byte CMD_AUTH = 15;
public static final byte RESERVED = 0;
public static final byte RESERVED_2 = 2;
public static final byte QOS_0 = 0; //fire and forget (no reply)
public static final byte QOS_1 = 1; //CMD_PUBLISH_ACK reply
public static final byte QOS_2 = 2; //CMD_PUBLISH_REC reply, CMD_PUBLISH_REL request, CMD_PUBLISH_CMP reply
public static final byte QOS_3 = 3; //not used
public static final byte FLAG_CLEAN_START = 2;
public static final byte FLAG_PASS = 0x40;
public static final byte FLAG_USER = (byte)0x80;
public static final byte CONNECT_SUCCESS = 0;
public static final byte CONNECT_NOT_AUTHORIZED = (byte)0x87;
public static Client[] ClientArrayType = new Client[0];
private class Client extends Thread {
public Socket s;
public InputStream is;
public OutputStream os;
public String ip;
public boolean client_active = true;
public String client_id;
public boolean auth;
public int ver = 4;
public int keep_alive;
public Client(Socket s) {
this.s = s;
}
public void run() {
try {
is = s.getInputStream();
os = s.getOutputStream();
ip = s.getInetAddress().getHostAddress();
if (debug) JFLog.log("connect:" + ip);
byte[] buf = new byte[bufsiz];
if (config.user == null || config.pass == null) {
auth = true;
}
while (server.active && client_active) {
int totalRead = 0;
int packetLength = -1; //excluding header + length fields
int totalLength = -1; //total packet length
int read;
Arrays.fill(buf, (byte)0);
while (server.active && client_active) {
if (packetLength == -1) {
read = is.read(buf, totalRead, 1);
} else {
read = is.read(buf, totalRead, totalLength - totalRead);
}
if (debug) JFLog.log("read=" + read);
if (read == -1) throw new Exception("bad read");
totalRead += read;
if (totalRead < 2) continue;
if (packetLength == -1) {
packetLength = getLength(buf, 1, totalRead);
if (packetLength != -1) {
totalLength = 1 + getLengthBytes(packetLength) + packetLength;
if (debug) JFLog.log("totalLength=" + totalLength);
}
}
if (packetLength == -1) continue;
if (totalRead < totalLength) continue;
try {
process(buf, totalLength, packetLength);
} catch (Exception e) {
JFLog.log(e);
}
break;
}
}
} catch (SocketException se) {
} catch (Exception e) {
JFLog.log(e);
}
unsubscribeAll(this);
if (debug) JFLog.log("disconnect:" + ip);
}
private void process(byte[] packet, int totalLength, int packetLength) throws Exception {
//totalLength = packet.length
//packetLength = length excluding header and packet length byte(s)
byte[] reply = null;
byte cmd = (byte)((packet[0] & 0xf0) >> 4);
short id = 0;
int pos = 1 + getLengthBytes(packetLength);
int topicLength;
String topic_name;
int msgLength;
String msg;
if (debug) JFLog.log("cmd=" + cmd);
switch (cmd) {
case CMD_CONNECT: {
//packet[pos + 0] = length_msb = 0x00
//packet[pos + 1] = length_msb = 0x04
//packet[pos + 2] thru [pos + 5] = "MQTT"
ver = packet[pos + 6]; //4 or 5
int flags = packet[pos + 7];
keep_alive = BE.getuint16(packet, pos + 8);
pos += 10;
if (ver == 5) {
int props_length = getLength(packet, pos, totalLength);
if (props_length == -1) throw new Exception("malformed packet");
int props_length_bytes = getLengthBytes(props_length);
pos += props_length_bytes;
if (props_length > 0) {
pos += props_length;
}
}
int client_id_length = BE.getuint16(packet, pos);
pos += 2;
client_id = getString(packet, pos, client_id_length);
if (debug) {
JFLog.log("client_id=" + client_id);
}
pos += client_id_length;
String user = null;
if ((flags & FLAG_USER) != 0) {
int user_length = BE.getuint16(packet, pos);
pos += 2;
user = getString(packet, pos, user_length);
pos += user_length;
}
String pass = null;
if ((flags & FLAG_PASS) != 0) {
int pass_length = BE.getuint16(packet, pos);
pos += 2;
pass = getString(packet, pos, pass_length);
pos += pass_length;
}
if (config.user != null && config.pass != null) {
//compare user/pass
if (user == null || !user.equals(config.user) || pass == null || !pass.equals(config.pass)) {
if (debug_auth) {
JFLog.log("auth failed:" + user + ":" + pass);
}
auth = false;
} else {
auth = true;
}
}
switch (ver) {
case 4: reply = new byte[4]; break;
case 5: reply = new byte[5]; break; //adds props
}
//reply = header , size , ack_flags, return_code=0, [props]
reply[0] = (byte)(CMD_CONNECT_ACK << 4);
setPacketLength(reply);
if (auth) {
reply[3] = CONNECT_SUCCESS;
} else {
reply[3] = CONNECT_NOT_AUTHORIZED;
send(reply);
reply = null;
os.flush();
disconnect();
break;
}
if (events != null) {
events.onConnect();
}
break;
}
case CMD_PUBLISH: {
if (!auth) {
disconnect();
break;
}
//header, size, topic, id, msg
boolean dup = (packet[0] & 0x08) != 0;
byte qos = (byte)((packet[0] & 0x06) >> 1);
boolean retain = (packet[0] & 0x01) != 0;
if (qos == QOS_3) throw new Exception("malformed packet");
topicLength = getStringLength(packet, pos);
pos += 2;
if (debug) JFLog.log("topic=" + pos + "/" + topicLength);
topic_name = getString(packet, pos, topicLength);
pos += topicLength;
if (qos > 0) {
id = getPacketID(packet, pos);
if (debug) JFLog.log("id=" + id);
pos += 2;
}
if (ver == 5) {
int props_length = getLength(packet, pos, totalLength);
if (props_length == -1) throw new Exception("malformed packet");
int props_length_bytes = getLengthBytes(props_length);
pos += props_length_bytes;
if (props_length > 0) {
pos += props_length;
}
}
msgLength = totalLength - pos;
if (debug) JFLog.log("msg=" + pos + "/" + msgLength);
msg = new String(packet, pos, msgLength);
if (debug_msg) JFLog.log("PUBLISH:" + ip + ":" + topic_name + ":" + msg);
Topic topic = getTopic(topic_name);
topic.publish(msg, retain);
switch (qos) {
case QOS_1: {
//CMD_PUBLISH_ACK
reply = new byte[4];
reply[0] = (byte)(CMD_PUBLISH_ACK << 4);
//reply = header , size , id_hi, id_lo
setPacketLength(reply);
setPacketID(reply, 2, id);
break;
}
case QOS_2: {
//CMD_PUBLISH_REC
reply = new byte[4];
reply[0] = (byte)(CMD_PUBLISH_REC << 4);
//reply = header , size , id_hi, id_lo
setPacketLength(reply);
setPacketID(reply, 2, id);
break;
}
}
if (events != null) {
events.onMessage(topic_name, msg);
}
if (forwarder != null) {
forwarder.publish(topic_name, msg);
}
break;
}
case CMD_PUBLISH_ACK:
if (!auth) {
disconnect();
break;
}
//??? should not get ???
break;
case CMD_PUBLISH_REC:
if (!auth) {
disconnect();
break;
}
//??? should not get ???
break;
case CMD_PUBLISH_REL:
if (!auth) {
disconnect();
break;
}
reply = new byte[4];
reply[0] = (byte)(CMD_PUBLISH_CMP << 4);
setPacketLength(reply);
id = getPacketID(packet, 2);
setPacketID(reply, 2, id);
break;
case CMD_PUBLISH_CMP:
if (!auth) {
disconnect();
break;
}
//???
break;
case CMD_SUBSCRIBE: {
if (!auth) {
disconnect();
break;
}
//cmd, size, id, [props], topic
id = getPacketID(packet, pos);
if (debug) JFLog.log("id=" + id);
pos += 2;
if (ver == 5) {
int props_length = getLength(packet, pos, totalLength);
int props_length_bytes = getLengthBytes(props_length);
pos += props_length_bytes;
if (props_length > 0) {
pos += props_length;
}
}
while (pos < totalLength) {
topicLength = getStringLength(packet, pos);
if (debug) JFLog.log("topic=" + pos + "/" + topicLength);
pos += 2;
topic_name = getString(packet, pos, topicLength);
pos += topicLength;
if (hasWildcard(topic_name)) {
Topic[] topics = getTopics(topic_name);
for(Topic topic : topics) {
topic.subscribe(this);
}
} else {
Topic topic = getTopic(topic_name);
topic.subscribe(this);
}
if (debug_msg) JFLog.log("SUBSCRIBE:" + ip + ":" + topic_name);
if (events != null) {
events.onSubscribe(topic_name);
}
pos++; //subscribe options (QOS)
}
reply = new byte[5];
//header , size , id_hi, id_lo, return_code=0
reply[0] = (byte)(CMD_SUBSCRIBE_ACK << 4);
setPacketLength(reply);
setPacketID(reply, 2, id);
break;
}
case CMD_UNSUBSCRIBE: {
if (!auth) {
disconnect();
break;
}
//cmd, size, id, topic
id = getPacketID(packet, pos);
if (debug) JFLog.log("id=" + id);
pos += 2;
if (ver == 5) {
int props_length = getLength(packet, pos, totalLength);
if (props_length == -1) throw new Exception("malformed packet");
int props_length_bytes = getLengthBytes(props_length);
pos += props_length_bytes;
if (props_length > 0) {
pos += props_length;
}
}
while (pos < totalLength) {
topicLength = getStringLength(packet, pos);
pos += 2;
if (debug) JFLog.log("topic=" + pos + "/" + topicLength);
topic_name = getString(packet, pos, topicLength);
pos += topicLength;
Topic topic = getTopic(topic_name);
topic.unsubscribe(this);
if (debug_msg) JFLog.log("UNSUB:" + ip + ":" + topic_name);
}
reply = new byte[4];
reply[0] = (byte)(CMD_UNSUBSCRIBE_ACK << 4);
//header , size , id_hi, id_lo
setPacketLength(reply);
setPacketID(reply, 2, id);
break;
}
case CMD_PING:
if (!auth) {
disconnect();
break;
}
reply = new byte[2];
reply[0] = (byte)(CMD_PONG << 4);
// setPacketLength(reply); //zero
if (debug_msg) JFLog.log("PING:" + ip);
break;
case CMD_DISCONNECT:
disconnect();
break;
}
if (reply != null) {
send(reply);
}
}
private void send(byte[] reply) throws Exception {
os.write(reply);
}
private short id = 0x0001;
public void publish(String topic, String msg, byte qos) throws Exception {
//each client could require different publish based on version (4 or 5)
byte[] topic_bytes = topic.getBytes();
int topic_length = topic_bytes.length;
byte[] msg_bytes = msg.getBytes();
int msg_length = msg_bytes.length;
int length = calcPacketLength(qos > 0, topic_length, false, msg_length);
int length_bytes = getLengthBytes(length);
byte[] packet = new byte[1 + length_bytes + length];
packet[0] = (byte)((CMD_PUBLISH << 4) | qos << 1);
setPacketLength(packet, length_bytes);
int pos = 1 + length_bytes;
setStringLength(packet, pos, (short)topic_length);
pos += 2;
System.arraycopy(topic_bytes, 0, packet, pos, topic_length);
pos += topic_length;
if (qos > 0) {
setPacketID(packet, pos, id++);
if (id == 0x7fff) {
id = 1;
}
pos += 2;
}
if (ver == 5) {
pos++; //properties length
}
System.arraycopy(msg_bytes, 0, packet, pos, msg_length);
pos += msg_length;
if (debug) {
JFLog.log("publish:" + topic + "=" + msg);
}
try {
os.write(packet);
} catch (Exception e) {
disconnect();
JFLog.log(e);
}
}
private void disconnect() {
unsubscribeAll(this);
client_active = false;
if (s != null) {
try {s.close();} catch (Exception e) {}
s = null;
}
}
private int calcPacketLength(boolean has_id, int topic_length, boolean has_opts, int msg_length) {
//does NOT include the header or length itself
int length = 0;
if (has_id) {
length += 2; //id
}
if (topic_length > 0) {
length += 2; //short length;
length += topic_length;
}
if (has_opts) {
length++; //sub : topic options
}
if (ver == 5) {
length++; //properties
}
if (msg_length > 0) {
length += msg_length;
}
return length;
}
private void setPacketLength(byte[] packet) {
int value = packet.length - 2;
int pos = 1;
byte ebyte;
do {
ebyte = (byte)(value % 0x80);
value /= 0x80;
if (value > 0) {
ebyte |= 0x80;
}
packet[pos++] = ebyte;
} while (value > 0);
}
private void setPacketLength(byte[] packet, int length_bytes) {
int value = packet.length - 1 - length_bytes;
int pos = 1;
byte ebyte;
do {
ebyte = (byte)(value % 0x80);
value /= 0x80;
if (value > 0) {
ebyte |= 0x80;
}
packet[pos++] = ebyte;
} while (value > 0);
}
private void setStringLength(byte[] data, int offset, short length) {
BE.setuint16(data, offset, length);
}
}
public static boolean createKeys() {
return KeyMgmt.keytool(new String[] {
"-genkey", "-debug", "-alias", "jfmqtt", "-keypass", "password", "-storepass", "password",
"-keystore", getKeyFile(), "-validity", "3650", "-dname", "CN=jfmqtt.sourceforge.net, OU=user, O=server, C=CA",
"-keyalg" , "RSA", "-keysize", "2048"
});
}
public static class JBusMethods {
public String getConfig() {
byte[] cfg = JF.readFile(getConfigFile());
if (cfg == null) cfg = new byte[0];
return new String(cfg);
}
public boolean setConfig(String cfg) {
//write new file
try {
FileOutputStream fos = new FileOutputStream(getConfigFile());
fos.write(cfg.getBytes());
fos.close();
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public boolean restart() {
service.stop();
service = new MQTTServer();
service.start();
return true;
}
public boolean genKeys() {
if (createKeys()) {
JFLog.log("Generated Keys");
return true;
} else {
return false;
}
}
public String getLogFile() {
return MQTTServer.getLogFile();
}
}
}