Index: XIOS/dev/dev_olga/src/client.cpp
===================================================================
--- XIOS/trunk/src/client.cpp	(revision 925)
+++ XIOS/dev/dev_olga/src/client.cpp	(revision 983)
@@ -18,5 +18,5 @@
     MPI_Comm CClient::interComm ;
     std::list<MPI_Comm> CClient::contextInterComms;
-    int CClient::serverLeader ;
+    int CClient::serverLeader;
     bool CClient::is_MPI_Initialized ;
     int CClient::rank = INVALID_RANK;
@@ -24,5 +24,14 @@
     StdOFStream CClient::m_errorStream;
 
-    void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)
+///---------------------------------------------------------------
+/*!
+ * \fn void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
+ * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups.
+ * \param [in] codeId identity of context.
+ * \param [in/out] localComm local communicator.
+ * \param [in/out] returnComm (intra)communicator of client group.
+ */
+
+    void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
     {
       int initialized ;
@@ -34,5 +43,5 @@
       if (!CXios::usingOasis)
       {
-// localComm doesn't given
+// localComm isn't given
         if (localComm == MPI_COMM_NULL)
         {
@@ -45,6 +54,6 @@
           boost::hash<string> hashString ;
 
-          unsigned long hashClient=hashString(codeId) ;
-          unsigned long hashServer=hashString(CXios::xiosCodeId) ;
+          unsigned long hashClient = hashString(codeId) ;
+          unsigned long hashServer = hashString(CXios::xiosCodeIdPrm);
           unsigned long* hashAll ;
           int size ;
@@ -58,5 +67,5 @@
           hashAll=new unsigned long[size] ;
 
-          MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ;
+          MPI_Allgather(&hashClient, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ;
 
           map<unsigned long, int> colors ;
@@ -77,5 +86,7 @@
           for (i=0; i < size; ++i)
           {
-            if (hashServer == hashAll[i])
+            if ((hashAll[i] == hashString(CXios::xiosCodeId))
+                || (hashAll[i] == hashString(CXios::xiosCodeIdPrm))
+                || (hashAll[i] == hashString(CXios::xiosCodeIdSnd)))
             {
               CXios::setUsingServer();
@@ -85,5 +96,4 @@
 
           myColor=colors[hashClient] ;
-
           MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ;
 
@@ -92,11 +102,10 @@
             int clientLeader=leaders[hashClient] ;
             serverLeader=leaders[hashServer] ;
-
             int intraCommSize, intraCommRank ;
             MPI_Comm_size(intraComm,&intraCommSize) ;
             MPI_Comm_rank(intraComm,&intraCommRank) ;
             info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize
-                 <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
-            MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
+                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
+             MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
           }
           else
@@ -123,5 +132,5 @@
       else
       {
-        // localComm doesn't given
+        // localComm isn't given
         if (localComm == MPI_COMM_NULL)
         {
@@ -150,6 +159,24 @@
     }
 
-
-    void CClient::registerContext(const string& id,MPI_Comm contextComm)
+    void CClient::initializeClientOnServer(const int rank, MPI_Comm& intraCommPrmSrv, const int srvSndLeader)
+    {
+      MPI_Comm_dup(intraCommPrmSrv, &intraComm) ;
+      serverLeader = srvSndLeader;
+      int intraCommSize, intraCommRank ;
+      MPI_Comm_size(intraComm,&intraCommSize) ;
+      MPI_Comm_rank(intraComm,&intraCommRank) ;
+      info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize
+          <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
+      MPI_Intercomm_create(intraComm, 0, CXios::globalComm, serverLeader, 0, &interComm) ;
+    }
+
+///---------------------------------------------------------------
+/*!
+ * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm)
+ * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups.
+ * \param [in] id id of context.
+ * \param [in] contextComm.
+ */
+    void CClient::registerContext(const string& id, MPI_Comm contextComm)
     {
       CContext::setCurrent(id) ;
@@ -158,5 +185,23 @@
       idServer += "_server";
 
-      if (!CXios::isServer)
+      if (CXios::isServer && !context->hasServer)
+      // Attached mode
+      {
+        MPI_Comm contextInterComm ;
+        MPI_Comm_dup(contextComm,&contextInterComm) ;
+        CContext* contextServer = CContext::create(idServer);
+
+        // Firstly, initialize context on client side
+        context->initClient(contextComm,contextInterComm, contextServer);
+
+        // Secondly, initialize context on server side
+        contextServer->initServer(contextComm,contextInterComm, context);
+
+        // Finally, we should return current context to context client
+        CContext::setCurrent(id);
+
+        contextInterComms.push_back(contextInterComm);
+      }
+      else
       {
         int size,rank,globalRank ;
@@ -170,5 +215,4 @@
         if (rank!=0) globalRank=0 ;
 
-
         CMessage msg ;
         msg<<idServer<<size<<globalRank ;
@@ -180,8 +224,8 @@
         buffer<<msg ;
 
-        MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
+        MPI_Send(buff, buffer.count(), MPI_CHAR, serverLeader, 1, CXios::globalComm) ;
         delete [] buff ;
 
-        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
+        MPI_Intercomm_create(contextComm, 0, CXios::globalComm, serverLeader, 10+globalRank, &contextInterComm) ;
         info(10)<<"Register new Context : "<<id<<endl ;
 
@@ -195,21 +239,4 @@
         MPI_Comm_free(&inter);
       }
-      else
-      {
-        MPI_Comm contextInterComm ;
-        MPI_Comm_dup(contextComm,&contextInterComm) ;
-        CContext* contextServer = CContext::create(idServer);
-
-        // Firstly, initialize context on client side
-        context->initClient(contextComm,contextInterComm, contextServer);
-
-        // Secondly, initialize context on server side
-        contextServer->initServer(contextComm,contextInterComm, context);
-
-        // Finally, we should return current context to context client
-        CContext::setCurrent(id);
-
-        contextInterComms.push_back(contextInterComm);
-      }
     }
 
@@ -221,5 +248,5 @@
       MPI_Comm_rank(intraComm,&rank) ;
  
-      if (!CXios::isServer)
+      if ((!CXios::isServer) || (CXios::serverLevel == 1))
       {
         MPI_Comm_rank(intraComm,&rank) ;
@@ -252,4 +279,5 @@
       report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
       report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ;
+
    }
 
