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
e5096e35
Commit
e5096e35
authored
May 04, 2015
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Небольшой рефакторинг TCPCheck, сделал тест для него.
parent
a88afa45
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
80 additions
and
27 deletions
+80
-27
TCPCheck.h
include/TCPCheck.h
+14
-17
TCPCheck.cc
src/Communications/TCP/TCPCheck.cc
+16
-9
Makefile.am
tests/Makefile.am
+2
-1
test_tcpcheck.cc
tests/test_tcpcheck.cc
+47
-0
uniset2.files
uniset2.files
+1
-0
No files found.
include/TCPCheck.h
View file @
e5096e35
#ifndef _TCPCheck_H_
#define _TCPCheck_H_
// -----------------------------------------------------------------------------
#include <atomic>
#include <cc++/socket.h>
#include "Mutex.h"
#include "ThreadCreator.h"
...
...
@@ -17,33 +18,29 @@ class TCPCheck
TCPCheck
();
~
TCPCheck
();
bool
check
(
const
std
::
string
&
_ip
,
int
_port
,
timeout_t
tout
,
timeout_t
sleep_msec
);
/*! Проверка связи с узлом
* \param _ip - ip проверяемого узла
* \param _port - порт для проверяемого узла
* \param tout - таймаут на попытку
* \param sleep_msec - пауза между проверками результата
*/
bool
check
(
const
std
::
string
&
_ip
,
int
_port
,
timeout_t
tout
,
timeout_t
sleep_msec
=
50
);
/*! \param iaddr - 'ip:port' */
bool
check
(
const
std
::
string
&
iaddr
,
timeout_t
tout
,
timeout_t
sleep_msec
);
bool
check
(
const
std
::
string
&
iaddr
,
timeout_t
tout
,
timeout_t
sleep_msec
=
50
);
protected
:
void
check_thread
();
inline
void
setResult
(
bool
res
)
{
UniSetTypes
::
uniset_rwmutex_wrlock
l
(
m
);
result
=
res
;
}
inline
bool
getResult
(
)
void
setResult
(
bool
s
)
{
bool
res
=
false
;
{
UniSetTypes
::
uniset_rwmutex_rlock
l
(
m
);
res
=
result
;
}
return
res
;
result
=
s
;
}
bool
result
;
std
::
string
iaddr
;
std
::
atomic_bool
result
=
{
false
};
std
::
string
ip
=
{
""
};
int
port
=
{
0
};
int
tout_msec
;
UniSetTypes
::
uniset_rwmutex
m
;
};
// -----------------------------------------------------------------------------
#endif // _TCPCheck_H_
...
...
src/Communications/TCP/TCPCheck.cc
View file @
e5096e35
...
...
@@ -3,11 +3,12 @@
#include "UniSetTypes.h"
#include "PassiveTimer.h"
#include "TCPCheck.h"
#include "UTCPStream.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
// -----------------------------------------------------------------------------
TCPCheck
::
TCPCheck
()
:
iaddr
(
""
),
tout_msec
(
0
)
tout_msec
(
0
)
{
}
// -----------------------------------------------------------------------------
...
...
@@ -16,19 +17,24 @@ TCPCheck::~TCPCheck()
}
// -----------------------------------------------------------------------------
bool
TCPCheck
::
check
(
const
std
::
string
&
ip
,
int
port
,
timeout_t
tout
,
timeout_t
sleep_msec
)
bool
TCPCheck
::
check
(
const
std
::
string
&
_iaddr
,
timeout_t
tout
,
timeout_t
sleep_msec
)
{
ostringstream
s
;
s
<<
ip
<<
":"
<<
port
;
return
check
(
s
.
str
(),
tout
,
sleep_msec
);
auto
v
=
UniSetTypes
::
explode_str
(
_iaddr
,
':'
);
if
(
v
.
size
()
<
2
)
return
false
;
return
check
(
v
[
0
],
UniSetTypes
::
uni_atoi
(
v
[
1
]),
tout
,
sleep_msec
);
}
// -----------------------------------------------------------------------------
bool
TCPCheck
::
check
(
const
std
::
string
&
_i
addr
,
timeout_t
tout
,
timeout_t
sleep_msec
)
bool
TCPCheck
::
check
(
const
std
::
string
&
_i
p
,
int
_port
,
timeout_t
tout
,
timeout_t
sleep_msec
)
{
iaddr
=
iaddr
;
ip
=
_ip
;
port
=
_port
;
tout_msec
=
tout
;
setResult
(
false
);
ThreadCreator
<
TCPCheck
>
t
(
this
,
&
TCPCheck
::
check_thread
);
t
.
setCancel
(
ost
::
Thread
::
cancelDeferred
);
t
.
start
();
...
...
@@ -41,7 +47,7 @@ bool TCPCheck::check( const std::string& _iaddr, timeout_t tout, timeout_t sleep
if
(
t
.
isRunning
()
)
// !getResult() )
t
.
stop
();
return
getResult
()
;
return
result
;
}
// -----------------------------------------------------------------------------
void
TCPCheck
::
check_thread
()
...
...
@@ -51,7 +57,8 @@ void TCPCheck::check_thread()
try
{
ost
::
Thread
::
setException
(
ost
::
Thread
::
throwException
);
ost
::
TCPStream
t
(
iaddr
.
c_str
(),
ost
::
Socket
::
IPV4
,
536
,
true
,
tout_msec
);
UTCPStream
t
;
t
.
create
(
ip
,
port
,
true
,
tout_msec
);
setResult
(
true
);
t
.
disconnect
();
}
...
...
tests/Makefile.am
View file @
e5096e35
...
...
@@ -25,7 +25,8 @@ test_pulse.cc \
test_modbustypes.cc
\
test_utypes.cc
\
test_mutex.cc
\
test_logserver.cc
test_logserver.cc
\
test_tcpcheck.cc
tests_with_conf_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
tests_with_conf_CPPFLAGS
=
-I
$(top_builddir)
/include
...
...
tests/test_tcpcheck.cc
0 → 100644
View file @
e5096e35
#include <catch.hpp>
#include <thread>
#include <atomic>
#include <future>
#include <ostream>
#include "TCPCheck.h"
#include "UniSetTypes.h"
using
namespace
std
;
static
int
port
=
2048
;
static
std
::
string
host
=
"localhost"
;
static
atomic_bool
cancel
=
{
false
};
// --------------------------------------------------------
bool
run_test_server
()
{
ost
::
InetAddress
addr
=
host
.
c_str
();
ost
::
TCPSocket
sock
(
addr
,
port
);
while
(
!
cancel
)
{
if
(
sock
.
isPendingConnection
(
500
)
)
{}
}
return
true
;
}
// --------------------------------------------------------
TEST_CASE
(
"TCPCheck"
,
"[tcpcheck]"
)
{
TCPCheck
t
;
auto
res
=
std
::
async
(
std
::
launch
::
async
,
run_test_server
);
ostringstream
ia
;
ia
<<
host
<<
":"
<<
port
;
CHECK
(
t
.
check
(
host
,
port
,
300
)
);
CHECK
(
t
.
check
(
ia
.
str
(),
300
)
);
CHECK_FALSE
(
t
.
check
(
"dummy_host_name"
,
port
,
300
)
);
CHECK_FALSE
(
t
.
check
(
"dummy_host_name:2048"
,
300
)
);
cancel
=
true
;
CHECK
(
res
.
get
()
);
}
// --------------------------------------------------------
uniset2.files
View file @
e5096e35
...
...
@@ -412,6 +412,7 @@ tests/test_ui.cc
tests/test_unixml.cc
tests/test_utypes.cc
tests/test_logserver.cc
tests/test_tcpcheck.cc
tests/tests-junit.xml
tests/tests.cc
tests/tests_bad_config.xml
...
...
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