package fb.rt;
import java.util.Vector;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.SocketConnection;
import fb.rt.net.FBCommException;
import java.io.*;
import javax.microedition.io.DatagramConnection;
public class Debug_CLT
{
private static Vector instances = new Vector();
private static String host = "localhost:61502";
public static void setHost(String newHost){
Debug_CLT.host = newHost;
for(int i = instances.size() - 1; i >= 0; i--){
Debug_CLT clt = (Debug_CLT)instances.elementAt(i);
clt.tearDownConnection();
clt.setUpConnection();
}
}
public static String getHost(){
return Debug_CLT.host;
}
protected DataOutputStream outputStream;
protected DataInputStream inputStream;
protected DatagramConnection client;
// protected String hostIp;
protected String XML;
protected byte[] buffer = new byte[2048];
private boolean failed;
public Debug_CLT(){
instances.addElement(this);
setUpConnection();
}
public boolean getFailed(){
return failed;
}
// public Debug_CLT(String id, String xmlsrc)
// {
// hostIp = id;
// XML = xmlsrc;
// }
public void setXML(String xmlsrc)
{
XML = xmlsrc;
}
public String writeOut()
{
if(client == null)
return "ERROR";
String response = "ERROR";
try
{
byte[] data = XML.getBytes();
Datagram datagram = client.newDatagram(data, data.length);
client.send(datagram);
// datagram = client.newDatagram(1024, host);
// client.receive(datagram);
// response = new String(datagram.getData(), 0, datagram.getLength());
response = "OK";
}
catch(Exception e)
{
System.out.println(e);
}
return response;
}
public void setUpConnection()
{
try
{
client = (DatagramConnection)Connector.open("datagram://" + host, Connector.WRITE, false);
}
catch(IOException e) {
System.out.println("Error setting up socket connection:\n" + e.toString());
failed = true;
}
catch(IllegalArgumentException e){
System.out.println("Invalid host address: " + host);
failed = true;
}
catch(Exception e) {
System.out.println(e);
failed = true;
}
}
public void tearDownConnection()
{
try
{
if(inputStream != null){
inputStream.close();
inputStream = null;
}
if(outputStream != null){
outputStream.close();
outputStream = null;
}
if(client != null){
client.close();
client = null;
}
}
catch(IOException e)
{
System.out.println("Error tearing down socket connection:\n" + e);
}
}
public static String getInetAddress(String id)
throws FBCommException
{
int n = id.indexOf(':');
if(n<=0){throw new FBCommException("INVALID_ID");}
try{
return (id.substring(0,n));
}
catch(IndexOutOfBoundsException e){throw new FBCommException("INVALID_ID");}
}
public static int getPort(String id)
throws FBCommException
{
int n = id.indexOf(':');
try
{
if(n <= 0 || n == id.length() - 1)
throw new FBCommException("INVALID_ID");
else
return Integer.valueOf(id.substring(n + 1)).intValue();
}
catch(NumberFormatException e)
{
throw new FBCommException("INVALID_ID");
}
}
public static void main(String args[])
{
Debug_CLT remoteClient = new Debug_CLT();
Debug_CLT.host = args[0];
remoteClient.XML = args[1];
remoteClient.setUpConnection();
remoteClient.writeOut();
remoteClient.tearDownConnection();
}
}