worked on splitting regions and implemented set_allocator / set_consumer
[sfa.git] / gacks / gacksserver.py
index 730fc26..5c58999 100644 (file)
@@ -16,6 +16,9 @@ from misc import *
 from record import *
 from geniticket import *
 
+from gacksexcep import *
+from gackscalendar import *
+
 ##
 # GacksServer is a GeniServer that serves component interface requests.
 #
@@ -41,10 +44,73 @@ class GacksServer(GeniServer):
         self.server.register_function(self.get_handle)
 
     def get_handle(self, rspec):
-        pass
+        handles = rspec_to_handles(rspec)
+        return handles_to_strings(handles)
+
+    def set_allocator(self, callerGID_str, handle_strs, allocatorGID_str, which, where, reqsig)
+        callerGID = GID(callerGID_str)
+        allocatorGID = GID(allocatorGID_str)
+
+        # TODO: verify callerGID ssl key
+
+        callerGID.verify_chain(self.trusted_cert_list)
+        allocatorGID.verify_chain(self.trusted_cert_list)
+
+        handles = strings_to_handles(handle_strs)
+        for handle in handles:
+            # find the existing records that overlap the handle
+            existing_recs = self.calendar.query_handles([handle])
+
+            if not existing_recs:
+                raise GacksResourceNotFound(hand.as_string())
+
+            # TODO: Merge existing_recs
+
+            for item in existing_recs:
+                if not item.contains_allocator(callerGID->get_name()):
+                    raise CallerNotAllocator(item.as_string())
+                if not item.is_superset(handle):
+                    raise RequestSpansReservations(handle.as_string() + " on " + item.as_string())
+
+            leftovers = []
+            results = []
+            for item in existing_recs:
+                if item.is_proper_supserset(handle):
+                    parts = item.clone().split_subset(handle.unitStart, handle.unitStop, handle.timeStart, handle.timeStop)
+                    results.extend(parts[0])
+                    leftovers.extend(parts[1:])
+                else:
+                    results.extend(item)
+
+            for item in existing_recs:
+                calendar.remove_record(item)
+
+            for item in leftovers:
+                calendar.insert_record(item)
+
+            for item in results:
+                item.set_allocator(callerGID->get_name(), allocatorGID->get_name(), which, where)
+                calendar.insert_record(item)
+
+    def set_consumer(self, callerGID_str, handle_strs, cred_str, reqsig):
+        callerGID = GID(string = callerGID_str)
+        cred = Credential(string = cred_str)
+
+        # TODO: verify callerGID ssl key
+
+        callerGID.verify_chain(self.trusted_cert_list)
+        cred.verify_chain(self.trusted_cert_list)
+
+        handles = strings_to_handles(handle_strs)
+        for handle in handles:
+            existing_recs = self.calendar.query_handles([handle])
+
+            if not existing_recs:
+                raise GacksResourceNotFound(hand.as_string())
 
-    def set_allocator(self, callerGid, Handle, allocatorGid, which, where, reqsig)
-        pass
+            for rec in existing_recs:
+                rec.set_consumer(cred.objectGID.get_name())
+                calendar.update_record(rec)
 
 if __name__ == "__main__":
     global TrustedRoots