Commit 4f7856a4 authored by Siraj Ravel's avatar Siraj Ravel

refactoring naming conventions

parent 8dfb119f
...@@ -21,25 +21,25 @@ type DaemonListener struct { ...@@ -21,25 +21,25 @@ type DaemonListener struct {
closed bool closed bool
} }
func NewDaemonListener(node *core.IpfsNode, addr string) (*DaemonListener, error) { 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) list, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println("new daemon listener.") fmt.Println("New daemon listener initialized.")
return &DaemonListener{ return &DaemonListener{
node: node, node: Ipfsnode,
list: list, list: list,
}, nil }, nil
} }
type Command struct {
Command string
Args []string
Opts map[string]interface{}
}
func NewCommand() *Command { func NewCommand() *Command {
return &Command{ return &Command{
Opts: make(map[string]interface{}), Opts: make(map[string]interface{}),
...@@ -49,7 +49,7 @@ func NewCommand() *Command { ...@@ -49,7 +49,7 @@ func NewCommand() *Command {
func (dl *DaemonListener) Listen() { func (dl *DaemonListener) Listen() {
fmt.Println("listen.") fmt.Println("listen.")
for { for {
c, err := dl.list.Accept() conn, err := dl.list.Accept()
fmt.Println("Loop!") fmt.Println("Loop!")
if err != nil { if err != nil {
if !dl.closed { if !dl.closed {
...@@ -57,27 +57,27 @@ func (dl *DaemonListener) Listen() { ...@@ -57,27 +57,27 @@ func (dl *DaemonListener) Listen() {
} }
return return
} }
go dl.handleConnection(c) go dl.handleConnection(conn)
} }
} }
func (dl *DaemonListener) handleConnection(c net.Conn) { func (dl *DaemonListener) handleConnection(conn net.Conn) {
defer c.Close() defer conn.Close()
dec := json.NewDecoder(c) dec := json.NewDecoder(conn)
var com Command var command Command
err := dec.Decode(&com) err := dec.Decode(&command)
if err != nil { if err != nil {
fmt.Fprintln(c, err) fmt.Fprintln(conn, err)
return return
} }
u.DOut("Got command: %v\n", com) u.DOut("Got command: %v\n", command)
ExecuteCommand(&com, dl.node, c) 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) u.DOut("executing command: %s\n", com.Command)
switch com.Command { switch com.Command {
case "add": case "add":
...@@ -86,7 +86,7 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) { ...@@ -86,7 +86,7 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
depth = -1 depth = -1
} }
for _, path := range com.Args { for _, path := range com.Args {
_, err := commands.AddPath(n, path, depth) _, err := commands.AddPath(Ipfsnode, path, depth)
if err != nil { if err != nil {
fmt.Fprintf(out, "addFile error: %v\n", err) fmt.Fprintf(out, "addFile error: %v\n", err)
continue continue
...@@ -94,13 +94,13 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) { ...@@ -94,13 +94,13 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
} }
case "cat": case "cat":
for _, fn := range com.Args { for _, fn := range com.Args {
nd, err := n.Resolver.ResolvePath(fn) DAGnode, err := Ipfsnode.Resolver.ResolvePath(fn)
if err != nil { if err != nil {
fmt.Fprintf(out, "catFile error: %v\n", err) fmt.Fprintf(out, "catFile error: %v\n", err)
return return
} }
read, err := dag.NewDagReader(nd, n.DAG) read, err := dag.NewDagReader(nd, Ipfsnode.DAG)
if err != nil { if err != nil {
fmt.Fprintln(out, err) fmt.Fprintln(out, err)
continue continue
...@@ -114,9 +114,9 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) { ...@@ -114,9 +114,9 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
} }
case "ls": case "ls":
for _, fn := range com.Args { for _, fn := range com.Args {
nd, err := n.Resolver.ResolvePath(fn) DAGnode, err := n.Resolver.ResolvePath(fn)
if err != nil { if err != nil {
fmt.Fprintf(out, "ls: %v\n", err) fmt.Fprintf(out, "ls error: %v\n", err)
return return
} }
...@@ -126,13 +126,13 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) { ...@@ -126,13 +126,13 @@ func ExecuteCommand(com *Command, n *core.IpfsNode, out io.Writer) {
} }
case "pin": case "pin":
for _, fn := range com.Args { for _, fn := range com.Args {
nd, err := n.Resolver.ResolvePath(fn) DAGnode, err := Ipfsnode.Resolver.ResolvePath(fn)
if err != nil { if err != nil {
fmt.Fprintf(out, "pin: %v\n", err) fmt.Fprintf(out, "pin error: %v\n", err)
return return
} }
err = n.PinDagNode(nd) err = Ipfsnode.PinDagNode(nd)
if err != nil { if err != nil {
fmt.Fprintf(out, "pin: %v\n", err) fmt.Fprintf(out, "pin: %v\n", err)
return return
......
...@@ -8,19 +8,23 @@ import ( ...@@ -8,19 +8,23 @@ import (
"time" "time"
) )
func SendCommand(com *Command, server string) error {
con, err := net.DialTimeout("tcp", server, time.Millisecond*300) //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 { if err != nil {
return err return err
} }
enc := json.NewEncoder(con) enc := json.NewEncoder(conn)
err = enc.Encode(com) err = enc.Encode(command)
if err != nil { if err != nil {
return err return err
} }
io.Copy(os.Stdout, con) io.Copy(os.Stdout, conn)
return nil return nil
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment