Merge pull request #833 from krishnasrinivas/server-service-2

Add json rpc ServerService - provides only dummy data right now
master
Harshavardhana 9 years ago
commit e821d4d0cc
  1. 4
      pkg/controller/router.go
  2. 34
      pkg/controller/rpc/rpc.go
  3. 94
      pkg/controller/rpc/server.go
  4. 84
      pkg/controller/rpc/sysinfo.go
  5. 4
      pkg/controller/rpc_test.go

@ -26,12 +26,10 @@ import (
// getRPCHandler rpc handler // getRPCHandler rpc handler
func getRPCHandler() http.Handler { func getRPCHandler() http.Handler {
s := rpc.NewServer() s := rpc.NewServer()
s.RegisterJSONCodec()
s.RegisterService(new(rpc.VersionService), "Version") s.RegisterService(new(rpc.VersionService), "Version")
s.RegisterService(new(rpc.SysInfoService), "SysInfo")
s.RegisterService(new(rpc.MemStatsService), "MemStats")
s.RegisterService(new(rpc.DonutService), "Donut") s.RegisterService(new(rpc.DonutService), "Donut")
s.RegisterService(new(rpc.AuthService), "Auth") s.RegisterService(new(rpc.AuthService), "Auth")
s.RegisterService(rpc.NewServerService(), "Server")
// Add new RPC services here // Add new RPC services here
return registerRPC(router.NewRouter(), s) return registerRPC(router.NewRouter(), s)
} }

@ -0,0 +1,34 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json"
)
type Server struct {
*rpc.Server
}
// NewServer - provide a new instance of RPC server
func NewServer() *Server {
s := &Server{}
s.Server = rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
return s
}

@ -18,34 +18,90 @@ package rpc
import ( import (
"net/http" "net/http"
"os"
"runtime"
"syscall"
"github.com/gorilla/rpc/v2" "github.com/minio/minio/pkg/probe"
"github.com/gorilla/rpc/v2/json"
) )
// Server rpc server container type MinioServer struct {
type Server struct { Name string `json:"name"`
RPCServer *rpc.Server IP string `json:"ip"`
ID string `json:"id"`
} }
// RegisterJSONCodec - register standard json codec type ServerArg struct {
func (s Server) RegisterJSONCodec() { MinioServer
s.RPCServer.RegisterCodec(json.NewCodec(), "application/json")
} }
// RegisterService - register new services type DummyReply struct{}
func (s Server) RegisterService(recv interface{}, name string) {
s.RPCServer.RegisterService(recv, name) type MemStatsReply struct {
runtime.MemStats `json:"memstats"`
} }
// NewServer - provide a new instance of RPC server type DiskStatsReply struct {
func NewServer() *Server { DiskStats syscall.Statfs_t `json:"diskstats"`
s := &Server{} }
s.RPCServer = rpc.NewServer()
return s type SysInfoReply struct {
Hostname string `json:"hostname"`
SysARCH string `json:"sys.arch"`
SysOS string `json:"sys.os"`
SysCPUS int `json:"sys.ncpus"`
Routines int `json:"goroutines"`
GOVersion string `json:"goversion"`
}
type ListReply struct {
List []MinioServer `json:"list"`
}
type ServerService struct {
list []MinioServer
}
func (this *ServerService) Add(r *http.Request, arg *ServerArg, reply *DummyReply) error {
this.list = append(this.list, MinioServer{arg.Name, arg.IP, arg.ID})
return nil
} }
// ServeHTTP wrapper method for http.Handler interface func (this *ServerService) MemStats(r *http.Request, arg *ServerArg, reply *MemStatsReply) error {
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { runtime.ReadMemStats(&reply.MemStats)
s.RPCServer.ServeHTTP(w, r) return nil
}
func (this *ServerService) DiskStats(r *http.Request, arg *ServerArg, reply *DiskStatsReply) error {
syscall.Statfs("/", &reply.DiskStats)
return nil
}
func (this *ServerService) SysInfo(r *http.Request, arg *ServerArg, reply *SysInfoReply) error {
reply.SysARCH = runtime.GOARCH
reply.SysOS = runtime.GOOS
reply.SysCPUS = runtime.NumCPU()
reply.Routines = runtime.NumGoroutine()
reply.GOVersion = runtime.Version()
var err error
reply.Hostname, err = os.Hostname()
if err != nil {
return probe.WrapError(probe.NewError(err))
}
return nil
}
func (this *ServerService) List(r *http.Request, arg *ServerArg, reply *ListReply) error {
reply.List = this.list
return nil
}
func NewServerService() *ServerService {
s := &ServerService{}
s.list = []MinioServer{
{"server.one", "192.168.1.1", "192.168.1.1"},
{"server.two", "192.168.1.2", "192.168.1.2"},
{"server.three", "192.168.1.3", "192.168.1.3"},
}
return s
} }

@ -1,84 +0,0 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"net/http"
"os"
"runtime"
"github.com/minio/minio/pkg/probe"
)
// SysInfoService -
type SysInfoService struct{}
// SysInfoReply -
type SysInfoReply struct {
Hostname string `json:"hostname"`
SysARCH string `json:"sys.arch"`
SysOS string `json:"sys.os"`
SysCPUS int `json:"sys.ncpus"`
Routines int `json:"goroutines"`
GOVersion string `json:"goversion"`
}
// MemStatsService -
type MemStatsService struct{}
// MemStatsReply -
type MemStatsReply struct {
runtime.MemStats `json:"memstats"`
}
func setSysInfoReply(sis *SysInfoReply) *probe.Error {
sis.SysARCH = runtime.GOARCH
sis.SysOS = runtime.GOOS
sis.SysCPUS = runtime.NumCPU()
sis.Routines = runtime.NumGoroutine()
sis.GOVersion = runtime.Version()
var err error
sis.Hostname, err = os.Hostname()
if err != nil {
return probe.NewError(err)
}
return nil
}
func setMemStatsReply(sis *MemStatsReply) *probe.Error {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
sis.MemStats = memStats
return nil
}
// Get method
func (s *SysInfoService) Get(r *http.Request, args *Args, reply *SysInfoReply) error {
if err := setSysInfoReply(reply); err != nil {
return probe.WrapError(err)
}
return nil
}
// Get method
func (s *MemStatsService) Get(r *http.Request, args *Args, reply *MemStatsReply) error {
if err := setMemStatsReply(reply); err != nil {
return probe.WrapError(err)
}
return nil
}

@ -45,7 +45,7 @@ func (s *MySuite) TearDownSuite(c *C) {
func (s *MySuite) TestMemStats(c *C) { func (s *MySuite) TestMemStats(c *C) {
op := rpc.Operation{ op := rpc.Operation{
Method: "MemStats.Get", Method: "Server.MemStats",
Request: rpc.Args{Request: ""}, Request: rpc.Args{Request: ""},
} }
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport) req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
@ -63,7 +63,7 @@ func (s *MySuite) TestMemStats(c *C) {
func (s *MySuite) TestSysInfo(c *C) { func (s *MySuite) TestSysInfo(c *C) {
op := rpc.Operation{ op := rpc.Operation{
Method: "SysInfo.Get", Method: "Server.SysInfo",
Request: rpc.Args{Request: ""}, Request: rpc.Args{Request: ""},
} }
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport) req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)

Loading…
Cancel
Save