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