package thundernet.afp;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  AFPmon.java                        Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  An AFP protocol analyzer.  Sits between an AFP client and an AFP server. */
/*  Simply displays data as it is passing through.                           */
/*                                                                           */
/*  Public Methods:                                                          */
/*      AFPmon()                                                             */
/*      main()                                                               */
/*      run()                                                                */
/*      ServerThread.run()                                                   */
/*                                                                           */
/*  Date      Who  Comment                                                   */
/*  -----------------------------------------------------------------------  */
/*  24mar01   acp  initial creation                                          */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import  java.io.*;
import  java.lang.*;
import  java.net.*;
import  java.util.*;
import  thundernet.afp.*;
import  thundernet.util.*;



public class AFPmon
{

    String              afpServer;          // AFP server to connect to
    int                 afpServerPort;      // port on AFP server to connect to
    int                 afpListenerPort;    // port to listen on for AFP packets
    BufferedInputStream cin;                // client side input stream
    Socket              client;
    Properties          config;
    OutputStream        cout;               // client side output stream
    AFPpacket           cpacket;            // packet received from client side
    boolean             dead = false;
    PrintStream         log;
    Socket              server;
    ServerSocket        serverSocket;
    ServerThread        serverThread;       // thread in which the server runs
    BufferedInputStream sin;                // server side input stream
    AFPpacket           spacket;            // packet received from server side
    OutputStream        sout;               // server side output stream



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  AFPmon()                           Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Primary constructor.  Initializes internal data.                         */
/*                                                                           */
/*  args:                                                                    */
/*       none           no comment                                           */
/*                                                                           */
/*  return:                                                                  */
/*       n/a                                                                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public AFPmon() throws java.lang.Exception
{

    log = System.out;

    //
    // read in the properties file and initialize instance variables
    //
    config = new Properties();
    config.load(new FileInputStream(System.getProperty("CONFIG")));

    //
    // get config information to talk to a real AFP server
    //
    afpServer = config.getProperty(AFP.PROP_AFP_SERVER_HOST);
    afpServerPort = Integer.parseInt(config.getProperty(AFP.PROP_AFP_LISTENER_PORT));

    //
    // get config information to act as an AFP server
    //  then grab the listener port
    //
    afpListenerPort = Integer.parseInt(config.getProperty(AFP.PROP_AFP_SERVER_PORT));
    serverSocket = new ServerSocket(afpListenerPort);

    cpacket = new AFPpacket();

}   /* end of method AFPmon() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  main()                             Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Program entry point.                                                     */
/*                                                                           */
/*  cmd line> java thundernet.afp.AFPmon -DCONFIG=<properties-file>          */
/*                                                                           */
/*  args:                                                                    */
/*       args       command line arguments                                   */
/*                                                                           */
/*  return:                                                                  */
/*       none                                                                */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public static void main(String[] args)
{

    AFPmon      afp_monitor;

    //
    // validate the command line before continuing
    //
    if (System.getProperty("CONFIG") == null)
    {
        System.out.println("AFPmon: CONFIG property not found");
        System.out.println("Usage: java thundernet.afp.AFPtester -DCONFIG=<properties-file>");
        System.exit(101);
    }

    //
    // instantiate the monitor and off we go
    //
    try
    {
        afp_monitor = new AFPmon();
        afp_monitor.run();
    }
    catch (java.lang.Exception e)
    {
        System.err.println(e);
        e.printStackTrace();
    }

}   /* end of method main() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  kill()                             Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Sets the dead flag so the server can die gracefully.                     */
/*                                                                           */
/*  args:                                                                    */
/*       none      no comment                                                */
/*                                                                           */
/*  return:                                                                  */
/*       none                                                                */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private void kill()
{

    dead = true;

}   /* end of method kill() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  run()                              Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  main control loop.                                                       */
/*                                                                           */
/*  args:                                                                    */
/*      none        no comment                                               */
/*                                                                           */
/*  return:                                                                  */
/*      none                                                                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private void run()
{

    boolean rc;

    while (!dead)
    {
        try
        {
            //
            // accept a connection from a client
            //
            client = serverSocket.accept();
            cin = new BufferedInputStream(client.getInputStream());
            cout = client.getOutputStream();

            cpacket = new AFPpacket();
            rc = cpacket.recv(cin);
            if (!rc)
            {
                log.println("error receiving packet");
                log.println(cpacket);
            }
            else
            {
                log.println(cpacket);

                //
                // establish connection to server
                //
                server = new Socket(afpServer, afpServerPort);
                sin = new BufferedInputStream(server.getInputStream());
                sout = server.getOutputStream();

                cpacket.send(sout);
            }

            client.close();
            server.close();
        }
        catch (java.lang.Exception e)
        {
            e.printStackTrace();
            this.kill();
        }
    }

}   /* end of method run() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  ServerThread                       Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  A simple thread in which the AFPmon server can run.                      */
/*                                                                           */
/*  Public Methods:                                                          */
/*      run()                                                                */
/*                                                                           */
/*  Date      Who  Comment                                                   */
/*  -----------------------------------------------------------------------  */
/*  24mar01   acp  initial creation                                          */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private class ServerThread extends Thread
{



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  run()                              Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Main thread loop.                                                        */
/*                                                                           */
/*  args:                                                                    */
/*      none            no comment                                           */
/*                                                                           */
/*  return:                                                                  */
/*      none                                                                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public void run()
{


}   /* end of method run() */

}   /* end of inner class ServerThread */

}   /* end of class AFPmon */


  Copyright © Thundernet Development Group Inc., 2003.
All rights reserved.