Readme and Install files lifting.
[Faustine.git] / interpretor / faust-0.9.47mr3 / architecture / osclib / oscpack / tests / OscReceiveTest.cpp
1 /*
2 oscpack -- Open Sound Control packet manipulation library
3 http://www.audiomulch.com/~rossb/oscpack
4
5 Copyright (c) 2004-2005 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 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.
21
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.
29 */
30 #include "OscReceiveTest.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <iostream>
35
36 #include "osc/OscReceivedElements.h"
37
38 #include "ip/UdpSocket.h"
39 #include "osc/OscPacketListener.h"
40
41
42 namespace osc{
43
44 class OscReceiveTestPacketListener : public OscPacketListener{
45 protected:
46
47 void ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint )
48 {
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
53 // parsing methods
54
55 try {
56 // argument stream, and argument iterator, used in different
57 // examples below.
58 ReceivedMessageArgumentStream args = m.ArgumentStream();
59 ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
60
61 if( strcmp( m.AddressPattern(), "/test1" ) == 0 ){
62
63 // example #1:
64 // parse an expected format using the argument stream interface:
65 bool a1;
66 osc::int32 a2;
67 float a3;
68 const char *a4;
69 args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage;
70
71 std::cout << "received '/test1' message with arguments: "
72 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
73
74 }else if( strcmp( m.AddressPattern(), "/test2" ) == 0 ){
75
76 // example #2:
77 // parse an expected format using the argument iterator interface
78 // this is a more complicated example of doing the same thing
79 // as above.
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();
86
87 std::cout << "received '/test2' message with arguments: "
88 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
89
90 }else if( strcmp( m.AddressPattern(), "/test3" ) == 0 ){
91
92 // example #3:
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
100
101 if( arg->IsBool() ){
102 bool a = (arg++)->AsBoolUnchecked();
103 std::cout << "received '/test3' message with bool argument: "
104 << a << "\n";
105 }else if( arg->IsInt32() ){
106 int a = (arg++)->AsInt32Unchecked();
107 std::cout << "received '/test3' message with int32 argument: "
108 << a << "\n";
109 }else if( arg->IsFloat() ){
110 float a = (arg++)->AsFloatUnchecked();
111 std::cout << "received '/test3' message with float argument: "
112 << a << "\n";
113 }else if( arg->IsString() ){
114 const char *a = (arg++)->AsStringUnchecked();
115 std::cout << "received '/test3' message with string argument: '"
116 << a << "'\n";
117 }else{
118 std::cout << "received '/test3' message with unexpected argument type\n";
119 }
120
121 if( arg != m.ArgumentsEnd() )
122 throw ExcessArgumentException();
123
124
125 }else if( strcmp( m.AddressPattern(), "/no_arguments" ) == 0 ){
126
127 args >> osc::EndMessage;
128 std::cout << "received '/no_arguments' message\n";
129
130 }else if( strcmp( m.AddressPattern(), "/a_bool" ) == 0 ){
131
132 bool a;
133 args >> a >> osc::EndMessage;
134 std::cout << "received '/a_bool' message: " << a << "\n";
135
136 }else if( strcmp( m.AddressPattern(), "/nil" ) == 0 ){
137
138 std::cout << "received '/nil' message\n";
139
140 }else if( strcmp( m.AddressPattern(), "/inf" ) == 0 ){
141
142 std::cout << "received '/inf' message\n";
143
144 }else if( strcmp( m.AddressPattern(), "/an_int" ) == 0 ){
145
146 osc::int32 a;
147 args >> a >> osc::EndMessage;
148 std::cout << "received '/an_int' message: " << a << "\n";
149
150 }else if( strcmp( m.AddressPattern(), "/a_float" ) == 0 ){
151
152 float a;
153 args >> a >> osc::EndMessage;
154 std::cout << "received '/a_float' message: " << a << "\n";
155
156 }else if( strcmp( m.AddressPattern(), "/a_char" ) == 0 ){
157
158 char a;
159 args >> a >> osc::EndMessage;
160 char s[2] = {0};
161 s[0] = a;
162 std::cout << "received '/a_char' message: '" << s << "'\n";
163
164 }else if( strcmp( m.AddressPattern(), "/an_rgba_color" ) == 0 ){
165
166 osc::RgbaColor a;
167 args >> a >> osc::EndMessage;
168 std::cout << "received '/an_rgba_color' message: " << a.value << "\n";
169
170 }else if( strcmp( m.AddressPattern(), "/a_midi_message" ) == 0 ){
171
172 osc::MidiMessage a;
173 args >> a >> osc::EndMessage;
174 std::cout << "received '/a_midi_message' message: " << a.value << "\n";
175
176 }else if( strcmp( m.AddressPattern(), "/an_int64" ) == 0 ){
177
178 osc::int64 a;
179 args >> a >> osc::EndMessage;
180 std::cout << "received '/an_int64' message: " << a << "\n";
181
182 }else if( strcmp( m.AddressPattern(), "/a_time_tag" ) == 0 ){
183
184 osc::TimeTag a;
185 args >> a >> osc::EndMessage;
186 std::cout << "received '/a_time_tag' message: " << a.value << "\n";
187
188 }else if( strcmp( m.AddressPattern(), "/a_double" ) == 0 ){
189
190 double a;
191 args >> a >> osc::EndMessage;
192 std::cout << "received '/a_double' message: " << a << "\n";
193
194 }else if( strcmp( m.AddressPattern(), "/a_string" ) == 0 ){
195
196 const char *a;
197 args >> a >> osc::EndMessage;
198 std::cout << "received '/a_string' message: '" << a << "'\n";
199
200 }else if( strcmp( m.AddressPattern(), "/a_symbol" ) == 0 ){
201
202 osc::Symbol a;
203 args >> a >> osc::EndMessage;
204 std::cout << "received '/a_symbol' message: '" << a.value << "'\n";
205
206 }else if( strcmp( m.AddressPattern(), "/a_blob" ) == 0 ){
207
208 osc::Blob a;
209 args >> a >> osc::EndMessage;
210 std::cout << "received '/a_blob' message\n";
211
212 }else{
213 std::cout << "unrecognised address pattern: "
214 << m.AddressPattern() << "\n";
215 }
216
217 }catch( Exception& e ){
218 std::cout << "error while parsing message: "
219 << m.AddressPattern() << ": " << e.what() << "\n";
220 }
221 }
222 };
223
224
225 void RunReceiveTest( int port )
226 {
227 osc::OscReceiveTestPacketListener listener;
228 UdpListeningReceiveSocket s(
229 IpEndpointName( IpEndpointName::ANY_ADDRESS, port ),
230 &listener );
231
232 std::cout << "listening for input on port " << port << "...\n";
233 std::cout << "press ctrl-c to end\n";
234
235 s.RunUntilSigInt();
236
237 std::cout << "finishing.\n";
238 }
239
240 } // namespace osc
241
242 #ifndef NO_OSC_TEST_MAIN
243
244 int main(int argc, char* argv[])
245 {
246 if( argc >= 2 && strcmp( argv[1], "-h" ) == 0 ){
247 std::cout << "usage: OscReceiveTest [port]\n";
248 return 0;
249 }
250
251 int port = 7000;
252
253 if( argc >= 2 )
254 port = atoi( argv[1] );
255
256 osc::RunReceiveTest( port );
257
258 return 0;
259 }
260
261 #endif /* NO_OSC_TEST_MAIN */
262