daemon.go 3.44 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 43 44 45 46 47 48
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
	}

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

54 55 56 57 58
	network, host, err := addr.DialArgs()
	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 66 67 68 69 70
	mstr, err := addr.String()
	if err != nil {
		return nil, err
	}

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

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

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

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

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

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

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

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

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

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

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

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

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