| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
#include "OscReceiveTest.h" |
|---|
| 31 |
|
|---|
| 32 |
#include <string.h> |
|---|
| 33 |
#include <iostream> |
|---|
| 34 |
|
|---|
| 35 |
#include "osc/OscReceivedElements.h" |
|---|
| 36 |
|
|---|
| 37 |
#include "ip/UdpSocket.h" |
|---|
| 38 |
#include "osc/OscPacketListener.h" |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
namespace osc{ |
|---|
| 42 |
|
|---|
| 43 |
class OscReceiveTestPacketListener : public OscPacketListener{ |
|---|
| 44 |
protected: |
|---|
| 45 |
|
|---|
| 46 |
void ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint ) |
|---|
| 47 |
{ |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
try { |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
ReceivedMessageArgumentStream args = m.ArgumentStream(); |
|---|
| 58 |
ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); |
|---|
| 59 |
|
|---|
| 60 |
if( strcmp( m.AddressPattern(), "/test1" ) == 0 ){ |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
bool a1; |
|---|
| 65 |
osc::int32 a2; |
|---|
| 66 |
float a3; |
|---|
| 67 |
const char *a4; |
|---|
| 68 |
args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage; |
|---|
| 69 |
|
|---|
| 70 |
std::cout << "received '/test1' message with arguments: " |
|---|
| 71 |
<< a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; |
|---|
| 72 |
|
|---|
| 73 |
}else if( strcmp( m.AddressPattern(), "/test2" ) == 0 ){ |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
bool a1 = (arg++)->AsBool(); |
|---|
| 80 |
int a2 = (arg++)->AsInt32(); |
|---|
| 81 |
float a3 = (arg++)->AsFloat(); |
|---|
| 82 |
const char *a4 = (arg++)->AsString(); |
|---|
| 83 |
if( arg != m.ArgumentsEnd() ) |
|---|
| 84 |
throw ExcessArgumentException(); |
|---|
| 85 |
|
|---|
| 86 |
std::cout << "received '/test2' message with arguments: " |
|---|
| 87 |
<< a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; |
|---|
| 88 |
|
|---|
| 89 |
}else if( strcmp( m.AddressPattern(), "/test3" ) == 0 ){ |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
if( arg->IsBool() ){ |
|---|
| 101 |
bool a = (arg++)->AsBoolUnchecked(); |
|---|
| 102 |
std::cout << "received '/test3' message with bool argument: " |
|---|
| 103 |
<< a << "\n"; |
|---|
| 104 |
}else if( arg->IsInt32() ){ |
|---|
| 105 |
int a = (arg++)->AsInt32Unchecked(); |
|---|
| 106 |
std::cout << "received '/test3' message with int32 argument: " |
|---|
| 107 |
<< a << "\n"; |
|---|
| 108 |
}else if( arg->IsFloat() ){ |
|---|
| 109 |
float a = (arg++)->AsFloatUnchecked(); |
|---|
| 110 |
std::cout << "received '/test3' message with float argument: " |
|---|
| 111 |
<< a << "\n"; |
|---|
| 112 |
}else if( arg->IsString() ){ |
|---|
| 113 |
const char *a = (arg++)->AsStringUnchecked(); |
|---|
| 114 |
std::cout << "received '/test3' message with string argument: '" |
|---|
| 115 |
<< a << "'\n"; |
|---|
| 116 |
}else{ |
|---|
| 117 |
std::cout << "received '/test3' message with unexpected argument type\n"; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
if( arg != m.ArgumentsEnd() ) |
|---|
| 121 |
throw ExcessArgumentException(); |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
}else if( strcmp( m.AddressPattern(), "/no_arguments" ) == 0 ){ |
|---|
| 125 |
|
|---|
| 126 |
args >> osc::EndMessage; |
|---|
| 127 |
std::cout << "received '/no_arguments' message\n"; |
|---|
| 128 |
|
|---|
| 129 |
}else if( strcmp( m.AddressPattern(), "/a_bool" ) == 0 ){ |
|---|
| 130 |
|
|---|
| 131 |
bool a; |
|---|
| 132 |
args >> a >> osc::EndMessage; |
|---|
| 133 |
std::cout << "received '/a_bool' message: " << a << "\n"; |
|---|
| 134 |
|
|---|
| 135 |
}else if( strcmp( m.AddressPattern(), "/nil" ) == 0 ){ |
|---|
| 136 |
|
|---|
| 137 |
std::cout << "received '/nil' message\n"; |
|---|
| 138 |
|
|---|
| 139 |
}else if( strcmp( m.AddressPattern(), "/inf" ) == 0 ){ |
|---|
| 140 |
|
|---|
| 141 |
std::cout << "received '/inf' message\n"; |
|---|
| 142 |
|
|---|
| 143 |
}else if( strcmp( m.AddressPattern(), "/an_int" ) == 0 ){ |
|---|
| 144 |
|
|---|
| 145 |
osc::int32 a; |
|---|
| 146 |
args >> a >> osc::EndMessage; |
|---|
| 147 |
std::cout << "received '/an_int' message: " << a << "\n"; |
|---|
| 148 |
|
|---|
| 149 |
}else if( strcmp( m.AddressPattern(), "/a_float" ) == 0 ){ |
|---|
| 150 |
|
|---|
| 151 |
float a; |
|---|
| 152 |
args >> a >> osc::EndMessage; |
|---|
| 153 |
std::cout << "received '/a_float' message: " << a << "\n"; |
|---|
| 154 |
|
|---|
| 155 |
}else if( strcmp( m.AddressPattern(), "/a_char" ) == 0 ){ |
|---|
| 156 |
|
|---|
| 157 |
char a; |
|---|
| 158 |
args >> a >> osc::EndMessage; |
|---|
| 159 |
char s[2] = {0}; |
|---|
| 160 |
s[0] = a; |
|---|
| 161 |
std::cout << "received '/a_char' message: '" << s << "'\n"; |
|---|
| 162 |
|
|---|
| 163 |
}else if( strcmp( m.AddressPattern(), "/an_rgba_color" ) == 0 ){ |
|---|
| 164 |
|
|---|
| 165 |
osc::RgbaColor a; |
|---|
| 166 |
args >> a >> osc::EndMessage; |
|---|
| 167 |
std::cout << "received '/an_rgba_color' message: " << a.value << "\n"; |
|---|
| 168 |
|
|---|
| 169 |
}else if( strcmp( m.AddressPattern(), "/a_midi_message" ) == 0 ){ |
|---|
| 170 |
|
|---|
| 171 |
osc::MidiMessage a; |
|---|
| 172 |
args >> a >> osc::EndMessage; |
|---|
| 173 |
std::cout << "received '/a_midi_message' message: " << a.value << "\n"; |
|---|
| 174 |
|
|---|
| 175 |
}else if( strcmp( m.AddressPattern(), "/an_int64" ) == 0 ){ |
|---|
| 176 |
|
|---|
| 177 |
osc::int64 a; |
|---|
| 178 |
args >> a >> osc::EndMessage; |
|---|
| 179 |
std::cout << "received '/an_int64' message: " << a << "\n"; |
|---|
| 180 |
|
|---|
| 181 |
}else if( strcmp( m.AddressPattern(), "/a_time_tag" ) == 0 ){ |
|---|
| 182 |
|
|---|
| 183 |
osc::TimeTag a; |
|---|
| 184 |
args >> a >> osc::EndMessage; |
|---|
| 185 |
std::cout << "received '/a_time_tag' message: " << a.value << "\n"; |
|---|
| 186 |
|
|---|
| 187 |
}else if( strcmp( m.AddressPattern(), "/a_double" ) == 0 ){ |
|---|
| 188 |
|
|---|
| 189 |
double a; |
|---|
| 190 |
args >> a >> osc::EndMessage; |
|---|
| 191 |
std::cout << "received '/a_double' message: " << a << "\n"; |
|---|
| 192 |
|
|---|
| 193 |
}else if( strcmp( m.AddressPattern(), "/a_string" ) == 0 ){ |
|---|
| 194 |
|
|---|
| 195 |
const char *a; |
|---|
| 196 |
args >> a >> osc::EndMessage; |
|---|
| 197 |
std::cout << "received '/a_string' message: '" << a << "'\n"; |
|---|
| 198 |
|
|---|
| 199 |
}else if( strcmp( m.AddressPattern(), "/a_symbol" ) == 0 ){ |
|---|
| 200 |
|
|---|
| 201 |
osc::Symbol a; |
|---|
| 202 |
args >> a >> osc::EndMessage; |
|---|
| 203 |
std::cout << "received '/a_symbol' message: '" << a.value << "'\n"; |
|---|
| 204 |
|
|---|
| 205 |
}else if( strcmp( m.AddressPattern(), "/a_blob" ) == 0 ){ |
|---|
| 206 |
|
|---|
| 207 |
osc::Blob a; |
|---|
| 208 |
args >> a >> osc::EndMessage; |
|---|
| 209 |
std::cout << "received '/a_blob' message\n"; |
|---|
| 210 |
|
|---|
| 211 |
}else{ |
|---|
| 212 |
std::cout << "unrecognised address pattern: " |
|---|
| 213 |
<< m.AddressPattern() << "\n"; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
}catch( Exception& e ){ |
|---|
| 217 |
std::cout << "error while parsing message: " |
|---|
| 218 |
<< m.AddressPattern() << ": " << e.what() << "\n"; |
|---|
| 219 |
} |
|---|
| 220 |
} |
|---|
| 221 |
}; |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
void RunReceiveTest( int port ) |
|---|
| 225 |
{ |
|---|
| 226 |
osc::OscReceiveTestPacketListener listener; |
|---|
| 227 |
UdpListeningReceiveSocket s( |
|---|
| 228 |
IpEndpointName( IpEndpointName::ANY_ADDRESS, port ), |
|---|
| 229 |
&listener ); |
|---|
| 230 |
|
|---|
| 231 |
std::cout << "listening for input on port " << port << "...\n"; |
|---|
| 232 |
std::cout << "press ctrl-c to end\n"; |
|---|
| 233 |
|
|---|
| 234 |
s.RunUntilSigInt(); |
|---|
| 235 |
|
|---|
| 236 |
std::cout << "finishing.\n"; |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
#ifndef NO_OSC_TEST_MAIN |
|---|
| 242 |
|
|---|
| 243 |
int main(int argc, char* argv[]) |
|---|
| 244 |
{ |
|---|
| 245 |
if( argc >= 2 && strcmp( argv[1], "-h" ) == 0 ){ |
|---|
| 246 |
std::cout << "usage: OscReceiveTest [port]\n"; |
|---|
| 247 |
return 0; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
int port = 7000; |
|---|
| 251 |
|
|---|
| 252 |
if( argc >= 2 ) |
|---|
| 253 |
port = atoi( argv[1] ); |
|---|
| 254 |
|
|---|
| 255 |
osc::RunReceiveTest( port ); |
|---|
| 256 |
|
|---|
| 257 |
return 0; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
#endif |
|---|
| 261 |
|
|---|