blob: 56c8b40e60f48e88694b021b09166c8ddd423ec1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package derms.net.il;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class IL {
public static Connection listen(InetAddress laddr, int lport) {
// TODO
}
public static Connection connect(InetAddress raddr, int rport) throws IOException {
DatagramSocket sock = new DatagramSocket();
sock.connect(raddr, rport);
InetAddress laddr = sock.getLocalAddress();
int lport = sock.getLocalPort();
Connection conn = new Connection(laddr, lport, raddr, rport, sock);
conn.sendCtl(Type.sync, conn.id0, 0);
conn.state.set(State.syncer);
conn.start();
while (conn.state.get() == State.syncer)
Thread.yield();
State state = conn.state.get();
switch (state) {
case established:
return conn;
case closed:
conn.close();
throw new IOException("failed to connect to " + raddr + ":" + rport);
default:
conn.close();
throw new IllegalStateException("illegal connection state: " + state);
}
}
}
|