summaryrefslogtreecommitdiff
path: root/src/base/checkFront.c
blob: 6c44580e8c6ffbe24fef78a2432791191020206f (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
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

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

#include "main.h"
#include "list.h"
#include "myTimer.h"
#include "checkFront.h"
#include "idManager.h"
#include "server.h"

typedef struct struct_checkfront_t {
	char *msg;
	my_time_t time;
	int id;
	int count;
	int type;
} checkfront_t;

static checkfront_t *newCheck(char *s, int type, int id)
{
	checkfront_t *new;

	assert(s != NULL);

	new = malloc(sizeof(checkfront_t));
	memset(new, 0, sizeof(checkfront_t));
	new->msg = strdup(s);
	new->time = timer_get_current_time();
	new->id = id;
	new->type = type;
	new->count = 0;

	return new;
}

static void destroyCheck(checkfront_t * p)
{
	assert(p != NULL);
	free(p->msg);
	free(p);
}

list_t *check_front_new()
{
	return list_new();
}

void check_front_msg_add(list_t * list, char *msg, int type, int id)
{
	if (type == CHECK_FRONT_TYPE_CHECK)
		id_inc(id);

	list_add(list, newCheck(msg, type, id));
}

void check_front_event(client_t * client)
{
	my_time_t currentTime;
	int i;

	currentTime = timer_get_current_time();

	for (i = 0; i < client->listSendMsg->count; i++) {
		checkfront_t *this;

		this = (checkfront_t *) client->listSendMsg->list[i];

		switch (this->type) {
			case CHECK_FRONT_TYPE_SIMPLE:
				server_send_client(client, this->msg);
				list_del_item(client->listSendMsg, i, destroyCheck);
				i--;
				break;
			case CHECK_FRONT_TYPE_CHECK:
				if (this->count == 0 || currentTime - this->time > CHECK_FRONT_SEND_TIME_INTERVAL) {
					server_send_client(client, this->msg);
					this->time = currentTime;
					this->count++;
				}
				if (this->count > CHECK_FRONT_MAX_COUNT_SEND) {
					id_del(this->id);
					list_del_item(client->listSendMsg, i, destroyCheck);
					i--;
				}
				break;
			default:
				assert(!_("Bad type of front!"));
				break;
		}
	}
}

void check_front_del_msg(list_t * listCheckFront, int id)
{
	int i;

	for (i = 0; i < listCheckFront->count; i++) {
		checkfront_t *this;

		this = (checkfront_t *) listCheckFront->list[i];

		if (this->id == id) {
			id_del(this->id);
			list_del_item(listCheckFront, i, destroyCheck);
			return;
		}
	}
}

void check_front_destroy(list_t * p)
{
	assert(p != NULL);
	list_destroy_item(p, destroyCheck);
}