Skip to content

Commit

Permalink
Client's method error handling. More test units.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktrzeciaknubisa committed May 21, 2014
1 parent 14d9ce1 commit 9c00436
Show file tree
Hide file tree
Showing 29 changed files with 969 additions and 173 deletions.
2 changes: 1 addition & 1 deletion JavaClient/Nubisa/jxm/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public Object call() throws Exception {
return null;
}

public void call(Object o) throws Exception {
public void call(Object o, Integer err) throws Exception {

}
}
136 changes: 81 additions & 55 deletions JavaClient/Nubisa/jxm/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.lang.reflect.Method;
import java.util.*;
import java.util.List;

/**
* jxm.io Java Client
Expand Down Expand Up @@ -254,29 +253,35 @@ public void goClose() {
* [/code]
* </pre>
*/
public void Subscribe(final String group, final Callback cb) throws Exception
{
if(group != null){
Map<String, Object> map = new HashMap<String, Object>();
map.put("gr", group);
map.put("en", enc);

this.Call("nb.ssTo", map, new Callback() {
@Override
public void call(Object o) throws Exception {
JSON js = (JSON)o;
onSub(js.getValue("key").toString());
lastMessId = js.getValue("did").toString();
if (cb != null) {
cb.call(group);
}
}
});
}
else{
throw new Exception( "Group name must be a non empty string" );
}
}
public void Subscribe(final String group, final Callback cb) throws Exception {
if (group != null) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("gr", group);
map.put("en", enc);

this.Call("nb.ssTo", map, new Callback() {
@Override
public void call(Object o, Integer err) throws Exception {
JSON js = (JSON) o;
if (err == 0) {
onSub(js.getValue("key").toString());
lastMessId = js.getValue("did").toString();
}

if (cb != null) {
cb.call(group, err);
}
}
});
} else {
Integer errCode = 6; /* must be non-empty string */
if (cb != null) {
cb.call(group, errCode);
} else {
throw new Exception("Error no " + errCode);
}
}
}

private void onSub(String en){
enc = en;
Expand Down Expand Up @@ -305,31 +310,38 @@ private void onSub(String en){
* [/code]
* </pre>
*/
public void Unsubscribe(final String group, final Callback cb) throws Exception
{
if (enc == null) {
return;
}

if(group != null){
Map<String, Object> map = new HashMap<String, Object>();
map.put("gr", group);
map.put("en", enc);

this.Call("nb.unTo", map, new Callback() {
@Override
public void call(Object o) throws Exception {
onSub(o.toString());
if (cb != null) {
cb.call(group);
}
}
});
}
else{
throw new Exception( "Group name must be a non empty string" );
}
}
public void Unsubscribe(final String group, final Callback cb) throws Exception {
if (enc == null) {
return;
}

if (group != null) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("gr", group);
map.put("en", enc);

this.Call("nb.unTo", map, new Callback() {
@Override
public void call(Object o, Integer err) throws Exception {
JSON js = (JSON) o;
if (err == 0) {
onSub(js.getValue("key").toString());
}

if (cb != null) {
cb.call(group, err);
}
}
});
} else {
Integer errCode = 6; /* must be non-empty string */
if (cb != null) {
cb.call(group, errCode);
} else {
throw new Exception("Error no " + errCode);
}
}
}

/**
* Sends message to all clients, that have already subscribed to the specific group.
Expand All @@ -347,14 +359,14 @@ public void call(Object o) throws Exception {
* [/code]
* </pre>
*/
public void SendToGroup(String groupName, String methodName, Object params){
public void SendToGroup(String groupName, String methodName, Object params, Callback cb){
Map<String, Object> map = new HashMap<String, Object>();
map.put("gr", groupName);
map.put("m", methodName);
map.put("j", params);
map.put("key", enc);

this.Call("nb.stGr", map, null);
this.Call("nb.stGr", map, cb);
}

/**
Expand Down Expand Up @@ -536,7 +548,7 @@ public Object getValue(String key){
}
}

private String lastMessId = null; //TODO use IT!!!!
private String lastMessId = null;

private void Eval(String msg)
{
Expand Down Expand Up @@ -594,8 +606,22 @@ else if (strIndex!=null){
if (n < 0) {
ssCall(n, param);
} else {
if(callbacks.size()>n-1){
callbacks.get(n-1).call(param);
Integer err = 0;

if (JSON.class.isAssignableFrom(param.getClass())) {
JSON p = (JSON)param;

Object nb_err = p.containsKey("nb_err") ? p.getValue("nb_err") : null;

if (nb_err != null) {
float fl1 = Float.valueOf(nb_err.toString()).floatValue();
err = (int)fl1;
}
}

if(callbacks.size()>n-1) {
callbacks.get(n-1).call(param, err);
callbacks.set(n-1, null);
}
}
}catch(Exception e){
Expand Down
104 changes: 77 additions & 27 deletions backend/jx_browser_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
var js = [];
var isArray = false, isObject = false;
var type = Object.prototype.toString.call(obj);
if (obj === null || type === '[object Null]') {
if (obj === null || type === '[object Null]' || type === "[object Undefined]") {
return "null";
} else
if (type === '[object Array]') {
Expand Down Expand Up @@ -107,15 +107,27 @@
};

jxcore.Callbacks = [];
jxcore.Call = function(methodName, json, cb){

jxcore.Call = function(methodName, json, cb, clientCB){

if(!methodName || !methodName.length) {
var errCode = 7; /* null or empty method name */
if (cb) {
cb(null, errCode);
return;
} else {
throw "Error no " + errCode;
}
}

var str = '{"m":"'+methodName+'"';

if(json){
str += ',"p":' + jxcore.toJSON(json);
}

if(cb){
jxcore.Callbacks.push(cb);
jxcore.Callbacks.push( {cb : cb, methodName: methodName, json : json, clientCB : clientCB } );
str += ',"i":' + (jxcore.Callbacks.length).toString();
}

Expand All @@ -125,46 +137,77 @@
};

