comparison oscpack/tests/OscReceiveTest.cpp @ 76:0ae87af84e2f

added oscgroups
author Rob Canning <rob@foo.net>
date Sun, 13 Jul 2014 10:07:41 +0100
parents
children
comparison
equal deleted inserted replaced
75:3a2845e3156e 76:0ae87af84e2f
1 /*
2 oscpack -- Open Sound Control (OSC) packet manipulation library
3 http://www.rossbencina.com/code/oscpack
4
5 Copyright (c) 2004-2013 Ross Bencina <rossb@audiomulch.com>
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files
9 (the "Software"), to deal in the Software without restriction,
10 including without limitation the rights to use, copy, modify, merge,
11 publish, distribute, sublicense, and/or sell copies of the Software,
12 and to permit persons to whom the Software is furnished to do so,
13 subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 /*
28 The text above constitutes the entire oscpack license; however,
29 the oscpack developer(s) also make the following non-binding requests:
30
31 Any person wishing to distribute modifications to the Software is
32 requested to send the modifications to the original developer so that
33 they can be incorporated into the canonical version. It is also
34 requested that these non-binding requests be included whenever the
35 above license is reproduced.
36 */
37 #include "OscReceiveTest.h"
38
39 #include <cstdlib>
40 #include <cstring>
41 #include <iostream>
42
43 #if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug
44 namespace std {
45 using ::__strcmp__; // avoid error: E2316 '__strcmp__' is not a member of 'std'.
46 }
47 #endif
48
49 #include "osc/OscReceivedElements.h"
50
51 #include "ip/UdpSocket.h"
52 #include "osc/OscPacketListener.h"
53
54
55 namespace osc{
56
57 class OscReceiveTestPacketListener : public OscPacketListener{
58 protected:
59
60 void ProcessMessage( const osc::ReceivedMessage& m,
61 const IpEndpointName& remoteEndpoint )
62 {
63 (void) remoteEndpoint; // suppress unused parameter warning
64
65 // a more complex scheme involving std::map or some other method of
66 // processing address patterns could be used here
67 // (see MessageMappingOscPacketListener.h for example). however, the main
68 // purpose of this example is to illustrate and test different argument
69 // parsing methods
70
71 try {
72 // argument stream, and argument iterator, used in different
73 // examples below.
74 ReceivedMessageArgumentStream args = m.ArgumentStream();
75 ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
76
77 if( std::strcmp( m.AddressPattern(), "/test1" ) == 0 ){
78
79 // example #1:
80 // parse an expected format using the argument stream interface:
81 bool a1;
82 osc::int32 a2;
83 float a3;
84 const char *a4;
85 args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage;
86
87 std::cout << "received '/test1' message with arguments: "
88 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
89
90 }else if( std::strcmp( m.AddressPattern(), "/test2" ) == 0 ){
91
92 // example #2:
93 // parse an expected format using the argument iterator interface
94 // this is a more complicated example of doing the same thing
95 // as above.
96 bool a1 = (arg++)->AsBool();
97 int a2 = (arg++)->AsInt32();
98 float a3 = (arg++)->AsFloat();
99 const char *a4 = (arg++)->AsString();
100 if( arg != m.ArgumentsEnd() )
101 throw ExcessArgumentException();
102
103 std::cout << "received '/test2' message with arguments: "
104 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
105
106 }else if( std::strcmp( m.AddressPattern(), "/test3" ) == 0 ){
107
108 // example #3:
109 // parse a variable argument format using the argument iterator
110 // interface. this is where it is necessary to use
111 // argument iterators instead of streams.
112 // When messages may contain arguments of varying type, you can
113 // use the argument iterator interface to query the types at
114 // runtime. this is more flexible that the argument stream
115 // interface, which requires each argument to have a fixed type
116
117 if( arg->IsBool() ){
118 bool a = (arg++)->AsBoolUnchecked();
119 std::cout << "received '/test3' message with bool argument: "
120 << a << "\n";
121 }else if( arg->IsInt32() ){
122 int a = (arg++)->AsInt32Unchecked();
123 std::cout << "received '/test3' message with int32 argument: "
124 << a << "\n";
125 }else if( arg->IsFloat() ){
126 float a = (arg++)->AsFloatUnchecked();
127 std::cout << "received '/test3' message with float argument: "
128 << a << "\n";
129 }else if( arg->IsString() ){
130 const char *a = (arg++)->AsStringUnchecked();
131 std::cout << "received '/test3' message with string argument: '"
132 << a << "'\n";
133 }else{
134 std::cout << "received '/test3' message with unexpected argument type\n";
135 }
136
137 if( arg != m.ArgumentsEnd() )
138 throw ExcessArgumentException();
139
140
141 }else if( std::strcmp( m.AddressPattern(), "/no_arguments" ) == 0 ){
142
143 args >> osc::EndMessage;
144 std::cout << "received '/no_arguments' message\n";
145
146 }else if( std::strcmp( m.AddressPattern(), "/a_bool" ) == 0 ){
147
148 bool a;
149 args >> a >> osc::EndMessage;
150 std::cout << "received '/a_bool' message: " << a << "\n";
151
152 }else if( std::strcmp( m.AddressPattern(), "/nil" ) == 0 ){
153
154 std::cout << "received '/nil' message\n";
155
156 }else if( std::strcmp( m.AddressPattern(), "/inf" ) == 0 ){
157
158 std::cout << "received '/inf' message\n";
159
160 }else if( std::strcmp( m.AddressPattern(), "/an_int" ) == 0 ){
161
162 osc::int32 a;
163 args >> a >> osc::EndMessage;
164 std::cout << "received '/an_int' message: " << a << "\n";
165
166 }else if( std::strcmp( m.AddressPattern(), "/a_float" ) == 0 ){
167
168 float a;
169 args >> a >> osc::EndMessage;
170 std::cout << "received '/a_float' message: " << a << "\n";
171
172 }else if( std::strcmp( m.AddressPattern(), "/a_char" ) == 0 ){
173
174 char a;
175 args >> a >> osc::EndMessage;
176 char s[2] = {0};
177 s[0] = a;
178 std::cout << "received '/a_char' message: '" << s << "'\n";
179
180 }else if( std::strcmp( m.AddressPattern(), "/an_rgba_color" ) == 0 ){
181
182 osc::RgbaColor a;
183 args >> a >> osc::EndMessage;
184 std::cout << "received '/an_rgba_color' message: " << a.value << "\n";
185
186 }else if( std::strcmp( m.AddressPattern(), "/a_midi_message" ) == 0 ){
187
188 osc::MidiMessage a;
189 args >> a >> osc::EndMessage;
190 std::cout << "received '/a_midi_message' message: " << a.value << "\n";
191
192 }else if( std::strcmp( m.AddressPattern(), "/an_int64" ) == 0 ){
193
194 osc::int64 a;
195 args >> a >> osc::EndMessage;
196 std::cout << "received '/an_int64' message: " << a << "\n";
197
198 }else if( std::strcmp( m.AddressPattern(), "/a_time_tag" ) == 0 ){
199
200 osc::TimeTag a;
201 args >> a >> osc::EndMessage;
202 std::cout << "received '/a_time_tag' message: " << a.value << "\n";
203
204 }else if( std::strcmp( m.AddressPattern(), "/a_double" ) == 0 ){
205
206 double a;
207 args >> a >> osc::EndMessage;
208 std::cout << "received '/a_double' message: " << a << "\n";
209
210 }else if( std::strcmp( m.AddressPattern(), "/a_string" ) == 0 ){
211
212 const char *a;
213 args >> a >> osc::EndMessage;
214 std::cout << "received '/a_string' message: '" << a << "'\n";
215
216 }else if( std::strcmp( m.AddressPattern(), "/a_symbol" ) == 0 ){
217
218 osc::Symbol a;
219 args >> a >> osc::EndMessage;
220 std::cout << "received '/a_symbol' message: '" << a.value << "'\n";
221
222 }else if( std::strcmp( m.AddressPattern(), "/a_blob" ) == 0 ){
223
224 osc::Blob a;
225 args >> a >> osc::EndMessage;
226 std::cout << "received '/a_blob' message\n";
227
228 }else{
229 std::cout << "unrecognised address pattern: "
230 << m.AddressPattern() << "\n";
231 }
232
233 }catch( Exception& e ){
234 std::cout << "error while parsing message: "
235 << m.AddressPattern() << ": " << e.what() << "\n";
236 }
237 }
238 };
239
240
241 void RunReceiveTest( int port )
242 {
243 osc::OscReceiveTestPacketListener listener;
244 UdpListeningReceiveSocket s(
245 IpEndpointName( IpEndpointName::ANY_ADDRESS, port ),
246 &listener );
247
248 std::cout << "listening for input on port " << port << "...\n";
249 std::cout << "press ctrl-c to end\n";
250
251 s.RunUntilSigInt();
252
253 std::cout << "finishing.\n";
254 }
255
256 } // namespace osc
257
258 #ifndef NO_OSC_TEST_MAIN
259
260 int main(int argc, char* argv[])
261 {
262 if( argc >= 2 && std::strcmp( argv[1], "-h" ) == 0 ){
263 std::cout << "usage: OscReceiveTest [port]\n";
264 return 0;
265 }
266
267 int port = 7000;
268
269 if( argc >= 2 )
270 port = std::atoi( argv[1] );
271
272 osc::RunReceiveTest( port );
273
274 return 0;
275 }
276
277 #endif /* NO_OSC_TEST_MAIN */
278