-
Notifications
You must be signed in to change notification settings - Fork 1
/
opePanelX.c
156 lines (137 loc) · 4.65 KB
/
opePanelX.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include "message.h"
#include "floor.h"
int main(int argc, char *argv[]) {
int i=0;
int server_sockfd, client_sockfd;
struct sockaddr_in server_address;
int addresslen = sizeof(struct sockaddr_in);
int fd;
fd_set readfds, testfds, clientfds;
struct message msg;
int fs_block_sz;
FILE *f, *fr;
/*Client variables=======================*/
int sockfd;
int result;
char hostname[]="localhost";
struct hostent *hostinfo;
struct sockaddr_in address;
int clientid;
FloorInfo floor;
/*Client==================================================*/
if(argc != 2){
printf("Sai tham số.\nUsage: floor [FLOOR_NO]\n");
exit(0);
}else{
int floorNo = atoi(argv[1]);
if (floorNo == -1) {
printf("Số tầng không tồn tại. Please recheck.\nUsage: floor [FLOOR_NO]\n");
exit(0);
} else if (floorNo > 5) {
printf("Chỉ có 5 tầng!\n");
exit(0);
} else if (floorNo == 1) {
printf("Tầng 1 đã được gọi. Hãy ơn gọi tầng khác !\n");
exit(0);
} else {
floor.floorNo = floorNo;
}
}
printf("*** Tầng %d bắt đầu kết nối...\n", floor.floorNo);
fflush(stdout);
/* Create a socket for the client */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server */
hostinfo = gethostbyname(hostname); /* look for host's name */
address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;
address.sin_family = AF_INET;
address.sin_port = htons(MYPORT);
/* Connect the socket to the server's socket */
if(connect(sockfd, (struct sockaddr *)&address, sizeof(address)) < 0){
perror("connecting");
exit(1);
}
/* init client variable */
clientid = -1;
fflush(stdout);
FD_ZERO(&clientfds);// init client description files
FD_SET(sockfd, &clientfds);// add socket client description file
FD_SET(0, &clientfds);// add socket client description file
/* Now wait for messages from the server */
while (1) {
testfds=clientfds;
select(FD_SETSIZE,&testfds,NULL,NULL,NULL);
for(fd=0;fd<FD_SETSIZE;fd++){
if(FD_ISSET(fd,&testfds)){
if(fd==sockfd){ /*Accept data from open socket */
//read data from open socket
result = read(sockfd, &msg, sizeof(struct message));
if (result == 0) return 1;
if (clientid < 0){ /* set client id before start */
if (msg.type == CONNECT_ACCEPTED){
printf("\nYêu cầu kết nối thành công\n");
setupMessage(&msg, REQUEST_JOIN, floor.floorNo);
write(sockfd, &msg, sizeof(struct message));
} else if (msg.type == JOIN_ACCEPTED){
clientid = 1;
printf("Join request is accepted (press \"0\" to exit)\n");
} else if (msg.type == JOIN_REJECTED){
perror("[ERROR] Server rejected connect for some reason\n");
close(sockfd);
exit(0);
} else {
printf("\n*** Unhandled message\n");
}
} else { /* start when clientid is set */
int id;
switch(msg.type){
case ELEVATOR_MOVING:
// printf("Door closes! Start moving...\n");
break;
case ELEVATOR_STOP:
// printf("Door opens!\n");
break;
case ELEVATOR_APPEAR:
printf("Thang máy đang ở tầng (%d)\n", (int)msg.floor);
break;
case SHUTDOWN_SERVER:
close(sockfd); //close the current client
exit(0);
break;
}
}
} else if(fd == 0){ /*process keyboard activiy*/
int floorRequest = -1;
fscanf(stdin, "%d", &floorRequest);
if (floorRequest == -1){
} else if (floorRequest == 0) {
printf("Đang đóng floor controller\n");
setupMessage(&msg, REQUEST_SHUTDOWN, 0);
write(sockfd, &msg, sizeof(struct message));
close(sockfd); //close the current client
exit(0); //end program
} else if (1 == floorRequest){
printf("You can\'t request to floor 1\n");
} else{
floorRequest = floor.floorNo;
setupMessage(&msg, REQUEST_FLOOR, floorRequest);
write(sockfd, &msg, sizeof(struct message));
}
}// end if
}// end if
}// end for
}// end while
}//main