I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit f56ac66d authored by zzz's avatar zzz
Browse files

Make DatagramServer a Handler, register with bridge

parent c662f178
No related branches found
No related tags found
No related merge requests found
...@@ -32,18 +32,21 @@ import net.i2p.util.Log; ...@@ -32,18 +32,21 @@ import net.i2p.util.Log;
* *
* @since 0.9.22 moved from SAMv3Handler * @since 0.9.22 moved from SAMv3Handler
*/ */
class SAMv3DatagramServer { class SAMv3DatagramServer implements Handler {
private static SAMv3DatagramServer _instance; private static SAMv3DatagramServer _instance;
private static DatagramChannel server; private static DatagramChannel server;
private final Thread _listener;
private final SAMBridge _parent;
/** /**
* Returns the singleton. * Returns the singleton.
* If this is the first call, will be instantiated and will listen * If this is the first call, will be instantiated and will listen
* on the default host:port 127.0.0.1:7655. * on the default host:port 127.0.0.1:7655.
* Don't make this the first call.
*/ */
public static SAMv3DatagramServer getInstance() throws IOException { public static SAMv3DatagramServer getInstance() throws IOException {
return getInstance(new Properties()); return getInstance(null, new Properties());
} }
/** /**
...@@ -54,15 +57,24 @@ class SAMv3DatagramServer { ...@@ -54,15 +57,24 @@ class SAMv3DatagramServer {
* *
* @param props ignored unless this is the first call * @param props ignored unless this is the first call
*/ */
public static SAMv3DatagramServer getInstance(Properties props) throws IOException { public static SAMv3DatagramServer getInstance(SAMBridge parent, Properties props) throws IOException {
synchronized(SAMv3DatagramServer.class) { synchronized(SAMv3DatagramServer.class) {
if (_instance==null) if (_instance==null) {
_instance = new SAMv3DatagramServer(props); _instance = new SAMv3DatagramServer(parent, props);
return _instance ; _instance.start();
}
} }
return _instance;
} }
private SAMv3DatagramServer(Properties props) throws IOException { /**
* Does not start listener.
* Caller must call start().
*
* @param parent may be null
*/
private SAMv3DatagramServer(SAMBridge parent, Properties props) throws IOException {
_parent = parent;
synchronized(SAMv3DatagramServer.class) { synchronized(SAMv3DatagramServer.class) {
if (server==null) if (server==null)
server = DatagramChannel.open(); server = DatagramChannel.open();
...@@ -78,13 +90,41 @@ class SAMv3DatagramServer { ...@@ -78,13 +90,41 @@ class SAMv3DatagramServer {
} }
server.socket().bind(new InetSocketAddress(host, port)); server.socket().bind(new InetSocketAddress(host, port));
new I2PAppThread(new Listener(server), "DatagramListener").start(); _listener = new I2PAppThread(new Listener(server), "SAM DatagramListener " + port);
}
/**
* Only call once.
* @since 0.9.22
*/
public void start() {
_listener.start();
if (_parent != null)
_parent.register(this);
}
/**
* Cannot be restarted.
* @since 0.9.22
*/
public void stopHandling() {
synchronized(SAMv3DatagramServer.class) {
if (server != null) {
try {
server.close();
} catch (IOException ioe) {}
server = null;
}
}
_listener.interrupt();
if (_parent != null)
_parent.unregister(this);
} }
public void send(SocketAddress addr, ByteBuffer msg) throws IOException { public void send(SocketAddress addr, ByteBuffer msg) throws IOException {
server.send(msg, addr); server.send(msg, addr);
} }
private static class Listener implements Runnable { private static class Listener implements Runnable {
private final DatagramChannel server; private final DatagramChannel server;
......
...@@ -520,12 +520,12 @@ class SAMv3Handler extends SAMv1Handler ...@@ -520,12 +520,12 @@ class SAMv3Handler extends SAMv1Handler
// Create the session // Create the session
if (style.equals("RAW")) { if (style.equals("RAW")) {
SAMv3DatagramServer.getInstance(i2cpProps); SAMv3DatagramServer.getInstance(bridge, i2cpProps);
SAMv3RawSession v3 = newSAMRawSession(nick); SAMv3RawSession v3 = newSAMRawSession(nick);
rawSession = v3; rawSession = v3;
this.session = v3; this.session = v3;
} else if (style.equals("DATAGRAM")) { } else if (style.equals("DATAGRAM")) {
SAMv3DatagramServer.getInstance(i2cpProps); SAMv3DatagramServer.getInstance(bridge, i2cpProps);
SAMv3DatagramSession v3 = newSAMDatagramSession(nick); SAMv3DatagramSession v3 = newSAMDatagramSession(nick);
datagramSession = v3; datagramSession = v3;
this.session = v3; this.session = v3;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment