-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
125 lines (116 loc) · 2.65 KB
/
main.cpp
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
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "user.h"
#include "connection.h"
#include <list>
#include "message.h"
#include "login.h"
#include "iplist.h"
#define MAX_LOGIN_TRY 3
iplist myipl = iplist();
void* do_client(void *xxx)
{
pthread_detach (pthread_self ());cout<<"thread created!"<<endl;
int pos,i=0;
login *mylog;
message mymsg = message();
connection mycon =*((connection *)xxx);
string h;
while(1)
{
myipl.debug_msg();
if(mycon.recive())
{
cout << "bad data"<<endl;
return NULL;
}
h=mycon.get_msg();
pos=h.find("\n",0);
h=h.substr(0,pos);
if(myipl.check(h))
{
mycon.say_err();
return NULL;
}
mylog= new login(mycon.get_msg());
//login wird erstellt mit den string "name\npasswd"
if((*mylog).proof())
{
mycon.say_err();
delete mylog;
i++;
if(i==MAX_LOGIN_TRY)
{
myipl.add(h);
return NULL;
}
}
else break;
}
mycon.say_ok();
user myusr = user((*mylog).get_name());
while(1)
{
if(mycon.recive())
{
cout<<"cant recive data"<<endl;
return NULL;
}
cout<<"recived"<<endl;
mymsg.open(mycon.get_msg());
switch(mymsg.get_type())
{
case 's':
h= mymsg.get_betreff()+ "\n"+ mymsg.get_message();
myusr.send(mymsg.get_reciverlist(),h);
cout<<mymsg.get_message()<<endl;
mycon.say_ok();
break;
case 'l':
//cout <<"sende list... "<<endl;
mycon.say_message(myusr.do_list());
break;
case 'r':
myusr.do_read(mymsg.get_msg_nmb());
break;
case 'd':
myusr.do_del(mymsg.get_msg_nmb());
break;
case 'q':
return NULL;
case 'e':
mycon.say_err();
break;
}
}
return NULL;
}
using namespace std;
int main()
{
string test;
list <pthread_t *> threads;
connection *mycon= new connection();
connection * copy = NULL;
while(1)
{
cout<<"waiting for someone.."<<endl;
if(!(*mycon).connect())
{
threads.push_back(new pthread_t);
cout << "createing thread..."<<endl;
// cout <<"ip: "<< debug <<endl;
copy = new connection(* mycon);
pthread_create( threads.back(), NULL, do_client,(void*) copy);
}
else return EXIT_FAILURE;
// pthread_create( threads.back(), NULL, do_client, (void*) &debug);
}
return EXIT_SUCCESS;
return 0;
}