#!/usr/bin/python

# IIclient.py
#       --copyright--                   Copyright 2007 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       September 23, 2006      bar
#       October 31, 2006        bar     expose port number
#                                       make sure the query goes out
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       December 1, 2007        bar     use tzlib's elapsed_time for timing
#       May 17, 2008            bar     email adr
#       --eodstamps--
##      \file
#
#
#       Be a client to an II (Instant Information) server server.
#
#

from    types                   import  ListType, TupleType

from    AmpClient               import  *
from    tgcmsg                  import  *

import  tzlib


IIport  = 13460


class an_ii_client(an_amp_client) :

    def __init__(me, host = "localhost", port = IIport, user_name = None, password = None, iiname = None, timeout = None) :

        host        =  host or "localhost"
        port        =  port or IIport
        if  int(port) == 0 :
            port    =  IIport


        iiname      =  iiname or user_name
        me.iiname   =  iiname

        if  timeout == None:
            timeout =  180.0
        me.timeout  =  timeout

        an_amp_client.__init__(me, host, port, user_name, password)




    def make_query(me, q, iiname = None, timeout = None) :
        """
            Make a query and return a response.
        """

        if  not iiname  :
            iiname      = me.iiname
        if  not iiname  :
            iiname      = "IIclient_py"

        if  not timeout :
            timeout     = me.timeout

        if  (not isinstance(q, ListType)) and (not isinstance(q, TupleType)) :
            q   = [ q ]

        q       = [ iiname ] + q

        if  me.user_name :
            q   = [ "authQuery" ] + q
        else :
            q   = [ "query"     ] + q

        if  me.screen_info :
            #                                                           print "iiclient q", q
            pass

        t   = tzlib.elapsed_time()

        sid = None

        while True :
            if  (not sid) and me.ready() :
                sid = me.sid_send(q)
                if  sid and me.screen_info :
                    # print "SID", sid
                    pass
                pass

            if  me.do_it() :

                while True :
                    r   = me.get_msg()
                    if  not r : break

                    if  (len(r) >= 3) and (r[1].lower() == "lstquery") :

                        try :
                            rsid    = int(r[0])
                            if  rsid   == sid :
                                return(r[-1])
                            pass
                        except ValueError :
                            pass

                        pass
                    pass

                pass

            if tzlib.elapsed_time() - t >= timeout :
                break
            pass

        return(None)


    pass                                                    # end of class an_ii_client



#
#
#   Test code.
#
#
if __name__ == '__main__' :
    import  sys

    if  len(sys.argv) < 7 :
        print "Tell me a server and port to connect to and user name and password and ii_name and query!"
    else :
        clt             = an_ii_client(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
        clt.screen_info = True

        while True :
            clt.do_it()
            if  clt.logged_on :
                r   = clt.make_query(sys.argv[6])
                if  r == None :
                    print "No reply"
                    sys.exit(101)

                print "reply: [" + r + "]"

                sys.exit(0)
            pass

    pass



__all__ = [
            'an_ii_client',

            'IIport',
          ]


#
#
#
# eof

