Comparing version 0.5.1 to 0.6.0
@@ -33,3 +33,4 @@ "use strict"; | ||
Constants.PrimativeTypeBoolean = "boolean"; | ||
Constants.RetainParam = "retain"; | ||
module.exports = Constants; |
@@ -20,3 +20,14 @@ "use strict"; | ||
/* create new object from description */; | ||
if (this.json.has(Constants.KeyParam) && this.translator.hasReference(this.json.get(Constants.KeyParam))) { | ||
if (aClass === null){ | ||
newInstance = {}; | ||
newInstance.constructor.__jjjrmi = { | ||
transient : !this.json.json.retain, | ||
class : this.json.json.type | ||
}; | ||
newInstance.constructor.__isTransient = function(){return this.__jjjrmi.transient;}; | ||
newInstance.constructor.__isEnum = function(){return false;}; | ||
newInstance.constructor.__getClass = function(){return this.__jjjrmi.class;}; | ||
} else if (this.json.has(Constants.KeyParam) && this.translator.hasReference(this.json.get(Constants.KeyParam))) { | ||
newInstance = this.translator.getReferredObject(this.json.get(Constants.KeyParam)); | ||
@@ -30,7 +41,7 @@ return newInstance; | ||
} | ||
if (newInstance.constructor.__isTransient()) this.translator.addTempReference(this.json.get(Constants.KeyParam), newInstance); | ||
else this.translator.addReference(this.json.get(Constants.KeyParam), newInstance); | ||
if (!aClass.__isTransient()) this.translator.addReference(this.json.get(Constants.KeyParam), newInstance); | ||
else this.translator.addTempReference(this.json.get(Constants.KeyParam), newInstance); | ||
if (this.translator.hasHandler(aClass)) { | ||
if (aClass && this.translator.hasHandler(aClass)) { | ||
let handler = this.translator.getHandler(aClass); | ||
@@ -37,0 +48,0 @@ handler.decode(this, newInstance); |
@@ -17,18 +17,3 @@ "use strict"; | ||
class Encoder { | ||
constructor(object, translator, keys) { | ||
if (object !== null && typeof object === "object" && !object instanceof Array) { | ||
if (typeof object.constructor === "undefined") { | ||
throw new Error(`Object missing constructor.`); | ||
} | ||
if (typeof object.constructor.__isTransient !== "function") { | ||
EncoderInterfaceException(object, "__isTransient"); | ||
} | ||
if (typeof object.constructor.__isEnum !== "function") { | ||
EncoderInterfaceException(object, "__isEnum"); | ||
} | ||
if (typeof object.constructor.__getClass !== "function") { | ||
EncoderInterfaceException(object, "__getClass"); | ||
} | ||
} | ||
constructor(object, translator, keys) { | ||
this.object = object; | ||
@@ -60,13 +45,2 @@ this.translator = translator; | ||
/* The object's class is expected to be generated from java at this point */ | ||
if (typeof this.object.constructor.__isEnum !== "function"){ | ||
throw new EncoderInterfaceException(this.object, "__isEnum"); | ||
} | ||
if (typeof this.object.constructor.__getClass !== "function"){ | ||
throw new EncoderInterfaceException(this.object, "__getClass"); | ||
} | ||
if (typeof this.object.constructor.__isTransient !== "function"){ | ||
throw new EncoderInterfaceException(this.object, "__isTransient"); | ||
} | ||
/* is Enum */ | ||
@@ -86,3 +60,3 @@ /* handler has been registered */ | ||
} | ||
else if (this.object.constructor.__isEnum()) { | ||
else if (this.object.constructor.__isEnum && this.object.constructor.__isEnum()) { | ||
return new EncodedEnum(this.object, this.translator, this.keys).toJSON(); | ||
@@ -173,3 +147,11 @@ } | ||
this.json[Constants.TypeParam] = this.object.constructor.__getClass(); | ||
if (!this.object.constructor || !this.object.constructor.__getClass){ | ||
this.json[Constants.TypeParam] = ""; | ||
} else { | ||
this.json[Constants.TypeParam] = this.object.constructor.__getClass(); | ||
} | ||
if (typeof this.json[Constants.RetainParam] === "undefined"){ | ||
this.json[Constants.RetainParam] = !this.object.constructor.__isTransient(); | ||
} | ||
this.json[Constants.FieldsParam] = {}; | ||
@@ -180,3 +162,3 @@ | ||
if (this.object.constructor.__isTransient()) { | ||
if (typeof object.constructor.__isTransient !== "function" || this.object.constructor.__isTransient()) { | ||
this.translator.addTempReference(key, this.object); | ||
@@ -186,7 +168,2 @@ } else { | ||
} | ||
if (typeof object.constructor.__isTransient !== "function") { | ||
window.object = object; | ||
throw new Error(`Field '__isTransient' of class '${object.constructor.name}' is not of type function, found type '${typeof object.constructor.__isTransient}'.`); | ||
} | ||
} | ||
@@ -196,3 +173,5 @@ | ||
for (let field in this.object) { | ||
this.setField(field, this.object[field]); | ||
if (field !== "__jjjrmi"){ | ||
this.setField(field, this.object[field]); | ||
} | ||
} | ||
@@ -209,4 +188,7 @@ return this.json; | ||
setField(name, value) { | ||
if (typeof value === "function") return; | ||
let encodedValue = new Encoder(value, this.translator, this.keys).encode(); | ||
if (encodedValue !== null) this.json[Constants.FieldsParam][name] = encodedValue; | ||
if (encodedValue !== null){ | ||
this.json[Constants.FieldsParam][name] = encodedValue; | ||
} | ||
} | ||
@@ -213,0 +195,0 @@ |
@@ -5,2 +5,3 @@ "use strict"; | ||
const JJJMessageType = require("./JJJMessageType"); | ||
const LOGGER = require("./Logger"); | ||
@@ -12,3 +13,2 @@ class JJJRMISocket { | ||
this.callback = {}; | ||
this.flags = Object.assign(JJJRMISocket.flags); | ||
this.socket = null; | ||
@@ -33,3 +33,3 @@ this.translator.copyFrom(JJJRMISocket.classes); | ||
async connect(url) { | ||
if (this.flags.CONNECT) console.log(`${this.jjjSocketName} connecting`); | ||
LOGGER.log("connect", `${this.jjjSocketName} connecting`); | ||
if (!url) url = this.getAddress(); | ||
@@ -75,2 +75,3 @@ | ||
methodRequest(src, methodName, args) { | ||
/* method requests come after an object has been sent, this is a sanity check */ | ||
if (!this.translator.hasReferredObject(src)) { | ||
@@ -81,2 +82,3 @@ console.warn("see window.debug for source"); | ||
} | ||
let uid = this.nextUID++; | ||
@@ -88,2 +90,3 @@ let ptr = this.translator.getReference(src); | ||
/* promise function */ | ||
let f = function (resolve, reject) { | ||
@@ -94,7 +97,8 @@ this.callback[uid] = { | ||
}; | ||
let packet = new MethodRequest(uid, ptr, methodName, argsArray); | ||
let encodedPacket = this.translator.encode(packet); | ||
if (this.flags.SENT) console.log(encodedPacket); | ||
LOGGER.log("sent", encodedPacket); | ||
let encodedString = JSON.stringify(encodedPacket, null, 4); | ||
if (this.flags.SENT && this.flags.VERBOSE) console.log(encodedString); | ||
LOGGER.verbose("sent", encodedString); | ||
@@ -114,8 +118,7 @@ if (this.socket !== null) this.socket.send(encodedString); | ||
onMessage(evt) { | ||
if (this.flags.RECEIVED && this.flags.VERBOSE) { | ||
let json = JSON.parse(evt.data); | ||
console.log(JSON.stringify(json, null, 2)); | ||
} | ||
LOGGER.verbose("received", JSON.stringify(JSON.parse(evt.data), null, 2)); | ||
/* the main translation from json to js is triggered here */ | ||
let rmiMessage = this.translator.decode(evt.data); | ||
if (this.flags.RECEIVED) console.log(rmiMessage); | ||
LOGGER.log("received", rmiMessage); | ||
@@ -125,3 +128,3 @@ switch (rmiMessage.type) { | ||
{ | ||
if (this.flags.ONMESSAGE) console.log(this.jjjSocketName + " FORGET"); | ||
LOGGER.log("onmessage", this.jjjSocketName + " FORGET"); | ||
this.translator.removeByKey(rmiMessage.key); | ||
@@ -132,3 +135,4 @@ break; | ||
{ | ||
if (this.flags.CONNECT || this.flags.ONMESSAGE) console.log(this.jjjSocketName + " READY"); | ||
LOGGER.log("onmessage", this.jjjSocketName + " READY"); | ||
LOGGER.log("connect", this.jjjSocketName + " READY"); | ||
this.onready(rmiMessage.getRoot()); | ||
@@ -140,3 +144,3 @@ break; | ||
{ | ||
if (this.flags.ONMESSAGE) console.log(`Response to client side request: ${this.jjjSocketName} ${rmiMessage.methodName}`); | ||
LOGGER.log("onmessage", `Response to client side request: ${this.jjjSocketName} ${rmiMessage.methodName}`); | ||
let callback = this.callback[rmiMessage.uid]; | ||
@@ -150,12 +154,5 @@ delete(this.callback[rmiMessage.uid]); | ||
{ | ||
if (this.flags.ONMESSAGE) console.log(`Server side originated request: ${this.jjjSocketName} ${rmiMessage.methodName}`); | ||
LOGGER.log("onmessage", `Server side originated request: ${this.jjjSocketName} ${rmiMessage.methodName}`); | ||
let target = this.translator.getReferredObject(rmiMessage.ptr); | ||
this.remoteMethodCallback(target, rmiMessage.methodName, rmiMessage.args); | ||
// let response = new InvocationResponse(rmiMessage.uid, InvocationResponseCode.SUCCESS); | ||
// let encodedResponse = this.translator.encode(response); | ||
// let encodedString = JSON.stringify(encodedResponse, null, 4); | ||
// | ||
// if (this.flags.ONMESSAGE) console.log(`Server side request: ${this.jjjSocketName} ${target.constructor.name}.${rmiMessage.methodName}`); | ||
// if (socket !== null) this.socket.send(encodedString); | ||
// else console.warn(`Socket "${this.socketName}" not connected.`); | ||
break; | ||
@@ -165,4 +162,4 @@ } | ||
{ | ||
if (!this.flags.SILENT) console.log(this.jjjSocketName + " EXCEPTION " + rmiMessage.methodName); | ||
if (!this.flags.SILENT) console.warn(rmiMessage); | ||
LOGGER.log("exception", `${this.jjjSocketName} EXCEPTION ${rmiMessage.methodName}`); | ||
LOGGER.log("exception", `rmiMessage`); | ||
let callback = this.callback[rmiMessage.uid]; | ||
@@ -175,3 +172,4 @@ delete(this.callback[rmiMessage.uid]); | ||
{ | ||
if (this.flags.CONNECT || this.flags.ONMESSAGE) console.log(this.jjjSocketName + " REJECTED_CONNECTION"); | ||
LOGGER.log("connect", this.jjjSocketName + " REJECTED_CONNECTION"); | ||
LOGGER.log("onmessage", this.jjjSocketName + " REJECTED_CONNECTION"); | ||
this.onreject(); | ||
@@ -206,3 +204,3 @@ break; | ||
if (typeof aClass !== "function") { | ||
console.log(aClass); | ||
LOGGER.log("exception", aClass); | ||
throw new Error(`paramater 'class' of method 'registerClass' is '${typeof aClass}', expected 'function'`); | ||
@@ -215,8 +213,7 @@ } | ||
if (JJJRMISocket.flags.ONREGISTER) console.log(`Register ${aClass.__getClass()}`); | ||
LOGGER.log("onregister", `Register ${aClass.__getClass()}`); | ||
this.translator.registerClass(aClass); | ||
for (let field in aClass) { | ||
if (JJJRMISocket.flags.ONREGISTER && JJJRMISocket.flags.VERBOSE) console.log(`considering ${aClass.__getClass()}.${field}`); | ||
LOGGER.verbose("onregister", `considering ${aClass.__getClass()}.${field}`); | ||
if (typeof aClass[field] === "function" && typeof aClass[field].__getClass === "function") { | ||
@@ -237,12 +234,2 @@ this.registerClass(aClass[field]); | ||
JJJRMISocket.flags = { | ||
SILENT: false, /* do not print exceptions to console */ | ||
CONNECT: false, /* show the subset of ONMESSAGE that deals with the initial connection */ | ||
ONMESSAGE: false, /* describe the action taken when a message is received */ | ||
SENT: false, /* show the send object, versbose shows the json text as well */ | ||
RECEIVED: false, /* show the received server object, verbose shows the json text as well */ | ||
VERBOSE: false, /* print raw text for SENT / RECEIVED */ | ||
ONREGISTER: false /* report classes as they are registered */ | ||
}; | ||
JJJRMISocket.classes = new Map(); | ||
@@ -252,12 +239,10 @@ | ||
if (typeof aClass !== "function") { | ||
console.log(aClass); | ||
LOGGER.log("exception", aClass); | ||
throw new Error(`paramater 'class' of method 'registerClass' is '${typeof aClass}', expected 'function'`); | ||
} | ||
if (typeof aClass.__getClass !== "function") console.verbose("onregister", `__getClass not of type function`); | ||
else if (typeof aClass.__isEnum !== "function") console.verbose("onregister", `__isEnum not of type function`); | ||
else if (typeof aClass.__isTransient !== "function") console.verbose("onregister", `__isTransient not of type function`); | ||
if (JJJRMISocket.flags.ONREGISTER && JJJRMISocket.flags.VERBOSE) { | ||
if (typeof aClass.__getClass !== "function") console.log(`__getClass not of type function`); | ||
if (typeof aClass.__isEnum !== "function") console.log(`__isEnum not of type function`); | ||
if (typeof aClass.__isTransient !== "function") console.log(`__isTransient not of type function`); | ||
} | ||
if (typeof aClass.__getClass !== "function") return; | ||
@@ -267,3 +252,3 @@ if (typeof aClass.__isEnum !== "function") return; | ||
if (JJJRMISocket.flags.ONREGISTER) console.log(`Register ${aClass.__getClass()}`); | ||
LOGGER.log("onregister", `Register ${aClass.__getClass()}`); | ||
@@ -273,3 +258,3 @@ JJJRMISocket.classes.set(aClass.__getClass(), aClass); | ||
for (let field in aClass) { | ||
if (JJJRMISocket.flags.ONREGISTER && JJJRMISocket.flags.VERBOSE) console.log(`considering ${aClass.__getClass()}.${field}`); | ||
LOGGER.verbose(`considering ${aClass.__getClass()}.${field}`); | ||
if (typeof aClass[field] === "function" && typeof aClass[field].__getClass === "function") { | ||
@@ -276,0 +261,0 @@ JJJRMISocket.registerClass(aClass[field]); |
{ | ||
"name": "jjjrmi", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "util", |
"use strict"; | ||
let JJJRMI = { | ||
javaEquivalent : { | ||
ArrayList : require("./java-equiv/ArrayList"), | ||
HashMap : require("./java-equiv/HashMap") | ||
}, | ||
JJJRMISocket : require("./JJJRMISocket") | ||
JJJRMISocket : require("./JJJRMISocket"), | ||
Logger : require("./Logger") | ||
}; | ||
module.exports = JJJRMI; |
"use strict"; | ||
let Encoder = require("./Encoder"); | ||
let Decoder = require("./Decoder"); | ||
let ArrayList = require("./java-equiv/ArrayList"); | ||
let ArrayList = require("./ArrayList"); | ||
let EncodedJSON = require("./EncodedJSON"); | ||
@@ -68,7 +68,16 @@ | ||
hasClass(classname){ | ||
return this.classmap.has(classname); | ||
} | ||
getClass(classname) { | ||
if (!this.classmap.has(classname)) throw new Error(`Class ${classname} not registered.`); | ||
if (!this.classmap.has(classname)) return null; | ||
return this.classmap.get(classname); | ||
} | ||
/** | ||
* Copy all registered classes from a given map. | ||
* @param {type} map | ||
* @returns {undefined} | ||
*/ | ||
copyFrom(map) { | ||
@@ -118,2 +127,8 @@ for (let classname of map.keys()) { | ||
} | ||
/** | ||
* Decode JSON to JS. | ||
* @param {type} json | ||
* @returns {r|Translator.decode.rvalue} | ||
*/ | ||
decode(json) { | ||
@@ -125,9 +140,12 @@ if (json === null) throw new Error("JSON object is null."); | ||
let rvalue = null; | ||
let eson = new EncodedJSON(json); | ||
new Decoder(eson, this, null).decode(r => { | ||
while (!this.deferred.isEmpty()) | ||
let encodedJSON = new EncodedJSON(json); | ||
new Decoder(encodedJSON, this, null).decode(r => { | ||
while (!this.deferred.isEmpty()){ | ||
this.deferred.remove(0).resume(); | ||
} | ||
this.clearTempReferences(); | ||
rvalue = r; | ||
}); | ||
return rvalue; | ||
@@ -139,5 +157,5 @@ } | ||
encode(object) { | ||
let toJSON = new Encoder(object, this).encode(); | ||
let encoded = new Encoder(object, this).encode(); | ||
this.clearTempReferences(); | ||
return toJSON; | ||
return encoded; | ||
} | ||
@@ -151,4 +169,4 @@ getAllReferredObjects() { | ||
} | ||
hasHandler(aClass) { | ||
return this.handlers.has(aClass.__getClass()); | ||
hasHandler(aClass) { | ||
return aClass.__getClass && this.handlers.has(aClass.__getClass()); | ||
} | ||
@@ -155,0 +173,0 @@ setHandler(aClass, handler) { |
39412
1142