1 24 25 package org.objectweb.tribe.adapters; 26 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 30 import org.objectweb.tribe.channel.ReliableGroupChannel; 31 import org.objectweb.tribe.common.GroupIdentifier; 32 import org.objectweb.tribe.common.log.Trace; 33 import org.objectweb.tribe.exceptions.ChannelException; 34 import org.objectweb.tribe.exceptions.NotConnectedException; 35 import org.objectweb.tribe.messages.MessageListener; 36 37 54 public class PullPushAdapter implements Runnable 55 { 56 protected ReliableGroupChannel channel = null; 57 protected MessageListener listener = null; 58 protected Thread readerThread = null; 59 private static Trace logger = Trace 60 .getLogger("org.objectweb.tribe.channel"); 61 private boolean isKilled; 62 63 70 public PullPushAdapter(ReliableGroupChannel channel, MessageListener listener) 71 { 72 this.channel = channel; 73 this.listener = listener; 74 start(); 75 } 76 77 82 public ReliableGroupChannel getChannel() 83 { 84 return channel; 85 } 86 87 90 public void start() 91 { 92 if (readerThread == null) 93 { 94 readerThread = new Thread (this, "PullPushAdapterThread"); 95 isKilled = false; 96 readerThread.start(); 97 Thread.yield(); } 99 } 100 101 104 public void stop() 105 { 106 if ((readerThread != null) && readerThread.isAlive()) 107 { 108 isKilled = true; 109 readerThread.interrupt(); 110 try 111 { 112 channel.send(null); 113 } 114 catch (Exception ignore) 115 { 116 } 117 } 118 try 119 { 120 readerThread.join(1000); 121 } 122 catch (Exception ex) 123 { 124 } 125 } 126 127 137 public ArrayList send(Serializable msg) throws ChannelException, 138 NotConnectedException 139 { 140 return channel.send(msg); 141 } 142 143 155 public ArrayList send(Serializable msg, ArrayList members) 156 throws ChannelException, NotConnectedException 157 { 158 return channel.send(msg, members); 159 } 160 161 174 public ArrayList send(Serializable msg, GroupIdentifier gid, ArrayList members) 175 throws ChannelException, NotConnectedException 176 { 177 return channel.send(msg, gid, members); 178 } 179 180 184 public void run() 185 { 186 Serializable msg; 187 188 while (!isKilled) 189 { 190 try 191 { 192 msg = channel.receive(); 193 listener.receive(msg); 194 } 195 catch (Exception e) 196 { 197 logger.debug("PullPushAdapter: Error while reading from channel", e); 198 } 199 } 200 } 201 202 } | Popular Tags |