Exemple #1
0
    def serve(self):
        DEBUG(self.options.classpath)
        # redirect stdout/stderr to a ZMQ socket
        self.sysout = sys.stdout
        self.syserr = sys.stderr
        try:
            context = zmq.Context()
            socket = context.socket(zmq.PUB)
            DEBUG('binding output to ' + self.options.out_url)
            socket.bind(self.options.out_url)
            sys.stdout = OutStream(socket, 'stdout')
            sys.stderr = sys.stdout
        except Exception:
            print >> self.sysout, \
                  '<<<%s>>> ZMQServer -- setup on %s failed:' \
                  % (os.getpid(), self.options.out_url)
            traceback.print_exc(file=self.sysout)
            sys.exit(1)

        try:
            self.obj = self.ctor()
            DEBUG('obj=' + str(self.obj))
            ZmqCompWrapper.serve(self.obj,
                                 rep_url=self.options.rep_url,
                                 pub_url=self.options.pub_url)
        except Exception:
            print >> self.sysout, \
                  '<<<%s>>> ZMQServer -- wrapper failed:' % os.getpid()
            traceback.print_exc(file=self.sysout)
            sys.exit(1)
class ZMQServer(object):
    ''' wraps an an openmdao object with ZMQ and runs it as a server
    '''
    def __init__(self, options):
        self.options = options

        parts = self.options.classpath.split('.')
        modpath = '.'.join(parts[:-1])
        __import__(modpath)
        try:
            mod = sys.modules[modpath]
            self.ctor = getattr(mod, parts[-1])
        except KeyError:
            print "ZMQServer can't locate %s" % self.options.classpath

    def serve(self):
        DEBUG(self.options.classpath)
        # redirect stdout/stderr to a ZMQ socket
        try:
            context = zmq.Context()
            socket = context.socket(zmq.PUB)
            DEBUG('binding output to ' + self.options.out_url)
            socket.bind(self.options.out_url)
            self.sysout = sys.stdout
            self.syserr = sys.stderr
            sys.stdout = OutStream(socket, 'stdout')
            sys.stderr = sys.stdout
        except Exception, err:
            print err, sys.exc_info()

        self.obj = self.ctor()
        DEBUG('obj=' + str(self.obj))
        ZmqCompWrapper.serve(self.obj,
                             rep_url=self.options.rep_url,
                             pub_url=self.options.pub_url)
        # redirect stdout/stderr to a ZMQ socket
        try:
            context = zmq.Context()
            socket = context.socket(zmq.PUB)            
            DEBUG('binding output to '+self.options.out_url)
            socket.bind(self.options.out_url)
            self.sysout = sys.stdout
            self.syserr = sys.stderr
            sys.stdout = OutStream(socket,'stdout')
            sys.stderr = sys.stdout
        except Exception, err:
            print err,sys.exc_info()

        self.obj = self.ctor()
        DEBUG('obj='+str(self.obj))
        ZmqCompWrapper.serve(self.obj, rep_url=self.options.rep_url, pub_url=self.options.pub_url)

    @staticmethod
    def get_options_parser():
        ''' create a parser for command line arguments
        '''
        parser = OptionParser()
        parser.add_option("-c", "--class",   dest='classpath', 
                          help="module path to class of top level component")
        parser.add_option("-r", "--rep_url", dest="rep_url",
                          help="the address of the RPC proxy")
        parser.add_option("-p", "--pub_url", dest="pub_url",
                          help="the address or the publisher")
        parser.add_option("-o", "--out_url", dest="out_url",
                          help="the address of the output stream")
        return parser