#
 # Thierry Parmentelat - INRIA
 # 
+import time
 
 from types import StringTypes
 
        'boot_state' : Parameter (str, "Boot state, see Node"),
         'model' : Parameter (str,"Model, see Node"),
         'version' : Parameter (str,"Version, see Node"),
-#        'date_created': Parameter(int, "Creation time, see Node"),
-#        'last_updated': Parameter(int, "Update time, see Node"),
+        'date_created': Parameter(int, "Creation time, see Node"),
+        'last_updated': Parameter(int, "Update time, see Node"),
+        'slice_ids': Parameter([int], "List of slices on this node"),
        }
 
     def __init__(self,api,fields={},uptodate=True):
        Row.__init__(self,api,fields)
        self.uptodate=uptodate
 
+    def validate_date_created(self,timestamp):
+        return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(timestamp))
+
+    def validate_last_updated(self,timestamp):
+        return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(timestamp))
+
     def purge_peer_node (self,commit=True):
         sql = "DELETE FROM peer_node WHERE node_id=%d"%self['node_id']
         self.api.db.do(sql)
                 sql += " AND (%s)" % foreign_node_filter.sql(api, "AND")
 
        self.selectall(sql)
+
+    # managing an index by hostname
+    def hostname_index(self):
+        if 'hostname' not in self.columns:
+            raise PLCFault,"ForeignNodes::index_hostname, hostname not selected"
+        self.index={}
+        for foreign_node in self:
+            self.index[foreign_node['hostname']]=foreign_node
+            
+    def hostname_add_by(self,foreign_node):
+        self.index[foreign_node['hostname']]=foreign_node
+
+    def hostname_locate(self,hostname):
+        return self.index[hostname]
+            
 
        # we get the whole table just in case 
        # a host would have switched from one plc to the other
        local_foreign_nodes = ForeignNodes (self.api)
+        # new to index it by hostname for searching later
+        local_foreign_nodes.hostname_index()
        
        ### mark entries for this peer outofdate
         old_count=0;
 
         ### these fields get copied through
         ### xxx need to figure how to revert unix timestamp to db timestamp format
-#        remote_fields = ['boot_state','model','version','date_created','last_updated']
-        remote_fields = ['boot_state','model','version']
+        remote_fields = ['boot_state','model','version','date_created','last_updated']
+
         
        ### scan the new entries, and mark them uptodate
        for node in current_peer_nodes:
            hostname = node['hostname']
             try:
-                foreign_node = ForeignNodes(self.api,{'hostname':hostname})[0]
+                foreign_node = local_foreign_nodes.hostname_locate(hostname)
                 if foreign_node['peer_id'] != peer_id:
                     ### the node has changed its plc, needs to update peer_node
                     old_peer_id = foreign_node['peer_id']
                 new_foreign_node.sync()
                 new_foreign_node.uptodate = True
                 self.manage_node(new_foreign_node,True)
+                local_foreign_nodes.hostname_add_by(new_foreign_node)
 
 
        ### delete entries that are not uptodate
 
 --
 -- Copyright (C) 2006 The Trustees of Princeton University
 --
--- $Id: planetlab4.sql,v 1.29 2006/11/08 22:07:29 mlhuang Exp $
+-- $Id: planetlab4.sql,v 1.30 2006/11/10 15:05:52 thierry Exp $
 --
 
 --------------------------------------------------------------------------------
 nodes.version,
 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
-node_slices.slice_ids,
+COALESCE(node_slices.slice_ids, '{}') AS slice_ids,
 nodes.deleted
 FROM nodes
 LEFT JOIN peer_node USING (node_id)