Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
"code.fbi.h-da.de/danet/gosdn/application-framework/event"
"google.golang.org/grpc"
)
// Application is an example for a sdn application
type Application struct {
eventService event.ServiceInterface
stopChannel chan os.Signal
grpcClientConn *grpc.ClientConn
}
// Run runs the application
func (a *Application) Run() {
signal.Notify(a.stopChannel, os.Interrupt, syscall.SIGTERM)
a.eventService.SubscribeToTopic("add", a.callback)
a.eventService.SetupEventReciever(a.stopChannel)
conn, err := grpc.Dial("localhost:55055", grpc.WithInsecure())
if err != nil {
panic(err)
}
a.grpcClientConn = conn
var forever chan struct{}
go func() {
for {
select {
case <-a.stopChannel:
close(forever)
a.grpcClientConn.Close()
return
}
}
}()
<-forever
}
func (a *Application) callback(event *event.Event) {
fmt.Printf("Event Callback: %+v \n", event)
ctx := context.Background()
userService := rbac.NewUserServiceClient(a.grpcClientConn)
request := &rbac.GetUserRequest{
Timestamp: time.Now().UnixNano(),
Name: event.ID.String(),
}
response, err := userService.GetUser(ctx, request)
if err != nil {
fmt.Printf("Error %+v\n ", err)
return
}
fmt.Printf("ID: %v, Name: %v \n", response.User.Id, response.User.Name)
for key, elem := range response.User.Roles {
fmt.Printf("Role on PND: %v %v \n", key, elem)
}