code reorg
[csql.git] / src / tools / csqlsqlserver.cxx
blob3d07b47fae107f110b00b7fae9bb5d847b68e4ba
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include <AbsSqlConnection.h>
17 #include <AbsSqlStatement.h>
18 #include <SqlConnection.h>
19 #include <SqlStatement.h>
20 #include <SqlFactory.h>
21 #include <CSql.h>
22 #include <SessionImpl.h>
23 #include <Network.h>
24 #include <SqlLogStatement.h> //for BindSqlField
25 #include <SqlNetworkHandler.h>
27 int srvStop =0;
28 bool gateway = false;
29 static void sigTermHandler(int sig)
31 printf("Received signal %d\nStopping the server\n", sig);
32 srvStop = 1;
35 void printUsage()
37 printf("Usage: csqlsqlserver \n");
38 printf("Description: Start the csql network server.\n");
39 return;
41 int main(int argc, char **argv)
43 int c = 0, opt = 0;
44 while ((c = getopt(argc, argv, "g?")) != EOF)
46 switch (c)
48 case '?' : { opt = 10; break; } //print help
49 case 'g' : { gateway = true; break; }
50 default: opt=10;
53 }//while options
55 if (opt == 10) {
56 printUsage();
57 return 0;
59 SessionImpl session;
60 DbRetVal rv = session.readConfigFile();
61 if (rv != OK)
63 printf("Unable to read the configuration file \n");
64 return 1;
66 os::signal(SIGINT, sigTermHandler);
67 os::signal(SIGTERM, sigTermHandler);
68 os::signal(SIGCHLD, SIG_IGN);
70 bool end = false;
71 SqlNetworkHandler::stmtID = 0;
72 if (!Conf::config.useCsqlSqlServer())
74 printf("Sql Network Server is set to OFF in csql.conf file\n");
75 return 1;
77 /*
78 FILE *fp = NULL;
79 int nwid =0;
80 char hostname[IDENTIFIER_LENGTH];
81 int port=0;
82 fp = fopen(Conf::config.getReplConfigFile(),"r");
83 if( fp == NULL ) {
84 printError(ErrSysInit, "Invalid path/filename for REPL_CONFIG_FILE.\n");
85 SqlNetworkHandler::conn->disconnect();
86 return 1;
88 bool found =false;
89 while(!feof(fp)) {
90 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
91 printf( "%d:%d:%s\n", nwid, port, hostname);
92 if (nwid == Conf::config.getNetworkID()) { found = true; break;}
94 fclose(fp);
95 if (!found)
97 printf("Info not found in REPL_CONFIG_FILE for nwid %d\n",
98 Conf::config.getNetworkID());
99 SqlNetworkHandler::conn->disconnect();
100 return 1;
103 int port = 0;
104 if ((port = Conf::config.getPort()) <= 1024)
106 printf("Invalid port Number\n");
107 return 1;
109 NetworkServer *nwServer;
110 nwServer = new TCPServer();
112 nwServer->setServerPort(port);
113 rv = nwServer->start();
114 if (rv != OK) {
115 printf("Unable to start the server\n");
116 delete nwServer;
117 return 1;
119 // printf("Network Server Started");
120 fd_set fdset;
121 int ret = 0;
122 struct timeval timeout, tval;
123 timeout.tv_sec = 5;
124 timeout.tv_usec = 0;
126 while(!srvStop)
128 FD_ZERO(&fdset);
129 FD_SET(nwServer->getSocket(), &fdset);
130 tval.tv_sec = timeout.tv_sec;
131 tval.tv_usec = timeout.tv_usec;
132 ret = os::select(nwServer->getSocket()+1, &fdset, 0, 0, &tval);
133 if (ret > 0) {
134 nwServer->handleClient();
136 // printf("Server Waiting for clients\n");
138 printf("Csql Network Daemon Exiting\n");
139 nwServer->stop();
140 delete nwServer;
141 return 0;