def StartReceiver(self, name, datasize=None, affin=0x7fffffff, prio=0, sendFromHead=1, outputname=None, nstore=10, port=4262, readFrom=0, readTo=-1, readStep=1): """Starts a receiver locally. This then receives data from the RTC and writes it to a local shared memory circular buffer, which other local clients can then read. name here includes prefix, but this shouldn't be sent to receiver. """ if outputname == None: outputname = name if datasize == None: #work out the size of the data... data = self.GetStream(name)[0] datasize = (data.size * data.itemsize + 32) * nstore + buffer.getCircHeaderSize() plist = [ "receiver", "-p%d" % port, "-a%d" % affin, "-i%d" % prio, "-n%d" % datasize, "-o/%s" % outputname, name[len(self.prefix):], "-q" ] if self.prefix != "": plist.append("-s%s" % self.prefix) if os.path.exists("/dev/shm/%s" % outputname): raise Exception("local /dev/shm/%s already exists" % outputname) p = subprocess.Popen(plist) #Now wait for it to bind, and get the bound port cnt = 0 while cnt < 100 and not os.path.exists("/dev/shm/%s" % outputname): cnt += 1 time.sleep(0.05) if cnt == 100: p.terminate() p.wait() raise Exception("Local /dev/shm/%s not found" % outputname) cb = buffer.Circular("/%s" % (outputname)) cnt = 0 s = None while cnt < 100 and s == None: s = cb.getLatest() if s == None: time.sleep(0.01) cnt += 1 if s != None: port = int(s[0][0]) else: p.terminate() p.wait() raise Exception("Unable to determine port of receiver") print "Local receiver has started and is listening on port %d" % port return port
def openSHM(self): self.shmOpen = 0 while self.shmOpen == 0: try: self.circbuf = buffer.Circular("/" + self.prefix + self.shmname, raw=self.raw) self.shmOpen = 1 except: print "Failed to open /dev/shm/%s%s" % (self.prefix, self.shmname) self.circbuf = None time.sleep(1) print "/dev/shm/%s%s opened" % (self.prefix, self.shmname)
def callback(self, data): """data[0]=="data",data[1]==name,data[2]==data,timestamp,fno """ d = data[2][0] timestamp = data[2][1] frameno = data[2][2] setdec = 0 print "Got data for frame ", frameno #Put the data into the shm array. if self.cb == None: #open the buffer to write into print "nodestream creating circular buffer %s: shape %s, type %s" % ( self.testprefix + self.prefix + self.name, str( d.shape), d.dtype.char) self.cb = buffer.Circular("/" + self.testprefix + self.prefix + self.name, owner=1, dims=d.shape, dtype=d.dtype.char, nstore=self.nstore) #self.owner=0#so that won't be removed if this process quits. #check the shape of the buffer if self.cb.getShape() != d.shape or self.cb.dtype[0] != d.dtype.char: print "nodestream reshaping" self.cb.reshape(d.shape, d.dtype.char) #and write the data. if self.cb.circsignal[0] != 0: self.timeDataLastRequested = timestamp self.cb.circsignal[0] = 0 elif self.timeDataLastRequested > 0 and timestamp - self.timeDataLastRequested > 60: #data hasn't been requested for last minute self.timeDataLastRequested = timestamp #Turn off the decimation of sender. print "No requests for last minute - nodestream turning off." self.cb.freq[0] = 0 elif self.timeDataLastRequested == 0: self.timeDataLastRequested = timestamp self.setDecimate() self.cb.add(d, timestamp, frameno) return 0
def __init__(self, name, prefix="", myhostname=None, testprefix=""): """Initialise the object. name is the name of the stream, prefix is the darc prefix. myhostname can be an IP address (but will be found automagically on linux), and testprefix can be used if you want to add an additional prefix to the shm buffer.""" self.name = name self.decimate = 100 self.prefix = prefix self.testprefix = testprefix self.nstore = 100 self.myhostname = myhostname #self.lock=lock.lock() self.ctrl = controlCorba.controlClient(debug=0, controlName=prefix) while self.ctrl.obj == None: self.ctrl = controlCorba.controlClient(debug=0, controlName=prefix) if self.ctrl.obj == None: time.sleep(1) self.cb = None self.timeDataLastRequested = 0 if self.checkSHM(): #The assumption here is that this shm is old and not written by anyone else... self.cb = buffer.Circular("/" + self.testprefix + self.prefix + self.name) self.cb.lastWritten[0] = -1