Created
March 16, 2016 19:20
-
-
Save timhughes/f85da0c2d88ecb4ab5e3 to your computer and use it in GitHub Desktop.
contrived example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClientProtocol(protocol.Protocol): | |
def dataTransmit(message): | |
self.transport.write(data) | |
def dataReceived(self, message): | |
self.factory.service.handle_message(message) | |
class MyClientFactory(protocol.ClientFactory): | |
protocol = MyClientProtocol | |
def __init__(self, service): | |
self.service = service | |
class MyService(service.Service): | |
def startService(self): | |
self.config = load_config(config_file) | |
def handleMessage(self, message): | |
# do something with data | |
decoded_msg = self.decodeMessage(message) | |
if decoded_msg['type'] == 'logon_response': | |
decoded_msg['result'] == 'good': | |
self.doSomthing() | |
def sendMessage(self, message) | |
############################### | |
# Not sure what to do here without coupling it to the protocol | |
############################## | |
def decodeMessage(self, data): | |
return json.loads(data) | |
def encodeMessage(self, data): | |
return json.dumps(data) | |
def logon(self): | |
logon_message_data = { | |
'user': self.config.user, | |
'pass': self.config.pass | |
} | |
msg = self.encodeMessage(logon_message_data) | |
self.sendMessage(msg) | |
def doSomthing(self) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment