API.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. *
  3. * Copyright (C) Andrew Smith 2012-2013
  4. *
  5. * Usage: java API command ip port
  6. *
  7. * If any are missing or blank they use the defaults:
  8. *
  9. * command = 'summary'
  10. * ip = '127.0.0.1'
  11. * port = '4028'
  12. *
  13. */
  14. import java.net.*;
  15. import java.io.*;
  16. class API
  17. {
  18. static private final int MAXRECEIVESIZE = 65535;
  19. static private Socket socket = null;
  20. private void closeAll() throws Exception
  21. {
  22. if (socket != null)
  23. {
  24. socket.close();
  25. socket = null;
  26. }
  27. }
  28. public void display(String result) throws Exception
  29. {
  30. String value;
  31. String name;
  32. String[] sections = result.split("\\|", 0);
  33. for (int i = 0; i < sections.length; i++)
  34. {
  35. if (sections[i].trim().length() > 0)
  36. {
  37. String[] data = sections[i].split(",", 0);
  38. for (int j = 0; j < data.length; j++)
  39. {
  40. String[] nameval = data[j].split("=", 2);
  41. if (j == 0)
  42. {
  43. if (nameval.length > 1
  44. && Character.isDigit(nameval[1].charAt(0)))
  45. name = nameval[0] + nameval[1];
  46. else
  47. name = nameval[0];
  48. System.out.println("[" + name + "] =>");
  49. System.out.println("(");
  50. }
  51. if (nameval.length > 1)
  52. {
  53. name = nameval[0];
  54. value = nameval[1];
  55. }
  56. else
  57. {
  58. name = "" + j;
  59. value = nameval[0];
  60. }
  61. System.out.println(" ["+name+"] => "+value);
  62. }
  63. System.out.println(")");
  64. }
  65. }
  66. }
  67. public void process(String cmd, InetAddress ip, int port) throws Exception
  68. {
  69. StringBuffer sb = new StringBuffer();
  70. char buf[] = new char[MAXRECEIVESIZE];
  71. int len = 0;
  72. System.out.println("Attempting to send '"+cmd+"' to "+ip.getHostAddress()+":"+port);
  73. try
  74. {
  75. socket = new Socket(ip, port);
  76. PrintStream ps = new PrintStream(socket.getOutputStream());
  77. ps.print(cmd.toLowerCase().toCharArray());
  78. ps.flush();
  79. InputStreamReader isr = new InputStreamReader(socket.getInputStream());
  80. while (0x80085 > 0)
  81. {
  82. len = isr.read(buf, 0, MAXRECEIVESIZE);
  83. if (len < 1)
  84. break;
  85. sb.append(buf, 0, len);
  86. if (buf[len-1] == '\0')
  87. break;
  88. }
  89. closeAll();
  90. }
  91. catch (IOException ioe)
  92. {
  93. System.err.println(ioe.toString());
  94. closeAll();
  95. return;
  96. }
  97. String result = sb.toString();
  98. System.out.println("Answer='"+result+"'");
  99. display(result);
  100. }
  101. public API(String command, String _ip, String _port) throws Exception
  102. {
  103. InetAddress ip;
  104. int port;
  105. try
  106. {
  107. ip = InetAddress.getByName(_ip);
  108. }
  109. catch (UnknownHostException uhe)
  110. {
  111. System.err.println("Unknown host " + _ip + ": " + uhe);
  112. return;
  113. }
  114. try
  115. {
  116. port = Integer.parseInt(_port);
  117. }
  118. catch (NumberFormatException nfe)
  119. {
  120. System.err.println("Invalid port " + _port + ": " + nfe);
  121. return;
  122. }
  123. process(command, ip, port);
  124. }
  125. public static void main(String[] params) throws Exception
  126. {
  127. String command = "summary";
  128. String ip = "127.0.0.1";
  129. String port = "4028";
  130. if (params.length > 0 && params[0].trim().length() > 0)
  131. command = params[0].trim();
  132. if (params.length > 1 && params[1].trim().length() > 0)
  133. ip = params[1].trim();
  134. if (params.length > 2 && params[2].trim().length() > 0)
  135. port = params[2].trim();
  136. new API(command, ip, port);
  137. }
  138. }