Is it possible to implement a kind of listener for Probe-Messages? I touhgt of something like the helloReceived()-function. Or do I have to implement a extra Message-Listener in my Device-Class with a handle-function?
Regards,
Robert
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
for this there is the MessageInformer. It takes a MessageListener and a MessageSelector to register to be informed about incoming/outgoing messages. The MessageSelector decides if the message will be delivered to the supplied MessageListener.
Here is some sample code:
publicclassExample{publicstaticvoidmain(String[]args)throwsWS4DIllegalStateException,IOException{JMEDSFramework.start(null);Log.setLogLevel(Log.DEBUG_LEVEL_ERROR);DefaultDevicetestDevice=newDefaultDevice(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);testDevice.start();MessageListeneryourDPWSMessageListener=newDPWSMessageListener();// Registering our listener with a selector that only accepts Probe messages. There is also the// AllMessageSelector. It can be used when we want to receive all messagesMessageInformer.getInstance().addMessageListener(yourDPWSMessageListener,newMessageSelector() {@Overridepublicbooleanmatches(Messagemsg){returnmsg.getType()==MessageConstants.PROBE_MESSAGE;}});}}classDPWSMessageListenerimplementsMessageListener{@OverridepublicvoidreceivedInboundMessage(Messagemsg,ConnectionInfoconnectionInfo,AttributedURIoptionalMessageId){// Our message selector has already determined that the message is a probeLog.error("Received Probe Message: "+msg.toString());}@OverridepublicvoidreceivedOutboundMessage(Messagemsg,ConnectionInfoconnectionInfo,AttributedURIoptionalMessageId){// we do not really care about those}}
I hope this helps you.
Best,
Jannis
Last edit: Jannis Müthing 2014-06-04
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
thank you very much! This advice was very helpful. But if I use the AllMessageSelector I don't get Hello-Messages, is that right? So I need the DefultClient with helloReceived, too.
Regards
Robert
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The MessageInformer only farwards all messages processed by JMEDS. Without anyone registered for hello message listening the stack will not process them and the message informer will not get its callback. So you could either make a default client and register it for hello listening:
...
JMEDSFramework.start(null);
Log.setLogLevel(Log.DEBUG_LEVEL_ERROR);
DefaultDevice testDevice = new DefaultDevice("DPWS");
testDevice.start();
DefaultClient client = new DefaultClient();
client.registerHelloListening();
MessageListener yourDPWSMessageListener = new DPWSMessageListener();
// Registering our listener with a selector that only accepts hello messages. There is also the
// AllMessageSelector. It can be used when we want to receive all messages
MessageInformer.getInstance().addMessageListener(yourDPWSMessageListener, new MessageSelector() {
@Override
public boolean matches(Message msg) {
return msg.getType() == MessageConstants.HELLO_MESSAGE;
}
});
...
Or if you want to register yourself directly you could do it without going through the message informer. It is a little more code though:
publicclassExample{publicstaticvoidmain(String[]args)throwsWS4DIllegalStateException,IOException{JMEDSFramework.start(null);CommunicationManagercommunicationManager=CommunicationManagerRegistry.getCommunicationManager(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);// the default auto bindings from the communication managerDiscoveryAutoBindingdiscoveryAutobinding=communicationManager.getAutoBindingFactory().createDiscoveryMulticastAutobinding();// an autobinding keeps its listener informed about changes on the network. We only need the listener to get to the actual discovery bindings.Listenerlistener=newListener();discoveryAutobinding.addAutoBindingListener(listener);// the message listenerDefaultIncomingMessageListenerhelloReceiver=newDefaultIncomingMessageListener(null){publicvoidhandle(HelloMessagehello,ConnectionInfoconnectionInfo){Log.error("Received hello: "+hello);}};// registering our interest in hello messages on all bindings in the autobinding. The autobinding can contain multiple bindings for multiple interfacesIteratorit=discoveryAutobinding.getDiscoveryBindings(listener);while(it.hasNext()){DiscoveryBindingb=(DiscoveryBinding)it.next();communicationManager.registerDiscovery(newint[]{MessageConstants.HELLO_MESSAGE},b,helloReceiver,null);}OutDispatcher.getInstance().registerHelloListener(helloReceiver);}}/** * Listener * We use this just to get the bindings from the autobinding. * In production code you might want to handle network changes (an interface going away or coming back etc.) in this listener! * * @author jmuethin */classListenerimplementsNetworkChangeListener,AutoBindingAndOutgoingDiscoveryInfoListener{publicvoidannounceNewCommunicationBindingAvailable(Bindingbinding,booleanisDiscovery){}publicvoidannounceNewDiscoveryBindingAvailable(DiscoveryBindingbinding,DiscoveryAutoBindingdab){}publicvoidannounceNewOutgoingDiscoveryInfoAvailable(OutgoingDiscoveryInfooutgoingDiscoveryInfo){}publicvoidannounceCommunicationBindingDestroyed(Bindingbinding,booleanisDiscovery){}publicvoidannounceDiscoveryBindingDestroyed(DiscoveryBindingbinding,DiscoveryAutoBindingdab){}publicvoidannounceOutgoingDiscoveryInfoDestroyed(OutgoingDiscoveryInfooutgoingDiscoveryInfo){}publicStringgetPath() {returnnull;}publicvoidannounceNewInterfaceAvailable(Objectiface){}publicvoidstartUpdates() {}publicvoidstopUpdates() {}}
Best,
Jannis
Last edit: Jannis Müthing 2014-06-11
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Next question is how to send a probeMatch manually? (if at all possible)
I built a message like this:
ProbeMatch pm = new ProbeMatch(device.getDiscoveryData());
ProbeMatchesMessage res = new ProbeMatchesMessage();
res.addProbeMatch(pm);
res.setResponseTo(probeMsg);
But how can I send this now?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can send any message using the internal send(...) methods in the communication manager (in your case the DPWSCommunicationManager). Specifically
public void send(Message message, XAddressInfo targetXAddrInfo, CredentialInfo credentialInfo) throws WS4DIllegalStateException
should do the job for you. That being said, this is not really an application the stack was designed for. As mentioned the send methods are internal and should be used with extreme care or better not at all. It looks like you are trying to reimplement functionality that should be the stacks responsibility.
Best,
Jannis
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello dear community,
Is it possible to implement a kind of listener for Probe-Messages? I touhgt of something like the helloReceived()-function. Or do I have to implement a extra Message-Listener in my Device-Class with a handle-function?
Regards,
Robert
I'm trying to implement my own IncomingMessageListener in my device-class now. It looks like this:
How can I achieve to use this listener instead of the DeviceMessageListener?
Last edit: Robert B. 2014-06-02
Hi Robert
for this there is the MessageInformer. It takes a MessageListener and a MessageSelector to register to be informed about incoming/outgoing messages. The MessageSelector decides if the message will be delivered to the supplied MessageListener.
Here is some sample code:
I hope this helps you.
Best,
Jannis
Last edit: Jannis Müthing 2014-06-04
Hello Jannis,
thank you very much! This advice was very helpful. But if I use the AllMessageSelector I don't get Hello-Messages, is that right? So I need the DefultClient with helloReceived, too.
Regards
Robert
Hi Robert!
The MessageInformer only farwards all messages processed by JMEDS. Without anyone registered for hello message listening the stack will not process them and the message informer will not get its callback. So you could either make a default client and register it for hello listening:
Or if you want to register yourself directly you could do it without going through the message informer. It is a little more code though:
Best,
Jannis
Last edit: Jannis Müthing 2014-06-11
We found an even better way to do this without using autobindings:
Best,
Jannis
Great! Thanks a lot for the support!
Ok this works fine now.
Next question is how to send a probeMatch manually? (if at all possible)
I built a message like this:
But how can I send this now?
Hi Robert!
You can send any message using the internal send(...) methods in the communication manager (in your case the DPWSCommunicationManager). Specifically
should do the job for you. That being said, this is not really an application the stack was designed for. As mentioned the send methods are internal and should be used with extreme care or better not at all. It looks like you are trying to reimplement functionality that should be the stacks responsibility.
Best,
Jannis