// Home | Go Back //

/*
 * Copyright (c) 2002-2003 iReasoning Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by iReasoning Inc. as part
 * of an iReasoning Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of iReasoning Inc.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD IREASONING SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */

import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.*;
import com.ireasoning.protocol.tl1.*;
/**
 * This class demonstrates some basic usage of TL1 APIs. It connects to a local
 * TL1 agent at port 9000, and issues several commands to get information.
 */
public class tl1 
{
    static class NotificationListener extends Thread
    {
        public void run()
        {
            try
            {
                TL1Session session = new TL1Session("localhost", 9000);
                TL1Command req = TL1Command.act_user("abc", "123");
                session.send(req);
                while(true)
                {//waiting for autonomous messages
                    TL1NotificationMsg[] notes = session.waitForNotificationMsg();
                    for (int i = 0; i < notes.length ; i++) 
                    {
                        System.out.println( "Got a new alarm:\r\n" + notes[i]);
                    }
                }
            }
            catch(Exception e)
            {
                System.out.println( e);
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args)  
    {
        try
        {
            System.out.println( "login to localhost");
            TL1Session session = new TL1Session("localhost", 9000);
            new NotificationListener().start();//start a thread for listening to notification messages
            TL1Command req = new TL1Command();

            System.out.println( "send login command" );
            req = TL1Command.act_user("abc", "123");
            session.send(req);

            req.setCommand("rtrv-inv:NodeA:SLOT-ALL:123;");
            print(session.send(req));
            
            req.setCommand("rtrv-eqpt:NodeA:SLOT-ALL:123;");
            TL1ResponseMsg  msg = (session.send(req))[0];

            //demonstrates parsing payload data
            TL1Line [] lines =  msg.getPayloadData();
            for (int j = 0; j < lines.length ; j++) 
            {
                TL1Section [] sections = lines[j].getSections();
                System.out.println( "cardname=" + lines[j].lookupValue("CARDNAME"));
                System.out.println( "line: " + j);
                for (int k = 0; k < sections.length ; k++) 
                {
                    System.out.println( "section: " + k);
                    TL1Field [] fields = sections[k].getFields();
                    for ( int m = 0; m < fields.length; m++)
                    {
                        TL1Field f = fields[m];
                        System.out.println("<" + f.getName() + "> = <" + f.getValue()
                                + ">");
                    }
                }
            }

            //log out
            req = TL1Command.canc_user("abc");
            session.send(req);

            //session.close();
        }
        catch(Exception e)
        {
            System.out.println( e);
            e.printStackTrace();
        }
    }

    /**
     * Prints out response messages
     */
    static void print(TL1ResponseMsg [] ms)
    {
        TL1Util.printOutputMsg(ms);
    }
}//end of class tl1