Copyright update for 2011
[bcusdk.git] / eibd / client / python / io.inc
blob64a41efe7b19c8e70ae68224eea8411d89893890
1
2 #   EIBD client library
3 #   Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
4
5 #   This program is free software; you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation; either version 2 of the License, or
8 #   (at your option) any later version.
9
10 #   In addition to the permissions in the GNU General Public License, 
11 #   you may link the compiled version of this file into combinations
12 #   with other programs, and distribute those combinations without any 
13 #   restriction coming from the use of this file. (The General Public 
14 #   License restrictions do apply in other respects; for example, they 
15 #   cover modification of the file, and distribution when not linked into 
16 #   a combine executable.)
17
18 #   This program is distributed in the hope that it will be useful,
19 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
20 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 #   GNU General Public License for more details.
22
23 #   You should have received a copy of the GNU General Public License
24 #   along with this program; if not, write to the Free Software
25 #   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
28 import errno;
29 import socket;
31 class EIBBuffer:
32   def __init__(self, buf = []):
33     self.buffer = buf
35 class EIBAddr:
36   def __init__(self, value = 0):
37     self.data = value
39 class EIBInt8:
40   def __init__(self, value = 0):
41     self.data = value
43 class EIBInt16:
44   def __init__(self, value = 0):
45     self.data = value
47 class EIBInt32:
48   def __init__(self, value = 0):
49     self.data = value
51 class EIBConnection:
52   def __init__(self):
53     self.data = []
54     self.readlen = 0
55     self.datalen = 0
56     self.fd = None
57     self.errno = 0
58     self.__complete = None
60   def EIBSocketLocal(self, path):
61     if self.fd != None:
62       self.errno = errno.EUSERS
63       return -1
64     fd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
65     fd.connect(path)
66     self.data = []
67     self.readlen = 0
68     self.fd = fd
69     return 0
71   def EIBSocketRemote(self, host, port = 6720):
72     if self.fd != None:
73       self.errno = errno.EUSERS
74       return -1
75     fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
76     fd.connect((host, port))
77     self.data = []
78     self.readlen = 0
79     self.fd = fd
80     return 0
82   def EIBSocketURL(self, url):
83     if url[0:6] == 'local:':
84       return self.EIBSocketLocal(url[6:])
85     if url[0:3] == 'ip:':
86       parts=url.split(':')
87       if (len(parts) == 2):
88         parts.append(6720)
89       return self.EIBSocketRemote(parts[1], int(parts[2]))
90     self.errno = errno.EINVAL
91     return -1
93   def EIBComplete(self):
94     if self.__complete == None:
95       self.errno = errno.EINVAL
96       return -1
97     return self.__complete()
99   def EIBClose(self):
100     if self.fd == None:
101       self.errno = errno.EINVAL
102       return -1
103     self.fd.close()
104     self.fd = None
106   def EIBClose_sync(self):
107     self.EIBReset()
108     return self.EIBClose()
110   def __EIB_SendRequest(self, data):
111     if self.fd == None:
112       self.errno = errno.ECONNRESET
113       return -1
114     if len(data) < 2 or len(data) > 0xffff:
115       self.errno = errno.EINVAL
116       return -1
117     data = [ (len(data)>>8)&0xff, (len(data))&0xff ] + data
118     result = ''
119     for i in data:
120       result += chr(i)
121     self.fd.send(result)
122     return 0
124   def EIB_Poll_FD(self):
125     if self.fd == None:
126       self.errno = errno.EINVAL
127       return -1
128     return self.fd
130   def EIB_Poll_Complete(self):
131     if self.__EIB_CheckRequest(False) == -1:
132       return -1
133     if self.readlen < 2 or (self.readlen >= 2 and self.readlen < self.datalen + 2):
134       return 0
135     return 1
137   def __EIB_GetRequest(self):
138      while True:
139       if self.__EIB_CheckRequest(True) == -1:
140         return -1
141       if self.readlen >= 2 and self.readlen >= self.datalen + 2:
142         self.readlen = 0
143         return 0
145   def __EIB_CheckRequest(self, block):
146     if self.fd == None:
147       self.errno = errno.ECONNRESET
148       return -1
149     if self.readlen == 0:
150       self.head = []
151       self.data = []
152     if self.readlen < 2:
153       self.fd.setblocking(block)
154       result = self.fd.recv (2-self.readlen)
155       for a in result:
156         self.head.append(ord(a))
157       self.readlen += len(result)
158     if self.readlen < 2:
159       return 0
160     self.datalen = (self.head[0] << 8) | self.head[1]
161     if self.readlen < self.datalen + 2:
162       self.fd.setblocking(block)
163       result = self.fd.recv (self.datalen + 2 -self.readlen)
164       for a in result:
165         self.data.append(ord(a))
166       self.readlen += len(result)
167     return 0