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