Listing 2: Code for a simple YAMI server.
// a simple YAMI server
#include "yami++.h"
using namespace YAMI;
#include <iostream>
#include <string>
using namespace std;
// the port of the server's agent
const int serverport = 12340;
// the name of the servant, as seen by clients
const string objectname = "calculator";
// a class implementing the servant
class Calculator : public PassiveObject
{
public:
void call(IncomingMsg &incoming)
{
// get the parameters
auto_ptr<ParamSet> incparamset(
incoming.getParameters());
int a = incparamset->getInt(0);
int b = incparamset->getInt(1);
int result = 0;
// get the message's name
string messagename(incoming.getMsgName());
if (messagename == "add")
result = a + b;
else if (messagename == "subtract")
result = a - b;
else if (messagename == "multiply")
result = a * b;
else if (messagename == "divide" && b != 0)
result = a / b;
else
{
// "throw an exception"
incoming.reject();
return;
}
ParamSet retparamset(1);
retparamset.setInt(0, result);
// send new set of parameters as a reply
incoming.reply(retparamset);
}
};
int main()
{
cout << "starting the server" << endl;
try
{
netInitialize();
{
Agent agent(serverport);
Calculator calc_server;
// register the servant so that the agent accepts and stores messages
// addressed to this object
agent.objectRegister(objectname,
Agent::ePassiveMultiThreaded, &calc_server);
cout << "waiting for invocations..."
<< endl;
sleep(0);
}
netCleanup();
}
catch (const exception &e)
{
cout << e.what() << endl;
}
}