github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/structured/rsm/kvclientmain.go (about)

     1  // Simple client to connect to the key-value service and exercise the
     2  // key-value RPC API.
     3  //
     4  // Usage: go run kvclientmain.go [clientid] [clock-update-rate] [ip1:port1] ... [ipN:portN]
     5  //
     6  // - [clientid] : the clientid uint8 to use for this client
     7  //
     8  // - [clock-update-rate] : the rate at which the local lamport clock
     9  //                         is reported to the server through a
    10  //                         clockupdate() call. Number of milliseconds
    11  //                         between clockupdate calls.
    12  //
    13  // - [ip1:port1] : the ip and TCP port of the 1st replica listening for clients
    14  // - ...
    15  // - [ipN:portN] : the ip and TCP port of the Nth replica listening for clients
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"./kvlib"
    23  	"strconv"
    24  )
    25  
    26  // Main server loop.
    27  func main() {
    28  	// parse args
    29  	usage := fmt.Sprintf("Usage: %s [clientid] [clock-update-rate] [ip1:port1] ... [ipN:portN]\n", os.Args[0])
    30  	if len(os.Args) < 4 {
    31  		fmt.Printf(usage)
    32  		os.Exit(1)
    33  	}
    34  
    35  	clientId, err := strconv.ParseUint(os.Args[1], 10, 8)
    36  	if err != nil {
    37  		fmt.Println(err)
    38  		os.Exit(1)
    39  	}
    40  
    41  	clockRate, err := strconv.ParseUint(os.Args[2], 10, 8)
    42  	if err != nil {
    43  		fmt.Println(err)
    44  		os.Exit(1)
    45  	}
    46  
    47  	addresses := []string{}
    48  	for i := 3; i < len(os.Args); i++ {
    49  		addresses = append(addresses, os.Args[i])
    50  	}
    51  
    52  	api, err := kvlib.Init(uint8(clientId), uint8(clockRate), addresses)
    53  	checkError(err)
    54  
    55  	err = api.Put("Hello", "World")
    56  	checkError(err)
    57  	val, err := api.Get("Hello")
    58  	checkError(err)
    59  	fmt.Println("Value was ", val)
    60  	err = api.Disconnect()
    61  	checkError(err)
    62  	fmt.Println("\nMission accomplished.")
    63  }
    64  
    65  // If error is non-nil, print it out and halt.
    66  func checkError(err error) {
    67  	if err != nil {
    68  		fmt.Fprintf(os.Stderr, "Error ", err.Error())
    69  		os.Exit(1)
    70  	}
    71  }
    72