/* * Created by Andrey Cherkashin (acherkashin) * http://acherkashin.me * * License * Copyright (c) 2015 Andrey Cherkashin * The project released under the MIT license: http://opensource.org/licenses/MIT */ package ragefist.core.environment; import java.util.logging.Level; import java.util.logging.Logger; import org.luaj.vm2.Globals; import org.luaj.vm2.LuaError; import org.luaj.vm2.lib.jse.JsePlatform; import ragefist.core.Processor; import ragefist.core.distribution.DistributedServerController.DistributedServerControllerAPI; import ragefist.core.environment.bindings.LuaEnvironment; import ragefist.core.environment.bindings.LuaProcessor; import ragefist.extension.Extension; /** * * @author acherkashin */ public class EnvironmentProcessor extends Processor implements Runnable { private static final Logger _log = Logger.getLogger(EnvironmentProcessor.class.getName()); private Globals _globals; private final int _id; private final String _luaConnectorFile; private final String _luaMainFile; private final DistributedServerControllerAPI _serverControllerAPI; private LuaEnvironment _luaEnviroment; private LuaProcessor _luaProcessor; // ---------------------------------------------------------------------- // // BINDING INTERFACE // ---------------------------------------------------------------------- // public final class EnvironmentBindingAPI { public int getEnvironmentId() { return _id; } public void processTasks() { EnvironmentProcessor.this.processTasks(); } public void reload() { reload(); } public Globals getGlobals() { return _globals; } public DistributedServerControllerAPI getServerController() { return _serverControllerAPI; } } // ---------------------------------------------------------------------- // // BUILDER // ---------------------------------------------------------------------- // public final static class EnvironmentBuilder { public int id = 0; public DistributedServerControllerAPI serverControllerAPI; public String luaConnectorFile; public String luaMainFile; protected void _checkBuildThrowExceptions() throws IllegalArgumentException { if (id < 0) { throw new IllegalArgumentException("EnvironmentBuilder.id < 0"); } if (luaMainFile == null || luaMainFile.isEmpty()) { throw new IllegalArgumentException("EnvironmentBuilder.luaMainFile is null or empty"); } if (serverControllerAPI == null) { throw new IllegalArgumentException("EnvironmentBuilder.serverControllerAPI is null"); } } public final EnvironmentProcessor build() throws IllegalArgumentException { _checkBuildThrowExceptions(); return new EnvironmentProcessor(this); } } private EnvironmentProcessor(EnvironmentBuilder builder) { _id = builder.id; _luaConnectorFile = builder.luaConnectorFile; _luaMainFile = builder.luaMainFile; _serverControllerAPI = builder.serverControllerAPI; } // ---------------------------------------------------------------------- // // PUBLIC INTERFACES // ---------------------------------------------------------------------- // public Globals getGlobals() { return _globals; } public EnvironmentBindingAPI newBindingAPI() { return this.new EnvironmentBindingAPI(); } // ---------------------------------------------------------------------- // // MAIN LOOP // ---------------------------------------------------------------------- // @Override public void run() { _globals = JsePlatform.standardGlobals(); // Lua bindings _luaEnviroment = new LuaEnvironment(this.new EnvironmentBindingAPI()); _luaProcessor = new LuaProcessor(this.new EnvironmentBindingAPI()); // Initialize bindings _globals.load(_luaEnviroment); _globals.load(_luaProcessor); // Initialize extensions' bindings for(Extension extension : _serverControllerAPI.getExtensions()) { extension.initializeLuaBindings(this); } // Package path _globals.load("package.path = package.path .. \";./lua/?.lua\"").call(); // Start Lua engine reload(); try { _globals.loadfile("./lua/" + _luaMainFile).call(); } catch(LuaError ex) { _log.log(Level.SEVERE, "LUA ERROR: ", ex); } while(true) { this.processTasks(); _luaProcessor.processTasks(); try { Thread.sleep(1); } catch (InterruptedException ex) { Logger.getLogger(EnvironmentProcessor.class.getName()).log(Level.SEVERE, null, ex); } } } public void reload() { _log.info("Environment " + _id + " is reloaded"); try { _globals.loadfile("./lua/" + _luaConnectorFile).call(); } catch(LuaError ex) { _log.log(Level.SEVERE, "LUA ERROR: ", ex); } } }