Back to index

nordugrid-arc-nox  1.1.0~rc6
EchoService.py
Go to the documentation of this file.
00001 import arc
00002 import time
00003 from arcom.security import parse_ssl_config, AuthRequest
00004 from arcom.logger import Logger
00005 log = Logger(arc.Logger(arc.Logger_getRootLogger(), 'EchoService.py'))
00006 
00007 wsrf_rp_ns = "http://docs.oasis-open.org/wsrf/rp-2"
00008 echo_ns = "http://www.nordugrid.org/schemas/echo"
00009 import threading
00010 
00011 class EchoService:
00012 
00013     def __init__(self, cfg):
00014         log.msg(arc.INFO, "EchoService (python) constructor called")
00015         # get the response-prefix from the config XML
00016         self.prefix = str(cfg.Get('prefix'))
00017         # get the response-suffix from the config XML
00018         self.suffix = str(cfg.Get('suffix'))
00019         log.msg("EchoService (python) has prefix '%s' and suffix '%s'" % (self.prefix, self.suffix))
00020         self.ssl_config = parse_ssl_config(cfg)
00021         thread_test = str(cfg.Get('ThreadTest'))
00022         if thread_test:
00023             threading.Thread(target = self.infinite, args=[thread_test]).start()
00024         
00025     def __del__(self):
00026         log.msg(arc.INFO, "EchoService (python) destructor called")
00027         
00028     def infinite(self, url):
00029         log.msg(arc.INFO, "EchoService (python) thread test starting")
00030         i = 0
00031         while True:
00032             try:
00033                 i += 1
00034                 cfg = arc.MCCConfig()
00035                 s = arc.ClientSOAP(cfg, arc.URL(url))
00036                 ns = arc.NS('echo', echo_ns)
00037                 outpayload = arc.PayloadSOAP(ns)
00038                 outpayload.NewChild('echo:echo').NewChild('echo:say').Set('hi!')
00039                 resp, status = s.process(outpayload)
00040                 log.msg(arc.INFO, "EchoService (python) thread test, iteration", i, status)
00041                 time.sleep(3)
00042             except Exception, e:
00043                 log.msg()                
00044 
00045     def RegistrationCollector(self, doc):
00046         regentry = arc.XMLNode('<RegEntry />')
00047         SrcAdv = regentry.NewChild('SrcAdv')
00048         SrcAdv.NewChild('Type').Set('org.nordugrid.tests.echo_python')
00049 
00050         #Place the document into the doc attribute
00051         doc.Replace(regentry)
00052         return True
00053 
00054     def GetLocalInformation(self):
00055         ns = arc.NS({'':'http://schemas.ogf.org/glue/2008/05/spec_2.0_d41_r01'})
00056         info = arc.XMLNode(ns,'Domains')
00057         service_node = info.NewChild('AdminDomain').NewChild('Services').NewChild('Service')
00058         service_node.NewChild('Type').Set('org.nordugrid.tests.echo_python')
00059         endpoint_node = service_node.NewChild('Endpoint')
00060         endpoint_node.NewChild('HealthState').Set('ok')
00061         endpoint_node.NewChild('ServingState').Set('production')    
00062         return info
00063 
00064     def process(self, inmsg, outmsg):
00065         log.msg("EchoService (python) 'Process' called")
00066         # time.sleep(10)
00067         # get the payload from the message
00068         inpayload = inmsg.Payload()
00069         log.msg(arc.VERBOSE, 'AuthRequest(inmsg) = ', AuthRequest(inmsg))
00070         log.msg(arc.VERBOSE, 'inmsg.Attributes().getAll() = ', inmsg.Attributes().getAll())
00071         log.msg(arc.INFO, "EchoService (python) got:", inpayload.GetXML())
00072         # the first child of the payload should be the name of the request
00073         request_node = inpayload.Child()
00074         # get the namespace
00075         request_namespace = request_node.Namespace()
00076         log.msg("EchoService (python) request_namespace:",  request_namespace)
00077         if request_namespace != echo_ns:
00078             if request_namespace == wsrf_rp_ns:
00079                 outpayload = arc.PayloadSOAP(arc.NS({'wsrf-rp':wsrf_rp_ns}))
00080                 outpayload.NewChild('wsrf-rp:GetResourcePropertyDocumentResponse').NewChild(self.GetLocalInformation())
00081                 outmsg.Payload(outpayload)
00082                 log.msg("outpayload", outpayload.GetXML())
00083                 return arc.MCC_Status(arc.STATUS_OK)
00084             raise Exception, 'wrong namespace. expected: %s' % echo_ns
00085         # get the name of the request without the namespace prefix
00086         # this is the name of the Body node's first child
00087         request_name = request_node.Name()
00088         # create an answer payload
00089         ns = arc.NS({'echo': echo_ns})
00090         outpayload = arc.PayloadSOAP(ns)
00091         # here we defined that 'echo' prefix will be the namespace prefix of 'http://www.nordugrid.org/schemas/echo'
00092         # get the message
00093         say = str(request_node.Get('say'))
00094         # put it between the response-prefix and the response-suffix
00095         hear = self.prefix + say + self.suffix
00096         if request_name == 'double':
00097             # if the name of the request is 'double'
00098             # we create a new echo message which we send to http://localhost:60000/Echo using the ClientSOAP object
00099             cfg = arc.MCCConfig()
00100             ssl = False
00101             if self.ssl_config:
00102                 cfg.AddCertificate(self.ssl_config.get('cert_file', None))
00103                 cfg.AddPrivateKey(self.ssl_config.get('key_file', None))
00104                 if self.ssl_config.has_key('ca_file'):
00105                     cfg.AddCAFile(self.ssl_config.get('ca_file', None))
00106                 else:
00107                     cfg.AddCADir(self.ssl_config.get('ca_dir', None))
00108                 ssl = True
00109             if ssl:
00110                 url = arc.URL('https://localhost:60000/Echo')
00111                 log.msg('Calling https://localhost:60000/Echo using ClientSOAP')
00112             else:
00113                 url = arc.URL('http://localhost:60000/Echo')
00114                 log.msg('Calling http://localhost:60000/Echo using ClientSOAP')
00115             # creating the ClientSOAP object
00116             s = arc.ClientSOAP(cfg, url)
00117             new_payload = arc.PayloadSOAP(ns)
00118             # creating the message
00119             new_payload.NewChild('echo:echo').NewChild('echo:say').Set(hear)
00120             log.msg('new_payload', new_payload.GetXML())
00121             # sending the message
00122             resp, status = s.process(new_payload)
00123             # get the response
00124             hear = str(resp.Get('echoResponse').Get('hear'))
00125         elif request_name == 'httplib':
00126             # if the name of the request is 'httplib'
00127             # we create a new echo message which we send to http://localhost:60000/echo using python's built-in http client
00128             import httplib
00129             log.msg('Calling http://localhost:60000/Echo using httplib')
00130             # create the connection
00131             h = httplib.HTTPConnection('localhost', 60000)
00132             new_payload = arc.PayloadSOAP(ns)
00133             # create the message
00134             new_payload.NewChild('echo:echo').NewChild('echo:say').Set(hear)
00135             log.msg('new_payload', new_payload.GetXML())
00136             # send the message
00137             h.request('POST', '/Echo', new_payload.GetXML())
00138             r = h.getresponse()
00139             response = r.read()
00140             log.msg(response)
00141             resp = arc.XMLNode(response)
00142             # get the response
00143             hear = str(resp.Child().Get('echoResponse').Get('hear'))
00144         elif request_name == 'wait':
00145             log.msg('Start waiting 10 sec...')
00146             time.sleep(10)
00147             log.msg('Waiting ends.')
00148         # we create a node at '/echo:echoResponse/echo:hear' and put the string in it
00149         outpayload.NewChild('echo:echoResponse').NewChild('echo:hear').Set(hear)
00150         outmsg.Payload(outpayload)
00151         log.msg("outpayload", outpayload.GetXML())
00152         # return with STATUS_OK
00153         return arc.MCC_Status(arc.STATUS_OK)
00154 
00155 # you can easily test this with this shellscript:
00156 """
00157 MESSAGE='<?xml version="1.0"?><soap-env:Envelope xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:star="http://www.nordugrid.org/schemas/echo"><soap-env:Body><star:echo><star:say>HELLO</star:say></star:echo></soap-env:Body></soap-env:Envelope>'
00158 echo Request:
00159 echo $MESSAGE
00160 echo
00161 echo Response:
00162 curl -d "$MESSAGE" http://localhost:60000/Echo
00163 echo
00164 """
00165 #