annotate oscpack/tests/OscReceiveTest.cpp @ 101:52e44ee1c791 tip master

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