Skip to content
Snippets Groups Projects
main.go 2.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • package main
    
    import (
    	"flag"
    	"fmt"
    	"net"
    	"os"
    	"path/filepath"
    
    	pb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/plugin-registry"
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin"
    
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus/util"
    
    	"golang.org/x/mod/sumdb/dirhash"
    
    	pluginFilePath  = "plugins"
    	pluginStorePath = "plugin-store.json"
    
    )
    
    func main() {
    	socket := flag.String("socket", "9000", "port for the grpc socket, e.g. 9000")
    	flag.Parse()
    
    	lislisten, err := net.Listen("tcp", fmt.Sprintf(":%s", *socket))
    	if err != nil {
    		panic(err)
    	}
    
    	pr := registerPlugins()
    
    	grpcServer := grpc.NewServer()
    	server := NewServer(pr)
    	pb.RegisterPluginRegistryServiceServer(grpcServer, server)
    	if err := grpcServer.Serve(lislisten); err != nil {
    		panic(err)
    	}
    }
    
    // TODO: The registration of plugins should result in the same UUID's even
    // after reboot. Therefore it would make sense to use a database. For a simple
    // prototype this is currently hardcoded.
    func registerPlugins() *PluginRegistry {
    
    	dirs, err := os.ReadDir(pluginFilePath)
    
    	if err != nil {
    		panic(err)
    	}
    
    	pr := &PluginRegistry{}
    
    	pluginStore := NewStore(pluginStorePath)
    
    	plugins, err := pluginStore.GetAll()
    	if err != nil {
    		fmt.Println(err)
    	}
    
    
    	for _, dir := range dirs {
    		fmt.Printf("File %+v\n", dir)
    
    		if dir.IsDir() {
    			dirPath := filepath.Join(pluginFilePath, dir.Name())
    			manifest, err := plugin.ReadManifestFromFile(dirPath)
    
    			dirHashed, err := dirhash.HashDir(dirPath, dir.Name()+manifest.Firmware, dirhash.DefaultHash)
    			if err != nil {
    				panic(err)
    
    			var plugin *Plugin
    			for i, p := range plugins {
    				if p.Hash == dirHashed {
    					plugin = p
    					plugins[i] = plugins[len(plugins)-1]
    					plugins = plugins[:len(plugins)-1]
    					break
    				}
    			}
    
    			if plugin == nil {
    				plugin = &Plugin{
    					ID:       uuid.New(),
    					Path:     filepath.Join(dirPath, util.BundledPluginName),
    					Hash:     dirHashed,
    					Manifest: manifest,
    				}
    
    	for _, p := range pr.Plugins {
    		fmt.Printf("registeredPlugin: %v\n", p)
    	}
    
    
    	if err := pluginStore.Update(pr.Plugins); err != nil {
    		fmt.Println(err)
    	}