KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > PullPushTestMux


1 // $Id: PullPushTestMux.java,v 1.4 2004/03/30 06:47:34 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.Channel;
8 import org.jgroups.JChannel;
9 import org.jgroups.Message;
10 import org.jgroups.MessageListener;
11 import org.jgroups.blocks.PullPushAdapter;
12
13
14
15
16 /**
17  * Uses PullPush building block to send/receive messages. Reception is passive, e.g. the receiver's
18  * receive() method is invoked whenever a message is received. The receiver has to register a callback method
19  * when creating the channel. Uses multiple MessageListeners
20  * @author Bela Ban
21  */

22 public class PullPushTestMux implements MessageListener {
23     private Channel channel;
24     private PullPushAdapter adapter;
25     MyListener[] listeners=null;
26
27
28     public PullPushTestMux() {
29     ;
30     }
31
32
33     public void receive(Message msg) {
34     System.out.println("Main receiver: received msg: " + msg);
35     }
36
37     public byte[] getState() { // only called if channel option GET_STATE_EVENTS is set to true
38
return null;
39     }
40
41     public void setState(byte[] state) {
42     
43     }
44
45
46     public void start() throws Exception JavaDoc {
47     int c;
48
49     channel=new JChannel();
50     channel.connect("PullPushTestMux");
51     adapter=new PullPushAdapter(channel);
52     adapter.setListener(this);
53
54     listeners=new MyListener[10];
55     for(int i=0; i < listeners.length; i++) {
56         listeners[i]=new MyListener(i, adapter);
57     }
58
59     while((c=choice()) != 'q') {
60         c-=48;
61         if(c < 0 || c > 9) {
62         System.err.println("Choose between 0 and 9");
63         continue;
64         }
65         if(c == 0)
66         adapter.send(new Message(null, null, "Message from default message listener"));
67         else
68         listeners[c].sendMessage();
69     }
70
71     channel.close();
72     System.exit(0);
73     }
74
75
76     int choice() {
77     int c;
78     System.out.println("\n[q]uit [0]: send message on default channel [1-9] send message on other channels:");
79     System.out.flush();
80     try {
81         c=System.in.read();
82     }
83     catch(Exception JavaDoc ex) {
84         return -1;
85     }
86     finally {
87         try {
88         System.in.skip(System.in.available());
89         }
90         catch(Exception JavaDoc ex) {
91         }
92     }
93     return c;
94     }
95
96
97     public static void main(String JavaDoc args[]) {
98     PullPushTestMux t=new PullPushTestMux();
99     try {
100         t.start();
101     }
102     catch(Exception JavaDoc e) {
103         System.err.println(e);
104     }
105     }
106
107
108     public class MyListener implements MessageListener {
109     Integer JavaDoc id=null;
110     PullPushAdapter ad=null;
111
112     MyListener(int id, PullPushAdapter ad) {
113         this.id=new Integer JavaDoc(id);
114         this.ad=ad;
115         ad.registerListener(this.id, this);
116     }
117     
118     public void receive(Message msg) {
119         System.out.println("MyListener #" + id + ": received message from " +
120                 msg.getSrc() + ": " + msg.getObject());
121     }
122
123     public byte[] getState() {
124         return null;
125     }
126
127     public void setState(byte[] state) {
128         ;
129     }
130
131     void sendMessage() throws Exception JavaDoc {
132         Message msg=new Message(null, null, "Message from " + id);
133         ad.send(id, msg);
134     }
135     }
136
137 }
138
Popular Tags