jxcore.enc = null;
var subreq = false; /* subscription request */
jxcore.Subscribe = function(group, cb){
if(group && group.length){
subreq = "subscription";
jxcore.Call("nb.ssTo", {gr:group, en:jxcore.enc}, function(r){
jxcore.enc = r.key;
if (r.did !== null) {
jxcore.dataId = r.did;
jxcore.Call("nb.ssTo", {gr:group, en:jxcore.enc}, function(r, err, cbData, clientCB){

if (!err) {
jxcore.enc = r.key;
if (r.did !== null) {
jxcore.dataId = r.did;
}
}
subreq = false;
if(cb){cb(group);}
});
if(clientCB){
var gr = cbData && cbData.gr ? cbData.gr : null;
clientCB(gr, err);
}
}, cb);
}
else{
throw "Group name must be a non empty string";
else {
var errCode = 6; /* must be non-empty string*/
if (cb) {
cb(group, errCode);
} else {
throw "Error no " + errCode;
}
}
};

jxcore.Unsubscribe = function(group, cb){
if (!jxcore.enc) {
if (!jx_obj.enc) {
var errCode = 2; /* not belonging to any group */
if (cb) {
cb(group, errCode);
} else {
throw "Error no " + errCode;
}
return;
}
if(group && group.length){
subreq = "unsubscription";
jxcore.Call("nb.unTo", {gr:group, en:jxcore.enc}, function(r){
subreq = false;
jxcore.enc = r;
if(cb){cb(group);}
});
jxcore.Call("nb.unTo", {gr:group, en:jxcore.enc}, function(r, err, cbData, clientCB){
if (!err) {
jxcore.enc = r.key;
}
if(clientCB){
var gr = cbData && cbData.gr ? cbData.gr : null;
clientCB(gr, err);
}
}, cb);
}
else{
throw "Group name must be a non empty string";
else {
var errCode = 6; /* must be non-empty string*/
if (cb) {
cb(group, errCode);
} else {
throw "Error no " + errCode;
}
}
};

jxcore.SendToGroup = function(groupName, methodName, json){
jxcore.SendToGroup = function(groupName, methodName, json, cb){
if(!groupName){
groupName="ALL";
}
jxcore.Call("nb.stGr", {key:jxcore.enc, gr:groupName, m:methodName, j:json});

if (cb) {
jxcore.Call("nb.stGr", {key:jxcore.enc, gr:groupName, m:methodName, j:json}, function(r, err, cbData, clientCB){
if(clientCB){
clientCB(err);
}
}, cb);
} else {
jxcore.Call("nb.stGr", {key:jxcore.enc, gr:groupName, m:methodName, j:json});
}
};

jxcore.Custom = {};
Expand Down Expand Up @@ -204,14 +247,21 @@
jxcore.sscall(p.i, p.p);
}
else if(p.i){
var arg1 = p.p.nb_err ? null : p.p;
var arg2 = p.p.nb_err ? p.p.nb_err : false;

var nb_method = jxcore.Callbacks[p.i-1].methodName.indexOf("nb.") === 0;
var arg3 = nb_method ? jxcore.Callbacks[p.i-1].json : undefined;
var arg4 = nb_method ? jxcore.Callbacks[p.i-1].clientCB : undefined;

jxcore.ActualCallbackId = p.i;
if(jxcore.backup && p.i>jxcore.Callbacks.length){
jxcore.backup[p.i-1](p.p);
jxcore.backup[p.i-1].cb(arg1, arg2, arg3, arg4);
jxcore.backup[p.i-1] = null;
}else{
/* on IE6 sometimes happened, that there was no callback ? */
if (jxcore.Callbacks[p.i-1]) {
jxcore.Callbacks[p.i-1](p.p);
jxcore.Callbacks[p.i-1].cb(arg1, arg2, arg3, arg4);
jxcore.Callbacks[p.i-1] = null;
}
}
Expand Down
Loading

0 comments on commit 9c00436

Please sign in to comment.