-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproductionBuffer.cpp
More file actions
113 lines (95 loc) · 3.26 KB
/
productionBuffer.cpp
File metadata and controls
113 lines (95 loc) · 3.26 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
/*******************************************************************
*
* DESCRIPTION: Atomic Model ProductionBuffer
*
* AUTHOR: Abdullah Alfaify
*
* EMAIL: mailto://aalfa064@uottawa.ca
*
* DATE: 14/12/2011
*
*******************************************************************/
/** include files **/
#include "productionBuffer.h" // class ProductionBuffer
#include "message.h" // class ExternalMessage, InternalMessage
#include "mainsimu.h" // MainSimulator::Instance().getParameter( ... )
/** public functions **/
/*******************************************************************
* Function Name: ProductionBuffer
* Description:
********************************************************************/
ProductionBuffer::ProductionBuffer( const string &name )
: Atomic( name )
, in( addInputPort( "in" ) )
, done( addInputPort( "done" ) )
, out( addOutputPort( "out" ) )
, mg( addOutputPort( "mg" ) )
, preparationTime( 0, 0, 1, 0 )
{
string time( MainSimulator::Instance().getParameter( description(), "preparation" ) ) ;
if( time != "" )
preparationTime = time ;
}
/*******************************************************************
* Function Name: initFunction
* Description: in this function we will set the initial values of the Buffer
********************************************************************/
Model &ProductionBuffer::initFunction()
{
// initially we clean up the Buffer.
elements.erase( elements.begin(), elements.end() ) ;
// set the max size of the buffer , it can be changed.
BufferMaxSize = 5;
return *this ;
}
/*******************************************************************
* Function Name: externalFunction
* Description:
********************************************************************/
Model &ProductionBuffer::externalFunction( const ExternalMessage &msg )
{
// incoming message to the (in) port
if( msg.port() == in )
{
//we have to check the size of the buffer to see the available space
if (elements.size() < BufferMaxSize )
{
// add the new element to the back of the Buffer.
elements.push_back( msg.value() ) ;
// first element is sent directly to production without waiting for done message
if( elements.size() == 1 )
holdIn( active, preparationTime ); // call output function
}
else
{
// this is not valid option since we ask the source to send a new element when free space available
}
}
if( msg.port() == done )
{
elements.pop_front() ; // delete the elements that has been already sent
if( !elements.empty() )
holdIn( active, preparationTime ); // call output function
}
return *this;
}
/*******************************************************************
* Function Name: internalFunction
* Description:
********************************************************************/
Model &ProductionBuffer::internalFunction( const InternalMessage & )
{
passivate();
return *this ;
}
/*******************************************************************
* Function Name: outputFunction
* Description:
********************************************************************/
Model &ProductionBuffer::outputFunction( const InternalMessage &msg )
{
sendOutput( msg.time(), out, elements.front() ) ;
// call for new element from the source
sendOutput( msg.time(), mg, elements.size() ) ;
return *this ;
}