44 |
import java.io.DataOutputStream; |
import java.io.DataOutputStream; |
45 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
46 |
import java.io.ObjectOutputStream; |
import java.io.ObjectOutputStream; |
47 |
|
import java.io.BufferedInputStream; |
48 |
|
import java.io.BufferedOutputStream; |
49 |
import java.io.ObjectOutput; |
import java.io.ObjectOutput; |
50 |
import java.io.ObjectInput; |
import java.io.ObjectInput; |
51 |
import java.io.IOException; |
import java.io.IOException; |
67 |
} |
} |
68 |
|
|
69 |
void acceptConnection() throws IOException { |
void acceptConnection() throws IOException { |
70 |
//System.out.println("Accepting connection on " + lport); |
//System.out.println("Accepting connection on " + sock); |
71 |
din = new DataInputStream(sock.getInputStream()); |
//Use BufferedXXXStream would be more efficient |
72 |
dout = new DataOutputStream(sock.getOutputStream()); |
din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); |
73 |
|
dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); |
74 |
|
|
75 |
int sig = din.readInt(); |
int sig = din.readInt(); |
76 |
if (sig != PROTOCOL_HEADER) { |
if (sig != PROTOCOL_HEADER) { |
88 |
// Send my hostname and port |
// Send my hostname and port |
89 |
dout.writeUTF(manager.serverName); |
dout.writeUTF(manager.serverName); |
90 |
dout.writeInt(manager.serverPort); |
dout.writeInt(manager.serverPort); |
91 |
|
dout.flush(); |
92 |
|
|
93 |
// Read their hostname and port |
// Read their hostname and port |
94 |
String rhost = din.readUTF(); |
String rhost = din.readUTF(); |
98 |
} |
} |
99 |
|
|
100 |
void makeConnection(int protocol) throws IOException { |
void makeConnection(int protocol) throws IOException { |
101 |
dout = new DataOutputStream(sock.getOutputStream()); |
//Use BufferedXXXStream would be more efficient |
102 |
din = new DataInputStream(sock.getInputStream()); |
din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); |
103 |
|
dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); |
104 |
|
|
105 |
// Send header |
// Send header |
106 |
dout.writeInt(PROTOCOL_HEADER); |
dout.writeInt(PROTOCOL_HEADER); |
107 |
dout.writeShort(PROTOCOL_VERSION); |
dout.writeShort(PROTOCOL_VERSION); |
108 |
dout.writeByte(protocol); |
dout.writeByte(protocol); |
109 |
dout.flush(); |
dout.flush(); |
110 |
|
|
111 |
if (protocol != SINGLE_OP_PROTOCOL) { |
if (protocol != SINGLE_OP_PROTOCOL) { |
112 |
// Get back ack. |
// Get back ack. |
113 |
int ack = din.readUnsignedByte(); |
int ack = din.readUnsignedByte(); |
122 |
// Send them my endpoint |
// Send them my endpoint |
123 |
dout.writeUTF(manager.serverName); |
dout.writeUTF(manager.serverName); |
124 |
dout.writeInt(manager.serverPort); |
dout.writeInt(manager.serverPort); |
125 |
|
dout.flush(); |
126 |
} |
} |
127 |
// Okay, ready to roll ... |
// Okay, ready to roll ... |
128 |
} |
} |
150 |
} |
} |
151 |
|
|
152 |
void disconnect() { |
void disconnect() { |
|
oin = null; |
|
|
oout = null; |
|
153 |
try { |
try { |
154 |
sock.close(); |
if(oout != null) |
155 |
|
oout.close(); |
156 |
} |
} |
157 |
catch (IOException _) { |
catch (IOException _) { |
158 |
} |
} |
159 |
|
|
160 |
|
oin = null; |
161 |
|
oout = null; |
162 |
din = null; |
din = null; |
163 |
dout = null; |
dout = null; |
164 |
sock = null; |
sock = null; |