def random_bytes(n): """ Make some random bytes, preferably of cryptographic quality. This function is used to generate random bytes for challenges and for the node's address in the circle. It would be preferable to have a urandom equivalent for windows, but alas... Ensures @E14: (type(ret) == types.StringType) and (len(ret) == n).""" check.check_has_type(n, types.IntType) check.check_assertion(n >= 0) #=@R27 ret = '' if n != 0: try: file = open('/dev/urandom','rb') ret = file.read(n) file.close() except IOError: pass for i in range(n - len(ret)): ret = ret + chr(random.randrange(256)) check.check_postcondition((type(ret) == types.StringType) and (len(ret) == n), '@E14') return ret
def request_meeting(chat, params): """ Request a meeting with the user "nickname". This function is attached to the chat command "/meet" Send a request to the receiver. If he/she accept, then a local meeting call will be emited. node.call receiver query="start meeting" if return yes: start a call to the receiver else: show the error message (rejected, or technical failure) """ check.check_matches(params, ['text']) check.check_assertion(1 <= len(params) <= 1) nickname = params[0] if not app.name_server.nicknames.has_key(nickname): app.chat.show("Unknown user '%s' at the local names server.\n"% nickname) return server_acq = app.name_server.nicknames[nickname] # we are the "client", the receiver is the "server". if server_acq.address == node.address: app.chat.show("You can not meet yourself, sorry.\n") return if not server_acq.online: app.chat.show("The user '%s' is not online.\n"% nickname) return Task(request_meeting_task, server_acq).start() return
def __init__(self, name_server, map, remember): check.check_isinstance(name_server, Name_server) check.check_matches(map, self.map_tmpl) check.check_has_type(remember, types.IntType) check.check_assertion(remember in (0, 1)) utility.Task_manager.__init__(self) self.editor_open = 0 self.watch = 0 # used to tell if the person logs in or out in chat window # nothing to do with 'watchers' self.distance = None self.drm = 0 self.address = None self.watching = 0 self.watched = 0 self.online = 0 self.status = { } # .name and .nickname will be filled in from map. We initialize # them here just to placate pychecker. # Note: Don't confuse self.name (check.is_name) with self.info['name'] # (person's preferred nickname). self.name = '(error)' self.nickname = '(error)' self.name_server = name_server for key,value in map.items(): setattr(self, key, value) self.remember = remember self.check_invar()
def release_lock(self, acquirer_name): """acquirer_name should be equal to the value passed in the corresponding acquire_lock call.""" check.check_assertion(self.lock_acquired_by != []) curr_acquirer = self.lock_acquired_by.pop() check.check_assertion(acquirer_name == curr_acquirer) self.lock.release()
def bisect(a, too_high): """Return the first index i in a such that too_high(a[i]) is true; or len(a) if too_high(x) is true for all elements x in a. The usual use for this function is deciding where to insert element; e.g. my_list.insert(bisect(my_list, lambda elem,x=x: x < elem), x) Requires: a is monotonic non-decreasing in the truth of too_high; i.e. exists[pos] ((all[j in [0, pos)] not too_high(a[j])) and all[j in [pos, len(a))] too_high(a[j])). Put another way: all[(i,j) s.t. i <= j] too_high(a[i]) implies too_high(a[j]). Note that this more or less requires that neither a nor too_high change during execution (say due to writes from another thread). Ensures: The return value equals pos above. I.e.: (all[j in [0, ret)] not too_high(a[j])) and (all[j in [ret, len(a))] too_high(a[j])). """ check.check_has_type(a, types.ListType) check.check_is_callable(too_high) lo = 0 hi = len(a) while lo < hi: mid = (lo+hi) >> 1 if too_high(a[mid]): hi = mid else: lo = mid+1 check.check_assertion((0 <= lo) and (lo <= len(a))) ## Check sortedness: #for elem in a[:lo]: # if too_high(elem): # check.show_bug('utility.bisect: unsorted input list (or bug in bisect code)') #for elem in a[lo:]: # if not too_high(elem): # check.show_bug('utility.bisect: unsorted input list (or bug in bisect code)') return lo