Created
January 9, 2014 21:56
-
-
Save george-silva/8342803 to your computer and use it in GitHub Desktop.
Twisted huh?
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
# coding: utf-8 | |
from twisted.internet.protocol import Factory | |
from twisted.protocols.basic import LineReceiver | |
from twisted.internet import reactor | |
class Gateway(LineReceiver): | |
"""Classe para lidar com as conexões""" | |
def __init__(self, modulos_autorizados=[]): | |
self.modulos_autorizados = modulos_autorizados | |
self.modulos_conectados = [] | |
def connectionMade(self): | |
self.sendLine("Modulo conectado") | |
def connectionLost(self, reason): | |
pass | |
def lineReceived(self, line): | |
if line[:4] not in self.modulos_conectados: | |
self.modulos_conectados.append(line[:4]) | |
if line[:4] in self.modulos_autorizados: | |
self.sendLine("ack") | |
else: | |
self.sendLine("ref") | |
class GatewayFactory(Factory): | |
def __init__(self, modulos_autorizados=["ABCD", "EFGH"]): | |
self.modulos_autorizados = modulos_autorizados | |
def buildProtocol(self, addr): | |
return Gateway(self.modulos_autorizados) | |
if __name__ == "__main__": | |
reactor.listenTCP(8666, GatewayFactory()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment