daemon.go 3.4 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
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
13
	core "github.com/jbenet/go-ipfs/core"
14
	"github.com/jbenet/go-ipfs/core/commands"
15
	u "github.com/jbenet/go-ipfs/util"
16

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

21 22
var log = logging.MustGetLogger("daemon")

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

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

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

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

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

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

60 61 62 63 64 65 66 67 68 69 70 71 72
	ofi, err := os.Create(confdir + "/rpcaddress")
	if err != nil {
		log.Warning("Could not create rpcaddress file: %s", err)
		return nil, err
	}

	_, err = ofi.Write([]byte(host))
	if err != nil {
		log.Warning("Could not write to rpcaddress file: %s", err)
		return nil, err
	}
	ofi.Close()

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

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

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

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

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

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

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

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

Siraj Ravel's avatar
Siraj Ravel committed
124
	u.DOut("Got command: %v\n", command)
125
	switch command.Command {
126
	case "add":
127
		err = commands.Add(dl.node, command.Args, command.Opts, conn)
128
	case "cat":
129
		err = commands.Cat(dl.node, command.Args, command.Opts, conn)
130
	case "ls":
131
		err = commands.Ls(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
132
	case "pin":
133
		err = commands.Pin(dl.node, command.Args, command.Opts, conn)
134 135
	case "publish":
		err = commands.Publish(dl.node, command.Args, command.Opts, conn)
136 137
	case "resolve":
		err = commands.Resolve(dl.node, command.Args, command.Opts, conn)
138
	default:
139 140 141 142
		err = fmt.Errorf("Invalid Command: '%s'", command.Command)
	}
	if err != nil {
		fmt.Fprintln(conn, err)
143 144 145 146 147
	}
}

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

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