2 oscpack -- Open Sound Control packet manipulation library
3 http://www.audiomulch.com/~rossb/oscpack
5 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>
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:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 Any person wishing to distribute modifications to the Software is
19 requested to send the modifications to the original developer so that
20 they can be incorporated into the canonical version.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include "OscReceiveTest.h"
36 #include "osc/OscReceivedElements.h"
38 #include "ip/UdpSocket.h"
39 #include "osc/OscPacketListener.h"
44 class OscReceiveTestPacketListener
: public OscPacketListener
{
47 void ProcessMessage( const osc::ReceivedMessage
& m
, const IpEndpointName
& remoteEndpoint
)
49 // a more complex scheme involving std::map or some other method of
50 // processing address patterns could be used here
51 // (see MessageMappingOscPacketListener.h for example). however, the main
52 // purpose of this example is to illustrate and test different argument
56 // argument stream, and argument iterator, used in different
58 ReceivedMessageArgumentStream args
= m
.ArgumentStream();
59 ReceivedMessage::const_iterator arg
= m
.ArgumentsBegin();
61 if( strcmp( m
.AddressPattern(), "/test1" ) == 0 ){
64 // parse an expected format using the argument stream interface:
69 args
>> a1
>> a2
>> a3
>> a4
>> osc::EndMessage
;
71 std::cout
<< "received '/test1' message with arguments: "
72 << a1
<< " " << a2
<< " " << a3
<< " " << a4
<< "\n";
74 }else if( strcmp( m
.AddressPattern(), "/test2" ) == 0 ){
77 // parse an expected format using the argument iterator interface
78 // this is a more complicated example of doing the same thing
80 bool a1
= (arg
++)->AsBool();
81 int a2
= (arg
++)->AsInt32();
82 float a3
= (arg
++)->AsFloat();
83 const char *a4
= (arg
++)->AsString();
84 if( arg
!= m
.ArgumentsEnd() )
85 throw ExcessArgumentException();
87 std::cout
<< "received '/test2' message with arguments: "
88 << a1
<< " " << a2
<< " " << a3
<< " " << a4
<< "\n";
90 }else if( strcmp( m
.AddressPattern(), "/test3" ) == 0 ){
93 // parse a variable argument format using the argument iterator
94 // interface. this is where it is necessary to use
95 // argument iterators instead of streams.
96 // When messages may contain arguments of varying type, you can
97 // use the argument iterator interface to query the types at
98 // runtime. this is more flexible that the argument stream
99 // interface, which requires each argument to have a fixed type
102 bool a
= (arg
++)->AsBoolUnchecked();
103 std::cout
<< "received '/test3' message with bool argument: "
105 }else if( arg
->IsInt32() ){
106 int a
= (arg
++)->AsInt32Unchecked();
107 std::cout
<< "received '/test3' message with int32 argument: "
109 }else if( arg
->IsFloat() ){
110 float a
= (arg
++)->AsFloatUnchecked();
111 std::cout
<< "received '/test3' message with float argument: "
113 }else if( arg
->IsString() ){
114 const char *a
= (arg
++)->AsStringUnchecked();
115 std::cout
<< "received '/test3' message with string argument: '"
118 std::cout
<< "received '/test3' message with unexpected argument type\n";
121 if( arg
!= m
.ArgumentsEnd() )
122 throw ExcessArgumentException();
125 }else if( strcmp( m
.AddressPattern(), "/no_arguments" ) == 0 ){
127 args
>> osc::EndMessage
;
128 std::cout
<< "received '/no_arguments' message\n";
130 }else if( strcmp( m
.AddressPattern(), "/a_bool" ) == 0 ){
133 args
>> a
>> osc::EndMessage
;
134 std::cout
<< "received '/a_bool' message: " << a
<< "\n";
136 }else if( strcmp( m
.AddressPattern(), "/nil" ) == 0 ){
138 std::cout
<< "received '/nil' message\n";
140 }else if( strcmp( m
.AddressPattern(), "/inf" ) == 0 ){
142 std::cout
<< "received '/inf' message\n";
144 }else if( strcmp( m
.AddressPattern(), "/an_int" ) == 0 ){
147 args
>> a
>> osc::EndMessage
;
148 std::cout
<< "received '/an_int' message: " << a
<< "\n";
150 }else if( strcmp( m
.AddressPattern(), "/a_float" ) == 0 ){
153 args
>> a
>> osc::EndMessage
;
154 std::cout
<< "received '/a_float' message: " << a
<< "\n";
156 }else if( strcmp( m
.AddressPattern(), "/a_char" ) == 0 ){
159 args
>> a
>> osc::EndMessage
;
162 std::cout
<< "received '/a_char' message: '" << s
<< "'\n";
164 }else if( strcmp( m
.AddressPattern(), "/an_rgba_color" ) == 0 ){
167 args
>> a
>> osc::EndMessage
;
168 std::cout
<< "received '/an_rgba_color' message: " << a
.value
<< "\n";
170 }else if( strcmp( m
.AddressPattern(), "/a_midi_message" ) == 0 ){
173 args
>> a
>> osc::EndMessage
;
174 std::cout
<< "received '/a_midi_message' message: " << a
.value
<< "\n";
176 }else if( strcmp( m
.AddressPattern(), "/an_int64" ) == 0 ){
179 args
>> a
>> osc::EndMessage
;
180 std::cout
<< "received '/an_int64' message: " << a
<< "\n";
182 }else if( strcmp( m
.AddressPattern(), "/a_time_tag" ) == 0 ){
185 args
>> a
>> osc::EndMessage
;
186 std::cout
<< "received '/a_time_tag' message: " << a
.value
<< "\n";
188 }else if( strcmp( m
.AddressPattern(), "/a_double" ) == 0 ){
191 args
>> a
>> osc::EndMessage
;
192 std::cout
<< "received '/a_double' message: " << a
<< "\n";
194 }else if( strcmp( m
.AddressPattern(), "/a_string" ) == 0 ){
197 args
>> a
>> osc::EndMessage
;
198 std::cout
<< "received '/a_string' message: '" << a
<< "'\n";
200 }else if( strcmp( m
.AddressPattern(), "/a_symbol" ) == 0 ){
203 args
>> a
>> osc::EndMessage
;
204 std::cout
<< "received '/a_symbol' message: '" << a
.value
<< "'\n";
206 }else if( strcmp( m
.AddressPattern(), "/a_blob" ) == 0 ){
209 args
>> a
>> osc::EndMessage
;
210 std::cout
<< "received '/a_blob' message\n";
213 std::cout
<< "unrecognised address pattern: "
214 << m
.AddressPattern() << "\n";
217 }catch( Exception
& e
){
218 std::cout
<< "error while parsing message: "
219 << m
.AddressPattern() << ": " << e
.what() << "\n";
225 void RunReceiveTest( int port
)
227 osc::OscReceiveTestPacketListener listener
;
228 UdpListeningReceiveSocket
s(
229 IpEndpointName( IpEndpointName::ANY_ADDRESS
, port
),
232 std::cout
<< "listening for input on port " << port
<< "...\n";
233 std::cout
<< "press ctrl-c to end\n";
237 std::cout
<< "finishing.\n";
242 #ifndef NO_OSC_TEST_MAIN
244 int main(int argc
, char* argv
[])
246 if( argc
>= 2 && strcmp( argv
[1], "-h" ) == 0 ){
247 std::cout
<< "usage: OscReceiveTest [port]\n";
254 port
= atoi( argv
[1] );
256 osc::RunReceiveTest( port
);
261 #endif /* NO_OSC_TEST_MAIN */