KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > ConnectionInfo


1 package com.ubermq.kernel;
2
3 import EDU.oswego.cs.dl.util.concurrent.*;
4 import com.ubermq.kernel.event.*;
5 import java.io.*;
6 import java.nio.*;
7 import java.nio.channels.*;
8 import java.util.*;
9
10 /**
11  * A ConnectionInfo that reads and writes from Channels.
12  */

13 public class ConnectionInfo
14     extends AbstractConnectionInfo
15 {
16     private WritableByteChannel out;
17     private ReadableByteChannel in;
18
19     private ReadWriteTransformThread writeHandler;
20
21     /**
22      * @param p the MessageProcessor that will handle datagrams as they are read in
23      * from the read buffer.
24      * @param f the DatagramFactory that will process raw byte streams and perform
25      * framing and interpretation.
26      */

27     public ConnectionInfo(IMessageProcessor p,
28                           IDatagramFactory f)
29     {
30         super(p,f);
31     }
32
33     void setIOHandler(ReadWriteTransformThread rwtt, int operation)
34     {
35         if (operation == SelectionKey.OP_WRITE)
36             this.writeHandler = rwtt;
37         else if (operation == SelectionKey.OP_READ)
38             rwtt.requestService(this);
39     }
40
41     public void start()
42     {
43         shouldProcess = true;
44     }
45
46     public void stop()
47     {
48         shouldProcess = false;
49     }
50
51     public ReadableByteChannel in() {return in;}
52     public WritableByteChannel out() {return out;}
53
54     /**
55      * Attaches an input and output channel to this connection. They may be
56      * the same object, if the channel implements both readable and writable
57      * interfaces.
58      */

59     public void attach(ReadableByteChannel in, WritableByteChannel out)
60     {
61         this.in = in;
62         this.out = out;
63     }
64
65     /**
66      * Overrides the <code>requestWrite</code> method to request a callback
67      * on the write thread when the connection is writeable. This is
68      * typically immediate - however, it is important <B>NOT</b> to fall into
69      * the trap of writing from the calling thread! In order to preserve
70      * the integrity of the output buffer, and in order to protect the ordering
71      * of packets as atomic units we must have a single thread of operation
72      * performing all NB write operations.<P>
73      *
74      * If writes do occur here, you will see nasty buffer corruption, overruns
75      * underruns and protocol violations.<P>
76      *
77      * @author JP
78      */

79     protected void requestWrite()
80         throws IOException
81     {
82         if (writeHandler == null)
83             throw new IllegalStateException JavaDoc("writeHandler cannot be null in requestWrite()");
84         
85         if (super.readyToWrite())
86         {
87             writeHandler.requestService(this);
88         }
89     }
90
91     protected void cancelWriteRequest()
92     {
93         if (writeHandler == null)
94             throw new IllegalStateException JavaDoc("writeHandler cannot be null in cancelWriteRequest()");
95         
96         writeHandler.cancelServiceRequest(this);
97     }
98
99     public int doWrite(ByteBuffer writeBuffer)
100         throws java.io.IOException JavaDoc
101     {
102         return out().write(writeBuffer);
103     }
104
105     /**
106      * Reads from the specified byte channel into the read buffer. This method
107      * is usually called by a dedicated I/O service thread when the channel
108      * has indicated that it has data to be consumed.
109      */

110     void readFrom(ReadableByteChannel channel,
111                   SelectionKey key)
112     {
113         ByteBuffer readBuffer = null;
114         try
115         {
116             readBuffer = getReadBuffer();
117             int n = channel.read(readBuffer);
118
119             // if were are at End Of Stream, we cancel
120
// the read selection key.
121
if (n == -1)
122             {
123                 key.cancel();
124
125                 // close the channels
126
close();
127             }
128         }
129         catch(java.io.IOException JavaDoc iox)
130         {
131             sendEvent(ConnectionEvent.CONNECTION_IO_EXCEPTION);
132             close();
133         }
134         catch(InterruptedException JavaDoc ie)
135         {
136             // return to caller
137
return;
138         }
139         finally
140         {
141             releaseReadBuffer(readBuffer);
142         }
143
144         // process the data
145
processData();
146     }
147 }
148
Popular Tags