summaryrefslogtreecommitdiffstats
path: root/src/server/ChatServer.cpp
blob: 7b6a9eab2f654999f056b4e4d29e50fda5ac8f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "ChatServer.hpp"
#include "UserInfo.hpp"
#include "ChatRoom.hpp"

const int qlen_for_listen = 6;

ChatServer::ChatServer(EventSelector *sel, DatabaseManager *db, int fd)
    : FdHandler(fd), the_selector(sel), dbase(db), room(0)
{
    the_selector->Add(this);
    lobby = new ChatRoom(this, std_id_lobby, 0);
    talkers = new StorageOfUsers();
}

ChatServer::~ChatServer()
{
    // right delete this stuff?
    if(room) {
        for(int i = 0; i < room_len; i++)
            DeleteRoom(i);

        delete[] room;
    }
    the_selector->Remove(this);

    while(talkers->Disconnect())
        ;
    delete talkers;
}

ChatServer *ChatServer::Start(EventSelector *sel, DatabaseManager *db, int port)
{
    int in_d = socket(AF_INET, SOCK_STREAM, 0);
    if(in_d == -1)
        return 0;

    int opt = 1;
    setsockopt(in_d, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);

    int res = bind(in_d, (struct sockaddr*) &addr, sizeof(addr));
    if(res == -1)
        return 0;

    res = listen(in_d, qlen_for_listen);
    if(res == -1)
        return 0;
    
    return new ChatServer(sel, db, in_d);
}

int ChatServer::AddRoom(char *password)
{
    if(!room) {
        room_len = 16;
        room = new ChatRoom*[room_len];
        for(int i = 0; i < room_len; i++) 
            room[i] = 0;
    }

    int id = -1;
    for(int i = 0; i < room_len; i++) {
        if(room[i] == 0) {
            id = i;
            room[i] = new ChatRoom(this, id, password);
            break;
        }
    }

    if(id == -1) {
        ChatRoom **tmp = new ChatRoom*[room_len+1];
        for(int i = 0; i < room_len; i++)
            tmp[i] = room[i];

        id = room_len;
        tmp[room_len] = new ChatRoom(this, id, password);
        room_len += 1;

        delete[] room;
        room = tmp;
    }

    return id;
}

bool ChatServer::DeleteRoom(int id)
{
    if(!room[id])
        return false;

    delete room[id];
    room[id] = 0;

    return true;
}

bool ChatServer::RoomExist(int id) const
{
    if(!room[id])
        return false;
 
    return true;
}

void ChatServer::GotoLobby(ChatRoom *cur_room, UserInfo *u)
{
    cur_room->RemoveSession(u);
    lobby->AddSession(u);
    u->SetRoom(lobby);
}

handle_room_enter ChatServer::ChangeSessionRoom(ChatRoom *cur_room,
    UserInfo *u, int id, char *pass)
{
    if(id < 0 || id >= room_len || !room[id])
        return enter_noexist;

    const char *secret_word = room[id]->GetSecretPass();
    CONSOLE_LOG("right pass: %s; user enter pass: %s\n", secret_word, pass);
    if(secret_word != 0) {
        if(!pass)
            return enter_private;
        if(strcmp(secret_word, pass) != 0)
            return enter_uncorrect_pass;
    }

    cur_room->RemoveSession(u);
    room[id]->AddSession(u);
    u->SetRoom(room[id]);
    return enter_success;
}

void ChatServer::CloseConnection(UserInfo *u) 
{
    talkers->RemoveUser(u);
    the_selector->Remove(u);
    delete u;
}

void ChatServer::Handle(bool r, bool w)
{
    if(!r)
        return;

    struct sockaddr_in addr;
    socklen_t len = sizeof(addr);
    int sd = accept(GetFd(), (struct sockaddr*) &addr, &len);
    if(sd == -1)
        return;

    UserInfo *u = new UserInfo(lobby, sd);
    talkers->AddUser(u);
    lobby->AddSession(u);
    the_selector->Add(u);

    u->Send("Welcome to WantChat! What is your name?");
}

///////////////////////////////////////////////////////////////

StorageOfUsers::~StorageOfUsers()
{
    while(first) {
        item *tmp = first;
        first = first->next;
        delete tmp;
    }
}

void StorageOfUsers::SendAllUsers(const char *msg, UserInfo *except,
    const int spec_msg)
{
    CONSOLE_LOG("Send message all: %s\n", msg);
    item *p;
    for(p = first; p; p = p->next)
        if(p->u != except)
            p->u->Send(msg, spec_msg);
}

UserInfo *StorageOfUsers::SearchUserByName(const char *name)
{
    item *tmp = first;
    while(tmp) {
        const char *search = name;
        const char *checking = tmp->u->GetName();
        if(strcmp(search, checking) == 0)
            return tmp->u;

        tmp = tmp->next;
    }

    return 0;
}

void StorageOfUsers::AddUser(UserInfo *u)
{
    item *p = new item;
    p->next = first;
    p->u = u;
    first = p;
    online++;
}

void StorageOfUsers::RemoveUser(UserInfo *u)
{
    item **p;
    for(p = &first; *p; p = &((*p)->next)) {
        if( ((*p)->u) == u ) {
            item *tmp = *p;
            *p = tmp->next;
            // not delete UserInfo!
            online--;
            delete tmp;
            tmp = 0; // null for first
            return;
        }
    }
}

UserInfo *StorageOfUsers::Disconnect()
{
    if(first) {
        item *tmp = first;
        first = first->next;
        UserInfo *rn = tmp->u;
        online--;
        delete tmp;
        return rn;
    }

    first = 0;
    return 0;
}