daemon.go 3.54 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
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
var log = u.Logger("daemon", logging.ERROR)
22

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
	ofi, err := os.Create(confdir + "/rpcaddress")
	if err != nil {
		log.Warning("Could not create rpcaddress file: %s", err)
		return nil, err
	}

66 67 68 69 70 71
	mstr, err := addr.String()
	if err != nil {
		return nil, err
	}

	_, err = ofi.Write([]byte(mstr))
72 73 74 75 76 77
	if err != nil {
		log.Warning("Could not write to rpcaddress file: %s", err)
		return nil, err
	}
	ofi.Close()

78
	list, err := net.Listen(network, host)
79 80 81
	if err != nil {
		return nil, err
	}
82
	log.Info("New daemon listener initialized.")
83 84

	return &DaemonListener{
Siraj Ravel's avatar
Siraj Ravel committed
85
		node: ipfsnode,
86
		list: list,
87
		lk:   lk,
88 89 90
	}, nil
}

91 92 93 94
func NewCommand() *Command {
	return &Command{
		Opts: make(map[string]interface{}),
	}
95 96 97
}

func (dl *DaemonListener) Listen() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99 100 101
	if dl.closed {
		panic("attempting to listen on a closed daemon Listener")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102
	// add ourselves to workgroup. and remove ourselves when done.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
	dl.wg.Add(1)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105
	defer dl.wg.Done()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106
	log.Info("daemon listening")
107
	for {
Siraj Ravel's avatar
Siraj Ravel committed
108
		conn, err := dl.list.Accept()
109 110
		if err != nil {
			if !dl.closed {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111
				log.Warning("DaemonListener Accept: %v", err)
112 113 114
			}
			return
		}
Siraj Ravel's avatar
Siraj Ravel committed
115
		go dl.handleConnection(conn)
116 117 118
	}
}

Siraj Ravel's avatar
Siraj Ravel committed
119 120
func (dl *DaemonListener) handleConnection(conn net.Conn) {
	defer conn.Close()
121

Siraj Ravel's avatar
Siraj Ravel committed
122
	dec := json.NewDecoder(conn)
123

Siraj Ravel's avatar
Siraj Ravel committed
124 125
	var command Command
	err := dec.Decode(&command)
126
	if err != nil {
Siraj Ravel's avatar
Siraj Ravel committed
127
		fmt.Fprintln(conn, err)
128 129
		return
	}
130

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

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

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