[Refactor] Shared VFS template for CoW foundation.
Refactors the Virtual file System creation process to improve memory efficiency at scale. This is laying the ground work for a copy-on-write system. Previously, each new VFS instance was a full copy, which won't scale to a large number of NPC's. Especially now that we are loading in entire Lua scripts. - Added VFSManager class which now manages the lifecycle of all VFS instances. - VFSManager creates a single "template" VFS on initilisation. The template holds the shared, read-only directories like '/bin' and their command scripts. - When a new VFS is created (for a client or NPC), it links it's 'bin' directory to the shared template's '/bin' by pointer rather than creating a copy of it. this makes sure the content for *all* common command scripts exists in memory only once, regardless of the number of NPC's we will later generate.
This commit is contained in:
		
							parent
							
								
									62d5f0526f
								
							
						
					
					
						commit
						ea0605711d
					
				@ -20,7 +20,8 @@ Terminal::Terminal(void) {
 | 
			
		||||
  _should_close = false;
 | 
			
		||||
 | 
			
		||||
  /* Create local file system for the client. */
 | 
			
		||||
  _local_vfs = vfs_manager::create_root_system("local");
 | 
			
		||||
  VFSManager vfs_manager;
 | 
			
		||||
  _local_vfs = vfs_manager.create_vfs("local");
 | 
			
		||||
  _local_cmd_processor = new CommandProcessor(_local_vfs);
 | 
			
		||||
  _current_path  = get_full_path(_local_cmd_processor->get_current_dir());
 | 
			
		||||
  _scroll_offset = 0;
 | 
			
		||||
 | 
			
		||||
@ -10,25 +10,16 @@ vfs_node* new_node(std::string name, vfs_node_type type, vfs_node* parent) {
 | 
			
		||||
  return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vfs_node* vfs_manager::create_root_system(const std::string& system_type) {
 | 
			
		||||
  /* Rood directory. */
 | 
			
		||||
  vfs_node* root = new_node("/", DIR_NODE, nullptr);
 | 
			
		||||
 | 
			
		||||
  /* Subdirectories. */
 | 
			
		||||
  vfs_node* home = new_node("home", DIR_NODE, root);
 | 
			
		||||
  vfs_node* bin  = new_node("bin", DIR_NODE, root);
 | 
			
		||||
  root->children["home"]  = home;
 | 
			
		||||
  root->children["bin"]   = bin;
 | 
			
		||||
 | 
			
		||||
  /* User diractory. */
 | 
			
		||||
  vfs_node* user = new_node("user", DIR_NODE, home);
 | 
			
		||||
  home->children["user"] = user;
 | 
			
		||||
 | 
			
		||||
  /* Create file. */
 | 
			
		||||
  vfs_node* readme = new_node("readme.txt", FILE_NODE, user);
 | 
			
		||||
  readme->content = "Welcome to your new virtual machine.";
 | 
			
		||||
  user->children["readme.txt"] = readme;
 | 
			
		||||
VFSManager::VFSManager(void) {
 | 
			
		||||
  /* Create template VFS that holds shared, read-only directories. */
 | 
			
		||||
  _vfs_root = new_node("/", DIR_NODE, nullptr);
 | 
			
		||||
  vfs_node* bin = new_node("bin", DIR_NODE, _vfs_root);
 | 
			
		||||
  _vfs_root->children["bin"] = bin;
 | 
			
		||||
 | 
			
		||||
  /* TODO:
 | 
			
		||||
   * Load all scripts from assets/scripts/bin into the bind node.
 | 
			
		||||
   * We'll create ls.lua manually for now.
 | 
			
		||||
   */
 | 
			
		||||
  vfs_node* ls_script = new_node("ls.lua", FILE_NODE, bin);
 | 
			
		||||
  ls_script->content = R"lua(-- /bin/ls.lua - Lists files in a directory.
 | 
			
		||||
                             local dir = current_dir -- Get directory object from C++.
 | 
			
		||||
@ -46,6 +37,26 @@ vfs_node* vfs_manager::create_root_system(const std::string& system_type) {
 | 
			
		||||
                             )lua";
 | 
			
		||||
 | 
			
		||||
  bin->children["ls.lua"] = ls_script;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
VFSManager::~VFSManager(void) {
 | 
			
		||||
  /* TODO: Implement recursive deletion of all created VFS nodes.*/
 | 
			
		||||
  //delete _vfs_root;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vfs_node* VFSManager::create_vfs(const std::string& system_type) {
 | 
			
		||||
  vfs_node* root = new_node("/", DIR_NODE, nullptr);
 | 
			
		||||
 | 
			
		||||
  /* Create directories for this specific VFS. */
 | 
			
		||||
  vfs_node* home = new_node("home", DIR_NODE, root);
 | 
			
		||||
  vfs_node* user = new_node("user", DIR_NODE, home);
 | 
			
		||||
  home->children["user"] = user;
 | 
			
		||||
  vfs_node* readme = new_node("readme.txt", FILE_NODE, user);
 | 
			
		||||
  readme->content = "Welcome to your new virtual machine.";
 | 
			
		||||
  user->children["readme.txt"] = readme;
 | 
			
		||||
 | 
			
		||||
  /* Link to the shared directories from the template. */
 | 
			
		||||
  root->children["bin"] = _vfs_root->children["bin"];
 | 
			
		||||
 | 
			
		||||
  if(system_type == "npc") {
 | 
			
		||||
    vfs_node* npc_file = new_node("npc_system.txt", FILE_NODE, root);
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,16 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
#include "vfs.h"
 | 
			
		||||
 | 
			
		||||
namespace vfs_manager {
 | 
			
		||||
  vfs_node* create_root_system(const std::string& system_type);
 | 
			
		||||
}
 | 
			
		||||
class VFSManager {
 | 
			
		||||
public:
 | 
			
		||||
  VFSManager(void);
 | 
			
		||||
  ~VFSManager(void); /* TODO: Implement recursive VFS deletion. */
 | 
			
		||||
 | 
			
		||||
  vfs_node* create_vfs(const std::string& system_type);
 | 
			
		||||
private:
 | 
			
		||||
  vfs_node* _vfs_root;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -9,10 +9,12 @@
 | 
			
		||||
int main(int argc, char** argv) {
 | 
			
		||||
  printf("=== Server starting ===\n");
 | 
			
		||||
 | 
			
		||||
  VFSManager vfs_manager;
 | 
			
		||||
 | 
			
		||||
  /* Create the world map of networks. */
 | 
			
		||||
  std::map<std::string, vfs_node*> world_vfs;
 | 
			
		||||
  world_vfs["8.8.8.8"] = vfs_manager::create_root_system("npc"); /* Basic npc system. */
 | 
			
		||||
  world_vfs["10.0.2.15"] = vfs_manager::create_root_system("npc"); /* Another one. */
 | 
			
		||||
  world_vfs["8.8.8.8"] = vfs_manager.create_vfs("npc"); /* Basic npc system. */
 | 
			
		||||
  world_vfs["10.0.2.15"] = vfs_manager.create_vfs("npc"); /* Another one. */
 | 
			
		||||
  printf("Created world with %zu networks.\n", world_vfs.size());
 | 
			
		||||
 | 
			
		||||
  /* Network manager now managed the whole world. */
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user