-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
125 lines (110 loc) · 3.36 KB
/
server.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
/*******************************************************************
*
* DESCRIPTION: Server Model
*
* AUTHOR: Khaldoon Al-Zoubi
*
*
* DATE: 20/11/2003
*
*******************************************************************/
/** include files **/
#include "server.h" // class Server
#include "message.h" // class ExternalMessage, InternalMessage
#include "mainsimu.h" // MainSimulator::Instance().getParameter( ... )
#include "distri.h" // class Distribution
/*******************************************************************
* Function Name: Server
* Description:
*
********************************************************************/
Server::Server( const string &name )
: Atomic( name )
, in( addInputPort( "in" ) )
, beReady( addInputPort( "beReady" ) )
, out( addOutputPort( "out" ) )
, request( addOutputPort( "request" ) )
{
string id( MainSimulator::Instance().getParameter( description(), "myId" ) ) ;
myId = atoi(id.data()); //accept any value from user.
try
{
dist = Distribution::create( MainSimulator::Instance().getParameter( description(), "distribution" ) );
MASSERT( dist ) ;
for ( register int i = 0; i < dist->varCount(); i++ )
{
string parameter( MainSimulator::Instance().getParameter( description(), dist->getVar( i ) ) );
dist->setVar( i, str2Value( parameter ) ) ;
}
} catch( InvalidDistribution &e )
{
e.addText( "The model " + description() + " has distribution problems!" ) ;
e.print(cerr);
MTHROW( e ) ;
} catch( MException &e )
{
MTHROW( e ) ;
}
}
/*******************************************************************
* Function Name: initFunction
* Description:
*
********************************************************************/
Model& Server::initFunction()
{
ready = false;
holdIn( active, Time::Zero); // To force sending initial request to the queue
return *this ;
}
/*******************************************************************
* Function Name: externalFunction
* Description:
********************************************************************/
Model& Server::externalFunction( const ExternalMessage &msg )
{
if ((this->state() == passive) && (msg.port() == beReady) && (myId == (int) msg.value()))
{
ready = true;
}
else if ((this->state() == passive) && (msg.port() == in) && (ready == true))
{
value = msg.value();
double service_time = fabs( distribution().get());
if (service_time > 0)
service_time = 1000/service_time;
else
service_time = 0;
holdIn(active, Time(0,0,0,service_time)) ;
}
return *this;
}
/*******************************************************************
* Function Name: internalFunction
* Description:
********************************************************************/
Model& Server::internalFunction( const InternalMessage & )
{
ready = false;
passivate();
return *this ;
}
/*******************************************************************
* Function Name: outputFunction
* Description:
********************************************************************/
Model &Server::outputFunction( const InternalMessage &msg )
{
if (ready == true) // if not initial execution
sendOutput( msg.time(), out, value) ;
sendOutput( msg.time(), request, myId) ;
return *this ;
}
/*******************************************************************
* Function Name:
* Description:
********************************************************************/
Server::~Server ()
{
delete dist;
}