-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordin.cpp
More file actions
228 lines (186 loc) · 6.68 KB
/
coordin.cpp
File metadata and controls
228 lines (186 loc) · 6.68 KB
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
/*******************************************************************
*
* DESCRIPTION: class Coordinator
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
* VERSION 2: Daniel A. Rodriguez
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
* mailto://drodrigu@dc.uba.ar
*
* DATE: 27/6/1998
* DATE: 4/6/1999 (v2)
*
*******************************************************************/
/** include files **/
#include "coordin.h" // header
#include "msgadm.h" // SingleMsgAdmin
#include "coupled.h" // class Coupled
#include "procadm.h"
using namespace std;
/*******************************************************************
* Function Name: Coordinator
********************************************************************/
Coordinator::Coordinator( Coupled *coupled )
: Processor( coupled )
, doneCount( 0 )
, receivedX( 1 )
, inminent( Processor::InvalidId )
{}
/*******************************************************************
* Function Name: receive
* Description: forward the init message to his childrens
********************************************************************/
Processor &Coordinator::receive( const InitMessage &msg )
{
Coupled &coupled( static_cast<Coupled &>( model() ) );
doneCount = coupled.children().size();
Coupled::ModelList::const_iterator cursor;
InitMessage init( msg.time(), this->Processor::id() );
for( cursor = coupled.children().begin() ; cursor != coupled.children().end() ; cursor++ )
SingleMsgAdm::Instance().send( init , *cursor );
return *this;
}
/*******************************************************************
* Function Name: receive
* Description: sends a X message to the port's influences
********************************************************************/
Processor &Coordinator::receive( const ExternalMessage &msg )
{
const InfluenceList &influList( msg.port().influences() ) ;
InfluenceList::const_iterator cursor( influList.begin() ) ;
// Old Code //////////////////////////////////////////////////////////
//MASSERT( !doneCount );
//doneCount = influList.size() ; // No permite eventos externos consecutivos
//////////////////////////////////////////////////////////////////////
// New Code //////////////////////////////////////////////////////////
if (doneCount == 0)
receivedX = 1;
else
receivedX++;
doneCount += influList.size() ;
//////////////////////////////////////////////////////////////////////
if( doneCount > 0 )
{
ExternalMessage xMsg( msg ) ;
xMsg.procId( this->Processor::id() ) ;
for( ; cursor != influList.end(); cursor++ )
{
xMsg.port( *(*cursor) ) ;
SingleMsgAdm::Instance().send( xMsg, (*cursor)->model() ) ;
}
} else
{
lastChange( msg.time() ) ;
if( inminentChild() != Processor::InvalidId )
{
nextChange( model().absoluteNext() - msg.time() ) ;
Processor &proc( SingleProcessorAdmin::Instance().processor( inminentChild() ) ) ;
nextChange( proc.model().absoluteNext() - msg.time() ) ;
}
DoneMessage doneMsg( msg.time(), this->Processor::id(), nextChange() ) ;
SingleMsgAdm::Instance().send( doneMsg, model().parentId() ) ;
}
return *this ;
}
/*******************************************************************
* Function Name: receive
* Description: sends the * message to the inminent child
********************************************************************/
Processor &Coordinator::receive( const InternalMessage &msg )
{
doneCount = 1 ;
InternalMessage internal( msg.time(), this->Processor::id() ) ;
SingleMsgAdm::Instance().send( internal, inminentChild() ) ;
return *this ;
}
/*******************************************************************
* Function Name: receive
* Description: translates the output event to a X messages for the
* influenced children and to a Y message for his father
********************************************************************/
Processor &Coordinator::receive( const OutputMessage &msg )
{
Coupled &coupled( static_cast< Coupled & >( model() ) );
const InfluenceList &influList( msg.port().influences() );
InfluenceList::const_iterator cursor( influList.begin() );
OutputMessage outMsg( msg );
outMsg.procId( this->Processor::id() );
ExternalMessage extMsg;
extMsg.time( msg.time() );
extMsg.procId( Processor::id() );
extMsg.value( msg.value() );
for( ; cursor != influList.end(); cursor++ )
{
// not found in the output list => send it as an external message
if( coupled.outputPorts().find( (*cursor)->id() ) == coupled.outputPorts().end() )
{
doneCount++;
extMsg.port( * (*cursor) );
SingleMsgAdm::Instance().send( extMsg, (*cursor)->model() );
} else
{
outMsg.port( * (*cursor) );
SingleMsgAdm::Instance().send( outMsg, coupled.parentId() );
}
}
return *this;
}
/*******************************************************************
* Function Name: receive
* Description: recalculates the inminent child and sends it to his
* father inside a done message
********************************************************************/
Processor &Coordinator::receive( const DoneMessage &msg )
{
Coupled &coupled( static_cast< Coupled & >( model() ) );
MASSERTMSG( doneCount > 0, "Unexpected Done message!" );
if( ! --doneCount )
{
lastChange( msg.time() );
recalcInminentChild();
if( inminentChild() == Processor::InvalidId )
nextChange( VTime::Inf );
else
{
Processor &proc = SingleProcessorAdmin::Instance().processor( inminentChild() );
nextChange( proc.model().absoluteNext() - msg.time() );
}
DoneMessage doneMsg( msg.time(), this->Processor::id(), coupled.nextChange() );
for (long i = 1; i <= receivedX; i++)
SingleMsgAdm::Instance().send( doneMsg, coupled.parentId() );
receivedX = 1;
}
return *this;
}
/*******************************************************************
* Function Name: id
********************************************************************/
Processor &Coordinator::id( const ProcId &pid )
{
this->Processor::id( pid );
MASSERT( pmodel() );
pmodel()->id( pid );
return *this;
}
/*******************************************************************
* Function Name: recalcInminentChild
********************************************************************/
Coordinator &Coordinator::recalcInminentChild()
{
Coupled &coupled( static_cast< Coupled & >( model() ) );
Coupled::ModelList::const_iterator cursor( coupled.children().begin() ) ;
VTime min( VTime::Inf );
inminentChild( Processor::InvalidId );
for( ; cursor != coupled.children().end() ; cursor++ )
{
Model &model( SingleProcessorAdmin::Instance().processor( *(cursor) ).model() ) ;
if( min > model.absoluteNext() )
{
min = model.absoluteNext();
inminentChild( *(cursor) );
}
}
return *this;
}