Esempio n. 1
0
int 
main (int argc, char *argv[])
{
  u_short logger_port  = argc > 1 ? atoi (argv[1]) : LOGGER_PORT;
  const char *logger_host = argc > 2 ? argv[2] : LOGGER_HOST;

  ACE_SOCK_Stream logger;
  ACE_SOCK_Connector connector;
  ACE_INET_Addr addr (logger_port, logger_host);
  ACE_Log_Record log_record (LM_DEBUG, 
			     ACE_OS::time ((time_t *) 0), 
			     ACE_OS::getpid ());

  if (connector.connect (logger, addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);
  
  log_record.msg_data (DATA);
  size_t len = log_record.length ();
  log_record.encode ();

  if (logger.send ((char *) &log_record, len) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send"), -1);
  else if (logger.close () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "close"), -1);
  return 0;
}
Esempio n. 2
0
///////////////////////////////////////////////////////////////////////////
//  <summary>
//  This method writes data from the given buffer to the underlying stream.
//  It can block or not, depending on the value of the blocking parameter.
//  </summary>
//
//  <param name = "buffer">
//  The buffer that contains the data to be written.
//  </param>
//
//  <param name = "size">
//  The size of the buffer in bytes of the buffer.
//  </param>
//
//  <param name = "blocking">
//  True if the write request should block;  false otherwise.
//  </param>
//
//  <param name = "bytesWritten">
//  An out parameter that will contain the number of bytes that have been
//  written to the stream.
//  </param>
//
//  <returns>
//  Returns a MgStreamStatus value indicating the status of the operation.
//  </returns>
MgStreamHelper::MgStreamStatus MgAceStreamHelper::WriteData(void* buffer,
    size_t size, bool blocking, size_t* bytesWritten)
{
    // Do not attempt writing zero byte to the socket as this could be problematic.
    if (0 == size)
    {
        return MgStreamHelper::mssDone;
    }

    ACE_ASSERT( buffer && size > 0 );
    MgStreamHelper::MgStreamStatus stat = MgStreamHelper::mssError;

    //  check parameters
    if ( buffer && size > 0 )
    {
        //  init out parameter
        if ( bytesWritten != NULL )
            *bytesWritten = 0;

        ACE_SOCK_Stream stream;
        stream.set_handle( m_handle );

        ssize_t res = 0;

        // On Linux, use MSG_NOSIGNAL to request not to send SIGPIPE on
        // errors on stream oriented sockets when the other end breaks
        // the connection. The EPIPE error is still returned.
        // Note that neither trapping the SIGPIPE signal via an
        // ACE_Event_Handler nor calling ACE_OS::signal(SIGPIPE, SIG_IGN)
        // seems to work.

        if ( blocking )
        {
            res = stream.send_n(buffer, size, MG_MSG_NOSIGNAL);
        }
        else
        {
            res = stream.send(buffer, size, MG_MSG_NOSIGNAL);
        }

        //  check for failure
        if ( res >= 0 )
        {
            //  update out parameter
            if ( bytesWritten != NULL )
                *bytesWritten = res;

            if ( res == (ssize_t)size )
            {
                stat = MgStreamHelper::mssDone;
            }
            else
            {
                stat = blocking ? MgStreamHelper::mssError : MgStreamHelper::mssNotDone;
            }
        }
    }

    return stat;
}
Esempio n. 3
0
void Collector::PollData()
{
	ACE_SOCK_Stream peer;
	ACE_SOCK_Connector connector;
	try
	{
		if(TryToConnect(connector,peer))
		{
			std::ostringstream oss;
			oss << "poll data form cluster: " << data_source_.name;
			LOG4CXX_INFO(log_,oss.str());

			char buf[T_SIZE] = "request";
			if((peer.send(buf,sizeof(buf)))<=0)
				throw "KSN POLLER COLLECTOR SEND REQUEST ERROR!!";
			char* ch = new char[T_SIZE]();
			if((peer.recv(ch,T_SIZE)<=0))
				throw "KSN POLLER COLLECTOR RECV ERROR!!";

			msg_ = new ACE_Message_Block(ch,sizeof(*ch));
			sender_->putq(msg_);
		}
		else
		{
			std::ostringstream oss;
			oss << "Can not poll data form " + data_source_.name;
			LOG4CXX_WARN(log_,oss.str());
		}
	}
	catch(const char* ch)
	{
		LOG4CXX_ERROR(log_,ch);
	}
}
Esempio n. 4
0
int	extractCron::do_job(char *jobdesc)
{
	ACE_SOCK_Connector conn;
	ACE_SOCK_Stream  peer;
	ACE_Time_Value tv(3, 0);
	ACE_INET_Addr	addr(harvestPort, harvestIPaddr);
	char	urlbuff[1024];
	StrStream	httpreq;
	int	ret;

	printf("JobDesc: %s\n", jobdesc);
	CGI::escape(urlbuff, jobdesc);
	httpreq.init(2); // 2 kilobytes buff
	httpreq.rawadd("GET /harvest?expr=");
	httpreq.rawadd(urlbuff);
	httpreq.rawadd(" HTTP/1.1\n\n");

	httpreq.print();
	
	if ( conn.connect(peer, addr) < 0) {
		printf("conn failed!\n");
		return 0;
	}

	ret = peer.send( httpreq.str(), httpreq.len() );
	return 0;
	
}
Esempio n. 5
0
static void *
client (void *)
{
  char buf[100];
  size_t mes_len;
  ACE_OS::sleep (1);

  ACE_DEBUG ((LM_DEBUG,
              " (%P) Client: Starting...\n"));

  ACE_SOCK_Stream stream;
  ACE_SOCK_Connector connector;
  ACE_OS::sprintf (buf, "Client: the life was good!");

  mes_len = (int) htonl (ACE_OS::strlen (buf) + 1);

  if (connector.connect (stream,
                         ACE_INET_Addr (SERV_TCP_PORT,
                                        ACE_DEFAULT_SERVER_HOST)) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket open"));

  if (stream.send (4,
                   (void *) &mes_len,
                   sizeof (size_t),
                   (void *)buf,
                   ACE_OS::strlen (buf) + 1) == -1)

    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket send"));

  if (stream.close () == -1)
    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket close"));

  ACE_DEBUG ((LM_DEBUG,
              "(%P) Client: Message has been sent, about to exit...\n"));
  return 0;
}
Esempio n. 6
0
/*the accept function*/
static ACE_THR_FUNC_RETURN accept_step1(void *arg)
{
  /*ACE_INET_Addr addr;*/
  ACE_SOCK_Stream stream;
  ACE_HANDLE handle = (ACE_HANDLE)(intptr_t) arg;
  stream.set_handle(handle);
  if(stream.disable(ACE_NONBLOCK) == -1){
    ACE_ERROR_RETURN((LM_ERROR,
		      "%p\n","get_remote_addr"),0);
  }
  /*
  ACE_DEBUG ((LM_INFO,
              "(%P|%t) client %s connected from %d\n",
              addr.get_host_name (),
              addr.get_port_number ()));
  */
  char* buf = new char[128];
  ACE_CString* str = new ACE_CString();
  do{
    int bytes= stream.recv(buf,128);
    ACE_DEBUG((LM_INFO,
	       "(%P|%t:%l) bytes = %d\n",bytes));
    if(bytes == -1|| bytes == 0){
      break;
    }
    for(int i=0 ;i < bytes; i++){
      *str += buf[i];
    }
  }while(true);
  delete[] buf;
  
  //the input format is '^^pqr->v1$$';
  int pos = str->find("->");
  int tail = str->find("$$");
  ACE_CString* pqr = new ACE_CString(str->substr(2,pos-2));
  ACE_CString* pv1 = new ACE_CString(str->substr(pos+2, tail - pos - 2));
  ACE_DEBUG((LM_INFO,
	     "(%P|%t:%l) pqr: %s\n pv1:%s\n",pqr->c_str(),pv1->c_str()));
	     
  bn* _pqr = from_hex(pqr);
  bn* _v1 = from_hex(pv1);
  bn* _pr = from_hex(&pr);
  bn* _r = npmod( _v1, _pr, _pqr);

  ACE_CString* result = _r->to_hex();
  ACE_DEBUG((LM_INFO,
	     "(%P|%t:%l)pqr:%s step1:%s ", pqr->c_str(), result->c_str()));
  ACE_CString reply = ACE_CString("ack");
  stream.send(reply.c_str() , reply.length());
  stream.close();
  /*send the step1 result to hub:10007
  */
  ACE_SOCK_Connector connector;
  ACE_INET_Addr hub_addr(port,hub.c_str());
  ACE_DEBUG((LM_DEBUG,
	     "(%P|%t:%l) port:%d host:%s\n", port, hub.c_str()));
  if(connector.connect(stream, hub_addr) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) %p\n",
                       "connection failed"),
                      0);
  }
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) connected to %s at port %d\n",
                hub_addr.get_host_name (),
                hub_addr.get_port_number ()));

  /*
   * message layout:
^^pqr->digest->senderid$$
  */
  
  ACE_CString input = ACE_CString("^^");
  input += *pqr;
  input += "->";
  input += *result;
  input += "->";
  input += id ;
  input += "$$";
  ACE_DEBUG((LM_INFO,
	     "(%P|%t:%l) input of step1:%s\n", input.c_str()));
  
  if(stream.send(input.c_str(),input.length()) != input.length()){
    ACE_ERROR((LM_ERROR,
	       "%p\n","send"));
  }
  stream.close();
  delete str;
  delete pqr;
  delete pv1;
  delete _pqr;
  delete _v1;
  delete _pr;
  delete _r;
  delete result;
  return 0;
}
Esempio n. 7
0
/*the repo function
  talk to the repo.jg.org at a regular interval, 1 minute,
  and send acknowledgement to repo.jg.org
 */
