Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1350 dpurdie 1
package com.erggroup.buildtool.daemon;
2
 
3
import com.erggroup.buildtool.daemon.BuildDaemon;
4
 
5
import org.apache.log4j.Logger;
6
import java.net.ServerSocket;
7
import java.net.Socket;
8
import java.lang.Thread;
9
import java.lang.Package;
10
import java.io.*;
11
 
12
//----------------------------------------------------------------------------
13
// CLASS              : NagiosThread
14
//
15
// DESCRIPTION        : A class to provide Nagios support
16
//
17
//
18
public class NagiosThread extends Thread {
19
    // Server socket
20
    private ServerSocket srv;
21
 
22
    // Flag that indicates whether the poller is running or not.
23
    private volatile boolean isRunning = true;
24
 
25
    /**Logger
26
     * @attribute
27
     */
28
    private static final Logger mLogger = Logger.getLogger(NagiosThread.class);
29
 
30
    // Constructor.
31
    public NagiosThread(ServerSocket srv) {
32
        this.isRunning = true;
33
        this.srv = srv;
34
    }
35
 
36
    // Method for terminating the listener
37
    public void terminate() {
38
        mLogger.fatal("NagiosThread terminate");
39
        this.isRunning = false;
40
    }
41
 
42
    /**
43
    * This method start the thread and performs all the operations.
44
    */
45
    public void run() {
46
        mLogger.fatal("NagiosThread Run");
47
        try {
48
            // Wait for connection from client.
49
            while (isRunning) {
50
                Socket socket = srv.accept();
51
 
52
            // Open a reader to receive (and ignore the input)
53
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
54
 
55
            // Write the status message to the outputstream
56
            // Currently this is too simple, but it is a start
57
            //
58
            try {
59
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
60
                wr.write("Build Daemon status: OK\n");
61
                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
62
                wr.write("HostName: "+ BuildDaemon.mHostname + "\n");
63
                wr.flush();
64
            } catch (IOException e) {
65
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
66
            }
67
 
68
            // Close the inputstream since we really don't care about it
69
            rd.close();
70
          }
71
        } catch (Exception e) {
72
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
73
        }
74
    }
75
}
76