added exporter service
This commit is contained in:
parent
7bd18c1a4c
commit
baae1428bd
67
src/bin/exporter/Exporter.hx
Normal file
67
src/bin/exporter/Exporter.hx
Normal file
@ -0,0 +1,67 @@
|
||||
package bin.exporter;
|
||||
|
||||
import kernel.net.Package.GenericPackage;
|
||||
import kernel.peripherals.exports.RedstoneExport;
|
||||
import kernel.peripherals.Peripherals.Peripheral;
|
||||
import kernel.ps.ProcessHandle;
|
||||
import kernel.ps.IProcess;
|
||||
|
||||
/**
|
||||
A service to expose local peripherals to the network.
|
||||
**/
|
||||
@:build(macros.Binstore.includeBin("Exporter", ["exporter"]))
|
||||
class Exporter implements IProcess {
|
||||
public function new() {}
|
||||
|
||||
public function run(handle:ProcessHandle) {
|
||||
if (handle.args.length < 2) {
|
||||
handle.writeLine("Not enough args: <addr> <type>");
|
||||
handle.close(false);
|
||||
}
|
||||
|
||||
var addr = handle.args[0];
|
||||
var expectedType = handle.args[1];
|
||||
|
||||
// Check if peripheral is present. Not the case for redstone.
|
||||
if (expectedType != kernel.peripherals.Redstone.TYPE_NAME && !Peripheral.isPresent(addr)) {
|
||||
handle.writeLine('Address: $addr not present');
|
||||
handle.close(false);
|
||||
}
|
||||
|
||||
var types = Peripheral.getTypes(addr);
|
||||
|
||||
// Check if the correct type is present. Not the case for redstone.
|
||||
if (expectedType != kernel.peripherals.Redstone.TYPE_NAME && !types.contains(expectedType)) {
|
||||
handle.writeLine('Expected type not machted on address: $addr. Wanted $expectedType got $types');
|
||||
handle.close(false);
|
||||
}
|
||||
|
||||
var peri = Peripheral.getFromType(addr, expectedType);
|
||||
|
||||
if (peri == null) {
|
||||
handle.writeLine('Failed to create peripheral for address: $addr');
|
||||
handle.close(false);
|
||||
}
|
||||
|
||||
var handePackFunc:(GenericPackage) -> Bool = null;
|
||||
|
||||
// Cast to RPC class
|
||||
// TODO: There must be a smarter way to do this.
|
||||
switch expectedType {
|
||||
case kernel.peripherals.Redstone.TYPE_NAME:
|
||||
handePackFunc = (p) -> {
|
||||
return RedstoneExport.handlePackage(cast peri, p);
|
||||
};
|
||||
// TODO: More peripherals
|
||||
}
|
||||
|
||||
handle.writeLine('Listening on export:$addr with type $expectedType');
|
||||
|
||||
// Listen for packages
|
||||
kernel.net.Net.registerProto('export:$addr', (p) -> {
|
||||
if (!handePackFunc(p)) {
|
||||
handle.writeLine("Failed handle package on export");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user