-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPypadClient.py
295 lines (234 loc) · 9.97 KB
/
PypadClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""
PypadClient.py
INTRODUCTION
Contains the PypadClient class.
The PypadClient class is the controller in the model view controller design
pattern of Pypad. It interfaces between the server and gui objects
AUTHORS
Steven Zhang, Jason Poon, Reyner Crosby
CREDITS
Pyro author and package: http://pyro.sourceforge.net
Remoteobjects Library: Allen Downey
Subject-Observer-Modifier template: Mark Sheldon
CHANGELOG
11/26/2010
Cleaned up code and comments.
Made the drawing canvas work a little more smoothly.
Made all loops into PypadClient class methods
-Steven
4/29/10
Cleaned up code and added comments
-Steven
4/28/10
Added collaboration abilities for the drawing canvas
- Steven
4/27/10
Added improved version of revision history.
- Jason
4/23/10
Switched to the Remote Object Library
Encapsulated methods by adding a client class for listening to server
Use server side pushing (notified as in obeserver-subject-modifer) for changes:
all changes now instantaneous
-Steven
4/23/10
Added revision history capabilities
-Jason
4/9/10
Created initial frame work
-Jason, Steven, Reyner
"""
from RemoteObject import *
from threading import Thread
from PypadGui import *
from time import sleep
from RemoteObject import *
import sys
DEBUG = False
class PypadClient(RemoteObject):
"""
A PypadClient (herein called client)
is an object that watches its PypadGui and the PypadServer.
Whenever the gui changes, the client sends that update to the server.
The server immediately responds by notifying all the other clients currently
running to update their guis.
That way, all the changes are (almost) instantaneous
Some code is modified from:
http://ece.olin.edu/sd/current/web/notes/25_subject_observer/subject_observer.html
"""
def __init__(self, serverName):
"""
Constructor for PypadClient object
Args:
serverName: string; the name of the PypadServer object to connect to
this name must match the defined in the instantiation of said
object
"""
# setting these flags to be true allows the initial gui to
# display the current data
self.textNeedsUpdating = True
self.drawingNeedsUpdating = True
ns = NameServer()
# register with the server
self.serverName = serverName
self.server = ns.get_proxy(self.serverName)
self.clientName, self.id = self.server.register()
print "I just registered with server."
# connect to the name server
RemoteObject.__init__(self, self.clientName)
print "I just registered with Name Server."
# modifying attributes
# Getters
def getClientName(self):
"""Getter for client name. added 4/24 Steven"""
return self.clientName
def getId(self):
"""Getter for id. added 11/26 Steven"""
return self.id
# the following methods are added by Steven
# following modification from Mark Sheldon's Subject, Modifier, Observer
# examples in the link above
def notify(self, type):
"""
This method is invoked remotely
When the PypadServer is modified, it invokes notify;
then the client sets off flags (corresponding to whether text or drawing
has changed), which allows the client to be updated in the loops below
Args:
type: 'text' or 'drawing'
"""
if(DEBUG): print 'Notified', type
if type == 'text':
if(DEBUG): print 'self.textNeedsUpdating = True'
self.textNeedsUpdating = True
elif type == 'drawing':
self.drawingNeedsUpdating = True
def modify(self, text=[], drawing=[], type = 'text'):
"""
Changed the state of the server data.
Args:
type: 'text' or 'drawing'.
Specifies whether text or drawing should be updated
"""
self.server.setState(self.name, newText = text, newDrawing = drawing, type = type)
if DEBUG: print 'Setting state to ' + str(self.server.getState())
def cleanup(self):
"""
Inherited RemoteObject method to stop itself
Unregisters self from PypadServer
Added by Steven Zhang 4/24
"""
print 'Disconnecting from server'
self.server.remove(self.clientName)
RemoteObject.cleanup(self)
def updateTextLoop(self, gui):
"""
This loop is the event loop that handles requests between client and gui
text.
The client checks to see if gui has changed. If so, it notifies the server
It also checks to see if server has flagged the client to update the gui.
If so, it updates the gui.
Args:
gui: the corresponding PypadGui object
Steven wrote most of this loop.
"""
if(DEBUG): print "in updateTextLoop"
while True:
# Check to see if gui has changed due to user input
if gui.t.hasTextChanged() == True:
if(DEBUG): print "Gui just changed"
gui.t.setTextAsUpdated()
self.modify(text = gui.t.getText(), type = 'text')
if(DEBUG): print "state=" + str(gui.t.getText())
# Checks to see client's textNeedsUpdating flag has been raised by
# server
if self.textNeedsUpdating == True:
if(DEBUG): print "client.textNeedsUpdating == True"
if(DEBUG): print self.server.getState('text')
self.textNeedsUpdating = False
gui.t.setText(self.server.getState('text'))
# This part which checks to see if the a person has inputed
# a request for new revision. Since we want to check this as much as
# possible, this request is contained in this loop, which does not pause
# like the updateRevLoop does.
# This is the only time gui's attributes are accessed directly
# We can easily implement getters and setters with more time
if gui.t.getRevUpdateFlag() == True:
newRev = int(gui.t.getRevNumReq())
#update to requested rev, but only if requested rev < current rev
if newRev < self.server.getRevNum():
self.modify(text = self.server.getHistory(newRev), type = 'text')
gui.t.setText(self.server.getState('text'))
gui.t.setRevUpdateFlag(False)
def updateRevLoop(self, gui):
"""
This loop is the event loop that handles requests between client and
the gui's revision box.
Jason wrote this loop.
"""
if(DEBUG): print "in updateRevLoop"
while True:
if(DEBUG): print gui.t.revInput
sleep(5)
# we pause so that the user can input something in the rev box before
# it gets overwritten by the automated revision updater
if gui.t.revInput == False:
# Continually update the revision number on each GUI.
gui.t.nameTextCtrl.SetValue(str(self.server.getRevNum()))
def updateDrawingLoop(self, gui):
"""
This loop is the event loop that handles requests between client and gui
drawing. Works in principle just like the updateTextLoop.
Jason wrote the initial loop
Steven refactored it to make methods similar named to the updateTextLoop
"""
if(DEBUG): print "in update Drawing Loop"
while(True):
sleep(1)
# drawing objects are full of data, so we don't want to update too often
# due to latency
if gui.d.hasDrawingChanged() == True:
print "Drawing just changed"
gui.d.setDrawingAsUpdated()
self.modify(drawing = gui.d.getDrawing(),type = 'drawing')
if self.drawingNeedsUpdating == True:
print "self.drawingNeedsUpdating == True"
self.drawingNeedsUpdating = False
gui.d.setDrawing(self.server.getState('drawing'))
def clientLoops(self, gui):
"""
Initializes the loops for text, rev, request, and drawing update loops
Added 11/16/10 by Steven
"""
t1 = Thread(target = self.updateTextLoop, args =[gui])
t1.start()
# in essence, the client request loop handles interfacing between the server
# and client. The other update loops handle interfacing between client and
# gui
t2 = Thread(target = self.requestLoop)
t2.start()
t3 = Thread(target = self.updateRevLoop, args =[gui])
t3.start()
t4 = Thread(target = self.updateDrawingLoop, args =[gui])
t4.start()
def main(script, *args):
"""
Starts the gui, client object and various loops
The client loop and the three loops for updating drawing, text, revisions
are in separate threads.
The gui loop runs on the initial/base thread
Written mostly by Steven
"""
serverName = 'Pypad_dot_com'
ns = NameServer()
serverData = ns.get_proxy(serverName)
client = PypadClient(serverName)
app = wx.App(False)
gui = PypadGui()
gui.t.SetTitle("Pypad client, connected to " + client.serverName
+ ':' + str(client.getId()) + '.')
client.clientLoops(gui)
app.MainLoop() #gui loop
print "in GUI Loop"
if __name__ == '__main__':
main(*sys.argv)