-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pub_Sub.java
1683 lines (1200 loc) · 43.8 KB
/
Pub_Sub.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
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
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JInternalFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.AccessException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.WindowStateListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
public class Pub_Sub extends JFrame implements BootStrap2ServerInterface, Serializable{
private JPanel contentPane;
private JTextField textField_Message;
private JTextField textField_QueueName;
static JButton button_subscriber_Tab;
static HashMap<String,ArrayList<String>> ServiceMessageData = new HashMap<>();
//rmi related variables
static String poiServer = "";
static String username = "";
static final int port = 7394;
static String bootstrap = "129.21.22.196";
static int bootstrapPort = 7394;
static Registry registry;
// poiServer HeartBeat check timer task
static Timer heartbeatTimer;
//counter for notifications
static int unreadMessageCounter = 0;
JList list_ServicesList;
static ArrayList<String> myPublishedServices = new ArrayList<String>();
//tcp for notifications
static int tcpPort = 6162;
static ServerSocket ss;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// Pub_Sub frame = new Pub_Sub();
// System.out.println("LOGIN WINDOW POI: " + BunnyME_Client.poiServer);
//
// frame.setVisible(true);
} catch (Exception e) {
// e.printStackTrace();
}
}
});
}
// public Pub_Sub()
// {
// }
/**
* Create the frame.
* @param queueList
* @throws RemoteException
* @throws AccessException
*/
public Pub_Sub(String poi, String user, ArrayList<String> queueList) throws AccessException, RemoteException {
//parameter received form the login window
poiServer = poi;
username = user;
System.out.println("poiServer: " + poiServer);
System.out.println("username: " + username);
//tcp listening thread for notifications
Thread heartbeat = new Thread(new KeepAlive());
heartbeat.start();
// Start registry
try {
registry = LocateRegistry.getRegistry(port);// use any no. less than
// 55000
registry.list();
registry.rebind("update", this);
} catch (Exception e) {
registry = LocateRegistry.createRegistry(port);
registry.rebind("update", this);
}
//UI components
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1479, 833);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label_BunnyMe = new JLabel("Bunny ME");
label_BunnyMe.setBounds(638, 10, 176, 49);
label_BunnyMe.setFont(new Font("Tahoma", Font.PLAIN, 40));
contentPane.add(label_BunnyMe);
JPanel panel_Parent = new JPanel();
panel_Parent.setBounds(0, 65, 1453, 66);
contentPane.add(panel_Parent);
panel_Parent.setLayout(null);
JPanel panel_Child = new JPanel();
panel_Child.setBounds(0, 127, 1453, 635);
contentPane.add(panel_Child);
panel_Child.setLayout(null);
JPanel panel_Welcome = new JPanel();
panel_Welcome.setLayout(null);
panel_Welcome.setBounds(0, 0, 1452, 507);
JLabel label = new JLabel("Welcome To Bunny ME, Please select either publisher or subscriber to proceed.");
label.setFont(new Font("Tahoma", Font.PLAIN, 30));
label.setBounds(169, 102, 1107, 180);
panel_Welcome.add(label);
JPanel panel_Publisher = new JPanel();
panel_Publisher.setBounds(0, 0, 1452, 635);
panel_Publisher.setLayout(null);
JPanel panel_Subscriber = new JPanel();
panel_Subscriber.setBounds(0, 21, 1452, 614);
JPanel panel_left = new JPanel();
panel_left.setBounds(0, 0, 394, 635);
panel_left.setLayout(null);
JPanel panel_ServiceList = new JPanel();
panel_ServiceList.setBounds(0, 0, 394, 498);
panel_ServiceList.setLayout(null);
JPanel panel_ServiceListInner = new JPanel();
panel_ServiceListInner.setBounds(21, 10, 373, 470);
panel_ServiceList.add(panel_ServiceListInner);
panel_ServiceListInner.setLayout(null);
list_ServicesList = new JList();
list_ServicesList.setBounds(33, 67, 308, 387);
panel_ServiceListInner.add(list_ServicesList);
list_ServicesList.setFont(new Font("Tahoma", Font.PLAIN, 30));
JLabel lblListOfServices = new JLabel(" List of Services:");
lblListOfServices.setBounds(0, 21, 352, 37);
panel_ServiceListInner.add(lblListOfServices);
lblListOfServices.setFont(new Font("Tahoma", Font.PLAIN, 30));
JPanel panel_ServiceDetails_Inner = new JPanel();
panel_ServiceDetails_Inner.setBounds(21, 0, 373, 234);
//panel_ServiceDetails.add(panel_ServiceDetails_Inner);
panel_ServiceDetails_Inner.setLayout(null);
JLabel lblServiceDetails = new JLabel(" Service Details:");
lblServiceDetails.setBounds(0, 21, 333, 37);
panel_ServiceDetails_Inner.add(lblServiceDetails);
lblServiceDetails.setFont(new Font("Tahoma", Font.PLAIN, 30));
JList list_ServiceDetails = new JList();
list_ServiceDetails.setBounds(47, 79, 305, 142);
panel_ServiceDetails_Inner.add(list_ServiceDetails);
list_ServiceDetails.setFont(new Font("Tahoma", Font.PLAIN, 30));
JButton btnUpdateQ = new JButton("Update Q");
btnUpdateQ.setBounds(60, 569, 298, 45);
btnUpdateQ.setFont(new Font("Tahoma", Font.PLAIN, 30));
JButton button_Message = new JButton("Message");
button_Message.setBounds(60, 519, 298, 45);
button_Message.setFont(new Font("Tahoma", Font.PLAIN, 30));
JPanel panel_right = new JPanel();
panel_right.setBounds(419, 32, 1012, 582);
panel_right.setLayout(null);
JPanel panel_updateService = new JPanel();
panel_updateService.setBounds(21, 21, 980, 540);
//
panel_updateService.setLayout(null);
JLabel label_UpdateService = new JLabel("Update Service");
label_UpdateService.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_UpdateService.setBounds(355, 0, 251, 37);
panel_updateService.add(label_UpdateService);
JPanel panel_updateService_Tabs = new JPanel();
panel_updateService_Tabs.setBounds(0, 52, 980, 44);
panel_updateService.add(panel_updateService_Tabs);
panel_updateService_Tabs.setLayout(null);
// panel_right.add(panel_updateService);
JButton button_Add_Tab = new JButton("Add");
button_Add_Tab.setBounds(0, 0, 492, 45);
panel_updateService_Tabs.add(button_Add_Tab);
button_Add_Tab.setFont(new Font("Tahoma", Font.PLAIN, 30));
JButton button_Remove_Tab = new JButton("Remove");
button_Remove_Tab.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_Remove_Tab.setBounds(488, 0, 492, 45);
panel_updateService_Tabs.add(button_Remove_Tab);
JPanel panel_updateService_Content = new JPanel();
panel_updateService_Content.setBounds(0, 96, 980, 444);
panel_updateService.add(panel_updateService_Content);
panel_updateService_Content.setLayout(null);
JPanel panel_RemoveQueue = new JPanel();
panel_RemoveQueue.setBounds(33, 37, 914, 363);
JButton button_Remove = new JButton("Remove");
button_Remove.setFont(new Font("Tahoma", Font.PLAIN, 30));
JLabel label_QueueRemoveMessage = new JLabel("");
label_QueueRemoveMessage.setFont(new Font("Tahoma", Font.PLAIN, 30));
JPanel panel_AddQueue = new JPanel();
panel_AddQueue.setLayout(null);
panel_AddQueue.setBounds(45, 48, 914, 363);
JLabel label_QueueRemoveStatus = new JLabel("");
label_QueueRemoveStatus.setFont(new Font("Tahoma", Font.PLAIN, 30));
GroupLayout gl_panel_RemoveQueue = new GroupLayout(panel_RemoveQueue);
gl_panel_RemoveQueue.setHorizontalGroup(
gl_panel_RemoveQueue.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_RemoveQueue.createSequentialGroup()
.addGroup(gl_panel_RemoveQueue.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_RemoveQueue.createSequentialGroup()
.addGap(132)
.addComponent(label_QueueRemoveMessage, GroupLayout.PREFERRED_SIZE, 702, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_RemoveQueue.createSequentialGroup()
.addGap(411)
.addComponent(button_Remove, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_RemoveQueue.createSequentialGroup()
.addGap(166)
.addComponent(label_QueueRemoveStatus, GroupLayout.PREFERRED_SIZE, 629, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(80, Short.MAX_VALUE))
);
gl_panel_RemoveQueue.setVerticalGroup(
gl_panel_RemoveQueue.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_RemoveQueue.createSequentialGroup()
.addGap(21)
.addComponent(label_QueueRemoveMessage, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)
.addGap(39)
.addComponent(label_QueueRemoveStatus, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
.addGap(31)
.addComponent(button_Remove, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
.addContainerGap(10, Short.MAX_VALUE))
);
panel_RemoveQueue.setLayout(gl_panel_RemoveQueue);
textField_QueueName = new JTextField();
textField_QueueName.setFont(new Font("Tahoma", Font.PLAIN, 30));
textField_QueueName.setColumns(10);
textField_QueueName.setBounds(211, 128, 580, 51);
panel_AddQueue.add(textField_QueueName);
JButton button_Add = new JButton("Add");
button_Add.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_Add.setBounds(411, 276, 198, 51);
panel_AddQueue.add(button_Add);
JLabel label_QueueName = new JLabel("Queue Name:");
label_QueueName.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_QueueName.setBounds(112, 63, 209, 42);
panel_AddQueue.add(label_QueueName);
JLabel label_AddQueueStatus = new JLabel("");
label_AddQueueStatus.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_AddQueueStatus.setBounds(221, 213, 570, 42);
panel_AddQueue.add(label_AddQueueStatus);
JPanel panel_SendMessage = new JPanel();
panel_SendMessage.setBounds(49, 36, 916, 503);
panel_SendMessage.setLayout(null);
JLabel lblSendAMessage = new JLabel("Send a Message:");
lblSendAMessage.setFont(new Font("Tahoma", Font.PLAIN, 30));
lblSendAMessage.setBounds(112, 108, 284, 43);
panel_SendMessage.add(lblSendAMessage);
JButton button_SendMessage = new JButton("Send Message");
button_SendMessage.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_SendMessage.setBounds(390, 345, 234, 48);
panel_SendMessage.add(button_SendMessage);
textField_Message = new JTextField();
textField_Message.setFont(new Font("Tahoma", Font.PLAIN, 30));
textField_Message.setBounds(213, 172, 626, 48);
panel_SendMessage.add(textField_Message);
textField_Message.setColumns(10);
JLabel label_sendMessageStatus = new JLabel("");
label_sendMessageStatus.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_sendMessageStatus.setBounds(266, 259, 559, 43);
panel_SendMessage.add(label_sendMessageStatus);
panel_Child.repaint();
panel_Child.revalidate();
JButton button_publisher_Tab = new JButton("Publisher");
button_publisher_Tab.setBounds(0, 0, 734, 66);
panel_Parent.add(button_publisher_Tab);
button_publisher_Tab.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_subscriber_Tab = new JButton("Subscriber");
button_subscriber_Tab.setBounds(733, 0, 725, 66);
panel_Parent.add(button_subscriber_Tab);
button_subscriber_Tab.setFont(new Font("Tahoma", Font.PLAIN, 30));
panel_Subscriber.setLayout(null);
JList list_SubscribedServices = new JList();
list_SubscribedServices.setFont(new Font("Tahoma", Font.PLAIN, 30));
list_SubscribedServices.setBounds(31, 110, 342, 343);
JLabel label_SubscribedServices = new JLabel("Subscribed Services:");
label_SubscribedServices.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_SubscribedServices.setBounds(31, 44, 342, 45);
JButton button_Unsubscribe = new JButton("Unsubscribe");
button_Unsubscribe.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_Unsubscribe.setBounds(31, 474, 342, 45);
JButton button_AddNewSubscription = new JButton("Add New Subscription");
button_AddNewSubscription.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_AddNewSubscription.setBounds(31, 540, 342, 45);
JPanel panel_SubscribedMessges = new JPanel();
panel_SubscribedMessges.setBounds(43, 33, 907, 485);
panel_SubscribedMessges.setLayout(null);
JLabel label_SubscribedMessages = new JLabel("Subscribed Messages:");
label_SubscribedMessages.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_SubscribedMessages.setBounds(51, 21, 308, 37);
panel_SubscribedMessges.add(label_SubscribedMessages);
JList list_SubscribedMessages = new JList();
list_SubscribedMessages.setFont(new Font("Tahoma", Font.PLAIN, 30));
list_SubscribedMessages.setBounds(87, 79, 759, 351);
panel_SubscribedMessges.add(list_SubscribedMessages);
JPanel panel_AddNewSubscription = new JPanel();
panel_AddNewSubscription.setBounds(21, 34, 970, 527);
panel_AddNewSubscription.setLayout(null);
JLabel label_UnsubscribedServices = new JLabel("Unsubscribed Services:");
label_UnsubscribedServices.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_UnsubscribedServices.setBounds(53, 0, 359, 62);
panel_AddNewSubscription.add(label_UnsubscribedServices);
JList list_UnsubscribedServices = new JList();
list_UnsubscribedServices.setFont(new Font("Tahoma", Font.PLAIN, 30));
list_UnsubscribedServices.setBounds(33, 70, 518, 436);
panel_AddNewSubscription.add(list_UnsubscribedServices);
JButton button_Subscribe = new JButton("Subscribe");
button_Subscribe.setFont(new Font("Tahoma", Font.PLAIN, 30));
button_Subscribe.setBounds(633, 369, 258, 56);
panel_AddNewSubscription.add(button_Subscribe);
heartbeatTimer = new Timer();
heartbeatTimer.schedule(new HeartBeatSchedule(), 5000, 5000);
JButton btnLogout = new JButton("LogOut");
btnLogout.setFont(new Font("Tahoma", Font.PLAIN, 30));
btnLogout.setBounds(1215, 9, 199, 50);
contentPane.add(btnLogout);
JLabel label_SubscriptionStatus = new JLabel("");
label_SubscriptionStatus.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_SubscriptionStatus.setBounds(598, 191, 320, 36);
panel_AddNewSubscription.add(label_SubscriptionStatus);
// logout user on closing the window RMI call
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
try {
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
send.LogOut(username);
ss.close();
} catch (Exception e) {
// e.printStackTrace();
}
dispose();
}
@Override
public void windowClosed(WindowEvent e) {
}
});
JLabel label_userName = new JLabel("");
label_userName.setFont(new Font("Tahoma", Font.PLAIN, 30));
label_userName.setBounds(21, 10, 286, 45);
contentPane.add(label_userName);
label_userName.setText(username);
//Initial Welcome layout
panel_Child.add(panel_Welcome);
//********************* Event Listeners *********************
//logout
btnLogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
//RMI call to the poi server
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
send.LogOut(username);
//navigate back to the login window
dispose();
LoginWindow lw = new LoginWindow();
lw.setVisible(true);
dispose();
} catch (Exception e) {
// e.printStackTrace();
}
}
});
// unsubscribe from a service
button_Unsubscribe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String QPattern = (String)list_SubscribedServices.getSelectedValue();
int QPatternIndex = list_SubscribedServices.getSelectedIndex();
if(QPattern == null)
{
//check at the client side
}
else
{
//RMI call to unsubscribe from a service
try {
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
boolean result = send.unsubscribe(username, QPattern);
DefaultListModel dlm = (DefaultListModel) list_SubscribedServices.getModel();
dlm.remove(QPatternIndex);
list_SubscribedServices.setModel(dlm);
} catch (Exception e)
{
// e.printStackTrace();
}
}
}
});
//add a new subscription- shows all unsubscribed services
button_AddNewSubscription.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//change panels on display
panel_right.removeAll();
panel_right.repaint();
panel_right.revalidate();
panel_right.add(panel_AddNewSubscription);
panel_Child.repaint();
panel_Child.revalidate();
//RMI call to received these unsubscribed services and display them
try {
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
ArrayList<String> result = send.getUnsubscribedQueues(username);
if(!result.isEmpty())
{
DefaultListModel dlm = new DefaultListModel();
for(int i=0; i<result.size(); i++)
{
dlm.addElement(result.get(i));
}
list_UnsubscribedServices.setModel(dlm);
}
} catch (Exception ex) {
// ex.printStackTrace();
}
}
});
//subscribe to a selected service
button_Subscribe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//get the selected service
String QPattern = (String) list_UnsubscribedServices.getSelectedValue();
int QPatternIndex = list_UnsubscribedServices.getSelectedIndex();
if(QPattern == null)
{
//check at client side
label_SubscriptionStatus.setText("Please Select a Queue");
}
else
{
//RMI call to subscribe to the selected service
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
boolean result = send.subscribe(username, QPattern);
if(result)
{
label_SubscriptionStatus.setText("Subscribed: " + QPattern);
//update list of subscribed services
try
{
BootStrap2ServerInterface fetchSubs = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
ArrayList<String> resultsubs = fetchSubs.getMySubscriptions(username);
if(!resultsubs.isEmpty())
{
DefaultListModel dlm = new DefaultListModel();
for(int i=0; i<resultsubs.size(); i++)
{
dlm.addElement(resultsubs.get(i));
}
list_SubscribedServices.setModel(dlm);
}
}
catch(Exception ex)
{
// ex.printStackTrace();
}
//remove the subscribed service from the list of unsubscribed services
DefaultListModel dd = (DefaultListModel) list_UnsubscribedServices.getModel();
dd.remove(QPatternIndex);
list_UnsubscribedServices.setModel(dd);
}
else
{
//incase quque does not exist
label_SubscriptionStatus.setText("Queue does not Exists");
}
}
} catch (Exception ex) {
label_SubscriptionStatus.setText("Connection Error!");
// ex.printStackTrace();
}
}
});
//select the service and display relevant panels
list_SubscribedServices.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
panel_right.removeAll();
panel_right.repaint();
panel_right.revalidate();
panel_right.add(panel_SubscribedMessges);
panel_Child.repaint();
panel_Child.revalidate();
try {
String QPattern = (String) list_SubscribedServices.getSelectedValue();
BootStrap2ServerInterface fetchsubsmsg = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
ArrayList<String> resultsubmsgs = fetchsubsmsg.ShiftQueue(username, QPattern);
DefaultListModel dlm = new DefaultListModel();
if(resultsubmsgs != null)
{
for(int i=0; i < resultsubmsgs.size(); i++)
{
dlm.addElement(resultsubmsgs.get(i));
}
list_SubscribedMessages.setModel(dlm);
}
}
catch(Exception ex)
{
// System.out.println();
}
}
});
myPublishedServices = queueList;
//set all the services in the system in the list intially
if(!queueList.isEmpty())
{
DefaultListModel dlm = new DefaultListModel();
for(int i=0; i<queueList.size(); i++)
{
dlm.addElement(queueList.get(i));
}
list_ServicesList.setModel(dlm);
}
//On choosing the publisher tab, view the relevant panels
button_publisher_Tab.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//remove all earlier stuff
panel_Child.removeAll();
panel_Child.repaint();
panel_Child.revalidate();
panel_left.removeAll();
panel_left.repaint();
panel_left.revalidate();
panel_right.removeAll();
panel_right.repaint();
panel_right.revalidate();
//add new panel
panel_Child.add(panel_Publisher);
panel_Child.add(panel_Publisher);
panel_Publisher.add(panel_left);
panel_Publisher.add(panel_right);
//set left panel
panel_left.add(panel_ServiceList);
panel_left.add(btnUpdateQ);
panel_left.add(button_Message);
//Set right panel
panel_right.add(panel_SendMessage);
// panel_right.add(panel_updateService);
//
//update service contents
// panel_updateService_Content.add(panel_AddQueue);
// panel_updateService_Content.add(panel_RemoveQueue);
panel_Child.repaint();
panel_Child.revalidate();
}
});
//On choosing the subscriber tab, view the relevant panels
button_subscriber_Tab.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
unreadMessageCounter = 0;
button_subscriber_Tab.setText("Subscriber");
//remove all earlier stuff
panel_Child.removeAll();
panel_Child.repaint();
panel_Child.revalidate();
panel_left.removeAll();
panel_left.repaint();
panel_left.revalidate();
panel_right.removeAll();
panel_right.repaint();
panel_right.revalidate();
//add new panel
panel_Child.add(panel_Subscriber);
panel_Subscriber.add(panel_left);
panel_Subscriber.add(panel_right);
panel_left.add(list_SubscribedServices);
panel_left.add(label_SubscribedServices);
panel_left.add(button_Unsubscribe);
panel_left.add(button_AddNewSubscription);
panel_right.add(panel_SubscribedMessges);
// panel_right.add(panel_AddNewSubscription);
panel_Child.repaint();
panel_Child.revalidate();
//RMI call to get all the subscribed services
try
{
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
ArrayList<String> result = send.getMySubscriptions(username);
if(!result.isEmpty())
{
DefaultListModel dlm = new DefaultListModel();
for(int i=0; i<result.size(); i++)
{
dlm.addElement(result.get(i));
}
list_SubscribedServices.setModel(dlm);
}
}
catch(Exception ex)
{
// ex.printStackTrace();
}
}
});
//list select event handler to to view relevant data on the screen
list_ServicesList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if(event.getValueIsAdjusting())
{
String selected = list_ServicesList.getSelectedValue().toString();
String removeText = "Are you sure you want to remove Queue: " + selected + "?";
label_QueueRemoveMessage.setText(removeText);
}
}
});
list_ServiceDetails.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
//remove all earlier stuff
panel_right.removeAll();
panel_right.repaint();
panel_right.revalidate();
//add new panel
panel_right.add(panel_SendMessage);
panel_right.repaint();
panel_right.revalidate();
}
});
//update button to either create a new service or remove and existing service
btnUpdateQ.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//remove all earlier stuff
panel_right.removeAll();
panel_right.repaint();
panel_right.revalidate();
//add new panel
panel_right.add(panel_updateService);
panel_right.repaint();
panel_right.revalidate();
}
});
//add tab in update service to add a new service
button_Add_Tab.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//remove all earlier stuff
panel_updateService_Content.removeAll();
panel_updateService_Content.repaint();
panel_updateService_Content.revalidate();
//add new panel
panel_updateService_Content.add(panel_AddQueue);
panel_updateService_Content.repaint();
panel_updateService_Content.revalidate();
}
});
//RMI call to add a new service
button_Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String QName = textField_QueueName.getText();
String QPattern = username + "." + QName;
System.out.println("poiServer:" + poiServer);
//check if queue already exists
if(myPublishedServices.contains(QPattern))
{
label_AddQueueStatus.setText("Queue Already Exists");
}
else
{
//create service
try
{
BootStrap2ServerInterface send = (BootStrap2ServerInterface) Naming.lookup("//" + poiServer + ":" + port + "/update");
ArrayList<String> result = send.createQueue(username, QName, QPattern);
System.out.println("Successful!!! Queue Created.");
label_AddQueueStatus.setText("Queue created Successfull!!!");
myPublishedServices = result;
// System.out.println("result: " + result);
if(!result.isEmpty())
{
DefaultListModel dlm = new DefaultListModel();
for(int i=0; i<result.size(); i++)
{
dlm.addElement(result.get(i));
}
list_ServicesList.setModel(dlm);
}
}
catch (Exception ex)
{
System.out.println("Connection Not Establisted!!! add new server");
label_AddQueueStatus.setText("Connection Error...");
// ex.printStackTrace();
}
}
}
});
//remove an existing service tab selection
button_Remove_Tab.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//remove all earlier stuff
panel_updateService_Content.removeAll();
panel_updateService_Content.repaint();
panel_updateService_Content.revalidate();
//add new panel
panel_updateService_Content.add(panel_RemoveQueue);
panel_updateService_Content.repaint();
panel_updateService_Content.revalidate();
String QName = textField_QueueName.getText();
String removeText = "Please select a service to delete:";
label_QueueRemoveMessage.setText(removeText);
}
});