-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
194 lines (169 loc) · 5.29 KB
/
client.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
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
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
typedef std::shared_ptr<tcp::socket> socket_ptr;
typedef std::shared_ptr<string> string_ptr;
typedef std::shared_ptr< queue<string_ptr> > messageQueue_ptr;
io_service service; // Boost Asio io_service object
messageQueue_ptr messageQueue(new queue<string_ptr>); // Message queue
tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001); // TCP socket for connecting to server
const int inputSize = 2048; // Maximum size for input buffer
string_ptr promptCpy; // Terminal prompt displayed to chat users
//predefined messages:
vector<string> messageList = {"hello","🐹","👾"};
// Function Declarations
bool isOwnMessage(string_ptr);
void displayLoop(socket_ptr);
void inboundLoop(socket_ptr, string_ptr);
void writeLoop(socket_ptr, string_ptr);
string* buildPrompt();
int main(int argc, char** argv)
{
try
{
boost::thread_group threads;
socket_ptr sock(new tcp::socket(service));
string_ptr prompt( buildPrompt() );
promptCpy = prompt;
sock->connect(ep);
cout << "Welcome to the ChatServer\nType \"exit\" to quit" << endl;
threads.create_thread(boost::bind(displayLoop, sock));
threads.create_thread(boost::bind(inboundLoop, sock, prompt));
threads.create_thread(boost::bind(writeLoop, sock, prompt));
threads.join_all();
}
catch(std::exception& e)
{
cerr << e.what() << endl;
}
puts("Press any key to continue...");
getc(stdin);
return EXIT_SUCCESS;
}
string* buildPrompt()
{
//const int inputSize = 256;
//char inputBuf[inputSize] = {0};
char nameBuf[inputSize] = {0};
string* prompt = new string(": ");
cout << "Please input a new username: ";
cin.getline(nameBuf, inputSize);
*prompt = (string)nameBuf + *prompt;
boost::algorithm::to_lower(*prompt);
return prompt;
}
void inboundLoop(socket_ptr sock, string_ptr prompt)
{
std::size_t bytesRead = 0;
char readBuf[1024] = {0};
for(;;)
{
if(sock->is_open() && sock->available())
{
bytesRead = sock->read_some(buffer(readBuf, inputSize));
string_ptr msg(new string(readBuf, bytesRead));
messageQueue->push(msg);
cout << "messages received correctly" << endl;
}
boost::this_thread::sleep( boost::posix_time::millisec(50));
}
}
void writeLoop(socket_ptr sock, string_ptr prompt)
{
// char inputBuf[inputSize] = {0};
// string inputMsg;
//
// for(;;)
// {
// cin.getline(inputBuf, inputSize);
// inputMsg = *prompt + (string)inputBuf + '\n';
//
// if(!inputMsg.empty())
// {
// sock->write_some(buffer(inputMsg, inputSize));
// }
//
// if(inputMsg.find("exit") != string::npos)
// exit(1);
//
// inputMsg.clear();
// memset(inputBuf, 0, inputSize);
// }
size_t messageCount = messageList.size();
srand((unsigned int)time(NULL));
for (;;){
int number = rand() % 3 + 1;
if (number == 1)
{
/*if (!sock->is_open())
{
sock.reset(new tcp::socket(service));
sock->connect(ep);
}*/
string temp = "msg";
sock->write_some(buffer(temp + *prompt + messageList[rand() % messageCount]));
boost::this_thread::sleep( boost::posix_time::millisec(1000));
}
else if (number == 2)
{
/*if (!sock->is_open())
{
sock.reset(new tcp::socket(service));
sock->connect(ep);
} */
string temp = "job";
temp += *prompt + " ";
int length = (rand() % 4 + 1) * 100;
int jobID = rand() % 1000 + 1;
int priority = rand() % 100 + 1;
temp += std::to_string(jobID) + " ";
temp += std::to_string(length) + " ";
temp += std::to_string(priority) + " ";
cout << "Job Create: " << " JobId: " << std::to_string(jobID) << " Job Priority: " << std::to_string(priority) << " Job length: " << std::to_string(priority) << endl;
sock->write_some(buffer(temp));
boost::this_thread::sleep( boost::posix_time::millisec(1000));
}
/*else
{
if (sock->is_open())
{
sock->write_some(buffer("exit"));
boost::this_thread::sleep( boost::posix_time::millisec(1000));
}
}*/
}
}
void displayLoop(socket_ptr sock)
{
for(;;)
{
if(!messageQueue->empty())
{
if(!isOwnMessage(messageQueue->front()))
{
cout << "\n" + *(messageQueue->front()) << endl;
}
messageQueue->pop();
}
boost::this_thread::sleep( boost::posix_time::millisec(50));
}
}
bool isOwnMessage(string_ptr message)
{
if(message->find(*promptCpy) != string::npos)
return true;
else
return false;
}