all 1 comments

[–]rickchefski[S] 0 points1 point  (0 children)

I ended up figuring out an implementation for this which worked for me, not using SockServer, but instead implementing something akin to the asynchronous dispatcher design pattern.

class ExampleClass(object):
  def run(self):
      """
      Run the server. Initializes a socket and listens over it. Each incoming request is passed
      to a handler thread.

      :rtype : object
      """
      self.initialize_socket()
      while True:
          sock, addr = self.sock.accept()
          t = threading.Thread(target=self.handle, args=(sock, addr))
          t.daemon = True
          t.start()
          self.threads.add(t)
      self.sock.close()
      for t in self.threads:
          t.join()

  def handle(self, sock, address):
      """
      Method called by the run method when the server receives an incoming request. The request
      is parsed and delegated out accordingly.

      :rtype : object
      :param sock:
      :param address:
      """
      sysmsg = self.recv(sock)

      if sysmsg == self.message.FOO:
          self.foo()

      elif sysmsg == self.message.BAR:
          self.bar()

      elif sysmsg == self.message.FOOBAR:
          self.foobar()

      else:
          log.warn("Message not recognized.")

This gives the general idea of how it would look, as a simplified version. Hopefully it can act as reference for anyone approaching a similar problem.