-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUDP(Daemon).c
More file actions
216 lines (187 loc) · 6.39 KB
/
ClientUDP(Daemon).c
File metadata and controls
216 lines (187 loc) · 6.39 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <dirent.h>
#include <mosquitto.h>
#include "mosquitto.h"
#define PORT 8080
#define MAX_PATH_LENGTH 1024
#define FILENAME "description.txt"
#define MQTT_BROKER "darshilpsheth.duckdns.org" // Replace with your MQTT broker address
#define MQTT_PORT 1883 // Replace with your MQTT broker port
#define MQTT_TOPIC "game"
// Function prototypes
int mosquitto_lib_init(void);
struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *userdata);
int mosquitto_connect(struct mosquitto *mosq, const char *host, int port, int keepalive);
int mosquitto_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, const void *payload, int qos, bool retain);
int mosquitto_disconnect(struct mosquitto *mosq);
void mosquitto_destroy(struct mosquitto *mosq);
int mosquitto_lib_cleanup(void);
struct mosquitto *mosq = NULL;
void printContents(const char *path) {
DIR *dir = opendir(path);
if (dir) {
printf("Contents of '%s':\n", path);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
printf("\n");
closedir(dir);
} else {
printf("Unable to open directory '%s'\n", path);
}
}
void printDescription(const char *path) {
FILE *file = fopen(path, "r");
if (file != NULL) {
char buffer[1024];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
} else {
printf("Unable to open description.txt in %s\n", path);
}
}
void sendMQTTMessage(const char *message) {
mosquitto_publish(mosq, NULL, MQTT_TOPIC, strlen(message), message, 1, false);
printf("Message \"%s\" published to MQTT topic \"%s\"\n", message, MQTT_TOPIC);
}
void sendDescriptionFile(const char *path) {
FILE *file = fopen(path, "r");
if (file != NULL) {
char buffer[1024];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
sendMQTTMessage(buffer);
}
fclose(file);
} else {
printf("Unable to open description.txt in %s\n", path);
}
}
int main() {
pid_t pid, sid;
pid = fork();
// If fork fails, exit
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
// If we got a good PID, then we can exit the parent process
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// Change the file mode mask
umask(0);
// Create a new SID for the child process
sid = setsid();
if (sid < 0) {
perror("SID creation failed");
exit(EXIT_FAILURE);
}
// Change the current working directory
if ((chdir("/")) < 0) {
perror("Change directory failed");
exit(EXIT_FAILURE);
}
// Close standard file descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
int sock;
struct sockaddr_in server;
char buffer[128];
char currentPath[MAX_PATH_LENGTH] = "/home/darshilsheth491/startroom/north";
// Create socket
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
// Bind
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
// Mosquitto setup
mosquitto_lib_init();
mosq = mosquitto_new("ExampleClient", true, NULL);
if (!mosq) {
fprintf(stderr, "Failed to initialize Mosquitto library\n");
exit(EXIT_FAILURE);
}
if (mosquitto_connect(mosq, MQTT_BROKER, MQTT_PORT, 20) != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "Failed to connect to the MQTT broker\n");
exit(EXIT_FAILURE);
}
sendDescriptionFile("/home/darshilsheth491/startroom/north/");
while (1) {
char tempPath[MAX_PATH_LENGTH];
strcpy(tempPath, currentPath);
memset(buffer, 0, sizeof(buffer));
// Receive data
if (recvfrom(sock, buffer, sizeof(buffer), 0, NULL, NULL) == -1) {
perror("Receive failed");
exit(EXIT_FAILURE);
}
printf("\nReceived message: %s\n", buffer);
char newPath[MAX_PATH_LENGTH];
snprintf(newPath, sizeof(newPath), "%s/%s", currentPath, buffer);
printf("Current path:%s", newPath);
printContents(newPath);
// Check for symbolic link 'back'
char linkPath[PATH_MAX];
snprintf(linkPath, sizeof(linkPath), "%s/back", newPath);
if (access(linkPath, F_OK) != -1) {
// 'back' symbolic link found, navigate into it
if (chdir(linkPath) == 0) {
printf("Navigated to '%s'\n", linkPath);
strcpy(newPath, linkPath);
DIR *dir = opendir(newPath);
if (chdir(newPath) == 0) {
if (access(FILENAME, F_OK) == 0) {
printf("File '%s' found in directory '%s'. Contents:\n", FILENAME, newPath);
printDescription(FILENAME);
strcpy(currentPath, newPath);
sendDescriptionFile(FILENAME); // Send description file to MQTT
}
}
continue; // Skip further processing in this iteration
}
}
printContents(newPath);
DIR *dir = opendir(newPath);
if (dir) {
closedir(dir);
if (chdir(newPath) == 0) {
if (access(FILENAME, F_OK) == 0) {
printf("File '%s' found in directory '%s'. Contents:\n", FILENAME, newPath);
printDescription(FILENAME);
strcpy(currentPath, newPath);
sendDescriptionFile(FILENAME); // Send description file to MQTT
}
} else {
printf("You cannot go that way!\n");
strcpy(newPath, tempPath);
}
} else {
printf("You cannot go that way!\n");
strcpy(newPath, tempPath);
}
}
// Cleanup and close Mosquitto
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
close(sock);
return 0;
}