-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor.cc
147 lines (125 loc) · 3.24 KB
/
vendor.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef FOODVENDOR
#define FOODVENDOR
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "include/vendor.h"
#include "include/exporters.h"
#include "include/simulatedProcessing.h"
#include "food.grpc.pb.h"
#include "food.pb.h"
#include <grpcpp/grpcpp.h>
#include <grpcpp/opencensus.h>
#include "absl/strings/escaping.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "opencensus/stats/stats.h"
#include "opencensus/tags/context_util.h"
#include "opencensus/tags/tag_map.h"
#include "opencensus/trace/context_util.h"
#include "opencensus/trace/sampler.h"
#include "opencensus/trace/span.h"
#include "opencensus/trace/trace_config.h"
class VendorService final : public food::Vendor::Service {
grpc::Status GetItemInfo (grpc::ServerContext *context,
const food::ItemStoreQuery* query,
food::ItemInfoReply* reply) override {
auto span = grpc::GetSpanFromServerContext(context);
std::string store = query->store();
std::string item = query->item();
int stock = getVendorStock()[store][item];
double price = getVendorPrices()[store][item];
reply->set_price(price);
reply->set_inventory(stock);
doDelay(&span);
span.AddAnnotation("Checking price and stock for " + item
+ " at " + store + "\n");
return randomlyFailedStatus();
}
};
void foodVendor(){
const std::string port = "127.0.0.1:8889";
// Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC.
grpc::RegisterOpenCensusPlugin();
// Register the gRPC views (latency, error count, etc).
grpc::RegisterOpenCensusViewsForExport();
RegisterExporters();
grpc::ServerBuilder builder;
VendorService service;
builder.AddListeningPort(port, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
std::cout << "Server listening on [::]:" << port << "\n";
server->Wait();
}
int main(){
foodVendor();
}
std::map<std::string, std::map<std::string, double>> vendorPrices {
{"Trader Joes", {
{"yeast", 4.49},
{"flour", 1.71},
{"sugar", 3.28},
{"noodles", 4.19},
{"chocolate", 2.12},
{"salt", 3.99},
}},
{"Target", {
{"yeast", 4.04},
{"flour", 0.9},
{"sugar", 4.23},
{"noodles", 4.35},
{"salt", 3.54},
}},
{"Kroger", {
{"yeast", 2.0},
{"flour", 2.49},
{"noodles", 4.06},
{"chocolate", 4.96},
{"salt", 4.59}
}},
{"Aldi", {
{"yeast", 3.49},
{"flour", 4.6},
{"sugar", 3.59},
{"noodles", 0.73},
{"chocolate", 4.13},
{"salt", 4.8}
}}
};
std::map<std::string, std::map<std::string, int>> vendorStock {
{"Trader Joes", {
{"yeast", 34},
{"flour", 55},
{"sugar", 0},
{"noodles", 69},
{"chocolate", 7},
{"salt", 34}
}},
{"Target", {
{"yeast", 51},
{"flour", 37},
{"sugar", 48},
{"noodles", 92},
{"salt", 18}
}},
{"Kroger", {
{"yeast", 33},
{"flour", 5},
{"noodles", 39},
{"chocolate", 27},
{"salt", 45}
}},
{"Aldi", {
{"yeast", 21},
{"flour", 7},
{"sugar", 68},
{"noodles", 99},
{"chocolate", 42},
{"salt", 1}
}},
};
std::map<std::string, std::map<std::string, double>> getVendorPrices () { return vendorPrices; }
std::map<std::string, std::map<std::string, int>> getVendorStock () { return vendorStock; }
#endif // FOODVENDOR