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:
18 v2 is the port identifier
20 if addr_type is TIPC_ADDR_NAMESEQ:
22 v2 is the lower port number
23 v3 is the upper port number
24 if addr_type is TIPC_ADDR_ID:
29 Even when ignored, v3 must be present and be an integer.
33 import socket
as socketmodule
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
, \
41 TIPC_SRC_DROPPABLE
, TIPC_DEST_DROPPABLE
, \
43 TIPC_LOW_IMPORTANCE
, TIPC_MEDIUM_IMPORTANCE
, \
44 TIPC_HIGH_IMPORTANCE
, TIPC_CRITICAL_IMPORTANCE
, \
45 TIPC_SUB_PORTS
, TIPC_SUB_SERVICE
, TIPC_SUB_CANCEL
, \
47 TIPC_PUBLISHED
, TIPC_WITHDRAWN
, TIPC_SUBSCR_TIMEOUT
, \
48 TIPC_CFG_SRV
, TIPC_TOP_SRV
52 def __init__(self
, stype
, fd
= None):
54 self
.fd
= tipc_ll
.socket(stype
)
59 self
.psock
= socketmodule
.fromfd(self
.fd
, AF_TIPC
, stype
)
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
)
76 "Binds to the given address."
77 return tipc_ll
.bind(self
.fd
, addr
)
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
)
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
105 "Returns the file descriptor number."
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
)
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))."""
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)
149 - addr: specifies the address to wait for (can be a name or a
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.
158 timeout
= TIPC_WAIT_FOREVER
160 atype
, stype
, lower
, upper
= addr
[:4]
162 if atype
== TIPC_ADDR_NAME
:
165 subs
= tipc_ll
.build_subscr(stype
, lower
, upper
,
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
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()
185 buf
= self
.recv(4092)
186 return tipc_ll
.parse_event(buf
)
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
209 If there are any errors, return None; otherwise return an event tuple
211 ( event, found_lower, found_upper,
212 (srv_type, lower, upper, timeout, filter) )
214 - event: one of TIPC_SUBSCR_TIMEOUT, TIPC_PUBLISHED or
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()
222 fd
= socket(socketmodule
.SOCK_SEQPACKET
)
225 timeout
= TIPC_WAIT_FOREVER
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()