-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDBus.java
More file actions
2118 lines (2039 loc) · 62.3 KB
/
Copy pathDBus.java
File metadata and controls
2118 lines (2039 loc) · 62.3 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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaforce.ipc;
import java.util.*;
import javaforce.*;
import javaforce.ipc.transport.*;
/** DBus IPC/RPC implementation.
*
* Supported:
* - Linux:Unix Sockets
* - Windows:Pipes
* - invoking remote method and returning value
* - signals (broadcasting method to all subscribed clients)
*
* Supported Data Types:
* - byte
* - short
* - int
* - long
* - double
* - boolean
* - String
* - javaforce.UShort
* - javaforce.UInteger
* - javaforce.ULong
* - dictionary entry (JFTuple<String,Object>)
* - struct (JFArray)
* - variant (JFVariant)
* - byte[]
* - short[]
* - int[]
* - long[]
* - double[]
* - boolean[]
* - String[]
* - javaforce.UShort[]
* - javaforce.UInteger[]
* - javaforce.ULong[]
* - array of dictionary entries (JFDictionary)
* - array of struct (JFArray[])
* - array of variant (JFVariant[])
*
* Notes:
* - sender field required to send back RPC reply
* - although technically not required for DBus on Linux
* it is required for the Windows Pipes implementation
* - methods must return a value (void is not supported)
* - return boolean at the least and always return true
* so caller knows call was successful
* - on Linux a dbus conf is installed to allow end points to
* use names that begin with "javaforce." (root only)
* Clients should just use a system supplied bus name
* by returning null from EndPoint.getEndPointName();
* Use getBusName() to determine supplied name after connect().
* - there is no way to implement a member with unsigned types
* since Java does not have primitive unsigned data types
* they are provided only for outbound calls to native methods that use them
* such as org.freedesktop.DBus.RequestName(String, uint32)
* - dictionary, struct and variant types are only supported in return data from an invoke()
*
* @author pquiring
*/
public class DBus implements IPC {
public static boolean debug = false;
public static boolean debug_msg = false;
public static boolean debug_reading = false;
//common interfaces
public static final String DBUS_MESSAGE_BUS = "org.freedesktop.DBus";
public static final String DBUS_PEER = "org.freedesktop.DBus.Peer";
//Ping()
//GetMachineId()
public static final String DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
//Get()
//Set()
//GetAll()
public static final String DBUS_INTROSPECTABLE = "org.freedesktop.DBus.Introspectable";
//Introspect()
public static final String DBUS_OBJECT_MANAGER = "org.freedesktop.DBus.ObjectManager";
//GetManagedObjects()
//InterfacesAdded()
//InterfacesRemoved()
private static final long max_packet_size = 32 * JF.MB;
private static class Field {
public byte type;
public String sign;
public Object value; //variant
public int idx = -1; //value index in rpkt
public String toString() {
return field_names[type] + "=" + value;
}
}
private static class Index {
public int idx;
}
private static class Type extends Index {
public int length;
public Type clone() {
Type c = new Type();
c.idx = idx;
c.length = length;
return c;
}
}
//Message Types
private static final byte MSG_CALL = 0x01;
private static final byte MSG_RETURN = 0x02;
private static final byte MSG_ERROR = 0x03;
private static final byte MSG_SIGNAL = 0x04;
private static final String[] msg_names = {
"null",
"call",
"return",
"error",
"signal"
};
// Field Types CALL RETURN ERROR SIGNAL Example (R=required O=optional)
private static final byte FIELD_PATH = 0x01; // R R /path/to/object
private static final byte FIELD_INTERFACE = 0x02; // O R com.example.InterfaceName
private static final byte FIELD_MEMBER = 0x03; // R R MethodName
private static final byte FIELD_ERROR_MSG = 0x04; // R Error msg
private static final byte FIELD_REPLY_SERIAL = 0x05; // R R Serial # from method_call
//6-9 = optional
private static final byte FIELD_DEST = 0x06; // Unique dest ID (message bus)
private static final byte FIELD_SENDER = 0x07; // Unique sender ID (message bus)
private static final byte FIELD_SIGNATURE = 0x08; // O O O If omitted assumes body.length = 0 (no args)
private static final byte FIELD_FD = 0x09; // # of fd (not used)
private static final String[] field_names = {
"NULL",
"PATH",
"INTERFACE",
"MEMBER",
"ERROR",
"REPLY_SERIAL",
"DEST",
"SENDER",
"SIGNATURE",
"FD"
};
//Data Types
public static final String TYPE_UINT8 = "y";
public static final String TYPE_INT16 = "n";
public static final String TYPE_UINT16 = "q";
public static final String TYPE_INT32 = "i";
public static final String TYPE_UINT32 = "u";
public static final String TYPE_INT64 = "x";
public static final String TYPE_UINT64 = "t";
public static final String TYPE_DOUBLE = "d";
public static final String TYPE_BOOLEAN = "b";
public static final String TYPE_STRING = "s";
public static final String TYPE_ARRAY = "a";
public static final String TYPE_STRUCT = "r";
public static final String TYPE_STRUCT_OPEN = "(";
public static final String TYPE_STRUCT_CLOSE = ")";
public static final String TYPE_DICT = "e"; //illegal : must use TYPE_ARRAY_DICT
public static final String TYPE_DICT_OPEN = "{";
public static final String TYPE_DICT_CLOSE = "}";
public static final String TYPE_VARIANT = "v";
public static final String TYPE_OBJECT_PATH = "o";
public static final String TYPE_SIGNATURE = "g";
public static final String TYPE_FD = "h";
public static final String TYPE_ARRAY_UINT8 = "ay";
public static final String TYPE_ARRAY_INT16 = "an";
public static final String TYPE_ARRAY_UINT16 = "aq";
public static final String TYPE_ARRAY_INT32 = "ai";
public static final String TYPE_ARRAY_UINT32 = "au";
public static final String TYPE_ARRAY_INT64 = "ax";
public static final String TYPE_ARRAY_UINT64 = "at";
public static final String TYPE_ARRAY_DOUBLE = "ad";
public static final String TYPE_ARRAY_BOOLEAN = "ab";
public static final String TYPE_ARRAY_STRING = "as";
public static final String TYPE_ARRAY_DICT = "ae";
public static final String TYPE_ARRAY_STRUCT = "ar";
public static final String TYPE_ARRAY_VARIANT = "av";
public static final String TYPE_ARRAY_OBJECT_PATH = "ao";
public static final byte TYPE__UINT8 = 'y';
public static final byte TYPE__INT16 = 'n';
public static final byte TYPE__UINT16 = 'q';
public static final byte TYPE__INT32 = 'i';
public static final byte TYPE__UINT32 = 'u';
public static final byte TYPE__INT64 = 'x';
public static final byte TYPE__UINT64 = 't';
public static final byte TYPE__DOUBLE = 'd';
public static final byte TYPE__BOOLEAN = 'b';
public static final byte TYPE__byte = 's';
public static final byte TYPE__ARRAY = 'a';
public static final byte TYPE__STRUCT = 'r';
public static final byte TYPE__STRUCT_OPEN = '(';
public static final byte TYPE__STRUCT_CLOSE = ')';
public static final byte TYPE__DICT = 'e'; //illegal : must use TYPE__ARRAY_DICT
public static final byte TYPE__DICT_OPEN = '{';
public static final byte TYPE__DICT_CLOSE = '}';
public static final byte TYPE__VARIANT = 'v';
public static final byte TYPE__OBJECT_PATH = 'o';
public static final byte TYPE__SIGNATURE = 'g';
public static final byte TYPE__FD = 'h';
/** Returns DBus data type of obj. */
public static String getObjectType(Object obj) {
//float32 is not supported ???
if (obj instanceof Byte) {
return TYPE_UINT8;
} else if (obj instanceof Short) {
return TYPE_INT16;
} else if (obj instanceof UShort) {
return TYPE_UINT16;
} else if (obj instanceof Integer) {
return TYPE_INT32;
} else if (obj instanceof UInteger) {
return TYPE_UINT32;
} else if (obj instanceof Double) {
return TYPE_DOUBLE;
} else if (obj instanceof Boolean) {
return TYPE_BOOLEAN;
} else if (obj instanceof String) {
return TYPE_STRING;
} else if (obj instanceof JFObjectPath) {
return TYPE_OBJECT_PATH;
} else if (obj instanceof JFVariant) {
return TYPE_VARIANT;
} else if (obj instanceof JFTuple) {
return TYPE_DICT;
} else if (obj instanceof JFArray) {
return TYPE_STRUCT;
} else if (obj instanceof byte[]) {
return TYPE_ARRAY_UINT8;
} else if (obj instanceof short[]) {
return TYPE_ARRAY_INT16;
} else if (obj instanceof UShort[]) {
return TYPE_ARRAY_UINT16;
} else if (obj instanceof int[]) {
return TYPE_ARRAY_INT32;
} else if (obj instanceof UInteger[]) {
return TYPE_ARRAY_UINT32;
} else if (obj instanceof long[]) {
return TYPE_ARRAY_INT64;
} else if (obj instanceof ULong[]) {
return TYPE_ARRAY_UINT64;
} else if (obj instanceof double[]) {
return TYPE_ARRAY_DOUBLE;
} else if (obj instanceof boolean[]) {
return TYPE_ARRAY_BOOLEAN;
} else if (obj instanceof String[]) {
return TYPE_ARRAY_STRING;
} else if (obj instanceof JFObjectPath[]) {
return TYPE_ARRAY_OBJECT_PATH;
} else if (obj instanceof JFDictionary) {
return TYPE_ARRAY_DICT;
} else if (obj instanceof JFArray[]) {
return TYPE_ARRAY_STRUCT;
} else if (obj instanceof JFVariant[]) {
return TYPE_ARRAY_VARIANT;
} else {
JFLog.log("DBus:Error:Unknown type:" + obj.getClass());
return "-";
}
}
private String getClassType(Class<?> cls) {
if (cls == byte.class) {
return TYPE_UINT8;
}
if (cls == short.class) {
return TYPE_INT16;
}
if (cls == UShort.class) {
return TYPE_UINT16;
}
if (cls == int.class) {
return TYPE_INT32;
}
if (cls == UInteger.class) {
return TYPE_UINT32;
}
if (cls == long.class) {
return TYPE_INT64;
}
if (cls == ULong.class) {
return TYPE_UINT64;
}
if (cls == double.class) {
return TYPE_DOUBLE;
}
if (cls == boolean.class) {
return TYPE_BOOLEAN;
}
if (cls == String.class) {
return TYPE_STRING;
}
if (cls == JFObjectPath.class) {
return TYPE_OBJECT_PATH;
}
if (cls == JFVariant.class) {
return TYPE_VARIANT;
}
if (cls == JFTuple.class) {
return TYPE_DICT;
}
if (cls == JFArray.class) {
return TYPE_STRUCT;
}
if (cls == String[].class) {
return TYPE_ARRAY_STRING;
}
if (cls == JFObjectPath[].class) {
return TYPE_ARRAY_OBJECT_PATH;
}
if (cls == JFVariant[].class) {
return TYPE_ARRAY_VARIANT;
}
if (cls == JFDictionary.class) {
return TYPE_ARRAY_DICT;
}
if (cls == JFArray[].class) {
return TYPE_ARRAY_STRUCT;
}
JFLog.log("DBus.getClassType() : Unknown Class : " + cls);
return null;
}
private Class<?> getType(String sign) {
switch (sign) {
case TYPE_UINT8:
return Byte.class;
case TYPE_INT16:
return Short.class;
case TYPE_UINT16:
return UShort.class;
case TYPE_INT32:
return Integer.class;
case TYPE_UINT32:
return UInteger.class;
case TYPE_INT64:
return Long.class;
case TYPE_UINT64:
return ULong.class;
case TYPE_DOUBLE:
return Double.class;
case TYPE_BOOLEAN:
return Boolean.class;
case TYPE_STRING:
return String.class;
case TYPE_OBJECT_PATH:
return JFObjectPath.class;
case TYPE_VARIANT:
return JFVariant.class;
case TYPE_DICT:
return JFTuple.class;
case TYPE_STRUCT:
return JFArray.class;
case TYPE_ARRAY_UINT8:
return byte[].class;
case TYPE_ARRAY_INT16:
return short[].class;
case TYPE_ARRAY_UINT16:
return UShort[].class;
case TYPE_ARRAY_INT32:
return int[].class;
case TYPE_ARRAY_UINT32:
return UInteger[].class;
case TYPE_ARRAY_INT64:
return long[].class;
case TYPE_ARRAY_UINT64:
return ULong[].class;
case TYPE_ARRAY_DOUBLE:
return double[].class;
case TYPE_ARRAY_STRING:
return String[].class;
case TYPE_ARRAY_OBJECT_PATH:
return JFObjectPath[].class;
case TYPE_ARRAY_VARIANT:
return JFVariant[].class;
case TYPE_ARRAY_DICT:
return JFDictionary.class;
case TYPE_ARRAY_STRUCT:
return JFArray[].class;
}
JFLog.log("DBus:Error:Type unknown:" + sign);
return null;
}
private static int tcp_port = -1;
private EndPoint ep;
private Reader reader;
private DBusTransport transport;
private int timeout = 30 * 1000;
private int serial = 1;
private Object serial_lock = new Object();
private ThreadQueue queue;
private Options options;
/** Options provides increase control of DBus operations. */
public static class Options {
/** Transport to use (null = create default for OS) */
public DBusTransport transport = null;
/** min threads for processing inbound calls queue. Default = 1 */
public int minThreads = 1;
/** max threads for processing inbound calls queue. Default = 16 */
public int maxThreads = 16;
/** idle time before an idle thread is closed (if # threads is more than minThreads). Default = 60 */
public int idleSeconds = 60;
}
/** Create DBus with specified EndPoint. */
public DBus(EndPoint ep) {
this.ep = ep;
this.transport = createTransport();
this.options = new Options();
}
/** Create DBus with specified EndPoint and transport. */
public DBus(EndPoint ep, DBusTransport transport) {
this.ep = ep;
this.transport = transport;
this.options = new Options();
}
/** Create DBus with specified EndPoint and more detailed options. */
public DBus(EndPoint ep, Options options) {
this.ep = ep;
if (options.transport == null) {
this.transport = createTransport();
} else {
this.transport = options.transport;
}
this.options = options;
}
/** Create transport suitable for OS. */
public static DBusTransport createTransport() {
if (JF.isUnix()) {
if (JF.isMac() && tcp_port != -1) {
return new TCPTransport(tcp_port);
}
return new UnixSocketTransport();
} else {
//Windows Pipes requires native support
if (!JF.hasNativeSupport() && tcp_port != -1) {
return new TCPTransport(tcp_port);
}
return new WinPipeTransport();
}
}
/** Create a client end point with system provided name.
* @param obj = where RPC methods reside
*/
public static EndPoint createEndPoint(Object obj) {
Dispatcher dispatcher = new Dispatcher(obj);
EndPoint ep = new EndPoint() {
public String getEndPointName() {
return null;
}
public Object dispatch(String method, Object[] args) throws Exception {
return dispatcher.dispatch(method, args);
}
};
return ep;
}
/** Create a EndPoint with specified name (servers).
* @param name = name of end point
* @param obj = where RPC methods reside
*/
public static EndPoint createEndPoint(String name, Object obj) {
Dispatcher dispatcher = new Dispatcher(obj);
EndPoint ep = new EndPoint() {
public String getEndPointName() {
return name;
}
public Object dispatch(String method, Object[] args) throws Exception {
return dispatcher.dispatch(method, args);
}
};
return ep;
}
/** Convert a message bus name to object path.
* org.freedesktop.DBus becomes /org/freedesktop/DBus
*/
public static String nameToPath(String name) {
return "/" + name.replaceAll("[.]", "/");
}
/** Set TCP port for systems that do not fully support DBus.
*
* Examples:
* - Windows without native Windows Pipes support.
* - MacOS
*/
public static void setTCPTransportPort(int port) {
tcp_port = port;
}
/** Connects to message bus. */
public boolean connect() {
String busname = ep.getEndPointName();
if (debug) JFLog.log("DBus:busName=" + busname);
if (queue != null) {
queue.close();
queue = null;
}
queue = new ThreadQueue(options.minThreads, options.maxThreads, options.idleSeconds);
return transport.connect(busname, this, new Runnable() {
public void run() {
reader = new Reader();
reader.start();
}
});
}
/** Disconnects from message bus. */
public boolean disconnect() {
boolean result = false;
try {
result = transport.disconnect(); //this should cause reader to abort
if (Thread.currentThread() != reader) {
reader.join();
}
reader = null;
if (queue != null) {
queue.close();
queue = null;
}
return result;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Set timeout for reply. Default = 30 seconds.
* @param ms = timeout (min = 3000, max = 60000)
*/
public void setTimeout(int ms) {
if (ms < 3000) ms = 3000;
if (ms > 60 * 1000) ms = 60 * 1000;
timeout = ms;
}
/** Returns bus name requested or assigned. */
public String getBusName() {
return transport.getBusName();
}
/** Return next serial # for unique msg. */
private int nextSerial() {
int value;
synchronized(serial_lock) {
value = serial++;
if (serial == Integer.MAX_VALUE) {
serial = 1;
}
}
return value;
}
private static class Invoke {
public int serial; //standard message identifier
public String return_signal; //return signal
public boolean nothing;
public Object value; //return value
public Object error; //return error
public Object lock = new Object(); //timeout/notify lock
}
private Object invokes_lock = new Object();
private ArrayList<Invoke> invokes = new ArrayList<>();
/** Invokes method in remote end point.
* @param dest = dot destination end point on bus
* @param path = slash path within service
* @param iface = dot interface name (optional if unique)
* @param method = method to invoke
* @param args = arguments
* @return return value from remote method
* @exception Exception thrown if method returned at error message or no reply within timeout duration
* Errors could be method not found, mismatch arguments, etc.
*/
public Object invoke(String dest, String path, String iface, String method, Object... args) throws Exception {
if (debug) JFLog.log("DBus.invoke:" + dest + "." + method);
Invoke invoke = new Invoke();
invoke.serial = nextSerial();
if (dest.equals(DBUS_MESSAGE_BUS)) {
if (method.equals("RequestName")) {
invoke.return_signal = "NameAcquired";
}
}
synchronized (invoke.lock) {
synchronized (invokes_lock) {
write_msg(MSG_CALL, dest, path, iface, invoke.serial, -1, method, args);
invokes.add(invoke);
}
try {
invoke.lock.wait(timeout);
} catch (Exception e) {
JFLog.log(e);
}
}
synchronized (invokes_lock) {
invokes.remove(invoke);
}
if (invoke.error != null) {
throw new Exception((String)invoke.error);
} else {
if (invoke.nothing) {
return null;
}
if (invoke.value == null) {
throw new Exception("DBus.timeout");
}
return invoke.value;
}
}
/** Invokes method in remote end point.
* @param dest = dot destination end point on bus
* @param method = method to invoke
* @param args = arguments
* @return return value from remote method
* @exception Exception thrown if method returned at error message or no reply within timeout duration
* Errors could be method not found, mismatch arguments, etc.
* Path and iface are inferred from dest.
*/
public Object invoke(String dest, String method, Object... args) throws Exception {
boolean dest_generic = dest.startsWith(":");
String iface = dest_generic ? "javaforce.endpoint" : dest;
String path = nameToPath(iface);
return invoke(dest, path, iface, method, args);
}
/** Invokes a method in all bus members that have subscribed to the method.
*
* @see subscribe
* @see unsubscribe
*/
public boolean signal(String path, String iface, String method, Object... args) {
try {
write_msg(MSG_SIGNAL, DBUS_MESSAGE_BUS, path, iface, nextSerial(), -1, method, args);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Subscribe to a signal from another client.
* @param rule = dbus rule (command separated list of key/value pairs)
* type='signal'
* sender='javaforce.originator'
* path='/javaforce/originator'
* interface='javaforce.originator'
* member='method_name'
* destination='javaforce.recipient'
* arg0,...='string_value'
* Typical rule = "type='signal', interface='javaforce.originator', member='Event'"
*/
public boolean subscribe(String rule) {
try {
invoke(DBUS_MESSAGE_BUS, "AddMatch", rule);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Unsubscribe to a signal from another client. */
public boolean unsubscribe(String rule) {
try {
invoke(DBUS_MESSAGE_BUS, "RemoveMatch", rule);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
private Object write_lock = new Object();
private boolean write(String path, byte[] data, int offset, int length) {
if (debug) {
JFLog.log("DBus.write.length=" + length);
JFLog.log("packet=", data, offset, length);
}
synchronized (write_lock) {
return transport.write(path, data, offset, length);
}
}
private int read(byte[] data, int offset, int length) {
return transport.read(data, offset, length);
}
int bodyLength;
/** Align buffer position to data type size. */
private void balign(int size) {
int align = bodyLength & (size-1);
if (align == 0) return;
int pad = size - align;
bodyLength += pad;
}
/** Generates method signature. */
private String gen_sign(Object[] args) {
int argsLength = args.length;
StringBuilder sign = new StringBuilder();
for (int a = 0; a < argsLength; a++) {
String dt = getObjectType(args[a]);
switch (dt) {
case TYPE_STRUCT: {
JFArray arr = (JFArray)args[a];
Object[] objs = arr.toArray();
sign.append("(");
sign.append(objs);
sign.append(")");
break;
}
case TYPE_DICT: {
JFTuple tuple = (JFTuple)args[a];
sign.append("{");
sign.append(getClassType(tuple.key_type));
sign.append(getClassType(tuple.value_type));
sign.append("}");
break;
}
case TYPE_ARRAY_DICT: {
JFDictionary map = (JFDictionary)args[a];
sign.append("a");
sign.append("{");
sign.append(getClassType(map.key_type));
sign.append(getClassType(map.value_type));
sign.append("}");
break;
}
case TYPE_ARRAY_STRUCT: {
JFTuple[] tuples = (JFTuple[])args[a];
sign.append("a");
sign.append(gen_sign(new Object[] {tuples[0]}));
break;
}
default:
sign.append(dt);
break;
}
}
return sign.toString();
}
//write buffer
private byte[] wpkt = new byte[1024];
private int wpos;
/** Align write buffer position to data type size. */
private void walign(int size) {
int align = wpos & (size-1);
if (align == 0) return;
int pad = size - align;
for(int i=0;i<pad;i++) {
wpkt[wpos++] = 0;
}
}
/** Expand write buffer as needed. */
private void wcheck(int size) throws Exception {
while (wpos + size > wpkt.length) {
int cur_len = wpkt.length;
byte[] new_wpkt = new byte[cur_len << 1];
System.arraycopy(wpkt, 0, new_wpkt, 0, cur_len);
wpkt = new_wpkt;
}
}
private void write_byte(byte value) throws Exception {
//no alignment check
wcheck(1);
wpkt[wpos++] = value;
}
private void write_byte(char value) throws Exception {
write_byte((byte)value);
}
private void write_short(short value) throws Exception {
walign(2);
wcheck(2);
LE.setuint16(wpkt, wpos, value);
wpos += 2;
}
private void write_ushort(UShort value) throws Exception {
walign(2);
wcheck(2);
LE.setuint16(wpkt, wpos, value.getValue());
wpos += 2;
}
private void write_int(int value) throws Exception {
walign(4);
wcheck(4);
LE.setuint32(wpkt, wpos, value);
wpos += 4;
}
private void write_uint(UInteger value) throws Exception {
walign(4);
wcheck(4);
LE.setuint32(wpkt, wpos, value.getValue());
wpos += 4;
}
private void write_long(long value) throws Exception {
walign(8);
wcheck(8);
LE.setuint64(wpkt, wpos, value);
wpos += 8;
}
private void write_ulong(ULong value) throws Exception {
walign(8);
wcheck(8);
LE.setuint64(wpkt, wpos, value.getValue());
wpos += 8;
}
private void write_double(double value) throws Exception {
walign(8);
wcheck(8);
LE.setdouble(wpkt, wpos, value);
wpos += 8;
}
private void write_boolean(boolean value) throws Exception {
walign(4);
wcheck(4);
LE.setuint32(wpkt, wpos, value ? 1 : 0);
wpos += 4;
}
private void write_String(String value) throws Exception {
int strlen = value.length();
write_int(strlen);
wcheck(strlen + 1);
System.arraycopy(value.getBytes(), 0, wpkt, wpos, strlen);
wpos += strlen;
wpkt[wpos++] = 0; //null
}
private void write_variant(JFVariant value) throws Exception {
String dt = getObjectType(value.value);
write_sign(dt);
write_type(value.value);
}
private void write_dict(JFTuple value) throws Exception {
walign(8);
write_type(value.key);
write_type(value.value);
}
private void write_struct(JFArray value) throws Exception {
walign(8);
Object[] arr = value.toArray();
for(Object obj : arr) {
write_type(obj);
}
}
private void write_array_byte(byte[] value) throws Exception {
write_int(value.length);
for(byte b : value) {
write_byte(b);
}
}
private void write_array_short(short[] value) throws Exception {
write_int(value.length * 2);
for(short b : value) {
write_short(b);
}
}
private void write_array_ushort(UShort[] value) throws Exception {
write_int(value.length * 2);
for(UShort b : value) {
write_ushort(b);
}
}
private void write_array_int(int[] value) throws Exception {
write_int(value.length * 4);
for(int b : value) {
write_int(b);
}
}
private void write_array_uint(UInteger[] value) throws Exception {
write_int(value.length * 4);
for(UInteger b : value) {
write_uint(b);
}
}
private void write_array_long(long[] value) throws Exception {
write_int(value.length * 8);
for(long b : value) {
write_long(b);
}
}
private void write_array_ulong(ULong[] value) throws Exception {
write_int(value.length * 8);
for(ULong b : value) {
write_ulong(b);
}
}
private void write_array_double(double[] value) throws Exception {
write_int(value.length * 8);
for(double b : value) {
write_double(b);
}
}
private void write_array_boolean(boolean[] value) throws Exception {
write_int(value.length * 4);
for(boolean b : value) {
write_boolean(b);
}
}
private void write_array_String(String[] value) throws Exception {
int len = 0;
for(String b : value) {
{
//align int
int align = len & 3;
if (align > 0) {
int pad = 4 - align;
len += pad;
}
}
len += 4; //String length
len += b.length(); //String
len++; //null
}
write_int(len);
for(String b : value) {
write_String(b);
}
}
@SuppressWarnings("unchecked")
private void write_array_dict(JFDictionary value) throws Exception {
write_int(-1); //array size (patch later)
int array_offset = wpos - 4;
walign(8);
int array_start = wpos;
Object[] keys = value.map.keySet().toArray(new String[0]);
JFTuple tuple = new JFTuple(value.key_type, value.value_type);
for(Object key : keys) {
tuple.key = key;
tuple.value = value.map.get(key);
write_dict(tuple);
}
//patch array size
int array_size = wpos - array_start;
LE.setuint32(wpkt, array_offset, array_size);
}
private void write_array_struct(JFArray[] value) throws Exception {
write_int(-1); //array size (patch later)
int array_offset = wpos - 4;
walign(8);
int array_start = wpos;
for(JFArray arr : value) {
write_struct(arr);
}
//patch array size
int array_size = wpos - array_start;
LE.setuint32(wpkt, array_offset, array_size);
}
private void write_array_variant(JFVariant[] value) throws Exception {
write_int(-1); //array size (patch later)
int array_offset = wpos - 4;
//NOTE : Variants start with sign which has 1 byte alignment
int array_start = wpos;
for(JFVariant arr : value) {
write_variant(arr);
}
//patch array size
int array_size = wpos - array_start;
LE.setuint32(wpkt, array_offset, array_size);
}
private void write_sign(char value) throws Exception {
write_byte((byte)1);
wcheck(1 + 1);
wpkt[wpos++] = (byte)value;
wpkt[wpos++] = 0; //null
}