Problem:
Running BacnetTest.java on v1.3 fails with:
java.lang.IllegalStateException: Cannot send a request in the socket listener thread.
at com.serotonin.bacnet4j.npdu.ip.IpNetwork.checkSendThread(IpNetwork.java:153)
at com.serotonin.bacnet4j.transport.Transport.send(Transport.java:147)
at com.serotonin.bacnet4j.LocalDevice.send(LocalDevice.java:370)
at BacnetTest.getExtendedDeviceInformation(BacnetTest.java:74)
at BacnetTest$Listener.iAmReceived(BacnetTest.java:56)
at com.serotonin.bacnet4j.event.DeviceEventHandler.fireIAmReceived(DeviceEventHandler.java:95)
at com.serotonin.bacnet4j.service.unconfirmed.IAmRequest.handle(IAmRequest.java:92)
at com.serotonin.bacnet4j.transport.Transport.incomingApdu(Transport.java:535)
at com.serotonin.bacnet4j.npdu.IncomingRequestParser.run(IncomingRequestParser.java:44)
at com.serotonin.bacnet4j.npdu.ip.IpNetwork.run(IpNetwork.java:243)
at java.lang.Thread.run(Thread.java:724)
Solution:
Run the sending message on a different thread.
Suggested changes to achieve Solution:
Change line 242 in com.serotonin.bacnet4j.npdu.ip.IpNetwork to execute in a separate thread, viz:
242:- new IncomingMessageExecutor(this, queue, link).run();
242:+ new Thread(new IncomingMessageExecutor(this, queue, link)).start();
and make inner class IncomingMessageExecutor implement Runnable, viz:
261:- class IncomingMessageExecutor extends IncomingRequestParser {
261:+ class IncomingMessageExecutor extends IncomingRequestParser implements Runnable {
Also, I came across the equality test below which really should be changed because testing for object reference equality is not strong enough, it needs to be testing for object identity equality:
152:- if (Thread.currentThread() == thread)
152:+ if (thread != null && thread.equals(Thread.currentThread()))
With these changes, the BacnetTest was successful.
Cheers,
Brenton
This is not necessary since Thread does not implement it's own equals method. '==' equality is sufficient.