aboutsummaryrefslogtreecommitdiff
path: root/connection.c
blob: 0b17b2be96f08d284dc27c89b9bcc19b79c75df9 (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
/*
 *
 * 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 <string.h>

#include "shared.h"
#include "template.h"
#include "interface.h"
#include "gui.h"
#include "connection.h"
#include "helpers.h"
#include "lua_functions.h"

int new_connection_from_template_name(struct interface* interface1, struct interface* interface2, const char* template_name) {
	struct connection_template* cur_template = connection_templates;
	bool template_found = false;
	for(int i = 0; i < connection_template_n; i++, cur_template = &connection_templates[i]) {
		if(strcmp(cur_template->name, template_name) == 0) {
			template_found = true;
			break;
		}
	}

	if(!template_found) {
		fprintf(stderr, "Template %s was not found", template_name);
		return -1;
	}
	
	return new_connection(interface1, interface2, cur_template);
}

// Create a new connection between two interfaces
int new_connection(struct interface* interface1, struct interface* interface2, struct connection_template* cur_template) {
	if(!interface1 || !interface2 || !interface1->template || !interface2->template) return -1;
	if(interface1 == interface2) {
		fprintf(stderr, "Can't connect a plug to the same port twice!\n");
		return -1;
	}

	// Check if selected ports are of the right type
	if(strcmp(cur_template->plug, interface1->template->port_type) != 0) {
		fprintf(stderr, "Plug type %s is not compatible with interface %d in device %s", cur_template->plug, interface1->parent_index, interface1->parent_device->name);
		return -1;
	}
	if(strcmp(cur_template->plug, interface2->template->port_type) != 0) {
		fprintf(stderr, "Plug type %s is not compatible with interface %d in device %s", cur_template->plug, interface2->parent_index, interface2->parent_device->name);
		return -1;
	}

	struct connection* new_connection = calloc_or_abort(sizeof(struct connection), 1);
	new_connection->template = cur_template;
	new_connection->interface1 = interface1;
	new_connection->interface2 = interface2;

	interface1->connection = new_connection;
	interface2->connection = new_connection;

	// Call a device handler for establishing a new connection
	my_lua_call(interface1->parent_device->lua_vm,
				"new_connection", 1, LUA_TNUMBER,
				interface1->parent_index, -1);
	my_lua_call(interface2->parent_device->lua_vm,
				"new_connection", 1, LUA_TNUMBER,
				interface2->parent_index, -1);

	return 0;
}

// Return 0 if we deleted the connection, return 1 if the connection to delete doesn't exist
int delete_connection(struct connection* del_connection) {
	if(!del_connection) return 1;
	del_connection->interface1->connection = NULL;
	del_connection->interface2->connection = NULL;

	// Call device to inform of connection loss
	my_lua_call(del_connection->interface1->parent_device->lua_vm,
				"remove_connection", 1, LUA_TNUMBER,
				del_connection->interface1->parent_index, -1);
	my_lua_call(del_connection->interface2->parent_device->lua_vm,
				"remove_connection", 1, LUA_TNUMBER,
				del_connection->interface2->parent_index, -1);

	free(del_connection);
	return 0;
}

// This gets called by an interface if it wants to send something
int connection_send(struct connection* send_connection, int connection_side, const char* send_data, int data_length, const char* data_type) {
	struct interface* recv_interface = NULL;

	if(connection_side == 1) {
		recv_interface = send_connection->interface2;
	} else if (connection_side == 2) {
		recv_interface = send_connection->interface1;
	} else {
		fprintf(stderr, "Uhh this should not happen, can't send from that interface'\n");
		return -1;
	}

	// Tell the gui we sent something, it will know what to change because of the fifo approach of device sending queues
	gui_con_send(send_connection, connection_side);

	interface_recv_data(recv_interface, send_data, data_length, data_type);
	return 0;
}
Jeremias Stotters git repositories generated by CGIT