def connect(self, dst): """Returns sk_chan, the Channel() of the new established socket""" if dst not in self.neighbours: raise ENotNeigh, ENotNeigh.errstr sk = randint(1, 2**32 - 1) self.sk_out[sk] = Channel() self.sk_in[sk] = Channel() dst.accept_chan.send((self, sk, self.sk_in[sk], self.sk_out[sk])) return sk
def __init__(self, ip=None, neighs={}): self.change_ip(ip) # {Node: Link} self.neighbours = {} self.recv_chan = Channel() self.accept_chan = Channel() self.sk_in = {} # {sk: sk_chan} self.sk_out = {} # {sk: sk_chan}
def connect(self, address): asyncore.dispatcher.connect(self, address) # UDP sockets do not connect. if self.socket.type != SOCK_DGRAM and not self.connected: if not self.connectChannel: # Prefer the sender. Do not block when sending, given that # there is a tasklet known to be waiting, this will happen. self.connectChannel = Channel(prefer_sender=True) self.connectChannel.recv()
def sendto(self, sendData, sendAddress): waitChannel = None for idx, (data, address, channel, sentBytes) in enumerate(self.sendToBuffers): if address == sendAddress: self.sendToBuffers[idx] = (data + sendData, address, channel, sentBytes) waitChannel = channel break if waitChannel is None: waitChannel = Channel(micro_send=True) self.sendToBuffers.append((sendData, sendAddress, waitChannel, 0)) return waitChannel.recv()
def __init__(self, sock): # This is worth doing. I was passing in an invalid socket which was # an instance of dispatcher and it was causing tasklet death. if not isinstance(sock, stdsocket.socket): raise StandardError("Invalid socket passed to dispatcher") asyncore.dispatcher.__init__(self, sock) # if self.socket.type == SOCK_DGRAM: # self.dgramRecvChannels = {} # self.dgramReadBuffers = {} #else: self.recvChannel = Channel(micro_send=True) self.readBufferString = '' self.readBufferList = [] self.sendBuffer = '' self.sendToBuffers = [] self.maxreceivebuf = 65536
micro(f, (8, 8)) mvoid() F1 = foo(1) F2 = foo(2) F1.void() F2.void() def xf(): print 'xf2', 1 c = Channel() csend(c, 1) micro(xf) crecv(c, 1) #print '---Atomic test---' def xf(): print 'xf4', 3 c = Channel(prefer_sender=False) crecv(c, 3) micro(xf) csend_atomic(c, 3)
def wait(self, t): """Waits the specified number of time units""" chan = Channel() self.ev_add(SimEvent(t, self._wake_up_wait, (chan, ))) chan.recv()
def __init__(self): self.curtime = 0 self.queue = [] self.looping = False self.simchan = Channel()
import sys sys.path.append('../../') import traceback import random from random import randint import pdb from ntk.sim.net import Net import ntk.sim.sim as sim import ntk.sim.wrap.xtime as xtime from ntk.lib.micro import micro, microfunc, allmicro_run, Channel ch = Channel() ch2 = Channel(True) ch3 = Channel() T = [] def myxtime(): t = xtime.time() T.append(t) return t @microfunc(True) def f(x): ch2.recv() print 'ch2'
def accept(self): if not self.acceptChannel: self.acceptChannel = Channel(micro_send=True) return self.acceptChannel.recv()