solaris changes
[csql.git] / src / tools / csqlreplserver.cxx
blob866addb1b5dcf6e766f5377de0942f37ae051a7f
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 fclose(fp);
84 NetworkServer *nwServer;
86 nwServer = new UDPServer();
88 nwServer->setServerPort(port);
89 rv = nwServer->start();
90 if (rv != OK) {
91 printf("Unable to start the server\n");
92 return 1;
94 printf("Replication server started\n");
95 fd_set fdset;
96 int ret = 0;
97 struct timeval timeout, tval;
98 timeout.tv_sec = 5;
99 timeout.tv_usec = 0;
101 while(!srvStop)
103 FD_ZERO(&fdset);
104 FD_SET(nwServer->getSocket(), &fdset);
105 tval.tv_sec = timeout.tv_sec;
106 tval.tv_usec = timeout.tv_usec;
107 ret = os::select(nwServer->getSocket()+1, &fdset, 0, 0, &tval);
108 if (ret > 0) {
109 nwServer->handleClient();
111 printf("Server Waiting for clients\n");
113 printf("Replication Server Exiting\n");
114 nwServer->stop();
115 SqlNetworkHandler::conn->disconnect();
116 delete SqlNetworkHandler::conn;
117 return 0;