summaryrefslogtreecommitdiff
path: root/src/base/protect.c
blob: a384734cf9b1d51517290322fef0a612e23084ed (plain)
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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "main.h"
#include "myTimer.h"
#include "protect.h"

protect_t *newProtect()
{
	protect_t *new;

	new = malloc(sizeof(protect_t));
	new->lastMove = timer_get_current_time();
	new->lastPing = timer_get_current_time();
	new->avarage = 0;
	new->count = 0;
	new->isDown = FALSE;

	return new;
}

void refreshLastMove(protect_t * p)
{
	my_time_t currentTime;
	my_time_t interval;

	currentTime = timer_get_current_time();
	interval = currentTime - p->lastMove;

	p->lastMove = timer_get_current_time();
	p->avarage += interval;
	p->count++;

	if (p->count == PROTECT_SPEED_AVARAGE) {
		int my_index;

		my_index = p->avarage / PROTECT_SPEED_AVARAGE;
		p->avarage = 0;
		p->count = 0;
#if 0
		if (my_index < PROTECT_SPEED_INTERVAL_TIMEOUT) {
			p->isDown = TRUE;
		}
#endif
		//printf("speed index = %d\n", my_index);
	}
}

void rereshLastPing(protect_t * p)
{
	//my_time_t currentTime;
	//my_time_t interval;
	//currentTime = timer_get_current_time();
	//interval = currentTime - p->lastPing;
	p->lastPing = timer_get_current_time();
	//printf("interval = %d\n", interval);
}

bool_t isDown(protect_t * p)
{
	my_time_t currentTime;
	my_time_t interval;

	currentTime = timer_get_current_time();
	interval = currentTime - p->lastPing;

	if (interval > PROTECT_PING_INTERVAL_TIMEOUT)
		p->isDown = TRUE;

	return p->isDown;
}

void destroyProtect(protect_t * p)
{
	assert(p != NULL);
	free(p);
}