ice hello

来源:互联网 发布:阿里云属于paas 编辑:程序博客网 时间:2024/06/06 07:34

第一步:写ice 接口描述语言 (Hello.ice)

// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#pragma oncemodule Demo{interface Hello{    idempotent void sayHello(int delay);    void shutdown();};};


第二步:使用Slice2cpp 生成客户端代码(Proxy)与服务端代码(有的术语叫存根stub,ice里称为适配器)
会生成几个文件  Hello.h Hello.cpp

Hello.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.2//// <auto-generated>//// Generated from file `Hello.ice'//// Warning: do not edit this file.//// </auto-generated>//#ifndef __Hello_h__#define __Hello_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 <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 < 2#       error Ice patch level mismatch!#   endif#endifnamespace IceProxy{namespace Demo{class Hello;void __read(::IceInternal::BasicStream*, ::IceInternal::ProxyHandle< ::IceProxy::Demo::Hello>&);::IceProxy::Ice::Object* upCast(::IceProxy::Demo::Hello*);}}namespace Demo{class Hello;::Ice::Object* upCast(::Demo::Hello*);typedef ::IceInternal::Handle< ::Demo::Hello> HelloPtr;typedef ::IceInternal::ProxyHandle< ::IceProxy::Demo::Hello> HelloPrx;void __patch(HelloPtr&, const ::Ice::ObjectPtr&);}namespace Demo{class Callback_Hello_sayHello_Base : virtual public ::IceInternal::CallbackBase { };typedef ::IceUtil::Handle< Callback_Hello_sayHello_Base> Callback_Hello_sayHelloPtr;class Callback_Hello_shutdown_Base : virtual public ::IceInternal::CallbackBase { };typedef ::IceUtil::Handle< Callback_Hello_shutdown_Base> Callback_Hello_shutdownPtr;}namespace IceProxy{namespace Demo{class Hello : virtual public ::IceProxy::Ice::Object{public:    void sayHello(::Ice::Int __p_delay)    {        sayHello(__p_delay, 0);    }    void sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx)    {        sayHello(__p_delay, &__ctx);    }#ifdef ICE_CPP11    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, 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_sayHello(__p_delay, 0, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent));    }    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())    {        return begin_sayHello(__p_delay, 0, ::Ice::newCallback(__completed, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, 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_sayHello(__p_delay, &__ctx, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_sayHello(::Ice::Int __p_delay, 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_sayHello(__p_delay, &__ctx, ::Ice::newCallback(__completed, __sent));    }#endif    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay)    {        return begin_sayHello(__p_delay, 0, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx)    {        return begin_sayHello(__p_delay, &__ctx, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, 0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, &__ctx, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Demo::Callback_Hello_sayHelloPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, 0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context& __ctx, const ::Demo::Callback_Hello_sayHelloPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_sayHello(__p_delay, &__ctx, __del, __cookie);    }    void end_sayHello(const ::Ice::AsyncResultPtr&);    private:    void sayHello(::Ice::Int, const ::Ice::Context*);    ::Ice::AsyncResultPtr begin_sayHello(::Ice::Int, const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);    public:    void shutdown()    {        shutdown(0);    }    void shutdown(const ::Ice::Context& __ctx)    {        shutdown(&__ctx);    }#ifdef ICE_CPP11    ::Ice::AsyncResultPtr    begin_shutdown(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_shutdown(0, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent));    }    ::Ice::AsyncResultPtr    begin_shutdown(const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __completed, const ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>& __sent = ::IceInternal::Function<void (const ::Ice::AsyncResultPtr&)>())    {        return begin_shutdown(0, ::Ice::newCallback(__completed, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_shutdown(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_shutdown(&__ctx, new ::IceInternal::Cpp11FnOnewayCallbackNC(__response, __exception, __sent), 0);    }    ::Ice::AsyncResultPtr    begin_shutdown(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_shutdown(&__ctx, ::Ice::newCallback(__completed, __sent));    }#endif    ::Ice::AsyncResultPtr begin_shutdown()    {        return begin_shutdown(0, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context& __ctx)    {        return begin_shutdown(&__ctx, ::IceInternal::__dummyCallback, 0);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context& __ctx, const ::Ice::CallbackPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(&__ctx, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Demo::Callback_Hello_shutdownPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(0, __del, __cookie);    }    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context& __ctx, const ::Demo::Callback_Hello_shutdownPtr& __del, const ::Ice::LocalObjectPtr& __cookie = 0)    {        return begin_shutdown(&__ctx, __del, __cookie);    }    void end_shutdown(const ::Ice::AsyncResultPtr&);    private:    void shutdown(const ::Ice::Context*);    ::Ice::AsyncResultPtr begin_shutdown(const ::Ice::Context*, const ::IceInternal::CallbackBasePtr&, const ::Ice::LocalObjectPtr& __cookie = 0);    public:        ::IceInternal::ProxyHandle<Hello> ice_context(const ::Ice::Context& __context) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_context(__context).get());    }        ::IceInternal::ProxyHandle<Hello> ice_adapterId(const ::std::string& __id) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_adapterId(__id).get());    }        ::IceInternal::ProxyHandle<Hello> ice_endpoints(const ::Ice::EndpointSeq& __endpoints) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_endpoints(__endpoints).get());    }        ::IceInternal::ProxyHandle<Hello> ice_locatorCacheTimeout(int __timeout) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_locatorCacheTimeout(__timeout).get());    }        ::IceInternal::ProxyHandle<Hello> ice_connectionCached(bool __cached) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_connectionCached(__cached).get());    }        ::IceInternal::ProxyHandle<Hello> ice_endpointSelection(::Ice::EndpointSelectionType __est) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_endpointSelection(__est).get());    }        ::IceInternal::ProxyHandle<Hello> ice_secure(bool __secure) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_secure(__secure).get());    }        ::IceInternal::ProxyHandle<Hello> ice_preferSecure(bool __preferSecure) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_preferSecure(__preferSecure).get());    }        ::IceInternal::ProxyHandle<Hello> ice_router(const ::Ice::RouterPrx& __router) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_router(__router).get());    }        ::IceInternal::ProxyHandle<Hello> ice_locator(const ::Ice::LocatorPrx& __locator) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_locator(__locator).get());    }        ::IceInternal::ProxyHandle<Hello> ice_collocationOptimized(bool __co) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_collocationOptimized(__co).get());    }        ::IceInternal::ProxyHandle<Hello> ice_invocationTimeout(int __timeout) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_invocationTimeout(__timeout).get());    }        ::IceInternal::ProxyHandle<Hello> ice_twoway() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_twoway().get());    }        ::IceInternal::ProxyHandle<Hello> ice_oneway() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_oneway().get());    }        ::IceInternal::ProxyHandle<Hello> ice_batchOneway() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_batchOneway().get());    }        ::IceInternal::ProxyHandle<Hello> ice_datagram() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_datagram().get());    }        ::IceInternal::ProxyHandle<Hello> ice_batchDatagram() const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_batchDatagram().get());    }        ::IceInternal::ProxyHandle<Hello> ice_compress(bool __compress) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_compress(__compress).get());    }        ::IceInternal::ProxyHandle<Hello> ice_timeout(int __timeout) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_timeout(__timeout).get());    }        ::IceInternal::ProxyHandle<Hello> ice_connectionId(const ::std::string& __id) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_connectionId(__id).get());    }        ::IceInternal::ProxyHandle<Hello> ice_encodingVersion(const ::Ice::EncodingVersion& __v) const    {        return dynamic_cast<Hello*>(::IceProxy::Ice::Object::ice_encodingVersion(__v).get());    }        static const ::std::string& ice_staticId();private:     virtual ::IceProxy::Ice::Object* __newInstance() const;};}}namespace Demo{class Hello : virtual public ::Ice::Object{public:    typedef HelloPrx ProxyType;    typedef HelloPtr 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 sayHello(::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;    ::Ice::DispatchStatus ___sayHello(::IceInternal::Incoming&, const ::Ice::Current&);    virtual void shutdown(const ::Ice::Current& = ::Ice::Current()) = 0;    ::Ice::DispatchStatus ___shutdown(::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 Hello& l, const Hello& r){    return static_cast<const ::Ice::Object&>(l) == static_cast<const ::Ice::Object&>(r);}inline bool operator<(const Hello& l, const Hello& r){    return static_cast<const ::Ice::Object&>(l) < static_cast<const ::Ice::Object&>(r);}}namespace Demo{template<class T>class CallbackNC_Hello_sayHello : public Callback_Hello_sayHello_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_Hello_sayHello(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::OnewayCallbackNC<T>(obj, cb, excb, sentcb)    {    }};template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, 0, excb, sentcb);}template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_sayHello<T>(instance, 0, excb, sentcb);}template<class T, typename CT>class Callback_Hello_sayHello : public Callback_Hello_sayHello_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_Hello_sayHello(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::OnewayCallback<T, CT>(obj, cb, excb, sentcb)    {    }};template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(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_Hello_sayHello<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_sayHello<T, CT>(instance, 0, excb, sentcb);}template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(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_Hello_sayHello<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_sayHelloPtrnewCallback_Hello_sayHello(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_sayHello<T, CT>(instance, 0, excb, sentcb);}template<class T>class CallbackNC_Hello_shutdown : public Callback_Hello_shutdown_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_Hello_shutdown(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::OnewayCallbackNC<T>(obj, cb, excb, sentcb)    {    }};template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, 0, excb, sentcb);}template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*cb)(), void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, cb, excb, sentcb);}template<class T> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*excb)(const ::Ice::Exception&), void (T::*sentcb)(bool) = 0){    return new CallbackNC_Hello_shutdown<T>(instance, 0, excb, sentcb);}template<class T, typename CT>class Callback_Hello_shutdown : public Callback_Hello_shutdown_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_Hello_shutdown(const TPtr& obj, Response cb, Exception excb, Sent sentcb)        : ::IceInternal::OnewayCallback<T, CT>(obj, cb, excb, sentcb)    {    }};template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(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_Hello_shutdown<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(const IceUtil::Handle<T>& instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_shutdown<T, CT>(instance, 0, excb, sentcb);}template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(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_Hello_shutdown<T, CT>(instance, cb, excb, sentcb);}template<class T, typename CT> Callback_Hello_shutdownPtrnewCallback_Hello_shutdown(T* instance, void (T::*excb)(const ::Ice::Exception&, const CT&), void (T::*sentcb)(bool, const CT&) = 0){    return new Callback_Hello_shutdown<T, CT>(instance, 0, excb, sentcb);}}#include <IceUtil/PopDisableWarnings.h>#endif
Hello.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.2//// <auto-generated>//// Generated from file `Hello.ice'//// Warning: do not edit this file.//// </auto-generated>//#include <Hello.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 < 2#       error Ice patch level mismatch!#   endif#endifnamespace{const ::std::string __Demo__Hello__sayHello_name = "sayHello";const ::std::string __Demo__Hello__shutdown_name = "shutdown";}::IceProxy::Ice::Object* ::IceProxy::Demo::upCast(::IceProxy::Demo::Hello* p) { return p; }void::IceProxy::Demo::__read(::IceInternal::BasicStream* __is, ::IceInternal::ProxyHandle< ::IceProxy::Demo::Hello>& v){    ::Ice::ObjectPrx proxy;    __is->read(proxy);    if(!proxy)    {        v = 0;    }    else    {        v = new ::IceProxy::Demo::Hello;        v->__copyFrom(proxy);    }}voidIceProxy::Demo::Hello::sayHello(::Ice::Int __p_delay, const ::Ice::Context* __ctx){    ::IceInternal::Outgoing __og(this, __Demo__Hello__sayHello_name, ::Ice::Idempotent, __ctx);    try    {        ::IceInternal::BasicStream* __os = __og.startWriteParams(::Ice::DefaultFormat);        __os->write(__p_delay);        __og.endWriteParams();    }    catch(const ::Ice::LocalException& __ex)    {        __og.abort(__ex);    }    __invoke(__og);}::Ice::AsyncResultPtrIceProxy::Demo::Hello::begin_sayHello(::Ice::Int __p_delay, const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie){    ::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __Demo__Hello__sayHello_name, __del, __cookie);    try    {        __result->prepare(__Demo__Hello__sayHello_name, ::Ice::Idempotent, __ctx);        ::IceInternal::BasicStream* __os = __result->startWriteParams(::Ice::DefaultFormat);        __os->write(__p_delay);        __result->endWriteParams();        __result->invoke();    }    catch(const ::Ice::Exception& __ex)    {        __result->abort(__ex);    }    return __result;}voidIceProxy::Demo::Hello::end_sayHello(const ::Ice::AsyncResultPtr& __result){    __end(__result, __Demo__Hello__sayHello_name);}voidIceProxy::Demo::Hello::shutdown(const ::Ice::Context* __ctx){    ::IceInternal::Outgoing __og(this, __Demo__Hello__shutdown_name, ::Ice::Normal, __ctx);    __og.writeEmptyParams();    __invoke(__og);}::Ice::AsyncResultPtrIceProxy::Demo::Hello::begin_shutdown(const ::Ice::Context* __ctx, const ::IceInternal::CallbackBasePtr& __del, const ::Ice::LocalObjectPtr& __cookie){    ::IceInternal::OutgoingAsyncPtr __result = new ::IceInternal::OutgoingAsync(this, __Demo__Hello__shutdown_name, __del, __cookie);    try    {        __result->prepare(__Demo__Hello__shutdown_name, ::Ice::Normal, __ctx);        __result->writeEmptyParams();        __result->invoke();    }    catch(const ::Ice::Exception& __ex)    {        __result->abort(__ex);    }    return __result;}voidIceProxy::Demo::Hello::end_shutdown(const ::Ice::AsyncResultPtr& __result){    __end(__result, __Demo__Hello__shutdown_name);}const ::std::string&IceProxy::Demo::Hello::ice_staticId(){    return ::Demo::Hello::ice_staticId();}::IceProxy::Ice::Object*IceProxy::Demo::Hello::__newInstance() const{    return new Hello;}::Ice::Object* Demo::upCast(::Demo::Hello* p) { return p; }namespace{const ::std::string __Demo__Hello_ids[2] ={    "::Demo::Hello",    "::Ice::Object"};}boolDemo::Hello::ice_isA(const ::std::string& _s, const ::Ice::Current&) const{    return ::std::binary_search(__Demo__Hello_ids, __Demo__Hello_ids + 2, _s);}::std::vector< ::std::string>Demo::Hello::ice_ids(const ::Ice::Current&) const{    return ::std::vector< ::std::string>(&__Demo__Hello_ids[0], &__Demo__Hello_ids[2]);}const ::std::string&Demo::Hello::ice_id(const ::Ice::Current&) const{    return __Demo__Hello_ids[0];}const ::std::string&Demo::Hello::ice_staticId(){#ifdef ICE_HAS_THREAD_SAFE_LOCAL_STATIC    static const ::std::string typeId = "::Demo::Hello";    return typeId;#else    return __Demo__Hello_ids[0];#endif}::Ice::DispatchStatusDemo::Hello::___sayHello(::IceInternal::Incoming& __inS, const ::Ice::Current& __current){    __checkMode(::Ice::Idempotent, __current.mode);    ::IceInternal::BasicStream* __is = __inS.startReadParams();    ::Ice::Int __p_delay;    __is->read(__p_delay);    __inS.endReadParams();    sayHello(__p_delay, __current);    __inS.__writeEmptyParams();    return ::Ice::DispatchOK;}::Ice::DispatchStatusDemo::Hello::___shutdown(::IceInternal::Incoming& __inS, const ::Ice::Current& __current){    __checkMode(::Ice::Normal, __current.mode);    __inS.readEmptyParams();    shutdown(__current);    __inS.__writeEmptyParams();    return ::Ice::DispatchOK;}namespace{const ::std::string __Demo__Hello_all[] ={    "ice_id",    "ice_ids",    "ice_isA",    "ice_ping",    "sayHello",    "shutdown"};}::Ice::DispatchStatusDemo::Hello::__dispatch(::IceInternal::Incoming& in, const ::Ice::Current& current){    ::std::pair< const ::std::string*, const ::std::string*> r = ::std::equal_range(__Demo__Hello_all, __Demo__Hello_all + 6, current.operation);    if(r.first == r.second)    {        throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);    }    switch(r.first - __Demo__Hello_all)    {        case 0:        {            return ___ice_id(in, current);        }        case 1:        {            return ___ice_ids(in, current);        }        case 2:        {            return ___ice_isA(in, current);        }        case 3:        {            return ___ice_ping(in, current);        }        case 4:        {            return ___sayHello(in, current);        }        case 5:        {            return ___shutdown(in, current);        }    }    assert(false);    throw ::Ice::OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);}voidDemo::Hello::__writeImpl(::IceInternal::BasicStream* __os) const{    __os->startWriteSlice(ice_staticId(), -1, true);    __os->endWriteSlice();}voidDemo::Hello::__readImpl(::IceInternal::BasicStream* __is){    __is->startReadSlice();    __is->endReadSlice();}void Demo::__patch(HelloPtr& handle, const ::Ice::ObjectPtr& v){    handle = ::Demo::HelloPtr::dynamicCast(v);    if(v && !handle)    {        IceInternal::Ex::throwUOE(::Demo::Hello::ice_staticId(), v);    }}



