Okay, I've made some huge progress, I think with my Telnet connection stuff. I'm curious what the Twisted pros think of this..syntax, if it is correct, etc. Here is what I'm going for, I want to have this application running, which provides an XML-RPC interface for 'controlling' it. The server, right now, can start Telnet clients at the request of XML-RPC clients. This seems to work, however, I'd also like to be able to stop said Telnet clients, or see which ones are running, etc. I can't wrap my brain around how to accomplish those bits of functionality. I've included the .tac file below for reference.
<br><br>Thanks so much for your assistance!<br><br>-Don<br><br><br>#Try to build a Twisted Connector Service the twisted way<br>from twisted.application import internet, service<br>from twisted.web import resource, server, static, xmlrpc
<br>import re, time<br><br><br>from EventTypes import Event<br>connectiontypes = {'TelnetConnection':'A telnet client connection.', 'FileTailConnection':'Facilitates tailing a log file'}<br><br><br>class Connection:<br> def __init__(self,name):
<br> # Constructor<br> self.name=name<br> #print "Connection name: %s" % <a href="http://self.name">self.name</a><br> self.status=False<br><br> def setStatus (self,status):<br> self.status=status
<br><br>####################<br># TCP Client Stuff<br>####################<br>from twisted.internet.protocol import Protocol, ReconnectingClientFactory<br>from twisted.conch.telnet import Telnet<br><br><br>##########################
<br># Telnet Clients<br>##########################<br>class TelnetClient(Telnet):<br> "TelnetClient class." <br> def connectionMade(self):<br> #print "connection made to %s" % <a href="http://self.factory.name">
self.factory.name</a><br> #if the cmd variable was set in the calling factory, lets iterate over it.<br> if self.factory.cmd:<br> for cmd in self.factory.cmd:<br> if "sleep" in cmd:<br> #print
re.compile('(?P<sleeptime>(\d+))').search(cmd).group('sleeptime')<br> sleeptime = int(re.compile('(?P<sleeptime>(\d+))').search(cmd).group('sleeptime'))<br> time.sleep(sleeptime)<br>
else:<br> self.write(cmd)<br> <br><br> def write(self, data):<br> #print data<br> Telnet._write(self, data+"\r\n")<br><br><br> def dataReceived(self, data):<br> print "received:", data
<br> #always send data to the eventQueue<br> self.factory.eventQueue.insert(Event(data, <a href="http://self.factory.name">self.factory.name</a>))<br> #check if we should send any commands based on data received<br>
if self.factory.expect:<br> #check if any of the expect dictionary keys are in data<br> for key in self.factory.expect.keys(): <br> if key in data:<br> for cmd in self.factory.expect
[key]:<br> if "sleep" in cmd:<br> sleeptime = int(re.compile('(?P<sleeptime>(\d+))').search(cmd).group('sleeptime'))<br> time.sleep(sleeptime)<br>
else:<br> self.write(cmd)<br> <br><br><br>class TelnetConnection(Connection, ReconnectingClientFactory):<br> "Telnets to host:port and executes cmd. cmd"<br>
protocol = TelnetClient<br> def __init__(self, name, eventQueue, cmd=None, expect=None): <br> Connection.__init__(self, name)<br> self.cmd = cmd<br> self.expect = expect<br> self.eventQueue = eventQueue
<br> <br><br> <br><br> def clientConnectionFailed(self, connector, reason):<br> print 'connection failed:', reason.getErrorMessage()<br> <br><br> def clientConnectionLost(self, connector, reason):
<br> print 'connection lost:', reason.getErrorMessage()<br><br><br>class ManagerService(service.Service):<br> def __init__(self):<br> pass<br><br> # some XML-RPC function calls for twisted server<br> def stop(self):
<br> return 'Shutdown initiated'<br><br> def getConnectionTypes(self):<br> """Return connection types"""<br> return connectiontypes<br><br> def xmlrpc_getActiveConnections(self):
<br> """Return all active connections"""<br><br> def getConnectionStatus(self, connection):<br> return "connection status"<br><br><br> def getResource(self):<br>
"""Return sum of arguments."""<br> r = resource.Resource()<br> x = xmlrpc.XMLRPC()<br> x.xmlrpc_stop = self.stop<br> x.xmlrpc_getConnectionStatus = self.getConnectionStatus
<br> x.xmlrpc_startTelnet = self.startTelnet<br> <br> r.putChild('RPC2', x)<br> return r<br><br> def startTelnet(self, host, port, name, queue, command, script):<br> from twisted.internet import reactor
<br> from EventQueue import Queue<br> #Create the Event Queue object<br> self.queue = queue<br><br> self.queue = Queue()<br><br> reactor.connectTCP(host, int(port), TelnetConnection(name,self.queue
,command,script))<br> return "%s started" % name<br><br><br>#create an application instance<br>application = service.Application('connectorservice', uid=1, gid=1)<br>m = ManagerService()<br>serviceCollection =
service.IServiceCollection(application)<br>internet.TCPServer(7090, server.Site(m.getResource())<br> ).setServiceParent(serviceCollection)<br>