ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / test / Ice / interceptor / AMDInterceptorI.cpp
blob79048fbf4ee3b6b62a6af4c057eac4b89520e790
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #include <AMDInterceptorI.h>
11 #include <Test.h>
12 #include <TestCommon.h>
14 AMDInterceptorI::AMDInterceptorI(const Ice::ObjectPtr& servant) :
15 InterceptorI(servant),
16 _defaultCb(new DispatchInterceptorAsyncCallbackI(*this)),
17 _actualStatus(Ice::DispatchAsync)
23 Ice::DispatchStatus
24 AMDInterceptorI::dispatch(Ice::Request& request)
26 class CallbackI : public Ice::DispatchInterceptorAsyncCallback
28 public:
30 CallbackI(AMDInterceptorI& interceptor) :
31 _interceptor(interceptor)
35 virtual bool response(bool ok)
37 test(ok);
38 return false;
41 virtual bool exception(const std::exception& ex)
43 test(dynamic_cast<const Test::RetryException*>(&ex) != 0);
44 return false;
47 virtual bool exception()
50 // Unexpected
52 test(false);
53 return false;
56 private:
58 AMDInterceptorI& _interceptor;
62 Ice::Current& current = const_cast<Ice::Current&>(request.getCurrent());
63 _lastOperation = current.operation;
65 Ice::DispatchInterceptorAsyncCallbackPtr cb = new CallbackI(*this);
67 if(_lastOperation == "amdAddWithRetry")
69 for(int i = 0; i < 10; ++i)
71 _lastStatus = _servant->ice_dispatch(request, cb);
72 test(_lastStatus == Ice::DispatchAsync);
75 current.ctx["retry"] = "no";
78 _lastStatus = _servant->ice_dispatch(request, _defaultCb);
79 return _lastStatus;
82 void
83 AMDInterceptorI::setActualStatus(Ice::DispatchStatus status)
85 IceUtil::Mutex::Lock lock(_mutex);
86 _actualStatus = status;
89 void
90 AMDInterceptorI::setActualStatus(const IceUtil::Exception& e)
92 IceUtil::Mutex::Lock lock(_mutex);
93 _exception.reset(e.ice_clone());
94 _actualStatus = Ice::DispatchAsync;
97 Ice::DispatchStatus
98 AMDInterceptorI::getActualStatus() const
100 IceUtil::Mutex::Lock lock(_mutex);
101 return _actualStatus;
104 IceUtil::Exception*
105 AMDInterceptorI::getException() const
107 IceUtil::Mutex::Lock lock(_mutex);
108 return _exception.get();
111 void
112 AMDInterceptorI::clear()
114 InterceptorI::clear();
115 IceUtil::Mutex::Lock lock(_mutex);
116 _actualStatus = Ice::DispatchAsync;
117 _exception.reset();
121 DispatchInterceptorAsyncCallbackI::DispatchInterceptorAsyncCallbackI(AMDInterceptorI& interceptor) :
122 _interceptor(interceptor)
126 bool
127 DispatchInterceptorAsyncCallbackI::response(bool ok)
129 _interceptor.setActualStatus(ok ? Ice::DispatchOK : Ice::DispatchUserException);
130 return true;
134 bool
135 DispatchInterceptorAsyncCallbackI::exception(const std::exception& ex)
138 // Only Ice exceptions are raised by this test
140 const IceUtil::Exception& ue = dynamic_cast<const IceUtil::Exception&>(ex);
141 _interceptor.setActualStatus(ue);
142 return true;
146 bool
147 DispatchInterceptorAsyncCallbackI::exception()
150 // Unexpected
152 test(false);
153 return true;