Browse Source

Add API.java + API.class to access the API

nfactor-troky
Kano 13 years ago
parent
commit
2a3c99f6a8
  1. BIN
      API.class
  2. 155
      API.java

BIN
API.class

Binary file not shown.

155
API.java

@ -0,0 +1,155 @@
/*
*
* Usage: java API command ip port
*
* If any are missing or blank they use the defaults:
*
* command = 'summary'
* ip = '127.0.0.1'
* port = '4028'
*
*/
import java.net.*;
import java.io.*;
class API
{
static private final int MAXRECEIVESIZE = 65535;
static private Socket socket = null;
private void closeAll() throws Exception
{
if (socket != null)
{
socket.close();
socket = null;
}
}
public void display(String result) throws Exception
{
String value;
String name;
String[] sections = result.split("\\|", 0);
for (int i = 0; i < sections.length; i++)
{
if (sections[i].trim().length() > 0)
{
String[] data = sections[i].split(",", 0);
for (int j = 0; j < data.length; j++)
{
String[] nameval = data[j].split("=", 2);
if (j == 0)
{
if (nameval.length > 1
&& Character.isDigit(nameval[1].charAt(0)))
name = nameval[0] + nameval[1];
else
name = nameval[0];
System.out.println("[" + name + "] =>");
System.out.println("(");
}
if (nameval.length > 1)
{
name = nameval[0];
value = nameval[1];
}
else
{
name = "" + j;
value = nameval[0];
}
System.out.println(" ["+name+"] => "+value);
}
System.out.println(")");
}
}
}
public void process(String cmd, InetAddress ip, int port) throws Exception
{
char buf[] = new char[MAXRECEIVESIZE];
int len = 0;
System.out.println("Attempting to send '"+cmd+"' to "+ip.getHostAddress()+":"+port);
try
{
socket = new Socket(ip, port);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.print(cmd.toLowerCase().toCharArray());
ps.flush();
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
len = isr.read(buf, 0, MAXRECEIVESIZE);
closeAll();
}
catch (IOException ioe)
{
System.err.println(ioe.toString());
closeAll();
return;
}
String result = new String(buf, 0, len);
System.out.println("Answer='"+result+"'");
display(result);
}
public API(String command, String _ip, String _port) throws Exception
{
InetAddress ip;
int port;
try
{
ip = InetAddress.getByName(_ip);
}
catch (UnknownHostException uhe)
{
System.err.println("Unknown host " + _ip + ": " + uhe);
return;
}
try
{
port = Integer.parseInt(_port);
}
catch (NumberFormatException nfe)
{
System.err.println("Invalid port " + _port + ": " + nfe);
return;
}
process(command, ip, port);
}
public static void main(String[] params) throws Exception
{
String command = "summary";
String ip = "127.0.0.1";
String port = "4028";
if (params.length > 0 && params[0].trim().length() > 0)
command = params[0].trim();
if (params.length > 1 && params[1].trim().length() > 0)
ip = params[1].trim();
if (params.length > 2 && params[2].trim().length() > 0)
port = params[2].trim();
new API(command, ip, port);
}
}
Loading…
Cancel
Save