2006-05-15 20:48:08 +02:00
|
|
|
import socket
|
2006-06-18 04:06:40 +02:00
|
|
|
import logging
|
2006-05-15 20:48:08 +02:00
|
|
|
|
2006-05-15 21:03:05 +02:00
|
|
|
from sugar.p2p.Notifier import Notifier
|
2006-05-15 20:48:08 +02:00
|
|
|
from sugar.p2p.model.AbstractModel import AbstractModel
|
|
|
|
from sugar.p2p import network
|
|
|
|
|
|
|
|
class ModelRequestHandler(object):
|
|
|
|
def __init__(self, model):
|
|
|
|
self._model = model
|
|
|
|
|
|
|
|
def get_value(self, key):
|
|
|
|
return self._model.get_value(key)
|
|
|
|
|
|
|
|
def set_value(self, key, value):
|
|
|
|
return self._model.set_value(key, value)
|
|
|
|
|
|
|
|
class LocalModel(AbstractModel):
|
|
|
|
SERVICE_TYPE = "_olpc_model._tcp"
|
|
|
|
|
2006-06-16 20:20:09 +02:00
|
|
|
def __init__(self, activity, pservice, service):
|
2006-05-15 20:48:08 +02:00
|
|
|
AbstractModel.__init__(self)
|
2006-06-16 19:06:11 +02:00
|
|
|
self._pservice = pservice
|
2006-06-16 20:20:09 +02:00
|
|
|
self._activity = activity
|
|
|
|
self._service = service
|
2006-05-15 20:48:08 +02:00
|
|
|
self._values = {}
|
|
|
|
|
|
|
|
self._setup_service()
|
2006-06-16 20:20:09 +02:00
|
|
|
self._notifier = Notifier(service)
|
2006-05-15 20:48:08 +02:00
|
|
|
|
|
|
|
def get_value(self, key):
|
|
|
|
return self._values[key]
|
|
|
|
|
|
|
|
def set_value(self, key, value):
|
|
|
|
self._values[key] = value
|
|
|
|
self._notify_model_change(key)
|
2006-05-15 21:03:05 +02:00
|
|
|
self._notifier.notify(key)
|
2006-05-15 20:48:08 +02:00
|
|
|
|
|
|
|
def _setup_service(self):
|
2006-06-16 20:20:09 +02:00
|
|
|
service = self._pservice.share_activity(self._activity,
|
2006-07-26 18:00:28 +02:00
|
|
|
stype = LocalModel.SERVICE_TYPE)
|
2006-05-15 20:48:08 +02:00
|
|
|
self._setup_server(service)
|
|
|
|
|
|
|
|
# FIXME this is duplicated with StreamReader
|
|
|
|
def _setup_server(self, service):
|
|
|
|
port = service.get_port()
|
2006-07-26 02:04:15 +02:00
|
|
|
logging.debug('Start model server on port %d' % (port))
|
|
|
|
p2p_server = network.GlibXMLRPCServer(("", port))
|
|
|
|
p2p_server.register_instance(ModelRequestHandler(self))
|