package net.hyperic.hq.mx4j;

import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class EchoService
    implements EchoServiceMBean
{
     // Configuration
    private String name;
    private int port;

    // Statistics
    private int  available     = 0;
    private long startTime;
    private long connections   = 0;
    private long requests      = 0;
    private long bytesSent     = 0;
    private long bytesReceived = 0;

    public EchoService(String name, int port) {
        this.name = name;
        this.port = port;

        this.available = 1;
        this.startTime = System.currentTimeMillis();
    }

    public Integer getAvailability() {
        return new Integer(available);
    }

    public void stop() {
        System.out.println("Stopping " + name + " service");
        this.available = 0;
    }

    public void start() {
        System.out.println("Starting " + name + " service");
        this.available = 1;
    }

    public Long getUptime() {
        return new Long(System.currentTimeMillis() - startTime);
    }

    public Long getNumConnections() {
        return new Long(connections);
    }

    public Long getNumRequests() {
        return new Long(requests);
    }

    public Long getBytesSent() {
        return new Long(bytesSent);
    }

    public Long getBytesReceived() {
        return new Long(bytesReceived);
    }

    public void run()
        throws IOException
    {
        ServerSocket serverSocket = null;
        
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + port);
            System.exit(1);
        }

        System.out.println(name + " listening on " + port);

        Socket clientSocket = null;
        while (true) {
            try {
                clientSocket = serverSocket.accept();
                connections++;
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.exit(1);
            }

            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
                                              true);
            if (this.available == 0) {
                String msg = "Service unavailable.  Goodbye.";
                out.println(msg);
                bytesSent += msg.length();
                
                out.close();
                clientSocket.close();

                continue;
            }
                
            BufferedReader in = 
                new BufferedReader(new InputStreamReader(clientSocket.
                                                         getInputStream()));

            String inputLine, outputLine;
            while ((inputLine = in.readLine()) != null) {
                requests++;
                bytesReceived += inputLine.length(); //Assume 1 byte chars
                if (inputLine.equals("quit")) {
                    break;
                }

                String msg = "echo: " + inputLine;
                out.println(msg);
                bytesSent += msg.length();
            }
            
            out.close();
            in.close();
            
            clientSocket.close();
        }
    }
}
