implement connecting and handshaking
This commit is contained in:
parent
bee1d8e227
commit
11eac2f1a5
|
@ -4,3 +4,4 @@ lib/*
|
||||||
include/*
|
include/*
|
||||||
local/*
|
local/*
|
||||||
src/*
|
src/*
|
||||||
|
build/*
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Copyright 2013 Rackspace, Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from twisted.internet import reactor
|
||||||
|
from twisted.python import log
|
||||||
|
|
||||||
|
from teeth_agent.agent import TeethAgent
|
||||||
|
|
||||||
|
log.startLogging(sys.stdout)
|
||||||
|
agent = TeethAgent()
|
||||||
|
agent.start('localhost', 8081)
|
||||||
|
reactor.run()
|
4
setup.py
4
setup.py
|
@ -18,8 +18,10 @@ limitations under the License.
|
||||||
|
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
from teeth_agent import agent
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='teeth-agent',
|
name='teeth-agent',
|
||||||
version='0.1-dev',
|
version=agent.AGENT_VERSION,
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
"""
|
||||||
|
Copyright 2013 Rackspace, Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
import pkg_resources
|
||||||
|
import simplejson as json
|
||||||
|
from twisted.internet.protocol import ReconnectingClientFactory
|
||||||
|
from twisted.internet import reactor
|
||||||
|
from twisted.python import log
|
||||||
|
|
||||||
|
from teeth_agent.protocol import TeethAgentProtocol
|
||||||
|
|
||||||
|
AGENT_VERSION = '0.1-dev'
|
||||||
|
|
||||||
|
|
||||||
|
class AgentClientHandler(TeethAgentProtocol):
|
||||||
|
def __init__(self):
|
||||||
|
TeethAgentProtocol.__init__(self, json.JSONEncoder())
|
||||||
|
|
||||||
|
def connectionMade(self):
|
||||||
|
def _response(result):
|
||||||
|
log.msg('Handshake successful', connection_id=result['id'])
|
||||||
|
|
||||||
|
self.send_command('handshake', 'a:b:c:d', AGENT_VERSION).addCallback(_response)
|
||||||
|
|
||||||
|
|
||||||
|
class AgentClientFactory(ReconnectingClientFactory):
|
||||||
|
protocol = AgentClientHandler
|
||||||
|
initialDelay = 1.0
|
||||||
|
maxDelay = 120
|
||||||
|
|
||||||
|
|
||||||
|
def buildProtocol(self, addr):
|
||||||
|
self.resetDelay()
|
||||||
|
return self.protocol()
|
||||||
|
|
||||||
|
def clientConnectionFailed(self, connector, reason):
|
||||||
|
log.err('Failed to connect, re-trying', delay=self.delay)
|
||||||
|
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
|
||||||
|
|
||||||
|
def clientConnectionLost(self, connector, reason):
|
||||||
|
log.err('Lost connection, re-connecting', delay=self.delay)
|
||||||
|
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
||||||
|
|
||||||
|
|
||||||
|
class TeethAgent(object):
|
||||||
|
client_factory = AgentClientFactory()
|
||||||
|
|
||||||
|
def start(self, host, port):
|
||||||
|
reactor.connectTCP(host, port, self.client_factory)
|
|
@ -42,16 +42,16 @@ class TeethAgentProtocol(LineReceiver):
|
||||||
elif 'result' in message:
|
elif 'result' in message:
|
||||||
self.handle_response(message)
|
self.handle_response(message)
|
||||||
|
|
||||||
def send_command(self, command):
|
def send_command(self, method, *args, **kwargs):
|
||||||
message_id = str(uuid.uuid4())
|
message_id = str(uuid.uuid4())
|
||||||
d = defer.Deferred()
|
d = defer.Deferred()
|
||||||
self.pending_command_deferreds[message_id] = d
|
self.pending_command_deferreds[message_id] = d
|
||||||
self.sendLine(self.encoder.encode({
|
self.sendLine(self.encoder.encode({
|
||||||
'id': message_id,
|
'id': message_id,
|
||||||
'version': DEFAULT_PROTOCOL_VERSION,
|
'version': DEFAULT_PROTOCOL_VERSION,
|
||||||
'method': command['method'],
|
'method': method,
|
||||||
'args': command.get('args', []),
|
'args': args,
|
||||||
'kwargs': command.get('kwargs', {}),
|
'kwargs': kwargs,
|
||||||
}))
|
}))
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue