Commit 7dcce5ad authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

[d] Add configurable addrport and identifier

Fix #3 - Should be merged asap in 192 branch
parent 2b63e8df
Pipeline #642 passed with stage
...@@ -5,10 +5,13 @@ import ( ...@@ -5,10 +5,13 @@ import (
"flag" "flag"
"fmt" "fmt"
"runtime" "runtime"
dapi "dfss/dfssd/api"
) )
var ( var (
verbose bool verbose bool
demo string
fca string // Path to the CA fca string // Path to the CA
fcert string // Path to the certificate fcert string // Path to the certificate
fkey string // Path to the private key fkey string // Path to the private key
...@@ -23,6 +26,7 @@ func init() { ...@@ -23,6 +26,7 @@ func init() {
flag.StringVar(&fcert, "cert", "cert.pem", "Path to the user certificate") flag.StringVar(&fcert, "cert", "cert.pem", "Path to the user certificate")
flag.StringVar(&fkey, "key", "key.pem", "Path to the private key") flag.StringVar(&fkey, "key", "key.pem", "Path to the private key")
flag.StringVar(&addrPort, "host", "localhost:9000", "Host of the DFSS platform") flag.StringVar(&addrPort, "host", "localhost:9000", "Host of the DFSS platform")
flag.StringVar(&demo, "d", "", "Demonstrator address and port (empty string disables debug)")
flag.IntVar(&localPort, "port", 9005, "Port to use for P2P communication between clients") flag.IntVar(&localPort, "port", 9005, "Port to use for P2P communication between clients")
flag.Usage = func() { flag.Usage = func() {
...@@ -73,6 +77,8 @@ var commands = map[string]command{ ...@@ -73,6 +77,8 @@ var commands = map[string]command{
func main() { func main() {
flag.Parse() flag.Parse()
arg := flag.Arg(0) arg := flag.Arg(0)
dapi.Configure(demo != "", demo, "client")
c, ok := commands[arg] c, ok := commands[arg]
if !ok || flag.NArg()-1 < c.nbArgs { if !ok || flag.NArg()-1 < c.nbArgs {
......
package api package api
import ( import (
"time"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
"time"
) )
var ( var (
// theses are the default parameters address, identifier string
address = "localhost:3000" demo bool
identifier = "platform"
demo = false
// lazy initializer // lazy initializer
dial *grpc.ClientConn dial *grpc.ClientConn
demoClient DemonstratorClient demoClient DemonstratorClient
) )
// Switch for demo mode // Configure is used to update current parameters.
// // Call it at least one time before the first DLog call.
// Should be used to pass the value of `-d` switch func Configure(activated bool, addrport, id string) {
func Switch(activationSwitch bool) { address = addrport
demo = activationSwitch identifier = id
return demo = activated
} }
// Lazy initialisation for demonstrator's connection to server // Lazy initialisation for demonstrator's connection to server
func dInit() error { func dInit() error {
var err error var err error
dial, err = grpc.Dial(address, grpc.WithInsecure()) dial, err = grpc.Dial(address, grpc.WithInsecure(), grpc.WithTimeout(10*time.Second))
if err != nil { if err != nil {
grpclog.Printf("Fail to dial: %v", err) grpclog.Printf("Fail to dial: %v", err)
return err
} }
demoClient = NewDemonstratorClient(dial) demoClient = NewDemonstratorClient(dial)
return nil
return err
} }
// DClose close the connection with demonstrator server (if any) // DClose close the connection with demonstrator server (if any)
...@@ -45,7 +44,7 @@ func DClose() { ...@@ -45,7 +44,7 @@ func DClose() {
if dial != nil { if dial != nil {
err := dial.Close() err := dial.Close()
if err != nil { if err != nil {
grpclog.Printf("Fail to close dialing: %v", err) grpclog.Printf("Failed to close dialing with demonstrator: %v", err)
} }
} }
} }
...@@ -53,9 +52,7 @@ func DClose() { ...@@ -53,9 +52,7 @@ func DClose() {
// DLog send a message to the demonstrator // DLog send a message to the demonstrator
// //
// The client is dialed in a lazy way // The client is dialed in a lazy way
// The default demonstrator server address is localhost:3000
func DLog(log string) { func DLog(log string) {
// check demo switch // check demo switch
if !demo { if !demo {
return return
......
...@@ -15,16 +15,16 @@ import ( ...@@ -15,16 +15,16 @@ import (
) )
var ( var (
verbose, demo bool verbose bool
path, country, org, unit, cn, port, address, dbURI string path, country, org, unit, cn, port, address, dbURI, demo string
keySize, rootValidity, certValidity int keySize, rootValidity, certValidity int
) )
func init() { func init() {
flag.BoolVar(&verbose, "v", false, "Print verbose messages") flag.BoolVar(&verbose, "v", false, "Print verbose messages")
flag.BoolVar(&demo, "d", false, "Enable demonstrator")
flag.StringVar(&demo, "d", "", "Demonstrator address and port (empty string disables debug)")
flag.StringVar(&port, "p", "9000", "Default port listening") flag.StringVar(&port, "p", "9000", "Default port listening")
flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening") flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening")
...@@ -67,7 +67,7 @@ func init() { ...@@ -67,7 +67,7 @@ func init() {
func main() { func main() {
flag.Parse() flag.Parse()
command := flag.Arg(0) command := flag.Arg(0)
dapi.Switch(demo) dapi.Configure(demo != "", demo, "platform")
switch command { switch command {
case "version": case "version":
......
...@@ -13,13 +13,14 @@ import ( ...@@ -13,13 +13,14 @@ import (
) )
var ( var (
verbose, demo bool verbose bool
fca string // Path to the CA fca string // Path to the CA
fcert string // Path to the certificate fcert string // Path to the certificate
fkey string // Path to the private key fkey string // Path to the private key
address string address string
dbURI string dbURI string
port string port string
demo string
) )
func init() { func init() {
...@@ -28,7 +29,7 @@ func init() { ...@@ -28,7 +29,7 @@ func init() {
flag.StringVar(&fca, "ca", "ca.pem", "Path to the root certificate") flag.StringVar(&fca, "ca", "ca.pem", "Path to the root certificate")
flag.StringVar(&fcert, "cert", "cert.pem", "Path to the user certificate") flag.StringVar(&fcert, "cert", "cert.pem", "Path to the user certificate")
flag.StringVar(&fkey, "key", "key.pem", "Path to the private key") flag.StringVar(&fkey, "key", "key.pem", "Path to the private key")
flag.BoolVar(&demo, "d", false, "Enable demonstrator") flag.StringVar(&demo, "d", "", "Demonstrator address and port (empty string disables debug)")
flag.StringVar(&port, "p", "9020", "Default port listening") flag.StringVar(&port, "p", "9020", "Default port listening")
flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening") flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening")
...@@ -58,7 +59,7 @@ func init() { ...@@ -58,7 +59,7 @@ func init() {
func main() { func main() {
flag.Parse() flag.Parse()
command := flag.Arg(0) command := flag.Arg(0)
dapi.Switch(demo) dapi.Configure(demo != "", demo, "ttp")
switch command { switch command {
case "version": case "version":
......
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