Changeset 6 in chevmsgr for trunk/msg.cpp


Ignore:
Timestamp:
08/29/15 02:20:50 (9 years ago)
Author:
cheese
Message:

modify client interface and message protocol

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/msg.cpp

    r5 r6  
    55    makeTemplate(_obj, _type);                  \
    66
    7 namespace Message
     7#define DELIMITER   "$"
     8
     9namespace Protocol
    810{
    911    // parser
    10     void Parser::parse(const std::string & message)
     12    void Message::parse(const std::string & message)
    1113    {
    1214        mObject = json::Deserialize(message);
    1315    }
    1416
    15     std::string Parser::type() const
     17    std::string Message::type() const
    1618    {
    1719        return get<std::string>("type");
     
    3133    void Request::makeTemplate(json::Object & obj, const std::string & type) const
    3234    {
    33         obj["id"] = mID;
    34         obj["type"] = type;
    35     }
    36 
    37     std::string Request::command(const std::string & cmd) const
    38     {
    39         DECLARE_TEMPLATE_OBJECT(obj, "command");
    40 
    41         obj["command"] = cmd;
    42 
    43         return json::Serialize(obj);
     35        obj[ProtocolType::ID  ] = mID;
     36        obj[ProtocolType::TYPE] = type;
    4437    }
    4538
    4639    std::string Request::sms(const std::string & phone) const
    4740    {
    48         DECLARE_TEMPLATE_OBJECT(obj, "sms");
     41        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SMS);
    4942
    50         obj["phone"] = phone;
     43        obj[ProtocolType::PHONE] = phone;
    5144
    5245        return json::Serialize(obj);
     
    5548    std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms)
    5649    {
    57         DECLARE_TEMPLATE_OBJECT(obj, "join");
     50        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::JOIN);
    5851
    59         obj["id" ] = id;
    60         obj["pw" ] = pw;
    61         obj["sms"] = sms;
     52        obj[ProtocolType::ID ] = id;
     53        obj[ProtocolType::PW ] = pw;
     54        obj[ProtocolType::SMS] = sms;
    6255
    6356        return json::Serialize(obj);
     
    6659    std::string Request::login(const std::string & pw) const
    6760    {
    68         DECLARE_TEMPLATE_OBJECT(obj, "login");
     61        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::LOGIN);
    6962
    70         obj["pw"] = mID + "$" + pw;
     63        obj[ProtocolType::PW] = mID + DELIMITER + pw;
    7164
    7265        return json::Serialize(obj);
    7366    }
    7467
    75     std::string Request::chat(const std::string & to, const std::string & sessid, const std::string & message) const
     68    std::string Request::addFriend(const std::string & id) const
    7669    {
    77         DECLARE_TEMPLATE_OBJECT(obj, "chat");
     70        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::ADD_FRIEND);
    7871
    79         obj["from"   ] = mID;
    80         obj["to"     ] = to;
    81         obj["sess-id"] = sessid;
    82         obj["message"] = message;
     72        obj[ProtocolType::ID] = id;
    8373
    8474        return json::Serialize(obj);
    8575    }
    8676
    87     std::string Request::friendList() const
     77    std::string Request::openSession(const std::string & to) const
    8878    {
    89         DECLARE_TEMPLATE_OBJECT(obj, "friend-list");
     79        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::OPEN_SESSION);
     80
     81        obj[ProtocolType::TO] = to;
     82
     83        return json::Serialize(obj);
     84    }
     85
     86    std::string Request::chat(const std::string & sessid, const std::string & message, const int sensitive) const
     87    {
     88        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::CHAT);
     89
     90        obj[ProtocolType::FROM      ] = mID;
     91        obj[ProtocolType::SESSION_ID] = sessid;
     92        obj[ProtocolType::MESSAGE   ] = message;
     93        obj[ProtocolType::SENSITIVE ] = sensitive;
     94
     95        return json::Serialize(obj);
     96    }
     97
     98    std::string Request::getFriendList() const
     99    {
     100        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::GET_FRIEND_LIST);
    90101
    91102        return json::Serialize(obj);
     
    95106    void Response::makeTemplate(json::Object & obj, const std::string & type) const
    96107    {
    97         obj["type"] = type;
     108        obj[ProtocolType::TYPE] = type;
    98109    }
    99110
    100     std::string Response::result(const bool status) const
     111    std::string Response::result(const std::string & requestType, const bool status) const
    101112    {
    102         DECLARE_TEMPLATE_OBJECT(obj, "result");
     113        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::RESULT);
    103114
    104         obj["result"] = status;
     115        obj[ProtocolType::RESULT] = status;
    105116
    106117        return json::Serialize(obj);
Note: See TracChangeset for help on using the changeset viewer.