Changeset 12 in chevmsgr


Ignore:
Timestamp:
08/30/15 15:08:31 (9 years ago)
Author:
cheese
Message:

add dialogs

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/msg.hpp

    r11 r12  
    5656        Message();
    5757
    58         inline void parse(const std::string & message);
     58        void parse(const std::string & message);
    5959
    6060        template<typename T>
    61         inline T get(const std::string & key) const
     61        T get(const std::string & key) const
    6262        {
    6363            return (T)mObject[key];
     
    6565
    6666        template<typename T>
    67         inline std::vector<T> getList(const std::string & key) const
     67        std::vector<T> getList(const std::string & key) const
    6868        {
    6969            std::vector<T> ret;
     
    7676        }
    7777
    78         inline std::vector<SFriend> getFriendList() const;
     78        std::vector<SFriend> getFriendList() const;
    7979
    80         inline std::string type() const;
     80        std::string type() const;
    8181    };
    8282
     
    9393
    9494    public:
    95         inline void setUserID(const std::string & id);
     95        void setUserID(const std::string & id);
    9696
    97         inline const std::string & getUserID();
     97        const std::string & getUserID();
    9898
    99         inline void makeTemplate(json::Object & obj, const std::string & type) const;
     99        void makeTemplate(json::Object & obj, const std::string & type) const;
    100100
    101         inline std::string sms(const std::string & phone) const;
     101        std::string sms(const std::string & phone) const;
    102102
    103         inline std::string join(const std::string & id, const std::string & pw, const std::string & sms);
     103        std::string join(const std::string & id, const std::string & pw, const std::string & sms);
    104104
    105         inline std::string login(const std::string & pw) const;
     105        std::string login(const std::string & pw) const;
    106106
    107         inline std::string addFriend(const std::string & id) const;
     107        std::string addFriend(const std::string & id) const;
    108108
    109         inline std::string openSession(const std::string & to) const;
     109        std::string openSession(const std::string & to) const;
    110110
    111         inline std::string chat(const std::string & sessid, const std::string & message, const int sensitive) const;
     111        std::string chat(const std::string & sessid, const std::string & message, const int sensitive) const;
    112112
    113         inline std::string getFriendList() const;
     113        std::string getFriendList() const;
    114114    };
    115115
     
    117117    {
    118118    public:
    119         inline void makeTemplate(json::Object & obj, const std::string & type) const;
     119        void makeTemplate(json::Object & obj, const std::string & type) const;
    120120
    121         inline std::string result(const std::string & requestType, const bool status) const;
     121        std::string result(const std::string & requestType, const bool status) const;
    122122
    123         inline std::string friendList(const std::vector<SFriend> & friendList) const;
     123        std::string friendList(const std::vector<SFriend> & friendList) const;
    124124
    125         inline std::string openSession(const std::vector<std::string> & idList) const;
     125        std::string openSession(const std::vector<std::string> & idList) const;
    126126    };
    127127};
     
    156156
    157157    for (size_t iter = 1; iter < strings.size(); iter++)
    158         concat += DELIMITER + strings[iter];
     158        concat += strings[iter];
    159159
    160160    return concat;
  • trunk/msgclnt.cpp

    r10 r12  
    99// --------------------------------------------------------------
    1010
    11 typedef struct SWorkerArg
     11typedef struct SMessageQWorkerArg
    1212{
    1313    cf::network::tcp * socket;
    1414    MessageQ * messageQ;
    15 } SWorkerArg;
    16 
    17 int worker(void * arg)
    18 {
    19     SWorkerArg * inst = (SWorkerArg *) arg;
    20 
    21     Protocol::Message parser;
     15} SMessageQWorkerArg;
     16
     17typedef struct SCallbackWorkerArg
     18{
     19    MessageQ * messageQ;
     20    SCallback callback;
     21} SCallbackWorkerArg;
     22
     23static int messageQworekr(void * arg)
     24{
     25    SMessageQWorkerArg * inst = (SMessageQWorkerArg *)arg;
     26
     27    Protocol::Message message;
    2228    cf::bin raw;
    2329
     
    2733        {
    2834            raw = inst->socket->receive();
    29             parser.parse(raw.toString());
     35            message.parse(raw.toString());
    3036        }
    3137        catch (cf::exception & e)
     
    3844        }
    3945
    40         inst->messageQ->push(parser);
     46        inst->messageQ->push(message);
    4147    }
    4248
     
    4652}
    4753
     54static inline SConversation toCnversation(const Protocol::Message & message)
     55{
     56    SConversation c;
     57
     58    c.sessid = message.get<std::string>(ProtocolType::SESSION_ID);
     59    c.message = message.get<std::string>(ProtocolType::MESSAGE);
     60    c.sensitive = message.get<int>(ProtocolType::SENSITIVE);
     61    c.isError = message.get<bool>(ProtocolType::RESULT);
     62
     63    c.from = message.get<std::string>(ProtocolType::FROM);
     64
     65    return c;
     66}
     67
     68static inline SOpenSession toOpenSession(const Protocol::Message & message)
     69{
     70    SOpenSession o;
     71
     72    o.sessid = message.get<std::string>(ProtocolType::SESSION_ID);
     73    o.idList = message.getList<std::string>(ProtocolType::ID_LIST);
     74
     75    return o;
     76}
     77
     78static int callbackWorker(void * arg)
     79{
     80    SCallbackWorkerArg * inst = (SCallbackWorkerArg *)arg;
     81
     82    Protocol::Message message;
     83
     84    while (true)
     85    {
     86        message = inst->messageQ->pop(ProtocolType::CHAT, false);
     87        if (message.type() != ProtocolType::NONE)
     88            inst->callback.onChat(toCnversation(message));
     89
     90        message = inst->messageQ->pop(ProtocolType::OPEN_SESSION, false);
     91        if (message.type() != ProtocolType::NONE)
     92            inst->callback.onOpenSession(toOpenSession(message));
     93    }
     94
     95    free(inst);
     96
     97    return 0;
     98}
     99
    48100// --------------------------------------------------------------
    49101
    50 void MessageQ::push(const Protocol::Message & parser)
     102void MessageQ::push(const Protocol::Message & message)
    51103{
    52104    mutex.lock();
    53     messageQ.push_back(parser);
     105    messageQ.push_back(message);
    54106    mutex.unlock();
    55107}
    56108
    57 Protocol::Message MessageQ::pop(const std::string & requestType)
    58 {
    59     while (true)
     109Protocol::Message MessageQ::pop(const std::string & requestType, bool isWait)
     110{
     111    while (isWait)
    60112    {
    61113        SYNCHRONIZED(mutex)
    62114        {
    63             Protocol::Message parser = messageQ.front();
    64 
    65             if (parser.type() == requestType)
     115            Protocol::Message message = messageQ.front();
     116
     117            if (message.type() == requestType)
    66118            {
    67119                messageQ.erase(messageQ.begin());
    68120
    69                 return parser;
     121                return message;
    70122            }
    71123        }
    72124    }
     125
     126    return Protocol::Message(); // return dummy NONE
    73127}
    74128
     
    76130
    77131chev::chev()
    78     : listener(worker)
     132    : listener(messageQworekr),
     133      caller(callbackWorker)
    79134{
    80135}
     
    96151        socket.connect(host, port);
    97152
    98         SWorkerArg * arg = (SWorkerArg *)malloc(sizeof(SWorkerArg));
     153        SMessageQWorkerArg * arg = (SMessageQWorkerArg *)malloc(sizeof(SMessageQWorkerArg));
    99154        if (!arg)
    100155            return false;
     
    203258    try
    204259    {
     260        idList.insert(idList.begin(), request.getUserID());
     261
    205262        if (idList.size() == 2)
    206263            std::sort(idList.begin(), idList.end());
    207264
    208         std::string concat = idList[0];
    209 
    210         for (int iter = 1; iter < idList.size(); iter++)
    211             concat += DELIMITER + idList[iter];
     265        std::string concat = joinStrings(idList);
    212266
    213267        if (sessionMap.find(concat) == sessionMap.end())
     
    251305}
    252306
    253 SConversation chev::listen()
    254 {
    255     SConversation c;
    256     Protocol::Message message = messageQ.pop(ProtocolType::CHAT);
    257 
    258     c.sessid    = message.get<std::string>(ProtocolType::SESSION_ID);
    259     c.message   = message.get<std::string>(ProtocolType::MESSAGE);
    260     c.sensitive = message.get<int>(ProtocolType::SENSITIVE);
    261     c.isError   = message.get<bool>(ProtocolType::RESULT);
    262 
    263     c.from      = message.get<std::string>(ProtocolType::FROM);
    264 
    265     return c;
    266 }
     307bool chev::listen(const SCallback & callback)
     308{
     309    SCallbackWorkerArg * arg = (SCallbackWorkerArg *)malloc(sizeof(SCallbackWorkerArg));
     310    if (!arg)
     311        return false;
     312
     313    arg->messageQ = &messageQ;
     314    arg->callback = callback;
     315
     316    caller.start(arg);
     317
     318    return true;
     319}
  • trunk/msgclnt.h

    r8 r12  
    2121} SConversation;
    2222
     23typedef struct SOpenSession
     24{
     25    std::string sessid;
     26    std::vector<std::string> idList;
     27} SOpenSession;
     28
     29typedef struct SCallback
     30{
     31    int(*onChat)(SConversation &);
     32    int(*onOpenSession)(SOpenSession &);
     33} SCallback;
     34
    2335class MessageQ
    2436{
     
    2840
    2941public:
    30     void push(const Protocol::Message & parser);
     42    void push(const Protocol::Message & message);
    3143
    32     Protocol::Message pop(const std::string & requestType);
     44    Protocol::Message pop(const std::string & requestType, bool isWait = true);
    3345};
    3446
     
    4052    cf::network::tcp    socket;
    4153    cf::task::thread    listener;
     54    cf::task::thread    caller;
    4255
    4356    MessageQ            messageQ;
     
    6881    bool tell(const SConversation & conversation);
    6982
    70     SConversation listen();
     83    bool listen(const SCallback & callback);
    7184};
Note: See TracChangeset for help on using the changeset viewer.