int
Peer_Handler::transmit_stdin (void)
{
// If return value is -1, then first_time_ must be reset to 1.
int result = 0;
if (this->connection_id_ != -1)
{
ACE_Message_Block *mb = 0;
ACE_NEW_RETURN (mb,
ACE_Message_Block (sizeof (Event)),
-1);
// Cast the message block payload into an <Event> pointer.
Event *event = (Event *) mb->rd_ptr ();
ssize_t n = ACE_OS::read (ACE_STDIN,
event->data_,
sizeof event->data_);
switch (n)
{
case 0:
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("stdin closing down\n")));
// Take stdin out of the ACE_Reactor so we stop trying to
// send events.
ACE_Reactor::instance ()->remove_handler
(ACE_STDIN,
ACE_Event_Handler::DONT_CALL | ACE_Event_Handler::READ_MASK);
mb->release ();
result = 0; //
break;
/* NOTREACHED */
case -1:
mb->release ();
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("read")));
result = 0; //
break;
/* NOTREACHED */
default:
// Do not return directly, save the return value.
result = this->transmit (mb, n, ROUTING_EVENT);
break;
/* NOTREACHED */
}
// Do not return at here, but at exit of function.
/*return 0;*/
}
else
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Must transmit over an opened channel.\n")));
result = -1; // Save return value at here, return at exit of function.
}
// If transmit error, the stdin-thread will be cancelled, so should
// reset first_time_ to 1, which will register_stdin_handler again.
if (result == -1)
first_time_ = 1;
return result;
}
开发者ID:asdlei00,项目名称:ACE,代码行数:65,代码来源:Peer.cpp
示例2: defined
template <class HANDLER> void
ACE_Asynch_Connector<HANDLER>::parse_address (const ACE_Asynch_Connect::Result &result,
ACE_INET_Addr &remote_address,
ACE_INET_Addr &local_address)
{
#if defined (ACE_HAS_IPV6)
// Getting the addresses.
sockaddr_in6 local_addr;
sockaddr_in6 remote_addr;
#else
// Getting the addresses.
sockaddr_in local_addr;
sockaddr_in remote_addr;
#endif /* ACE_HAS_IPV6 */
// Get the length.
int local_size = sizeof (local_addr);
int remote_size = sizeof (remote_addr);
// Get the local address.
if (ACE_OS::getsockname (result.connect_handle (),
reinterpret_cast<sockaddr *> (&local_addr),
&local_size) < 0)
ACE_ERROR ((LM_ERROR,
ACE_TEXT("%p\n"),
ACE_TEXT("ACE_Asynch_Connector::<getsockname> failed")));
// Get the remote address.
if (ACE_OS::getpeername (result.connect_handle (),
reinterpret_cast<sockaddr *> (&remote_addr),
&remote_size) < 0)
ACE_ERROR ((LM_ERROR,
ACE_TEXT("%p\n"),
ACE_TEXT("ACE_Asynch_Connector::<getpeername> failed")));
// Set the addresses.
local_address.set (reinterpret_cast<sockaddr_in *> (&local_addr),
local_size);
remote_address.set (reinterpret_cast<sockaddr_in *> (&remote_addr),
remote_size);
#if 0
// @@ Just debugging.
char local_address_buf [BUFSIZ];
char remote_address_buf [BUFSIZ];
if (local_address.addr_to_string (local_address_buf,
sizeof local_address_buf) == -1)
ACE_ERROR ((LM_ERROR,
"Error:%m:can't obtain local_address's address string"));
ACE_DEBUG ((LM_DEBUG,
"ACE_Asynch_Connector<HANDLER>::parse_address : "
"Local address %s\n",
local_address_buf));
if (remote_address.addr_to_string (remote_address_buf,
sizeof remote_address_buf) == -1)
ACE_ERROR ((LM_ERROR,
"Error:%m:can't obtain remote_address's address string"));
ACE_DEBUG ((LM_DEBUG,
"ACE_Asynch_Connector<HANDLER>::parse_address : "
"Remote address %s\n",
remote_address_buf));
#endif /* 0 */
return;
}
static bool
timeout_test (void)
{
bool status = true;
SYNCH_QUEUE mq;
MyTask task1;
task1.create_reactor ();
task1.start (1);
TestHandler test_handler (task1.get_reactor (), mq);
// The reactor of taks1 that uses a hrtimer will trigger a timeout in
// 5 seconds which will enqueue a message block in the queue. At the
// same moment we calculate a timeout for the dequeue operation for
// 3 seconds in the future. Than we set the system time 4 seconds back.
// The condition should timeout because the queue is empty and the trigger
// only fires after the condition has timed out.
// Next we start another dequeue for 3 seconds in the future which should
// return before timing out because by then the trigger should have fired.
// In case of using regular system time policy for message queue and
// dequeue timeouts the first dequeue would not have timed out because
// between calculating the timeout and starting the dequeue the system time
// shifted back 4 sec causing the trigger to fire before the timeout elapsed.
// In case timeshifting does not work because of priority problems or such
// the test should succeed.
if (!test_handler.trigger_in (ACE_Time_Value (5, 0)))
ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) Unable to schedule trigger.\n"),
false);
if (!mq.is_empty ())
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("New queue is not empty!\n")));
status = false;
}
else
{
ACE_Message_Block *b;
ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv;
tv = (tv.now () + ACE_Time_Value (3,0)); // Now (monotonic time) + 3 sec
// shift back in time 4 sec
set_system_time (ACE_OS::gettimeofday () - ACE_Time_Value (4, 0));
if (mq.dequeue_head (b, &tv) != -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Dequeued before timeout elapsed!\n")));
status = false;
}
else if (errno != EWOULDBLOCK)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Dequeue timeout should be EWOULDBLOCK, got")));
status = false;
}
else
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("First dequeue timed out: OK\n")));
tv = (tv.now () + ACE_Time_Value (3,0)); // Now (monotonic time) + 3 sec
if (mq.dequeue_head (b, &tv) != -1)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Second dequeue succeeded: OK\n")));
delete b;
}
else
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Second dequeue timed out!\n")));
status = false;
}
}
// restore time
set_system_time (ACE_OS::gettimeofday () + ACE_Time_Value (4, 0));
}
ACE_DEBUG((LM_INFO,
"(%P|%t) Asking worker thread to finish.\n"));
task1.stop ();
ACE_Thread_Manager::instance ()->wait ();
return status;
}
void
Worker::setup (void)
{
// Make sure we have a connection to the server using the test
// protocol.
this->policy_manager_->set_policy_overrides (this->test_protocol_policy_,
CORBA::SET_OVERRIDE);
// Since the network maybe unavailable temporarily, make sure to try
// for a few times before giving up.
for (CORBA::ULong j = 0;;)
{
try
{
// Send a message to ensure that the connection is setup.
this->test_->oneway_sync ();
break;
}
catch (const CORBA::TRANSIENT &)
{
++j;
if (j < this->number_of_connection_attempts_)
{
ACE_OS::sleep (1);
continue;
}
}
ACE_ERROR ((LM_ERROR,
"Cannot setup test protocol\n"));
ACE_OS::exit (-1);
}
const char *test_protocol = 0;
if (this->test_protocol_tag_ == IOP::TAG_INTERNET_IOP)
test_protocol = "IIOP";
else if (this->test_protocol_tag_ == TAO_TAG_DIOP_PROFILE)
test_protocol = "DIOP";
else if (this->test_protocol_tag_ == TAO_TAG_SCIOP_PROFILE)
test_protocol = "SCIOP";
// Use IIOP for setting up the test since the test protocol maybe
// unreliable.
this->policy_manager_->set_policy_overrides (this->base_protocol_policy_,
CORBA::SET_OVERRIDE);
// Since the network maybe unavailable temporarily, make sure to try
// for a few times before giving up.
for (CORBA::ULong k = 0;;)
{
try
{
// Let the server know what to expect..
this->test_->start_test (this->session_id_,
test_protocol,
this->invocation_rate_,
this->message_size_,
this->iterations_);
break;
}
catch (const CORBA::TRANSIENT &)
{
ACE_OS::sleep (1);
if (k < this->number_of_connection_attempts_)
{
ACE_OS::sleep (1);
continue;
}
}
ACE_ERROR ((LM_ERROR,
"Cannot setup base protocol\n"));
ACE_OS::exit (-1);
}
return;
}
开发者ID:CCJY,项目名称:ATCD,代码行数:85,代码来源:Sender_exec.cpp
示例5: ACE_ERROR
int
ClientApp::run (int argc, ACE_TCHAR* argv[])
{
CORBA::ORB_var orb
= CORBA::ORB_init (argc, argv);
// Parse the command-line args for this application.
// * Raises -1 if problems are encountered.
// * Returns 1 if the usage statement was explicitly requested.
// * Returns 0 otherwise.
int result = this->parse_args (argc, argv);
if (result != 0)
{
return result;
}
CORBA::Object_var obj
= orb->string_to_object(this->ior_.c_str());
if (CORBA::is_nil(obj.in()))
{
ACE_ERROR((LM_ERROR,
"(%P|%t) Failed to convert IOR string to obj ref.\n"));
throw TestException();
}
Foo_var foo = Foo::_narrow(obj.in());
if (CORBA::is_nil(foo.in()))
{
ACE_ERROR((LM_ERROR,
"(%P|%t) Failed to narrow obj ref to Foo interface.\n"));
throw TestException();
}
// Create the callback object using the child poa with the custom
// strategy.
obj = orb->resolve_initial_references("RootPOA");
if (CORBA::is_nil(obj.in()))
{
ACE_ERROR((LM_ERROR,
"(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n"));
throw TestException();
}
PortableServer::POA_var root_poa
= PortableServer::POA::_narrow(obj.in());
if (CORBA::is_nil(root_poa.in()))
{
ACE_ERROR((LM_ERROR,
"(%P|%t) Failed to narrow obj ref to POA interface.\n"));
throw TestException();
}
PortableServer::POAManager_var poa_manager
= root_poa->the_POAManager();
// Create the child POA.
CORBA::PolicyList policies(0);
policies.length(0);
PortableServer::POA_var child_poa
= root_poa->create_POA("ChildPoa",
poa_manager.in(),
policies);
if (CORBA::is_nil(child_poa.in()))
{
ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
"Failed to create the child POA.\n"));
throw TestException();
}
// Create the thread pool servant dispatching strategy object, and
// hold it in a (local) smart pointer variable.
TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_tp_strategy =
new TAO::CSD::TP_Strategy();
csd_tp_strategy->set_num_threads(1);
// Tell the strategy to apply itself to the child poa.
if (csd_tp_strategy->apply_to(child_poa.in()) == false)
{
ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
"Failed to apply custom dispatching strategy to child poa.\n"));
throw TestException();
}
// Create the servant object.
Callback_i* servant = new Callback_i ();
// local smart pointer variable to deal with releasing the reference
// to the servant object when the smart pointer object falls out of scope.
PortableServer::ServantBase_var owner_transfer(servant);
// Activate the servant using the Child POA.
PortableServer::ObjectId_var oid
= child_poa->activate_object(servant);
//.........这里部分代码省略.........
void
Receiver::open (ACE_HANDLE handle,
ACE_Message_Block &message_block)
{
ACE_DEBUG ((LM_DEBUG,
"%N:%l:Receiver::open called\n"));
// New connection, so initiate stuff.
// Cache the new connection
this->handle_ = handle;
// File offset starts at zero
this->file_offset_ = 0;
// Open dump file (in OVERLAPPED mode)
this->dump_file_ = ACE_OS::open (dump_file,
O_CREAT | O_RDWR | O_TRUNC | \
FILE_FLAG_OVERLAPPED);
if (this->dump_file_ == ACE_INVALID_HANDLE)
{
ACE_ERROR ((LM_ERROR,
"%p\n",
"ACE_OS::open"));
return;
}
// Initiate <ACE_Asynch_Write_File>.
if (this->wf_.open (*this,
this->dump_file_) == -1)
{
ACE_ERROR ((LM_ERROR,
"%p\n",
"ACE_Asynch_Write_File::open"));
return;
}
// Initiate <ACE_Asynch_Read_Stream>.
if (this->rs_.open (*this, this->handle_) == -1)
{
ACE_ERROR ((LM_ERROR,
"%p\n",
"ACE_Asynch_Read_Stream::open"));
return;
}
// Fake the result and make the <handle_read_stream> get
// called. But, not, if there is '0' is transferred.
if (message_block.length () != 0)
{
// Duplicate the message block so that we can keep it around.
ACE_Message_Block &duplicate =
*message_block.duplicate ();
// Fake the result so that we will get called back.
ACE_Asynch_Read_Stream_Result_Impl *fake_result =
ACE_Proactor::instance ()->create_asynch_read_stream_result (this->proxy (),
this->handle_,
duplicate,
initial_read_size,
0,
ACE_INVALID_HANDLE,
0,
0);
size_t bytes_transferred = message_block.length ();
// <complete> for Accept would have already moved the <wr_ptr>
// forward. Update it to the beginning position.
duplicate.wr_ptr (duplicate.wr_ptr () - bytes_transferred);
// This will call the callback.
fake_result->complete (message_block.length (),
1,
0);
// Zap the fake result.
delete fake_result;
}
else
// Otherwise, make sure we proceed. Initiate reading the socket
// stream.
if (this->initiate_read_stream () == -1)
return;
}
//.........这里部分代码省略.........
case '\xb2':
case '\xb3':
case '\xb4':
case '\xb5':
case '\xb6':
case '\xb7':
case '\xb8':
case '\xb9':
case '\xba':
case '\xbb':
case '\xbc':
case '\xbd':
case '\xbe':
case '\xbf':
case '\xc0':
case '\xc1':
case '\xc2':
case '\xc3':
case '\xc4':
case '\xc5':
case '\xc6':
case '\xc7':
case '\xc8':
case '\xc9':
case '\xca':
case '\xcb':
case '\xcc':
case '\xcd':
case '\xce':
case '\xcf':
case '\xd0':
case '\xd1':
case '\xd2':
case '\xd3':
case '\xd4':
case '\xd5':
case '\xd6':
case '\xd7':
case '\xd8':
case '\xd9':
case '\xda':
case '\xdb':
case '\xdc':
case '\xdd':
case '\xde':
case '\xdf':
case '\xe0':
case '\xe1':
case '\xe2':
case '\xe3':
case '\xe4':
case '\xe5':
case '\xe6':
case '\xe7':
case '\xe8':
case '\xe9':
case '\xea':
case '\xeb':
case '\xec':
case '\xed':
case '\xee':
case '\xef':
case '\xf0':
case '\xf1':
case '\xf2':
case '\xf3':
case '\xf4':
case '\xf5':
case '\xf6':
case '\xf7':
case '\xf8':
case '\xf9':
case '\xfa':
case '\xfb':
case '\xfc':
case '\xfd':
case '\xfe':
case '\xff':
{
// Test works
retval = 0;
}
break;
}
if (retval != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("ERROR: Switch doesn't worked as expected\n")));
}
else
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("Switch worked as expected\n")));
}
ACE_END_TEST;
return retval;
}
int
Logging_Handler::handle_input (ACE_HANDLE)
{
ACE_Log_Record log_record;
// We need to use the old two-read trick here since TCP sockets
// don't support framing natively. Allocate a message block for the
// payload; initially at least large enough to hold the header, but
// needs some room for alignment.
ACE_Message_Block *payload_p = 0;
ACE_Message_Block *header_p = 0;
ACE_NEW_RETURN (header_p,
ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE),
-1);
auto_ptr <ACE_Message_Block> header (header_p);
// Align the Message Block for a CDR stream
ACE_CDR::mb_align (header.get ());
ACE_CDR::Boolean byte_order;
ACE_CDR::ULong length;
ssize_t count = ACE::recv_n (this->peer ().get_handle (),
header->wr_ptr (),
8);
switch (count)
{
// Handle shutdown and error cases.
default:
case -1:
case 0:
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("server logging daemon closing down\n")));
return -1;
/* NOTREACHED */
case 8:
// Just fall through in this case..
break;
}
header->wr_ptr (8); // Reflect addition of 8 bytes.
// Create a CDR stream to parse the 8-byte header.
ACE_InputCDR header_cdr (header.get ());
// Extract the byte-order and use helper methods to disambiguate
// octet, booleans, and chars.
header_cdr >> ACE_InputCDR::to_boolean (byte_order);
// Set the byte-order on the stream...
header_cdr.reset_byte_order (byte_order);
// Extract the length
header_cdr >> length;
ACE_NEW_RETURN (payload_p,
ACE_Message_Block (length),
-1);
auto_ptr <ACE_Message_Block> payload (payload_p);
// Ensure there's sufficient room for log record payload.
ACE_CDR::grow (payload.get (), 8 + ACE_CDR::MAX_ALIGNMENT + length);
// Use <recv_n> to obtain the contents.
if (ACE::recv_n (this->peer ().get_handle (),
payload->wr_ptr (),
length) <= 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("recv_n()")));
return -1;
}
payload->wr_ptr (length); // Reflect additional bytes
ACE_InputCDR payload_cdr (payload.get ());
payload_cdr.reset_byte_order (byte_order);
payload_cdr >> log_record; // Finally extract the <ACE_log_record>.
log_record.length (length);
log_record.print (ACE_TEXT_CHAR_TO_TCHAR (this->peer_name_), 1, stderr);
return 0;
}
int
run_main (int , ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Monotonic_Task_Test"));
int status = 0;
# if defined (ACE_HAS_THREADS)
MyTask my_task;
if (my_task.start () == 0)
{
// shift back in time 4 sec; this would mess up timeouts if
// monotonic timer was not used
ACE_Time_Value tv_shift (4, 0);
set_system_time (ACE_OS::gettimeofday () - tv_shift);
if (my_task.put_message () == 0)
{
// task should now have finished dequeueing and started waiting for stop signal
// wait (2sec) on thread manager should timeout
// use the time policy aware gettimeofday()
// method of the task to get current time
ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv (my_task.gettimeofday ());
tv += ACE_Time_Value (2, 0);
// shift another 3 sec back in time; without monotonic timer support in
// thread manager this would mess up the timed wait
tv_shift += ACE_Time_Value (3, 0);
set_system_time (ACE_OS::gettimeofday () - ACE_Time_Value (3,0));
if (my_task.thr_mgr ()->wait (&tv) == 0)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Thread manager did not time out\n")));
status = 1;
}
else
{
ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv_now (my_task.gettimeofday ());
ACE_DEBUG ((LM_INFO, ACE_TEXT ("Thread manager timed out at %#T\n"), &tv_now));
}
}
else
status = 1;
// ok, now stop task
if (my_task.stop () != 0)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to stop task\n")));
status = 1;
}
// restore time
set_system_time (ACE_OS::gettimeofday () + tv_shift);
}
else
status = 1;
# endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return status;
}
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT("Compiler_Features_13_Test"));
// As usual, the exit status from the test is 0 on success, 1 on
// failure
int status = 0;
{
// Make sure const cast works. Compilation is interesting, the
// functionality test here is just to make sure the compiler does
// not optimize things away ...
int x = 5;
int const & y = x;
const_cast<int&>(y) = 3;
if (x != 3)
{
status = 1;
ACE_ERROR((LM_ERROR,
ACE_TEXT("Wrong value after const_cast,")
ACE_TEXT(" expected %d, got %d\n"),
3, x));
}
}
// Make sure dynamic cast through pointers work ...
Derived d;
d.value = 24;
Base * b1 = &d;
Derived * d1 = dynamic_cast<Derived*>(b1);
if (d1 == 0)
{
status = 1;
ACE_ERROR((LM_ERROR,
ACE_TEXT("dynamic_cast returns null, expected value\n")));
}
d1->value = 42;
if (d.value != 42)
{
ACE_ERROR((LM_ERROR,
ACE_TEXT("Wrong value after dynamic_cast, expected %d, got %d\n"),
42, d.value));
}
// Make sure dynamic cast detects invalid casts
Another a;
Base * b2 = &a;
Derived * d2 = dynamic_cast<Derived*>(b2);
if (d2 != 0)
{
status = 1;
ACE_ERROR((LM_ERROR,
ACE_TEXT("dynamic_cast should return null\n")));
}
// Make sure dynamic cast raises an exception
Base & b3 = a;
try
{
(void) dynamic_cast<Derived&>(b3);
status = 1;
ACE_ERROR((LM_ERROR,
ACE_TEXT("dynamic_cast should have raised exception\n")));
}
catch(std::exception const &)
{
}
catch(...)
{
status = 1;
ACE_ERROR((LM_ERROR,
ACE_TEXT("dynamic_cast should have raised std::exception\n")));
}
{
// Just test these compile ...
double x = 42.0;
int y = static_cast<int>(x);
void * z = reinterpret_cast<void*>(y);
if (z == 0)
{
ACE_ERROR((LM_ERROR,
ACE_TEXT("My hack to make sure the code is not ")
ACE_TEXT("optimized away backfired!\n")));
}
}
ACE_END_TEST;
return status;
}
请发表评论