1
1
package main
2
2
3
3
import (
4
- "context"
5
- "encoding/json"
4
+ "bufio"
6
5
"fmt"
7
6
"log"
8
7
"os"
9
8
10
- "github.com/jalapeno-api-gateway/protorepo-jagw-go/jagw"
11
9
"google.golang.org/grpc"
12
10
"google.golang.org/grpc/credentials/insecure"
11
+ "github.com/jalapeno-api-gateway/demo-sr-app/fetch"
12
+ "github.com/jalapeno-api-gateway/protorepo-jagw-go/jagw"
13
13
)
14
14
15
15
func main () {
16
- // Get Endpoint as <ip-address>:<port> using the parameters passed by the user
17
- endpoint := fmt .Sprintf ("%s:%s" , os .Args [1 ], os .Args [2 ])
16
+ log .Print ("Starting SR-App." )
17
+
18
+ // Get Endpoints
19
+ rsEndpoint := getRequestServiceEndpoint ()
20
+ ssEndpoint := getSubscriptionServiceEndpoint ()
18
21
19
22
// Setup Request Service Connection
20
- var connection * grpc.ClientConn
21
- connection , err := grpc .Dial (endpoint , grpc .WithTransportCredentials (insecure .NewCredentials ()))
22
- if err != nil {
23
- log .Fatalf ("Failed to setup request service connection: %s" , err )
23
+ var rsConnection * grpc.ClientConn
24
+ rsConnection , rsErr := grpc .Dial (rsEndpoint , grpc .WithTransportCredentials (insecure .NewCredentials ()))
25
+ if rsErr != nil {
26
+ log .Fatalf ("Failed to setup request service connection: %s" , rsErr )
24
27
}
25
- defer connection .Close ()
28
+ defer rsConnection .Close ()
26
29
27
- // Create Client
28
- client := jagw .NewRequestServiceClient (connection )
29
-
30
- // Request all Nodes
31
- request := & jagw.TopologyRequest {
32
- Keys : []string {},
33
- Properties : []string {},
30
+ // Setup Subscription Service Connection
31
+ var ssConnection * grpc.ClientConn
32
+ ssConnection , ssErr := grpc .Dial (ssEndpoint , grpc .WithTransportCredentials (insecure .NewCredentials ()))
33
+ if ssErr != nil {
34
+ log .Fatalf ("Failed to setup subscription service connection: %s" , ssErr )
34
35
}
36
+ defer ssConnection .Close ()
35
37
36
- response , err := client .GetLsNodes (context .Background (), request )
37
- if err != nil {
38
- log .Fatalf ("Error when calling GetLsNodes on request service: %s" , err )
39
- }
38
+ // Create Clients
39
+ rsClient := jagw .NewRequestServiceClient (rsConnection )
40
+ ssClient := jagw .NewSubscriptionServiceClient (ssConnection )
40
41
41
- prettyPrint (response )
42
+ // Demo requests and subscriptions
43
+ makeTopologyRequests (rsClient )
44
+ makeTelemetryRequests (rsClient )
45
+ makeTopologySubscriptions (ssClient )
46
+ makeTelemetrySubscriptions (ssClient )
42
47
}
43
48
44
- func prettyPrint (any interface {}) {
45
- s , _ := json .MarshalIndent (any , "" , " " )
46
- fmt .Printf ("%s\n \n " , string (s ))
49
+ func getRequestServiceEndpoint () string {
50
+ serverAddress := os .Args [1 ]
51
+ requestServicePort := os .Args [2 ]
52
+ return fmt .Sprintf ("%s:%s" , serverAddress , requestServicePort ) // Returns Endpoint as <ip-address>:<port>
53
+ }
54
+
55
+ func getSubscriptionServiceEndpoint () string {
56
+ serverAddress := os .Args [1 ]
57
+ subscriptionServicePort := os .Args [3 ]
58
+ return fmt .Sprintf ("%s:%s" , serverAddress , subscriptionServicePort ) // Returns Endpoint as <ip-address>:<port>
59
+ }
60
+
61
+ func makeTopologyRequests (rsClient jagw.RequestServiceClient ) {
62
+ // Just for demo purposes:
63
+ // Use as stdin scanner to wait for user input before continuing to next request
64
+ input := bufio .NewScanner (os .Stdin )
65
+
66
+ // Make requests
67
+ fmt .Print ("Press 'Enter' to: REQUEST ALL NODES" )
68
+ input .Scan ()
69
+ fetch .GetAllNodes (rsClient )
70
+
71
+ fmt .Print ("Press 'Enter' to: REQUEST SPECIFIC NODES AND SPECIFIC PROPERTIES" )
72
+ input .Scan ()
73
+ fetch .GetSpecificNodes (rsClient )
47
74
}
75
+
76
+ func makeTelemetryRequests (rsClient jagw.RequestServiceClient ) {
77
+ // Just for demo purposes:
78
+ // Use as stdin scanner to wait for user input before continuing to next request
79
+ input := bufio .NewScanner (os .Stdin )
80
+
81
+ // Make requests
82
+ fmt .Print ("Press 'Enter' to: REQUEST DATA RATES OF PAST 60 SECONDS OF SPECIFIC NODE" )
83
+ input .Scan ()
84
+ fetch .GetDataRatesOfSpecificNode (rsClient )
85
+ }
86
+
87
+ func makeTopologySubscriptions (ssClient jagw.SubscriptionServiceClient ) {
88
+ // Just for demo purposes:
89
+ // Use as stdin scanner to wait for user input before continuing to next request
90
+ input := bufio .NewScanner (os .Stdin )
91
+
92
+ // Make subscriptions
93
+ fmt .Print ("Press 'Enter' to: SUBSCRIBE TO ALL LINKS" )
94
+ input .Scan ()
95
+ fetch .SubscribeToAllLinks (ssClient )
96
+
97
+ fmt .Print ("Press 'Enter' to: SUBSCRIBE TO SPECIFIC LINK" )
98
+ input .Scan ()
99
+ fetch .SubscribeToSpecificLink (ssClient )
100
+ }
101
+
102
+ func makeTelemetrySubscriptions (ssClient jagw.SubscriptionServiceClient ) {
103
+ // Just for demo purposes:
104
+ // Use as stdin scanner to wait for user input before continuing to next request
105
+ input := bufio .NewScanner (os .Stdin )
106
+
107
+ // Make subscriptions
108
+ fmt .Print ("Press 'Enter' to: SUBSCRIBE TO TELEMETRY DATA OF SPECIFIC NODE" )
109
+ input .Scan ()
110
+ fetch .SubscribeToTelemetryDataOfSpecificNode (ssClient )
111
+ }
0 commit comments