def recv(self, nbits=0): ''' Retrieve `nbits` bits from the hardware. If `nbits` > 0, return the bits retrieved. If there are no enough bits currently available, `recv` will block until enough bits become available. If `nbits` is 0, return a iterable which when iterated over will retrieve 1 bit at a time. Equivalent to: recv() while True: yield recv(1) ''' if not nbits: return iter(functools.partial(self.recv, 1), None) while True: while True: try: self.ebuffer += self.iqueue.get(block=False) except Queue.Empty: break if len(self.ebuffer) >= nbits: ret, self.ebuffer = utils.split_at(self.ebuffer, nbits) return ret else: mint.wait(0) continue
def _send(self): # fetch from entity & send to peer port at specified rate self.obuffer += self.fetch_from(self.e2p_queue, block=False) #print '{} {}\n'.format(self, self.obuffer) bits, self.obuffer = utils.split_at(self.obuffer, self.rate) if bits: self.p2p_queue.put(bits)
def transfer(self, port1, port2): bit = port1.recv(1) #import random #bit = '1' if random.randint(0, 1) else '0' self.pipes[port1] += bit bits, self.pipes[port1] = utils.split_at(self.pipes[port1], 1) port2.send(bits)
def collect_bits(self, port): # TODO # self.data += ibuffer.take_bytes() # # collect bits at multiples of 8 bits, port.ibuffer = utils.split_at(port.ibuffer, len(port.ibuffer) // 8 * 8) if bits: self.data += utils.unbitify(bits)
def get_bit(self, port): if port.ibuffer: self.pipes[port] += port.ibuffer port.ibuffer = '' else: self.pipes[port] += self.noise_bit() bit, self.pipes[port] = utils.split_at(self.pipes[port], 1) return bit
def collect_bits(self, port): # TODO # self.data += ibuffer.take_bytes() # # collect bits at multiples of 8 bits, port.ibuffer = utils.split_at(port.ibuffer, len(port.ibuffer) // 8 * 8) if bits: self.data += utils.unbitify(bits) print '{} Collected: {}'.format(self, utils.format_bytes(self.data)) raw_input()
def recv(self, nbits=None, block=True): if block: if nbits is None: raise Exception('blocking recv must specify the number of bits') while len(self.entity_buffer) < nbits: self.entity_buffer += self.fetch_from(self.p2e_queue, block=True) bits, self.entity_buffer = utils.split_at(self.entity_buffer, nbits) return bits else: self.entity_buffer += self.fetch_from(self.p2e_queue, block=False) bits, self.entity_buffer = self.entity_buffer, '' return bits
def handoff(self): if not self.peer: return sent, self.obuffer = utils.split_at(self.obuffer, min(self.rate, self.peer.rate)) self.peer.ibuffer += sent # TODO # this ought to be implemented like this: # buffer as a descriptor # when altered, emit the signal # so bit_arrive then is no need to be emitted here # writting the ugly self.peer... etc. self.peer.bit_arrive(self.peer)
def recv(self, nbits): while True: while True: try: self.ebuffer += self.iqueue.get(block=False) except Queue.Empty: break if len(self.ebuffer) >= nbits: ret, self.ebuffer = utils.split_at(self.ebuffer, nbits) return ret else: mint.wait(0) continue
def recv(self, nbits=None, block=True): if block: if nbits is None: raise Exception( 'blocking recv must specify the number of bits') while len(self.entity_buffer) < nbits: self.entity_buffer += self.fetch_from(self.p2e_queue, block=True) bits, self.entity_buffer = utils.split_at(self.entity_buffer, nbits) return bits else: self.entity_buffer += self.fetch_from(self.p2e_queue, block=False) bits, self.entity_buffer = self.entity_buffer, '' return bits
def get(self, nbits=None): if not nbits: nbits = len(self.ibuffer) ret, self.ibuffer = utils.split_at(self.ibuffer, nbits) return ret
def output(self): self.osymbol, self.obuffer = utils.split_at(self.obuffer, 1)
def transfer(self, port1, port2): data = port1.recv(block=False) self.pipes[port1] += data or '0' * self.rate bits, self.pipes[port1] = utils.split_at(self.pipes[port1], self.rate) port2.send(bits)
def transfer(self, port1, port2): self.pipes[port1] += port1.recv(1) bits, self.pipes[port1] = utils.split_at(self.pipes[port1], 1) port2.send(bits)
def recv(self, nbits=0): if not nbits: return iter(functools.partial(self.recv, 1), '') bits, self.bits = utils.split_at(self.bits, nbits) return bits
def _recv(self): # fetch from peer port & send to entity at specified rate self.ibuffer = self.fetch_from(self.peer.p2p_queue, block=False) bits, self.ibuffer = utils.split_at(self.ibuffer, self.rate) if bits: self.p2e_queue.put(bits)
def output(self): bits, self.obuffer = utils.split_at(self.obuffer, 1) self.osymbol = bits self.obits += bits
def recv(self, nbytes): while len(self.data) < nbytes: yield self.port.bit_arrive.event ret, self.data = utils.split_at(self.data, nbytes) mint.ret(ret)