-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsclient.cpp
More file actions
182 lines (156 loc) · 5.49 KB
/
hsclient.cpp
File metadata and controls
182 lines (156 loc) · 5.49 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
/*******************************************************************
*
* DESCRIPTION: Atomic Model HSClient
*
* AUTHOR: Ariel Gonzalez
*
* EMAIL: mailto://egonzalz@dc.uba.ar
* mailto://agonzalez@tecnet-ibermatica.com.ar
*
* DATE: 26/9/2003
*
*******************************************************************/
/** include files **/
#include "hsclient.h" // class HSClient
#include "message.h" // class ExternalMessage, InternalMessage
#include "mainsimu.h" // MainSimulator::Instance().getParameter( ... )
#include "strutil.h" // str2Int( ... )
#define MAX_POLEOS 5
/** public functions **/
/*******************************************************************
* Function Name: HSClient
* Description:
********************************************************************/
HSClient::HSClient( const string &name )
: Atomic( name )
, start( addInputPort( "start" ) )
, stop( addInputPort( "stop" ) )
, red_in( addInputPort( "red_in" ) )
, nombre_logico( addOutputPort( "nombre_logico" ) )
, red_out( addOutputPort( "red_out" ) )
, poll_time( 0, 0, 5, 0 )
{
string ptime( MainSimulator::Instance().getParameter( description(), "poll_time" ) ) ;
string nodo( MainSimulator::Instance().getParameter( description(), "nodo_hs" ) ) ;
string nodop( MainSimulator::Instance().getParameter( description(), "nodo_hs_prim" ) ) ;
string nodos( MainSimulator::Instance().getParameter( description(), "nodo_hs_sec" ) ) ;
if( ptime != "" ) poll_time = ptime ;
nodo_hs = str2Int (nodo);
nodo_hs_prim = str2Int (nodop);
nodo_hs_sec = str2Int (nodos);
}
/*******************************************************************
* Function Name: initFunction
* Description:
* Precondition: El tiempo del proximo evento interno es Infinito
********************************************************************/
Model &HSClient::initFunction()
{
estado_hsclient = hsc_inactivo;
ultima_respuesta = 0;
polear_primario = 1;
cant_poleos_sin_respuesta = 0;
//holdIn (active, poll_time);
return *this ;
}
/*******************************************************************
* Function Name: externalFunction
* Description:
********************************************************************/
Model &HSClient::externalFunction( const ExternalMessage &msg )
{
Time tiempo_cero (0,0,0,0);
if (( state() == passive ) && ( msg.port() == start ))
{
estado_hsclient = hsc_poleando;
ultima_respuesta = 0;
holdIn (active, tiempo_cero);
}
if (( state() == active ) && ( msg.port() == stop )) {
cout << msg.time() << " / CLIENTE / HOT-STANDBY DETENIDO.\n";
passivate ();
}
if (( state() == active ) && ( msg.port() == red_in ))
{
ultima_respuesta = msg.value();
estado_hsclient = hsc_recibiendo;
holdIn (active, tiempo_cero);
}
return *this;
}
/*******************************************************************
* Function Name: internalFunction
* Description:
********************************************************************/
Model &HSClient::internalFunction( const InternalMessage & msg)
{
Time tiempo_cero (0,0,0,0);
if (( state() == active) && ( estado_hsclient == hsc_recibiendo )) {
cant_poleos_sin_respuesta = 0;
estado_hsclient = hsc_inactivo;
holdIn (active, tiempo_cero);
}
if (( state() == active) && ( estado_hsclient == hsc_poleando )) {
estado_hsclient = hsc_inactivo;
if (cant_poleos_sin_respuesta < (MAX_POLEOS-1))
cant_poleos_sin_respuesta ++ ;
else {
polear_primario = 1 - polear_primario;
cant_poleos_sin_respuesta = 0;
if (polear_primario==1)
cout << msg.time() << " / CLIENTE / CAMBIANDO EL DESTINO DE POLEO A NODO PRIMARIO\n";
else
cout << msg.time() << " / CLIENTE / CAMBIANDO EL DESTINO DE POLEO A NODO SECUNDARIO\n";
}
holdIn (active, tiempo_cero);
}
if (( state() == active) && ( estado_hsclient == hsc_inactivo )) {
estado_hsclient = hsc_poleando;
holdIn (active, poll_time);
}
return *this ;
}
/*******************************************************************
* Function Name: outputFunction
* Description:
********************************************************************/
Model &HSClient::outputFunction( const InternalMessage &msg )
{
Value poleo, nlog;
int rol;
int status_nodo_hs;
if (( state() == active) && ( estado_hsclient == hsc_poleando )) {
// envia un poleo
if ( polear_primario == 1 )
poleo = obtener_poleo_hs ( nodo_hs_prim );
else poleo = obtener_poleo_hs ( nodo_hs_sec );
sendOutput( msg.time(), red_out, poleo ) ;
}
if (( state() == active) && ( estado_hsclient == hsc_recibiendo )) {
// envia un nombre_logico
status_nodo_hs = procesar_poleo_hs ( ultima_respuesta );
cout << msg.time() << " / CLIENTE / RECIBE RESPUESTA CON ESTADO ";
if ( status_nodo_hs == PRIM_MASTER) cout << "PRIM_MASTER\n";
else if ( status_nodo_hs == PRIM_SLAVE) cout << "PRIM_SLAVE\n";
else if ( status_nodo_hs == SEC_MASTER) cout << "SEC_MASTER\n";
else if ( status_nodo_hs == SEC_SLAVE) cout << "SEC_SLAVE\n";
else cout << "?????\n";
if (( status_nodo_hs == PRIM_MASTER) ||
( status_nodo_hs == SEC_SLAVE))
{
cout << msg.time() << " / CLIENTE / Remapeando Nombre Lógico a Primario...\n";
rol = PRIMARIO;
nlog = obtener_msg_nombre_logico( nodo_hs, rol );
sendOutput( msg.time(), nombre_logico, nlog ) ;
}
else if (( status_nodo_hs == SEC_MASTER) ||
( status_nodo_hs == PRIM_SLAVE))
{
cout << msg.time() << " / CLIENTE / Remapeando Nombre Lógico a Secundario...\n";
rol = SECUNDARIO;
nlog = obtener_msg_nombre_logico( nodo_hs, rol );
sendOutput( msg.time(), nombre_logico, nlog ) ;
}
}
return *this ;
}