def ping(host, port, count):
    try:
        privId = str(GUID())
        eventClient = EventClient(privId, (host, port), channel)
        eventClient.Start()
        eventClient.Send(ConnectEvent(channel, privId))

        for i in range(1, count):
            eventClient.Send(HeartbeatEvent(channel, "%s -- %d" % (privId, i)))
        queue.put(0)
    except EventClientConnectionException:
        print sys.exc_type, sys.exc_value
        queue.put(2)
    except:
        print sys.exc_type, sys.exc_value
        queue.put(3)
    done.set()
class EventServiceClient:
    '''
    Sends events.
    '''
    def __init__(self, parent):
        '''
        Starts EventServiceSender
        '''
        self.privateId = str(GUID())
        self.eventOutList = parent.eventsOut
        self.eventReceivedList = []
        self.lock = parent.sendLock
        self.nrOfEvent = parent.nrOfEvent
        self.parent = parent
        self.name = parent.index
        parent.index = parent.index + 1

    def GetName(self):
        '''Returns unique name of client'''
        return str(self.name)

    def CreateEventClient(self, channelId, eventLocation):
        '''Create event client and register events'''
        self.eventLocation = eventLocation
        self.channelId = channelId

        # Create event client and connect to event service.
        self.eventClient = EventClient(self.privateId, self.eventLocation,
                                       str(self.channelId))
        self.ReceiveEvents()
        self.eventClient.start()
        self.eventClient.Send(ConnectEvent(self.channelId, self.privateId))

    def ShutDown(self):
        '''Stop event client'''
        self.eventClient.Stop()

    def SendEvents(self, event):
        '''Send events'''
        index = 0

        for i in range(self.nrOfEvent):
            index = index + 1
            event.data.name = " S-" + str(index)

            self.lock.acquire()
            self.eventOutList.append(event.eventType + event.data.name)
            self.lock.release()

            self.eventClient.Send(event)

    def ReceiveEvents(self):
        '''Registers event callbacks'''
        coherenceCallbacks = {
            Event.ENTER: self.AddUserEvent,
            Event.EXIT: self.RemoveUserEvent,
            Event.MODIFY_USER: self.ModifyUserEvent,
            Event.ADD_DATA: self.AddDataEvent,
            Event.UPDATE_DATA: self.UpdateDataEvent,
            Event.REMOVE_DATA: self.RemoveDataEvent,
            Event.ADD_SERVICE: self.AddServiceEvent,
            Event.REMOVE_SERVICE: self.RemoveServiceEvent,
            Event.ADD_APPLICATION: self.AddApplicationEvent,
            Event.REMOVE_APPLICATION: self.RemoveApplicationEvent,
            Event.ADD_CONNECTION: self.AddConnectionEvent,
            Event.REMOVE_CONNECTION: self.RemoveConnectionEvent,
            Event.SET_CONNECTIONS: self.SetConnectionsEvent,
            Event.ADD_STREAM: self.AddStreamEvent,
            Event.MODIFY_STREAM: self.ModifyStreamEvent,
            Event.REMOVE_STREAM: self.RemoveStreamEvent
        }

        for e in coherenceCallbacks.keys():
            self.eventClient.RegisterCallback(e, coherenceCallbacks[e])

    #
    # Event callbacks
    #

    def AddUserEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def RemoveUserEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def ModifyUserEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def AddDataEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def UpdateDataEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def RemoveDataEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def AddServiceEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def RemoveServiceEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def AddApplicationEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def RemoveApplicationEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def AddConnectionEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def RemoveConnectionEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def SetConnectionsEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def AddStreamEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def ModifyStreamEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()

    def RemoveStreamEvent(self, event):
        self.eventReceivedList.append(event.eventType + event.data.name)
        self.parent.ReceivedEvent()
