From: Scott Baker Date: Wed, 8 Oct 2008 00:31:48 +0000 (+0000) Subject: check in gacks handle code X-Git-Tag: sfa-0.9-0@14641~831 X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=commitdiff_plain;h=c80b365d0fcc034001fb2b9959c8935085e87890 check in gacks handle code --- diff --git a/gacks/gackscalendar.py b/gacks/gackscalendar.py new file mode 100644 index 00000000..33d3af9b --- /dev/null +++ b/gacks/gackscalendar.py @@ -0,0 +1,56 @@ +class GacksCalendar: + def __init__(self): + pass + + def query(self, id, unitStart, unitStop, timeStart, timeStop): + pass + + def insert_record(self, item): + pass + +class GacksListCalendar(GacksCalendar): + def __init__(self): + self.items = [] + + def test_id(x, y): + if not x: + return True + else: + return (x == y) + + def test_lesser(x, y): + if not x: + return True + else if y==INFINITY: + return True + else: + return (x=y) + + def query(self, id, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY): + list = [] + for item in self.items: + if test_id(id, item.id) and + test_lesser(unitStart, item.unitStop) and + test_greater_equal(unitStop, item.unitStart) and + test_lesser(timeStart, item.timeStop) and + test_greater_equal(timeStop, item.timeStart): + list = list + item + return list + + def insert_record(self, item): + conflicts = self.query(item.id, item.unitStart, item.unitStop, item.timeStart, item.timeStop) + for conflict in conflicts: + self.items.remove(conflict) + + self.items.append(item) + diff --git a/gacks/gackshandle.py b/gacks/gackshandle.py new file mode 100644 index 00000000..69c95da0 --- /dev/null +++ b/gacks/gackshandle.py @@ -0,0 +1,151 @@ +## +# This module implements Gacks handles +## + +import sys + +## +# GacksHandle is an object representing a handle. A handle is the following +# tuple: +# +# (id, unitstart, unitstop, timestart, timestop) +# +# id is a text identifier for the resource, for example "CPU" +# +# unitstart and unitstop form an interval [unitstart, unitstop) in unit +# space. For example, [0, 10) could represent 10% of a CPU. +# +# timestart and timestop form an interval [timestart, timestop) in time +# space. +# +# If unitstop==INFINITY, or timestop==INFINITY, then it is treated as infinity +# +# The GacksHandle also doubles as an RSPEC + +INFINITY = "inf" #sys.maxint + +class GacksHandle: + def __init__(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, string=None): + self.id = id + self.unitStart = unitStart + self.unitStop = unitStop + self.timeStart = timeStart + self.timeStop = timeStop + if string: + self.load_from_string(string) + + def as_string(self): + return str(self.id) + "#" + \ + str(self.unitStart) + "-" + str(self.unitStop) + "#" + \ + str(self.timeStart) + "-" + str(self.timeStop) + + def parse_range(self, str): + parts = str.split("-") + if len(parts)!=2: + raise ValueError + + if parts[0] != INFINITY: + parts[0] = int(parts[0]) + + if parts[1] != INFINITY: + parts[1] = int(parts[1]) + + return parts + + def load_from_string(self, str): + parts = str.split("#") + + self.id = parts[0] + + if len(parts) > 1: + (self.unitStart, self.unitStop) = self.parse_range(parts[1]) + + if len(parts) > 2: + (self.timeStart, self.timeStop) = self.parse_range(parts[2]) + + def get_quantity(self): + if self.unitStop == INFINITY: + return INFINITY + else: + return self.unitStop-self.unitStart + + def get_duration(self): + if self.timeStop == INFINITY: + return INFINITY + else: + return self.timeStop-self.timeStart + + def dump(self): + print str(self.id) + ": " + \ + "units " + str(self.unitStart) + "-" + str(self.unitStop) + \ + "time " + str(self.timeStart) + "-" + str(self.timeStop) + + def clone(self): + return GacksHandle(self.id, self.unitStart, self.unitStop, + self.timeStart, self.timeStop) + + def split(self, unit=None, time=None): + h1 = self.clone() + h2 = self.clone() + + if unit: + h1.unitStop = unit + h2.unitStart = unit + + if time: + h1.timeStop = time + h2.timeStart = time + + return (h1, h2) + + +class GacksRecord(GacksHandle): + def __init__(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, allocatorHRNs=[], consumerHRN=None): + GacksHandle.__init__(self, id, unitStart, unitStop, timeStart, timeStop) + self.allocatorHRNs = allocatorHRNs + self.consumerHRN = consumerHRN + + def dump(self): + GacksHandle.dump(self) + print " allocators:", ", ".join(self.allocatorHRNs) + print " consumer:", self.consumerHRN + + def clone(self): + return GacksRecord(self.id, self.unitStart, self.unitStop, + self.timeStart, self.timeStop, + self.allocatorHRNs, self.consumerHRN) + + def set_allocator(self, callerHRN, allocatorHRN, which, where): + # build up a list of the positions of callerHRN inside of the + # allocator list + + positions = [] + for i, hrn in enumerate(self.allocatorHRNs): + if hrn == callerHRN: + positions.append(i) + + pos = positions[which] + + # truncate the allocator list at the appropriate place. + # if where==True, + # keep callerHRN in the list and append allocatorHRN after + # otherwise, + # remove callerHRN and replace with allocatorHRN + + if where: + self.allocatorHRNs = self.allocatorHRNs[:(pos+1)] + else: + self.allocatorHRNs = self.allocatorHRNs[:pos] + + self.allocatorHRNs.append(allocatorHRN) + + def get_allocators(self): + return self.allocatorHRNs[:] + + def set_consumer(self, consumerHRN): + self.consumerHRN = consumerHRN + + def get_consumer(self): + return self.consumerHRN + + diff --git a/gacks/gacksrspec.py b/gacks/gacksrspec.py new file mode 100644 index 00000000..3bd1f0e2 --- /dev/null +++ b/gacks/gacksrspec.py @@ -0,0 +1,2 @@ +foo +bar diff --git a/gacks/gacksserver.py b/gacks/gacksserver.py new file mode 100644 index 00000000..730fc26f --- /dev/null +++ b/gacks/gacksserver.py @@ -0,0 +1,71 @@ +## +# Gacks Server +## + +import tempfile +import os + +import sys + +from cert import * +from gid import * +from geniserver import * +from excep import * +from trustedroot import * +from misc import * +from record import * +from geniticket import * + +## +# GacksServer is a GeniServer that serves component interface requests. +# + +class GacksServer(GeniServer): + + ## + # Create a new GacksServer object. + # + # @param ip the ip address to listen on + # @param port the port to listen on + # @param key_file private key filename of registry + # @param cert_file certificate filename containing public key (could be a GID file) + + def __init__(self, ip, port, key_file, cert_file): + GeniServer.__init__(self, ip, port, key_file, cert_file) + + ## + # Register the server RPCs for Gacks + + def register_functions(self): + GeniServer.register_functions(self) + self.server.register_function(self.get_handle) + + def get_handle(self, rspec): + pass + + def set_allocator(self, callerGid, Handle, allocatorGid, which, where, reqsig) + pass + +if __name__ == "__main__": + global TrustedRoots + + key_file = "gacksserver.key" + cert_file = "gacksserver.cert" + + # if no key is specified, then make one up + if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)): + key = Keypair(create=True) + key.save_to_file(key_file) + + cert = Certificate(subject="component") + cert.set_issuer(key=key, subject="component") + cert.set_pubkey(key) + cert.sign() + cert.save_to_file(cert_file) + + TrustedRoots = TrustedRootList() + + s = ComponentManager("", 12346, key_file, cert_file) + s.trusted_cert_list = TrustedRoots.get_list() + s.run() +