Unit and chain banks
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/localhots/yeast/impl"
|
||||
)
|
||||
|
||||
type (
|
||||
Bank struct {
|
||||
config string
|
||||
units map[string]*Unit
|
||||
}
|
||||
)
|
||||
|
||||
func NewBank(config string) *Bank {
|
||||
return &Bank{
|
||||
config: config,
|
||||
units: map[string]*Unit{},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bank) Unit(name string) Caller {
|
||||
if u, ok := b.units[name]; ok {
|
||||
// Check for unit implementation and create a unit if there is none
|
||||
if imp := impl.New(u.Impl); imp != nil {
|
||||
return imp
|
||||
} else {
|
||||
return u
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bank) Reload() {
|
||||
f, err := os.Open(b.config)
|
||||
if err != nil {
|
||||
panic("Failed to open units config: " + b.config)
|
||||
}
|
||||
bs, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
panic("Failed to read units config: " + b.config)
|
||||
}
|
||||
|
||||
var conf map[string]map[string]interface{}
|
||||
if err := json.Unmarshal(bs, &conf); err != nil {
|
||||
panic("Failed to parse units config: " + b.config)
|
||||
}
|
||||
|
||||
b.units = map[string]*Unit{}
|
||||
for name, meta := range conf {
|
||||
b.units[name] = &Unit{
|
||||
Name: name,
|
||||
Impl: meta["impl"].(string),
|
||||
Config: meta["config"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bank) Units() []string {
|
||||
list := []string{}
|
||||
for name, _ := range b.units {
|
||||
list = append(list, name)
|
||||
}
|
||||
return list
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
units = map[string]*Unit{}
|
||||
)
|
||||
|
||||
func LoadUnits(path string) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic("Failed to open units config: " + path)
|
||||
}
|
||||
b, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
panic("Failed to parse units config: " + path)
|
||||
}
|
||||
|
||||
var conf map[string]map[string]interface{}
|
||||
json.Unmarshal(b, &conf)
|
||||
|
||||
for name, meta := range conf {
|
||||
units[name] = &Unit{
|
||||
Name: name,
|
||||
Impl: meta["impl"].(string),
|
||||
Config: meta["config"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Units() []string {
|
||||
list := []string{}
|
||||
for name, _ := range units {
|
||||
list = append(list, name)
|
||||
}
|
||||
return list
|
||||
}
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/localhots/yeast/impl"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -21,19 +19,6 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func New(name string) Caller {
|
||||
if u, ok := units[name]; ok {
|
||||
// Check for unit implementation and create a unit if there is none
|
||||
if imp := impl.New(u.Impl); imp != nil {
|
||||
return imp
|
||||
} else {
|
||||
return u
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Unit) Call(data []byte) (resp []byte, err error) {
|
||||
var (
|
||||
addr = &net.UnixAddr{u.socketPath(), "unix"}
|
||||
|
||||
Reference in New Issue
Block a user