因为公司需要让我了解一下ICE,所以写下来我的一些了解情况供分享。因为接触不久所以写的比较浅显,请多包涵。
ICE是什么
ICE is a modern object-oriented toolkit that enables you to build distributed applications with minimal effort:
配置VS2013开发环境
HelloWorld
module Demo {
interface Printer {
void printString(string s);
};
};
2. VS2013 ICE add-in 安装后,可在VS TOOLS -> Ice Configuration 中勾选Enable Ice Builder。#include <iostream>
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
class PrinterI : public Printer // the servant
{
public:
virtual void printString(const string &s, const Ice::Current ¤t);
};
void PrinterI::printString(const string &s, const Ice::Current ¤t)
{
cout << "In function " << __FUNCTION__ << " with string: " << s << endl;
}
int main(int arc, char **argv)
{
int status = 0;
Ice::CommunicatorPtr ic;
try {
ic = Ice::initialize(arc, argv);
Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter",
"default -p 10000:default -p 10001");
Ice::ObjectPtr object = new PrinterI();
adapter->add(object, ic->stringToIdentity("SimplePrinter"));
adapter->activate();
ic->waitForShutdown();
} catch (const Ice::Exception &e) {
cerr << e << endl;
status = 1;
} catch (const char *msg) {
cerr << msg << endl;
status = 1;
}
if (NULL != ic) {
try {
ic->destroy();
} catch (const Ice::Exception &e) {
cerr << e << endl;
status = 1;
}
}
return status;
}
4. 添加Client 端调用 printString()的代码:#include <iostream>
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
int main(int argc, char **argv)
{
int status = 0;
Ice::CommunicatorPtr ic;
try {
ic = Ice::initialize(argc, argv);
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000:default -p 10001");
PrinterPrx printer = PrinterPrx::checkedCast(base);
// PrinterPrx printer = PrinterPrx::uncheckedCast(base->ice_connectionCached(false));
if (!printer)
throw "Invalid proxy";
printer->printString("Hello, World!");
} catch (const Ice::Exception &e) {
cerr << e << endl;
status = 1;
} catch (const char *msg) {
cerr << msg << endl;
status = 1;
}
if (NULL != ic)
ic->destroy();
return status;
}
5. 编译运行,运气好的话调用客户端的代码后在服务器端就可以看到 Hello, World了。