Remove TODO file
[remote/remote-gui.git] / src / motecontrol / client / SimpleMote.java
blobd8c71e5d653dcb7d0df2aedabf433fa57ec8d172
1 package diku.distlab.motecontrolclientlib.client;
2 import diku.distlab.motecontrolclientlib.protocols.*;
4 import java.io.*;
6 public class SimpleMote extends Mote {
8 protected SimpleMoteStatus status;
9 protected SimpleMoteManager moteManager;
10 protected PipedOutputStream dataOutput;
11 protected Thread dataThread;
13 protected SimpleMote(long id,SimpleMoteManager moteManager) {
14 super(id);
15 this.moteManager = moteManager;
16 this.status = new SimpleMoteStatus(this);
19 public SimpleMoteStatus getStatus() {
20 return status;
23 protected void dataOut(MsgPayload data)
25 if (dataOutput != null)
27 try
29 byte[] buf = data.getData();
30 dataOutput.write(buf);
31 dataOutput.flush();
33 catch (Exception e)
35 //e.printStackTrace();
36 try
38 dataOutput.close();
40 catch (IOException e2){ e2.printStackTrace();}
41 finally
43 dataOutput = null;
51 public void connectDataSource(final InputStream dataInput)
53 // create a thread reading from the stream and creating data output
54 Runnable r = new Runnable() {
55 public void run() {
56 BufferedInputStream input = new BufferedInputStream(dataInput);
57 byte[] bytes;
58 long skipped;
59 try{
60 while(true)
62 input.mark(200);
63 skipped = input.skip(199);
64 bytes = new byte[(int)skipped];
65 input.reset();
66 input.read(bytes);
67 moteManager.sendData(id,bytes);
70 catch (Exception e)
72 e.printStackTrace();
77 dataThread = new Thread(r);
78 dataThread.start();
81 public void connectDataSource(final PipedOutputStream source)
83 // create a thread reading from the stream and creating data output
84 Runnable r = new Runnable() {
85 public void run() {
86 try{
87 PipedInputStream dataInput = new PipedInputStream(source);
88 BufferedInputStream input = new BufferedInputStream(dataInput);
89 byte[] bytes;
90 long skipped;
91 while(true)
93 input.mark(200);
94 skipped = input.skip(199);
95 bytes = new byte[(int)skipped];
96 input.reset();
97 input.read(bytes);
98 moteManager.sendData(id,bytes);
101 catch (Exception e) {e.printStackTrace();}
105 dataThread = new Thread(r);
106 dataThread.start();
109 public void connectDataSink(PipedInputStream sink) throws IOException
113 if (this.dataOutput != null) this.dataOutput.close();
115 catch (Exception e) {e.printStackTrace();}
116 finally
118 this.dataOutput = new PipedOutputStream(sink);