of_rpc => grpc
This commit is contained in:
parent
513937a41d
commit
85efc8ea7a
19
config_server.go
Normal file
19
config_server.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
pb "loafle.com/overflow/crawler_go/grpc"
|
||||
)
|
||||
|
||||
type ConfigServer struct {
|
||||
}
|
||||
|
||||
func (s *ConfigServer) Add(c context.Context, in *pb.Input) (*pb.Output, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *ConfigServer) Remove(c context.Context, in *pb.Input) (*pb.Output, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *ConfigServer) Init(c context.Context, in *pb.InputArray) (*pb.Output, error) {
|
||||
return nil, nil
|
||||
}
|
25
data_server.go
Normal file
25
data_server.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
pb "loafle.com/overflow/crawler_go/grpc"
|
||||
)
|
||||
|
||||
type DataServer struct {
|
||||
}
|
||||
|
||||
func (s *DataServer) Get(c context.Context, in *pb.Input) (*pb.Output, error) {
|
||||
|
||||
output := &pb.Output{}
|
||||
|
||||
if c, ok := g_crawlers[in.Name.String()]; ok {
|
||||
rd, err := c.Get(in.Id)
|
||||
if err != nil {
|
||||
// process error
|
||||
}
|
||||
output.Data = rd
|
||||
} else {
|
||||
output.Data = []byte("Not Assign Crawler")
|
||||
}
|
||||
return output, nil
|
||||
}
|
|
@ -6,22 +6,22 @@ import (
|
|||
"google.golang.org/grpc"
|
||||
"loafle.com/overflow/crawler_go"
|
||||
pb "loafle.com/overflow/crawler_go/grpc"
|
||||
"log"
|
||||
"net"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/redis_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/activedirectory_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/cassandra_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/dns_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/ftp_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/ftps_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/http_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/imap_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/ldap_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/mongodb_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/mssql_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/mysql_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/pgsql_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/netbios_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/oracle_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/pgsql_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/pop3_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/redis_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/rmi_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/smb_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/smtp_protocol_crawler_go"
|
||||
|
@ -30,8 +30,8 @@ import (
|
|||
"loafle.com/overflow/crawler_go/health_crawler/ssh_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/telnet_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/wmi_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/cassandra_protocol_crawler_go"
|
||||
"loafle.com/overflow/crawler_go/health_crawler/http_protocol_crawler_go"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
var g_crawlers map[string]crawler.Crawler
|
||||
|
@ -43,42 +43,9 @@ func AddDelegate(name string, c crawler.Crawler) {
|
|||
g_crawlers[name] = c
|
||||
}
|
||||
|
||||
type ConfigServer struct {
|
||||
}
|
||||
|
||||
func (s *ConfigServer) Add(c context.Context, in *pb.Input) (*pb.Output, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *ConfigServer) Remove(c context.Context, in *pb.Input) (*pb.Output, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *ConfigServer) Init(c context.Context, in *pb.InputArray) (*pb.Output, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type DataServer struct {
|
||||
}
|
||||
|
||||
func (s *DataServer) Get(c context.Context, in *pb.Input) (*pb.Output, error) {
|
||||
|
||||
output := &pb.Output{}
|
||||
|
||||
if c, ok := g_crawlers[in.Name.String()]; ok {
|
||||
rd,err := c.Get(in.Id)
|
||||
if err != nil {
|
||||
// process error
|
||||
}
|
||||
output.Data = rd
|
||||
} else {
|
||||
output.Data = []byte("Not Assign Crawler")
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
|
||||
func initCrawlers() {
|
||||
g_crawlers = make(map[string]crawler.Crawler,0)
|
||||
|
||||
g_crawlers = make(map[string]crawler.Crawler, 0)
|
||||
|
||||
AddDelegate(pb.Crawlers_HEALTH_ACTIVEDIRECTORY.String(), activedirectory_protocol_crawler_go.NewActiveDirectoryHealthCrawler())
|
||||
AddDelegate(pb.Crawlers_HEALTH_DNS.String(), dns_protocol_crawler_go.NewDNSHealthCrawler())
|
||||
AddDelegate(pb.Crawlers_HEALTH_FTP.String(), ftp_protocol_crawler_go.NewFTPHealthCrawler())
|
||||
|
@ -106,7 +73,7 @@ func initCrawlers() {
|
|||
AddDelegate(pb.Crawlers_HEALTH_HTTP.String(), http_protocol_crawler_go.NewHTTPHealthCrawler())
|
||||
}
|
||||
|
||||
func start(rc pb.ConfigServer,rd pb.DataServer) {
|
||||
func start(rc pb.ConfigServer, rd pb.DataServer) {
|
||||
|
||||
port := flag.String("Port", "50000", "RPC Port Default 50000")
|
||||
flag.Parse()
|
||||
|
@ -118,8 +85,8 @@ func start(rc pb.ConfigServer,rd pb.DataServer) {
|
|||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
s := grpc.NewServer()
|
||||
pb.RegisterConfigServer(s,rc)
|
||||
pb.RegisterDataServer(s,rd)
|
||||
pb.RegisterConfigServer(s, rc)
|
||||
pb.RegisterDataServer(s, rd)
|
||||
if err := s.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
|
@ -127,5 +94,5 @@ func start(rc pb.ConfigServer,rd pb.DataServer) {
|
|||
|
||||
func main() {
|
||||
initCrawlers()
|
||||
start(&ConfigServer{},&DataServer{})
|
||||
start(&ConfigServer{}, &DataServer{})
|
||||
}
|
||||
|
|
|
@ -1,29 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
pb "loafle.com/overflow/crawler_go/grpc"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"loafle.com/overflow/redis_protocol_crawler_go"
|
||||
"time"
|
||||
"loafle.com/overflow/crawler_go"
|
||||
pb "loafle.com/overflow/crawler_go/grpc"
|
||||
"loafle.com/overflow/redis_protocol_crawler_go"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
|
||||
type call func() crawler.Crawler
|
||||
|
||||
func startRPCServerTest(cc call) {
|
||||
rr:=cc()
|
||||
AddDelegate(pb.Crawlers_HEALTH_REDIS.String(),rr)
|
||||
go start(&ConfigServer{},&DataServer{})
|
||||
rr := cc()
|
||||
AddDelegate(pb.Crawlers_HEALTH_REDIS.String(), rr)
|
||||
go start(&ConfigServer{}, &DataServer{})
|
||||
}
|
||||
|
||||
func clientCall(t *testing.T,cl pb.Crawlers) {
|
||||
func clientCall(t *testing.T, cl pb.Crawlers) {
|
||||
time.Sleep(2 * time.Second)
|
||||
conn, err := grpc.Dial("localhost:50000", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
|
@ -31,25 +29,24 @@ func clientCall(t *testing.T,cl pb.Crawlers) {
|
|||
}
|
||||
defer conn.Close()
|
||||
c := pb.NewDataClient(conn)
|
||||
|
||||
|
||||
in := &pb.Input{
|
||||
Name: cl,
|
||||
Id:"test_redis_sid",
|
||||
Id: "test_redis_sid",
|
||||
}
|
||||
|
||||
|
||||
out, err := c.Get(context.Background(), in)
|
||||
if err != nil {
|
||||
log.Fatalf("could not greet: %v", err)
|
||||
}
|
||||
|
||||
|
||||
var check bool
|
||||
json.Unmarshal(out.Data, &check)
|
||||
assert.Equal(t, true, check)
|
||||
}
|
||||
|
||||
|
||||
func TestRedisGRPC(t *testing.T) {
|
||||
go startRPCServerTest( func() crawler.Crawler {
|
||||
go startRPCServerTest(func() crawler.Crawler {
|
||||
rr := redis_protocol_crawler_go.NewRedisHeahthCrawler()
|
||||
m := make(map[string]interface{}, 0)
|
||||
m["ip"] = "192.168.1.104"
|
||||
|
@ -59,7 +56,7 @@ func TestRedisGRPC(t *testing.T) {
|
|||
rr.PutConfig("test_redis_sid", m)
|
||||
return rr
|
||||
})
|
||||
clientCall(t,pb.Crawlers_HEALTH_REDIS)
|
||||
clientCall(t, pb.Crawlers_HEALTH_REDIS)
|
||||
}
|
||||
|
||||
//type configcall func()
|
||||
|
|
Loading…
Reference in New Issue
Block a user