Home Help Center

I create two CommunicatorPtr,is your right?

Hi!
In my program,i create two CommunicatorPtr.Becuse i want to response some clients use CommunicatorPtr1,and tranfer files use CommunicatorPtr2.I don't konw if is your right.:(

my code:

//////////////////////////////////////////////////////////////////////////
//功能:初始化通信器
//参数:无
//返回值:true:成功,false:失败
//////////////////////////////////////////////////////////////////////////

bool IceServer::Inital()
{
bool status;
char CrtPort[10],DataPort[10],strPort[50];
ConfigMap MyConfig( "CFSSConfig.ini" , "=" );//读配置文件
int dummy = 0;
try
{
//////////////////////////////////////////////////////////////////////////
//创建两个ICE通信器
props1 = Ice::createProperties();
props1->setProperty("Ice.Trace.Network", "1");//跟踪消息
ic1 = Ice::initializeWithProperties(dummy, 0 , props1);

props2 = Ice::createProperties();
props2->setProperty("Ice.Trace.Network", "1");//跟踪消息
ic2 = Ice::initializeWithProperties( dummy , 0, props2);

strcpy( CrtPort , strdup( MyConfig["CtrCommandPort"].c_str() ) );//通信端口
strcpy( DataPort , strdup( MyConfig["DataTransferPort"].c_str() ) );//数据传输端口
strcpy( strPort , "default -p " );

strcat( strPort , CrtPort );//监听安管服务器端口
adapter= ic1->createObjectAdapterWithEndpoints("ServerForExe", strPort );//用于接收命令

strcpy( strPort , "");//清空strport
strcpy( strPort , "default -p " );
strcat( strPort , DataPort );

transfile_adapter= ic2->createObjectAdapterWithEndpoints("TransFile", strPort );//用于传送文件

}
catch(const Ice::Exception & ex)
{
cerr << ex << endl;
status = false;
}
catch (const char * msg)
{
cerr << msg << endl;
status = false;
}
return true;
}



thank for your view:)

Comments

  • matthewmatthew NL, CanadaMember Matthew NewhookOrganization: ZeroC, Inc.Project: Internet Communications Engine ✭✭✭
    Why do you want to create two communicators? You almost certainly do not need to do this. Your code also has numerous other problems. For example:

    char CrtPort[10],DataPort[10],strPort[50];
    strcpy( CrtPort , strdup( MyConfig["CtrCommandPort"].c_str() ) );//通信端口

    This is wrong. Firstly, you duplicate a string and then don't free (strdup). Next, you copy this string into a string that probably doesn't have enough space (9 characters + null terminator). You should use std::string, not incorrectly attempt to manage the memory like this.
  • klsmlzmklsmlzm Member
    Thanks for your suggestion!
    I create a communicator to accept client's messages(endpoint at 30005),and create another communicators to transfer files between two programs of server(endpoint at 30006).
    Begin,i tried to create two Adapters and a communicator for my application,but i failed.This program can not transfer files,if i use two communicators,this program can wrok normal.
  • benoitbenoit Rennes, FranceAdministrators, ZeroC Staff Benoit FoucherOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    You shouldn't need to create 2 communicators. You should be able to just create two object adapters. What was the problem when you use 2 object adapters?

    Note that by default, object adapters share the same server thread pool and this thread pool has only one thread. You can bump the number of threads in this thread pool with the Ice.ThreadPool.Server.Size property. This will allow multiple requests to be dispatched concurrently. You can also configure your object adapters to use their own thread pool. For that, you just need to set the <adapter name>.ThreadPool.Size property to an integer value superior to 0. See the Ice manual for more information on these properties.

    Hope this helps.

    Cheers,
    Benoit.
  • klsmlzmklsmlzm Member
    Hi!
    now,my code is:
    class DrcIceServer : virtual public Ice::Application {
    public:
    virtual int run(int, char * []) {
    int status,dummy;
    char CrtPort[10],DataPort[10],strPort[50];
    ConfigMap MyConfig( "CFSSConfig.ini" , "=" );//读配置文件
    status = dummy = 0;

    Ice::PropertiesPtr props = Ice::createProperties();
    props->setProperty("Ice.Trace.Network", "1");
    Ice::CommunicatorPtr ic = communicator();

    try
    {
    ic = Ice::initializeWithProperties( dummy , 0 , props);

    strcpy( CrtPort , MyConfig["CtrCommandPort"].c_str() );//通信端口
    strcpy( DataPort , MyConfig["DataTransferPort"].c_str() );//数据传输端口
    strcpy( strPort , "default -p " );

    strcat( strPort , CrtPort );//监听安管服务器端口
    Ice::ObjectAdapterPtr adapter= ic->createObjectAdapterWithEndpoints(
    "ServerForExe", strPort );//用于接收命令

    strcpy( strPort , "");//清空strport
    strcpy( strPort , "default -p " );
    strcat( strPort , DataPort );

    Ice::ObjectAdapterPtr transfile_adapter= ic->createObjectAdapterWithEndpoints(
    "TransFile", strPort );//用于传送文件

    Ice::ObjectPtr object = new CenterToClientI;//管理主机至安管中心服务接口
    Ice::ObjectPtr objectTransfer = new FileTransferI;//文件传输服务接口

    adapter->add(object,Ice::stringToIdentity("ExeTask"));
    adapter->activate();

    transfile_adapter->add(objectTransfer,Ice::stringToIdentity("FileTransfer"));
    transfile_adapter->activate();

    ic->waitForShutdown();
    }
    catch (const Ice::Exception & e)
    {
    cerr << e << endl;
    status = 1;
    }
    catch (const char * msg)
    {
    cerr << msg << endl;
    status = 1;
    }
    if (ic)
    {
    try
    {
    ic->destroy();
    }
    catch (const Ice::Exception & e)
    {
    cerr << e << endl;
    status = 1;
    }
    }
    return status;
    }
    };



    According to your suggestion,i create a communicator and two ObjectAdapter.A ObjectAdapter was used to accepte commands from clients at endpoint "CrtPort",and another ObjectAdapter was used to transfer files at endpoint "DataPort".When i send a command which hope transfer file "xxx" from "A" to "B",but "A" and client which sernd command occur "Dead Lock".I don't know why like this.

    I have attached my image of system' structure.
  • matthewmatthew NL, CanadaMember Matthew NewhookOrganization: ZeroC, Inc.Project: Internet Communications Engine ✭✭✭
    Do you have nested callbacks? So A calls B which then calls back on A which calls on B and so on (see the demo demo/Ice/nested for an example of such an application).

    A -> B -> A -> B -> A?

    If you are doing then then you must ensure that A & B have sufficient threads in the server thread pool. Try:

    Ice.ThreadPool.Server.Size=5

    in the configuration for server A & B.

    If this isn't the problem, then we need more information? Where is the deadlock occurring? If you run the app under a debugger a stack trace of the various participants is helpful.
  • klsmlzmklsmlzm Member
    Thank you very much for matthew,benoit and zeroc!Your are very excellence team.
    I set Ice.ThreadPool.Server.Size=5,my program run is right!
    Cheers,
    klsmlzm.:)
  • benoitbenoit Rennes, FranceAdministrators, ZeroC Staff Benoit FoucherOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Hi,

    I would also recommend you to take a look at the articles "Avoiding deadlocks" in the issue 4 and 5 of the newsletter available here. These two issues discuss how deadlocks occur and the best ways to solve them!

    Cheers,
    Benoit.
Sign In or Register to comment.