Sampler in ChucK
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2627 lines
78KB

  1. #!/usr/bin/python
  2. """
  3. This module contains an OpenSoundControl implementation (in Pure Python), based (somewhat) on the
  4. good old 'SimpleOSC' implementation by Daniel Holth & Clinton McChesney.
  5. This implementation is intended to still be 'Simple' to the user, but much more complete
  6. (with OSCServer & OSCClient classes) and much more powerful
  7. (the OSCMultiClient supports subscriptions & message-filtering,
  8. OSCMessage & OSCBundle are now proper container-types)
  9. ================
  10. OpenSoundControl
  11. ================
  12. OpenSoundControl is a network-protocol for sending (small) packets of addressed data over network sockets.
  13. This OSC-implementation uses the UDP/IP protocol for sending and receiving packets.
  14. (Although it is theoretically possible to send OSC-packets over TCP, almost all known implementations use UDP)
  15. OSC-packets come in two kinds:
  16. - OSC-messages consist of an 'address'-string (not to be confused with a (host:port) network-address!),
  17. followed by a string of 'typetags' associated with the message's arguments (ie. 'payload'),
  18. and finally the arguments themselves, encoded in an OSC-specific way.
  19. The OSCMessage class makes it easy to create & manipulate OSC-messages of this kind in a 'pythonesque' way
  20. (that is, OSCMessage-objects behave a lot like lists)
  21. - OSC-bundles are a special type of OSC-message containing only OSC-messages as 'payload'. Recursively.
  22. (meaning; an OSC-bundle could contain other OSC-bundles, containing OSC-bundles etc.)
  23. OSC-bundles start with the special keyword '#bundle' and do not have an OSC-address. (but the OSC-messages
  24. a bundle contains will have OSC-addresses!)
  25. Also, an OSC-bundle can have a timetag, essentially telling the receiving Server to 'hold' the bundle until
  26. the specified time.
  27. The OSCBundle class allows easy cration & manipulation of OSC-bundles.
  28. see also http://opensoundcontrol.org/spec-1_0
  29. ---------
  30. To send OSC-messages, you need an OSCClient, and to receive OSC-messages you need an OSCServer.
  31. The OSCClient uses an 'AF_INET / SOCK_DGRAM' type socket (see the 'socket' module) to send
  32. binary representations of OSC-messages to a remote host:port address.
  33. The OSCServer listens on an 'AF_INET / SOCK_DGRAM' type socket bound to a local port, and handles
  34. incoming requests. Either one-after-the-other (OSCServer) or in a multi-threaded / multi-process fashion
  35. (ThreadingOSCServer / ForkingOSCServer). If the Server has a callback-function (a.k.a. handler) registered
  36. to 'deal with' (i.e. handle) the received message's OSC-address, that function is called, passing it the (decoded) message
  37. The different OSCServers implemented here all support the (recursive) un-bundling of OSC-bundles,
  38. and OSC-bundle timetags.
  39. In fact, this implementation supports:
  40. - OSC-messages with 'i' (int32), 'f' (float32), 's' (string) and 'b' (blob / binary data) types
  41. - OSC-bundles, including timetag-support
  42. - OSC-address patterns including '*', '?', '{,}' and '[]' wildcards.
  43. (please *do* read the OSC-spec! http://opensoundcontrol.org/spec-1_0 it explains what these things mean.)
  44. In addition, the OSCMultiClient supports:
  45. - Sending a specific OSC-message to multiple remote servers
  46. - Remote server subscription / unsubscription (through OSC-messages, of course)
  47. - Message-address filtering.
  48. ---------
  49. Stock, V2_Lab, Rotterdam, 2008
  50. ----------
  51. Changelog:
  52. ----------
  53. v0.3.0 - 27 Dec. 2007
  54. Started out to extend the 'SimpleOSC' implementation (v0.2.3) by Daniel Holth & Clinton McChesney.
  55. Rewrote OSCMessage
  56. Added OSCBundle
  57. v0.3.1 - 3 Jan. 2008
  58. Added OSClient
  59. Added OSCRequestHandler, loosely based on the original CallbackManager
  60. Added OSCServer
  61. Removed original CallbackManager
  62. Adapted testing-script (the 'if __name__ == "__main__":' block at the end) to use new Server & Client
  63. v0.3.2 - 5 Jan. 2008
  64. Added 'container-type emulation' methods (getitem(), setitem(), __iter__() & friends) to OSCMessage
  65. Added ThreadingOSCServer & ForkingOSCServer
  66. - 6 Jan. 2008
  67. Added OSCMultiClient
  68. Added command-line options to testing-script (try 'python OSC.py --help')
  69. v0.3.3 - 9 Jan. 2008
  70. Added OSC-timetag support to OSCBundle & OSCRequestHandler
  71. Added ThreadingOSCRequestHandler
  72. v0.3.4 - 13 Jan. 2008
  73. Added message-filtering to OSCMultiClient
  74. Added subscription-handler to OSCServer
  75. Added support fon numpy/scipy int & float types. (these get converted to 'standard' 32-bit OSC ints / floats!)
  76. Cleaned-up and added more Docstrings
  77. v0.3.5 - 14 aug. 2008
  78. Added OSCServer.reportErr(...) method
  79. -----------------
  80. Original Comments
  81. -----------------
  82. > Open SoundControl for Python
  83. > Copyright (C) 2002 Daniel Holth, Clinton McChesney
  84. >
  85. > This library is free software; you can redistribute it and/or modify it under
  86. > the terms of the GNU Lesser General Public License as published by the Free
  87. > Software Foundation; either version 2.1 of the License, or (at your option) any
  88. > later version.
  89. >
  90. > This library is distributed in the hope that it will be useful, but WITHOUT ANY
  91. > WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  92. > PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  93. > details.
  94. > You should have received a copy of the GNU Lesser General Public License along
  95. > with this library; if not, write to the Free Software Foundation, Inc., 59
  96. > Temple Place, Suite 330, Boston, MA 02111-1307 USA
  97. > For questions regarding this module contact Daniel Holth <dholth@stetson.edu>
  98. > or visit http://www.stetson.edu/~ProctoLogic/
  99. > Changelog:
  100. > 15 Nov. 2001:
  101. > Removed dependency on Python 2.0 features.
  102. > - dwh
  103. > 13 Feb. 2002:
  104. > Added a generic callback handler.
  105. > - dwh
  106. """
  107. import math, re, socket, select, string, struct, sys, threading, time, types
  108. from SocketServer import UDPServer, DatagramRequestHandler, ForkingMixIn, ThreadingMixIn
  109. global version
  110. version = ("0.3","5b", "$Rev: 5294 $"[6:-2])
  111. global FloatTypes
  112. FloatTypes = [types.FloatType]
  113. global IntTypes
  114. IntTypes = [types.IntType]
  115. ##
  116. # numpy/scipy support:
  117. ##
  118. try:
  119. from numpy import typeDict
  120. for ftype in ['float32', 'float64', 'float128']:
  121. try:
  122. FloatTypes.append(typeDict[ftype])
  123. except KeyError:
  124. pass
  125. for itype in ['int8', 'int16', 'int32', 'int64']:
  126. try:
  127. IntTypes.append(typeDict[itype])
  128. IntTypes.append(typeDict['u' + itype])
  129. except KeyError:
  130. pass
  131. # thanks for those...
  132. del typeDict, ftype, itype
  133. except ImportError:
  134. pass
  135. ######
  136. #
  137. # OSCMessage classes
  138. #
  139. ######
  140. class OSCMessage(object):
  141. """ Builds typetagged OSC messages.
  142. OSCMessage objects are container objects for building OSC-messages.
  143. On the 'front' end, they behave much like list-objects, and on the 'back' end
  144. they generate a binary representation of the message, which can be sent over a network socket.
  145. OSC-messages consist of an 'address'-string (not to be confused with a (host, port) IP-address!),
  146. followed by a string of 'typetags' associated with the message's arguments (ie. 'payload'),
  147. and finally the arguments themselves, encoded in an OSC-specific way.
  148. On the Python end, OSCMessage are lists of arguments, prepended by the message's address.
  149. The message contents can be manipulated much like a list:
  150. >>> msg = OSCMessage("/my/osc/address")
  151. >>> msg.append('something')
  152. >>> msg.insert(0, 'something else')
  153. >>> msg[1] = 'entirely'
  154. >>> msg.extend([1,2,3.])
  155. >>> msg += [4, 5, 6.]
  156. >>> del msg[3:6]
  157. >>> msg.pop(-2)
  158. 5
  159. >>> print msg
  160. /my/osc/address ['something else', 'entirely', 1, 6.0]
  161. OSCMessages can be concatenated with the + operator. In this case, the resulting OSCMessage
  162. inherits its address from the left-hand operand. The right-hand operand's address is ignored.
  163. To construct an 'OSC-bundle' from multiple OSCMessage, see OSCBundle!
  164. Additional methods exist for retreiving typetags or manipulating items as (typetag, value) tuples.
  165. """
  166. def __init__(self, address=""):
  167. """Instantiate a new OSCMessage.
  168. The OSC-address can be specified with the 'address' argument
  169. """
  170. self.clear(address)
  171. def setAddress(self, address):
  172. """Set or change the OSC-address
  173. """
  174. self.address = address
  175. def clear(self, address=""):
  176. """Clear (or set a new) OSC-address and clear any arguments appended so far
  177. """
  178. self.address = address
  179. self.clearData()
  180. def clearData(self):
  181. """Clear any arguments appended so far
  182. """
  183. self.typetags = ","
  184. self.message = ""
  185. def append(self, argument, typehint=None):
  186. """Appends data to the message, updating the typetags based on
  187. the argument's type. If the argument is a blob (counted
  188. string) pass in 'b' as typehint.
  189. 'argument' may also be a list or tuple, in which case its elements
  190. will get appended one-by-one, all using the provided typehint
  191. """
  192. if type(argument) == types.DictType:
  193. argument = argument.items()
  194. elif isinstance(argument, OSCMessage):
  195. raise TypeError("Can only append 'OSCMessage' to 'OSCBundle'")
  196. if hasattr(argument, '__iter__'):
  197. for arg in argument:
  198. self.append(arg, typehint)
  199. return
  200. if typehint == 'b':
  201. binary = OSCBlob(argument)
  202. tag = 'b'
  203. elif typehint == 't':
  204. binary = OSCTimeTag(argument)
  205. tag = 't'
  206. else:
  207. tag, binary = OSCArgument(argument, typehint)
  208. self.typetags += tag
  209. self.message += binary
  210. def getBinary(self):
  211. """Returns the binary representation of the message
  212. """
  213. binary = OSCString(self.address)
  214. binary += OSCString(self.typetags)
  215. binary += self.message
  216. return binary
  217. def __repr__(self):
  218. """Returns a string containing the decode Message
  219. """
  220. return str(decodeOSC(self.getBinary()))
  221. def __str__(self):
  222. """Returns the Message's address and contents as a string.
  223. """
  224. return "%s %s" % (self.address, str(self.values()))
  225. def __len__(self):
  226. """Returns the number of arguments appended so far
  227. """
  228. return (len(self.typetags) - 1)
  229. def __eq__(self, other):
  230. """Return True if two OSCMessages have the same address & content
  231. """
  232. if not isinstance(other, self.__class__):
  233. return False
  234. return (self.address == other.address) and (self.typetags == other.typetags) and (self.message == other.message)
  235. def __ne__(self, other):
  236. """Return (not self.__eq__(other))
  237. """
  238. return not self.__eq__(other)
  239. def __add__(self, values):
  240. """Returns a copy of self, with the contents of 'values' appended
  241. (see the 'extend()' method, below)
  242. """
  243. msg = self.copy()
  244. msg.extend(values)
  245. return msg
  246. def __iadd__(self, values):
  247. """Appends the contents of 'values'
  248. (equivalent to 'extend()', below)
  249. Returns self
  250. """
  251. self.extend(values)
  252. return self
  253. def __radd__(self, values):
  254. """Appends the contents of this OSCMessage to 'values'
  255. Returns the extended 'values' (list or tuple)
  256. """
  257. out = list(values)
  258. out.extend(self.values())
  259. if type(values) == types.TupleType:
  260. return tuple(out)
  261. return out
  262. def _reencode(self, items):
  263. """Erase & rebuild the OSCMessage contents from the given
  264. list of (typehint, value) tuples"""
  265. self.clearData()
  266. for item in items:
  267. self.append(item[1], item[0])
  268. def values(self):
  269. """Returns a list of the arguments appended so far
  270. """
  271. return decodeOSC(self.getBinary())[2:]
  272. def tags(self):
  273. """Returns a list of typetags of the appended arguments
  274. """
  275. return list(self.typetags.lstrip(','))
  276. def items(self):
  277. """Returns a list of (typetag, value) tuples for
  278. the arguments appended so far
  279. """
  280. out = []
  281. values = self.values()
  282. typetags = self.tags()
  283. for i in range(len(values)):
  284. out.append((typetags[i], values[i]))
  285. return out
  286. def __contains__(self, val):
  287. """Test if the given value appears in the OSCMessage's arguments
  288. """
  289. return (val in self.values())
  290. def __getitem__(self, i):
  291. """Returns the indicated argument (or slice)
  292. """
  293. return self.values()[i]
  294. def __delitem__(self, i):
  295. """Removes the indicated argument (or slice)
  296. """
  297. items = self.items()
  298. del items[i]
  299. self._reencode(items)
  300. def _buildItemList(self, values, typehint=None):
  301. if isinstance(values, OSCMessage):
  302. items = values.items()
  303. elif type(values) == types.ListType:
  304. items = []
  305. for val in values:
  306. if type(val) == types.TupleType:
  307. items.append(val[:2])
  308. else:
  309. items.append((typehint, val))
  310. elif type(values) == types.TupleType:
  311. items = [values[:2]]
  312. else:
  313. items = [(typehint, values)]
  314. return items
  315. def __setitem__(self, i, val):
  316. """Set indicatated argument (or slice) to a new value.
  317. 'val' can be a single int/float/string, or a (typehint, value) tuple.
  318. Or, if 'i' is a slice, a list of these or another OSCMessage.
  319. """
  320. items = self.items()
  321. new_items = self._buildItemList(val)
  322. if type(i) != types.SliceType:
  323. if len(new_items) != 1:
  324. raise TypeError("single-item assignment expects a single value or a (typetag, value) tuple")
  325. new_items = new_items[0]
  326. # finally...
  327. items[i] = new_items
  328. self._reencode(items)
  329. def setItem(self, i, val, typehint=None):
  330. """Set indicated argument to a new value (with typehint)
  331. """
  332. items = self.items()
  333. items[i] = (typehint, val)
  334. self._reencode(items)
  335. def copy(self):
  336. """Returns a deep copy of this OSCMessage
  337. """
  338. msg = self.__class__(self.address)
  339. msg.typetags = self.typetags
  340. msg.message = self.message
  341. return msg
  342. def count(self, val):
  343. """Returns the number of times the given value occurs in the OSCMessage's arguments
  344. """
  345. return self.values().count(val)
  346. def index(self, val):
  347. """Returns the index of the first occurence of the given value in the OSCMessage's arguments.
  348. Raises ValueError if val isn't found
  349. """
  350. return self.values().index(val)
  351. def extend(self, values):
  352. """Append the contents of 'values' to this OSCMessage.
  353. 'values' can be another OSCMessage, or a list/tuple of ints/floats/strings
  354. """
  355. items = self.items() + self._buildItemList(values)
  356. self._reencode(items)
  357. def insert(self, i, val, typehint = None):
  358. """Insert given value (with optional typehint) into the OSCMessage
  359. at the given index.
  360. """
  361. items = self.items()
  362. for item in reversed(self._buildItemList(val)):
  363. items.insert(i, item)
  364. self._reencode(items)
  365. def popitem(self, i):
  366. """Delete the indicated argument from the OSCMessage, and return it
  367. as a (typetag, value) tuple.
  368. """
  369. items = self.items()
  370. item = items.pop(i)
  371. self._reencode(items)
  372. return item
  373. def pop(self, i):
  374. """Delete the indicated argument from the OSCMessage, and return it.
  375. """
  376. return self.popitem(i)[1]
  377. def reverse(self):
  378. """Reverses the arguments of the OSCMessage (in place)
  379. """
  380. items = self.items()
  381. items.reverse()
  382. self._reencode(items)
  383. def remove(self, val):
  384. """Removes the first argument with the given value from the OSCMessage.
  385. Raises ValueError if val isn't found.
  386. """
  387. items = self.items()
  388. # this is not very efficient...
  389. i = 0
  390. for (t, v) in items:
  391. if (v == val):
  392. break
  393. i += 1
  394. else:
  395. raise ValueError("'%s' not in OSCMessage" % str(m))
  396. # but more efficient than first calling self.values().index(val),
  397. # then calling self.items(), which would in turn call self.values() again...
  398. del items[i]
  399. self._reencode(items)
  400. def __iter__(self):
  401. """Returns an iterator of the OSCMessage's arguments
  402. """
  403. return iter(self.values())
  404. def __reversed__(self):
  405. """Returns a reverse iterator of the OSCMessage's arguments
  406. """
  407. return reversed(self.values())
  408. def itervalues(self):
  409. """Returns an iterator of the OSCMessage's arguments
  410. """
  411. return iter(self.values())
  412. def iteritems(self):
  413. """Returns an iterator of the OSCMessage's arguments as
  414. (typetag, value) tuples
  415. """
  416. return iter(self.items())
  417. def itertags(self):
  418. """Returns an iterator of the OSCMessage's arguments' typetags
  419. """
  420. return iter(self.tags())
  421. class OSCBundle(OSCMessage):
  422. """Builds a 'bundle' of OSC messages.
  423. OSCBundle objects are container objects for building OSC-bundles of OSC-messages.
  424. An OSC-bundle is a special kind of OSC-message which contains a list of OSC-messages
  425. (And yes, OSC-bundles may contain other OSC-bundles...)
  426. OSCBundle objects behave much the same as OSCMessage objects, with these exceptions:
  427. - if an item or items to be appended or inserted are not OSCMessage objects,
  428. OSCMessage objectss are created to encapsulate the item(s)
  429. - an OSC-bundle does not have an address of its own, only the contained OSC-messages do.
  430. The OSCBundle's 'address' is inherited by any OSCMessage the OSCBundle object creates.
  431. - OSC-bundles have a timetag to tell the receiver when the bundle should be processed.
  432. The default timetag value (0) means 'immediately'
  433. """
  434. def __init__(self, address="", time=0):
  435. """Instantiate a new OSCBundle.
  436. The default OSC-address for newly created OSCMessages
  437. can be specified with the 'address' argument
  438. The bundle's timetag can be set with the 'time' argument
  439. """
  440. super(OSCBundle, self).__init__(address)
  441. self.timetag = time
  442. def __str__(self):
  443. """Returns the Bundle's contents (and timetag, if nonzero) as a string.
  444. """
  445. if (self.timetag > 0.):
  446. out = "#bundle (%s) [" % self.getTimeTagStr()
  447. else:
  448. out = "#bundle ["
  449. if self.__len__():
  450. for val in self.values():
  451. out += "%s, " % str(val)
  452. out = out[:-2] # strip trailing space and comma
  453. return out + "]"
  454. def setTimeTag(self, time):
  455. """Set or change the OSCBundle's TimeTag
  456. In 'Python Time', that's floating seconds since the Epoch
  457. """
  458. if time >= 0:
  459. self.timetag = time
  460. def getTimeTagStr(self):
  461. """Return the TimeTag as a human-readable string
  462. """
  463. fract, secs = math.modf(self.timetag)
  464. out = time.ctime(secs)[11:19]
  465. out += ("%.3f" % fract)[1:]
  466. return out
  467. def append(self, argument, typehint = None):
  468. """Appends data to the bundle, creating an OSCMessage to encapsulate
  469. the provided argument unless this is already an OSCMessage.
  470. Any newly created OSCMessage inherits the OSCBundle's address at the time of creation.
  471. If 'argument' is an iterable, its elements will be encapsuated by a single OSCMessage.
  472. Finally, 'argument' can be (or contain) a dict, which will be 'converted' to an OSCMessage;
  473. - if 'addr' appears in the dict, its value overrides the OSCBundle's address
  474. - if 'args' appears in the dict, its value(s) become the OSCMessage's arguments
  475. """
  476. if isinstance(argument, OSCMessage):
  477. binary = OSCBlob(argument.getBinary())
  478. else:
  479. msg = OSCMessage(self.address)
  480. if type(argument) == types.DictType:
  481. if 'addr' in argument:
  482. msg.setAddress(argument['addr'])
  483. if 'args' in argument:
  484. msg.append(argument['args'], typehint)
  485. else:
  486. msg.append(argument, typehint)
  487. binary = OSCBlob(msg.getBinary())
  488. self.message += binary
  489. self.typetags += 'b'
  490. def getBinary(self):
  491. """Returns the binary representation of the message
  492. """
  493. binary = OSCString("#bundle")
  494. binary += OSCTimeTag(self.timetag)
  495. binary += self.message
  496. return binary
  497. def _reencapsulate(self, decoded):
  498. if decoded[0] == "#bundle":
  499. msg = OSCBundle()
  500. msg.setTimeTag(decoded[1])
  501. for submsg in decoded[2:]:
  502. msg.append(self._reencapsulate(submsg))
  503. else:
  504. msg = OSCMessage(decoded[0])
  505. tags = decoded[1].lstrip(',')
  506. for i in range(len(tags)):
  507. msg.append(decoded[2+i], tags[i])
  508. return msg
  509. def values(self):
  510. """Returns a list of the OSCMessages appended so far
  511. """
  512. out = []
  513. for decoded in decodeOSC(self.getBinary())[2:]:
  514. out.append(self._reencapsulate(decoded))
  515. return out
  516. def __eq__(self, other):
  517. """Return True if two OSCBundles have the same timetag & content
  518. """
  519. if not isinstance(other, self.__class__):
  520. return False
  521. return (self.timetag == other.timetag) and (self.typetags == other.typetags) and (self.message == other.message)
  522. def copy(self):
  523. """Returns a deep copy of this OSCBundle
  524. """
  525. copy = super(OSCBundle, self).copy()
  526. copy.timetag = self.timetag
  527. return copy
  528. ######
  529. #
  530. # OSCMessage encoding functions
  531. #
  532. ######
  533. def OSCString(next):
  534. """Convert a string into a zero-padded OSC String.
  535. The length of the resulting string is always a multiple of 4 bytes.
  536. The string ends with 1 to 4 zero-bytes ('\x00')
  537. """
  538. OSCstringLength = math.ceil((len(next)+1) / 4.0) * 4
  539. return struct.pack(">%ds" % (OSCstringLength), str(next))
  540. def OSCBlob(next):
  541. """Convert a string into an OSC Blob.
  542. An OSC-Blob is a binary encoded block of data, prepended by a 'size' (int32).
  543. The size is always a mutiple of 4 bytes.
  544. The blob ends with 0 to 3 zero-bytes ('\x00')
  545. """
  546. if type(next) in types.StringTypes:
  547. OSCblobLength = math.ceil((len(next)) / 4.0) * 4
  548. binary = struct.pack(">i%ds" % (OSCblobLength), OSCblobLength, next)
  549. else:
  550. binary = ""
  551. return binary
  552. def OSCArgument(next, typehint=None):
  553. """ Convert some Python types to their
  554. OSC binary representations, returning a
  555. (typetag, data) tuple.
  556. """
  557. if not typehint:
  558. if type(next) in FloatTypes:
  559. binary = struct.pack(">f", float(next))
  560. tag = 'f'
  561. elif type(next) in IntTypes:
  562. binary = struct.pack(">i", int(next))
  563. tag = 'i'
  564. else:
  565. binary = OSCString(next)
  566. tag = 's'
  567. elif typehint == 'f':
  568. try:
  569. binary = struct.pack(">f", float(next))
  570. tag = 'f'
  571. except ValueError:
  572. binary = OSCString(next)
  573. tag = 's'
  574. elif typehint == 'i':
  575. try:
  576. binary = struct.pack(">i", int(next))
  577. tag = 'i'
  578. except ValueError:
  579. binary = OSCString(next)
  580. tag = 's'
  581. else:
  582. binary = OSCString(next)
  583. tag = 's'
  584. return (tag, binary)
  585. def OSCTimeTag(time):
  586. """Convert a time in floating seconds to its
  587. OSC binary representation
  588. """
  589. if time > 0:
  590. fract, secs = math.modf(time)
  591. binary = struct.pack('>ll', long(secs), long(fract * 1e9))
  592. else:
  593. binary = struct.pack('>ll', 0L, 1L)
  594. return binary
  595. ######
  596. #
  597. # OSCMessage decoding functions
  598. #
  599. ######
  600. def _readString(data):
  601. """Reads the next (null-terminated) block of data
  602. """
  603. length = string.find(data,"\0")
  604. nextData = int(math.ceil((length+1) / 4.0) * 4)
  605. return (data[0:length], data[nextData:])
  606. def _readBlob(data):
  607. """Reads the next (numbered) block of data
  608. """
  609. length = struct.unpack(">i", data[0:4])[0]
  610. nextData = int(math.ceil((length) / 4.0) * 4) + 4
  611. return (data[4:length+4], data[nextData:])
  612. def _readInt(data):
  613. """Tries to interpret the next 4 bytes of the data
  614. as a 32-bit integer. """
  615. if(len(data)<4):
  616. print "Error: too few bytes for int", data, len(data)
  617. rest = data
  618. integer = 0
  619. else:
  620. integer = struct.unpack(">i", data[0:4])[0]
  621. rest = data[4:]
  622. return (integer, rest)
  623. def _readLong(data):
  624. """Tries to interpret the next 8 bytes of the data
  625. as a 64-bit signed integer.
  626. """
  627. high, low = struct.unpack(">ll", data[0:8])
  628. big = (long(high) << 32) + low
  629. rest = data[8:]
  630. return (big, rest)
  631. def _readTimeTag(data):
  632. """Tries to interpret the next 8 bytes of the data
  633. as a TimeTag.
  634. """
  635. high, low = struct.unpack(">ll", data[0:8])
  636. if (high == 0) and (low <= 1):
  637. time = 0.0
  638. else:
  639. time = int(high) + float(low / 1e9)
  640. rest = data[8:]
  641. return (time, rest)
  642. def _readFloat(data):
  643. """Tries to interpret the next 4 bytes of the data
  644. as a 32-bit float.
  645. """
  646. if(len(data)<4):
  647. print "Error: too few bytes for float", data, len(data)
  648. rest = data
  649. float = 0
  650. else:
  651. float = struct.unpack(">f", data[0:4])[0]
  652. rest = data[4:]
  653. return (float, rest)
  654. def decodeOSC(data):
  655. """Converts a binary OSC message to a Python list.
  656. """
  657. table = {"i":_readInt, "f":_readFloat, "s":_readString, "b":_readBlob}
  658. decoded = []
  659. address, rest = _readString(data)
  660. if address.startswith(","):
  661. typetags = address
  662. address = ""
  663. else:
  664. typetags = ""
  665. if address == "#bundle":
  666. time, rest = _readTimeTag(rest)
  667. decoded.append(address)
  668. decoded.append(time)
  669. while len(rest)>0:
  670. length, rest = _readInt(rest)
  671. decoded.append(decodeOSC(rest[:length]))
  672. rest = rest[length:]
  673. elif len(rest)>0:
  674. if not len(typetags):
  675. typetags, rest = _readString(rest)
  676. decoded.append(address)
  677. decoded.append(typetags)
  678. if typetags.startswith(","):
  679. for tag in typetags[1:]:
  680. value, rest = table[tag](rest)
  681. decoded.append(value)
  682. else:
  683. raise OSCError("OSCMessage's typetag-string lacks the magic ','")
  684. return decoded
  685. ######
  686. #
  687. # Utility functions
  688. #
  689. ######
  690. def hexDump(bytes):
  691. """ Useful utility; prints the string in hexadecimal.
  692. """
  693. print "byte 0 1 2 3 4 5 6 7 8 9 A B C D E F"
  694. num = len(bytes)
  695. for i in range(num):
  696. if (i) % 16 == 0:
  697. line = "%02X0 : " % (i/16)
  698. line += "%02X " % ord(bytes[i])
  699. if (i+1) % 16 == 0:
  700. print "%s: %s" % (line, repr(bytes[i-15:i+1]))
  701. line = ""
  702. bytes_left = num % 16
  703. if bytes_left:
  704. print "%s: %s" % (line.ljust(54), repr(bytes[-bytes_left:]))
  705. def getUrlStr(*args):
  706. """Convert provided arguments to a string in 'host:port/prefix' format
  707. Args can be:
  708. - (host, port)
  709. - (host, port), prefix
  710. - host, port
  711. - host, port, prefix
  712. """
  713. if not len(args):
  714. return ""
  715. if type(args[0]) == types.TupleType:
  716. host = args[0][0]
  717. port = args[0][1]
  718. args = args[1:]
  719. else:
  720. host = args[0]
  721. port = args[1]
  722. args = args[2:]
  723. if len(args):
  724. prefix = args[0]
  725. else:
  726. prefix = ""
  727. if len(host) and (host != '0.0.0.0'):
  728. try:
  729. (host, _, _) = socket.gethostbyaddr(host)
  730. except socket.error:
  731. pass
  732. else:
  733. host = 'localhost'
  734. if type(port) == types.IntType:
  735. return "%s:%d%s" % (host, port, prefix)
  736. else:
  737. return host + prefix
  738. def parseUrlStr(url):
  739. """Convert provided string in 'host:port/prefix' format to it's components
  740. Returns ((host, port), prefix)
  741. """
  742. if not (type(url) in types.StringTypes and len(url)):
  743. return (None, '')
  744. i = url.find("://")
  745. if i > -1:
  746. url = url[i+3:]
  747. i = url.find(':')
  748. if i > -1:
  749. host = url[:i].strip()
  750. tail = url[i+1:].strip()
  751. else:
  752. host = ''
  753. tail = url
  754. for i in range(len(tail)):
  755. if not tail[i].isdigit():
  756. break
  757. else:
  758. i += 1
  759. portstr = tail[:i].strip()
  760. tail = tail[i:].strip()
  761. found = len(tail)
  762. for c in ('/', '+', '-', '*'):
  763. i = tail.find(c)
  764. if (i > -1) and (i < found):
  765. found = i
  766. head = tail[:found].strip()
  767. prefix = tail[found:].strip()
  768. prefix = prefix.strip('/')
  769. if len(prefix) and prefix[0] not in ('+', '-', '*'):
  770. prefix = '/' + prefix
  771. if len(head) and not len(host):
  772. host = head
  773. if len(host):
  774. try:
  775. host = socket.gethostbyname(host)
  776. except socket.error:
  777. pass
  778. try:
  779. port = int(portstr)
  780. except ValueError:
  781. port = None
  782. return ((host, port), prefix)
  783. ######
  784. #
  785. # OSCClient class
  786. #
  787. ######
  788. class OSCClient(object):
  789. """Simple OSC Client. Handles the sending of OSC-Packets (OSCMessage or OSCBundle) via a UDP-socket
  790. """
  791. # set outgoing socket buffer size
  792. sndbuf_size = 4096 * 8
  793. def __init__(self, server=None):
  794. """Construct an OSC Client.
  795. When the 'address' argument is given this client is connected to a specific remote server.
  796. - address ((host, port) tuple): the address of the remote server to send all messages to
  797. Otherwise it acts as a generic client:
  798. If address == 'None', the client doesn't connect to a specific remote server,
  799. and the remote address must be supplied when calling sendto()
  800. - server: Local OSCServer-instance this client will use the socket of for transmissions.
  801. If none is supplied, a socket will be created.
  802. """
  803. self.socket = None
  804. if server == None:
  805. self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  806. self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.sndbuf_size)
  807. self._fd = self.socket.fileno()
  808. self.server = None
  809. else:
  810. self.setServer(server)
  811. self.client_address = None
  812. def setServer(self, server):
  813. """Associate this Client with given server.
  814. The Client will send from the Server's socket.
  815. The Server will use this Client instance to send replies.
  816. """
  817. if not isinstance(server, OSCServer):
  818. raise ValueError("'server' argument is not a valid OSCServer object")
  819. if self.socket != None:
  820. self.close()
  821. self.socket = server.socket.dup()
  822. self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.sndbuf_size)
  823. self._fd = self.socket.fileno()
  824. self.server = server
  825. if self.server.client != None:
  826. self.server.client.close()
  827. self.server.client = self
  828. def close(self):
  829. """Disconnect & close the Client's socket
  830. """
  831. if self.socket != None:
  832. self.socket.close()
  833. self.socket = None
  834. def __str__(self):
  835. """Returns a string containing this Client's Class-name, software-version
  836. and the remote-address it is connected to (if any)
  837. """
  838. out = self.__class__.__name__
  839. out += " v%s.%s-%s" % version
  840. addr = self.address()
  841. if addr:
  842. out += " connected to osc://%s" % getUrlStr(addr)
  843. else:
  844. out += " (unconnected)"
  845. return out
  846. def __eq__(self, other):
  847. """Compare function.
  848. """
  849. if not isinstance(other, self.__class__):
  850. return False
  851. isequal = cmp(self.socket._sock, other.socket._sock)
  852. if isequal and self.server and other.server:
  853. return cmp(self.server, other.server)
  854. return isequal
  855. def __ne__(self, other):
  856. """Compare function.
  857. """
  858. return not self.__eq__(other)
  859. def address(self):
  860. """Returns a (host,port) tuple of the remote server this client is
  861. connected to or None if not connected to any server.
  862. """
  863. try:
  864. return self.socket.getpeername()
  865. except socket.error:
  866. return None
  867. def connect(self, address):
  868. """Bind to a specific OSC server:
  869. the 'address' argument is a (host, port) tuple
  870. - host: hostname of the remote OSC server,
  871. - port: UDP-port the remote OSC server listens to.
  872. """
  873. try:
  874. self.socket.connect(address)
  875. self.client_address = address
  876. except socket.error, e:
  877. self.client_address = None
  878. raise OSCClientError("SocketError: %s" % str(e))
  879. if self.server != None:
  880. self.server.return_port = address[1]
  881. def sendto(self, msg, address, timeout=None):
  882. """Send the given OSCMessage to the specified address.
  883. - msg: OSCMessage (or OSCBundle) to be sent
  884. - address: (host, port) tuple specifing remote server to send the message to
  885. - timeout: A timeout value for attempting to send. If timeout == None,
  886. this call blocks until socket is available for writing.
  887. Raises OSCClientError when timing out while waiting for the socket.
  888. """
  889. if not isinstance(msg, OSCMessage):
  890. raise TypeError("'msg' argument is not an OSCMessage or OSCBundle object")
  891. ret = select.select([],[self._fd], [], timeout)
  892. try:
  893. ret[1].index(self._fd)
  894. except:
  895. # for the very rare case this might happen
  896. raise OSCClientError("Timed out waiting for file descriptor")
  897. try:
  898. self.socket.connect(address)
  899. self.socket.sendall(msg.getBinary())
  900. if self.client_address:
  901. self.socket.connect(self.client_address)
  902. except socket.error, e:
  903. if e[0] in (7, 65): # 7 = 'no address associated with nodename', 65 = 'no route to host'
  904. raise e
  905. else:
  906. raise OSCClientError("while sending to %s: %s" % (str(address), str(e)))
  907. def send(self, msg, timeout=None):
  908. """Send the given OSCMessage.
  909. The Client must be already connected.
  910. - msg: OSCMessage (or OSCBundle) to be sent
  911. - timeout: A timeout value for attempting to send. If timeout == None,
  912. this call blocks until socket is available for writing.
  913. Raises OSCClientError when timing out while waiting for the socket,
  914. or when the Client isn't connected to a remote server.
  915. """
  916. if not isinstance(msg, OSCMessage):
  917. raise TypeError("'msg' argument is not an OSCMessage or OSCBundle object")
  918. ret = select.select([],[self._fd], [], timeout)
  919. try:
  920. ret[1].index(self._fd)
  921. except:
  922. # for the very rare case this might happen
  923. raise OSCClientError("Timed out waiting for file descriptor")
  924. try:
  925. self.socket.sendall(msg.getBinary())
  926. except socket.error, e:
  927. if e[0] in (7, 65): # 7 = 'no address associated with nodename', 65 = 'no route to host'
  928. raise e
  929. else:
  930. raise OSCClientError("while sending: %s" % str(e))
  931. ######
  932. #
  933. # FilterString Utility functions
  934. #
  935. ######
  936. def parseFilterStr(args):
  937. """Convert Message-Filter settings in '+<addr> -<addr> ...' format to a dict of the form
  938. { '<addr>':True, '<addr>':False, ... }
  939. Returns a list: ['<prefix>', filters]
  940. """
  941. out = {}
  942. if type(args) in types.StringTypes:
  943. args = [args]
  944. prefix = None
  945. for arg in args:
  946. head = None
  947. for plus in arg.split('+'):
  948. minus = plus.split('-')
  949. plusfs = minus.pop(0).strip()
  950. if len(plusfs):
  951. plusfs = '/' + plusfs.strip('/')
  952. if (head == None) and (plusfs != "/*"):
  953. head = plusfs
  954. elif len(plusfs):
  955. if plusfs == '/*':
  956. out = { '/*':True } # reset all previous filters
  957. else:
  958. out[plusfs] = True
  959. for minusfs in minus:
  960. minusfs = minusfs.strip()
  961. if len(minusfs):
  962. minusfs = '/' + minusfs.strip('/')
  963. if minusfs == '/*':
  964. out = { '/*':False } # reset all previous filters
  965. else:
  966. out[minusfs] = False
  967. if prefix == None:
  968. prefix = head
  969. return [prefix, out]
  970. def getFilterStr(filters):
  971. """Return the given 'filters' dict as a list of
  972. '+<addr>' | '-<addr>' filter-strings
  973. """
  974. if not len(filters):
  975. return []
  976. if '/*' in filters.keys():
  977. if filters['/*']:
  978. out = ["+/*"]
  979. else:
  980. out = ["-/*"]
  981. else:
  982. if False in filters.values():
  983. out = ["+/*"]
  984. else:
  985. out = ["-/*"]
  986. for (addr, bool) in filters.items():
  987. if addr == '/*':
  988. continue
  989. if bool:
  990. out.append("+%s" % addr)
  991. else:
  992. out.append("-%s" % addr)
  993. return out
  994. # A translation-table for mapping OSC-address expressions to Python 're' expressions
  995. OSCtrans = string.maketrans("{,}?","(|).")
  996. def getRegEx(pattern):
  997. """Compiles and returns a 'regular expression' object for the given address-pattern.
  998. """
  999. # Translate OSC-address syntax to python 're' syntax
  1000. pattern = pattern.replace(".", r"\.") # first, escape all '.'s in the pattern.
  1001. pattern = pattern.replace("(", r"\(") # escape all '('s.
  1002. pattern = pattern.replace(")", r"\)") # escape all ')'s.
  1003. pattern = pattern.replace("*", r".*") # replace a '*' by '.*' (match 0 or more characters)
  1004. pattern = pattern.translate(OSCtrans) # change '?' to '.' and '{,}' to '(|)'
  1005. return re.compile(pattern)
  1006. ######
  1007. #
  1008. # OSCMultiClient class
  1009. #
  1010. ######
  1011. class OSCMultiClient(OSCClient):
  1012. """'Multiple-Unicast' OSC Client. Handles the sending of OSC-Packets (OSCMessage or OSCBundle) via a UDP-socket
  1013. This client keeps a dict of 'OSCTargets'. and sends each OSCMessage to each OSCTarget
  1014. The OSCTargets are simply (host, port) tuples, and may be associated with an OSC-address prefix.
  1015. the OSCTarget's prefix gets prepended to each OSCMessage sent to that target.
  1016. """
  1017. def __init__(self, server=None):
  1018. """Construct a "Multi" OSC Client.
  1019. - server: Local OSCServer-instance this client will use the socket of for transmissions.
  1020. If none is supplied, a socket will be created.
  1021. """
  1022. super(OSCMultiClient, self).__init__(server)
  1023. self.targets = {}
  1024. def _searchHostAddr(self, host):
  1025. """Search the subscribed OSCTargets for (the first occurence of) given host.
  1026. Returns a (host, port) tuple
  1027. """
  1028. try:
  1029. host = socket.gethostbyname(host)
  1030. except socket.error:
  1031. pass
  1032. for addr in self.targets.keys():
  1033. if host == addr[0]:
  1034. return addr
  1035. raise NotSubscribedError((host, None))
  1036. def _updateFilters(self, dst, src):
  1037. """Update a 'filters' dict with values form another 'filters' dict:
  1038. - src[a] == True and dst[a] == False: del dst[a]
  1039. - src[a] == False and dst[a] == True: del dst[a]
  1040. - a not in dst: dst[a] == src[a]
  1041. """
  1042. if '/*' in src.keys(): # reset filters
  1043. dst.clear() # 'match everything' == no filters
  1044. if not src.pop('/*'):
  1045. dst['/*'] = False # 'match nothing'
  1046. for (addr, bool) in src.items():
  1047. if (addr in dst.keys()) and (dst[addr] != bool):
  1048. del dst[addr]
  1049. else:
  1050. dst[addr] = bool
  1051. def _setTarget(self, address, prefix=None, filters=None):
  1052. """Add (i.e. subscribe) a new OSCTarget, or change the prefix for an existing OSCTarget.
  1053. - address ((host, port) tuple): IP-address & UDP-port
  1054. - prefix (string): The OSC-address prefix prepended to the address of each OSCMessage
  1055. sent to this OSCTarget (optional)
  1056. """
  1057. if address not in self.targets.keys():
  1058. self.targets[address] = ["",{}]
  1059. if prefix != None:
  1060. if len(prefix):
  1061. # make sure prefix starts with ONE '/', and does not end with '/'
  1062. prefix = '/' + prefix.strip('/')
  1063. self.targets[address][0] = prefix
  1064. if filters != None:
  1065. if type(filters) in types.StringTypes:
  1066. (_, filters) = parseFilterStr(filters)
  1067. elif type(filters) != types.DictType:
  1068. raise TypeError("'filters' argument must be a dict with {addr:bool} entries")
  1069. self._updateFilters(self.targets[address][1], filters)
  1070. def setOSCTarget(self, address, prefix=None, filters=None):
  1071. """Add (i.e. subscribe) a new OSCTarget, or change the prefix for an existing OSCTarget.
  1072. the 'address' argument can be a ((host, port) tuple) : The target server address & UDP-port
  1073. or a 'host' (string) : The host will be looked-up
  1074. - prefix (string): The OSC-address prefix prepended to the address of each OSCMessage
  1075. sent to this OSCTarget (optional)
  1076. """
  1077. if type(address) in types.StringTypes:
  1078. address = self._searchHostAddr(address)
  1079. elif (type(address) == types.TupleType):
  1080. (host, port) = address[:2]
  1081. try:
  1082. host = socket.gethostbyname(host)
  1083. except:
  1084. pass
  1085. address = (host, port)
  1086. else:
  1087. raise TypeError("'address' argument must be a (host, port) tuple or a 'host' string")
  1088. self._setTarget(address, prefix, filters)
  1089. def setOSCTargetFromStr(self, url):
  1090. """Adds or modifies a subscribed OSCTarget from the given string, which should be in the
  1091. '<host>:<port>[/<prefix>] [+/<filter>]|[-/<filter>] ...' format.
  1092. """
  1093. (addr, tail) = parseUrlStr(url)
  1094. (prefix, filters) = parseFilterStr(tail)
  1095. self._setTarget(addr, prefix, filters)
  1096. def _delTarget(self, address, prefix=None):
  1097. """Delete the specified OSCTarget from the Client's dict.
  1098. the 'address' argument must be a (host, port) tuple.
  1099. If the 'prefix' argument is given, the Target is only deleted if the address and prefix match.
  1100. """
  1101. try:
  1102. if prefix == None:
  1103. del self.targets[address]
  1104. elif prefix == self.targets[address][0]:
  1105. del self.targets[address]
  1106. except KeyError:
  1107. raise NotSubscribedError(address, prefix)
  1108. def delOSCTarget(self, address, prefix=None):
  1109. """Delete the specified OSCTarget from the Client's dict.
  1110. the 'address' argument can be a ((host, port) tuple), or a hostname.
  1111. If the 'prefix' argument is given, the Target is only deleted if the address and prefix match.
  1112. """
  1113. if type(address) in types.StringTypes:
  1114. address = self._searchHostAddr(address)
  1115. if type(address) == types.TupleType:
  1116. (host, port) = address[:2]
  1117. try:
  1118. host = socket.gethostbyname(host)
  1119. except socket.error:
  1120. pass
  1121. address = (host, port)
  1122. self._delTarget(address, prefix)
  1123. def hasOSCTarget(self, address, prefix=None):
  1124. """Return True if the given OSCTarget exists in the Client's dict.
  1125. the 'address' argument can be a ((host, port) tuple), or a hostname.
  1126. If the 'prefix' argument is given, the return-value is only True if the address and prefix match.
  1127. """
  1128. if type(address) in types.StringTypes:
  1129. address = self._searchHostAddr(address)
  1130. if type(address) == types.TupleType:
  1131. (host, port) = address[:2]
  1132. try:
  1133. host = socket.gethostbyname(host)
  1134. except socket.error:
  1135. pass
  1136. address = (host, port)
  1137. if address in self.targets.keys():
  1138. if prefix == None:
  1139. return True
  1140. elif prefix == self.targets[address][0]:
  1141. return True
  1142. return False
  1143. def getOSCTargets(self):
  1144. """Returns the dict of OSCTargets: {addr:[prefix, filters], ...}
  1145. """
  1146. out = {}
  1147. for ((host, port), pf) in self.targets.items():
  1148. try:
  1149. (host, _, _) = socket.gethostbyaddr(host)
  1150. except socket.error:
  1151. pass
  1152. out[(host, port)] = pf
  1153. return out
  1154. def getOSCTarget(self, address):
  1155. """Returns the OSCTarget matching the given address as a ((host, port), [prefix, filters]) tuple.
  1156. 'address' can be a (host, port) tuple, or a 'host' (string), in which case the first matching OSCTarget is returned
  1157. Returns (None, ['',{}]) if address not found.
  1158. """
  1159. if type(address) in types.StringTypes:
  1160. address = self._searchHostAddr(address)
  1161. if (type(address) == types.TupleType):
  1162. (host, port) = address[:2]
  1163. try:
  1164. host = socket.gethostbyname(host)
  1165. except socket.error:
  1166. pass
  1167. address = (host, port)
  1168. if (address in self.targets.keys()):
  1169. try:
  1170. (host, _, _) = socket.gethostbyaddr(host)
  1171. except socket.error:
  1172. pass
  1173. return ((host, port), self.targets[address])
  1174. return (None, ['',{}])
  1175. def clearOSCTargets(self):
  1176. """Erases all OSCTargets from the Client's dict
  1177. """
  1178. self.targets = {}
  1179. def updateOSCTargets(self, dict):
  1180. """Update the Client's OSCTargets dict with the contents of 'dict'
  1181. The given dict's items MUST be of the form
  1182. { (host, port):[prefix, filters], ... }
  1183. """
  1184. for ((host, port), (prefix, filters)) in dict.items():
  1185. val = [prefix, {}]
  1186. self._updateFilters(val[1], filters)
  1187. try:
  1188. host = socket.gethostbyname(host)
  1189. except socket.error:
  1190. pass
  1191. self.targets[(host, port)] = val
  1192. def getOSCTargetStr(self, address):
  1193. """Returns the OSCTarget matching the given address as a ('osc://<host>:<port>[<prefix>]', ['<filter-string>', ...])' tuple.
  1194. 'address' can be a (host, port) tuple, or a 'host' (string), in which case the first matching OSCTarget is returned
  1195. Returns (None, []) if address not found.
  1196. """
  1197. (addr, (prefix, filters)) = self.getOSCTarget(address)
  1198. if addr == None:
  1199. return (None, [])
  1200. return ("osc://%s" % getUrlStr(addr, prefix), getFilterStr(filters))
  1201. def getOSCTargetStrings(self):
  1202. """Returns a list of all OSCTargets as ('osc://<host>:<port>[<prefix>]', ['<filter-string>', ...])' tuples.
  1203. """
  1204. out = []
  1205. for (addr, (prefix, filters)) in self.targets.items():
  1206. out.append(("osc://%s" % getUrlStr(addr, prefix), getFilterStr(filters)))
  1207. return out
  1208. def connect(self, address):
  1209. """The OSCMultiClient isn't allowed to connect to any specific
  1210. address.
  1211. """
  1212. return NotImplemented
  1213. def sendto(self, msg, address, timeout=None):
  1214. """Send the given OSCMessage.
  1215. The specified address is ignored. Instead this method calls send() to
  1216. send the message to all subscribed clients.
  1217. - msg: OSCMessage (or OSCBundle) to be sent
  1218. - address: (host, port) tuple specifing remote server to send the message to
  1219. - timeout: A timeout value for attempting to send. If timeout == None,
  1220. this call blocks until socket is available for writing.
  1221. Raises OSCClientError when timing out while waiting for the socket.
  1222. """
  1223. self.send(msg, timeout)
  1224. def _filterMessage(self, filters, msg):
  1225. """Checks the given OSCMessge against the given filters.
  1226. 'filters' is a dict containing OSC-address:bool pairs.
  1227. If 'msg' is an OSCBundle, recursively filters its constituents.
  1228. Returns None if the message is to be filtered, else returns the message.
  1229. or
  1230. Returns a copy of the OSCBundle with the filtered messages removed.
  1231. """
  1232. if isinstance(msg, OSCBundle):
  1233. out = msg.copy()
  1234. msgs = out.values()
  1235. out.clearData()
  1236. for m in msgs:
  1237. m = self._filterMessage(filters, m)
  1238. if m: # this catches 'None' and empty bundles.
  1239. out.append(m)
  1240. elif isinstance(msg, OSCMessage):
  1241. if '/*' in filters.keys():
  1242. if filters['/*']:
  1243. out = msg
  1244. else:
  1245. out = None
  1246. elif False in filters.values():
  1247. out = msg
  1248. else:
  1249. out = None
  1250. else:
  1251. raise TypeError("'msg' argument is not an OSCMessage or OSCBundle object")
  1252. expr = getRegEx(msg.address)
  1253. for addr in filters.keys():
  1254. if addr == '/*':
  1255. continue
  1256. match = expr.match(addr)
  1257. if match and (match.end() == len(addr)):
  1258. if filters[addr]:
  1259. out = msg
  1260. else:
  1261. out = None
  1262. break
  1263. return out
  1264. def _prefixAddress(self, prefix, msg):
  1265. """Makes a copy of the given OSCMessage, then prepends the given prefix to
  1266. The message's OSC-address.
  1267. If 'msg' is an OSCBundle, recursively prepends the prefix to its constituents.
  1268. """
  1269. out = msg.copy()
  1270. if isinstance(msg, OSCBundle):
  1271. msgs = out.values()
  1272. out.clearData()
  1273. for m in msgs:
  1274. out.append(self._prefixAddress(prefix, m))
  1275. elif isinstance(msg, OSCMessage):
  1276. out.setAddress(prefix + out.address)
  1277. else:
  1278. raise TypeError("'msg' argument is not an OSCMessage or OSCBundle object")
  1279. return out
  1280. def send(self, msg, timeout=None):
  1281. """Send the given OSCMessage to all subscribed OSCTargets
  1282. - msg: OSCMessage (or OSCBundle) to be sent
  1283. - timeout: A timeout value for attempting to send. If timeout == None,
  1284. this call blocks until socket is available for writing.
  1285. Raises OSCClientError when timing out while waiting for the socket.
  1286. """
  1287. for (address, (prefix, filters)) in self.targets.items():
  1288. if len(filters):
  1289. out = self._filterMessage(filters, msg)
  1290. if not out: # this catches 'None' and empty bundles.
  1291. continue
  1292. else:
  1293. out = msg
  1294. if len(prefix):
  1295. out = self._prefixAddress(prefix, msg)
  1296. binary = out.getBinary()
  1297. ret = select.select([],[self._fd], [], timeout)
  1298. try:
  1299. ret[1].index(self._fd)
  1300. except:
  1301. # for the very rare case this might happen
  1302. raise OSCClientError("Timed out waiting for file descriptor")
  1303. try:
  1304. while len(binary):
  1305. sent = self.socket.sendto(binary, address)
  1306. binary = binary[sent:]
  1307. except socket.error, e:
  1308. if e[0] in (7, 65): # 7 = 'no address associated with nodename', 65 = 'no route to host'
  1309. raise e
  1310. else:
  1311. raise OSCClientError("while sending to %s: %s" % (str(address), str(e)))
  1312. ######
  1313. #
  1314. # OSCRequestHandler classes
  1315. #
  1316. ######
  1317. class OSCRequestHandler(DatagramRequestHandler):
  1318. """RequestHandler class for the OSCServer
  1319. """
  1320. def dispatchMessage(self, pattern, tags, data):
  1321. """Attmept to match the given OSC-address pattern, which may contain '*',
  1322. against all callbacks registered with the OSCServer.
  1323. Calls the matching callback and returns whatever it returns.
  1324. If no match is found, and a 'default' callback is registered, it calls that one,
  1325. or raises NoCallbackError if a 'default' callback is not registered.
  1326. - pattern (string): The OSC-address of the receied message
  1327. - tags (string): The OSC-typetags of the receied message's arguments, without ','
  1328. - data (list): The message arguments
  1329. """
  1330. if len(tags) != len(data):
  1331. raise OSCServerError("Malformed OSC-message; got %d typetags [%s] vs. %d values" % (len(tags), tags, len(data)))
  1332. expr = getRegEx(pattern)
  1333. replies = []
  1334. matched = 0
  1335. for addr in self.server.callbacks.keys():
  1336. match = expr.match(addr)
  1337. if match and (match.end() == len(addr)):
  1338. reply = self.server.callbacks[addr](pattern, tags, data, self.client_address)
  1339. matched += 1
  1340. if isinstance(reply, OSCMessage):
  1341. replies.append(reply)
  1342. elif reply != None:
  1343. raise TypeError("Message-callback %s did not return OSCMessage or None: %s" % (self.server.callbacks[addr], type(reply)))
  1344. if matched == 0:
  1345. if 'default' in self.server.callbacks:
  1346. reply = self.server.callbacks['default'](pattern, tags, data, self.client_address)
  1347. if isinstance(reply, OSCMessage):
  1348. replies.append(reply)
  1349. elif reply != None:
  1350. raise TypeError("Message-callback %s did not return OSCMessage or None: %s" % (self.server.callbacks['default'], type(reply)))
  1351. else:
  1352. raise NoCallbackError(pattern)
  1353. return replies
  1354. def setup(self):
  1355. """Prepare RequestHandler.
  1356. Unpacks request as (packet, source socket address)
  1357. Creates an empty list for replies.
  1358. """
  1359. (self.packet, self.socket) = self.request
  1360. self.replies = []
  1361. def _unbundle(self, decoded):
  1362. """Recursive bundle-unpacking function"""
  1363. if decoded[0] != "#bundle":
  1364. self.replies += self.dispatchMessage(decoded[0], decoded[1][1:], decoded[2:])
  1365. return
  1366. now = time.time()
  1367. timetag = decoded[1]
  1368. if (timetag > 0.) and (timetag > now):
  1369. time.sleep(timetag - now)
  1370. for msg in decoded[2:]:
  1371. self._unbundle(msg)
  1372. def handle(self):
  1373. """Handle incoming OSCMessage
  1374. """
  1375. decoded = decodeOSC(self.packet)
  1376. if not len(decoded):
  1377. return
  1378. self._unbundle(decoded)
  1379. def finish(self):
  1380. """Finish handling OSCMessage.
  1381. Send any reply returned by the callback(s) back to the originating client
  1382. as an OSCMessage or OSCBundle
  1383. """
  1384. if self.server.return_port:
  1385. self.client_address = (self.client_address[0], self.server.return_port)
  1386. if len(self.replies) > 1:
  1387. msg = OSCBundle()
  1388. for reply in self.replies:
  1389. msg.append(reply)
  1390. elif len(self.replies) == 1:
  1391. msg = self.replies[0]
  1392. else:
  1393. return
  1394. self.server.client.sendto(msg, self.client_address)
  1395. class ThreadingOSCRequestHandler(OSCRequestHandler):
  1396. """Multi-threaded OSCRequestHandler;
  1397. Starts a new RequestHandler thread for each unbundled OSCMessage
  1398. """
  1399. def _unbundle(self, decoded):
  1400. """Recursive bundle-unpacking function
  1401. This version starts a new thread for each sub-Bundle found in the Bundle,
  1402. then waits for all its children to finish.
  1403. """
  1404. if decoded[0] != "#bundle":
  1405. self.replies += self.dispatchMessage(decoded[0], decoded[1][1:], decoded[2:])
  1406. return
  1407. now = time.time()
  1408. timetag = decoded[1]
  1409. if (timetag > 0.) and (timetag > now):
  1410. time.sleep(timetag - now)
  1411. now = time.time()
  1412. children = []
  1413. for msg in decoded[2:]:
  1414. t = threading.Thread(target = self._unbundle, args = (msg,))
  1415. t.start()
  1416. children.append(t)
  1417. # wait for all children to terminate
  1418. for t in children:
  1419. t.join()
  1420. ######
  1421. #
  1422. # OSCServer classes
  1423. #
  1424. ######
  1425. class OSCServer(UDPServer):
  1426. """A Synchronous OSCServer
  1427. Serves one request at-a-time, until the OSCServer is closed.
  1428. The OSC address-pattern is matched against a set of OSC-adresses
  1429. that have been registered to the server with a callback-function.
  1430. If the adress-pattern of the message machtes the registered address of a callback,
  1431. that function is called.
  1432. """
  1433. # set the RequestHandlerClass, will be overridden by ForkingOSCServer & ThreadingOSCServer
  1434. RequestHandlerClass = OSCRequestHandler
  1435. # define a socket timeout, so the serve_forever loop can actually exit.
  1436. socket_timeout = 1
  1437. # DEBUG: print error-tracebacks (to stderr)?
  1438. print_tracebacks = False
  1439. def __init__(self, server_address, client=None, return_port=0):
  1440. """Instantiate an OSCServer.
  1441. - server_address ((host, port) tuple): the local host & UDP-port
  1442. the server listens on
  1443. - client (OSCClient instance): The OSCClient used to send replies from this server.
  1444. If none is supplied (default) an OSCClient will be created.
  1445. - return_port (int): if supplied, sets the default UDP destination-port
  1446. for replies coming from this server.
  1447. """
  1448. UDPServer.__init__(self, server_address, self.RequestHandlerClass)
  1449. self.callbacks = {}
  1450. self.setReturnPort(return_port)
  1451. self.error_prefix = ""
  1452. self.info_prefix = "/info"
  1453. self.socket.settimeout(self.socket_timeout)
  1454. self.running = False
  1455. self.client = None
  1456. if client == None:
  1457. self.client = OSCClient(server=self)
  1458. else:
  1459. self.setClient(client)
  1460. def setClient(self, client):
  1461. """Associate this Server with a new local Client instance, closing the Client this Server is currently using.
  1462. """
  1463. if not isinstance(client, OSCClient):
  1464. raise ValueError("'client' argument is not a valid OSCClient object")
  1465. if client.server != None:
  1466. raise OSCServerError("Provided OSCClient already has an OSCServer-instance: %s" % str(client.server))
  1467. # Server socket is already listening at this point, so we can't use the client's socket.
  1468. # we'll have to force our socket on the client...
  1469. client_address = client.address() # client may be already connected
  1470. client.close() # shut-down that socket
  1471. # force our socket upon the client
  1472. client.socket = self.socket.dup()
  1473. client.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, client.sndbuf_size)
  1474. client._fd = client.socket.fileno()
  1475. client.server = self
  1476. if client_address:
  1477. client.connect(client_address)
  1478. if not self.return_port:
  1479. self.return_port = client_address[1]
  1480. if self.client != None:
  1481. self.client.close()
  1482. self.client = client
  1483. def serve_forever(self):
  1484. """Handle one request at a time until server is closed."""
  1485. self.running = True
  1486. while self.running:
  1487. self.handle_request() # this times-out when no data arrives.
  1488. def close(self):
  1489. """Stops serving requests, closes server (socket), closes used client
  1490. """
  1491. self.running = False
  1492. self.client.close()
  1493. self.server_close()
  1494. def __str__(self):
  1495. """Returns a string containing this Server's Class-name, software-version and local bound address (if any)
  1496. """
  1497. out = self.__class__.__name__
  1498. out += " v%s.%s-%s" % version
  1499. addr = self.address()
  1500. if addr:
  1501. out += " listening on osc://%s" % getUrlStr(addr)
  1502. else:
  1503. out += " (unbound)"
  1504. return out
  1505. def __eq__(self, other):
  1506. """Compare function.
  1507. """
  1508. if not isinstance(other, self.__class__):
  1509. return False
  1510. return cmp(self.socket._sock, other.socket._sock)
  1511. def __ne__(self, other):
  1512. """Compare function.
  1513. """
  1514. return not self.__eq__(other)
  1515. def address(self):
  1516. """Returns a (host,port) tuple of the local address this server is bound to,
  1517. or None if not bound to any address.
  1518. """
  1519. try:
  1520. return self.socket.getsockname()
  1521. except socket.error:
  1522. return None
  1523. def setReturnPort(self, port):
  1524. """Set the destination UDP-port for replies returning from this server to the remote client
  1525. """
  1526. if (port > 1024) and (port < 65536):
  1527. self.return_port = port
  1528. else:
  1529. self.return_port = None
  1530. def setSrvInfoPrefix(self, pattern):
  1531. """Set the first part of OSC-address (pattern) this server will use to reply to server-info requests.
  1532. """
  1533. if len(pattern):
  1534. pattern = '/' + pattern.strip('/')
  1535. self.info_prefix = pattern
  1536. def setSrvErrorPrefix(self, pattern=""):
  1537. """Set the OSC-address (pattern) this server will use to report errors occuring during
  1538. received message handling to the remote client.
  1539. If pattern is empty (default), server-errors are not reported back to the client.
  1540. """
  1541. if len(pattern):
  1542. pattern = '/' + pattern.strip('/')
  1543. self.error_prefix = pattern
  1544. def addMsgHandler(self, address, callback):
  1545. """Register a handler for an OSC-address
  1546. - 'address' is the OSC address-string.
  1547. the address-string should start with '/' and may not contain '*'
  1548. - 'callback' is the function called for incoming OSCMessages that match 'address'.
  1549. The callback-function will be called with the same arguments as the 'msgPrinter_handler' below
  1550. """
  1551. for chk in '*?,[]{}# ':
  1552. if chk in address:
  1553. raise OSCServerError("OSC-address string may not contain any characters in '*?,[]{}# '")
  1554. if type(callback) not in (types.FunctionType, types.MethodType):
  1555. raise OSCServerError("Message callback '%s' is not callable" % repr(callback))
  1556. if address != 'default':
  1557. address = '/' + address.strip('/')
  1558. self.callbacks[address] = callback
  1559. def delMsgHandler(self,address):
  1560. """Remove the registered handler for the given OSC-address
  1561. """
  1562. del self.callbacks[address]
  1563. def getOSCAddressSpace(self):
  1564. """Returns a list containing all OSC-addresses registerd with this Server.
  1565. """
  1566. return self.callbacks.keys()
  1567. def addDefaultHandlers(self, prefix="", info_prefix="/info", error_prefix="/error"):
  1568. """Register a default set of OSC-address handlers with this Server:
  1569. - 'default' -> noCallback_handler
  1570. the given prefix is prepended to all other callbacks registered by this method:
  1571. - '<prefix><info_prefix' -> serverInfo_handler
  1572. - '<prefix><error_prefix> -> msgPrinter_handler
  1573. - '<prefix>/print' -> msgPrinter_handler
  1574. and, if the used Client supports it;
  1575. - '<prefix>/subscribe' -> subscription_handler
  1576. - '<prefix>/unsubscribe' -> subscription_handler
  1577. Note: the given 'error_prefix' argument is also set as default 'error_prefix' for error-messages
  1578. *sent from* this server. This is ok, because error-messages generally do not elicit a reply from the receiver.
  1579. To do this with the serverInfo-prefixes would be a bad idea, because if a request received on '/info' (for example)
  1580. would send replies to '/info', this could potentially cause a never-ending loop of messages!
  1581. Do *not* set the 'info_prefix' here (for incoming serverinfo requests) to the same value as given to
  1582. the setSrvInfoPrefix() method (for *replies* to incoming serverinfo requests).
  1583. For example, use '/info' for incoming requests, and '/inforeply' or '/serverinfo' or even just '/print' as the
  1584. info-reply prefix.
  1585. """
  1586. self.error_prefix = error_prefix
  1587. self.addMsgHandler('default', self.noCallback_handler)
  1588. self.addMsgHandler(prefix + info_prefix, self.serverInfo_handler)
  1589. self.addMsgHandler(prefix + error_prefix, self.msgPrinter_handler)
  1590. self.addMsgHandler(prefix + '/print', self.msgPrinter_handler)
  1591. if isinstance(self.client, OSCMultiClient):
  1592. self.addMsgHandler(prefix + '/subscribe', self.subscription_handler)
  1593. self.addMsgHandler(prefix + '/unsubscribe', self.subscription_handler)
  1594. def printErr(self, txt):
  1595. """Writes 'OSCServer: txt' to sys.stderr
  1596. """
  1597. sys.stderr.write("OSCServer: %s\n" % txt)
  1598. def sendOSCerror(self, txt, client_address):
  1599. """Sends 'txt', encapsulated in an OSCMessage to the default 'error_prefix' OSC-addres.
  1600. Message is sent to the given client_address, with the default 'return_port' overriding
  1601. the client_address' port, if defined.
  1602. """
  1603. lines = txt.split('\n')
  1604. if len(lines) == 1:
  1605. msg = OSCMessage(self.error_prefix)
  1606. msg.append(lines[0])
  1607. elif len(lines) > 1:
  1608. msg = OSCBundle(self.error_prefix)
  1609. for line in lines:
  1610. msg.append(line)
  1611. else:
  1612. return
  1613. if self.return_port:
  1614. client_address = (client_address[0], self.return_port)
  1615. self.client.sendto(msg, client_address)
  1616. def reportErr(self, txt, client_address):
  1617. """Writes 'OSCServer: txt' to sys.stderr
  1618. If self.error_prefix is defined, sends 'txt' as an OSC error-message to the client(s)
  1619. (see printErr() and sendOSCerror())
  1620. """
  1621. self.printErr(txt)
  1622. if len(self.error_prefix):
  1623. self.sendOSCerror(txt, client_address)
  1624. def sendOSCinfo(self, txt, client_address):
  1625. """Sends 'txt', encapsulated in an OSCMessage to the default 'info_prefix' OSC-addres.
  1626. Message is sent to the given client_address, with the default 'return_port' overriding
  1627. the client_address' port, if defined.
  1628. """
  1629. lines = txt.split('\n')
  1630. if len(lines) == 1:
  1631. msg = OSCMessage(self.info_prefix)
  1632. msg.append(lines[0])
  1633. elif len(lines) > 1:
  1634. msg = OSCBundle(self.info_prefix)
  1635. for line in lines:
  1636. msg.append(line)
  1637. else:
  1638. return
  1639. if self.return_port:
  1640. client_address = (client_address[0], self.return_port)
  1641. self.client.sendto(msg, client_address)
  1642. ###
  1643. # Message-Handler callback functions
  1644. ###
  1645. def handle_error(self, request, client_address):
  1646. """Handle an exception in the Server's callbacks gracefully.
  1647. Writes the error to sys.stderr and, if the error_prefix (see setSrvErrorPrefix()) is set,
  1648. sends the error-message as reply to the client
  1649. """
  1650. (e_type, e) = sys.exc_info()[:2]
  1651. self.printErr("%s on request from %s: %s" % (e_type.__name__, getUrlStr(client_address), str(e)))
  1652. if self.print_tracebacks:
  1653. import traceback
  1654. traceback.print_exc() # XXX But this goes to stderr!
  1655. if len(self.error_prefix):
  1656. self.sendOSCerror("%s: %s" % (e_type.__name__, str(e)), client_address)
  1657. def noCallback_handler(self, addr, tags, data, client_address):
  1658. """Example handler for OSCMessages.
  1659. All registerd handlers must accept these three arguments:
  1660. - addr (string): The OSC-address pattern of the received Message
  1661. (the 'addr' string has already been matched against the handler's registerd OSC-address,
  1662. but may contain '*'s & such)
  1663. - tags (string): The OSC-typetags of the received message's arguments. (without the preceding comma)
  1664. - data (list): The OSCMessage's arguments
  1665. Note that len(tags) == len(data)
  1666. - client_address ((host, port) tuple): the host & port this message originated from.
  1667. a Message-handler function may return None, but it could also return an OSCMessage (or OSCBundle),
  1668. which then gets sent back to the client.
  1669. This handler prints a "No callback registered to handle ..." message.
  1670. Returns None
  1671. """
  1672. self.reportErr("No callback registered to handle OSC-address '%s'" % addr, client_address)
  1673. def msgPrinter_handler(self, addr, tags, data, client_address):
  1674. """Example handler for OSCMessages.
  1675. All registerd handlers must accept these three arguments:
  1676. - addr (string): The OSC-address pattern of the received Message
  1677. (the 'addr' string has already been matched against the handler's registerd OSC-address,
  1678. but may contain '*'s & such)
  1679. - tags (string): The OSC-typetags of the received message's arguments. (without the preceding comma)
  1680. - data (list): The OSCMessage's arguments
  1681. Note that len(tags) == len(data)
  1682. - client_address ((host, port) tuple): the host & port this message originated from.
  1683. a Message-handler function may return None, but it could also return an OSCMessage (or OSCBundle),
  1684. which then gets sent back to the client.
  1685. This handler prints the received message.
  1686. Returns None
  1687. """
  1688. txt = "OSCMessage '%s' from %s: " % (addr, getUrlStr(client_address))
  1689. txt += str(data)
  1690. self.printErr(txt) # strip trailing comma & space
  1691. def serverInfo_handler(self, addr, tags, data, client_address):
  1692. """Example handler for OSCMessages.
  1693. All registerd handlers must accept these three arguments:
  1694. - addr (string): The OSC-address pattern of the received Message
  1695. (the 'addr' string has already been matched against the handler's registerd OSC-address,
  1696. but may contain '*'s & such)
  1697. - tags (string): The OSC-typetags of the received message's arguments. (without the preceding comma)
  1698. - data (list): The OSCMessage's arguments
  1699. Note that len(tags) == len(data)
  1700. - client_address ((host, port) tuple): the host & port this message originated from.
  1701. a Message-handler function may return None, but it could also return an OSCMessage (or OSCBundle),
  1702. which then gets sent back to the client.
  1703. This handler returns a reply to the client, which can contain various bits of information
  1704. about this server, depending on the first argument of the received OSC-message:
  1705. - 'help' | 'info' : Reply contains server type & version info, plus a list of
  1706. available 'commands' understood by this handler
  1707. - 'list' | 'ls' : Reply is a bundle of 'address <string>' messages, listing the server's
  1708. OSC address-space.
  1709. - 'clients' | 'targets' : Reply is a bundle of 'target osc://<host>:<port>[<prefix>] [<filter>] [...]'
  1710. messages, listing the local Client-instance's subscribed remote clients.
  1711. """
  1712. if len(data) == 0:
  1713. return None
  1714. cmd = data.pop(0)
  1715. reply = None
  1716. if cmd in ('help', 'info'):
  1717. reply = OSCBundle(self.info_prefix)
  1718. reply.append(('server', str(self)))
  1719. reply.append(('info_command', "ls | list : list OSC address-space"))
  1720. reply.append(('info_command', "clients | targets : list subscribed clients"))
  1721. elif cmd in ('ls', 'list'):
  1722. reply = OSCBundle(self.info_prefix)
  1723. for addr in self.callbacks.keys():
  1724. reply.append(('address', addr))
  1725. elif cmd in ('clients', 'targets'):
  1726. if hasattr(self.client, 'getOSCTargetStrings'):
  1727. reply = OSCBundle(self.info_prefix)
  1728. for trg in self.client.getOSCTargetStrings():
  1729. reply.append(('target',) + trg)
  1730. else:
  1731. cli_addr = self.client.address()
  1732. if cli_addr:
  1733. reply = OSCMessage(self.info_prefix)
  1734. reply.append(('target', "osc://%s/" % getUrlStr(cli_addr)))
  1735. else:
  1736. self.reportErr("unrecognized command '%s' in /info request from osc://%s. Try 'help'" % (cmd, getUrlStr(client_address)), client_address)
  1737. return reply
  1738. def _subscribe(self, data, client_address):
  1739. """Handle the actual subscription. the provided 'data' is concatenated together to form a
  1740. '<host>:<port>[<prefix>] [<filter>] [...]' string, which is then passed to
  1741. parseUrlStr() & parseFilterStr() to actually retreive <host>, <port>, etc.
  1742. This 'long way 'round' approach (almost) guarantees that the subscription works,
  1743. regardless of how the bits of the <url> are encoded in 'data'.
  1744. """
  1745. url = ""
  1746. have_port = False
  1747. for item in data:
  1748. if (type(item) == types.IntType) and not have_port:
  1749. url += ":%d" % item
  1750. have_port = True
  1751. elif type(item) in types.StringTypes:
  1752. url += item
  1753. (addr, tail) = parseUrlStr(url)
  1754. (prefix, filters) = parseFilterStr(tail)
  1755. if addr != None:
  1756. (host, port) = addr
  1757. if not host:
  1758. host = client_address[0]
  1759. if not port:
  1760. port = client_address[1]
  1761. addr = (host, port)
  1762. else:
  1763. addr = client_address
  1764. self.client._setTarget(addr, prefix, filters)
  1765. trg = self.client.getOSCTargetStr(addr)
  1766. if trg[0] != None:
  1767. reply = OSCMessage(self.info_prefix)
  1768. reply.append(('target',) + trg)
  1769. return reply
  1770. def _unsubscribe(self, data, client_address):
  1771. """Handle the actual unsubscription. the provided 'data' is concatenated together to form a
  1772. '<host>:<port>[<prefix>]' string, which is then passed to
  1773. parseUrlStr() to actually retreive <host>, <port> & <prefix>.
  1774. This 'long way 'round' approach (almost) guarantees that the unsubscription works,
  1775. regardless of how the bits of the <url> are encoded in 'data'.
  1776. """
  1777. url = ""
  1778. have_port = False
  1779. for item in data:
  1780. if (type(item) == types.IntType) and not have_port:
  1781. url += ":%d" % item
  1782. have_port = True
  1783. elif type(item) in types.StringTypes:
  1784. url += item
  1785. (addr, _) = parseUrlStr(url)
  1786. if addr == None:
  1787. addr = client_address
  1788. else:
  1789. (host, port) = addr
  1790. if not host:
  1791. host = client_address[0]
  1792. if not port:
  1793. try:
  1794. (host, port) = self.client._searchHostAddr(host)
  1795. except NotSubscribedError:
  1796. port = client_address[1]
  1797. addr = (host, port)
  1798. try:
  1799. self.client._delTarget(addr)
  1800. except NotSubscribedError, e:
  1801. txt = "%s: %s" % (e.__class__.__name__, str(e))
  1802. self.printErr(txt)
  1803. reply = OSCMessage(self.error_prefix)
  1804. reply.append(txt)
  1805. return reply
  1806. def subscription_handler(self, addr, tags, data, client_address):
  1807. """Handle 'subscribe' / 'unsubscribe' requests from remote hosts,
  1808. if the local Client supports this (i.e. OSCMultiClient).
  1809. Supported commands:
  1810. - 'help' | 'info' : Reply contains server type & version info, plus a list of
  1811. available 'commands' understood by this handler
  1812. - 'list' | 'ls' : Reply is a bundle of 'target osc://<host>:<port>[<prefix>] [<filter>] [...]'
  1813. messages, listing the local Client-instance's subscribed remote clients.
  1814. - '[subscribe | listen | sendto | target] <url> [<filter> ...] : Subscribe remote client/server at <url>,
  1815. and/or set message-filters for messages being sent to the subscribed host, with the optional <filter>
  1816. arguments. Filters are given as OSC-addresses (or '*') prefixed by a '+' (send matching messages) or
  1817. a '-' (don't send matching messages). The wildcard '*', '+*' or '+/*' means 'send all' / 'filter none',
  1818. and '-*' or '-/*' means 'send none' / 'filter all' (which is not the same as unsubscribing!)
  1819. Reply is an OSCMessage with the (new) subscription; 'target osc://<host>:<port>[<prefix>] [<filter>] [...]'
  1820. - '[unsubscribe | silence | nosend | deltarget] <url> : Unsubscribe remote client/server at <url>
  1821. If the given <url> isn't subscribed, a NotSubscribedError-message is printed (and possibly sent)
  1822. The <url> given to the subscribe/unsubscribe handler should be of the form:
  1823. '[osc://][<host>][:<port>][<prefix>]', where any or all components can be omitted.
  1824. If <host> is not specified, the IP-address of the message's source is used.
  1825. If <port> is not specified, the <host> is first looked up in the list of subscribed hosts, and if found,
  1826. the associated port is used.
  1827. If <port> is not specified and <host> is not yet subscribed, the message's source-port is used.
  1828. If <prefix> is specified on subscription, <prefix> is prepended to the OSC-address of all messages
  1829. sent to the subscribed host.
  1830. If <prefix> is specified on unsubscription, the subscribed host is only unsubscribed if the host,
  1831. port and prefix all match the subscription.
  1832. If <prefix> is not specified on unsubscription, the subscribed host is unsubscribed if the host and port
  1833. match the subscription.
  1834. """
  1835. if not isinstance(self.client, OSCMultiClient):
  1836. raise OSCServerError("Local %s does not support subsctiptions or message-filtering" % self.client.__class__.__name__)
  1837. addr_cmd = addr.split('/')[-1]
  1838. if len(data):
  1839. if data[0] in ('help', 'info'):
  1840. reply = OSCBundle(self.info_prefix)
  1841. reply.append(('server', str(self)))
  1842. reply.append(('subscribe_command', "ls | list : list subscribed targets"))
  1843. reply.append(('subscribe_command', "[subscribe | listen | sendto | target] <url> [<filter> ...] : subscribe to messages, set filters"))
  1844. reply.append(('subscribe_command', "[unsubscribe | silence | nosend | deltarget] <url> : unsubscribe from messages"))
  1845. return reply
  1846. if data[0] in ('ls', 'list'):
  1847. reply = OSCBundle(self.info_prefix)
  1848. for trg in self.client.getOSCTargetStrings():
  1849. reply.append(('target',) + trg)
  1850. return reply
  1851. if data[0] in ('subscribe', 'listen', 'sendto', 'target'):
  1852. return self._subscribe(data[1:], client_address)
  1853. if data[0] in ('unsubscribe', 'silence', 'nosend', 'deltarget'):
  1854. return self._unsubscribe(data[1:], client_address)
  1855. if addr_cmd in ('subscribe', 'listen', 'sendto', 'target'):
  1856. return self._subscribe(data, client_address)
  1857. if addr_cmd in ('unsubscribe', 'silence', 'nosend', 'deltarget'):
  1858. return self._unsubscribe(data, client_address)
  1859. class ForkingOSCServer(ForkingMixIn, OSCServer):
  1860. """An Asynchronous OSCServer.
  1861. This server forks a new process to handle each incoming request.
  1862. """
  1863. # set the RequestHandlerClass, will be overridden by ForkingOSCServer & ThreadingOSCServer
  1864. RequestHandlerClass = ThreadingOSCRequestHandler
  1865. class ThreadingOSCServer(ThreadingMixIn, OSCServer):
  1866. """An Asynchronous OSCServer.
  1867. This server starts a new thread to handle each incoming request.
  1868. """
  1869. # set the RequestHandlerClass, will be overridden by ForkingOSCServer & ThreadingOSCServer
  1870. RequestHandlerClass = ThreadingOSCRequestHandler
  1871. ######
  1872. #
  1873. # OSCError classes
  1874. #
  1875. ######
  1876. class OSCError(Exception):
  1877. """Base Class for all OSC-related errors
  1878. """
  1879. def __init__(self, message):
  1880. self.message = message
  1881. def __str__(self):
  1882. return self.message
  1883. class OSCClientError(OSCError):
  1884. """Class for all OSCClient errors
  1885. """
  1886. pass
  1887. class OSCServerError(OSCError):
  1888. """Class for all OSCServer errors
  1889. """
  1890. pass
  1891. class NoCallbackError(OSCServerError):
  1892. """This error is raised (by an OSCServer) when an OSCMessage with an 'unmatched' address-pattern
  1893. is received, and no 'default' handler is registered.
  1894. """
  1895. def __init__(self, pattern):
  1896. """The specified 'pattern' should be the OSC-address of the 'unmatched' message causing the error to be raised.
  1897. """
  1898. self.message = "No callback registered to handle OSC-address '%s'" % pattern
  1899. class NotSubscribedError(OSCClientError):
  1900. """This error is raised (by an OSCMultiClient) when an attempt is made to unsubscribe a host
  1901. that isn't subscribed.
  1902. """
  1903. def __init__(self, addr, prefix=None):
  1904. if prefix:
  1905. url = getUrlStr(addr, prefix)
  1906. else:
  1907. url = getUrlStr(addr, '')
  1908. self.message = "Target osc://%s is not subscribed" % url
  1909. ######
  1910. #
  1911. # Testing Program
  1912. #
  1913. ######
  1914. if __name__ == "__main__":
  1915. import optparse
  1916. default_port = 2222
  1917. # define command-line options
  1918. op = optparse.OptionParser(description="OSC.py OpenSoundControl-for-Python Test Program")
  1919. op.add_option("-l", "--listen", dest="listen",
  1920. help="listen on given host[:port]. default = '0.0.0.0:%d'" % default_port)
  1921. op.add_option("-s", "--sendto", dest="sendto",
  1922. help="send to given host[:port]. default = '127.0.0.1:%d'" % default_port)
  1923. op.add_option("-t", "--threading", action="store_true", dest="threading",
  1924. help="Test ThreadingOSCServer")
  1925. op.add_option("-f", "--forking", action="store_true", dest="forking",
  1926. help="Test ForkingOSCServer")
  1927. op.add_option("-u", "--usage", action="help", help="show this help message and exit")
  1928. op.set_defaults(listen=":%d" % default_port)
  1929. op.set_defaults(sendto="")
  1930. op.set_defaults(threading=False)
  1931. op.set_defaults(forking=False)
  1932. # Parse args
  1933. (opts, args) = op.parse_args()
  1934. addr, server_prefix = parseUrlStr(opts.listen)
  1935. if addr != None and addr[0] != None:
  1936. if addr[1] != None:
  1937. listen_address = addr
  1938. else:
  1939. listen_address = (addr[0], default_port)
  1940. else:
  1941. listen_address = ('', default_port)
  1942. targets = {}
  1943. for trg in opts.sendto.split(','):
  1944. (addr, prefix) = parseUrlStr(trg)
  1945. if len(prefix):
  1946. (prefix, filters) = parseFilterStr(prefix)
  1947. else:
  1948. filters = {}
  1949. if addr != None:
  1950. if addr[1] != None:
  1951. targets[addr] = [prefix, filters]
  1952. else:
  1953. targets[(addr[0], listen_address[1])] = [prefix, filters]
  1954. elif len(prefix) or len(filters):
  1955. targets[listen_address] = [prefix, filters]
  1956. welcome = "Welcome to the OSC testing program."
  1957. print welcome
  1958. hexDump(welcome)
  1959. print
  1960. message = OSCMessage()
  1961. message.setAddress("/print")
  1962. message.append(44)
  1963. message.append(11)
  1964. message.append(4.5)
  1965. message.append("the white cliffs of dover")
  1966. print message
  1967. hexDump(message.getBinary())
  1968. print "\nMaking and unmaking a message.."
  1969. strings = OSCMessage("/prin{ce,t}")
  1970. strings.append("Mary had a little lamb")
  1971. strings.append("its fleece was white as snow")
  1972. strings.append("and everywhere that Mary went,")
  1973. strings.append("the lamb was sure to go.")
  1974. strings.append(14.5)
  1975. strings.append(14.5)
  1976. strings.append(-400)
  1977. raw = strings.getBinary()
  1978. print strings
  1979. hexDump(raw)
  1980. print "Retrieving arguments..."
  1981. data = raw
  1982. for i in range(6):
  1983. text, data = _readString(data)
  1984. print text
  1985. number, data = _readFloat(data)
  1986. print number
  1987. number, data = _readFloat(data)
  1988. print number
  1989. number, data = _readInt(data)
  1990. print number
  1991. print decodeOSC(raw)
  1992. print "\nTesting Blob types."
  1993. blob = OSCMessage("/pri*")
  1994. blob.append("","b")
  1995. blob.append("b","b")
  1996. blob.append("bl","b")
  1997. blob.append("blo","b")
  1998. blob.append("blob","b")
  1999. blob.append("blobs","b")
  2000. blob.append(42)
  2001. print blob
  2002. hexDump(blob.getBinary())
  2003. print1 = OSCMessage()
  2004. print1.setAddress("/print")
  2005. print1.append("Hey man, that's cool.")
  2006. print1.append(42)
  2007. print1.append(3.1415926)
  2008. print "\nTesting OSCBundle"
  2009. bundle = OSCBundle()
  2010. bundle.append(print1)
  2011. bundle.append({'addr':"/print", 'args':["bundled messages:", 2]})
  2012. bundle.setAddress("/*print")
  2013. bundle.append(("no,", 3, "actually."))
  2014. print bundle
  2015. hexDump(bundle.getBinary())
  2016. # Instantiate OSCClient
  2017. print "\nInstantiating OSCClient:"
  2018. if len(targets):
  2019. c = OSCMultiClient()
  2020. c.updateOSCTargets(targets)
  2021. else:
  2022. c = OSCClient()
  2023. c.connect(listen_address) # connect back to our OSCServer
  2024. print c
  2025. if hasattr(c, 'getOSCTargetStrings'):
  2026. print "Sending to:"
  2027. for (trg, filterstrings) in c.getOSCTargetStrings():
  2028. out = trg
  2029. for fs in filterstrings:
  2030. out += " %s" % fs
  2031. print out
  2032. # Now an OSCServer...
  2033. print "\nInstantiating OSCServer:"
  2034. # define a message-handler function for the server to call.
  2035. def printing_handler(addr, tags, stuff, source):
  2036. msg_string = "%s [%s] %s" % (addr, tags, str(stuff))
  2037. sys.stdout.write("OSCServer Got: '%s' from %s\n" % (msg_string, getUrlStr(source)))
  2038. # send a reply to the client.
  2039. msg = OSCMessage("/printed")
  2040. msg.append(msg_string)
  2041. return msg
  2042. if opts.threading:
  2043. s = ThreadingOSCServer(listen_address, c, return_port=listen_address[1])
  2044. elif opts.forking:
  2045. s = ForkingOSCServer(listen_address, c, return_port=listen_address[1])
  2046. else:
  2047. s = OSCServer(listen_address, c, return_port=listen_address[1])
  2048. print s
  2049. # Set Server to return errors as OSCMessages to "/error"
  2050. s.setSrvErrorPrefix("/error")
  2051. # Set Server to reply to server-info requests with OSCMessages to "/serverinfo"
  2052. s.setSrvInfoPrefix("/serverinfo")
  2053. # this registers a 'default' handler (for unmatched messages),
  2054. # an /'error' handler, an '/info' handler.
  2055. # And, if the client supports it, a '/subscribe' & '/unsubscribe' handler
  2056. s.addDefaultHandlers()
  2057. s.addMsgHandler("/print", printing_handler)
  2058. # if client & server are bound to 'localhost', server replies return to itself!
  2059. s.addMsgHandler("/printed", s.msgPrinter_handler)
  2060. s.addMsgHandler("/serverinfo", s.msgPrinter_handler)
  2061. print "Registered Callback-functions:"
  2062. for addr in s.getOSCAddressSpace():
  2063. print addr
  2064. print "\nStarting OSCServer. Use ctrl-C to quit."
  2065. st = threading.Thread(target=s.serve_forever)
  2066. st.start()
  2067. if hasattr(c, 'targets') and listen_address not in c.targets.keys():
  2068. print "\nSubscribing local Server to local Client"
  2069. c2 = OSCClient()
  2070. c2.connect(listen_address)
  2071. subreq = OSCMessage("/subscribe")
  2072. subreq.append(listen_address)
  2073. print "sending: ", subreq
  2074. c2.send(subreq)
  2075. c2.close()
  2076. time.sleep(0.1)
  2077. print "\nRequesting OSC-address-space and subscribed clients from OSCServer"
  2078. inforeq = OSCMessage("/info")
  2079. for cmd in ("info", "list", "clients"):
  2080. inforeq.clearData()
  2081. inforeq.append(cmd)
  2082. print "sending: ", inforeq
  2083. c.send(inforeq)
  2084. time.sleep(0.1)
  2085. print2 = print1.copy()
  2086. print2.setAddress('/noprint')
  2087. print "\nSending Messages"
  2088. for m in (message, print1, print2, strings, bundle):
  2089. print "sending: ", m
  2090. c.send(m)
  2091. time.sleep(0.1)
  2092. print "\nThe next message's address will match both the '/print' and '/printed' handlers..."
  2093. print "sending: ", blob
  2094. c.send(blob)
  2095. time.sleep(0.1)
  2096. print "\nBundles can be given a timestamp.\nThe receiving server should 'hold' the bundle until its time has come"
  2097. waitbundle = OSCBundle("/print")
  2098. waitbundle.setTimeTag(time.time() + 5)
  2099. if s.__class__ == OSCServer:
  2100. waitbundle.append("Note how the (single-thread) OSCServer blocks while holding this bundle")
  2101. else:
  2102. waitbundle.append("Note how the %s does not block while holding this bundle" % s.__class__.__name__)
  2103. print "Set timetag 5 s into the future"
  2104. print "sending: ", waitbundle
  2105. c.send(waitbundle)
  2106. time.sleep(0.1)
  2107. print "Recursing bundles, with timetags set to 10 s [25 s, 20 s, 10 s]"
  2108. bb = OSCBundle("/print")
  2109. bb.setTimeTag(time.time() + 10)
  2110. b = OSCBundle("/print")
  2111. b.setTimeTag(time.time() + 25)
  2112. b.append("held for 25 sec")
  2113. bb.append(b)
  2114. b.clearData()
  2115. b.setTimeTag(time.time() + 20)
  2116. b.append("held for 20 sec")
  2117. bb.append(b)
  2118. b.clearData()
  2119. b.setTimeTag(time.time() + 15)
  2120. b.append("held for 15 sec")
  2121. bb.append(b)
  2122. if s.__class__ == OSCServer:
  2123. bb.append("Note how the (single-thread) OSCServer handles the bundle's contents in order of appearance")
  2124. else:
  2125. bb.append("Note how the %s handles the sub-bundles in the order dictated by their timestamps" % s.__class__.__name__)
  2126. bb.append("Each bundle's contents, however, are processed in random order (dictated by the kernel's threading)")
  2127. print "sending: ", bb
  2128. c.send(bb)
  2129. time.sleep(0.1)
  2130. print "\nMessages sent!"
  2131. print "\nWaiting for OSCServer. Use ctrl-C to quit.\n"
  2132. try:
  2133. while True:
  2134. time.sleep(30)
  2135. except KeyboardInterrupt:
  2136. print "\nClosing OSCServer."
  2137. s.close()
  2138. print "Waiting for Server-thread to finish"
  2139. st.join()
  2140. print "Closing OSCClient"
  2141. c.close()
  2142. print "Done"
  2143. sys.exit(0)