serverManage.cpp
00001
00002
00003
00004
00005
00006
00007 #include "nettyping.h"
00008 #include "packetTypes.h"
00009 #include "serverManage.h"
00010 #include "httpAccess.h"
00011 #include "timeUtils.h"
00012 #include <unistd.h>
00013
00014
00015 ServerManage::ServerManage(const char* host, const char* page)
00016 : activated(false), meta_nortify(false),
00017 meta_host(host), meta_page(page), server_port(0) {
00018 }
00019
00020
00021 ServerManage::~ServerManage(void) {
00022 }
00023
00024
00025 std::string ServerManage::getOwnIP(void) {
00026 return (con) ? con->getOwnIP() : "";
00027 }
00028
00029
00030 bool ServerManage::activate(unsigned short port) {
00031 if (activated) {
00032 delete con;
00033 con = NULL;
00034 }
00035
00036 try {
00037 con = new TcpipServer();
00038 con->activate(port);
00039 activated = true;
00040
00041 } catch (ConnectionDevice_Exception& e) {
00042 activated = false;
00043 }
00044 return activated;
00045 }
00046
00047
00048 bool ServerManage::isActivated(void) {
00049 return activated;
00050 }
00051
00052
00053
00054 void ServerManage::nortifyToMetaServer(void) {
00055 if (meta_nortify) {
00056 HttpAccess request(meta_host.c_str(), 80);
00057 request.addItem("port", server_port);
00058 request.addItem("people", con_users.size());
00059 request.sendPostRequest(meta_page.c_str());
00060 }
00061 }
00062
00063
00064 void ServerManage::processFork(unsigned short port, bool nortify) {
00065 server_port = port;
00066
00067 pid_t pid = fork();
00068 if (pid != 0) {
00069 return;
00070 }
00071 meta_nortify = nortify;
00072
00073
00074 TcpipDevice* new_user = con->accept(FirstTimeout);
00075 if (!new_user) {
00076 _exit(1);
00077 }
00078 con_users.push_back(new_user);
00079 nortifyToMetaServer();
00080
00081 processServerTask();
00082 }
00083
00084
00085 std::string ServerManage::createPacket(unsigned char packet_type,
00086 const unsigned char* data) {
00087
00088
00089
00090 return "";
00091 }
00092
00093
00094 void ServerManage::setOtherUser(int index, std::string packet) {
00095
00096
00097
00098 }
00099
00100
00101 void ServerManage::handlePacket(int index, TcpipDevice* con) {
00102
00103
00104 }
00105
00106
00107 void ServerManage::clearPacket(void) {
00108 user_packet.assign(con_users.size(), "");
00109 }
00110
00111
00112 void ServerManage::sendPacket(void) {
00113 int index = 0;
00114 for (std::vector<std::string>::iterator it = user_packet.begin();
00115 it != user_packet.end(); ++it, ++index) {
00116 if (!it->empty()) {
00117 con_users[index]->send(it->c_str(), it->size());
00118 }
00119 }
00120 }
00121
00122
00123 void ServerManage::processServerTask(void) {
00124 unsigned long nortify_sec = VXV::GetTicks() / 1000;
00125 while (con_users.size() > 0) {
00126
00127
00128 clearPacket();
00129 int index = 0;
00130 for (std::vector<TcpipDevice*>::iterator it = con_users.begin();
00131 it != con_users.end(); ++it, ++index) {
00132 if ((*it)->size() > 0) {
00133 handlePacket(index, *it);
00134 }
00135 if (!(*it)->isConnected()) {
00136 setOtherUser(index, createPacket(RemoveUser, NULL));
00137 }
00138 }
00139
00140 sendPacket();
00141
00142
00143 bool deleted = false;
00144 for (std::vector<TcpipDevice*>::iterator it = con_users.begin();
00145 it != con_users.end();) {
00146 if (!(*it)->isConnected()) {
00147 it = con_users.erase(it);
00148 deleted = true;
00149 } else {
00150 ++it;
00151 }
00152 }
00153 if (deleted) {
00154 nortifyToMetaServer();
00155 }
00156
00157
00158 TcpipDevice* new_user = con->accept(100);
00159 if (new_user) {
00160 if (con_users.size() < HandleUsersMax) {
00161 con_users.push_back(new_user);
00162 nortifyToMetaServer();
00163 } else {
00164 delete new_user;
00165 }
00166 }
00167
00168
00169 if (meta_nortify) {
00170 unsigned long now_sec = VXV::GetTicks() / 1000;
00171 if (now_sec > (nortify_sec + MetaServerUpdateInterval)) {
00172 nortifyToMetaServer();
00173 nortify_sec += MetaServerUpdateInterval;
00174 }
00175 }
00176 }
00177 _exit(0);
00178 }
00179