-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_log.c
174 lines (129 loc) · 3.71 KB
/
server_log.c
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
//
// log.c
// SD15-Product
//
// Created by Nuno Alexandre on 13/12/14.
// Copyright (c) 2014 Nuno Alexandre. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "general_utils.h"
#include "message.h"
#include "server_log.h"
#include "network_utils.h"
#include "table_skel.h"
#include "table.h"
FILE *_log_fd;
char * _log_file;
/*
* checks if the server_log_file exists and is accessable to read&write
*/
int server_log_setup_file() {
_log_fd = fopen(_log_file, "ab+");
return _log_fd != NULL;
}
/*
* initializes the log
*/
void server_log_init( char * filepath ) {
server_log_file_create(filepath);
if (server_log_setup_file() == FAILED );
}
void server_log_file_create(char * filepath) {
int fname_size = (int) strlen(filepath) + 8 + 1;
_log_file = malloc ( fname_size);
malloc ( fname_size );
_log_file[0] = '\0'; // ensures the memory is an empty string
strcat(_log_file,filepath);
strcat(_log_file,"_LOG.txt");
}
/*
* Logs buffer message into the server_log_file
*/
int server_log_write(char * buffer, int n_bytes ) {
FILE* fp = fopen(_log_file, "a");
if ( fp == NULL )
return FAILED;
fprintf(fp, "%s\n",buffer);
if ( buffer != NULL )
free(buffer);
fclose(fp);
return SUCCEEDED;
}
int server_log_message( struct message_t * message ) {
char * buffer = message_to_string(message);
return server_log_write(buffer, (int) strlen(buffer));
}
/*
* Returns a message_t * built from the command string.
* Assumes the command is valid.
*/
struct message_t * server_log_to_message (const char * command, int callFromServer ) {
//get opcode
int opcode = find_opcode_as_int(command);
//get ctype
int ctype = find_ctype(command);
void * message_content = NULL;
if ( ctype == CT_TUPLE ) {
message_content = create_tuple_from_input (command);
}
else if ( ctype == CT_ENTRY ) {
message_content = entry_create_from_string(command);
}
else if ( ctype == CT_RESULT ) {
int resultValue = 0;
message_content = &resultValue;
}
//create and return message
struct message_t * message = message_create_with(opcode, ctype, message_content);
return message;
}
int server_log_invoke_over_table(struct table_t * table) {
if ( table==NULL)
return FAILED;
char * line = NULL;
size_t len = 0;
ssize_t read;
FILE* fp = fopen(_log_file, "r");
if (fp == NULL)
return 0;
while( (read = getline(&line, &len, fp) ) != -1 ) {
struct message_t * operation = server_log_to_message(line, YES);
struct message_t ** msg_out = NULL;
invoke(operation, &msg_out);
free_message2(operation, NO);
}
return SUCCEEDED;
}
int server_log_send_to ( int addressee_fd, int from_operation_n ) {
char * line = NULL;
size_t len = 0;
ssize_t read;
FILE* fp = fopen(_log_file, "r");
if (fp == NULL)
return 0;
int n_operation = 0;
while ( (read = getline(&line, &len, fp) ) != -1 ) {
n_operation++;
if ( n_operation > from_operation_n ) {
struct message_t * operation = server_log_to_message(line, YES);
if ( send_message(addressee_fd, operation) == SUCCEEDED )
free_message(operation);
}
}
return SUCCEEDED;
}
//table_skel_update_neighboor
void server_log_print() {
char * line = NULL;
size_t len = 0;
ssize_t read;
FILE* fp = fopen(_log_file, "r");
if (fp == NULL)
return;
while( (read = getline(&line, &len, fp) ) != -1 )
printf("%s\n", line);
fclose(fp);
}