Last active
April 10, 2016 12:35
-
-
Save abhigenie92/ada33befa8456f1ee7d16c711014e1dd to your computer and use it in GitHub Desktop.
abhigenie92
This file contains hidden or 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
Aim: associate each protocol with a username | |
Method right now: I make the client send it's username to server upon connecting in the connectionMade method. This is | |
thus, the first msg of the client. | |
The server differentiates this msg from client from rest by using flag unregistered in the class StrokeEcho, | |
Required approach: | |
Do this without the use of flag by appending something to the first msg the client sends while establishing | |
a connection with the server and then receive it the server end. I am assuming the client sends some data to | |
the server when I call connectTCP like it's IP-address etc. I want to modify that msg and receive at the | |
server end. | |
======== | |
# client code | |
from twisted.internet.protocol import ClientFactory | |
from twisted.protocols.basic import NetstringReceiver | |
import json | |
class StrokeClient(NetstringReceiver): | |
def connectionMade(self): | |
self.factory.canvas_obj.stroke_conn.protocol = self | |
self.sendString(json.dumps(self.factory.username)) | |
print "Connected to stroke remote server" | |
def stringReceived(self, data): | |
if data: | |
print data | |
self.factory.canvas_obj.draw(data) | |
class StrokeClientFactory(ClientFactory): | |
protocol = StrokeClient | |
def __init__(self, canvas_obj, username): | |
self.canvas_obj = canvas_obj | |
self.username = username | |
=========== | |
# server code | |
# -*- coding: utf-8 -*- | |
# -*- coding: utf-8 -*- | |
from twisted.internet.defer import Deferred | |
from twisted.internet.protocol import Factory | |
from twisted.protocols.basic import NetstringReceiver | |
import json | |
class StrokeEcho(NetstringReceiver): | |
username='' | |
def __init__(self, unregistered=True): | |
self.unregistered = unregistered | |
def connectionMade(self): | |
print "Connected client:",self | |
def stringReceived(self, string): | |
if self.unregistered: | |
username=json.loads(string) | |
self.unregistered=False | |
else: | |
for protocol in self.factory.protocols: | |
if protocol!=self: | |
protocol.sendString(string) | |
def connectionLost(self, reason): | |
print "Client disconnected:", self.transport.getPeer() | |
self.factory.disconnectOne(self) | |
class StrokeEchoFactory(Factory): | |
protocol = StrokeEcho | |
def __init__(self,server_username,factory_type,rooms): | |
self.protocols = set() | |
self.echoers = [] | |
self.server_username = server_username | |
self.closePort = Deferred() | |
self.rooms=rooms | |
def buildProtocol(self, addr): | |
protocol = Factory.buildProtocol(self, addr) | |
self.protocols.add(protocol) | |
return protocol | |
def disconnectOne(self, server_protocol): | |
self.protocols.remove(server_protocol) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment