Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring naming conventions #37

Merged
merged 3 commits into from
Sep 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package daemon

import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand All @@ -13,33 +12,34 @@ import (
u "github.com/jbenet/go-ipfs/util"
)

var ErrInvalidCommand = errors.New("invalid command")

//DaemonListener listens to an initialized IPFS node and can send it commands instead of
//starting up a new set of connections
type DaemonListener struct {
node *core.IpfsNode
list net.Listener
closed bool
}

func NewDaemonListener(node *core.IpfsNode, addr string) (*DaemonListener, error) {
//Command accepts user input and can be sent to the running IPFS node
type Command struct {
Command string
Args []string
Opts map[string]interface{}
}

func NewDaemonListener(ipfsnode *core.IpfsNode, addr string) (*DaemonListener, error) {
list, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
fmt.Println("new daemon listener.")
fmt.Println("New daemon listener initialized.")

return &DaemonListener{
node: node,
node: ipfsnode,
list: list,
}, nil
}

type Command struct {
Command string
Args []string
Opts map[string]interface{}
}

func NewCommand() *Command {
return &Command{
Opts: make(map[string]interface{}),
Expand All @@ -49,35 +49,35 @@ func NewCommand() *Command {
func (dl *DaemonListener) Listen() {
fmt.Println("listen.")
for {
c, err := dl.list.Accept()
conn, err := dl.list.Accept()
fmt.Println("Loop!")
if err != nil {
if !dl.closed {
u.PErr("DaemonListener Accept: %v\n", err)
}
return
}
go dl.handleConnection(c)
go dl.handleConnection(conn)
}
}

func (dl *DaemonListener) handleConnection(c net.Conn) {
defer c.Close()
func (dl *DaemonListener) handleConnection(conn net.Conn) {
defer conn.Close()

dec := json.NewDecoder(c)
dec := json.NewDecoder(conn)

var com Command
err := dec.Decode(&com)
var command Command
err := dec.Decode(&command)
if err != nil {
fmt.Fprintln(c, err)
fmt.Fprintln(conn, err)
return
}

u.DOut("Got command: %v\n", com)
ExecuteCommand(&com, dl.node, c)
u.DOut("Got command: %v\n", command)
ExecuteCommand(&command, dl.node, conn)
}

func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
func ExecuteCommand(com *Command, ipfsnode *core.IpfsNode, out io.Writer) {
u.DOut("executing command: %s\n", com.Command)
switch com.Command {
case "add":
Expand All @@ -86,21 +86,21 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
depth = -1
}
for _, path := range com.Args {
_, err := commands.AddPath(n, path, depth)
_, err := commands.AddPath(ipfsnode, path, depth)
if err != nil {
fmt.Fprintf(out, "addFile error: %v\n", err)
continue
}
}
case "cat":
for _, fn := range com.Args {
nd, err := n.Resolver.ResolvePath(fn)
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
fmt.Fprintf(out, "catFile error: %v\n", err)
return
}

read, err := dag.NewDagReader(nd, n.DAG)
read, err := dag.NewDagReader(dagnode, ipfsnode.DAG)
if err != nil {
fmt.Fprintln(out, err)
continue
Expand All @@ -114,25 +114,25 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
}
case "ls":
for _, fn := range com.Args {
nd, err := n.Resolver.ResolvePath(fn)
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
fmt.Fprintf(out, "ls: %v\n", err)
fmt.Fprintf(out, "ls error: %v\n", err)
return
}

for _, link := range nd.Links {
for _, link := range dagnode.Links {
fmt.Fprintf(out, "%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
}
}
case "pin":
for _, fn := range com.Args {
nd, err := n.Resolver.ResolvePath(fn)
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
fmt.Fprintf(out, "pin: %v\n", err)
fmt.Fprintf(out, "pin error: %v\n", err)
return
}

err = n.PinDagNode(nd)
err = ipfsnode.PinDagNode(dagnode)
if err != nil {
fmt.Fprintf(out, "pin: %v\n", err)
return
Expand Down
13 changes: 8 additions & 5 deletions daemon/daemon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import (
"time"
)

func SendCommand(com *Command, server string) error {
con, err := net.DialTimeout("tcp", server, time.Millisecond*300)
//SendCommand connects to the address on the network with a timeout and encodes the connection into JSON
func SendCommand(command *Command, server string) error {

conn, err := net.DialTimeout("tcp", server, time.Millisecond*300)

if err != nil {
return err
}

enc := json.NewEncoder(con)
err = enc.Encode(com)
enc := json.NewEncoder(conn)
err = enc.Encode(command)
if err != nil {
return err
}

io.Copy(os.Stdout, con)
io.Copy(os.Stdout, conn)

return nil
}