第三步:根据业务实现定义的接口 HelloI.h HelloI.cpp

HelloI.h

// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#ifndef HELLO_I_H#define HELLO_I_H#include <Hello.h>class HelloI : public Demo::Hello{public:    virtual void sayHello(int delay, const Ice::Current&);    virtual void shutdown(const Ice::Current&);};#endif

HelloI.cpp

// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#include <IceUtil/IceUtil.h>#include <Ice/Ice.h>#include <HelloI.h>using namespace std;voidHelloI::sayHello(int delay, const Ice::Current&){    if(delay != 0)    {        IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(delay));    }    cout << "Hello World!" << endl;}voidHelloI::shutdown(const Ice::Current& c){    cout << "Shutting down..." << endl;    c.adapter->getCommunicator()->shutdown();}

第四步:实现客户端与服务器

client.cpp
说明:
1) communicator() 得到 通信器(Ice::Communicator),这个例子中没有说明怎样自己构造一个通信器,后面学习时再看
2) 由通信器去得到Proxy对象,现在不清楚有多少种方法可以得到代理对象,例子是通过字符串得到的,"Hello.proxy"对应

   config.client 中的 Hello.Proxy=hello:tcp -p 10000:udp -p 10000:ssl -p 10001
3)通信方式由Proxy对象控制,例子中是通过下面方法调用
  双向:       HelloPrx twoway = propertyToProxy("Hello.Proxy")->ice_twoway()
  单向:       HelloPrx oneway = twoway->ice_oneway();
  批量单向:   HelloPrx batchOneway = twoway->ice_batchOneway(); 
  数据报:     HelloPrx datagram = twoway->ice_datagram();
  批量数据报: HelloPrx batchDatagram = twoway->ice_batchDatagram();