static ACE_THR_FUNC_RETURN
fetch_step2(void *){
  ACE_INET_Addr addr(repo_port, repo_host.c_str());
  ACE_SOCK_Connector con;
  ACE_SOCK_Stream stream;
  do{
  if(con.connect(stream, addr) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t:%l) %p\n",
                       "connection failed"),
                      0);
  }
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t:%l) connected to %s at port %d\n",
                addr.get_host_name (),
                addr.get_port_number ()));
  ACE_CString hb = ACE_CString("^^hb->");
  hb+=id+"$$";
  ACE_DEBUG((LM_INFO,
	     "%l: %s\n", hb.c_str()));
  
  stream.send(hb.c_str(),hb.length());
  stream.close_writer();
  ACE_DEBUG((LM_INFO,
	     "%l\n"));
  
  char* buf = new char[128];
  ACE_CString str;
  do{
    int bytes = stream.recv(buf,128);
    if(bytes == -1 || bytes == 0){
      break;
    }
    for(int i=0; i< bytes; i++){
      str += buf[i];
    }
  }while(true);
  delete[] buf;
  stream.close();

  ACE_CString acks = ACE_CString("^^");
  int p1=2;
  int p2= str.find("->",p1);
  int p3 = str.find("->",p2);
  int p4 = str.find("||",p3);
  node *head=NULL, *tail=NULL;
  while(p1 != -1 && p2 != -1 && p3 != -1){
    ACE_CString* pqr = new ACE_CString(str.substr(p1, p2 - p1));
    ACE_CString* v2 = new ACE_CString(str.substr(p2, p3 - p2));
    ACE_CString txn_id = str.substr(p3, p4 - p3);
    acks+="ack->"+txn_id+"||";
    node* n = new node(pqr, v2);
    if(head == NULL){
      head = n;
      tail = n;
    }else{
      tail->next = n;
      tail = n;
    }
  }
  acks+="$$";
  //  delete str;
  
  if(con.connect(stream, addr) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t:%l) %p\n",
                       "connection failed"),
                      0);
  }
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t:%l) connected to %s at port %d\n",
                addr.get_host_name (),
                addr.get_port_number ()));
  //send out the acks
  ACE_DEBUG((LM_INFO,
	     "(%P|%t:%l) %s\n",acks.c_str()));
  stream.send(acks.c_str(),acks.length());
  stream.close();
  node* tmp=head;
  //    ACE_SOCK_Connector connector;
  ACE_INET_Addr _9907addr(9907,"localhost");
  //ACE_SOCK_Stream stream;
  if(con.connect(stream, _9907addr) == -1){
    ACE_ERROR_RETURN((LM_ERROR,
		      "(%P|%t:%l) %p\n",
		      "connection failed"),0);
  }else
    ACE_DEBUG ((LM_DEBUG,
		"(%P|%t) connected to %s at port %d\n",
		_9907addr.get_host_name (),
		_9907addr.get_port_number ()));

  ACE_CString reply = ACE_CString("^^");
  while(tmp!=NULL){
    bn* pqr = from_hex(tmp->pqr);
    bn* v2 = from_hex(tmp->v2);
    bn* prv = from_hex(&pr);
    bn* res = npmod(v2,prv,pqr);
    ACE_CString* resStr = res->to_hex();
    /*send the output to localhost:9907*/
    reply += tmp->pqr->c_str();
    reply += "->" + *resStr + "||";
    ACE_DEBUG((LM_DEBUG,
	       "(%P|%t) %s\n",reply.c_str()));
    delete pqr;
    delete v2;
    delete prv;
    delete res;
    delete resStr;
    tmp = tmp->next;
  }
  reply += "$$";
  stream.send(reply.c_str(),reply.length());
  stream.close();
  reclaim_node(head);
  /*sleep for 1 second, for the next poll;*/
  ACE_OS::sleep(1000);
  }while(true);
  //  delete str;
}
Esempio n. 8
0
/*
 *                    N W R I T E
 */
