Skip to content

Instantly share code, notes, and snippets.

@oxsav
Created July 10, 2013 10:13
Show Gist options
  • Save oxsav/5965179 to your computer and use it in GitHub Desktop.
Save oxsav/5965179 to your computer and use it in GitHub Desktop.
//well I'm doing a presence service with vert.x so I need to verify who are on my friend's list
//then I will have to list all of them in the client in a list
//to do that I will need to contact my mongodb and need to know who is on my friends list and his state presence
//ONCLIENT
//this is my request to the module on the server from the client.
eb.send(address, {
"action": "update",
"sessionId": mySession,
"username": username,
"newstate": value,
"address": addressUser,
"login" : login
},
function(reply){
//doesn't matter
});
//ON THE SERVER
final String myUser = username;
final String presence = statePresence;
final Message<JsonObject> messagem = message;
friendsOnline = new JsonArray();
JsonObject notPres = new JsonObject();
notPres.putString("action", "find");
notPres.putString("collection", "groupprofile");
JsonObject matcher = new JsonObject();
matcher.putString("admin", myUser);
notPres.putObject("matcher", matcher);
friendsOn = new JsonArray();
//Will get my friends list
eb.send("test.my_persistor",
notPres,
new Handler<Message<JsonObject>>() {
public void handle(Message<JsonObject> message) {
JsonObject result = new JsonObject(message.body.getArray("results").get(0).toString());
int i;
if(result.getArray("authorised_presence").size()>0){
System.out.println("tamanho 1 : " + result.getArray("authorised_presence").size());
for(i=1; i <= result.getArray("authorised_presence").size(); i++){
JsonObject user = new JsonObject(result.getArray("authorised_presence").get(i-1).toString());
//see the state presence of each person on my list of friends
seeOnlineFriends(user.toString(), myUser, presence, messagem, result.getArray("authorised_presence").size()+1, result.getArray("authorised_presence"), i);
}
}
}
});
//SEEONLINEFRIENDS FUNCTION
//this function will see for each friend if have a session created (sessionmanager module) and what is his state presence
private void seeOnlineFriends(String user, String username, String statePresence, Message<JsonObject> message, final int tamanho, final JsonArray listaAmigos, final int i){
final JsonObject friend = new JsonObject(user);
final Message<JsonObject> messagem = message;
JsonObject doAction = new JsonObject();
doAction.putString("action", "status");
doAction.putString("report", "matches");
JsonObject matches = new JsonObject();
matches.putString("username", friend.getString("name"));
doAction.putObject("data", matches);
eb.send("test.session_manager",
doAction,
new Handler<Message<JsonObject>>() {
public void handle(Message<JsonObject> message) {
System.out.println("Entrei");
System.out.println(tamanho);
System.out.println(i);
if(message.body.getBoolean("matches")){
JsonObject state = new JsonObject(message.body.getArray("sessions").get(0).toString());
JsonObject onFriend = new JsonObject("{ \"username\":\""+friend.getString("name")+"\" , \"state\": \""+state.getObject("data").getString("state")+"\" }");
friendsOnline.addObject(onFriend);
for(int j = 0 ; j < friendsOnline.size(); j++)
System.out.println("Object" + j + " :" + friendsOnline.get(j).toString());
}
if(!message.body.getBoolean("matches")){
JsonObject offFriend = new JsonObject("{ \"username\":\""+friend.getString("name")+"\" , \"state\": \"Offline\" }");
friendsOnline.addObject(offFriend);
for(int j = 0 ; j < friendsOnline.size(); j++)
System.out.println("Object" + j + " :" + friendsOnline.get(j).toString());
}
if(i == tamanho-1){
System.out.println("i " + i);
System.out.println("tamanho "+tamanho);
System.out.println(" Friends Online: " + friendsOnline.size());
JsonObject resposta = new JsonObject();
resposta.putString("status","ok");
resposta.putArray("friends",friendsOnline);
messagem.reply(resposta);
sendOK(messagem,resposta);
}
}
});
//THIS REPRESENTS THE SCHEMA USED IN MONGODB
//groupprofile collection
{
"_id" : "b9ddad4f-3f85-46da-9360-0d58a78c3fa4",
"admin" : "vasco",
//my list of friends
"authorised_presence" : [ { "name" : "Oxsav" } ], "group_elements" : [ { } ],
//#####
"links" : [ { } ],
"name" : "private.vasco",
"photo" : "", "sessionId" :
"", "type" : "private",
"url_pub" : "private.vasco@webrtcptinovacao.pt"
}
@oxsav
Copy link
Author

oxsav commented Jul 10, 2013

test => friends list [test3, test4, test5]
test2 => friends list [test6, test7, test8]

if both do the request to the server at the same time the answer to the test will be:
[{name: test3, statePresence: online}, {name: test4, statePresence: online}, {name: test5, statePresence: online}, {name: test6, statePresence: online},{name: test7, statePresence: online}, {name: test8, statePresence: online}]

and to test2:
[{name: test6, statePresence: online},{name: test7, statePresence: online}, {name: test8, statePresence: online}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment