Distributed Event Logging
by Ivan Kiselev

Example 1:
interface EventLog
{
  void log(in string aHost,     // a host name
           in string aType,     // event type
           in string aSource,   // source name
           in string aMessage); // text message
};


Listing One
public class Log extends Exception
{
  private static EventLog remoteLog = null; // Stub for remote service
  private static String hostName = ""; // How this machine is called
  static
  {
    try
    {
      hostName = java.net.InetAddress.getLocalHost().getHostName();
    }
    catch(java.net.UnknownHostException e){}
  }
  public Log() { super(); }
  public Log(String message) { super(message); }
  public Log(String eventSource, String messageType, String message)
  {
    super(message);
    Log.log(eventSource, messageType, message);
  }
  // Actually logs an event.
  public static void log(String eventSource, String messageType,
                                                    String aMessage)
  {
    if( null == remoteLog )
    {
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[0], null);
      remoteLog = EventLogHelper.bind(orb);
    }
    try
    {
      synchronized(remoteLog)
      {
        remoteLog.log(hostName, messageType, eventSource, aMessage);
      }
    }
    catch(Throwable e) {}
  }
}


Listing Two
public class TestEventLogService
{
  public static void main(String args[])
  {
    try
    {
      // Just log some stuff as we go ...
      Log.log("main", "UNKNOWN", "Message of unknown type");
      Log.log("main", "DISCARD", "Message to be discarded");
      // Or throw it as an exception
      throw new Log("main", "ERROR", "Error message");
    }
    catch(Exception e) {}
  }
}


Listing Three
import java.util.*;
import java.net.*;
public class UDPBridge
{
  public static final int PORT=16500;
  public static final String DOMAIN="ALL-SYSTEMS.MCAST.NET";
  public static final String DELIMITER="|";
  private static byte[] buf = new byte[65509];

  static public void main(String[] args)
  {
    try
    {
      // Create a UDP socket
      MulticastSocket ms = new MulticastSocket(PORT);
      ms.joinGroup(InetAddress.getByName(DOMAIN));
      // Initialize CORBA connection
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[0], null);
      EventLog remoteLog = EventLogHelper.bind(orb);

      while(true)
      {
        // Receive the message
        DatagramPacket dp = new DatagramPacket(buf, buf.length);
        ms.receive(dp);

        // Parse the event
        String s = new String(dp.getData(), 0, 0, dp.getLength());
        StringTokenizer t = new StringTokenizer(s, DELIMITER);

        // Send the event message to the Event Log Service
        String remoteHost = dp.getAddress().getHostName();
        remoteLog.log(remoteHost,
           t.nextToken(), t.nextToken(), t.nextToken());
      }
    }
    catch(Exception e) {}
  }
}


Listing Four
package Log;
use Socket;
use Sys::Hostname;

my($iaddr, $proto, $paddr, $host, $port);

$port = 16500;
$host = "ALL-SYSTEMS.MCAST.NET";

$iaddr = gethostbyname($host);
$proto = getprotobyname('udp');
$paddr = sockaddr_in($port, $iaddr);

socket(SOCKET,PF_INET, SOCK_DGRAM, $proto) or die "Socket: $!";
connect(SOCKET, $paddr) or die "Connect: $!";
sub log
{
  defined(send(SOCKET, join('|', @_), 0)) or die "Send: $!";
}
1;


Listing Five
use Log;
Log::log($ARGV[0], $ARGV[1], $ARGV[2]);


Listing Six
import java.io.*;
import java.util.*;
import java.text.*;

public class EventLogService extends _EventLogImplBase
{
  private static PrintWriter out = null;
  private static DateFormat dateFormat =
       new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  private static Hashtable dispatcher = new Hashtable();
  public synchronized void log(String aHost,
       String aType, String aSource, String aMessage)
  {
    try
    {
      // Dispatch according to the type
      LogCapable handler = (LogCapable) dispatcher.get(aType);
      if( null != handler )
      {
        handler.log(aHost, aType, aSource, aMessage);
        return;
      }
      // Write the log string to the log file
      // for all types without custom handlers
      out.println(aHost+"|"+ dateFormat.format(new Date())+
             "|"+aSource+"|"+aType+"|"+aMessage);
    }
    catch(Exception e) {}
  }
  public EventLogService(String name) {super(name);}
  // This method will start the event log service.
  public static void main(String[] args)
  {
    try
    {
      // Create the type dispatch table
      for(int i=0; i<args.length; i++)
      {
        String typeName = args[i].substring(0, args[i].indexOf('='));
        String className = args[i].substring(args[i].indexOf('=')+1);
        LogCapable handler = (LogCapable)
           Class.forName(className).newInstance();
        dispatcher.put(typeName, handler);
      }
      // Initialize the ORB.
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
      // Create the service object.
      EventLogService service = new EventLogService("log");
      // Open the log file
      out = new PrintWriter(new BufferedWriter(
               new FileWriter("logfile.log")), true);
      // Initialize the BOA.
      com.inprise.vbroker.CORBA.BOA boa =
        ((com.inprise.vbroker.CORBA.ORB)orb).BOA_init();
      boa.obj_is_ready(service);
      // Wait for incoming requests
      boa.impl_is_ready();
    }
    catch(Throwable e) {}
  }
}


Listing Seven
public interface LogCapable
{
  public void log(String aHost, String aType, String aSource,  String aMessage);
}


Listing Eight
public class BlackHole implements LogCapable
{
  public synchronized void log(String aHost,
            String aType, String aSource, String aMessage)
  {
    // Nothing!
  }
}
