Esempio n. 1
0
    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
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
0
 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)
Esempio n. 6
0
 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
Esempio n. 7
0
 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
Esempio n. 8
0
 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)
Esempio n. 9
0
 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()
Esempio n. 10
0
 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
Esempio n. 11
0
 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)
Esempio n. 12
0
 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
Esempio n. 13
0
 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
Esempio n. 14
0
 def get(self, nbits=None):
     if not nbits:
         nbits = len(self.ibuffer)
     ret, self.ibuffer = utils.split_at(self.ibuffer, nbits)
     return ret
Esempio n. 15
0
 def output(self):
     self.osymbol, self.obuffer = utils.split_at(self.obuffer, 1)
Esempio n. 16
0
 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)
Esempio n. 17
0
 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)
Esempio n. 18
0
 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
Esempio n. 19
0
 def get(self, nbits=None):
     if not nbits:
         nbits = len(self.ibuffer)
     ret, self.ibuffer = utils.split_at(self.ibuffer, nbits)
     return ret
Esempio n. 20
0
 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)
Esempio n. 21
0
 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)
Esempio n. 22
0
 def output(self):
     bits, self.obuffer = utils.split_at(self.obuffer, 1)
     self.osymbol = bits
     self.obits += bits
Esempio n. 23
0
 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)
Esempio n. 24
0
 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)
Esempio n. 25
0
 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)
Esempio n. 26
0
 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)
Esempio n. 27
0
 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