"""
Module **sesf.msgtransfer2.service** has class ``Service``, which is
the concrete version of msgtransfer2 service. RPC-accessable via pyrodaemon
`sesf.msgtransfer2.service`. Run ``python service.py`` to start
a Service instance, which can then be accessed by a distributed program
that uses msgtransfer2 service.
"""
from sesf.msgtransfer2.serviceparts import ServiceParts
import Pyro4, argparse, time, random
from threading import Thread, Lock, Condition
from sesf.util.service import (do_service_output, do_service_input)
from sesf.util.pyro import (start_pyrodaemon)
def random_delay():
time.sleep(random.random()*1)
# Expose class instance to Pyro4 RPCs
[docs]@Pyro4.expose
class Service():
def __init__(self):
self.serviceparts = ServiceParts()
self.lck = Lock()
self.cond = Condition(self.lck)
# start Pyro4 daemon, register with Pyro4 nameserver, enter requestLoop
Thread(name='pyrodaemon', target=start_pyrodaemon,
args=(self, 'sesf.msgtransfer2.service')).start()
def send(self, j, msg):
x = self.serviceparts
random_delay()
# print(j, "x.send call", (msg,), flush=True)
do_service_input(j, x.send_CIC, x.send_CU, (msg,), self.cond)
rval = do_service_output(j, x.send_ROC, x.send_RU, self.cond)
# rval == (1, True) | (1, False)
rval = rval[0]
random_delay()
# print(j, "x.send return", rval, flush=True)
return rval
def recv(self, j):
x = self.serviceparts
random_delay()
# print(j, "x.recv call", flush=True)
do_service_input(j, x.recv_CIC, x.recv_CU, (), self.cond)
rval = do_service_output(j, x.recv_ROC, x.recv_RU, self.cond)
# rval == ((False),) | ((True, msg),)
rval = rval[0]
random_delay()
# print(j, "x.recv return", rval, flush=True)
return rval
@Pyro4.oneway
def end(self, j):
x = self.serviceparts
random_delay()
# print(j, "x.end call", flush=True)
do_service_input(j, x.end_CIC, x.end_CU, (), self.cond)
rval = do_service_output(j, x.end_ROC, x.end_RU, self.cond)
time.sleep(2)
self.pyrodaemon.shutdown()
return rval
###### End ServiceModel #########################################
if __name__ == '__main__':
print('starting msgtransfer2.service', flush=True)
service = Service()