4)对量通信发送数据后,通过调用下面的方法刷新(真正将数据发送出去)
  batchOneway->ice_flushBatchRequests();
 batchDatagram->ice_flushBatchRequests();
5)设置发送超时时间间隔,单位ms
 twoway = twoway->ice_invocationTimeout(timeout);
 oneway = oneway->ice_invocationTimeout(timeout);
 batchOneway = batchOneway->ice_invocationTimeout(timeout);
6)设置是否走安全通道 是(true) 否(false),安全通道时,证书路径在配置文件中
  twoway = twoway->ice_secure(secure);
  oneway = oneway->ice_secure(secure);
  batchOneway = batchOneway->ice_secure(secure);
  datagram = datagram->ice_secure(secure);
  batchDatagram = batchDatagram->ice_secure(secure);

7) 调用是客户端传递参数,服务端执行后返回结果(有结果返回时,应该是twoway)

// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#include <Ice/Ice.h>#include <Hello.h>using namespace std;using namespace Demo;class HelloClient : public Ice::Application{public:    HelloClient();    virtual int run(int, char*[]);private:    void menu();};intmain(int argc, char* argv[]){#ifdef ICE_STATIC_LIBS    Ice::registerIceSSL();#endif    HelloClient app;    return app.main(argc, argv, "config.client");}HelloClient::HelloClient() :    //    // Since this is an interactive demo we don't want any signal    // handling.    //    Ice::Application(Ice::NoSignalHandling){}intHelloClient::run(int argc, char* argv[]){    if(argc > 1)    {        cerr << appName() << ": too many arguments" << endl;        return EXIT_FAILURE;    }    HelloPrx twoway = HelloPrx::checkedCast(        communicator()->propertyToProxy("Hello.Proxy")->ice_twoway()->ice_secure(false));    if(!twoway)    {        cerr << argv[0] << ": invalid proxy" << endl;        return EXIT_FAILURE;    }    HelloPrx oneway = twoway->ice_oneway();    HelloPrx batchOneway = twoway->ice_batchOneway();    HelloPrx datagram = twoway->ice_datagram();    HelloPrx batchDatagram = twoway->ice_batchDatagram();    bool secure = false;    int timeout = -1;    int delay = 0;    menu();    char c = 'x';    do    {        try        {            cout << "==> ";            cin >> c;            if(c == 't')            {                twoway->sayHello(delay);            }            else if(c == 'o')            {                oneway->sayHello(delay);            }            else if(c == 'O')            {                batchOneway->sayHello(delay);            }            else if(c == 'd')            {                if(secure)                {                    cout << "secure datagrams are not supported" << endl;                }                else                {                    datagram->sayHello(delay);                }            }            else if(c == 'D')            {                if(secure)                {                    cout << "secure datagrams are not supported" << endl;                }                else                {                    batchDatagram->sayHello(delay);                }            }            else if(c == 'f')            {                batchOneway->ice_flushBatchRequests();                batchDatagram->ice_flushBatchRequests();            }            else if(c == 'T')            {                if(timeout == -1)                {                    timeout = 2000;                }                else                {                    timeout = -1;                }                twoway = twoway->ice_invocationTimeout(timeout);                oneway = oneway->ice_invocationTimeout(timeout);                batchOneway = batchOneway->ice_invocationTimeout(timeout);                if(timeout == -1)                {                    cout << "timeout is now switched off" << endl;                }                else                {                    cout << "timeout is now set to 2000ms" << endl;                }            }            else if(c == 'P')            {                if(delay == 0)                {                    delay = 2500;                }                else                {                    delay = 0;                }                if(delay == 0)                {                    cout << "server delay is now deactivated" << endl;                }                else                {                    cout << "server delay is now set to 2500ms" << endl;                }            }            else if(c == 'S')            {                secure = !secure;                twoway = twoway->ice_secure(secure);                oneway = oneway->ice_secure(secure);                batchOneway = batchOneway->ice_secure(secure);                datagram = datagram->ice_secure(secure);                batchDatagram = batchDatagram->ice_secure(secure);                if(secure)                {                    cout << "secure mode is now on" << endl;                }                else                {                    cout << "secure mode is now off" << endl;                }            }            else if(c == 's')            {                twoway->shutdown();            }            else if(c == 'x')            {                // Nothing to do            }            else if(c == '?')            {                menu();            }            else            {                cout << "unknown command `" << c << "'" << endl;                menu();            }        }        catch(const Ice::Exception& ex)        {            cerr << ex << endl;        }    }    while(cin.good() && c != 'x');    return EXIT_SUCCESS;}voidHelloClient::menu(){    cout <<        "usage:\n"        "t: send greeting as twoway\n"        "o: send greeting as oneway\n"        "O: send greeting as batch oneway\n"        "d: send greeting as datagram\n"        "D: send greeting as batch datagram\n"        "f: flush all batch requests\n"        "T: set a timeout\n"        "P: set server delay\n"        "S: switch secure mode on/off\n"        "s: shutdown server\n"        "x: exit\n"        "?: help\n";}


