def create_append_subscribe(): # http://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python name_str = 'python.test.writer_subscriber2_test.' + ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(10)) print "Name: " + name_str; gcl_name = gdp.GDP_NAME(name_str); print "Trying to create using " + platform.node(); logd_name = gdp.GDP_NAME(platform.node()); print "About to create " + name_str try: gdp.GDP_GCL.create(gcl_name, logd_name, ''); except : # If run with "python writer_subscriber2_test.py foo" # and the router and logd daemons are not running, then we end up here. # FIXME: Probably don't want to hardcode in the log name logd_name = gdp.GDP_NAME('edu.berkeley.eecs.gdp-01.gdplogd'); gdp.GDP_GCL.create(gcl_name, logd_name, ''); print "Created " + name_str print "Get the writer" gcl_handle_writer = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_AO); print "Get the subscriber" gcl_handle_subscriber = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) print "Make the subscribe call" # This is the actual subscribe call. gcl_handle_subscriber.subscribe(0, 0, None) print "About to loop" count = 0 outputList = [] while count < 10: count += 1 line = str(count) # Create a minimalist datum dictionary datum = {"data": line} print "About to append data" gcl_handle_writer.append(datum) # Write this datum to the GCL print "Done appending data" timeout = {'tv_sec':1, 'tv_nsec':0, 'tv_accuracy':0.0} print "About to call get_next_event()" event = gcl_handle_subscriber.get_next_event(timeout) datum = event["datum"] handle = event["gcl_handle"] print datum outputList.append(datum['data']) expectedList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] if outputList != expectedList: raise ValueError(', '.join(map(str, outputList)) + " was not equal to " + ', '.join(map(str, expectedList))) print "OK"
def gcl_subscription_init(): gdp.gdp_init() gcl_input = "ph.edu.upd.pcari.jasper.data" print "gcl: [%r]" % gcl_input gcl_name = gdp.GDP_NAME(gcl_input) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) return gcl_handle
def open_gcl_write(gcl_complete_name_str): print "opening for writing: %s" % gcl_complete_name_str skey = gdp.EP_CRYPTO_KEY(filename=gcl_complete_name_str + ".pem", keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) gcl_name = gdp.GDP_NAME(gcl_complete_name_str) return gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_AO, open_info={'skey': skey})
def main(*args): obj_name_mapping = {} for name_str in args: # create a python object gcl_name = gdp.GDP_NAME(name_str) # Assume that the GCL already exists gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) obj_name_mapping[gcl_handle] = name_str # this is the actual subscribe call gcl_handle.subscribe(0, 0, None) while True: # This blocks, until there is a new event event = gdp.GDP_GCL.get_next_event(None) datum = event["datum"] gcl_name = obj_name_mapping[event["gcl_handle"]] readable_time = time.ctime(datum["ts"]["tv_sec"] + (datum["ts"]["tv_nsec"] * 1.0 / 10**9)) print_str = ">>> gcl_name: %s\n>>> recno: %d, ts: %s\n%s" % ( gcl_name, datum["recno"], readable_time, datum["data"]) print print_str
def main(name_str): # Create a GDP_NAME object from a python string provided as argument. print "Name: " + name_str gcl_name = gdp.GDP_NAME(name_str) gcl_handle_writer = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_AO) gcl_handle_subscriber = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) # this is the actual subscribe call gcl_handle_subscriber.subscribe(0, 0, None) count = 0 while count < 10: count += 1 line = str(count) # Create a minimalist datum dictionary datum = {"data": line} gcl_handle_writer.append(datum) # Write this datum to the GCL timeout = {'tv_sec': 1, 'tv_nsec': 0, 'tv_accuracy': 0.0} print "About to call get_next_event()" event = gcl_handle_subscriber.get_next_event(timeout) datum = event["datum"] handle = event["gcl_handle"] print datum
def store_reported_location(fileName, logNames): """Example implementation of the listener for reported locations. The function listens for reported locations and stores them in a file. logNames - List of log names where the locations are reported (implements the report location primitive(s) of the SLI). fileName - Name of the file where the received locations are stored. """ obj_name_mapping = {} for name_str in logNames: gcl_name = gdp.GDP_NAME(name_str) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) obj_name_mapping[gcl_handle] = name_str gcl_handle.subscribe(0, 0, None) while True: event = gdp.GDP_GCL.get_next_event(None) timestamp_end = time.time() datum = event["datum"] gcl_name = obj_name_mapping[event["gcl_handle"]] data = datum["data"] timestamp_start = float(json.loads(data)['timestamp_start']) print gcl_name + str(': ') + 'New location information received.' print 'Latency: ' + str(timestamp_end - timestamp_start) string = gcl_name + ',' + str(timestamp_end) + ',' + data + '\n' with open(fileName, 'a') as the_file: the_file.write(string)
def request_location(parameters, logName): """ Implementation of the request location primitive of the SLI. parameters: location_type - type of location information: global/local/semantic dimensionality - for global/local location information type, is it 2D/3D? accuracy - desired accuracy of the requested location information period - desired period of location information provisioning on_event - when is location information provided - periodically with the period, on change of step from preceding location step - size of change of location information duration - duration of location information provisioning movement - do you want historical information, .e.g speed, orientation logName - name of the log for requesting location information """ location_type = parameters['location_type'] dimensionality = parameters['dimensionality'] accuracy = parameters['accuracy'] period = parameters['period'] provisioning_type = parameters['provisioning_type'] step = parameters['step'] duration = parameters['duration'] movement = parameters['movement'] # Evaluation purpose only, can be removed later timestamp_start = parameters['timestamp_start'] gcl_name = gdp.GDP_NAME(logName) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA) # First parameter for evaluation purposes only, can be removed later. data = json.dumps({'timestamp_start': timestamp_start, 'location_type': location_type, 'dimensionality': dimensionality, 'accuracy': accuracy, 'period': period, 'provisioning_type': provisioning_type, 'step': step, 'duration': duration, 'movement': movement}) gcl_handle.append({'data': data}) return 'New request written in the Request location log...'
def gcl_subscription_init(config_file): gdp.gdp_init() gcl_input = write_config("inputs.txt")[0] print "gcl: [%r]" % gcl_input gcl_name = gdp.GDP_NAME(gcl_input) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) return gcl_handle
def gcl_append_init(gcl_input, pem_input): gdp.gdp_init() gcl_name = gdp.GDP_NAME(gcl_input) skey = gdp.EP_CRYPTO_KEY(filename=pem_input, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA, {"skey": skey}) return gcl_handle
def gcl_append_init(): gdp.gdp_init() gcl_input = 'ph.edu.upd.pcari.jasper.data' pem_input = '_data.pem' gcl_name = gdp.GDP_NAME(gcl_input) skey = gdp.EP_CRYPTO_KEY(filename=pem_input, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA, {"skey":skey}) return gcl_handle
def gdp_thread(): gdp.gdp_init() print("GDP: connected.") gcl_name = gdp.GDP_NAME(args.gdp_log) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA) print("GDP: got log handle") while True: im_fname = gdp_image_queue.get() im = Image.open(im_fname) data = {"data": im.tostring()} gcl_handle.append(data) print("GDP: posted " + im_fname)
def gdp_source(): rospy.init_node("gdp_source") gdp.gdp_init() lock = Lock() args = _parse_args() topic_dict = {} gcl_name = gdp.GDP_NAME(args.logname) loghandle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) loghandle.subscribe(0, 0, None) try: buf = "" while not rospy.is_shutdown(): event = loghandle.get_next_event(None) data = event["datum"]["data"] d = pickle.loads(data) if args.preserve: topic = d["topic"] else: topic = "/gdp" + rospy.names.resolve_name(d["topic"]) topic_type = d["topic_type"] try: assert len(buf) == d["offset"] except AssertionError: ## This is when we start at the wrong time, and some ## chunks of a message already have been missed. continue buf = buf + d["data"] if len(buf) == d["msg_size"]: with lock: ## get publisher, create if doesn't exist pub = topic_dict.get(topic, None) if pub is None: msg_class = roslib.message.get_message_class( topic_type) pub = rospy.Publisher(topic, msg_class, queue_size=10) topic_dict[topic] = pub print "Publishing message" pub.publish(pickle.loads(zlib.decompress(buf))) buf = "" except rospy.ROSInterruptException: pass del loghandle
def gcl_append_init(config_file): gdp.gdp_init() gcl_input, pem_input = write_config(config_file) print "gcl: [%r]" % gcl_input print "pem: [%r]" % pem_input gcl_name = gdp.GDP_NAME(gcl_input) skey = gdp.EP_CRYPTO_KEY(filename=pem_input, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA, {"skey": skey}) return gcl_handle
def main(provisioning_service_id): print 'Creating the Register service log...' string = 'lemic.localization.esi.register_service' print subprocess.call([ './../gdp/apps/gcl-create', '-k', 'none', '-G', 'localhost', 'test.localization', string ]) print 'The Register service log most probably already exists, but no worries, ignore the previous error!' # Check if a service with that ID already exists! gdp.gdp_init('localhost') gcl_name = gdp.GDP_NAME(string) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) # Read the whole register service log recno = 1 services = [] while True: try: datum = gcl_handle.read(recno) services.append(json.loads(datum['data'])['service_id']) recno += 1 except: break if provisioning_service_id not in services: # Write an entry in the log logName = 'lemic.localization.esi.register_service' gcl_name = gdp.GDP_NAME(logName) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA) data = json.dumps({'service_id': provisioning_service_id}) gcl_handle.append({'data': data}) print 'Provisioning service ' + str( provisioning_service_id ) + ' successfully registered for provisioning.' else: print 'Log ID has to be unique! Be creative!'
def main(name_str): # create a python object _name = gdp.GDP_NAME(name_str) gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # this is the actual subscribe call gin_handle.subscribe_by_recno(0, 0, None) while True: # This blocks, until there is a new event event = gin_handle.get_next_event(None) datum = event["datum"] print datum["buf"].peek()
def main(name_str, keyfile=None): # the data that will be written data = generate_random_data(1000, 1000) # XXX handling secret key at Python level doesn't work at the moment open_info = {} if keyfile is not None: skey = gdp.EP_CRYPTO_KEY(filename=keyfile, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) open_info = {'skey': skey} gin_name = gdp.GDP_NAME(name_str) _name = "".join(["%0.2x" % ord(x) for x in gin_name.internal_name()]) print "opening", _name gin_handle = gdp.GDP_GIN(gin_name, gdp.GDP_MODE_RA, open_info) # writing the actual data for (idx, s) in enumerate(data): print "writing message", idx datum = gdp.GDP_DATUM() datum["buf"].write(s) # C implementation does not handle more than # a single datum appends at a time... yet. gin_handle.append_async([datum]) ## collect as many events as we had append operations for idx in xrange(len(data)): e = gin_handle.get_next_event(None) assert e["type"] == gdp.GDP_EVENT_CREATED or \ e["type"] == gdp.GDP_EVENT_SUCCESS ################################ #### reading the data back #### ################################ read_data = [] gin_handle.read_by_recno_async(-1 * len(data), len(data)) while True: e = gin_handle.get_next_event(None) if e["type"] == gdp.GDP_EVENT_DONE: break assert e["type"] == gdp.GDP_EVENT_DATA read_data.append(e["datum"]["buf"].peek()) print len(data), len(read_data) for idx in xrange(len(data)): # verify correctness if data[idx] == read_data[idx]: print "message %d matches" % idx
def push_rss_to_log(): """ Storing the collected RSS scans into a GDP log. """ logName = 'lemic.localization.resources' scan = get_rss_scan() gcl_name = gdp.GDP_NAME(logName) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA) gcl_handle.append({'data': scan}) return 'New scan pushed to the resource log...'
def read(self, name_str): """ Reads a info_log and returns the capabilities, permissions, public key(s) (if present) and certificate (if present) described in the info_log Parameters: name_str - name of the info_log from which to read """ gcl_name = gdp.GDP_NAME(name_str) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) recno = 1 capabilities = [] permissions = [] pkeys = [] certificate = None try: datum = gcl_handle.read(recno) num_capabilities = int(datum['data']) recno += 1 datum = gcl_handle.read(recno) num_permissions = int(datum['data']) recno += 1 datum = gcl_handle.read(recno) num_pkeys = int(datum['data']) recno += 1 while recno <= 3 + num_capabilities: datum = gcl_handle.read(recno) capabilities.append(datum['data']) recno += 1 while recno <= 3 + num_capabilities + num_permissions: datum = gcl_handle.read(recno) permissions.append(datum['data']) recno += 1 while recno <= 3 + num_capabilities + num_permissions + num_pkeys: datum = gcl_handle.read(recno) pkeys.append(datum['data']) recno += 1 datum = gcl_handle.read(recno) certificate = datum['data'] except: pass # Error could be because there is no certificate return capabilities, permissions, pkeys, certificate # capabilities = ["capability1", "capability2", "capability3"] # permissions = ["permission1", "permission2", "permission3"] # pkeys = ["pkey1", "pkey2", "pkey3", "pkey4"] # certificate = "test_certificate" # return capabilities, permissions, pkeys, certificate
def gdp_sink(): rospy.init_node("gdp_sink") gdp.gdp_init() lock = Lock() args = _parse_args() gcl_name = gdp.GDP_NAME(args.logname) lh = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA) for t in args.topic: rospy.Subscriber(t, AnyMsg, callback, callback_args=(t, lh, lock)) rospy.spin()
def get_resource(): logName = 'lemic.localization.resources' gcl_name = gdp.GDP_NAME(logName) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) data = {} datum1 = gcl_handle.read(-1) data[0] = json.loads(datum1['data'])['data'] datum2 = gcl_handle.read(-2) data[1] = json.loads(datum2['data'])['data'] datum3 = gcl_handle.read(-3) data[2] = json.loads(datum3['data'])['data'] datum4 = gcl_handle.read(-4) data[3] = json.loads(datum4['data'])['data'] return data
def open_gcl_read(gcl_complete_name_str, timeout=0): print "opening for reading: %s" % gcl_complete_name_str start = time.time() handler = [] while time.time() - start <= timeout + 1: try: gcl_name = gdp.GDP_NAME(gcl_complete_name_str) handler = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) break except: pass if timeout == 0: break del gcl_name return handler
def initialize_gcl_handles(self): print('GDPDataProcessor: Initializing GCL handles') for addr_gdp in self.list_addr_dict: print('GDPDataProcessor.initialize_gcl_handles: Initializing %s' % (addr_gdp['addr'])) try: gcl_name = gdp.GDP_NAME(addr_gdp['addr']) addr_gdp['gcl_name'] = gcl_name addr_gdp['gcl_handle'] = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RO) print('GDPDataProcessor.initialize_gcl_handles: Success!') except: addr_gdp['gcl_name'] = None addr_gdp['gcl_handle'] = None print('GDPDataProcessor.initialize_gcl_handles: FAIL') print('GDPDataProcessor: Finished!')
def main(name_str, tv_sec, tv_nsec, tv_accuracy=0.5): # create a python object _name = gdp.GDP_NAME(name_str) print _name.printable_name() # Assume that the GCL already exists gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # Create a dictionary for the timestamp. This is the preferred format # for timstamps in gdp. ts = {'tv_sec': tv_sec, 'tv_nsec': tv_nsec, 'tv_accuracy': tv_accuracy} # query by time stamp datum = gin_handle.read_by_ts(ts) print datum
def fetch_creator(self, rid): """ When we received a response, lookup the specific row id in the dupdb and return appropriate information for creating a spoofed response back to the creator. Also pdate the specific row to indicate that we have seen a response for the given log. Raise an UnknownResponse exception in case something is wrong. """ ## First get a connection from the pool conn = self.dupdb_cpool.get_connection() cur = conn.cursor() try: ## Fetch the original creator and rid from our database cur.execute( """SELECT creator, rid, ack_seen FROM logs WHERE rowid=?""", (rid, )) dbrows = cur.fetchall() ## We should know about this specific unique request ID assert len(dbrows) == 1 (__creator, orig_rid, ack_seen) = dbrows[0] creator = gdp.GDP_NAME(__creator).internal_name() ## We should not have seen a response already for this rid assert ack_seen == 0 logging.debug("Setting ack_seen to 1 for row %d", rid) cur.execute("UPDATE logs SET ack_seen=1 WHERE rowid=?", (rid, )) conn.commit() ## don't forget to return the connection back to the pool cur.close() self.dupdb_cpool.return_connection(conn) return (creator, orig_rid) except AssertionError as e: ## XXX handling errors via exceptions is probably better ## don't forget to return the connection back to the pool cur.close() self.dupdb_cpool.return_connection(conn) raise UnknownResponse
def main(name_str, keyfile): skey = gdp.EP_CRYPTO_KEY(filename=keyfile, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) # Create a GDP_NAME object from a python string provided as argument _name = gdp.GDP_NAME(name_str) # There's a GCL with the given name, so let's open it gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_AO, open_info={'skey': skey}) datum = gdp.GDP_DATUM() while True: line = sys.stdin.readline().strip() # read from stdin datum["buf"].reset() datum["buf"].write(line) gin_handle.append(datum) # Write this datum to the GCL
def __init__(self, srcSink="", nameStr="", paramName="", lagVal=0, normMeth='none', key="", password=""): if nameStr == "": raise ValueError("GCL name must be provided.") if paramName == "": raise ValueError("JSON parameter name must be provided.") # Log name in GDP self.gclName = gdp.GDP_NAME(nameStr) if srcSink == "": raise ValueError("Source/Sink must be provided.") elif srcSink == "GDP_I": self.IO = 'in' # Assume that GCL already exists and create the GCL handle self.gclHandle = gdp.GDP_GCL(self.gclName, gdp.GDP_MODE_RO) elif srcSink == "GDP_O": self.IO = 'out' if key == "" or password == "": raise ValueError("Key path and password must \ be provided.") else: skey = gdp.EP_CRYPTO_KEY(filename=key, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) open_info = {'skey': skey} # TODO Bypass password prompt # Assume that GCL already exists and create the GCL handle self.gclHandle = gdp.GDP_GCL(self.gclName, gdp.GDP_MODE_RA, open_info) # JSON parameter name to be used in each log record self.param = paramName # Lag from the current record. Can be used to implement time series functions. self.lag = lagVal # Normalization method for data: # 'none': no normalization # 'lin': linear normalization: mean-zeroed and divided by std self.norm = normMeth # Normalization parameters (i.e., avg, std etc.) self.normParam = {}
def main(name_str, start, stop): # create a python object _name = gdp.GDP_NAME(name_str) print _name.printable_name() # Assume that the GCL already exists gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # initialize this to the first record number recno = start while recno <= stop: try: datum = gin_handle.read_by_recno(recno) print datum["buf"].peek() recno += 1 except Exception as e: # Typically, end of log. raise e
def request_discovery(ils_id, services): """ Discovery of provisioning features form the available provisioning services. ils_id - ID of this integrated location service, needed for shared provisioning services services - services whose provisioning features are to be requested """ # Request features discovery for service_id in services: # Writing an entry to a 'service discovery' log of each requested provisioning service logName = 'lemic.localization.esi.service_discovery_' + str(service_id) gcl_name = gdp.GDP_NAME(logName) gcl_handle = gdp.GDP_GCL(gcl_name, gdp.GDP_MODE_RA) data = json.dumps({'ils_id': ils_id}) gcl_handle.append({'data': data}) return
def main(name_str, keyfile=None): # the data that will be written data = generate_random_data(100, 10) # XXX handling secret key at Python level doesn't work at the moment open_info = {} if keyfile is not None: skey = gdp.EP_CRYPTO_KEY(filename=keyfile, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) open_info = {'skey': skey} gin_name = gdp.GDP_NAME(name_str) _name = "".join(["%0.2x" % ord(x) for x in gin_name.internal_name()]) print "opening", _name gin_handle = gdp.GDP_GIN(gin_name, gdp.GDP_MODE_RA, open_info) # writing the actual data datum = gdp.GDP_DATUM() # reusable datum for (idx, s) in enumerate(data): print "writing message", idx datum["buf"].reset() datum["buf"].write(s) gin_handle.append(datum) ################################ #### reading the data back #### ################################ # read by recno (synchronous) read_by_recno = [] for idx in xrange(-1*len(data),0): print "reading message", -1*idx, "from the end" datum = gin_handle.read_by_recno(idx) # -n => n-th record from end read_by_recno.append(datum["buf"].peek()) for idx in xrange(len(data)): # verify correctness if data[idx] == read_by_recno[idx]: print "message %d matches" % idx
def __init__(self, logname, limit=10000): """ Initialize with just the log name, and optionally cache size limit is the number of records to keep in the cache. This is a soft limit, which means that we will go over the limit on various occasions, but we will try to be within a certain factor of the specified limit (by default, 2). This enables us to minimize the cleanup overhead. """ gdp.gdp_init() # No side-effects of calling this multiple times # gdp.dbg_set("*=20") self.logname = logname self.lh = gdp.GDP_GIN(gdp.GDP_NAME(logname), gdp.GDP_MODE_RO) self.limit = limit self.cache = {} # recno => record cache (limited size) self.atime = {} # recno => time of access (same size as cache) ## populate the limits self.leastRecent() self.mostRecent()