⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sobczak.txt

📁 用C++编的小程序
💻 TXT
字号:
YAMI: A Simple ORB Library       
by Maciej Sobczak     


// the passive servant interface
namespace YAMI
{
    class PassiveObject
    {
    public:
        virtual void call(IncomingMsg &msg) = 0;
    };
    // ...
}


Listing 2:

// 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;
    }
}


Listing 3:

// a simple YAMI client
#include "yami++.h"
using namespace YAMI;
#include <iostream>
#include <string>

using namespace std;
const string serverhost = "127.0.0.1";
const int    serverport = 12340;
const int    clientport = 12341;
// arbitrary name, used locally to distinguish between different domains
const string domainname  = "someDomain";
// name of the logical remote object
const string objectname  = "calculator";
int main()
{
    try
    {
        netInitialize();
        {
        Agent agent(clientport);
        // register the remote domain
        agent.domainRegister(domainname, serverhost, serverport, 2);
        string messagename = "multiply";
        ParamSet paramset(2);
        paramset.setInt(0, 5);
        paramset.setInt(1, 6);
        // send the message
        auto_ptr<Message> message(
            agent.send(domainname, objectname, messagename, paramset));
        message->wait();
        Message::eStatus status = message->getStatus();
        if (status == Message::eReplied)
        {
            // read the response
            auto_ptr<ParamSet> response(
                message->getResponse());
            int result = response->getInt(0);
            cout << "the result is: " << result << endl;
        }
        else
        {
            cout << "the message was not replied correctly"
                << endl;
        }
        }
        netCleanup();
    }
    catch (const exception &e)
    {
        cout << e.what() << endl;
    }
}

Listing 4:

# a simple YAMI client in Python. this code can be used also in the
# interactive Python console

from YAMI import *

a = Agent(12341)
a.domainRegister('someDomain', '127.0.0.1', 12340, 2)

msg = a.send('someDomain', 'calculator', 'multiply', [5, 6])
msg.wait()

if msg.getStatus() == eReplied:
        results = msg.getResponse()
        print 'the result is:', results[0]
else:
        print 'the message was not replied correctly'


Listing 5:

# a simple YAMI client in Tcl. this code can be used also in the
# interactive Tcl console

package require YAMI
set agent [YAMI::Agent 12341]
$agent domainRegister someDomain 127.0.0.1 12340 2
set message [$agent send someDomain calculator \
    multiply {{5 i} {6 i}}]
$message wait
if {[$message getStatus] == "eReplied"} {
    set results [$message getResponse]
    puts "the result is: [lindex $results 0]"
} else {
    puts "the message was not replied correctly"
}
$message -delete
$agent -delete


Listing 6:

calculator
{
    add < (int a, int b) > (int c).
    subtract < (int a, int b) > (int c).
    multiply < (int a, int b) > (int c).
    divide < (int a, int b) > (int c).
}.


Listing 7:
// 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;
    }
}


Listing 8:
// a YAMI client built with YDL
#include "yami++.h"
#include "calc_client.h"

using namespace YAMI;
#include <iostream>
#include <string>
using namespace std;
const string serverhost = "127.0.0.1";
const int    serverport = 12340;
const int    clientport = 12341;
const string domainname  = "someDomain";
const string objectname  = "calculator";
int main()
{
    try
    {
        netInitialize();
        {
        Agent agent(clientport);
        agent.domainRegister(domainname,
            serverhost, serverport, 2);
        // create a local representative of the server        
        calculator calc(agent, domainname, objectname);
        int result;
        // send the message
        calc.multiply(5, 6, result);
        cout << "the result is: " << result << endl;
        }
        netCleanup();
    }
    catch (const exception &e)
    {
        cout << e.what() << endl;
    }
}






5


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -