-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
131 lines (120 loc) · 3.22 KB
/
client.c
File metadata and controls
131 lines (120 loc) · 3.22 KB
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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
// mai intai clientul furnizeaza informatia => acesta scrie, serverul ii preia comenzile si le va transmite inapoi
void quit()
{
pid_t current_pid = getpid();
kill(current_pid,SIGKILL);
}
void writing_channel(char b[]){
int fd = open("canal", O_WRONLY);
if(fd == -1){
printf("An error occured at opening the channel");
exit(1);
}
write(fd, b, strlen(b)+1);
close(fd);
}
void reading_channel(char a[])
{
int fd = open("canal", O_RDONLY);
if(fd == -1){
printf("An error occured at opening the channel");
exit(1);
}
read(fd, a, 80);
close(fd);
}
void reading_users_info(){
FILE* fd= fopen("canal", "r");
if(fd == NULL){
printf("An error occured at opening the channel");
exit(1);
}
char buf[256];
while(fgets(buf, 256,fd) != NULL){
buf[strlen(buf)-1]='\0';
fprintf(stdout, "%s\n", buf);
fgets(buf, 256, fd);
buf[strlen(buf)-1]='\0';
fprintf(stdout, "%s\n", buf);
fgets(buf, 256, fd);
buf[strlen(buf)-1]='\0';
fprintf(stdout, "Sec %s\n", buf);
fgets(buf, 256, fd);
buf[strlen(buf)-1]='\0';
fprintf(stdout, "Usec %s\n\n\n", buf);
}
fclose(fd);
}
void reading_proc_info(){
FILE* fd= fopen("canal", "r");
if(fd == NULL){
printf("An error occured at opening the channel");
exit(1);
}
char buf[80];
int i;
for( i = 0; i < 5; i++) {
fgets(buf, 80, fd);
buf[strlen(buf)-1]='\0';
fprintf(stdout, "%s\n", buf);
}
fclose(fd);
}
int main()
{
int fd;
mkfifo("canal",0666);
char a[80], b[80];
int login = 0, ok = 0, ok1 = 0;
while(1){
if((strstr(a,"username")==NULL && login == 0) || (strstr(a,"id")==NULL && login == 1))
{
printf("CLIENT: ");
}
// se scrie si transmite informatia catre server
fgets(b, 80, stdin);
writing_channel(b);
printf("\n");
//se deschide canalul de citire pentru a primi informatiile de la server
if( strstr(b,"users")!=NULL && login == 1){ // pt comanda users: get-logged-users
reading_users_info();
}
else if( ok == 1 && login == 1 && ok1 == 0){ // pt comanda info : get-proc-info
reading_proc_info();
printf("\nCLIENT: ");
ok1 = 1;
}
else {
if(strstr(b,"info") != NULL) ok1 = 0; // pt a repeta comanda info
reading_channel(a); // pt celelalte comenzi
printf("SERVER: %s\n", a);
if( strstr(a,"User found") != NULL) {
login = 1;
}
else if( strstr(a,"Logged out") != NULL) {
login = 0;
}
}
//pt afisaj in consola
if(strstr(a,"username") !=NULL){
printf("Username: ");
}
else if(strstr(a,"process") !=NULL && ok1 == 0){
printf("PID: ");
ok = 1;
}
else if( strstr(a,"Goodbye") != NULL) {
quit();
}
}
return 0;
}