aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2024-03-31 16:03:08 +0200
committerJeremias Stotter <jeremias@stotter.eu>2024-03-31 18:01:58 +0200
commit0430a3a0184931f26f6435ab6c3d93a54400bd8c (patch)
tree708afe439d3b405e07cb7821d4e32348f1123e6a
parent4371c1f460fbe2e6bb99101166a1c6bf6abd80cf (diff)
downloadnetwork-simulator-0430a3a0184931f26f6435ab6c3d93a54400bd8c.tar.gz
network-simulator-0430a3a0184931f26f6435ab6c3d93a54400bd8c.tar.bz2
network-simulator-0430a3a0184931f26f6435ab6c3d93a54400bd8c.zip
Add interface lua state reloading
-rw-r--r--device.c25
-rw-r--r--device.h2
-rw-r--r--gui.c2
-rw-r--r--interface.c20
-rw-r--r--interface.h2
5 files changed, 36 insertions, 15 deletions
diff --git a/device.c b/device.c
index ca1e646..0210dab 100644
--- a/device.c
+++ b/device.c
@@ -350,21 +350,36 @@ struct device* dev_from_name(struct device* start, const char* name) {
}
// Reload all the templates lua files
-void reload_templates() {
+void dev_reload_templates() {
LL_FOR(struct device, start_device, dev) {
// Ask the device to save
- if(my_lua_call(dev->lua_vm, "save_state", 1, -1)) {
+ if(dev->lua_vm && my_lua_call(dev->lua_vm, "save_state", 1, -1)) {
size_t state_len = 0;
- const char* state = lua_tolstring(dev->lua_vm, -1, &state_len);
+ const char* state_temp = lua_tolstring(dev->lua_vm, -1, &state_len);
+
+ // Need to copy the string to a temporary buffer because lua_close will free it
+ char* state = malloc_or_abort(state_len+1);
+ memcpy(state, state_temp, state_len+1);
// Destroy the lua vm
lua_close(dev->lua_vm);
// Create the new vm
dev->lua_vm = init_dev_lua_state(dev);
// Load the state in
-
my_lua_call(dev->lua_vm, "load_state", 1, LUA_TSTRING, state_len, state, -1);
+
+ free(state);
+ }
+
+ // Reload interface VMs
+ for(int i = 0; i < dev->template->slots; i++) {
+ if((!dev->interfaces[i].template) || (!dev->interfaces[i].lua_vm))
+ continue;
+
+ lua_close(dev->interfaces[i].lua_vm);
+
+ dev->interfaces[i].lua_vm = init_interface_lua_state(&(dev->interfaces[i]));
+
}
}
- // @todo also add something like this for interfaces
}
diff --git a/device.h b/device.h
index 23e85ac..2d27317 100644
--- a/device.h
+++ b/device.h
@@ -28,5 +28,5 @@ int device_cmdline(struct device* target_device, const char* cmdline);
void append_cmdline(struct device* dev, const char* in_string, int in_string_len);
struct device* dev_from_name(struct device* start, const char* name);
void append_cmd_history(struct device* dev, const char* text);
-void reload_templates();
+void dev_reload_templates();
#endif
diff --git a/gui.c b/gui.c
index 9328126..dfdc556 100644
--- a/gui.c
+++ b/gui.c
@@ -1565,7 +1565,7 @@ void gui_toggle_interface_nums_action(GSimpleAction* action, GVariant* value, gp
}
void gui_reload_templates(GSimpleAction* action, GVariant* value, gpointer user_data) {
- reload_templates();
+ dev_reload_templates();
}
void packet_list_item_text_factory_bind(GtkSignalListItemFactory* self, GObject* object, gpointer user_data) {
diff --git a/interface.c b/interface.c
index 79510c9..3aab89d 100644
--- a/interface.c
+++ b/interface.c
@@ -59,6 +59,17 @@ int new_interface_from_template_name(struct device* parent_device, int slot_inde
return new_interface(parent_device, slot_index, cur_template);
}
+lua_State* init_interface_lua_state(struct interface* new_interface) {
+ lua_State *lua_vm = luaL_newstate();
+ register_lua_helpers(lua_vm);
+ setup_interface_functions(lua_vm);
+ luaL_openlibs(lua_vm);
+ if(luaL_dofile(lua_vm, new_interface->template->firmware) != 0) {
+ printf("LUA ERROR: %s\n", lua_tostring(lua_vm, -1));
+ }
+ return lua_vm;
+}
+
int new_interface(struct device* parent_device, int slot_index, struct interface_template* cur_template) {
if(!parent_device) return -1;
if(slot_index > parent_device->template->slots - 1) {
@@ -82,15 +93,8 @@ int new_interface(struct device* parent_device, int slot_index, struct interface
new_interface->connection = NULL;
new_interface->start_queue = NULL;
// initiate the firmware for this device
- lua_State *lua_vm = luaL_newstate();
- register_lua_helpers(lua_vm);
- setup_interface_functions(lua_vm);
- luaL_openlibs(lua_vm);
- if(luaL_dofile(lua_vm, new_interface->template->firmware) != 0) {
- printf("LUA ERROR: %s\n", lua_tostring(lua_vm, -1));
- }
- new_interface->lua_vm = lua_vm;
+ new_interface->lua_vm = init_interface_lua_state(new_interface);
return 0;
}
diff --git a/interface.h b/interface.h
index 5a1c137..5c1ffab 100644
--- a/interface.h
+++ b/interface.h
@@ -13,10 +13,12 @@
#ifndef NETSIM_INTERFACE
#define NETSIM_INTERFACE
+#include <lua.h>
#include "shared.h"
int new_interface(struct device* parent_device, int slot_index, struct interface_template* cur_template);
int new_interface_from_template_name(struct device* parent_device, int slot_index, const char* template_name);
int delete_interface(struct interface* del_interface);
int interface_send_frame(struct interface* send_interface, const char* frame, int frame_len, const char* frame_type);
int interface_recv_data(struct interface* recv_interface, const char* data, int data_length, const char* data_type);
+lua_State* init_interface_lua_state(struct interface* new_interface);
#endif
Jeremias Stotters git repositories generated by CGIT