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