2016-11-13 21:16:20 -08:00
|
|
|
package constellation
|
|
|
|
|
|
|
|
import (
|
2018-08-03 06:51:50 -07:00
|
|
|
"errors"
|
2016-11-13 21:16:20 -08:00
|
|
|
"fmt"
|
2018-02-08 13:16:40 -08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-08-03 06:51:50 -07:00
|
|
|
"strings"
|
2016-11-13 21:16:20 -08:00
|
|
|
"time"
|
2018-08-03 06:51:50 -07:00
|
|
|
|
|
|
|
"github.com/patrickmn/go-cache"
|
2016-11-13 21:16:20 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Constellation struct {
|
2018-08-03 06:51:50 -07:00
|
|
|
node *Client
|
|
|
|
c *cache.Cache
|
|
|
|
isConstellationNotInUse bool
|
2016-11-13 21:16:20 -08:00
|
|
|
}
|
|
|
|
|
2018-08-03 06:51:50 -07:00
|
|
|
var (
|
|
|
|
ErrConstellationIsntInit = errors.New("Constellation not in use")
|
|
|
|
)
|
|
|
|
|
2016-11-13 21:16:20 -08:00
|
|
|
func (g *Constellation) Send(data []byte, from string, to []string) (out []byte, err error) {
|
2018-08-03 06:51:50 -07:00
|
|
|
if g.isConstellationNotInUse {
|
|
|
|
return nil, ErrConstellationIsntInit
|
|
|
|
}
|
2017-09-01 05:54:00 -07:00
|
|
|
out, err = g.node.SendPayload(data, from, to)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-13 21:16:20 -08:00
|
|
|
}
|
|
|
|
g.c.Set(string(out), data, cache.DefaultExpiration)
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 06:30:42 -08:00
|
|
|
func (g *Constellation) SendSignedTx(data []byte, to []string) (out []byte, err error) {
|
|
|
|
if g.isConstellationNotInUse {
|
|
|
|
return nil, ErrConstellationIsntInit
|
|
|
|
}
|
|
|
|
out, err = g.node.SendSignedPayload(data, to)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:16:20 -08:00
|
|
|
func (g *Constellation) Receive(data []byte) ([]byte, error) {
|
2018-08-03 06:51:50 -07:00
|
|
|
if g.isConstellationNotInUse {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-11-13 21:16:20 -08:00
|
|
|
if len(data) == 0 {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
// Ignore this error since not being a recipient of
|
|
|
|
// a payload isn't an error.
|
|
|
|
// TODO: Return an error if it's anything OTHER than
|
|
|
|
// 'you are not a recipient.'
|
|
|
|
dataStr := string(data)
|
|
|
|
x, found := g.c.Get(dataStr)
|
|
|
|
if found {
|
|
|
|
return x.([]byte), nil
|
|
|
|
}
|
|
|
|
pl, _ := g.node.ReceivePayload(data)
|
|
|
|
g.c.Set(dataStr, pl, cache.DefaultExpiration)
|
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 13:16:40 -08:00
|
|
|
func New(path string) (*Constellation, error) {
|
|
|
|
info, err := os.Lstat(path)
|
2016-11-13 21:16:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-08 13:16:40 -08:00
|
|
|
// We accept either the socket or a configuration file that points to
|
|
|
|
// a socket.
|
2018-08-03 06:51:50 -07:00
|
|
|
isSocket := info.Mode()&os.ModeSocket != 0
|
2018-02-08 13:16:40 -08:00
|
|
|
if !isSocket {
|
|
|
|
cfg, err := LoadConfig(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path = filepath.Join(cfg.WorkDir, cfg.Socket)
|
|
|
|
}
|
|
|
|
err = RunNode(path)
|
2016-11-13 21:16:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-08 13:16:40 -08:00
|
|
|
n, err := NewClient(path)
|
2016-11-13 21:16:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Constellation{
|
|
|
|
node: n,
|
2016-11-13 21:35:27 -08:00
|
|
|
c: cache.New(5*time.Minute, 5*time.Minute),
|
2018-08-03 06:51:50 -07:00
|
|
|
isConstellationNotInUse: false,
|
2016-11-13 21:16:20 -08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 13:16:40 -08:00
|
|
|
func MustNew(path string) *Constellation {
|
2018-08-03 06:51:50 -07:00
|
|
|
if strings.EqualFold(path, "ignore") {
|
|
|
|
return &Constellation{
|
|
|
|
node: nil,
|
|
|
|
c: nil,
|
|
|
|
isConstellationNotInUse: true,
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 13:16:40 -08:00
|
|
|
g, err := New(path)
|
2016-11-13 21:16:20 -08:00
|
|
|
if err != nil {
|
2018-02-08 13:16:40 -08:00
|
|
|
panic(fmt.Sprintf("MustNew: Failed to connect to Constellation (%s): %v", path, err))
|
2016-11-13 21:16:20 -08:00
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2018-02-08 13:16:40 -08:00
|
|
|
func MaybeNew(path string) *Constellation {
|
|
|
|
if path == "" {
|
2016-11-13 21:16:20 -08:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-08 13:16:40 -08:00
|
|
|
return MustNew(path)
|
2016-11-13 21:16:20 -08:00
|
|
|
}
|