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
25628e82
Commit
25628e82
authored
Apr 17, 2011
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Дописал немного документации по UNet2.
Удалил уже теперь не актуальный UDPExchange. Теперь для этого UNet2.
parent
611033c7
Hide whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
54 additions
and
4144 deletions
+54
-4144
RTUExchange.cc
extensions/RTUExchange/RTUExchange.cc
+0
-23
Makefile.am
extensions/UDPExchange/Makefile.am
+0
-47
UDPExchange.cc
extensions/UDPExchange/UDPExchange.cc
+0
-635
UDPExchange.h
extensions/UDPExchange/UDPExchange.h
+0
-119
UDPNReceiver.cc
extensions/UDPExchange/UDPNReceiver.cc
+0
-137
UDPNReceiver.h
extensions/UDPExchange/UDPNReceiver.h
+0
-54
UDPPacket.cc
extensions/UDPExchange/UDPPacket.cc
+0
-38
UDPPacket.h
extensions/UDPExchange/UDPPacket.h
+0
-49
UDPReceiver.cc
extensions/UDPExchange/UDPReceiver.cc
+0
-461
UDPReceiver.h
extensions/UDPExchange/UDPReceiver.h
+0
-83
UDPSender.cc
extensions/UDPExchange/UDPSender.cc
+0
-577
UDPSender.h
extensions/UDPExchange/UDPSender.h
+0
-112
UNetExchange.cc
extensions/UDPExchange/UNetExchange.cc
+0
-430
UNetExchange.h
extensions/UDPExchange/UNetExchange.h
+0
-87
UNetReceiver.cc
extensions/UDPExchange/UNetReceiver.cc
+0
-378
UNetReceiver.h
extensions/UDPExchange/UNetReceiver.h
+0
-147
UNetSender.cc
extensions/UDPExchange/UNetSender.cc
+0
-264
UNetSender.h
extensions/UDPExchange/UNetSender.h
+0
-90
create_links.sh
extensions/UDPExchange/create_links.sh
+0
-7
libUniSetUDP.pc.in
extensions/UDPExchange/libUniSetUDP.pc.in
+0
-11
start_fg.sh
extensions/UDPExchange/start_fg.sh
+0
-8
start_fg2.sh
extensions/UDPExchange/start_fg2.sh
+0
-8
start_fg_receiver.sh
extensions/UDPExchange/start_fg_receiver.sh
+0
-8
start_fg_sender.sh
extensions/UDPExchange/start_fg_sender.sh
+0
-8
start_fg_unetexchange.sh
extensions/UDPExchange/start_fg_unetexchange.sh
+0
-7
udpexchange.cc
extensions/UDPExchange/udpexchange.cc
+0
-85
udpreceiver.cc
extensions/UDPExchange/udpreceiver.cc
+0
-91
udpsender.cc
extensions/UDPExchange/udpsender.cc
+0
-89
unetexchange.cc
extensions/UDPExchange/unetexchange.cc
+0
-89
UDPPacket.h
extensions/UNet2/UDPPacket.h
+0
-2
UNetExchange.h
extensions/UNet2/UNetExchange.h
+31
-0
UniExchange.h
extensions/UniNetwork/UniExchange.h
+23
-0
No files found.
extensions/RTUExchange/RTUExchange.cc
View file @
25628e82
...
...
@@ -937,29 +937,6 @@ void RTUExchange::sigterm( int signo )
{
cerr
<<
myname
<<
": ********* SIGTERM("
<<
signo
<<
") ********"
<<
endl
;
activated
=
false
;
/*! \todo Нужно ли выставлять безопасное состояние. МОжно ведь не успеть совершить "обемен" */
// выставление безопасного состояния на выходы....
/*
RSMap::iterator it=rsmap.begin();
for( ; it!=rsmap.end(); ++it )
{
// if( it->stype!=UniversalIO::DigitalOutput && it->stype!=UniversalIO::AnalogOutput )
// continue;
if( it->safety == NoSafetyState )
continue;
try
{
}
catch( UniSetTypes::Exception& ex )
{
dlog[Debug::WARN] << myname << "(sigterm): " << ex << std::endl;
}
catch(...){}
}
*/
UniSetObject_LT
::
sigterm
(
signo
);
}
// ------------------------------------------------------------------------------------------
...
...
extensions/UDPExchange/Makefile.am
deleted
100644 → 0
View file @
611033c7
bin_PROGRAMS
=
@PACKAGE@-udpexchange @PACKAGE@-udpsender @PACKAGE@-udpreceiver
# не забывайте править версию в pc-файле
UUDP_VER
=
@LIBVER@
lib_LTLIBRARIES
=
libUniSetUDP.la
libUniSetUDP_la_LDFLAGS
=
-version-info
$(UUDP_VER)
libUniSetUDP_la_LIBADD
=
$(top_builddir)
/lib/libUniSet.la
\
$(top_builddir)
/extensions/SharedMemory/libUniSetSharedMemory.la
\
$(top_builddir)
/extensions/lib/libUniSetExtensions.la
\
$(SIGC_LIBS)
$(COMCPP_LIBS)
libUniSetUDP_la_CXXFLAGS
=
-I
$(top_builddir)
/extensions/include
-I
$(top_builddir)
/extensions/SharedMemory
$(SIGC_CFLAGS)
$(COMCPP_CFLAGS)
libUniSetUDP_la_SOURCES
=
UDPPacket.cc UDPExchange.cc UDPSender.cc UDPNReceiver.cc UDPReceiver.cc
#UDPSender.cc
@PACKAGE@
_udpexchange_SOURCES
=
udpexchange.cc
@PACKAGE@
_udpexchange_LDADD
=
libUniSetUDP.la
$(top_builddir)
/lib/libUniSet.la
\
$(top_builddir)
/extensions/SharedMemory/libUniSetSharedMemory.la
\
$(top_builddir)
/extensions/lib/libUniSetExtensions.la
\
$(SIGC_LIBS)
$(COMCPP_LIBS)
@PACKAGE@
_udpexchange_CXXFLAGS
=
-I
$(top_builddir)
/extensions/include
-I
$(top_builddir)
/extensions/SharedMemory
$(SIGC_CFLAGS)
$(COMCPP_CFLAGS)
@PACKAGE@
_udpsender_SOURCES
=
udpsender.cc
@PACKAGE@
_udpsender_LDADD
=
libUniSetUDP.la
$(top_builddir)
/lib/libUniSet.la
\
$(top_builddir)
/extensions/SharedMemory/libUniSetSharedMemory.la
\
$(top_builddir)
/extensions/lib/libUniSetExtensions.la
\
$(SIGC_LIBS)
$(COMCPP_LIBS)
@PACKAGE@
_udpsender_CXXFLAGS
=
-I
$(top_builddir)
/extensions/include
-I
$(top_builddir)
/extensions/SharedMemory
$(SIGC_CFLAGS)
$(COMCPP_CFLAGS)
@PACKAGE@
_udpreceiver_SOURCES
=
udpreceiver.cc
@PACKAGE@
_udpreceiver_LDADD
=
libUniSetUDP.la
$(top_builddir)
/lib/libUniSet.la
\
$(top_builddir)
/extensions/SharedMemory/libUniSetSharedMemory.la
\
$(top_builddir)
/extensions/lib/libUniSetExtensions.la
\
$(SIGC_LIBS)
$(COMCPP_LIBS)
@PACKAGE@
_udpreceiver_CXXFLAGS
=
-I
$(top_builddir)
/extensions/include
-I
$(top_builddir)
/extensions/SharedMemory
$(SIGC_CFLAGS)
$(COMCPP_CFLAGS)
# install
devel_include_HEADERS
=
*
.h
devel_includedir
=
$(pkgincludedir)
/extensions
pkgconfigdir
=
$(libdir)
/pkgconfig
pkgconfig_DATA
=
libUniSetUDP.pc
all-local
:
ln
-sf
../UDPExchange/
$(devel_include_HEADERS)
../include
extensions/UDPExchange/UDPExchange.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UDPExchange.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
UDPExchange
::
UDPExchange
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmId
,
SharedMemory
*
ic
)
:
UniSetObject_LT
(
objId
),
shm
(
0
),
initPause
(
0
),
udp
(
0
),
activated
(
false
),
dlist
(
100
),
maxItem
(
0
)
{
if
(
objId
==
DefaultObjectId
)
throw
UniSetTypes
::
SystemError
(
"(UDPExchange): objId=-1?!! Use --udp-name"
);
// xmlNode* cnode = conf->getNode(myname);
cnode
=
conf
->
getNode
(
myname
);
if
(
cnode
==
NULL
)
throw
UniSetTypes
::
SystemError
(
"(UDPExchange): Not find conf-node for "
+
myname
);
shm
=
new
SMInterface
(
shmId
,
&
ui
,
objId
,
ic
);
UniXML_iterator
it
(
cnode
);
// определяем фильтр
s_field
=
conf
->
getArgParam
(
"--udp-filter-field"
);
s_fvalue
=
conf
->
getArgParam
(
"--udp-filter-value"
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): read fileter-field='"
<<
s_field
<<
"' filter-value='"
<<
s_fvalue
<<
"'"
<<
endl
;
// ---------- init RS ----------
// UniXML_iterator it(cnode);
s_host
=
conf
->
getArgParam
(
"--udp-host"
,
it
.
getProp
(
"host"
));
if
(
s_host
.
empty
()
)
throw
UniSetTypes
::
SystemError
(
myname
+
"(UDPExchange): Unknown host. Use --udp-host"
);
host
=
s_host
.
c_str
();
buildReceiverList
();
// port = conf->getArgInt("--udp-port",it.getProp("port"));
if
(
port
<=
0
||
port
==
DefaultObjectId
)
throw
UniSetTypes
::
SystemError
(
myname
+
"(UDPExchange): Unknown port address"
);
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
"(UDPExchange): UDP set to "
<<
s_host
<<
":"
<<
port
<<
endl
;
try
{
udp
=
new
ost
::
UDPBroadcast
(
host
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
thr
=
new
ThreadCreator
<
UDPExchange
>
(
this
,
&
UDPExchange
::
poll
);
recvTimeout
=
conf
->
getArgPInt
(
"--udp-recv-timeout"
,
it
.
getProp
(
"recvTimeout"
),
5000
);
sendTimeout
=
conf
->
getArgPInt
(
"--udp-send-timeout"
,
it
.
getProp
(
"sendTimeout"
),
5000
);
polltime
=
conf
->
getArgPInt
(
"--udp-polltime"
,
it
.
getProp
(
"polltime"
),
100
);
// -------------------------------
if
(
shm
->
isLocalwork
()
)
{
readConfiguration
();
dlist
.
resize
(
maxItem
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): dlist size = "
<<
dlist
.
size
()
<<
endl
;
}
else
ic
->
addReadItem
(
sigc
::
mem_fun
(
this
,
&
UDPExchange
::
readItem
)
);
// ********** HEARTBEAT *************
string
heart
=
conf
->
getArgParam
(
"--udp-heartbeat-id"
,
it
.
getProp
(
"heartbeat_id"
));
if
(
!
heart
.
empty
()
)
{
sidHeartBeat
=
conf
->
getSensorID
(
heart
);
if
(
sidHeartBeat
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
": не найден идентификатор для датчика 'HeartBeat' "
<<
heart
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
int
heartbeatTime
=
getHeartBeatTime
();
if
(
heartbeatTime
)
ptHeartBeat
.
setTiming
(
heartbeatTime
);
else
ptHeartBeat
.
setTiming
(
UniSetTimer
::
WaitUpTime
);
maxHeartBeat
=
conf
->
getArgPInt
(
"--udp-heartbeat-max"
,
it
.
getProp
(
"heartbeat_max"
),
10
);
test_id
=
sidHeartBeat
;
}
else
{
test_id
=
conf
->
getSensorID
(
"TestMode_S"
);
if
(
test_id
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
"(init): test_id unknown. 'TestMode_S' not found..."
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): test_id="
<<
test_id
<<
endl
;
activateTimeout
=
conf
->
getArgPInt
(
"--activate-timeout"
,
20000
);
timeout_t
msec
=
conf
->
getArgPInt
(
"--udp-timeout"
,
it
.
getProp
(
"timeout"
),
3000
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): udp-timeout="
<<
msec
<<
" msec"
<<
endl
;
}
// -----------------------------------------------------------------------------
UDPExchange
::~
UDPExchange
()
{
for
(
ReceiverList
::
iterator
it
=
rlist
.
begin
();
it
!=
rlist
.
end
();
it
++
)
delete
(
*
it
);
delete
udp
;
delete
shm
;
delete
thr
;
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
waitSMReady
()
{
// waiting for SM is ready...
int
ready_timeout
=
conf
->
getArgInt
(
"--udp-sm-ready-timeout"
,
"15000"
);
if
(
ready_timeout
==
0
)
ready_timeout
=
15000
;
else
if
(
ready_timeout
<
0
)
ready_timeout
=
UniSetTimer
::
WaitUpTime
;
if
(
!
shm
->
waitSMready
(
ready_timeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(waitSMReady): Не дождались готовности SharedMemory к работе в течение "
<<
ready_timeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
step
()
{
if
(
!
activated
)
return
;
if
(
sidHeartBeat
!=
DefaultObjectId
&&
ptHeartBeat
.
checkTime
()
)
{
try
{
shm
->
localSaveValue
(
aitHeartBeat
,
sidHeartBeat
,
maxHeartBeat
,
getId
());
ptHeartBeat
.
reset
();
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(step): (hb) "
<<
ex
<<
std
::
endl
;
}
}
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
poll
()
{
dlist
.
resize
(
maxItem
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): dlist size = "
<<
dlist
.
size
()
<<
endl
;
for
(
ReceiverList
::
iterator
it
=
rlist
.
begin
();
it
!=
rlist
.
end
();
it
++
)
{
(
*
it
)
->
setReceiveTimeout
(
recvTimeout
);
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(poll): start exchange for "
<<
(
*
it
)
->
getName
()
<<
endl
;
(
*
it
)
->
start
();
}
ost
::
IPV4Broadcast
h
=
s_host
.
c_str
();
try
{
udp
->
setPeer
(
h
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
();
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(poll): "
<<
s
.
str
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
while
(
activated
)
{
try
{
send
();
}
catch
(
ost
::
SockException
&
e
)
{
cerr
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(step): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
cerr
<<
myname
<<
"(step): catch ..."
<<
std
::
endl
;
}
msleep
(
polltime
);
}
cerr
<<
"************* execute FINISH **********"
<<
endl
;
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
send
()
{
cout
<<
myname
<<
": send..."
<<
endl
;
/*
UniSetUDP::UDPHeader h;
h.nodeID = conf->getLocalNode();
h.procID = getId();
h.dcount = mypack.size();
if( udp->isPending(ost::Socket::pendingOutput) )
{
size_t ret = udp->send((char*)(&h),sizeof(h));
if( ret<(size_t)sizeof(h) )
{
cerr << myname << "(send data header): ret=" << ret << " sizeof=" << sizeof(h) << endl;
return;
}
*/
/*! \todo Подумать нужен ли здесь mutex */
UniSetUDP
::
UDPMessage
::
UDPDataList
::
iterator
it
=
mypack
.
dlist
.
begin
();
for
(
;
it
!=
mypack
.
dlist
.
end
();
++
it
)
{
// while( !udp->isPending(ost::Socket::pendingOutput) )
// msleep(30);
cout
<<
myname
<<
"(send): "
<<
(
*
it
)
<<
endl
;
size_t
ret
=
udp
->
send
((
char
*
)(
&
(
*
it
)),
sizeof
(
UniSetUDP
::
UDPData
));
if
(
ret
<
(
size_t
)
sizeof
(
UniSetUDP
::
UDPData
)
)
{
cerr
<<
myname
<<
"(send data): ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
UniSetUDP
::
UDPData
)
<<
endl
;
break
;
}
}
// }
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
)
{
try
{
switch
(
msg
->
type
)
{
case
UniSetTypes
:
:
Message
::
SysCommand
:
{
UniSetTypes
::
SystemMessage
sm
(
msg
);
sysCommand
(
&
sm
);
}
break
;
case
Message
:
:
SensorInfo
:
{
SensorMessage
sm
(
msg
);
sensorInfo
(
&
sm
);
}
break
;
default
:
break
;
}
}
catch
(
SystemError
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(SystemError): "
<<
ex
<<
std
::
endl
;
// throw SystemError(ex);
raise
(
SIGTERM
);
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): catch ...
\n
"
;
}
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
sysCommand
(
UniSetTypes
::
SystemMessage
*
sm
)
{
switch
(
sm
->
command
)
{
case
SystemMessage
:
:
StartUp
:
{
waitSMReady
();
// подождать пока пройдёт инициализация датчиков
// см. activateObject()
msleep
(
initPause
);
PassiveTimer
ptAct
(
activateTimeout
);
while
(
!
activated
&&
!
ptAct
.
checkTime
()
)
{
cout
<<
myname
<<
"(sysCommand): wait activate..."
<<
endl
;
msleep
(
300
);
if
(
activated
)
break
;
}
if
(
!
activated
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(sysCommand): ************* don`t activate?! ************"
<<
endl
;
{
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
10000
);
askSensors
(
UniversalIO
::
UIONotify
);
}
thr
->
start
();
}
case
SystemMessage
:
:
FoldUp
:
case
SystemMessage
:
:
Finish
:
askSensors
(
UniversalIO
::
UIODontNotify
);
break
;
case
SystemMessage
:
:
WatchDog
:
{
// ОПТИМИЗАЦИЯ (защита от двойного перезаказа при старте)
// Если идёт локальная работа
// (т.е. UDPExchange запущен в одном процессе с SharedMemory2)
// то обрабатывать WatchDog не надо, т.к. мы и так ждём готовности SM
// при заказе датчиков, а если SM вылетит, то вместе с этим процессом(UDPExchange)
if
(
shm
->
isLocalwork
()
)
break
;
askSensors
(
UniversalIO
::
UIONotify
);
}
break
;
case
SystemMessage
:
:
LogRotate
:
{
// переоткрываем логи
unideb
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
string
fname
=
unideb
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
unideb
.
logFile
(
fname
);
unideb
<<
myname
<<
"(sysCommand): ***************** UNIDEB LOG ROTATE *****************"
<<
std
::
endl
;
}
dlog
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
fname
=
dlog
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
dlog
.
logFile
(
fname
);
dlog
<<
myname
<<
"(sysCommand): ***************** dlog LOG ROTATE *****************"
<<
std
::
endl
;
}
}
break
;
default
:
break
;
}
}
// ------------------------------------------------------------------------------------------
void
UDPExchange
::
askSensors
(
UniversalIO
::
UIOCommand
cmd
)
{
if
(
!
shm
->
waitSMworking
(
test_id
,
activateTimeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(askSensors): Не дождались готовности(work) SharedMemory к работе в течение "
<<
activateTimeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
kill
(
SIGTERM
,
getpid
());
// прерываем (перезапускаем) процесс...
throw
SystemError
(
err
.
str
());
}
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
++
it
)
{
try
{
shm
->
askSensor
(
it
->
si
.
id
,
cmd
);
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
dlog
[
Debug
::
WARN
]
<<
myname
<<
"(askSensors): "
<<
ex
<<
std
::
endl
;
}
catch
(...){}
}
}
// ------------------------------------------------------------------------------------------
void
UDPExchange
::
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
)
{
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
++
it
)
{
if
(
it
->
si
.
id
==
sm
->
id
)
{
uniset_spin_lock
lock
(
it
->
val_lock
);
it
->
val
=
sm
->
value
;
if
(
it
->
pack_it
!=
mypack
.
dlist
.
end
()
)
it
->
pack_it
->
val
=
sm
->
value
;
}
break
;
}
}
// ------------------------------------------------------------------------------------------
bool
UDPExchange
::
activateObject
()
{
// блокирование обработки Starsp
// пока не пройдёт инициализация датчиков
// см. sysCommand()
{
activated
=
false
;
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
5000
);
UniSetObject_LT
::
activateObject
();
initIterators
();
activated
=
true
;
}
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UDPExchange
::
sigterm
(
int
signo
)
{
cerr
<<
myname
<<
": ********* SIGTERM("
<<
signo
<<
") ********"
<<
endl
;
activated
=
false
;
udp
->
disconnect
();
for
(
ReceiverList
::
iterator
it
=
rlist
.
begin
();
it
!=
rlist
.
end
();
it
++
)
(
*
it
)
->
stop
();
UniSetObject_LT
::
sigterm
(
signo
);
}
// ------------------------------------------------------------------------------------------
void
UDPExchange
::
readConfiguration
()
{
// readconf_ok = false;
xmlNode
*
root
=
conf
->
getXMLSensorsSection
();
if
(
!
root
)
{
ostringstream
err
;
err
<<
myname
<<
"(readConfiguration): не нашли корневого раздела <sensors>"
;
throw
SystemError
(
err
.
str
());
}
UniXML_iterator
it
(
root
);
if
(
!
it
.
goChildren
()
)
{
std
::
cerr
<<
myname
<<
"(readConfiguration): раздел <sensors> не содержит секций ?!!
\n
"
;
return
;
}
for
(
;
it
.
getCurrent
();
it
.
goNext
()
)
{
if
(
check_item
(
it
)
)
initItem
(
it
);
}
// readconf_ok = true;
}
// ------------------------------------------------------------------------------------------
bool
UDPExchange
::
check_item
(
UniXML_iterator
&
it
)
{
if
(
s_field
.
empty
()
)
return
true
;
// просто проверка на не пустой field
if
(
s_fvalue
.
empty
()
&&
it
.
getProp
(
s_field
).
empty
()
)
return
false
;
// просто проверка что field = value
if
(
!
s_fvalue
.
empty
()
&&
it
.
getProp
(
s_field
)
!=
s_fvalue
)
return
false
;
return
true
;
}
// ------------------------------------------------------------------------------------------
bool
UDPExchange
::
readItem
(
UniXML
&
xml
,
UniXML_iterator
&
it
,
xmlNode
*
sec
)
{
if
(
check_item
(
it
)
)
initItem
(
it
);
return
true
;
}
// ------------------------------------------------------------------------------------------
bool
UDPExchange
::
initItem
(
UniXML_iterator
&
it
)
{
string
sname
(
it
.
getProp
(
"name"
)
);
string
tid
=
it
.
getProp
(
"id"
);
ObjectId
sid
;
if
(
!
tid
.
empty
()
)
{
sid
=
UniSetTypes
::
uni_atoi
(
tid
);
if
(
sid
<=
0
)
sid
=
DefaultObjectId
;
}
else
sid
=
conf
->
getSensorID
(
sname
);
if
(
sid
==
DefaultObjectId
)
{
if
(
dlog
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(readItem): ID not found for "
<<
sname
<<
endl
;
return
false
;
}
UItem
p
;
p
.
si
.
id
=
sid
;
p
.
si
.
node
=
conf
->
getLocalNode
();
mypack
.
addData
(
sid
,
0
);
p
.
pack_it
=
(
mypack
.
dlist
.
end
()
--
);
if
(
maxItem
>=
dlist
.
size
()
)
dlist
.
resize
(
maxItem
+
10
);
dlist
[
maxItem
]
=
p
;
maxItem
++
;
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(initItem): add "
<<
p
<<
endl
;
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UDPExchange
::
initIterators
()
{
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
it
++
)
{
shm
->
initDIterator
(
it
->
dit
);
shm
->
initAIterator
(
it
->
ait
);
}
shm
->
initAIterator
(
aitHeartBeat
);
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
help_print
(
int
argc
,
char
*
argv
[]
)
{
cout
<<
"--udp-polltime msec - Пауза между опросаом карт. По умолчанию 200 мсек."
<<
endl
;
cout
<<
"--udp-heartbeat-id - Данный процесс связан с указанным аналоговым heartbeat-дачиком."
<<
endl
;
cout
<<
"--udp-heartbeat-max - Максимальное значение heartbeat-счётчика для данного процесса. По умолчанию 10."
<<
endl
;
cout
<<
"--udp-ready-timeout - Время ожидания готовности SM к работе, мсек. (-1 - ждать 'вечно')"
<<
endl
;
cout
<<
"--udp-initPause - Задержка перед инициализацией (время на активизация процесса)"
<<
endl
;
cout
<<
"--udp-notRespondSensor - датчик связи для данного процесса "
<<
endl
;
cout
<<
"--udp-sm-ready-timeout - время на ожидание старта SM"
<<
endl
;
cout
<<
" Настройки протокола UDP: "
<<
endl
;
cout
<<
"--udp-host [ip|hostname] - Адрес сервера"
<<
endl
;
cout
<<
"--udp-send-timeout - Таймаут на посылку ответа."
<<
endl
;
}
// -----------------------------------------------------------------------------
UDPExchange
*
UDPExchange
::
init_udpexchange
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
icID
,
SharedMemory
*
ic
)
{
string
name
=
conf
->
getArgParam
(
"--udp-name"
,
"UDPExchange1"
);
if
(
name
.
empty
()
)
{
cerr
<<
"(udpexchange): Не задан name'"
<<
endl
;
return
0
;
}
ObjectId
ID
=
conf
->
getObjectID
(
name
);
if
(
ID
==
UniSetTypes
::
DefaultObjectId
)
{
cerr
<<
"(udpexchange): идентификатор '"
<<
name
<<
"' не найден в конф. файле!"
<<
" в секции "
<<
conf
->
getObjectsSection
()
<<
endl
;
return
0
;
}
dlog
[
Debug
::
INFO
]
<<
"(rsexchange): name = "
<<
name
<<
"("
<<
ID
<<
")"
<<
endl
;
return
new
UDPExchange
(
ID
,
icID
,
ic
);
}
// -----------------------------------------------------------------------------
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UDPExchange
::
UItem
&
p
)
{
return
os
<<
" sid="
<<
p
.
si
.
id
;
}
// -----------------------------------------------------------------------------
void
UDPExchange
::
buildReceiverList
()
{
xmlNode
*
n
=
conf
->
getXMLNodesSection
();
if
(
!
n
)
{
dlog
[
Debug
::
WARN
]
<<
myname
<<
"(buildReceiverList): <nodes> not found! ignore..."
<<
endl
;
return
;
}
UniXML_iterator
it
(
n
);
if
(
!
it
.
goChildren
()
)
{
dlog
[
Debug
::
WARN
]
<<
myname
<<
"(buildReceiverList): <nodes> is empty?! ignore..."
<<
endl
;
return
;
}
for
(
;
it
.
getCurrent
();
it
.
goNext
()
)
{
ObjectId
n_id
=
conf
->
getNodeID
(
it
.
getProp
(
"name"
)
);
if
(
n_id
==
conf
->
getLocalNode
()
)
{
port
=
it
.
getIntProp
(
"udp_port"
);
if
(
port
<=
0
)
port
=
n_id
;
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(buildReceiverList): init myport port="
<<
port
<<
endl
;
continue
;
}
int
p
=
it
.
getIntProp
(
"udp_port"
);
if
(
p
<=
0
)
p
=
n_id
;
if
(
p
==
DefaultObjectId
)
{
dlog
[
Debug
::
WARN
]
<<
myname
<<
"(buildReceiverList): node="
<<
it
.
getProp
(
"name"
)
<<
" unknown port. ignore..."
<<
endl
;
continue
;
}
UDPNReceiver
*
r
=
new
UDPNReceiver
(
p
,
host
,
shm
->
getSMID
(),
shm
->
SM
());
rlist
.
push_back
(
r
);
}
}
// ------------------------------------------------------------------------------------------
extensions/UDPExchange/UDPExchange.h
deleted
100644 → 0
View file @
611033c7
#ifndef UDPExchange_H_
#define UDPExchange_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <vector>
#include <cc++/socket.h>
#include "UniSetObject_LT.h"
#include "Trigger.h"
#include "Mutex.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UDPPacket.h"
#include "UDPNReceiver.h"
// -----------------------------------------------------------------------------
class
UDPExchange
:
public
UniSetObject_LT
{
public
:
UDPExchange
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
virtual
~
UDPExchange
();
/*! глобальная функция для инициализации объекта */
static
UDPExchange
*
init_udpexchange
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
/*! глобальная функция для вывода help-а */
static
void
help_print
(
int
argc
,
char
*
argv
[]
);
struct
UItem
{
UItem
()
:
val
(
0
)
{}
IOController_i
::
SensorInfo
si
;
IOController
::
AIOStateList
::
iterator
ait
;
IOController
::
DIOStateList
::
iterator
dit
;
UniSetTypes
::
uniset_spin_mutex
val_lock
;
UniSetUDP
::
UDPMessage
::
UDPDataList
::
iterator
pack_it
;
long
val
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UItem
&
p
);
};
protected
:
xmlNode
*
cnode
;
std
::
string
s_field
;
std
::
string
s_fvalue
;
SMInterface
*
shm
;
void
poll
();
void
recv
();
void
send
();
void
step
();
virtual
void
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
);
void
sysCommand
(
UniSetTypes
::
SystemMessage
*
msg
);
void
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
);
void
askSensors
(
UniversalIO
::
UIOCommand
cmd
);
void
waitSMReady
();
virtual
bool
activateObject
();
// действия при завершении работы
virtual
void
sigterm
(
int
signo
);
void
initIterators
();
bool
initItem
(
UniXML_iterator
&
it
);
bool
readItem
(
UniXML
&
xml
,
UniXML_iterator
&
it
,
xmlNode
*
sec
);
void
readConfiguration
();
bool
check_item
(
UniXML_iterator
&
it
);
void
buildReceiverList
();
private
:
UDPExchange
();
bool
initPause
;
UniSetTypes
::
uniset_mutex
mutex_start
;
PassiveTimer
ptHeartBeat
;
UniSetTypes
::
ObjectId
sidHeartBeat
;
int
maxHeartBeat
;
IOController
::
AIOStateList
::
iterator
aitHeartBeat
;
UniSetTypes
::
ObjectId
test_id
;
int
polltime
;
/*!< переодичность обновления данных, [мсек] */
ost
::
UDPBroadcast
*
udp
;
ost
::
IPV4Host
host
;
ost
::
tpport_t
port
;
std
::
string
s_host
;
UniSetTypes
::
uniset_mutex
pollMutex
;
Trigger
trTimeout
;
int
recvTimeout
;
int
sendTimeout
;
bool
activated
;
int
activateTimeout
;
UniSetUDP
::
UDPMessage
mypack
;
typedef
std
::
vector
<
UItem
>
DMap
;
DMap
dlist
;
int
maxItem
;
typedef
std
::
list
<
UDPNReceiver
*>
ReceiverList
;
ReceiverList
rlist
;
ThreadCreator
<
UDPExchange
>*
thr
;
};
// -----------------------------------------------------------------------------
#endif // UDPExchange_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPNReceiver.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UDPNReceiver.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
UDPNReceiver
::
UDPNReceiver
(
ost
::
tpport_t
p
,
ost
::
IPV4Host
h
,
UniSetTypes
::
ObjectId
shmId
,
IONotifyController
*
ic
)
:
shm
(
0
),
ui
(
conf
),
activate
(
false
),
udp
(
0
),
host
(
h
),
port
(
p
),
recvTimeout
(
5000
),
conn
(
false
)
{
{
ostringstream
s
;
s
<<
host
<<
":"
<<
port
;
myname
=
s
.
str
();
}
shm
=
new
SMInterface
(
shmId
,
&
ui
,
DefaultObjectId
,
ic
);
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
"(UDPNReceiver): UDP set to "
<<
host
<<
":"
<<
port
<<
endl
;
try
{
udp
=
new
ost
::
UDPDuplex
(
host
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
thr
=
new
ThreadCreator
<
UDPNReceiver
>
(
this
,
&
UDPNReceiver
::
poll
);
thr
->
start
();
}
// -----------------------------------------------------------------------------
UDPNReceiver
::~
UDPNReceiver
()
{
delete
udp
;
delete
shm
;
delete
thr
;
}
// -----------------------------------------------------------------------------
void
UDPNReceiver
::
poll
()
{
while
(
1
)
{
if
(
!
activate
)
{
msleep
(
1000
);
continue
;
}
try
{
recv
();
}
catch
(
ost
::
SockException
&
e
)
{
cerr
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(step): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
cerr
<<
myname
<<
"(step): catch ..."
<<
std
::
endl
;
}
}
cerr
<<
"************* execute FINISH **********"
<<
endl
;
}
// -----------------------------------------------------------------------------
void
UDPNReceiver
::
recv
()
{
cout
<<
myname
<<
": recv....(timeout="
<<
recvTimeout
<<
")"
<<
endl
;
// UniSetUDP::UDPHeader h;
// receive
if
(
udp
->
isInputReady
(
recvTimeout
)
)
{
/*
size_t ret = udp->UDPReceive::receive(&h,sizeof(h));
if( ret<(size_t)sizeof(h) )
{
cerr << myname << "(receive): ret=" << ret << " sizeof=" << sizeof(h) << endl;
return;
}
cout << myname << "(receive): header: " << h << endl;
if( h.dcount <=0 )
{
cout << " data=0" << endl;
return;
}
*/
UniSetUDP
::
UDPData
d
;
// ignore echo...
#if 0
if( h.nodeID == conf->getLocalNode() && h.procID == getId() )
{
for( int i=0; i<h.dcount;i++ )
{
size_t ret = udp->UDPReceive::receive(&d,sizeof(d));
if( ret < (size_t)sizeof(d) )
return;
}
return;
}
#endif
for
(
int
i
=
0
;
i
<
100
;
i
++
)
{
size_t
ret
=
udp
->
UDPReceive
::
receive
(
&
d
,
sizeof
(
d
));
if
(
ret
<
(
size_t
)
sizeof
(
d
)
)
{
cerr
<<
myname
<<
"(receive data "
<<
i
<<
"): ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
d
)
<<
endl
;
break
;
}
cout
<<
myname
<<
"(receive data "
<<
i
<<
"): "
<<
d
<<
endl
;
}
}
// else
// {
// cout << "no InputReady.." << endl;
// }
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPNReceiver.h
deleted
100644 → 0
View file @
611033c7
#ifndef UDPNReceiver_H_
#define UDPNReceiver_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <vector>
#include <cc++/socket.h>
#include "Mutex.h"
#include "Trigger.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UDPPacket.h"
// -----------------------------------------------------------------------------
class
UDPNReceiver
{
public
:
UDPNReceiver
(
ost
::
tpport_t
port
,
ost
::
IPV4Host
host
,
UniSetTypes
::
ObjectId
shmID
,
IONotifyController
*
ic
=
0
);
virtual
~
UDPNReceiver
();
inline
int
getPort
(){
return
port
;
}
inline
bool
isConnetcion
(){
return
conn
;
}
inline
void
start
(){
activate
=
true
;
}
inline
void
stop
(){
activate
=
false
;
}
inline
void
setReceiveTimeout
(
int
t
){
recvTimeout
=
t
;
}
inline
std
::
string
getName
(){
return
myname
;
}
protected
:
SMInterface
*
shm
;
UniversalInterface
ui
;
void
poll
();
void
recv
();
std
::
string
myname
;
private
:
UDPNReceiver
();
bool
activate
;
ost
::
UDPDuplex
*
udp
;
ost
::
IPV4Host
host
;
ost
::
tpport_t
port
;
int
recvTimeout
;
bool
conn
;
ThreadCreator
<
UDPNReceiver
>*
thr
;
};
// -----------------------------------------------------------------------------
#endif // UDPNReceiver_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPPacket.cc
deleted
100644 → 0
View file @
611033c7
#include "UDPPacket.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetUDP
;
// -----------------------------------------------------------------------------
std
::
ostream
&
UniSetUDP
::
operator
<<
(
std
::
ostream
&
os
,
UniSetUDP
::
UDPHeader
&
p
)
{
return
os
<<
"nodeID="
<<
p
.
nodeID
<<
" procID="
<<
p
.
procID
<<
" dcount="
<<
p
.
dcount
;
}
// -----------------------------------------------------------------------------
std
::
ostream
&
UniSetUDP
::
operator
<<
(
std
::
ostream
&
os
,
UniSetUDP
::
UDPData
&
p
)
{
return
os
<<
"id="
<<
p
.
id
<<
" val="
<<
p
.
val
;
}
// -----------------------------------------------------------------------------
std
::
ostream
&
UniSetUDP
::
operator
<<
(
std
::
ostream
&
os
,
UniSetUDP
::
UDPMessage
&
p
)
{
return
os
;
}
// -----------------------------------------------------------------------------
UDPMessage
::
UDPMessage
()
{
}
// -----------------------------------------------------------------------------
void
UDPMessage
::
addData
(
const
UniSetUDP
::
UDPData
&
dat
)
{
dlist
.
push_back
(
dat
);
}
// -----------------------------------------------------------------------------
void
UDPMessage
::
addData
(
long
id
,
long
val
)
{
UDPData
d
(
id
,
val
);
addData
(
d
);
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPPacket.h
deleted
100644 → 0
View file @
611033c7
// -----------------------------------------------------------------------------
#ifndef UDPPacket_H_
#define UDPPacket_H_
// -----------------------------------------------------------------------------
#include <list>
#include <ostream>
#include "UniSetTypes.h"
// -----------------------------------------------------------------------------
namespace
UniSetUDP
{
struct
UDPHeader
{
long
nodeID
;
long
procID
;
long
dcount
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UDPHeader
&
p
);
}
__attribute__
((
packed
));
struct
UDPData
{
UDPData
()
:
id
(
UniSetTypes
::
DefaultObjectId
),
val
(
0
){}
UDPData
(
long
id
,
long
val
)
:
id
(
id
),
val
(
val
){}
long
id
;
long
val
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UDPData
&
p
);
}
__attribute__
((
packed
));
struct
UDPMessage
:
public
UDPHeader
{
UDPMessage
();
void
addData
(
const
UDPData
&
dat
);
void
addData
(
long
id
,
long
val
);
inline
int
size
(){
return
dlist
.
size
();
}
typedef
std
::
list
<
UDPData
>
UDPDataList
;
UDPDataList
dlist
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UDPMessage
&
p
);
};
}
// -----------------------------------------------------------------------------
#endif // UDPPacket_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPReceiver.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UDPReceiver.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
UDPReceiver
::
UDPReceiver
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmId
,
SharedMemory
*
ic
)
:
UniSetObject_LT
(
objId
),
shm
(
0
),
initPause
(
0
),
udp
(
0
),
activated
(
false
)
{
if
(
objId
==
DefaultObjectId
)
throw
UniSetTypes
::
SystemError
(
"(UDPReceiver): objId=-1?!! Use --udp-name"
);
// xmlNode* cnode = conf->getNode(myname);
cnode
=
conf
->
getNode
(
myname
);
if
(
cnode
==
NULL
)
throw
UniSetTypes
::
SystemError
(
"(UDPReceiver): Not find conf-node for "
+
myname
);
shm
=
new
SMInterface
(
shmId
,
&
ui
,
objId
,
ic
);
UniXML_iterator
it
(
cnode
);
// определяем фильтр
s_field
=
conf
->
getArgParam
(
"--udp-filter-field"
);
s_fvalue
=
conf
->
getArgParam
(
"--udp-filter-value"
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): read fileter-field='"
<<
s_field
<<
"' filter-value='"
<<
s_fvalue
<<
"'"
<<
endl
;
// ---------- init RS ----------
// UniXML_iterator it(cnode);
string
s_host
=
conf
->
getArgParam
(
"--udp-host"
,
it
.
getProp
(
"host"
));
if
(
s_host
.
empty
()
)
throw
UniSetTypes
::
SystemError
(
myname
+
"(UDPReceiver): Unknown host. Use --udp-host"
);
port
=
conf
->
getArgInt
(
"--udp-port"
,
it
.
getProp
(
"port"
));
if
(
port
<=
0
)
throw
UniSetTypes
::
SystemError
(
myname
+
"(UDPReceiver): Unknown port address. Use --udp-port"
);
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
"(UDPReceiver): UDP set to "
<<
s_host
<<
":"
<<
port
<<
endl
;
host
=
s_host
.
c_str
();
try
{
udp
=
new
ost
::
UDPDuplex
(
host
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
thr
=
new
ThreadCreator
<
UDPReceiver
>
(
this
,
&
UDPReceiver
::
poll
);
recvTimeout
=
conf
->
getArgPInt
(
"--udp-recv-timeout"
,
it
.
getProp
(
"recvTimeout"
),
5000
);
polltime
=
conf
->
getArgPInt
(
"--udp-polltime"
,
it
.
getProp
(
"polltime"
),
100
);
// -------------------------------
// ********** HEARTBEAT *************
string
heart
=
conf
->
getArgParam
(
"--udp-heartbeat-id"
,
it
.
getProp
(
"heartbeat_id"
));
if
(
!
heart
.
empty
()
)
{
sidHeartBeat
=
conf
->
getSensorID
(
heart
);
if
(
sidHeartBeat
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
": не найден идентификатор для датчика 'HeartBeat' "
<<
heart
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
int
heartbeatTime
=
getHeartBeatTime
();
if
(
heartbeatTime
)
ptHeartBeat
.
setTiming
(
heartbeatTime
);
else
ptHeartBeat
.
setTiming
(
UniSetTimer
::
WaitUpTime
);
maxHeartBeat
=
conf
->
getArgPInt
(
"--udp-heartbeat-max"
,
it
.
getProp
(
"heartbeat_max"
),
10
);
test_id
=
sidHeartBeat
;
}
else
{
test_id
=
conf
->
getSensorID
(
"TestMode_S"
);
if
(
test_id
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
"(init): test_id unknown. 'TestMode_S' not found..."
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): test_id="
<<
test_id
<<
endl
;
activateTimeout
=
conf
->
getArgPInt
(
"--activate-timeout"
,
20000
);
timeout_t
msec
=
conf
->
getArgPInt
(
"--udp-timeout"
,
it
.
getProp
(
"timeout"
),
3000
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): udp-timeout="
<<
msec
<<
" msec"
<<
endl
;
}
// -----------------------------------------------------------------------------
UDPReceiver
::~
UDPReceiver
()
{
delete
udp
;
delete
shm
;
delete
thr
;
}
// -----------------------------------------------------------------------------
void
UDPReceiver
::
waitSMReady
()
{
// waiting for SM is ready...
int
ready_timeout
=
conf
->
getArgInt
(
"--udp-sm-ready-timeout"
,
"15000"
);
if
(
ready_timeout
==
0
)
ready_timeout
=
15000
;
else
if
(
ready_timeout
<
0
)
ready_timeout
=
UniSetTimer
::
WaitUpTime
;
if
(
!
shm
->
waitSMready
(
ready_timeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(waitSMReady): Не дождались готовности SharedMemory к работе в течение "
<<
ready_timeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
// -----------------------------------------------------------------------------
/*
void UDPReceiver::timerInfo( TimerMessage *tm )
{
if( tm->id == tmExchange )
step();
}
*/
// -----------------------------------------------------------------------------
void
UDPReceiver
::
step
()
{
// {
// uniset_mutex_lock l(pollMutex,2000);
// poll();
// }
if
(
!
activated
)
return
;
if
(
sidHeartBeat
!=
DefaultObjectId
&&
ptHeartBeat
.
checkTime
()
)
{
try
{
shm
->
localSaveValue
(
aitHeartBeat
,
sidHeartBeat
,
maxHeartBeat
,
getId
());
ptHeartBeat
.
reset
();
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(step): (hb) "
<<
ex
<<
std
::
endl
;
}
}
}
// -----------------------------------------------------------------------------
void
UDPReceiver
::
poll
()
{
try
{
// udp->connect(host,port);
// udp->UDPSocket::setPeer(host,port);
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(step): "
<<
ex
<<
std
::
endl
;
// reise(SIGTERM);
return
;
}
while
(
activated
)
{
try
{
recv
();
// send();
}
catch
(
ost
::
SockException
&
e
)
{
cerr
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(step): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
cerr
<<
myname
<<
"(step): catch ..."
<<
std
::
endl
;
}
msleep
(
polltime
);
}
cerr
<<
"************* execute FINISH **********"
<<
endl
;
}
// -----------------------------------------------------------------------------
void
UDPReceiver
::
recv
()
{
cout
<<
myname
<<
": recv....(timeout="
<<
recvTimeout
<<
")"
<<
endl
;
UniSetUDP
::
UDPHeader
h
;
// receive
if
(
udp
->
isInputReady
(
recvTimeout
)
)
{
size_t
ret
=
udp
->
UDPReceive
::
receive
(
&
h
,
sizeof
(
h
));
if
(
ret
<
sizeof
(
h
)
)
{
cerr
<<
myname
<<
"(receive): ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
h
)
<<
endl
;
return
;
}
cout
<<
myname
<<
"(receive): header: "
<<
h
<<
endl
;
if
(
h
.
dcount
<=
0
)
{
cout
<<
" data=0"
<<
endl
;
return
;
}
UniSetUDP
::
UDPData
d
;
// ignore echo...
#if 0
if( h.nodeID == conf->getLocalNode() && h.procID == getId() )
{
for( int i=0; i<h.dcount;i++ )
{
size_t ret = udp->UDPReceive::receive(&d,sizeof(d));
if( ret < sizeof(d) )
return;
}
return;
}
#endif
for
(
int
i
=
0
;
i
<
h
.
dcount
;
i
++
)
{
size_t
ret
=
udp
->
UDPReceive
::
receive
(
&
d
,
sizeof
(
d
));
if
(
ret
<
sizeof
(
d
)
)
{
cerr
<<
myname
<<
"(receive data "
<<
i
<<
"): ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
d
)
<<
endl
;
break
;
}
cout
<<
myname
<<
"(receive data "
<<
i
<<
"): "
<<
d
<<
endl
;
}
}
// else
// {
// cout << "no InputReady.." << endl;
// }
}
// -----------------------------------------------------------------------------
void
UDPReceiver
::
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
)
{
try
{
switch
(
msg
->
type
)
{
case
UniSetTypes
:
:
Message
::
SysCommand
:
{
UniSetTypes
::
SystemMessage
sm
(
msg
);
sysCommand
(
&
sm
);
}
break
;
case
Message
:
:
SensorInfo
:
{
SensorMessage
sm
(
msg
);
sensorInfo
(
&
sm
);
}
break
;
default
:
break
;
}
}
catch
(
SystemError
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(SystemError): "
<<
ex
<<
std
::
endl
;
// throw SystemError(ex);
raise
(
SIGTERM
);
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): catch ...
\n
"
;
}
}
// -----------------------------------------------------------------------------
void
UDPReceiver
::
sysCommand
(
UniSetTypes
::
SystemMessage
*
sm
)
{
switch
(
sm
->
command
)
{
case
SystemMessage
:
:
StartUp
:
{
waitSMReady
();
// подождать пока пройдёт инициализация датчиков
// см. activateObject()
msleep
(
initPause
);
PassiveTimer
ptAct
(
activateTimeout
);
while
(
!
activated
&&
!
ptAct
.
checkTime
()
)
{
cout
<<
myname
<<
"(sysCommand): wait activate..."
<<
endl
;
msleep
(
300
);
if
(
activated
)
break
;
}
if
(
!
activated
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(sysCommand): ************* don`t activate?! ************"
<<
endl
;
{
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
10000
);
askSensors
(
UniversalIO
::
UIONotify
);
}
thr
->
start
();
}
case
SystemMessage
:
:
FoldUp
:
case
SystemMessage
:
:
Finish
:
askSensors
(
UniversalIO
::
UIODontNotify
);
break
;
case
SystemMessage
:
:
WatchDog
:
{
// ОПТИМИЗАЦИЯ (защита от двойного перезаказа при старте)
// Если идёт локальная работа
// (т.е. UDPReceiver запущен в одном процессе с SharedMemory2)
// то обрабатывать WatchDog не надо, т.к. мы и так ждём готовности SM
// при заказе датчиков, а если SM вылетит, то вместе с этим процессом(UDPReceiver)
if
(
shm
->
isLocalwork
()
)
break
;
askSensors
(
UniversalIO
::
UIONotify
);
}
break
;
case
SystemMessage
:
:
LogRotate
:
{
// переоткрываем логи
unideb
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
string
fname
=
unideb
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
unideb
.
logFile
(
fname
);
unideb
<<
myname
<<
"(sysCommand): ***************** UNIDEB LOG ROTATE *****************"
<<
std
::
endl
;
}
dlog
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
fname
=
dlog
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
dlog
.
logFile
(
fname
);
dlog
<<
myname
<<
"(sysCommand): ***************** dlog LOG ROTATE *****************"
<<
std
::
endl
;
}
}
break
;
default
:
break
;
}
}
// ------------------------------------------------------------------------------------------
void
UDPReceiver
::
askSensors
(
UniversalIO
::
UIOCommand
cmd
)
{
if
(
!
shm
->
waitSMworking
(
test_id
,
activateTimeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(askSensors): Не дождались готовности(work) SharedMemory к работе в течение "
<<
activateTimeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
kill
(
SIGTERM
,
getpid
());
// прерываем (перезапускаем) процесс...
throw
SystemError
(
err
.
str
());
}
}
// ------------------------------------------------------------------------------------------
void
UDPReceiver
::
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
)
{
}
// ------------------------------------------------------------------------------------------
bool
UDPReceiver
::
activateObject
()
{
// блокирование обработки Starsp
// пока не пройдёт инициализация датчиков
// см. sysCommand()
{
activated
=
false
;
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
5000
);
UniSetObject_LT
::
activateObject
();
initIterators
();
activated
=
true
;
}
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UDPReceiver
::
sigterm
(
int
signo
)
{
cerr
<<
myname
<<
": ********* SIGTERM("
<<
signo
<<
") ********"
<<
endl
;
activated
=
false
;
udp
->
disconnect
();
UniSetObject_LT
::
sigterm
(
signo
);
}
// ------------------------------------------------------------------------------------------
void
UDPReceiver
::
initIterators
()
{
shm
->
initAIterator
(
aitHeartBeat
);
}
// -----------------------------------------------------------------------------
void
UDPReceiver
::
help_print
(
int
argc
,
char
*
argv
[]
)
{
cout
<<
"--udp-polltime msec - Пауза между опросаом карт. По умолчанию 200 мсек."
<<
endl
;
cout
<<
"--udp-heartbeat-id - Данный процесс связан с указанным аналоговым heartbeat-дачиком."
<<
endl
;
cout
<<
"--udp-heartbeat-max - Максимальное значение heartbeat-счётчика для данного процесса. По умолчанию 10."
<<
endl
;
cout
<<
"--udp-ready-timeout - Время ожидания готовности SM к работе, мсек. (-1 - ждать 'вечно')"
<<
endl
;
cout
<<
"--udp-initPause - Задержка перед инициализацией (время на активизация процесса)"
<<
endl
;
cout
<<
"--udp-notRespondSensor - датчик связи для данного процесса "
<<
endl
;
cout
<<
"--udp-sm-ready-timeout - время на ожидание старта SM"
<<
endl
;
cout
<<
" Настройки протокола RS: "
<<
endl
;
cout
<<
"--udp-dev devname - файл устройства"
<<
endl
;
cout
<<
"--udp-speed - Скорость обмена (9600,19920,38400,57600,115200)."
<<
endl
;
cout
<<
"--udp-my-addr - адрес текущего узла"
<<
endl
;
cout
<<
"--udp-recv-timeout - Таймаут на ожидание ответа."
<<
endl
;
}
// -----------------------------------------------------------------------------
UDPReceiver
*
UDPReceiver
::
init_udpreceiver
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
icID
,
SharedMemory
*
ic
)
{
string
name
=
conf
->
getArgParam
(
"--udp-name"
,
"UDPReceiver1"
);
if
(
name
.
empty
()
)
{
cerr
<<
"(udpexchange): Не задан name'"
<<
endl
;
return
0
;
}
ObjectId
ID
=
conf
->
getObjectID
(
name
);
if
(
ID
==
UniSetTypes
::
DefaultObjectId
)
{
cerr
<<
"(udpexchange): идентификатор '"
<<
name
<<
"' не найден в конф. файле!"
<<
" в секции "
<<
conf
->
getObjectsSection
()
<<
endl
;
return
0
;
}
dlog
[
Debug
::
INFO
]
<<
"(rsexchange): name = "
<<
name
<<
"("
<<
ID
<<
")"
<<
endl
;
return
new
UDPReceiver
(
ID
,
icID
,
ic
);
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPReceiver.h
deleted
100644 → 0
View file @
611033c7
#ifndef UDPReceiver_H_
#define UDPReceiver_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <vector>
#include <cc++/socket.h>
#include "UniSetObject_LT.h"
#include "Trigger.h"
#include "Mutex.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UDPPacket.h"
// -----------------------------------------------------------------------------
class
UDPReceiver
:
public
UniSetObject_LT
{
public
:
UDPReceiver
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
virtual
~
UDPReceiver
();
/*! глобальная функция для инициализации объекта */
static
UDPReceiver
*
init_udpreceiver
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
/*! глобальная функция для вывода help-а */
static
void
help_print
(
int
argc
,
char
*
argv
[]
);
protected
:
xmlNode
*
cnode
;
std
::
string
s_field
;
std
::
string
s_fvalue
;
SMInterface
*
shm
;
void
poll
();
void
recv
();
void
step
();
virtual
void
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
);
void
sysCommand
(
UniSetTypes
::
SystemMessage
*
msg
);
void
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
);
void
askSensors
(
UniversalIO
::
UIOCommand
cmd
);
void
waitSMReady
();
virtual
bool
activateObject
();
// действия при завершении работы
virtual
void
sigterm
(
int
signo
);
void
initIterators
();
private
:
UDPReceiver
();
bool
initPause
;
UniSetTypes
::
uniset_mutex
mutex_start
;
PassiveTimer
ptHeartBeat
;
UniSetTypes
::
ObjectId
sidHeartBeat
;
int
maxHeartBeat
;
IOController
::
AIOStateList
::
iterator
aitHeartBeat
;
UniSetTypes
::
ObjectId
test_id
;
int
polltime
;
/*!< переодичность обновления данных, [мсек] */
ost
::
UDPDuplex
*
udp
;
ost
::
IPV4Host
host
;
ost
::
tpport_t
port
;
UniSetTypes
::
uniset_mutex
pollMutex
;
Trigger
trTimeout
;
int
recvTimeout
;
bool
activated
;
int
activateTimeout
;
ThreadCreator
<
UDPReceiver
>*
thr
;
};
// -----------------------------------------------------------------------------
#endif // UDPReceiver_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPSender.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UDPSender.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
UDPSender
::
UDPSender
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmId
,
SharedMemory
*
ic
)
:
UniSetObject_LT
(
objId
),
shm
(
0
),
initPause
(
0
),
udp
(
0
),
activated
(
false
),
dlist
(
100
),
maxItem
(
0
)
{
if
(
objId
==
DefaultObjectId
)
throw
UniSetTypes
::
SystemError
(
"(UDPSender): objId=-1?!! Use --udp-name"
);
// xmlNode* cnode = conf->getNode(myname);
cnode
=
conf
->
getNode
(
myname
);
if
(
cnode
==
NULL
)
throw
UniSetTypes
::
SystemError
(
"(UDPSender): Not find conf-node for "
+
myname
);
shm
=
new
SMInterface
(
shmId
,
&
ui
,
objId
,
ic
);
UniXML_iterator
it
(
cnode
);
// определяем фильтр
s_field
=
conf
->
getArgParam
(
"--udp-filter-field"
);
s_fvalue
=
conf
->
getArgParam
(
"--udp-filter-value"
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): read fileter-field='"
<<
s_field
<<
"' filter-value='"
<<
s_fvalue
<<
"'"
<<
endl
;
// ---------- init RS ----------
// UniXML_iterator it(cnode);
string
s_host
=
conf
->
getArgParam
(
"--udp-host"
,
it
.
getProp
(
"host"
));
if
(
s_host
.
empty
()
)
throw
UniSetTypes
::
SystemError
(
myname
+
"(UDPSender): Unknown host. Use --udp-host"
);
port
=
conf
->
getArgInt
(
"--udp-port"
,
it
.
getProp
(
"port"
));
if
(
port
<=
0
)
throw
UniSetTypes
::
SystemError
(
myname
+
"(UDPSender): Unknown port address. Use --udp-port"
);
bool
broadcast
=
conf
->
getArgInt
(
"--udp-broadcast"
,
it
.
getProp
(
"broadcast"
));
host
=
s_host
.
c_str
();
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
"(UDPSender): UDP set to "
<<
s_host
<<
":"
<<
port
<<
" broadcast="
<<
broadcast
<<
endl
;
try
{
if
(
!
broadcast
)
udp
=
new
ost
::
UDPSocket
();
else
udp
=
new
ost
::
UDPBroadcast
(
host
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
();
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
s
.
str
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
thr
=
new
ThreadCreator
<
UDPSender
>
(
this
,
&
UDPSender
::
poll
);
sendTimeout
=
conf
->
getArgPInt
(
"--udp-send-timeout"
,
it
.
getProp
(
"sendTimeout"
),
5000
);
sendtime
=
conf
->
getArgPInt
(
"--udp-sendtime"
,
it
.
getProp
(
"sendtime"
),
100
);
// -------------------------------
if
(
shm
->
isLocalwork
()
)
{
readConfiguration
();
dlist
.
resize
(
maxItem
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): dlist size = "
<<
dlist
.
size
()
<<
endl
;
}
else
ic
->
addReadItem
(
sigc
::
mem_fun
(
this
,
&
UDPSender
::
readItem
)
);
// ********** HEARTBEAT *************
string
heart
=
conf
->
getArgParam
(
"--udp-heartbeat-id"
,
it
.
getProp
(
"heartbeat_id"
));
if
(
!
heart
.
empty
()
)
{
sidHeartBeat
=
conf
->
getSensorID
(
heart
);
if
(
sidHeartBeat
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
": не найден идентификатор для датчика 'HeartBeat' "
<<
heart
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
int
heartbeatTime
=
getHeartBeatTime
();
if
(
heartbeatTime
)
ptHeartBeat
.
setTiming
(
heartbeatTime
);
else
ptHeartBeat
.
setTiming
(
UniSetTimer
::
WaitUpTime
);
maxHeartBeat
=
conf
->
getArgPInt
(
"--udp-heartbeat-max"
,
it
.
getProp
(
"heartbeat_max"
),
10
);
test_id
=
sidHeartBeat
;
}
else
{
test_id
=
conf
->
getSensorID
(
"TestMode_S"
);
if
(
test_id
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
"(init): test_id unknown. 'TestMode_S' not found..."
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): test_id="
<<
test_id
<<
endl
;
activateTimeout
=
conf
->
getArgPInt
(
"--activate-timeout"
,
20000
);
timeout_t
msec
=
conf
->
getArgPInt
(
"--udp-timeout"
,
it
.
getProp
(
"timeout"
),
3000
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): udp-timeout="
<<
msec
<<
" msec"
<<
endl
;
}
// -----------------------------------------------------------------------------
UDPSender
::~
UDPSender
()
{
delete
udp
;
delete
shm
;
delete
thr
;
}
// -----------------------------------------------------------------------------
void
UDPSender
::
waitSMReady
()
{
// waiting for SM is ready...
int
ready_timeout
=
conf
->
getArgInt
(
"--udp-sm-ready-timeout"
,
"15000"
);
if
(
ready_timeout
==
0
)
ready_timeout
=
15000
;
else
if
(
ready_timeout
<
0
)
ready_timeout
=
UniSetTimer
::
WaitUpTime
;
if
(
!
shm
->
waitSMready
(
ready_timeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(waitSMReady): Не дождались готовности SharedMemory к работе в течение "
<<
ready_timeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
// -----------------------------------------------------------------------------
void
UDPSender
::
step
()
{
if
(
!
activated
)
return
;
if
(
sidHeartBeat
!=
DefaultObjectId
&&
ptHeartBeat
.
checkTime
()
)
{
try
{
shm
->
localSaveValue
(
aitHeartBeat
,
sidHeartBeat
,
maxHeartBeat
,
getId
());
ptHeartBeat
.
reset
();
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(step): (hb) "
<<
ex
<<
std
::
endl
;
}
}
}
// -----------------------------------------------------------------------------
void
UDPSender
::
poll
()
{
dlist
.
resize
(
maxItem
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): dlist size = "
<<
dlist
.
size
()
<<
endl
;
try
{
udp
->
setPeer
(
host
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
();
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(poll): "
<<
s
.
str
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
while
(
activated
)
{
try
{
send
();
}
catch
(
ost
::
SockException
&
e
)
{
cerr
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(step): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
cerr
<<
myname
<<
"(step): catch ..."
<<
std
::
endl
;
}
msleep
(
sendtime
);
}
cerr
<<
"************* execute FINISH **********"
<<
endl
;
}
// -----------------------------------------------------------------------------
void
UDPSender
::
send
()
{
cout
<<
myname
<<
": send..."
<<
endl
;
UniSetUDP
::
UDPHeader
h
;
h
.
nodeID
=
conf
->
getLocalNode
();
h
.
procID
=
getId
();
h
.
dcount
=
mypack
.
size
();
// receive
size_t
ret
=
udp
->
send
((
char
*
)(
&
h
),
sizeof
(
h
));
if
(
ret
<
(
size_t
)
sizeof
(
h
)
)
{
cerr
<<
myname
<<
"(send data header): ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
h
)
<<
endl
;
return
;
}
/*! \todo Подумать нужен ли здесь mutex */
UniSetUDP
::
UDPMessage
::
UDPDataList
::
iterator
it
=
mypack
.
dlist
.
begin
();
for
(
;
it
!=
mypack
.
dlist
.
end
();
++
it
)
{
cout
<<
myname
<<
"(send): "
<<
(
*
it
)
<<
endl
;
size_t
ret
=
udp
->
send
((
char
*
)(
&
(
*
it
)),
sizeof
(
*
it
));
if
(
ret
<
(
size_t
)
sizeof
(
*
it
)
)
{
cerr
<<
myname
<<
"(send data): ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
*
it
)
<<
endl
;
break
;
}
}
}
// -----------------------------------------------------------------------------
void
UDPSender
::
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
)
{
try
{
switch
(
msg
->
type
)
{
case
UniSetTypes
:
:
Message
::
SysCommand
:
{
UniSetTypes
::
SystemMessage
sm
(
msg
);
sysCommand
(
&
sm
);
}
break
;
case
Message
:
:
SensorInfo
:
{
SensorMessage
sm
(
msg
);
sensorInfo
(
&
sm
);
}
break
;
default
:
break
;
}
}
catch
(
SystemError
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(SystemError): "
<<
ex
<<
std
::
endl
;
// throw SystemError(ex);
raise
(
SIGTERM
);
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): catch ...
\n
"
;
}
}
// -----------------------------------------------------------------------------
void
UDPSender
::
sysCommand
(
UniSetTypes
::
SystemMessage
*
sm
)
{
switch
(
sm
->
command
)
{
case
SystemMessage
:
:
StartUp
:
{
waitSMReady
();
// подождать пока пройдёт инициализация датчиков
// см. activateObject()
msleep
(
initPause
);
PassiveTimer
ptAct
(
activateTimeout
);
while
(
!
activated
&&
!
ptAct
.
checkTime
()
)
{
cout
<<
myname
<<
"(sysCommand): wait activate..."
<<
endl
;
msleep
(
300
);
if
(
activated
)
break
;
}
if
(
!
activated
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(sysCommand): ************* don`t activate?! ************"
<<
endl
;
{
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
10000
);
askSensors
(
UniversalIO
::
UIONotify
);
}
thr
->
start
();
}
case
SystemMessage
:
:
FoldUp
:
case
SystemMessage
:
:
Finish
:
askSensors
(
UniversalIO
::
UIODontNotify
);
break
;
case
SystemMessage
:
:
WatchDog
:
{
// ОПТИМИЗАЦИЯ (защита от двойного перезаказа при старте)
// Если идёт локальная работа
// (т.е. UDPSender запущен в одном процессе с SharedMemory2)
// то обрабатывать WatchDog не надо, т.к. мы и так ждём готовности SM
// при заказе датчиков, а если SM вылетит, то вместе с этим процессом(UDPSender)
if
(
shm
->
isLocalwork
()
)
break
;
askSensors
(
UniversalIO
::
UIONotify
);
}
break
;
case
SystemMessage
:
:
LogRotate
:
{
// переоткрываем логи
unideb
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
string
fname
=
unideb
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
unideb
.
logFile
(
fname
);
unideb
<<
myname
<<
"(sysCommand): ***************** UNIDEB LOG ROTATE *****************"
<<
std
::
endl
;
}
dlog
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
fname
=
dlog
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
dlog
.
logFile
(
fname
);
dlog
<<
myname
<<
"(sysCommand): ***************** dlog LOG ROTATE *****************"
<<
std
::
endl
;
}
}
break
;
default
:
break
;
}
}
// ------------------------------------------------------------------------------------------
void
UDPSender
::
askSensors
(
UniversalIO
::
UIOCommand
cmd
)
{
if
(
!
shm
->
waitSMworking
(
test_id
,
activateTimeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(askSensors): Не дождались готовности(work) SharedMemory к работе в течение "
<<
activateTimeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
kill
(
SIGTERM
,
getpid
());
// прерываем (перезапускаем) процесс...
throw
SystemError
(
err
.
str
());
}
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
++
it
)
{
try
{
shm
->
askSensor
(
it
->
si
.
id
,
cmd
);
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
dlog
[
Debug
::
WARN
]
<<
myname
<<
"(askSensors): "
<<
ex
<<
std
::
endl
;
}
catch
(...){}
}
}
// ------------------------------------------------------------------------------------------
void
UDPSender
::
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
)
{
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
++
it
)
{
if
(
it
->
si
.
id
==
sm
->
id
)
{
uniset_spin_lock
lock
(
it
->
val_lock
);
it
->
val
=
sm
->
value
;
if
(
it
->
pack_it
!=
mypack
.
dlist
.
end
()
)
it
->
pack_it
->
val
=
sm
->
value
;
}
break
;
}
}
// ------------------------------------------------------------------------------------------
bool
UDPSender
::
activateObject
()
{
// блокирование обработки StarUp
// пока не пройдёт инициализация датчиков
// см. sysCommand()
{
activated
=
false
;
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
5000
);
UniSetObject_LT
::
activateObject
();
initIterators
();
activated
=
true
;
}
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UDPSender
::
sigterm
(
int
signo
)
{
cerr
<<
myname
<<
": ********* SIGTERM("
<<
signo
<<
") ********"
<<
endl
;
activated
=
false
;
udp
->
disconnect
();
UniSetObject_LT
::
sigterm
(
signo
);
}
// ------------------------------------------------------------------------------------------
void
UDPSender
::
readConfiguration
()
{
// readconf_ok = false;
xmlNode
*
root
=
conf
->
getXMLSensorsSection
();
if
(
!
root
)
{
ostringstream
err
;
err
<<
myname
<<
"(readConfiguration): не нашли корневого раздела <sensors>"
;
throw
SystemError
(
err
.
str
());
}
UniXML_iterator
it
(
root
);
if
(
!
it
.
goChildren
()
)
{
std
::
cerr
<<
myname
<<
"(readConfiguration): раздел <sensors> не содержит секций ?!!
\n
"
;
return
;
}
for
(
;
it
.
getCurrent
();
it
.
goNext
()
)
{
if
(
check_item
(
it
)
)
initItem
(
it
);
}
// readconf_ok = true;
}
// ------------------------------------------------------------------------------------------
bool
UDPSender
::
check_item
(
UniXML_iterator
&
it
)
{
if
(
s_field
.
empty
()
)
return
true
;
// просто проверка на не пустой field
if
(
s_fvalue
.
empty
()
&&
it
.
getProp
(
s_field
).
empty
()
)
return
false
;
// просто проверка что field = value
if
(
!
s_fvalue
.
empty
()
&&
it
.
getProp
(
s_field
)
!=
s_fvalue
)
return
false
;
return
true
;
}
// ------------------------------------------------------------------------------------------
bool
UDPSender
::
readItem
(
UniXML
&
xml
,
UniXML_iterator
&
it
,
xmlNode
*
sec
)
{
if
(
check_item
(
it
)
)
initItem
(
it
);
return
true
;
}
// ------------------------------------------------------------------------------------------
bool
UDPSender
::
initItem
(
UniXML_iterator
&
it
)
{
string
sname
(
it
.
getProp
(
"name"
)
);
string
tid
=
it
.
getProp
(
"id"
);
ObjectId
sid
;
if
(
!
tid
.
empty
()
)
{
sid
=
UniSetTypes
::
uni_atoi
(
tid
);
if
(
sid
<=
0
)
sid
=
DefaultObjectId
;
}
else
sid
=
conf
->
getSensorID
(
sname
);
if
(
sid
==
DefaultObjectId
)
{
if
(
dlog
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(readItem): ID not found for "
<<
sname
<<
endl
;
return
false
;
}
UItem
p
;
p
.
si
.
id
=
sid
;
p
.
si
.
node
=
conf
->
getLocalNode
();
mypack
.
addData
(
sid
,
0
);
p
.
pack_it
=
(
mypack
.
dlist
.
end
()
--
);
if
(
maxItem
>=
dlist
.
size
()
)
dlist
.
resize
(
maxItem
+
10
);
dlist
[
maxItem
]
=
p
;
maxItem
++
;
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(initItem): add "
<<
p
<<
endl
;
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UDPSender
::
initIterators
()
{
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
it
++
)
{
shm
->
initDIterator
(
it
->
dit
);
shm
->
initAIterator
(
it
->
ait
);
}
shm
->
initAIterator
(
aitHeartBeat
);
}
// -----------------------------------------------------------------------------
void
UDPSender
::
help_print
(
int
argc
,
char
*
argv
[]
)
{
cout
<<
"--udp-sendtime msec - Пауза между опросами. По умолчанию 200 мсек."
<<
endl
;
cout
<<
"--udp-heartbeat-id - Данный процесс связан с указанным аналоговым heartbeat-дачиком."
<<
endl
;
cout
<<
"--udp-heartbeat-max - Максимальное значение heartbeat-счётчика для данного процесса. По умолчанию 10."
<<
endl
;
cout
<<
"--udp-ready-timeout - Время ожидания готовности SM к работе, мсек. (-1 - ждать 'вечно')"
<<
endl
;
cout
<<
"--udp-initPause - Задержка перед инициализацией (время на активизация процесса)"
<<
endl
;
cout
<<
"--udp-sm-ready-timeout - время на ожидание старта SM"
<<
endl
;
cout
<<
" Настройки протокола UDP: "
<<
endl
;
cout
<<
"--udp-host [ip|hostname] - Адрес сервера"
<<
endl
;
cout
<<
"--udp-port - Порт."
<<
endl
;
cout
<<
"--udp-send-timeout - Таймаут на посылку ответа."
<<
endl
;
}
// -----------------------------------------------------------------------------
UDPSender
*
UDPSender
::
init_udpsender
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
icID
,
SharedMemory
*
ic
)
{
string
name
=
conf
->
getArgParam
(
"--udp-name"
,
"UDPSender1"
);
if
(
name
.
empty
()
)
{
cerr
<<
"(UDPSender): Не задан name'"
<<
endl
;
return
0
;
}
ObjectId
ID
=
conf
->
getObjectID
(
name
);
if
(
ID
==
UniSetTypes
::
DefaultObjectId
)
{
cerr
<<
"(UDPSender): идентификатор '"
<<
name
<<
"' не найден в конф. файле!"
<<
" в секции "
<<
conf
->
getObjectsSection
()
<<
endl
;
return
0
;
}
dlog
[
Debug
::
INFO
]
<<
"(rsexchange): name = "
<<
name
<<
"("
<<
ID
<<
")"
<<
endl
;
return
new
UDPSender
(
ID
,
icID
,
ic
);
}
// -----------------------------------------------------------------------------
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UDPSender
::
UItem
&
p
)
{
return
os
<<
" sid="
<<
p
.
si
.
id
;
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UDPSender.h
deleted
100644 → 0
View file @
611033c7
#ifndef UDPSender_H_
#define UDPSender_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <vector>
#include <cc++/socket.h>
#include "UniSetObject_LT.h"
#include "Trigger.h"
#include "Mutex.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UDPPacket.h"
// -----------------------------------------------------------------------------
class
UDPSender
:
public
UniSetObject_LT
{
public
:
UDPSender
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
virtual
~
UDPSender
();
/*! глобальная функция для инициализации объекта */
static
UDPSender
*
init_udpsender
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
/*! глобальная функция для вывода help-а */
static
void
help_print
(
int
argc
,
char
*
argv
[]
);
struct
UItem
{
UItem
()
:
val
(
0
)
{}
IOController_i
::
SensorInfo
si
;
IOController
::
AIOStateList
::
iterator
ait
;
IOController
::
DIOStateList
::
iterator
dit
;
UniSetTypes
::
uniset_spin_mutex
val_lock
;
UniSetUDP
::
UDPMessage
::
UDPDataList
::
iterator
pack_it
;
long
val
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UItem
&
p
);
};
protected
:
xmlNode
*
cnode
;
std
::
string
s_field
;
std
::
string
s_fvalue
;
SMInterface
*
shm
;
void
poll
();
void
recv
();
void
send
();
void
step
();
virtual
void
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
);
void
sysCommand
(
UniSetTypes
::
SystemMessage
*
msg
);
void
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
);
void
askSensors
(
UniversalIO
::
UIOCommand
cmd
);
void
waitSMReady
();
virtual
bool
activateObject
();
// действия при завершении работы
virtual
void
sigterm
(
int
signo
);
void
initIterators
();
bool
initItem
(
UniXML_iterator
&
it
);
bool
readItem
(
UniXML
&
xml
,
UniXML_iterator
&
it
,
xmlNode
*
sec
);
void
readConfiguration
();
bool
check_item
(
UniXML_iterator
&
it
);
private
:
UDPSender
();
bool
initPause
;
UniSetTypes
::
uniset_mutex
mutex_start
;
PassiveTimer
ptHeartBeat
;
UniSetTypes
::
ObjectId
sidHeartBeat
;
int
maxHeartBeat
;
IOController
::
AIOStateList
::
iterator
aitHeartBeat
;
UniSetTypes
::
ObjectId
test_id
;
int
sendtime
;
/*!< переодичность посылки данных, [мсек] */
ost
::
UDPSocket
*
udp
;
ost
::
IPV4Host
host
;
ost
::
tpport_t
port
;
UniSetTypes
::
uniset_mutex
sendMutex
;
Trigger
trTimeout
;
int
sendTimeout
;
bool
activated
;
int
activateTimeout
;
UniSetUDP
::
UDPMessage
mypack
;
typedef
std
::
vector
<
UItem
>
DMap
;
DMap
dlist
;
int
maxItem
;
ThreadCreator
<
UDPSender
>*
thr
;
};
// -----------------------------------------------------------------------------
#endif // UDPSender_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UNetExchange.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UNetExchange.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
UNetExchange
::
UNetExchange
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmId
,
SharedMemory
*
ic
)
:
UniSetObject_LT
(
objId
),
shm
(
0
),
initPause
(
0
),
activated
(
false
),
no_sender
(
false
),
sender
(
0
)
{
if
(
objId
==
DefaultObjectId
)
throw
UniSetTypes
::
SystemError
(
"(UNetExchange): objId=-1?!! Use --unet-name"
);
// xmlNode* cnode = conf->getNode(myname);
cnode
=
conf
->
getNode
(
myname
);
if
(
cnode
==
NULL
)
throw
UniSetTypes
::
SystemError
(
"(UNetExchange): Not found conf-node for "
+
myname
);
shm
=
new
SMInterface
(
shmId
,
&
ui
,
objId
,
ic
);
UniXML_iterator
it
(
cnode
);
// определяем фильтр
s_field
=
conf
->
getArgParam
(
"--unet-filter-field"
);
s_fvalue
=
conf
->
getArgParam
(
"--unet-filter-value"
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): read fileter-field='"
<<
s_field
<<
"' filter-value='"
<<
s_fvalue
<<
"'"
<<
endl
;
int
recvTimeout
=
conf
->
getArgPInt
(
"--unet-recv-timeout"
,
it
.
getProp
(
"recvTimeout"
),
5000
);
int
lostTimeout
=
conf
->
getArgPInt
(
"--unet-lost-timeout"
,
it
.
getProp
(
"lostTimeout"
),
recvTimeout
);
int
recvpause
=
conf
->
getArgPInt
(
"--unet-recvpause"
,
it
.
getProp
(
"recvpause"
),
10
);
int
sendpause
=
conf
->
getArgPInt
(
"--unet-sendpause"
,
it
.
getProp
(
"sendpause"
),
150
);
int
updatepause
=
conf
->
getArgPInt
(
"--unet-updatepause"
,
it
.
getProp
(
"updatepause"
),
100
);
steptime
=
conf
->
getArgPInt
(
"--unet-steptime"
,
it
.
getProp
(
"steptime"
),
1000
);
int
maxDiff
=
conf
->
getArgPInt
(
"--unet-maxdifferense"
,
it
.
getProp
(
"maxDifferense"
),
1000
);
int
maxProcessingCount
=
conf
->
getArgPInt
(
"--unet-maxprocessingcount"
,
it
.
getProp
(
"maxProcessingCount"
),
100
);
no_sender
=
conf
->
getArgInt
(
"--unet-nosender"
,
it
.
getProp
(
"nosender"
));
xmlNode
*
nodes
=
conf
->
getXMLNodesSection
();
if
(
!
nodes
)
throw
UniSetTypes
::
SystemError
(
"(UNetExchange): Not found <nodes>"
);
UniXML_iterator
n_it
(
nodes
);
if
(
!
n_it
.
goChildren
()
)
throw
UniSetTypes
::
SystemError
(
"(UNetExchange): Items not found for <nodes>"
);
for
(
;
n_it
.
getCurrent
();
n_it
.
goNext
()
)
{
string
h
(
n_it
.
getProp
(
"ip"
));
if
(
!
n_it
.
getProp
(
"unet_ip"
).
empty
()
)
h
=
n_it
.
getProp
(
"unet_ip"
);
int
p
=
n_it
.
getIntProp
(
"id"
);
if
(
!
n_it
.
getProp
(
"unet_port"
).
empty
()
)
p
=
n_it
.
getIntProp
(
"unet_port"
);
string
n
(
n_it
.
getProp
(
"name"
));
if
(
n
==
conf
->
getLocalNodeName
()
)
{
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): init sender.. my node "
<<
n_it
.
getProp
(
"name"
)
<<
endl
;
sender
=
new
UNetSender
(
h
,
p
,
shm
,
s_field
,
s_fvalue
,
ic
);
sender
->
setSendPause
(
sendpause
);
continue
;
}
if
(
!
n_it
.
getProp
(
"unet_ignore"
).
empty
()
)
{
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): unet_ignore.. for "
<<
n_it
.
getProp
(
"name"
)
<<
endl
;
continue
;
}
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): add UNetReceiver for "
<<
h
<<
":"
<<
p
<<
endl
;
if
(
checkExistUNetHost
(
h
,
p
)
)
{
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): "
<<
h
<<
":"
<<
p
<<
" already added! Ignore.."
<<
endl
;
continue
;
}
UNetReceiver
*
r
=
new
UNetReceiver
(
h
,
p
,
shm
);
r
->
setReceiveTimeout
(
recvTimeout
);
r
->
setLostTimeout
(
lostTimeout
);
r
->
setReceivePause
(
recvpause
);
r
->
setUpdatePause
(
updatepause
);
r
->
setMaxDifferens
(
maxDiff
);
r
->
setMaxProcessingCount
(
maxProcessingCount
);
recvlist
.
push_back
(
r
);
}
// -------------------------------
// ********** HEARTBEAT *************
string
heart
=
conf
->
getArgParam
(
"--unet-heartbeat-id"
,
it
.
getProp
(
"heartbeat_id"
));
if
(
!
heart
.
empty
()
)
{
sidHeartBeat
=
conf
->
getSensorID
(
heart
);
if
(
sidHeartBeat
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
": не найден идентификатор для датчика 'HeartBeat' "
<<
heart
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
int
heartbeatTime
=
getHeartBeatTime
();
if
(
heartbeatTime
)
ptHeartBeat
.
setTiming
(
heartbeatTime
);
else
ptHeartBeat
.
setTiming
(
UniSetTimer
::
WaitUpTime
);
maxHeartBeat
=
conf
->
getArgPInt
(
"--unet-heartbeat-max"
,
it
.
getProp
(
"heartbeat_max"
),
10
);
test_id
=
sidHeartBeat
;
}
else
{
test_id
=
conf
->
getSensorID
(
"TestMode_S"
);
if
(
test_id
==
DefaultObjectId
)
{
ostringstream
err
;
err
<<
myname
<<
"(init): test_id unknown. 'TestMode_S' not found..."
;
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): test_id="
<<
test_id
<<
endl
;
activateTimeout
=
conf
->
getArgPInt
(
"--activate-timeout"
,
20000
);
timeout_t
msec
=
conf
->
getArgPInt
(
"--unet-timeout"
,
it
.
getProp
(
"timeout"
),
3000
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): udp-timeout="
<<
msec
<<
" msec"
<<
endl
;
}
// -----------------------------------------------------------------------------
UNetExchange
::~
UNetExchange
()
{
for
(
ReceiverList
::
iterator
it
=
recvlist
.
begin
();
it
!=
recvlist
.
end
();
++
it
)
delete
(
*
it
);
delete
sender
;
delete
shm
;
}
// -----------------------------------------------------------------------------
bool
UNetExchange
::
checkExistUNetHost
(
const
std
::
string
addr
,
ost
::
tpport_t
port
)
{
ost
::
IPV4Address
a1
(
addr
.
c_str
());
for
(
ReceiverList
::
iterator
it
=
recvlist
.
begin
();
it
!=
recvlist
.
end
();
++
it
)
{
if
(
(
*
it
)
->
getAddress
()
==
a1
.
getAddress
()
&&
(
*
it
)
->
getPort
()
==
port
)
return
true
;
}
return
false
;
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
startReceivers
()
{
for
(
ReceiverList
::
iterator
it
=
recvlist
.
begin
();
it
!=
recvlist
.
end
();
++
it
)
(
*
it
)
->
start
();
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
initSender
(
const
std
::
string
s_host
,
const
ost
::
tpport_t
port
,
UniXML_iterator
&
it
)
{
if
(
no_sender
)
return
;
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
waitSMReady
()
{
// waiting for SM is ready...
int
ready_timeout
=
conf
->
getArgInt
(
"--unet-sm-ready-timeout"
,
"15000"
);
if
(
ready_timeout
==
0
)
ready_timeout
=
15000
;
else
if
(
ready_timeout
<
0
)
ready_timeout
=
UniSetTimer
::
WaitUpTime
;
if
(
!
shm
->
waitSMready
(
ready_timeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(waitSMReady): Не дождались готовности SharedMemory к работе в течение "
<<
ready_timeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
throw
SystemError
(
err
.
str
());
}
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
timerInfo
(
TimerMessage
*
tm
)
{
if
(
!
activated
)
return
;
if
(
tm
->
id
==
tmStep
)
step
();
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
step
()
{
if
(
!
activated
)
return
;
if
(
sidHeartBeat
!=
DefaultObjectId
&&
ptHeartBeat
.
checkTime
()
)
{
try
{
shm
->
localSaveValue
(
aitHeartBeat
,
sidHeartBeat
,
maxHeartBeat
,
getId
());
ptHeartBeat
.
reset
();
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(step): (hb) "
<<
ex
<<
std
::
endl
;
}
}
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
)
{
try
{
switch
(
msg
->
type
)
{
case
UniSetTypes
:
:
Message
::
SysCommand
:
{
UniSetTypes
::
SystemMessage
sm
(
msg
);
sysCommand
(
&
sm
);
}
break
;
case
Message
:
:
SensorInfo
:
{
SensorMessage
sm
(
msg
);
sensorInfo
(
&
sm
);
}
break
;
case
Message
:
:
Timer
:
{
TimerMessage
tm
(
msg
);
timerInfo
(
&
tm
);
}
break
;
default
:
break
;
}
}
catch
(
SystemError
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(SystemError): "
<<
ex
<<
std
::
endl
;
// throw SystemError(ex);
raise
(
SIGTERM
);
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(processingMessage): catch ...
\n
"
;
}
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
sysCommand
(
UniSetTypes
::
SystemMessage
*
sm
)
{
switch
(
sm
->
command
)
{
case
SystemMessage
:
:
StartUp
:
{
waitSMReady
();
// подождать пока пройдёт инициализация датчиков
// см. activateObject()
msleep
(
initPause
);
PassiveTimer
ptAct
(
activateTimeout
);
while
(
!
activated
&&
!
ptAct
.
checkTime
()
)
{
cout
<<
myname
<<
"(sysCommand): wait activate..."
<<
endl
;
msleep
(
300
);
if
(
activated
)
break
;
}
if
(
!
activated
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(sysCommand): ************* don`t activate?! ************"
<<
endl
;
{
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
10000
);
askSensors
(
UniversalIO
::
UIONotify
);
}
askTimer
(
tmStep
,
steptime
);
startReceivers
();
if
(
sender
)
sender
->
start
();
}
case
SystemMessage
:
:
FoldUp
:
case
SystemMessage
:
:
Finish
:
askSensors
(
UniversalIO
::
UIODontNotify
);
break
;
case
SystemMessage
:
:
WatchDog
:
{
// ОПТИМИЗАЦИЯ (защита от двойного перезаказа при старте)
// Если идёт локальная работа
// (т.е. UNetExchange запущен в одном процессе с SharedMemory2)
// то обрабатывать WatchDog не надо, т.к. мы и так ждём готовности SM
// при заказе датчиков, а если SM вылетит, то вместе с этим процессом(UNetExchange)
if
(
shm
->
isLocalwork
()
)
break
;
askSensors
(
UniversalIO
::
UIONotify
);
}
break
;
case
SystemMessage
:
:
LogRotate
:
{
// переоткрываем логи
unideb
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
string
fname
=
unideb
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
unideb
.
logFile
(
fname
);
unideb
<<
myname
<<
"(sysCommand): ***************** UNIDEB LOG ROTATE *****************"
<<
std
::
endl
;
}
dlog
<<
myname
<<
"(sysCommand): logRotate"
<<
std
::
endl
;
fname
=
dlog
.
getLogFile
();
if
(
!
fname
.
empty
()
)
{
dlog
.
logFile
(
fname
);
dlog
<<
myname
<<
"(sysCommand): ***************** dlog LOG ROTATE *****************"
<<
std
::
endl
;
}
}
break
;
default
:
break
;
}
}
// ------------------------------------------------------------------------------------------
void
UNetExchange
::
askSensors
(
UniversalIO
::
UIOCommand
cmd
)
{
if
(
!
shm
->
waitSMworking
(
test_id
,
activateTimeout
,
50
)
)
{
ostringstream
err
;
err
<<
myname
<<
"(askSensors): Не дождались готовности(work) SharedMemory к работе в течение "
<<
activateTimeout
<<
" мсек"
;
dlog
[
Debug
::
CRIT
]
<<
err
.
str
()
<<
endl
;
kill
(
SIGTERM
,
getpid
());
// прерываем (перезапускаем) процесс...
throw
SystemError
(
err
.
str
());
}
}
// ------------------------------------------------------------------------------------------
void
UNetExchange
::
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
)
{
if
(
sender
)
sender
->
update
(
sm
->
id
,
sm
->
value
);
}
// ------------------------------------------------------------------------------------------
bool
UNetExchange
::
activateObject
()
{
// блокирование обработки Starsp
// пока не пройдёт инициализация датчиков
// см. sysCommand()
{
activated
=
false
;
UniSetTypes
::
uniset_mutex_lock
l
(
mutex_start
,
5000
);
UniSetObject_LT
::
activateObject
();
initIterators
();
activated
=
true
;
}
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UNetExchange
::
sigterm
(
int
signo
)
{
cerr
<<
myname
<<
": ********* SIGTERM("
<<
signo
<<
") ********"
<<
endl
;
activated
=
false
;
UniSetObject_LT
::
sigterm
(
signo
);
}
// ------------------------------------------------------------------------------------------
void
UNetExchange
::
initIterators
()
{
shm
->
initAIterator
(
aitHeartBeat
);
}
// -----------------------------------------------------------------------------
void
UNetExchange
::
help_print
(
int
argc
,
char
*
argv
[]
)
{
cout
<<
"--unet-recvpause msec - Пауза между получением пакетов. По умолчанию 10 мсек."
<<
endl
;
cout
<<
"--unet-updatepause msec - Пауза между обновлением данных в SM. По умолчанию 100 мсек."
<<
endl
;
cout
<<
"--unet-heartbeat-id - Данный процесс связан с указанным аналоговым heartbeat-дачиком."
<<
endl
;
cout
<<
"--unet-heartbeat-max - Максимальное значение heartbeat-счётчика для данного процесса. По умолчанию 10."
<<
endl
;
cout
<<
"--unet-ready-timeout - Время ожидания готовности SM к работе, мсек. (-1 - ждать 'вечно')"
<<
endl
;
cout
<<
"--unet-initPause - Задержка перед инициализацией (время на активизация процесса)"
<<
endl
;
cout
<<
"--unet-notRespondSensor - датчик связи для данного процесса "
<<
endl
;
cout
<<
"--unet-sm-ready-timeout - время на ожидание старта SM"
<<
endl
;
cout
<<
" Настройки протокола RS: "
<<
endl
;
}
// -----------------------------------------------------------------------------
UNetExchange
*
UNetExchange
::
init_unetexchange
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
icID
,
SharedMemory
*
ic
)
{
string
name
=
conf
->
getArgParam
(
"--unet-name"
,
"UNetExchange1"
);
if
(
name
.
empty
()
)
{
cerr
<<
"(unetexchange): Не задан name'"
<<
endl
;
return
0
;
}
ObjectId
ID
=
conf
->
getObjectID
(
name
);
if
(
ID
==
UniSetTypes
::
DefaultObjectId
)
{
cerr
<<
"(unetexchange): идентификатор '"
<<
name
<<
"' не найден в конф. файле!"
<<
" в секции "
<<
conf
->
getObjectsSection
()
<<
endl
;
return
0
;
}
dlog
[
Debug
::
INFO
]
<<
"(unetexchange): name = "
<<
name
<<
"("
<<
ID
<<
")"
<<
endl
;
return
new
UNetExchange
(
ID
,
icID
,
ic
);
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UNetExchange.h
deleted
100644 → 0
View file @
611033c7
#ifndef UNetExchange_H_
#define UNetExchange_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <queue>
#include <cc++/socket.h>
#include "UniSetObject_LT.h"
#include "Trigger.h"
#include "Mutex.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UNetReceiver.h"
#include "UNetSender.h"
// -----------------------------------------------------------------------------
class
UNetExchange
:
public
UniSetObject_LT
{
public
:
UNetExchange
(
UniSetTypes
::
ObjectId
objId
,
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
virtual
~
UNetExchange
();
/*! глобальная функция для инициализации объекта */
static
UNetExchange
*
init_unetexchange
(
int
argc
,
char
*
argv
[],
UniSetTypes
::
ObjectId
shmID
,
SharedMemory
*
ic
=
0
);
/*! глобальная функция для вывода help-а */
static
void
help_print
(
int
argc
,
char
*
argv
[]
);
bool
checkExistUNetHost
(
const
std
::
string
host
,
ost
::
tpport_t
port
);
protected
:
xmlNode
*
cnode
;
std
::
string
s_field
;
std
::
string
s_fvalue
;
SMInterface
*
shm
;
void
step
();
virtual
void
processingMessage
(
UniSetTypes
::
VoidMessage
*
msg
);
void
sysCommand
(
UniSetTypes
::
SystemMessage
*
msg
);
void
sensorInfo
(
UniSetTypes
::
SensorMessage
*
sm
);
void
timerInfo
(
UniSetTypes
::
TimerMessage
*
tm
);
void
askSensors
(
UniversalIO
::
UIOCommand
cmd
);
void
waitSMReady
();
virtual
bool
activateObject
();
// действия при завершении работы
virtual
void
sigterm
(
int
signo
);
void
initIterators
();
void
startReceivers
();
void
initSender
(
const
std
::
string
host
,
const
ost
::
tpport_t
port
,
UniXML_iterator
&
it
);
enum
Timer
{
tmStep
};
private
:
UNetExchange
();
bool
initPause
;
UniSetTypes
::
uniset_mutex
mutex_start
;
PassiveTimer
ptHeartBeat
;
UniSetTypes
::
ObjectId
sidHeartBeat
;
int
maxHeartBeat
;
IOController
::
AIOStateList
::
iterator
aitHeartBeat
;
UniSetTypes
::
ObjectId
test_id
;
int
steptime
;
/*!< периодичность вызова step, [мсек] */
bool
activated
;
int
activateTimeout
;
typedef
std
::
list
<
UNetReceiver
*>
ReceiverList
;
ReceiverList
recvlist
;
bool
no_sender
;
/*!< флаг отключения посылки сообщений */
UNetSender
*
sender
;
};
// -----------------------------------------------------------------------------
#endif // UNetExchange_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UNetReceiver.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UNetReceiver.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
bool
UNetReceiver
::
PacketCompare
::
operator
()(
const
UniSetUDP
::
UDPMessage
&
lhs
,
const
UniSetUDP
::
UDPMessage
&
rhs
)
const
{
// if( lhs.msg.header.num == rhs.msg.header.num )
// return (lhs.msg < rhs.msg);
return
lhs
.
msg
.
header
.
num
>
rhs
.
msg
.
header
.
num
;
}
// ------------------------------------------------------------------------------------------
UNetReceiver
::
UNetReceiver
(
const
std
::
string
s_host
,
const
ost
::
tpport_t
port
,
SMInterface
*
smi
)
:
shm
(
smi
),
recvpause
(
10
),
updatepause
(
100
),
udp
(
0
),
recvTimeout
(
5000
),
lostTimeout
(
5000
),
lostPackets
(
0
),
activated
(
false
),
r_thr
(
0
),
u_thr
(
0
),
pnum
(
0
),
maxDifferens
(
1000
),
waitClean
(
false
),
rnum
(
0
),
maxProcessingCount
(
100
),
icache
(
200
),
cache_init_ok
(
false
)
{
{
ostringstream
s
;
s
<<
"("
<<
s_host
<<
":"
<<
port
<<
")"
;
myname
=
s
.
str
();
}
try
{
// ost::IPV4Cidr ci(s_host.c_str());
// addr = ci.getBroadcast();
// cerr << "****************** addr: " << addr << endl;
addr
=
s_host
.
c_str
();
udp
=
new
ost
::
UDPDuplex
(
addr
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
();
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(init): "
<<
s
.
str
()
<<
std
::
endl
;
throw
SystemError
(
s
.
str
());
}
r_thr
=
new
ThreadCreator
<
UNetReceiver
>
(
this
,
&
UNetReceiver
::
receive
);
u_thr
=
new
ThreadCreator
<
UNetReceiver
>
(
this
,
&
UNetReceiver
::
update
);
ptRecvTimeout
.
setTiming
(
recvTimeout
);
}
// -----------------------------------------------------------------------------
UNetReceiver
::~
UNetReceiver
()
{
delete
r_thr
;
delete
u_thr
;
delete
udp
;
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
setReceiveTimeout
(
timeout_t
msec
)
{
recvTimeout
=
msec
;
ptRecvTimeout
.
setTiming
(
msec
);
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
setLostTimeout
(
timeout_t
msec
)
{
lostTimeout
=
msec
;
ptLostTimeout
.
setTiming
(
msec
);
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
setReceivePause
(
timeout_t
msec
)
{
recvpause
=
msec
;
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
setUpdatePause
(
timeout_t
msec
)
{
updatepause
=
msec
;
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
setMaxProcessingCount
(
int
set
)
{
maxProcessingCount
=
set
;
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
setMaxDifferens
(
unsigned
long
set
)
{
maxDifferens
=
set
;
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
start
()
{
if
(
!
activated
)
{
activated
=
true
;
u_thr
->
start
();
r_thr
->
start
();
}
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
update
()
{
cerr
<<
"******************* udpate start"
<<
endl
;
while
(
activated
)
{
try
{
real_update
();
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(update): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(update): catch ..."
<<
std
::
endl
;
}
msleep
(
updatepause
);
}
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
real_update
()
{
UniSetUDP
::
UDPMessage
p
;
// обрабатываем пока, очередь либо не опустеет
// либо обнаружится "дырка" в последовательности
// но при этом обрабатываем не больше maxProcessingCount
// за один раз..
int
k
=
maxProcessingCount
;
while
(
k
>
0
)
{
{
// lock qpack
uniset_mutex_lock
l
(
packMutex
);
if
(
qpack
.
empty
()
)
return
;
p
=
qpack
.
top
();
unsigned
long
sub
=
labs
(
p
.
msg
.
header
.
num
-
pnum
);
if
(
pnum
>
0
)
{
// если sub > maxDifferens
// значит это просто "разрыв"
// и нам ждать lostTimeout не надо
// сразу начинаем обрабатывать новые пакеты
// а если > 1 && < maxDifferens
// значит это временная "дырка"
// и надо подождать lostTimeout
// чтобы констатировать потерю пакета..
if
(
sub
>
1
&&
sub
<
maxDifferens
)
{
if
(
!
ptLostTimeout
.
checkTime
()
)
return
;
lostPackets
++
;
}
else
if
(
p
.
msg
.
header
.
num
==
pnum
)
{
/* а что делать если идут повторные пакеты ?!
* для надёжности лучше обрабатывать..
* для "оптимизации".. лучше игнорировать
*/
qpack
.
pop
();
// пока выбрали вариант "оптимизации"
continue
;
}
}
ptLostTimeout
.
reset
();
// удаляем из очереди, только если
// всё в порядке с последовательностью..
qpack
.
pop
();
pnum
=
p
.
msg
.
header
.
num
;
}
// unlock qpack
k
--
;
// cerr << myname << "(update): " << p.msg.header << endl;
initCache
(
p
,
!
cache_init_ok
);
for
(
size_t
i
=
0
;
i
<
p
.
msg
.
header
.
dcount
;
i
++
)
{
try
{
UniSetUDP
::
UDPData
&
d
=
p
.
msg
.
dat
[
i
];
ItemInfo
&
ii
(
icache
[
i
]);
if
(
ii
.
id
!=
d
.
id
)
{
dlog
[
Debug
::
WARN
]
<<
myname
<<
"(update): reinit cache for sid="
<<
d
.
id
<<
endl
;
ii
.
id
=
d
.
id
;
shm
->
initAIterator
(
ii
.
ait
);
shm
->
initDIterator
(
ii
.
dit
);
}
if
(
ii
.
iotype
==
UniversalIO
::
DigitalInput
)
shm
->
localSaveState
(
ii
.
dit
,
d
.
id
,
d
.
val
,
shm
->
ID
());
else
if
(
ii
.
iotype
==
UniversalIO
::
AnalogInput
)
shm
->
localSaveValue
(
ii
.
ait
,
d
.
id
,
d
.
val
,
shm
->
ID
());
else
if
(
ii
.
iotype
==
UniversalIO
::
AnalogOutput
)
shm
->
localSetValue
(
ii
.
ait
,
d
.
id
,
d
.
val
,
shm
->
ID
());
else
if
(
ii
.
iotype
==
UniversalIO
::
DigitalOutput
)
shm
->
localSetState
(
ii
.
dit
,
d
.
id
,
d
.
val
,
shm
->
ID
());
else
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(update): Unknown iotype for sid="
<<
d
.
id
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(update): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(update): catch ..."
<<
std
::
endl
;
}
}
}
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
receive
()
{
cerr
<<
"******************* receive start"
<<
endl
;
ptRecvTimeout
.
setTiming
(
recvTimeout
);
while
(
activated
)
{
try
{
if
(
recv
()
)
ptRecvTimeout
.
reset
();
}
catch
(
ost
::
SockException
&
e
)
{
cerr
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(poll): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
cerr
<<
myname
<<
"(poll): catch ..."
<<
std
::
endl
;
}
msleep
(
recvpause
);
}
cerr
<<
"************* execute FINISH **********"
<<
endl
;
}
// -----------------------------------------------------------------------------
bool
UNetReceiver
::
recv
()
{
if
(
!
udp
->
isInputReady
(
recvTimeout
)
)
return
false
;
size_t
ret
=
udp
->
UDPReceive
::
receive
(
&
(
pack
.
msg
),
sizeof
(
pack
.
msg
));
if
(
ret
<
sizeof
(
UniSetUDP
::
UDPHeader
)
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(receive): FAILED header ret="
<<
ret
<<
" sizeof="
<<
sizeof
(
UniSetUDP
::
UDPHeader
)
<<
endl
;
return
false
;
}
size_t
sz
=
pack
.
msg
.
header
.
dcount
*
sizeof
(
UniSetUDP
::
UDPData
)
+
sizeof
(
UniSetUDP
::
UDPHeader
);
if
(
ret
<
sz
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(receive): FAILED data ret="
<<
ret
<<
" sizeof="
<<
sz
<<
" packnum="
<<
pack
.
msg
.
header
.
num
<<
endl
;
return
false
;
}
if
(
rnum
>
0
&&
labs
(
pack
.
msg
.
header
.
num
-
rnum
)
>
maxDifferens
)
{
/* А что делать если мы уже ждём и ещё не "разгребли предыдущее".. а тут уже повторный "разрыв"
* Можно откинуть всё.. что сложили во временную очередь и заново "копить" (но тогда теряем информацию)
* А можно породолжать складывать во временную, но тогда есть риск "никогда" не разгрести временную
* очередь, при "частых обрывах". Потому-что update будет на каждом разрыве ждать ещё lostTimeout..
*/
// Пока выбираю.. чистить qtmp. Это будет соотвествовать логике работы с картами у которых ограничен буфер приёма.
// Обычно "кольцевой". Т.е. если не успели обработать и "вынуть" из буфера информацию.. он будет переписан новыми данными
if
(
waitClean
)
{
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(receive): reset qtmp.."
<<
endl
;
while
(
!
qtmp
.
empty
()
)
qtmp
.
pop
();
}
waitClean
=
true
;
}
rnum
=
pack
.
msg
.
header
.
num
;
// cerr << myname << "(receive): recv DATA OK. ret=" << ret << " sizeof=" << sz
// << " header: " << pack.msg.header
// << " waitClean=" << waitClean
// << endl;
{
// lock qpack
uniset_mutex_lock
l
(
packMutex
,
500
);
if
(
!
waitClean
)
{
qpack
.
push
(
pack
);
return
true
;
}
if
(
!
qpack
.
empty
()
)
{
// cerr << myname << "(receive): copy to qtmp..."
// << " header: " << pack.msg.header
// << endl;
qtmp
.
push
(
pack
);
}
else
{
// cerr << myname << "(receive): copy from qtmp..." << endl;
// очередь освободилась..
// то копируем в неё всё что набралось...
while
(
!
qtmp
.
empty
()
)
{
qpack
.
push
(
qtmp
.
top
());
qtmp
.
pop
();
}
// не забываем и текущий поместить в очередь..
qpack
.
push
(
pack
);
waitClean
=
false
;
}
}
// unlock qpack
return
true
;
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
initIterators
()
{
for
(
ItemVec
::
iterator
it
=
icache
.
begin
();
it
!=
icache
.
end
();
++
it
)
{
shm
->
initAIterator
(
it
->
ait
);
shm
->
initDIterator
(
it
->
dit
);
}
}
// -----------------------------------------------------------------------------
void
UNetReceiver
::
initCache
(
UniSetUDP
::
UDPMessage
&
pack
,
bool
force
)
{
if
(
!
force
&&
pack
.
msg
.
header
.
dcount
==
icache
.
size
()
)
return
;
dlog
[
Debug
::
INFO
]
<<
myname
<<
": init icache.."
<<
endl
;
cache_init_ok
=
true
;
icache
.
resize
(
pack
.
msg
.
header
.
dcount
);
for
(
size_t
i
=
0
;
i
<
icache
.
size
();
i
++
)
{
ItemInfo
&
d
(
icache
[
i
]);
if
(
d
.
id
!=
pack
.
msg
.
dat
[
i
].
id
)
{
d
.
id
=
pack
.
msg
.
dat
[
i
].
id
;
d
.
iotype
=
conf
->
getIOType
(
d
.
id
);
shm
->
initAIterator
(
d
.
ait
);
shm
->
initDIterator
(
d
.
dit
);
}
}
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UNetReceiver.h
deleted
100644 → 0
View file @
611033c7
#ifndef UNetReceiver_H_
#define UNetReceiver_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <queue>
#include <cc++/socket.h>
#include "UniSetObject_LT.h"
#include "Trigger.h"
#include "Mutex.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UDPPacket.h"
// -----------------------------------------------------------------------------
/* Основная идея: сделать проверку очерёдности пакетов, но при этом использовать UDP.
* ===============
* Собственно реализация сделана так:
* В данных передаётся номер пакета. На случай если несколько пакетов придут не в той последовательности
* что были посланы, сделана очередь с приоритетом. В качестве приориета используется номер пакета
* (чем меньше тем старше). При этом обработка ведётся только тех пакетов, которые идут "подряд",
* как только встречается "дырка" происходит ожидание её "заполения". Если в течение времени (lostTimeout)
* "дырка" не исчезает, увеличивается счётчик потерянных пакетов и обработка продолжается дальше..
* Всё это реализовано в функции UNetReceiver::real_update()
*
* КЭШ
* ===
* Для оптимизации работы с SM, т.к. в пакетах приходят только пары [id,value] сделан кэш итераторов.
* Кэш расчитан на то, что принимаемые пакеты всегда имеют одну и ту же длину и последовательность.
* Идея проста: сделан вектор размером с количество принимаемых данных. В векторе хранятся итераторы (и всё что необходимо).
* Порядокый номер данных в пакете является индексом в кэше.
* Для защиты от изменения поседовательности внутри пакета, в кэше хранится ID сохраняемого датчика, и если он не совпадёт с тем,
* ID который пришёл в пакете - элемент кэша обновляется.
* Если количество пришедших данных не совпадают с размером кэша.. кэш обновляется.
*
* Обработка сбоя или переполнения счётчика пакетов(перехода через максимум)
* =========================================================================
* Для защиты от сбоя счётика сделана следующая логика:
* Если номер очередного пришедшего пакета отличается от последнего обработанного на maxDifferens, то считается,
* что произошёл сбой счётчика и происходит ожидание пока функция update, не обработает основную очередь полностью.
* При этом принимаемые пакеты складываются во временную очередь qtmp. Как только основная очередь пустеет,
* в неё копируется всё накопленное во временной очереди..и опять идёт штатная обработка.
* Если во время "ожидания" опять происходит "разрыв" в номерах пакетов, то временная очередь чиститься
* и данные которые в ней были теряются! Аналог ограниченного буфера (у любых карт), когда новые данные
* затирают старые, если их не успели вынуть и обработать.
* \todo Сделать защиту от бесконечного ожидания "очистки" основной очереди.
*/
// -----------------------------------------------------------------------------
class
UNetReceiver
{
public
:
UNetReceiver
(
const
std
::
string
host
,
const
ost
::
tpport_t
port
,
SMInterface
*
smi
);
~
UNetReceiver
();
void
start
();
void
receive
();
void
update
();
inline
bool
isRecvOK
(){
return
ptRecvTimeout
.
checkTime
();
}
inline
unsigned
long
getLostPacketsNum
(){
return
lostPackets
;
}
void
setReceiveTimeout
(
timeout_t
msec
);
void
setReceivePause
(
timeout_t
msec
);
void
setUpdatePause
(
timeout_t
msec
);
void
setLostTimeout
(
timeout_t
msec
);
void
setMaxDifferens
(
unsigned
long
set
);
void
setMaxProcessingCount
(
int
set
);
inline
ost
::
IPV4Address
getAddress
(){
return
addr
;
}
inline
ost
::
tpport_t
getPort
(){
return
port
;
}
protected
:
SMInterface
*
shm
;
bool
recv
();
void
step
();
void
real_update
();
void
initIterators
();
private
:
UNetReceiver
();
int
recvpause
;
/*!< пауза меджду приёмами пакетов, [мсек] */
int
updatepause
;
/*!< переодичность обновления данных в SM, [мсек] */
ost
::
UDPReceive
*
udp
;
ost
::
IPV4Address
addr
;
ost
::
tpport_t
port
;
std
::
string
myname
;
UniSetTypes
::
uniset_mutex
pollMutex
;
PassiveTimer
ptRecvTimeout
;
timeout_t
recvTimeout
;
timeout_t
lostTimeout
;
PassiveTimer
ptLostTimeout
;
unsigned
long
lostPackets
;
/*!< счётчик потерянных пакетов */
bool
activated
;
ThreadCreator
<
UNetReceiver
>*
r_thr
;
// receive thread
ThreadCreator
<
UNetReceiver
>*
u_thr
;
// update thread
// функция определения приоритетного сообщения для обработки
struct
PacketCompare
:
public
std
::
binary_function
<
UniSetUDP
::
UDPMessage
,
UniSetUDP
::
UDPMessage
,
bool
>
{
bool
operator
()(
const
UniSetUDP
::
UDPMessage
&
lhs
,
const
UniSetUDP
::
UDPMessage
&
rhs
)
const
;
};
typedef
std
::
priority_queue
<
UniSetUDP
::
UDPMessage
,
std
::
vector
<
UniSetUDP
::
UDPMessage
>
,
PacketCompare
>
PacketQueue
;
PacketQueue
qpack
;
/*!< очередь принятых пакетов (отсортированных по возрастанию номера пакета) */
UniSetUDP
::
UDPMessage
pack
;
/*!< просто буфер для получения очереlного сообщения */
UniSetTypes
::
uniset_mutex
packMutex
;
/*!< mutex для работы с очередью */
unsigned
long
pnum
;
/*!< текущий номер обработанного сообщения, для проверки непрерывности последовательности пакетов */
/*! максимальная разница межд номерами пакетов, при которой считается, что счётчик пакетов
* прошёл через максимум или сбился...
*/
unsigned
long
maxDifferens
;
PacketQueue
qtmp
;
/*!< очередь на время обработки(очистки) основной очереди */
bool
waitClean
;
/*!< флаг означающий, что ждём очистики очереди до конца */
unsigned
long
rnum
;
/*!< текущий номер принятого сообщения, для проверки "переполнения" или "сбоя" счётчика */
int
maxProcessingCount
;
/*! максимальное число обрабатываемых за один раз сообщений */
struct
ItemInfo
{
long
id
;
IOController
::
AIOStateList
::
iterator
ait
;
IOController
::
DIOStateList
::
iterator
dit
;
UniversalIO
::
IOTypes
iotype
;
};
typedef
std
::
vector
<
ItemInfo
>
ItemVec
;
ItemVec
icache
;
/*!< кэш итераторов */
bool
cache_init_ok
;
void
initCache
(
UniSetUDP
::
UDPMessage
&
pack
,
bool
force
=
false
);
};
// -----------------------------------------------------------------------------
#endif // UNetReceiver_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/UNetSender.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "Exceptions.h"
#include "Extensions.h"
#include "UNetSender.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
UNetSender
::
UNetSender
(
const
std
::
string
s_host
,
const
ost
::
tpport_t
port
,
SMInterface
*
smi
,
const
std
::
string
s_f
,
const
std
::
string
s_val
,
SharedMemory
*
ic
)
:
s_field
(
s_f
),
s_fvalue
(
s_val
),
shm
(
smi
),
s_host
(
s_host
),
sendpause
(
150
),
activated
(
false
),
dlist
(
100
),
maxItem
(
0
),
packetnum
(
1
),
s_thr
(
0
)
{
{
ostringstream
s
;
s
<<
"("
<<
s_host
<<
":"
<<
port
<<
")"
;
myname
=
s
.
str
();
}
// определяем фильтр
// s_field = conf->getArgParam("--udp-filter-field");
// s_fvalue = conf->getArgParam("--udp-filter-value");
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): read fileter-field='"
<<
s_field
<<
"' filter-value='"
<<
s_fvalue
<<
"'"
<<
endl
;
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
"(UNetSender): UDP set to "
<<
s_host
<<
":"
<<
port
<<
endl
;
try
{
addr
=
s_host
.
c_str
();
udp
=
new
ost
::
UDPBroadcast
(
addr
,
port
);
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
throw
SystemError
(
s
.
str
());
}
s_thr
=
new
ThreadCreator
<
UNetSender
>
(
this
,
&
UNetSender
::
send
);
// -------------------------------
if
(
shm
->
isLocalwork
()
)
{
readConfiguration
();
dlist
.
resize
(
maxItem
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): dlist size = "
<<
dlist
.
size
()
<<
endl
;
}
else
ic
->
addReadItem
(
sigc
::
mem_fun
(
this
,
&
UNetSender
::
readItem
)
);
// выставляем поля, которые не меняются
mypack
.
msg
.
header
.
nodeID
=
conf
->
getLocalNode
();
mypack
.
msg
.
header
.
procID
=
shm
->
ID
();
}
// -----------------------------------------------------------------------------
UNetSender
::~
UNetSender
()
{
delete
s_thr
;
delete
udp
;
delete
shm
;
}
// -----------------------------------------------------------------------------
void
UNetSender
::
update
(
UniSetTypes
::
ObjectId
id
,
long
value
)
{
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
++
it
)
{
if
(
it
->
si
.
id
==
id
)
{
uniset_spin_lock
lock
(
it
->
val_lock
);
it
->
val
=
value
;
}
break
;
}
}
// -----------------------------------------------------------------------------
void
UNetSender
::
send
()
{
dlist
.
resize
(
maxItem
);
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(init): dlist size = "
<<
dlist
.
size
()
<<
endl
;
/*
ost::IPV4Broadcast h = s_host.c_str();
try
{
udp->setPeer(h,port);
}
catch( ost::SockException& e )
{
ostringstream s;
s << e.getString() << ": " << e.getSystemErrorString();
dlog[Debug::CRIT] << myname << "(poll): " << s.str() << endl;
throw SystemError(s.str());
}
*/
while
(
activated
)
{
try
{
real_send
();
}
catch
(
ost
::
SockException
&
e
)
{
cerr
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(
UniSetTypes
::
Exception
&
ex
)
{
cerr
<<
myname
<<
"(send): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
cerr
<<
myname
<<
"(send): catch ..."
<<
std
::
endl
;
}
msleep
(
sendpause
);
}
cerr
<<
"************* execute FINISH **********"
<<
endl
;
}
// -----------------------------------------------------------------------------
void
UNetSender
::
real_send
()
{
mypack
.
msg
.
header
.
num
=
packetnum
++
;
if
(
packetnum
>
UniSetUDP
::
MaxPacketNum
)
packetnum
=
1
;
// cout << "************* send header: " << mypack.msg.header << endl;
int
sz
=
mypack
.
byte_size
()
+
sizeof
(
UniSetUDP
::
UDPHeader
);
if
(
!
udp
->
isPending
(
ost
::
Socket
::
pendingOutput
)
)
return
;
size_t
ret
=
udp
->
send
(
(
char
*
)
&
(
mypack
.
msg
),
sz
);
if
(
ret
<
sz
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(send): FAILED ret="
<<
ret
<<
" < sizeof="
<<
sz
<<
endl
;
}
// -----------------------------------------------------------------------------
void
UNetSender
::
start
()
{
if
(
!
activated
)
{
activated
=
true
;
s_thr
->
start
();
}
}
// -----------------------------------------------------------------------------
void
UNetSender
::
readConfiguration
()
{
xmlNode
*
root
=
conf
->
getXMLSensorsSection
();
if
(
!
root
)
{
ostringstream
err
;
err
<<
myname
<<
"(readConfiguration): not found <sensors>"
;
throw
SystemError
(
err
.
str
());
}
UniXML_iterator
it
(
root
);
if
(
!
it
.
goChildren
()
)
{
std
::
cerr
<<
myname
<<
"(readConfiguration): empty <sensors>?!!"
<<
endl
;
return
;
}
for
(
;
it
.
getCurrent
();
it
.
goNext
()
)
{
if
(
check_item
(
it
)
)
initItem
(
it
);
}
}
// ------------------------------------------------------------------------------------------
bool
UNetSender
::
check_item
(
UniXML_iterator
&
it
)
{
if
(
s_field
.
empty
()
)
return
true
;
// просто проверка на не пустой field
if
(
s_fvalue
.
empty
()
&&
it
.
getProp
(
s_field
).
empty
()
)
return
false
;
// просто проверка что field = value
if
(
!
s_fvalue
.
empty
()
&&
it
.
getProp
(
s_field
)
!=
s_fvalue
)
return
false
;
return
true
;
}
// ------------------------------------------------------------------------------------------
bool
UNetSender
::
readItem
(
UniXML
&
xml
,
UniXML_iterator
&
it
,
xmlNode
*
sec
)
{
if
(
check_item
(
it
)
)
initItem
(
it
);
return
true
;
}
// ------------------------------------------------------------------------------------------
bool
UNetSender
::
initItem
(
UniXML_iterator
&
it
)
{
string
sname
(
it
.
getProp
(
"name"
)
);
string
tid
=
it
.
getProp
(
"id"
);
ObjectId
sid
;
if
(
!
tid
.
empty
()
)
{
sid
=
UniSetTypes
::
uni_atoi
(
tid
);
if
(
sid
<=
0
)
sid
=
DefaultObjectId
;
}
else
sid
=
conf
->
getSensorID
(
sname
);
if
(
sid
==
DefaultObjectId
)
{
if
(
dlog
)
dlog
[
Debug
::
CRIT
]
<<
myname
<<
"(readItem): ID not found for "
<<
sname
<<
endl
;
return
false
;
}
UItem
p
;
p
.
si
.
id
=
sid
;
p
.
si
.
node
=
conf
->
getLocalNode
();
mypack
.
addData
(
sid
,
0
);
p
.
pack_ind
=
mypack
.
size
()
-
1
;
if
(
maxItem
>=
mypack
.
size
()
)
dlist
.
resize
(
maxItem
+
10
);
dlist
[
maxItem
]
=
p
;
maxItem
++
;
if
(
dlog
.
debugging
(
Debug
::
INFO
)
)
dlog
[
Debug
::
INFO
]
<<
myname
<<
"(initItem): add "
<<
p
<<
endl
;
return
true
;
}
// ------------------------------------------------------------------------------------------
void
UNetSender
::
initIterators
()
{
DMap
::
iterator
it
=
dlist
.
begin
();
for
(
;
it
!=
dlist
.
end
();
it
++
)
{
shm
->
initDIterator
(
it
->
dit
);
shm
->
initAIterator
(
it
->
ait
);
}
}
// -----------------------------------------------------------------------------
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UNetSender
::
UItem
&
p
)
{
return
os
<<
" sid="
<<
p
.
si
.
id
;
}
// -----------------------------------------------------------------------------
extensions/UDPExchange/UNetSender.h
deleted
100644 → 0
View file @
611033c7
#ifndef UNetSender_H_
#define UNetSender_H_
// -----------------------------------------------------------------------------
#include <ostream>
#include <string>
#include <vector>
#include <cc++/socket.h>
#include "UniSetObject_LT.h"
#include "Trigger.h"
#include "Mutex.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "ThreadCreator.h"
#include "UDPPacket.h"
#include "UDPNReceiver.h"
// -----------------------------------------------------------------------------
/*
* Для защиты от потери пакета при переполнении "номера пакета".
* UNetReceiver при обнаружении "разрыва" в последовательнности, просто игнорирует пакет, обновляет счётчик
* и начинает обработку пока буфер опять не заполнится..
*/
class
UNetSender
{
public
:
UNetSender
(
const
std
::
string
host
,
const
ost
::
tpport_t
port
,
SMInterface
*
smi
,
const
std
::
string
s_field
=
""
,
const
std
::
string
s_fvalue
=
""
,
SharedMemory
*
ic
=
0
);
~
UNetSender
();
struct
UItem
{
UItem
()
:
val
(
0
)
{}
IOController_i
::
SensorInfo
si
;
IOController
::
AIOStateList
::
iterator
ait
;
IOController
::
DIOStateList
::
iterator
dit
;
UniSetTypes
::
uniset_spin_mutex
val_lock
;
int
pack_ind
;
long
val
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UItem
&
p
);
};
void
start
();
void
send
();
void
real_send
();
void
update
(
UniSetTypes
::
ObjectId
id
,
long
value
);
inline
void
setSendPause
(
int
msec
){
sendpause
=
msec
;
}
protected
:
std
::
string
s_field
;
std
::
string
s_fvalue
;
SMInterface
*
shm
;
void
initIterators
();
bool
initItem
(
UniXML_iterator
&
it
);
bool
readItem
(
UniXML
&
xml
,
UniXML_iterator
&
it
,
xmlNode
*
sec
);
void
readConfiguration
();
bool
check_item
(
UniXML_iterator
&
it
);
private
:
UNetSender
();
ost
::
UDPBroadcast
*
udp
;
ost
::
IPV4Address
addr
;
ost
::
tpport_t
port
;
std
::
string
s_host
;
std
::
string
myname
;
int
sendpause
;
bool
activated
;
UniSetUDP
::
UDPMessage
mypack
;
typedef
std
::
vector
<
UItem
>
DMap
;
DMap
dlist
;
int
maxItem
;
unsigned
long
packetnum
;
ThreadCreator
<
UNetSender
>*
s_thr
;
// send thread
};
// -----------------------------------------------------------------------------
#endif // UNetSender_H_
// -----------------------------------------------------------------------------
extensions/UDPExchange/create_links.sh
deleted
100755 → 0
View file @
611033c7
#!/bin/sh
ln
-s
-f
../../Utilities/scripts/uniset-start.sh
ln
-s
-f
../../Utilities/scripts/uniset-stop.sh stop.sh
ln
-s
-f
../../Utilities/scripts/uniset-functions.sh
ln
-s
-f
../../conf/test.xml test.xml
extensions/UDPExchange/libUniSetUDP.pc.in
deleted
100644 → 0
View file @
611033c7
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: libUniSetUDP
Description: Support library for UniSetIOControl
Requires: libUniSetExtensions
Version: @VERSION@
Libs: -L${libdir} -lUniSetUDP
Cflags: -I${includedir}/uniset
extensions/UDPExchange/start_fg.sh
deleted
100755 → 0
View file @
611033c7
#!/bin/sh
uniset-start.sh
-f
./uniset-udpexchange
--udp-name
UDPExchange
--udp-host
192.168.56.255
\
--udp-broadcast
1
--udp-polltime
1000
\
--confile
test.xml
\
--dlog-add-levels
info,crit,warn
# --udp-filter-field udp --udp-filter-value 1 \
extensions/UDPExchange/start_fg2.sh
deleted
100755 → 0
View file @
611033c7
#!/bin/sh
uniset-start.sh
-f
./uniset-udpexchange
--udp-name
UDPExchange2
--udp-host
localhost
--udp-port
2049
\
--confile
test.xml
\
--udp-filter-field
udp
--udp-filter-value
2
\
--udp-ip
\
--dlog-add-levels
info,crit,warn
extensions/UDPExchange/start_fg_receiver.sh
deleted
100755 → 0
View file @
611033c7
#!/bin/sh
uniset-start.sh
-f
./uniset-udpreceiver
--udp-name
UDPExchange
\
--udp-host
192.168.56.255
--udp-port
3000
\
--confile
test.xml
\
--udp-filter-field
udp
--udp-filter-value
1
\
--dlog-add-levels
info,crit,warn
extensions/UDPExchange/start_fg_sender.sh
deleted
100755 → 0
View file @
611033c7
#!/bin/sh
uniset-start.sh
-f
./uniset-udpsender
--udp-name
UDPExchange
\
--udp-host
192.168.56.255
--udp-port
2050
--udp-broadcast
1
\
--udp-sendtime
2000
\
--confile
test.xml
\
--dlog-add-levels
info,crit,warn
# --udp-filter-field udp --udp-filter-value 1 \
extensions/UDPExchange/start_fg_unetexchange.sh
deleted
100755 → 0
View file @
611033c7
#!/bin/sh
uniset-start.sh
-f
./uniset-unetexchange
--unet-name
UNetExchange
\
--confile
test.xml
\
--unet-filter-field
rs
--unet-filter-value
2
--unet-maxdifferense
40
\
--dlog-add-levels
info,crit,warn
extensions/UDPExchange/udpexchange.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "ObjectsActivator.h"
#include "Extensions.h"
#include "UDPExchange.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
int
main
(
int
argc
,
char
**
argv
)
{
try
{
if
(
argc
>
1
&&
(
!
strcmp
(
argv
[
1
],
"--help"
)
||
!
strcmp
(
argv
[
1
],
"-h"
))
)
{
cout
<<
"--smemory-id objectName - SharedMemory objectID. Default: read from <SharedMemory>"
<<
endl
;
cout
<<
"--confile filename - configuration file. Default: configure.xml"
<<
endl
;
cout
<<
"--udp-logfile filename - logfilename. Default: udpexchange.log"
<<
endl
;
cout
<<
endl
;
UDPExchange
::
help_print
(
argc
,
argv
);
return
0
;
}
string
confile
=
UniSetTypes
::
getArgParam
(
"--confile"
,
argc
,
argv
,
"configure.xml"
);
conf
=
new
Configuration
(
argc
,
argv
,
confile
);
string
logfilename
(
conf
->
getArgParam
(
"--udp-logfile"
));
if
(
logfilename
.
empty
()
)
logfilename
=
"udpexchange.log"
;
conf
->
initDebug
(
dlog
,
"dlog"
);
std
::
ostringstream
logname
;
string
dir
(
conf
->
getLogDir
());
logname
<<
dir
<<
logfilename
;
unideb
.
logFile
(
logname
.
str
()
);
dlog
.
logFile
(
logname
.
str
()
);
ObjectId
shmID
=
DefaultObjectId
;
string
sID
=
conf
->
getArgParam
(
"--smemory-id"
);
if
(
!
sID
.
empty
()
)
shmID
=
conf
->
getControllerID
(
sID
);
else
shmID
=
getSharedMemoryID
();
if
(
shmID
==
DefaultObjectId
)
{
cerr
<<
sID
<<
"? SharedMemoryID not found in "
<<
conf
->
getControllersSection
()
<<
" section"
<<
endl
;
return
1
;
}
UDPExchange
*
rs
=
UDPExchange
::
init_udpexchange
(
argc
,
argv
,
shmID
);
if
(
!
rs
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpexchange): init не прошёл..."
<<
endl
;
return
1
;
}
ObjectsActivator
act
;
act
.
addObject
(
static_cast
<
class
UniSetObject
*>
(
rs
));
SystemMessage
sm
(
SystemMessage
::
StartUp
);
act
.
broadcast
(
sm
.
transport_msg
()
);
unideb
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
unideb
[
Debug
::
ANY
]
<<
"(main): -------------- UDP Exchange START -------------------------
\n\n
"
;
dlog
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
dlog
[
Debug
::
ANY
]
<<
"(main): -------------- UDP Exchange START -------------------------
\n\n
"
;
act
.
run
(
false
);
// msleep(500);
// rs->execute();
return
0
;
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpexchange): "
<<
ex
<<
std
::
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpexchange): catch ..."
<<
std
::
endl
;
}
return
1
;
}
extensions/UDPExchange/udpreceiver.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "ObjectsActivator.h"
#include "Extensions.h"
#include "UDPReceiver.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
int
main
(
int
argc
,
char
**
argv
)
{
try
{
if
(
argc
>
1
&&
(
!
strcmp
(
argv
[
1
],
"--help"
)
||
!
strcmp
(
argv
[
1
],
"-h"
))
)
{
cout
<<
"--smemory-id objectName - SharedMemory objectID. Default: read from <SharedMemory>"
<<
endl
;
cout
<<
"--confile filename - configuration file. Default: configure.xml"
<<
endl
;
cout
<<
"--udp-logfile filename - logfilename. Default: udpexchange.log"
<<
endl
;
cout
<<
endl
;
UDPReceiver
::
help_print
(
argc
,
argv
);
return
0
;
}
string
confile
=
UniSetTypes
::
getArgParam
(
"--confile"
,
argc
,
argv
,
"configure.xml"
);
conf
=
new
Configuration
(
argc
,
argv
,
confile
);
string
logfilename
(
conf
->
getArgParam
(
"--udp-logfile"
));
if
(
logfilename
.
empty
()
)
logfilename
=
"udpexchange.log"
;
conf
->
initDebug
(
dlog
,
"dlog"
);
std
::
ostringstream
logname
;
string
dir
(
conf
->
getLogDir
());
logname
<<
dir
<<
logfilename
;
unideb
.
logFile
(
logname
.
str
()
);
dlog
.
logFile
(
logname
.
str
()
);
ObjectId
shmID
=
DefaultObjectId
;
string
sID
=
conf
->
getArgParam
(
"--smemory-id"
);
if
(
!
sID
.
empty
()
)
shmID
=
conf
->
getControllerID
(
sID
);
else
shmID
=
getSharedMemoryID
();
if
(
shmID
==
DefaultObjectId
)
{
cerr
<<
sID
<<
"? SharedMemoryID not found in "
<<
conf
->
getControllersSection
()
<<
" section"
<<
endl
;
return
1
;
}
UDPReceiver
*
udp
=
UDPReceiver
::
init_udpreceiver
(
argc
,
argv
,
shmID
);
if
(
!
udp
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpreceiver): init не прошёл..."
<<
endl
;
return
1
;
}
ObjectsActivator
act
;
act
.
addObject
(
static_cast
<
class
UniSetObject
*>
(
udp
));
SystemMessage
sm
(
SystemMessage
::
StartUp
);
act
.
broadcast
(
sm
.
transport_msg
()
);
unideb
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
unideb
[
Debug
::
ANY
]
<<
"(main): -------------- UDPRecevier START -------------------------
\n\n
"
;
dlog
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
dlog
[
Debug
::
ANY
]
<<
"(main): -------------- UDPReceiver START -------------------------
\n\n
"
;
act
.
run
(
false
);
// msleep(500);
// rs->execute();
return
0
;
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpexchange): "
<<
ex
<<
std
::
endl
;
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
();
dlog
[
Debug
::
CRIT
]
<<
s
.
str
()
<<
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpexchange): catch ..."
<<
std
::
endl
;
}
return
1
;
}
extensions/UDPExchange/udpsender.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "ObjectsActivator.h"
#include "Extensions.h"
#include "UDPSender.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
int
main
(
int
argc
,
char
**
argv
)
{
try
{
if
(
argc
>
1
&&
(
!
strcmp
(
argv
[
1
],
"--help"
)
||
!
strcmp
(
argv
[
1
],
"-h"
))
)
{
cout
<<
"--smemory-id objectName - SharedMemory objectID. Default: read from <SharedMemory>"
<<
endl
;
cout
<<
"--confile filename - configuration file. Default: configure.xml"
<<
endl
;
cout
<<
"--udp-logfile filename - logfilename. Default: udpexchange.log"
<<
endl
;
cout
<<
endl
;
UDPSender
::
help_print
(
argc
,
argv
);
return
0
;
}
string
confile
=
UniSetTypes
::
getArgParam
(
"--confile"
,
argc
,
argv
,
"configure.xml"
);
conf
=
new
Configuration
(
argc
,
argv
,
confile
);
string
logfilename
(
conf
->
getArgParam
(
"--udp-logfile"
));
if
(
logfilename
.
empty
()
)
logfilename
=
"udpexchange.log"
;
conf
->
initDebug
(
dlog
,
"dlog"
);
std
::
ostringstream
logname
;
string
dir
(
conf
->
getLogDir
());
logname
<<
dir
<<
logfilename
;
unideb
.
logFile
(
logname
.
str
()
);
dlog
.
logFile
(
logname
.
str
()
);
ObjectId
shmID
=
DefaultObjectId
;
string
sID
=
conf
->
getArgParam
(
"--smemory-id"
);
if
(
!
sID
.
empty
()
)
shmID
=
conf
->
getControllerID
(
sID
);
else
shmID
=
getSharedMemoryID
();
if
(
shmID
==
DefaultObjectId
)
{
cerr
<<
sID
<<
"? SharedMemoryID not found in "
<<
conf
->
getControllersSection
()
<<
" section"
<<
endl
;
return
1
;
}
UDPSender
*
udp
=
UDPSender
::
init_udpsender
(
argc
,
argv
,
shmID
);
if
(
!
udp
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpsender): init не прошёл..."
<<
endl
;
return
1
;
}
ObjectsActivator
act
;
act
.
addObject
(
static_cast
<
class
UniSetObject
*>
(
udp
));
SystemMessage
sm
(
SystemMessage
::
StartUp
);
act
.
broadcast
(
sm
.
transport_msg
()
);
unideb
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
unideb
[
Debug
::
ANY
]
<<
"(main): -------------- UDPSender START -------------------------
\n\n
"
;
dlog
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
dlog
[
Debug
::
ANY
]
<<
"(main): -------------- UDPSender START -------------------------
\n\n
"
;
act
.
run
(
false
);
// msleep(500);
// rs->execute();
return
0
;
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpsender): "
<<
ex
<<
std
::
endl
;
}
catch
(
ost
::
SockException
&
e
)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpsender): "
<<
e
.
getSystemErrorString
()
<<
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
"(udpsender): catch ..."
<<
std
::
endl
;
}
return
1
;
}
extensions/UDPExchange/unetexchange.cc
deleted
100644 → 0
View file @
611033c7
#include <sstream>
#include "ObjectsActivator.h"
#include "Extensions.h"
#include "UNetExchange.h"
// -----------------------------------------------------------------------------
using
namespace
std
;
using
namespace
UniSetTypes
;
using
namespace
UniSetExtensions
;
// -----------------------------------------------------------------------------
int
main
(
int
argc
,
char
**
argv
)
{
try
{
if
(
argc
>
1
&&
(
!
strcmp
(
argv
[
1
],
"--help"
)
||
!
strcmp
(
argv
[
1
],
"-h"
))
)
{
cout
<<
"--smemory-id objectName - SharedMemory objectID. Default: read from <SharedMemory>"
<<
endl
;
cout
<<
"--confile filename - configuration file. Default: configure.xml"
<<
endl
;
cout
<<
"--unet-logfile filename - logfilename. Default: udpexchange.log"
<<
endl
;
cout
<<
endl
;
UNetExchange
::
help_print
(
argc
,
argv
);
return
0
;
}
string
confile
=
UniSetTypes
::
getArgParam
(
"--confile"
,
argc
,
argv
,
"configure.xml"
);
conf
=
new
Configuration
(
argc
,
argv
,
confile
);
string
logfilename
(
conf
->
getArgParam
(
"--unet-logfile"
));
if
(
logfilename
.
empty
()
)
logfilename
=
"udpexchange.log"
;
conf
->
initDebug
(
dlog
,
"dlog"
);
std
::
ostringstream
logname
;
string
dir
(
conf
->
getLogDir
());
logname
<<
dir
<<
logfilename
;
unideb
.
logFile
(
logname
.
str
()
);
dlog
.
logFile
(
logname
.
str
()
);
ObjectId
shmID
=
DefaultObjectId
;
string
sID
=
conf
->
getArgParam
(
"--smemory-id"
);
if
(
!
sID
.
empty
()
)
shmID
=
conf
->
getControllerID
(
sID
);
else
shmID
=
getSharedMemoryID
();
if
(
shmID
==
DefaultObjectId
)
{
cerr
<<
sID
<<
"? SharedMemoryID not found in "
<<
conf
->
getControllersSection
()
<<
" section"
<<
endl
;
return
1
;
}
UNetExchange
*
unet
=
UNetExchange
::
init_unetexchange
(
argc
,
argv
,
shmID
);
if
(
!
unet
)
{
dlog
[
Debug
::
CRIT
]
<<
"(unetexchange): init failed.."
<<
endl
;
return
1
;
}
ObjectsActivator
act
;
act
.
addObject
(
static_cast
<
class
UniSetObject
*>
(
unet
));
SystemMessage
sm
(
SystemMessage
::
StartUp
);
act
.
broadcast
(
sm
.
transport_msg
()
);
unideb
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
unideb
[
Debug
::
ANY
]
<<
"(main): -------------- UDPRecevier START -------------------------
\n\n
"
;
dlog
(
Debug
::
ANY
)
<<
"
\n\n\n
"
;
dlog
[
Debug
::
ANY
]
<<
"(main): -------------- UDPReceiver START -------------------------
\n\n
"
;
act
.
run
(
false
);
return
0
;
}
catch
(
Exception
&
ex
)
{
dlog
[
Debug
::
CRIT
]
<<
"(unetexchange): "
<<
ex
<<
std
::
endl
;
}
catch
(
ost
::
SockException
&
e
)
{
ostringstream
s
;
s
<<
e
.
getString
()
<<
": "
<<
e
.
getSystemErrorString
();
dlog
[
Debug
::
CRIT
]
<<
s
.
str
()
<<
endl
;
}
catch
(...)
{
dlog
[
Debug
::
CRIT
]
<<
"(unetexchange): catch ..."
<<
std
::
endl
;
}
return
1
;
}
extensions/UNet2/UDPPacket.h
View file @
25628e82
...
...
@@ -59,8 +59,6 @@ namespace UniSetUDP
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
UDPMessage
&
p
);
};
}
// -----------------------------------------------------------------------------
#endif // UDPPacket_H_
...
...
extensions/UNet2/UNetExchange.h
View file @
25628e82
...
...
@@ -14,6 +14,37 @@
#include "UNetReceiver.h"
#include "UNetSender.h"
// -----------------------------------------------------------------------------
/*!
\page pageUNetExchange2 Сетевой обмен на основе UDP (UNet2)
\par Обмен построен на основе протокола UDP.
Основная идея заключается в том, что каждый узел на порту равном своему ID
посылает в сеть UDP-пакеты содержащие данные считанные из локальной SM. Формат данных - это набор
пар "id - value". Другие узлы принимают их. Помимо этого процесс, данный процесс запускает
по потоку приёма для каждого другого узла и ловит пакеты от них, сохраняя данные в SM.
\par При своём старте процесс считывает из секции <nodes> список узлов с которыми необходимо вести обмен.
Открывает по потоку приёма на каждый узел и поток передачи для своих данных. А так же параметры
своего узла.
\par Пример конфигурирования
По умолчанию при считывании используются свойства \a ip и \a id - в качестве порта.
Но можно переопределять эти параметры, при помощи указания \a unet_port и/или \a unet_ip.
\code
<nodes port="2809">
<item ip="127.0.0.1" name="LocalhostNode" textname="Локальный узел" unet_ignore="1" unet_port="3000" unet_ip="192.168.56.1">
<iocards>
...
</iocards>
</item>
<item ip="192.168.56.10" name="Node1" textname="Node1" unet_port="3001" unet_ip="192.168.56.2"/>
<item ip="192.168.56.11" name="Node2" textname="Node2" unet_port="3002" unet_ip="192.168.56.3"/>
</nodes>
\endcode
*/
// -----------------------------------------------------------------------------
class
UNetExchange
:
public
UniSetObject_LT
{
...
...
extensions/UniNetwork/UniExchange.h
View file @
25628e82
...
...
@@ -9,6 +9,29 @@
#include "SharedMemory.h"
#include "PassiveTimer.h"
// -----------------------------------------------------------------------------
/*!
\page pageUniExchange Обмен между узлами на основе TCP/IP (UniNet).
\par Обмен построен на основе функций IOControl-ера получения списка дискретных
и аналоговых датчиков. Работает через удалённые CORBA-вызовы.
\par Процесс считывает из конфигурационного файла список узлов которые необходимо
опрашивать (точнее список IOControl-еров), запускается поток обмена, в котором
эти узлы ПОСЛЕДОВАТЕЛЬНО опрашиваются..
\par Пример записи в конфигурационном файле для опроса пяти узлов...
\code
<UniExchange name="UniExchange">
<item name="UniExchange2" node="Node2"/>
<item id="3001" node_id="Node2"/>
<item name="UniExchange3" node="Node3"/>
<item name="UniExchange4" node="Node4"/>
<item name="UniExchange5" node="Node5"/>
</UniExchange>
\endcode
Запись можно делать по "id" или по "name"
*/
// -----------------------------------------------------------------------------
class
UniExchange
:
public
IOController
{
...
...
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