Esempio n. 1
0
    def shutdown(self, args):
        """
    Shuts down the session, shuts down the underlying SocketCommunicator.

    @param args: the args tuple for the shutdown_hook.
    @type  args: tuple
    """
        if len(args) > 0:
            quiet = 1
        else:
            quiet = 0

        # unregister with the shutdown hook
        # exported.hook_unregister("shutdown_hook", self.shutdown)
        if self.getName() != "common":
            if quiet == 0:
                event.OutputEvent(
                    "Session %s disconnected.\n\"#zap %s\" to kill the session.\n"
                    % (self._name, self._name)).enqueue()
            exported.hook_spam("disconnect_hook", {
                "session": self,
                "host": self._host,
                "port": self._port
            })

            if self._socket:
                self._socket.shutdown()

            self._host = None
            self._port = 0
            self._socket = None
Esempio n. 2
0
  def run(self):
    """
    While the connection hasn't been shut down, we spin through this
    loop retrieving data from the mud,
    """
    from lyntin import exported
    try:
      data = ''
      while not self._shutdownflag:
        newdata = self._pollForData()

        if newdata:
          newdata = self._filterIncomingData(newdata)
          if newdata == "":
            continue

          last_index = 0
          alldata = (data+newdata).replace("\r","")
          # incrementally walk through each line in the data,
          # adjusting last_index to the end of the previous match
          for (m) in self._line_regex.finditer(alldata):
            oneline = alldata[last_index:m.end()]
            last_index = m.end()
            self.handleData(oneline)
          # keep the remainder (empty if alldata ended with a delimiter)
          data = alldata[last_index:]

        elif newdata == '':
          # if we got back an empty string, then something's amiss
          # and we should dump them.
          if data:
            self.handleData(data)
          if self._shutdownflag == 0 and self._session:
            self._session.shutdown(())
          break

        elif not self._good_prompts and data:
          # Now we have rest of the input which is neither delimited prompt
          # nor complete line, and we yet did not see this server
          # delimiting it's prompts with telnet GA or EOR option.
          # We'll handle these data because the socket read was timed out.
          self.handleData(data)
          data = ''

    except SystemExit:
      if self._session:
        self._session.shutdown(())

    except:
      exported.write_traceback("socket exception")
      if self._session:
        self._session.shutdown(())

    # if we hit this point, we want to shut down the socket
    try:    self._sock.shutdown(2)
    except: pass

    try:    self._sock.close()
    except: pass

    self._sock = None
    self._session = None

    # sometimes the mud will hose up with echo off--we want to kick it
    # on again.
    self._config.change("mudecho", "on")

    # output message so the user knows what happened.
    event.OutputEvent(message.Message("Lost connection to: %s\n" % self._host)).enqueue()