mirror of
https://github.com/Oibaf66/frodo-wii.git
synced 2024-11-22 11:29:24 +01:00
Some non-tested data registry implementation for the network broker (not yet
active)
This commit is contained in:
parent
1e50aa3c28
commit
9affe4ffab
@ -5,6 +5,8 @@ import SocketServer, random
|
||||
# Local imports
|
||||
import stats, image
|
||||
|
||||
DATA_KEY_RANGE = 1000
|
||||
|
||||
FRODO_NETWORK_PROTOCOL_VERSION = 4
|
||||
FRODO_NETWORK_MAGIC = 0x1976
|
||||
|
||||
@ -15,6 +17,7 @@ SELECT_PEER = 93 # The client selects who to connect to
|
||||
DISCONNECT = 96 # Disconnect from a peer
|
||||
PING = 95 # Are you alive?
|
||||
ACK = 94 # Yep
|
||||
REGISTER_DATA = 90
|
||||
STOP = 55 # No more packets
|
||||
|
||||
pkt_type_to_str = {
|
||||
@ -128,7 +131,7 @@ class ConnectToBrokerPacket(Packet):
|
||||
self.name = ""
|
||||
self.server_id = 0
|
||||
self.avatar = 0
|
||||
self.screenshot = ""
|
||||
self.screenshot_key = -1
|
||||
|
||||
def demarshal_from_data(self, data):
|
||||
Packet.demarshal_from_data(self, data)
|
||||
@ -143,8 +146,7 @@ class ConnectToBrokerPacket(Packet):
|
||||
|
||||
if self.version >= 4:
|
||||
self.avatar = struct.unpack(">L", data[88:92])[0]
|
||||
self.screenshot = struct.unpack(">%ds" % (image.SCREENSHOT_SIZE),
|
||||
data[92:92 + image.SCREENSHOT_SIZE])[0]
|
||||
self.screenshot_key = struct.unpack(">L", data[92:96])[0]
|
||||
|
||||
def get_key(self):
|
||||
return self.key
|
||||
@ -152,8 +154,8 @@ class ConnectToBrokerPacket(Packet):
|
||||
def get_avatar(self):
|
||||
return self.avatar
|
||||
|
||||
def get_screenshot(self):
|
||||
return self.screenshot
|
||||
def get_screenshot_key(self):
|
||||
return self.screenshot_key
|
||||
|
||||
def get_name(self):
|
||||
return self.name
|
||||
@ -175,9 +177,9 @@ class ListPeersPacket(Packet):
|
||||
self.n_peers = self.n_peers + 1
|
||||
self.size = self.size + 80
|
||||
|
||||
# Add avatar and screenshot size
|
||||
# Add avatar and screenshot key size
|
||||
if self.version >= 4:
|
||||
self.size = self.size + image.SCREENSHOT_SIZE + 4
|
||||
self.size = self.size + 8
|
||||
|
||||
def marshal(self):
|
||||
out = struct.pack(">L16sHxx", self.n_peers, "", 0)
|
||||
@ -192,8 +194,7 @@ class ListPeersPacket(Packet):
|
||||
peer.is_master, name,
|
||||
0, peer.id, self.version)
|
||||
if self.version >= 4:
|
||||
out = out + struct.pack(">L%ds" % (image.SCREENSHOT_SIZE),
|
||||
peer.avatar, peer.screenshot)
|
||||
out = out + struct.pack(">LL", peer.avatar, peer.screenshot_key)
|
||||
|
||||
return Packet.marshal(self) + out
|
||||
|
||||
@ -229,7 +230,8 @@ class Peer:
|
||||
self.id = id
|
||||
|
||||
self.avatar = 0
|
||||
self.screenshot = ""
|
||||
self.screenshot_key = -1
|
||||
self.data_key = srv.get_data_key()
|
||||
|
||||
# Assume it's alive now
|
||||
self.last_ping = cur_time()
|
||||
@ -257,17 +259,9 @@ class Peer:
|
||||
return
|
||||
|
||||
self.avatar = pkt.get_avatar()
|
||||
self.screenshot = pkt.get_screenshot()
|
||||
self.screenshot_key = pkt.get_screenshot_key() + self.data_key
|
||||
|
||||
self.srv.log_connection(self.name, self.country)
|
||||
try:
|
||||
# Save as one of the last few screenshots
|
||||
which = random.randrange(0,12)
|
||||
img = image.image_from_data(self.screenshot)
|
||||
if img.interestingness > 0.4 or random.randrange(0, 10) < 2:
|
||||
img.save("%s%d.png" % (self.srv.image_dir, which))
|
||||
except Exception, e:
|
||||
log_info("Could not convert image data" + str(e))
|
||||
|
||||
# Send list of peers if this is not a master
|
||||
lp = ListPeersPacket(FRODO_NETWORK_PROTOCOL_VERSION)
|
||||
@ -280,6 +274,20 @@ class Peer:
|
||||
self.addr[0], self.addr[1]) )
|
||||
self.send_packet(lp.marshal())
|
||||
|
||||
if pkt.type == REGISTER_DATA:
|
||||
# Save screenshot (maybe) if this is the screenshot key
|
||||
if pkt.get_key() == self.screenshot_key - self.data_key:
|
||||
print "NYI!"
|
||||
return
|
||||
try:
|
||||
which = random.randrange(0,12)
|
||||
img = image.image_from_data(self.screenshot)
|
||||
if img.interestingness > 0.4 or random.randrange(0, 10) < 2:
|
||||
img.save("%s%d.png" % (self.srv.image_dir, which))
|
||||
except Exception, e:
|
||||
log_info("Could not convert image data" + str(e))
|
||||
|
||||
|
||||
if pkt.type == SELECT_PEER:
|
||||
peer = self.srv.get_peer_by_id( pkt.get_id() )
|
||||
|
||||
@ -375,6 +383,8 @@ class Broker(SocketServer.UDPServer):
|
||||
self.stat_data = stat_data
|
||||
self.image_dir = image_dir
|
||||
|
||||
self.data_key = 0
|
||||
|
||||
stats.load(self.stat_data)
|
||||
try:
|
||||
stats.generate_html(self.stat_html)
|
||||
@ -382,6 +392,12 @@ class Broker(SocketServer.UDPServer):
|
||||
# Don't care if it fails
|
||||
pass
|
||||
|
||||
def get_data_key(self):
|
||||
out = self.data_key
|
||||
self.data_key = (self.data_key + DATA_KEY_RANGE) & 0xffffffff
|
||||
|
||||
return out
|
||||
|
||||
def log_connection(self, who, country):
|
||||
stats.add_connection(who, country)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user