Skip to content

Instantly share code, notes, and snippets.

@oxsav
Created July 10, 2013 10:13

Revisions

  1. @amaralv amaralv created this gist Jul 10, 2013.
    132 changes: 132 additions & 0 deletions Requests to Server
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    //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" : "[email protected]"
    }