daemon.go 3.96 KB
Newer Older
1 2 3 4
package daemon

import (
	"encoding/json"
5
	"fmt"
6 7
	"io"
	"os"
8
	"path"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	"sync"
10

11
	core "github.com/jbenet/go-ipfs/core"
12
	"github.com/jbenet/go-ipfs/core/commands"
13
	u "github.com/jbenet/go-ipfs/util"
14

15
	lock "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
16
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
17
	manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
18 19
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
var log = u.Logger("daemon")
21

22 23 24
// LockFile is the filename of the daemon lock, relative to config dir
const LockFile = "daemon.lock"

25 26
// DaemonListener listens to an initialized IPFS node and can send it commands instead of
// starting up a new set of connections
27
type DaemonListener struct {
28
	node   *core.IpfsNode
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
	list   manet.Listener
30
	closed bool
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	wg     sync.WaitGroup
32
	lk     io.Closer
33 34
}

Siraj Ravel's avatar
Siraj Ravel committed
35
//Command accepts user input and can be sent to the running IPFS node
Siraj Ravel's avatar
Siraj Ravel committed
36 37 38 39 40 41
type Command struct {
	Command string
	Args    []string
	Opts    map[string]interface{}
}

42
func NewDaemonListener(ipfsnode *core.IpfsNode, addr ma.Multiaddr, confdir string) (*DaemonListener, error) {
43 44 45 46 47 48
	var err error
	confdir, err = u.TildeExpansion(confdir)
	if err != nil {
		return nil, err
	}

49
	lk, err := daemonLock(confdir)
50 51 52 53 54 55
	if err != nil {
		return nil, err
	}

	ofi, err := os.Create(confdir + "/rpcaddress")
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56
		log.Warningf("Could not create rpcaddress file: %s", err)
57 58 59
		return nil, err
	}

60
	_, err = ofi.Write([]byte(addr.String()))
61
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
		log.Warningf("Could not write to rpcaddress file: %s", err)
63 64 65 66
		return nil, err
	}
	ofi.Close()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
	list, err := manet.Listen(addr)
68 69 70
	if err != nil {
		return nil, err
	}
71
	log.Info("New daemon listener initialized.")
72 73

	return &DaemonListener{
Siraj Ravel's avatar
Siraj Ravel committed
74
		node: ipfsnode,
75
		list: list,
76
		lk:   lk,
77 78 79
	}, nil
}

80 81 82 83
func NewCommand() *Command {
	return &Command{
		Opts: make(map[string]interface{}),
	}
84 85 86
}

func (dl *DaemonListener) Listen() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89 90
	if dl.closed {
		panic("attempting to listen on a closed daemon Listener")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	// add ourselves to workgroup. and remove ourselves when done.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
	dl.wg.Add(1)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94
	defer dl.wg.Done()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95
	log.Info("daemon listening")
96
	for {
Siraj Ravel's avatar
Siraj Ravel committed
97
		conn, err := dl.list.Accept()
98 99
		if err != nil {
			if !dl.closed {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100
				log.Warning("DaemonListener Accept: %v", err)
101 102 103
			}
			return
		}
Siraj Ravel's avatar
Siraj Ravel committed
104
		go dl.handleConnection(conn)
105 106 107
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
func (dl *DaemonListener) handleConnection(conn manet.Conn) {
Siraj Ravel's avatar
Siraj Ravel committed
109
	defer conn.Close()
110

Siraj Ravel's avatar
Siraj Ravel committed
111
	dec := json.NewDecoder(conn)
112

Siraj Ravel's avatar
Siraj Ravel committed
113 114
	var command Command
	err := dec.Decode(&command)
115
	if err != nil {
Siraj Ravel's avatar
Siraj Ravel committed
116
		fmt.Fprintln(conn, err)
117 118
		return
	}
119

120
	log.Debugf("Got command: %v", command)
121
	switch command.Command {
122
	case "add":
123
		err = commands.Add(dl.node, command.Args, command.Opts, conn)
124
	case "cat":
125
		err = commands.Cat(dl.node, command.Args, command.Opts, conn)
126
	case "ls":
127
		err = commands.Ls(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
128
	case "pin":
129
		err = commands.Pin(dl.node, command.Args, command.Opts, conn)
130 131
	case "publish":
		err = commands.Publish(dl.node, command.Args, command.Opts, conn)
132 133
	case "resolve":
		err = commands.Resolve(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
134
	case "diag":
Jeromy's avatar
Jeromy committed
135
		err = commands.Diag(dl.node, command.Args, command.Opts, conn)
136 137 138 139
	case "blockGet":
		err = commands.BlockGet(dl.node, command.Args, command.Opts, conn)
	case "blockPut":
		err = commands.BlockPut(dl.node, command.Args, command.Opts, conn)
Henry's avatar
Henry committed
140 141
	case "log":
		err = commands.Log(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
142 143
	case "unpin":
		err = commands.Unpin(dl.node, command.Args, command.Opts, conn)
144
	case "updateApply":
145
		command.Opts["onDaemon"] = true
146
		err = commands.UpdateApply(dl.node, command.Args, command.Opts, conn)
147
	default:
148 149 150
		err = fmt.Errorf("Invalid Command: '%s'", command.Command)
	}
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151
		log.Errorf("%s: %s", command.Command, err)
152
		fmt.Fprintln(conn, err)
153 154 155 156 157
	}
}

func (dl *DaemonListener) Close() error {
	dl.closed = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158 159 160 161
	err := dl.list.Close()
	dl.wg.Wait() // wait till done before releasing lock.
	dl.lk.Close()
	return err
162
}
163 164 165 166

func daemonLock(confdir string) (io.Closer, error) {
	return lock.Lock(path.Join(confdir, LockFile))
}