-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPsmFactory.cpp
152 lines (132 loc) · 5.08 KB
/
PsmFactory.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
//==============================================================================
//
// PsmFactory.cpp
//
// Copyright (C) 2013-2022 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 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 GNU General Public License along
// with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "PsmFactory.h"
#include "Debug.h"
#include "GlobalAddress.h"
#include "LocalAddress.h"
#include "Message.h"
#include "MsgHeader.h"
#include "MsgPort.h"
#include "ProtocolSM.h"
#include "PsmContext.h"
using namespace NodeBase;
//------------------------------------------------------------------------------
namespace SessionBase
{
PsmFactory::PsmFactory(Id fid, ContextType type, ProtocolId prid,
c_string name) : MsgFactory(fid, type, prid, name)
{
Debug::ft("PsmFactory.ctor");
}
//------------------------------------------------------------------------------
PsmFactory::~PsmFactory()
{
Debug::ftnt("PsmFactory.dtor");
}
//------------------------------------------------------------------------------
Context* PsmFactory::AllocContext() const
{
Debug::ft("PsmFactory.AllocContext");
return new PsmContext(GetFaction());
}
//------------------------------------------------------------------------------
fn_name PsmFactory_AllocIcPsm = "PsmFactory.AllocIcPsm";
ProtocolSM* PsmFactory::AllocIcPsm
(const Message& msg, ProtocolLayer& lower) const
{
Debug::ft(PsmFactory_AllocIcPsm);
Debug::SwLog(PsmFactory_AllocIcPsm, strOver(this), Fid());
return nullptr;
}
//------------------------------------------------------------------------------
void PsmFactory::Patch(sel_t selector, void* arguments)
{
MsgFactory::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
Factory::Rc PsmFactory::ReceiveMsg
(Message& msg, bool atIoLevel, TransTrace* tt, Context*& ctx)
{
Debug::ft("PsmFactory.ReceiveMsg");
MsgPort* port = nullptr;
auto header = msg.Header();
if(header->rxAddr.bid != NIL_ID)
{
// Find the context using the port to which the message is addressed.
//
port = MsgPort::Find(header->rxAddr);
if(port == nullptr) return PortNotFound;
ctx = port->GetContext();
if(ctx == nullptr) return ContextNotFound;
}
else
{
// The message isn't addressed to a port. There are three cases:
// (a) Initial message. Create a context and port. A port is created
// at I/O level (that is, as soon as a message arrives) because it
// immediately records the peer's address. MsgPort::FindPeer can
// then deliver a subsequent message to the same port and context
// if the dialog's initiator sends another message before it has
// received a reply. Similarly, the MsgPort constructor invokes
// PortAllocated on the factory that received this message. The
// factory can save the port in a user profile so that subsequent
// messages from the user can also be delivered to the port and
// context that were just created to handle this initial message.
// (b) Join message. SsmContext::ReceiveMsg has set CTX to the context
// that will handle this session. Create a port on that context.
// (c) Subsequent message. The message was not addressed to a port
// because the sender had not yet received a reply, so it still
// doesn't know the address of the port that was allocated for its
// initial message. This is prevented, for intraprocessor messages
// only, by MsgPort::UpdatePeer. But for an interprocessor message,
// we must search for the port that contains the sender's address.
//
if(header->initial)
{
if(!header->join) // case (a)
{
ctx = AllocContext();
if(ctx == nullptr) return CtxAllocFailed;
IncrContexts();
port = new MsgPort(msg, *ctx);
}
else // case (b)
{
if(ctx == nullptr) return ContextNotFound;
port = new MsgPort(msg, *ctx);
}
}
else // case (c)
{
port = MsgPort::FindPeer(msg.GetSender());
if(port == nullptr) return PortNotFound;
ctx = port->GetContext();
}
// We just created or found the port that will receive the
// message, so update the message header with its address.
//
msg.SetRxAddr(port->LocAddr().SbAddr());
header->rxAddr = port->LocAddr().SbAddr();
}
return MsgFactory::ReceiveMsg(msg, atIoLevel, tt, ctx);
}
}