Skip to content

Instantly share code, notes, and snippets.

@autodidaddict
Created July 23, 2018 11:13
Show Gist options
  • Save autodidaddict/75538cbd456b991e0e6f722e3241a5a2 to your computer and use it in GitHub Desktop.
Save autodidaddict/75538cbd456b991e0e6f722e3241a5a2 to your computer and use it in GitHub Desktop.
Ballerina gRPC attempt
import ballerina/grpc;
endpoint grpc:Listener listener {
host: "localhost",
port: 9090
};
map <droneInfo> dronesMap;
type droneInfo record {
string id;
string name;
int battery_remaining;
};
@grpc:ServiceConfig
service droneMgmt bind listener {
getDrone(endpoint caller, string droneId) {
if (dronesMap.hasKey(droneId)) {
//json droneDetails = check <json>dronesMap[droneId];
//payload = droneDetails.toString();
var payload = dronesMap[droneId];
_ = caller->send(payload);
} else {
// payload = "No such drone";
}
_ = caller->complete();
}
addDrone(endpoint caller, droneInfo addDroneRequest) {
string droneId =
addDroneRequest.id;
dronesMap[droneId] =
addDroneRequest;
string payload = "Status: Drone created; DroneID = " + droneId;
_ = caller ->send(payload);
_ = caller ->complete();
}
updateDrone(endpoint caller, droneInfo updateDroneRequest) {
string payload;
string droneId =
updateDroneRequest.id;
if (dronesMap .hasKey(droneId)) {
dronesMap[droneId] =
updateDroneRequest;
payload = "Drone '" + droneId + "' updated.";
} else {
payload = "Drone '" + droneId + "' not found.";
}
_ = caller ->send(payload);
_ = caller ->complete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment