Package fb.rt

Source Code of fb.rt.Debug_CLT

package fb.rt;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

import fb.rt.net.FBCommException;

// Referenced classes of package fb.rt.net:
//            FBCommException

public class Debug_CLT
{

    public Debug_CLT(String id, String xmlsrc)
    {
        hostIp = id;
        XML = xmlsrc;
    }

    public void setXML(String xmlsrc)
    {
        XML = xmlsrc;
    }

    public String writeOut()
    {
        StringBuffer fileLines = new StringBuffer();
        try
        {
//            socketWriter.println(XML);
//            socketWriter.flush();
//            String line = null;
//            line = socketReader.readLine();
//            fileLines.append(String.valueOf(line) + "\n");

          byte[] data = XML.getBytes();
          InetAddress address = InetAddress.getByName(getInetAddress(hostIp));
          DatagramPacket packet = new DatagramPacket(data, data.length, address, getPort(hostIp));
          socket.send(packet);
         
//          data = new byte[1024];
//          packet = new DatagramPacket(data, data.length);
//          socket.receive(packet);
//          fileLines.append(new String(packet.getData(), 0, packet.getLength()));
        }
        catch(FBCommException e)
        {
            System.out.println("Error setting up socket connection: unknown host at " + hostIp.toString());
        }
        catch(IOException e)
        {
            System.out.println("Error!");
        }
        return fileLines.toString();
    }

    public void setUpConnection()
    {
        try
        {
//          Socket client = new Socket(getInetAddress(hostIp), getPort(hostIp));
//            socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
//            socketWriter = new PrintWriter(client.getOutputStream());
         
          socket = new DatagramSocket();
        }
        catch(IOException e)
        {
            System.out.println("Error setting up socket connection: " + e.toString());
        }
    }

    public void tearDownConnection()
    {
//        try
//        {
          socket.close();
//            socketWriter.close();
//            socketReader.close();
//        }
//        catch(IOException e)
//        {
//            System.out.println("Error tearing down socket connection: " + e.toString());
//        }
    }

    public static String getInetAddress(String id)
        throws FBCommException
    {
      int n = id.indexOf(':');
      if(n<=0){throw new FBCommException("INVALID_ID");}
      try{System.out.println("Host " + (id.substring(0,n)));
        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(args[0], args[1]);
        remoteClient.setUpConnection();
        remoteClient.writeOut();
        remoteClient.tearDownConnection();
    }

    protected DatagramSocket socket;
    protected BufferedReader socketReader;
    protected PrintWriter socketWriter;
    protected String hostIp;
    protected String XML;
}

TOP

Related Classes of fb.rt.Debug_CLT

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.