summaryrefslogtreecommitdiff
path: root/java/examples/Logger/simple-server/LoggingClient.java
blob: e6ea986c0116d1c3be1dca12bc1001128077ed25 (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
/**
 * Main class that acts as an example logging client.
 */

import java.io.*;
import java.net.*;
import JACE.SOCK_SAP.*;
import LogRecord;
import LoggerConstants;

public class LoggingClient implements Runnable
{
  private String loggerHost_;
  private int port_;
  private int maxIter_;
  private static final int DEFAULT_ITERATIONS = 10;
  
  public static void main(String[] args)
  {
    // Really need to put code in here to parse options
    int iter;
    int port;
    String host;

	iter = (args.length > 0) ? Integer.parseInt(args[0])
          : DEFAULT_ITERATIONS;
	port = (args.length > 1) ? Integer.parseInt(args[1])
	  : LoggerConstants.DEFAULT_SERVER_PORT;
	host = (args.length > 2) ? args[2]
	  : LoggerConstants.DEFAULT_SERVER_HOSTNAME;

    LoggingClient lc = new LoggingClient(iter, port, host);
    lc.run();
  }

  public LoggingClient()
  {

    this(DEFAULT_ITERATIONS,
	 LoggerConstants.DEFAULT_SERVER_PORT,
	 LoggerConstants.DEFAULT_SERVER_HOSTNAME);
  }

  public LoggingClient(int iterations, int thePort, String theHost)
  {
    maxIter_ = iterations;
    port_ = thePort;
    loggerHost_ = theHost;
  }

  public void run()
  {
    SOCKStream logger = new SOCKStream();
    SOCKConnector connector = new SOCKConnector();
    // INETAddr addr = new INETAddr(port_, loggerHost_);

    LogRecord rec = new LogRecord(9, 2, 0);

    try
      {
        connector.connect(logger, loggerHost_, port_);

	int oneSecond = 1000;
	// Currently SOCKStream uses DataInputStream for its input stream,
	// and PrintStream for its output stream.  It probably ought to use
	// DataOutputStream for the output stream for symmetry, or at least
	// provide a mechanism for changing the type of the filter stream
	// used (which might be better in the long run...give it the class
	// id).
	BufferedOutputStream bos = new BufferedOutputStream((OutputStream) logger.outputStream(), LogRecord.MAXLOGMSGLEN);
	DataOutputStream dos = new DataOutputStream(bos);

	for (int i = 0; i < maxIter_; i++)
	  {
	    // Need to overload LogRecord.msgData to take a String
	    // argument so that it's easy to create instances with text
	    // inside.
	    rec.msgData("message = " + i);
	    try
	      {
		dos.writeInt(rec.length());
		rec.streamOutTo(dos);
		bos.flush();
		rec.print("localhost", true, System.err);
	      }
	    catch (IOException ex) { }

	    try
	      {
		Thread.sleep(oneSecond);
	      }
	    catch (InterruptedException ex) { }
	  }

	try { logger.close(); } catch (IOException ex) { }

      }
    catch (SocketException ex)
      {
        System.err.println("socket exception: " + ex);
      }
    catch (IOException ex)
      {
        System.err.println("io exception: " + ex);
      }

  }
}