anchor.diff:                                         ]}
                                  ]).
   
  -
  --spec is_empty(sets:set()) -> true | false.
  -is_empty(Set) ->
  -    sets:is_subset(Set,sets:new()).
  -
   persist_message(Message,Context,Session,ThreadId,Body,Data) ->
       {_, ObjectId} = proplists:lookup(objectid, Data),
       {Id, NextId} = ObjectId,
  
positive.code:     mqtt_server:handle_message(Message,Context#?CONTEXT{data=NewData}).
  
  online_status(Message,Context,Status,UserId,ClientId) ->
      io:format("online/~p",[UserId]),
      case Status of
          <<"offline">> ->
             case logoff(UserId,ClientId) of
                 {atomic, Clients} ->
                     case is_empty(Clients) of
                         true ->
                              mqtt_server:handle_message(Message,Context);
                         false ->
                              {noreply, Context#?CONTEXT{timestamp=os:timestamp()}, Context#?CONTEXT.timeout}
                     end;
                 _ ->
                      {noreply, Context#?CONTEXT{timestamp=os:timestamp()}, Context#?CONTEXT.timeout}
             end;
          _ ->
              case update_status(Status,UserId,ClientId) of
                  {atomic, {Status,_Clients}} ->
                      {noreply, Context#?CONTEXT{timestamp=os:timestamp()}, Context#?CONTEXT.timeout}
                      ;
                  {atomic, {_PrevStatus,_Clients}} ->
                      mqtt_server:handle_message(Message,Context);
                  _ ->
                      {noreply, Context#?CONTEXT{timestamp=os:timestamp()}, Context#?CONTEXT.timeout}
  
negative.code:     end.
  
  update_status(Status,UserId,ClientId) ->
      Transaction =
      fun() ->
              case mnesia:wread({mqtt_presence,UserId}) of
                  [P=#mqtt_presence{clients=Clients,last_status=PrevStatus}] ->
                      NewClients = sets:add_element(ClientId,Clients),
                      mnesia:write(P#mqtt_presence{clients=NewClients,last_status=Status}),
                      {PrevStatus,NewClients}
                      ;
                  _DoesNotExist ->
                      Clients = sets:new(),
                      NewClients = sets:add_element(ClientId, Clients),
                      mnesia:write(#mqtt_presence{user=UserId,clients=NewClients,last_status=Status}),
                      {none,NewClients}
              end
      end,
      mnesia:transaction(Transaction).
  
  logoff(UserId,ClientId) ->
      Transaction = fun() ->
                            case mnesia:wread({mqtt_presence,UserId}) of
                                [P=#mqtt_presence{clients=Clients}] ->
                                    NewClients = sets:del_element(ClientId,Clients),
                                    mnesia:write(P#mqtt_presence{clients=NewClients}),
                                    sets:size(NewClients)
                                    ;
                                _DoesNotExist ->
                                    mnesia:abort("No such presence object")
                            end
                    end,
      mnesia:transaction(Transaction).
  
  
  