Hello. I tried for the first time UDP with Grizzly, but my test failed. I
need to know if what I'm doing is supported in Grizzly.
I'm sending a UDP and I'm waiting for the response. Here a testcase that
works
DatagramSocket ssdpUniSock = new DatagramSocket(null);
// on va envoyer un M-SEARCH
StringBuffer sb = new StringBuffer();
sb.append("M-SEARCH * HTTP/1.1").append("\r\n");
sb.append("HOST: 239.255.255.250:1900").append("\r\n");
sb.append("MAN: \"ssdp:discover\"").append("\r\n");
sb.append("MX: 3").append("\r\n");
sb.append("ST: upnp:rootdevice").append("\r\n");
sb.append("\r\n");
String msg = sb.toString();
InetAddress inetAddr = InetAddress.getByName("239.255.255.250");
DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(),
msg.length(), inetAddr, 1900);
ssdpUniSock.send(dgmPacket);
// wait for the answer
byte ssdvRecvBuf[] = new byte[1024];
DatagramPacket dgmPacket2 = new DatagramPacket(ssdvRecvBuf, 1024);
ssdpUniSock.receive(dgmPacket2);
int packetLen = dgmPacket2.getLength();
String packetData = new String(dgmPacket2.getData(), 0, packetLen);
System.out.println(packetData);
ssdpUniSock.close();
I try with Grizzly but I never saw an answer. I saw my message sent with
Wireshark.. but no answers.
I put debug on all the methods of ProtocolChainCallbackHandler, but I only
see the message for "On Connect..."
I suppose the Grizzly is waiting on the same port that the client sent the
message right ?
here my code for Grizzly test. (GrizzlyClientProtocolChain.java (the
client)
here the init method.
public void init() {
f_tg = new ThreadGroup("Client Threads");
final CountDownLatch started = new CountDownLatch(1);
controller = new Controller();
UDPSelectorHandler udpSelectorHandler = new
UDPSelectorHandler(true);
controller.addSelectorHandler(udpSelectorHandler);
BaseSelectionKeyHandler selectionKeyHandler = new
BaseSelectionKeyHandler();
// to be notify when a client/server close the connection
selectionKeyHandler.setConnectionCloseHandler(new
ConnectionCloseHandler() {
public void locallyClosed(SelectionKey key) {
if(s_logger.isDebugEnabled()){
s_logger.debug(key + " is being locally cancelled");
}
}
public void remotlyClosed(SelectionKey key) {
if(s_logger.isDebugEnabled()){
s_logger.debug(key + " is being remotly cancelled
(connection closed)");
}
}
});
udpSelectorHandler.setSelectionKeyHandler(selectionKeyHandler);
// STATE Listener
controller.addStateListener(new ControllerStateListenerAdapter() {
public void onException(Throwable e) {
s_logger.error("Grizzly controller exception:" +
e.getMessage());
}
public void onReady() {
if(s_logger.isDebugEnabled()){
s_logger.debug("Ready!");
}
started.countDown();
}
});
// the protocol chain
EchoQueryProtocolFilter protocolParser = new
EchoQueryProtocolFilter();
EchoQueryManagerFilter echoManagerFilter = new
EchoQueryManagerFilter(this);
final ProtocolChain protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(protocolParser);
protocolChain.addFilter(echoManagerFilter);
((DefaultProtocolChain) protocolChain).setContinuousExecution(true);
ProtocolChainInstanceHandler pciHandler = new
DefaultProtocolChainInstanceHandler() {
public boolean offer(ProtocolChain protocolChain) {
return false;
}
public ProtocolChain poll() {
return protocolChain;
}
};
controller.setProtocolChainInstanceHandler(pciHandler);
// start the client
new Thread(f_tg, controller).start();
try {
started.await();
} catch (Exception e) {
s_logger.error("Timeout in wait" + e.getMessage());
}
// extra stuff : Queue
blockingQueue = new LinkedBlockingQueue<QueueItem>();
queueConsumer = new QueueConsumer(this, blockingQueue);
new Thread(f_tg, queueConsumer,"QueueConsumer").start();
connector_handler = (UDPConnectorHandler)
controller.acquireConnectorHandler(Controller.Protocol.UDP);
}