Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: da8edd91ee
Fetching contributors…

Cannot retrieve contributors at this time

92 lines (77 sloc) 3.164 kb
# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__
# disable bogus pylint warnings about trying to access or set missing class members
# pylint: disable=E1101,W0201
import re
import sys
import socket
import errno
import asynchat
from TcpBaseSocket import TcpBaseSocket
OPTS_KEYS = ('lineMode', 'connectHandler', 'lineHandler')
class TcpStreamSocket(TcpBaseSocket):
def __init__(self, protocol, dispatcher, optsDict):
TcpBaseSocket.__init__(self, protocol, dispatcher)
for k in OPTS_KEYS:
setattr(self, '_' + k, optsDict[k])
assert self._lineMode # wrapper currently only supports line buffering
self._sock = None
def connect(self, serverPort):
self.endpoint = '%s:%s' % (self._protocol._protoName, serverPort)
host, portString = serverPort.split(':', 1)
port = int(portString)
self.startup()
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
#print 'connecting to server at %s' % self.endpoint
asynchat.async_chat.connect(self, (host, port))
def _initServerSocket(self, sock, addressInfo):
self._sock = sock
hostIp, hostPort = addressInfo
try:
hostName, _, _ = socket.gethostbyaddr(hostIp)
except (socket.gaierror, socket.herror):
# could not resolve hostname, just use ip address
endpointHostName = hostIp
else:
endpointHostName = re.sub('\..*$', '', hostName)
self.endpoint = '%s:%s:%s' % (self._protocol._protoName, endpointHostName, hostPort)
print '\naccepting client connection from %s' % self.endpoint
self.startup()
def write(self, text):
try:
self.send(text)
except socket.error:
self.close()
raise # let SharedScheduler handle the exception
def startup(self):
self._closed = False
self._ibuffer = []
if self._sock != None:
self.set_socket(self._sock) # tell asyncore base class about the socket
self.set_terminator('\n')
def handle_connect(self):
"""What to do when we are connected."""
if self._connectHandler:
self._connectHandler(self)
def handleLine(self, line):
"""What to do when we receive a line."""
if self._lineHandler:
self._lineHandler(self, line)
def collect_incoming_data(self, data):
self._ibuffer.append(data)
def found_terminator(self):
cmd = "".join(self._ibuffer)
cmd = re.sub(r'\r$', '', cmd)
self._ibuffer = []
self.handleLine(cmd)
def handle_error(self):
_errClass, errObject, _errTB = sys.exc_info()[:3]
if isinstance(errObject, socket.error) and errObject.args[0] == errno.ECONNREFUSED:
print >> sys.stderr, 'connection to %s refused' % self.endpoint
self.abort()
else:
self.abort()
raise # pass the buck to SharedScheduler error handler
Jump to Line
Something went wrong with that request. Please try again.