-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_user.c
189 lines (167 loc) · 4.33 KB
/
server_user.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Implemented by Rakim
#ifndef SERVER
#define SERVER
#include <stdio.h>
#include <stdlib.h>
#include "log.c"
#include "student.h"
#include "usrcode/main_mem_cpy.c"
#include "tuple.h"
#include "generate_students.c"
// Functional Prototypes
void server();
void registerStudent();
void delete(int id, node* head);
void printFile(char* filename);
int main()
{
//generate();
server();
}
void server()
{
node *head = NULL;
char op_id = 0; // OPERATION ID, 0->registration, 1-> search, 2-> update, 3-> delete.
int st_id = 0;
// FILE *waitingQueue = fopen("waiting_queue", "r");
// tuple temp;
int count = 0;
double time = GetTime();
// int sum = 0;
while (1)
{
//fputs("> ", stdout);
scanf("%c", &op_id);
//op_id = '1';
// st_id = temp.stdID;
// op_id = '0'+temp.opID;
// int newRoomNumber = temp.roomNo;
// if (op_id == '2')
// {
// printf("%c %d %d\n", op_id, st_id, temp.roomNo);
// }
// else
// {
// printf("%c %d\n", op_id, st_id);
// }
switch(op_id)
{
case '0':
registerStudent();
break;
case '1':
scanf("%d", &st_id);
search(&head, st_id);
break;
case '2':
int newRoomNumber;
scanf("%d", &st_id);
scanf("%d\n", &newRoomNumber);
update(&head, st_id, newRoomNumber);
break;
case'3':
//scanf("%d", &st_id);
delete(st_id, head);
//printFile("disk.tmp");
break;
case '4':
printFile("disk");
break;
}
count++;
if (GetTime()-time >= 1)
{
printf("Throughput: %d\n", count);
count = 0;
time = GetTime();
}
}
while (head != NULL)
{
removeHeadNode(&head, 1);
}
printf("Mean Response Time: %lf\n", (GetTime()-time)/(1.0*count));
printf("Throughput: %lf\n", (1.0*count)/(GetTime()-time));
}
// Register a user and update the file accordingly
// First get the last roll no. used and then use generate_student(id) to write it to the file.
// Use printFile to see if the file is updated.
void registerStudent()
{
writeTwo();
FILE* db = fopen("disk", "r+");
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
int id = 0;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
id = temp.id;
}
struct Student s;
id++;
s = generateStudent(id);
fwrite(&s, 1, sizeof(struct Student), db);
fclose(db);
}
// Delete a line from the file
void delete(int id, node* head)
{
/*
*
* Implement the logic for deleting here.
*
* First search file, but instead of returning a number, get the line number.
* Now make a new copy of the file and copy all the lines except that one.
* Delete the original file and rename the current file to disk.
*
* refer https://www.w3resource.com/c-programming-exercises/file-handling/c-file-handling-exercise-8.php
*
*/
FILE* db = fopen("disk", "r");
FILE* dbTemp = fopen("disk.tmp", "w");
writeTwo();
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
if (dbTemp == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
if (temp.id != id)
{
fwrite(&temp, 1, sizeof(struct Student), dbTemp);
}
}
fclose(db);
fclose(dbTemp);
remove("disk");
rename("disk.tmp", "disk");
deleteNode(&head, id);
return;
}
void printFile(char* filename)
{
FILE* db = fopen(filename, "r");
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
printf("%d: %s\n", temp.id, temp.name);
}
fclose(db);
}
#endif