API.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. char buf[] = new char[MAXRECEIVESIZE];
  68. int len = 0;
  69. System.out.println("Attempting to send '"+cmd+"' to "+ip.getHostAddress()+":"+port);
  70. try
  71. {
  72. socket = new Socket(ip, port);
  73. PrintStream ps = new PrintStream(socket.getOutputStream());
  74. ps.print(cmd.toLowerCase().toCharArray());
  75. ps.flush();
  76. InputStreamReader isr = new InputStreamReader(socket.getInputStream());
  77. len = isr.read(buf, 0, MAXRECEIVESIZE);
  78. closeAll();
  79. }
  80. catch (IOException ioe)
  81. {
  82. System.err.println(ioe.toString());
  83. closeAll();
  84. return;
  85. }
  86. String result = new String(buf, 0, len);
  87. System.out.println("Answer='"+result+"'");
  88. display(result);
  89. }
  90. public API(String command, String _ip, String _port) throws Exception
  91. {
  92. InetAddress ip;
  93. int port;
  94. try
  95. {
  96. ip = InetAddress.getByName(_ip);
  97. }
  98. catch (UnknownHostException uhe)
  99. {
  100. System.err.println("Unknown host " + _ip + ": " + uhe);
  101. return;
  102. }
  103. try
  104. {
  105. port = Integer.parseInt(_port);
  106. }
  107. catch (NumberFormatException nfe)
  108. {
  109. System.err.println("Invalid port " + _port + ": " + nfe);
  110. return;
  111. }
  112. process(command, ip, port);
  113. }
  114. public static void main(String[] params) throws Exception
  115. {
  116. String command = "summary";
  117. String ip = "127.0.0.1";
  118. String port = "4028";
  119. if (params.length > 0 && params[0].trim().length() > 0)
  120. command = params[0].trim();
  121. if (params.length > 1 && params[1].trim().length() > 0)
  122. ip = params[1].trim();
  123. if (params.length > 2 && params[2].trim().length() > 0)
  124. port = params[2].trim();
  125. new API(command, ip, port);
  126. }
  127. }