Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
c7d5572c
Commit
c7d5572c
authored
May 04, 2015
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TCPCheck: добавил функцию ping (пока реализация через system).
parent
e5096e35
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
6 deletions
+67
-6
TCPCheck.h
include/TCPCheck.h
+16
-4
TCPCheck.cc
src/Communications/TCP/TCPCheck.cc
+37
-1
test_tcpcheck.cc
tests/test_tcpcheck.cc
+14
-1
No files found.
include/TCPCheck.h
View file @
c7d5572c
...
@@ -3,14 +3,12 @@
...
@@ -3,14 +3,12 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
#include <atomic>
#include <atomic>
#include <cc++/socket.h>
#include <cc++/socket.h>
#include "Mutex.h"
#include "ThreadCreator.h"
#include "ThreadCreator.h"
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
/*! Вспомогательный класс для проверки связи, реализованный через создание потока,
/*! Вспомогательный класс для проверки связи, реализованный через создание потока,
чтобы при проверке не было "зависания" при недоступности адреса.
чтобы при проверке не было "зависания" при недоступности адреса.
Смысл: создаётся поток, в нём происходит проверка, а в вызвавший поток приостанавливается
Смысл: создаётся поток, в нём происходит проверка, а в вызвавший поток приостанавливается
на время timeout, по истечении которого созданный поток "принудительно"(в любом случае)
на время timeout, по истечении которого созданный поток "принудительно"(в любом случае!) уничтожается..
уничтожается..
*/
*/
class
TCPCheck
class
TCPCheck
{
{
...
@@ -18,19 +16,31 @@ class TCPCheck
...
@@ -18,19 +16,31 @@ class TCPCheck
TCPCheck
();
TCPCheck
();
~
TCPCheck
();
~
TCPCheck
();
/*! Проверка связи с
узлом
/*! Проверка связи с
сервисом на определённом порту
* \param _ip - ip проверяемого узла
* \param _ip - ip проверяемого узла
* \param _port - порт для проверяемого узла
* \param _port - порт для проверяемого узла
* \param tout - таймаут на попытку
* \param tout - таймаут на попытку
* \param sleep_msec - пауза между проверками результата
* \param sleep_msec - пауза между проверками результата
*
* Для проверки идёт попытка открыть соединение, но данные не посылаются, а соединение сразу закрывается.
* \note Нужно быть уверенным, что сервис не зависнет от таких попыток "соединений"
*/
*/
bool
check
(
const
std
::
string
&
_ip
,
int
_port
,
timeout_t
tout
,
timeout_t
sleep_msec
=
50
);
bool
check
(
const
std
::
string
&
_ip
,
int
_port
,
timeout_t
tout
,
timeout_t
sleep_msec
=
50
);
/*! \param iaddr - 'ip:port' */
/*! \param iaddr - 'ip:port' */
bool
check
(
const
std
::
string
&
iaddr
,
timeout_t
tout
,
timeout_t
sleep_msec
=
50
);
bool
check
(
const
std
::
string
&
iaddr
,
timeout_t
tout
,
timeout_t
sleep_msec
=
50
);
/*! Проверка связи с узлом командой ping
* \note Вызывается через system()! Это может быть опасно с точки зрения безопасности..
* \todo Возможно стоит написать свою реализацию ping
*/
bool
ping
(
const
std
::
string
&
_ip
,
timeout_t
tout
=
1000
,
timeout_t
sleep_msec
=
200
,
const
std
::
string
&
ping_argc
=
"-c 1 -w 0.1 -q -n"
);
protected
:
protected
:
void
check_thread
();
void
check_thread
();
void
ping_thread
();
void
setResult
(
bool
s
)
void
setResult
(
bool
s
)
{
{
...
@@ -41,6 +51,8 @@ class TCPCheck
...
@@ -41,6 +51,8 @@ class TCPCheck
std
::
string
ip
=
{
""
};
std
::
string
ip
=
{
""
};
int
port
=
{
0
};
int
port
=
{
0
};
int
tout_msec
;
int
tout_msec
;
std
::
string
ping_args
=
{
"-c 1 -w 0.1 -q -n"
};
};
};
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
#endif // _TCPCheck_H_
#endif // _TCPCheck_H_
...
...
src/Communications/TCP/TCPCheck.cc
View file @
c7d5572c
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
#include <sstream>
#include <sstream>
#include <cstdlib>
#include "UniSetTypes.h"
#include "UniSetTypes.h"
#include "PassiveTimer.h"
#include "PassiveTimer.h"
#include "TCPCheck.h"
#include "TCPCheck.h"
...
@@ -53,7 +54,6 @@ bool TCPCheck::check( const std::string& _ip, int _port, timeout_t tout, timeout
...
@@ -53,7 +54,6 @@ bool TCPCheck::check( const std::string& _ip, int _port, timeout_t tout, timeout
void
TCPCheck
::
check_thread
()
void
TCPCheck
::
check_thread
()
{
{
setResult
(
false
);
setResult
(
false
);
try
try
{
{
ost
::
Thread
::
setException
(
ost
::
Thread
::
throwException
);
ost
::
Thread
::
setException
(
ost
::
Thread
::
throwException
);
...
@@ -65,3 +65,39 @@ void TCPCheck::check_thread()
...
@@ -65,3 +65,39 @@ void TCPCheck::check_thread()
catch
(
ost
::
Exception
&
e
)
{}
catch
(
ost
::
Exception
&
e
)
{}
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
bool
TCPCheck
::
ping
(
const
std
::
string
&
_ip
,
timeout_t
tout
,
timeout_t
sleep_msec
,
const
std
::
string
&
_ping_args
)
{
ip
=
_ip
;
tout_msec
=
tout
;
ping_args
=
_ping_args
;
setResult
(
false
);
ThreadCreator
<
TCPCheck
>
t
(
this
,
&
TCPCheck
::
ping_thread
);
t
.
setCancel
(
ost
::
Thread
::
cancelDeferred
);
t
.
start
();
PassiveTimer
pt
(
tout
);
while
(
!
pt
.
checkTime
()
&&
t
.
isRunning
()
)
msleep
(
sleep_msec
);
if
(
t
.
isRunning
()
)
// !getResult() )
t
.
stop
();
return
result
;
}
// -----------------------------------------------------------------------------
void
TCPCheck
::
ping_thread
()
{
setResult
(
false
);
ostringstream
cmd
;
cmd
<<
"ping "
<<
ping_args
<<
" "
<<
ip
<<
" 2>/dev/null 1>/dev/null"
;
int
ret
=
system
(
cmd
.
str
().
c_str
());
int
res
=
WEXITSTATUS
(
ret
);
setResult
((
res
==
0
));
}
// -----------------------------------------------------------------------------
tests/test_tcpcheck.cc
View file @
c7d5572c
...
@@ -26,7 +26,7 @@ bool run_test_server()
...
@@ -26,7 +26,7 @@ bool run_test_server()
return
true
;
return
true
;
}
}
// --------------------------------------------------------
// --------------------------------------------------------
TEST_CASE
(
"TCPCheck
"
,
"[tcp
check]"
)
TEST_CASE
(
"TCPCheck
::check"
,
"[tcpcheck][tcpcheck_
check]"
)
{
{
TCPCheck
t
;
TCPCheck
t
;
...
@@ -45,3 +45,16 @@ TEST_CASE("TCPCheck", "[tcpcheck]" )
...
@@ -45,3 +45,16 @@ TEST_CASE("TCPCheck", "[tcpcheck]" )
CHECK
(
res
.
get
()
);
CHECK
(
res
.
get
()
);
}
}
// --------------------------------------------------------
// --------------------------------------------------------
TEST_CASE
(
"TCPCheck::ping"
,
"[tcpcheck][tcpcheck_ping]"
)
{
TCPCheck
t
;
auto
res
=
std
::
async
(
std
::
launch
::
async
,
run_test_server
);
CHECK
(
t
.
ping
(
host
)
);
CHECK_FALSE
(
t
.
ping
(
"dummy_host_name"
)
);
cancel
=
true
;
CHECK
(
res
.
get
()
);
}
// --------------------------------------------------------
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment