-
Notifications
You must be signed in to change notification settings - Fork 0
/
userMSN.h
188 lines (148 loc) · 3.64 KB
/
userMSN.h
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
#include <string.h>
#include <stdlib.h>
#define MAX_NUM_PARAMS_REQUEST 10
typedef enum{
AUSENTE,
PRESENTE,
}state_t;
typedef enum{
GET_STATE,
POST_NAME_LOCATION_STATE,
GET_LOCATION,
AUTH,
CHALLENGE,
EXIT,
}request_type_t;
typedef enum{
OK,
ERROR,
STATE,
LOCATION,
}response_t;
typedef struct{
state_t state;
char name[50];
char location[15];
char secret[256];
}user_t;
struct node_type{
user_t user;
struct node_type* next;
struct node_type* previous;
};// node_t;
typedef struct node_type node_t;
typedef struct{
char* req[MAX_NUM_PARAMS_REQUEST+1];
int num_params;
request_type_t req_type;
}request_msn_t;
int split_request(char* req_buff, request_msn_t* req);
void usercpy(user_t *dest,user_t* src);
void freeRequest(request_msn_t* req);
node_t* addUser(user_t* user,node_t* user_list);//SEGFAULT en usercpy
void showUser(user_t* user);
char* state2str();
int split_request(char* req_buff, request_msn_t* req){
char* str =req_buff;
char * pch;
char *saveptr;
int index = 0;
req->num_params = 0;
//printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok_r (str," ,-",&saveptr);
if(pch == NULL)
return 0;
req->req_type = atoi(pch);
pch = strtok_r (NULL," ,-",&saveptr);
while (pch != NULL)
{
req->req[index] = strdup(pch);
req->num_params++;
index++;
//printf ("%s\n",pch);
pch = strtok_r (NULL, " ,-",&saveptr);
}
return 1;
}
void freeRequest(request_msn_t* req){
int i;
for(i = 0; i < req->num_params; i++){
free(req->req[i]);
}
}
node_t* addUser(user_t* user,node_t* user_list){
node_t* actual_node = user_list;
if(actual_node==NULL){
actual_node = malloc(sizeof(node_t));
usercpy(&actual_node->user,user);
actual_node->next=NULL;
actual_node->previous = NULL;
return actual_node;
}
for(actual_node = user_list; actual_node!= NULL ;
actual_node = actual_node->next ){
if(strcmp(actual_node->user.name,user->name)==0){
usercpy(&actual_node->user,user);
return user_list;
}
if(actual_node->next == NULL){
actual_node->next = malloc(sizeof(node_t));
usercpy(&actual_node->next->user,user);
actual_node->next->next=NULL;
actual_node->next->previous = actual_node->next;
return user_list;
}
}
}
void usercpy(user_t *dest,user_t* src){
dest->state = src->state;
strcpy(dest->name,src->name);
strcpy(dest->location,src->location);
strcpy(dest->secret,src->secret);
}
void showUser(user_t* user){
printf("Name: %s\nLocation: %s\nState: %s\n\n",user->name,
user->location,state2str(user->state));
}
char* state2str(state_t state){
if(state==AUSENTE)
return "ausente";
else return "presente";
}
node_t* removeUser(char* user_name,node_t* list){
node_t* actual = list;
node_t* tmp ;
while(actual!=NULL){
if(strcmp(actual->user.name,user_name)==0){
actual->previous->next=actual->next;
tmp = actual->next;
free(actual);
actual = tmp;
}
actual=actual->next;
}
return NULL;
}
user_t* getUser(char* user_name,node_t* list){
node_t* actual = list;
while(actual!=NULL){
if(strcmp(actual->user.name,user_name)==0){
return &actual->user;
}
actual = actual->next;
}
return NULL;
}
void showUsers(node_t* list){
node_t* actual = list;
while(actual!=NULL){
showUser(&actual->user);
actual=actual->next;
}
}
void showMenu(){
printf("%i - Get state from 'user'\n",GET_STATE);
printf("%i - Post name,location and state - user_secret\n",POST_NAME_LOCATION_STATE);
printf("%i - Get location 'user'\n",GET_LOCATION);
printf("%i - Close conection\n\n",EXIT);
}