annotate external/oscpack/tests/OscSendTests.cpp @ 509:0284d2152e17

Add support for outputting featutes using OSC (for use with the Wekinator, etc).
author tomwalters@google.com
date Fri, 22 Jun 2012 12:22:08 +0000
parents
children
rev   line source
tomwalters@509 1 /*
tomwalters@509 2 oscpack -- Open Sound Control packet manipulation library
tomwalters@509 3 http://www.audiomulch.com/~rossb/oscpack
tomwalters@509 4
tomwalters@509 5 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>
tomwalters@509 6
tomwalters@509 7 Permission is hereby granted, free of charge, to any person obtaining
tomwalters@509 8 a copy of this software and associated documentation files
tomwalters@509 9 (the "Software"), to deal in the Software without restriction,
tomwalters@509 10 including without limitation the rights to use, copy, modify, merge,
tomwalters@509 11 publish, distribute, sublicense, and/or sell copies of the Software,
tomwalters@509 12 and to permit persons to whom the Software is furnished to do so,
tomwalters@509 13 subject to the following conditions:
tomwalters@509 14
tomwalters@509 15 The above copyright notice and this permission notice shall be
tomwalters@509 16 included in all copies or substantial portions of the Software.
tomwalters@509 17
tomwalters@509 18 Any person wishing to distribute modifications to the Software is
tomwalters@509 19 requested to send the modifications to the original developer so that
tomwalters@509 20 they can be incorporated into the canonical version.
tomwalters@509 21
tomwalters@509 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
tomwalters@509 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tomwalters@509 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tomwalters@509 25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
tomwalters@509 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
tomwalters@509 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
tomwalters@509 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tomwalters@509 29 */
tomwalters@509 30 #include "OscSendTests.h"
tomwalters@509 31
tomwalters@509 32 #include <iostream>
tomwalters@509 33 #include <string.h>
tomwalters@509 34
tomwalters@509 35 #include "osc/OscOutboundPacketStream.h"
tomwalters@509 36
tomwalters@509 37 #include "ip/UdpSocket.h"
tomwalters@509 38 #include "ip/IpEndpointName.h"
tomwalters@509 39
tomwalters@509 40 #define IP_MTU_SIZE 1536
tomwalters@509 41
tomwalters@509 42 namespace osc{
tomwalters@509 43
tomwalters@509 44 void RunSendTests( const IpEndpointName& host )
tomwalters@509 45 {
tomwalters@509 46 char buffer[IP_MTU_SIZE];
tomwalters@509 47 osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
tomwalters@509 48 UdpTransmitSocket socket( host );
tomwalters@509 49
tomwalters@509 50 p.Clear();
tomwalters@509 51 p << osc::BeginMessage( "/test1" )
tomwalters@509 52 << true << 23 << (float)3.1415 << "hello" << osc::EndMessage;
tomwalters@509 53 socket.Send( p.Data(), p.Size() );
tomwalters@509 54
tomwalters@509 55 // test1 message with too few arguments
tomwalters@509 56 p.Clear();
tomwalters@509 57 p << osc::BeginMessage( "/test1" )
tomwalters@509 58 << true << osc::EndMessage;
tomwalters@509 59 socket.Send( p.Data(), p.Size() );
tomwalters@509 60
tomwalters@509 61 // test1 message with too many arguments
tomwalters@509 62 p.Clear();
tomwalters@509 63 p << osc::BeginMessage( "/test1" )
tomwalters@509 64 << true << 23 << (float)3.1415 << "hello" << 42 << osc::EndMessage;
tomwalters@509 65 socket.Send( p.Data(), p.Size() );
tomwalters@509 66
tomwalters@509 67 // test1 message with wrong argument type
tomwalters@509 68 p.Clear();
tomwalters@509 69 p << osc::BeginMessage( "/test1" )
tomwalters@509 70 << true << 1.0 << (float)3.1415 << "hello" << osc::EndMessage;
tomwalters@509 71 socket.Send( p.Data(), p.Size() );
tomwalters@509 72
tomwalters@509 73 p.Clear();
tomwalters@509 74 p << osc::BeginMessage( "/test2" )
tomwalters@509 75 << true << 23 << (float)3.1415 << "hello" << osc::EndMessage;
tomwalters@509 76 socket.Send( p.Data(), p.Size() );
tomwalters@509 77
tomwalters@509 78 // send four /test3 messages, each with a different type of argument
tomwalters@509 79 p.Clear();
tomwalters@509 80 p << osc::BeginMessage( "/test3" )
tomwalters@509 81 << true << osc::EndMessage;
tomwalters@509 82 socket.Send( p.Data(), p.Size() );
tomwalters@509 83
tomwalters@509 84 p.Clear();
tomwalters@509 85 p << osc::BeginMessage( "/test3" )
tomwalters@509 86 << 23 << osc::EndMessage;
tomwalters@509 87 socket.Send( p.Data(), p.Size() );
tomwalters@509 88
tomwalters@509 89 p.Clear();
tomwalters@509 90 p << osc::BeginMessage( "/test3" )
tomwalters@509 91 << (float)3.1415 << osc::EndMessage;
tomwalters@509 92 socket.Send( p.Data(), p.Size() );
tomwalters@509 93
tomwalters@509 94 p.Clear();
tomwalters@509 95 p << osc::BeginMessage( "/test3" )
tomwalters@509 96 << "hello" << osc::EndMessage;
tomwalters@509 97 socket.Send( p.Data(), p.Size() );
tomwalters@509 98
tomwalters@509 99
tomwalters@509 100 // send a bundle
tomwalters@509 101 p.Clear();
tomwalters@509 102 p << osc::BeginBundle();
tomwalters@509 103
tomwalters@509 104 p << osc::BeginMessage( "/no_arguments" )
tomwalters@509 105 << osc::EndMessage;
tomwalters@509 106
tomwalters@509 107 p << osc::BeginMessage( "/a_bool" )
tomwalters@509 108 << true << osc::EndMessage;
tomwalters@509 109
tomwalters@509 110 p << osc::BeginMessage( "/a_bool" )
tomwalters@509 111 << false << osc::EndMessage;
tomwalters@509 112
tomwalters@509 113 p << osc::BeginMessage( "/a_bool" )
tomwalters@509 114 << (bool)1234 << osc::EndMessage;
tomwalters@509 115
tomwalters@509 116 p << osc::BeginMessage( "/nil" )
tomwalters@509 117 << osc::Nil << osc::EndMessage;
tomwalters@509 118
tomwalters@509 119 p << osc::BeginMessage( "/inf" )
tomwalters@509 120 << osc::Infinitum << osc::EndMessage;
tomwalters@509 121
tomwalters@509 122 p << osc::BeginMessage( "/an_int" ) << 1234 << osc::EndMessage;
tomwalters@509 123
tomwalters@509 124 p << osc::BeginMessage( "/a_float" )
tomwalters@509 125 << 3.1415926f << osc::EndMessage;
tomwalters@509 126
tomwalters@509 127 p << osc::BeginMessage( "/a_char" )
tomwalters@509 128 << 'c' << osc::EndMessage;
tomwalters@509 129
tomwalters@509 130 p << osc::BeginMessage( "/an_rgba_color" )
tomwalters@509 131 << osc::RgbaColor(0x22334455) << osc::EndMessage;
tomwalters@509 132
tomwalters@509 133 p << osc::BeginMessage( "/a_midi_message" )
tomwalters@509 134 << MidiMessage(0x7F) << osc::EndMessage;
tomwalters@509 135
tomwalters@509 136 p << osc::BeginMessage( "/an_int64" )
tomwalters@509 137 << (int64)(0xFFFFFFF) << osc::EndMessage;
tomwalters@509 138
tomwalters@509 139 p << osc::BeginMessage( "/a_time_tag" )
tomwalters@509 140 << osc::TimeTag(0xFFFFFFFUL) << osc::EndMessage;
tomwalters@509 141
tomwalters@509 142 p << osc::BeginMessage( "/a_double" )
tomwalters@509 143 << (double)3.1415926 << osc::EndMessage;
tomwalters@509 144
tomwalters@509 145 p << osc::BeginMessage( "/a_string" )
tomwalters@509 146 << "hello world" << osc::EndMessage;
tomwalters@509 147
tomwalters@509 148 p << osc::BeginMessage( "/a_symbol" )
tomwalters@509 149 << osc::Symbol("foobar") << osc::EndMessage;
tomwalters@509 150
tomwalters@509 151 // blob
tomwalters@509 152 {
tomwalters@509 153 char blobData[] = "abcd";
tomwalters@509 154
tomwalters@509 155 p << osc::BeginMessage( "/a_blob" )
tomwalters@509 156 << osc::Blob( blobData, 4 )
tomwalters@509 157 << osc::EndMessage;
tomwalters@509 158 }
tomwalters@509 159
tomwalters@509 160 p << osc::EndBundle;
tomwalters@509 161 socket.Send( p.Data(), p.Size() );
tomwalters@509 162
tomwalters@509 163
tomwalters@509 164
tomwalters@509 165 // nested bundles, and multiple messages in bundles...
tomwalters@509 166 p.Clear();
tomwalters@509 167 p << osc::BeginBundle( 1234 )
tomwalters@509 168 << osc::BeginMessage( "/an_int" ) << 1 << osc::EndMessage
tomwalters@509 169 << osc::BeginMessage( "/an_int" ) << 2 << osc::EndMessage
tomwalters@509 170 << osc::BeginMessage( "/an_int" ) << 3 << osc::EndMessage
tomwalters@509 171 << osc::BeginMessage( "/an_int" ) << 4 << osc::EndMessage
tomwalters@509 172 << osc::BeginBundle( 12345 )
tomwalters@509 173 << osc::BeginMessage( "/an_int" ) << 5 << osc::EndMessage
tomwalters@509 174 << osc::BeginMessage( "/an_int" ) << 6 << osc::EndMessage
tomwalters@509 175 << osc::EndBundle
tomwalters@509 176 << osc::EndBundle;
tomwalters@509 177
tomwalters@509 178 socket.Send( p.Data(), p.Size() );
tomwalters@509 179 }
tomwalters@509 180
tomwalters@509 181 } // namespace osc
tomwalters@509 182
tomwalters@509 183 #ifndef NO_OSC_TEST_MAIN
tomwalters@509 184
tomwalters@509 185 int main(int argc, char* argv[])
tomwalters@509 186 {
tomwalters@509 187 if( argc >= 2 && strcmp( argv[1], "-h" ) == 0 ){
tomwalters@509 188 std::cout << "usage: OscSendTests [hostname [port]]\n";
tomwalters@509 189 return 0;
tomwalters@509 190 }
tomwalters@509 191
tomwalters@509 192 char *hostName = "localhost";
tomwalters@509 193 int port = 7000;
tomwalters@509 194
tomwalters@509 195 if( argc >= 2 )
tomwalters@509 196 hostName = argv[1];
tomwalters@509 197
tomwalters@509 198 if( argc >= 3 )
tomwalters@509 199 port = atoi( argv[2] );
tomwalters@509 200
tomwalters@509 201
tomwalters@509 202 IpEndpointName host( hostName, port );
tomwalters@509 203
tomwalters@509 204 char hostIpAddress[ IpEndpointName::ADDRESS_STRING_LENGTH ];
tomwalters@509 205 host.AddressAsString( hostIpAddress );
tomwalters@509 206
tomwalters@509 207 std::cout << "sending test messages to " << hostName
tomwalters@509 208 << " (" << hostIpAddress << ") on port " << port << "...\n";
tomwalters@509 209
tomwalters@509 210 osc::RunSendTests( host );
tomwalters@509 211 }
tomwalters@509 212
tomwalters@509 213 #endif /* NO_OSC_TEST_MAIN */