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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// **********************************************************************
//
// Copyright (c) 2003-2018 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <TestCommon.h>
#include <Test.h>
using namespace std;
using namespace Ice;
using namespace Test;
class PingReplyI : public PingReply, public IceUtil::Monitor<IceUtil::Mutex>
{
public:
virtual void
reply(const Ice::Current&)
{
Lock sync(*this);
++_replies;
notify();
}
void
reset()
{
_replies = 0;
}
bool
waitReply(int expectedReplies, const IceUtil::Time& timeout)
{
Lock sync(*this);
IceUtil::Time end = IceUtil::Time::now() + timeout;
while(_replies < expectedReplies)
{
IceUtil::Time delay = end - IceUtil::Time::now();
if(delay > IceUtil::Time::seconds(0))
{
timedWait(delay);
}
else
{
break;
}
}
return _replies == expectedReplies;
}
private:
int _replies;
};
ICE_DEFINE_PTR(PingReplyIPtr, PingReplyI);
void
allTests(const CommunicatorPtr& communicator)
{
communicator->getProperties()->setProperty("ReplyAdapter.Endpoints", "udp");
Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("ReplyAdapter");
PingReplyIPtr replyI = ICE_MAKE_SHARED(PingReplyI);
PingReplyPrxPtr reply = ICE_UNCHECKED_CAST(PingReplyPrx, adapter->addWithUUID(replyI))->ice_datagram();
adapter->activate();
cout << "testing udp... " << flush;
ObjectPrxPtr base = communicator->stringToProxy("test -d:" + getTestEndpoint(communicator, 0, "udp"));
TestIntfPrxPtr obj = ICE_UNCHECKED_CAST(TestIntfPrx, base);
int nRetry = 5;
bool ret;
while(nRetry-- > 0)
{
replyI->reset();
obj->ping(reply);
obj->ping(reply);
obj->ping(reply);
ret = replyI->waitReply(3, IceUtil::Time::seconds(2));
if(ret)
{
break; // Success
}
// If the 3 datagrams were not received within the 2 seconds, we try again to
// receive 3 new datagrams using a new object. We give up after 5 retries.
replyI = ICE_MAKE_SHARED(PingReplyI);
reply = ICE_UNCHECKED_CAST(PingReplyPrx, adapter->addWithUUID(replyI))->ice_datagram();
}
test(ret);
if(communicator->getProperties()->getPropertyAsInt("Ice.Override.Compress") == 0)
{
//
// Only run this test if compression is disabled, the test expect fixed message size
// to be sent over the wire.
//
Test::ByteSeq seq;
try
{
seq.resize(1024);
while(true)
{
seq.resize(seq.size() * 2 + 10);
replyI->reset();
obj->sendByteSeq(seq, reply);
replyI->waitReply(1, IceUtil::Time::seconds(10));
}
}
catch(const DatagramLimitException&)
{
test(seq.size() > 16384);
}
obj->ice_getConnection()->close(ICE_SCOPED_ENUM(ConnectionClose, GracefullyWithWait));
communicator->getProperties()->setProperty("Ice.UDP.SndSize", "64000");
seq.resize(50000);
try
{
replyI->reset();
obj->sendByteSeq(seq, reply);
test(!replyI->waitReply(1, IceUtil::Time::milliSeconds(500)));
}
catch(const Ice::LocalException& ex)
{
cerr << ex << endl;
test(false);
}
}
cout << "ok" << endl;
ostringstream endpoint;
if(communicator->getProperties()->getProperty("Ice.IPv6") == "1")
{
endpoint << "udp -h \"ff15::1:1\" -p " << getTestPort(communicator->getProperties(), 10);
#if defined(__APPLE__) || defined(_WIN32)
endpoint << " --interface \"::1\""; // Use loopback to prevent other machines to answer. the multicast requests.
#endif
}
else
{
endpoint << "udp -h 239.255.1.1 -p " << getTestPort(communicator->getProperties(), 10);
#if defined(__APPLE__) || defined(_WIN32)
endpoint << " --interface 127.0.0.1"; // Use loopback to prevent other machines to answer.
#endif
}
base = communicator->stringToProxy("test -d:" + endpoint.str());
TestIntfPrxPtr objMcast = ICE_UNCHECKED_CAST(TestIntfPrx, base);
#if (!defined(__APPLE__) || (defined(__APPLE__) && !TARGET_OS_IPHONE))
cout << "testing udp multicast... " << flush;
nRetry = 5;
while(nRetry-- > 0)
{
replyI->reset();
objMcast->ping(reply);
ret = replyI->waitReply(5, IceUtil::Time::seconds(2));
if(ret)
{
break; // Success
}
replyI = ICE_MAKE_SHARED(PingReplyI);
reply = ICE_UNCHECKED_CAST(PingReplyPrx, adapter->addWithUUID(replyI))->ice_datagram();
}
if(!ret)
{
cout << "failed (is a firewall enabled?)" << endl;
}
else
{
cout << "ok" << endl;
}
#endif
cout << "testing udp bi-dir connection... " << flush;
obj->ice_getConnection()->setAdapter(adapter);
objMcast->ice_getConnection()->setAdapter(adapter);
nRetry = 5;
while(nRetry-- > 0)
{
replyI->reset();
obj->pingBiDir(reply->ice_getIdentity());
obj->pingBiDir(reply->ice_getIdentity());
obj->pingBiDir(reply->ice_getIdentity());
ret = replyI->waitReply(3, IceUtil::Time::seconds(2));
if(ret)
{
break; // Success
}
// If the 3 datagrams were not received within the 2 seconds, we try again to
// receive 3 new datagrams using a new object. We give up after 5 retries.
replyI = ICE_MAKE_SHARED(PingReplyI);
reply = ICE_UNCHECKED_CAST(PingReplyPrx, adapter->addWithUUID(replyI))->ice_datagram();
}
test(ret);
cout << "ok" << endl;
//
// Sending the replies back on the multicast UDP connection doesn't work for most
// platform (it works for macOS Leopard but not Snow Leopard, doesn't work on SLES,
// Windows...). For Windows, see UdpTransceiver constructor for the details. So
// we don't run this test.
//
// cout << "testing udp bi-dir connection... " << flush;
// nRetry = 5;
// while(nRetry-- > 0)
// {
// replyI->reset();
// objMcast->pingBiDir(reply->ice_getIdentity());
// ret = replyI->waitReply(5, IceUtil::Time::seconds(2));
// if(ret)
// {
// break; // Success
// }
// replyI = new PingReplyI;
// reply = PingReplyPrx::uncheckedCast(adapter->addWithUUID(replyI))->ice_datagram();
// }
// if(!ret)
// {
// cout << "failed (is a firewall enabled?)" << endl;
// }
// else
// {
// cout << "ok" << endl;
// }
}
|