server.cpp
来自「这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用」· C++ 代码 · 共 576 行 · 第 1/2 页
CPP
576 行
// server.cpp,v 1.13 2003/11/02 23:27:23 dhinton Exp
#include "tao/ORB.h"
#include "tao/RTCORBA/RTCORBA.h"
#include "ace/Thread_Manager.h"
#include "ace/High_Res_Timer.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_unistd.h"
static int test_try_lock_flag =
#if defined (ACE_HAS_MUTEX_TIMEOUTS) && !defined (ACE_HAS_WTHREADS)
1;
#else
// Don't test try_lock timed waits unless the underlying OS supports it
// Windows is the exception. It supports some mutex timeouts, so
// it has ACE_HAS_MUTEX_TIMEOUTS defined, but it doesn't support
// thread mutex timeouts which is what is needed for this to work.
0;
#endif /* defined (ACE_HAS_MUTEX_TIMEOUTS) && !defined (ACE_HAS_WTHREADS) */
// Parse command-line arguments.
static int
parse_args (int argc, char *argv[])
{
ACE_Get_Opt get_opts (argc, argv, "t");
int c;
while ((c = get_opts ()) != -1)
switch (c)
{
case 't':
test_try_lock_flag = 0;
break;
case '?':
default:
ACE_ERROR_RETURN ((LM_ERROR,
"usage: %s "
"-t"
"\n",
argv [0]),
-1);
}
return 0;
}
static int
check_for_nil (CORBA::Object_ptr obj, const char *msg)
{
if (CORBA::is_nil (obj))
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Object reference <%s> is nil\n",
msg),
-1);
else
return 0;
}
static int
test_mutex_simple (RTCORBA::RTORB_ptr rt_orb)
{
// Test the basic interface of the RTCORBA::Mutex This test should
// run even on platforms without thread support.
ACE_TRY_NEW_ENV
{
RTCORBA::Mutex_var my_mutex;
my_mutex = rt_orb->create_mutex (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
my_mutex->lock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
my_mutex->unlock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
my_mutex->lock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
my_mutex->unlock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
rt_orb->destroy_mutex (my_mutex.in () ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"Unexpected exception caught in test_mutex_simple()");
return -1;
}
ACE_ENDTRY;
return 0;
}
#if (TAO_HAS_NAMED_RT_MUTEXES == 1)
static int
test_named_mutex_simple (RTCORBA::RTORB_ptr rt_orb)
{
// Test the basic interface of the named RTCORBA::Mutex(es) This
// test should run even on platforms without thread support.
ACE_TRY_NEW_ENV
{
RTCORBA::Mutex_var larry_mutex1;
RTCORBA::Mutex_var moe_mutex1;
CORBA::Boolean created_flag;
larry_mutex1 = rt_orb->create_named_mutex ("larry",
created_flag ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
if (created_flag != 1)
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Expected named mutex larry to be created, but it wasn't\n"),
-1);
moe_mutex1 = rt_orb->create_named_mutex ("moe",
created_flag
ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
if (created_flag != 1)
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Expected named mutex moe to be created, but it wasn't\n"),
-1);
larry_mutex1->lock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
larry_mutex1->unlock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
// Test creating the mutex a second time
{
RTCORBA::Mutex_var larry_mutex2;
larry_mutex2 = rt_orb->create_named_mutex ("larry",
created_flag
ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
if (created_flag != 0)
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Expected named mutex to already be created, but it wasn't\n"),
-1);
// test the pointers...
if (ACE_reinterpret_cast (void *, larry_mutex1.in ())
!= ACE_reinterpret_cast (void *, larry_mutex2.in ()))
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Should have gotten the same mutex, but didn't\n"),
-1);
larry_mutex2->lock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
larry_mutex2->unlock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
}
// test opening the mutex
{
RTCORBA::Mutex_var larry_mutex3;
larry_mutex3 = rt_orb->open_named_mutex ("larry"
ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
// test the pointers...
if (ACE_reinterpret_cast (void *,larry_mutex1.in ())
!= ACE_reinterpret_cast (void *,larry_mutex3.in ()))
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Should have gotten the same mutex, but didn't\n"),
-1);
larry_mutex3->lock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
larry_mutex3->unlock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
}
// Make sure that nothing has been broken behind the scenes.
larry_mutex1->lock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
larry_mutex1->unlock (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
rt_orb->destroy_mutex (larry_mutex1.in () ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
rt_orb->destroy_mutex (moe_mutex1.in () ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"Unexpected exception caught in test_named_mutex_simple()");
return -1;
}
ACE_ENDTRY;
return 0;
}
static int
test_named_mutex_exception (RTCORBA::RTORB_ptr rt_orb)
{
// Test that open_named_mutex returns an exception when the mutex
// name isn't found.
// This test should run even on platforms without thread support.
ACE_TRY_NEW_ENV
{
RTCORBA::Mutex_var larry_mutex1;
larry_mutex1 = rt_orb->open_named_mutex ("larry" ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
ACE_ERROR_RETURN ((LM_ERROR,
"Expected a MutexNotFound exception, but didn't get one.\n"),
-1);
}
ACE_CATCH (RTCORBA::RTORB::MutexNotFound, ex)
{
ACE_DEBUG ((LM_DEBUG, "Caught expected MutexNotFound exception.\n"));
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"Unexpected exception caught in test_named_mutex_exception()");
return -1;
}
ACE_ENDTRY;
return 0;
}
#endif /* TAO_HAS_NAMED_RT_MUTEXES == 1 */
#if defined (ACE_HAS_THREADS)
const size_t MAX_ITERATIONS=10;
const size_t MAX_THREADS=4;
struct Mutex_Test_Data
{
RTCORBA::Mutex_ptr mutex;
int *shared_var;
int *error_flag;
};
static void *
mutex_test_thread (void *args)
{
Mutex_Test_Data *data = ACE_reinterpret_cast (Mutex_Test_Data *, args);
RTCORBA::Mutex_ptr mutex = data->mutex;
int *shared_var = data->shared_var;
ACE_OS::srand (ACE_OS::time (0));
ACE_TRY_NEW_ENV
{
for (size_t i = 0; i < MAX_ITERATIONS / 2; i++)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%P|%t) = trying to lock on iteration %d\n"),
i));
mutex->lock ();
ACE_TRY_CHECK;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%P|%t) = locked on iteration %d\n"),
i));
// Check if the shared var is a value it shouldn't be when
// we're under the lock.
if (*shared_var != 0)
{
ACE_ERROR ((LM_ERROR,
"Expected shared_var to be 0 under the mutex\n"));
*data->error_flag = 1;
}
*shared_var = 1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?