3 # Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
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.
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.)
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.
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.
32 def __init__(self, buf = []):
36 def __init__(self, value = 0):
40 def __init__(self, value = 0):
44 def __init__(self, value = 0):
48 def __init__(self, value = 0):
58 self.__complete = None
60 def EIBSocketLocal(self, path):
62 self.errno = errno.EUSERS
64 fd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
71 def EIBSocketRemote(self, host, port = 6720):
73 self.errno = errno.EUSERS
75 fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
76 fd.connect((host, port))
82 def EIBSocketURL(self, url):
83 if url[0:6] == 'local:':
84 return self.EIBSocketLocal(url[6:])
89 return self.EIBSocketRemote(parts[1], int(parts[2]))
90 self.errno = errno.EINVAL
93 def EIBComplete(self):
94 if self.__complete == None:
95 self.errno = errno.EINVAL
97 return self.__complete()
101 self.errno = errno.EINVAL
106 def EIBClose_sync(self):
108 return self.EIBClose()
110 def __EIB_SendRequest(self, data):
112 self.errno = errno.ECONNRESET
114 if len(data) < 2 or len(data) > 0xffff:
115 self.errno = errno.EINVAL
117 data = [ (len(data)>>8)&0xff, (len(data))&0xff ] + data
124 def EIB_Poll_FD(self):
126 self.errno = errno.EINVAL
130 def EIB_Poll_Complete(self):
131 if self.__EIB_CheckRequest(False) == -1:
133 if self.readlen < 2 or (self.readlen >= 2 and self.readlen < self.datalen + 2):
137 def __EIB_GetRequest(self):
139 if self.__EIB_CheckRequest(True) == -1:
141 if self.readlen >= 2 and self.readlen >= self.datalen + 2:
145 def __EIB_CheckRequest(self, block):
147 self.errno = errno.ECONNRESET
149 if self.readlen == 0:
153 self.fd.setblocking(block)
154 result = self.fd.recv (2-self.readlen)
156 self.head.append(ord(a))
157 self.readlen += len(result)
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)
165 self.data.append(ord(a))
166 self.readlen += len(result)