Exemple #3
0
class SharedCovise(wxApp):
    """
    The SharedCovise is an Access Grid 2 Application.
    """
    appName = "Shared Covise"
    appDescription = "A shared covise is a set of slides that someone presents to share an idea, plan, or activity with a group."
    appMimetype = "application/x-ag-shared-covise"

    def __init__(self, arg, url, log=None):
        wxApp.__init__(self, arg)
        """
        This is the constructor for the Shared Covise.
        """
        # Initialize state in the shared covise
        self.url = url
        self.eventQueue = Queue.Queue(5)
        self.log = log
        self.masterId = None
        self.numSteps = 0
        self.hostName = "vision.rus.uni-stuttgart.de"
        self.serverPort = 31098
        self.masterSocket = None

        # Get a handle to the application object in the venue
        self.log.debug("Getting application proxy (%s).", url)
        self.appProxy = Client.Handle(self.url).GetProxy()

        # Join the application object, get a private ID in response
        self.log.debug("Joining application.")
        (self.publicId, self.privateId) = self.appProxy.Join()

        # Get the information about our Data Channel
        self.log.debug("Retrieving data channel information.")
        (self.channelId, esl) = self.appProxy.GetDataChannel(self.privateId)

        # Connect to the Data Channel, using the EventClient class
        # The EventClient class is a general Event Channel Client, but since
        # we use the Event Channel for a Data Channel we can use the
        # Event Client Class as a Data Client. For more information on the
        # Event Channel look in AccessGrid\EventService.py
        # and AccessGrid\EventClient.py
        self.log.debug("Connecting to data channel.")
        self.eventClient = EventClient(self.privateId, esl, self.channelId)
        self.eventClient.start()
        self.eventClient.Send(ConnectEvent(self.channelId, self.privateId))

        # Register callbacks with the Data Channel to handle incoming
        # events.
        self.log.debug("Registering for events.")
        self.eventClient.RegisterCallback(SharedCovEvent.MASTER,
                                          self.RecvMaster)
        self.eventClient.RegisterCallback(SharedCovEvent.CLEAN,
                                          self.CleanCovise)

        self.masterHost = ""
        self.masterPort = 0
        self.masterHost = self.appProxy.GetData(self.privateId,
                                                SharedCovKey.MASTERHOST)
        self.masterPort = self.appProxy.GetData(self.privateId,
                                                SharedCovKey.MASTERPORT)
        self.frame.SetMasterHost(self.masterHost + ":" + str(self.masterPort))

        self.log.debug("fit.")

        self.log.debug("SharedCovise V1.0")

        # read configueation file
        userapp = Platform.GetUserAppPath()
        configFileName = userapp + "\\SharedCovise.config"
        configFile = None
        try:
            configFile = file(configFileName, 'r')
        except:
            print "could no open config file"
        if configFile != None:
            entry = configFile.readline(200)
            if entry != "":
                self.hostName = entry.rstrip("\n")
            entry = configFile.readline(200)
            if entry != "":
                self.serverPort = int(entry)

        self.frame.SetPort(self.serverPort)
        self.frame.SetHost(self.hostName)

        if self.masterHost == self.hostName and self.serverPort == self.serverPort:
            #connect to our Daemon and find out if covise is still running
            message = "check"
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((HOST, PORT))
                s.send(message)
                self.masterSocket = s
                self.appProxy.SetData(self.privateId, SharedCovKey.MASTERHOST,
                                      self.hostName)
                self.appProxy.SetData(self.privateId, SharedCovKey.MASTERPORT,
                                      self.serverPort)
                self.eventClient.Send(
                    Event(SharedCovEvent.MASTER, self.channelId,
                          (self.publicId, self.publicId)))
                Thread(target=self.monitorMaster).start()
            except:
                self.appProxy.SetData(self.privateId, SharedCovKey.MASTERHOST,
                                      "")
                self.appProxy.SetData(self.privateId, SharedCovKey.MASTERPORT,
                                      0)
                self.eventClient.Send(
                    Event(SharedCovEvent.MASTER, self.channelId,
                          (self.publicId, self.publicId)))
                print "could not connect to AccessGridDaemon\n"

    def OnInit(self):
        # Create the GUI

        self.frame = SharedCoviseFrame(NULL, -1, "Shared Covise", self)
        self.frame.Fit()
        self.frame.Show(true)
        self.SetTopWindow(self.frame)

        return true

    def Start(self):
        """
        Start the UI app loop
        """
        self.MainLoop()

        # When the quit event gets processed, the running flag gets cleared
        self.log.debug("Shutting down...")
        self.Quit()

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #
    # Methods registered as callbacks with the UI
    # These methods typically put an event in the event queue.
    #

    def DoJoin(self):
        """
        This method handles NEXT button presses in the UI.
        The event is only sent if the local user is the "master"
        """

        self.log.debug("Method DoJoin called")
        # Put the event on the queue
        if self.masterHost == "":
            #become master and start covise
            #tell the master which client wants to join
            HOST = self.hostName  # Our AccessGrid Daemon
            PORT = self.serverPort  # Our port number
            message = "startCovise\n"
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((HOST, PORT))
                s.send(message)
                self.masterSocket = s
                self.appProxy.SetData(self.privateId, SharedCovKey.MASTERHOST,
                                      self.hostName)
                self.appProxy.SetData(self.privateId, SharedCovKey.MASTERPORT,
                                      self.serverPort)
                self.eventClient.Send(
                    Event(SharedCovEvent.MASTER, self.channelId,
                          (self.publicId, self.publicId)))
                Thread(target=self.monitorMaster).start()
            except:
                print "could not connect to AccessGridDaemon\n"
            ##data = s.recv(1024)
        else:
            #tell the master which client wants to join
            HOST = self.masterHost  # The master AccessGrid Daemon
            PORT = self.masterPort  # The master Port
            message = "join " + self.hostName + ":" + str(
                self.serverPort) + "\n"
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((HOST, PORT))
                s.send(message)
            except:
                print "could not connect to masters AccessGridDaemon\n"
            ##data = s.recv(1024)
            s.close()
            #print 'Received', `data`

        #print "join done\n"
    def monitorMaster(self):
        data = self.masterSocket.recv(1024)
        while data != "" and data.find("masterLeft") < 0:
            #print "XX"+data+"XX"+str(data.find("masterLeft"))+"\n"
            data = self.masterSocket.recv(1024)

        #print "XX"+data+"XX\n"
        self.appProxy.SetData(self.privateId, SharedCovKey.MASTERHOST, "")
        self.appProxy.SetData(self.privateId, SharedCovKey.MASTERPORT, 0)
        self.eventClient.Send(
            Event(SharedCovEvent.MASTER, self.channelId,
                  (self.publicId, self.publicId)))
        self.masterSocket.close()
        #print "Master Covise left\n"
        self.masterSocket = None

    def CleanCovise(self, event):
        self.log.debug("Method CleanCovise called")
        # Put the event on the queue
        HOST = self.hostName  # Our AccessGrid Daemon
        PORT = self.serverPort  # Our port number
        message = "cleanCovise\n"
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((HOST, PORT))
            s.send(message)
            s.close()
        except:
            print "could not connect to AccessGridDaemon\n"
            ##data = s.recv(1024)

    def monitorMaster(self):
        data = self.masterSocket.recv(1024)
        while data != "" and data.find("masterLeft") < 0:
            #print "XX"+data+"XX"+str(data.find("masterLeft"))+"\n"
            data = self.masterSocket.recv(1024)

        #print "XX"+data+"XX\n"
        self.appProxy.SetData(self.privateId, SharedCovKey.MASTERHOST, "")
        self.appProxy.SetData(self.privateId, SharedCovKey.MASTERPORT, 0)
        self.eventClient.Send(
            Event(SharedCovEvent.MASTER, self.channelId,
                  (self.publicId, self.publicId)))
        self.masterSocket.close()
        #print "Master Covise left\n"
        self.masterSocket = None

    def saveConfig(self):

        userapp = Platform.GetUserAppPath()
        configFileName = userapp + "\\SharedCovise.config"
        configFile = None
        try:
            configFile = file(configFileName, 'w')
        except:
            print "could no save config file"
        if configFile != None:
            configFile.write(self.hostName + "\n")
            configFile.write(str(self.serverPort) + "\n")

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #
    # Methods registered as callbacks with EventClient
    # These methods typically put an event in the event queue.
    #

    def RecvMaster(self, event):
        """
        This callback puts a "master" event from the network
        on the event queue
        """

        self.log.debug("Method RecvMaster called")
        self.masterHost = ""
        self.masterPort = 0
        self.masterHost = self.appProxy.GetData(self.privateId,
                                                SharedCovKey.MASTERHOST)
        self.masterPort = self.appProxy.GetData(self.privateId,
                                                SharedCovKey.MASTERPORT)
        self.frame.SetMasterHost(self.masterHost + ":" + str(self.masterPort))

    def Quit(self, data=None):
        """
        This is the _real_ Quit method that tells the viewer to quit
        """
        # Turn off the main loop
        self.log.debug("Method Quit called")
        if self.masterSocket != None:
            self.masterSocket.close()
        if self.masterHost == self.hostName:
            self.appProxy.SetData(self.privateId, SharedCovKey.MASTERHOST, "")
            self.appProxy.SetData(self.privateId, SharedCovKey.MASTERPORT, 0)
            self.eventClient.Send(
                Event(SharedCovEvent.MASTER, self.channelId,
                      (self.publicId, self.publicId)))