第一步: 编写ice 接口文件 ,这个例子实现了两个接口
Callback.ice
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
#pragma once
#include <Ice/Identity.ice>
module Demo
{
interface CallbackReceiver
{
void callback(int num);
};
interface CallbackSender
{
void addClient(Ice::Identity ident);
};
};
第二步:Slice2cpp 生成辅助代码,客户端代码(Proxy)与服务端代码(适配器)
Callback.h Callback.cpp
Callback.h
// **********************************************************************
//
// Copyright (c) 2003-2016 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.
//
// **********************************************************************
//
// Ice version 3.6.3
//
// <auto-generated>
//
// Generated from file `Callback.ice'
//
// Warning: do not edit this file.
//
// </auto-generated>
//
#ifndef __Callback_h__
#define __Callback_h__
#include <IceUtil/PushDisableWarnings.h>
#include <Ice/ProxyF.h>
#include <Ice/ObjectF.h>
#include <Ice/Exception.h>
#include <Ice/LocalObject.h>
#include <Ice/StreamHelpers.h>
#include <Ice/Proxy.h>
#include <Ice/GCObject.h>
#include <Ice/AsyncResult.h>
#include <Ice/Incoming.h>
#include <IceUtil/ScopedArray.h>
#include <IceUtil/Optional.h>
#include <Ice/StreamF.h>
#include <Ice/Identity.h>
#include <IceUtil/UndefSysMacros.h>
#ifndef ICE_IGNORE_VERSION
# if ICE_INT_VERSION / 100 != 306
# error Ice version mismatch!
# endif
# if ICE_INT_VERSION % 100 > 50
# error Beta header file detected
# endif
# if ICE_INT_VERSION % 100 < 3
# error Ice patch level mismatch!
# endif
#endif
namespace IceProxy
{
namespace Demo
{
class CallbackReceiver;
void __read(::IceInternal::BasicStream*, ::IceInternal::ProxyHandle< ::IceProxy::Demo::CallbackReceiver>&);
::IceProxy::Ice::Object* upCast(::IceProxy::Demo::CallbackReceiver*);
class CallbackSender;
void __read(::IceInternal::BasicStream*, ::IceInternal::ProxyHandle< ::IceProxy::Demo::CallbackSender>&);
::IceProxy::Ice::Object* upCast(::IceProxy::Demo::CallbackSender*);
}
}
namespace Demo
{
class CallbackReceiver;
::Ice::Object* upCast(::Demo::CallbackReceiver*);
typedef ::IceInternal::Handle< ::Demo::CallbackReceiver> CallbackReceiverPtr;
typedef ::IceInternal::ProxyHandle< ::IceProxy::Demo::CallbackReceiver> CallbackReceiverPrx;
void __patch(CallbackReceiverPtr&, const ::Ice::ObjectPtr&);
class CallbackSender;
::Ice::Object* upCast(::Demo::CallbackSender*);
typedef ::IceInternal::Handle< ::Demo::CallbackSender> CallbackSenderPtr;
typedef ::IceInternal::ProxyHandle< ::IceProxy::Demo::CallbackSender> CallbackSenderPrx;
void __patch(CallbackSenderPtr&, const ::Ice::ObjectPtr&);
}
namespace Demo
{
class Callback_CallbackReceiver_callback_Base : virtual public ::IceInternal::CallbackBase { };
typedef ::IceUtil::Handle< Callback_CallbackReceiver_callback_Base> Callback_CallbackReceiver_callbackPtr;
class Callback_CallbackSender_addClient_Base : virtual public ::IceInternal::CallbackBase { };
typedef ::IceUtil::Handle< Callback_CallbackSender_addClient_Base> Callback_CallbackSender_addClientPtr;
}
namespace IceProxy
{
namespace Demo
{
class CallbackReceiver : virtual public ::IceProxy::Ice::Object
{
public:
void callback(::Ice::Int __p_num)
{
callback(__p_num, 0);
}
void callback(::Ice::Int __p_num, const ::Ice::Context& __ctx)
{
callback(__p_num, &__ctx);
}
#ifdef ICE_CPP11
::Ice::AsyncResultPtr
begin_callback(::Ice::Int __p_num, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())
{
return begin_callback(__p_num, 0, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent));
}
::Ice::AsyncResultPtr
begin_callback(::Ice::Int __p_num, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())
{
return begin_callback(__p_num, 0, ::Ice::newCallback(__completed, __sent), 0);
}
::Ice::AsyncResultPtr
begin_callback(::Ice::Int __p_num, const ::Ice::Context& __ctx, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())
{
return begin_callback(__p_num, &__ctx, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent), 0);
}
::Ice::AsyncResultPtr
begin_callback(::Ice::Int __p_num, const ::Ice::Context& __ctx, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())
{
return begin_callback(__p_num, &__ctx, ::Ice::newCallback(__completed, __sent));
}
#endif
::Ice::AsyncResultPtr begin_callback(::Ice::Int __p_num)
{
return begin_callback(__p_num, 0, ::IceInternal::__dummyCallback, 0);
}
::Ice::AsyncResultPtr begin_callback(::Ice::Int __p_num, const ::Ice::Context& __ctx)
{
return begin_callback(__p_num, &__ctx, ::IceInternal::__dummyCallback, 0);
}
::Ice::AsyncResultPtr begin_callback(::Ice::Int __p_num, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_callback(__p_num, 0, __del, __cookie);
}
::Ice::AsyncResultPtr begin_callback(::Ice::Int __p_num, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_callback(__p_num, &__ctx, __del, __cookie);
}
::Ice::AsyncResultPtr begin_callback(::Ice::Int __p_num, const ::Demo::Callback_CallbackReceiver_callbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_callback(__p_num, 0, __del, __cookie);
}
::Ice::AsyncResultPtr begin_callback(::Ice::Int __p_num, const ::Ice::Context& __ctx, const ::Demo::Callback_CallbackReceiver_callbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_callback(__p_num, &__ctx, __del, __cookie);
}
void end_callback(const ::Ice::AsyncResultPtr&);
private:
void callback(::Ice::Int, const ::Ice::Context*);
::Ice::AsyncResultPtr begin_callback(::Ice::Int, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);
public:
::IceInternal::ProxyHandle<CallbackReceiver> ice_context(const ::Ice::Context& __context) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_context(__context).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_adapterId(const ::std::string& __id) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_adapterId(__id).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_endpoints(const ::Ice::EndpointSeq& __endpoints) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_endpoints(__endpoints).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_locatorCacheTimeout(int __timeout) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_locatorCacheTimeout(__timeout).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_connectionCached(bool __cached) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_connectionCached(__cached).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_endpointSelection(::Ice::EndpointSelectionType __est) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_endpointSelection(__est).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_secure(bool __secure) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_secure(__secure).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_preferSecure(bool __preferSecure) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_preferSecure(__preferSecure).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_router(const ::Ice::RouterPrx& __router) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_router(__router).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_locator(const ::Ice::LocatorPrx& __locator) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_locator(__locator).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_collocationOptimized(bool __co) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_collocationOptimized(__co).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_invocationTimeout(int __timeout) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_invocationTimeout(__timeout).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_twoway() const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_twoway().get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_oneway() const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_oneway().get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_batchOneway() const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_batchOneway().get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_datagram() const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_datagram().get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_batchDatagram() const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_batchDatagram().get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_compress(bool __compress) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_compress(__compress).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_timeout(int __timeout) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_timeout(__timeout).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_connectionId(const ::std::string& __id) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_connectionId(__id).get());
}
::IceInternal::ProxyHandle<CallbackReceiver> ice_encodingVersion(const ::Ice::EncodingVersion& __v) const
{
return dynamic_cast<CallbackReceiver*>(::IceProxy::Ice::Object::ice_encodingVersion(__v).get());
}
static const ::std::string& ice_staticId();
private:
virtual ::IceProxy::Ice::Object* __newInstance() const;
};
class CallbackSender : virtual public ::IceProxy::Ice::Object
{
public:
void addClient(const ::Ice::Identity& __p_ident)
{
addClient(__p_ident, 0);
}
void addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context& __ctx)
{
addClient(__p_ident, &__ctx);
}
#ifdef ICE_CPP11
::Ice::AsyncResultPtr
begin_addClient(const ::Ice::Identity& __p_ident, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())
{
return begin_addClient(__p_ident, 0, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent));
}
::Ice::AsyncResultPtr
begin_addClient(const ::Ice::Identity& __p_ident, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())
{
return begin_addClient(__p_ident, 0, ::Ice::newCallback(__completed, __sent), 0);
}
::Ice::AsyncResultPtr
begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context& __ctx, const ::IceInternal::Function<void ()>& __response, const ::IceInternal::Function<void (const ::Ice::Exception&)>& __exception = ::IceInternal::Function<void (const ::Ice::Exception&)>(), const ::IceInternal::Function<void (bool)>& __sent = ::IceInternal::Function<void (bool)>())
{
return begin_addClient(__p_ident, &__ctx, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent), 0);
}
::Ice::AsyncResultPtr
begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context& __ctx, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())
{
return begin_addClient(__p_ident, &__ctx, ::Ice::newCallback(__completed, __sent));
}
#endif
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity& __p_ident)
{
return begin_addClient(__p_ident, 0, ::IceInternal::__dummyCallback, 0);
}
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context& __ctx)
{
return begin_addClient(__p_ident, &__ctx, ::IceInternal::__dummyCallback, 0);
}
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_addClient(__p_ident, 0, __del, __cookie);
}
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_addClient(__p_ident, &__ctx, __del, __cookie);
}
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity& __p_ident, const ::Demo::Callback_CallbackSender_addClientPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_addClient(__p_ident, 0, __del, __cookie);
}
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context& __ctx, const ::Demo::Callback_CallbackSender_addClientPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)
{
return begin_addClient(__p_ident, &__ctx, __del, __cookie);
}
void end_addClient(const ::Ice::AsyncResultPtr&);
private:
void addClient(const ::Ice::Identity&, const ::Ice::Context*);
::Ice::AsyncResultPtr begin_addClient(const ::Ice::Identity&, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);
public:
::IceInternal::ProxyHandle<CallbackSender> ice_context(const ::Ice::Context& __context) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_context(__context).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_adapterId(const ::std::string& __id) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_adapterId(__id).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_endpoints(const ::Ice::EndpointSeq& __endpoints) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_endpoints(__endpoints).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_locatorCacheTimeout(int __timeout) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_locatorCacheTimeout(__timeout).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_connectionCached(bool __cached) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_connectionCached(__cached).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_endpointSelection(::Ice::EndpointSelectionType __est) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_endpointSelection(__est).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_secure(bool __secure) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_secure(__secure).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_preferSecure(bool __preferSecure) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_preferSecure(__preferSecure).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_router(const ::Ice::RouterPrx& __router) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_router(__router).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_locator(const ::Ice::LocatorPrx& __locator) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_locator(__locator).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_collocationOptimized(bool __co) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_collocationOptimized(__co).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_invocationTimeout(int __timeout) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_invocationTimeout(__timeout).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_twoway() const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_twoway().get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_oneway() const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_oneway().get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_batchOneway() const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_batchOneway().get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_datagram() const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_datagram().get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_batchDatagram() const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_batchDatagram().get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_compress(bool __compress) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_compress(__compress).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_timeout(int __timeout) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_timeout(__timeout).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_connectionId(const ::std::string& __id) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_connectionId(__id).get());
}
::IceInternal::ProxyHandle<CallbackSender> ice_encodingVersion(const ::Ice::EncodingVersion& __v) const
{
return dynamic_cast<CallbackSender*>(::IceProxy::Ice::Object::ice_encodingVersion(__v).get());
}
static const ::std::string& ice_staticId();
private:
virtual ::IceProxy::Ice::Object* __newInstance() const;
};
}
}
namespace Demo
{
class CallbackReceiver : virtual public ::Ice::Object
{
public:
typedef CallbackReceiverPrx ProxyType;
typedef CallbackReceiverPtr PointerType;
virtual bool ice_isA(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) const;
virtual ::std::vector< ::std::string> ice_ids(const ::Ice::Current& = ::Ice::Current()) const;
virtual const ::std::string& ice_id(const ::Ice::Current& = ::Ice::Current()) const;
static const ::std::string& ice_staticId();
virtual void callback(::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;
::Ice::DispatchStatus ___callback(::IceInternal::Incoming&, const ::Ice::Current&);
virtual ::Ice::DispatchStatus __dispatch(::IceInternal::Incoming&, const ::Ice::Current&);
protected:
virtual void __writeImpl(::IceInternal::BasicStream*) const;
virtual void __readImpl(::IceInternal::BasicStream*);
using ::Ice::Object::__writeImpl;
using ::Ice::Object::__readImpl;
};
inline bool operator==(const CallbackReceiver& l, const CallbackReceiver& r)
{
return static_cast<const ::Ice::Object&>(l) == static_cast<const ::Ice::Object&>(r);
}
inline bool operator<(const CallbackReceiver& l, const CallbackReceiver& r)
{
return static_cast<const ::Ice::Object&>(l) < static_cast<const ::Ice::Object&>(r);
}
class CallbackSender : virtual public ::Ice::Object
{
public:
typedef CallbackSenderPrx ProxyType;
typedef CallbackSenderPtr PointerType;
virtual bool ice_isA(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) const;
virtual ::std::vector< ::std::string> ice_ids(const ::Ice::Current& = ::Ice::Current()) const;
virtual const ::std::string& ice_id(const ::Ice::Current& = ::Ice::Current()) const;
static const ::std::string& ice_staticId();
virtual void addClient(const ::Ice::Identity&, const ::Ice::Current& = ::Ice::Current()) = 0;
::Ice::DispatchStatus ___addClient(::IceInternal::Incoming&, const ::Ice::Current&);
virtual ::Ice::DispatchStatus __dispatch(::IceInternal::Incoming&, const ::Ice::Current&);
protected:
virtual void __writeImpl(::IceInternal::BasicStream*) const;
virtual void __readImpl(::IceInternal::BasicStream*);
using ::Ice::Object::__writeImpl;
using ::Ice::Object::__readImpl;
};
inline bool operator==(const CallbackSender& l, const CallbackSender& r)
{
return static_cast<const ::Ice::Object&>(l) == static_cast<const ::Ice::Object&>(r);
}
inline bool operator<(const CallbackSender& l, const CallbackSender& r)
{
return static_cast<const ::Ice::Object&>(l) < static_cast<const ::Ice::Object&>(r);
}
}
namespace Demo
{
template<class T>
class CallbackNC_CallbackReceiver_callback : public Callback_CallbackReceiver_callback_Base, public ::IceInternal::OnewayCallbackNC<T>
{
public:
typedef IceUtil::Handle<T> TPtr;
typedef void (T::*Exception)(const ::Ice::Exception&);
typedef void (T::*Sent)(bool);
typedef void (T::*Response)();
CallbackNC_CallbackReceiver_callback(const TPtr& obj, Response cb, Exception excb, Sent sentcb)
: ::IceInternal::OnewayCallbackNC<T>(obj, cb, excb, sentcb)
{
}
};
template<class T> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackReceiver_callback<T>(instance, cb, excb, sentcb);
}
template<class T> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackReceiver_callback<T>(instance, 0, excb, sentcb);
}
template<class T> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackReceiver_callback<T>(instance, cb, excb, sentcb);
}
template<class T> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackReceiver_callback<T>(instance, 0, excb, sentcb);
}
template<class T, typename CT>
class Callback_CallbackReceiver_callback : public Callback_CallbackReceiver_callback_Base, public ::IceInternal::OnewayCallback<T, CT>
{
public:
typedef IceUtil::Handle<T> TPtr;
typedef void (T::*Exception)(const ::Ice::Exception& , const CT&);
typedef void (T::*Sent)(bool , const CT&);
typedef void (T::*Response)(const CT&);
Callback_CallbackReceiver_callback(const TPtr& obj, Response cb, Exception excb, Sent sentcb)
: ::IceInternal::OnewayCallback<T, CT>(obj, cb, excb, sentcb)
{
}
};
template<class T, typename CT> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(const IceUtil::Handle<T>& instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackReceiver_callback<T, CT>(instance, cb, excb, sentcb);
}
template<class T, typename CT> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackReceiver_callback<T, CT>(instance, 0, excb, sentcb);
}
template<class T, typename CT> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(T* instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackReceiver_callback<T, CT>(instance, cb, excb, sentcb);
}
template<class T, typename CT> Callback_CallbackReceiver_callbackPtr
newCallback_CallbackReceiver_callback(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackReceiver_callback<T, CT>(instance, 0, excb, sentcb);
}
template<class T>
class CallbackNC_CallbackSender_addClient : public Callback_CallbackSender_addClient_Base, public ::IceInternal::OnewayCallbackNC<T>
{
public:
typedef IceUtil::Handle<T> TPtr;
typedef void (T::*Exception)(const ::Ice::Exception&);
typedef void (T::*Sent)(bool);
typedef void (T::*Response)();
CallbackNC_CallbackSender_addClient(const TPtr& obj, Response cb, Exception excb, Sent sentcb)
: ::IceInternal::OnewayCallbackNC<T>(obj, cb, excb, sentcb)
{
}
};
template<class T> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackSender_addClient<T>(instance, cb, excb, sentcb);
}
template<class T> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackSender_addClient<T>(instance, 0, excb, sentcb);
}
template<class T> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackSender_addClient<T>(instance, cb, excb, sentcb);
}
template<class T> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0)
{
return new CallbackNC_CallbackSender_addClient<T>(instance, 0, excb, sentcb);
}
template<class T, typename CT>
class Callback_CallbackSender_addClient : public Callback_CallbackSender_addClient_Base, public ::IceInternal::OnewayCallback<T, CT>
{
public:
typedef IceUtil::Handle<T> TPtr;
typedef void (T::*Exception)(const ::Ice::Exception& , const CT&);
typedef void (T::*Sent)(bool , const CT&);
typedef void (T::*Response)(const CT&);
Callback_CallbackSender_addClient(const TPtr& obj, Response cb, Exception excb, Sent sentcb)
: ::IceInternal::OnewayCallback<T, CT>(obj, cb, excb, sentcb)
{
}
};
template<class T, typename CT> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(const IceUtil::Handle<T>& instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackSender_addClient<T, CT>(instance, cb, excb, sentcb);
}
template<class T, typename CT> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackSender_addClient<T, CT>(instance, 0, excb, sentcb);
}
template<class T, typename CT> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(T* instance, void (T::*cb)(const CT&), void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackSender_addClient<T, CT>(instance, cb, excb, sentcb);
}
template<class T, typename CT> Callback_CallbackSender_addClientPtr
newCallback_CallbackSender_addClient(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0)
{
return new Callback_CallbackSender_addClient<T, CT>(instance, 0, excb, sentcb);
}
}
#include <IceUtil/PopDisableWarnings.h>
#endif
Callback.cpp
// **********************************************************************
//
// Copyright (c) 2003-2016 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.
//
// **********************************************************************
//
// Ice version 3.6.3
//
// <auto-generated>
//
// Generated from file `Callback.ice'
//
// Warning: do not edit this file.
//
// </auto-generated>
//
#include <Callback.h>
#include <IceUtil/PushDisableWarnings.h>
#include <Ice/LocalException.h>
#include <Ice/ObjectFactory.h>
#include <Ice/Outgoing.h>
#include <Ice/OutgoingAsync.h>
#include <Ice/BasicStream.h>
#include <IceUtil/Iterator.h>
#include <IceUtil/PopDisableWarnings.h>
#ifndef ICE_IGNORE_VERSION
# if ICE_INT_VERSION / 100 != 306
# error Ice version mismatch!
# endif
# if ICE_INT_VERSION % 100 > 50
# error Beta header file detected
# endif
# if ICE_INT_VERSION % 100 < 3
# error Ice patch level mismatch!
# endif
#endif
namespace
{
const ::std::string __Demo__CallbackReceiver__callback_name = "callback";
const ::std::string __Demo__CallbackSender__addClient_name = "addClient";
}
::IceProxy::Ice::Object* ::IceProxy::Demo::upCast(::IceProxy::Demo::CallbackReceiver* p) { return p; }
void
::IceProxy::Demo::__read(::IceInternal::BasicStream* __is, ::IceInternal::ProxyHandle< ::IceProxy::Demo::CallbackReceiver>& v)
{
::Ice::ObjectPrx proxy;
__is->read(proxy);
if(!proxy)
{
v = 0;
}
else
{
v = new ::IceProxy::Demo::CallbackReceiver;
v->__copyFrom(proxy);
}
}
void
IceProxy::Demo::CallbackReceiver::callback(::Ice::Int __p_num, const ::Ice::Context* __ctx)
{
::IceInternal::Outgoing __og(this, __Demo__CallbackReceiver__callback_name, ::Ice::Normal, __ctx);
try
{
::IceInternal::BasicStream* __os = __og.startWriteParams(::Ice::DefaultFormat);
__os->write(__p_num);
__og.endWriteParams();
}
catch(const ::Ice::LocalException& __ex)
{
__og.abort(__ex);
}
__invoke(__og);
}
::Ice::AsyncResultPtr
IceProxy::Demo::CallbackReceiver::begin_callback(::Ice::Int __p_num, const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie)
{
::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __Demo__CallbackReceiver__callback_name, __del, __cookie);
try
{
__result->prepare(__Demo__CallbackReceiver__callback_name, ::Ice::Normal, __ctx);
::IceInternal::BasicStream* __os = __result->startWriteParams(::Ice::DefaultFormat);
__os->write(__p_num);
__result->endWriteParams();
__result->invoke();
}
catch(const ::Ice::Exception& __ex)
{
__result->abort(__ex);
}
return __result;
}
void
IceProxy::Demo::CallbackReceiver::end_callback(const ::Ice::AsyncResultPtr& __result)
{
__end(__result, __Demo__CallbackReceiver__callback_name);
}
const ::std::string&
IceProxy::Demo::CallbackReceiver::ice_staticId()
{
return ::Demo::CallbackReceiver::ice_staticId();
}
::IceProxy::Ice::Object*
IceProxy::Demo::CallbackReceiver::__newInstance() const
{
return new CallbackReceiver;
}
::IceProxy::Ice::Object* ::IceProxy::Demo::upCast(::IceProxy::Demo::CallbackSender* p) { return p; }
void
::IceProxy::Demo::__read(::IceInternal::BasicStream* __is, ::IceInternal::ProxyHandle< ::IceProxy::Demo::CallbackSender>& v)
{
::Ice::ObjectPrx proxy;
__is->read(proxy);
if(!proxy)
{
v = 0;
}
else
{
v = new ::IceProxy::Demo::CallbackSender;
v->__copyFrom(proxy);
}
}
void
IceProxy::Demo::CallbackSender::addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context* __ctx)
{
::IceInternal::Outgoing __og(this, __Demo__CallbackSender__addClient_name, ::Ice::Normal, __ctx);
try
{
::IceInternal::BasicStream* __os = __og.startWriteParams(::Ice::DefaultFormat);
__os->write(__p_ident);
__og.endWriteParams();
}
catch(const ::Ice::LocalException& __ex)
{
__og.abort(__ex);
}
__invoke(__og);
}
::Ice::AsyncResultPtr
IceProxy::Demo::CallbackSender::begin_addClient(const ::Ice::Identity& __p_ident, const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie)
{
::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __Demo__CallbackSender__addClient_name, __del, __cookie);
try
{
__result->prepare(__Demo__CallbackSender__addClient_name, ::Ice::Normal, __ctx);
::IceInternal::BasicStream* __os = __result->startWriteParams(::Ice::DefaultFormat);
__os->write(__p_ident);
__result->endWriteParams();
__result->invoke();
}
catch(const ::Ice::Exception& __ex)
{
__result->abort(__ex);
}
return __result;
}
void
IceProxy::Demo::CallbackSender::end_addClient(const ::Ice::AsyncResultPtr& __result)
{
__end(__result, __Demo__CallbackSender__addClient_name);
}
const ::std::string&
IceProxy::Demo::CallbackSender::ice_staticId()
{
return ::Demo::CallbackSender::ice_staticId();
}
::IceProxy::Ice::Object*
IceProxy::Demo::CallbackSender::__newInstance() const
{
return new CallbackSender;
}
::Ice::Object* Demo::upCast(::Demo::CallbackReceiver* p) { return p; }
namespace
{
const ::std::string __Demo__CallbackReceiver_ids[2] =
{
"::Demo::CallbackReceiver",
"::Ice::Object"
};
}
bool
Demo::CallbackReceiver::ice_isA(const ::std::string& _s, const ::Ice::Current&) const
{
return ::std::binary_search(__Demo__CallbackReceiver_ids, __Demo__CallbackReceiver_ids + 2, _s);
}
::std::vector< ::std::string>
Demo::CallbackReceiver::ice_ids(const ::Ice::Current&) const
{
return ::std::vector< ::std::string>(&__Demo__CallbackReceiver_ids[0], &__Demo__CallbackReceiver_ids[2]);
}
const ::std::string&
Demo::CallbackReceiver::ice_id(const ::Ice::Current&) const
{
return __Demo__CallbackReceiver_ids[0];
}
const ::std::string&
Demo::CallbackReceiver::ice_staticId()
{
#ifdef ICE_HAS_THREAD_SAFE_LOCAL_STATIC
static const ::std::string typeId = "::Demo::CallbackReceiver";
return typeId;
#else
return __Demo__CallbackReceiver_ids[0];
#endif
}
::Ice::DispatchStatus
Demo::CallbackReceiver::___callback(::IceInternal::Incoming& __inS, const ::Ice::Current& __current)
{
__checkMode(::Ice::Normal, __current.mode);
::IceInternal::BasicStream* __is = __inS.startReadParams();
::Ice::Int __p_num;
__is->read(__p_num);
__inS.endReadParams();
callback(__p_num, __current);
__inS.__writeEmptyParams();
return ::Ice::DispatchOK;
}
namespace
{
const ::std::string __Demo__CallbackReceiver_all[] =
{
"callback",
"ice_id",
"ice_ids",
"ice_isA",
"ice_ping"
};
}
::Ice::DispatchStatus
Demo::CallbackReceiver::__dispatch(::IceInternal::Incoming& in, const ::Ice::Current& current)
{
::std::pair< const ::std::string*, const ::std::string*> r = ::std::equal_range(__Demo__CallbackReceiver_all, __Demo__CallbackReceiver_all + 5, current.operation);
if(r.first == r.second)
{
throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}
switch(r.first - __Demo__CallbackReceiver_all)
{
case 0:
{
return ___callback(in, current);
}
case 1:
{
return ___ice_id(in, current);
}
case 2:
{
return ___ice_ids(in, current);
}
case 3:
{
return ___ice_isA(in, current);
}
case 4:
{
return ___ice_ping(in, current);
}
}
assert(false);
throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}
void
Demo::CallbackReceiver::__writeImpl(::IceInternal::BasicStream* __os) const
{
__os->startWriteSlice(ice_staticId(), -1, true);
__os->endWriteSlice();
}
void
Demo::CallbackReceiver::__readImpl(::IceInternal::BasicStream* __is)
{
__is->startReadSlice();
__is->endReadSlice();
}
void
Demo::__patch(CallbackReceiverPtr& handle, const ::Ice::ObjectPtr& v)
{
handle = ::Demo::CallbackReceiverPtr::dynamicCast(v);
if(v && !handle)
{
IceInternal::Ex::throwUOE(::Demo::CallbackReceiver::ice_staticId(), v);
}
}
::Ice::Object* Demo::upCast(::Demo::CallbackSender* p) { return p; }
namespace
{
const ::std::string __Demo__CallbackSender_ids[2] =
{
"::Demo::CallbackSender",
"::Ice::Object"
};
}
bool
Demo::CallbackSender::ice_isA(const ::std::string& _s, const ::Ice::Current&) const
{
return ::std::binary_search(__Demo__CallbackSender_ids, __Demo__CallbackSender_ids + 2, _s);
}
::std::vector< ::std::string>
Demo::CallbackSender::ice_ids(const ::Ice::Current&) const
{
return ::std::vector< ::std::string>(&__Demo__CallbackSender_ids[0], &__Demo__CallbackSender_ids[2]);
}
const ::std::string&
Demo::CallbackSender::ice_id(const ::Ice::Current&) const
{
return __Demo__CallbackSender_ids[0];
}
const ::std::string&
Demo::CallbackSender::ice_staticId()
{
#ifdef ICE_HAS_THREAD_SAFE_LOCAL_STATIC
static const ::std::string typeId = "::Demo::CallbackSender";
return typeId;
#else
return __Demo__CallbackSender_ids[0];
#endif
}
::Ice::DispatchStatus
Demo::CallbackSender::___addClient(::IceInternal::Incoming& __inS, const ::Ice::Current& __current)
{
__checkMode(::Ice::Normal, __current.mode);
::IceInternal::BasicStream* __is = __inS.startReadParams();
::Ice::Identity __p_ident;
__is->read(__p_ident);
__inS.endReadParams();
addClient(__p_ident, __current);
__inS.__writeEmptyParams();
return ::Ice::DispatchOK;
}
namespace
{
const ::std::string __Demo__CallbackSender_all[] =
{
"addClient",
"ice_id",
"ice_ids",
"ice_isA",
"ice_ping"
};
}
::Ice::DispatchStatus
Demo::CallbackSender::__dispatch(::IceInternal::Incoming& in, const ::Ice::Current& current)
{
::std::pair< const ::std::string*, const ::std::string*> r = ::std::equal_range(__Demo__CallbackSender_all, __Demo__CallbackSender_all + 5, current.operation);
if(r.first == r.second)
{
throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}
switch(r.first - __Demo__CallbackSender_all)
{
case 0:
{
return ___addClient(in, current);
}
case 1:
{
return ___ice_id(in, current);
}
case 2:
{
return ___ice_ids(in, current);
}
case 3:
{
return ___ice_isA(in, current);
}
case 4:
{
return ___ice_ping(in, current);
}
}
assert(false);
throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}
void
Demo::CallbackSender::__writeImpl(::IceInternal::BasicStream* __os) const
{
__os->startWriteSlice(ice_staticId(), -1, true);
__os->endWriteSlice();
}
void
Demo::CallbackSender::__readImpl(::IceInternal::BasicStream* __is)
{
__is->startReadSlice();
__is->endReadSlice();
}
void
Demo::__patch(CallbackSenderPtr& handle, const ::Ice::ObjectPtr& v)
{
handle = ::Demo::CallbackSenderPtr::dynamicCast(v);
if(v && !handle)
{
IceInternal::Ex::throwUOE(::Demo::CallbackSender::ice_staticId(), v);
}
}
第三步:根据业务实现接口
接口CallbackSender的实现,这个接口由服务端使用,同时CallbackReceiver接口在client.cpp中实现,由客户端使用
CallbackI.h
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
#ifndef CALLBACK_I_H
#define CALLBACK_I_H
#include <IceUtil/IceUtil.h>
#include <Callback.h>
#include <set>
class CallbackSenderI;
typedef IceUtil::Handle<CallbackSenderI> CallbackSenderIPtr;
class CallbackSenderI : public Demo::CallbackSender, public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
{
public:
CallbackSenderI(const Ice::CommunicatorPtr&);
void destroy();
virtual void addClient(const Ice::Identity&, const Ice::Current&);
virtual void run();
private:
Ice::CommunicatorPtr _communicator;
bool _destroy;
std::set<Demo::CallbackReceiverPrx> _clients;
};
#endif
CallbackI.cpp
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <CallbackI.h>
using namespace std;
using namespace Ice;
using namespace Demo;
CallbackSenderI::CallbackSenderI(const Ice::CommunicatorPtr& communicator) :
_communicator(communicator), _destroy(false)
{
}
void
CallbackSenderI::destroy()
{
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lck(*this);
cout << "destroying callback sender" << endl;
_destroy = true;
notify();
}
getThreadControl().join();
}
void
CallbackSenderI::addClient(const Identity& ident, const Current& current)
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lck(*this);
cout << "adding client `" << _communicator->identityToString(ident) << "'"<< endl;
CallbackReceiverPrx client = CallbackReceiverPrx::uncheckedCast(current.con->createProxy(ident));
_clients.insert(client);
}
void
CallbackSenderI::run()
{
int num = 0;
bool destroyed = false;
while(!destroyed)
{
std::set<Demo::CallbackReceiverPrx> clients;
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lck(*this);
timedWait(IceUtil::Time::seconds(2));
if(_destroy)
{
destroyed = true;
continue;
}
clients = _clients;
}
if(!clients.empty())
{
++num;
for(set<CallbackReceiverPrx>::iterator p = clients.begin(); p != clients.end(); ++p)
{
try
{
(*p)->callback(num);
}
catch(const Exception& ex)
{
cerr << "removing client `" << _communicator->identityToString((*p)->ice_getIdentity()) << "':\n"
<< ex << endl;
IceUtil::Monitor<IceUtil::Mutex>::Lock lck(*this);
_clients.erase(*p);
}
}
}
}
}
第四步:实现客户端与服务器
客户端实际就是实现了CallbackReceiver
说明:
1) 通信器communitor()
2)得到 CallbackSenderPrx server = communicator()->propertyToProxy("CallbackSender.Proxy")
3)为了实现服务器回调,客户端通过 communicator() 生成了一个空的适配器,相当于将客户端变成了一个服务器
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("");
4)为servant CallbackReceiverI生成id ,并加入适配器
Ice::Identity ident;
ident.name = IceUtil::generateUUID();
ident.category = "";
CallbackReceiverPtr cr = new CallbackReceiverI;
adapter->add(cr, ident);
ident.name = IceUtil::generateUUID();
ident.category = "";
CallbackReceiverPtr cr = new CallbackReceiverI;
adapter->add(cr, ident);
5) 激活适配器
adapter->activate();
6)取客户端的通信器,并将上面的适配器加入这个通信器 server->ice_getConnection()->setAdapter(adapter);
7)将适配器做为server的客户端
server->addClient(ident);
8) 通信器检测事件并等待结束
communicator()->waitForShutdown();
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
#include <IceUtil/IceUtil.h>
#include <Ice/Ice.h>
#include <Callback.h>
using namespace std;
using namespace Demo;
class CallbackReceiverI : public CallbackReceiver
{
public:
virtual void
callback(Ice::Int num, const Ice::Current&)
{
cout << "received callback #" << num << endl;
}
};
class CallbackClient : public Ice::Application
{
public:
virtual int run(int, char*[]);
};
int
main(int argc, char* argv[])
{
CallbackClient app;
return app.main(argc, argv, "config.client");
}
int
CallbackClient::run(int argc, char*[])
{
if(argc > 1)
{
cerr << appName() << ": too many arguments" << endl;
return EXIT_FAILURE;
}
CallbackSenderPrx server = CallbackSenderPrx::checkedCast(communicator()->propertyToProxy("CallbackSender.Proxy"));
if(!server)
{
cerr << appName() << ": invalid proxy" << endl;
return EXIT_FAILURE;
}
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("");
Ice::Identity ident;
ident.name = IceUtil::generateUUID();
ident.category = "";
CallbackReceiverPtr cr = new CallbackReceiverI;
adapter->add(cr, ident);
adapter->activate();
server->ice_getConnection()->setAdapter(adapter);
server->addClient(ident);
communicator()->waitForShutdown();
return EXIT_SUCCESS;
}
server.cpp
说明:
1) 创建适配器时,Callback.Server 对应config.server 文件中的 Callback.Server.Endpoints=tcp -p 10000:ws -p 10002
2) 创建servant CallbackSenderIPtr sender = new CallbackSenderI(communicator());
3)将servant 加入适配器 adapter->add(sender, communicator()->stringToIdentity("sender"));
4) 激活适配器 .
adapter->activate();
5) svervant CallbackSenderI 除了实现Callback.ice中定义的接口外,还继承了IceUtil::Thread IceUtil::Monitor<IceUtil::Mutex>两个类,
使svervant CallbackSenderI 具有线程能力
启动线程 sender->start();
6)IceUtil::Monitor<IceUtil::Mutex> 这个类还不太清楚做什么,后面再回来改吧
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <CallbackI.h>
using namespace std;
using namespace Demo;
class CallbackServer : public Ice::Application
{
public:
virtual int run(int, char*[]);
};
int
main(int argc, char* argv[])
{
CallbackServer app;
return app.main(argc, argv, "config.server");
}
int
CallbackServer::run(int argc, char*[])
{
if(argc > 1)
{
cerr << appName() << ": too many arguments" << endl;
return EXIT_FAILURE;
}
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Callback.Server");
CallbackSenderIPtr sender = new CallbackSenderI(communicator());
adapter->add(sender, communicator()->stringToIdentity("sender"));
adapter->activate();
sender->start();
try
{
communicator()->waitForShutdown();
}
catch(...)
{
sender->destroy();
throw;
}
sender->destroy();
return EXIT_SUCCESS;
}
五 配置文件
config.client
#
# The client reads this property to create the reference to the
# "CallbackSender" object in the server.
#
CallbackSender.Proxy=sender:tcp -p 10000
#
# Uncomment to use the WebSocket transport instead.
#
#CallbackSender.Proxy=sender:ws -p 10002
#
# Only connect to the localhost interface by default.
#
Ice.Default.Host=localhost
#
# Warn about connection exceptions
#
Ice.Warn.Connections=1
#
# Network Tracing
#
# 0 = no network tracing
# 1 = trace connection establishment and closure
# 2 = like 1, but more detailed
# 3 = like 2, but also trace data transfer
#
#Ice.Trace.Network=1
#
# Protocol Tracing
#
# 0 = no protocol tracing
# 1 = trace protocol messages
#
#Ice.Trace.Protocol=1
config.server
#
# The server creates one single object adapter with the name
# "Callback.Server". The following line sets the endpoints for this
# adapter.
#
Callback.Server.Endpoints=tcp -p 10000:ws -p 10002
#
# Only listen on the localhost interface by default.
#
Ice.Default.Host=localhost
#
# Warn about connection exceptions
#
Ice.Warn.Connections=1
#
# Network Tracing
#
# 0 = no network tracing
# 1 = trace connection establishment and closure
# 2 = like 1, but more detailed
# 3 = like 2, but also trace data transfer
#
#Ice.Trace.Network=1
#
# Protocol Tracing
#
# 0 = no protocol tracing
# 1 = trace protocol messages
#
#Ice.Trace.Protocol=1