diff --git a/java/org/servalproject/servaldna/AbstractMdpProtocol.java b/java/org/servalproject/servaldna/AbstractMdpProtocol.java index aa35ce8f..c489eefc 100644 --- a/java/org/servalproject/servaldna/AbstractMdpProtocol.java +++ b/java/org/servalproject/servaldna/AbstractMdpProtocol.java @@ -35,6 +35,15 @@ public abstract class AbstractMdpProtocol extends ChannelSelector.Handler { socket.close(); } + public void rebind() throws IOException { + selector.unregister(this); + try{ + socket.rebind(); + }finally{ + selector.register(this); + } + } + protected abstract void parse(MdpPacket response); @Override diff --git a/java/org/servalproject/servaldna/MdpSocket.java b/java/org/servalproject/servaldna/MdpSocket.java index 41a28123..dd868643 100644 --- a/java/org/servalproject/servaldna/MdpSocket.java +++ b/java/org/servalproject/servaldna/MdpSocket.java @@ -51,6 +51,8 @@ public class MdpSocket{ public synchronized void bind(SubscriberId sid, int port) throws IOException { if (loopbackMdpPort==0) throw new IOException("Loopback MDP port has not been set"); + if (sid==null) + throw new NullPointerException(); if (sid.equals(this.sid) && this.port == port) return; if (this.sid!=null) @@ -80,6 +82,14 @@ public class MdpSocket{ this.port = packet.getLocalPort(); } + public void rebind() throws IOException{ + if (this.sid==null) + return; + SubscriberId sid = this.sid; + this.sid=null; + bind(sid, this.port); + } + public SelectableChannel getChannel() throws IOException { if (channel == null){ channel = DatagramChannel.open(); @@ -114,6 +124,7 @@ public class MdpSocket{ // ignore errors due to servald stopping. e.printStackTrace(); } + sid = null; } if (channel!=null){ try {