Trading Loop Inizialized Complete at the Browser

This commit is contained in:
Luis Fernando Molina committed 2018-05-23 12:47:55 +02:00
1 parent a14a7f655f
commit dbae3aeb1e
6 files changed
+427 -21

No files matched your search

+13 -13
View File
@@ -13,17 +13,17 @@
logger.initialize();
let thisObject = {
requireBot: requireBot,
requireCommons: requireCommons
downloadBot: downloadBot,
downloadCommons: downloadCommons
};
return thisObject;
function requireBot(pCloudStorage, pProcessConfig, callBackFunction) {
function downloadBot(pCloudStorage, pProcessConfig, callBackFunction) {
try {
if (FULL_LOG === true) { logger.write("[INFO] requireBot -> Entering function."); }
if (FULL_LOG === true) { logger.write("[INFO] downloadBot -> Entering function."); }
let filePath = "Bots" + "/" + bot.devTeam + "/" + bot.repo + "/" + pProcessConfig.name + "/" + "User.Bot.js";
@@ -31,37 +31,37 @@
function onDownloaded(pModule) {
callBackFunction(global.DEFAULT_OK_RESPONSE, pModule);
callBackFunction(window.DEFAULT_OK_RESPONSE, pModule);
return;
}
} catch (err) {
logger.write("[ERROR] requireBot -> err = " + err.message);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
logger.write("[ERROR] downloadBot -> err = " + err.message);
callBackFunction(window.DEFAULT_FAIL_RESPONSE);
}
}
function requireCommons(pCloudStorage, callBackFunction) {
function downloadCommons(pCloudStorage, callBackFunction) {
try {
if (FULL_LOG === true) { logger.write("[INFO] requireCommons -> Entering function."); }
if (FULL_LOG === true) { logger.write("[INFO] downloadCommons -> Entering function."); }
let filePath = bot.devTeam + "/" + bot.repo + "/" + "Commons.js";
let filePath = "Bots" + "/" + bot.devTeam + "/" + bot.repo + "/" + "Commons.js";
downloadModule(filePath, onDownloaded)
function onDownloaded(pModule) {
callBackFunction(global.DEFAULT_OK_RESPONSE, pModule);
callBackFunction(window.DEFAULT_OK_RESPONSE, pModule);
return;
}
} catch (err) {
logger.write("[ERROR] requireCommons -> err = " + err.message);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
logger.write("[ERROR] downloadCommons -> err = " + err.message);
callBackFunction(window.DEFAULT_FAIL_RESPONSE);
}
}
+3 -1
View File
@@ -35,6 +35,9 @@
<Compile Include="CloudVM\CloudRequire.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="Server\BotsScripts.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="Server\Permissions.js">
<SubType>Code</SubType>
</Compile>
@@ -75,7 +78,6 @@
<Compile Include="NewBot.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="Scripts\DummyWeb.js" />
<Compile Include="Scripts\require.js" />
<Compile Include="Server\CloudScripts.js">
<SubType>Code</SubType>
-5
View File
@@ -1,5 +0,0 @@
function newDummyWeb() {
}
+334
View File
@@ -0,0 +1,334 @@
exports.newBotScripts = function newBotScripts() {
let thisObject = {
loadBotScripts: loadBotScripts,
initialize: initialize
}
const GITHUB = require('../Server/Github');
let github = GITHUB.newGithub();
const STORAGE = require('../Server/Storage');
let storage = STORAGE.newStorage();
let serverConfig;
let githubData;
let storageData;
let fileSystemData;
let ecosystem;
let ecosystemObject;
return thisObject;
function initialize(pEcosystem, pEcosystemObject, pServerConfig, pGithubData, pStorageData, pFileSystemData, callBackFunction) {
serverConfig = pServerConfig;
githubData = pGithubData;
fileSystemData = pFileSystemData;
storageData = pStorageData;
ecosystem = pEcosystem;
ecosystemObject = pEcosystemObject;
callBackFunction();
}
function loadBotScripts(callBackfunction) {
try {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Entering function."); }
/*
Here we go through the webModules Array and bring them all.
*/
let modulesToBeRetrieved = 0;
let modulesRetrieved = 0;
for (let i = 0; i < ecosystemObject.devTeams.length; i++) {
let devTeam = ecosystemObject.devTeams[i];
for (let j = 0; j < devTeam.bots.length; j++) {
let bot = devTeam.bots[j];
getCommons(devTeam.codeName, bot.repo, onCommons);
function onCommons() {
for (let k = 0; k < bot.processes.length; k++) {
let process = bot.processes[k];
modulesToBeRetrieved++;
getUserBot(devTeam.codeName, bot.repo, process.name, onUserBot);
function onUserBot() {
modulesRetrieved++;
if (modulesRetrieved === modulesToBeRetrieved) {
callBackfunction();
}
}
}
}
}
}
function getCommons(pDevTeam, pBot, callBack) {
let moduleName = "Commons.js";
let key = pDevTeam + "." + pBot;
let path = pDevTeam + "/" + pBot;
switch (serverConfig.configAndPlugins.Location) {
case 'Cloud': {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> Entering Case."); }
storage.getStorageData(path, moduleName, onDataArrived);
function onDataArrived(pData) {
try {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); }
if (pData !== undefined) {
let data = pData.toString();
data = data.trim(); // remove first byte with some encoding.
data = browserify(data);
fileSystemData.set(key, data);
}
callBack();
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err);
}
}
break;
}
case 'File System': {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> File System -> Entering Case."); }
let fs = require('fs');
try {
let fileName = '../Bots/' + path + "/" + moduleName;
fs.readFile(fileName, onFileRead);
function onFileRead(err, pData) {
try {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> File System -> onFileRead -> Entering function."); }
if (pData !== undefined) {
let data = pData.toString();
data = data.trim(); // remove first byte with some encoding.
data = browserify(data);
fileSystemData.set(key, data);
}
callBack();
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> File System -> onFileRead -> File = " + fileName + " Error = " + err);
}
}
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> File System -> File = " + fileName + " Error = " + err);
}
break;
}
case 'Github': {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Github -> Entering Case."); }
github.getGithubData(path, moduleName, onDataArrived);
function onDataArrived(pData) {
try {
if (pData !== undefined) {
let data = pData.toString();
data = data.trim(); // remove first byte with some encoding.
data = browserify(data);
fileSystemData.set(key, data);
}
callBack();
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> Github -> onDataArrived -> Error = " + err);
}
}
break;
}
}
}
function getUserBot(pDevTeam, pBot, pProcess, callBack) {
let moduleName = "User.Bot.js";
let key = pDevTeam + "." + pBot + "." + pProcess;
let path = pDevTeam + "/" + pBot + "/" + pProcess;
switch (serverConfig.configAndPlugins.Location) {
case 'Cloud': {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> Entering Case."); }
storage.getStorageData(path, moduleName, onDataArrived);
function onDataArrived(pData) {
try {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); }
if (pData !== undefined) {
let data = pData.toString();
data = data.trim(); // remove first byte with some encoding.
data = browserify(data);
fileSystemData.set(key, data);
}
callBack();
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err);
}
}
break;
}
case 'File System': {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> File System -> Entering Case."); }
let fs = require('fs');
try {
let fileName = '../Bots/' + path + "/" + moduleName;
fs.readFile(fileName, onFileRead);
function onFileRead(err, pData) {
try {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> File System -> onFileRead -> Entering function."); }
if (pData !== undefined) {
let data = pData.toString();
data = data.trim(); // remove first byte with some encoding.
data = browserify(data);
fileSystemData.set(key, data);
}
callBack();
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> File System -> onFileRead -> File = " + fileName + " Error = " + err);
}
}
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> File System -> File = " + fileName + " Error = " + err);
}
break;
}
case 'Github': {
if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Github -> Entering Case."); }
github.getGithubData(path, moduleName, onDataArrived);
function onDataArrived(pData) {
try {
if (pData !== undefined) {
let data = pData.toString();
data = data.trim(); // remove first byte with some encoding.
data = browserify(data);
fileSystemData.set(key, data);
}
callBack();
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> Github -> onDataArrived -> Error = " + err);
}
}
break;
}
}
}
function browserify(pData) {
let result = pData.substring(pData.indexOf('=') + 2);
result = replaceAll(result, "require", "webRequire");
result = replaceAll(result, "global", "window");
return result;
function replaceAll(text, search, replacement) {
return text.replace(new RegExp(search, 'g'), replacement);
}
}
}
catch (err) {
console.log("[ERROR] BotScripts -> retrieveScripts -> Error = " + err);
}
}
}
+1
View File
@@ -10,6 +10,7 @@
<script type="text/javascript" src="Scripts/Require.js"></script>
<script type="text/javascript" src="CloudVM/CloudRequire.js"></script>
<script type="text/javascript" src="CloudVM/CloudSupport.js"></script>
<script type="text/javascript" src="CloudVM/WebFS.js"></script>
+76 -2
View File
@@ -77,9 +77,21 @@ function initialize() {
function onInitialized() {
HTMLCloudScripts = pHTMLCloudScripts;
startHtttpServer();
const BOT_SCRIPTS = require('./Server/BotsScripts');
let botScripts = BOT_SCRIPTS.newBotScripts();
botScripts.initialize(ecosystem, ecosystemObject, serverConfig, githubData, storageData, fileSystemData, onInitialized);
function onInitialized() {
botScripts.loadBotScripts(onScriptsLoaded);
function onScriptsLoaded(){
HTMLCloudScripts = pHTMLCloudScripts;
startHtttpServer();
}
}
}
}
}
@@ -412,6 +424,68 @@ function onBrowserRequest(request, response) {
}
break;
case "Bots": // This means the cloud folder.
{
let map;
switch (serverConfig.configAndPlugins.Location) {
case 'Cloud': {
if (CONSOLE_LOG === true) { console.log("[INFO] server -> onBrowserRequest -> readEcosystemConfig -> Cloud -> Entering Case."); }
map = storageData;
break;
}
case 'File System': {
if (CONSOLE_LOG === true) { console.log("[INFO] server -> onBrowserRequest -> readEcosystemConfig -> File System -> Entering Case."); }
map = fileSystemData;
break;
}
case 'Github': {
if (CONSOLE_LOG === true) { console.log("[INFO] server -> onBrowserRequest -> readEcosystemConfig -> Github -> Entering Case."); }
github.getGithubData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived);
map = githubData;
break;
}
}
let key;
if (requestParameters[4] !== undefined) {
key = requestParameters[2] + "." + requestParameters[3] + "." + requestParameters[4];
} else {
key = requestParameters[2] + "." + requestParameters[3];
}
let script = map.get(key);
if (script !== undefined) {
respondWithContent(script, response);
} else {
if (CONSOLE_LOG === true) { console.log("[WARN] server -> onBrowserRequest -> readEcosystemConfig -> Script Not Found."); }
if (CONSOLE_LOG === true) { console.log("[WARN] server -> onBrowserRequest -> readEcosystemConfig -> key = " + key); }
respondWithContent("", response);
}
}
break;
case "Plotters": // This means the plotter folder, not to be confused with the Plotters script!
{