12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package at.acdp.urweb.sclient;
- import at.acdp.urweb.sclient.data.VersionMessage;
- import java.io.*;
- import java.net.Socket;
- import java.nio.charset.StandardCharsets;
- public class SecondaryClient {
- private final String ip;
- private final int port;
- private volatile boolean _running = true;
- private Socket rt;
- private OutputStream os;
- private DataInputStream in;
- public SecondaryClient(String ip, int port) {
- this.ip = ip;
- this.port = port;
- }
- public void connect() throws IOException {
- this.rt = new Socket(ip, port);
- this.os = rt.getOutputStream();
- this.in = new DataInputStream(rt.getInputStream());
- VersionMessage vm = new VersionMessage();
- vm.readVersionMessage(in);
- System.out.println(vm);
- readReply(in);
- }
- private void readReply(DataInputStream di) throws IOException {
- int size=di.readInt();
- int mType= di.readByte() &0xff;
- switch (mType) {
- case 20:
- long ts=di.readLong();
- byte source=di.readByte();
- int robotMessageType=di.readByte() & 0xff;
- switch(robotMessageType) {
- default:
- System.out.println("rtype: " +robotMessageType);
- int x=di.available();
- byte[] buf=new byte[size-15];
- di.read(buf);
- System.out.println(buf);
- }
- break;
- case 16:
- System.out.println("16 msg" + mType);
- int remaining=size-5;
- while (remaining >0) {
- int sublength = in.readInt();
- remaining-=sublength;
- System.out.println("sublength: " + sublength);
- int subType = in.readByte();
- System.out.println("subtype: " + subType);
- byte[] res = new byte[sublength - 5];
- in.read(res);
- }
- break;
- default:
- System.out.println("unknown msg" + mType);
- byte[] buf=new byte[size];
- di.read(buf);
- }
- }
- public void writeCmd(String cmd) {
- try {
- System.out.println("send cmd:" + cmd);
- os.write(cmd.getBytes(StandardCharsets.UTF_8));
- readReply(in);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|