int
Nwrite (ACE_SOCK_Stream &s, void *buf, int count)
{
  numCalls++;
  return s.send (buf, count);
}
Esempio n. 9
0
// receive the heartbeat information, and return back the enquened messages;
static ACE_THR_FUNC_RETURN
commu(void* arg){
  ACE_INET_Addr addr;
  ACE_SOCK_Stream stream;
  ACE_HANDLE handle =(ACE_HANDLE)(intptr_t) arg;
  stream.set_handle(handle);
  if(stream.disable(ACE_NONBLOCK) == -1){
    ACE_ERROR_RETURN((LM_ERROR,
		      "%p\n","disable"),0);
  }
  else if(stream.get_remote_addr(addr)== -1){
    ACE_ERROR_RETURN((LM_ERROR,
		      "%p\n","get_remote_addr"),0);
  }
  ACE_DEBUG((LM_INFO,
	     "(%P|%t) client %s connected from %d\n",
	     addr.get_host_name(),addr.get_port_number()));
  ACE_CString str ;
  char* ch = new char[128];  
  do{
    int bytes = stream.recv(ch, 128);
    if(bytes == -1){
      ACE_ERROR((LM_ERROR,
		 "%p\n","recv"));
      break;
    }
    if(bytes == 0){
      ACE_DEBUG((LM_INFO,
		 "(%P|%t) reached end of input, connection closed by client\n"));
      break;
    }
    for(int i = 0; i< bytes; i++){
      str += ch[i];
    }
  }while(true);
  delete[] ch;
  ACE_DEBUG((LM_INFO,
	     "received message:%s\n", str.c_str()));
  
  /*
    the layout of the heartbeat message:
    ^^hb->senderid$$
   */
  /*if got a heartbeat message*/
  int pos = str.find("^^hb->");
  if( pos >= 0 ){
    int p2 = str.find("$$");
    ACE_CString id= str.substr(6, p2 -6);
    //    delete str;
    ACE_CString sql = "select pqr, v2,txn_id from step2 where recipient='"+ id +"' and transmitted='false' ";
    ACE_DEBUG((LM_DEBUG,
	       "%s\n", sql.c_str()));
    PGconn* con;
    PGresult* res;
    con = PQconnectdb("dbname=pq");
    if(PQstatus(con)!= CONNECTION_OK){
      ACE_DEBUG((LM_INFO,
		 "Connection to database failed:%s\n",
		 PQerrorMessage(con)));
      reclaim_conn(con);
    }
    res = PQexec(con, sql.c_str());
    int n = PQntuples(res);
    if(n == 0){
      /*no pending messages at all, exit immediately*/
      ACE_DEBUG((LM_INFO,
		 "(%P|%t) no pending messages at all\n"));
      PQclear(res);
      reclaim_conn(con);
      stream.close();
      //      delete str;
      return 0;
  }
    /*there are pending messages*/
    else{
      /* the reply message is in format "^^pqr1->v1->txn_id1||pqr2->v2->txn_id2||pqr3->v3->txn_id3||$$"*/
      ACE_CString reply = ACE_CString("^^");
      for(int i=0; i < n ; i++){
	reply += PQgetvalue(res, i, 0);
	reply += "->";
	reply += PQgetvalue(res, i, 1);
	reply += "->";
	reply += PQgetvalue(res, i, 2);
	reply += "||";
      }
      reply += "$$";
      PQclear(res);
      /*    reclaim_conn(con);
       */
      stream.send(reply.c_str(), reply.length());
      ACE_DEBUG((LM_INFO,
		 "(%P|%t:%l) %s\n", reply.c_str()));
      ACE_DEBUG((LM_INFO,
		 "(%P|%t) close the writer\n"));
      stream.close();
      //      delete str;
      return 0;
    }
  }
  
  /*if got an ack message
    '^^ack->txn_id2||ack->txn_id2||$$'
   */
  int p = str.find("^^ack->");
  if( p >= 0){
    PGconn* con;
    PGresult* res;
    con = PQconnectdb("dbname=pq");
    if(PQstatus(con)!= CONNECTION_OK){
      ACE_DEBUG((LM_INFO,
		 "Connection to database failed:%s\n",
		 PQerrorMessage(con)));
      reclaim_conn(con);
    }
    do{
      p += 7;
      int p2 = str.find("||",p);
      if(p2< 0){
	ACE_DEBUG((LM_INFO,
		   "reached the end of the txn_ids\n"));
	break;
      }
      ACE_CString txn_id = str.substr(pos, p2 - p);
      ACE_CString sql = ACE_CString("update step2 set transmitted=TRUE where txn_id='");
      sql += txn_id;
      sql += "'";
      ACE_DEBUG((LM_INFO,
	       "(%P|%t:%l) sql:%s\n" , sql.c_str()));
      res = PQexec(con, sql.c_str());
      PQclear(res);
      p = p2;
    }while(true);
    reclaim_conn(con);
    stream.close();
    //    delete str;
    return 0;
  }
  return 0;
}
Esempio n. 10
0
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
    if(argc < 4 )
    {
        printf("Usage: %s <ip> <port> <content> [rtimes] [interval]\n", argv[0]);
	ACE_DEBUG((LM_ERROR, "ERROR:\n"));
        return 1;
    }
        
    char buffer[1024];    
    
    int rtimes = 1; //执行次数
    if(argc > 4 )
    {
        rtimes = atoi(argv[4]);
    }
    
    int interval = 1;//执行间隔
    if(argc > 5 )
    {
        interval = atoi(argv[5]);
    }
    
    ACE_INET_Addr svr_addr(atoi(argv[2]),argv[1]);
    ACE_Time_Value time_out(1); //等待 1 s 
    ACE_Time_Value zero(0,0); //不等待 立即返回 
    ACE_Time_Value *time_null=NULL; //会导致永久阻塞
    
    int send_len = 0;
    int recv_len = 0;

