summaryrefslogtreecommitdiff
path: root/java/netsvcs/Logger/ServerLoggingHandler.java
blob: 9088e17d9d1ba6bd7ea2a24ea31364e68c3b1629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Logger
 *
 * = FILENAME
 *    ServerLoggingHandler.java
 *
 *@author Chris Cleeland, Everett Anderson
 *
 *************************************************/
package netsvcs.Logger;

import JACE.SOCK_SAP.*;
import JACE.Connection.*;
import JACE.OS.*;
import java.util.*;
import java.io.*;

/**
 *
 * <p><h2>DESCRIPTION</h2>
 *
 * <blockquote>
 * Created by ServerLoggingAcceptor every time a client connects.  This reads 
 * a logging statement passes it to the LogMessageReceiver for processing.
 * </blockquote>
 *
 * @see netsvcs.Logger.ServerLoggingAcceptor
 */
public class ServerLoggingHandler extends SvcHandler
{
  // Processes log messages
  private LogMessageReceiver receiver_;

  /**
   * Constructor
   *
   *@param receiver    LogMessageReceiver that handles what to do with a message
   */
  public ServerLoggingHandler (LogMessageReceiver receiver)
  {
    super();
    this.receiver_ = receiver;
  }

  /**
   * Start this handler in its own thread
   */
  public int open(Object obj)
  {
    new Thread (this).start();
    return 0;
  }

  /**
   * Accessor: get the host name of the connected client
   */
  protected String hostName ()
  {
    return new String(this.peer().socket().getInetAddress().getHostName());
  }

  /**
   * Receive input from the client, and send it to the LMR.  This is the
   * main loop for this thread.
   */
  public void run()
  {
    DataInputStream dis = new DataInputStream(this.peer().inputStream());

    for (;;)
      {
	// Messages arrive in the following format:
	// o 4 byte length (network format)
	// o message, in ACE.LogRecord format
	//
	// Hey!  We need exception catching in here too!
	try
	  {
	    // Reconstitute a log message from the wire
	    LogRecord rec = new LogRecord();

	    // We don't really need this, because
	    // the object already knows how to
	    // extract itself properly.  However,
	    // in order to interoperate with the
	    // C++ version, this must be extracted.
	    // Plus, it makes a convenient way to
	    // check everything.
	    int length = dis.readInt();
	
	    rec.streamInFrom(dis);
	
	    if (rec.length() == length)
	      {
		// Give the record to the log processor
		this.receiver_.logRecord(this.hostName(),
					   rec);
	      }
	    else
	      {
		ACE.ERROR(Thread.currentThread().getName() + ": Length error");
	      }
	  }
	catch (EOFException eof)
	  {
	    try { 
	      this.stream_.close(); 
	    } catch (IOException n) { }

	    return;
	  }
	catch (IOException ioe)
	  {
	    ACE.ERROR(Thread.currentThread().getName()
			       + ": "
			       + ioe);
	  }
      }
  }
};