-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.c
92 lines (79 loc) · 2.07 KB
/
driver.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
#include "utils.h"
enum states{SEND=1, RCV, PRINT_DETAILS, SHOW_MENU, QUIT, WAIT};
void print_banner();
int main(int argc, char* argv[]){
print_banner();
nmb_t nmbid = msgget_nmb();
enum states state = SHOW_MENU;
char ip[INET_ADDRSTRLEN], port[10], text[LENGTH];
struct msgbuf msg;
int type;
int my_port;
get_my_port(nmbid, &my_port);
printf("My Port: %d\n", my_port);
while(1){
printf(" __________________________ \n\n");
switch(state){
case SHOW_MENU:
printf("Choose an option:\n");
printf("1 Send a message\n");
printf("2 Read a message\n");
printf("3 Print Details\n");
printf("4 Show menu\n");
printf("5 Quit\n");
state = WAIT;
break;
case SEND:
printf("Enter IP:\n");
scanf("%s", ip);
printf("Enter Port:\n");
scanf("%s", port);
fgetc(stdin);
printf("Enter message:\n");
fgets(text, sizeof(text), stdin);
msg.mtype = get_mtype(ip, atoi(port));
strcpy(msg.mtext, text);
printf("Sending message...\n");
if(msgsnd_nmb(nmbid, msg, sizeof(msg), 0) < 0){
die("msgsnd_nmb() failed");
}
printf("Message sent...\n");
state = SHOW_MENU;
break;
case RCV:
printf("Reading a message...\n");
if(msgrcv_nmb(nmbid, &msg, sizeof(msg), (long)my_port, 0) < 0){
die("msgsnd_nmb() failed...");
}
printf("Here you go:\n");
printf("%s\n", msg.mtext);;
state = SHOW_MENU;
break;
case PRINT_DETAILS:
printf("My Port: %d\n", my_port);
state = SHOW_MENU;
case WAIT:
scanf("%d", &state);
break;
case QUIT:
msgrem_nmb(nmbid);
printf("Bye Bye...\n");
exit(0);
default:
printf("Bad choice, try again...\n");
state = SHOW_MENU;
}
}
return 0;
}
void print_banner(){
// tadaaa
printf("\n");
printf(" ** ** ** ** **** \n");
printf(" *** ** ** ** ** ** \n");
printf(" ****** *** *** **** \n");
printf(" ** *** ** * ** ** ** \n");
printf(" ** ** * ** ** * **** \n");
printf(" __________________________ \n");
printf("\n");
}