aRts audio server
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.

object.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. Copyright (C) 2000 Stefan Westerfeld
  3. stefan@space.twc.de
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef OBJECT_H
  18. #define OBJECT_H
  19. #include "buffer.h"
  20. #include "connection.h"
  21. #include "notification.h"
  22. #include <assert.h>
  23. #include <map>
  24. #include <list>
  25. #include "arts_export.h"
  26. /*
  27. * BC - Status (2002-03-08): Object_base, Object_skel, Object_stub
  28. *
  29. * All of them have to be kept binary compatible carefully, due to interaction
  30. * with generated code. There are d ptrs in _skel and _stub, NOT TO BE USED
  31. * NORMALLY. Normally, do use _internalData instead, as this is much faster
  32. * than creating two d objects per MCOP implementation/stub. Handle with care.
  33. */
  34. namespace Arts {
  35. /* custom dispatching functions */
  36. typedef void (*DispatchFunction)(void *object, Buffer *request, Buffer *result);
  37. typedef void (*OnewayDispatchFunction)(void *object, Buffer *request);
  38. typedef void (*DynamicDispatchFunction)(void *object, long methodID, Buffer *request, Buffer *result);
  39. class ScheduleNode;
  40. class Object_skel;
  41. class Object_stub;
  42. class FlowSystem;
  43. class MethodDef;
  44. class ObjectReference;
  45. class WeakReferenceBase;
  46. class Object;
  47. class ObjectManager;
  48. class DynamicSkeletonData;
  49. class DynamicSkeletonBase;
  50. class ARTS_EXPORT Object_base : public NotificationClient {
  51. private:
  52. friend class DynamicRequest;
  53. friend class ObjectManager;
  54. bool _deleteOk; // ensure that "delete" is not called manually
  55. protected:
  56. /**
  57. * ObjectInternalData contains private data structures for
  58. * - Object_base
  59. * - Object_stub
  60. * - Object_skel
  61. *
  62. * This is an optimization over adding each of them private data pointers,
  63. * which would lead to some more bloat.
  64. */
  65. class ObjectInternalData *_internalData;
  66. struct ObjectStreamInfo;
  67. Object_base();
  68. virtual ~Object_base();
  69. /*
  70. * internal management for streams
  71. */
  72. ScheduleNode *_scheduleNode;
  73. std::list<ObjectStreamInfo *> _streamList;
  74. virtual Object_skel *_skel();
  75. virtual Object_stub *_stub();
  76. enum ObjectLocation { objectIsLocal, objectIsRemote };
  77. virtual ObjectLocation _location() const = 0;
  78. long _objectID;
  79. Connection *_connection;
  80. std::string _internalObjectID; // two objects are "_isEqual" when these match
  81. long _nextNotifyID;
  82. long _refCnt; // reference count
  83. static long _staticObjectCount;
  84. void _destroy(); // use this instead of delete (takes care of
  85. // properly removing flow system node)
  86. public:
  87. static unsigned long _IID; // interface ID
  88. /**
  89. * custom messaging: these can be used to send a custom data to other
  90. * objects. Warning: these are *not* usable for local objects. You may
  91. * only use these functions if you know that you are talking to a remote
  92. * object. Use _allocCustomMessage to allocate a message. Put the data
  93. * you want to send in the Buffer. After that, call _sendCustomMessage.
  94. * Don't free the buffer - this will happen automatically.
  95. */
  96. virtual Buffer *_allocCustomMessage(long handlerID);
  97. virtual void _sendCustomMessage(Buffer *data);
  98. /*
  99. * generic capabilities, which allow find out what you can do with an
  100. * object even if you don't know it's interface
  101. */
  102. virtual long _lookupMethod(const Arts::MethodDef &) = 0;
  103. virtual std::string _interfaceName() = 0;
  104. virtual class InterfaceDef _queryInterface(const std::string& name) = 0;
  105. virtual class TypeDef _queryType(const std::string& name) = 0;
  106. virtual class EnumDef _queryEnum(const std::string& name) = 0;
  107. virtual std::string _toString() = 0;
  108. /*
  109. * stuff for streaming (put in a seperate interface?)
  110. */
  111. virtual void calculateBlock(unsigned long cycles);
  112. ScheduleNode *_node();
  113. virtual FlowSystem _flowSystem() = 0;
  114. /*
  115. * reference counting
  116. */
  117. virtual void _release() = 0;
  118. virtual void _copyRemote() = 0;
  119. virtual void _useRemote() = 0;
  120. virtual void _releaseRemote() = 0;
  121. // BC issue: added _cancelCopyRemote here to avoid virtual function
  122. void _cancelCopyRemote();
  123. void _addWeakReference(WeakReferenceBase *reference);
  124. void _removeWeakReference(WeakReferenceBase *reference);
  125. inline Object_base *_copy() {
  126. assert(_refCnt > 0);
  127. _refCnt++;
  128. return this;
  129. }
  130. // Default I/O info
  131. virtual std::vector<std::string> _defaultPortsIn() const;
  132. virtual std::vector<std::string> _defaultPortsOut() const;
  133. // cast operation
  134. virtual void *_cast(unsigned long iid);
  135. void *_cast(const std::string& interface);
  136. // Run-time type compatibility check
  137. virtual bool _isCompatibleWith(const std::string& interfacename) = 0;
  138. // Aggregation
  139. virtual std::string _addChild(Arts::Object child, const std::string& name) = 0;
  140. virtual bool _removeChild(const std::string& name) = 0;
  141. virtual Arts::Object _getChild(const std::string& name) = 0;
  142. virtual std::vector<std::string> * _queryChildren() = 0;
  143. /*
  144. * when this is true, a fatal communication error has occurred (of course
  145. * only possible for remote objects) - maybe your returncode is invalid,
  146. * maybe your last invocation didn't succeed...
  147. */
  148. virtual bool _error();
  149. inline static long _objectCount() { return _staticObjectCount; }
  150. inline long _mkNotifyID() { return _nextNotifyID++; }
  151. // object creation
  152. static Object_base *_create(const std::string& subClass = "Object");
  153. // comparison
  154. bool _isEqual(Object_base *object) const;
  155. // static converter (from reference)
  156. static Object_base *_fromString(const std::string& objectref);
  157. static Object_base *_fromReference(class ObjectReference ref, bool needcopy);
  158. };
  159. /*
  160. * Dispatching
  161. */
  162. class Buffer;
  163. class MethodDef;
  164. class Object_skel_private;
  165. class AnyConstRef;
  166. class AttributeDef;
  167. class ARTS_EXPORT Object_skel : virtual public Object_base {
  168. private:
  169. friend class Object_base;
  170. friend class DynamicSkeletonData;
  171. friend class DynamicSkeletonBase;
  172. Object_skel_private *_d_skel;// do not use until there is a very big problem
  173. // reference counting - remote object watching
  174. long _remoteSendCount; // don't kill objects just sent to other server
  175. bool _remoteSendUpdated; // timeout if they don't want the object
  176. std::list<class Connection *> _remoteUsers; // who is using it?
  177. protected:
  178. void _addMethod(DispatchFunction disp, void *object, const MethodDef& md);
  179. void _addMethod(OnewayDispatchFunction disp, void *object,
  180. const MethodDef& md);
  181. void _addMethod(DynamicDispatchFunction disp, void *object,
  182. const MethodDef& md);
  183. void _initStream(const std::string& name, void *ptr, long flags);
  184. /** stuff relative to attribute notifications **/
  185. bool _initAttribute(const Arts::AttributeDef& attribute);
  186. static bool _QueryInitStreamFunc(Object_skel *skel,const std::string& name);
  187. bool _generateSlots(const std::string& name, const std::string& interface);
  188. /** for DynamicSkeleton: **/
  189. const MethodDef& _dsGetMethodDef(long methodID);
  190. protected:
  191. void _defaultNotify(const Notification& notification);
  192. void notify(const Notification& notification);
  193. void _emit_changed(const char *stream, const AnyConstRef& value);
  194. /**
  195. * custom messaging: this is used to install a custom data handler that
  196. * can be used to receive non-standard messages
  197. */
  198. long _addCustomMessageHandler(OnewayDispatchFunction handler, void *object);
  199. Object_skel *_skel();
  200. ObjectLocation _location() const;
  201. public:
  202. Object_skel();
  203. virtual ~Object_skel();
  204. // reference counting connection drop
  205. void _disconnectRemote(class Connection *connection);
  206. void _referenceClean();
  207. // synchronous & asynchronous dispatching
  208. void _dispatch(Buffer *request, Buffer *result,long methodID);
  209. void _dispatch(Buffer *request, long methodID);
  210. long _lookupMethod(const MethodDef &);
  211. /*
  212. * standard interface for every object skeleton
  213. */
  214. static std::string _interfaceNameSkel();
  215. virtual void _buildMethodTable();
  216. /*
  217. * reference counting
  218. */
  219. virtual void _release();
  220. virtual void _copyRemote();
  221. virtual void _useRemote();
  222. virtual void _releaseRemote();
  223. /*
  224. * streaming
  225. */
  226. FlowSystem _flowSystem();
  227. /*
  228. * to inspect the (remote) object interface
  229. */
  230. virtual std::string _interfaceName();
  231. InterfaceDef _queryInterface(const std::string& name);
  232. TypeDef _queryType(const std::string& name);
  233. EnumDef _queryEnum(const std::string& name);
  234. virtual std::string _toString();
  235. // Run-time type compatibility check
  236. bool _isCompatibleWith(const std::string& interfacename);
  237. // Aggregation
  238. std::string _addChild(Arts::Object child, const std::string& name);
  239. bool _removeChild(const std::string& name);
  240. Arts::Object _getChild(const std::string& name);
  241. std::vector<std::string> * _queryChildren();
  242. };
  243. class Object_stub_private;
  244. class ARTS_EXPORT Object_stub : virtual public Object_base {
  245. private:
  246. friend class Object_base;
  247. Object_stub_private *_d_stub;// do not use until there is a very big problem
  248. protected:
  249. long _lookupCacheRandom;
  250. Object_stub();
  251. Object_stub(Connection *connection, long objectID);
  252. virtual ~Object_stub();
  253. virtual Object_stub *_stub();
  254. ObjectLocation _location() const;
  255. enum { _lookupMethodCacheSize = 337 };
  256. static struct methodCacheEntry {
  257. methodCacheEntry() : obj(NULL),method(NULL),ID(0) {} ;
  258. Object_stub *obj;
  259. const char *method;
  260. long ID;
  261. } *_lookupMethodCache;
  262. long _lookupMethodFast(const char *method);
  263. long _lookupMethod(const MethodDef &);
  264. public:
  265. /*
  266. * custom messaging
  267. */
  268. Buffer *_allocCustomMessage(long handlerID);
  269. void _sendCustomMessage(Buffer *data);
  270. /*
  271. * to inspect the (remote) object interface
  272. */
  273. std::string _interfaceName();
  274. InterfaceDef _queryInterface(const std::string& name);
  275. TypeDef _queryType(const std::string& name);
  276. EnumDef _queryEnum(const std::string& name);
  277. std::string _toString();
  278. /*
  279. * streaming
  280. */
  281. FlowSystem _flowSystem();
  282. /*
  283. * reference counting
  284. */
  285. virtual void _release();
  286. virtual void _copyRemote();
  287. virtual void _useRemote();
  288. virtual void _releaseRemote();
  289. // Run-time type compatibility check
  290. bool _isCompatibleWith(const std::string& interfacename);
  291. // Aggregation
  292. std::string _addChild(Arts::Object child, const std::string& name);
  293. bool _removeChild(const std::string& name);
  294. Arts::Object _getChild(const std::string& name);
  295. std::vector<std::string> * _queryChildren();
  296. /*
  297. * communication error? this is true when your connection to the remote
  298. * object is lost (e.g. when the remote server crashed) - your return
  299. * values are then undefined, so check this before relying too much
  300. * on some invocation
  301. */
  302. bool _error();
  303. /*
  304. * global cleanup
  305. */
  306. static void _cleanupMethodCache();
  307. };
  308. }
  309. #endif