Package org.cafesip.reference.jiplet

Source Code of org.cafesip.reference.jiplet.ExternalEntity

/*
* Created on Oct 30, 2005
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.reference.jiplet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import org.cafesip.jiplet.JipletContext;
import org.cafesip.jiplet.JipletLogger;
import org.cafesip.jiplet.JipletSignal;

/**
* @author Amit Chatterjee
*/
public class ExternalEntity extends Thread
{
    private int port = 9998;

    private ServerSocket socket;

    private String name;
   
    private JipletContext context;
   
    /**
     * A constructor for this class.
     *
     * @throws IOException
     *
     * 
     */
    public ExternalEntity(JipletContext context, String name, int port) throws IOException
    {
        super("External TCP/IP socket entity");
        if (port >= 0)
        {
            this.port = port;
        }
       
        this.name = name;
        this.context = context;
       
        init();
    }

    public void init() throws IOException
    {
        socket = new ServerSocket(port);
    }

    public void run()
    {
        try
        {
            while (isInterrupted() == false)
            {
                Socket s = socket.accept();
                JipletLogger.info("New socket connection received from " + s.getRemoteSocketAddress());
                BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
                String line = "";
                while (line != null)
                {
                    line = reader.readLine();
                    if (line == null)
                    {
                        JipletLogger.info("Connection closed from the remote socket");
                        break; // accept another connection
                    }
                   
                    line = line.trim();
                    if (line.length() == 0)
                    {
                        // ignore blank line
                        continue;
                    }
                   
                    if (line.equals("quit") == true)
                    {
                        JipletLogger.info("A quit command is received. Going to close the connection");
                        reader.close();
                        s.close();
                        break; // accept another connection
                    }
                   
                    // pass on the received message as a signal to the jiplet
                    JipletSignal signal = new JipletSignal();
                    signal.setEventObject(line);
                   
                    if (context.sendSignal(name, signal) == false)
                    {
                        JipletLogger.error("Error sending signal to the jiplet");
                    }                   
                }
            }
        }
        catch (Exception e)
        {
            JipletLogger.fatal("Exception " + e.getClass().getName() + " : "
                    + e.getMessage() + "\n" + JipletLogger.getStackTrace(e));
        }
    }
}
TOP

Related Classes of org.cafesip.reference.jiplet.ExternalEntity

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.