Commit bb0e59a7 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Implement the node manager a bit more seriously.

git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype2@17 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 023e33f7
......@@ -7,6 +7,8 @@ from connection import Connection as BaseConnection
from database import DatabaseManager
from config import ConfigurationManager
from protocol import Packet, MASTER_NODE_TYPE, STORAGE_NODE_TYPE, CLIENT_NODE_TYPE
from node import NodeManager, MasterNode, StorageNode, ClientNode, \
RUNNING_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, BROKEN_STATE
class Connection(BaseConnection):
"""This class provides a master-specific connection."""
......@@ -69,6 +71,7 @@ class Application:
# Internal attributes.
self.dm = DatabaseManager(self.database, self.user, self.password)
self.cm = ConnectionManager(app = self, connection_klass = Connection)
self.nm = NodeManager()
def initializeDatabase(self):
"""Initialize a database by recreating all the tables.
......@@ -154,6 +157,9 @@ class Application:
# Now ready to load persistent data from the database.
self.loadData()
for ip_address, port in self.master_node_list:
self.nm.add(MasterNode(ip_address = ip_address, port = port))
# Make a listening port.
self.cm.listen(self.ip_address, self.port)
......
WORKING_STATE = 0
TEMPORARILY_DOWN_STATE = 1
DOWN_STATE = 2
BROKEN_STATE = 3
RUNNING_STATE = 0
TEMPORARILY_DOWN_STATE = 2
DOWN_STATE = 3
BROKEN_STATE = 4
class Node(object):
"""This class represents a node."""
def __init__(self, ip_address = None, port = None, uuid = None):
self.state = WORKING_STATE
self.state = RUNNING_STATE
self.ip_address = ip_address
self.port = port
self.uuid = uuid
self.manager = None
def getState(self):
return self.state
def changeState(self, new_state):
def setState(self, new_state):
self.state = new_state
def setServer(self, ip_address, port):
self.ip_address = ip_address
self.port = port
self.manager.registerServer(self)
def getServer(self):
return self.ip_address, self.port
def setUUID(self, uuid):
self.uuid = uuid
self.manager.registerUUID(self)
def getUUID(self):
return self.uuid
class MasterNode(Node):
"""This class represents a master node."""
pass
......@@ -34,7 +50,56 @@ class NodeManager(object):
"""This class manages node status."""
def __init__(self):
self.node_dict = {}
self.node_list = []
self.server_dict = {}
self.uuid_dict = {}
def add(self, node):
self.node_list.append(node)
if node.getServer()[0] is not None:
self.registerServer(node)
if node.getUUID() is not None:
self.registerUUID(node)
def remove(self, node):
self.node_list.remove(node)
self.unregisterServer(node)
self.unregisterUUID(node)
def registerServer(self, node):
self.server_dict[node.getServer()] = node
def unregisterServer(self, node):
try:
del self.server_dict[node.getServer()]
except KeyError:
pass
def registerUUID(self, node):
self.server_dict[node.getUUID()] = node
def unregisterUUID(self, node):
try:
del self.server_dict[node.getUUID()]
except KeyError:
pass
def getNodeList(self, filter = None):
if filter is None:
return list(self.node_list)
return [n for n in self.node_list if filter(n)]
def getMasterNodeList(self):
return self.getNodeList(filter = lambda node: isinstance(node, MasterNode))
def getStorageNodeList(self):
return self.getNodeList(filter = lambda node: isinstance(node, StorageNode))
def getClientNodeList(self):
return self.getNodeList(filter = lambda node: isinstance(node, ClientNode))
def getNodeByServer(self, ip_address, port):
return self.server_dict.get((ip_address, port))
def add(self, *args):
def getNodeByUUID(self, uuid):
return self.uuid_dict.get(uuid)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment