package thundernet.afp;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  AFPtester.java                     Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Connects to an AFP server as specified in the afpTest.properties file.   */
/*  Responds to user commands to generate particular conversations.          */
/*                                                                           */
/*  Public Methods:                                                          */
/*      AFPtester()                                                          */
/*      main()                                                               */
/*                                                                           */
/*  Date      Who  Comment                                                   */
/*  -----------------------------------------------------------------------  */
/*  24mar01   acp  initial creation                                          */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

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



public class AFPtester
{

    private String              afpHost;
    private int                 afpPort;
    private Properties          config;
    private BufferedInputStream in;
    private PrintStream         log;
    private OutputStream        out;
    private AFPpacket           packet;
    private byte[]              mySenderId;
    private Socket              socket;



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  AFPtester()                        Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Primary constructor.  Initializes internal data.                         */
/*                                                                           */
/*  args:                                                                    */
/*       none      no comment                                                */
/*                                                                           */
/*  return:                                                                  */
/*       n/a                                                                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public AFPtester() 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")));

    afpHost = config.getProperty(AFP.PROP_AFP_SERVER_HOST);
    afpPort = Integer.parseInt(config.getProperty(AFP.PROP_AFP_SERVER_PORT));

    packet = new AFPpacket();

    socket = new Socket(afpHost, afpPort);
    in = new BufferedInputStream(socket.getInputStream());
    out = socket.getOutputStream();

    mySenderId = new byte[6];
    mySenderId[0] = (byte)0xff;
    mySenderId[1] = (byte)0xff;
    mySenderId[2] = (byte)0xff;
    mySenderId[3] = (byte)0xff;
    mySenderId[4] = (byte)0xff;
    mySenderId[5] = (byte)0xff;

}   /* end of method AFPtester() */



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

