[Twisted-Python] flushing data to clients after a global variable is updated in server
Jessica Tsui
jesadjust at gmail.com
Wed Apr 29 01:33:57 MDT 2015
Hi, it's me again. I am still working on the kivy app, and now I am trying
to add a function to the app - the server side has two seconds to decide if
he wants to alter the original data sent from one client to other clients.
If he would like to do so, he has to press a button. If he does not press
the button in 2 seconds, the original data will be sent to other clients
automatically.
Right now I am trying to achieve that with the following code - by calling
the MultiClientEcho().dataReceived(msg, "censored") line in the function as
if MultiClientEcho received another data, however once i click the button,
the program crashed and said (MultiClientEcho().dataReceived(msg,
"censored")
TypeError: __init__() takes exactly 3 arguments (1 given))
I wonder how can I fix this and achieve the function I am aiming at?
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.context_instructions import Color
from kivy.graphics.instructions import Instruction
from kivy.base import runTouchApp
from kivy.lang import Builder
import socket
from kivy.core.window import Window
import pygame
import random
from kivy.support import install_twisted_reactor
install_twisted_reactor()
from twisted.internet import protocol, defer
from time import sleep
from twisted.internet import reactor, task
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Protocol, Factory
censored = 0
class MultiClientEcho(protocol.Protocol):
def __init__(self, factory, app):
self.factory = factory
self.app = app
def connectionMade(self):
self.factory.clients.append(self)
def dataReceived(self, data):
storedmessage = self.factory.app.handle_message(data)
global censored
def f(data):
for client in self.factory.clients:
client.transport.write(data)
print "this will run in 1 sec after it's
scheduled: %s" % data
if censored == 0 and storedmessage:
reactor.callLater(2, f, data)
# client.transport.write(data)
elif censored == 1:
reactor.callLater(0, f, 'censored')
censored == 0
def connectionLost(self, reason):
self.factory.clients.remove(self)
class MultiClientEchoFactory(protocol.Factory):
protocol = MultiClientEcho
def __init__(self, app):
self.clients = []
self.app = app
def buildProtocol(self, addr):
return MultiClientEcho(self, self.app)
class ServerApp(App):
def build(self):
self.label = Label(text="server started\n")
self.approve_btn = Button(text="approve")
# self.approve_btn.bind(on_release=self.send_message)
self.banned_btn = Button(text="banned")
self.banned_btn.bind(on_release=self.banned_message)
self.layout = BoxLayout(orientation='vertical', spacing=10)
reactor.listenTCP(8000, MultiClientEchoFactory(self))
self.layout.add_widget(self.label)
self.layout.add_widget(self.banned_btn)
return self.layout
def banned_message(self, msg):
global censored
censored = 1
self.label.text += "censored\n"
MultiClientEcho().dataReceived(msg, "censored")
print censored
return censored
def handle_message(self, msg):
self.label.text += "%s\n" % msg
return msg
if __name__ == '__main__':
ServerApp().run()
for i in range(0,1):
print 1-i
sleep(0.1)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20150429/f8f7b0e4/attachment-0001.html>
More information about the Twisted-Python
mailing list