-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
84 lines (74 loc) · 2.78 KB
/
main.c
File metadata and controls
84 lines (74 loc) · 2.78 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
/*Licensed under the MIT License.
For details on the licensing terms, see the LICENSE file.
SPDX-License-Identifier: MIT
Copyright 2024-2025 (c) Fraunhofer IOSB (Author: Florian Düwel)*/
#include "include/get_children.h"
#include "include/config_interpreter.h"
#include "include/client_configurator.h"
#include "pthread.h"
#include <stdio.h>
#include "signal.h"
#include "unistd.h"
/*//todo currently copied from https://github.com/LiamBindle/MQTT-C/blob/master/examples/templates/posix_sockets.h
#include "include/templates/posix_sockets.h"*/
UA_Boolean running = true;
static void stopHandler(int sign) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Received Ctrl-C");
running = 0;
}
static UA_ByteString loadFile(const char *const path) {
UA_ByteString fileContents = UA_STRING_NULL;
/* Open the file */
FILE *fp = fopen(path, "rb");
if(!fp) {
//errno = 0; /* We read errno also from the tcp layer... */
printf("failed to load the file\n");
return fileContents;
}
/* Get the file length, allocate the data and read */
fseek(fp, 0, SEEK_END);
fileContents.length = (size_t)ftell(fp);
fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
if(fileContents.data) {
fseek(fp, 0, SEEK_SET);
size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
if(read != fileContents.length)
UA_ByteString_clear(&fileContents);
} else {
fileContents.length = 0;
}
fclose(fp);
return fileContents;
}
int main(void) {
signal(SIGINT, stopHandler); /* catches ctrl-c */
sleep(5);
shopfloor_config shopfloor;
UA_ByteString sf_conf = loadFile("../configs/demo_scenario.json5");
get_shopfloor_dict(&shopfloor, sf_conf);
for (size_t i=0; i< shopfloor.nbr_resources; i++){
printf("Application Name: %s \n", shopfloor.clients[i].application_name);
printf("Resource IP: %s \n", shopfloor.clients[i].resource_ip);
printf("Port: %s \n", shopfloor.clients[i].port);
//shopfloor.clients[i].mqtt_client = &mqtt_client;
shopfloor.clients[i].running = &running;
printf("start client thread\n");
pthread_create(&shopfloor.clients[i].thread_id, NULL, start_client, &shopfloor.clients[i]);
}
while(running) {
sleep(1);
}
//wait for the threads to be terminated
sleep(5);
printf("shut down application\n");
/* Clean up */
for (size_t i=0; i< shopfloor.nbr_resources; i++){
free(shopfloor.clients[i].resource_ip);
free(shopfloor.clients[i].application_name);
free(shopfloor.clients[i].port);
free(shopfloor.clients[i].service_name);
}
free(shopfloor.clients);
UA_ByteString_clear(&sf_conf);
return EXIT_SUCCESS;
}