Esempio n. 1
0
 def read(self, rlen=None):
     d = ''
     if self._outWaiting() > 0:
         d += DWIO.read(self, rlen)
         if d:
             if self.debug:
                 print "ch:i: read:", canonicalize(d)
     elif self.conn:
         d += self.conn.read(rlen)
         if d:
             if self.debug:
                 print "ch:c: read:", canonicalize(d)
     # print "d: (%s)" % d
     return d
Esempio n. 2
0
    def write(self, data, ifs=('\r', '\n')):
        if self.debug:
            print "ch: write:", canonicalize(data)
        wdata = ''
        w = 0
        pos = -1

        if not self.eatTwo and self.state in [
                DWV_S_ONLINE, DWV_S_INBOUND, DWV_S_TCPOUT
        ] and self.conn:
            if self.wbuf:
                w += self.conn.write(self.wbuf)
                self.wbuf = ''
            w += self.conn.write(data)
        else:
            if self.echo:
                self.rq.put(data)
                self.rb.add(len(data))
            self.wbuf += data
            for c in ifs:
                pos = self.wbuf.find(c)
                if pos >= 0:
                    break
            if pos < 0:
                w += len(data)
            # while pos >= 0:
            else:

                if self.eatTwo:
                    if self.debug:
                        print "ch: eating: %s" % canonicalize(
                            self.wbuf[:pos + 1])
                if self.echo:
                    self.rq.put("\r")
                    self.rb.add(1)
                wdata = self.wbuf[:pos]
                self.wbuf = self.wbuf[pos + 1:]
                w += pos + 1
                if self.debug:
                    print "wdata=(%s) wbuf=(%s)" % (wdata, self.wbuf)
                if self.eatTwo:
                    self.eatTwo = False

                else:
                    wdata = wdata.lstrip().rstrip()
                    if wdata:
                        self.cq.put(wdata)
        return w
Esempio n. 3
0
    def writeX(self, data, ifs=('\r', '\n')):
        if self.debug:
            print "ch: write:", canonicalize(data)
        wdata = ''
        w = 0
        pos = -1

        if self.conn:
            if self.wbuf:
                w += self.conn.write(self.wbuf)
                self.wbuf = ''
            w += self.conn.write(data)
        else:
            if self.echo:
                self.rq.put(data)
                self.rb.add(len(data))
                self.rq.put("\r")
                self.rb.add(1)
            if self.wbuf:
                wbl = len(self.wbuf)
                self.cq.put(self.wbuf)
                self.cq.add(wbl)
                w += wbl
                self.wbuf = ''
            wdata = data.lstrip().rstrip()
            if wdata:
                wdl = len(wdata)
                self.cq.put(wdata)
                self.cq.add(wdl)
                w += wdl
        return w
Esempio n. 4
0
 def _write(self, data):
     if self.abort:
         return -1
     # time.sleep(0.010)
     if self.debug and data:
         print "serwrite: len=%d %s" % (len(data), dwlib.canonicalize(data))
     return self.ser.write(data)
Esempio n. 5
0
	def _write(self, data):
		if self.abort:
			return -1
                #time.sleep(0.010)
		if self.debug and data:
			print "serwrite: len=%d %s"%(len(data),dwlib.canonicalize(data))
		return self.ser.write(data)
Esempio n. 6
0
	def read(self, rlen=None):
		d = ''
		if self._outWaiting()>0:
			d +=  DWIO.read(self, rlen)
			#d += self.rq.get()
			#self.rb.sub(len(d))
			if d:
				if self.debug:
					print "ch:i: read:",canonicalize(d)
		#elif self.connected == False:
		#	self.rb.close()
		#elif self.conn and self.conn.outWaiting()>0:
		elif self.conn:
			d += self.conn.read(rlen)
			if d:
				if self.debug:
					print "ch:c: read:",canonicalize(d)
		#print "d: (%s)" % d
		return d
Esempio n. 7
0
def dwPrint(s, data, debug=False):
   for c in data:
      dd = OP_PRINT
      dd += c
      s.send(dd)
   s.send(OP_PRINTFLUSH)

   if debug:
      print("printed data\n")
      print(dwlib.canonicalize(data))
Esempio n. 8
0
	def _write(self, data):
		if not self.isConnected():
			return 0
		if self.debug and data != '':
			print "tel write:" , canonicalize(data)
		try:
			self.conn.write(data)
		except Exception as ex:
			print str(ex)
			print "ERROR: Connection Closed"
			self._close()
		return len(data)
Esempio n. 9
0
 def _write(self, data):
     if not self.isConnected():
         return 0
     if self.debug and data != '':
         print "tel write:", canonicalize(data)
     try:
         self.conn.write(data)
     except Exception as ex:
         print str(ex)
         print "ERROR: Connection Closed"
         self._close()
     return len(data)
Esempio n. 10
0
    def _read(self, count=None):
        if self.abort:
            return ''
        data = ''
        # print "dwserial: read"
        if count:
            data = self.ser.read(count)
        else:
            data = self.ser.read()

        if self.debug and data:
            print "serread: len=%d %s" % (len(data), dwlib.canonicalize(data))
        return data
Esempio n. 11
0
	def _read(self, count=None):
		if self.abort:
			return ''
		data = ''
		#print "dwserial: read"
		if count:
			data = self.ser.read(count)
		else:
			data = self.ser.read()

		if self.debug and data:
			print "serread: len=%d %s"%(len(data),dwlib.canonicalize(data))
		return data
