Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create extensible service discovery protocol, with initial basic impl…
…ementation
- Loading branch information
Showing
10 changed files
with
279 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.servalproject.servaldna; | ||
|
||
import java.io.IOException; | ||
import java.nio.channels.SelectableChannel; | ||
import java.nio.channels.SelectionKey; | ||
|
||
/** | ||
* Created by jeremy on 8/05/14. | ||
*/ | ||
public abstract class AbstractMdpProtocol<T> extends ChannelSelector.Handler { | ||
private final ChannelSelector selector; | ||
protected final MdpSocket socket; | ||
protected final AsyncResult<T> results; | ||
|
||
public AbstractMdpProtocol(ChannelSelector selector, AsyncResult<T> results) throws IOException { | ||
socket = new MdpSocket(); | ||
socket.bind(); | ||
this.selector = selector; | ||
this.results = results; | ||
selector.register(this); | ||
} | ||
|
||
public void close(){ | ||
try { | ||
selector.unregister(this); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
socket.close(); | ||
} | ||
|
||
protected abstract void parse(MdpPacket response); | ||
|
||
@Override | ||
public void read() { | ||
try { | ||
MdpPacket response = new MdpPacket(); | ||
socket.receive(response); | ||
parse(response); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public SelectableChannel getChannel() throws IOException { | ||
return socket.getChannel(); | ||
} | ||
|
||
@Override | ||
public int getInterest() { | ||
return SelectionKey.OP_READ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.servalproject.servaldna; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Created by jeremy on 8/05/14. | ||
*/ | ||
public class MdpServiceLookup extends AbstractMdpProtocol<MdpServiceLookup.ServiceResult> { | ||
|
||
public static class ServiceResult extends Properties { | ||
public final SubscriberId subscriberId; | ||
public ServiceResult(SubscriberId subscriberId){ | ||
this.subscriberId = subscriberId; | ||
} | ||
|
||
public String toString(){ | ||
return "ServiceResult{subscriberId="+subscriberId+", "+super.toString()+"}"; | ||
} | ||
} | ||
|
||
public MdpServiceLookup(ChannelSelector selector, AsyncResult<ServiceResult> results) throws IOException { | ||
super(selector, results); | ||
} | ||
|
||
public void sendRequest(SubscriberId destination, String pattern) throws IOException { | ||
MdpPacket request = new MdpPacket(); | ||
if (destination.isBroadcast()) | ||
request.setFlags(MdpPacket.MDP_FLAG_NO_CRYPT); | ||
request.setRemoteSid(destination); | ||
request.setRemotePort(MdpPacket.MDP_PORT_SERVICE_DISCOVERY); | ||
request.payload.put(pattern.getBytes()); | ||
request.payload.put((byte)0); | ||
request.payload.flip(); | ||
socket.send(request); | ||
} | ||
|
||
public static class BuffStream extends InputStream{ | ||
private final ByteBuffer buff; | ||
public BuffStream(ByteBuffer buff){ | ||
this.buff = buff; | ||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if (!buff.hasRemaining()) | ||
return -1; | ||
return buff.get()&0xFF; | ||
} | ||
|
||
@Override | ||
public void mark(int readLimit){ | ||
buff.mark(); | ||
} | ||
|
||
@Override | ||
public void reset() throws IOException { | ||
buff.rewind(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
// noop | ||
} | ||
|
||
@Override | ||
public int read(byte[] dst, int dstOffset, int charCount) throws IOException { | ||
if (!buff.hasRemaining()) | ||
return -1; | ||
if (charCount > buff.remaining()) | ||
charCount = buff.remaining(); | ||
buff.get(dst, dstOffset, charCount); | ||
return charCount; | ||
} | ||
} | ||
|
||
@Override | ||
protected void parse(MdpPacket response) { | ||
try { | ||
ServiceResult result = new ServiceResult(response.getRemoteSid()); | ||
result.load(new BuffStream(response.payload)); | ||
results.result(result); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} catch (AbstractId.InvalidBinaryException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.