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