comparison oscpack/tests/OscSendTests.cpp @ 76:0ae87af84e2f

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