Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

package com.erggroup.buildtool.daemon;

import com.erggroup.buildtool.daemon.BuildDaemon;

import org.apache.log4j.Logger;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.Thread;
import java.lang.Package;
import java.io.*;

//----------------------------------------------------------------------------
// CLASS              : NagiosThread
//
// DESCRIPTION        : A class to provide Nagios support
//
//
public class NagiosThread extends Thread {
    // Server socket
    private ServerSocket srv;

    // Flag that indicates whether the poller is running or not.
    private volatile boolean isRunning = true;

    /**Logger
     * @attribute
     */
    private static final Logger mLogger = Logger.getLogger(NagiosThread.class);
    
    // Constructor.
    public NagiosThread(ServerSocket srv) {
        this.isRunning = true;
        this.srv = srv;
    }

    // Method for terminating the listener
    public void terminate() {
        mLogger.fatal("NagiosThread terminate");
        this.isRunning = false;
    }

    /**
    * This method start the thread and performs all the operations.
    */
    public void run() {
        mLogger.fatal("NagiosThread Run");
        try {
            // Wait for connection from client.
            while (isRunning) {
                Socket socket = srv.accept();

            // Open a reader to receive (and ignore the input)
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // Write the status message to the outputstream
            // Currently this is too simple, but it is a start
            //
            try {
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                wr.write("Build Daemon status: OK\n");
                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
                wr.write("HostName: "+ BuildDaemon.mHostname + "\n");
                wr.flush();
            } catch (IOException e) {
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
            }

            // Close the inputstream since we really don't care about it
            rd.close();
          }
        } catch (Exception e) {
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
        }
    }
}