I am writing an mxj external that needs to communicate with a TCP Server, sending and receiving data. The server is a custom hardware system. It is *not* something from the Max/MSP world, it knows nothing about atoms, it uses a binary format that I have to adhere to. So no [mxj net.send], no [mxj net.receive], and I cannot use the TcpReceiver/TcpSender classes.
Sending data is OK. I have written test code that simply blocks while waiting to receive a reply in response to the sent packet and that works. But I cannot afford to block and I need to be well-behaved if the host dies or a LAN cable is broken. So I am trying to put the wait-for-response code into a Java Thread using the runnable Interface. This is where problems begin.
What the attached code is supposed to do:
Initialization: allocate a buffer as a template for data to send over TCP. Some other standard stuff.
- Check if we're connected. If necessary, build a connection.
- Send data to server
- Start thread to listen for a response
- allocate buffer to store data once it's come in over the network.
- If the data has arrived:
...then parse it
...else increment a counter and sleep for a while; punt if the counter indicates a time out.
Does this make sense? What is the code doing wrong? The attachment has been stripped of the gory details of the data formats, otherwise the real thing. In my real life example the code hits the timeout at the outlet(0, -1) statement (line 171).
I greatly appreciate the help.