📄 clidb.cpp
字号:
"<INPUT TYPE=submit NAME=\"action\" VALUE=\"Change\"></FORM><P>"
"<H2>Select client</H2>"
"<FORM METHOD=POST ACTION=\"" << con.getStub() << "\">"
"<INPUT TYPE=HIDDEN NAME=\"socket\" VALUE=\""<< con.getAddress() <<
"\"><INPUT TYPE=hidden NAME=\"page\" VALUE=\"editClientForm\">"
"<INPUT TYPE=hidden NAME=\"manager\" VALUE=\"" << the.name << "\">"
"<INPUT TYPE=hidden NAME=\"segment\" VALUE=\""
<< the.segments->name << "\">"
"<SELECT SIZE=15 NAME=\"client\">";
the.clientsArray = &the.segments->clients;
if (the.clients.select(the.qClients) != 0) {
print(con, the.clients);
con << TAG <<
"</SELECT><BR><INPUT TYPE=submit VALUE=\"Select\"> ";
} else {
con << TAG << EMPTY_LIST;
}
con << TAG << "<INPUT TYPE=submit NAME=\"new\" VALUE=\"New\"></FORM>";
}
con << TAG << "</BODY></HTML>";
return true;
}
bool login(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
the.name = con.get("name");
if (the.managers.select(the.qManager) == 0) {
return error(con, "No such manager");
}
if (!Manager::loginFromAnyHost
&& strcmp(the.managers->IPaddr, "*") != 0
&& strcmp(the.managers->IPaddr, con.getPeer()) != 0)
{
return error(con, "You can not login from this host");
}
con.addPair("manager", the.name);
return managerForm(con);
}
bool shutdown(WWWconnection& con)
{
con << TAG <<
HTML_HEAD "<TITLE>ClientDB message</TITLE></HEAD><BODY>"
"<H1>ClientDB server is terminated</H1></BODY></HTML>";
return false;
}
bool addManager(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
Manager manager;
the.name = con.get("segment");
if (the.segments.select(the.qSegment, dbCursorForUpdate) == 0) {
return error(con, "No such segment");
}
the.name = con.get("name");
if (the.managers.select(the.qManager) != 0) {
return error(con, "Manager with such name already exists");
}
manager.name = con.get("name");
manager.IPaddr = con.get("ipaddr");
manager.flags = con.get("miniadmin") ? Manager::isMinAdmin : 0;
manager.segment = the.segments.currentId();
insert(manager);
return managerForm(con);
}
bool addClient(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
Client client;
the.sequencer.select(dbCursorForUpdate);
the.name = con.get("segment");
if (the.segments.select(the.qSegment) == 0) {
return error(con, "No such segment");
}
client.organization = con.get("organization");
client.phone = con.get("phone");
client.phone2 = con.get("phone2");
client.fax = con.get("fax");
client.email = con.get("email");
client.www = con.get("www");
client.person = con.get("person");
client.person2 = con.get("person2");
client.lpr = con.get("lpr");
client.addr = con.get("addr");
client.info = con.get("info");
client.status = con.get("status");
client.clientId = ++the.sequencer->lastClientId;
client.segment = the.segments.currentId();
the.sequencer.update();
insert(client);
return managerForm(con);
}
bool addSegment(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
Segment segment;
segment.name = the.name = con.get("name");
if (the.segments.select(the.qSegment, dbCursorForUpdate) != 0) {
return error(con, "Segment with such name already exists");
}
insert(segment);
return managerForm(con);
}
bool addHistory(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
the.sequencer.select(dbCursorForUpdate);
the.clientId = atoi(con.get("client"));
if (the.clients.select(the.qClient) == 0) {
error(con, "No such client");
return true;
}
History his;
his.messageId = ++the.sequencer->lastMessageId;
his.message = con.get("message");
his.client = the.clients.currentId();
the.name = con.get("manager");
if (the.managers.select(the.qManager) == 0) {
error(con, "No such manger");
return true;
}
his.manager = the.managers.currentId();
his.date = dbDateTime::current();
insert(his);
the.sequencer.update();
return managerForm(con);
}
bool editManager(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
the.name = con.get("oldName");
if (the.managers.select(the.qManager, dbCursorForUpdate) == 0) {
error(con, "No such manager");
return true;
}
if (strcmp(con.get("action"), "Remove") == 0) {
the.managers.remove();
return managerForm(con);
}
the.managers->name = con.get("name");
the.managers->IPaddr = con.get("ipaddr");
if (con.get("miniadmin")) {
the.managers->flags |= Manager::isMinAdmin;
} else {
the.managers->flags &= ~Manager::isMinAdmin;
}
the.managers.update();
return managerForm(con);
}
bool editClient(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
the.clientId = atoi(con.get("client"));
if (the.clients.select(the.qClient, dbCursorForUpdate) == 0) {
error(con, "No such client");
return true;
}
if (strcmp(con.get("action"), "Remove") == 0) {
the.clients.remove();
} else {
the.clients->organization = con.get("organization");
the.clients->phone = con.get("phone");
the.clients->phone2 = con.get("phone2");
the.clients->fax = con.get("fax");
the.clients->email = con.get("email");
the.clients->www = con.get("www");
the.clients->person = con.get("person");
the.clients->person2 = con.get("person2");
the.clients->lpr = con.get("lpr");
the.clients->addr = con.get("addr");
the.clients->info = con.get("info");
the.clients->status = con.get("status");
the.clients.update();
}
return managerForm(con);
}
bool editSegment(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
the.name = con.get("oldName");
if (the.segments.select(the.qSegment, dbCursorForUpdate) == 0) {
error(con, "No such segment");
return true;
}
if (strcmp(con.get("action"), "Remove") == 0) {
the.segments.remove();
} else {
the.segments->name = con.get("name");
the.segments.update();
}
return managerForm(con);
}
bool editHistory(WWWconnection& con)
{
ThreadContext& the = ThreadContext::of(con);
the.messageId = atoi(con.get("messageId"));
if (the.messages.select(the.qMessage, dbCursorForUpdate) == 0) {
error(con, "No such message");
return true;
}
if (strcmp(con.get("action"), "Remove") == 0) {
the.messages.remove();
} else {
the.messages->message = con.get("message");
the.messages.update();
}
return managerForm(con);
}
WWWapi::dispatcher dispatchTable[] = {
{"addManagerForm", addManagerForm},
{"addClientForm", addClientForm},
{"addSegmentForm", addSegmentForm},
{"addHistoryForm", addHistoryForm},
{"editManagerForm", editManagerForm},
{"editClientForm", editClientForm},
{"editSegmentForm", editSegmentForm},
{"editHistoryForm", editHistoryForm},
{"managerForm", managerForm},
{"login", login},
{"shutdown", shutdown},
{"addManager", addManager},
{"addClient", addClient},
{"addSegment", addSegment},
{"addHistory", addHistory},
{"editManager", editManager},
{"editClient", editClient},
{"editSegment", editSegment},
{"editHistory", editHistory}
};
#ifdef USE_EXTERNAL_HTTP_SERVER
CGIapi wwwServer(db, itemsof(dispatchTable), dispatchTable);
char* defaultAddress = "localhost:6101";
socket_t::socket_domain domain = socket_t::sock_local_domain;
#else
#ifdef USE_QUEUE_MANAGER
HTTPapi wwwServer(db, itemsof(dispatchTable), dispatchTable, true);
#else
HTTPapi wwwServer(db, itemsof(dispatchTable), dispatchTable, false);
#endif
char* defaultAddress = "localhost:80";
socket_t::socket_domain domain = socket_t::sock_global_domain;
#endif
int main(int argc, char* argv[])
{
char* address = (argc > 1) ? argv[1] : defaultAddress;
if (!wwwServer.open(address, domain)) {
fprintf(stderr, "Failed to open WWW session\n");
return EXIT_FAILURE;
}
if (!db.open("clidb")) {
fprintf(stderr, "Failed to open database\n");
return EXIT_FAILURE;
}
dbCursor<Sequencer> sequencer;
if (sequencer.select() == 0) {
Sequencer seq;
seq.lastClientId = 0;
seq.lastMessageId = 0;
insert(seq);
Manager administrator;
administrator.name = "administrator";
administrator.IPaddr = "*";
administrator.flags = Manager::isAdministrator;
insert(administrator);
}
db.commit();
if (argc > 2 && strcmp(argv[2], "login_from_any_host") == 0) {
Manager::loginFromAnyHost = true;
}
#ifdef USE_QUEUE_MANAGER
QueueManager qmgr(wwwServer, db);
qmgr.start();
#else
WWWconnection con;
while (wwwServer.connect(con) && wwwServer.serve(con));
#endif
db.close();
printf("End of session\n");
return EXIT_SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -