Exemplo n.º 1
0
/******************************ConnectCommand****************************************/
int ConnectCommand::execute( ossSocket & sock, std::vector<std::string> & argVec )
{
   int rc = EDB_OK;
   _address = argVec[0];
   _port = atoi(argVec[1].c_str());
   sock.close();
   sock.setAddress(_address.c_str(), _port);
   rc = sock.initSocket();
   if ( rc )
   {
      printf ( "Failed to init socket, rc = %d", rc ) ;
      goto error ;
   }
   rc = sock.connect();
   if ( rc )
   {
      printf ( "Failed to connect, rc = %d", rc ) ;
      goto error ;
   }
   sock.disableNagle();
done :
   return rc ;
error :
   goto done ;
}
Exemplo n.º 2
0
/******************************ConnectCommand****************************************/
int ConnectCommand::execute( ossSocket & sock, std::vector<std::string> & argVec )
{
   int ret = EDB_OK;
   if(argVec.size() < 2)
   {
      printf("too little argument for fuction: ConnectCommand::execute()\n");
      return getError(EDB_QUERY_INVALID_ARGUMENT);
   }
   _address = argVec[0];
   _port = atoi(argVec[1].c_str());
   sock.close();
   sock.setAddress(_address.c_str(), _port);
   ret = sock.initSocket();
   if(ret)
   {
      return getError(EDB_SOCK_INIT_FAILED);
   }
   ret = sock.connect();
   if(ret)
   {
      return getError(EDB_SOCK_CONNECT_FAILED);
   }
   sock.disableNagle();
   return ret;
}
Exemplo n.º 3
0
int ICommand::recvReply( ossSocket & sock )
{
   // define message data length.
   int length = 0;
   int ret = EDB_OK;
   // fill receive buffer with 0.
   memset(_recvBuf, 0, RECV_BUF_SIZE);
   if( !sock.isConnected() )
   {
      return getError(EDB_SOCK_NOT_CONNECT);
   }
   while(1)
   {
      // receive data from the server.first receive the length of the data.
      ret = sock.recv(_recvBuf, sizeof(int));
      if( EDB_TIMEOUT == ret )
      {
         continue;
      }
      else if( EDB_NETWORK_CLOSE == ret )
      {
         return getError(EDB_SOCK_REMOTE_CLOSED);
      }
      else
      {
         break;
      }
   }
   // get the value of length.
   length = *(int*)_recvBuf;
   // judge the length is valid or not.
   if(length > recvBufferSize)
   {
      _recvBuf = (char*)realloc ( _recvBuf, sizeof(char) * length ) ;
      *(int *)_recvBuf = length;
      recvBufferSize = length;
      //return getError(EDB_RECV_DATA_LENGTH_ERROR);
   }

   // receive data from the server.second receive the last data.
   while(1)
   {
      ret = sock.recv(&_recvBuf[sizeof(int)],length-sizeof(int));
      if(ret == EDB_TIMEOUT)
      {
         continue;
      }
      else if(EDB_NETWORK_CLOSE == ret)
      {

         return getError(EDB_SOCK_REMOTE_CLOSED);
      }
      else
      {
         break;
      }
   }
   return ret;

}
Exemplo n.º 4
0
int QuitCommand::execute( ossSocket & sock, std::vector<std::string> & argVec )
{
   int ret = EDB_OK;
   if( !sock.isConnected() )
   {
      return getError(EDB_SOCK_NOT_CONNECT);
   }
   ret = sendOrder( sock, OP_DISCONNECT);
   sock.close();
   ret = handleReply();
   return ret;
}
Exemplo n.º 5
0
int ICommand::sendOrder( ossSocket & sock, OnMsgBuild onMsgBuild  )
{
   int ret = EDB_OK;
   bson::BSONObj bsonData;
   try {
      bsonData = bson::fromjson(_jsonString);
   } catch( std::exception & e) {
      return getError(EDB_INVALID_RECORD);
   }
   memset(_sendBuf,0, SEND_BUF_SIZE);
   int size = SEND_BUF_SIZE;
   char * pSendBuf = _sendBuf;
   ret = onMsgBuild(&pSendBuf, &size, bsonData);
   if(ret)
   {
      return getError(EDB_MSG_BUILD_FAILED);
   }
   ret = sock.send( pSendBuf, *(int*)pSendBuf );
   if(ret)
   {
      return getError(EDB_SOCK_SEND_FAILD);
   }
   return ret;

}
Exemplo n.º 6
0
int InsertCommand::execute( ossSocket & sock, std::vector<std::string> & argVec )
{
   int rc = EDB_OK;
   if( argVec.size() <1 )
   {
      return getError(EDB_INSERT_INVALID_ARGUMENT);
   }
   _jsonString = argVec[0];
     if( !sock.isConnected() )
   {
      return getError(EDB_SOCK_NOT_CONNECT);
   }

   rc = sendOrder( sock, msgBuildInsert );
   PD_RC_CHECK ( rc, PDERROR, "Failed to send order, rc = %d", rc ) ;

   rc = recvReply( sock );
   PD_RC_CHECK ( rc, PDERROR, "Failed to receive reply, rc = %d", rc ) ;
   rc = handleReply();
   PD_RC_CHECK ( rc, PDERROR, "Failed to receive reply, rc = %d", rc ) ;
done :
   return rc;
error :
   goto done ;
}
Exemplo n.º 7
0
/******************************ConnectCommand****************************************/
int ConnectCommand::execute( ossSocket & sock, std::vector<std::string> & argVec )
{
   int ret = EDB_OK;
   _address = argVec[0];
   _port = atoi(argVec[1].c_str());
   sock.close();
   sock.setAddress(_address.c_str(), _port);
   ret = sock.initSocket();
   if(ret)
   {
      return getError(EDB_SOCK_INIT_FAILED);
   }
   ret = sock.connect();
   if(ret)
   {
      return getError(EDB_SOCK_CONNECT_FAILED);
   }
   sock.disableNagle();
   return ret;
}
Exemplo n.º 8
0
int ICommand::sendOrder( ossSocket & sock, int opCode )
{
   int ret = EDB_OK;
   memset(_sendBuf, 0, SEND_BUF_SIZE);
   char * pSendBuf = _sendBuf;
   MsgHeader *header = (MsgHeader*)pSendBuf;
   header->messageLen = sizeof(MsgHeader);
   header->opCode = opCode;
   ret = sock.send(pSendBuf, *(int*)pSendBuf);
   return ret;
}
Exemplo n.º 9
0
int ICommand::sendOrder( ossSocket & sock, int opCode )
{
   int ret = EDB_OK;
   memset(_sendBuf, 0, SEND_BUF_SIZE);
   char * pSendBuf = _sendBuf;
   const char *pStr = "hello world" ;
   *(int*)pSendBuf=strlen(pStr)+1 + sizeof(int) ;
   memcpy ( &pSendBuf[4], pStr, strlen(pStr)+1 ) ;
   /*MsgHeader *header = (MsgHeader*)pSendBuf;
   header->messageLen = sizeof(MsgHeader);
   header->opCode = opCode;*/
   ret = sock.send(pSendBuf, *(int*)pSendBuf);
   return ret;
}
Exemplo n.º 10
0
int SnapshotCommand::execute( ossSocket & sock, std::vector<std::string> &argVec)
{
   int rc = EDB_OK;
   if( !sock.isConnected() )
   {
      return getError(EDB_SOCK_NOT_CONNECT);
   }

   rc = sendOrder( sock, OP_SNAPSHOT );
   PD_RC_CHECK ( rc, PDERROR, "Failed to send order, rc = %d", rc);
   rc = recvReply( sock );
   PD_RC_CHECK ( rc, PDERROR, "Failed to receive reply, rc = %d", rc);
   rc = handleReply();
   PD_RC_CHECK ( rc, PDERROR, "Failed to receive reply, rc = %d", rc);
done :
   return rc;
error :
   goto done ;
}