Skip to content

Commit 8ec1824

Browse files
committed
fixed history for gRPC
2 parents 0f9a3ce + fc8b350 commit 8ec1824

File tree

8 files changed

+115
-172
lines changed

8 files changed

+115
-172
lines changed

grpc_mockData/client.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ function main() {
1717
"localhost:50051",
1818
grpc.credentials.createInsecure()
1919
);
20+
// set request value
2021
// CLI prompt or hard code the variable for the message "name": "string"
2122
let user;
23+
// returns an array containing the command line arguments passed when the Node.js process was launched
24+
// starts on 2 because process.argv contains the whole command-line invocation
25+
// argv[0] and argv[1] will be node example.js
2226
if (process.argv.length >= 3) {
2327
user = process.argv[2];
2428
} else {
2529
user = "world";
2630
}
31+
32+
// create metadata
2733
const meta = new grpc.Metadata();
2834
meta.add("testing", "metadata is working");
2935

@@ -45,19 +51,22 @@ function main() {
4551
} else {
4652
console.log(response);
4753
}
48-
// client.close();
49-
});
50-
stream.write({ name: "hello first stream" });
51-
stream.write({ name: "hello 2nd stream" });
52-
stream.end({ name: "hello end stream" });
53-
54+
client.close();
55+
})
56+
stream.write({ name: 'hello 1st client side stream'});
57+
stream.write({name: 'hello 2nd client side stream' })
58+
// ends client streaming
59+
stream.end({ name: 'hello end client side stream'})
60+
5461
// bidi streaming
5562
const streamBidi = client.sayHelloBidi(meta);
63+
// reads streaming data
5664
streamBidi.on("error", console.error);
5765
streamBidi.on("data", console.log);
5866
streamBidi.on("end", () => client.close());
59-
60-
streamBidi.write({ name: "hello bidi stream 1" });
61-
streamBidi.end({ name: "hello bidi stream 2" });
67+
// write data
68+
streamBidi.write ({ name: 'hello 1st bi-di stream'});
69+
// ends data
70+
streamBidi.end({ name: 'hello 2nd bi-di stream'})
6271
}
6372
main();

