You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/rpc"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RPCEndpoints int
|
|
|
|
|
|
|
|
type Signal struct {
|
|
|
|
PID int
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRPCServer 初始化监听传入LCM任务的RPC服务器
|
|
|
|
// 由RPC客户端执行
|
|
|
|
func InitRPCServer() error {
|
|
|
|
endpoints := new(RPCEndpoints)
|
|
|
|
// Publish the receivers methods
|
|
|
|
err := rpc.Register(endpoints)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("format of service Task isn't correct. Reason: %v", err)
|
|
|
|
}
|
|
|
|
// Register a HTTP handler
|
|
|
|
rpc.HandleHTTP()
|
|
|
|
// Listen to TPC connections on port 36866
|
|
|
|
listener, err := net.Listen("tcp", ":36866")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Listen error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
|
|
|
|
// Start accept incoming HTTP connections
|
|
|
|
go func() {
|
|
|
|
err = http.Serve(listener, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs <- fmt.Errorf("error serving: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errs:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rpc *RPCEndpoints) ShutDown(sig Signal, response *Signal) error {
|
|
|
|
|
|
|
|
pid := os.Getpid()
|
|
|
|
*response = Signal{pid, "Shutting down application"}
|
|
|
|
|
|
|
|
// time.Sleep(3 * time.Second)
|
|
|
|
return nil
|
|
|
|
}
|