annotate trunk/external/oscpack/tests/OscReceiveTest.cpp @ 706:f8e90b5d85fd tip

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