root/oscpack/trunk/tests/OscReceiveTest.cpp

Revision 29, 9.5 kB (checked in by ross, 3 years ago)

moved non library code from /osc to /tests and /examples, made all cross-directory includes relative to base directory eg. updated makefile (untested)

  • Property svn:eol-style set to native
Line 
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 <iostream>
34
35 #include "osc/OscReceivedElements.h"
36
37 #include "ip/UdpSocket.h"
38 #include "osc/OscPacketListener.h"
39
40
41 namespace osc{
42
43 class OscReceiveTestPacketListener : public OscPacketListener{
44 protected:
45
46     void ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint )
47     {
48         // a more complex scheme involving std::map or some other method of
49         // processing address patterns could be used here
50                 // (see MessageMappingOscPacketListener.h for example). however, the main
51         // purpose of this example is to illustrate and test different argument
52         // parsing methods
53
54         try {
55             // argument stream, and argument iterator, used in different
56             // examples below.
57             ReceivedMessageArgumentStream args = m.ArgumentStream();
58             ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
59            
60             if( strcmp( m.AddressPattern(), "/test1" ) == 0 ){
61
62                 // example #1:
63                 // parse an expected format using the argument stream interface:
64                 bool a1;
65                 osc::int32 a2;
66                 float a3;
67                 const char *a4;
68                 args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage;
69
70                 std::cout << "received '/test1' message with arguments: "
71                         << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
72
73             }else if( strcmp( m.AddressPattern(), "/test2" ) == 0 ){
74
75                 // example #2:
76                 // parse an expected format using the argument iterator interface
77                 // this is a more complicated example of doing the same thing
78                 // as above.
79                 bool a1 = (arg++)->AsBool();
80                 int a2 = (arg++)->AsInt32();
81                 float a3 = (arg++)->AsFloat();
82                 const char *a4 = (arg++)->AsString();
83                 if( arg != m.ArgumentsEnd() )
84                     throw ExcessArgumentException();
85
86                 std::cout << "received '/test2' message with arguments: "
87                          << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
88
89             }else if( strcmp( m.AddressPattern(), "/test3" ) == 0 ){
90
91                 // example #3:
92                 // parse a variable argument format using the argument iterator
93                 // interface. this is where it is necessary to use
94                 // argument iterators instead of streams.
95                 // When messages may contain arguments of varying type, you can
96                 // use the argument iterator interface to query the types at
97                 // runtime. this is more flexible that the argument stream
98                 // interface, which requires each argument to have a fixed type
99                 
100                 if( arg->IsBool() ){
101                     bool a = (arg++)->AsBoolUnchecked();
102                     std::cout << "received '/test3' message with bool argument: "
103                         << a << "\n";
104                 }else if( arg->IsInt32() ){
105                     int a = (arg++)->AsInt32Unchecked();
106                     std::cout << "received '/test3' message with int32 argument: "
107                         << a << "\n";
108                 }else if( arg->IsFloat() ){
109                     float a = (arg++)->AsFloatUnchecked();
110                     std::cout << "received '/test3' message with float argument: "
111                         << a << "\n";
112                 }else if( arg->IsString() ){
113                     const char *a = (arg++)->AsStringUnchecked();
114                     std::cout << "received '/test3' message with string argument: '"
115                         << a << "'\n";
116                 }else{
117                     std::cout << "received '/test3' message with unexpected argument type\n";
118                 }
119                
120                 if( arg != m.ArgumentsEnd() )
121                     throw ExcessArgumentException();
122
123                    
124             }else if( strcmp( m.AddressPattern(), "/no_arguments" ) == 0 ){
125
126                 args >> osc::EndMessage;
127                 std::cout << "received '/no_arguments' message\n";
128
129             }else if( strcmp( m.AddressPattern(), "/a_bool" ) == 0 ){
130
131                 bool a;
132                 args >> a >> osc::EndMessage;
133                 std::cout << "received '/a_bool' message: " << a << "\n";
134
135             }else if( strcmp( m.AddressPattern(), "/nil" ) == 0 ){
136
137                 std::cout << "received '/nil' message\n";
138
139             }else if( strcmp( m.AddressPattern(), "/inf" ) == 0 ){
140
141                 std::cout << "received '/inf' message\n";
142
143             }else if( strcmp( m.AddressPattern(), "/an_int" ) == 0 ){
144
145                 osc::int32 a;
146                 args >> a >> osc::EndMessage;
147                 std::cout << "received '/an_int' message: " << a << "\n";
148
149             }else if( strcmp( m.AddressPattern(), "/a_float" ) == 0 ){
150
151                 float a;
152                 args >> a >> osc::EndMessage;
153                 std::cout << "received '/a_float' message: " << a << "\n";
154
155             }else if( strcmp( m.AddressPattern(), "/a_char" ) == 0 ){
156
157                 char a;
158                 args >> a >> osc::EndMessage;
159                 char s[2] = {0};
160                 s[0] = a;
161                 std::cout << "received '/a_char' message: '" << s << "'\n";
162
163             }else if( strcmp( m.AddressPattern(), "/an_rgba_color" ) == 0 ){
164
165                 osc::RgbaColor a;
166                 args >> a >> osc::EndMessage;
167                 std::cout << "received '/an_rgba_color' message: " << a.value << "\n";
168
169             }else if( strcmp( m.AddressPattern(), "/a_midi_message" ) == 0 ){
170
171                 osc::MidiMessage a;
172                 args >> a >> osc::EndMessage;
173                 std::cout << "received '/a_midi_message' message: " << a.value << "\n";
174
175             }else if( strcmp( m.AddressPattern(), "/an_int64" ) == 0 ){
176
177                 osc::int64 a;
178                 args >> a >> osc::EndMessage;
179                 std::cout << "received '/an_int64' message: " << a << "\n";
180
181             }else if( strcmp( m.AddressPattern(), "/a_time_tag" ) == 0 ){
182
183                 osc::TimeTag a;
184                 args >> a >> osc::EndMessage;
185                 std::cout << "received '/a_time_tag' message: " << a.value << "\n";
186
187             }else if( strcmp( m.AddressPattern(), "/a_double" ) == 0 ){
188
189                 double a;
190                 args >> a >> osc::EndMessage;
191                 std::cout << "received '/a_double' message: " << a << "\n";
192
193             }else if( strcmp( m.AddressPattern(), "/a_string" ) == 0 ){
194
195                 const char *a;
196                 args >> a >> osc::EndMessage;
197                 std::cout << "received '/a_string' message: '" << a << "'\n";
198
199             }else if( strcmp( m.AddressPattern(), "/a_symbol" ) == 0 ){
200
201                 osc::Symbol a;
202                 args >> a >> osc::EndMessage;
203                 std::cout << "received '/a_symbol' message: '" << a.value << "'\n";
204
205              }else if( strcmp( m.AddressPattern(), "/a_blob" ) == 0 ){
206
207                 osc::Blob a;
208                 args >> a >> osc::EndMessage;
209                 std::cout << "received '/a_blob' message\n";
210
211             }else{
212                 std::cout << "unrecognised address pattern: "
213                         << m.AddressPattern() << "\n";
214             }
215
216         }catch( Exception& e ){
217             std::cout << "error while parsing message: "
218                         << m.AddressPattern() << ": " << e.what() << "\n";
219         }
220     }   
221 };
222
223
224 void RunReceiveTest( int port )
225 {
226     osc::OscReceiveTestPacketListener listener;
227         UdpListeningReceiveSocket s(
228             IpEndpointName( IpEndpointName::ANY_ADDRESS, port ),
229             &listener );
230
231         std::cout << "listening for input on port " << port << "...\n";
232         std::cout << "press ctrl-c to end\n";
233
234         s.RunUntilSigInt();
235
236         std::cout << "finishing.\n";
237 }
238
239 } // namespace osc
240
241 #ifndef NO_OSC_TEST_MAIN
242
243 int main(int argc, char* argv[])
244 {
245         if( argc >= 2 && strcmp( argv[1], "-h" ) == 0 ){
246         std::cout << "usage: OscReceiveTest [port]\n";
247         return 0;
248     }
249
250         int port = 7000;
251
252         if( argc >= 2 )
253                 port = atoi( argv[1] );
254
255     osc::RunReceiveTest( port );
256
257     return 0;
258 }
259
260 #endif /* NO_OSC_TEST_MAIN */
261
Note: See TracBrowser for help on using the browser.