Skip to content

Commit 3d0411c

Browse files
authored
Merge pull request #51 from oslabs-beta/grpc-nested-testing
gRPC unary nested request and nested sever response and gitignore update
2 parents c68835a + e9d74ba commit 3d0411c

File tree

5 files changed

+73
-107
lines changed

5 files changed

+73
-107
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ ehthumbs.db
5555
# Windows shortcuts #
5656
#####################
5757
*.lnk
58+
59+
# proto files #
60+
#####################
61+
*.proto

grpc_mockData/protos/hw2.proto

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package helloworld;
55
// The greeting service definition.
66
service Greeter {
77
// Sends a greeting
8-
rpc SayHello (HelloRequest) returns (HelloReply) {}
8+
rpc SayHello (HelloRequest) returns (HelloReply) {} // single unary stream
9+
rpc SayHelloNested (HelloNestedRequest) returns (HelloNestedReply) {} // nested unary stream
910
rpc SayHelloCS (stream HelloRequest) returns (HelloReply) {}
1011
rpc SayHellos (HelloRequest) returns (stream HelloReply) {}
1112
rpc SayHelloBidi (stream HelloRequest) returns (stream HelloReply) {}
@@ -21,6 +22,16 @@ message HelloReply {
2122
string message = 1;
2223
}
2324

25+
message HelloNestedRequest {
26+
HelloRequest firstPerson = 1;
27+
HelloRequest secondPerson = 2;
28+
}
29+
30+
message HelloNestedReply {
31+
// create array of nested server response
32+
repeated HelloReply serverMessage = 1;
33+
34+
}
2435
// The request message containing the user's name.
2536
message HelloHowOldRequest {
2637
int32 age = 1;

grpc_mockData/server.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ function sayHello(ctx) {
3535
metadata.set('indeed', 'it do')
3636
// Watcher creates a watch execution context for the watch
3737
// The execution context provides scripts and templates with access to the watch metadata
38-
console.log("received metadata from client request", ctx.meta)
39-
// console.dir(ctx.metadata, { depth: 3, colors: true });
38+
console.log("received metadata from client request", ctx.metadata)
39+
console.dir(ctx.metadata, { depth: 3, colors: true });
4040
console.log(`got sayHello request name: ${ctx.req.name}`);
41-
41+
4242
// an alias to ctx.response.res
4343
// This is set only in case of DUPLEX calls, to the the gRPC call reference itself
4444
ctx.res = { message: "Hello " + ctx.req.name };
@@ -49,7 +49,28 @@ function sayHello(ctx) {
4949

5050
console.log(`set sayHello response: ${ctx.res.message}`);
5151
}
52+
// nested Unary stream
53+
54+
function sayHelloNested(ctx) {
55+
// create new metadata
56+
let metadata = new grpc.Metadata();
57+
metadata.set('it', 'works?')
58+
metadata.set('indeed', 'it do')
59+
// Watcher creates a watch execution context for the watch
60+
// The execution context provides scripts and templates with access to the watch metadata
61+
console.log("received metadata from client request", ctx.metadata)
62+
console.dir(ctx.metadata, { depth: 3, colors: true });
63+
// console.log("ctx line 64 from server.js", ctx)
64+
65+
// nested unary response call
66+
let firstPerson = ctx.req.firstPerson.name;
67+
let secondPerson = ctx.req.secondPerson.name;
68+
console.log("firstPerson line 68 from server.js:", firstPerson)
69+
ctx.res = {"serverMessage": [{message: "Hello! " + firstPerson}, {message: 'Hello! ' + secondPerson}]}
5270

71+
// send response header metadata object directly as an argument and that is set and sent
72+
ctx.sendMetadata(metadata)
73+
}
5374
// Server-Side Stream
5475
// used highland library to manage asynchronous data
5576
async function sayHellos(ctx) {
@@ -64,7 +85,9 @@ async function sayHellos(ctx) {
6485

6586
// alias for ctx.request.req
6687
// In case of UNARY and RESPONSE_STREAM calls it is simply the gRPC call's request
88+
6789
let reqMessages = {"message": 'hello!!! ' + ctx.req.name}
90+
6891
dataStream.push(reqMessages)
6992
reqMessages = dataStream
7093
let streamData = await hl(reqMessages)
@@ -80,6 +103,7 @@ async function sayHellos(ctx) {
80103
ctx.res.end()
81104
}
82105

106+
83107
// Client-Side stream
84108
function sayHelloCs (ctx) {
85109
// create new metadata
@@ -146,7 +170,7 @@ function sayHelloBidi(ctx) {
146170
*/
147171
function main() {
148172
const app = new Mali(PROTO_PATH, "Greeter");
149-
app.use({ sayHello, sayHellos, sayHelloCs, sayHelloBidi });
173+
app.use({ sayHello, sayHelloNested, sayHellos, sayHelloCs, sayHelloBidi });
150174
app.start(HOSTPORT);
151175
console.log(`Greeter service running @ ${HOSTPORT}`);
152176
}
Lines changed: 26 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,39 @@
1-
2-
// Copyright 2015 gRPC authors.
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
161
syntax = "proto3";
172

18-
option java_multiple_files = true;
19-
option java_package = "io.grpc.examples.routeguide";
20-
option java_outer_classname = "RouteGuideProto";
21-
option objc_class_prefix = "RTG";
22-
23-
package routeguide;
24-
25-
// Interface exported by the server.
26-
service RouteGuide {
27-
// A simple RPC.
28-
//
29-
// Obtains the feature at a given position.
30-
//
31-
// A feature with an empty name is returned if there's no feature at the given
32-
// position.
33-
rpc GetFeature(Point) returns (Feature) {}
34-
35-
// A server-to-client streaming RPC.
36-
//
37-
// Obtains the Features available within the given Rectangle. Results are
38-
// streamed rather than returned at once (e.g. in a response message with a
39-
// repeated field), as the rectangle may cover a large area and contain a
40-
// huge number of features.
41-
rpc ListFeatures(Rectangle) returns (stream Feature) {}
3+
package helloworld;
424

43-
// A client-to-server streaming RPC.
44-
//
45-
// Accepts a stream of Points on a route being traversed, returning a
46-
// RouteSummary when traversal is completed.
47-
rpc RecordRoute(stream Point) returns (RouteSummary) {}
48-
49-
// A Bidirectional streaming RPC.
50-
//
51-
// Accepts a stream of RouteNotes sent while a route is being traversed,
52-
// while receiving other RouteNotes (e.g. from other users).
53-
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
5+
// The greeting service definition.
6+
service Greeter {
7+
// Sends a greeting
8+
// rpc SayHello (HelloRequest) returns (HelloReply) {}
9+
rpc SayHello (HelloNestedRequest) returns (HelloNestedReply) {}
10+
rpc SayHelloCS (stream HelloRequest) returns (HelloReply) {}
11+
rpc SayHellos (HelloRequest) returns (stream HelloReply) {}
12+
rpc SayHelloBidi (stream HelloRequest) returns (stream HelloReply) {}
5413
}
5514

56-
// Points are represented as latitude-longitude pairs in the E7 representation
57-
// (degrees multiplied by 10**7 and rounded to the nearest integer).
58-
// Latitudes should be in the range +/- 90 degrees and longitude should be in
59-
// the range +/- 180 degrees (inclusive).
60-
message Point {
61-
int32 latitude = 1;
62-
int32 longitude = 2;
15+
// The request message containing the user's name.
16+
message HelloRequest {
17+
string name = 1;
6318
}
6419

65-
// A latitude-longitude rectangle, represented as two diagonally opposite
66-
// points "lo" and "hi".
67-
message Rectangle {
68-
// One corner of the rectangle.
69-
Point lo = 1;
70-
71-
// The other corner of the rectangle.
72-
Point hi = 2;
20+
// The response message containing the greetings
21+
message HelloReply {
22+
string message = 1;
7323
}
7424

75-
// A feature names something at a given point.
76-
//
77-
// If a feature could not be named, the name is empty.
78-
message Feature {
79-
// The name of the feature.
80-
string name = 1;
81-
82-
// The point where the feature is detected.
83-
Point location = 2;
25+
message HelloNestedRequest {
26+
HelloRequest firstPerson = 1;
27+
HelloRequest secondPerson = 2;
8428
}
8529

86-
// A RouteNote is a message sent while at a given point.
87-
message RouteNote {
88-
// The location from which the message is sent.
89-
Point location = 1;
90-
91-
// The message to be sent.
92-
string message = 2;
30+
message HelloNestedReply {
31+
repeated HelloReply serverMessage = 1;
9332
}
94-
95-
// A RouteSummary is received in response to a RecordRoute rpc.
96-
//
97-
// It contains the number of individual points received, the number of
98-
// detected features, and the total distance covered as the cumulative sum of
99-
// the distance between each point.
100-
message RouteSummary {
101-
// The number of points received.
102-
int32 point_count = 1;
103-
104-
// The number of known features passed while traversing the route.
105-
int32 feature_count = 2;
106-
107-
// The distance covered in metres.
108-
int32 distance = 3;
109-
110-
// The duration of the traversal in seconds.
111-
int32 elapsed_time = 4;
33+
// The request message containing the user's name.
34+
message HelloHowOldRequest {
35+
int32 age = 1;
36+
}
37+
message HelloAge {
38+
int32 age = 1;
11239
}
113-

src/client/controllers/grpcController.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,17 @@ grpcController.openGrpcConnection = (reqResObj, connectionArray) => {
112112
}
113113

114114
if (rpcType === 'UNARY') {
115-
let query = reqResObj.queryArr[0];
115+
let query = reqResObj.queryArr[0]
116+
116117
// Open Connection and set time sent for Unary
117118
reqResObj.connection = 'open';
118119
reqResObj.timeSent = Date.now();
119120
// make Unary call
120121
client[rpc](query, meta, (err, data)=> {
122+
console.log("query from line 122 in grpcController", query)
121123
if (err) {
122124
console.log('unary error' , err);
123125
}
124-
// console.log('sent UNARY request', data);
125126
// Close Connection and set time received for Unary
126127
reqResObj.timeReceived = Date.now();
127128
reqResObj.connection = 'closed';

0 commit comments

Comments
 (0)