// 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.protocol.*;
import com.ireasoning.protocol.tl1.*;

/**
 * This class demonstrates a alarm receiver for collecting TL1 notification
 * messages. It connects to a local TL1 agent at port 9000, and waits for
 * alarms.
 */
 
public class alarm 
{
    /**
     * One implementation of TL1 notification listener, using the waitForNotificationMsg method
     */
    private static void alarmImpl1()
    {
        try
        {
            TL1Session session = new TL1Session("localhost", 9000);

            TL1Command  req = TL1Command.act_user("abc", "123");
            print(session.send(req));

            //launch a new thread which keeps sending a randomly picked command
            //to agent every 60 seconds
            session.launchKeepAliveThread("rtrv-eqpt:NodeA:SLOT-ALL:123;", 60, false);
            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();
        }
    }
 
    /**
     * One implementation of TL1 notification listener, using the asynchronous handleMsg method
     */
    private static void alarmImpl2()
    {
        try
        {
            String hostname = "localhost";
            int port = 9000;
            System.out.println( "login to " + hostname + ":" + port);

            TL1Session session = new TL1Session(hostname, port);
            session.addListener(new ListenerImpl());
            TL1Command req = new TL1Command();

            //Login to the TL1 agent
            req = TL1Command.act_user("abc", "123");
            //synchronously send login command
            session.send(req);
            
            //Starts a thread listening for notifications
            session.startThread();
        }
        catch(Exception e)
        {
            System.out.println( e);
            e.printStackTrace();
        }
    }
 
    static class ListenerImpl implements Listener
    {
        /**
         * Callback method, to be called when there is new message
         */
        public void handleMsg(Object msgSender, Msg msg)
        {
            TL1OutputMsg tl1msg = (TL1OutputMsg) msg;
            print(new TL1OutputMsg[]{tl1msg});
        }
    }

    public static void main(String[] args)  
    {
        //Two implementations of TL1 notificaiton listener:

        //alarmImpl1();
        alarmImpl2();
    }

    static void print(TL1OutputMsg[] ms)
    {
        TL1Util.printOutputMsg(ms);
    }
}//end of class alarm