1
0
Fork 0

Caller interface applies to both units and chains

This commit is contained in:
Gregory Eremin 2015-02-11 01:15:37 +07:00
parent 2ccd02a1c9
commit 512770b91a
2 changed files with 48 additions and 7 deletions

34
core/chain.go Normal file
View File

@ -0,0 +1,34 @@
package core
type (
Chain struct {
Flow Flow
Links []Caller
}
Caller interface {
Call([]byte) ([]byte, error)
Units() []string
}
)
func (c *Chain) Call(data []byte) (resp []byte, err error) {
return data, nil
}
func (c *Chain) Units() []string {
// Collecting unique unit names using map
units := map[string]*struct{}{}
for _, caller := range c.Links {
for _, unit := range caller.Units() {
units[unit] = nil
}
}
// Extracting names to a slice
uniq := []string{}
for unit, _ := range units {
uniq = append(uniq, unit)
}
return uniq
}

View File

@ -19,9 +19,14 @@ func New(name string) *Unit {
}
}
func (u *Unit) Send(data []byte) (resp []byte, err error) {
conn, err := net.DialUnix("unix", nil, &net.UnixAddr{u.socketPath(), "unix"})
if err != nil {
func (u *Unit) Call(data []byte) (resp []byte, err error) {
var (
addr = &net.UnixAddr{u.socketPath(), "unix"}
conn *net.UnixConn
buf bytes.Buffer
)
if conn, err = net.DialUnix("unix", nil, addr); err != nil {
fmt.Println("Failed opening socket:", err.Error())
return
}
@ -36,14 +41,16 @@ func (u *Unit) Send(data []byte) (resp []byte, err error) {
return
}
var respBuf bytes.Buffer
if _, err = respBuf.ReadFrom(conn); err != nil {
if _, err = buf.ReadFrom(conn); err != nil {
fmt.Println("Failed read data from socket:", err.Error())
return
}
resp = respBuf.Bytes()
return
return buf.Bytes(), nil
}
func (u *Unit) Units() []string {
return []string{u.Name}
}
func (u *Unit) socketPath() string {