annotate trunk/external/oscpack/examples/SimpleReceive.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 Example of two different ways to process received OSC messages using oscpack.
tomwalters@570 3 Receives the messages from the SimpleSend.cpp example.
tomwalters@570 4 */
tomwalters@570 5
tomwalters@570 6 #include <iostream>
tomwalters@570 7
tomwalters@570 8 #include "osc/OscReceivedElements.h"
tomwalters@570 9 #include "osc/OscPacketListener.h"
tomwalters@570 10 #include "ip/UdpSocket.h"
tomwalters@570 11
tomwalters@570 12
tomwalters@570 13 #define PORT 7000
tomwalters@570 14
tomwalters@570 15 class ExamplePacketListener : public osc::OscPacketListener {
tomwalters@570 16 protected:
tomwalters@570 17
tomwalters@570 18 virtual void ProcessMessage( const osc::ReceivedMessage& m,
tomwalters@570 19 const IpEndpointName& remoteEndpoint )
tomwalters@570 20 {
tomwalters@570 21 try{
tomwalters@570 22 // example of parsing single messages. osc::OsckPacketListener
tomwalters@570 23 // handles the bundle traversal.
tomwalters@570 24
tomwalters@570 25 if( strcmp( m.AddressPattern(), "/test1" ) == 0 ){
tomwalters@570 26 // example #1 -- argument stream interface
tomwalters@570 27 osc::ReceivedMessageArgumentStream args = m.ArgumentStream();
tomwalters@570 28 bool a1;
tomwalters@570 29 osc::int32 a2;
tomwalters@570 30 float a3;
tomwalters@570 31 const char *a4;
tomwalters@570 32 args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage;
tomwalters@570 33
tomwalters@570 34 std::cout << "received '/test1' message with arguments: "
tomwalters@570 35 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
tomwalters@570 36
tomwalters@570 37 }else if( strcmp( m.AddressPattern(), "/test2" ) == 0 ){
tomwalters@570 38 // example #2 -- argument iterator interface, supports
tomwalters@570 39 // reflection for overloaded messages (eg you can call
tomwalters@570 40 // (*arg)->IsBool() to check if a bool was passed etc).
tomwalters@570 41 osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
tomwalters@570 42 bool a1 = (arg++)->AsBool();
tomwalters@570 43 int a2 = (arg++)->AsInt32();
tomwalters@570 44 float a3 = (arg++)->AsFloat();
tomwalters@570 45 const char *a4 = (arg++)->AsString();
tomwalters@570 46 if( arg != m.ArgumentsEnd() )
tomwalters@570 47 throw osc::ExcessArgumentException();
tomwalters@570 48
tomwalters@570 49 std::cout << "received '/test2' message with arguments: "
tomwalters@570 50 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
tomwalters@570 51 }
tomwalters@570 52 }catch( osc::Exception& e ){
tomwalters@570 53 // any parsing errors such as unexpected argument types, or
tomwalters@570 54 // missing arguments get thrown as exceptions.
tomwalters@570 55 std::cout << "error while parsing message: "
tomwalters@570 56 << m.AddressPattern() << ": " << e.what() << "\n";
tomwalters@570 57 }
tomwalters@570 58 }
tomwalters@570 59 };
tomwalters@570 60
tomwalters@570 61 int main(int argc, char* argv[])
tomwalters@570 62 {
tomwalters@570 63 ExamplePacketListener listener;
tomwalters@570 64 UdpListeningReceiveSocket s(
tomwalters@570 65 IpEndpointName( IpEndpointName::ANY_ADDRESS, PORT ),
tomwalters@570 66 &listener );
tomwalters@570 67
tomwalters@570 68 std::cout << "press ctrl-c to end\n";
tomwalters@570 69
tomwalters@570 70 s.RunUntilSigInt();
tomwalters@570 71
tomwalters@570 72 return 0;
tomwalters@570 73 }
tomwalters@570 74