From 00e97f3decde46e85fba871a20aa79e27cb679e3 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 8 Oct 2009 13:07:04 +0000 Subject: correction des bug connexion concurrente correction sur le ready message attente des deux endRegexp --- Ivy/IvyApplication.cxx | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'Ivy/IvyApplication.cxx') diff --git a/Ivy/IvyApplication.cxx b/Ivy/IvyApplication.cxx index a262fa2..4b41bbe 100644 --- a/Ivy/IvyApplication.cxx +++ b/Ivy/IvyApplication.cxx @@ -12,6 +12,9 @@ #define ARG_START 2 #define ARG_END 3 +#define MAXPORT(a,b) ((a>b) ? a : b) + + static char * firstArg( char *s, const char separator ) { char *ptr = s; @@ -46,6 +49,8 @@ IvyApplication::IvyApplication(Ivy * bus) remoteService = 0; /* unknown or unconnected application */ appname = "Unknown"; AppConnectedCallbackCalled = false; + readyToSend = 0; + InitializeCriticalSection(&m_SendReadySection ); } IvyApplication::~IvyApplication() @@ -56,6 +61,9 @@ IvyApplication::~IvyApplication() regexp_in.clear(); if ( m_hSocket != INVALID_SOCKET ) Close(); + // Release resources used by the critical section object. + DeleteCriticalSection(&m_SendReadySection); + } @@ -87,7 +95,14 @@ UINT IvyApplication::Create() #endif return port; } - +void IvyApplication::SendReadyToPeer() +{ + EnterCriticalSection (&m_SendReadySection); + readyToSend++; + if ( !bus->ready_message.empty() && readyToSend == 2 ) + SendMsg( bus->ready_message.c_str() ); + LeaveCriticalSection (&m_SendReadySection); +} void IvyApplication::OnReceive(char * line) { int err; @@ -191,7 +206,7 @@ void IvyApplication::OnReceive(char * line) } break; case StartRegexp: - + { #ifdef IVY_DEBUG TRACE("Regexp Start id=%d\n", id); #endif //IVY_DEBUG @@ -199,12 +214,29 @@ void IvyApplication::OnReceive(char * line) appname = arg; /* remote socket port */ remoteService = id; - if ( bus->CheckConnected( this ) ) + IvyApplication *other = bus->CheckConnected( this ); + if ( other ) { - TRACE("Quitting already connected %s\n", appname.c_str()); - SendMsg( Error, Error, "already connected" ); -// bus->RemoveApplication( this ); - Close(); + // Dilemma choose the rigth client to close + // the symetric processing will try to close each other + // only one side may be closed + IvyApplication *target; + if (MAXPORT(other->GetLocalPort(), other->GetRemotePort()) > MAXPORT( this->GetLocalPort(), this->GetRemotePort() )) + { + target = other; + TRACE("choose %s other ports %d,%d\n", appname.c_str(), target->GetLocalPort(), target->GetRemotePort()); + } + else + { + target = this; + TRACE("choose %s this ports %d,%d\n", appname.c_str(), target->GetRemotePort(), target->GetLocalPort()); + } + + TRACE("Quitting already connected %s\n", appname.c_str()); + target->SendMsg( Error, Error, "already connected" ); + // bus->RemoveApplication( target ); + target->Close(); + } } break; case EndRegexp: @@ -215,8 +247,7 @@ void IvyApplication::OnReceive(char * line) bus->CallApplicationConnectedCallback( this ); AppConnectedCallbackCalled = true; - if ( !bus->ready_message.empty() ) - SendMsg( bus->ready_message.c_str() ); + SendReadyToPeer(); break; case Msg: -- cgit v1.1