def advance(self): """Move to the next buffer swap""" if self.savedData == None: self.savedData = serialise.ReadMessage(self.fd) data = self.savedData self.savedData = None vdict = data[2] for k in vdict.keys(): self.buf.set(k, vdict[k][0], comment=vdict[k][1]) self.buf.set("switchTime", data[1]) self.buf.set("frameno", data[0]) return self.buf.copy()
def getState(self, fno=None, tstamp=None): """Get the state of the rtc at frame number fno, or at time tstamp""" if fno == None and tstamp == None: raise Exception( "getState must specify a frame or time at which to get the state" ) if fno != None: if self.lastfno > fno: #Have to reset and start from the start print "Rewinding" self.fd.close() self.fd = open(self.fname) elif self.lastfno == fno: return self.buf.copy() elif tstamp != None: if self.lasttstamp > tstamp: #rewind print "Rewinding" self.fd.close() self.fd = open(self.fname) elif self.lasttstamp == tstamp: return self.buf.copy() finished = 0 self.lasttstamp = tstamp self.lastfno = fno while finished == 0: if self.savedData != None: data = self.savedData self.savedData = None else: data = serialise.ReadMessage(self.fd) if data == None: print "Got to end of stored parameters" finished = 1 else: if (fno != None and data[0] > fno) or (tstamp != None and data[1] > tstamp): print "Got to position" finished = 1 self.savedData = data if self.lasttstamp == None: self.laststamp = data[1] if self.lastfno == None: self.lastfno = data[0] else: #put the data into the buffer vdict = data[2] for k in vdict.keys(): self.buf.set(k, vdict[k][0], comment=vdict[k][1]) self.buf.set("switchTime", data[1]) self.buf.set("frameno", data[0]) return self.buf.copy()
def recv(self): """Receive data from a socket. Expecting data=[info,tag,whatever] if info=="data" whatever will be a dictionary of returned data. if info=="warning", whatever will be a string warning message. tag is the tag sent, or None if not obtainable. @return: The data received from the socket @rtype: List """ data = serialise.ReadMessage(self.sock.fileno()) #print data return data
def readsock(self, sock): """Reads the socket, obtaining 'data'. data.action will be a control word, e.g. 'cmd' If data.action=='now': data.command will be command to be exec'd. data.ret (if present) will be list of things to return. If data.action=='rpt':#repeat command data.command will be command to be exec'd. data.ret (if present) will be list of things to return. If data.action=='cmd':#command to execute during break... data.command will be command to be exec'd. data.ret (if present) will be list of things to return. If data.action=='del':#delete command from regular/cmd list this will be deleted... @param sock: Socket to read @type sock: socket.socket instance """ try: tmp = serialise.ReadMessage(sock.fileno()) except: print "WARNING Connection reset by peer." return -1 if not tmp: return -1 #connection closed. #print "socketdata:",tmp data = ConnObj.ConnMsg(None, None) data.unpickle(tmp) #data=cPickle.loads(data[0])#unpickle a ConnMsg object. action = data.action tag = data.tag print "INFORMATION got data: %s %s %s %s" % (str( data.action), str(data.command), str(data.tag), str(data.ret)) #cmd=data.pop(0) if action == "now": if self.globals == None: self.cmdList.append([data, sock]) else: self.execCmd(data, sock) elif action == "cmd": self.cmdList.append([data, sock]) elif action[:3] == "rpt": freq = 1 if len(action) > 3: freq = int(action[3:]) if freq < 1: freq = 1 self.rptCmdList.append([data, sock, freq, 0]) elif action == "del": remlist = [] #print "Action del:",data,sock for cmd in self.rptCmdList: #print cmd try: if (data.command == "" or data.command == None or cmd[0].command == data.command ) and sock == cmd[1] and (tag == None or cmd[0].tag == tag): #if cmd[:2]==[data,sock]: remlist.append(cmd) except: print "ERROR deleting", data, sock, cmd print data.command for cmd in remlist: print "INFORMATION Deleting action", cmd self.rptCmdList.remove(cmd) while [data, sock] in self.cmdList: print "Deleting action:", [data, sock] self.cmdList.remove([data, sock]) elif action == "add": #prepend data.command to config.postList... print "INFORMATION SockConn - got data action add" if type(data.command) == type(()) and len( data.command) == 2 and type(data.command[0]) == type(""): if type(self.globals) != type(None): print "INFORMATION Adding to config.postList - %s" % data.command[ 0] self.globals["ctrl"].config.postAdd(data.command) print "Added post variable %s to config.postList" % data.command[ 0] else: print "ERROR Cannot add post variable %s to config, SockConn has no globals" % data.command[ 0] else: print "ERROR SockConn - action add not received with non-valid data, should be tuple of (str,data)." else: print action, data serialise.Send(["warning", tag, "data not understood"], sock)
def getNextChange(self): """Get timestamp and frame number and data of next switch""" if self.savedData == None: self.savedData = serialise.ReadMessage(self.fd) return self.savedData #fno,time,dictionary of {key:(value,comment)}
#along with this program. If not, see <http://www.gnu.org/licenses/>. import socket,serialise,os,numpy,time class SockConnTest: """Class for testing purposes""" def __init__(self,port,host=os.environ["HOSTNAME"]): self.port=port self.host=host self.sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((host,port)) def test(self): """Send test data""" a=numpy.array([[1,22,33],[4,55,66]],"i") b=numpy.array([[11,122,133],[14,155,166]],"i") b[1]=a[0] print "Contiguous?",b.iscontiguous(),b serialise.Send(["hello there",10,(1,2,3),{5:2,3:4},a,b],self.sock) print "sent..." if __name__=="__main__": """Testing purposes""" import sys if len(sys.argv)==1: s=SockConnTest(9000) s.test() else: s=SockConnTest(8000) s.test() data=serialise.ReadMessage(s.sock.fileno()) print data