    AFPtester   afp_tester;

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

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

}   /* end of method main() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  recvPacket()                       Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Receives and AFP packet from the server and sets up the local packet     */
/*      object.                                                              */
/*                                                                           */
/*  args:                                                                    */
/*       none      no comment                                                */
/*                                                                           */
/*  return:                                                                  */
/*       none                                                                */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private boolean recvPacket() throws java.io.IOException
{

    byte[]  body;
    int     body_length;
    int     c;          // count of bytes read in via in.read()
    boolean rc = false;

    packet = new AFPpacket();

    packet.type = (byte)(in.read() & 0xff);
    if ((packet.type == AFPpacket.TYPE_EDX_MSG) || (packet.type == AFPpacket.TYPE_QUERY_MSG))
    {
        c = in.read(packet.ID, 0, packet.ID.length);
        if (c == packet.ID.length)
        {
            packet.version = (byte)(in.read() & 0xff);
            if (packet.version == AFPpacket.VERSION_1_0)
            {
                packet.cmd = (byte)(in.read() & 0xff);
                if (packet.cmd != -1)
                {
                    packet.priority = (byte)(in.read() & 0xff);
                    if ((packet.priority == AFPpacket.PRIORITY_EDX)
                     || (packet.priority == AFPpacket.PRIORITY_EVENT)
                     || (packet.priority == AFPpacket.PRIORITY_QUERY))
                    {
                        c = in.read(packet.length, 0, packet.length.length);
                        if (c == packet.length.length)
                        {
                            //
                            // calculate body length and read body
                            //
                            body_length = AFP.byteArray2Int(packet.length, 4, 1);
                            body = new byte[body_length];
                            packet.body = body;
                            c = in.read(body, 0, body_length);
                            if (c == body_length)
                            {
                                packet.eot = (byte)(in.read() & 0xff);
                                if (packet.eot == AFPpacket.EOT)
                                {
                                    rc = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return rc;

}   /* end of method recvPacket() */



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

    byte[]  app_id = new byte[4];

    app_id[0] = (byte)0x12;
    app_id[1] = (byte)0x34;
    app_id[2] = (byte)0x56;
    app_id[3] = (byte)0x78;

    //
    // send an AFP EDX message packet to the server
    //
    try
    {
        sendAuthenticate(app_id);
        log.println("Sent Auth packet ...");

        recvPacket();
        if (packet.cmd == AFPpacket.CMD_OK_TO_SEND)
        {
            log.println("Recv OK To Send packet ...");
            sendEdx(new String("<xml> Hello World </xml>").getBytes());
            log.println("Sent EDX packet ...");

            recvPacket();
            if (packet.cmd == AFPpacket.CMD_ACK)
            {
                log.println("ACK received!!!  We win!!!!!");
            }
        }
    }
    catch (java.lang.Exception e)
    {
        log.println(e);
        e.printStackTrace();
    }

    //
    // clean up after yourself
    //
    try
    {
        socket.close();
    }
    catch (java.io.IOException e)
    {
        log.println(e);
        e.printStackTrace();
    }

}   /* end of method run() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  sendAuthenticate()                 Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Sends an AFP Authentication packet to the server.                        */
/*                                                                           */
/*  args:                                                                    */
/*       none      no comment                                                */
/*                                                                           */
/*  return:                                                                  */
/*       none                                                                */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private void sendAuthenticate(byte[] argAppId) throws java.io.IOException
{

    packet.clear();
    //
    // set up the packet and then send it
    //
    packet.type = AFPpacket.TYPE_EDX_MSG;
    System.arraycopy(mySenderId, 0, packet.ID, 0, packet.ID.length);
    packet.version = AFPpacket.VERSION_1_0;
    packet.cmd = AFPpacket.CMD_AUTHENTICATE;
    packet.priority = AFPpacket.PRIORITY_EDX;

        // Since we know this is an Authenticate packet, we
        // know the length and content of the body now.
    packet.length[0] = (byte)0x00;
    packet.length[1] = (byte)0x00;
    packet.length[2] = (byte)0x00;
    packet.length[3] = (byte)0x00;
    packet.length[4] = (byte)0x00;
    packet.length[5] = (byte)0x00;
    packet.length[6] = (byte)0x00;
    packet.length[7] = (byte)0x04;

    packet.body = new byte[4];
    packet.body[0] = argAppId[0];
    packet.body[1] = argAppId[1];
    packet.body[2] = argAppId[2];
    packet.body[3] = argAppId[3];

    packet.eot = AFPpacket.EOT;

    out.write(packet.assemble());
    out.flush();

}   /* end of method sendAuthenticate() */


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  sendEdx()                          Copyright (c) ThunderNet Productions  */
/*                                     2001.  All rights reserved.           */
/*                                                                           */
/*  Sends an AFP Edx            packet to the server.                        */
/*                                                                           */
/*  args:                                                                    */
/*      argBody         body of message                                      */
/*                                                                           */
/*  return:                                                                  */
/*       none                                                                */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private void sendEdx(byte[] argBody) throws java.io.IOException
{

    byte[]  body_length;

    packet.clear();
    //
    // set up the packet and then send it
    //
    packet.type = AFPpacket.TYPE_EDX_MSG;
    System.arraycopy(mySenderId, 0, packet.ID, 0, packet.ID.length);
    packet.version = AFPpacket.VERSION_1_0;
    packet.cmd = AFPpacket.CMD_EDX_MESSAGE;
    packet.priority = AFPpacket.PRIORITY_EDX;

        // compute the body length
    body_length = AFP.int2ByteArray(argBody.length, 1);
    packet.length[0] = (byte)0x00;
    packet.length[1] = (byte)0x00;
    packet.length[2] = (byte)0x00;
    packet.length[3] = (byte)0x00;
    System.arraycopy(body_length, 0, packet.length, 4, 4);

    packet.body = argBody;

    packet.eot = AFPpacket.EOT;

    out.write(packet.assemble());
    out.flush();

}   /* end of method sendEdx() */



}   /* end of class AFPtester */


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