Esempio n. 12
0
	def _read(self, count=256):
		data = ''
		if not self.isConnected():
			return data
		try:
			#data = self.conn.read_very_eager()
			data = self.conn.read_some()
			if data == '':
				raise Exception("EOF")
		except Exception as ex:
			print str(ex)
			print "ERROR: Connection Closed"
			self._close()
		if self.debug and data != '':
			print "tel read:" , canonicalize(data)
		return data
Esempio n. 13
0
 def _read(self, count=256):
     data = ''
     if not self.isConnected():
         return data
     try:
         # data = self.conn.read_very_eager()
         data = self.conn.read_some()
         if data == '':
             raise Exception("EOF")
     except Exception as ex:
         print str(ex)
         print "ERROR: Connection Closed"
         self._close()
     if self.debug and data != '':
         print "tel read:", canonicalize(data)
     return data
Esempio n. 14
0
	def _write(self, data):
		if self.abort or not self.conn:
			return -1
		n = 0
		wi = []
		try:
			(_, wi, _) = select.select([], [self.conn.fileno()], [], 1)
		except Exception as e:
			print str(e)
			raise ("Connection closed")
			self._close()
			n = -1
		if any(wi):
			try:
				n = self.conn.send(data)
				if self.debug:
					print "socket write:",self,canonicalize(data)
			except Exception as e:
				print str(e)
				self._close()
				n = -1

		return n
Esempio n. 15
0
    def _write(self, data):
        if self.abort or not self.conn:
            return -1
        n = 0
        wi = []
        try:
            (_, wi, _) = select.select([], [self.conn.fileno()], [], 1)
        except Exception as e:
            print(str(e))
            raise ("Connection closed")
            self._close()
            n = -1
        if any(wi):
            try:
                n = self.conn.send(data)
                if self.debug:
                    print "socket write:", self, canonicalize(data)
            except Exception as e:
                print(str(e))
                self._close()
                n = -1

        return n
Esempio n. 16
0
 def _read(self, count=256):
     data = None
     if self.abort or not self.conn:
         return ''
     ri = []
     try:
         (ri, _, _) = select.select([self.conn.fileno()], [], [], 1)
     except Exception as e:
         print(str(e))
         raise Exception("Connection closed")
         self._close()
     if any(ri):
         # print "dwsocket: reading"
         data = self.conn.recv(count)
     # else:
     # print "dwsocket: waiting"
     if data == '':
         raise Exception("Connection closed")
         self._close()
     # if data:
     # 	print "r",data
     if self.debug and data is not None:
         self._print("%s: socket read: %s" % (self, canonicalize(data)))
     return data
Esempio n. 17
0
	def _read(self, count=256):
		data = None
		if self.abort or not self.conn:
			return ''
		ri = []
		try:
			(ri, _, _) = select.select([self.conn.fileno()], [], [], 1)
		except Exception as e:
			print str(e)
			raise Exception("Connection closed")
			self._close()
		if any(ri):
			#print "dwsocket: reading"
			data = self.conn.recv(count)
		#else:
			#print "dwsocket: waiting"
		if data == '':
			raise Exception("Connection closed")
			self._close()
		#if data:
		#	print "r",data
		if self.debug and data != None:
			print "socket read:",self,canonicalize(data)
		return data
Esempio n. 18
0
	def write(self, data, ifs=('\r','\n')):
		if self.debug:
			print "ch: write:",canonicalize(data)
		wdata = ''
		w=0
		pos=-1
		#print "dwio read %d" % rlen
		#if not self.wt.is_alive():
		#	# Start the background reader thread only
		#	# when someone asks to start reading from it
		#	self.wt.start()
		if not self.eatTwo and self.online and self.conn:
			if self.wbuf:
				w += self.conn.write(self.wbuf)
				self.wbuf = ''
			w +=  self.conn.write(data)
		else:
			if self.echo:
				self.rq.put(data)
				self.rb.add(len(data))
			self.wbuf += data
			for c in ifs:
				pos = self.wbuf.find(c)
				if pos >= 0:
					break
			if pos < 0:
				w += len(data)
			#while pos >= 0:
			else:
				
				if self.eatTwo:
					if self.debug:
						print "ch: eating: %s" % canonicalize(self.wbuf[:pos+1])
				if self.echo:
					self.rq.put("\r")
					self.rb.add(1)
				wdata = self.wbuf[:pos]
				self.wbuf = self.wbuf[pos+1:]
				w += pos + 1	
				if self.debug:
					print "wdata=(%s) wbuf=(%s)" % (wdata, self.wbuf)
				if self.eatTwo:
					self.eatTwo=False
					
				else:
					wdata = wdata.lstrip().rstrip()
					if wdata:
						self.cq.put(wdata)
				#self._cmdWorker()
				#print "parser",wdata
				#res = self.parser.parse(wdata)
				#if isinstance(res, DWIO):
				#	self.conn = res
				#elif res:
				#	res = "0 OK\r"+res#+'\r\n'
				#	print "res",res
				#	self.rq.put(res)
				#	self.rb.add(len(res))
				#	self.connected = False
				#pos = self.wbuf.find(ifs)

			#pos = data.find(ifs)
			#if pos >= 0:
			#	wdata = self.wbuf + data[:pos]
			#	self.wbuf = data[pos+1:]
			#	w = pos + 1
			#if wdata:
			#	res = self.parser.parse(wdata)
			#	if isinstance(res, DWIO):
			#		self.conn = res
			#	elif res:
			#		self.rq.put(res)
			#		self.rb.add(len(res))
			#else:
			#	self.wbuf += data
			#	w = len(data)
		return w