""" usage: client.py """ import sys import cjson from twisted.internet.protocol import ClientFactory from twisted.internet import reactor from twisted.protocols.basic import NetstringReceiver from pprint import PrettyPrinter from string import split pprinter = PrettyPrinter().pprint def makeAction( action, value): msg = { "method":"action", "action":action, "value":value } return msg class PokerClientProtocol(NetstringReceiver): def dealWithData(self,data): print "---------------" pprinter(data) if data["stat"] == "fail": print "error?" return if data["you"]["required_action"]: print "---------------" print "We need some input. Possible actions:" p_acts = data["you"]["possible_actions"] vals = [None,None] while not vals[0] in p_acts: pprinter(p_acts) str = raw_input('Action? format: OR ,:\n') vals = split(str,",") vals.append(None) str = cjson.encode(makeAction(vals[0], vals[1])) print "Sending %s" % str self.sendString( str ) def __init__(self,table_name, user_id, client_id ): self.table_name = table_name self.user_id = user_id self.client_id = client_id def connectionMade(self): login = {"method":"connect", "user_id":self.user_id, "client_id":self.client_id, "table_name":self.table_name} self.sendString(cjson.encode(login)) def stringReceived(self, line): decoded = cjson.decode(line) self.dealWithData(decoded) def connectionLost(self, reason): pass class PokerClientFactory(ClientFactory): protocol = PokerClientProtocol def buildProtocol( self, addr ): p = self.protocol(self.table_name, self.user_id, self.client_id) p.factory = self return p def __init__( self, table_name, user_id, client_id ): self.table_name = table_name self.user_id = user_id self.client_id = client_id def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage() reactor.stop() def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage() reactor.stop() def main(): if len(sys.argv) != 6: print __doc__ sys.exit(0) hostname = sys.argv[1] port = sys.argv[2] table_name = sys.argv[3] user_id = sys.argv[4] client_id = sys.argv[5] factory = PokerClientFactory(table_name, user_id, client_id ) # 8007 is the port you want to run under. Choose something >1024 reactor.connectTCP(hostname,int(port),factory) reactor.run() if __name__ == "__main__": main()