summaryrefslogtreecommitdiff
path: root/examples/Connection/blocking/SPIPE-connector.cpp
blob: 5ec480c72a2636766ac8f696b0277883c437ae95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#if !defined (SPIPE_CONNECTOR_C)
// $Id$

#define SPIPE_CONNECTOR_C


#include "ace/SPIPE_Addr.h"
#include "ace/SPIPE_Connector.h"
#include "ace/Get_Opt.h"
#include "SPIPE-connector.h"

Peer_Handler::Peer_Handler (int iterations)
  : iterations_ (iterations)
{
}

Peer_Handler::~Peer_Handler ()
{
}

int 
Peer_Handler::open (void *)
{
  ACE_DEBUG ((LM_DEBUG, "activating %d\n", this->get_handle ()));

  // If iterations_ has not been set, read from stdin.
  if (iterations_ == 0)
    {
      this->display_menu ();
      if (ACE::register_stdin_handler (this,
				      ACE_Service_Config::reactor (),
				      ACE_Service_Config::thr_mgr ()) == -1)
	ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "register_stdin_handler"), -1);
    }
  else // If iterations_ has been set, send iterations_ buffers.
    {
      char *buffer = 
	"Oh give me a home\n"
	"Where the buffalo roam,\n"
	"And the deer and the antelope play.\n"
	"Where seldom is heard\n"
	"A discouraging word,\n"
	"And the skies are not cloudy all day.\n";
      int length = ACE_OS::strlen (buffer);
      
      while (iterations_-- > 0
	     && this->peer ().send_n (buffer, length) == length)
	continue;
    }
  return this->peer ().close ();
}

int 
Peer_Handler::handle_input (ACE_HANDLE)
{
  char buf[BUFSIZ];

  ssize_t n = ACE_OS::read (ACE_STDIN, buf, sizeof buf);

  if (n > 0)
    if (this->peer ().send (buf, n) != n)
      ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "write failed"), -1);
    else if (n == 0) /* Explicitly close the connection. */
      {
	if (this->peer ().close () == -1) 
	  ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "close"), 1);
	return -1;
      }
    else
      this->display_menu ();
  return 0;
}

int 
Peer_Handler::handle_close (ACE_HANDLE,
			    ACE_Reactor_Mask)
{
  ACE_DEBUG ((LM_DEBUG, "Shutting down\n"));
  return 0;
}

ACE_HANDLE 
Peer_Handler::get_handle (void) const
{
  return this->peer ().get_handle ();
}

void
Peer_Handler::display_menu (void)
{
  ACE_DEBUG ((LM_DEBUG, "\nplease enter input..: "));
}

IPC_Client::IPC_Client (void)
  : iterations_ (0),
    rendezvous_ ("acepipe"),
    done_handler_ (ACE_Sig_Handler_Ex (ACE_Service_Config::end_proactor_event_loop))
{
}

IPC_Client::~IPC_Client (void)
{
}

// Dynamic linking hooks.

int 
IPC_Client::init (int argc, char *argv[])
{
  if (this->parse_args (argc, argv) == -1)
    return -1;
  // Handle signals through the ACE_Reactor.
  else if (ACE_Service_Config::reactor ()->register_handler
	   (SIGINT, &this->done_handler_) == -1)
    return -1;

  ACE_DEBUG ((LM_DEBUG, "Opening %s\n", rendezvous_));

  // Connect to the peer, reusing the local addr if necessary.
  if (this->connect (new Peer_Handler (iterations_),
		     ACE_SPIPE_Addr (rendezvous_),
		     ACE_Synch_Options::defaults,
		     *((ACE_SPIPE_Addr *) &ACE_Addr::sap_any),
		     0,
		     O_RDWR | FILE_FLAG_OVERLAPPED,
		     0) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "connect"), -1);

  return 0;
}

int 
IPC_Client::fini (void)
{
  return 0;
}

int 
IPC_Client::svc (void)
{
  ACE_Service_Config::run_reactor_event_loop ();
  return 0;
}

int 
IPC_Client::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
{
  return 0;
}

int
IPC_Client::parse_args (int argc, char *argv[])
{
  ACE_LOG_MSG->open (argv[0]);

  ACE_Get_Opt get_opt (argc, argv, "ui:r:");

  for (int c; (c = get_opt ()) != -1; )
    {
      switch (c)
	{
	case 'r':
	  rendezvous_ = get_opt.optarg;
	  break;
	case 'i':
	  iterations_ = ACE_OS::atoi (get_opt.optarg);
	  break;
	case 'u':
	default:
	  ACE_ERROR_RETURN ((LM_ERROR,
			     "usage: %n -i <iterations>\n"
			     "-r <rendezvous>\n"), -1);
	  break;
	}
    }

  return 0;
}


#endif /* SPIPE_CONNECTOR */