Some non-tested data registry implementation for the network broker (not yet

active)
This commit is contained in:
simon.kagstrom 2010-02-06 16:56:11 +00:00
parent 1e50aa3c28
commit 9affe4ffab

View File

@ -5,6 +5,8 @@ import SocketServer, random
# Local imports # Local imports
import stats, image import stats, image
DATA_KEY_RANGE = 1000
FRODO_NETWORK_PROTOCOL_VERSION = 4 FRODO_NETWORK_PROTOCOL_VERSION = 4
FRODO_NETWORK_MAGIC = 0x1976 FRODO_NETWORK_MAGIC = 0x1976
@ -15,6 +17,7 @@ SELECT_PEER = 93 # The client selects who to connect to
DISCONNECT = 96 # Disconnect from a peer DISCONNECT = 96 # Disconnect from a peer
PING = 95 # Are you alive? PING = 95 # Are you alive?
ACK = 94 # Yep ACK = 94 # Yep
REGISTER_DATA = 90
STOP = 55 # No more packets STOP = 55 # No more packets
pkt_type_to_str = { pkt_type_to_str = {
@ -128,7 +131,7 @@ class ConnectToBrokerPacket(Packet):
self.name = "" self.name = ""
self.server_id = 0 self.server_id = 0
self.avatar = 0 self.avatar = 0
self.screenshot = "" self.screenshot_key = -1
def demarshal_from_data(self, data): def demarshal_from_data(self, data):
Packet.demarshal_from_data(self, data) Packet.demarshal_from_data(self, data)
@ -143,8 +146,7 @@ class ConnectToBrokerPacket(Packet):
if self.version >= 4: if self.version >= 4:
self.avatar = struct.unpack(">L", data[88:92])[0] self.avatar = struct.unpack(">L", data[88:92])[0]
self.screenshot = struct.unpack(">%ds" % (image.SCREENSHOT_SIZE), self.screenshot_key = struct.unpack(">L", data[92:96])[0]
data[92:92 + image.SCREENSHOT_SIZE])[0]
def get_key(self): def get_key(self):
return self.key return self.key
@ -152,8 +154,8 @@ class ConnectToBrokerPacket(Packet):
def get_avatar(self): def get_avatar(self):
return self.avatar return self.avatar
def get_screenshot(self): def get_screenshot_key(self):
return self.screenshot return self.screenshot_key
def get_name(self): def get_name(self):
return self.name return self.name
@ -175,9 +177,9 @@ class ListPeersPacket(Packet):
self.n_peers = self.n_peers + 1 self.n_peers = self.n_peers + 1
self.size = self.size + 80 self.size = self.size + 80
# Add avatar and screenshot size # Add avatar and screenshot key size
if self.version >= 4: if self.version >= 4:
self.size = self.size + image.SCREENSHOT_SIZE + 4 self.size = self.size + 8
def marshal(self): def marshal(self):
out = struct.pack(">L16sHxx", self.n_peers, "", 0) out = struct.pack(">L16sHxx", self.n_peers, "", 0)
@ -192,8 +194,7 @@ class ListPeersPacket(Packet):
peer.is_master, name, peer.is_master, name,
0, peer.id, self.version) 0, peer.id, self.version)
if self.version >= 4: if self.version >= 4:
out = out + struct.pack(">L%ds" % (image.SCREENSHOT_SIZE), out = out + struct.pack(">LL", peer.avatar, peer.screenshot_key)
peer.avatar, peer.screenshot)
return Packet.marshal(self) + out return Packet.marshal(self) + out
@ -229,7 +230,8 @@ class Peer:
self.id = id self.id = id
self.avatar = 0 self.avatar = 0
self.screenshot = "" self.screenshot_key = -1
self.data_key = srv.get_data_key()
# Assume it's alive now # Assume it's alive now
self.last_ping = cur_time() self.last_ping = cur_time()
@ -257,17 +259,9 @@ class Peer:
return return
self.avatar = pkt.get_avatar() 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) 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 # Send list of peers if this is not a master
lp = ListPeersPacket(FRODO_NETWORK_PROTOCOL_VERSION) lp = ListPeersPacket(FRODO_NETWORK_PROTOCOL_VERSION)
@ -280,6 +274,20 @@ class Peer:
self.addr[0], self.addr[1]) ) self.addr[0], self.addr[1]) )
self.send_packet(lp.marshal()) 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: if pkt.type == SELECT_PEER:
peer = self.srv.get_peer_by_id( pkt.get_id() ) peer = self.srv.get_peer_by_id( pkt.get_id() )
@ -375,6 +383,8 @@ class Broker(SocketServer.UDPServer):
self.stat_data = stat_data self.stat_data = stat_data
self.image_dir = image_dir self.image_dir = image_dir
self.data_key = 0
stats.load(self.stat_data) stats.load(self.stat_data)
try: try:
stats.generate_html(self.stat_html) stats.generate_html(self.stat_html)
@ -382,6 +392,12 @@ class Broker(SocketServer.UDPServer):
# Don't care if it fails # Don't care if it fails
pass 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): def log_connection(self, who, country):
stats.add_connection(who, country) stats.add_connection(who, country)