rob@76: /* rob@76: oscpack -- Open Sound Control (OSC) packet manipulation library rob@76: http://www.rossbencina.com/code/oscpack rob@76: rob@76: Copyright (c) 2004-2013 Ross Bencina rob@76: rob@76: Permission is hereby granted, free of charge, to any person obtaining rob@76: a copy of this software and associated documentation files rob@76: (the "Software"), to deal in the Software without restriction, rob@76: including without limitation the rights to use, copy, modify, merge, rob@76: publish, distribute, sublicense, and/or sell copies of the Software, rob@76: and to permit persons to whom the Software is furnished to do so, rob@76: subject to the following conditions: rob@76: rob@76: The above copyright notice and this permission notice shall be rob@76: included in all copies or substantial portions of the Software. rob@76: rob@76: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, rob@76: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF rob@76: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. rob@76: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR rob@76: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF rob@76: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION rob@76: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rob@76: */ rob@76: rob@76: /* rob@76: The text above constitutes the entire oscpack license; however, rob@76: the oscpack developer(s) also make the following non-binding requests: rob@76: rob@76: Any person wishing to distribute modifications to the Software is rob@76: requested to send the modifications to the original developer so that rob@76: they can be incorporated into the canonical version. It is also rob@76: requested that these non-binding requests be included whenever the rob@76: above license is reproduced. rob@76: */ rob@76: #include "OscReceiveTest.h" rob@76: rob@76: #include rob@76: #include rob@76: #include rob@76: rob@76: #if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug rob@76: namespace std { rob@76: using ::__strcmp__; // avoid error: E2316 '__strcmp__' is not a member of 'std'. rob@76: } rob@76: #endif rob@76: rob@76: #include "osc/OscReceivedElements.h" rob@76: rob@76: #include "ip/UdpSocket.h" rob@76: #include "osc/OscPacketListener.h" rob@76: rob@76: rob@76: namespace osc{ rob@76: rob@76: class OscReceiveTestPacketListener : public OscPacketListener{ rob@76: protected: rob@76: rob@76: void ProcessMessage( const osc::ReceivedMessage& m, rob@76: const IpEndpointName& remoteEndpoint ) rob@76: { rob@76: (void) remoteEndpoint; // suppress unused parameter warning rob@76: rob@76: // a more complex scheme involving std::map or some other method of rob@76: // processing address patterns could be used here rob@76: // (see MessageMappingOscPacketListener.h for example). however, the main rob@76: // purpose of this example is to illustrate and test different argument rob@76: // parsing methods rob@76: rob@76: try { rob@76: // argument stream, and argument iterator, used in different rob@76: // examples below. rob@76: ReceivedMessageArgumentStream args = m.ArgumentStream(); rob@76: ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); rob@76: rob@76: if( std::strcmp( m.AddressPattern(), "/test1" ) == 0 ){ rob@76: rob@76: // example #1: rob@76: // parse an expected format using the argument stream interface: rob@76: bool a1; rob@76: osc::int32 a2; rob@76: float a3; rob@76: const char *a4; rob@76: args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage; rob@76: rob@76: std::cout << "received '/test1' message with arguments: " rob@76: << a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/test2" ) == 0 ){ rob@76: rob@76: // example #2: rob@76: // parse an expected format using the argument iterator interface rob@76: // this is a more complicated example of doing the same thing rob@76: // as above. rob@76: bool a1 = (arg++)->AsBool(); rob@76: int a2 = (arg++)->AsInt32(); rob@76: float a3 = (arg++)->AsFloat(); rob@76: const char *a4 = (arg++)->AsString(); rob@76: if( arg != m.ArgumentsEnd() ) rob@76: throw ExcessArgumentException(); rob@76: rob@76: std::cout << "received '/test2' message with arguments: " rob@76: << a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/test3" ) == 0 ){ rob@76: rob@76: // example #3: rob@76: // parse a variable argument format using the argument iterator rob@76: // interface. this is where it is necessary to use rob@76: // argument iterators instead of streams. rob@76: // When messages may contain arguments of varying type, you can rob@76: // use the argument iterator interface to query the types at rob@76: // runtime. this is more flexible that the argument stream rob@76: // interface, which requires each argument to have a fixed type rob@76: rob@76: if( arg->IsBool() ){ rob@76: bool a = (arg++)->AsBoolUnchecked(); rob@76: std::cout << "received '/test3' message with bool argument: " rob@76: << a << "\n"; rob@76: }else if( arg->IsInt32() ){ rob@76: int a = (arg++)->AsInt32Unchecked(); rob@76: std::cout << "received '/test3' message with int32 argument: " rob@76: << a << "\n"; rob@76: }else if( arg->IsFloat() ){ rob@76: float a = (arg++)->AsFloatUnchecked(); rob@76: std::cout << "received '/test3' message with float argument: " rob@76: << a << "\n"; rob@76: }else if( arg->IsString() ){ rob@76: const char *a = (arg++)->AsStringUnchecked(); rob@76: std::cout << "received '/test3' message with string argument: '" rob@76: << a << "'\n"; rob@76: }else{ rob@76: std::cout << "received '/test3' message with unexpected argument type\n"; rob@76: } rob@76: rob@76: if( arg != m.ArgumentsEnd() ) rob@76: throw ExcessArgumentException(); rob@76: rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/no_arguments" ) == 0 ){ rob@76: rob@76: args >> osc::EndMessage; rob@76: std::cout << "received '/no_arguments' message\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_bool" ) == 0 ){ rob@76: rob@76: bool a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_bool' message: " << a << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/nil" ) == 0 ){ rob@76: rob@76: std::cout << "received '/nil' message\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/inf" ) == 0 ){ rob@76: rob@76: std::cout << "received '/inf' message\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/an_int" ) == 0 ){ rob@76: rob@76: osc::int32 a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/an_int' message: " << a << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_float" ) == 0 ){ rob@76: rob@76: float a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_float' message: " << a << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_char" ) == 0 ){ rob@76: rob@76: char a; rob@76: args >> a >> osc::EndMessage; rob@76: char s[2] = {0}; rob@76: s[0] = a; rob@76: std::cout << "received '/a_char' message: '" << s << "'\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/an_rgba_color" ) == 0 ){ rob@76: rob@76: osc::RgbaColor a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/an_rgba_color' message: " << a.value << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_midi_message" ) == 0 ){ rob@76: rob@76: osc::MidiMessage a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_midi_message' message: " << a.value << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/an_int64" ) == 0 ){ rob@76: rob@76: osc::int64 a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/an_int64' message: " << a << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_time_tag" ) == 0 ){ rob@76: rob@76: osc::TimeTag a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_time_tag' message: " << a.value << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_double" ) == 0 ){ rob@76: rob@76: double a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_double' message: " << a << "\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_string" ) == 0 ){ rob@76: rob@76: const char *a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_string' message: '" << a << "'\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_symbol" ) == 0 ){ rob@76: rob@76: osc::Symbol a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_symbol' message: '" << a.value << "'\n"; rob@76: rob@76: }else if( std::strcmp( m.AddressPattern(), "/a_blob" ) == 0 ){ rob@76: rob@76: osc::Blob a; rob@76: args >> a >> osc::EndMessage; rob@76: std::cout << "received '/a_blob' message\n"; rob@76: rob@76: }else{ rob@76: std::cout << "unrecognised address pattern: " rob@76: << m.AddressPattern() << "\n"; rob@76: } rob@76: rob@76: }catch( Exception& e ){ rob@76: std::cout << "error while parsing message: " rob@76: << m.AddressPattern() << ": " << e.what() << "\n"; rob@76: } rob@76: } rob@76: }; rob@76: rob@76: rob@76: void RunReceiveTest( int port ) rob@76: { rob@76: osc::OscReceiveTestPacketListener listener; rob@76: UdpListeningReceiveSocket s( rob@76: IpEndpointName( IpEndpointName::ANY_ADDRESS, port ), rob@76: &listener ); rob@76: rob@76: std::cout << "listening for input on port " << port << "...\n"; rob@76: std::cout << "press ctrl-c to end\n"; rob@76: rob@76: s.RunUntilSigInt(); rob@76: rob@76: std::cout << "finishing.\n"; rob@76: } rob@76: rob@76: } // namespace osc rob@76: rob@76: #ifndef NO_OSC_TEST_MAIN rob@76: rob@76: int main(int argc, char* argv[]) rob@76: { rob@76: if( argc >= 2 && std::strcmp( argv[1], "-h" ) == 0 ){ rob@76: std::cout << "usage: OscReceiveTest [port]\n"; rob@76: return 0; rob@76: } rob@76: rob@76: int port = 7000; rob@76: rob@76: if( argc >= 2 ) rob@76: port = std::atoi( argv[1] ); rob@76: rob@76: osc::RunReceiveTest( port ); rob@76: rob@76: return 0; rob@76: } rob@76: rob@76: #endif /* NO_OSC_TEST_MAIN */ rob@76: