SecondaryClient.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package at.acdp.urweb.sclient;
  2. import at.acdp.urweb.sclient.data.VersionMessage;
  3. import java.io.*;
  4. import java.net.Socket;
  5. import java.nio.charset.StandardCharsets;
  6. public class SecondaryClient {
  7. private final String ip;
  8. private final int port;
  9. private volatile boolean _running = true;
  10. private Socket rt;
  11. private OutputStream os;
  12. private DataInputStream in;
  13. public SecondaryClient(String ip, int port) {
  14. this.ip = ip;
  15. this.port = port;
  16. }
  17. public void connect() throws IOException {
  18. this.rt = new Socket(ip, port);
  19. this.os = rt.getOutputStream();
  20. this.in = new DataInputStream(rt.getInputStream());
  21. VersionMessage vm = new VersionMessage();
  22. vm.readVersionMessage(in);
  23. System.out.println(vm);
  24. readReply(in);
  25. }
  26. private void readReply(DataInputStream di) throws IOException {
  27. int size=di.readInt();
  28. int mType= di.readByte() &0xff;
  29. switch (mType) {
  30. case 20:
  31. long ts=di.readLong();
  32. byte source=di.readByte();
  33. int robotMessageType=di.readByte() & 0xff;
  34. switch(robotMessageType) {
  35. default:
  36. System.out.println("rtype: " +robotMessageType);
  37. int x=di.available();
  38. byte[] buf=new byte[size-15];
  39. di.read(buf);
  40. System.out.println(buf);
  41. }
  42. break;
  43. case 16:
  44. System.out.println("16 msg" + mType);
  45. int remaining=size-5;
  46. while (remaining >0) {
  47. int sublength = in.readInt();
  48. remaining-=sublength;
  49. System.out.println("sublength: " + sublength);
  50. int subType = in.readByte();
  51. System.out.println("subtype: " + subType);
  52. byte[] res = new byte[sublength - 5];
  53. in.read(res);
  54. }
  55. break;
  56. default:
  57. System.out.println("unknown msg" + mType);
  58. byte[] buf=new byte[size];
  59. di.read(buf);
  60. }
  61. }
  62. public void writeCmd(String cmd) {
  63. try {
  64. System.out.println("send cmd:" + cmd);
  65. os.write(cmd.getBytes(StandardCharsets.UTF_8));
  66. readReply(in);
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }