-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from filecoin-project/feat/networking
Add in basic libp2p host, and commands for establishing connectivity
- Loading branch information
Showing
11 changed files
with
516 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
cmds "gx/ipfs/QmWGgKRz5S24SqaAapF5PPCfYfLT7MexJZewN5M82CQTzs/go-ipfs-cmds" | ||
cmdkit "gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit" | ||
|
||
"github.com/filecoin-project/go-filecoin/node" | ||
) | ||
|
||
// idOutput is the output of the /id api endpoint | ||
type idOutput struct { | ||
Addresses []string | ||
ID string | ||
AgentVersion string | ||
ProtocolVersion string | ||
PublicKey string | ||
} | ||
|
||
func idOutputFromNode(fcn *node.Node) *idOutput { | ||
var out idOutput | ||
for _, a := range fcn.Host.Addrs() { | ||
out.Addresses = append(out.Addresses, fmt.Sprintf("%s/ipfs/%s", a, fcn.Host.ID().Pretty())) | ||
} | ||
out.ID = fcn.Host.ID().Pretty() | ||
return &out | ||
} | ||
|
||
var idCmd = &cmds.Command{ | ||
Options: []cmdkit.Option{ | ||
// TODO: ideally copy this from the `ipfs id` command | ||
cmdkit.StringOption("format", "f", "specify an output format"), | ||
}, | ||
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) { | ||
fcn := GetNode(env) | ||
|
||
out := idOutputFromNode(fcn) | ||
|
||
re.Emit(out) | ||
}, | ||
Type: idOutput{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, val *idOutput) error { | ||
format, found := req.Options["format"].(string) | ||
if found { | ||
output := idFormatSubstitute(format, val) | ||
_, err := fmt.Fprint(w, output) | ||
return err | ||
} | ||
|
||
marshaled, err := json.MarshalIndent(val, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
marshaled = append(marshaled, byte('\n')) | ||
|
||
_, err = w.Write(marshaled) | ||
return err | ||
}), | ||
}, | ||
} | ||
|
||
func idFormatSubstitute(format string, val *idOutput) string { | ||
output := format | ||
output = strings.Replace(output, "<id>", val.ID, -1) | ||
output = strings.Replace(output, "<aver>", val.AgentVersion, -1) | ||
output = strings.Replace(output, "<pver>", val.ProtocolVersion, -1) | ||
output = strings.Replace(output, "<pubkey>", val.PublicKey, -1) | ||
output = strings.Replace(output, "<addrs>", strings.Join(val.Addresses, "\n"), -1) | ||
output = strings.Replace(output, "\\n", "\n", -1) | ||
output = strings.Replace(output, "\\t", "\t", -1) | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.