├── tests ├── print-env.sh └── run-tests.sh ├── agents ├── pipe_test.go ├── cmd_test.go ├── type.go ├── role.go ├── status.go ├── null.go ├── descr.go ├── utils_test.go ├── response.go ├── pipe.go ├── utils.go ├── response_test.go ├── collection_test.go ├── cmd.go ├── agent.go └── collection.go ├── .gitignore ├── log ├── writers.go ├── writers_test.go ├── log_test.go └── log.go ├── TODO ├── ringio.go ├── client ├── utils.go ├── log.go ├── run.go ├── close.go ├── error.go ├── ping.go ├── kill.go ├── stop.go ├── io.go ├── list.go ├── input.go ├── open.go ├── output.go ├── start.go ├── source.go ├── sink.go ├── client_test.go └── client.go ├── config ├── config_test.go └── config.go ├── onexit ├── onexit_test.go └── onexit.go ├── utils ├── utils_test.go └── utils.go ├── pipe ├── pipe_test.go └── pipe.go ├── README.md ├── msg ├── filter.go ├── filter_test.go ├── msg.go └── msg_test.go ├── server ├── server_test.go └── server.go └── LICENSE /tests/print-env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "$RINGIO_DEBUG_VAR" 4 | -------------------------------------------------------------------------------- /agents/pipe_test.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestPipeIsAgent(t *testing.T) { 8 | meta := NewAgentMetadata() 9 | meta.Role = AgentRoleSource 10 | p := NewAgentPipe("test", meta) 11 | if Agent(p) == nil { 12 | t.Error("Cast failed") 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /agents/cmd_test.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestCmdIsAgent(t *testing.T) { 8 | meta := NewAgentMetadata() 9 | meta.Role = AgentRoleSource 10 | p := NewAgentCmd([]string{"test", "test"}, meta) 11 | if Agent(p) == nil { 12 | t.Error("Cast failed") 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /agents/type.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | type AgentType int 4 | 5 | const ( 6 | AgentTypeNull AgentType = iota 7 | AgentTypeCmd 8 | AgentTypePipe 9 | ) 10 | 11 | func (t AgentType) String() string { 12 | switch t { 13 | case AgentTypeCmd: 14 | return "$" 15 | case AgentTypePipe: 16 | return "|" 17 | } 18 | 19 | return "?" 20 | } 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /log/writers.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | ) 7 | 8 | type NullWriter struct{} 9 | 10 | func (n *NullWriter) Write(b []byte) (int, error) { 11 | return len(b), nil 12 | } 13 | 14 | type NewlineWriter struct { 15 | w io.Writer 16 | } 17 | 18 | func NewNewlineWriter(w io.Writer) *NewlineWriter { 19 | return &NewlineWriter{w: w} 20 | } 21 | 22 | func (nw *NewlineWriter) Write(b []byte) (int, error) { 23 | return nw.w.Write([]byte(fmt.Sprintf("%s\n", b))) 24 | } 25 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | * Show ENV in verbose listing. 2 | 3 | * Building with -race shows problems in pipe.go 4 | 5 | * ringio -k -> forbicly remove a session socket. 6 | 7 | * Use a decent (non-std library) flags parsing library. 8 | 9 | * Reimplement as library with different backends: 10 | - On linux, implement a epoll backend 11 | - Backend without ringbuffer 12 | 13 | * Add documentation to all public functions of the new package. 14 | 15 | * seekable ringbuf (reader can seek to beginning or end.) 16 | -------------------------------------------------------------------------------- /ringio.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/dullgiulio/ringio/client" 7 | "github.com/dullgiulio/ringio/config" 8 | "github.com/dullgiulio/ringio/onexit" 9 | "github.com/dullgiulio/ringio/utils" 10 | ) 11 | 12 | func main() { 13 | // Handle interrupt signals. 14 | onexit.HandleInterrupt() 15 | 16 | config.Init() 17 | 18 | cli := client.NewCli() 19 | if err := cli.ParseArgs(os.Args); err != nil { 20 | utils.Fatal(err) 21 | } 22 | 23 | cli.Run() 24 | onexit.Exit(0) 25 | } 26 | -------------------------------------------------------------------------------- /log/writers_test.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func TestNullWriter(t *testing.T) { 9 | n := new(NullWriter) 10 | 11 | if l, err := n.Write([]byte("1234567890")); l != 10 || err != nil { 12 | t.Log("Did expect to write 10 bytes and no error") 13 | t.Fail() 14 | } 15 | } 16 | 17 | func TestNewlineWriter(t *testing.T) { 18 | var buf []byte 19 | 20 | b := bytes.NewBuffer(buf) 21 | w := NewNewlineWriter(b) 22 | 23 | w.Write([]byte("123456789")) 24 | 25 | if b.Bytes()[9] != '\n' { 26 | t.Error("NewlineWriter must write a newline") 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /client/utils.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "os" 7 | 8 | "github.com/dullgiulio/ringio/utils" 9 | ) 10 | 11 | func getAllSessions() []string { 12 | dir := utils.GetDotfileDir() 13 | 14 | files, err := ioutil.ReadDir(dir) 15 | if err != nil { 16 | utils.Error(err) 17 | } 18 | 19 | var sockets []string 20 | 21 | for _, finfo := range files { 22 | mode := finfo.Mode() 23 | 24 | if mode&os.ModeSocket == os.ModeSocket { 25 | sockets = append(sockets, finfo.Name()) 26 | } 27 | } 28 | 29 | return sockets 30 | } 31 | 32 | func printList(list []string) { 33 | for _, l := range list { 34 | fmt.Printf("%s\n", l) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /agents/role.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | type AgentRole int 4 | 5 | const ( 6 | AgentRoleSink AgentRole = iota 7 | AgentRoleSource 8 | AgentRoleErrors 9 | AgentRoleLog 10 | ) 11 | 12 | func (r AgentRole) String() string { 13 | switch r { 14 | case AgentRoleSink: 15 | return "->" 16 | case AgentRoleSource: 17 | return "<-" 18 | case AgentRoleErrors: 19 | return "&&" 20 | case AgentRoleLog: 21 | return "||" 22 | } 23 | 24 | return "" 25 | } 26 | 27 | func (r AgentRole) Text() string { 28 | switch r { 29 | case AgentRoleSink: 30 | return "output" 31 | case AgentRoleSource: 32 | return "input" 33 | case AgentRoleErrors: 34 | return "errors" 35 | case AgentRoleLog: 36 | return "logs" 37 | } 38 | 39 | return "" 40 | } 41 | -------------------------------------------------------------------------------- /client/log.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | 7 | "github.com/dullgiulio/ringio/server" 8 | "github.com/dullgiulio/ringio/utils" 9 | ) 10 | 11 | type CommandLog struct { 12 | client *rpc.Client 13 | response *server.RPCResp 14 | } 15 | 16 | func NewCommandLog() *CommandLog { 17 | return &CommandLog{ 18 | response: new(server.RPCResp), 19 | } 20 | } 21 | 22 | func (c *CommandLog) Help() string { 23 | return `Print internal log` 24 | } 25 | 26 | func (c *CommandLog) Init(fs *flag.FlagSet) bool { 27 | // nothing to do yet. 28 | return false 29 | } 30 | 31 | func (c *CommandLog) Run(cli *Cli) error { 32 | c.client = cli.GetClient() 33 | 34 | addLogAgentPipe(c.client, c.response, utils.GetRandomDotfile()) 35 | 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /config/config_test.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestDefaults(t *testing.T) { 8 | if C.RingbufSize != defaults.RingbufSize { 9 | t.Error("Invalid default") 10 | } 11 | 12 | if C.RingbufLogSize != defaults.RingbufLogSize { 13 | t.Error("Invalid default") 14 | } 15 | 16 | if C.MaxLineSize != defaults.MaxLineSize { 17 | t.Error("Invalid default") 18 | } 19 | 20 | if C.AutoRun != defaults.AutoRun { 21 | t.Error("Invalid default") 22 | } 23 | 24 | if C.PrintLog != defaults.PrintLog { 25 | t.Error("Invalid default") 26 | } 27 | } 28 | 29 | func TestRingbufInit(t *testing.T) { 30 | if GetLogRingbuf() != nil { 31 | t.Error("Ringbuf allocated before being initialized") 32 | } 33 | 34 | Init() 35 | 36 | GetLogRingbuf().Cancel() 37 | } 38 | -------------------------------------------------------------------------------- /client/run.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | 7 | "github.com/dullgiulio/ringio/server" 8 | "github.com/dullgiulio/ringio/utils" 9 | ) 10 | 11 | type CommandRun struct { 12 | client *rpc.Client 13 | response *server.RPCResp 14 | } 15 | 16 | func NewCommandRun() *CommandRun { 17 | return &CommandRun{ 18 | response: new(server.RPCResp), 19 | } 20 | } 21 | 22 | func (c *CommandRun) Help() string { 23 | return `Run all processes` 24 | } 25 | 26 | func (c *CommandRun) Init(fs *flag.FlagSet) bool { 27 | // nothing to do yet. 28 | return false 29 | } 30 | 31 | func (c *CommandRun) Run(cli *Cli) error { 32 | c.client = cli.GetClient() 33 | 34 | if err := c.client.Call("RPCServer.Run", &server.RPCReq{}, &c.response); err != nil { 35 | utils.Fatal(err) 36 | } 37 | 38 | return nil 39 | } 40 | -------------------------------------------------------------------------------- /client/close.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | 7 | "github.com/dullgiulio/ringio/server" 8 | "github.com/dullgiulio/ringio/utils" 9 | ) 10 | 11 | type CommandClose struct { 12 | client *rpc.Client 13 | response *server.RPCResp 14 | } 15 | 16 | func NewCommandClose() *CommandClose { 17 | return &CommandClose{ 18 | response: new(server.RPCResp), 19 | } 20 | } 21 | 22 | func (c *CommandClose) Help() string { 23 | return `Close a session` 24 | } 25 | 26 | func (c *CommandClose) Init(fs *flag.FlagSet) bool { 27 | // nothing to do yet. 28 | return false 29 | } 30 | 31 | func (c *CommandClose) Run(cli *Cli) error { 32 | c.client = cli.GetClient() 33 | 34 | if err := c.client.Call("RPCServer.Close", &server.RPCReq{}, &c.response); err != nil { 35 | utils.Fatal(err) 36 | } 37 | 38 | return nil 39 | } 40 | -------------------------------------------------------------------------------- /client/error.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | 7 | "github.com/dullgiulio/ringio/agents" 8 | "github.com/dullgiulio/ringio/server" 9 | "github.com/dullgiulio/ringio/utils" 10 | ) 11 | 12 | type CommandError struct { 13 | client *rpc.Client 14 | response *server.RPCResp 15 | } 16 | 17 | func NewCommandError() *CommandError { 18 | return &CommandError{ 19 | response: new(server.RPCResp), 20 | } 21 | } 22 | 23 | func (c *CommandError) Help() string { 24 | return `Output data gathered from the standard error (stderr)` 25 | } 26 | 27 | func (c *CommandError) Init(fs *flag.FlagSet) bool { 28 | // nothing to do yet. 29 | return false 30 | } 31 | 32 | func (c *CommandError) Run(cli *Cli) error { 33 | c.client = cli.GetClient() 34 | 35 | addErrorsAgentPipe(c.client, &agents.AgentMetadata{Filter: cli.Filter}, c.response, utils.GetRandomDotfile()) 36 | 37 | return nil 38 | } 39 | -------------------------------------------------------------------------------- /config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/dullgiulio/ringbuf" 5 | ) 6 | 7 | type _Config struct { 8 | RingbufSize int64 9 | RingbufLogSize int64 10 | MaxLineSize int64 11 | AutoExit bool 12 | AutoRun bool 13 | PrintLog bool 14 | logring *ringbuf.Ringbuf 15 | } 16 | 17 | var C *_Config 18 | 19 | var defaults = _Config{ 20 | RingbufSize: 1024, 21 | RingbufLogSize: 1024, 22 | MaxLineSize: 1024, 23 | AutoRun: true, 24 | PrintLog: false, 25 | } 26 | 27 | func init() { 28 | C = &defaults 29 | } 30 | 31 | func Init() { 32 | C.logring = ringbuf.NewRingbuf(C.RingbufLogSize) 33 | go C.logring.Run() 34 | } 35 | 36 | func GetLogRingbuf() *ringbuf.Ringbuf { 37 | return C.logring 38 | } 39 | 40 | // This module is not thread safe. After initialization from flags, 41 | // only read access is done on the immutable configuration option. 42 | -------------------------------------------------------------------------------- /agents/status.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | type AgentStatus int 4 | 5 | const ( 6 | AgentStatusNone AgentStatus = iota 7 | AgentStatusRunning 8 | AgentStatusKilled 9 | AgentStatusStopped 10 | AgentStatusFinished 11 | ) 12 | 13 | func (s AgentStatus) IsRunning() bool { 14 | return s == AgentStatusRunning 15 | } 16 | 17 | func (s AgentStatus) String() string { 18 | switch s { 19 | case AgentStatusRunning: 20 | return "R" 21 | case AgentStatusStopped: 22 | return "S" 23 | case AgentStatusKilled: 24 | return "K" 25 | case AgentStatusFinished: 26 | return "F" 27 | } 28 | 29 | return "?" 30 | } 31 | 32 | func (s AgentStatus) Text() string { 33 | switch s { 34 | case AgentStatusRunning: 35 | return "running" 36 | case AgentStatusStopped: 37 | return "stopped" 38 | case AgentStatusKilled: 39 | return "killed" 40 | case AgentStatusFinished: 41 | return "finished" 42 | } 43 | 44 | return "none" 45 | } 46 | -------------------------------------------------------------------------------- /onexit/onexit_test.go: -------------------------------------------------------------------------------- 1 | package onexit 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestExit(t *testing.T) { 8 | exitVal := 0 9 | 10 | SetFunc(func(i int) { 11 | exitVal = i 12 | }) 13 | 14 | Exit(10) 15 | 16 | if exitVal != 10 { 17 | t.Error("Did not exit with status 10") 18 | } 19 | } 20 | 21 | func TestDefer(t *testing.T) { 22 | exitVal := 0 23 | 24 | SetFunc(func(i int) { 25 | exitVal += i 26 | }) 27 | 28 | Defer(func() { exitVal++ }) 29 | Defer(func() { exitVal++ }) 30 | 31 | Exit(1) 32 | 33 | if exitVal != 3 { 34 | t.Error("Some defer function was not called correctly") 35 | } 36 | } 37 | 38 | func TestPendingExit(t *testing.T) { 39 | exitVal := 0 40 | done := make(chan struct{}) 41 | 42 | SetFunc(func(i int) { 43 | exitVal = i 44 | done <- struct{}{} 45 | }) 46 | 47 | HandleInterrupt() 48 | PendingExit(10) 49 | 50 | <-done 51 | 52 | if exitVal != 10 { 53 | t.Error("Did not exit with status 10") 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | export RINGIO_HOME=. 4 | export RINGIO=../ringio 5 | export RINGIO_TEST_FAILURE=0 6 | 7 | ringio_test_error() { 8 | echo -e "err\t${@:1}" >&2 9 | RINGIO_TEST_FAILURE=1 10 | } 11 | 12 | nohup $RINGIO test open >/dev/null 2>&1 & 13 | 14 | # TODO: Implement --quiet option. 15 | RINGIO_DEBUG_VAR=debugval $RINGIO test input print-env.sh >/dev/null 16 | if [ "$?" != '0' ]; then 17 | ringio_test_error "Adding agent failed" 18 | fi 19 | 20 | if [ "$($RINGIO test output --no-wait | grep debugval)" == '' ]; then 21 | ringio_test_error "Environment variable not passed to subprocess" 22 | fi 23 | 24 | $RINGIO test close 25 | if [ "$?" != '0' ]; then 26 | ringio_test_error "Ending session failed" 27 | fi 28 | 29 | if [ "$(ls -1 .ringio/ | wc -l)" != '0' ]; then 30 | ringio_test_error "Expected ringio home to be empty" 31 | fi 32 | 33 | rm -rf .ringio 34 | 35 | if [ "$RINGIO_TEST_FAILURE" == '0' ]; then 36 | echo "OK" 37 | fi 38 | 39 | exit $RINGIO_TEST_FAILURE 40 | 41 | -------------------------------------------------------------------------------- /utils/utils_test.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "path" 7 | "strings" 8 | "testing" 9 | "time" 10 | ) 11 | 12 | func TestDotdir(t *testing.T) { 13 | tmphome := fmt.Sprintf("/tmp/%d", time.Now().Unix()/int64(time.Millisecond)) 14 | os.Setenv("HOME", tmphome) 15 | 16 | dir := GetDotfileDir() 17 | if !strings.HasPrefix(dir, tmphome) { 18 | t.Error("Dot file was not created under the temporary home directory") 19 | } 20 | 21 | basepath := getBasepath("") 22 | 23 | if !strings.HasPrefix(basepath, "/tmp/") { 24 | t.Error("Base path without HOME is not in /tmp") 25 | } 26 | 27 | file := GetRandomDotfile() 28 | 29 | if file == "" { 30 | t.Error("Did not expect the filename to be nil") 31 | } 32 | 33 | if !strings.HasPrefix(file, tmphome) { 34 | t.Error("Dot file was not created under the temporary home directory") 35 | } 36 | 37 | if FileInDotpath(path.Base(file)) != file { 38 | t.Error("Basename does not equal to the dotpath") 39 | } 40 | 41 | if err := os.RemoveAll(tmphome); err != nil { 42 | t.Error(err) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /client/ping.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | "net/rpc" 7 | "time" 8 | 9 | "github.com/dullgiulio/ringio/server" 10 | "github.com/dullgiulio/ringio/utils" 11 | ) 12 | 13 | type CommandPing struct { 14 | client *rpc.Client 15 | maxWait int 16 | } 17 | 18 | func NewCommandPing() *CommandPing { 19 | return &CommandPing{} 20 | } 21 | 22 | func (c *CommandPing) Help() string { 23 | return `Ping a session` 24 | } 25 | 26 | func (c *CommandPing) Init(fs *flag.FlagSet) bool { 27 | fs.IntVar(&c.maxWait, "wait", 2, "Number of seconds to wait before giving up") 28 | return true 29 | } 30 | 31 | func (c *CommandPing) Run(cli *Cli) error { 32 | c.client = cli.GetClient() 33 | 34 | resp := 0 35 | 36 | callRes := c.client.Go("RPCServer.Ping", &server.RPCReq{}, &resp, nil) 37 | 38 | select { 39 | case <-callRes.Done: 40 | case <-time.After(time.Duration(c.maxWait) * time.Second): 41 | break 42 | } 43 | 44 | if resp != 1 { 45 | utils.Fatal(errors.New("Looks like the session is not responding properly.")) 46 | } 47 | 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /client/kill.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | "net/rpc" 7 | 8 | "github.com/dullgiulio/ringio/server" 9 | "github.com/dullgiulio/ringio/utils" 10 | ) 11 | 12 | type CommandKill struct { 13 | client *rpc.Client 14 | response *server.RPCResp 15 | } 16 | 17 | func NewCommandKill() *CommandKill { 18 | return &CommandKill{ 19 | response: new(server.RPCResp), 20 | } 21 | } 22 | 23 | func (c *CommandKill) Help() string { 24 | return `Kill a specified agent` 25 | } 26 | 27 | func (c *CommandKill) Init(fs *flag.FlagSet) bool { 28 | // nothing to do yet. 29 | return false 30 | } 31 | 32 | func (c *CommandKill) Run(cli *Cli) error { 33 | c.client = cli.GetClient() 34 | 35 | argsErr := errors.New("Kill must be followed by an argument.") 36 | 37 | if cli.Filter == nil { 38 | utils.Fatal(argsErr) 39 | } 40 | 41 | in := cli.Filter.GetIn() 42 | 43 | if len(in) == 0 { 44 | utils.Fatal(argsErr) 45 | } 46 | 47 | for _, id := range in { 48 | if err := c.client.Call("RPCServer.Kill", id, &c.response); err != nil { 49 | utils.Error(err) 50 | } 51 | } 52 | 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /client/stop.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | "net/rpc" 7 | 8 | "github.com/dullgiulio/ringio/server" 9 | "github.com/dullgiulio/ringio/utils" 10 | ) 11 | 12 | type CommandStop struct { 13 | client *rpc.Client 14 | response *server.RPCResp 15 | } 16 | 17 | func NewCommandStop() *CommandStop { 18 | return &CommandStop{ 19 | response: new(server.RPCResp), 20 | } 21 | } 22 | 23 | func (c *CommandStop) Help() string { 24 | return `Stop a specified agent by stop sending data to it` 25 | } 26 | 27 | func (c *CommandStop) Init(fs *flag.FlagSet) bool { 28 | // nothing to do yet. 29 | return false 30 | } 31 | 32 | func (c *CommandStop) Run(cli *Cli) error { 33 | c.client = cli.GetClient() 34 | 35 | argsErr := errors.New("Stop must be followed by an argument.") 36 | 37 | if cli.Filter == nil { 38 | utils.Fatal(argsErr) 39 | } 40 | 41 | in := cli.Filter.GetIn() 42 | 43 | if len(in) == 0 { 44 | utils.Fatal(argsErr) 45 | } 46 | 47 | for _, id := range in { 48 | if err := c.client.Call("RPCServer.Stop", id, &c.response); err != nil { 49 | utils.Error(err) 50 | } 51 | } 52 | 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /client/io.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | "os" 7 | 8 | "github.com/dullgiulio/ringio/agents" 9 | "github.com/dullgiulio/ringio/server" 10 | "github.com/dullgiulio/ringio/utils" 11 | ) 12 | 13 | type CommandIO struct { 14 | client *rpc.Client 15 | response *server.RPCResp 16 | name string 17 | } 18 | 19 | func NewCommandIO() *CommandIO { 20 | return &CommandIO{ 21 | response: new(server.RPCResp), 22 | } 23 | } 24 | 25 | func (c *CommandIO) Help() string { 26 | return `Open an input and an ouput session to current terminal` 27 | } 28 | 29 | func (c *CommandIO) Init(fs *flag.FlagSet) bool { 30 | fs.StringVar(&c.name, "name", "", "Name or comment of this stream") 31 | return true 32 | } 33 | 34 | func (c *CommandIO) Run(cli *Cli) error { 35 | c.client = cli.GetClient() 36 | 37 | metaSource := agents.AgentMetadata{ 38 | User: os.Getenv("USER"), 39 | Name: c.name, 40 | } 41 | metaSink := metaSource 42 | metaSink.Filter = cli.Filter 43 | 44 | go addSourceAgentPipe(c.client, c.response, &metaSource, utils.GetRandomDotfile()) 45 | addSinkAgentPipe(c.client, &metaSink, c.response, utils.GetRandomDotfile()) 46 | 47 | return nil 48 | } 49 | -------------------------------------------------------------------------------- /onexit/onexit.go: -------------------------------------------------------------------------------- 1 | package onexit 2 | 3 | import ( 4 | "os" 5 | "os/signal" 6 | "sync" 7 | ) 8 | 9 | type Func func() 10 | 11 | type onExit struct { 12 | m sync.Mutex 13 | i int 14 | s chan os.Signal 15 | fs []Func 16 | e func(int) 17 | } 18 | 19 | var _onExit onExit 20 | 21 | func init() { 22 | _onExit = onExit{ 23 | s: make(chan os.Signal), 24 | fs: make([]Func, 0), 25 | e: os.Exit, 26 | } 27 | } 28 | 29 | func SetFunc(e func(int)) { 30 | _onExit.m.Lock() 31 | defer _onExit.m.Unlock() 32 | 33 | _onExit.e = e 34 | } 35 | 36 | func Defer(f Func) { 37 | _onExit.m.Lock() 38 | defer _onExit.m.Unlock() 39 | 40 | _onExit.fs = append(_onExit.fs, f) 41 | } 42 | 43 | func Exit(i int) { 44 | _onExit.m.Lock() 45 | defer _onExit.m.Unlock() 46 | 47 | for _, f := range _onExit.fs { 48 | f() 49 | } 50 | 51 | _onExit.e(i) 52 | } 53 | 54 | func PendingExit(i int) { 55 | _onExit.m.Lock() 56 | _onExit.i = i 57 | _onExit.m.Unlock() 58 | 59 | _onExit.s <- os.Interrupt 60 | } 61 | 62 | func HandleInterrupt() { 63 | signal.Notify(_onExit.s, os.Interrupt) 64 | 65 | go func() { 66 | <-_onExit.s 67 | 68 | _onExit.m.Lock() 69 | i := _onExit.i 70 | _onExit.m.Unlock() 71 | 72 | Exit(i) 73 | }() 74 | } 75 | -------------------------------------------------------------------------------- /client/list.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | "fmt" 7 | "net/rpc" 8 | 9 | "github.com/dullgiulio/ringio/agents" 10 | "github.com/dullgiulio/ringio/server" 11 | "github.com/dullgiulio/ringio/utils" 12 | ) 13 | 14 | type CommandList struct { 15 | client *rpc.Client 16 | verbose bool 17 | } 18 | 19 | func NewCommandList() *CommandList { 20 | return &CommandList{} 21 | } 22 | 23 | func (c *CommandList) Help() string { 24 | return `List all agents` 25 | } 26 | 27 | func (c *CommandList) Init(fs *flag.FlagSet) bool { 28 | fs.BoolVar(&c.verbose, "verbose", false, "Long version of the list") 29 | return true 30 | } 31 | 32 | func (c *CommandList) Run(cli *Cli) error { 33 | c.client = cli.GetClient() 34 | 35 | responseList := agents.NewAgentMessageResponseList() 36 | if err := c.client.Call("RPCServer.List", &server.RPCReq{}, &responseList); err != nil { 37 | utils.Fatal(err) 38 | } else { 39 | if responseList.Agents != nil { 40 | for _, a := range *responseList.Agents { 41 | if c.verbose { 42 | fmt.Printf("%s\n", a.Text()) 43 | } else { 44 | fmt.Printf("%s\n", &a) 45 | } 46 | } 47 | } else { 48 | utils.Fatal(errors.New("This session is currently empty.")) 49 | } 50 | } 51 | return nil 52 | } 53 | -------------------------------------------------------------------------------- /agents/null.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "github.com/dullgiulio/ringbuf" 5 | "github.com/dullgiulio/ringio/msg" 6 | ) 7 | 8 | type AgentNull struct { 9 | meta *AgentMetadata 10 | cancel chan bool 11 | } 12 | 13 | func NewAgentNull(id int, meta *AgentMetadata) *AgentNull { 14 | meta.ID = id 15 | return &AgentNull{ 16 | meta: meta, 17 | cancel: make(chan bool), 18 | } 19 | } 20 | 21 | func (a *AgentNull) Meta() *AgentMetadata { 22 | return a.meta 23 | } 24 | 25 | func (a *AgentNull) Init() { 26 | if a.meta.Options == nil { 27 | a.meta.Options = &AgentOptions{} 28 | } 29 | } 30 | 31 | func (a *AgentNull) Descr() AgentDescr { 32 | return AgentDescr{ 33 | Args: []string{"nullagent"}, 34 | Meta: *a.meta, 35 | Type: AgentTypeNull, 36 | } 37 | } 38 | 39 | func (a *AgentNull) String() string { 40 | return "AgentNull" 41 | } 42 | 43 | func (a *AgentNull) Stop() error { 44 | return nil 45 | } 46 | 47 | func (a *AgentNull) Kill() error { 48 | return nil 49 | } 50 | 51 | func (a *AgentNull) WaitFinish() error { 52 | a.cancel <- true 53 | return nil 54 | } 55 | 56 | func (a *AgentNull) StartWrite() {} 57 | 58 | func (a *AgentNull) InputToRingbuf(errors, output *ringbuf.Ringbuf) { 59 | <-a.cancel 60 | } 61 | 62 | func (a *AgentNull) OutputFromRingbuf(rStdout, rErrors, rOutput *ringbuf.Ringbuf, filter *msg.Filter) { 63 | <-a.cancel 64 | } 65 | -------------------------------------------------------------------------------- /client/input.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | "os" 7 | 8 | "github.com/dullgiulio/ringio/agents" 9 | "github.com/dullgiulio/ringio/server" 10 | "github.com/dullgiulio/ringio/utils" 11 | ) 12 | 13 | type CommandInput struct { 14 | client *rpc.Client 15 | response *server.RPCResp 16 | name string 17 | ignoreEnv bool 18 | } 19 | 20 | func NewCommandInput() *CommandInput { 21 | return &CommandInput{ 22 | response: new(server.RPCResp), 23 | } 24 | } 25 | 26 | func (c *CommandInput) Help() string { 27 | return `Reads data from stdin (and stderr for processes) and writes it into the ringbuf` 28 | } 29 | 30 | func (c *CommandInput) Init(fs *flag.FlagSet) bool { 31 | fs.StringVar(&c.name, "name", "", "Name or comment of this source") 32 | fs.BoolVar(&c.ignoreEnv, "ignore-env", false, "Ignore current environment when running a subprocess") 33 | return true 34 | } 35 | 36 | func (c *CommandInput) Run(cli *Cli) error { 37 | c.client = cli.GetClient() 38 | 39 | meta := agents.AgentMetadata{ 40 | User: os.Getenv("USER"), 41 | Name: c.name, 42 | } 43 | 44 | if !c.ignoreEnv { 45 | meta.Env = os.Environ() 46 | } 47 | 48 | if len(cli.Args) == 0 { 49 | addSourceAgentPipe(c.client, c.response, &meta, utils.GetRandomDotfile()) 50 | } else { 51 | addSourceAgentCmd(c.client, c.response, &meta, cli.Args) 52 | } 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /pipe/pipe_test.go: -------------------------------------------------------------------------------- 1 | package pipe 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/dullgiulio/ringio/log" 8 | ) 9 | 10 | func TestCreatePipe(t *testing.T) { 11 | pipeName := "/tmp/testp" 12 | 13 | go func() { 14 | nw := new(log.NullWriter) 15 | log.AddWriter(nw) 16 | 17 | if !log.Run(log.LevelError) { 18 | t.Log("Did not expect logger to return an error") 19 | t.Fail() 20 | } 21 | }() 22 | 23 | p := New(pipeName) 24 | if err := p.Create(); err != nil { 25 | t.Error(err) 26 | } 27 | if ok := p.OpenWrite(); !ok { 28 | t.Log("Did not expect OpenWrite to fail") 29 | t.Fail() 30 | } else { 31 | defer os.Remove(pipeName) 32 | 33 | if stat, err := p.file.Stat(); err != nil { 34 | t.Error(err) 35 | t.Fail() 36 | } else { 37 | mode := stat.Mode() & os.ModeNamedPipe 38 | if mode != mode|os.ModeNamedPipe { 39 | t.Log("Expected file to be a named pipe") 40 | t.Fail() 41 | } 42 | } 43 | 44 | if i, err := p.Write([]byte("something")); i < 5 || err != nil { 45 | t.Error(err) 46 | t.Fail() 47 | } 48 | } 49 | 50 | pr := New(pipeName) 51 | if ok := pr.OpenRead(); !ok { 52 | t.Log("Did not expect OpenRead to fail") 53 | t.Fail() 54 | } else { 55 | os.Remove(pipeName) 56 | 57 | var buf [100]byte 58 | 59 | if n, err := p.Read(buf[:]); n < 5 || err != nil { 60 | t.Log("Read: ", n, "Error: ", err) 61 | t.Fail() 62 | } 63 | } 64 | 65 | p.Close() 66 | } 67 | -------------------------------------------------------------------------------- /client/open.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | 6 | "github.com/dullgiulio/ringio/config" 7 | "github.com/dullgiulio/ringio/log" 8 | "github.com/dullgiulio/ringio/server" 9 | "github.com/dullgiulio/ringio/utils" 10 | ) 11 | 12 | type CommandOpen struct { 13 | minLogLevel string 14 | } 15 | 16 | func NewCommandOpen() *CommandOpen { 17 | return &CommandOpen{} 18 | } 19 | 20 | func (c *CommandOpen) Help() string { 21 | return `Open a new session` 22 | } 23 | 24 | func (c *CommandOpen) Init(fs *flag.FlagSet) bool { 25 | fs.Int64Var(&config.C.RingbufSize, "ringbuf-size", config.C.RingbufSize, "Max number of lines contained in a ringbuffer") 26 | fs.Int64Var(&config.C.MaxLineSize, "line-size", config.C.MaxLineSize, "Max size in bytes of a single line read from stdin") 27 | fs.Int64Var(&config.C.RingbufLogSize, "ringbuf-log-size", config.C.RingbufSize, "Max number of lines for log scrollback") 28 | fs.BoolVar(&config.C.AutoRun, "autostart", config.C.AutoRun, "Automatically run new commands when added") 29 | fs.BoolVar(&config.C.PrintLog, "verbose", config.C.PrintLog, "Print logging on the command line") 30 | fs.StringVar(&c.minLogLevel, "min-log-level", "info", "Minimum loggging level: debug, info, warn, fatal") 31 | return true 32 | } 33 | 34 | func (c *CommandOpen) Run(cli *Cli) error { 35 | minLogLevel, err := log.LevelFromString(c.minLogLevel) 36 | if err != nil { 37 | utils.Fatal(err) 38 | } 39 | 40 | server.Init() 41 | server.Run(minLogLevel, cli.Session) 42 | 43 | return nil 44 | } 45 | -------------------------------------------------------------------------------- /client/output.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "net/rpc" 6 | "os" 7 | 8 | "github.com/dullgiulio/ringio/agents" 9 | "github.com/dullgiulio/ringio/server" 10 | "github.com/dullgiulio/ringio/utils" 11 | ) 12 | 13 | type CommandOutput struct { 14 | client *rpc.Client 15 | response *server.RPCResp 16 | meta *agents.AgentMetadata 17 | ignoreEnv bool 18 | } 19 | 20 | func NewCommandOutput() *CommandOutput { 21 | return &CommandOutput{ 22 | response: new(server.RPCResp), 23 | meta: agents.NewAgentMetadata(), 24 | } 25 | } 26 | 27 | func (c *CommandOutput) Help() string { 28 | return `Output data from the ringbuf` 29 | } 30 | 31 | func (c *CommandOutput) Init(fs *flag.FlagSet) bool { 32 | fs.BoolVar(&c.meta.Options.NoWait, "no-wait", false, "Don't wait for future output, exit when finished dumping past data") 33 | fs.BoolVar(&c.meta.Options.PrintMeta, "metadata", false, "Display metadata in front of each message") 34 | fs.BoolVar(&c.ignoreEnv, "ignore-env", false, "Ignore current environment when running a subprocess") 35 | fs.StringVar(&c.meta.Name, "name", "", "Name or comment of this sink") 36 | return true 37 | } 38 | 39 | func (c *CommandOutput) Run(cli *Cli) error { 40 | c.client = cli.GetClient() 41 | 42 | c.meta.Filter = cli.Filter 43 | 44 | if !c.ignoreEnv { 45 | c.meta.Env = os.Environ() 46 | } 47 | 48 | if len(cli.Args) == 0 { 49 | addSinkAgentPipe(c.client, c.meta, c.response, utils.GetRandomDotfile()) 50 | } else { 51 | addSinkAgentCmd(c.client, c.meta, c.response, cli.Args) 52 | } 53 | 54 | return nil 55 | } 56 | -------------------------------------------------------------------------------- /utils/utils.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "os" 7 | "path" 8 | "time" 9 | ) 10 | 11 | var _dotfileDir string 12 | var _letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-") 13 | 14 | func getBasepath(home string) string { 15 | if home == "" { 16 | return path.Join(os.TempDir(), ".ringio-"+os.Getenv("USER")) 17 | } 18 | 19 | return path.Join(home, ".ringio") 20 | } 21 | 22 | func getRingioHome() string { 23 | home := os.Getenv("RINGIO_HOME") 24 | if home == "" { 25 | return os.Getenv("HOME") 26 | } 27 | return home 28 | } 29 | 30 | func GetDotfileDir() string { 31 | if _dotfileDir != "" { 32 | return _dotfileDir 33 | } 34 | 35 | rand.Seed(time.Now().UTC().UnixNano()) 36 | basepath := getBasepath(getRingioHome()) 37 | 38 | if os.MkdirAll(basepath, 0750) != nil { 39 | panic("Unable to create directory for ringio home!") 40 | } 41 | 42 | _dotfileDir = basepath 43 | return _dotfileDir 44 | } 45 | 46 | func randSeq(n int) string { 47 | b := make([]rune, n) 48 | l := len(_letters) 49 | 50 | for i := range b { 51 | b[i] = _letters[rand.Intn(l)] 52 | } 53 | 54 | return string(b) 55 | } 56 | 57 | func GetRandomDotfile() string { 58 | return FileInDotpath(randSeq(8)) 59 | } 60 | 61 | func FileInDotpath(filename string) string { 62 | return path.Join(GetDotfileDir(), path.Base(filename)) 63 | } 64 | 65 | func Fatal(err error) { 66 | fmt.Fprintf(os.Stderr, "ringio: %s\n", err) 67 | os.Exit(1) 68 | } 69 | 70 | func Error(err error) { 71 | fmt.Fprintf(os.Stderr, "ringio: %s\n", err) 72 | } 73 | -------------------------------------------------------------------------------- /log/log_test.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "bytes" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | func TestLevel(t *testing.T) { 10 | if LevelDebug.String() != "DEBUG" { 11 | t.Error("Unexpected string value") 12 | } 13 | 14 | if l, e := LevelFromString("debug"); e != nil || l != LevelDebug { 15 | t.Error("Unexpected value from string") 16 | } 17 | 18 | if _, e := LevelFromString("no-level"); e == nil { 19 | t.Error("Expected error on invalid level string value") 20 | } 21 | } 22 | 23 | func TestLogging(t *testing.T) { 24 | var buf []byte 25 | 26 | b := bytes.NewBuffer(buf) 27 | w := NewNewlineWriter(b) 28 | 29 | AddWriter(w) 30 | 31 | proceed := make(chan struct{}) 32 | 33 | go func() { 34 | if !Run(LevelWarn) { 35 | t.Error("Expected success after cancel") 36 | } 37 | proceed <- struct{}{} 38 | }() 39 | 40 | Debug(FacilityDefault, "Some debug message") 41 | Info(FacilityDefault, "Some info message") 42 | Cancel() 43 | 44 | <-proceed 45 | 46 | if b.String() != "" { 47 | t.Error("Debug or info message written even when minimum is Warn.") 48 | } 49 | 50 | b.Reset() 51 | 52 | go func() { 53 | if Run(LevelWarn) { 54 | t.Error("Expected run to return false after calling Fatal") 55 | } 56 | proceed <- struct{}{} 57 | }() 58 | 59 | Warn(FacilityDefault, "Some warn message") 60 | Fatal(FacilityDefault, "Some fatal error") 61 | 62 | <-proceed 63 | 64 | s := b.String() 65 | 66 | if !strings.Contains(s, "WARN: ringio: Some warn message") || 67 | !strings.Contains(s, "FAIL: ringio: Some fatal error") { 68 | t.Error("Not all messages were correctly logged") 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /client/start.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | "net/rpc" 7 | 8 | "github.com/dullgiulio/ringio/server" 9 | "github.com/dullgiulio/ringio/utils" 10 | ) 11 | 12 | type CommandStart struct { 13 | client *rpc.Client 14 | response *server.RPCResp 15 | startAll bool 16 | } 17 | 18 | func NewCommandStart() *CommandStart { 19 | return &CommandStart{ 20 | response: new(server.RPCResp), 21 | } 22 | } 23 | 24 | func (c *CommandStart) Help() string { 25 | return `Start a specific agent` 26 | } 27 | 28 | func (c *CommandStart) Init(fs *flag.FlagSet) bool { 29 | fs.BoolVar(&c.startAll, "all", false, "Start all that has never been started") 30 | return true 31 | } 32 | 33 | func (c *CommandStart) agent(cli *Cli) { 34 | argsErr := errors.New("Start must be followed by an argument.") 35 | 36 | if cli.Filter == nil { 37 | utils.Fatal(argsErr) 38 | } 39 | 40 | in := cli.Filter.GetIn() 41 | 42 | if len(in) == 0 { 43 | utils.Fatal(argsErr) 44 | } 45 | 46 | for _, id := range in { 47 | if err := c.client.Call("RPCServer.Start", id, &c.response); err != nil { 48 | utils.Error(err) 49 | } 50 | } 51 | } 52 | 53 | func (c *CommandStart) all(cli *Cli) { 54 | if cli.Filter != nil { 55 | utils.Fatal(errors.New("Filtering when --all is specified makes no sense.")) 56 | } 57 | 58 | if err := c.client.Call("RPCServer.StartAll", &server.RPCReq{}, &c.response); err != nil { 59 | utils.Fatal(err) 60 | } 61 | } 62 | 63 | func (c *CommandStart) Run(cli *Cli) error { 64 | c.client = cli.GetClient() 65 | 66 | if c.startAll { 67 | c.all(cli) 68 | } else { 69 | c.agent(cli) 70 | } 71 | 72 | return nil 73 | } 74 | -------------------------------------------------------------------------------- /client/source.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net/rpc" 7 | "os" 8 | 9 | "github.com/dullgiulio/ringio/agents" 10 | "github.com/dullgiulio/ringio/onexit" 11 | "github.com/dullgiulio/ringio/pipe" 12 | "github.com/dullgiulio/ringio/server" 13 | "github.com/dullgiulio/ringio/utils" 14 | ) 15 | 16 | func addSourceAgentPipe(client *rpc.Client, response *server.RPCResp, meta *agents.AgentMetadata, pipeName string) { 17 | var id int 18 | 19 | p := pipe.New(pipeName) 20 | 21 | if err := p.Create(); err != nil { 22 | utils.Fatal(err) 23 | } 24 | 25 | if err := p.OpenWriteErr(); err != nil { 26 | p.Remove() 27 | utils.Fatal(err) 28 | } 29 | 30 | meta.Role = agents.AgentRoleSource 31 | 32 | if err := client.Call("RPCServer.Add", &server.RPCReq{ 33 | Agent: &agents.AgentDescr{ 34 | Args: []string{pipeName}, 35 | Meta: *meta, 36 | Type: agents.AgentTypePipe, 37 | }, 38 | }, &id); err != nil { 39 | p.Remove() 40 | utils.Fatal(err) 41 | } 42 | 43 | p.Remove() 44 | 45 | onexit.Defer(func() { 46 | if err := client.Call("RPCServer.Stop", id, &response); err != nil { 47 | utils.Fatal(err) 48 | } 49 | }) 50 | 51 | // Write to pipe from stdin. 52 | r := bufio.NewReader(os.Stdin) 53 | 54 | if _, err := r.WriteTo(p); err != nil { 55 | utils.Fatal(err) 56 | } 57 | } 58 | 59 | func addSourceAgentCmd(client *rpc.Client, response *server.RPCResp, meta *agents.AgentMetadata, args []string) { 60 | var id int 61 | 62 | meta.Role = agents.AgentRoleSource 63 | 64 | if err := client.Call("RPCServer.Add", &server.RPCReq{ 65 | Agent: &agents.AgentDescr{ 66 | Args: args, 67 | Meta: *meta, 68 | Type: agents.AgentTypeCmd, 69 | }, 70 | }, &id); err != nil { 71 | utils.Fatal(err) 72 | } 73 | 74 | fmt.Printf("Added agent %%%d\n", id) 75 | } 76 | -------------------------------------------------------------------------------- /agents/descr.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "strings" 7 | ) 8 | 9 | type AgentDescr struct { 10 | Args []string 11 | Meta AgentMetadata 12 | Type AgentType 13 | } 14 | 15 | func (a *AgentDescr) String() string { 16 | var args string 17 | 18 | if a.Type == AgentTypeCmd { 19 | args = strings.Join(a.Args, " ") 20 | } else if a.Type == AgentTypePipe { 21 | args = "[pipe]" 22 | } 23 | 24 | filter := "" 25 | 26 | if a.Meta.Role == AgentRoleSink && 27 | a.Meta.Filter != nil { 28 | filter = fmt.Sprintf(" [%s]", a.Meta.Filter.String()) 29 | } 30 | 31 | name := "" 32 | 33 | if a.Meta.Name != "" { 34 | name = " # " + a.Meta.Name 35 | } 36 | 37 | return fmt.Sprintf("%d %s %s%s %s%s", 38 | a.Meta.ID, a.Meta.Status.String(), a.Meta.Role.String(), filter, args, name) 39 | } 40 | 41 | func (a *AgentDescr) Text() string { 42 | var b bytes.Buffer 43 | 44 | dateFormat := "2006-01-02 15:04:05 -0700 MST" 45 | descr := "[pipe]" 46 | 47 | if a.Type == AgentTypeCmd { 48 | descr = strings.Join(a.Args, " ") 49 | } 50 | 51 | if a.Meta.Name != "" { 52 | descr += " # " + a.Meta.Name 53 | } 54 | 55 | fmt.Fprintf(&b, "%%%d %s agent\n", a.Meta.ID, a.Meta.Role.Text()) 56 | fmt.Fprintf(&b, "status: %s\n", a.Meta.Status.Text()) 57 | fmt.Fprintf(&b, "descr: %s\n", descr) 58 | 59 | if a.Meta.Status != AgentStatusNone { 60 | fmt.Fprintf(&b, "started: %s\n", a.Meta.Started.Format(dateFormat)) 61 | } else { 62 | fmt.Fprintf(&b, "started: not started\n") 63 | } 64 | 65 | if a.Meta.Status == AgentStatusFinished { 66 | fmt.Fprintf(&b, "finished: %s\n", a.Meta.Finished.Format(dateFormat)) 67 | } else { 68 | fmt.Fprintf(&b, "finished: not finished\n") 69 | } 70 | 71 | if a.Meta.User != "" { 72 | fmt.Fprintf(&b, "user: %s\n", a.Meta.User) 73 | } 74 | 75 | return b.String() 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RINGIO 2 | 3 | [![Build Status](https://drone.io/github.com/dullgiulio/ringio/status.png)](https://drone.io/github.com/dullgiulio/ringio/latest) 4 | 5 | Ringio (pronounced *ring-yo*) is a tool for creating interactive data pipes. It is what you get when you mix "screen"/"tmux" with "tee". 6 | 7 | ### Usage 8 | 9 | - Start a new ringio session 10 | ```bash 11 | $ ringio web-logs open & 12 | ``` 13 | - Add some input agents 14 | ```bash 15 | $ ringio web-logs input tail -f /var/log/httpd/access_log 16 | Added agent %1 17 | ``` 18 | - Add some output agents or get output on the terminal 19 | ```bash 20 | $ ringio web-logs output ./count-useragents 21 | Added agent %2 22 | $ ringio web-logs output wc -l 23 | Added agent %3 24 | $ ringio web-logs output # Will print to the console. 25 | ``` 26 | - List agents for a session: 27 | ```bash 28 | $ ringio web-logs list 29 | 1 R <- tail -f /var/log/httpd/access_log 30 | 2 R -> ./count-useragents 31 | 3 R -> wc -l 32 | ``` 33 | - See the internal log for the session: 34 | ```bash 35 | $ ringio web-logs log 36 | ``` 37 | - Stop the agent counting the lines: 38 | ```bash 39 | $ ringio web-logs stop %3 40 | ``` 41 | - Retrieve 'wc -l' output (you can see any output by filtering it explicitly): 42 | ```bash 43 | $ ringio web-logs output %3 -no-wait 44 | 4526 45 | ``` 46 | - Close the session. 47 | ```bash 48 | $ ringio web-logs close 49 | ``` 50 | 51 | ### Filtering 52 | 53 | - Input and output agents can be filtered by writing their ID: 54 | ```bash 55 | $ ringio my-session output 3 56 | ``` 57 | Will display the output of agent %3. To filter out, negate the ID of the agent (ex: -3). 58 | 59 | ### Installation 60 | 61 | You need the Go lang development environment installed and set up: 62 | 63 | ```bash 64 | $ go get -u github.com/dullgiulio/ringio 65 | ``` 66 | 67 | Please see [the releases page](https://github.com/dullgiulio/ringio/releases) for further information. 68 | -------------------------------------------------------------------------------- /msg/filter.go: -------------------------------------------------------------------------------- 1 | package msg 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "sort" 7 | ) 8 | 9 | type Filter struct { 10 | Included []int 11 | Excluded []int 12 | } 13 | 14 | func NewFilter() *Filter { 15 | return &Filter{ 16 | Included: make([]int, 0), 17 | Excluded: make([]int, 0), 18 | } 19 | } 20 | 21 | func (f *Filter) In(id int) { 22 | f.Included = append(f.Included, id) 23 | sort.Ints(f.Included) 24 | } 25 | 26 | func (f *Filter) GetIn() []int { 27 | return f.Included 28 | } 29 | 30 | func (f *Filter) Out(id int) { 31 | f.Excluded = append(f.Excluded, id) 32 | sort.Ints(f.Excluded) 33 | } 34 | 35 | func (f *Filter) GetOut() []int { 36 | return f.Excluded 37 | } 38 | 39 | func (f *Filter) String() string { 40 | var buffer bytes.Buffer 41 | 42 | lin := len(f.Included) 43 | lout := len(f.Excluded) 44 | i := 0 45 | 46 | if lin > 0 { 47 | for _, in := range f.Included { 48 | buffer.WriteString(fmt.Sprintf("%d", in)) 49 | 50 | if i != lin-1 { 51 | buffer.WriteByte(',') 52 | } 53 | 54 | i++ 55 | } 56 | 57 | if lout > 0 { 58 | buffer.WriteByte(',') 59 | } 60 | } 61 | 62 | i = 0 63 | 64 | if lout > 0 { 65 | for _, out := range f.Excluded { 66 | buffer.WriteString(fmt.Sprintf("-%d", out)) 67 | 68 | if i != lout-1 { 69 | buffer.WriteByte(',') 70 | } 71 | 72 | i++ 73 | } 74 | } 75 | 76 | return buffer.String() 77 | } 78 | 79 | func (m *Message) Allowed(f *Filter) bool { 80 | if f == nil { 81 | return true 82 | } 83 | 84 | id := m.senderID 85 | 86 | if len(f.Excluded) > 0 { 87 | for _, out := range f.Excluded { 88 | if out == id { 89 | return false 90 | } 91 | } 92 | } 93 | 94 | if len(f.Included) > 0 { 95 | for _, in := range f.Included { 96 | if in == id { 97 | return true 98 | } 99 | } 100 | } else { 101 | return true 102 | } 103 | 104 | return false 105 | } 106 | -------------------------------------------------------------------------------- /pipe/pipe.go: -------------------------------------------------------------------------------- 1 | package pipe 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "syscall" 7 | 8 | "github.com/dullgiulio/ringio/log" 9 | ) 10 | 11 | type Pipe struct { 12 | file *os.File 13 | filename string 14 | isOpen bool 15 | } 16 | 17 | func New(filename string) *Pipe { 18 | return &Pipe{ 19 | filename: filename, 20 | } 21 | } 22 | 23 | func (p *Pipe) String() string { 24 | return p.filename 25 | } 26 | 27 | func (p *Pipe) Remove() error { 28 | return os.Remove(p.filename) 29 | } 30 | 31 | func (p *Pipe) OpenWrite() bool { 32 | if err := p.OpenWriteErr(); err != nil { 33 | log.Error(log.FacilityPipe, err) 34 | return false 35 | } 36 | 37 | log.Debug(log.FacilityPipe, "Pipe was opened for writing at", p.filename) 38 | return true 39 | } 40 | 41 | func (p *Pipe) Create() error { 42 | if err := syscall.Mknod(p.filename, syscall.S_IFIFO|0600, 0); err != nil { 43 | return fmt.Errorf("Creating pipe for write failed: %s", err) 44 | } 45 | 46 | return nil 47 | } 48 | 49 | func (p *Pipe) OpenWriteErr() error { 50 | file, err := os.OpenFile(p.filename, os.O_RDWR|os.O_APPEND, 0600) 51 | if err != nil { 52 | return fmt.Errorf("Opening pipe for writing failed: %s", err) 53 | } 54 | 55 | p.isOpen = true 56 | p.file = file 57 | 58 | return nil 59 | } 60 | 61 | func (p *Pipe) Write(b []byte) (n int, err error) { 62 | return p.file.Write(b) 63 | } 64 | 65 | func (p *Pipe) OpenRead() bool { 66 | if err := p.OpenReadErr(); err != nil { 67 | log.Error(log.FacilityPipe, err) 68 | return false 69 | } 70 | 71 | log.Debug(log.FacilityPipe, "Pipe was opened for reading at", p.filename) 72 | return true 73 | } 74 | 75 | func (p *Pipe) OpenReadErr() error { 76 | file, err := os.Open(p.filename) 77 | if err != nil { 78 | return fmt.Errorf("Opening pipe for reading failed: %s", err) 79 | } 80 | 81 | p.file = file 82 | 83 | return nil 84 | } 85 | 86 | func (p *Pipe) Read(b []byte) (n int, err error) { 87 | return p.file.Read(b) 88 | } 89 | 90 | func (p *Pipe) File() *os.File { 91 | return p.file 92 | } 93 | 94 | func (p *Pipe) Close() error { 95 | return p.file.Close() 96 | } 97 | -------------------------------------------------------------------------------- /agents/utils_test.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "testing" 7 | 8 | "github.com/dullgiulio/ringbuf" 9 | "github.com/dullgiulio/ringio/msg" 10 | ) 11 | 12 | func TestWriteToChan(t *testing.T) { 13 | var buf bytes.Buffer 14 | 15 | for i := 0; i < 100; i++ { 16 | fmt.Fprintf(&buf, "Some string %d\n", i) 17 | } 18 | 19 | c := make(chan []byte) 20 | cancel := make(chan bool) 21 | 22 | go writeToChan(c, cancel, &buf) 23 | 24 | for i := 0; i < 100; i++ { 25 | // Won't contain the newline now. 26 | s := fmt.Sprintf("Some string %d", i) 27 | readS := <-c 28 | 29 | if s != string(readS) { 30 | t.Error("Unexpected difference in reading to channel") 31 | break 32 | } 33 | } 34 | } 35 | 36 | type BufferCloser bytes.Buffer 37 | 38 | func (b BufferCloser) Close() error { 39 | return nil 40 | } 41 | 42 | func (b BufferCloser) Read(p []byte) (n int, err error) { 43 | buf := bytes.Buffer(b) 44 | return buf.Read(p) 45 | } 46 | 47 | func (b BufferCloser) Write(p []byte) (n int, err error) { 48 | buf := bytes.Buffer(b) 49 | return buf.Write(p) 50 | } 51 | 52 | func TestWriteToRingbuf(t *testing.T) { 53 | var buf bytes.Buffer 54 | 55 | for i := 0; i < 100; i++ { 56 | fmt.Fprintf(&buf, "Some string %d\n", i) 57 | } 58 | 59 | go readLogs(t) 60 | 61 | ring := ringbuf.NewRingbuf(100) 62 | go ring.Run() 63 | 64 | cancel := make(chan bool, 1) 65 | cancel <- true 66 | 67 | cancelled := writeToRingbuf(0, BufferCloser(buf), ring, cancel, nil) 68 | 69 | if !cancelled { 70 | t.Error("Cancelling writeToRingbuf didn't cancel") 71 | } 72 | 73 | ring.EOF() 74 | 75 | reader := ringbuf.NewReader(ring) 76 | readCh := reader.ReadCh() 77 | i := 0 78 | 79 | for data := range readCh { 80 | str := fmt.Sprintf("Some string %d", i) 81 | 82 | if string(data.([]byte)) != str { 83 | t.Error("Unexpected string was written") 84 | } 85 | 86 | i++ 87 | } 88 | 89 | reader.Cancel() 90 | ring.Cancel() 91 | } 92 | 93 | func TestReadFromRingbuf(t *testing.T) { 94 | var buf bytes.Buffer 95 | 96 | ring := ringbuf.NewRingbuf(100) 97 | go ring.Run() 98 | 99 | for i := 0; i < 10; i++ { 100 | m := msg.Msg(0, []byte(fmt.Sprintf("Some string %d\n", i))) 101 | ring.Write(m) 102 | } 103 | 104 | ring.EOF() 105 | 106 | cancel := make(chan bool) 107 | 108 | cancelled := readFromRingbuf(BufferCloser(buf), nil, msg.FORMAT_NEWLINE, ring, nil, cancel, nil) 109 | 110 | if cancelled { 111 | t.Error("Did not expect readFromRingbuf to be cancelled") 112 | } 113 | 114 | ring.Cancel() 115 | } 116 | -------------------------------------------------------------------------------- /msg/filter_test.go: -------------------------------------------------------------------------------- 1 | package msg 2 | 3 | import ( 4 | "bytes" 5 | "encoding/gob" 6 | "testing" 7 | ) 8 | 9 | func _compSlice(s1, s2 []int) bool { 10 | l1 := len(s1) 11 | l2 := len(s2) 12 | 13 | if l1 != l2 { 14 | return false 15 | } 16 | 17 | for i := 0; i < l1; i++ { 18 | if s1[i] != s2[i] { 19 | return false 20 | } 21 | } 22 | 23 | return true 24 | } 25 | 26 | func TestNewFilter(t *testing.T) { 27 | f := NewFilter() 28 | 29 | if len(f.Included) != 0 { 30 | t.Error("In vector not initialized properly") 31 | } 32 | 33 | if len(f.Excluded) != 0 { 34 | t.Error("Out vector not initialized properly") 35 | } 36 | 37 | f.In(1) 38 | 39 | if len(f.Included) != 1 && f.Included[0] != 1 { 40 | t.Error("In entry not set correctly") 41 | } 42 | 43 | f.Out(2) 44 | 45 | if len(f.Excluded) != 1 && f.Excluded[0] != 2 { 46 | t.Error("Out entry not set correctly") 47 | } 48 | } 49 | 50 | func TestFilter(t *testing.T) { 51 | m := Msg(1, []byte("Test message")) 52 | 53 | if !m.Allowed(nil) { 54 | t.Error("Allowed returned false on nil filter") 55 | } 56 | 57 | f := NewFilter() 58 | // Both filtered in and out, is not allowed. 59 | f.Out(2) 60 | f.Out(1) 61 | f.In(2) 62 | f.In(1) 63 | 64 | if !_compSlice(f.GetOut(), []int{1, 2}) { 65 | t.Error("Unexpected return from GetOut") 66 | } 67 | 68 | if !_compSlice(f.GetIn(), []int{1, 2}) { 69 | t.Error("Unexpected return from GetIn") 70 | } 71 | 72 | if f.String() != "1,2,-1,-2" { 73 | t.Error("Unexpected string representation") 74 | } 75 | 76 | if m.Allowed(f) { 77 | t.Error("Did not expect message to be allowed") 78 | } 79 | 80 | f = NewFilter() 81 | // Filtered in must be allowed. 82 | f.In(1) 83 | 84 | if f.String() != "1" { 85 | t.Error("Unexpected string representation") 86 | } 87 | 88 | if !m.Allowed(f) { 89 | t.Error("Did not expect message to be disallowed") 90 | } 91 | 92 | f = NewFilter() 93 | // Filtered out, but not this one. 94 | f.Out(10) 95 | 96 | if f.String() != "-10" { 97 | t.Error("Unexpected string representation") 98 | } 99 | 100 | if !m.Allowed(f) { 101 | t.Error("Did not expect message to be disallowed") 102 | } 103 | 104 | f = NewFilter() 105 | // Something is filtered in, the rest must be filtered out. 106 | f.In(2) 107 | 108 | if m.Allowed(f) { 109 | t.Error("Did not expect message to be allowed") 110 | } 111 | } 112 | 113 | func TestCanGob(t *testing.T) { 114 | var buf bytes.Buffer 115 | 116 | f := NewFilter() 117 | f.In(1) 118 | f.Out(2) 119 | 120 | enc := gob.NewEncoder(&buf) 121 | if err := enc.Encode(f); err != nil { 122 | t.Error(err) 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /msg/msg.go: -------------------------------------------------------------------------------- 1 | package msg 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io" 7 | "strconv" 8 | "strings" 9 | "time" 10 | ) 11 | 12 | type Format int 13 | 14 | const ( 15 | FORMAT_NULL Format = 1 << iota 16 | FORMAT_ID 17 | FORMAT_TIMESTAMP 18 | FORMAT_DATA 19 | FORMAT_NEWLINE 20 | FORMAT_META = FORMAT_ID | FORMAT_TIMESTAMP | FORMAT_DATA 21 | ) 22 | 23 | type Message struct { 24 | senderID int 25 | time int64 26 | data []byte 27 | } 28 | 29 | func makeTimestamp() int64 { 30 | return time.Now().UnixNano() / int64(time.Millisecond) 31 | } 32 | 33 | func Msg(senderID int, data []byte) Message { 34 | return Message{senderID, makeTimestamp(), data} 35 | } 36 | 37 | // XXX: We implement only the combinations we actually use. 38 | func (m Message) WriteFormat(w io.Writer, f Format) (int, error) { 39 | mask := FORMAT_META | FORMAT_NEWLINE 40 | 41 | if (f & mask) == mask { 42 | return fmt.Fprintf(w, "%s: % 3d: %s\n", time.Unix(m.time, 0).Format(time.RFC3339), m.senderID, m.data) 43 | } 44 | 45 | mask = FORMAT_META 46 | 47 | if (f & mask) == mask { 48 | return fmt.Fprintf(w, "%s: % 3d: %s", time.Unix(m.time, 0).Format(time.RFC3339), m.senderID, m.data) 49 | } 50 | 51 | if (f & FORMAT_NEWLINE) == FORMAT_NEWLINE { 52 | return fmt.Fprintf(w, "%s\n", m.data) 53 | } 54 | 55 | return fmt.Fprintf(w, "%s", m.data) 56 | } 57 | 58 | func (m Message) Format(f Format) string { 59 | var b bytes.Buffer 60 | 61 | m.WriteFormat(&b, f) 62 | return b.String() 63 | } 64 | 65 | func (m Message) String() string { 66 | return fmt.Sprintf("%s: % 3d: %s", time.Unix(m.time, 0).Format(time.RFC3339), m.senderID, m.data) 67 | } 68 | 69 | func (m Message) Data() []byte { 70 | return m.data 71 | } 72 | 73 | func Parse(msg []byte) (Message, error) { 74 | m := Message{} 75 | 76 | if len(msg) < 34 { 77 | return m, fmt.Errorf("Invalid string to parse: string too short") 78 | } 79 | 80 | if t, err := time.Parse(time.RFC3339, string(msg[0:25])); err != nil { 81 | return m, err 82 | } else { 83 | m.time = t.Unix() 84 | } 85 | 86 | str := string(msg[27:]) 87 | end := strings.Index(str, ": ") 88 | 89 | if end < 0 { 90 | return m, fmt.Errorf("Invalid string to parse: agent ID not found") 91 | } 92 | 93 | if senderid, err := strconv.Atoi(strings.TrimSpace(str[0:end])); err != nil { 94 | return m, err 95 | } else { 96 | m.senderID = senderid 97 | } 98 | 99 | m.data = []byte(str[end+2:]) 100 | 101 | return m, nil 102 | } 103 | 104 | func Cast(i interface{}) Message { 105 | if m, ok := i.(Message); !ok { 106 | if d, ok := i.([]byte); !ok { 107 | panic("Cast to msg.Message failed") 108 | } else { 109 | return Msg(0, d) 110 | } 111 | } else { 112 | return m 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /server/server_test.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/dullgiulio/ringio/agents" 7 | "github.com/dullgiulio/ringio/log" 8 | "github.com/dullgiulio/ringio/onexit" 9 | ) 10 | 11 | func TestRPCServer(t *testing.T) { 12 | proceed := make(chan struct{}) 13 | 14 | go func() { 15 | if !log.Run(log.LevelWarn) { 16 | t.Error("Expected success after cancel") 17 | } 18 | proceed <- struct{}{} 19 | }() 20 | 21 | s := NewRPCServer("test-socket", true) 22 | 23 | resp := 0 24 | 25 | req := &RPCReq{ 26 | Agent: &agents.AgentDescr{ 27 | Args: []string{}, 28 | Meta: agents.AgentMetadata{Role: agents.AgentRoleSource}, 29 | Type: agents.AgentTypeNull, 30 | }, 31 | } 32 | 33 | if err := s.Ping(req, &resp); err != nil || resp != 1 { 34 | t.Error(err) 35 | } 36 | 37 | if err := s.Add(req, &resp); err != nil { 38 | t.Error(err) 39 | } 40 | 41 | if resp == 0 { 42 | t.Error("Expected an ID as response") 43 | } 44 | 45 | var done RPCResp 46 | 47 | if err := s.Kill(resp, &done); err != nil { 48 | t.Error(err) 49 | } 50 | 51 | if !done { 52 | t.Error("Expected Kill to return success") 53 | } 54 | 55 | done = false 56 | 57 | if err := s.Run(nil, &done); err != nil { 58 | t.Error(err) 59 | } 60 | 61 | if !done { 62 | t.Error("Expected Run to return success") 63 | } 64 | 65 | // TODO: Test List 66 | list := agents.NewAgentMessageResponseList() 67 | 68 | if err := s.List(nil, &list); err != nil { 69 | t.Error(err) 70 | } 71 | 72 | if !done { 73 | t.Error("Expected List to return success") 74 | } 75 | 76 | if list.Agents == nil { 77 | t.Error("Did not expect retrieved list for List command to be null") 78 | } 79 | 80 | exit := make(chan int) 81 | 82 | onexit.SetFunc(func(i int) { 83 | exit <- i 84 | }) 85 | onexit.HandleInterrupt() 86 | 87 | go func() { 88 | if err := s.Close(nil, &done); err != nil { 89 | t.Error(err) 90 | } 91 | }() 92 | 93 | if i := <-exit; i != 0 { 94 | t.Error("Successful Close did not exit with zero") 95 | } 96 | 97 | // Check that the server doesn't accept commands after Close. 98 | if err := s.Add(req, &resp); err == nil { 99 | t.Error("Expected error on Add on closed server") 100 | } 101 | 102 | if err := s.Stop(resp, &done); err == nil { 103 | t.Error("Expected error on Stop on closed server") 104 | } 105 | 106 | if err := s.List(nil, &list); err == nil { 107 | t.Error("Expected error on List on closed server") 108 | } 109 | 110 | if err := s.Run(nil, &done); err == nil { 111 | t.Error("Expected error on Run on closed server") 112 | } 113 | 114 | if err := s.Close(nil, &done); err == nil { 115 | t.Error("Expected error on Close on closed server") 116 | } 117 | 118 | <-proceed 119 | } 120 | -------------------------------------------------------------------------------- /agents/response.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | type AgentMessageResponse interface { 4 | Ok() 5 | Err(error) 6 | Data(interface{}) 7 | Get() (interface{}, error) 8 | } 9 | 10 | type AgentMessageResponseString struct { 11 | readyCh chan bool 12 | data string 13 | err error 14 | } 15 | 16 | func NewAgentMessageResponseString() AgentMessageResponseString { 17 | return AgentMessageResponseString{ 18 | readyCh: make(chan bool), 19 | } 20 | } 21 | 22 | func (r *AgentMessageResponseString) Get() (interface{}, error) { 23 | <-r.readyCh 24 | return r.data, r.err 25 | } 26 | 27 | func (r *AgentMessageResponseString) Ok() { 28 | r.readyCh <- true 29 | } 30 | 31 | func (r *AgentMessageResponseString) Err(err error) { 32 | r.err = err 33 | r.readyCh <- false 34 | } 35 | 36 | func (r *AgentMessageResponseString) Data(data interface{}) { 37 | r.data = data.(string) 38 | } 39 | 40 | type AgentMessageResponseInt struct { 41 | i int 42 | err chan error 43 | } 44 | 45 | func NewAgentMessageResponseInt() *AgentMessageResponseInt { 46 | return &AgentMessageResponseInt{ 47 | err: make(chan error), 48 | } 49 | } 50 | 51 | func (r *AgentMessageResponseInt) Get() (i interface{}, e error) { 52 | return r.i, <-r.err 53 | } 54 | 55 | func (r *AgentMessageResponseInt) Ok() { 56 | r.err <- nil 57 | } 58 | 59 | func (r *AgentMessageResponseInt) Err(err error) { 60 | r.err <- err 61 | } 62 | 63 | func (r *AgentMessageResponseInt) Data(i interface{}) { 64 | r.i = i.(int) 65 | } 66 | 67 | type AgentMessageResponseBool chan error 68 | 69 | func NewAgentMessageResponseBool() AgentMessageResponseBool { 70 | return make(chan error) 71 | } 72 | 73 | func (r AgentMessageResponseBool) Get() (interface{}, error) { 74 | err := <-r 75 | val := false 76 | 77 | if err == nil { 78 | val = true 79 | } 80 | 81 | return val, err 82 | } 83 | 84 | func (r AgentMessageResponseBool) Ok() { 85 | r <- nil 86 | } 87 | 88 | func (r AgentMessageResponseBool) Err(err error) { 89 | r <- err 90 | } 91 | 92 | func (r AgentMessageResponseBool) Data(interface{}) { 93 | // Do nothin' 94 | } 95 | 96 | type AgentMessageResponseList struct { 97 | errorCh chan error 98 | Agents *[]AgentDescr 99 | Error error 100 | } 101 | 102 | func NewAgentMessageResponseList() AgentMessageResponseList { 103 | return AgentMessageResponseList{ 104 | errorCh: make(chan error), 105 | } 106 | } 107 | 108 | func (r *AgentMessageResponseList) Get() (interface{}, error) { 109 | r.Error = <-r.errorCh 110 | return r.Agents, r.Error 111 | } 112 | 113 | func (r *AgentMessageResponseList) Ok() { 114 | r.errorCh <- nil 115 | } 116 | 117 | func (r *AgentMessageResponseList) Err(err error) { 118 | r.errorCh <- err 119 | } 120 | 121 | func (r *AgentMessageResponseList) Data(data interface{}) { 122 | r.Agents = data.(*[]AgentDescr) 123 | } 124 | -------------------------------------------------------------------------------- /agents/pipe.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/dullgiulio/ringbuf" 7 | "github.com/dullgiulio/ringio/log" 8 | "github.com/dullgiulio/ringio/msg" 9 | "github.com/dullgiulio/ringio/pipe" 10 | ) 11 | 12 | type AgentPipe struct { 13 | pipe *pipe.Pipe 14 | meta *AgentMetadata 15 | cancelCh chan bool 16 | writeCh chan struct{} 17 | } 18 | 19 | func NewAgentPipe(pipeName string, meta *AgentMetadata) *AgentPipe { 20 | return &AgentPipe{ 21 | pipe: pipe.New(pipeName), 22 | meta: meta, 23 | cancelCh: make(chan bool), 24 | writeCh: make(chan struct{}), 25 | } 26 | } 27 | 28 | func (a *AgentPipe) Init() { 29 | if a.meta.Options == nil { 30 | a.meta.Options = &AgentOptions{} 31 | } 32 | } 33 | 34 | func (a *AgentPipe) Meta() *AgentMetadata { 35 | return a.meta 36 | } 37 | 38 | func (a *AgentPipe) Descr() AgentDescr { 39 | return AgentDescr{ 40 | Args: []string{a.String()}, 41 | Meta: *a.meta, 42 | Type: AgentTypePipe, 43 | } 44 | } 45 | 46 | func (a *AgentPipe) String() string { 47 | return "|" + a.pipe.String() 48 | } 49 | 50 | func (a *AgentPipe) cancel() error { 51 | if a.meta.Status.IsRunning() { 52 | a.cancelCh <- true 53 | } 54 | 55 | return nil 56 | } 57 | 58 | func (a *AgentPipe) Stop() error { 59 | if err := a.cancel(); err != nil { 60 | return err 61 | } 62 | 63 | log.Info(log.FacilityAgent, fmt.Sprintf("PipeAgent %d has been stopped", a.meta.ID)) 64 | return nil 65 | } 66 | 67 | func (a *AgentPipe) Kill() error { 68 | if err := a.Stop(); err != nil { 69 | return err 70 | } 71 | 72 | log.Info(log.FacilityAgent, fmt.Sprintf("PipeAgent %d has been killed", a.meta.ID)) 73 | return nil 74 | } 75 | 76 | func (a *AgentPipe) WaitFinish() error { 77 | return nil 78 | } 79 | 80 | func (a *AgentPipe) InputToRingbuf(rErrors, rOutput *ringbuf.Ringbuf) { 81 | if ok := a.pipe.OpenRead(); !ok { 82 | return 83 | } 84 | 85 | id := a.meta.ID 86 | 87 | cancelled := writeToRingbuf(id, a.pipe.File(), rOutput, a.cancelCh, nil) 88 | 89 | if !cancelled { 90 | <-a.cancelCh 91 | } 92 | 93 | close(a.cancelCh) 94 | a.pipe.Close() 95 | } 96 | 97 | func (a *AgentPipe) StartWrite() { 98 | a.writeCh <- struct{}{} 99 | } 100 | 101 | func (a *AgentPipe) OutputFromRingbuf(rStdout, rErrors, rOutput *ringbuf.Ringbuf, filter *msg.Filter) { 102 | if ok := a.pipe.OpenWrite(); !ok { 103 | // TODO:XXX: Must return the error to the user. 104 | return 105 | } 106 | 107 | // Wait for the client to be ready to receive 108 | <-a.writeCh 109 | 110 | cancelled := readFromRingbuf(a.pipe.File(), filter, a.meta.Options.getMask(), rOutput, 111 | makeReaderOptions(a.meta.Options), a.cancelCh, nil) 112 | 113 | if !cancelled { 114 | <-a.cancelCh 115 | } 116 | 117 | close(a.cancelCh) 118 | close(a.writeCh) 119 | a.pipe.Close() 120 | } 121 | -------------------------------------------------------------------------------- /msg/msg_test.go: -------------------------------------------------------------------------------- 1 | package msg 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func TestString(t *testing.T) { 10 | m := Message{10, 1416926265, []byte("Some text here")} 11 | tstr := time.Unix(1416926265, 0).Format(time.RFC3339) 12 | mstr := m.String() 13 | 14 | if mstr != fmt.Sprintf("%s: 10: Some text here", tstr) { 15 | t.Error(fmt.Errorf("Unexpected string format for Message, got '%s'", mstr)) 16 | } 17 | } 18 | 19 | func TestFormat(t *testing.T) { 20 | m := Message{10, 1416926265, []byte("Some text here")} 21 | tstr := time.Unix(1416926265, 0).Format(time.RFC3339) 22 | mstr := m.Format(FORMAT_META | FORMAT_NEWLINE) 23 | 24 | if mstr != fmt.Sprintf("%s: 10: Some text here\n", tstr) { 25 | t.Error(fmt.Errorf("Unexpected formatted message with newline, got '%s'", mstr)) 26 | } 27 | 28 | mstr = m.Format(FORMAT_META) 29 | if mstr != fmt.Sprintf("%s: 10: Some text here", tstr) { 30 | t.Error(fmt.Errorf("Unexpected formatted message, got '%s'", mstr)) 31 | } 32 | 33 | mstr = m.Format(FORMAT_NEWLINE) 34 | if mstr != "Some text here\n" { 35 | t.Error(fmt.Errorf("Unexpected data message with newline, got '%s'", mstr)) 36 | } 37 | 38 | mstr = m.Format(FORMAT_DATA) 39 | if mstr != "Some text here" { 40 | t.Error(fmt.Errorf("Unexpected data message, got '%s'", mstr)) 41 | } 42 | } 43 | 44 | func TestParse(t *testing.T) { 45 | msg := []byte("2014-11-25T16:37:45+02:00: 5: Test message text to parse") 46 | m, err := Parse(msg) 47 | 48 | if err != nil { 49 | t.Error(err) 50 | } 51 | 52 | if m.senderID != 5 { 53 | t.Error("Expected senderID to be set correctly") 54 | } 55 | 56 | if m.time != 1416926265 { 57 | t.Error("Expected time to be set correctly") 58 | } 59 | 60 | if string(m.data) != "Test message text to parse" { 61 | t.Error("Expected data to be set correctly") 62 | } 63 | } 64 | 65 | func TestTimestap(t *testing.T) { 66 | ts := makeTimestamp() / 1000 67 | tm := time.Unix(ts, 0) 68 | 69 | if tm.After(time.Now()) { 70 | t.Error("Time was given in the future") 71 | } 72 | 73 | if ts <= 1416926265 { 74 | t.Error("Time was given before the test was written") 75 | } 76 | } 77 | 78 | func TestCasting(t *testing.T) { 79 | str := "some random string" 80 | m := Cast([]byte(str)) 81 | 82 | if string(m.Data()) != str { 83 | t.Error("Casting did not preserve data") 84 | } 85 | 86 | str = "some data" 87 | m = Msg(2, []byte(str)) 88 | mc := Cast(m) 89 | 90 | if string(mc.Data()) != str || mc.senderID != 2 { 91 | t.Error("Casting did not preserve a Message") 92 | } 93 | } 94 | 95 | func TestInvalid(t *testing.T) { 96 | str := []byte("no string but string was expected") 97 | _, err := Parse(str) 98 | 99 | if err == nil { 100 | t.Error("Expected error after passing invalid message") 101 | } 102 | 103 | defer func() { 104 | if r := recover(); r == nil { 105 | t.Error("Expected panic()") 106 | } 107 | }() 108 | 109 | // Will trigger a panic. 110 | Cast(interface{}(NewFilter())) 111 | } 112 | -------------------------------------------------------------------------------- /agents/utils.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "sync" 8 | 9 | "github.com/dullgiulio/ringbuf" 10 | "github.com/dullgiulio/ringio/log" 11 | "github.com/dullgiulio/ringio/msg" 12 | ) 13 | 14 | func makeReaderOptions(opts *AgentOptions) *ringbuf.ReaderOptions { 15 | ropts := &ringbuf.ReaderOptions{} 16 | 17 | if opts != nil { 18 | if opts.NoWait { 19 | ropts.NoStarve = true 20 | } 21 | } 22 | 23 | return ropts 24 | } 25 | 26 | // TODO:XXX: Causes data race. This routine should terminate when the reader is closed 27 | // This probably means it needs to be moved somewhere else. 28 | func writeToChan(c chan<- []byte, cancel chan bool, reader io.Reader) { 29 | scanner := bufio.NewScanner(reader) 30 | 31 | for scanner.Scan() { 32 | c <- scanner.Bytes() 33 | } 34 | 35 | if err := scanner.Err(); err != nil { 36 | log.Error(log.FacilityAgent, fmt.Errorf("bufio.Scanner: %v", err)) 37 | } 38 | 39 | log.Debug(log.FacilityAgent, "Writing into channel from input has terminated") 40 | 41 | close(c) 42 | } 43 | 44 | func writeToRingbuf(id int, reader io.Reader, ring *ringbuf.Ringbuf, cancel chan bool, wg *sync.WaitGroup) (cancelled bool) { 45 | if wg != nil { 46 | defer wg.Done() 47 | } 48 | 49 | c := make(chan []byte) 50 | 51 | go writeToChan(c, cancel, reader) 52 | 53 | for { 54 | select { 55 | case data, ok := <-c: 56 | if !ok { 57 | return 58 | } 59 | 60 | ring.Write(msg.Msg(id, data)) 61 | case <-cancel: 62 | cancelled = true 63 | log.Debug(log.FacilityAgent, "Writing into ringbuf from input has been cancelled") 64 | return 65 | } 66 | } 67 | } 68 | 69 | func _readInnerLoop(c <-chan interface{}, cancel <-chan bool, 70 | output io.Writer, filter *msg.Filter, format msg.Format) (cancelled bool) { 71 | for { 72 | select { 73 | case data := <-c: 74 | if data == nil { 75 | return 76 | } 77 | 78 | m := msg.Cast(data) 79 | 80 | if !m.Allowed(filter) { 81 | continue 82 | } 83 | 84 | if _, err := m.WriteFormat(output, format); err != nil { 85 | log.Error(log.FacilityAgent, fmt.Errorf("bufio.Write: %v", err)) 86 | return 87 | } 88 | case <-cancel: 89 | cancelled = true 90 | return 91 | } 92 | } 93 | } 94 | 95 | func readFromRingbuf(writer io.WriteCloser, filter *msg.Filter, flags msg.Format, 96 | ring *ringbuf.Ringbuf, readerOpts *ringbuf.ReaderOptions, 97 | cancel <-chan bool, wg *sync.WaitGroup) (cancelled bool) { 98 | if wg != nil { 99 | defer wg.Done() 100 | } 101 | 102 | reader := ringbuf.NewReader(ring) 103 | 104 | if readerOpts != nil { 105 | reader.SetOptions(readerOpts) 106 | } 107 | 108 | c := reader.ReadCh() 109 | 110 | cancelled = _readInnerLoop(c, cancel, writer, filter, flags) 111 | 112 | reader.Cancel() 113 | writer.Close() 114 | 115 | if cancelled { 116 | log.Debug(log.FacilityAgent, "Read from ringbuf has been cancelled") 117 | } else { 118 | log.Debug(log.FacilityAgent, "Read from ringbuf terminated") 119 | } 120 | 121 | return 122 | } 123 | -------------------------------------------------------------------------------- /client/sink.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net/rpc" 7 | "os" 8 | 9 | "github.com/dullgiulio/ringio/agents" 10 | "github.com/dullgiulio/ringio/onexit" 11 | "github.com/dullgiulio/ringio/pipe" 12 | "github.com/dullgiulio/ringio/server" 13 | "github.com/dullgiulio/ringio/utils" 14 | ) 15 | 16 | func addErrorsAgentPipe(client *rpc.Client, meta *agents.AgentMetadata, response *server.RPCResp, pipeName string) { 17 | _addSinkAgentPipe(client, meta, response, pipeName, agents.AgentRoleErrors) 18 | } 19 | 20 | func addSinkAgentPipe(client *rpc.Client, meta *agents.AgentMetadata, response *server.RPCResp, pipeName string) { 21 | _addSinkAgentPipe(client, meta, response, pipeName, agents.AgentRoleSink) 22 | } 23 | 24 | func addLogAgentPipe(client *rpc.Client, response *server.RPCResp, pipeName string) { 25 | _addSinkAgentPipe(client, &agents.AgentMetadata{}, response, pipeName, agents.AgentRoleLog) 26 | } 27 | 28 | func _addSinkAgentPipe(client *rpc.Client, meta *agents.AgentMetadata, 29 | response *server.RPCResp, pipeName string, role agents.AgentRole) { 30 | var id int 31 | 32 | p := pipe.New(pipeName) 33 | 34 | meta.Role = role 35 | 36 | if err := p.Create(); err != nil { 37 | utils.Fatal(fmt.Errorf("Couldn't create pipe: %s", err)) 38 | } 39 | 40 | if err := client.Call("RPCServer.Add", &server.RPCReq{ 41 | Agent: &agents.AgentDescr{ 42 | Args: []string{pipeName}, 43 | Meta: *meta, 44 | Type: agents.AgentTypePipe, 45 | }, 46 | }, &id); err != nil { 47 | p.Remove() 48 | utils.Fatal(err) 49 | } 50 | 51 | done := make(chan struct{}) 52 | 53 | go func() { 54 | if err := client.Call("RPCServer.WriteReady", id, &response); err != nil { 55 | p.Remove() 56 | utils.Fatal(err) 57 | } 58 | 59 | close(done) 60 | }() 61 | 62 | // Open will block until the pipe is opened on the other side. 63 | if err := p.OpenReadErr(); err != nil { 64 | p.Remove() 65 | utils.Fatal(fmt.Errorf("Couldn't open pipe for reading: %s", err)) 66 | } 67 | 68 | <-done 69 | p.Remove() 70 | 71 | r := bufio.NewReader(p) 72 | 73 | if _, err := r.WriteTo(os.Stdout); err != nil { 74 | utils.Fatal(err) 75 | } 76 | 77 | onexit.Defer(func() { 78 | if err := client.Call("RPCServer.Stop", id, &response); err != nil { 79 | utils.Fatal(err) 80 | } 81 | }) 82 | } 83 | 84 | func addErrorsAgentCmd(client *rpc.Client, meta *agents.AgentMetadata, response *server.RPCResp, args []string) { 85 | _addSinkAgentCmd(client, meta, response, args, agents.AgentRoleErrors) 86 | } 87 | 88 | func addSinkAgentCmd(client *rpc.Client, meta *agents.AgentMetadata, response *server.RPCResp, args []string) { 89 | _addSinkAgentCmd(client, meta, response, args, agents.AgentRoleSink) 90 | } 91 | 92 | func _addSinkAgentCmd(client *rpc.Client, meta *agents.AgentMetadata, 93 | response *server.RPCResp, args []string, role agents.AgentRole) { 94 | var id int 95 | 96 | meta.Role = role 97 | 98 | if err := client.Call("RPCServer.Add", &server.RPCReq{ 99 | Agent: &agents.AgentDescr{ 100 | Args: args, 101 | Meta: *meta, 102 | Type: agents.AgentTypeCmd, 103 | }, 104 | }, &id); err != nil { 105 | utils.Fatal(err) 106 | } 107 | 108 | fmt.Printf("Added agent %%%d\n", id) 109 | } 110 | -------------------------------------------------------------------------------- /log/log.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "fmt" 7 | "io" 8 | "strings" 9 | "sync" 10 | "time" 11 | ) 12 | 13 | type Logger struct { 14 | loggers []io.Writer 15 | lock *sync.Mutex 16 | c chan *Message 17 | } 18 | 19 | var _logger Logger 20 | 21 | type Facility string 22 | 23 | const ( 24 | FacilityRing Facility = "ringbuf" 25 | FacilityAgent = "agent" 26 | FacilityDefault = "ringio" 27 | FacilityStdout = "stdout" 28 | FacilityStderr = "stderr" 29 | FacilityPipe = "pipe" 30 | ) 31 | 32 | type Level int 33 | 34 | const ( 35 | LevelDebug Level = iota 36 | LevelInfo 37 | LevelWarn 38 | LevelError 39 | LevelFail 40 | LevelCancel 41 | ) 42 | 43 | type Message struct { 44 | facility Facility 45 | level Level 46 | message string 47 | } 48 | 49 | var prefixes = map[Level]string{ 50 | LevelDebug: "DEBUG", 51 | LevelInfo: "INFO", 52 | LevelWarn: "WARN", 53 | LevelError: "ERROR", 54 | LevelFail: "FAIL", 55 | } 56 | 57 | func (l Level) String() string { 58 | return prefixes[l] 59 | } 60 | 61 | func LevelFromString(s string) (Level, error) { 62 | s = strings.ToUpper(s) 63 | 64 | for l, sl := range prefixes { 65 | if sl == s { 66 | return l, nil 67 | } 68 | } 69 | 70 | return LevelInfo, errors.New("Invalid value") 71 | } 72 | 73 | func init() { 74 | _logger = Logger{ 75 | loggers: make([]io.Writer, 0), 76 | lock: new(sync.Mutex), 77 | c: make(chan *Message), 78 | } 79 | } 80 | 81 | func _string(ds []interface{}) string { 82 | buffer := new(bytes.Buffer) 83 | 84 | for _, d := range ds { 85 | buffer.WriteString(fmt.Sprintf("%v ", d)) 86 | } 87 | 88 | return buffer.String() 89 | } 90 | 91 | func AddWriter(w io.Writer) { 92 | _logger.lock.Lock() 93 | defer _logger.lock.Unlock() 94 | 95 | _logger.loggers = append(_logger.loggers, w) 96 | } 97 | 98 | func Debug(facility Facility, message ...interface{}) { 99 | _logger.c <- &Message{facility, LevelDebug, _string(message)} 100 | } 101 | 102 | func Info(facility Facility, message ...interface{}) { 103 | _logger.c <- &Message{facility, LevelInfo, _string(message)} 104 | } 105 | 106 | func Warn(facility Facility, message ...interface{}) { 107 | _logger.c <- &Message{facility, LevelWarn, _string(message)} 108 | } 109 | 110 | func Error(facility Facility, message ...interface{}) { 111 | _logger.c <- &Message{facility, LevelError, _string(message)} 112 | } 113 | 114 | func Fatal(facility Facility, message ...interface{}) { 115 | _logger.c <- &Message{facility, LevelFail, _string(message)} 116 | } 117 | 118 | func Cancel() { 119 | _logger.c <- &Message{level: LevelCancel} 120 | } 121 | 122 | func Run(minLevel Level) bool { 123 | for m := range _logger.c { 124 | if m.level == LevelCancel { 125 | return true 126 | } 127 | 128 | if m.level < minLevel { 129 | continue 130 | } 131 | 132 | t := time.Now().Format(time.RFC3339) 133 | s := []byte(_string([]interface{}{t, m.level.String() + ":", m.facility + ":", m.message})) 134 | 135 | _logger.lock.Lock() 136 | 137 | for _, l := range _logger.loggers { 138 | l.Write(s) 139 | } 140 | 141 | _logger.lock.Unlock() 142 | 143 | if m.level == LevelFail { 144 | return false 145 | } 146 | } 147 | 148 | return true 149 | } 150 | -------------------------------------------------------------------------------- /agents/response_test.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | ) 7 | 8 | func TestResponseString(t *testing.T) { 9 | rs := NewAgentMessageResponseString() 10 | r := AgentMessageResponse(&rs) 11 | 12 | go func() { 13 | r.Err(errors.New("Some error")) 14 | }() 15 | 16 | d, e := r.Get() 17 | 18 | if d.(string) != "" { 19 | t.Error("Expected response data to be empty string") 20 | } 21 | 22 | if e == nil { 23 | t.Error("Expected response error") 24 | } 25 | 26 | if e.Error() != "Some error" { 27 | t.Error("Unexpected error returned") 28 | } 29 | 30 | rs = NewAgentMessageResponseString() 31 | r = AgentMessageResponse(&rs) 32 | 33 | go func() { 34 | r.Data("some string") 35 | r.Ok() 36 | }() 37 | 38 | d, e = r.Get() 39 | 40 | if d.(string) != "some string" { 41 | t.Error("Unexpected data in response") 42 | } 43 | 44 | if e != nil { 45 | t.Error("Unexpected error in response") 46 | } 47 | } 48 | 49 | func TestResponseInt(t *testing.T) { 50 | rs := NewAgentMessageResponseInt() 51 | r := AgentMessageResponse(rs) 52 | 53 | go func() { 54 | r.Err(errors.New("Some error")) 55 | }() 56 | 57 | d, e := r.Get() 58 | 59 | if d.(int) != 0 { 60 | t.Error("Expected response data to be empty string") 61 | } 62 | 63 | if e == nil { 64 | t.Error("Expected response error") 65 | } 66 | 67 | if e.Error() != "Some error" { 68 | t.Error("Unexpected error returned") 69 | } 70 | 71 | rs = NewAgentMessageResponseInt() 72 | r = AgentMessageResponse(rs) 73 | 74 | go func() { 75 | r.Data(100) 76 | r.Ok() 77 | }() 78 | 79 | d, e = r.Get() 80 | 81 | if d.(int) != 100 { 82 | t.Error("Unexpected data in response") 83 | } 84 | 85 | if e != nil { 86 | t.Error("Unexpected error in response") 87 | } 88 | } 89 | 90 | func TestResponseBool(t *testing.T) { 91 | rs := NewAgentMessageResponseBool() 92 | r := AgentMessageResponse(&rs) 93 | 94 | go func() { 95 | r.Err(errors.New("Some error")) 96 | }() 97 | 98 | d, e := r.Get() 99 | 100 | if d.(bool) != false { 101 | t.Error("Expected response data to be empty string") 102 | } 103 | 104 | if e == nil { 105 | t.Error("Expected response error") 106 | } 107 | 108 | if e.Error() != "Some error" { 109 | t.Error("Unexpected error returned") 110 | } 111 | 112 | rs = NewAgentMessageResponseBool() 113 | r = AgentMessageResponse(&rs) 114 | 115 | go func() { 116 | // Calling .Data() on Bool doesn't do anything by choice. 117 | r.Ok() 118 | }() 119 | 120 | d, e = r.Get() 121 | 122 | if d.(bool) != true { 123 | t.Error("Unexpected data in response") 124 | } 125 | 126 | if e != nil { 127 | t.Error("Unexpected error in response") 128 | } 129 | } 130 | 131 | func TestResponseList(t *testing.T) { 132 | rs := NewAgentMessageResponseList() 133 | r := AgentMessageResponse(&rs) 134 | 135 | go func() { 136 | r.Err(errors.New("Some error")) 137 | }() 138 | 139 | d, e := r.Get() 140 | 141 | if d.(*[]AgentDescr) != nil { 142 | t.Error("Expected response data to be nil") 143 | } 144 | 145 | if e == nil { 146 | t.Error("Expected response error") 147 | } 148 | 149 | if e.Error() != "Some error" { 150 | t.Error("Unexpected error returned") 151 | } 152 | 153 | rs = NewAgentMessageResponseList() 154 | r = AgentMessageResponse(&rs) 155 | 156 | go func() { 157 | ag := []AgentDescr{ 158 | AgentDescr{}, 159 | AgentDescr{}, 160 | } 161 | 162 | r.Data(&ag) 163 | r.Ok() 164 | }() 165 | 166 | d, e = r.Get() 167 | 168 | ag := d.(*[]AgentDescr) 169 | if len(*ag) != 2 { 170 | t.Error("Unexpected data in response") 171 | } 172 | 173 | if e != nil { 174 | t.Error("Unexpected error in response") 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /agents/collection_test.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/dullgiulio/ringio/log" 7 | ) 8 | 9 | func readLogs(t *testing.T) { 10 | nw := new(log.NullWriter) 11 | log.AddWriter(nw) 12 | 13 | if !log.Run(log.LevelError) { 14 | t.Log("Did not expect logger to return an error") 15 | t.Fail() 16 | } 17 | } 18 | 19 | func TestAgentAdding(t *testing.T) { 20 | go readLogs(t) 21 | 22 | proceed := make(chan struct{}) 23 | ac := NewCollection() 24 | go func() { 25 | ac.Run(true) 26 | proceed <- struct{}{} 27 | }() 28 | 29 | meta := NewAgentMetadata() 30 | meta.Role = AgentRoleSource 31 | 32 | a0 := NewAgentNull(0, meta) 33 | resp := NewAgentMessageResponseBool() 34 | 35 | ac.Add(a0, &resp) 36 | if _, err := resp.Get(); err != nil { 37 | t.Error(err) 38 | } 39 | 40 | meta = a0.Meta() 41 | 42 | if meta.ID == 0 { 43 | t.Error("Did not expect ID to be still zero after adding") 44 | } 45 | firstID := meta.ID 46 | 47 | if meta.Role != AgentRoleSource { 48 | t.Error("Adding an agent changed its role") 49 | } 50 | 51 | if meta.Status == AgentStatusRunning { 52 | t.Error("Did not expected agent to be in running state") 53 | } 54 | 55 | if !meta.Started.IsZero() { 56 | t.Error("Did not expected starting time to be set") 57 | } 58 | 59 | if !meta.Finished.IsZero() { 60 | t.Error("Expected finish time to be still undefined") 61 | } 62 | 63 | meta = NewAgentMetadata() 64 | meta.Role = AgentRoleSource 65 | 66 | a1 := NewAgentNull(0, meta) 67 | resp = NewAgentMessageResponseBool() 68 | 69 | ac.Add(a1, &resp) 70 | resp.Get() 71 | 72 | ac.Start(a1, &resp) 73 | resp.Get() 74 | 75 | meta = a1.Meta() 76 | 77 | if meta.ID == 0 { 78 | t.Error("Did not expect ID to be still zero after adding") 79 | } 80 | 81 | if meta.ID <= firstID { 82 | t.Error("Expected second ID to be greater than the first") 83 | } 84 | 85 | if meta.Role != AgentRoleSource { 86 | t.Error("Adding an agent changed its role") 87 | } 88 | 89 | if meta.Status != AgentStatusRunning { 90 | t.Error("Expected agent to be in running state") 91 | } 92 | 93 | if meta.Started.IsZero() { 94 | t.Error("Expected starting time to be set") 95 | } 96 | 97 | if !meta.Finished.IsZero() { 98 | t.Error("Expected finish time to be still undefined") 99 | } 100 | 101 | ac.Start(a0, &resp) 102 | resp.Get() 103 | 104 | ac.Cancel(&resp) 105 | resp.Get() 106 | 107 | <-proceed 108 | 109 | meta = a0.Meta() 110 | if meta.ID == 0 { 111 | t.Error("Did not expect ID to be still zero after cancel") 112 | } 113 | 114 | if meta.Role != AgentRoleSource { 115 | t.Error("Stopping an agent changed its role") 116 | } 117 | 118 | if meta.Status != AgentStatusFinished { 119 | t.Error("Expected agent to be in finished state") 120 | } 121 | 122 | if meta.Started.IsZero() { 123 | t.Error("Expected starting time to be set") 124 | } 125 | 126 | if meta.Finished.IsZero() { 127 | t.Error("Expected finish time to be still undefined") 128 | } 129 | 130 | meta = a1.Meta() 131 | if meta.ID == 0 { 132 | t.Error("Did not expect ID to be still zero after cancel") 133 | } 134 | 135 | if meta.Role != AgentRoleSource { 136 | t.Error("Stopping an agent changed its role") 137 | } 138 | 139 | if meta.Status != AgentStatusFinished { 140 | t.Error("Expected agent to be in finished state") 141 | } 142 | 143 | if meta.Started.IsZero() { 144 | t.Error("Expected starting time to be set") 145 | } 146 | 147 | if meta.Finished.IsZero() { 148 | t.Error("Expected finish time to be still undefined") 149 | } 150 | 151 | log.Cancel() 152 | } 153 | 154 | func TestInvalidActions(t *testing.T) { 155 | go readLogs(t) 156 | 157 | proceed := make(chan struct{}) 158 | ac := NewCollection() 159 | go func() { 160 | ac.Run(false) 161 | proceed <- struct{}{} 162 | }() 163 | 164 | resp := NewAgentMessageResponseBool() 165 | 166 | // Test that cancel exits correctly. 167 | ac.Cancel(&resp) 168 | resp.Get() 169 | 170 | <-proceed 171 | 172 | log.Cancel() 173 | } 174 | -------------------------------------------------------------------------------- /client/client_test.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestArgsParsing(t *testing.T) { 8 | cli := NewCli() 9 | err := cli.ParseArgs([]string{"ringio", "session-name", "input"}) 10 | 11 | if err != nil { 12 | t.Error(err) 13 | t.Fail() 14 | } 15 | 16 | if cli.Session != "session-name" { 17 | t.Error("Expected 'session-name' as session name") 18 | } 19 | 20 | if cli.CommandStr != "input" { 21 | t.Error("Expected 'input' as command name") 22 | } 23 | 24 | err = cli.ParseArgs([]string{"ringio", "session-name", "output", "-no-wait", "tail", "-f", "/some/file"}) 25 | 26 | if err != nil { 27 | t.Error(err) 28 | t.Fail() 29 | } 30 | 31 | if cli.CommandStr != "output" { 32 | t.Error("Expected 'output' as command name") 33 | } 34 | 35 | if cli.Session != "session-name" { 36 | t.Error("Expected 'session-name' as session name") 37 | } 38 | 39 | if len(cli.NArgs) != 1 { 40 | t.Error("Expected all arguments to be parsed") 41 | } 42 | 43 | if cli.NArgs[0] != "-no-wait" { 44 | t.Error("Expected all arguments to be parsed") 45 | } 46 | 47 | if cli.Args[0] != "tail" || 48 | cli.Args[1] != "-f" || 49 | cli.Args[2] != "/some/file" { 50 | t.Error("Expected all args after command name") 51 | } 52 | } 53 | 54 | func TestFilterArgs(t *testing.T) { 55 | cli := NewCli() 56 | nargs, args := cli.parseOptions([]string{ 57 | "-my-arg", "%1", "%-2", "--other-arg", "4", "-3", "-last-arg", "command", "-cmdarg"}) 58 | 59 | if cli.Filter.String() != "1,4,-2,-3" { 60 | t.Error("Expected '1,4,-2,-3' as filter") 61 | } 62 | 63 | if nargs[0] != "-my-arg" { 64 | t.Error("NArgs don't start properly") 65 | } 66 | 67 | if nargs[1] != "-other-arg" { 68 | t.Error("NArgs not parsed in the middle") 69 | } 70 | 71 | if nargs[2] != "-last-arg" { 72 | t.Error("NArgs not parsed in last position") 73 | } 74 | 75 | if args[0] != "command" { 76 | t.Error("Args don't start properly") 77 | } 78 | 79 | if args[1] != "-cmdarg" { 80 | t.Error("Args don't finish properly") 81 | } 82 | } 83 | 84 | func TestArgsAndFilterAfter(t *testing.T) { 85 | cli := NewCli() 86 | nargs, args := cli.parseOptions([]string{"-my-arg", "%1"}) 87 | 88 | if cli.Filter.String() != "1" { 89 | t.Error("Expected '1' as filter") 90 | } 91 | 92 | if nargs[0] != "-my-arg" { 93 | t.Error("NArgs don't start properly") 94 | } 95 | 96 | if len(args) > 0 { 97 | t.Error("Args are not empty as expected") 98 | } 99 | } 100 | 101 | func TestArgsAndFilterBefore(t *testing.T) { 102 | cli := NewCli() 103 | nargs, args := cli.parseOptions([]string{"%1", "-my-arg"}) 104 | 105 | if cli.Filter.String() != "1" { 106 | t.Error("Expected '1' as filter") 107 | } 108 | 109 | if nargs[0] != "-my-arg" { 110 | t.Error("NArgs don't start properly") 111 | } 112 | 113 | if len(args) > 0 { 114 | t.Error("Args are not empty as expected") 115 | } 116 | } 117 | 118 | func TestCorrectClient(t *testing.T) { 119 | cli := NewCli() 120 | c := cli.getCommand() 121 | 122 | if c != nil { 123 | t.Error("Invalid client instance") 124 | } 125 | 126 | cli.Command = Output 127 | c = cli.getCommand() 128 | if _, ok := c.(*CommandOutput); !ok { 129 | t.Error("Invalid client instance") 130 | } 131 | 132 | cli.Command = Input 133 | c = cli.getCommand() 134 | if _, ok := c.(*CommandInput); !ok { 135 | t.Error("Invalid client instance") 136 | } 137 | 138 | cli.Command = IO 139 | c = cli.getCommand() 140 | if _, ok := c.(*CommandIO); !ok { 141 | t.Error("Invalid client instance") 142 | } 143 | 144 | cli.Command = Log 145 | c = cli.getCommand() 146 | if _, ok := c.(*CommandLog); !ok { 147 | t.Error("Invalid client instance") 148 | } 149 | 150 | cli.Command = Run 151 | c = cli.getCommand() 152 | if _, ok := c.(*CommandRun); !ok { 153 | t.Error("Invalid client instance") 154 | } 155 | 156 | cli.Command = Open 157 | c = cli.getCommand() 158 | if _, ok := c.(*CommandOpen); !ok { 159 | t.Error("Invalid client instance") 160 | } 161 | 162 | cli.Command = Close 163 | c = cli.getCommand() 164 | if _, ok := c.(*CommandClose); !ok { 165 | t.Error("Invalid client instance") 166 | } 167 | 168 | cli.Command = List 169 | c = cli.getCommand() 170 | if _, ok := c.(*CommandList); !ok { 171 | t.Error("Invalid client instance") 172 | } 173 | 174 | cli.Command = Stop 175 | c = cli.getCommand() 176 | if _, ok := c.(*CommandStop); !ok { 177 | t.Error("Invalid client instance") 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /agents/cmd.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "fmt" 5 | "os/exec" 6 | "strings" 7 | "sync" 8 | 9 | "github.com/dullgiulio/ringbuf" 10 | "github.com/dullgiulio/ringio/log" 11 | "github.com/dullgiulio/ringio/msg" 12 | ) 13 | 14 | type AgentCmd struct { 15 | cmd *exec.Cmd 16 | meta *AgentMetadata 17 | cancelInCh chan bool 18 | cancelOutCh chan bool 19 | } 20 | 21 | func NewAgentCmd(cmd []string, meta *AgentMetadata) *AgentCmd { 22 | a := &AgentCmd{ 23 | cmd: exec.Command(cmd[0], cmd[1:]...), 24 | meta: meta, 25 | cancelInCh: make(chan bool), 26 | cancelOutCh: make(chan bool), 27 | } 28 | a.cmd.Env = a.meta.Env 29 | return a 30 | } 31 | 32 | func (a *AgentCmd) Init() { 33 | if a.meta.Options == nil { 34 | a.meta.Options = &AgentOptions{} 35 | } 36 | } 37 | 38 | func (a *AgentCmd) Meta() *AgentMetadata { 39 | return a.meta 40 | } 41 | 42 | func (a *AgentCmd) Descr() AgentDescr { 43 | return AgentDescr{ 44 | Args: a.cmd.Args, 45 | Meta: *a.meta, 46 | Type: AgentTypeCmd, 47 | } 48 | } 49 | 50 | func (a *AgentCmd) String() string { 51 | return strings.Join(a.cmd.Args, " ") 52 | } 53 | 54 | func (a *AgentCmd) cancelReading() { 55 | a.cancelInCh <- true // Stop reading from stderr 56 | a.cancelInCh <- true // Stop reading from stdout 57 | } 58 | 59 | func (a *AgentCmd) Stop() error { 60 | // Stop data pipes to this process. 61 | if a.meta.Status.IsRunning() { 62 | if a.meta.Role == AgentRoleSink { 63 | a.cancelOutCh <- true // Stop writing to this agent 64 | 65 | // We don't cancel reading because a sink might still print 66 | // useful information, for example aggregated results. 67 | } else { 68 | // In all other cases, stop reading immediately. 69 | a.cancelReading() 70 | } 71 | } 72 | 73 | // Subprocess should exit normally now, a successive Wait() will finish it. 74 | 75 | log.Info(log.FacilityAgent, fmt.Sprintf("CmdAgent %d has been stopped", a.meta.ID)) 76 | return nil 77 | } 78 | 79 | func (a *AgentCmd) Kill() error { 80 | // If the agent is running, we need to stop all goroutines that 81 | // are reading from or writing to the agent. 82 | if a.meta.Status.IsRunning() { 83 | a.cancelReading() 84 | 85 | if a.meta.Role == AgentRoleSink { 86 | a.cancelOutCh <- true 87 | } 88 | } 89 | 90 | // An agent that was stopped only has a goroutine running. 91 | if a.meta.Status == AgentStatusStopped { 92 | if a.meta.Role == AgentRoleSink { 93 | a.cancelOutCh <- true 94 | } 95 | } 96 | 97 | if err := a.cmd.Process.Kill(); err != nil { 98 | return err 99 | } 100 | 101 | log.Info(log.FacilityAgent, fmt.Sprintf("CmdAgent %d has been killed", a.meta.ID)) 102 | return nil 103 | } 104 | 105 | func (a *AgentCmd) WaitFinish() error { 106 | return a.cmd.Wait() 107 | } 108 | 109 | func (a *AgentCmd) StartWrite() {} 110 | 111 | func (a *AgentCmd) InputToRingbuf(rErrors, rOutput *ringbuf.Ringbuf) { 112 | stdout, err := a.cmd.StdoutPipe() 113 | 114 | if err != nil { 115 | log.Error(log.FacilityAgent, err) 116 | return 117 | } 118 | 119 | stderr, err := a.cmd.StderrPipe() 120 | 121 | if err != nil { 122 | log.Error(log.FacilityAgent, err) 123 | return 124 | } 125 | 126 | if err := a.cmd.Start(); err != nil { 127 | log.Error(log.FacilityAgent, err) 128 | return 129 | } 130 | 131 | id := a.meta.ID 132 | 133 | wg := new(sync.WaitGroup) 134 | wg.Add(2) 135 | 136 | go writeToRingbuf(id, stderr, rErrors, a.cancelInCh, wg) 137 | go writeToRingbuf(id, stdout, rOutput, a.cancelInCh, wg) 138 | 139 | wg.Wait() 140 | 141 | // Close stdout to trigger a SIGPIPE on next write. 142 | stderr.Close() 143 | stdout.Close() 144 | 145 | close(a.cancelInCh) 146 | close(a.cancelOutCh) 147 | } 148 | 149 | func (a *AgentCmd) OutputFromRingbuf(rStdout, rErrors, rOutput *ringbuf.Ringbuf, filter *msg.Filter) { 150 | stdout, err := a.cmd.StdoutPipe() 151 | 152 | if err != nil { 153 | log.Error(log.FacilityAgent, err) 154 | return 155 | } 156 | 157 | stderr, err := a.cmd.StderrPipe() 158 | 159 | if err != nil { 160 | log.Error(log.FacilityAgent, err) 161 | return 162 | } 163 | 164 | stdin, err := a.cmd.StdinPipe() 165 | 166 | if err != nil { 167 | log.Error(log.FacilityAgent, err) 168 | return 169 | } 170 | 171 | if err := a.cmd.Start(); err != nil { 172 | log.Error(log.FacilityAgent, err) 173 | return 174 | } 175 | 176 | id := a.meta.ID 177 | 178 | wg := new(sync.WaitGroup) 179 | wg.Add(3) 180 | 181 | go writeToRingbuf(id, stdout, rStdout, a.cancelInCh, wg) 182 | go writeToRingbuf(id, stderr, rErrors, a.cancelInCh, wg) 183 | go readFromRingbuf(stdin, filter, a.meta.Options.getMask(), rOutput, makeReaderOptions(a.meta.Options), a.cancelOutCh, wg) 184 | 185 | wg.Wait() 186 | 187 | close(a.cancelInCh) 188 | close(a.cancelOutCh) 189 | } 190 | -------------------------------------------------------------------------------- /agents/agent.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | "github.com/dullgiulio/ringbuf" 8 | "github.com/dullgiulio/ringio/config" 9 | "github.com/dullgiulio/ringio/msg" 10 | ) 11 | 12 | type AgentMetadata struct { 13 | ID int 14 | Role AgentRole 15 | Status AgentStatus 16 | Started time.Time 17 | Finished time.Time 18 | User string 19 | Name string 20 | Env []string 21 | Filter *msg.Filter 22 | Options *AgentOptions 23 | } 24 | 25 | func NewAgentMetadata() *AgentMetadata { 26 | return &AgentMetadata{ 27 | Filter: new(msg.Filter), 28 | Options: new(AgentOptions), 29 | } 30 | } 31 | 32 | type AgentOptions struct { 33 | NoWait bool 34 | PrintMeta bool 35 | } 36 | 37 | func (ao *AgentOptions) getMask() msg.Format { 38 | if ao.PrintMeta { 39 | return msg.FORMAT_META | msg.FORMAT_NEWLINE 40 | } 41 | 42 | return msg.FORMAT_NEWLINE 43 | } 44 | 45 | type Agent interface { 46 | Init() 47 | // Gracefully stop an Agent. 48 | Stop() error 49 | // Forced stop of an Agent. Can be force onto stopped agents. 50 | Kill() error 51 | // Returns when the Agent is definitely finished and can be marked as such. 52 | WaitFinish() error 53 | String() string 54 | Meta() *AgentMetadata 55 | Descr() AgentDescr 56 | InputToRingbuf(errors, output *ringbuf.Ringbuf) 57 | OutputFromRingbuf(stdout, errors, output *ringbuf.Ringbuf, filter *msg.Filter) 58 | StartWrite() 59 | } 60 | 61 | func (ac *Collection) isFilteringSinkAgents(filter *msg.Filter) bool { 62 | if filter == nil { 63 | return false 64 | } 65 | 66 | fin := filter.GetIn() 67 | haveSinks := false 68 | 69 | if len(fin) == 0 { 70 | return false 71 | } 72 | 73 | for _, a := range ac.agents { 74 | meta := a.Meta() 75 | 76 | if meta.Role == AgentRoleSink { 77 | for _, id := range fin { 78 | if meta.ID == id { 79 | return true 80 | } 81 | } 82 | 83 | haveSinks = true 84 | } 85 | } 86 | 87 | if haveSinks { 88 | return false 89 | } 90 | 91 | return true 92 | } 93 | 94 | func (ac *Collection) validateFilter(self int, filter *msg.Filter) error { 95 | if filter == nil { 96 | return nil 97 | } 98 | 99 | fin := filter.GetIn() 100 | fout := filter.GetOut() 101 | 102 | agentIDs := make(map[int]struct{}) 103 | 104 | for i := 0; i < len(ac.agents); i++ { 105 | a := ac.agents[i] 106 | meta := a.Meta() 107 | 108 | agentIDs[meta.ID] = struct{}{} 109 | } 110 | 111 | for _, id := range fin { 112 | if _, ok := agentIDs[id]; !ok { 113 | return fmt.Errorf("%%%d is not a valid agent", id) 114 | } 115 | 116 | if id == self { 117 | return fmt.Errorf("Cannot filter in the agent itself") 118 | } 119 | } 120 | 121 | for _, id := range fout { 122 | if _, ok := agentIDs[id]; !ok { 123 | return fmt.Errorf("%%%d is not a valid agent", id) 124 | } 125 | 126 | if id == self { 127 | return fmt.Errorf("Cannot filter out the agent itself") 128 | } 129 | } 130 | 131 | return nil 132 | } 133 | 134 | func (ac *Collection) inputToRingbuf(a Agent) { 135 | a.InputToRingbuf(ac.errors, ac.output) 136 | ac.waitFinish(a) 137 | } 138 | 139 | func (ac *Collection) outputFromRingbuf(a Agent, filter *msg.Filter, filtersSinks bool) { 140 | if filtersSinks { 141 | a.OutputFromRingbuf(ac.stdout, ac.errors, ac.stdout, filter) 142 | } else { 143 | a.OutputFromRingbuf(ac.stdout, ac.errors, ac.output, filter) 144 | } 145 | 146 | // XXX: This may cause Wait() to be called twice. We get an error, but 147 | // does no harm. 148 | ac.waitFinish(a) 149 | } 150 | 151 | func (ac *Collection) errorsFromRingbuf(a Agent, filter *msg.Filter) { 152 | // We both read and write on errors. 153 | a.OutputFromRingbuf(ac.errors, ac.errors, ac.errors, filter) 154 | 155 | // XXX: This may cause Wait() to be called twice. We get an error, but 156 | // does no harm. 157 | ac.waitFinish(a) 158 | } 159 | 160 | func (ac *Collection) logFromRingbuf(a Agent) { 161 | logring := config.GetLogRingbuf() 162 | 163 | a.OutputFromRingbuf(logring, logring, logring, nil) 164 | 165 | // XXX: This may cause Wait() to be called twice. We get an error, but 166 | // does no harm. 167 | ac.waitFinish(a) 168 | } 169 | 170 | func (ac *Collection) runAgent(a Agent) error { 171 | meta := a.Meta() 172 | 173 | // Notice that this validation doesn't make safe access to ac: 174 | // it is safe to call it here, but not an a go routine. 175 | if err := ac.validateFilter(meta.ID, meta.Filter); err != nil { 176 | return err 177 | } 178 | 179 | filtersSinks := ac.isFilteringSinkAgents(meta.Filter) 180 | 181 | meta.Started = time.Now() 182 | meta.Status = AgentStatusRunning 183 | 184 | go func(ac *Collection, a Agent) { 185 | switch meta.Role { 186 | case AgentRoleSource: 187 | ac.inputToRingbuf(a) 188 | case AgentRoleErrors: 189 | ac.errorsFromRingbuf(a, meta.Filter) 190 | case AgentRoleSink: 191 | ac.outputFromRingbuf(a, meta.Filter, filtersSinks) 192 | case AgentRoleLog: 193 | ac.logFromRingbuf(a) 194 | } 195 | }(ac, a) 196 | 197 | return nil 198 | } 199 | -------------------------------------------------------------------------------- /server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "errors" 5 | "net" 6 | "net/rpc" 7 | "os" 8 | "sync" 9 | 10 | "github.com/dullgiulio/ringbuf" 11 | "github.com/dullgiulio/ringio/agents" 12 | "github.com/dullgiulio/ringio/config" 13 | "github.com/dullgiulio/ringio/log" 14 | "github.com/dullgiulio/ringio/onexit" 15 | "github.com/dullgiulio/ringio/utils" 16 | ) 17 | 18 | type RPCServer struct { 19 | ac *agents.Collection 20 | resp agents.AgentMessageResponseBool 21 | socket string 22 | over bool 23 | mux *sync.Mutex 24 | } 25 | 26 | func NewRPCServer(socket string, autorun bool) *RPCServer { 27 | ac := agents.NewCollection() 28 | s := &RPCServer{ 29 | socket: socket, 30 | ac: ac, 31 | resp: agents.NewAgentMessageResponseBool(), 32 | mux: &sync.Mutex{}, 33 | } 34 | 35 | go ac.Run(autorun) 36 | 37 | return s 38 | } 39 | 40 | type RPCReq struct { 41 | Agent *agents.AgentDescr 42 | } 43 | 44 | type RPCResp bool 45 | 46 | var errSessionOver = errors.New("Session has already terminated") 47 | 48 | func Init() { 49 | if config.C.PrintLog { 50 | nw := log.NewNewlineWriter(os.Stderr) 51 | log.AddWriter(nw) 52 | } 53 | 54 | lw := ringbuf.NewBytes(config.GetLogRingbuf()) 55 | log.AddWriter(lw) 56 | } 57 | 58 | func Run(logLevel log.Level, sessionName string) (returnStatus int) { 59 | socket := utils.FileInDotpath(sessionName) 60 | s := NewRPCServer(socket, config.C.AutoRun) 61 | 62 | // Serve RPC calls. 63 | go s.serve() 64 | 65 | // Print all logged information. 66 | if !log.Run(logLevel) { 67 | returnStatus = 1 68 | } 69 | 70 | s.cleanup() 71 | 72 | return 73 | } 74 | 75 | func (s *RPCServer) Ping(req *RPCReq, result *int) error { 76 | *result = 1 77 | 78 | // XXX: It would be nice to verify that the agents routine is actually running. 79 | 80 | return nil 81 | } 82 | 83 | func (s *RPCServer) Add(req *RPCReq, result *int) error { 84 | s.mux.Lock() 85 | defer s.mux.Unlock() 86 | 87 | var agent agents.Agent 88 | 89 | if s.over { 90 | return errSessionOver 91 | } 92 | 93 | ag := req.Agent 94 | resp := agents.NewAgentMessageResponseInt() 95 | 96 | switch ag.Type { 97 | case agents.AgentTypePipe: 98 | agent = agents.NewAgentPipe(ag.Args[0], &ag.Meta) 99 | case agents.AgentTypeCmd: 100 | agent = agents.NewAgentCmd(ag.Args, &ag.Meta) 101 | case agents.AgentTypeNull: 102 | agent = agents.NewAgentNull(0, &ag.Meta) 103 | } 104 | 105 | s.ac.Add(agent, resp) 106 | 107 | i, err := resp.Get() 108 | if err != nil { 109 | return err 110 | } 111 | 112 | *result = i.(int) 113 | 114 | if config.C.AutoRun || ag.Type == agents.AgentTypePipe { 115 | s.ac.Start(agent, &s.resp) 116 | 117 | if _, err := s.resp.Get(); err != nil { 118 | return err 119 | } 120 | } 121 | 122 | return nil 123 | } 124 | 125 | func (s *RPCServer) List(req *RPCReq, result *agents.AgentMessageResponseList) error { 126 | s.mux.Lock() 127 | defer s.mux.Unlock() 128 | 129 | if s.over { 130 | return errSessionOver 131 | } 132 | 133 | *result = agents.NewAgentMessageResponseList() 134 | 135 | s.ac.List(result) 136 | result.Get() 137 | 138 | return result.Error 139 | } 140 | 141 | func (s *RPCServer) Run(req *RPCReq, result *RPCResp) error { 142 | s.mux.Lock() 143 | defer s.mux.Unlock() 144 | 145 | if s.over { 146 | return errSessionOver 147 | } 148 | 149 | *result = true 150 | 151 | s.ac.StartAll(&s.resp) 152 | s.resp.Get() 153 | 154 | return nil 155 | } 156 | 157 | func (s *RPCServer) StartAll(req *RPCReq, result *RPCResp) error { 158 | s.mux.Lock() 159 | defer s.mux.Unlock() 160 | 161 | if s.over { 162 | return errSessionOver 163 | } 164 | 165 | s.ac.StartAll(&s.resp) 166 | s.resp.Get() 167 | 168 | *result = true 169 | return nil 170 | } 171 | 172 | func (s *RPCServer) Start(id int, result *RPCResp) error { 173 | s.mux.Lock() 174 | defer s.mux.Unlock() 175 | 176 | if s.over { 177 | return errSessionOver 178 | } 179 | 180 | na := agents.NewAgentNull(id, agents.NewAgentMetadata()) // Role is unimportant here. 181 | s.ac.Start(na, &s.resp) 182 | s.resp.Get() 183 | 184 | *result = true 185 | return nil 186 | } 187 | 188 | func (s *RPCServer) WriteReady(id int, result *RPCResp) error { 189 | s.mux.Lock() 190 | defer s.mux.Unlock() 191 | 192 | na := agents.NewAgentNull(id, agents.NewAgentMetadata()) // Role is unimportant here. 193 | s.ac.WriteReady(na, &s.resp) 194 | s.resp.Get() 195 | 196 | *result = true 197 | return nil 198 | } 199 | 200 | func (s *RPCServer) Stop(id int, result *RPCResp) error { 201 | s.mux.Lock() 202 | defer s.mux.Unlock() 203 | 204 | if s.over { 205 | return errSessionOver 206 | } 207 | 208 | na := agents.NewAgentNull(id, agents.NewAgentMetadata()) // Role is unimportant here. 209 | s.ac.Stop(na, &s.resp) 210 | s.resp.Get() 211 | 212 | *result = true 213 | return nil 214 | } 215 | 216 | func (s *RPCServer) Kill(id int, result *RPCResp) error { 217 | s.mux.Lock() 218 | defer s.mux.Unlock() 219 | 220 | if s.over { 221 | return errSessionOver 222 | } 223 | 224 | na := agents.NewAgentNull(id, agents.NewAgentMetadata()) // Role is unimportant here. 225 | s.ac.Kill(na, &s.resp) 226 | s.resp.Get() 227 | 228 | *result = true 229 | return nil 230 | } 231 | 232 | func (s *RPCServer) Close(req *RPCReq, result *RPCResp) error { 233 | s.mux.Lock() 234 | defer s.mux.Unlock() 235 | 236 | if s.over { 237 | return errSessionOver 238 | } 239 | 240 | // This session has terminated. 241 | s.over = true 242 | 243 | *result = true 244 | 245 | s.ac.Cancel(&s.resp) 246 | s.resp.Get() 247 | 248 | log.Cancel() 249 | 250 | onexit.PendingExit(0) 251 | 252 | return nil 253 | } 254 | 255 | func (s *RPCServer) cleanup() { 256 | s.mux.Lock() 257 | defer s.mux.Unlock() 258 | 259 | // This session has terminated. 260 | s.over = true 261 | os.Remove(s.socket) 262 | } 263 | 264 | func (s *RPCServer) serve() { 265 | l, err := net.Listen("unix", s.socket) 266 | if err != nil { 267 | utils.Fatal(err) 268 | } 269 | 270 | onexit.Defer(s.cleanup) 271 | 272 | srv := rpc.NewServer() 273 | if err = srv.Register(s); err != nil { 274 | log.Fatal(log.FacilityDefault, err) 275 | onexit.PendingExit(1) 276 | return 277 | } 278 | 279 | srv.Accept(l) 280 | } 281 | -------------------------------------------------------------------------------- /client/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "net/rpc" 7 | "os" 8 | "strconv" 9 | 10 | "github.com/dullgiulio/ringio/msg" 11 | "github.com/dullgiulio/ringio/onexit" 12 | "github.com/dullgiulio/ringio/utils" 13 | ) 14 | 15 | type Command int 16 | 17 | const ( 18 | None Command = iota 19 | Output 20 | Input 21 | IO 22 | Error 23 | Log 24 | Run 25 | Open 26 | Close 27 | List 28 | Start 29 | Stop 30 | Kill 31 | Ping 32 | listSessions 33 | help 34 | ) 35 | 36 | type Client interface { 37 | Help() string 38 | Init(fs *flag.FlagSet) bool 39 | Run(cli *Cli) error 40 | } 41 | 42 | type Cli struct { 43 | NArgs []string 44 | Args []string 45 | Session string 46 | Command Command 47 | CommandStr string 48 | Filter *msg.Filter 49 | argsLen int 50 | flagset *flag.FlagSet 51 | client Client 52 | haveArgs bool 53 | } 54 | 55 | var cmdCommand = map[string]Command{ 56 | "input": Input, 57 | "in": Input, 58 | "output": Output, 59 | "out": Output, 60 | "io": IO, 61 | "errors": Error, 62 | "error": Error, 63 | "err": Error, 64 | "log": Log, 65 | "run": Run, 66 | "open": Open, 67 | "close": Close, 68 | "list": List, 69 | "ls": List, 70 | "start": Start, 71 | "stop": Stop, 72 | "kill": Kill, 73 | "ping": Ping, 74 | } 75 | 76 | func NewCli() *Cli { 77 | return &Cli{} 78 | } 79 | 80 | func (cli *Cli) ParseArgs(args []string) error { 81 | if len(args) <= 1 { 82 | cli.Help() 83 | } else { 84 | cli.Session = args[1] 85 | } 86 | 87 | switch cli.Session { 88 | case "help": 89 | if err := cli.initCommandArgs(args); err != nil { 90 | return err 91 | } 92 | cli.commandHelp() 93 | case "-ls": 94 | cli.listSessions() 95 | } 96 | 97 | if err := cli.initCommandArgs(args); err != nil { 98 | return err 99 | } 100 | 101 | cli.NArgs, cli.Args = cli.parseOptions(cli.NArgs) 102 | if err := cli.flagset.Parse(cli.NArgs); err != nil { 103 | return err 104 | } 105 | 106 | return nil 107 | } 108 | 109 | func (cli *Cli) listSessions() { 110 | sessions := getAllSessions() 111 | 112 | if len(sessions) == 0 { 113 | fmt.Printf("No sessions open. Please run 'ringio open &' to start a session\n") 114 | onexit.Exit(0) 115 | } 116 | 117 | fmt.Printf("Available open sessions:\n\n") 118 | printList(sessions) 119 | 120 | fmt.Printf("\nPlease use 'ringio ping' to verify a session is active.\n") 121 | onexit.Exit(1) 122 | } 123 | 124 | func (cli *Cli) initCommandArgs(args []string) error { 125 | errNoCommand := fmt.Errorf("No command specified") 126 | 127 | cli.argsLen = len(args) - 3 128 | if cli.argsLen < 0 { 129 | return errNoCommand 130 | } 131 | 132 | if args[2] == "" { 133 | return errNoCommand 134 | } 135 | 136 | cli.CommandStr = args[2] 137 | 138 | if command, ok := cmdCommand[cli.CommandStr]; !ok { 139 | return fmt.Errorf("Unsupported command %s", cli.CommandStr) 140 | } else { 141 | cli.Command = command 142 | } 143 | 144 | cli.CommandStr = args[2] 145 | cli.NArgs = args[3:] 146 | 147 | cli.flagset = flag.NewFlagSet("`ringio "+cli.CommandStr+"'", flag.ExitOnError) 148 | 149 | if cli.client = cli.getCommand(); cli.client == nil { 150 | return fmt.Errorf("Unsupported command %s", cli.CommandStr) 151 | } 152 | 153 | cli.haveArgs = cli.client.Init(cli.flagset) 154 | return nil 155 | } 156 | 157 | func (cli *Cli) Help() { 158 | fmt.Print( 159 | `Usage: ringio open & 160 | ringio in|input [%job...] [-%job...] [COMMAND...] 161 | ringio out|output [%job...] [-%job...] [COMMAND...] 162 | ringio io [%job...] [-%job...] 163 | ringio run 164 | ringio list 165 | ringio start %job... 166 | ringio stop %job... 167 | ringio kill %job... 168 | ringio log 169 | ringio close 170 | 171 | Type 'ringio help ' for help on any command. 172 | `) 173 | onexit.Exit(1) 174 | } 175 | 176 | func (cli *Cli) commandHelp() { 177 | fmt.Fprintf(os.Stderr, "ringio: %s: ", cli.CommandStr) 178 | fmt.Fprintf(os.Stderr, cli.client.Help()) 179 | fmt.Fprint(os.Stderr, "\n") 180 | 181 | if cli.haveArgs { 182 | fmt.Fprintf(os.Stderr, "\nSupported options:\n\n") 183 | cli.flagset.PrintDefaults() 184 | } 185 | 186 | fmt.Fprintf(os.Stderr, "\nRun `ringio' without argument to see basic usage information.\n") 187 | onexit.Exit(1) 188 | } 189 | 190 | // XXX: "-option arg" is not accepted, but only "-option=arg". 191 | func (cli *Cli) parseOptions(args []string) ([]string, []string) { 192 | var nargs []string 193 | 194 | for i := range args { 195 | arg := args[i] 196 | 197 | if args[i][0] == '%' { 198 | arg = args[i][1:] 199 | } 200 | 201 | if d, err := strconv.Atoi(arg); err == nil { 202 | if cli.Filter == nil { 203 | cli.Filter = msg.NewFilter() 204 | } 205 | 206 | if d < 0 { 207 | cli.Filter.Out(-d) 208 | } else { 209 | cli.Filter.In(d) 210 | } 211 | } else if args[i][0] == '-' { 212 | // It's a command argument 213 | arg := args[i] 214 | 215 | // Make --gnu-style args into -g-args 216 | if args[i][1] == '-' { 217 | arg = args[i][1:] 218 | } 219 | 220 | nargs = append(nargs, arg) 221 | } else { 222 | return nargs, args[i:] 223 | } 224 | } 225 | 226 | return nargs, []string{} 227 | } 228 | 229 | func (cli *Cli) getCommand() Client { 230 | if cli.Command == None { 231 | return nil 232 | } 233 | 234 | switch cli.Command { 235 | case Output: 236 | return NewCommandOutput() 237 | case Input: 238 | return NewCommandInput() 239 | case IO: 240 | return NewCommandIO() 241 | case Error: 242 | return NewCommandError() 243 | case Log: 244 | return NewCommandLog() 245 | case Run: 246 | return NewCommandRun() 247 | case Ping: 248 | return NewCommandPing() 249 | case Open: 250 | return NewCommandOpen() 251 | case Close: 252 | return NewCommandClose() 253 | case List: 254 | return NewCommandList() 255 | case Start: 256 | return NewCommandStart() 257 | case Stop: 258 | return NewCommandStop() 259 | case Kill: 260 | return NewCommandKill() 261 | } 262 | 263 | return nil 264 | } 265 | 266 | func (cli *Cli) Run() { 267 | if err := cli.client.Run(cli); err != nil { 268 | utils.Fatal(err) 269 | } 270 | } 271 | 272 | func (cli *Cli) GetClient() *rpc.Client { 273 | client, err := rpc.Dial("unix", utils.FileInDotpath(cli.Session)) 274 | if err != nil { 275 | utils.Fatal(err) 276 | } 277 | 278 | return client 279 | } 280 | -------------------------------------------------------------------------------- /agents/collection.go: -------------------------------------------------------------------------------- 1 | package agents 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | "time" 7 | 8 | "github.com/dullgiulio/ringbuf" 9 | "github.com/dullgiulio/ringio/config" 10 | "github.com/dullgiulio/ringio/log" 11 | ) 12 | 13 | type messageType int 14 | 15 | const ( 16 | agentMessageAdd messageType = iota 17 | agentMessageStart 18 | agentMessageStop 19 | agentMessageKill 20 | agentMessageFinished 21 | agentMessageList 22 | agentMessageStartAll 23 | agentMessageCancel 24 | agentMessageWriteReady 25 | ) 26 | 27 | type agentMessage struct { 28 | status messageType 29 | response AgentMessageResponse 30 | // This might not be a real agent but just a container for the metadata. 31 | agent Agent 32 | } 33 | 34 | func newAgentMessage(status messageType, response AgentMessageResponse, agent Agent) agentMessage { 35 | return agentMessage{status: status, response: response, agent: agent} 36 | } 37 | 38 | type Collection struct { 39 | agents []Agent 40 | requestCh chan agentMessage 41 | output *ringbuf.Ringbuf 42 | errors *ringbuf.Ringbuf 43 | stdout *ringbuf.Ringbuf 44 | } 45 | 46 | func NewCollection() *Collection { 47 | return &Collection{ 48 | agents: make([]Agent, 0), 49 | requestCh: make(chan agentMessage), 50 | // Output from source agents. 51 | output: ringbuf.NewRingbuf(config.C.RingbufSize), 52 | // All errors. 53 | errors: ringbuf.NewRingbuf(config.C.RingbufSize), 54 | // Output from sink agents. 55 | stdout: ringbuf.NewRingbuf(config.C.RingbufSize), 56 | } 57 | } 58 | 59 | func (c *Collection) Cancel(response AgentMessageResponse) { 60 | c.requestCh <- newAgentMessage(agentMessageCancel, response, nil) 61 | } 62 | 63 | func (c *Collection) Add(a Agent, response AgentMessageResponse) { 64 | c.requestCh <- newAgentMessage(agentMessageAdd, response, a) 65 | } 66 | 67 | func (c *Collection) Start(a Agent, response AgentMessageResponse) { 68 | c.requestCh <- newAgentMessage(agentMessageStart, response, a) 69 | } 70 | 71 | func (c *Collection) StartAll(response AgentMessageResponse) { 72 | c.requestCh <- newAgentMessage(agentMessageStartAll, response, nil) 73 | } 74 | 75 | func (c *Collection) Finished(a Agent, response AgentMessageResponse) { 76 | c.requestCh <- newAgentMessage(agentMessageFinished, response, a) 77 | } 78 | 79 | func (c *Collection) Kill(a Agent, response AgentMessageResponse) { 80 | c.requestCh <- newAgentMessage(agentMessageKill, response, a) 81 | } 82 | 83 | func (c *Collection) Stop(a Agent, response AgentMessageResponse) { 84 | c.requestCh <- newAgentMessage(agentMessageStop, response, a) 85 | } 86 | 87 | func (c *Collection) List(response AgentMessageResponse) { 88 | c.requestCh <- newAgentMessage(agentMessageList, response, nil) 89 | } 90 | 91 | func (c *Collection) WriteReady(a Agent, response AgentMessageResponse) { 92 | c.requestCh <- newAgentMessage(agentMessageWriteReady, response, a) 93 | } 94 | 95 | func (c *Collection) add(newAgent Agent) int { 96 | maxID := 0 97 | 98 | for _, a := range c.agents { 99 | meta := a.Meta() 100 | 101 | if id := meta.ID; id > maxID { 102 | maxID = id 103 | } 104 | } 105 | 106 | maxID++ 107 | 108 | meta := newAgent.Meta() 109 | meta.ID = maxID 110 | 111 | c.agents = append(c.agents, newAgent) 112 | return maxID 113 | } 114 | 115 | // Implement a sort.Interface that sorts by (role,started date) and prints. 116 | func (c *Collection) Len() int { 117 | l := len(c.agents) 118 | return l 119 | } 120 | 121 | func (c *Collection) Less(i, j int) bool { 122 | metaI := c.agents[i].Meta() 123 | metaJ := c.agents[j].Meta() 124 | 125 | if metaI.Status < metaJ.Status { 126 | return true 127 | } 128 | 129 | if metaI.Status == metaJ.Status && 130 | metaI.ID < metaJ.ID { 131 | return true 132 | } 133 | 134 | return false 135 | } 136 | 137 | func (c *Collection) Swap(i, j int) { 138 | c.agents[i], c.agents[j] = c.agents[j], c.agents[i] 139 | } 140 | 141 | func (c *Collection) getRealAgent(agent Agent) Agent { 142 | var realAgent Agent 143 | 144 | meta := agent.Meta() 145 | 146 | for _, a := range c.agents { 147 | if a == agent || a.Meta().ID == meta.ID { 148 | realAgent = a 149 | break 150 | } 151 | } 152 | 153 | return realAgent 154 | } 155 | 156 | func (c *Collection) waitFinish(agent Agent) { 157 | if err := agent.WaitFinish(); err != nil { 158 | // Something went wrong, this agent is not finished. 159 | log.Error(log.FacilityAgent, fmt.Sprintf("Error waiting for %d: %s", agent.Meta().ID, err)) 160 | } 161 | 162 | // We signal back to the collection that this agents is finished. 163 | resp := NewAgentMessageResponseBool() 164 | c.Finished(agent, &resp) 165 | 166 | if _, err := resp.Get(); err != nil { 167 | log.Error(log.FacilityAgent, fmt.Sprintf("Error cleaning up %d: %s", agent.Meta().ID, err)) 168 | } 169 | } 170 | 171 | func (c *Collection) stopOrKillAgent(msg agentMessage, kill bool) { 172 | realAgent := c.getRealAgent(msg.agent) 173 | 174 | if realAgent == nil { 175 | msg.response.Err(fmt.Errorf("Agent %d not found", msg.agent.Meta().ID)) 176 | return 177 | } 178 | 179 | meta := realAgent.Meta() 180 | isRunning := meta.Status.IsRunning() 181 | 182 | if !kill { 183 | // Cannot stop an Agent that is not in running state. 184 | if !isRunning { 185 | msg.response.Err(fmt.Errorf("Agent %d is not marked as running", meta.ID)) 186 | return 187 | } 188 | 189 | if err := realAgent.Stop(); err != nil { 190 | log.Error(log.FacilityAgent, err) 191 | msg.response.Err(err) 192 | return 193 | } 194 | 195 | meta.Status = AgentStatusStopped 196 | } else { 197 | if err := realAgent.Kill(); err != nil { 198 | log.Error(log.FacilityAgent, err) 199 | msg.response.Err(err) 200 | return 201 | } 202 | 203 | meta.Status = AgentStatusKilled 204 | } 205 | 206 | msg.response.Ok() 207 | 208 | // Only if it was running before it was stopped, we need 209 | // to wait for the Agent to clean up. If it wasn't running, 210 | // we must have a routine already waiting for it. 211 | if isRunning { 212 | go c.waitFinish(realAgent) 213 | } 214 | } 215 | 216 | func (c *Collection) startAgent(agent Agent) error { 217 | meta := agent.Meta() 218 | 219 | switch meta.Status { 220 | case AgentStatusKilled, AgentStatusStopped, AgentStatusRunning: 221 | return fmt.Errorf("Agent %d is already running", meta.ID) 222 | } 223 | 224 | agent.Init() 225 | 226 | if err := c.runAgent(agent); err != nil { 227 | return err 228 | } 229 | 230 | log.Info(log.FacilityAgent, fmt.Sprintf("Started agent %d", meta.ID)) 231 | return nil 232 | } 233 | 234 | func (c *Collection) Run(autorun bool) { 235 | go c.errors.Run() 236 | go c.output.Run() 237 | go c.stdout.Run() 238 | 239 | for msg := range c.requestCh { 240 | switch msg.status { 241 | case agentMessageAdd: 242 | id := c.add(msg.agent) 243 | 244 | log.Info(log.FacilityAgent, "Added new agent", msg.agent) 245 | 246 | msg.response.Data(id) 247 | msg.response.Ok() 248 | case agentMessageKill: 249 | c.stopOrKillAgent(msg, true) 250 | case agentMessageStop: 251 | c.stopOrKillAgent(msg, false) 252 | case agentMessageStart: 253 | realAgent := c.getRealAgent(msg.agent) 254 | 255 | if realAgent == nil { 256 | err := fmt.Errorf("Agent %d not found", msg.agent.Meta().ID) 257 | log.Error(log.FacilityAgent, err) 258 | msg.response.Err(err) 259 | continue 260 | } 261 | 262 | if err := c.startAgent(realAgent); err != nil { 263 | log.Error(log.FacilityAgent, err) 264 | msg.response.Err(err) 265 | } else { 266 | msg.response.Ok() 267 | } 268 | case agentMessageCancel: 269 | // Kill all outstanding processes. 270 | for _, a := range c.agents { 271 | meta := a.Meta() 272 | 273 | if !meta.Status.IsRunning() { 274 | continue 275 | } 276 | 277 | // Kill running agents. 278 | if err := a.Kill(); err != nil { 279 | log.Error(log.FacilityAgent, err) 280 | } else { 281 | meta.Status = AgentStatusKilled 282 | 283 | if err := a.WaitFinish(); err != nil { 284 | log.Error(log.FacilityAgent, err) 285 | } 286 | 287 | meta.Finished = time.Now() 288 | meta.Status = AgentStatusFinished 289 | } 290 | } 291 | 292 | c.Close() 293 | msg.response.Ok() 294 | return 295 | case agentMessageStartAll: 296 | // Start all agents that haven't been started yet. 297 | for _, a := range c.agents { 298 | meta := a.Meta() 299 | 300 | if meta.Status != AgentStatusNone { 301 | continue 302 | } 303 | 304 | if err := c.runAgent(a); err != nil { 305 | log.Error(log.FacilityAgent, err.Error()) 306 | } 307 | } 308 | 309 | log.Info(log.FacilityAgent, "Starting all agents") 310 | 311 | msg.response.Ok() 312 | case agentMessageFinished: 313 | meta := msg.agent.Meta() 314 | meta.Finished = time.Now() 315 | meta.Status = AgentStatusFinished 316 | 317 | log.Info(log.FacilityAgent, "Agent", msg.agent, "finished") 318 | 319 | msg.response.Ok() 320 | case agentMessageList: 321 | var agents []AgentDescr 322 | 323 | sort.Sort(c) 324 | 325 | for _, a := range c.agents { 326 | agents = append(agents, a.Descr()) 327 | } 328 | 329 | msg.response.Data(&agents) 330 | msg.response.Ok() 331 | case agentMessageWriteReady: 332 | realAgent := c.getRealAgent(msg.agent) 333 | realAgent.StartWrite() 334 | msg.response.Ok() 335 | } 336 | } 337 | } 338 | 339 | func (c *Collection) Close() { 340 | c.output.Cancel() 341 | c.errors.Cancel() 342 | c.stdout.Cancel() 343 | } 344 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------