1:
38:
39:
40: package ;
41:
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: public class UnicastConnection
53: implements Runnable, ProtocolConstants {
54:
55: UnicastConnectionManager manager;
56: Socket sock;
57: DataInputStream din;
58: DataOutputStream dout;
59: ObjectInputStream oin;
60: ObjectOutputStream oout;
61:
62:
63: long reviveTime = 0;
64: long expireTime = Long.MAX_VALUE;
65:
66: UnicastConnection(UnicastConnectionManager man, Socket sock) {
67: this.manager = man;
68: this.sock = sock;
69: }
70:
71: void acceptConnection() throws IOException {
72:
73:
74: din = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
75: dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
76:
77: int sig = din.readInt();
78: if (sig != PROTOCOL_HEADER) {
79: throw new IOException("bad protocol header");
80: }
81: short ver = din.readShort();
82: if (ver != PROTOCOL_VERSION) {
83: throw new IOException("bad protocol version");
84: }
85: int protocol = din.readUnsignedByte();
86: if (protocol != SINGLE_OP_PROTOCOL) {
87:
88: dout.writeByte(PROTOCOL_ACK);
89:
90:
91: dout.writeUTF(manager.serverName);
92: dout.writeInt(manager.serverPort);
93: dout.flush();
94:
95:
96: String rhost = din.readUTF();
97: int rport = din.readInt();
98: }
99:
100: }
101:
102: void makeConnection(int protocol) throws IOException {
103:
104: din = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
105:
106: dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
107:
108:
109: dout.writeInt(PROTOCOL_HEADER);
110: dout.writeShort(PROTOCOL_VERSION);
111: dout.writeByte(protocol);
112: dout.flush();
113:
114: if (protocol != SINGLE_OP_PROTOCOL) {
115:
116: int ack = din.readUnsignedByte();
117: if (ack != PROTOCOL_ACK) {
118: throw new RemoteException("Unsupported protocol");
119: }
120:
121:
122: String dicard_rhost = din.readUTF();
123: int discard_rport = din.readInt();
124:
125:
126: dout.writeUTF(manager.serverName);
127: dout.writeInt(manager.serverPort);
128: dout.flush();
129: }
130:
131: }
132:
133: DataInputStream getDataInputStream() throws IOException {
134: return (din);
135: }
136:
137: DataOutputStream getDataOutputStream() throws IOException {
138: return (dout);
139: }
140:
141:
146: ObjectInputStream getObjectInputStream() throws IOException {
147: if (oin == null) {
148: throw new IOException("no ObjectInputtream for reading more objects");
149: }
150: return (oin);
151: }
152:
153:
158: ObjectInputStream startObjectInputStream() throws IOException {
159: return (oin = new RMIObjectInputStream(din));
160: }
161:
162:
167: ObjectOutputStream getObjectOutputStream() throws IOException {
168: if (oout == null) {
169: throw new IOException("no ObjectOutputStream for sending more objects");
170: }
171: return (oout);
172: }
173:
174:
179: ObjectOutputStream startObjectOutputStream() throws IOException {
180: return (oout = new RMIObjectOutputStream(dout));
181: }
182:
183: void disconnect() {
184: try {
185: if(oout != null)
186: oout.close();
187: sock.close();
188: }
189: catch (IOException _) {
190: }
191:
192: oin = null;
193: oout = null;
194: din = null;
195: dout = null;
196: sock = null;
197: }
198:
199: public static final long CONNECTION_TIMEOUT = 10000L;
200:
201: static boolean isExpired(UnicastConnection conn, long l){
202: if (l <= conn.expireTime )
203: return false;
204: return true;
205: }
206:
207: static void resetTime(UnicastConnection conn){
208: long l = System.currentTimeMillis();
209: conn.reviveTime = l;
210: conn.expireTime = l + CONNECTION_TIMEOUT;
211: }
212:
213:
216: public void run() {
217: do{
218: try {
219: UnicastServer.dispatch(this);
220:
221:
222:
223: } catch (Exception e ){
224: manager.discardConnection(this);
225: break;
226: }
227: }while(true);
228: }
229:
230:
231: }