Skip to content
Snippets Groups Projects
Select Git revision
  • c2d5c814ce84beb97dd0e4ab819ad4cc09f36f80
  • master default protected
2 results

server.go

Blame
  • user avatar
    Xavier Perret authored
    c2d5c814
    History
    server.go 2.89 KiB
    package main
    
    import (
    	"context"
    	"fmt"
    	"gopkg.in/yaml.v3"
    	"log"
    	"net"
    	command_line "node/command-line"
    	ObjectStorage "node/object-storage"
    	ProcessConnection "node/process-connection"
    	. "node/types"
    	"node/utilities"
    	"os"
    	"strconv"
    	"sync"
    )
    
    /**
     * @file Server.go - Server for the blockchain
     * @brief Distributed Systems - Blockchain, each instance of this program is a node in the blockchain
     * @author Xavier Perret
     * @date 28/10/2022
     * @version 1.3
     */
    
    func listenForConnections(serverConfig Config, objectStorage Blob, addressToListenOn string, amIRoot bool) {
    	port := strconv.FormatInt(int64(serverConfig.Port), 10)
    	completeAddress := addressToListenOn + ":" + port
    
    	server, err := net.Listen("tcp", completeAddress)
    	if err != nil {
    		fmt.Println("Error while creating the server :", err)
    		os.Exit(1)
    	}
    
    	fmt.Println("Server is ready to accept connections")
    	fmt.Println("listening on ", completeAddress)
    
    	var mu sync.Mutex
    	for {
    		// Listening for connections
    		conn, err := server.Accept()
    		if err != nil {
    			fmt.Println("Error accepting: ", err.Error())
    		} else {
    			go ProcessConnection.ProcessClient(conn, server, objectStorage, serverConfig, amIRoot, &mu)
    		}
    	}
    }
    
    func main() {
    	ctx := context.Background()
    	argsLen := len(os.Args)
    	isThereEnoughArgs := argsLen <= 1
    	if isThereEnoughArgs {
    		fmt.Println("First argument should be the path of the config file '--path=<config.yaml>'")
    		os.Exit(1)
    	}
    
    	// init configuration
    	args := os.Args[1:]
    	fmt.Println("Program started with arguments", args)
    	fmt.Println("config file path is ", args[0][7:])
    
    	buf, err := os.ReadFile(args[0][7:])
    	if err != nil {
    		fmt.Println("Error while reading the config file", err)
    		fmt.Println("Exiting...")
    		os.Exit(1)
    	}
    
    	var serverConfig Config
    	err = yaml.Unmarshal(buf, &serverConfig)
    	if err != nil {
    		os.Exit(1)
    	}
    
    	utilities.PrintConfig(serverConfig)
    
    	amIRoot := argsLen == 3
    	if amIRoot {
    		if os.Args[3] == "--root" {
    			amIRoot = true
    		}
    	}
    	if !amIRoot {
    		fmt.Println("Third argument is not --root meaning this node is not meant to be directly stimulated by the client")
    	}
    
    	azureCreds := ObjectStorage.InitAzureCredentials("hepiadistributedsystems")
    	blobName := "blob-number-" + strconv.Itoa(serverConfig.ID)
    	objectStorage := ObjectStorage.InitializeBlobFromObjectStorageCreds(blobName, azureCreds)
    
    	addressToListenOn := "0.0.0.0"
    	go listenForConnections(serverConfig, objectStorage, addressToListenOn, amIRoot)
    	command_line.ServerUserInputLoop(serverConfig, true, objectStorage)
    
    	_, err = objectStorage.BlockBlobClient.Delete(ctx, nil)
    	if err != nil {
    		fmt.Println("Error while deleting the blob", err)
    		fmt.Println("This previous error is normal if the blob was already deleted or wasn't created")
    	}
    
    	// Delete the container
    	fmt.Println("Deleting the container " + objectStorage.BlobName)
    	_, err = objectStorage.ContainerClient.Delete(ctx, nil)
    
    	if err != nil {
    		log.Fatalf("Failure: %+v", err)
    	}
    }