adding sqlserver executable
[csql.git] / src / tools / csqlsqlserver.cxx
blob41c6cf5c7403c39a0ac99a062f7ae25a3131175f
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 <Network.h>
23 #include <SqlLogStatement.h> //for BindSqlField
24 #include <SqlNetworkHandler.h>
26 int srvStop =0;
27 static void sigTermHandler(int sig)
29 printf("Received signal %d\nStopping the server\n", sig);
30 srvStop = 1;
33 void printUsage()
35 printf("Usage: csqlreplserver \n");
36 printf("Description: Start the csql replication server.\n");
37 return;
39 int main(int argc, char **argv)
41 int c = 0, opt = 0;
42 while ((c = getopt(argc, argv, "?")) != EOF)
44 switch (c)
46 case '?' : { opt = 10; break; } //print help
47 default: opt=10;
50 }//while options
52 if (opt == 10) {
53 printUsage();
54 return 0;
57 os::signal(SIGINT, sigTermHandler);
58 os::signal(SIGTERM, sigTermHandler);
60 bool end = false;
61 SqlNetworkHandler::type = CSql;
62 SqlNetworkHandler::conn = SqlFactory::createConnection(CSql);
63 DbRetVal rv = SqlNetworkHandler::conn->connect("root", "manager");
64 if (rv != OK) return 1;
65 if (!Conf::config.useReplication())
67 printf("Replication is set to OFF in csql.conf file\n");
68 SqlNetworkHandler::conn->disconnect();
69 return 1;
73 FILE *fp = NULL;
74 int nwid =0;
75 char hostname[IDENTIFIER_LENGTH];
76 int port=0;
77 fp = fopen(Conf::config.getReplConfigFile(),"r");
78 if( fp == NULL ) {
79 printError(ErrSysInit, "Invalid path/filename for REPL_CONFIG_FILE.\n");
80 SqlNetworkHandler::conn->disconnect();
81 return 1;
83 bool found =false;
84 while(!feof(fp)) {
85 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
86 printf( "%d:%d:%s\n", nwid, port, hostname);
87 if (nwid == Conf::config.getNetworkID()) { found = true; break;}
89 fclose(fp);
90 if (!found)
92 printf("Info not found in REPL_CONFIG_FILE for nwid %d\n",
93 Conf::config.getNetworkID());
94 SqlNetworkHandler::conn->disconnect();
95 return 1;
97 NetworkServer *nwServer;
99 nwServer = new UDPServer();
101 nwServer->setServerPort(port);
102 rv = nwServer->start();
103 if (rv != OK) {
104 printf("Unable to start the server\n");
105 return 1;
107 printf("Replication server started\n");
108 fd_set fdset;
109 int ret = 0;
110 struct timeval timeout, tval;
111 timeout.tv_sec = 5;
112 timeout.tv_usec = 0;
114 while(!srvStop)
116 FD_ZERO(&fdset);
117 FD_SET(nwServer->getSocket(), &fdset);
118 tval.tv_sec = timeout.tv_sec;
119 tval.tv_usec = timeout.tv_usec;
120 ret = os::select(nwServer->getSocket()+1, &fdset, 0, 0, &tval);
121 if (ret > 0) {
122 nwServer->handleClient();
124 printf("Server Waiting for clients\n");
126 printf("Replication Server Exiting\n");
127 nwServer->stop();
128 SqlNetworkHandler::conn->disconnect();
129 delete SqlNetworkHandler::conn;
130 return 0;