Repository navigation
Expand file tree
/
Copy pathPubnubDemoConsole.java
More file actions
965 lines (819 loc) · 35.1 KB
/
Copy pathPubnubDemoConsole.java
File metadata and controls
965 lines (819 loc) · 35.1 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
package com.pubnub.examples;
import com.pubnub.api.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Hashtable;
import java.util.Scanner;
import static java.lang.System.out;
public class PubnubDemoConsole {
Pubnub pubnub;
String publish_key = "demo";
String subscribe_key = "demo";
String secret_key = "";
String cipher_key = "";
boolean SSL;
Scanner reader;
public PubnubDemoConsole(String publish_key, String subscribe_key, String secret_key,
String cipher_key) {
this.publish_key = publish_key;
this.subscribe_key = subscribe_key;
this.secret_key = secret_key;
this.cipher_key = cipher_key;
}
public PubnubDemoConsole() {
}
private void notifyUser(Object message) {
out.println(message.toString());
}
private void publish(String channel, boolean store) {
notifyUser("Enter the message for publish. To exit loop enter QUIT");
String message = "";
Callback cb = new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("PUBLISH : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("PUBLISH : " + error);
}
};
while (true) {
Hashtable args = new Hashtable(2);
message = reader.nextLine();
if (message.equalsIgnoreCase("QUIT")) {
break;
}
try {
Integer i = Integer.parseInt(message);
pubnub.publish(channel, i, store, cb);
continue;
} catch (Exception e) {
}
try {
Double d = Double.parseDouble(message);
pubnub.publish(channel, d, store, cb);
continue;
} catch (Exception e) {
}
try {
JSONArray js = new JSONArray(message);
pubnub.publish(channel, js, store, cb);
continue;
} catch (Exception e) {
}
try {
JSONObject js = new JSONObject(message);
pubnub.publish(channel, js, store, cb);
continue;
} catch (Exception e) {
}
pubnub.publish(channel, message, store, cb);
}
}
private void subscribe(final String channel) {
try {
pubnub.subscribe(channel, new Callback() {
@Override
public void connectCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : CONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void disconnectCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : DISCONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
public void reconnectCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : RECONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void successCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : " + channel + " : "
+ message.getClass() + " : " + message.toString());
}
@Override
public void successCallback(String channel, Object message, String timetoken) {
notifyUser("SUBSCRIBE : [TT - " + timetoken + "] " + channel + " : "
+ message.getClass() + " : " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
/*
# Switch on error code, see PubnubError.java
if (error.errorCode == 112) {
# Bad Auth Key!
unsubscribe, get a new auth key, subscribe, etc...
} else if (error.errorCode == 113) {
# Need to set Auth Key !
unsubscribe, set auth, resubscribe
}
*/
notifyUser("SUBSCRIBE : ERROR on channel " + channel
+ " : " + error.toString());
if (error.errorCode == PubnubError.PNERR_TIMEOUT)
pubnub.disconnectAndResubscribe();
}
});
} catch (Exception e) {
}
}
private void presence(String channel) {
try {
pubnub.presence(channel, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("PRESENCE : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("PRESENCE : " + error);
}
});
} catch (PubnubException e) {
}
}
private void history(String channel, int count, boolean includeToken) {
pubnub.history(channel, includeToken, count, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("HISTORY : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("HISTORY : " + error);
}
});
}
private void hereNow(String channel) {
boolean metadata = getBooleanFromConsole("Metadata");
boolean uuids = getBooleanFromConsole("Return UUIDs");
pubnub.hereNow(channel, metadata, uuids, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("HERE NOW : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("HERE NOW : " + error);
}
});
}
private void unsubscribe(String channel) {
pubnub.unsubscribe(channel);
}
private void unsubscribeFromGroup(String groupName) {
pubnub.channelGroupUnsubscribe(groupName);
}
private void unsubscribePresence(String channel) {
pubnub.unsubscribePresence(channel);
}
private void time() {
pubnub.time(new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("TIME : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("TIME : " + error);
}
});
}
private void disconnectAndResubscribe() {
pubnub.disconnectAndResubscribe();
}
private void disconnectAndResubscribeWithTimetoken(String timetoken) {
pubnub.disconnectAndResubscribeWithTimetoken(timetoken);
}
public void startDemo() {
reader = new Scanner(System.in);
notifyUser("HINT:\tTo test Re-connect and catch-up");
notifyUser("\tDisconnect your machine from network/internet and");
notifyUser("\tre-connect your machine after sometime");
this.SSL = getBooleanFromConsole("SSL");
if (this.publish_key.length() == 0) this.publish_key = getStringFromConsole("Publish Key");
if (this.subscribe_key.length() == 0) this.subscribe_key = getStringFromConsole("Subscribe Key");
if (this.secret_key.length() == 0) this.secret_key = getStringFromConsole("Secret Key", true);
if (this.cipher_key.length() == 0) this.cipher_key = getStringFromConsole("Cipher Key", true);
pubnub = new Pubnub(this.publish_key, this.subscribe_key, this.secret_key, this.cipher_key, this.SSL);
pubnub.setCacheBusting(false);
displayMenuOptions();
String channelName = null;
int command = 0;
while ((command = reader.nextInt()) != 9) {
reader.nextLine();
switch (command) {
case 0:
displayMenuOptions();
break;
case 1:
{
boolean isGroup = getBooleanFromConsole("Group");
if (isGroup) {
String groupName = getStringFromConsole("Subscribe: Enter Group name");
subscribeToGroup(groupName);
/*
notifyUser("Subscribed to following groups: ");
notifyUser(PubnubUtil.joinString(
pubnub.getSubscribedGroupsArray(), " : "));
*/
} else {
channelName = getStringFromConsole("Subscribe: Enter Channel name");
subscribe(channelName);
notifyUser("Subscribed to following channels: ");
notifyUser(PubnubUtil.joinString(
pubnub.getSubscribedChannelsArray(), " : "));
}
}
break;
case 2:
channelName = getStringFromConsole("Channel Name");
boolean store = getBooleanFromConsole("Store", true);
publish(channelName, store);
break;
case 3:
channelName = getStringFromConsole("Channel Name");
presence(channelName);
break;
case 4:
channelName = getStringFromConsole("Channel Name");
int count = getIntFromConsole("Count");
boolean includeToken = getBooleanFromConsole("Include Timetokens");
history(channelName, count, includeToken);
break;
case 5:
channelName = getStringFromConsole("Channel Name", true);
hereNow(channelName);
break;
case 6:
{
boolean isGroup = getBooleanFromConsole("Group");
if (isGroup) {
String groupName = getStringFromConsole("UnSubscribe: Enter Group name");
unsubscribeFromGroup(channelName);
} else {
channelName = getStringFromConsole("UnSubscribe: Enter Channel name");
unsubscribe(channelName);
}
}
break;
case 7:
channelName = getStringFromConsole("Channel Name");
unsubscribePresence(channelName);
break;
case 8:
time();
break;
case 10:
disconnectAndResubscribe();
break;
case 11:
notifyUser("Disconnect and Resubscribe with timetoken : Enter timetoken");
String timetoken = getStringFromConsole("Timetoken");
disconnectAndResubscribeWithTimetoken(timetoken);
break;
case 12:
pubnub.setResumeOnReconnect(pubnub.isResumeOnReconnect() ? false
: true);
notifyUser("RESUME ON RECONNECT : " + pubnub.isResumeOnReconnect());
break;
case 13:
int maxRetries = getIntFromConsole("Max Retries");
setMaxRetries(maxRetries);
break;
case 14:
int retryInterval = getIntFromConsole("Retry Interval");
setRetryInterval(retryInterval);
break;
case 15:
int windowInterval = getIntFromConsole("Window Interval");
setWindowInterval(windowInterval);
break;
case 16:
int subscribeTimeout = getIntFromConsole("Subscribe Timeout ( in milliseconds) ");
setSubscribeTimeout(subscribeTimeout);
break;
case 17:
int nonSubscribeTimeout = getIntFromConsole("Non Subscribe Timeout ( in milliseconds) ");
setNonSubscribeTimeout(nonSubscribeTimeout);
break;
case 18:
notifyUser("Set/Unset Auth Key: Enter blank for unsetting key");
String authKey = getStringFromConsole("Auth Key");
pubnub.setAuthKey(authKey);
break;
case 19:
pamGrant();
break;
case 20:
pamRevoke();
break;
case 21:
pamAudit();
break;
case 22:
pubnub.setOrigin(getStringFromConsole("Origin"));
break;
case 23:
pubnub.setDomain(getStringFromConsole("Domain"));
break;
case 24:
pubnub.setCacheBusting(true);
break;
case 25:
pubnub.setCacheBusting(false);
break;
case 26:
notifyUser("Set UUID");
String uuid = getStringFromConsole("UUID");
pubnub.setUUID(uuid);
break;
case 27:
int heartbeat = getIntFromConsole("Pubnub Presence Heartbeat ( in seconds ), Current value : " + pubnub.getHeartbeat());
pubnub.setHeartbeat(heartbeat, new Callback(){
@Override
public void successCallback(String channel, Object message) {
System.out.println(System.currentTimeMillis() / 1000 + " : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
System.out.println(System.currentTimeMillis() / 1000 + " : " + error);
}
});
break;
case 28:
int heartbeatInterval = getIntFromConsole("Pubnub Presence Heartbeat Interval ( in seconds ), Current value : " + pubnub.getHeartbeatInterval());
pubnub.setHeartbeatInterval(heartbeatInterval, new Callback(){
@Override
public void successCallback(String channel, Object message) {
System.out.println(System.currentTimeMillis() / 1000 + " : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
System.out.println(System.currentTimeMillis() / 1000 + " : " + error);
}
});
break;
case 29:
getState();
break;
case 30:
setState();
break;
case 31:
String uid = getStringFromConsole("UUID", true);
if (uid == null || uid.length() == 0) uid = pubnub.getUUID();
whereNow(uid);
break;
case 32:
// add channel to channel group
{
String group = getStringFromConsole("Group");
String channel = getStringFromConsole("Channel");
addChannelToGroup(group, channel);
}
break;
case 33:
// remove channel from group
{
String group = getStringFromConsole("Group");
String channel = getStringFromConsole("Channel");
removeChannelFromGroup(group, channel);
}
break;
case 34:
// list channels for channel group
{
String group = getStringFromConsole("Group");
listChannelsForGroup(group);
}
break;
case 35:
// list groups
{
String namespace = getStringFromConsole("Namespace", true);
listGroups(namespace);
}
break;
case 36:
// remove group
{
String group = getStringFromConsole("Group");
removeGroup(group);
}
break;
case 37:
// list namespaces
{
listNamespaces();
}
break;
case 38:
// remove namespace
{
String namespace = getStringFromConsole("Namespace");
removeNamespace(namespace);
}
break;
default:
notifyUser("Invalid Input");
}
displayMenuOptions();
}
notifyUser("Exiting");
pubnub.shutdown();
}
private void subscribeToGroup(String groupName) {
try {
pubnub.channelGroupSubscribe(groupName, new Callback() {
@Override
public void connectCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : CONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void disconnectCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : DISCONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
public void reconnectCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : RECONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void successCallback(String channel, Object message) {
notifyUser("SUBSCRIBE : " + channel + " : "
+ message.getClass() + " : " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("SUBSCRIBE : ERROR on channel " + channel
+ " : " + error.toString());
if (error.errorCode == PubnubError.PNERR_TIMEOUT)
pubnub.disconnectAndResubscribe();
}
});
} catch (Exception e) {
}
}
private void removeNamespace(String namespace) {
pubnub.channelGroupRemoveNamespace(namespace, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) REMOVE NAMESPACE : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) REMOVE NAMESPACE : " + error);
}
});
}
private void listNamespaces() {
pubnub.channelGroupListNamespaces(new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) LIST NAMESPACES : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) LIST NAMESPACES : " + error);
}
});
}
private void removeGroup(String group) {
pubnub.channelGroupRemoveGroup(group, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) REMOVE GROUP : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) REMOVE GROUP : " + error);
}
});
}
private void listGroups(String namespace) {
Callback cb = new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) LIST GROUPS : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) LIST GROUPS : " + error);
}
};
if (namespace != null && namespace.length() > 0) {
pubnub.channelGroupListGroups(namespace, cb);
} else {
pubnub.channelGroupListGroups(cb);
}
}
private void listChannelsForGroup(String group) {
pubnub.channelGroupListChannels(group, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) LIST CHANNELS : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) LIST CHANNELS : " + error);
}
});
}
private void removeChannelFromGroup(String group, String channel) {
pubnub.channelGroupRemoveChannel(group, channel, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) REMOVE CHANNEL : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) REMOVE CHANNEL : " + error);
}
});
}
private void addChannelToGroup(String group, String channel) {
pubnub.channelGroupAddChannel(group, channel, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("(CHANNEL GROUP) ADD CHANNEL : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("(CHANNEL GROUP) ADD CHANNEL : " + error);
}
});
}
private void whereNow(String uuid) {
pubnub.whereNow(uuid, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("WHERE NOW : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("WHERE NOW : " + error);
}
});
}
private void setState() {
boolean isGroup = getBooleanFromConsole("Group");
if (!isGroup) {
String channel = getStringFromConsole("Channel");
String uuid = getStringFromConsole("UUID", true);
if (uuid == null || uuid.length() == 0) uuid = pubnub.getUUID();
JSONObject metadata = getJSONObjectFromConsole("Metadata");
pubnub.setState(channel, uuid, metadata, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("SUBSCRIBER SET STATE : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("SUBSCRIBER SET STATE : " + error);
}
});
} else {
String group = getStringFromConsole("Group");
String uuid = getStringFromConsole("UUID", true);
if (uuid == null || uuid.length() == 0) uuid = pubnub.getUUID();
JSONObject metadata = getJSONObjectFromConsole("Metadata");
pubnub.channelGroupSetState(group, uuid, metadata, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("SUBSCRIBER SET STATE : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("SUBSCRIBER SET STATE : " + error);
}
});
}
}
private void getState() {
String channel = getStringFromConsole("Channel");
String uuid = getStringFromConsole("UUID", true);
if (uuid == null || uuid.length() == 0) uuid = pubnub.getUUID();
pubnub.getState(channel, uuid, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("SUBSCRIBER GET STATE : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("SUBSCRIBER GET STATE : " + error);
}
});
}
private String getStringFromConsole(String message, boolean optional) {
int attempt_count = 0;
String input = null;
do {
if (attempt_count > 0) System.out.print("Invalid input. ");
String message1 = "Enter " + message ;
message1 = (optional)?message1+" ( Optional input. You can skip by pressing enter )":message1;
notifyUser(message1);
input = reader.nextLine();
attempt_count++;
} while ((input == null || input.length() == 0) && !optional);
notifyUser(message + " : " + input);
return input;
}
private JSONObject getJSONObjectFromConsole(String message, boolean optional) {
int attempt_count = 0;
String input = null;
JSONObject input_jso = null;
do {
if (attempt_count > 0) System.out.print("Invalid input. ");
String message1 = "Enter " + message ;
message1 = (optional)?message1+" ( Optional input. You can skip by pressing enter )":message1;
notifyUser(message1);
input = reader.nextLine();
try {
input_jso = new JSONObject(input);
} catch (Exception e) {input_jso = null;}
attempt_count++;
} while ((input_jso == null || input_jso.length() == 0) && !optional);
notifyUser(message + " : " + input_jso);
return input_jso;
}
private JSONObject getJSONObjectFromConsole(String message) {
return getJSONObjectFromConsole(message, false);
}
private String getStringFromConsole(String message) {
return getStringFromConsole(message, false);
}
private int getIntFromConsole(String message, boolean optional) {
int attempt_count = 0;
String input = null;
int returnVal = -1;
do {
if (attempt_count > 0) notifyUser("Invalid input. ");
String message1 = "Enter " + message;
message1 = (optional)?message1+" ( Optional input. You can skip by pressing enter ) ":message1;
notifyUser(message1);
input = reader.nextLine();
attempt_count++;
returnVal = Integer.parseInt(input);
} while ((input == null || input.length() == 0 || returnVal < -1) && !optional);
notifyUser(message + " : " + returnVal);
return returnVal;
}
private int getIntFromConsole(String message) {
return getIntFromConsole(message, false);
}
private boolean getBooleanFromConsole(String message, boolean optional) {
int attempt_count = 0;
String input = null;
boolean returnVal = false;
do {
if (attempt_count > 0) notifyUser("Invalid input. ");
String message1 = message + " ? ( Enter Yes/No or Y/N )";
message1 = (optional)?message1+" ( Optional input. You can skip by pressing enter ) ":message1;
notifyUser(message1);
input = reader.nextLine();
attempt_count++;
} while ((input == null || input.length() == 0 ||
( !input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no") &&
!input.equalsIgnoreCase("y") && !input.equalsIgnoreCase("n"))) && !optional);
returnVal = (input.equalsIgnoreCase("y") || input.equalsIgnoreCase("yes"))?true:false;
notifyUser(message + " : " + returnVal);
return returnVal;
}
private boolean getBooleanFromConsole(String message) {
return getBooleanFromConsole(message, false);
}
private void pamGrant() {
String channel = getStringFromConsole("Channel");
String auth_key = getStringFromConsole("Auth Key");
boolean read = getBooleanFromConsole("Read");
boolean write = getBooleanFromConsole("Write");
int ttl = getIntFromConsole("TTL");
pubnub.pamGrant(channel, auth_key, read, write, ttl, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("CHANNEL : " + channel + " , " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("CHANNEL : " + channel + " , " + error.toString());
}
});
}
private void pamAudit() {
String channel = getStringFromConsole("Channel", true);
String auth_key = getStringFromConsole("Auth Key", true);
Callback cb = new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("CHANNEL : " + channel + " , " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("CHANNEL : " + channel + " , " + error.toString());
}
};
if (channel != null && channel.length() > 0) {
if (auth_key != null && auth_key.length() != 0) {
pubnub.pamAudit(channel, auth_key, cb);
} else {
pubnub.pamAudit(channel, cb);
}
} else {
pubnub.pamAudit(cb);
}
}
private void pamRevoke() {
String channel = getStringFromConsole("Enter Channel");
String auth_key = getStringFromConsole("Auth Key");
pubnub.pamRevoke(channel, auth_key, new Callback() {
@Override
public void successCallback(String channel, Object message) {
notifyUser("CHANNEL : " + channel + " , " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("CHANNEL : " + channel + " , " + error.toString());
}
});
}
private void setMaxRetries(int maxRetries) {
pubnub.setMaxRetries(maxRetries);
}
private void setRetryInterval(int retryInterval) {
pubnub.setRetryInterval(retryInterval);
}
private void setWindowInterval(int windowInterval) {
pubnub.setWindowInterval(windowInterval);
}
private void setSubscribeTimeout(int subscribeTimeout) {
pubnub.setSubscribeTimeout(subscribeTimeout);
}
private void setNonSubscribeTimeout(int nonSubscribeTimeout) {
pubnub.setNonSubscribeTimeout(nonSubscribeTimeout);
}
private void displayMenuOptions() {
notifyUser("ENTER 1 FOR Subscribe "
+ "(Currently subscribed to "
+ this.pubnub.getCurrentlySubscribedChannelNames() + ")");
notifyUser("ENTER 2 FOR Publish");
notifyUser("ENTER 3 FOR Presence");
notifyUser("ENTER 4 FOR History");
notifyUser("ENTER 5 FOR Here Now");
notifyUser("ENTER 6 FOR Unsubscribe");
notifyUser("ENTER 7 FOR Presence-Unsubscribe");
notifyUser("ENTER 8 FOR Time");
notifyUser("ENTER 9 FOR EXIT OR QUIT");
notifyUser("ENTER 10 FOR Disconnect-And-Resubscribe");
notifyUser("ENTER 11 FOR Disconnect-And-Resubscribe with timetoken");
notifyUser("ENTER 12 FOR Toggle Resume On Reconnect ( current: " + pubnub.getResumeOnReconnect() + " )");
notifyUser("ENTER 13 FOR Setting MAX Retries ( current: " + pubnub.getMaxRetries() + " )");
notifyUser("ENTER 14 FOR Setting Retry Interval ( current: " + pubnub.getRetryInterval() + " milliseconds )");
notifyUser("ENTER 15 FOR Setting Window Interval ( current: " + pubnub.getWindowInterval() + " milliseconds )");
notifyUser("ENTER 16 FOR Setting Subscribe Timeout ( current: " + pubnub.getSubscribeTimeout() + " milliseconds )");
notifyUser("ENTER 17 FOR Setting Non Subscribe Timeout ( current: " + pubnub.getNonSubscribeTimeout() + " milliseconds )");
notifyUser("ENTER 18 FOR Setting/Unsetting auth key ( current: " + pubnub.getAuthKey() + " )");
notifyUser("ENTER 19 FOR PAM grant");
notifyUser("ENTER 20 FOR PAM revoke");
notifyUser("ENTER 21 FOR PAM Audit");
notifyUser("ENTER 22 FOR Setting Origin ( current: " + pubnub.getOrigin() + " )");
notifyUser("ENTER 23 FOR Setting Domain ( current: "+ pubnub.getDomain() + " )");
notifyUser("ENTER 24 FOR Enabling Cache Busting ( current: " + pubnub.getCacheBusting() + " )");
notifyUser("ENTER 25 FOR Disabling Cache Busting ( current: " + pubnub.getCacheBusting() + " )");
notifyUser("ENTER 26 FOR Setting UUID ( current: " + pubnub.getUUID() + " )");
notifyUser("ENTER 27 FOR Setting Presence Heartbeat ( current: " + pubnub.getHeartbeat() + " )");
notifyUser("ENTER 28 FOR Setting Presence Heartbeat Interval ( current: " + pubnub.getHeartbeatInterval() + " )");
notifyUser("ENTER 29 FOR Getting Subscriber State");
notifyUser("ENTER 30 FOR Setting Subscriber State");
notifyUser("ENTER 31 FOR Where Now");
notifyUser("ENTER 32 FOR [Channel Group] Add Channel");
notifyUser("ENTER 33 FOR [Channel Group] Remove Channel");
notifyUser("ENTER 34 FOR [Channel Group] List Channels");
notifyUser("ENTER 35 FOR [Channel Group] List Groups");
notifyUser("ENTER 36 FOR [Channel Group] Remove Group");
notifyUser("ENTER 37 FOR [Channel Group] List Namespaces");
notifyUser("ENTER 38 FOR [Channel Group] Remove Namespace");
notifyUser("\nENTER 0 to display this menu");
}
/**
* @param args
*/
public static void main(String[] args) {
PubnubDemoConsole pdc;
if (args.length == 4) {
pdc = new PubnubDemoConsole(args[0], args[1], args[2], args[3]);
} else
pdc = new PubnubDemoConsole();
pdc.startDemo();
}
}