/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// This example is from the ACE Programmers Guide.
////  Chapter:  "The Acceptor/Connector"  (Connection Initialization)
//// For details please see the guide at
//// http://www.cs.wustl.edu/~schmidt/ACE.html
////  AUTHOR: Umar Syyid ([email protected])
//// and Ambreen Ilyas ([email protected])
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Example 5
#include "ace/Reactor.h"
#include "ace/Svc_Handler.h"
#include "ace/Acceptor.h"
#include "ace/Synch.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/Thread.h"

//Add our own Reactor singleton
typedef ACE_Singleton<ACE_Reactor,ACE_Null_Mutex> Reactor;
//Create an Acceptor
typedef ACE_Acceptor<MyServiceHandler,ACE_SOCK_ACCEPTOR> Acceptor;
//Create a Connector
typedef ACE_Connector<MyServiceHandler,ACE_SOCK_CONNECTOR> Connector;

class MyServiceHandler:
public ACE_Svc_Handler<ACE_SOCK_STREAM,ACE_NULL_SYNCH>{
public:
 //Used by the two threads "globally" to determine their peer stream
 static ACE_SOCK_Stream* Peer;

//Thread ID used to identify the threads
 ACE_thread_t t_id;

int open(void*){
  ACE_DEBUG((LM_DEBUG,"Acceptor: received new connection\n"));

//Register with the reactor to remember this handlle
  Reactor::instance() ->register_handler(this,ACE_Event_Handler::READ_MASK);

//Determine the peer stream and record it globally
  MyServiceHandler::Peer=&peer();
 
  //Spawn new thread to send string every second
  ACE_Thread::spawn((ACE_THR_FUNC)send_data,0,THR_NEW_LWP,&t_id);
 
  //keep the service handler registered by returning 0 to the reactor
  return 0;
  }
 

static void* send_data(void*){
  while(1){
   ACE_DEBUG((LM_DEBUG,"Hello World\n"));
   Peer->send_n("Hello World",sizeof("Hello World"));
 
   //Go to sleep for a second before sending again
   ACE_OS::sleep(1);
    }
  return 0;
  }
 

int handle_input(ACE_HANDLE){
  char* data= new char[12];
 
  //Check if peer aborted the connection
  if(Peer.recv_n(data,12)==0){
   ACE_DEBUG((LM_DEBUG,"Peer probably aborted connection\n"));
   ACE_Thread::cancel(t_id); //kill sending thread ..
   return -1; //de-register from the Reactor.
   }

  //Show what you got..
  ACE_DEBUG((LM_DEBUG,"<< %s\n",data));
 
  //keep yourself registered
  return 0;
        }
};

//Global stream identifier used by both threads
ACE_SOCK_Stream * MyServiceHandler::Peer=0;
 

void main_accept(){
 ACE_INET_Addr addr(PORT_NO);
 Acceptor myacceptor(addr,Reactor::instance());
 while(1)
  Reactor::instance()->handle_events();

 return 0;
 }

void main_connect(){
 ACE_INET_Addr addr(PORT_NO,HOSTNAME);
 Connector myconnector;
 myconnector.connect(my_svc_handler,addr);
 while(1)
  Reactor::instance()->handle_events();

 }
 

int main(int argc, char* argv[]){
 // Use ACE_Get_Opt to parse and obtain arguments and then call the
 // approriate function for accept or connect.
//...
}
 
 Next Example