KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transport > channel > ChannelList


1
2 package transport.channel;
3
4 import java.nio.channels.SocketChannel JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Set JavaDoc;
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 /**
22  *
23  */

24 public class ChannelList extends EggBase
25 {
26     private static final Log LOG = LogFactory.getLog(ChannelList.class);
27     private Collection JavaDoc channels = new HashSet JavaDoc();
28     
29     public ChannelList()
30     {
31         super();
32     }
33
34     public void init()
35     {
36         // EMPTY
37
}
38
39     /* (non-Javadoc)
40      * @see egg.Egg#handle(java.lang.Object)
41      */

42     public void handle(Object JavaDoc 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 JavaDoc 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 JavaDoc 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     // ---------------------------------------------------------------------- //
74
// Implementation support methods
75
// ---------------------------------------------------------------------- //
76

77     private SocketChannel JavaDoc findChannel(int id)
78     {
79         if (LOG.isDebugEnabled())
80             LOG.debug("Finding channel "+id);
81         
82         SocketChannel JavaDoc channel = null;
83         for (Iterator JavaDoc it = channels.iterator(); it.hasNext(); )
84         {
85             SocketChannel JavaDoc ch = (SocketChannel JavaDoc) it.next();
86             if (ch.hashCode() == id)
87             {
88                 channel = ch;
89                 break;
90             }
91         }
92         return channel;
93     }
94 }
95
96
Popular Tags