itm / wsn-device-drivers

Drivers for Wireless Sensor Network Devices
Other
6 stars 4 forks source link

Move the LockedInputStream management in the DeviceAsync #28

Closed mlegenhausen closed 13 years ago

mlegenhausen commented 13 years ago

Allows to remove the monitor method.

pfisterer commented 13 years ago

Hi Malte, having had a look at the LockedInputStream, I discovered some issues for code that uses this InputStream (e.g., OioWorker.java). This code will assume that the stream is closed when it is only "locked".

I think it would be best to rewrite the code to use a Piped(Input|Output)Stream (cf. PipedInputStream and an example at WisemlStreaming on how to use it).

You would then read from the device and write what you have read from the device to an outputstream that pipes it to an InputStream that is used by users reading from the device. When you "lock" the stream, it basically means that you don't write to the piped stream until you are done with the operation that requires the locking.

mlegenhausen commented 13 years ago

The only problem is that the stream does not block on the read method when no data is availble. Java 6 API Doc InputStream: http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#read() This is of cause a bug! A simple workaround would be simply to wait until new data is available.

The other approuches I had also in mind, but they seemed to me a little bit to complicated, cause I simply want to read from the source InputStream when no Operation is in the queue.

mlegenhausen commented 13 years ago

I added a new branch feature/issue28. Please review if the bug is gone.

mlegenhausen commented 13 years ago

From the JavaDoc of the read() method:

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

The previous implementation of the LockedInputStream returns always -1 when the stream was locked, but never blocks. So other applications that uses this InputStream thought that the stream is closed. The new version now blocks until data is available and returns -1 when the stream is closed.

mlegenhausen commented 13 years ago

Will be merged tomorrow in the in the developer branch when there are no more issues.

mlegenhausen commented 13 years ago

Merged in develop branch. Remote branch deleted.