Add Java API rebind for mdp sockets

This commit is contained in:
Jeremy Lakeman 2014-09-16 14:41:59 +09:30
parent 68bd515241
commit 8667060ae0
2 changed files with 20 additions and 0 deletions

View File

@ -35,6 +35,15 @@ public abstract class AbstractMdpProtocol<T> extends ChannelSelector.Handler {
socket.close(); socket.close();
} }
public void rebind() throws IOException {
selector.unregister(this);
try{
socket.rebind();
}finally{
selector.register(this);
}
}
protected abstract void parse(MdpPacket response); protected abstract void parse(MdpPacket response);
@Override @Override

View File

@ -51,6 +51,8 @@ public class MdpSocket{
public synchronized void bind(SubscriberId sid, int port) throws IOException { public synchronized void bind(SubscriberId sid, int port) throws IOException {
if (loopbackMdpPort==0) if (loopbackMdpPort==0)
throw new IOException("Loopback MDP port has not been set"); throw new IOException("Loopback MDP port has not been set");
if (sid==null)
throw new NullPointerException();
if (sid.equals(this.sid) && this.port == port) if (sid.equals(this.sid) && this.port == port)
return; return;
if (this.sid!=null) if (this.sid!=null)
@ -80,6 +82,14 @@ public class MdpSocket{
this.port = packet.getLocalPort(); 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 { public SelectableChannel getChannel() throws IOException {
if (channel == null){ if (channel == null){
channel = DatagramChannel.open(); channel = DatagramChannel.open();
@ -114,6 +124,7 @@ public class MdpSocket{
// ignore errors due to servald stopping. // ignore errors due to servald stopping.
e.printStackTrace(); e.printStackTrace();
} }
sid = null;
} }
if (channel!=null){ if (channel!=null){
try { try {