Changeset 16 in chevmsgr


Ignore:
Timestamp:
11/08/15 19:59:07 (9 years ago)
Author:
cheese
Message:

버그 수정 및 add friend 코드 작업

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/msg.cpp

    r14 r16  
    104104        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::ADD_FRIEND);
    105105
    106         obj[ProtocolType::ID] = id;
     106        obj[ProtocolType::FRIEND_ID] = id;
    107107
    108108        return json::Serialize(obj);
  • trunk/msg.hpp

    r14 r16  
    4444    DECLARE_KEY(GET_FRIEND_LIST);
    4545    DECLARE_KEY(FRIEND_LIST);
     46    DECLARE_KEY(FRIEND_ID);
    4647    DECLARE_KEY(ID_LIST);
    4748    DECLARE_KEY(RESULT);
  • trunk/msgclnt.cpp

    r14 r16  
    294294
    295295            sessionMap[concat] = message.get<std::string>(ProtocolType::SESSION_ID);
     296
     297            bool result = messageQ.pop(ProtocolType::OPEN_SESSION).get<bool>(ProtocolType::RESULT);
     298            if (!result)
     299                THROW_EXCEPTION("cannot open session for [" + concat + "]");
    296300        }
    297301
  • trunk/msgsrv.cpp

    r15 r16  
    3737int cb_getSMS(void * userArg, int argc, char ** argv, char ** colName)
    3838{
    39     std::string * SMS = (std::string *) userArg;
    40 
    41     *SMS = argv[1];
     39    std::string * sms = (std::string *) userArg;
     40
     41    for (int i = 0; i < argc; i++)
     42    {
     43        if (stricmp(colName[i], "sms"))
     44        {
     45            *sms = argv[i];
     46            break;
     47        }
     48    }
    4249
    4350    return 0;
    4451}
    4552
    46 int cb_login(void * userArg, int argc, char ** argv, char ** colName)
    47 {
     53int cb_exist(void * userArg, int argc, char ** argv, char ** colName)
     54{
     55    // È£ÃâµÇ¸é Àִ°Å
    4856    bool * result = (bool *)userArg;
    4957
     
    5361}
    5462
    55 int cb_join(void * userArg, int argc, char ** argv, char ** colName)
    56 {
    57     bool * isExist = (bool *)userArg;
    58 
    59     *isExist = true;
    60 
    61     return 0;
    62 }
    6363
    6464//===============================================================
     
    106106            "CREATE TABLE T_ACCOUNT"
    107107            "("
    108             "   num INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
    109             "   id  TEXT,"
    110             "   pw  TEXT"
     108            "   id TEXT PRIMARY KEY,"
     109            "   pw TEXT"
    111110            ")";
    112111        sqlCreateAuthTable =
    113112            "CREATE TABLE T_AUTH"
    114113            "("
    115             "   id      TEXT PRIMARY KEY NOT NULL,"
    116             "   sms1    TEXT,"
    117             "   ip1     TEXT,"
    118             "   sms2    TEXT,"
    119             "   ip2     TEXT,"
    120             "   sms3    TEXT,"
    121             "   ip3     TEXT,"
    122             "   sms4    TEXT,"
    123             "   ip4     TEXT,"
    124             "   sms5    TEXT,"
    125             "   ip5     TEXT"
     114            "   id  TEXT NOT NULL,"
     115            "   ip  TEXT NOT NULL,"
     116            "   sms TEXT NOT NULL"
    126117            ")";
    127118        sqlCreateFriendsTable =
    128119            "CREATE TABLE T_FRIENDS"
    129120            "("
    130             "   id      TEXT PRIMARY KEY NOT NULL,"
    131             "   friend  TEXT"
     121            "   id      TEXT NOT NULL,"
     122            "   friend  TEXT NOT NULL"
    132123            ")";
    133124
     
    157148            std::string query = "select * from T_ACCOUNT where id ='" + id + "' and pw = '" + pw + "'";
    158149
    159             this->exec(query, cb_login, &result);
     150            this->exec(query, cb_exist, &result);
    160151
    161152            return result;
     
    172163        try
    173164        {
    174             bool isExist = false;
    175             std::string existQuery = "select * from T_ACCOUNT where id = '" + id + "'";
    176             std::string insertQuery = "insert into T_ACCOUNT(id, pw) values('" + id + "', '" + pw + "')";
    177 
    178             this->exec(existQuery, cb_join, &isExist);
    179 
    180             if (!isExist)
    181                 this->exec(insertQuery, NULL, NULL);
     165            // account
     166            bool isExistAccount = false;
     167            std::string existAccount = "select * from T_ACCOUNT where id = '" + id + "'";
     168            std::string insertAccount = "insert into T_ACCOUNT(id, pw) values('" + id + "', '" + pw + "')";
     169
     170            // auth
     171            bool isExistAuth = false;
     172            std::string existAuth = "select * from T_AUTH where id = '" + id + "' and sms = '" + sms + "' and ip = '" + ip + "'";
     173            std::string insertAuth = "insert into T_AUTH(id, sms, ip) values( '" + id + "', '" + sms + "' , '" + ip + "')";
     174
     175            this->exec(existAccount, cb_exist, &isExistAccount);
     176
     177            if (!isExistAccount)
     178            {
     179                this->exec(insertAccount, NULL, NULL);
     180                this->exec(insertAuth, NULL, NULL);
     181            }
     182            else
     183            {
     184                this->exec(existAuth, cb_exist, &isExistAuth);
     185                if (!isExistAuth)
     186                    this->exec(insertAuth, NULL, NULL);
     187            }
    182188        }
    183189        catch (cf::exception & e)
     
    189195    }
    190196
    191     std::string getFriendList(std::string & id)
     197    std::vector<std::string> getFriendList(std::string & id)
    192198        throw(cf::exception)
    193199    {
     
    207213    }
    208214
    209     int getSMS(std::string & id)
    210     {
    211         int result = 0;
    212         std::string smsMsg;
    213 
     215    std::string getSMS(std::string & id, std::string & ip)
     216    {
    214217        try
    215218        {
    216             std::string query = "select * from T_AUTH where id = '" + id + "'";
    217 
    218             this->exec(query, cb_getSMS, &smsMsg);
     219            std::string sms;
     220            std::string query = "select * from T_AUTH where id = '" + id + "' and ip = '" + ip + "'";
     221
     222            this->exec(query, cb_getSMS, &sms);
     223            if (sms.length() == 0)
     224                THROW_EXCEPTION("sms was not found for '" + id + "' from '" + ip + "'");
     225
     226            return sms;
    219227        }
    220228        catch (cf::exception & e)
     
    224232    }
    225233
    226     int addFriend(std::string & id)
     234    bool addFriend(std::string & id, std::string & friendID)
    227235    {
    228236        try
    229237        {
    230             std::string query = "insert into T_FRIENDS values('" + id + "')";
     238            std::string query = "insert into T_FRIENDS values('" + id + "', '" + friendID + "')";
    231239
    232240            this->exec(query, NULL, NULL);
     241
     242            return true;
    233243        }
    234244        catch (cf::exception & e)
    235245        {
    236246            FORWARD_EXCEPTION(e);
     247            return false;
    237248        }
    238249    }
     
    345356}
    346357
    347 static bool login(const Protocol::Message & parser, cf::network::tcp & sock, cf::bin & key)
     358static bool login(const Protocol::Message & parser, cf::network::tcp & sock, std::string & ip, std::string sms)
    348359    throw (cf::exception)
    349360{
     
    354365    if (result)
    355366    {
     367        cf::bin seed = sms + DELIMITER + ip;
     368        cf::bin key = crypto().sha256(seed);
     369
    356370        LoginSession loginSess;
     371
    357372        loginSess.sock = &sock;
    358373        loginSess.key = key;
     
    422437}
    423438
     439static bool addFriend(const Protocol::Message & message)
     440{
     441    Protocol::Message parser = message;
     442    std::string result = "";
     443    std::string id = parser.get<std::string>(ProtocolType::ID);
     444    std::string friendID = parser.get<std::string>(ProtocolType::FRIEND_ID);
     445   
     446    return dbmgr.addFriend(id, friendID);
     447}
     448
    424449static std::string keyExchange(const std::string sms, const std::string address)
    425450{
     
    456481            else if (parser.type() == ProtocolType::LOGIN)
    457482            {
     483                id = parser.get<std::string>(ProtocolType::ID);
    458484                std::string ip = sock.peer().address();
    459                 cf::bin seed = sms + DELIMITER + ip;
    460                 cf::bin key = crypto().sha256(seed);
    461 
    462                 if (login(parser, sock, key))
    463                     id = parser.get<std::string>(ProtocolType::ID);
    464 
    465                 loggedIn = true;
     485                sms = dbmgr.getSMS(id, ip);
     486
     487                // ½ÇÆÐÇÏ¸é ¾Õ¿¡¼­ exceptionÀ¸·Î ´Ù 󸮵Ê
     488                // ¿©±ä ¼º°øÇÒ ¶§¿¡¸¸ ¿È
     489                if (login(parser, sock, ip, sms))
     490                    loggedIn = true;
    466491            }
    467492
     
    504529            {
    505530                result = chat(parser);
    506                 sock->send(response.result(parser.type(), result));
    507531            }
    508532            else if (parser.type() == ProtocolType::OPEN_SESSION)
     
    510534                result = openSession(parser);
    511535            }
     536            else if (parser.type() == ProtocolType::ADD_FRIEND)
     537            {
     538                result = addFriend(parser);
     539            }
     540
     541            sock->send(response.result(parser.type(), result));
    512542        }
    513543    }
  • trunk/msvc14/testClient/test.cpp

    r14 r16  
    99#define CIN(v) std::cout << #v << "\t: "; std::cin >> v
    1010
     11std::string mode;
    1112std::string ip;
    1213std::string port;
     
    5657    std::ifstream f(ini);
    5758
     59    std::getline(f, mode);
    5860    std::getline(f, ip);
    5961    std::getline(f, port);
     
    7476    T(c.connect(ip, atoi(port.c_str())));
    7577    T(c.listen(cb));
    76     T(c.sms(phone));
    7778
    78     CIN(sms);
    79     T(c.join(id, pw, sms));
    80     std::getline(std::cin, sms);
     79    sms = "633174";// CIN(sms);
     80    if (mode == "join")
     81    {
     82        T(c.sms(phone));
     83        T(c.join(id, pw, sms));
     84    }
     85
     86    //std::getline(std::cin, sms);
    8187
    8288    T(c.login(id, pw));
Note: See TracChangeset for help on using the changeset viewer.