server.cpp
说明:
1)例子通过communitor()得到通信器
2)通过通信器得到对象适配器,例子是适配Hello接口,实际可能会有多个接口 Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Hello");

   “Hello" 对应config.server文件中的 Hello.Endpoints=tcp -p 10000:udp -p 10000:ssl -p 10001:ws -p 10002:wss -p 10003
3) 创建servant ,并将其加入适配器
   Demo::HelloPtr hello = new HelloI;
  adapter->add(hello, communicator()->stringToIdentity("hello"));
4)激活适配器
   adapter->activate();
5)通信器 wait for exit
  communicator()->waitForShutdown();
问题:
1) HelloServer从App继承,线程方法是通过communicator()等待结束事件的,如果多线程时应该怎样处理比较好呢?

// **********************************************************************//// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.//// **********************************************************************#include <Ice/Ice.h>#include <HelloI.h>using namespace std;class HelloServer : public Ice::Application{public:    virtual int run(int, char*[]);};intmain(int argc, char* argv[]){#ifdef ICE_STATIC_LIBS    Ice::registerIceSSL();#endif    HelloServer app;    return app.main(argc, argv, "config.server");}intHelloServer::run(int argc, char*[]){    if(argc > 1)    {        cerr << appName() << ": too many arguments" << endl;        return EXIT_FAILURE;    }    Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Hello");    Demo::HelloPtr hello = new HelloI;    adapter->add(hello, communicator()->stringToIdentity("hello"));    adapter->activate();    communicator()->waitForShutdown();    return EXIT_SUCCESS;}


