📄 main.cpp
字号:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <signal.h>
#include <cstdio>
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include "Listing.h"
#include "Group.h"
#include "CategoryHierarchy.h"
#include "processrequest.h"
#include "main.h"
#include "Shortest.h"
using namespace std;
CategoryHierarchy categories;
Listing advertisements;
Group users;
int category_counter = 0;
int advertisement_counter = 0;
string active_user = "";
bool create_verified = true;
bool login_failed = false;
//just for debug
class Debug_ZHU{
static void addUser(const Client& client){
users.add(new Client(client));
}
//all is added in the root catorgroy
static void addAdvertisement( Advertisement& ad){
ad.setNumber(advertisement_counter);
advertisement_counter++;//modify the gloable var
advertisements.add(new Advertisement(ad));
users[ad.getEmail()]->addOffering(ad.getNumber());
categories[0]->addItem(ad.getNumber());
}
public:
static void preDebug(){
cout<<"In the preDebug!"<<endl;
Date start(0,0,0,0,0,0);
Date end(0,0,0,0,0,0);
addUser(Client(string("AF"),string("AL"),string("A@"),string("f")));
addUser(Client(string("BF"),string("BL"),string("B@"),string("f")));
addUser(Client(string("CF"),string("CL"),string("C@"),string("f")));
addUser(Client(string("DF"),string("DL"),string("D@"),string("f")));
addUser(Client(string("EF"),string("EL"),string("E@"),string("f")));
addUser(Client(string("FF"),string("FL"),string("F@"),string("f")));
//增加G@用户
addUser(Client(string("GF"),string("GL"),string("G@"),string("f")));
active_user = "A@";
Advertisement adver1("ad1","A@","Des1",start,end,1);//0
addAdvertisement(adver1);
Advertisement adver2("ad2","A@","Des2",start,end,1);//1
addAdvertisement(adver2);
Advertisement adver3("ad3","A@","Des3",start,end,1);//2
addAdvertisement(adver3);
Advertisement adver4("ad4","E@","Des4",start,end,1);//3
addAdvertisement(adver4);
Advertisement adver5("ad5","A@","Des5",start,end,1);//4
addAdvertisement(adver5);
Advertisement adver6("ad6","E@","Des6",start,end,1);//5
addAdvertisement(adver6);
//G发布了一个产品
Advertisement adver7("ad7","G@","Des7",start,end,1);//6
addAdvertisement(adver7);
Advertisement adver8("ad8","G@","Des8",start,end,1);//7
addAdvertisement(adver8);
advertisements[0]->getBids().push(Bid("B@",100,1,start));
users["B@"]->addBid(0);
advertisements[1]->getBids().push(Bid("C@",101,1,start));
users["C@"]->addBid(1);
advertisements[2]->getBids().push(Bid("D@",102,1,start));
users["D@"]->addBid(2);
advertisements[3]->getBids().push(Bid("D@",103,1,start));
users["D@"]->addBid(3);
advertisements[1]->getBids().push(Bid("E@",1000,1,start));
users["E@"]->addBid(1);
advertisements[4]->getBids().push(Bid("C@",100,1,start));
users["C@"]->addBid(4);
advertisements[5]->getBids().push(Bid("F@",1000,1,start));
users["F@"]->addBid(5);
//F 购买
advertisements[6]->getBids().push(Bid("F@",100,1,start));
users["F@"]->addBid(6);
//B 购买
advertisements[7]->getBids().push(Bid("B@",100,1,start));
users["B@"]->addBid(7);
//Shortest st("E@");
//st.get_path("B@");
//st.displayMap("debug.txt");
}
};
/*
* Not this is explicitly not concurrent -- the auction data structures
* are not necessarily concurrency safe
*/
int main (int argc, char *argv[]) {
/*
* argv[1] should be the port number
*/
/*if (2 != argc) {
cout << "Usage: " << argv[0] << " [port]" << endl << endl;
return -1;
}*/
int port = 4000;//atoi(argv[1]);
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}
/*
* Create and configure the socket
*/
int connfd = -1;
int listenfd = -1;
struct sockaddr_in cliaddr;
struct sockaddr_in servaddr;
int clilen = sizeof (cliaddr);
listenfd = socket (AF_INET, SOCK_STREAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
servaddr.sin_port = htons(port);
/*
* Bind to the socket
*/
bind (listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
/*
* Configure the listen queue
*/
listen (listenfd, BACKLOG);
/*
* Main work loop:
* Listen for connection, accept it, process request, print html page
*/
Category top_level(CategoryHierarchy::NO_PARENT, "Top Level");
top_level.setNumber(category_counter);
category_counter++;
categories.add(new Category(top_level));
Debug_ZHU::preDebug();
while (true) {
// Wait for the knock at the door
if (0 > (connfd = accept (listenfd,
(struct sockaddr *) &cliaddr, &clilen)) ) {
if (errno == EINTR)
continue;
else {
cerr << "unknown error: " << errno << " " << connfd << endl;
exit(-1); // Who knows? Bad foo. Maybe continue for roubustness?
}
}
// Process the request
char req_buffer[REQ_MAX];
int count;
for (count=0; count<REQ_MAX; count++) {
int retval = recv (connfd, req_buffer+count, 1, 0);
if (4 == req_buffer[count]) { // 4 is EOT, a.ka. CTRL-D
req_buffer[count] = '\0';
break;
}
}
// Move request to stream for C++ style processing
ostringstream oss;
oss.str("");
oss << req_buffer;
memset (req_buffer, 0, REQ_MAX);
cerr << oss.str() << endl;
istringstream iss (oss.str());
processrequest (iss, connfd, port);
// Done with this request
closesocket (connfd);
connfd = -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -