-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_exit_event.cpp
191 lines (147 loc) · 10.3 KB
/
process_exit_event.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
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
int process_exit_event(event *temp_event_exits, event *exits, event *events, client clients_vector[NUMBER_CLIENTS], int connections[][NUMBER_CLIENTS], int clients_state[NUMBER_CLIENTS], int servers_state[NUMBER_SERVERS], int *servers_up, int *number_events, int event_id, int end_host, int *clients_up )
{
int i,j, client_id, server_id;
double when,exp_paramenter;
struct event *temp_event, *temp_event2;
struct event **ant, **pont;
ant = ( event **) malloc(sizeof ( event *));
pont = ( event **) malloc(sizeof ( event *));
if(event_id==CLIENT_EXIT_EVENT)
{
client_id=end_host;
//if(DEBUG_LEVEL>0) printf("%f CLIENT_EXIT_EVENT %d\n", t, client_id);
if(clients_state[client_id]==0) printf("ERROR: %f CLIENT_EXIT_EVENT %d\n", t, client_id);
clients_state[client_id]=0;
client_exit_time[client_id]=t;
(*clients_up)--;
if(DEBUG_LEVEL>3) printf("%f CLIENT_EXIT_EVENT %d\n", client_exit_time[client_id], client_id);
//gerar eventos de desconexao com delta 3 para todos os servidores i conectados a client
for(i=0;i<NUMBER_SERVERS;i++)
{
//cada cliente possui um vetor own_connections[]
//se os dois valores sao iguais a 1, a conexao esta ativa e nao ha pedidos de desconexao ja alocados
if((connections[i][client_id]==1) && (clients_vector[client_id].own_connections[i]==1))
{
//criando evento de desconexao para cliente no vetor de eventos
while(true)
{
exp_paramenter = (float)(1/(float)DELTA3);
when = t+gera_aleatorio(exp_paramenter);
if( when < t )
{
printf("ERROR: %f ERROR_TIME_EVENT - DISCONNECTION_EVENT %d %d %f\n", t, i, client_id, when);
return 1;
}
busca_tempo (events, when, ant, pont);
if(*pont==NULL) break;
}
//if(DEBUG_LEVEL>0) printf( "\n%f 1 REMAINING EVENTS TO PROCESS %d\n", t, (*number_events));
insere(events,DISCONNECTION_EVENT,when,i,client_id);
(*number_events)++;
if(DEBUG_LEVEL>3) printf("%f PUSH DISCONNECTION_EVENT %d %d %f\n", t, i, client_id, when);
//if(DEBUG_LEVEL>0) printf( "\n%f 2 REMAINING EVENTS TO PROCESS %d\n", t, (*number_events));
}
}
//deletar evento de saida da lista de saidas
i = erase(exits,temp_event_exits) ;
if( i == 1 ) return 1;
(*number_events)--;
//if(DEBUG_LEVEL>0) printf( "\n%f 3 REMAINING EVENTS TO PROCESS %d\n", t, (*number_events));
//deletar evento de fluxo relativo ao cliente que saiu do sistema
//passar endereco da estrura
temp_event = clients_vector[client_id].flow_event;
if(clients_vector[client_id].flow_event_alloc==1)
{
//neste caso, o evento de fluxo estava alocado na lista de eventos
//tirar da lista de eventos, eventos de fluxo nao sao desalocados pela funcao erase
i = erase(events,temp_event);
if( i == 1 ) return 1;
(*number_events)--;
clients_vector[client_id].flow_event_alloc=0;
//puts("removendo evento de fluxo");
}
free(temp_event);
clients_vector[client_id].flow_event=NULL;
clients_vector[client_id].exit_event=NULL;
//puts("saindo de exit event");
//verificar se ficou algum pedido de conexao pendente e cancelar
temp_event = get_next(events);
while(temp_event != NULL)
{
event_id = get_event_id(temp_event);
i = get_client_id(temp_event);
if( (event_id == CONNECTION_EVENT) && ( i == client_id) )
{
temp_event2 = temp_event;
temp_event = get_next(temp_event);
i = erase(events,temp_event2);
if( i == 1 ) return 1;
(*number_events)--;
//puts("apagando evento de conexao");
}
else{
temp_event = get_next(temp_event);}
}
}
else if(event_id==SERVER_EXIT_EVENT)
{
server_id=end_host;
if(servers_state[server_id]==0) printf("ERROR: %f SERVER_EXIT_EVENT %d\n", t, server_id);
//if(DEBUG_LEVEL>0) printf("%f SERVER_EXIT_EVENT %d\n", t, server_id);
(*servers_up)--;
servers_state[server_id]=0;
server_exit_time[server_id]=t;
if(DEBUG_LEVEL>0) printf("%f SERVER_EXIT_EVENT %d\n", server_exit_time[server_id], server_id);
//gerar eventos de desconexao com delta 3 para todos os servidores i conectados a client
for(j=0;j<NUMBER_CLIENTS;j++)
{
//cada cliente possui um vetor own_connections[]
//se os dois valores sao iguais a 1, a conexao esta ativa e nao ha pedidos de desconexao ja alocados
if((connections[server_id][j]==1) && (clients_vector[j].own_connections[server_id]==1))
{
//criando evento de desconexao para cliente no vetor de eventos
while(true)
{
exp_paramenter = (float)(1/(float)DELTA3);
when = t+gera_aleatorio(exp_paramenter);
if( when < t )
{
printf("ERROR: %f ERROR_TIME_EVENT - DISCONNECTION_EVENT %d %d %f\n", t, server_id, j, when);
return 1;
}
busca_tempo (events, when, ant, pont);
if(*pont==NULL) break;
}
clients_vector[j].own_connections[server_id]=0;
insere(events,DISCONNECTION_EVENT,when,server_id,j);
(*number_events)++;
if(DEBUG_LEVEL>0) printf("%f PUSH DISCONNECTION_EVENT %d %d %f\n", t, server_id, j, when);
//atualizando tempo do ultimo evento de conexao
if( when > clients_vector[j].last_connection_scheduled ) clients_vector[j].last_connection_scheduled = when;
}
}
//deletar evento de saida da lista de saidas
i = erase(events,temp_event_exits) ;
if( i == 1 ) return 1;
(*number_events)--;
//verificar se ficou algum pedido de conexao pendente e cancelar
temp_event = get_next(events);
while(temp_event != NULL)
{
event_id = get_event_id(temp_event);
i = get_server_id(temp_event);
//printf("%f PUSH DISCONNECTION_EVENT %d %d %f\n", t, server_id, j, when);
if( (event_id == CONNECTION_EVENT) && ( i == server_id) )
{
temp_event2 = temp_event;
temp_event = get_next(temp_event);
i = erase(events,temp_event2);
if( i == 1 ) return 1;
(*number_events)--;
}
else{
temp_event = get_next(temp_event);}
}
}
return 0;
}