start method
This method controls the start of the plugin. The plugin's start() method is used to initialize the default configuration and receive the standard input from the core lightning and decoding the JSON from the input message and preparing the plugin object which will allow for a seamless communication between core lightning and the plugin on top of the JSONRPCv2.0 protocol.
Implementation
@override
void start() async {
_defaultPluginConfiguration();
try {
String? messageSocket;
/// TODO: move this in async way
while ((messageSocket = stdin.readLineSync()) != null) {
// Already checked is stdin is not null, why trim and check again??
if (messageSocket!.trim().isEmpty) {
continue;
}
var jsonRequest = Request.fromJson(jsonDecode(messageSocket));
try {
HashMap<String, Object> param;
if (jsonRequest.params is Map) {
param = HashMap<String, Object>.from(jsonRequest.params);
} else {
param = HashMap();
}
if (jsonRequest.id == null) {
await _handlingSubscription(
event: jsonRequest.method, request: param);
} else {
var result = await _callRPCMethod(jsonRequest.method, param);
var response =
Response(id: jsonRequest.id, result: result).toJson();
stdout.write(json.encode(response));
}
} catch (ex) {
var response = Response(
id: jsonRequest.id,
error: Error(code: -1, message: ex.toString()))
.toJson();
stdout.write(json.encode(response));
}
}
} catch (error, stacktrace) {
stderr.write(stacktrace);
stderr.write(error);
}
}