Commit a4f088fc authored by Richer Maximilien's avatar Richer Maximilien Committed by Loïck Bonniot

[d/api] Implement demonstrator's API service

parent c10902cd
Pipeline #235 passed with stage
......@@ -28,6 +28,7 @@ Unit tests:
- "go test -coverprofile dfssp_user.part -v dfss/dfssp/user"
- "go test -coverprofile dfssp_contract.part -v dfss/dfssp/contract"
- "go test -coverprofile dfssp_templates.part -v dfss/dfssp/templates"
- "go test -coverprofile dfssd.part -v dfss/dfssd"
- "echo 'mode: set' *part > c.out"
- "grep -h -v 'mode: set' *part >> c.out"
- "go tool cover -html=c.out -o coverage.html"
......@@ -47,6 +48,7 @@ ARM tests:
- "go test -cover -short -v dfss/dfssp/user"
- "go test -cover -short -v dfss/dfssp/contract"
- "go test -cover -short -v dfss/dfssp/templates"
- "go test -cover -short -v dfss/dfssd"
Code lint:
stage: test
......
all:
protoc --go_out=plugins=grpc:. demonstrator.proto
package api
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"time"
)
var (
// theses are the default parameters
address = "localhost:3000"
identifier = "platform"
demo = false
// lazy initializer
dial *grpc.ClientConn
demoClient DemonstratorClient
)
// Switch for demo mode
//
// Should be used to pass the value of `-d` switch
func Switch(activationSwitch bool) {
demo = activationSwitch
return
}
// Lazy initialisation for demonstrator's connection to server
func dInit() error {
var err error
dial, err = grpc.Dial(address, grpc.WithInsecure())
if err != nil {
grpclog.Printf("Fail to dial: %v", err)
}
demoClient = NewDemonstratorClient(dial)
return err
}
// DClose close the connection with demonstrator server (if any)
//
// This should be called at the end of any program that import this library
func DClose() {
if dial != nil {
err := dial.Close()
if err != nil {
grpclog.Printf("Fail to close dialing: %v", err)
}
}
}
// DLog send a message to the demonstrator
//
// The client is dialed in a lazy way
// The default demonstrator server address is localhost:3000
func DLog(log string) {
// check demo switch
if !demo {
return
}
// lazy initialisation
if dial == nil {
err := dInit()
if err != nil {
return // fail silently
}
}
_, err := demoClient.SendLog(
context.Background(),
&Log{Timestamp: time.Now().UnixNano(), Identifier: identifier, Log: log})
if err != nil {
grpclog.Printf("Fail to send message: %v", err)
}
}
// Code generated by protoc-gen-go.
// source: demonstrator.proto
// DO NOT EDIT!
/*
Package api is a generated protocol buffer package.
It is generated from these files:
demonstrator.proto
It has these top-level messages:
Log
Ack
*/
package api
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// Log message to display information
type Log struct {
Timestamp int64 `protobuf:"varint,1,opt,name=timestamp" json:"timestamp,omitempty"`
Identifier string `protobuf:"bytes,2,opt,name=identifier" json:"identifier,omitempty"`
Log string `protobuf:"bytes,3,opt,name=log" json:"log,omitempty"`
}
func (m *Log) Reset() { *m = Log{} }
func (m *Log) String() string { return proto.CompactTextString(m) }
func (*Log) ProtoMessage() {}
func (*Log) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
// Empty ack message
type Ack struct {
}
func (m *Ack) Reset() { *m = Ack{} }
func (m *Ack) String() string { return proto.CompactTextString(m) }
func (*Ack) ProtoMessage() {}
func (*Ack) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func init() {
proto.RegisterType((*Log)(nil), "api.Log")
proto.RegisterType((*Ack)(nil), "api.Ack")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// Client API for Demonstrator service
type DemonstratorClient interface {
// Log message.
//
// Send the UnixNano timetamp, sender's identifier and log message
// Returns nothing ?
SendLog(ctx context.Context, in *Log, opts ...grpc.CallOption) (*Ack, error)
}
type demonstratorClient struct {
cc *grpc.ClientConn
}
func NewDemonstratorClient(cc *grpc.ClientConn) DemonstratorClient {
return &demonstratorClient{cc}
}
func (c *demonstratorClient) SendLog(ctx context.Context, in *Log, opts ...grpc.CallOption) (*Ack, error) {
out := new(Ack)
err := grpc.Invoke(ctx, "/api.Demonstrator/SendLog", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Demonstrator service
type DemonstratorServer interface {
// Log message.
//
// Send the UnixNano timetamp, sender's identifier and log message
// Returns nothing ?
SendLog(context.Context, *Log) (*Ack, error)
}
func RegisterDemonstratorServer(s *grpc.Server, srv DemonstratorServer) {
s.RegisterService(&_Demonstrator_serviceDesc, srv)
}
func _Demonstrator_SendLog_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
in := new(Log)
if err := dec(in); err != nil {
return nil, err
}
out, err := srv.(DemonstratorServer).SendLog(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
var _Demonstrator_serviceDesc = grpc.ServiceDesc{
ServiceName: "api.Demonstrator",
HandlerType: (*DemonstratorServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SendLog",
Handler: _Demonstrator_SendLog_Handler,
},
},
Streams: []grpc.StreamDesc{},
}
var fileDescriptor0 = []byte{
// 148 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x49, 0xcd, 0xcd,
0xcf, 0x2b, 0x2e, 0x29, 0x4a, 0x2c, 0xc9, 0x2f, 0xd2, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62,
0x4e, 0x2c, 0xc8, 0x54, 0xb2, 0xe4, 0x62, 0xf6, 0xc9, 0x4f, 0x17, 0x12, 0xe4, 0xe2, 0x2c, 0xc9,
0xcc, 0x4d, 0x2d, 0x2e, 0x49, 0xcc, 0x2d, 0x90, 0x60, 0x54, 0x60, 0xd4, 0x60, 0x16, 0x12, 0xe2,
0xe2, 0xca, 0x4c, 0x49, 0xcd, 0x2b, 0xc9, 0x4c, 0xcb, 0x4c, 0x2d, 0x92, 0x60, 0x02, 0x8a, 0x71,
0x0a, 0x71, 0x73, 0x31, 0xe7, 0xe4, 0xa7, 0x4b, 0x30, 0x83, 0x38, 0x4a, 0xac, 0x5c, 0xcc, 0x8e,
0xc9, 0xd9, 0x46, 0xfa, 0x5c, 0x3c, 0x2e, 0x48, 0x86, 0x0b, 0xc9, 0x73, 0xb1, 0x07, 0xa7, 0xe6,
0xa5, 0x80, 0x4c, 0xe5, 0xd0, 0x03, 0x5a, 0xa1, 0x07, 0x64, 0x49, 0x41, 0x58, 0x40, 0xe5, 0x4a,
0x0c, 0x49, 0x6c, 0x60, 0xeb, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x0a, 0xd5, 0xa7, 0xa4,
0x94, 0x00, 0x00, 0x00,
}
syntax = "proto3";
package api;
service Demonstrator {
// Log message.
//
// Send the UnixNano timetamp, sender's identifier and log message
// Returns nothing ?
rpc SendLog(Log) returns (Ack) {}
}
// Log message to display information
message Log {
int64 timestamp = 1;
string identifier = 2;
string log = 3;
}
// Empty ack message
message Ack {
}
package main
import (
dapi "dfss/dfssd/api"
"testing"
)
// Test for a client-server dialing
//
// Cannot add output statement because output includes timestamp
func TestServerAndClient(t *testing.T) {
// Start a server
go func() {
err := listen("localhost:3000")
if err != nil {
t.Error("Unable to start server")
}
}()
// Start a client
go func() {
defer dapi.DClose()
// this one fails silently, so you can't really test it
dapi.DLog("This is a log message from a client")
}()
}
......@@ -4,6 +4,7 @@ import (
"dfss"
"flag"
"fmt"
"os"
"runtime"
)
......@@ -25,6 +26,7 @@ func init() {
fmt.Println("\nThe commands are:")
fmt.Println(" help print this help")
fmt.Println(" version print dfss client version")
fmt.Println(" start start demonstrator server")
fmt.Println("\nFlags:")
flag.PrintDefaults()
......@@ -40,6 +42,11 @@ func main() {
switch command {
case "version":
fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
case "start":
err := listen("localhost:3000")
if err != nil {
os.Exit(1)
}
default:
flag.Usage()
}
......
package main
import (
api "dfss/dfssd/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"log"
"net"
)
type server struct{}
// Sendlog Handler
//
// Handle incoming log messages
func (s *server) SendLog(ctx context.Context, in *api.Log) (*api.Ack, error) {
// TODO send message to log management
log.Printf("[%d] %s:: %s", in.Timestamp, in.Identifier, in.Log)
return &api.Ack{}, nil
}
// Listen with gRPG service
func listen(addrPort string) error {
// open tcp socket
lis, err := net.Listen("tcp", addrPort)
if err != nil {
grpclog.Fatalf("Failed to open tcp socket: %v", err)
return err
}
log.Printf("Server listening on %s", addrPort)
// bootstrap gRPC service !
grpcServer := grpc.NewServer()
api.RegisterDemonstratorServer(grpcServer, &server{})
err = grpcServer.Serve(lis)
return err
}
......@@ -2,6 +2,7 @@ package main
import (
"dfss"
dapi "dfss/dfssd/api"
"dfss/dfssp/authority"
"dfss/dfssp/server"
"dfss/net"
......@@ -12,7 +13,7 @@ import (
)
var (
verbose bool
verbose, demo bool
path, country, org, unit, cn, port, address, dbURI string
keySize, validity int
)
......@@ -20,6 +21,7 @@ var (
func init() {
flag.BoolVar(&verbose, "v", false, "Print verbose messages")
flag.BoolVar(&demo, "d", false, "Enable demonstrator")
flag.StringVar(&port, "p", "9000", "Default port listening")
flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening")
......@@ -60,6 +62,7 @@ func init() {
func main() {
flag.Parse()
command := flag.Arg(0)
dapi.Switch(demo)
switch command {
case "version":
......@@ -70,9 +73,11 @@ func main() {
fmt.Println("An error occured during the initialization operation:", err)
os.Exit(1)
}
dapi.DLog("Private key generated !")
case "start":
srv := server.GetServer(path, dbURI, verbose)
fmt.Println("Listening on " + address + ":" + port)
dapi.DLog("Platform server started on " + address + ":" + port)
err := net.Listen(address+":"+port, srv)
if err != nil {
fmt.Println(err)
......@@ -80,4 +85,6 @@ func main() {
default:
flag.Usage()
}
dapi.DClose()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment