Added CLI args parsing

master
Mike Goelzer 6 years ago
parent 5a6c500533
commit 4f95246130
No known key found for this signature in database
GPG Key ID: EDAC46A37751AD6D

@ -10,9 +10,13 @@
```
cd content-dht-provide-find
make
./dht-interop
./dht-interop -b ../util/private_key.bin
```
`-b` means bootstrap mode. In this example, the go program is always the bootstrap node, so `-b` is always required.
Note that the node ID of `dht-interop` is always `Qm...6aJ9oRuEzWa` because it is being read in from `../util/private_key.bin` (a private key marshalled to X.509 generated by `util/private-key-gen`). This is to keep the peer id of the bootstrap server stable across invocations.
**Second terminal:** run the command printed out by dht-interop, replacing 127.0.0.1 with the IP of the server where dht-interop is listening. Example:
First time only:
@ -26,7 +30,6 @@ Running the Node.js program:
node js-dht-test/index.js /ip4/127.0.0.1/tcp/9876/ipfs/QmehVYruznbyDZuHBV4vEHESpDevMoAovET6aJ9oRuEzWa
```
Note that the node ID of `dht-interop` is always `Qm...6aJ9oRuEzWa` because it is being read in from `util/private_key.bin` (a private key marshalled to X.509 generated by `util/private-key-gen`). This is to keep the peer id of the bootstrap server stable across invocations.
## Demo 2: PubSub

@ -63,14 +63,37 @@ func handleConn(conn net.Conn) {
}
}
func parseArgs() (bool, string) {
usage := fmt.Sprintf("Usage: %s [-b] [PRIVATE_KEY]\n\n-b is bootstrap mode (creates DHT)\nPRIVATE_KEY is the path to a private key like '../util/private_key.bin'\n", os.Args[0])
var bBootstrap bool = false
var privKeyFilePath string
var args []string = os.Args[1:]
if (len(args) == 0) || (len(args) > 2) {
fmt.Printf("Error: wrong number of arguments\n\n%s", usage)
os.Exit(1)
}
if args[0] == "-b" {
bBootstrap = true
args = args[1:]
}
privKeyFilePath = args[0]
return bBootstrap, privKeyFilePath
}
func main() {
ctx := context.Background()
bBootstrap, privKeyFilePath := parseArgs()
if !bBootstrap {
fmt.Printf("Error: bootstrap mode required in this example")
os.Exit(1)
}
//
// Read the private key
//
var privBytes []byte
privBytes, err := ioutil.ReadFile("../util/private_key.bin")
privBytes, err := ioutil.ReadFile(privKeyFilePath)
if err != nil {
fmt.Println("ioutil.ReadFile: failed: %v", err)
panic(err)

Loading…
Cancel
Save