Update collection of protocols to state of architecture
This commit is contained in:
parent
23da938cc9
commit
e3b1091e9f
|
@ -17,6 +17,9 @@ import (
|
|||
"gitea.mmo.to/ppForge/ppforge/protocol"
|
||||
)
|
||||
|
||||
// globalCOP is the global variable holding the protocol collections
|
||||
var globalCOP ProtocolCollectionList
|
||||
|
||||
// ProtocolCollectionList is a list of all databases, with the most simple interface
|
||||
type ProtocolCollectionList struct {
|
||||
PCs []COP
|
||||
|
@ -53,16 +56,20 @@ type ProtocolCollectionEntry struct {
|
|||
}
|
||||
|
||||
// Init initializes the databases, opens the default one and checks for others if available
|
||||
func Init() *ProtocolCollectionList {
|
||||
func init() {
|
||||
// initialize main list
|
||||
databases := ProtocolCollectionList{[]COP{}}
|
||||
globalCOP := ProtocolCollectionList{[]COP{}}
|
||||
// initialize default db
|
||||
fdb := FileCOP{COP{map[string]ProtocolCollectionEntry{}, false}, globals.CollectionOfProtocolsDir}
|
||||
fdb.Open(globals.CollectionOfProtocolsDir)
|
||||
fdb.Sync()
|
||||
databases.PCs = append(databases.PCs, fdb.COP)
|
||||
log.Printf("Amount of databases available %d", len(databases.PCs))
|
||||
return &databases
|
||||
globalCOP.PCs = append(globalCOP.PCs, fdb.COP)
|
||||
log.Printf("Amount of databases available %d", len(globalCOP.PCs))
|
||||
}
|
||||
|
||||
// GetCOP returns the global collection of Protocols
|
||||
func GetCOP() ProtocolCollectionList {
|
||||
return globalCOP
|
||||
}
|
||||
|
||||
// COP represents a Collection of Protocols
|
||||
|
@ -71,6 +78,17 @@ type COP struct {
|
|||
closed bool
|
||||
}
|
||||
|
||||
// COPer interface defines the functions a CollectionOfProtocols should have at least.
|
||||
type COPer interface {
|
||||
Open(string) error
|
||||
Close() error
|
||||
Protocols() ([]ProtocolCollectionEntry, error)
|
||||
Add(*protocol.Protocol) error
|
||||
Sync() error
|
||||
Update(*protocol.Protocol) error
|
||||
Get(string) (*protocol.Protocol, error)
|
||||
}
|
||||
|
||||
// FileCOP implements
|
||||
type FileCOP struct {
|
||||
COP
|
||||
|
@ -176,3 +194,9 @@ func (fd *FileCOP) Sync() error {
|
|||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// Get a protocol by name
|
||||
func (fd *FileCOP) Get(prot string) (*protocol.Protocol, error) {
|
||||
entry := fd.protocols[prot]
|
||||
return protocol.LoadNew(entry.Path)
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
// Protocol structure
|
||||
type Protocol struct {
|
||||
Metadata ProtocolMeta
|
||||
Structure []*Field
|
||||
|
@ -14,23 +15,29 @@ type Protocol struct {
|
|||
JavaScript string
|
||||
}
|
||||
|
||||
func (p Protocol) ToJson() string {
|
||||
data, err := json.MarshalIndent(p, "", " ")
|
||||
// ToJSON conversion
|
||||
func (prot Protocol) ToJSON() string {
|
||||
data, err := json.MarshalIndent(prot, "", " ")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// NewProtocolStructure which is empty
|
||||
func NewProtocolStructure() *Protocol {
|
||||
p := Protocol{}
|
||||
return &p
|
||||
}
|
||||
|
||||
// AppendField to protocol
|
||||
func (prot *Protocol) AppendField(field Field) int {
|
||||
i := len(prot.Structure)
|
||||
prot.Structure = append(prot.Structure, &field)
|
||||
return i + 1
|
||||
}
|
||||
|
||||
// AddField to protocol
|
||||
func (prot *Protocol) AddField(index int, field *Field) {
|
||||
if len(prot.Structure) == index {
|
||||
prot.AppendField(*field)
|
||||
|
@ -43,6 +50,7 @@ func (prot *Protocol) AddField(index int, field *Field) {
|
|||
prot.Structure = ret
|
||||
}
|
||||
|
||||
// UpdateFieldByName in the protocol
|
||||
func (prot *Protocol) UpdateFieldByName(name string, field *Field) {
|
||||
var fnd int = -1
|
||||
for i, f := range prot.Structure {
|
||||
|
@ -55,10 +63,12 @@ func (prot *Protocol) UpdateFieldByName(name string, field *Field) {
|
|||
}
|
||||
}
|
||||
|
||||
// UpdateFieldByElement in the protocol
|
||||
func (prot *Protocol) UpdateFieldByElement(element int, field *Field) {
|
||||
prot.Structure[element] = field
|
||||
}
|
||||
|
||||
// RemoveFieldByName from the protocol
|
||||
func (prot *Protocol) RemoveFieldByName(name string) {
|
||||
element := -1
|
||||
for i, f := range prot.Structure {
|
||||
|
@ -72,6 +82,7 @@ func (prot *Protocol) RemoveFieldByName(name string) {
|
|||
prot.RemoveFieldByElement(element)
|
||||
}
|
||||
|
||||
// RemoveFieldByElement from the protocol
|
||||
func (prot *Protocol) RemoveFieldByElement(field int) {
|
||||
ret := make([]*Field, 0)
|
||||
for i, f := range prot.Structure {
|
||||
|
@ -83,6 +94,7 @@ func (prot *Protocol) RemoveFieldByElement(field int) {
|
|||
prot.Structure = ret
|
||||
}
|
||||
|
||||
// Load a protocol from path into the current reference
|
||||
func (prot *Protocol) Load(path string) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
|
@ -91,6 +103,8 @@ func (prot *Protocol) Load(path string) error {
|
|||
err = json.Unmarshal(data, prot)
|
||||
return err
|
||||
}
|
||||
|
||||
// Save a protocol at a path location
|
||||
func (prot *Protocol) Save(path string) error {
|
||||
data, err := json.MarshalIndent(*prot, "", " ")
|
||||
if err != nil {
|
||||
|
@ -100,6 +114,7 @@ func (prot *Protocol) Save(path string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// LoadNew protocol from path
|
||||
func LoadNew(path string) (*Protocol, error) {
|
||||
prot := NewProtocolStructure()
|
||||
err := prot.Load(path)
|
||||
|
|
Loading…
Reference in New Issue