annotate external/oscpack/tests/OscReceiveTest.cpp @ 509:0284d2152e17

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