Page MenuHomeSealhub

No OneTemporary

diff --git a/lib/config/config-manager.js b/lib/config/config-manager.js
index 013f5a1f..51ea5eed 100644
--- a/lib/config/config-manager.js
+++ b/lib/config/config-manager.js
@@ -1,24 +1,54 @@
var fs = require("fs");
var path = require("path");
+var merge = require("merge");
+
var ConfigManager = new function(){
- this.config = {};
+ default_config = {};
+ config = {};
+
+ function modify_config(config_object, key, value){
+ var key_elements = key.split('.');
+ var new_values = {};
+ var current_position = new_values;
+ for(var i=0; i<key_elements.length-1; i++){
+ current_position[key] = {};
+ current_position = current_position[key];
+ }
+ current_position[key_elements[key_elements.length-1]]=value;
+ default_config = merge.recursive(default_config, new_values);
+ }
- this.set_config = function(config){
- this.config = config;
+ this.set_config = function(key, new_config){
+ if(arguments.length==1){
+ config = config;
+ }else{
+ modify_config(config, key, new_config);
+ }
}
- this.getConfiguration = function(){
- return this.config;
+ this.set_default_config = function(key, value){
+ if(arguments.length==1){
+ default_config = default_config;
+ }else{
+ modify_config(default_config, key, value);
+ }
}
+ this.get_configuration = function(){
+ return merge(default_config, config);
+ }
+
+ this.get_config = this.get_configuration;
+
this.get_chip_config = function(longid){
- return this.config.chip_config && this.config.chip_config[longid];
+ return config.chip_config && config.chip_config[longid];
}
this.get_dispatcher_config = function(){
- return this.config.dispatcher_config || {};
+ return config.dispatcher_config || {};
}
+
}
module.exports = ConfigManager;
\ No newline at end of file
diff --git a/lib/dispatchers/dispatcher-distributed.js b/lib/dispatchers/dispatcher-distributed.js
index 86cb864e..c0d6c1f6 100644
--- a/lib/dispatchers/dispatcher-distributed.js
+++ b/lib/dispatchers/dispatcher-distributed.js
@@ -1,148 +1,161 @@
var io_server = require('socket.io');
var io_client = require('socket.io-client');
var Dispatcher = require("./dispatcher.js");
-var config = require("../config/config-manager.js").getConfiguration();
+var ConfigManager = require("../config/config-manager.js");
DispatcherDistributed = function(layer_name){
this.callback_queue = {};
this.error_queue = {};
this.socket_client = null;
this.socket_server = null;
this.max_request_id = 0;
this.layer_name = layer_name;
+ ConfigManager.set_default_config(
+ "layer_config",
+ {
+ "db":{
+ "host": "localhost",
+ "port": "7766"
+ },
+ "biz":{
+ "host": "localhost",
+ "port": "7767"
+ }
+ }
+ );
}
DispatcherDistributed.prototype = Object.create(Dispatcher.prototype);
DispatcherDistributed.prototype.get_request_id = function(){
this.max_request_id+=1;
return this.max_request_id;
}
DispatcherDistributed.prototype.add_callback_to_queue = function(id, callback, error_handler){
this.callback_queue[id] = callback;
this.error_queue[id] = error_handler;
}
DispatcherDistributed.prototype.resolve_to_callback = function(response_data){
var callback = this.callback_queue[response_data.request_id];
callback(response_data.payload);
delete this.callback_queue[response_data.request_id];
delete this.error_queue[response_data.request_id];
}
DispatcherDistributed.prototype.resolve_to_error = function(response_error){
var error_handler = this.error_queue[response_error.request_id];
if(error_handler){
error_handler(response_error);
}
delete this.callback_queue[response_error.request_id];
delete this.error_queue[response_error.request_id];
}
DispatcherDistributed.prototype.call_over_socket = function(method_name, args){
console.log("calling a remote procedure:", method_name, "with arguments:", args);
var id = this.get_request_id();
this.socket_client.emit("request", {
method_name: method_name,
arguments: args,
request_id: id
});
return new Promise(function(resolve, reject){
this.add_callback_to_queue(id, function(data){
console.log("dispatcher-web.js data", data);
resolve(data);
}, function(error){
- console.log("error in promise:", error.error);
- reject(error.error)
+ console.log("error in promise:", error.error);
+ reject(new Sealious.Errors.Error(error.message, error.error));
});
}.bind(this))
}
DispatcherDistributed.prototype.connect_to_layer = function(layer_name){
- if(config.layer_config[layer_name]==undefined){
+ var layer_config = ConfigManager.get_config().layer_config;
+ if(layer_config[layer_name]==undefined){
throw new Sealious.Errors.Error("Dispatcher tried to connect to layer of name `" + layer_name + "`, but this layer was not found in config.json.");
}
- var the_other_layer_config = config.layer_config[layer_name];
+ var the_other_layer_config = layer_config[layer_name];
var connection_url = "http://" + the_other_layer_config.host + ":" + the_other_layer_config.port;
this.socket_client = io_client.connect(connection_url, {reconnect: true});
this.socket_client.on("response", function(data){
this.resolve_to_callback(data);
}.bind(this));
this.socket_client.on("throw_error", function(data){
this.resolve_to_error(data);
}.bind(this));
}
DispatcherDistributed.prototype.listen = function(){
var that = this;
this.socket_server = io_server();
- var cfg = config.layer_config[this.layer_name];
+ var cfg = ConfigManager.get_config().layer_config[this.layer_name];
this.socket_server.listen(cfg.port);
console.log(this.layer_name + " layer listening on :" + cfg.port);
this.socket_server.on('connection', function(socket) {
console.log("dispatcher-distributed: a layer from above connected");
socket.on("request", function(data) {
var method_name = data.method_name;
var arguments = data.arguments;
console.log(arguments);
console.log(Object.keys(arguments));
var arguments_array = Object.keys(arguments).map(function (key) {console.log(data.arguments[key]); return data.arguments[key]});
var request_id = data.request_id;
console.log("received a request to call procedure `" + method_name + "` with arguments ", arguments_array);
var method_path = method_name.split(".");
var method_to_call = that;
for (var i = 0; i < method_path.length; i++) {
method_to_call = method_to_call[method_path[i]];
};
method_to_call.apply(that, arguments_array)
.then(function(result) {
var response = {};
response.request_id = request_id;
response.payload = result;
socket.emit("response", response);
}).catch(function(err) {
console.log("caught error:", err);
var response = {};
response.type = "error";
response.error = err;
response.request_id = request_id;
console.log("responding with ", response);
console.log(err instanceof Error, err.stack);
socket.emit("throw_error", response);
});
});
socket.on('logout', function() {
socket.disconnect(); // zamknięcie połączenia
});
socket.on('disconnect', function() {
console.log("Upper layer disconnected"); // event rozłączenia
});
});
}
DispatcherDistributed.prototype.layer_order = ["web", "biz", "db"]
DispatcherDistributed.prototype.start = function(){
var current_layer_index = this.layer_order.indexOf(this.layer_name);
console.log("CURRENT LAYER NAME:", this.layer_name);
console.log("CURRENT LAYER INDEX:", current_layer_index);
if(this.layer_order[current_layer_index+1]!=undefined){ //if we're not on the bottom-most layer
this.connect_to_layer(this.layer_order[current_layer_index+1])
}
if(this.layer_order[current_layer_index-1]!=undefined){
this.listen();
}
- var cfg = config.layer_config[this.layer_name];
}
module.exports = DispatcherDistributed;
\ No newline at end of file
diff --git a/lib/response/error.js b/lib/response/error.js
index f30de393..223ac5cf 100644
--- a/lib/response/error.js
+++ b/lib/response/error.js
@@ -1,95 +1,96 @@
var Response = require("./response.js");
var SealiousErrors = {};
-SealiousErrors.Error = function(message){
- var ret = new Error(message);
- ret.message = message;
- ret.is_user_fault = false;
- ret.is_developer_fault;
- ret.type = "error";
- ret.http_code = 500;
- return ret;
+SealiousErrors.Error = function(message, params){
+ //var ret = new Error(message);
+ params = params || {};
+ this.message = message;
+ this.is_error = true;
+ this.is_user_fault = params.is_user_fault===undefined? false : params.is_user_fault;
+ this.is_developer_fault = params.is_developer_fault===undefined? false : params.is_developer_fault;
+ this.type = params.type===undefined? "error" : params.type;
+ this.http_code = params.http_code===undefined? 500 : params.http_code;
}
SealiousErrors.Error.prototype = new function(){
this.is_error = true;
this.toResponse = function(){
return new Response({}, true, this.type, this.message);
}
this.toString = function(){
}
}
SealiousErrors.ValidationError = function(message){
var err = new SealiousErrors.Error(message);
err.is_user_fault = true;
err.type="validation";
return err;
}
SealiousErrors.ValueExists = function(message){
var err = new SealiousErrors.Error(message);
err.is_user_fault = true;
err.type="valueExists";
this.http_code = 409;
return err;
}
SealiousErrors.InvalidCredentials = function(message){
var err = new SealiousErrors.Error(message);
err.is_user_fault = true;
err.type="validation";
err.http_code = 401;
return err;
}
SealiousErrors.NotFound = function(message){
var err = new SealiousErrors.Error(message);
err.is_user_fault = true;
err.type="not_found";
err.http_code = 404;
return err;
}
SealiousErrors.InternalConnectionError = function(message){
var err = new SealiousErrors.Error(message);
err.is_user_fault = false;
err.type="internal_connection_error";
err.http_code = 500;
return err;
}
SealiousErrors.DependencyError = function(message){
var err = new SealiousErrors.Error(message);
err.is_user_fault = false;
err.is_developer_fault = true;
err.type="dependency_error";
err.http_code = 500;
return err;
}
/*
process.on('uncaughtException', function (exception) {
switch(exception.code){
case "EACCES":
console.log("\nNo root access - closing!\n");
process.exit();
break;
case "EADDRINUSE":
console.log("\nPort 80 is already taken - closing!\n");
process.exit();
break;
default:
console.log(exception.stack);
throw exception;
break;
}
});
*/
module.exports = SealiousErrors;
\ No newline at end of file
diff --git a/package.json b/package.json
index 9707dc9d..d045a4bb 100644
--- a/package.json
+++ b/package.json
@@ -1,53 +1,53 @@
{
"name": "sealious",
"version": "0.1.14",
"description": "",
"main": "./main.js",
"scripts": {
"test": "mocha ./tests/test.js && mocha ./tests/test.js -R travis-cov"
},
"bin": {
"sealious": "./shell.js"
},
"repository": {
"type": "git",
"url": "https://github.com/groovy354/Prometheus.git"
},
"keywords": [
"prometheus"
],
"author": "Kuba Orlik",
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/groovy354/Prometheus/issues"
},
"dependencies": {
"Set": "~0.4.1",
"bluebird": "~2.3.11",
"boom": "~2.6.0",
"glue": "~1.0.0",
"hapi": ">8.0.0",
"http": "0.0.0",
"ip": "~0.3.2",
- "merge": "~1.2.0",
+ "merge": "^1.2.0",
"open": "0.0.5",
"query-string": "~1.0.0",
"socket.io": "~1.2.0",
"socket.io-client": "~1.2.0"
},
"devDependencies": {
"blanket": "1.1.6",
"mocha": "*",
"npm-server": "0.0.1",
"should": "~4.4.0",
"travis-cov ": "*"
},
"config": {
"blanket": {
"pattern": "node_modules"
}
},
- "peerDependencies":{
+ "peerDependencies": {
"sealious-base-chips": "^0.0.6"
}
}

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 22, 07:31 (5 m, 56 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
547628
Default Alt Text
(12 KB)

Event Timeline