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

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

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

16
	lock "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
17
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
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 29 30
	node   *core.IpfsNode
	list   net.Listener
	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
	if err != nil {
		return nil, err
	}

54
	network, host, err := ma.DialArgs(addr)
55 56 57 58
	if err != nil {
		return nil, err
	}

59 60 61 62 63 64
	ofi, err := os.Create(confdir + "/rpcaddress")
	if err != nil {
		log.Warning("Could not create rpcaddress file: %s", err)
		return nil, err
	}

65
	_, err = ofi.Write([]byte(addr.String()))
66 67 68 69 70 71
	if err != nil {
		log.Warning("Could not write to rpcaddress file: %s", err)
		return nil, err
	}
	ofi.Close()

72
	list, err := net.Listen(network, host)
73 74 75
	if err != nil {
		return nil, err
	}
76
	log.Info("New daemon listener initialized.")
77 78

	return &DaemonListener{
Siraj Ravel's avatar
Siraj Ravel committed
79
		node: ipfsnode,
80
		list: list,
81
		lk:   lk,
82 83 84
	}, nil
}

85 86 87 88
func NewCommand() *Command {
	return &Command{
		Opts: make(map[string]interface{}),
	}
89 90 91
}

func (dl *DaemonListener) Listen() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94 95
	if dl.closed {
		panic("attempting to listen on a closed daemon Listener")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
	// add ourselves to workgroup. and remove ourselves when done.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97
	dl.wg.Add(1)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99
	defer dl.wg.Done()

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

Siraj Ravel's avatar
Siraj Ravel committed
113 114
func (dl *DaemonListener) handleConnection(conn net.Conn) {
	defer conn.Close()
115

Siraj Ravel's avatar
Siraj Ravel committed
116
	dec := json.NewDecoder(conn)
117

Siraj Ravel's avatar
Siraj Ravel committed
118 119
	var command Command
	err := dec.Decode(&command)
120
	if err != nil {
Siraj Ravel's avatar
Siraj Ravel committed
121
		fmt.Fprintln(conn, err)
122 123
		return
	}
124

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
125
	log.Debug("Got command: %v", command)
126
	switch command.Command {
127
	case "add":
128
		err = commands.Add(dl.node, command.Args, command.Opts, conn)
129
	case "cat":
130
		err = commands.Cat(dl.node, command.Args, command.Opts, conn)
131
	case "ls":
132
		err = commands.Ls(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
133
	case "pin":
134
		err = commands.Pin(dl.node, command.Args, command.Opts, conn)
135 136
	case "publish":
		err = commands.Publish(dl.node, command.Args, command.Opts, conn)
137 138
	case "resolve":
		err = commands.Resolve(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
139
	case "diag":
Jeromy's avatar
Jeromy committed
140
		err = commands.Diag(dl.node, command.Args, command.Opts, conn)
141
	default:
142 143 144 145
		err = fmt.Errorf("Invalid Command: '%s'", command.Command)
	}
	if err != nil {
		fmt.Fprintln(conn, err)
146 147 148 149 150
	}
}

func (dl *DaemonListener) Close() error {
	dl.closed = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151 152 153 154
	err := dl.list.Close()
	dl.wg.Wait() // wait till done before releasing lock.
	dl.lk.Close()
	return err
155
}
156 157 158 159

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