EOS 2  1.1.0
Einfache Objektbasierte Sprache
Connection.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.ev3.brick;
2 
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintStream;
6 import java.net.Socket;
7 import java.text.MessageFormat;
8 
9 import de.lathanda.eos.ev3.comm.Identification;
10 import de.lathanda.eos.ev3.exceptions.Ev3NotFoundException;
11 import de.lathanda.eos.ev3.exceptions.Ev3SeekerThread;
12 
13 public class Connection {
14  private static Ev3SeekerThread seeker = Ev3SeekerThread.getInstance();
15  private static String CONNECT_MSG = "GET /target?sn= {0} VMTP1.0\n Protocol: EV3";
16  private Socket ev3conn;
17  private PrintStream ev3out;
18  private InputStream ev3in;
19  private ReplyReceiver rr;
20  public void connect() throws IOException {
21  Identification ev3 = seeker.getAnyEv3();
22  if (ev3 == null) {
23  throw new Ev3NotFoundException();
24  }
25  connect(ev3);
26  }
27  public void connect(String id) throws IOException {
28  Identification ev3 = seeker.seekEv3(id);
29  if (ev3 == null) {
30  throw new Ev3NotFoundException(id);
31  }
32  connect(ev3);
33  }
34  public void connect(Identification ev3) throws IOException {
35  String msg = MessageFormat.format(CONNECT_MSG, ev3.getSn());
36  ev3conn = new Socket(ev3.getInetadress(), ev3.getPort());
37  ev3out = new PrintStream(ev3conn.getOutputStream());
38  ev3in = ev3conn.getInputStream();
39  ev3out.println(msg);
40  rr = new ReplyReceiver();
41  new Thread(rr).start();
42  }
43  private class ReplyReceiver implements Runnable {
44  private static final int LF = 10;
45  private volatile boolean running = true;
46  private ReplyReceiver() throws IOException {
47  StringBuilder answer = new StringBuilder();
48  int data = 0;
49  while ((data = ev3in.read()) != LF) {
50  answer.append((char)data);
51  }
52  System.out.println(answer);
53  }
54  @Override
55  public void run() {
56  //read initial message
57  while (running) {
58 
59  }
60  }
61 
62  }
63 }
void connect(Identification ev3)
Definition: Connection.java:34
Impressum