1 2 package transport.channel; 3 4 import java.nio.channels.SocketChannel ; 5 import java.util.Collection ; 6 import java.util.HashMap ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.LinkedList ; 10 import java.util.Map ; 11 import java.util.Set ; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 17 import jegg.EggBase; 18 import jegg.impl.Dispatcher; 19 import jegg.impl.EggShell; 20 21 24 public class ChannelList extends EggBase 25 { 26 private static final Log LOG = LogFactory.getLog(ChannelList.class); 27 private Collection channels = new HashSet (); 28 29 public ChannelList() 30 { 31 super(); 32 } 33 34 public void init() 35 { 36 } 38 39 42 public void handle(Object message) 43 { 44 LOG.warn("Unexpected message: " + message); 45 } 46 47 public void handle(AddChannelCommand add) 48 { 49 LOG.info("Received new channel"); 50 SocketChannel ch = add.getChannel(); 51 channels.add(ch); 52 getContext().send(add); 53 } 54 55 public void handle(GetChannelCommand get) 56 { 57 int id = get.getID(); 58 59 if (LOG.isDebugEnabled()) 60 LOG.debug("Getting channel "+id); 61 62 SocketChannel channel = findChannel(id); 63 if (null != channel) 64 { 65 getContext().respond(new GetChannelResponse(id,channel)); 66 } 67 else 68 { 69 getContext().respond(new NoSuchChannelException(id)); 70 } 71 } 72 73 77 private SocketChannel findChannel(int id) 78 { 79 if (LOG.isDebugEnabled()) 80 LOG.debug("Finding channel "+id); 81 82 SocketChannel channel = null; 83 for (Iterator it = channels.iterator(); it.hasNext(); ) 84 { 85 SocketChannel ch = (SocketChannel ) it.next(); 86 if (ch.hashCode() == id) 87 { 88 channel = ch; 89 break; 90 } 91 } 92 return channel; 93 } 94 } 95 96 | Popular Tags |