[Bio] / ModelEditor / MeSharedEditServer.py Repository:
ViewVC logotype

View of /ModelEditor/MeSharedEditServer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Fri Jan 7 14:40:33 2005 UTC (14 years, 5 months ago) by efrank
Branch: MAIN
CVS Tags: post-st-migration2, pre-wxPython-2515-migration, post-st-migration, treatman-lastDay-0, preTreatmanDyn
Changes since 1.2: +1 -1 lines
migration to new schematools generated classes.
new chemdb.
factor chem out of seed data factory
imporved configuration of modeleditor

#--------------------------------------------------------------
# 
# MeSharedEditServer.py
#
#
#
# HISTORY:
#    22Oct04 efrank      First version
#
# BUGS:
#--------------------------------------------------------------

"""
XmlRpc based server to shuttle data around between ModelEditor
instances.  Right now, control is managed through AG shared app.
"""

import copy
from threading import Thread
from threading import Lock
import xmlrpclib
from SimpleXMLRPCServer                import SimpleXMLRPCServer
from KahDataServices.XmlRpcConverter   import XmlRpcConverter
from KahDataServices.ConversionManager import ConversionManager
from KahUtils.get_local_hostname       import get_local_hostname


__clipboard      = None
__nCb          = None
__server       = None
__serverThread = None
__cbLock       = None



def meSharedEditServerStart( ):
    """
    @type host: string   
    @type port: int
    """

    global __server, __clipboard, __nCb, __serverThread, __cbLock

    __clipboard = {}
    __nCb     = 0

    __cbLock  = Lock()

    host=get_local_hostname()
    __server = SimpleXMLRPCServer((host, 0))
    (ip,port) = __server.socket.getsockname()
    print "meSharedEditServerStart: using ", (host,port)
    __server.register_function( getRecoElemById )

    __serverThread = Thread(target = meSharedEditServerThread,
                            args   = () )

    print "starting server thread"
    # need to make daemon???
    __serverThread.setDaemon(True)
    __serverThread.start()
    print "server thread started"
    
    return (host,port)


def meSharedEditServerThread():
    global __server, __clipboard, __nCb, __serverThread, __cbLock
    print "in server thread"

    #server = SimpleXMLRPCServer((host, port))
    __server.serve_forever()

    return


def meSharedEditServerKill():
    """ kills the server thread """
    global __server, __clipboard, __nCb, __serverThread, __cbLock
    print "stopping shared edit server"
    #__serverThread.xx()
    return


def getCbDescriptor():
    """
    @rtype: list of pairs (itemName:string, itemId:int)

    Form descriptor for clipboard to put on AG shared AP
    """

    global __server, __clipboard, __nCb, __serverThread, __cbLock

    desc = []
    __cbLock.acquire()
    print __clipboard.keys()
    print __clipboard.values()
    for n in range(__nCb):
        print "trying ", n, __clipboard[n]
        desc.append( ( __clipboard[n].getName(), n) )
    __cbLock.release()

    return desc



def putOnClipboard( re ):
    """
    @type re: RecoElem.  The item to put on the clipboard.
    @rtype: int   Integer id to use to get it back again.

    We do not deep copy the re.  Caller should do that if
    worried.
    """
    global __server, __clipboard, __nCb, __serverThread, __cbLock

    __cbLock.acquire()
    __clipboard[__nCb] = re
    __cbLock.release()
    __nCb += 1

    return __nCb


def getRecoElemByIdLocal(id):
    """
    Just returns recoElem with clipboard key, id.  No xmlrpc 

    @type id: int
    @rtype: KahOM.RecoElem
    """

    global __server, __clipboard, __nCb, __serverThread, __cbLock

    #print "local trying for ", id
    __cbLock.acquire()
    try:
        #print "sending "
        __clipboard[id].dump()
        re = copy.deepcopy( __clipboard[id] )
    except Exception, e:
        print "getRecoElemByIdLocal", e
        re = None

    __cbLock.release()

    return re


def getRecoElemById(id):
    """ Get RecoElem with given integer id (key into clipboard. Return xmlrpc """
    # [0] is needed because xmlrpclib.dumps is used 
    global __server, __clipboard, __nCb, __serverThread, __cbLock

    try:
        print "trying for id ", id
        __cbLock.acquire()
        re = __clipboard[id]
        #print "sending re: "
        #re.dump()
        mgr = ConversionManager (XmlRpcConverter(), debug=1)
        c = mgr.output(re)
        #print "rep is ", c
        __cbLock.release()
        cRe = xmlrpclib.dumps((c,))
        #print "dumps is ", cRe
    except Exception, e:
        __cbLock.release()
        print e
        return None
    
    return cRe

    


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3