1//
2// Copyright (c) ZeroC, Inc. All rights reserved.
3//
4
5#import <acm/TestI.h>
6
7@implementation ACMConnectionCallbackI
8-(id) init
9{
10    self = [super init];
11    if(!self)
12    {
13        return nil;
14    }
15    _cond = [[NSCondition alloc] init];
16    _count = 0;
17    return self;
18}
19#if defined(__clang__) && !__has_feature(objc_arc)
20-(void) dealloc
21{
22    [_cond release];
23    [super dealloc];
24}
25#endif
26-(void) heartbeat:(id<ICEConnection>)__unused connection
27{
28    [_cond lock];
29    ++_count;
30    [_cond signal];
31    [_cond unlock];
32}
33-(void) waitForCount:(int)count
34{
35    [_cond lock];
36    @try
37    {
38        while(_count < count)
39        {
40            [_cond wait];
41        }
42    }
43    @finally
44    {
45        [_cond unlock];
46    }
47}
48@end
49
50@implementation ACMRemoteCommunicatorI
51-(id<TestACMRemoteObjectAdapterPrx>) createObjectAdapter:(ICEInt)timeout close:(ICEInt)close heartbeat:(ICEInt)heartbeat
52                                                 current:(ICECurrent*)current
53{
54    id<ICECommunicator> com = [current.adapter getCommunicator];
55    id<ICEProperties> properties = [com getProperties];
56    NSString* protocol = [properties getPropertyWithDefault:@"Ice.Default.Protocol" value:@"tcp"];
57    NSString* host = [properties getPropertyWithDefault:@"Ice.Default.Host" value:@"127.0.0.1"];
58
59    NSString* name = [ICEUtil generateUUID];
60    if(timeout >= 0)
61    {
62        [properties setProperty:[name stringByAppendingString:@".ACM.Timeout"]
63                          value:[NSString stringWithFormat:@"%d", timeout]];
64    }
65    if(close >= 0)
66    {
67        [properties setProperty:[name stringByAppendingString:@".ACM.Close"]
68                          value:[NSString stringWithFormat:@"%d", close]];
69    }
70    if(heartbeat >= 0)
71    {
72        [properties setProperty:[name stringByAppendingString:@".ACM.Heartbeat"]
73                          value:[NSString stringWithFormat:@"%d", heartbeat]];
74    }
75    [properties setProperty:[name stringByAppendingString:@".ThreadPool.Size"] value:@"12"];
76    id<ICEObjectAdapter> adapter =
77        [com createObjectAdapterWithEndpoints:name
78                                    endpoints:[NSString stringWithFormat:@"%@ -h \"%@\"", protocol, host]];
79
80    ACMRemoteObjectAdapterI* remoteAdapter = ICE_AUTORELEASE([[ACMRemoteObjectAdapterI alloc] initWithAdapter:adapter]);
81    return [TestACMRemoteObjectAdapterPrx uncheckedCast:[current.adapter addWithUUID:remoteAdapter]];
82}
83-(void) shutdown:(ICECurrent*)current
84{
85    [[current.adapter getCommunicator] shutdown];
86}
87@end
88
89@implementation ACMRemoteObjectAdapterI
90-(id) initWithAdapter:(id<ICEObjectAdapter>)adapter
91{
92    self = [super init];
93    if(!self)
94    {
95        return nil;
96    }
97    _adapter = ICE_RETAIN(adapter);
98    _testIntf = ICE_RETAIN([TestACMTestIntfPrx uncheckedCast:[_adapter add:[TestACMTestIntfI testIntf]
99                                                    identity:[ICEUtil stringToIdentity:@"test"]]]);
100    [_adapter activate];
101
102    return self;
103}
104#if defined(__clang__) && !__has_feature(objc_arc)
105-(void) dealloc
106{
107    [_adapter release];
108    [_testIntf release];
109    [super dealloc];
110}
111#endif
112
113-(id<TestACMTestIntfPrx>) getTestIntf:(ICECurrent*)__unused current
114{
115    return _testIntf;
116}
117-(void) activate:(ICECurrent*)__unused current
118{
119    [_adapter activate];
120}
121-(void) hold:(ICECurrent*)__unused current
122{
123    [_adapter hold];
124}
125-(void) deactivate:(ICECurrent*)__unused current
126{
127    @try
128    {
129        [_adapter activate];
130    }
131    @catch(ICEObjectAdapterDeactivatedException* ex)
132    {
133    }
134}
135@end
136
137@implementation TestACMTestIntfI
138-(id) init
139{
140    self = [super init];
141    if(!self)
142    {
143        return nil;
144    }
145    _cond = [[NSCondition alloc] init];
146    return self;
147}
148
149#if defined(__clang__) && !__has_feature(objc_arc)
150-(void) dealloc
151{
152    [_cond release];
153    [super dealloc];
154}
155#endif
156-(void) sleep:(ICEInt)delay current:(ICECurrent*)__unused current
157{
158    [_cond lock];
159    @try
160    {
161        [_cond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:delay]];
162    }
163    @finally
164    {
165        [_cond unlock];
166    }
167}
168-(void) sleepAndHold:(ICEInt)delay current:(ICECurrent*)current
169{
170    [_cond lock];
171    [current.adapter hold];
172    @try
173    {
174        [_cond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:delay]];
175    }
176    @finally
177    {
178        [_cond unlock];
179    }
180}
181-(void) interruptSleep:(ICECurrent*)__unused current
182{
183    [_cond lock];
184    [_cond signal];
185    [_cond unlock];
186}
187-(void) startHeartbeatCount:(ICECurrent*) current
188{
189    ACMConnectionCallbackI* callback = [ACMConnectionCallbackI new];
190    _callback = callback;
191    [current.con setHeartbeatCallback:^(id<ICEConnection> c)
192    {
193        [callback heartbeat:c];
194    }];
195}
196-(void) waitForHeartbeatCount:(int)count current:(ICECurrent*)__unused current
197{
198    [_callback waitForCount:count];
199    ICE_RELEASE(_callback);
200}
201@end
202