New release (and URL)
[pytipc.git] / tipc.py
blobb07c13dcbfdb6b88be7121f07f7882670657871c
2 """
3 TIPC python wrapper
4 Alberto Bertogli (albertito@gmail.com)
7 Addresses are expressed as (addr_type, v1, v2, v3 [, scope]);
8 where addr_type can be one of:
9 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
10 and scope can be one of:
11 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
14 The meaning of v1, v2 and v3 depend on the value of addr_type:
16 if addr_type is TIPC_ADDR_NAME:
17 v1 is the server type
18 v2 is the port identifier
19 v3 is ignored
20 if addr_type is TIPC_ADDR_NAMESEQ:
21 v1 is the server type
22 v2 is the lower port number
23 v3 is the upper port number
24 if addr_type is TIPC_ADDR_ID:
25 v1 is the node
26 v2 is the ref
27 v3 is ignored
29 Even when ignored, v3 must be present and be an integer.
30 """
32 import os
33 import socket as socketmodule
34 import tipc_ll
36 # Exported constants
37 from tipc_ll import AF_TIPC, SOL_TIPC, \
38 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, TIPC_ADDR_ID, \
39 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, TIPC_NODE_SCOPE, \
40 TIPC_IMPORTANCE, \
41 TIPC_SRC_DROPPABLE, TIPC_DEST_DROPPABLE, \
42 TIPC_CONN_TIMEOUT, \
43 TIPC_LOW_IMPORTANCE, TIPC_MEDIUM_IMPORTANCE, \
44 TIPC_HIGH_IMPORTANCE, TIPC_CRITICAL_IMPORTANCE, \
45 TIPC_SUB_PORTS, TIPC_SUB_SERVICE, TIPC_SUB_CANCEL, \
46 TIPC_WAIT_FOREVER, \
47 TIPC_PUBLISHED, TIPC_WITHDRAWN, TIPC_SUBSCR_TIMEOUT, \
48 TIPC_CFG_SRV, TIPC_TOP_SRV
51 class TIPCSocket:
52 def __init__(self, stype, fd = None):
53 if not fd:
54 self.fd = tipc_ll.socket(stype)
55 else:
56 self.fd = fd
58 self.closed = 0
59 self.psock = socketmodule.fromfd(self.fd, AF_TIPC, stype)
61 self.family = AF_TIPC
62 self.type = stype
63 self.proto = 0
65 def __del__(self):
66 self.close()
69 # Friendly functions for the ones implemented in tipc_ll
71 def connect(self, addr):
72 "Connects to the given server."
73 return tipc_ll.connect(self.fd, addr)
75 def bind(self, addr):
76 "Binds to the given address."
77 return tipc_ll.bind(self.fd, addr)
79 def accept(self):
80 "Accepts a new connection, returns (conn, addr)."
81 newfd, addr = tipc_ll.accept(self.fd)
82 return (TIPCSocket(self.type, fd = newfd), addr)
84 def sendto(self, buf, addr):
85 "Sends a message/buffer to the given address."
86 return tipc_ll.sendto(self.fd, buf, addr)
88 def recvfrom(self, maxlen = 64 * 1024):
89 "Receives a message/buffer, returns (buffer, address)."
90 return tipc_ll.recvfrom(self.fd, maxlen)
92 def close(self):
93 "Closes the socket."
94 if not self.closed:
95 self.closed = 1
96 return os.close(self.fd)
98 def getsockname(self):
99 return tipc_ll.getsockname(self.fd)
102 # Other useful functions, some of them based on socketmodule
104 def fileno(self):
105 "Returns the file descriptor number."
106 return self.fd
108 def send(self, buf):
109 "Sends a message/buffer."
110 return self.psock.send(buf)
112 def recv(self, bufsize):
113 "Receives a message/buffer."
114 b, a = self.recvfrom(bufsize)
115 return b
117 def listen(self, backlog):
118 "Listen for connections made to the socket."
119 return self.psock.listen(backlog)
121 def setblocking(self, flag):
122 "Set blocking or non-blocking mode for the socket."
123 return self.psock.setblocking(flag)
125 def getsockopt(self, level, optname, buflen = None):
126 """Return the value of the given socket option (see the Unix
127 man page getsockopt(2))."""
128 if not buflen:
129 return self.psock.getsockopt(level, optname)
130 return self.psock.getsockopt(level, optname, buflen)
132 def setsockopt(self, level, optname, value):
133 """Set the value of the given socket option (see the Unix
134 manual page setsockopt(2))"""
135 return self.psock.setsockopt(level, optname, value)
137 def shutdown(self, how):
138 "Shut down one or both halves of the connection."
139 return self.psock.shutdown(how)
142 # Subscription handling
144 def send_subscr(self, addr, timeout = None, filter = TIPC_SUB_SERVICE):
146 Sends a subscription message to the server (the socket must
147 be connected to TIPC_ADDR_NAME, TIPC_TOP_SRV, TIPC_TOP_SRV, 0)
148 for this to work).
149 - addr: specifies the address to wait for (can be a name or a
150 nameseq).
151 - timeout: timeout in milliseconds (use None to wait
152 forever), defaults to None.
153 - filter: Either TIPC_SUB_SERVICE or TIPC_SUB_PORTS, see TIPC
154 documentation for details. Defaults to TIPC_SUB_SERVICE.
157 if not timeout:
158 timeout = TIPC_WAIT_FOREVER
160 atype, stype, lower, upper = addr[:4]
162 if atype == TIPC_ADDR_NAME:
163 upper = lower
165 subs = tipc_ll.build_subscr(stype, lower, upper,
166 timeout, filter)
167 self.send(subs)
169 def recv_subscr_event(self):
171 Receives a subscription event from the socket.
173 It returns None if there was an error receiving the
174 subscription, or the tuple:
175 ( event, found_lower, found_upper,
176 (srv_type, lower, upper, timeout, filter) )
178 - event: one of TIPC_SUBSCR_TIMEOUT, TIPC_PUBLISHED or
179 TIPC_WITHDRAWN.
180 - found_lower, found_upper: the lower and upper ports found.
181 - (srv_type, lower, upper, timeout, filter): information on
182 the subscription that generated the event (see send_subscr()
183 for details).
185 buf = self.recv(4092)
186 return tipc_ll.parse_event(buf)
189 def socket(stype):
191 Creates and returns a socket object of the given type.
193 The type can be one of SOCK_RDM, SOCK_SEQPACKET, SOCK_STREAM and
194 SOCK_DGRAM, see TIPC documentation for more information.
196 return TIPCSocket(stype)
199 def wait_for(addr, timeout = None, filter = None):
201 Waits for a server to appear on the given address.
203 If a timeout is specified and no server came up after that number of
204 milliseconds, returns None.
205 The optional filter parameter can be either TIPC_SUB_SERVICE or
206 TIPC_SUB_PORTS, see TIPC documentation for more details; it defaults
207 to TIPC_SUB_SERVICE.
209 If there are any errors, return None; otherwise return an event tuple
210 composed of:
211 ( event, found_lower, found_upper,
212 (srv_type, lower, upper, timeout, filter) )
214 - event: one of TIPC_SUBSCR_TIMEOUT, TIPC_PUBLISHED or
215 TIPC_WITHDRAWN.
216 - found_lower, found_upper: the lower and upper ports found.
217 - (srv_type, lower, upper, timeout, filter): information on
218 the subscription that generated the event (see send_subscr()
219 for details).
222 fd = socket(socketmodule.SOCK_SEQPACKET)
224 if not timeout:
225 timeout = TIPC_WAIT_FOREVER
226 if not filter:
227 filter = TIPC_SUB_SERVICE
229 topsrv_addr = (TIPC_ADDR_NAME, TIPC_TOP_SRV,
230 TIPC_TOP_SRV, TIPC_TOP_SRV)
231 fd.connect(topsrv_addr)
233 fd.send_subscr(addr, timeout, filter)
234 return fd.recv_subscr_event()