Split the model classes in different files

This commit is contained in:
Marco Pesenti Gritti 2006-05-15 12:27:36 -04:00
parent 931c362543
commit e0a046cd53
6 changed files with 48 additions and 37 deletions

View File

@ -22,5 +22,6 @@ sugar/__installed__.py
sugar/browser/Makefile sugar/browser/Makefile
sugar/chat/Makefile sugar/chat/Makefile
sugar/p2p/Makefile sugar/p2p/Makefile
sugar/p2p/model/Makefile
sugar/shell/Makefile sugar/shell/Makefile
]) ])

View File

@ -3,7 +3,6 @@ sugar_PYTHON = \
__init__.py \ __init__.py \
Buddy.py \ Buddy.py \
Group.py \ Group.py \
Model.py \
Service.py \ Service.py \
StreamReader.py \ StreamReader.py \
StreamWriter.py \ StreamWriter.py \

View File

@ -1,24 +1,10 @@
MODEL_SERVICE_TYPE = "_olpc_model._tcp"
MODEL_SERVICE_PORT = 6300
import socket import socket
import xmlrpclib import xmlrpclib
from sugar.p2p.Service import Service from sugar.p2p.Service import Service
import network
class RemoteModel: MODEL_SERVICE_TYPE = "_olpc_model._tcp"
def __init__(self, service): MODEL_SERVICE_PORT = 6300
self._service = service
addr = "http://%s:%d" % (service.get_address(), service.get_port())
self._client = xmlrpclib.ServerProxy(addr)
def get_value(self, key):
return self._client.get_value(key)
def set_value(self, key, value):
self._client.set_value(key, value)
class ModelRequestHandler(object): class ModelRequestHandler(object):
def __init__(self, model): def __init__(self, model):
@ -64,23 +50,3 @@ class LocalModel:
port = port + 1 port = port + 1
tries = tries - 1 tries = tries - 1
service.set_port(port) service.set_port(port)
class Store:
def __init__(self, group):
self._group = group
self._local_models = {}
def create_model(self, model_id):
model = LocalModel(self._group, model_id)
self._local_models[model_id] = model
return model
def get_model(self, model_id):
if self._local_models.has_key(model_id):
return self._local_models(model_id)
else:
service = self._group.get_service(model_id, MODEL_SERVICE_TYPE)
if service:
return RemoteModel(service)
else:
return None

View File

@ -0,0 +1,6 @@
sugardir = $(pythondir)/sugar/p2p/model
sugar_PYTHON = \
__init__.py \
LocalModel.py \
RemoteModel.py \
Store.py

View File

@ -0,0 +1,17 @@
import xmlrpclib
from sugar.p2p.Service import Service
import network
class RemoteModel:
def __init__(self, service):
self._service = service
addr = "http://%s:%d" % (service.get_address(), service.get_port())
self._client = xmlrpclib.ServerProxy(addr)
def get_value(self, key):
return self._client.get_value(key)
def set_value(self, key, value):
self._client.set_value(key, value)

22
sugar/p2p/model/Store.py Normal file
View File

@ -0,0 +1,22 @@
from sugar.p2p.model.RemoteModel import RemoteModel
from sugar.p2p.model.LocalModel import LocalModel
class Store:
def __init__(self, group):
self._group = group
self._local_models = {}
def create_model(self, model_id):
model = LocalModel(self._group, model_id)
self._local_models[model_id] = model
return model
def get_model(self, model_id):
if self._local_models.has_key(model_id):
return self._local_models(model_id)
else:
service = self._group.get_service(model_id, MODEL_SERVICE_TYPE)
if service:
return RemoteModel(service)
else:
return None