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
20b3449d
Commit
20b3449d
authored
Nov 23, 2016
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(TCPCheck): переделал класс на использование condition
parent
d5b8a3d2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
61 deletions
+56
-61
TCPCheck.h
include/TCPCheck.h
+6
-8
TCPCheck.cc
src/Communications/TCP/TCPCheck.cc
+50
-53
No files found.
include/TCPCheck.h
View file @
20b3449d
...
@@ -38,32 +38,30 @@ class TCPCheck
...
@@ -38,32 +38,30 @@ class TCPCheck
* \param _ip - ip проверяемого узла
* \param _ip - ip проверяемого узла
* \param _port - порт для проверяемого узла
* \param _port - порт для проверяемого узла
* \param tout - таймаут на попытку
* \param tout - таймаут на попытку
* \param sleep_msec - пауза между проверками результата
*
*
* Для проверки идёт попытка открыть соединение, но данные не посылаются, а соединение сразу закрывается.
* Для проверки идёт попытка открыть соединение, но данные не посылаются, а соединение сразу закрывается.
* \note Нужно быть уверенным, что сервис не зависнет от таких попыток "соединений"
* \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
);
/*! \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
);
/*! Проверка связи с узлом командой ping
/*! Проверка связи с узлом командой ping
* \note Вызывается через system()! Это может быть опасно с точки зрения безопасности..
* \note Вызывается через system()! Это может быть опасно с точки зрения безопасности..
* \todo Возможно стоит написать свою реализацию ping
* \todo Возможно стоит написать свою реализацию ping
*/
*/
bool
ping
(
const
std
::
string
&
_ip
,
timeout_t
tout
=
1000
,
timeout_t
sleep_msec
=
2
00
,
const
std
::
string
&
ping_argc
=
"-c 1 -w 0.1 -q -n"
);
bool
ping
(
const
std
::
string
&
_ip
,
timeout_t
tout
=
10
00
,
const
std
::
string
&
ping_argc
=
"-c 1 -w 0.1 -q -n"
);
protected
:
protected
:
void
check_thread
();
void
check_thread
();
void
ping_thread
();
void
ping_thread
();
void
setResult
(
bool
s
)
std
::
condition_variable
thr_event
;
{
std
::
mutex
thr_mutex
;
result
=
s
;
std
::
atomic_bool
thr_finished
=
{
false
};
}
std
::
atomic_bool
result
=
{
false
};
std
::
atomic_bool
result
=
{
false
};
std
::
string
ip
=
{
""
};
std
::
string
ip
=
{
""
};
...
...
src/Communications/TCP/TCPCheck.cc
View file @
20b3449d
...
@@ -18,7 +18,6 @@
...
@@ -18,7 +18,6 @@
#include <sstream>
#include <sstream>
#include <cstdlib>
#include <cstdlib>
#include "UniSetTypes.h"
#include "UniSetTypes.h"
#include "PassiveTimer.h"
#include "ThreadCreator.h"
#include "ThreadCreator.h"
#include "TCPCheck.h"
#include "TCPCheck.h"
#include "UTCPStream.h"
#include "UTCPStream.h"
...
@@ -38,102 +37,99 @@ TCPCheck::~TCPCheck() noexcept
...
@@ -38,102 +37,99 @@ TCPCheck::~TCPCheck() noexcept
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
bool
TCPCheck
::
check
(
const
std
::
string
&
_iaddr
,
timeout_t
tout
,
timeout_t
sleep_msec
)
bool
TCPCheck
::
check
(
const
std
::
string
&
_iaddr
,
timeout_t
tout
)
{
{
auto
v
=
uniset
::
explode_str
(
_iaddr
,
':'
);
auto
v
=
uniset
::
explode_str
(
_iaddr
,
':'
);
if
(
v
.
size
()
<
2
)
if
(
v
.
size
()
<
2
)
return
false
;
return
false
;
return
check
(
v
[
0
],
uniset
::
uni_atoi
(
v
[
1
]),
tout
,
sleep_msec
);
return
check
(
v
[
0
],
uniset
::
uni_atoi
(
v
[
1
]),
tout
);
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
bool
TCPCheck
::
check
(
const
std
::
string
&
_ip
,
int
_port
,
timeout_t
tout
,
timeout_t
sleep_msec
)
template
<
typename
T
>
class
TGuard
{
{
ip
=
_ip
;
public
:
port
=
_port
;
tout_msec
=
tout
;
setResult
(
false
);
ThreadCreator
<
TCPCheck
>
t
(
this
,
&
TCPCheck
::
check_thread
);
TGuard
(
T
*
m
,
typename
ThreadCreator
<
T
>::
Action
a
)
:
// t.setCancel(ost::Thread::cancelDeferred);
t
(
m
,
a
)
{
t
.
start
();
t
.
start
();
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
}
~
TGuard
()
{
if
(
t
.
isRunning
()
)
t
.
stop
();
}
PassiveTimer
pt
(
tout
);
protected
:
ThreadCreator
<
T
>
t
;
};
// -----------------------------------------------------------------------------
bool
TCPCheck
::
check
(
const
std
::
string
&
_ip
,
int
_port
,
timeout_t
tout
)
{
ip
=
_ip
;
port
=
_port
;
tout_msec
=
tout
;
thr_finished
=
false
;
result
=
false
;
while
(
!
pt
.
checkTime
()
&&
t
.
isRunning
()
)
TGuard
<
TCPCheck
>
t
(
this
,
&
TCPCheck
::
check_thread
);
msleep
(
sleep_msec
);
if
(
t
.
isRunning
()
)
// !getResult() )
std
::
unique_lock
<
std
::
mutex
>
lock
(
thr_mutex
);
t
.
stop
();
thr_event
.
wait_for
(
lock
,
std
::
chrono
::
milliseconds
(
tout
),
[
=
]()
{
return
(
thr_finished
==
true
);
}
);
return
result
;
return
result
;
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void
TCPCheck
::
check_thread
()
void
TCPCheck
::
check_thread
()
{
{
setResult
(
false
);
thr_finished
=
false
;
result
=
false
;
try
try
{
{
UTCPStream
t
;
UTCPStream
t
;
t
.
create
(
ip
,
port
,
tout_msec
);
t
.
create
(
ip
,
port
,
tout_msec
);
t
.
setKeepAliveParams
(
(
tout_msec
>
1000
?
tout_msec
/
1000
:
1
)
);
t
.
setKeepAliveParams
(
(
tout_msec
>
1000
?
tout_msec
/
1000
:
1
)
);
setResult
(
true
)
;
result
=
true
;
t
.
disconnect
();
t
.
disconnect
();
}
}
catch
(
...
)
{}
catch
(
...
)
{}
}
// -----------------------------------------------------------------------------
template
<
typename
T
>
class
TGuard
{
public
:
TGuard
(
T
*
m
,
typename
ThreadCreator
<
T
>::
Action
a
)
:
t
(
m
,
a
)
{
t
.
start
();
}
~
TGuard
()
{
if
(
t
.
isRunning
()
)
t
.
stop
();
}
inline
bool
isRunning
()
{
return
t
.
isRunning
();
}
protected
:
ThreadCreator
<
T
>
t
;
};
thr_finished
=
true
;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
bool
TCPCheck
::
ping
(
const
std
::
string
&
_ip
,
timeout_t
tout
,
timeout_t
sleep_msec
,
const
std
::
string
&
_ping_args
)
bool
TCPCheck
::
ping
(
const
std
::
string
&
_ip
,
timeout_t
tout
,
const
std
::
string
&
_ping_args
)
{
{
ip
=
_ip
;
ip
=
_ip
;
tout_msec
=
tout
;
tout_msec
=
tout
;
ping_args
=
_ping_args
;
ping_args
=
_ping_args
;
thr_finished
=
false
;
result
=
false
;
setResult
(
false
);
TGuard
<
TCPCheck
>
t
(
this
,
&
TCPCheck
::
ping_thread
);
TGuard
<
TCPCheck
>
t
(
this
,
&
TCPCheck
::
ping_thread
);
PassiveTimer
pt
(
tout
);
std
::
unique_lock
<
std
::
mutex
>
lock
(
thr_mutex
);
thr_event
.
wait_for
(
lock
,
std
::
chrono
::
milliseconds
(
tout
),
[
=
]()
while
(
!
pt
.
checkTime
()
&&
t
.
isRunning
()
)
{
msleep
(
sleep_msec
);
return
(
thr_finished
==
true
);
}
);
return
result
;
return
result
;
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void
TCPCheck
::
ping_thread
()
void
TCPCheck
::
ping_thread
()
{
{
setResult
(
false
);
thr_finished
=
false
;
result
=
false
;
ostringstream
cmd
;
ostringstream
cmd
;
cmd
<<
"ping "
<<
ping_args
<<
" "
<<
ip
<<
" 2>/dev/null 1>/dev/null"
;
cmd
<<
"ping "
<<
ping_args
<<
" "
<<
ip
<<
" 2>/dev/null 1>/dev/null"
;
...
@@ -141,7 +137,8 @@ void TCPCheck::ping_thread()
...
@@ -141,7 +137,8 @@ void TCPCheck::ping_thread()
int
ret
=
system
(
cmd
.
str
().
c_str
());
int
ret
=
system
(
cmd
.
str
().
c_str
());
int
res
=
WEXITSTATUS
(
ret
);
int
res
=
WEXITSTATUS
(
ret
);
setResult
((
res
==
0
));
result
=
(
res
==
0
);
thr_finished
=
true
;
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
}
// end of namespace uniset
}
// end of namespace uniset
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