Add stub for protocoldb
build Details

This commit is contained in:
Marcel M. Otte 2023-04-12 19:49:12 +02:00
parent 0e932342e0
commit 80f1578fb5
1 changed files with 94 additions and 0 deletions

94
protocoldb/protocoldb.go Normal file
View File

@ -0,0 +1,94 @@
package protocoldb
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"gitea.mmo.to/ProtocolPacketForger/ppf/protocol"
"gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl"
)
type PPFDatabases struct {
Databases []ProtocolDatabaseProvider
}
type ProtocolDatabaseProvider interface {
Open(path string) error
Close() error
Protocols() map[string]protocol.DOPMeta
}
type ProtocolDatabaseManager interface {
Add(prot *protocol.ProtocolStructure, path string)
Update(prot *protocol.ProtocolStructure, path string)
// no remove.
UpdateDatabase() error // scans for new protocols
}
type DatabaseEntry struct {
Path string
Protocol protocol.DOPMeta
}
// FileDatabase implements ProtocolDatabaseProvider and ProtocolDatabaseManager
type FileDatabase struct {
Path string
protocols map[string]DatabaseEntry
closed bool
}
type FileDatabaseFromGitRemote struct {
FileDatabase
RemoteGitRepository string
}
func (fd *FileDatabase) Open(path string) error {
fileinfo, err := os.Stat(path)
if err == os.ErrNotExist {
return err
}
if !fileinfo.IsDir() {
return fmt.Errorf("Path %s is not a directory", path)
}
fd.Path = path
fd.closed = false
return nil
}
func (fd *FileDatabase) Close() error {
for k := range fd.protocols {
delete(fd.protocols, k)
}
fd.closed = true
return nil
}
func (fd *FileDatabase) Protocols() map[string]DatabaseEntry {
if !fd.closed {
fd.protocols = map[string]DatabaseEntry{}
// recurse recursively through the path
filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() {
prot, err := protocolctl.LoadNew(path)
if err != nil {
// add to map
fd.protocols[prot.Metadata.Name] = DatabaseEntry{path, prot.Metadata}
}
}
return nil
})
}
return fd.protocols
}
func (fd *FileDatabase) Add(prot *protocol.ProtocolStructure, path string) {
}
func (fd *FileDatabase) Update(prot *protocol.ProtocolStructure, path string) {
}
func (fd *FileDatabase) UpdateDatabase() error {
}