Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.16 KB | None | 0 0
  1. /*
  2.  
  3. A trivial application to illustrate how the fdlib library can be used
  4. in assignment 1 for UBC CS 416 2018W1.
  5.  
  6. Usage:
  7. go run client.go
  8. */
  9.  
  10. package main
  11.  
  12. // Expects fdlib.go to be in the ./fdlib/ dir, relative to
  13. // this client.go file
  14. import "./fdlib"
  15.  
  16. import "fmt"
  17. import "os"
  18. import "time"
  19.  
  20. func main() {
  21.     // Local (127.0.0.1) hardcoded IPs to simplify testing.
  22.     localIpPort := "198.162.33.54:8080"
  23.     toMonitorIpPort := "198.162.33.23:9999" // TODO: change this to remote node
  24.     var lostMsgThresh uint8 = 5
  25.  
  26.     // TODO: generate a new random epoch nonce on each run
  27.     var epochNonce uint64 = 0xffff
  28.     var chCapacity uint8 = 5
  29.  
  30.     // Initialize fdlib. Note the use of multiple assignment:
  31.     // https://gobyexample.com/multiple-return-values
  32.     fd, notifyCh, err := fdlib.Initialize(epochNonce, chCapacity)
  33.     if checkError(err) != nil {
  34.         return
  35.     }
  36.  
  37.     // Stop monitoring and stop responding on exit.
  38.     // Defers are really cool, check out: https://blog.golang.org/defer-panic-and-recover
  39.     defer fd.StopMonitoring()
  40.     defer fd.StopResponding()
  41.  
  42.     err = fd.StartResponding(localIpPort)
  43.     if checkError(err) != nil {
  44.         return
  45.     }
  46.  
  47.     fmt.Println("Started responding to heartbeats.")
  48.  
  49.     // Add a monitor for a remote node.
  50.     localIpPortMon := "198.162.33.54:9999"
  51.     err = fd.AddMonitor(localIpPortMon, toMonitorIpPort, lostMsgThresh)
  52.     if checkError(err) != nil {
  53.         return
  54.     }
  55.  
  56.     fmt.Println("Started to monitor node: ", toMonitorIpPort)
  57.  
  58.     // Wait indefinitely, blocking on the notify channel, to detect a
  59.     // failure.
  60.     select {
  61.     case notify := <-notifyCh:
  62.         fmt.Println("Detected a failure of", notify)
  63.         // Re-add the remote node for monitoring.
  64.         err := fd.AddMonitor(localIpPortMon, toMonitorIpPort, lostMsgThresh)
  65.         if checkError(err) != nil {
  66.             return
  67.         }
  68.         fmt.Println("Started to monitor node: ", toMonitorIpPort)
  69.     case <-time.After(time.Duration(int(lostMsgThresh)*3) * time.Second):
  70.         // case <-time.After(time.Second):
  71.         fmt.Println("No failures detected")
  72.     }
  73. }
  74.  
  75. // If error is non-nil, print it out and return it.
  76. func checkError(err error) error {
  77.     if err != nil {
  78.         fmt.Fprintf(os.Stderr, "Error ", err.Error())
  79.         return err
  80.     }
  81.     return nil
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement