Home Help Center

Connecting on two proxies

EastonEaston Member Gerhard EnglerOrganization: StudentProject: Two Server
Hello @all,

I am trying to get involved with ICE and I copied the HelloWorld example for C++. I would like to make this example reliable for connection losses, so I copied the server, changed the portnumber and added the entries for the connection on the second server. When the client starts, there appear on both servers "Hello World" :)
My goal is to implement a distributed system, when server_1 is down (or shutdown while the client is running) the client should be able to stay connected on server_2 an the other way.
In the HelloWorld example the client connects directly to the server, but I am needing a routine like a request:
DO

IF server_1 is up AND server_2 is down
THEN connect to server_1
ELSE IF server_1 is down AND server_2 is up
THEN connect to server_2
ELSE IF server_1 is up AND server_2 is up
THEN connect to both

WHILE server_1 is up OR server_2 is up


My first try is like the code below, but everytime I shut down a server, I get an exception "Network.cpp:2365: Ice::ConnectionRefusedException:"
   int status = 0;
    Ice::CommunicatorPtr ic;
    try
    {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectPrx base1 = ic->stringToProxy("SimplePrinter1:default -p 10010");                
        Ice::ObjectPrx base2 = ic->stringToProxy("SimplePrinter2:default -p 10011");
        Ice::ConnectionPtr con1 = base1->ice_getConnection();
        Ice::ConnectionPtr con2 = base2->ice_getConnection();
        PrtHelloPrx printer1;
        PrtHelloPrx printer2;
        
        do
        {
            if(con1 && !con2)
            {
                //base1 = ic->stringToProxy("SimplePrinter1:default -p 10010");
                printer1 = PrtHelloPrx::checkedCast(base1);
                if(!printer1)
                  throw "Invalid proxy";
            
                printer1->printString("###Server1 ###");
                printer1->printString(base1->ice_toString());
                 
            }else if(con2 && !con1)
            {
                //base2 = ic->stringToProxy("SimplePrinter2:default -p 10011");
                printer2 = PrtHelloPrx::checkedCast(base1);
                if(!printer2)
                  throw "Invalid proxy";
            
                printer2->printString(">>>  Server2 <<<");
                printer2->printString(base2->ice_toString());    
                 
            }else
            {
                //base1 = ic->stringToProxy("SimplePrinter1:default -p 10010");
                //base2 = ic->stringToProxy("SimplePrinter2:default -p 10011");
                printer1 = PrtHelloPrx::checkedCast(base1);
                printer2 = PrtHelloPrx::checkedCast(base2);
                if(!printer1 || !printer2)
                  throw "Invalid proxy";
            
                printer1->printString("### Server1 ###");
                printer1->printString(base1->ice_toString());        
                printer2->printString(">>> Server2 <<<");
                printer2->printString(base2->ice_toString());
            }
        }while(con1 || con2);
        
         }

Is anybody here, who can help me with that issue?

Thank you very much in advance

Comments

  • benoitbenoit Rennes, FranceAdministrators, ZeroC Staff Benoit FoucherOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Hi,

    What about always try to invoke on both servers as show below?
    int status = 0;
    Ice::CommunicatorPtr ic = Ice::initialize(argc, argv);
    PrtHelloPrx printer1 = PrtHelloPrx::uncheckedCast(ic->stringToProxy("SimplePrinter1:default -p 10010"));
    PrtHelloPrx printer2 = PrtHelloPrx::uncheckedCast(ic->stringToProxy("SimplePrinter1:default -p 10011"));
    bool isUp1;
    bool isUp2;
    do
    {
        try
        {
            printer1->printString(">>>  Server1 <<<");
            isUp1 = true;
        }
        catch(const Ice::LocalException& ex)
        {
             isUp1 = false; // server is down!
        }
    
        try
        {
            printer2->printString(">>>  Server2 <<<");
            isUp2 = true;
        }
        catch(const Ice::LocalException& ex)
        {        
             isUp2 = false; // server is down!
        }
    }
    while(isUp1 || isUp2);
    

    Cheers,
    Benoit.
  • EastonEaston Member Gerhard EnglerOrganization: StudentProject: Two Server
    Hi benoit,

    as I expected, it was something easy and my thinking was to complicated. Your Version is working fine and at least on server is receiving messages from the client.

    Thank you very much :)
Sign In or Register to comment.