aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 171756476b9310c30a7182ad565cff2d7477f7bc (plain)
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
/*
 *
 * Copyright (C) 2022 Jeremias Stotter <jeremias@stotter.eu>
 *
 * This file is part of network-simulator.
 *
 * network-simulator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * network-simulator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with network-simulator. If not, see <https://www.gnu.org/licenses/>. 
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>

#include <gtk/gtk.h>

#include "shared.h"
#include "template.h"
#include "device.h"
#include "interface.h"
#include "connection.h"
#include "files.h"
#include "helpers.h"
#include "lua_functions.h"
#include "decorations.h"

#include "gui.h"

#define HELP_TEXT "The following options are available:\n\
-t <template folder> \n\
	Select the folder in which the templates are stored \n\
-h\n\
	Display this menu\n\
-s\n\
	Start in step mode\n\
"

char* packet_info_dir = NULL;

unsigned long timestamp = 0;

// This will increment on any action and be set to 0 when saving.
int actions_since_save = 0;

// This function is called to advance forward in time
int timestep() {
	timestamp++;
	if(!start_device) return true;
	// Info to pass to the gui
	struct timestep_entry_ll* start_entry = NULL;
	struct timestep_entry_ll* cur_entry = NULL;
	struct timestep_entry_ll* prev = NULL;
	// Go through all the devices
	LL_FOR(struct device, start_device, current_device) {
		for(int i = 0; i < current_device->template->slots; i++) {
			struct interface* current_interface = &current_device->interfaces[i];
			// Look if we have anything to send
			if(current_interface->start_queue && current_interface->start_queue->timestamp < timestamp) {
				struct connection* send_connection = current_interface->connection;
				if(!send_connection) continue;
				// Find out which side of the connection we are connected to
				int connection_side = send_connection->interface1 == current_interface ? 1 : 2;
				connection_send(send_connection, connection_side, current_interface->start_queue->data, current_interface->start_queue->data_length, current_interface->start_queue->data_type);
				// Generate the data we will pass to the gui
				struct timestep_entry_ll* last_entry = cur_entry;
				cur_entry = malloc_or_abort(sizeof(struct timestep_entry_ll));
				cur_entry->send_timestamp = timestamp;
				cur_entry->send_entry = current_interface->start_queue;
				cur_entry->next = NULL;
				cur_entry->prev = prev;
				if(!start_entry)
					start_entry = cur_entry;
				else
					last_entry->next = cur_entry;
				// Advance the start of the queue
				// Note wo don't free here, the gui still needs the info and will free the memory later				
				current_interface->start_queue = current_interface->start_queue->next;
				prev = cur_entry;
			}
		}
	}
	// Call every devices timestamp function
	for(struct device* cur_dev = start_device; cur_dev; cur_dev = cur_dev->next) {
		my_lua_call(cur_dev->lua_vm, "timestamp", 0, -1);
	}
	gui_timestep(timestamp, start_entry);
	return true;
}

int sim_flag = false;

int main(int argc, char** argv) {
	char option;
	char* template_dir_name = NULL;
	while((option = getopt(argc, argv, "ht:s")) != -1) {
		switch(option) {
			case('h'):
				printf(HELP_TEXT);
				return 0;
				break;
			case('t'):
				template_dir_name = optarg;
				break;
			case('s'):
				sim_flag = true;
				break;
			default:
				printf("option %c was given\n", option);
				break;
		}
	}

	if(parse_templates(template_dir_name) != 0) 
		return errno;

	if(optind < argc) {
		load_file(argv[optind]);
		save_path = malloc(strlen(argv[optind]) + 1);
		strcpy(save_path, argv[optind]);
	}
	
	// Start the GUI
	GtkApplication* app;
	int status;


#if GLIB_MINOR_VERSION<74
	#define G_APPLICATION_DEFAULT_FLAGS G_APPLICATION_FLAGS_NONE
#endif
	
	app = gtk_application_new(NULL, G_APPLICATION_DEFAULT_FLAGS);
	g_signal_connect(app, "activate", G_CALLBACK(activate_main_window), NULL);
	// We want to handle argv and argc ourselfes
	status = g_application_run(G_APPLICATION(app), 0, NULL);
	
	// terminate
	g_object_unref(app);
	return status;
}

Jeremias Stotters git repositories generated by CGIT