rpc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "net/rpc"
  20. "syscall"
  21. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  22. )
  23. func (a *Agent) serveRPC(port string) {
  24. rpc.Register(a)
  25. rpc.HandleHTTP()
  26. l, e := net.Listen("tcp", port)
  27. if e != nil {
  28. plog.Fatal(e)
  29. }
  30. plog.Println("agent listening on", port)
  31. go http.Serve(l, nil)
  32. }
  33. func (a *Agent) RPCStart(args []string, pid *int) error {
  34. plog.Printf("start etcd with args %v", args)
  35. err := a.start(args...)
  36. if err != nil {
  37. plog.Println("error starting etcd", err)
  38. return err
  39. }
  40. *pid = a.cmd.Process.Pid
  41. return nil
  42. }
  43. func (a *Agent) RPCStop(args struct{}, reply *struct{}) error {
  44. plog.Printf("stop etcd")
  45. err := a.stopWithSig(syscall.SIGTERM)
  46. if err != nil {
  47. plog.Println("error stopping etcd", err)
  48. return err
  49. }
  50. return nil
  51. }
  52. func (a *Agent) RPCRestart(args struct{}, pid *int) error {
  53. plog.Printf("restart etcd")
  54. err := a.restart()
  55. if err != nil {
  56. plog.Println("error restarting etcd", err)
  57. return err
  58. }
  59. *pid = a.cmd.Process.Pid
  60. return nil
  61. }
  62. func (a *Agent) RPCCleanup(args struct{}, reply *struct{}) error {
  63. plog.Printf("cleanup etcd")
  64. err := a.cleanup()
  65. if err != nil {
  66. plog.Println("error cleaning up etcd", err)
  67. return err
  68. }
  69. return nil
  70. }
  71. func (a *Agent) RPCTerminate(args struct{}, reply *struct{}) error {
  72. plog.Printf("terminate etcd")
  73. err := a.terminate()
  74. if err != nil {
  75. plog.Println("error terminating etcd", err)
  76. }
  77. return nil
  78. }
  79. func (a *Agent) RPCDropPort(port int, reply *struct{}) error {
  80. plog.Printf("drop port %d", port)
  81. err := a.dropPort(port)
  82. if err != nil {
  83. plog.Println("error dropping port", err)
  84. }
  85. return nil
  86. }
  87. func (a *Agent) RPCRecoverPort(port int, reply *struct{}) error {
  88. plog.Printf("recover port %d", port)
  89. err := a.recoverPort(port)
  90. if err != nil {
  91. plog.Println("error recovering port", err)
  92. }
  93. return nil
  94. }
  95. func (a *Agent) RPCSetLatency(args []int, reply *struct{}) error {
  96. if len(args) != 2 {
  97. return fmt.Errorf("SetLatency needs two args, got (%v)", args)
  98. }
  99. plog.Printf("set latency of %dms (+/- %dms)", args[0], args[1])
  100. err := a.setLatency(args[0], args[1])
  101. if err != nil {
  102. plog.Println("error setting latency", err)
  103. }
  104. return nil
  105. }
  106. func (a *Agent) RPCRemoveLatency(args struct{}, reply *struct{}) error {
  107. plog.Println("removing latency")
  108. err := a.setLatency(0, 0)
  109. if err != nil {
  110. plog.Println("error removing latency")
  111. }
  112. return nil
  113. }
  114. func (a *Agent) RPCStatus(args struct{}, status *client.Status) error {
  115. *status = a.status()
  116. return nil
  117. }