五 配置文件 

config.client

## The client reads this property to create the reference to the# "hello" object in the server.#Hello.Proxy=hello:tcp -p 10000:udp -p 10000:ssl -p 10001## Uncomment to use the WebSocket transports instead.##Hello.Proxy=hello:ws -p 10002:udp -p 10000:wss -p 10003## 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## Security Tracing## 0 = no security tracing# 1 = trace messages##IceSSL.Trace.Security=1## SSL Configuration#Ice.Plugin.IceSSL=IceSSL:createIceSSLIceSSL.DefaultDir=../../../certsIceSSL.CAs=cacert.pemIceSSL.CertFile=client.p12IceSSL.Password=passwordIceSSL.Keychain=client.keychainIceSSL.KeychainPassword=password## IceMX configuration.##Ice.Admin.Endpoints=tcp -p 10004Ice.Admin.InstanceName=clientIceMX.Metrics.Debug.GroupBy=idIceMX.Metrics.ByParent.GroupBy=parent

config.server

## The server creates one single object adapter with the name# "Hello". The following line sets the endpoints for this# adapter.## When no -h <host> option is specified in the endpoints, the default# value from the Ice.Default.Host property is used. If this property# isn't set, the endpoints will listen on all available network# interfaces.#Hello.Endpoints=tcp -p 10000:udp -p 10000:ssl -p 10001:ws -p 10002:wss -p 10003## Only listen on the localhost interface by default. You can comment# out this property to allow listening on all available interfaces.#Ice.Default.Host=localhost## For secure WebSocket (WSS) clients and Windows Store App clients,# you should disable this property. JavaScript browser clients and# Windows Store App clients don't use client-side authentication.##IceSSL.VerifyPeer=0## 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## Security Tracing## 0 = no security tracing# 1 = trace messages##IceSSL.Trace.Security=1## SSL Configuration#Ice.Plugin.IceSSL=IceSSL:createIceSSLIceSSL.DefaultDir=../../../certsIceSSL.CAs=cacert.pemIceSSL.CertFile=server.p12IceSSL.Password=passwordIceSSL.Keychain=server.keychainIceSSL.KeychainPassword=password## IceMX configuration##Ice.Admin.Endpoints=tcp -p 10004Ice.Admin.InstanceName=serverIceMX.Metrics.Debug.GroupBy=idIceMX.Metrics.ByParent.GroupBy=parent

六 服务器开的端口

[root@cnlink hello]# netstat -anp|grep serverActive Internet connections (servers and established)tcp        0      0 127.0.0.1:10000         0.0.0.0:*               LISTEN      30362/./server      tcp        0      0 127.0.0.1:10001         0.0.0.0:*               LISTEN      30362/./server      tcp        0      0 127.0.0.1:10002         0.0.0.0:*               LISTEN      30362/./server      tcp        0      0 127.0.0.1:10003         0.0.0.0:*               LISTEN      30362/./server      udp        0      0 127.0.0.1:10000         0.0.0.0:*                           30362/./server    


0 0
原创粉丝点击