Line data Source code
1 :
2 : /* Copyright (c) 2010-2014, Stefan Eilemann <eile@eyescale.ch>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #ifndef EQSERVER_CONFIGUPDATESYNCVISITOR_H
19 : #define EQSERVER_CONFIGUPDATESYNCVISITOR_H
20 :
21 : #include "configVisitor.h" // base class
22 :
23 : namespace eq
24 : {
25 : namespace server
26 : {
27 : namespace
28 : {
29 :
30 : class ConfigUpdateSyncVisitor : public ConfigVisitor
31 : {
32 : public:
33 4 : ConfigUpdateSyncVisitor()
34 : : _runningChannels( 0 )
35 : , _failure( false )
36 4 : , _sync( false )
37 4 : {}
38 4 : virtual ~ConfigUpdateSyncVisitor() {}
39 :
40 6 : VisitorResult visitPre( Config* ) override
41 : {
42 6 : _runningChannels = 0;
43 6 : _failure = false;
44 6 : _sync = false;
45 6 : return TRAVERSE_CONTINUE;
46 : }
47 :
48 6 : VisitorResult visitPre( Node* node ) override
49 6 : { return _updateDown( node ); }
50 6 : VisitorResult visitPost( Node* node ) override
51 : {
52 6 : const VisitorResult& result = _updateUp( node );
53 6 : node->flushSendBuffer();
54 6 : return result;
55 : }
56 :
57 6 : VisitorResult visitPre( Pipe* pipe ) override
58 6 : { return _updateDown( pipe ); }
59 4 : VisitorResult visitPost( Pipe* pipe ) override
60 4 : { return _updateUp( pipe ); }
61 :
62 4 : VisitorResult visitPre( Window* window ) override
63 4 : { return _updateDown( window ); }
64 4 : VisitorResult visitPost( Window* window ) override
65 4 : { return _updateUp( window ); }
66 :
67 8 : VisitorResult visit( Channel* channel ) override
68 : {
69 8 : const VisitorResult result = _updateUp( channel );
70 8 : if( channel->isRunning( ))
71 0 : ++_runningChannels;
72 8 : return result;
73 : }
74 :
75 4 : size_t getNumRunningChannels() const { return _runningChannels; }
76 4 : bool hadFailure() const { return _failure; }
77 6 : bool needsSync() const { return _sync; }
78 :
79 : private:
80 : size_t _runningChannels;
81 : bool _failure;
82 : bool _sync; // call again after init failure
83 :
84 16 : template< class T > VisitorResult _updateDown( T* entity ) const
85 : {
86 16 : const uint32_t state = entity->getState() & ~STATE_DELETE;
87 16 : switch( state )
88 : {
89 : case STATE_INITIALIZING:
90 : case STATE_INIT_FAILED:
91 : case STATE_INIT_SUCCESS:
92 : case STATE_EXITING:
93 : case STATE_EXIT_FAILED:
94 : case STATE_EXIT_SUCCESS:
95 : case STATE_RUNNING:
96 14 : return TRAVERSE_CONTINUE;
97 :
98 : case STATE_STOPPED:
99 : case STATE_FAILED:
100 2 : return TRAVERSE_PRUNE;
101 : }
102 0 : LBUNREACHABLE;
103 0 : return TRAVERSE_PRUNE;
104 : }
105 :
106 22 : template< class T > VisitorResult _updateUp( T* entity )
107 : {
108 22 : const uint32_t state = entity->getState() & ~STATE_DELETE;
109 22 : switch( state )
110 : {
111 : case STATE_INITIALIZING:
112 : case STATE_INIT_FAILED:
113 : case STATE_INIT_SUCCESS:
114 8 : if( !entity->syncConfigInit( ))
115 : {
116 6 : entity->sync();
117 6 : _failure = true;
118 6 : _sync = true;
119 12 : LBWARN << lunchbox::className( entity ) << " init failed"
120 6 : << std::endl;
121 : }
122 : else
123 2 : entity->sync();
124 8 : return TRAVERSE_CONTINUE;
125 :
126 : case STATE_EXITING:
127 : case STATE_EXIT_FAILED:
128 : case STATE_EXIT_SUCCESS:
129 8 : if( !entity->syncConfigExit( ))
130 : {
131 0 : entity->sync();
132 0 : _failure = true;
133 0 : LBWARN << lunchbox::className( entity ) << " exit failed"
134 0 : << std::endl;
135 : }
136 : else
137 8 : entity->sync();
138 8 : return TRAVERSE_CONTINUE;
139 :
140 : case STATE_RUNNING:
141 : case STATE_STOPPED:
142 : case STATE_FAILED:
143 6 : return TRAVERSE_CONTINUE;
144 : }
145 0 : LBUNREACHABLE;
146 0 : return TRAVERSE_PRUNE;
147 : }
148 : };
149 :
150 : }
151 : }
152 : }
153 :
154 : #endif // EQSERVER_CONFIGUPDATESYNCVISITOR_H
|