1 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 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() { return null; 39 } 40 41 public void setState(byte[] state) { 42 43 } 44 45 46 public void start() throws Exception { 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 ex) { 84 return -1; 85 } 86 finally { 87 try { 88 System.in.skip(System.in.available()); 89 } 90 catch(Exception ex) { 91 } 92 } 93 return c; 94 } 95 96 97 public static void main(String args[]) { 98 PullPushTestMux t=new PullPushTestMux(); 99 try { 100 t.start(); 101 } 102 catch(Exception e) { 103 System.err.println(e); 104 } 105 } 106 107 108 public class MyListener implements MessageListener { 109 Integer id=null; 110 PullPushAdapter ad=null; 111 112 MyListener(int id, PullPushAdapter ad) { 113 this.id=new Integer (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 { 132 Message msg=new Message(null, null, "Message from " + id); 133 ad.send(id, msg); 134 } 135 } 136 137 } 138 | Popular Tags |