00001 /* 00002 * WOscLib, an object oriented OSC library. 00003 * Copyright (C) 2005 Uli Clemens Franke, Weiss Engineering LTD, Switzerland. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * For details see lgpl.txt 00020 * 00021 * Weiss Engineering LTD. 00022 * Florastrass 42 00023 * 8610 Uster 00024 * Switzerland 00025 * 00026 * uli.franke@weiss.ch 00027 */ 00028 00029 /** WOscReceiver header file. 00030 * \file 00031 * 00032 * $Author: cls-nebadje $ ( \ref _UcfWOscLib ) 00033 * $Date: 2006-08-12 20:02:28 $ 00034 * $Revision: 1.6 $ 00035 * 00036 * Copyright (c) Weiss Engineering Ltd 00037 * 00038 */ 00039 #ifndef __WOSCRECEIVER_H__ 00040 #define __WOSCRECEIVER_H__ 00041 00042 00043 #include "WOscContainer.h" 00044 #include "WOscPriorityQueue.h" 00045 #include "WOscException.h" 00046 #include "WOscBundle.h" 00047 #include "WOscLib.h" 00048 00049 /** An OSC-receiving-system. 00050 * The WOscReceiver-class is a complete system for processing 00051 * incoming OSC-data (i.e. bundles or messages). It queues 00052 * bundles, which have to be delayed because of their time-tags, 00053 * unbundles bundles, passes the correct return addresses to the 00054 * methods or errorhandlers. 00055 * 00056 * The address space can be set dynamically, 00057 * new methods/containers can be added/removed. 00058 * 00059 * The receiver can be wrapped into a thread easily: 00060 * Poll periodically for network packets while calling the 00061 * priority-queue management function ( managePriorityQueue() ). 00062 * When receiving a network packet, pass it to the 00063 * networkReceive(char* data, int dataLen, WOscNetReturn *networkReturnAddress) 00064 * function. 00065 * 00066 * \remarks 00067 * Override the networkSend(char* data, int dataLen, WOscNetReturn *networkReturnAddress) 00068 * function for providing a network return-path and a handler 00069 * for defective packets, 00070 * handleOffendingPackets(char* data, int dataLen, WOscNetReturn *networkReturnAddress) 00071 * 00072 * \see 00073 * None. 00074 */ 00075 00076 #if WOSC_USE_PRIORITY_QUEUE == 1 00077 class WOscReceiver: private WOscPriorityQueue{ 00078 #else 00079 class WOscReceiver{ 00080 #endif // #if WOSC_USE_PRIORITY_QUEUE == 1 00081 00082 public: 00083 #if WOSC_USE_PRIORITY_QUEUE == 1 00084 WOscReceiver(int initialQueueCapacity = DEFAULT_QUEUE_CAPACITY, WOscSystemTime* systemTime = NULL); 00085 #else 00086 WOscReceiver(WOscSystemTime* systemTime = NULL); 00087 #endif // #if WOSC_USE_PRIORITY_QUEUE == 1 00088 00089 virtual ~WOscReceiver(); 00090 00091 void SetAddressSpace(WOscContainer* addrSpaceRootContainer); 00092 WOscContainer* GetAddressSpace(); 00093 00094 void NetworkReceive(const char* data, int dataLen, WOscNetReturn* networkReturnAddress); 00095 #if WOSC_USE_PRIORITY_QUEUE == 1 00096 void ManagePriorityQueue(); 00097 #endif 00098 00099 00100 /** Virtual function which has to be overridden by the user. 00101 * It then can be used to send OSC messages from within WOscReceiverMethod 00102 * message callbacks. It is the interface to the network layer for sending 00103 * messages. 00104 * 00105 * \param data 00106 * Pointer to buffer containning the data to be sent. 00107 * 00108 * \param dataLen 00109 * Length of the data in the buffer. 00110 * 00111 * \param networkReturnAddress 00112 * Destination network address for the data. 00113 * 00114 */ 00115 virtual void NetworkSend(const char* data, int dataLen, const WOscNetReturn* networkReturnAddress) = 0; 00116 00117 /** Constants related to a WOscReceiver. 00118 */ 00119 enum Constants{ 00120 DEFAULT_QUEUE_CAPACITY = 32, /**< Default capacity of the priority queue.*/ 00121 }; 00122 00123 protected: 00124 00125 /** Handler for packets which caused either an internal exception or another 00126 * error. The user can decide what to do with such packets by 00127 * overriding this function. 00128 * 00129 * \param data 00130 * Pointer to the buffer containing the offending packet data. 00131 * 00132 * \param dataLen 00133 * Lenght of the buffer containg the offending packet data. 00134 * 00135 * \param exception 00136 * Exception which has been generated during processing this packet. 00137 * 00138 */ 00139 virtual void HandleOffendingPackets(const char* const data, int dataLen, const WOscException& exception) = 0; 00140 /** Handler for OSC messages which didn't match any address in the OSC 00141 * address space. The user can define an own action by overriding this 00142 * pure virtual function. 00143 * 00144 * \param msg 00145 * Pointer to Message which hasn't been processed. 00146 * 00147 * \param networkReturnAddress 00148 * Network address of the sender of this message. 00149 * 00150 */ 00151 virtual void HandleNonmatchedMessages(const WOscMessage* msg, const WOscNetReturn* networkReturnAddress) = 0; 00152 00153 private: 00154 void ProcessBundle(const char* data, unsigned int len, WOscNetReturn* networkReturnAddress); 00155 void ProcessMessage(const char* data, unsigned int len, const WOscNetReturn* networkReturnAddress); 00156 00157 WOscContainer* m_addrSpaceRootContainer; /**< The address space root container.*/ 00158 WOscSystemTime* m_systemTime; /**< The system-time generator. */ 00159 /** The callback list of methods which match a certain message. The object 00160 * is cached here to avoid reallocation for each message dispatch. The size 00161 * of the vector is kept. Reallocation happens only if capacity is low. 00162 */ 00163 WOscCallbackList m_callbackList; 00164 /** An example of how to use WOscReceiver. 00165 * \example WOscServer.cpp 00166 * An example of how to use WOscReceiver is shown here. 00167 * The header (WOscServer.h) prepended: 00168 * \include WOscServer.h 00169 */ 00170 }; 00171 00172 #endif // #ifndef __WOSCRECEIVER_H__