1
0
Fork 0

Unit abstraction

This commit is contained in:
Gregory Eremin 2015-02-10 02:54:36 +07:00
parent fb5cc85375
commit 2ca922d321
3 changed files with 67 additions and 23 deletions

View File

@ -1,23 +0,0 @@
package main
import (
"flag"
"code.google.com/p/go.net/context"
"github.com/localhots/yeast/core"
)
func main() {
var num int
flag.IntVar(&num, "num", 0, "Pass this number")
flag.Parse()
chain, ok := core.NewChain("calculate")
if !ok {
println("Bad chain: calculate")
return
}
ctx := context.WithValue(context.Background(), "num", num)
core.ProcessChain(ctx, chain)
}

16
socket_send.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"github.com/localhots/yeast/unit"
)
func main() {
u := unit.New("uuid")
msg := []byte("{}")
fmt.Println("Sending message:", string(msg))
resp, _ := u.Send(msg)
fmt.Println("Reply recieved:", string(resp))
}

51
unit/unit.go Normal file
View File

@ -0,0 +1,51 @@
package unit
import (
"bytes"
"fmt"
"net"
"strings"
)
type (
Unit struct {
Name string
}
)
func New(name string) *Unit {
return &Unit{
Name: name,
}
}
func (u *Unit) Send(data []byte) (resp []byte, err error) {
conn, err := net.DialUnix("unix", nil, &net.UnixAddr{u.socketPath(), "unix"})
if err != nil {
fmt.Println("Failed opening socket:", err.Error())
return
}
defer conn.Close()
if _, err = conn.Write(data); err != nil {
fmt.Println("Failed to write data to socket:", err.Error())
return
}
if err = conn.CloseWrite(); err != nil {
fmt.Println("Failed to close socket for reading:", err.Error())
return
}
var respBuf bytes.Buffer
if _, err = respBuf.ReadFrom(conn); err != nil {
fmt.Println("Failed read data from socket:", err.Error())
return
}
resp = respBuf.Bytes()
return
}
func (u *Unit) socketPath() string {
return strings.Join([]string{"/tmp/unit_", u.Name, ".sock"}, "")
}