00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <AbsSqlConnection.h>
00017 #include <AbsSqlStatement.h>
00018 #include <SqlConnection.h>
00019 #include <SqlStatement.h>
00020 #include <SqlFactory.h>
00021 #include <CSql.h>
00022 #include <Network.h>
00023 #include <SqlLogStatement.h>
00024 #include <SqlNetworkHandler.h>
00025
00026 int srvStop =0;
00027 static void sigTermHandler(int sig)
00028 {
00029 printf("Received signal %d\nStopping the server\n", sig);
00030 srvStop = 1;
00031 }
00032
00033 void printUsage()
00034 {
00035 printf("Usage: csqlreplserver \n");
00036 printf("Description: Start the csql replication server.\n");
00037 return;
00038 }
00039 int main(int argc, char **argv)
00040 {
00041 int c = 0, opt = 0;
00042 while ((c = getopt(argc, argv, "?")) != EOF)
00043 {
00044 switch (c)
00045 {
00046 case '?' : { opt = 10; break; }
00047 default: opt=10;
00048
00049 }
00050 }
00051
00052 if (opt == 10) {
00053 printUsage();
00054 return 0;
00055 }
00056
00057 os::signal(SIGINT, sigTermHandler);
00058 os::signal(SIGTERM, sigTermHandler);
00059
00060 bool end = false;
00061 SqlNetworkHandler::type = CSql;
00062 SqlNetworkHandler::conn = SqlFactory::createConnection(CSql);
00063 DbRetVal rv = SqlNetworkHandler::conn->connect("root", "manager");
00064 if (rv != OK) return 1;
00065 if (!Conf::config.useReplication())
00066 {
00067 printf("Replication is set to OFF in csql.conf file\n");
00068 SqlNetworkHandler::conn->disconnect();
00069 return 1;
00070 }
00071
00072
00073 FILE *fp = NULL;
00074 int nwid =0;
00075 char hostname[IDENTIFIER_LENGTH];
00076 int port=0;
00077 fp = fopen(Conf::config.getReplConfigFile(),"r");
00078 if( fp == NULL ) {
00079 printError(ErrSysInit, "Invalid path/filename for REPL_CONFIG_FILE.\n");
00080 SqlNetworkHandler::conn->disconnect();
00081 return 1;
00082 }
00083 bool found =false;
00084 while(!feof(fp)) {
00085 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
00086 printf( "%d:%d:%s\n", nwid, port, hostname);
00087 if (nwid == Conf::config.getNetworkID()) { found = true; break;}
00088 }
00089 fclose(fp);
00090 if (!found)
00091 {
00092 printf("Info not found in REPL_CONFIG_FILE for nwid %d\n",
00093 Conf::config.getNetworkID());
00094 SqlNetworkHandler::conn->disconnect();
00095 return 1;
00096 }
00097 NetworkServer *nwServer;
00098
00099 nwServer = new UDPServer();
00100
00101 nwServer->setServerPort(port);
00102 rv = nwServer->start();
00103 if (rv != OK) {
00104 printf("Unable to start the server\n");
00105 return 1;
00106 }
00107 printf("Replication server started\n");
00108 fd_set fdset;
00109 int ret = 0;
00110 struct timeval timeout, tval;
00111 timeout.tv_sec = 5;
00112 timeout.tv_usec = 0;
00113
00114 while(!srvStop)
00115 {
00116 FD_ZERO(&fdset);
00117 FD_SET(nwServer->getSocket(), &fdset);
00118 tval.tv_sec = timeout.tv_sec;
00119 tval.tv_usec = timeout.tv_usec;
00120 ret = os::select(nwServer->getSocket()+1, &fdset, 0, 0, &tval);
00121 if (ret > 0) {
00122 nwServer->handleClient();
00123 }
00124 printf("Server Waiting for clients\n");
00125 }
00126 printf("Replication Server Exiting\n");
00127 nwServer->stop();
00128 SqlNetworkHandler::conn->disconnect();
00129 delete SqlNetworkHandler::conn;
00130 return 0;
00131 }