-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathState.cpp
266 lines (216 loc) · 7.08 KB
/
State.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//==============================================================================
//
// State.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "State.h"
#include <bitset>
#include <cstdint>
#include <ostream>
#include <string>
#include "Algorithms.h"
#include "Debug.h"
#include "EventHandler.h"
#include "Formatters.h"
#include "NbTypes.h"
#include "Registry.h"
#include "Service.h"
#include "ServiceRegistry.h"
#include "Singleton.h"
#include "SysTypes.h"
using namespace NodeBase;
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace SessionBase
{
const State::Id State::MaxId = 63;
//------------------------------------------------------------------------------
fn_name State_ctor = "State.ctor";
State::State(ServiceId sid, Id stid) :
sid_(sid),
handlers_{NIL_ID},
msgAnalyzers_{NIL_ID}
{
Debug::ft(State_ctor);
stid_.SetId(stid);
// Check that the state's service is registered.
//
auto svc = Singleton<ServiceRegistry>::Instance()->Services().At(sid);
if(svc == nullptr)
{
Debug::SwLog(State_ctor, "service not found", pack2(sid, stid));
return;
}
// Register system-defined event handlers.
//
// The Analyze Message event applies to every state.
//
handlers_[Event::AnalyzeMsg] = EventHandler::AnalyzeMsg;
// The Analyze SAP and SNP events only apply to a modifier's states.
//
if(svc->IsModifier())
{
handlers_[Event::AnalyzeSap] = EventHandler::AnalyzeSap;
handlers_[Event::AnalyzeSnp] = EventHandler::AnalyzeSnp;
}
// The Force Transition event applies to a modifiable service's states.
//
if(svc->IsModifiable())
{
handlers_[Event::ForceTransition] = EventHandler::ForceTransition;
}
// The Initiation Request event applies to a modifiable service's state
// and to any modifier service's state.
//
if(svc->IsModifiable() || svc->IsModifier())
{
handlers_[Event::InitiationReq] = EventHandler::InitiationReq;
}
// The Media Failure event applies to any service's state.
//
handlers_[Event::MediaFailure] = EventHandler::MediaFailure;
// Register the state with its service.
//
svc->BindState(*this);
}
//------------------------------------------------------------------------------
fn_name State_dtor = "State.dtor";
State::~State()
{
Debug::ftnt(State_dtor);
Debug::SwLog(State_dtor, UnexpectedInvocation, 0);
auto svc = Singleton<ServiceRegistry>::Extant()->Services().At(sid_);
if(svc != nullptr) svc->UnbindState(*this);
}
//------------------------------------------------------------------------------
fn_name State_BindEventHandler = "State.BindEventHandler";
bool State::BindEventHandler(EventHandlerId ehid, EventId eid)
{
// Check that
// o the event handler is not private to the framework
// o the event is not private to the framework
// o an event handler is not already registered
//
if(!EventHandler::AppCanUse(ehid))
{
Debug::SwLog(State_BindEventHandler,
"invalid event", pack3(sid_, Stid(), ehid));
return false;
}
if(!Event::AppCanHandle(eid))
{
Debug::SwLog(State_BindEventHandler,
"unexpected event", pack3(sid_, Stid(), ehid));
return false;
}
if(handlers_[eid] != NIL_ID)
{
Debug::SwLog(State_BindEventHandler,
"replacing event handler", pack3(sid_, Stid(), ehid));
}
handlers_[eid] = ehid;
return true;
}
//------------------------------------------------------------------------------
fn_name State_BindMsgAnalyzer = "State.BindMsgAnalyzer";
bool State::BindMsgAnalyzer(EventHandlerId ehid, ServicePortId pid)
{
// Check that
// o the analyzer is not private to the framework
// o the port is valid
// o a message analyzer is not already registered
//
if(!EventHandler::AppCanUse(ehid))
{
Debug::SwLog(State_BindMsgAnalyzer,
"invalid event", pack4(sid_, Stid(), ehid, pid));
return false;
}
if(!Service::IsValidPortId(pid))
{
Debug::SwLog(State_BindMsgAnalyzer,
"invalid ServicePortId", pack4(sid_, Stid(), ehid, pid));
return false;
}
if(msgAnalyzers_[pid] != NIL_ID)
{
Debug::SwLog(State_BindMsgAnalyzer,
"replacing message analyzer", pack4(sid_, Stid(), ehid, pid));
}
msgAnalyzers_[pid] = ehid;
return true;
}
//------------------------------------------------------------------------------
ptrdiff_t State::CellDiff()
{
uintptr_t local;
auto fake = reinterpret_cast<const State*>(&local);
return ptrdiff(&fake->stid_, fake);
}
//------------------------------------------------------------------------------
void State::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Immutable::Display(stream, prefix, options);
if(!options.test(DispVerbose)) return;
auto svc = Singleton<ServiceRegistry>::Instance()->Services().At(sid_);
stream << prefix << "stid : " << stid_.to_str() << CRLF;
stream << prefix << "sid : " << int(sid_);
stream << " (" << strObj(svc) << ')' << CRLF;
auto lead1 = prefix + spaces(2);
auto lead2 = prefix + spaces(4);
stream << prefix << "handlers [EventId]" << CRLF;
for(size_t i = 0; i <= Event::MaxId; ++i)
{
if(handlers_[i] != NIL_ID)
{
stream << lead1 << '[' << strName(svc->EventName(i), i) << ']' << CRLF;
stream << lead2 << strClass(svc->Handlers().At(handlers_[i])) << CRLF;
}
}
stream << prefix << "msgAnalyzers [ServicePortId]" << CRLF;
for(size_t i = 0; i <= MaxServicePortId; ++i)
{
if(msgAnalyzers_[i] != NIL_ID)
{
stream << lead1 << '[' << svc->PortName(i) << ']' << CRLF;
stream << lead2
<< strClass(svc->Handlers().At(msgAnalyzers_[i])) << CRLF;
}
}
}
//------------------------------------------------------------------------------
EventHandlerId State::GetHandler(EventId eid) const
{
if(!Event::IsValidId(eid)) return NIL_ID;
return handlers_[eid];
}
//------------------------------------------------------------------------------
EventHandlerId State::MsgAnalyzer(ServicePortId pid) const
{
if(!Service::IsValidPortId(pid)) return NIL_ID;
return msgAnalyzers_[pid];
}
//------------------------------------------------------------------------------
void State::Patch(sel_t selector, void* arguments)
{
Immutable::Patch(selector, arguments);
}
}