grpc_mockData/server.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,73 @@ const dataStream = [
2626
* Implements the SayHello RPC method.
2727
*/
2828

29-
29+
// Unary stream
30+
// ctx = watch execution context
3031
function sayHello(ctx) {
32+
// create new metadata
3133
let metadata = new grpc.Metadata();
3234
metadata.set('it', 'works?')
3335
metadata.set('indeed', 'it do')
34-
console.dir(ctx.metadata, { depth: 3, colors: true });
36+
// Watcher creates a watch execution context for the watch
37+
// The execution context provides scripts and templates with access to the watch metadata
38+
console.log("received metadata from client request", ctx.metadata)
39+
// console.dir(ctx.metadata, { depth: 3, colors: true });
3540
console.log(`got sayHello request name: ${ctx.req.name}`);
41+
42+
// an alias to ctx.response.res
43+
// This is set only in case of DUPLEX calls, to the the gRPC call reference itself
3644
ctx.res = { message: "Hello " + ctx.req.name };
37-
// let metadata = new grpc.Metadata();
38-
// metadata.set('it', 'does?')
39-
metadata.set('UNARY', 'yes')
4045

46+
// send response header metadata object directly as an argument and that is set and sent
47+
metadata.set('UNARY', 'yes')
4148
ctx.sendMetadata(metadata)
49+
4250
console.log(`set sayHello response: ${ctx.res.message}`);
4351
}
4452

53+
// Server-Side Stream
54+
// used highland library to manage asynchronous data
4555
async function sayHellos(ctx) {
56+
// create new metadata
4657
let metadata = new grpc.Metadata();
4758
metadata.set('it', 'works?')
4859
metadata.set('indeed', 'it do')
60+
// The execution context provides scripts and templates with access to the watch metadata
4961
console.dir(ctx.metadata, { depth: 3, colors: true });
62+
// converts a request into strings
5063
console.log(`got sayHellos request name:`, JSON.stringify(ctx.req, null, 4));
64+
65+
// alias for ctx.request.req
66+
// In case of UNARY and RESPONSE_STREAM calls it is simply the gRPC call's request
5167
let reqMessages = {"message": 'hello!!! ' + ctx.req.name}
5268
dataStream.push(reqMessages)
5369
reqMessages = dataStream
54-
console.log('what is this?????', reqMessages)
5570
let streamData = await hl(reqMessages)
5671
ctx.res = streamData;
5772
metadata.set('serverStream', 'indeed')
5873
dataStream.pop()
5974

75+
// send response header metadata object directly as an argument and that is set and sent
6076
ctx.sendMetadata(metadata)
6177

6278
console.log(`done sayHellos`);
79+
// ends server stream
6380
ctx.res.end()
64-
// dataStream = [];
6581
}
6682

83+
// Client-Side stream
6784
function sayHelloCs (ctx) {
85+
// create new metadata
6886
let metadata = new grpc.Metadata();
6987
metadata.set('it', 'works?')
7088
metadata.set('indeed', 'it do')
7189
metadata.set('clientStream', 'indubitably')
90+
// The execution context provides scripts and templates with access to the watch metadata
7291
console.dir(ctx.metadata, { depth: 3, colors: true })
7392
console.log('got sayHelloClients')
74-
let counter = 0
75-
// console.log("ctx content:",ctx.req)
93+
let counter = 0;
7694
let messages = [];
95+
// client streaming calls to write messages and end writing before you can get the response
7796
return new Promise((resolve, reject) => {
7897
hl(ctx.req)
7998
.map(message => {
@@ -82,10 +101,8 @@ function sayHelloCs (ctx) {
82101
ctx.response.res = { message: 'Client stream: ' + message.name }
83102
messages.push(message.name)
84103
ctx.sendMetadata(metadata)
85-
86-
87-
88104
})
105+
// returns all the elements as an array
89106
.collect()
90107
.toCallback((err, result) => {
91108
if (err) return reject(err)
@@ -97,11 +114,14 @@ function sayHelloCs (ctx) {
97114
})
98115
}
99116

117+
// Bi-Di stream
100118
function sayHelloBidi(ctx) {
119+
// create new metadata
101120
let metadata = new grpc.Metadata();
102121
metadata.set('it', 'works?')
103122
metadata.set('indeed', 'it do')
104123
console.log("got sayHelloBidi");
124+
// The execution context provides scripts and templates with access to the watch metadata
105125
console.dir(ctx.metadata, { depth: 3, colors: true });
106126
let counter = 0;
107127
ctx.req.on("data", d => {
@@ -112,9 +132,10 @@ function sayHelloBidi(ctx) {
112132
});
113133
metadata.set('bidiStream', 'ohyes')
114134
ctx.sendMetadata(metadata);
135+
// calls end to client before closing server
115136
ctx.req.on("end", () => {
116137
console.log(`done sayHelloBidi counter ${counter}`);
117-
138+
// ends server stream
118139
ctx.res.end();
119140
});
120141
}

src/client/components/composer/NewRequest/GRPCAutoInputForm.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React, { Component } from 'react';
22
import dropDownArrow from '../../../../assets/icons/arrow_drop_down_white_192x192.png'
33
import GRPCBodyEntryForm from "./GRPCBodyEntryForm.jsx";
4-
import { VariablesInAllowedPositionRule } from 'graphql/validation';
5-
import ReactDOM from "react-dom";
64

75
class GRPCAutoInputForm extends Component {
86
constructor(props) {
Lines changed: 19 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,29 @@
1-
// Copyright 2015 gRPC authors.
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
151
syntax = "proto3";
162

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

42-
// A client-to-server streaming RPC.
43-
//
44-
// Accepts a stream of Points on a route being traversed, returning a
45-
// RouteSummary when traversal is completed.
46-
rpc RecordRoute(stream Point) returns (RouteSummary) {}
47-
48-
// A Bidirectional streaming RPC.
49-
//
50-
// Accepts a stream of RouteNotes sent while a route is being traversed,
51-
// while receiving other RouteNotes (e.g. from other users).
52-
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
53-
}
54-
55-
// Points are represented as latitude-longitude pairs in the E7 representation
56-
// (degrees multiplied by 10**7 and rounded to the nearest integer).
57-
// Latitudes should be in the range +/- 90 degrees and longitude should be in
58-
// the range +/- 180 degrees (inclusive).
59-
message Point {
60-
int32 latitude = 1;
61-
int32 longitude = 2;
5+
// The greeting service definition.
6+
service Greeter {
7+
// Sends a greeting
8+
rpc SayHello (HelloRequest) returns (HelloReply) {}
9+
rpc SayHelloCS (stream HelloRequest) returns (HelloReply) {}
10+
rpc SayHellos (HelloRequest) returns (stream HelloReply) {}
11+
rpc SayHelloBidi (stream HelloRequest) returns (stream HelloReply) {}
6212
}
6313

64-
// A latitude-longitude rectangle, represented as two diagonally opposite
65-
// points "lo" and "hi".
66-
message Rectangle {
67-
// One corner of the rectangle.
68-
Point lo = 1;
69-
70-
// The other corner of the rectangle.
71-
Point hi = 2;
14+
service Butler {
15+
// Sends a greeting
16+
rpc SayHello (HelloRequest) returns (HelloReply) {}
17+
rpc SayHelloCS (stream HelloRequest) returns (HelloReply) {}
18+
rpc SayHellos (HelloRequest) returns (stream HelloReply) {}
19+
rpc SayHelloBidi (stream HelloRequest) returns (stream HelloReply) {}
7220
}
73-
74-
// A feature names something at a given point.
75-
//
76-
// If a feature could not be named, the name is empty.
77-
message Feature {
78-
// The name of the feature.
21+
// The request message containing the user's name.
22+
message HelloRequest {
7923
string name = 1;
80-
81-
// The point where the feature is detected.
82-
Point location = 2;
83-
}
84-
85-
// A RouteNote is a message sent while at a given point.
86-
message RouteNote {
87-
// The location from which the message is sent.
88-
Point location = 1;
89-
90-
// The message to be sent.
91-
string message = 2;
9224
}
9325

94-
// A RouteSummary is received in response to a RecordRoute rpc.
95-
//
96-
// It contains the number of individual points received, the number of
97-
// detected features, and the total distance covered as the cumulative sum of
98-
// the distance between each point.
99-
message RouteSummary {
100-
// The number of points received.
101-
int32 point_count = 1;
102-
103-
// The number of known features passed while traversing the route.
104-
int32 feature_count = 2;
105-
106-
// The distance covered in metres.
107-
int32 distance = 3;
108-
109-
// The duration of the traversal in seconds.
110-
int32 elapsed_time = 4;
26+
// The response message containing the greetings
27+
message HelloReply {
28+
string message = 1;
11129
}

src/client/components/display/History.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class History extends Component {
7979
this.props.setNewRequestBody(requestBodyObj);
8080
this.props.setNewRequestStreams(requestStreamsObj)
8181
// for gRPC
82-
if (this.props.content.gRPC) {
82+
if (this.props.content && this.props.content.gRPC) {
8383
// need to place logic in callback otherwise code won't work and returns null
8484
this.setState({
8585
...this.state

src/client/components/display/RequestTabs.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class RequestTabs extends Component {
1212
}
1313

1414
handleTabSelect(val) {
15+
let headers = "Request Headers"
16+
if (this.props.requestContent.bodyType === 'GRPC') {
17+
headers = "Request Metadata"
18+
}
19+
1520
switch (val) {
1621
case "Request Body":
1722
this.setState({
@@ -28,7 +33,7 @@ class RequestTabs extends Component {
2833
openTab: val
2934
});
3035
break;
31-
case "Request Headers":
36+
case headers:
3237
this.setState({
3338
openTab: val
3439
});
@@ -73,7 +78,7 @@ class RequestTabs extends Component {
7378
: <p className="reqResContent" key={`reqResContent${this.props.requestContent.id}`} >No Request Variables</p>
7479
}
7580

76-
else if (this.state.openTab === "Request Headers") {
81+
else if (this.state.openTab === headers) {
7782
tabContentShown = [];
7883
if (this.props.requestContent.headers && this.props.requestContent.headers.length > 0) {
7984
this.props.requestContent.headers.forEach((cur, idx) => {
@@ -86,7 +91,7 @@ class RequestTabs extends Component {
8691
});
8792
}
8893
else {
89-
tabContentShown.push(<p className="reqResContent" key={`reqResContent${this.props.requestContent.id}`} >No Request Headers</p>)
94+
tabContentShown.push(<p className="reqResContent" key={`reqResContent${this.props.requestContent.id}`} >No {headers}</p>)
9095
}
9196
}
9297

src/client/components/display/ResponseEventsDisplay.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,29 @@ const ResponseEventsDisplay = ({ response }) => {
1919
<div className="okay" dangerouslySetInnerHTML={{__html: createDOMPurify.sanitize(events[0])}} />
2020
)
2121
}
22+
else if (events.length > 1) {
23+
if (events) {
24+
displayContents.push(
25+
<div className="json-response" key="jsonresponsediv">
26+
<JSONPretty data={events} space="4" theme={{
27+
main: 'line-height:1.3; color: midnightblue; background:#RRGGBB; overflow:auto;',
28+
key: 'color:#0089D0;', // bluetwo
29+
string: 'color:#15B78F;',// greenone
30+
value: 'color:#fd971f;', // a nice orange
31+
boolean: 'color:#E00198;', // gqlpink
32+
}}
33+
/>
34+
</div>
35+
);
36+
}
37+
38+
}
2239
// Otherwise, render a single display
2340
else {
2441
if (events) {
2542
displayContents.push(
2643
<div className="json-response" key="jsonresponsediv">
27-
<JSONPretty data={events} space="4" theme={{
44+
<JSONPretty data={events[0]} space="4" theme={{
2845
main: 'line-height:1.3; color: midnightblue; background:#RRGGBB; overflow:auto;',
2946
key: 'color:#0089D0;', // bluetwo
3047
string: 'color:#15B78F;',// greenone

0 commit comments

Comments
 (0)