I'm writing some code to receive an Inputstream from a node. for this I use a ProcessBuilder which starts up my coap-client to be able to receive the packets.
I buffer all my input. check code below:
This is my main code. The code that reads out the buffer is listed below:command = path + "./coap-client -m GET -p 61616 coap://[aaaa::c30c:0:0:379]/notifications -s 90"; try { synchronized(command) { final ProcessBuilder pb = new ProcessBuilder("bash", "-c", command); // merge child's error and normal output streams. // Note it is not called setRedirectErrorStream. pb.redirectErrorStream( true ); //p = Runtime.getRuntime().exec(command); p = pb.start(); System.out.println("process started"); } InputStream is = p.getInputStream(); new Thread( new Receiver( is ) ).start(); try { p.waitFor(); } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally {} }
public void run() { String line; final BufferedReader br = new BufferedReader( new InputStreamReader( is ), 100); /* keep small for testing */ line = br.readLine(); System.out.println(line); try { while ( ( line = br.readLine() ) != null ) { System.out.println( line ); line = br.readLine(); } br.close(); } catch (IOException e) { e.printStackTrace(); } }
The packages I receive in my buffer are JSON formatted packets. I'm able to display those packets on the eclipse console but they appear with a 1 packet delay.
When I start my program I should immediately get the acknowledge package from the node. But the problem is that I only get my Ack-packet when a new notification has arrived in the buffer... I've looked for delays in the readLine() function but I haven't found any solution yet.
please help me out here...