Listing 7: A YAMI server in YDL.
// a YAMI server built with YDL
#include "yami++.h"
#include "calc_server.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";
class Calculator : public calculator_Skel
{
private:
void add(int a, int b, int &c) { c = a + b; }
void subtract(int a, int b, int &c) { c = a - b; }
void multiply(int a, int b, int &c) { c = a * b; }
void divide(int a, int b, int &c)
{
if (b == 0)
{
// dividing by 0 not allowed
throw 0;
}
c = a / b;
}
};
int main()
{
cout << "starting the server" << endl;
try
{
netInitialize();
{
Agent agent(serverport);
Calculator calc_server;
agent.objectRegister(objectname,
Agent::ePassiveMultiThreaded, &calc_server);
cout << "waiting for invocations..."
<< endl;
sleep(0);
}
netCleanup();
}
catch (const exception &e)
{
cout << e.what() << endl;
}
}