コード例 #1
0
 def main(self):
    self.myPLS = TCPServer(listenport=self.listenport)
    self.link((self.myPLS,"protocolHandlerSignal"),(self,"_oobinfo"))
    self.addChildren(self.myPLS)
    yield _Axon.Ipc.newComponent(self.myPLS)
    while 1:
       self.pause()
       if self.dataReady("_oobinfo"):
          data = self.recv("_oobinfo")
          if isinstance(data,_ki.newCSA):
             yield self.handleNewCSA(data)
          if isinstance(data,_ki.shutdownCSA):# socketShutdown):
             # Socket shutdown and died.
             # Unlink the CSA. A new one might connect!
             theCSA = data.object
             if theCSA in self.rejectedCSAs:
                 self.rejectedCSAs.remove(theCSA)
             else:
                 self.send(producerFinished(self), "signal")
                 self.CSA = None
             self.removeChild(theCSA)
             yield 1
       yield 1
コード例 #2
0
class SingleServer(_Axon.Component.component):
   Inboxes= { "inbox"    : "Data received on this inbox is sent to the first client who connects",
              "control"  : "Default inbox, not actually listened to",  
              "_oobinfo" : "We receive notification of connection on this inbox"
            }
   Outboxes={ "outbox"      : "Any data received from the first connection accepted is sent to this outbox",
              "signal"      : "When the client disconnects a producerFinished message is sent here", 
              "_CSA_signal" : "Outbox for sending messages to the CSA. Currently unused."
            }
   def __init__(self, port=1601):
      super(SingleServer,self).__init__()
      self.listenport = port
      self.CSA = None
      self.rejectedCSAs = []
      self.myPLS = None

   def main(self):
      self.myPLS = TCPServer(listenport=self.listenport)
      self.link((self.myPLS,"protocolHandlerSignal"),(self,"_oobinfo"))
      self.addChildren(self.myPLS)
      yield _Axon.Ipc.newComponent(self.myPLS)
      while 1:
         self.pause()
         if self.dataReady("_oobinfo"):
            data = self.recv("_oobinfo")
            if isinstance(data,_ki.newCSA):
               yield self.handleNewCSA(data)
            if isinstance(data,_ki.shutdownCSA):# socketShutdown):
               # Socket shutdown and died.
               # Unlink the CSA. A new one might connect!
               theCSA = data.object
               if theCSA in self.rejectedCSAs:
                   self.rejectedCSAs.remove(theCSA)
               else:
                   self.send(producerFinished(self), "signal")
                   self.CSA = None
               self.removeChild(theCSA)
               yield 1
         yield 1

   def stop(self):
       self.send(producerFinished(self), "signal")
       self.CSA._deliver(producerFinished(self),"control")
       self.myPLS._deliver(serverShutdown(self),"control")
       super(SingleServer,self).stop()

   def handleNewCSA(self, data):
      newCSA = data.object
      if self.CSA is None:
         self.CSA = newCSA

         # Wire in the CSA to the outside connectivity points
         self.link((self.CSA,"outbox"),(self,"outbox"), passthrough=2)
         self.link((self,"inbox"),(self.CSA,"inbox"), passthrough=1)
         self.link((self,"_CSA_signal"), (self.CSA, "control"))

      else:
         # We already have a connected socket, so we want to throw this connection away.
         # we'll send it a stop signal, but we still need to add it to the scheduler
         # otherwise itdoesn't get a chance to act on it. We'll add it to a 'rejected'
         # list so we know to clean it up slightly differently when we get told it has
         # shut down
         newCSA._deliver(producerFinished(self),"control")
         self.rejectedCSAs.append(newCSA)

      self.addChildren(newCSA)
      return _Axon.Ipc.newComponent(newCSA)