Magentix2  2.1.1
es.upv.dsic.gti_ia.core.BridgeAgentOutIn Class Reference
Inheritance diagram for es.upv.dsic.gti_ia.core.BridgeAgentOutIn:
Collaboration diagram for es.upv.dsic.gti_ia.core.BridgeAgentOutIn:

List of all members.

Classes

class  httpToACL

Public Member Functions

 BridgeAgentOutIn (AgentID aid) throws Exception
 BridgeAgentOutIn (AgentID aid, int http_port) throws Exception
void execute ()
void exit ()

Static Public Member Functions

static int getHttp_port ()

Package Attributes

Configuration configuration = Configuration.getConfiguration()

Static Package Attributes

static int http_port

Detailed Description

This agent routes messages from outside the platform to inside the platform.

Author:
Ricard Lopez Fogues

Definition at line 25 of file BridgeAgentOutIn.java.


Constructor & Destructor Documentation

Creates a new BrideAgentOutIn

Parameters:
aid
Exceptions:
Exception

Definition at line 55 of file BridgeAgentOutIn.java.

                                                              {
                super(aid);

                // crear objeto DatagramSocket para enviar y recibir paquetes
                try {
                        // Sacamos el http port del properties.
                        BridgeAgentOutIn.http_port = configuration.getBridgeHttpPort();

                        // socket = new DatagramSocket(5000);
                        socket = new ServerSocket(BridgeAgentOutIn.http_port);

                }

                // procesar los problemas que pueden ocurrir al crear el objeto
                // DatagramSocket
                catch (SocketException excepcionSocket) {
                        excepcionSocket.printStackTrace();
                        System.exit(1);
                }

        }
es.upv.dsic.gti_ia.core.BridgeAgentOutIn.BridgeAgentOutIn ( AgentID  aid,
int  http_port 
) throws Exception

Creates a new BrideAgentOutIn

Parameters:
aid
httpport
Exceptions:
Exception

Definition at line 85 of file BridgeAgentOutIn.java.

                                                                             {
                super(aid);

                // crear objeto DatagramSocket para enviar y recibir paquetes
                try {
                        BridgeAgentOutIn.http_port = http_port;
                        // socket = new DatagramSocket(5000);
                        socket = new ServerSocket(BridgeAgentOutIn.http_port);
                }

                // procesar los problemas que pueden ocurrir al crear el objeto
                // DatagramSocket
                catch (SocketException excepcionSocket) {
                        excepcionSocket.printStackTrace();
                        System.exit(1);
                }

        }

Member Function Documentation

Waits for a message and processes it, in parallel.

Reimplemented from es.upv.dsic.gti_ia.core.BaseAgent.

Definition at line 108 of file BridgeAgentOutIn.java.

                              {

                while (!finalized) {
                        // Escuchar por protocolo http y enviar a quien corresponda
                        // recibir paquete, mostrar su contenido
                        try {

                                InputStream is;
                                //logger.getRootLogger().setLevel(Level.DEBUG);
                                logger.info("BridgeAgentOutIn waiting receive external FIPA-Messages");

                                s = socket.accept(); // Socket Cliente
                                // Monitor m = new Monitor();
                                // m.waiting(10);

                                is = s.getInputStream();
                                OutputStream os = s.getOutputStream();

                                try {
                                        Thread.sleep(10);
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }

                                StringBuffer stb = new StringBuffer();
                                int i;
                                byte[] buffer = new byte[16384];

                                /*
                                 * while ((is.available() > 0) && (i = is.read(buffer)) != -1) {
                                 * stb.append(new String(buffer, 0, i)); }
                                 */

                                boolean condicion = false;

                                /*
                                 * Parche: HTTP 1.1 no cierra la conexi�n, por lo que no podemos
                                 * leer hasta eof. La soluci�n adoptada es que al final de la
                                 * lectura, si los bytes leidos son menores que el tama�o del
                                 * buffer y adem�s al final se encuentra la cadena "\r\n\r\n" se
                                 * asume el final del mensaje. No funcionar�a este parche, si y
                                 * solo si: - El tama�o del mensaje total coincide exactamente
                                 * con el tama�o del buffer, pues no cortariamos el mensaje. -
                                 * El tama�o leido es menor al del buffer y el final del paquete
                                 * coincide con la cadena \r\n\r\n, pero no es el final del
                                 * mensaje sino un trozo del mensaje total.
                                 */

                                try {
                                        while ((i = is.read(buffer)) != -1) {
                                                stb.append(new String(buffer, 0, i));

                                                char[] temp = new char[4]; // Array temporal, en busca
                                                                                                        // de final de mensaje.
                                                stb.getChars(stb.length() - 4, stb.length(), temp, 0);

                                                if (temp[0] == '\r' && temp[1] == '\n'
                                                                && temp[2] == '\r' && temp[3] == '\n') {
                                                        condicion = true;
                                                }

                                                if (i < buffer.length && condicion) {
                                                        break;
                                                }

                                        }
                                } catch (Exception e) {
                                        // logger.error(e.getMessage() + " - " + e.getStackTrace());
                                        System.err.println(e.getMessage());
                                }

                                String texto = new String(stb);

                                httpToACL hilo = new httpToACL(stringToInputStream(texto));

                                String OK = "HTTP/1.0 200 OK\r\n\r\n";
                                byte a[] = OK.getBytes();

                                os.write(a);

                                // Cerrar
                                os.close();
                                is.close();

                                hilo.start();

                                s.close();

                                /* Esto era con UDP */

                                // System.out.println("Received on BridgeAgentOutIn: "
                                // + new String(recibirPaquete.getData()));
                                //
                                // // mostrar la información del paquete recibido
                                // System.out.println("\nPackage received:"
                                // + "\nfrom host: "
                                // + recibirPaquete.getAddress()
                                // + "\nPort of the host: "
                                // + recibirPaquete.getPort()
                                // + "\nLenght: "
                                // + recibirPaquete.getLength()
                                // + "\nContent:\n\t"
                                // + new String(recibirPaquete.getData(), 0,
                                // recibirPaquete.getLength()));
                                //
                                // // creamos nuevo hilo encargado de enviar el mensaje
                                // // httpToACL hilo = new httpToACL(stringToInputStream(new
                                // String(
                                // // recibirPaquete.getData())));
                                // // hilo.run();
                        }

                        // procesar los problemas que pueden ocurrir al manipular el paquete
                        catch (IOException excepcionES) {

                                if (excepcionES.getClass().toString()
                                                .equals("class java.net.SocketException"))
                                        System.out.println("BridgeAgentOutIn Socket Closed");
                                else {
                                        System.err.println("Error on BridgeAgentOutIn, "
                                                        + excepcionES.toString() + "\n");
                                        excepcionES.printStackTrace();
                                }

                        }

                }
        }

Closes the connection and finalizes the listening process.

Definition at line 531 of file BridgeAgentOutIn.java.

                           {
                try {

                        if (s != null)
                                s.close();

                        socket.close();

                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                this.finalized = true;
                System.out.println("Bridge Agent Out In leave the system");
        }

Gets the HTTP port where agent is waiting

Returns:
http port

Definition at line 43 of file BridgeAgentOutIn.java.

                                         {
                return http_port;
        }

Member Data Documentation

BridgeAgentOutIn runs on 8082 port

Definition at line 36 of file BridgeAgentOutIn.java.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables