API.java 3.1 KB

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