Compare commits
8 Commits
c86d0e1d8b
...
13fe7fbab1
| Author | SHA1 | Date | |
|---|---|---|---|
| 13fe7fbab1 | |||
| d0c01800a6 | |||
| fd2b53167e | |||
| 2051f486d4 | |||
| f8b226bae5 | |||
| cd6808e62c | |||
| 30304ec5e6 | |||
| 00a9c8dc4f |
@@ -9,7 +9,7 @@ using Lambda;
|
||||
class Disk extends CLIAppBase {
|
||||
public function new() {
|
||||
registerSyncSubcommand("ls", (args)->{
|
||||
Peripheral.instance.getDrives().foreach(drive -> {
|
||||
Peripheral.instance.getAllDrives().foreach(drive -> {
|
||||
var addr = drive.getAddr();
|
||||
var label = drive.getDiskLabel();
|
||||
var id = drive.getDiskID();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package bin;
|
||||
|
||||
import kernel.service.ServiceManager;
|
||||
import kernel.log.Log;
|
||||
import kernel.Timer;
|
||||
import kernel.ps.ProcessHandle;
|
||||
@@ -11,7 +12,9 @@ class HelloWorldService implements Process {
|
||||
public function run(handle:ProcessHandle) {
|
||||
Log.debug("HelloWorldService started");
|
||||
handle.write("Hello World! Started\n");
|
||||
this.startTimer(handle);
|
||||
// this.startTimer(handle);
|
||||
var srv: HelloWorldService = ServiceManager.instance.get("HelloWorldService");
|
||||
srv.startTimer(handle);
|
||||
}
|
||||
|
||||
public function startTimer(handle: ProcessHandle) {
|
||||
|
||||
@@ -19,7 +19,7 @@ class Net extends CLIAppBase {
|
||||
});
|
||||
|
||||
registerSyncSubcommand("iface", (args)->{
|
||||
var modems = Peripheral.instance.getModems();
|
||||
var modems = Peripheral.instance.getAllModems();
|
||||
|
||||
for (modem in modems) {
|
||||
handle.writeLine(modem.name());
|
||||
|
||||
38
src/bin/Perf.hx
Normal file
38
src/bin/Perf.hx
Normal file
@@ -0,0 +1,38 @@
|
||||
package bin;
|
||||
|
||||
import kernel.peripherals.Peripherals.Peripheral;
|
||||
import lib.CLIAppBase;
|
||||
|
||||
class Perf extends CLIAppBase {
|
||||
public function new() {
|
||||
registerSyncSubcommand("inspect",(args)->{
|
||||
if (args.length < 1) return false;
|
||||
|
||||
var result = Peripheral.instance.inspect(args[0]);
|
||||
|
||||
if (result == null){
|
||||
handle.writeLine("No peripheral found on side "+args[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
handle.writeLine("Types:");
|
||||
for (type in result.types){
|
||||
handle.writeLine(" "+type);
|
||||
}
|
||||
|
||||
handle.writeLine("Methods:");
|
||||
for (method in result.methods){
|
||||
handle.writeLine(" "+method);
|
||||
}
|
||||
|
||||
return true;
|
||||
},"<side>");
|
||||
|
||||
registerSyncSubcommand("list",(args)->{
|
||||
for (addr in Peripheral.instance.getAllAddresses()){
|
||||
handle.writeLine('$addr => ${Peripheral.instance.getTypes(addr).join(", ")}');
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ class Service extends CLIAppBase {
|
||||
|
||||
var name = args[0];
|
||||
|
||||
var result = ServiceManager.instace.start(name);
|
||||
var result = ServiceManager.instance.start(name);
|
||||
return handleResult(result);
|
||||
},"Start a service");
|
||||
|
||||
@@ -25,7 +25,7 @@ class Service extends CLIAppBase {
|
||||
|
||||
var name = args[0];
|
||||
|
||||
var result = ServiceManager.instace.stop(name);
|
||||
var result = ServiceManager.instance.stop(name);
|
||||
return handleResult(result);
|
||||
},"Stop a service");
|
||||
|
||||
@@ -38,7 +38,7 @@ class Service extends CLIAppBase {
|
||||
var binName = args[1];
|
||||
var rest = args.slice(2);
|
||||
|
||||
var result = ServiceManager.instace.register(name, binName, rest);
|
||||
var result = ServiceManager.instance.register(name, binName, rest);
|
||||
return handleResult(result);
|
||||
},"Register a new service");
|
||||
|
||||
@@ -49,13 +49,13 @@ class Service extends CLIAppBase {
|
||||
|
||||
var name = args[0];
|
||||
|
||||
var result = ServiceManager.instace.unregister(name);
|
||||
var result = ServiceManager.instance.unregister(name);
|
||||
return handleResult(result);
|
||||
},"Unregister a service");
|
||||
|
||||
registerSyncSubcommand("list", (args) ->{
|
||||
|
||||
var list = ServiceManager.instace.listRunning();
|
||||
var list = ServiceManager.instance.listRunning();
|
||||
|
||||
for (name in list) {
|
||||
this.handle.writeLine(name);
|
||||
@@ -63,6 +63,15 @@ class Service extends CLIAppBase {
|
||||
|
||||
return true;
|
||||
},"List all services");
|
||||
|
||||
registerSyncSubcommand("enable", (args) ->{
|
||||
if (args.length < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ServiceManager.instance.enable(args[0]);
|
||||
return true;
|
||||
},"Enable a service");
|
||||
}
|
||||
|
||||
private function handleResult(res: Outcome<Noise,String>): Bool {
|
||||
|
||||
92
src/bin/srsc/CLI.hx
Normal file
92
src/bin/srsc/CLI.hx
Normal file
@@ -0,0 +1,92 @@
|
||||
package bin.srsc;
|
||||
|
||||
import kernel.net.Package.NetworkID;
|
||||
import lib.RessourceNames;
|
||||
import lib.CLIAppBase;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class CLI extends CLIAppBase {
|
||||
public function new() {
|
||||
registerAsyncSubcommand("get", (args) -> {
|
||||
if (args.length < 2) {
|
||||
handle.writeLine("Not enough arguments");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
var id:NetworkID = Std.parseInt(args[0]);
|
||||
if (id == null) {
|
||||
handle.writeLine("Invalid id");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
return RessourceNames.get(args[1], id).map((res) -> {
|
||||
switch (res) {
|
||||
case Success(data):
|
||||
if (data == null) {
|
||||
handle.writeLine("Not found");
|
||||
} else {
|
||||
handle.writeLine("Found: " + data);
|
||||
}
|
||||
case Failure(error):
|
||||
handle.writeLine("Error: " + error);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}, "<id> <name>");
|
||||
|
||||
registerAsyncSubcommand("register", (args) -> {
|
||||
if (args.length < 3) {
|
||||
handle.writeLine("Not enough arguments");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
var id:NetworkID = Std.parseInt(args[0]);
|
||||
if (id == null) {
|
||||
handle.writeLine("Invalid id");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
var id2:NetworkID = Std.parseInt(args[2]);
|
||||
if (id2 == null) {
|
||||
handle.writeLine("Invalid id");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
return RessourceNames.register(args[1], id2, id).map((res) -> {
|
||||
switch (res) {
|
||||
case Success(data):
|
||||
handle.writeLine("Success");
|
||||
case Failure(error):
|
||||
handle.writeLine("Error: " + error);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}, "<id> <name> <id>");
|
||||
|
||||
registerAsyncSubcommand("unregister", (args) -> {
|
||||
if (args.length < 2) {
|
||||
handle.writeLine("Not enough arguments");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
var id:NetworkID = Std.parseInt(args[0]);
|
||||
if (id == null) {
|
||||
handle.writeLine("Invalid id");
|
||||
return Future.sync(false);
|
||||
}
|
||||
|
||||
return RessourceNames.unregister(args[1], id).map((res) -> {
|
||||
switch (res) {
|
||||
case Success(data):
|
||||
handle.writeLine("Success");
|
||||
case Failure(error):
|
||||
handle.writeLine("Error: " + error);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}, "<id> <name>");
|
||||
}
|
||||
}
|
||||
32
src/bin/srsc/PackageTypes.hx
Normal file
32
src/bin/srsc/PackageTypes.hx
Normal file
@@ -0,0 +1,32 @@
|
||||
package bin.srsc;
|
||||
|
||||
import kernel.net.Package.NetworkID;
|
||||
|
||||
typedef GetRequest = {
|
||||
public var ?type:String;
|
||||
public var name:String;
|
||||
}
|
||||
|
||||
typedef GetResponse = {
|
||||
public var success:Bool;
|
||||
public var netID:NetworkID;
|
||||
}
|
||||
|
||||
typedef RegisterRequest = {
|
||||
public var ?type:String;
|
||||
public var name:String;
|
||||
public var netID:NetworkID;
|
||||
}
|
||||
|
||||
typedef RegisterResponse = {
|
||||
public var success:Bool;
|
||||
}
|
||||
|
||||
typedef UnregisterRequest = {
|
||||
public var ?type:String;
|
||||
public var name:String;
|
||||
}
|
||||
|
||||
typedef UnregisterResponse = {
|
||||
public var success:Bool;
|
||||
}
|
||||
104
src/bin/srsc/SiteRessourceController.hx
Normal file
104
src/bin/srsc/SiteRessourceController.hx
Normal file
@@ -0,0 +1,104 @@
|
||||
package bin.srsc;
|
||||
|
||||
import kernel.log.Log;
|
||||
import lib.KVStore;
|
||||
import bin.srsc.PackageTypes;
|
||||
import kernel.net.Package;
|
||||
import kernel.ps.ProcessHandle;
|
||||
import kernel.ps.Process;
|
||||
|
||||
/**
|
||||
The SiteRessourceController is responsible for the management of ressources on the site.
|
||||
It makes sure that ressources are have a unique id and that they are not duplicated.
|
||||
It also keeps track of the responsible NetID for each ressource. Kinda like a DNS.
|
||||
**/
|
||||
class SiteRessourceController implements Process {
|
||||
public static inline final SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO:String = "srsc";
|
||||
|
||||
private var handle:ProcessHandle;
|
||||
private var ressources:Map<String, NetworkID>;
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function run(handle:ProcessHandle) {
|
||||
this.handle = handle;
|
||||
|
||||
load();
|
||||
|
||||
// Register proto
|
||||
kernel.net.Net.instance.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg);
|
||||
}
|
||||
|
||||
private function handleMsg(pkg:Package) {
|
||||
/*
|
||||
There are 3 types of messages:
|
||||
- Register a new ressource name
|
||||
- Unregister a ressource name
|
||||
- Get the responsible NetID for a ressource name
|
||||
*/
|
||||
|
||||
switch (pkg.data.type) {
|
||||
case "register":
|
||||
pkg.respond(handleRegister(pkg.data));
|
||||
case "unregister":
|
||||
pkg.respond(handleUnregister(pkg.data));
|
||||
case "get":
|
||||
pkg.respond(handleGet(pkg.data));
|
||||
default:
|
||||
handle.writeLine("Unknown message type: " + pkg.data.type);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleGet(data:GetRequest):GetResponse {
|
||||
var res = get(data.name);
|
||||
return {success: res != null, netID: res};
|
||||
}
|
||||
|
||||
private function handleRegister(data:RegisterRequest):RegisterResponse {
|
||||
var res = register(data.name, data.netID);
|
||||
return {success: res};
|
||||
}
|
||||
|
||||
private function handleUnregister(data:UnregisterRequest):UnregisterResponse {
|
||||
unregister(data.name);
|
||||
return {success: true};
|
||||
}
|
||||
|
||||
public function register(name:String, netID:NetworkID):Bool {
|
||||
if (ressources.exists(name) && ressources.get(name) != netID) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ressources.set(name, netID);
|
||||
save();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function unregister(name:String) {
|
||||
if (ressources.exists(name)) {
|
||||
ressources.remove(name);
|
||||
}
|
||||
save();
|
||||
}
|
||||
|
||||
public function get(name:String):Null<NetworkID> {
|
||||
return ressources.get(name);
|
||||
}
|
||||
|
||||
private function load() {
|
||||
var store = KVStore.getStoreForClass();
|
||||
var data:Null<Map<String, NetworkID>> = store.get("ressources");
|
||||
|
||||
if (data != null) {
|
||||
this.ressources = data;
|
||||
} else {
|
||||
this.ressources = new Map<String, NetworkID>();
|
||||
}
|
||||
}
|
||||
|
||||
private function save() {
|
||||
var store = KVStore.getStoreForClass();
|
||||
store.set("ressources", this.ressources);
|
||||
store.save();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,9 @@ class DCEHack {
|
||||
new bin.Terminal(),
|
||||
new bin.Turtle(),
|
||||
new bin.HelloWorldService(),
|
||||
new bin.srsc.SiteRessourceController(),
|
||||
new bin.srsc.CLI(),
|
||||
new bin.Perf(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,6 @@ class Init {
|
||||
KernelEvents.instance.startEventLoop();
|
||||
});
|
||||
|
||||
ServiceManager.instace = new ServiceManager();
|
||||
ServiceManager.instance = new ServiceManager();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package kernel.binstore;
|
||||
|
||||
import bin.Perf;
|
||||
import bin.srsc.CLI;
|
||||
import bin.srsc.SiteRessourceController;
|
||||
import bin.HelloWorldService;
|
||||
import bin.Service;
|
||||
import bin.LSPS;
|
||||
@@ -27,7 +30,10 @@ class BinStore {
|
||||
{c: Turtle, name: "Turtle", aliases: ["turtle"]},
|
||||
{c: LSPS, name: "PM", aliases: ["lsps"]},
|
||||
{c: Service, name: "Service", aliases: ["service","srv"]},
|
||||
{c: HelloWorldService, name: "HelloWorldService", aliases: ["hello-service"] }
|
||||
{c: HelloWorldService, name: "HelloWorldService", aliases: ["hello-service"] },
|
||||
{c: SiteRessourceController, name: "SiteRessourceController", aliases: ["srsc"]},
|
||||
{c: CLI, name: "SRSC CLI", aliases: ["srsc-cli"]},
|
||||
{c: Perf, name: "Perf", aliases: ["perf"]}
|
||||
];
|
||||
|
||||
@:allow(kernel.Init)
|
||||
|
||||
@@ -31,7 +31,7 @@ class Net {
|
||||
|
||||
@:allow(kernel.Init)
|
||||
private function new() {
|
||||
this.interfaces = [for (e in Peripheral.instance.getModems()) e ]; // TODO: is this the way to do it?
|
||||
this.interfaces = [for (e in Peripheral.instance.getAllModems()) e ]; // TODO: is this the way to do it?
|
||||
this.interfaces.push(Loopback.instance);
|
||||
|
||||
for (interf in interfaces){
|
||||
@@ -279,7 +279,7 @@ class Net {
|
||||
data: null,
|
||||
};
|
||||
|
||||
for (modem in Peripheral.instance.getModems()) {
|
||||
for (modem in Peripheral.instance.getAllModems()) {
|
||||
if (!modem.isWireless()) continue;
|
||||
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack);
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class Routing {
|
||||
private function brodcastRoutingTable() {
|
||||
var pack = newRoutDiscoverPackage();
|
||||
|
||||
for (modem in Peripheral.instance.getModems()) {
|
||||
for (modem in Peripheral.instance.getAllModems()) {
|
||||
modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package kernel.peripherals;
|
||||
|
||||
import cc.Peripheral;
|
||||
using tink.CoreApi;
|
||||
|
||||
class Drive implements IPeripheral {
|
||||
public static inline final TYPE_NAME:String = "drive";
|
||||
|
||||
public final onDiskInsert:Signal<Noise>;
|
||||
public final onDiskEject:Signal<Noise>;
|
||||
|
||||
@@ -12,9 +15,9 @@ class Drive implements IPeripheral {
|
||||
private final onDiskEjectTrigger:SignalTrigger<Noise> = Signal.trigger();
|
||||
|
||||
@:allow(kernel.peripherals)
|
||||
private function new(native: cc.periphs.Disk,addr: String) {
|
||||
private function new(addr: String) {
|
||||
this.addr = addr;
|
||||
this.native = native;
|
||||
this.native = Peripheral.wrap(addr);
|
||||
|
||||
this.onDiskInsert = this.onDiskInsertTrigger.asSignal();
|
||||
this.onDiskEject = this.onDiskEjectTrigger.asSignal();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package kernel.peripherals;
|
||||
|
||||
import cc.Peripheral;
|
||||
import kernel.log.Log;
|
||||
import kernel.net.Package;
|
||||
import kernel.net.INetworkInterface;
|
||||
@@ -7,6 +8,8 @@ import kernel.net.INetworkInterface;
|
||||
using tink.CoreApi;
|
||||
|
||||
class Modem implements INetworkInterface implements IPeripheral {
|
||||
public static inline final TYPE_NAME:String = "modem";
|
||||
|
||||
public final addr:String;
|
||||
public var onMessage(default, null):Signal<{pack:Package,dist:Null<Float>}>;
|
||||
|
||||
@@ -14,9 +17,9 @@ class Modem implements INetworkInterface implements IPeripheral {
|
||||
private final native:cc.periphs.Modem.Modem;
|
||||
|
||||
@:allow(kernel.peripherals)
|
||||
private function new(nativePeripherals:cc.periphs.Modem.Modem, addr:String) {
|
||||
private function new(addr:String) {
|
||||
this.onMessage = onMessageTrigger.asSignal();
|
||||
this.native = nativePeripherals;
|
||||
this.native = Peripheral.wrap(addr);
|
||||
this.addr = addr;
|
||||
|
||||
KernelEvents.instance.onModemMessage.handle(params ->{
|
||||
|
||||
@@ -17,93 +17,115 @@ class Peripheral {
|
||||
**/
|
||||
public static var instance:Peripheral;
|
||||
|
||||
private var screens: Array<Screen> = [];
|
||||
private var modes: Array<Modem> = [];
|
||||
private var drives:Array<Drive> = [];
|
||||
private var redstone:Array<Redstone> = [];
|
||||
private var printers:Array<Printer> = [];
|
||||
|
||||
@:allow(kernel.Init)
|
||||
private function new() {
|
||||
KernelEvents.instance.onPeripheral.handle(this.updatePeripherals);
|
||||
KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals);
|
||||
updatePeripherals();
|
||||
|
||||
redstone = [ for(side in [Side.Top,Side.Right,Side.Left,Side.Front,Side.Bottom,Side.Back]) new Redstone(side) ];
|
||||
}
|
||||
|
||||
private function updatePeripherals() {
|
||||
findScreens();
|
||||
findModems();
|
||||
findDrives();
|
||||
public function getAllAddresses(): Array<String> {
|
||||
return cc.Peripheral.getNames().toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
Get all connected screens.
|
||||
**/
|
||||
public function getScreens():ReadOnlyArray<Screen> {
|
||||
return this.screens;
|
||||
public function isPresent(addr: String): Bool {
|
||||
return cc.Peripheral.isPresent(addr);
|
||||
}
|
||||
|
||||
private function findScreens():Void {
|
||||
var allScreens = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "monitor");
|
||||
this.screens = allScreens.map(s -> return new Screen((cc.Peripheral.wrap(s) : Dynamic), s));
|
||||
public function getTypes(addr: String): Array<String> {
|
||||
if (!cc.Peripheral.isPresent(addr)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return cc.Peripheral.getType(addr).toArray();
|
||||
}
|
||||
|
||||
public function getScreen(addr: String): Screen {
|
||||
return this.screens.find(item -> item.getAddr() == addr);
|
||||
private function findAddrByType(type: String): Array<String> {
|
||||
return getAllAddresses().filter(addr -> getTypes(addr).contains(type));
|
||||
}
|
||||
|
||||
private function safeGetAddr(addr: String, type: String): Null<String> {
|
||||
if (!isPresent(addr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var types = getTypes(addr);
|
||||
if (!types.contains(type)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
/**
|
||||
Get all connected modems.
|
||||
**/
|
||||
public function getModems():ReadOnlyArray<Modem> {
|
||||
return this.modes;
|
||||
public function inspect(addr: String): Null<{ types: Array<String>, methods: Array<String>}> {
|
||||
if (!isPresent(addr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var types = getTypes(addr);
|
||||
var methodsMap = cc.Peripheral.getMethods(addr).toArray();
|
||||
var methods: Array<String> = [];
|
||||
|
||||
for (method in methodsMap) {
|
||||
methods.push(method);
|
||||
}
|
||||
|
||||
return {
|
||||
types: types,
|
||||
methods: methods
|
||||
};
|
||||
}
|
||||
|
||||
public function getScreen(addr: String): Null<Screen> {
|
||||
var addr = safeGetAddr(addr, Screen.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Screen(addr);
|
||||
}
|
||||
|
||||
|
||||
public function getAllScreens(): Array<Screen> {
|
||||
return [ for (addr in findAddrByType(Screen.TYPE_NAME)) new Screen(addr)];
|
||||
}
|
||||
|
||||
public function getModem(addr: String): Null<Modem> {
|
||||
return this.modes.find(item -> item.getAddr() == addr);
|
||||
var addr = safeGetAddr(addr, Modem.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Modem(addr);
|
||||
}
|
||||
|
||||
private function findModems():Void {
|
||||
var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem");
|
||||
this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s));
|
||||
}
|
||||
|
||||
/**
|
||||
Get all connected drives.
|
||||
**/
|
||||
public function getDrives(): ReadOnlyArray<Drive> {
|
||||
return this.drives;
|
||||
public function getAllModems(): Array<Modem> {
|
||||
return [ for (addr in findAddrByType(Modem.TYPE_NAME)) new Modem(addr)];
|
||||
}
|
||||
|
||||
public function getDrive(addr: String): Null<Drive> {
|
||||
return this.drives.find(item -> item.getAddr() == addr);
|
||||
var addr = safeGetAddr(addr, Drive.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Drive(addr);
|
||||
}
|
||||
|
||||
private function findDrives() {
|
||||
var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive");
|
||||
this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s));
|
||||
public function getAllDrives(): Array<Drive> {
|
||||
return [ for (addr in findAddrByType(Drive.TYPE_NAME)) new Drive(addr)];
|
||||
}
|
||||
|
||||
/**
|
||||
Get redstone peripheral.
|
||||
**/
|
||||
public function getRedstone(side: String): Redstone {
|
||||
return this.redstone.find(item -> item.getAddr() == side);
|
||||
// TODO: maybe handle restone differently to not duplicate event listeners
|
||||
return new Redstone(side);
|
||||
}
|
||||
|
||||
/**
|
||||
Get all connected printers.
|
||||
**/
|
||||
|
||||
public function getPrinters():ReadOnlyArray<Printer> {
|
||||
return this.printers;
|
||||
public function getPrinter(addr: String):Null<Printer> {
|
||||
var addr = safeGetAddr(addr, Printer.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Printer(addr);
|
||||
}
|
||||
|
||||
private function findPrinters() {
|
||||
var allPrinters = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "printer");
|
||||
this.printers = allPrinters.map(s -> return new Printer((cc.Peripheral.wrap(s) : Dynamic), s));
|
||||
public function getAllPrinters(): Array<Printer> {
|
||||
return [ for (addr in findAddrByType(Printer.TYPE_NAME)) new Printer(addr)];
|
||||
}
|
||||
|
||||
public function getEnergyStorage(addr: String): Null<EnergyStorage> {
|
||||
var addr = safeGetAddr(addr, EnergyStorage.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new EnergyStorage(addr);
|
||||
}
|
||||
|
||||
public function getAllEnergyStorages(): Array<EnergyStorage> {
|
||||
return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package kernel.peripherals;
|
||||
|
||||
import cc.Peripheral;
|
||||
import lib.Rect;
|
||||
import lib.Pos;
|
||||
|
||||
class Printer implements IPeripheral {
|
||||
public static inline final TYPE_NAME:String = "printer";
|
||||
|
||||
private final native:cc.periphs.Printer.Printer;
|
||||
private final addr:String;
|
||||
|
||||
public function new(native: cc.periphs.Printer.Printer, addr: String) {
|
||||
this.native = native;
|
||||
public function new(addr: String) {
|
||||
this.native = Peripheral.wrap(addr);
|
||||
this.addr = addr;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package kernel.peripherals;
|
||||
|
||||
import cc.Peripheral;
|
||||
import lib.Pos;
|
||||
import cc.Term.TerminalSize;
|
||||
import kernel.ui.TermWriteable;
|
||||
@@ -9,6 +10,8 @@ import lib.Color;
|
||||
using tink.CoreApi;
|
||||
|
||||
class Screen implements TermWriteable implements IPeripheral {
|
||||
public static inline final TYPE_NAME:String = "monitor";
|
||||
|
||||
private final nativ:cc.periphs.Monitor.Monitor;
|
||||
private final addr:String;
|
||||
|
||||
@@ -17,10 +20,10 @@ class Screen implements TermWriteable implements IPeripheral {
|
||||
public final onResize:Signal<Vec2<Int>>;
|
||||
|
||||
@:allow(kernel.peripherals)
|
||||
private function new(nativePeripherals:cc.periphs.Monitor.Monitor, addr:String) {
|
||||
private function new(addr:String) {
|
||||
this.onResize = onResizeTrigger.asSignal();
|
||||
|
||||
this.nativ = nativePeripherals;
|
||||
this.nativ = Peripheral.wrap(addr);
|
||||
this.addr = addr;
|
||||
|
||||
KernelEvents.instance.onMonitorResize.handle(addr -> {
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package kernel.service;
|
||||
|
||||
import kernel.ps.Process;
|
||||
import kernel.ps.ProcessManager;
|
||||
import kernel.ps.ProcessHandle;
|
||||
import kernel.binstore.BinStore;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class Service {
|
||||
private final binName:String;
|
||||
private final name:String;
|
||||
private final args:Array<String>;
|
||||
private var pid:PID;
|
||||
public final binName:String;
|
||||
public final name:String;
|
||||
public final args:Array<String>;
|
||||
public var pid:PID;
|
||||
public var ps: Process;
|
||||
|
||||
@:allow(kernel.service.ServiceManager)
|
||||
private function new(binName: String,name: String,?args: Array<String> ) {
|
||||
@@ -26,9 +27,9 @@ class Service {
|
||||
throw new Error('Bin ${this.binName} not found');
|
||||
}
|
||||
|
||||
var ps = Type.createInstance(bin.c,this.args);
|
||||
this.ps = Type.createInstance(bin.c,this.args);
|
||||
|
||||
this.pid = ProcessManager.run(ps,{});
|
||||
this.pid = ProcessManager.run(this.ps,{});
|
||||
}
|
||||
|
||||
public function stop() {
|
||||
|
||||
@@ -7,7 +7,7 @@ import lib.KVStore;
|
||||
using tink.CoreApi;
|
||||
|
||||
class ServiceManager {
|
||||
public static var instace: ServiceManager;
|
||||
public static var instance: ServiceManager;
|
||||
|
||||
private final services:Map<String,Service> = new Map();
|
||||
|
||||
@@ -19,7 +19,7 @@ class ServiceManager {
|
||||
/**
|
||||
Add a service to be automatically started.
|
||||
**/
|
||||
private function enable(name: String) {
|
||||
public function enable(name: String) {
|
||||
if (!this.services.exists(name)){
|
||||
return; // Service must be started
|
||||
}
|
||||
@@ -128,4 +128,16 @@ class ServiceManager {
|
||||
}
|
||||
return running;
|
||||
}
|
||||
|
||||
public function get(name: String): Null<Dynamic> {
|
||||
if (!this.services.exists(name)){
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Maybe there is a way to check types here?
|
||||
|
||||
var srv = this.services.get(name);
|
||||
return srv.ps;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ class WindowManager {
|
||||
}
|
||||
|
||||
public function getOutputs():ReadOnlyArray<String> {
|
||||
var arr = Peripheral.instance.getScreens().map(screen -> return screen.getAddr());
|
||||
var arr = Peripheral.instance.getAllScreens().map(screen -> return screen.getAddr());
|
||||
arr.push("main");
|
||||
return arr;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ class KVStore {
|
||||
|
||||
public function new(namespace: String) {
|
||||
this.namespace = namespace;
|
||||
this.load();
|
||||
}
|
||||
|
||||
public static function removeNamespace(namespace: String): Void {
|
||||
|
||||
66
src/lib/RessourceNames.hx
Normal file
66
src/lib/RessourceNames.hx
Normal file
@@ -0,0 +1,66 @@
|
||||
package lib;
|
||||
|
||||
import bin.srsc.PackageTypes.UnregisterRequest;
|
||||
import kernel.log.Log;
|
||||
import bin.srsc.PackageTypes.RegisterRequest;
|
||||
import bin.srsc.PackageTypes.GetRequest;
|
||||
import bin.srsc.SiteRessourceController;
|
||||
import kernel.net.Net;
|
||||
import kernel.net.Package.NetworkID;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class RessourceNames {
|
||||
private static final SITE_CONTROLLER:NetworkID = 0; // Temporary TODO: Change to real ID
|
||||
|
||||
public static function get(name: String, controllerID: NetworkID = -1): Promise<Null<NetworkID>> {
|
||||
if (controllerID == -1) controllerID = SITE_CONTROLLER;
|
||||
|
||||
var payload: GetRequest = {name: name, type: "get"};
|
||||
|
||||
return Net.instance.sendAndAwait(
|
||||
SITE_CONTROLLER,
|
||||
SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO,
|
||||
payload
|
||||
).map((res)->{
|
||||
switch (res){
|
||||
case Success(pkg):
|
||||
return Success(pkg.data.netID);
|
||||
case Failure(error):
|
||||
return Failure(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function register(name: String, netID: NetworkID, controllerID: NetworkID = -1): Promise<Bool> {
|
||||
if (controllerID == -1) controllerID = SITE_CONTROLLER;
|
||||
|
||||
var payload: RegisterRequest = {name: name, netID: netID, type: "register"};
|
||||
|
||||
return Net.instance.sendAndAwait(
|
||||
SITE_CONTROLLER,
|
||||
SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO,
|
||||
payload
|
||||
).map((res)->{
|
||||
switch (res){
|
||||
case Success(pkg):
|
||||
return Success(pkg.data.success);
|
||||
case Failure(error):
|
||||
return Failure(error);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function unregister(name: String, controllerID: NetworkID = -1): Promise<Noise> {
|
||||
if (controllerID == -1) controllerID = SITE_CONTROLLER;
|
||||
|
||||
var payload: UnregisterRequest = {name: name, type: "unregister"};
|
||||
|
||||
return Net.instance.sendAndAwait(
|
||||
SITE_CONTROLLER,
|
||||
SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO,
|
||||
payload
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user