/
Server - Copy.py
213 lines (159 loc) · 6.54 KB
/
Server - Copy.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
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
from direct.distributed.PyDatagram import PyDatagram
from direct.distributed.PyDatagramIterator import PyDatagramIterator
from time import time
import random
class Application(ShowBase):
def __init__(self):
ShowBase.__init__(self)
server = Server(Protocol(), 9999)
self.smiley = loader.loadModel("smiley")
self.smiley.setPythonTag("velocity", 0)
self.smiley.reparentTo(render)
self.smiley.setPos(0, 0, 30)
self.cam.setPos(0, -100, 10)
#client = Client(ClientProtocol(self.smiley))
#client.connect("localhost", 9999, 3000)
taskMgr.add(self.updateSmiley, "updateSmiley")
'''
print "Starting server"
ShowBase.__init__(self)
#server = Server(ServerProtocol(), 9999)
client = Client(ClientProtocol())
client.connect("128.113.232.77", 9999, 3000)
data = PyDatagram()
data.addUint8(0)
data.addString(str(time()))
client.send(data)
while True:
inputString = raw_input('\t:')
#print inputString
reply = PyDatagram()
reply.addUint8(0)
reply.addString(str(time()))
client.send(reply)
'''
def updateSmiley(self, task):
#print "Updating client smiley"
vel = self.smiley.getPythonTag("velocity")
z = self.smiley.getZ()
if z <= 0:
vel = random.uniform(0.1, 0.8)
self.smiley.setZ(z + vel)
self.smiley.setX(20)
vel -= 0.01
self.smiley.setPythonTag("velocity", vel)
return task.cont
class NetCommon:
def __init__(self, protocol):
self.manager = ConnectionManager()
self.reader = QueuedConnectionReader(self.manager, 0)
self.writer = ConnectionWriter(self.manager, 0)
self.protocol = protocol
taskMgr.add(self.updateReader, "updateReader")
def updateReader(self, task):
if self.reader.dataAvailable():
data = NetDatagram()
self.reader.getData(data)
reply = self.protocol.process(data)
if reply != None:
self.writer.send(reply, data.getConnection())
return task.cont
class Server(NetCommon):
def __init__(self, protocol, port):
NetCommon.__init__(self, protocol)
self.listener = QueuedConnectionListener(self.manager, 0)
socket = self.manager.openTCPServerRendezvous(port, 100)
self.listener.addConnection(socket)
self.connections = []
self.smiley = ServerSmiley()
self.frowney = loader.loadModel("frowney")
self.frowney.reparentTo(render)
taskMgr.add(self.updateListener, "updateListener")
taskMgr.add(self.updateSmiley, "updateSmiley")
taskMgr.doMethodLater(0.1, self.syncSmiley, "syncSmiley")
def updateListener(self, task):
if self.listener.newConnectionAvailable():
connection = PointerToConnection()
if self.listener.getNewConnection(connection):
connection = connection.p()
self.connections.append(connection)
self.reader.addConnection(connection)
print "Server: New connection established."
return task.cont
def updateSmiley(self, task):
self.smiley.update()
self.frowney.setPos(self.smiley.pos)
return task.cont
def syncSmiley(self, task):
print "SYNCING SMILEYS!"
sync = PyDatagram()
sync.addFloat32(self.smiley.vel)
sync.addFloat32(self.smiley.pos.getZ())
self.broadcast(sync)
return task.again
def broadcast(self, datagram):
for conn in self.connections:
self.writer.send(datagram, conn)
class Client(NetCommon):
def __init__(self, protocol):
NetCommon.__init__(self, protocol)
def connect(self, host, port, timeout):
self.connection = self.manager.openTCPClientConnection(host, port, timeout)
if self.connection:
self.reader.addConnection(self.connection)
print "Client: Connected to server."
def send(self, datagram):
if self.connection:
self.writer.send(datagram, self.connection)
class Protocol:
def process(self, data):
return None
def printMessage(self, title, msg):
print "%s %s" % (title, msg)
def buildReply(self, msgid, data):
reply = PyDatagram()
reply.addUint8(msgid)
reply.addString(data)
return reply
class ServerProtocol(Protocol):
def process(self, data):
it = PyDatagramIterator(data)
msgid = it.getUint8()
if msgid == 0:
return self.handleHello(it)
elif msgid == 1:
return self.handleQuestion(it)
elif msgid == 2:
return self.handleBye(it)
def handleHello(self, it):
self.printMessage("Server received:", it.getString())
return self.buildReply(0, "Hello, too!")
def handleQuestion(self, it):
self.printMessage("Server received:", it.getString())
return self.buildReply(1, "I'm fine. How are you?")
def handleBye(self, it):
self.printMessage("Server received:", it.getString())
return self.buildReply(2, "Bye!")
class ClientProtocol(Protocol):
def __init__(self, smiley):
self.smiley = smiley
def process(self, data):
it = PyDatagramIterator(data)
vel = it.getFloat32()
z = it.getFloat32()
diff = z - self.smiley.getZ()
self.smiley.setPythonTag("velocity", vel + diff * 0.03)
return None
class ServerSmiley:
def __init__(self):
self.pos = Vec3(0, 0, 30)
self.vel = 0
def update(self):
#print "Updating server smiley"
z = self.pos.getZ()
if z <= 0:
self.vel = random.uniform(0.1, 0.8)
self.pos.setZ(z + self.vel)
self.vel -= 0.01