blob: 6aa4e482e6c92edb67e11bb596da9839eb8ccf81 (
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
38
39
|
package derms.net.runicast;
import derms.net.ConcurrentDatagramSocket;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
/** TODO */
public class ReliableUnicast {
public static Connection listen(InetAddress laddr, int lport) {
// TODO
}
public static Connection connect(InetAddress raddr, int rport) throws IOException {
ConcurrentDatagramSocket sock = new ConcurrentDatagramSocket();
sock.connect(raddr, rport);
InetAddress laddr = sock.getLocalAddress();
int lport = sock.getLocalPort();
Connection conn = new Connection(laddr, lport, raddr, rport, sock);
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);
}
}
}
|