#ifdef UDP

    ACE_INET_Addr my_addr; //ACE_static_cast (u_short, 10101)
    ACE_SOCK_Dgram udp(my_addr);//(my_addr)
    for(int i=0; i<rtimes; i++)
    {
        memset(buffer, 0, sizeof(buffer));
        send_len = snprintf(buffer, "%s\r\n", argv[3]);
        
        ssize_t sent = udp.send (buffer, send_len, svr_addr);
        ACE_DEBUG((LM_INFO, 
            "[%D] send udp msg"		
            ",send=%d"
            ",msg is %s"     
            "\n"
            ,sent
            ,buffer
            ));
        
        memset(buffer, 0, sizeof(buffer));
        recv_len = udp.recv (buffer, sizeof(buffer), svr_addr, 0, &time_out);
        
        ACE_DEBUG((LM_INFO, 
            "[%D] recv udp msg"
            ",recv_len=%d"
            ",msg is %s"     
            "\n"
            ,recv_len
            ,buffer
            ));
        usleep(interval*1000);        
    }    
    udp.close ();
    
#else
       
    ACE_SOCK_Connector connector;
    ACE_SOCK_Stream peer;
    
    //peer.enable(ACE_NONBLOCK); //没效果,导致提示 Bad file descriptor 
    if(-1 == connector.connect(peer, svr_addr))
    {
        ACE_DEBUG((LM_INFO, 
                    "[%D] connect failed,ip=%s"
                    ",port=%s"                    
                    ",errno=%d"
                    ",errmsg=%s"
                    "\n"
                    , argv[1]
                    , argv[2]
                    ,errno
                    ,strerror(errno)
                    ));
        return -1;
    }
    
    for(int i=0; i<rtimes; i++)
    {
        memset(buffer, 0, sizeof(buffer));
#ifndef MSG_LEN_SIZE		
        send_len = sprintf(buffer, "%s\n", argv[3]);
        send_len = peer.send(buffer, send_len);
        ACE_DEBUG((LM_INFO, 
            "[%D] send msg"                    
            ",errno=%d"
            ",errmsg=%s"
            ",send_len=%d"
            ",msg is %s" 
            ,errno
            ,strerror(errno)
            ,send_len
            ,buffer
            ));

        // 接收消息 
        memset(buffer, 0, sizeof(buffer));
        recv_len = peer.recv(buffer, sizeof(buffer), &time_out);
        if(recv_len>0)
        {
            ACE_DEBUG((LM_INFO, "[%D] recv_len=%d,msg is ", recv_len));
            write(1, buffer, recv_len);
            write(1, "\n", 2);
        }
        else if(0==recv_len)
        {
            printf("server closed socket\n");
        }
        else
        {
            if(ETIME==errno)
                printf("server timeout\n");
            else
                printf("server return error,errno=%d,errmsg=%s\n", errno, strerror(errno));
        }
#else
        sprintf(buffer+MSG_LEN_SIZE, "%s\n", argv[3]);
                
        send_len = strlen(buffer+MSG_LEN_SIZE)+MSG_LEN_SIZE; 
        
        //int *p_len = (int*)buffer; //把前面字节设置为数据长度
        //*p_len = htonl(send_len-MSG_LEN_SIZE); //转成网络字节序
        uint16_t *p_len = (uint16_t*)buffer;
        *p_len = htons(send_len-MSG_LEN_SIZE);
        
        ACE_DEBUG((LM_INFO, 
            "[%D] send msg with head"
            ",send_len=%d"
            ",*p_len=%d"
            ",msg is %s"
            "\n"
            ,send_len
            ,ntohs(*p_len) //显示正常信息
            ,buffer+MSG_LEN_SIZE
            ));
        send_len = peer.send(buffer, send_len);
        ACE_DEBUG((LM_INFO,"[%D] send msg"
			",errno=%d,errmsg=%s"
			",ret=%d,msg=%s"
            , errno
            , strerror(errno)
            , send_len, buffer+MSG_LEN_SIZE
            ));
        
        // 接收消息
        memset(buffer, 0, sizeof(buffer));
        recv_len = peer.recv(buffer, sizeof(buffer), &time_out);
        if(recv_len>0)
        {
            ACE_DEBUG((LM_INFO, "[%D] recv_len=%d,msg is ", recv_len));
            write(1, buffer, recv_len);
            write(1, "\n", 2);
        }
        else if(0==recv_len)
        {
            printf("server closed socket\n");
        }
        else
        {
            if(ETIME==errno)
                printf("server timeout\n");
            else
                printf("server return error,errno=%d,errmsg=%s\n", errno, strerror(errno));
        }
#endif
        usleep(interval*1000);
    }

    //晚点退出,测试用 
    //sleep(100);
    peer.close();
