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

import (
	"encoding/json"
5
	"fmt"
6
	"io"
7
	"net"
8
	"os"
9
	"path"
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
	logging "github.com/op/go-logging"
15

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

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

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
31
	lk     io.Closer
32 33
}

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

41 42 43 44 45 46 47
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
	}

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

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

58 59 60 61 62 63 64 65 66 67 68 69 70
	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()

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

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

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

func (dl *DaemonListener) Listen() {
91
	fmt.Println("listen.")
92
	for {
Siraj Ravel's avatar
Siraj Ravel committed
93
		conn, err := dl.list.Accept()
94
		fmt.Println("Loop!")
95 96 97 98
		if err != nil {
			if !dl.closed {
				u.PErr("DaemonListener Accept: %v\n", err)
			}
99
			dl.lk.Close()
100 101
			return
		}
Siraj Ravel's avatar
Siraj Ravel committed
102
		go dl.handleConnection(conn)
103 104 105
	}
}

Siraj Ravel's avatar
Siraj Ravel committed
106 107
func (dl *DaemonListener) handleConnection(conn net.Conn) {
	defer conn.Close()
108

Siraj Ravel's avatar
Siraj Ravel committed
109
	dec := json.NewDecoder(conn)
110

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

Siraj Ravel's avatar
Siraj Ravel committed
118
	u.DOut("Got command: %v\n", command)
119
	switch command.Command {
120
	case "add":
121
		err = commands.Add(dl.node, command.Args, command.Opts, conn)
122
	case "cat":
123
		err = commands.Cat(dl.node, command.Args, command.Opts, conn)
124
	case "ls":
125
		err = commands.Ls(dl.node, command.Args, command.Opts, conn)
Jeromy's avatar
Jeromy committed
126
	case "pin":
127
		err = commands.Pin(dl.node, command.Args, command.Opts, conn)
128
	default:
129 130 131 132
		err = fmt.Errorf("Invalid Command: '%s'", command.Command)
	}
	if err != nil {
		fmt.Fprintln(conn, err)
133 134 135 136 137 138 139
	}
}

func (dl *DaemonListener) Close() error {
	dl.closed = true
	return dl.list.Close()
}
140 141 142 143

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