Remove, unused
This commit is contained in:
parent
280be4fd1f
commit
1f057e0d0b
116
chat/model.py
116
chat/model.py
@ -1,116 +0,0 @@
|
||||
import p2p
|
||||
|
||||
class ModelListener:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def model_changed(self, key, value):
|
||||
pass
|
||||
|
||||
class Model:
|
||||
def __init__(self):
|
||||
creator = Owner.getInstance()
|
||||
self._uri = "urn:model:" + creator.get_id() + ":" + random()
|
||||
self._values = {}
|
||||
self.__listeners = []
|
||||
|
||||
def get_value(self, key):
|
||||
return self._values[key]
|
||||
|
||||
def set_value(self, key, value):
|
||||
self._values[key] = value
|
||||
notify_changed(key, value)
|
||||
|
||||
def add_listener(self, listener):
|
||||
self.__listeners.add(listener)
|
||||
|
||||
def notify_changed(self, key, value):
|
||||
for listener in self.__listeners:
|
||||
listener.model_changed(key, value)
|
||||
|
||||
def serialize(model):
|
||||
return [ self._uri, self._values ]
|
||||
|
||||
serialize = staticmethod(serialize)
|
||||
|
||||
def deserialize(data):
|
||||
[uri, values] = data
|
||||
|
||||
# FIXME How damn I do this right in python?
|
||||
model = Model()
|
||||
model._uri = uri
|
||||
model._values = values
|
||||
|
||||
return model
|
||||
|
||||
deserialize = staticmethod(deserialize)
|
||||
|
||||
class ModelChangeSet:
|
||||
def __init__(self):
|
||||
self._values = {}
|
||||
|
||||
def add(self, key, value):
|
||||
self._values[key] = value
|
||||
|
||||
class ClientModelListener(ModelListener):
|
||||
def __init__(self, client, model):
|
||||
self._model = model
|
||||
|
||||
def model_changed(self, key, value):
|
||||
client.add_change(self._model, key, value)
|
||||
|
||||
class ModelStore:
|
||||
instance = None
|
||||
|
||||
def __init__(self):
|
||||
self._models = {}
|
||||
self._group = p2p.Group.get_instance()
|
||||
|
||||
input_pipe = p2p.InputPipe(self._group, "model-store")
|
||||
input_pipe.listen(self._model_request)
|
||||
|
||||
def get_instance():
|
||||
if not ModelStore.instance:
|
||||
ModelStore.instance = ModelStore()
|
||||
return ModelStore.instance
|
||||
|
||||
get_instance = staticmethod(get_instance)
|
||||
|
||||
def add_model(self, model):
|
||||
self._models[model.get_id(), model]
|
||||
|
||||
def _model_request(self, buddy, msg):
|
||||
parts = msg.split(':')
|
||||
model_id = self._group.get_buddy(parts[3])
|
||||
model = self._models[model_id]
|
||||
return model.serialize()
|
||||
|
||||
class ModelContext:
|
||||
def __init__(self):
|
||||
self._store = ModelStore.get_instance()
|
||||
self._changes = {}
|
||||
|
||||
def _add_model(self, model):
|
||||
self._store.add_model(model)
|
||||
|
||||
change_set = ModelChangeSet()
|
||||
self.__changes[model_id, change_set]
|
||||
|
||||
listener = ClientModelListener(self)
|
||||
model.add_listener(listener)
|
||||
|
||||
def fetch(self, model_uri):
|
||||
parts = model_uri.split(':')
|
||||
buddy = Group.get_instance().get_buddy(parts[2])
|
||||
|
||||
output_pipe = p2p.OutputPipe(group, buddy, "model-store")
|
||||
model = output_pipe.send(model_uri)
|
||||
|
||||
self._add_model(model)
|
||||
|
||||
def commit(self):
|
||||
pass
|
||||
|
||||
def add_change(self, model, key, value):
|
||||
change_set = self.__changes[model.get_id()]
|
||||
change_set.add(key, value)
|
163
chat/p2p.py
163
chat/p2p.py
@ -1,163 +0,0 @@
|
||||
import os
|
||||
import pwd
|
||||
import xmlrpclib
|
||||
import socket
|
||||
import traceback
|
||||
|
||||
import presence
|
||||
import BuddyList
|
||||
import network
|
||||
|
||||
class GroupRequestHandler(object):
|
||||
def __init__(self, group):
|
||||
self._group = group
|
||||
|
||||
def message(self, message):
|
||||
address = network.get_authinfo()
|
||||
self._group.recv(address[0], message)
|
||||
return True
|
||||
|
||||
class Owner:
|
||||
instance = None
|
||||
|
||||
def __init__(self):
|
||||
ent = pwd.getpwuid(os.getuid())
|
||||
self._nick = ent[0]
|
||||
if not self._nick or not len(self._nick):
|
||||
self._nick = "n00b"
|
||||
self._realname = ent[4]
|
||||
if not self._realname or not len(self._realname):
|
||||
self._realname = "Some Clueless User"
|
||||
|
||||
def get_realname(self):
|
||||
return self._realname
|
||||
|
||||
def get_nick(self):
|
||||
return self._nick
|
||||
|
||||
def get_instance():
|
||||
if not Owner.instance:
|
||||
Owner.instance = Owner()
|
||||
return Owner.instance
|
||||
|
||||
get_instance = staticmethod(get_instance)
|
||||
|
||||
class Group:
|
||||
instance = None
|
||||
|
||||
_SERVER_PORT = 6666
|
||||
|
||||
def __init__(self):
|
||||
self._pipes = {}
|
||||
|
||||
def get_instance():
|
||||
if not Group.instance:
|
||||
Group.instance = Group()
|
||||
Group.instance.join()
|
||||
return Group.instance
|
||||
|
||||
get_instance = staticmethod(get_instance)
|
||||
|
||||
def join(self):
|
||||
self._pannounce = presence.PresenceAnnounce()
|
||||
|
||||
rname = Owner.get_instance().get_realname()
|
||||
nick = Owner.get_instance().get_nick()
|
||||
|
||||
self._buddy_list = BuddyList.BuddyList(rname)
|
||||
self._buddy_list.start()
|
||||
|
||||
self._pannounce.register_service(rname, self._SERVER_PORT, presence.OLPC_CHAT_SERVICE,
|
||||
name = nick, realname = rname)
|
||||
|
||||
self._p2p_req_handler = GroupRequestHandler(self)
|
||||
self._p2p_server = network.GlibXMLRPCServer(("", self._SERVER_PORT))
|
||||
self._p2p_server.register_instance(self._p2p_req_handler)
|
||||
|
||||
self._gc_controller = network.GroupChatController('224.0.0.221', 6666, self._recv_group_message)
|
||||
self._gc_controller.start()
|
||||
|
||||
def get_buddy_list(self):
|
||||
return self._buddy_list
|
||||
|
||||
def _serialize_msg(self, pipe_id, msg):
|
||||
return pipe_id + "|" + msg
|
||||
|
||||
def _deserialize_msg(self, msg):
|
||||
sep_index = msg.find("|")
|
||||
pipe_id = msg[0 : sep_index]
|
||||
message = msg[sep_index + 1 :]
|
||||
return [pipe_id, message]
|
||||
|
||||
def send(self, buddy, pipe_id, msg):
|
||||
addr = "http://%s:%d" % (buddy.address(), buddy.port())
|
||||
peer = xmlrpclib.ServerProxy(addr)
|
||||
success = True
|
||||
try:
|
||||
print self._serialize_msg(pipe_id, msg)
|
||||
peer.message(self._serialize_msg(pipe_id, msg))
|
||||
except (socket.error, xmlrpclib.Fault, xmlrpclib.ProtocolError), e:
|
||||
print "Message Send Error:"
|
||||
traceback.print_exc()
|
||||
success = False
|
||||
return success
|
||||
|
||||
def broadcast(self, pipe_id, msg):
|
||||
self._gc_controller.send_msg(self._serialize_msg(pipe_id, msg))
|
||||
|
||||
def register_pipe(self, input_pipe):
|
||||
self._pipes[input_pipe.get_id()] = input_pipe
|
||||
|
||||
def _recv_group_message(self, msg):
|
||||
self.recv(msg['addr'], msg['data'])
|
||||
|
||||
def recv(self, address, message):
|
||||
sender = self._buddy_list.find_buddy_by_address(address)
|
||||
[pipe_id, msg] = self._deserialize_msg(message)
|
||||
pipe = self._pipes[pipe_id]
|
||||
if pipe:
|
||||
pipe.recv(sender, msg)
|
||||
|
||||
class AbstractPipe:
|
||||
def __init__(self, group, pipe_id=None):
|
||||
self._group = group
|
||||
self._pipe_id = pipe_id
|
||||
|
||||
def get_id(self):
|
||||
return self._pipe_id
|
||||
|
||||
def send(self, msg):
|
||||
pass
|
||||
|
||||
class AbstractOutputPipe(AbstractPipe):
|
||||
def __init__(self, group, pipe_id=None):
|
||||
AbstractPipe.__init__(self, group, pipe_id)
|
||||
|
||||
def send(self, msg):
|
||||
pass
|
||||
|
||||
class OutputPipe(AbstractOutputPipe):
|
||||
def __init__(self, group, buddy, pipe_id=None):
|
||||
AbstractOutputPipe.__init__(self, group, pipe_id)
|
||||
self._buddy = buddy
|
||||
|
||||
def send(self, msg):
|
||||
return self._group.send(self._buddy, self._pipe_id, msg)
|
||||
|
||||
class BroadcastOutputPipe(AbstractOutputPipe):
|
||||
def __init__(self, group, pipe_id=None):
|
||||
AbstractOutputPipe.__init__(self, group, pipe_id)
|
||||
|
||||
def send(self, msg):
|
||||
return self._group.broadcast(self._pipe_id, msg)
|
||||
|
||||
class InputPipe(AbstractPipe):
|
||||
def __init__(self, group, pipe_id=None):
|
||||
AbstractPipe.__init__(self, group, pipe_id)
|
||||
group.register_pipe(self)
|
||||
|
||||
def listen(self, callback):
|
||||
self.__callback = callback
|
||||
|
||||
def recv(self, sender, msg):
|
||||
self.__callback(sender, msg)
|
Loading…
Reference in New Issue
Block a user