#endif
    
    return 0;
}
Esempio n. 11
0
static ACE_THR_FUNC_RETURN
process(void* arg){
  ACE_INET_Addr addr;
  ACE_SOCK_Stream stream;
  ACE_HANDLE handle = (ACE_HANDLE) (intptr_t) arg;
  stream.set_handle(handle);
  /*make sure we're not in non-blocking mode.*/
  if(stream.disable(ACE_NONBLOCK) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "disable"),
		      0);
  }
  else if(stream.get_remote_addr(addr) == -1){
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "get_remote_addr"),
		      0);
  }
  ACE_DEBUG ((LM_INFO,
	      "(%P|%t:%l) client %s connected from %d\n",
	      addr.get_host_name (),
	      addr.get_port_number ()));
  
  int r_bytes = 0;
  char buf[SIZE];
  ACE_CString cs;
  do{
    r_bytes = stream.recv(buf, SIZE);

    if(r_bytes == 0 || r_bytes == -1){
      ACE_DEBUG((LM_INFO,
		 "(%P|%t:%l) r_bytes = %d, exit from the loop\n", r_bytes));
      break;
    }
    for(int i=0; i< r_bytes; i++){
      cs += buf[i];
    }
  }while(true);
  stream.close_reader();
  /*
  ACE_DEBUG((LM_INFO,
	     "%s\n", cs.c_str()));
  */
  /*the layout of the message would be:
   * ^^pq||step1$$
   * 1st step: get the d by pq
   * 2nd calculate
   * 3rd send back the digest
   */
  int p0, p1;
  int len = cs.length();
  p0 = 2;
  p1 = cs.find("||");
  ACE_CString pq = cs.substr(2, p1 - 2);
  ACE_CString step1 = cs.substr(p1 + 2, len - p1 -4);
  /*
  ACE_DEBUG((LM_INFO,
	     "pq = %s\n step1 = %s\n", pq.c_str(), step1.c_str()));
  */
  //get the d
  ACE_CString sql = "select d,textid from player0 where pq='";
  sql += pq;
  sql += "'";
  /*
  ACE_DEBUG((LM_INFO,
	     "sql = %s\n", sql.c_str()));
*/
  PGconn* con;
  con = PQconnectdb("host=45.33.3.188 port=5432 dbname=nv user=dec");
  PGresult* res;
  if(PQstatus(con)!= CONNECTION_OK){
    ACE_DEBUG((LM_INFO,
	       "Connection to database failed:%s\n",
	       PQerrorMessage(con)));
    reclaim_conn(con);
  }
  res = PQexec(con, sql.c_str());
  int n = PQntuples(res);
  if(n != 1){
    ACE_ERROR((LM_ERROR,
	       "there is significant error: %d\n", n));
    //    return 0;
  }
  ACE_CString d = "";
  d += PQgetvalue(res, 0, 0);
  /*
  ACE_DEBUG((LM_INFO,
	     "d = %s\n", d.c_str()));
*/
  bn* pq_ = from_hex(&pq);
  bn* d_ = from_hex(&d);
  bn* step1_ = from_hex(&step1);
  bn* step2_ = npmod(step1_ , d_ , pq_ );
  ACE_CString* step2 = step2_->to_hex();
  
  //check if the pq is taking the ownnership?

  /* if pq is 'centralbank', bypass the check, otherwise check the payer whether has the ownnership of the note;
   * select count(*) from ownership0 o, player0 p where o.owner = p.textid  and o.note='0x12345678' and p.pq='0x12345678';
   */
  bn* e_ = new bn(1);
  e_->addat(0,0x10001);
  bn* step3_ = npmod(step2_ , e_ , pq_ );
  ACE_CString* rawMsg = encode(step3_);
  ACE_DEBUG((LM_INFO,
	     "%T :%l step3 = %s\n", rawMsg->c_str()));
  if(verify_ownership(rawMsg, pq)){
    //update the ownership;
    int pos1 = rawMsg->find("->");
    ACE_CString note = rawMsg->substr(2, pos1 -2);
    int rawlen = rawMsg->length();
    ACE_CString id = rawMsg->substr(pos1 + 2, rawlen - pos1 - 6);
    update_ownership(note, id);
    /*insert the log entry*/
    insert_logentry(pq, *step2, *rawMsg, note, id);
    stream.send( step2->c_str(), step2->length());
    /*
    ACE_DEBUG((LM_INFO,
	       "%T :%l the step2 message is:\n%s\n", step2->c_str()));
    */
  }else{
    ACE_DEBUG((LM_INFO,
	       "%T :%l invalid transfer\n"));
    ACE_CString reply ="invalid transfer\n";
    stream.send( reply.c_str(), reply.length());
  }
  delete step2 ;
  delete pq_;
  delete d_;
  delete step1_;
  delete step2_;
  delete e_;
  delete step3_;
  delete rawMsg;
  /*
  stream.close_writer();
  */
  stream.close();
  ACE_DEBUG((LM_INFO,
	     "%T :%l closed the stream\n"));

}