7 int main(
int argc,
char *argv[]){
11 (sscanf(argv[7],
"%d",&finTime) != 1) ||
14 fatal(
"usage: SqlProxy intIp extIp db host user pass runTime");
17 if (extIp == 0) fatal(
"can't retrieve default IP address");
20 if(!sp.
init(intIp,extIp,argv[3],argv[4],argv[5],argv[6])) fatal(
"Failed to init client proxy sockets");
21 sp.
run(1000000*finTime);
24 bool SqlProxy::init(ipa_t intIp, ipa_t extIp,
char* db,
char* host,
char* user,
char* pass) {
28 if(tcpSockInt < 0 || tcpSockExt < 0 ||
33 cerr <<
"failed to initialize external socket" << endl;
37 sqlconn =
new mysqlpp::Connection(db,host,user,pass);
38 }
catch(
const mysqlpp::Exception& er) {
39 fatal(
"cannot connect to mysql db");
45 uint32_t now,nextTime;
46 now = nextTime = Util::getTime();
47 while(now <= runTime) {
48 ipa_t avIp; ipp_t avPort;
52 char* sqlStr =
new char[500];
53 if(
Np4d::recvBuf(sqlSock,sqlStr,500) <= 0) cerr <<
"couldn't receive sql string\n";
54 vector<user_pass> results;
56 mysqlpp::Query q = sqlconn->query();
59 if(results.size() > 0) {
60 string buf = (string)results[0].pass;
61 Np4d::sendBuf(sqlSock,(
char*)buf.c_str(),buf.size()+1);
63 }
catch(mysqlpp::Exception e) {
64 cerr <<
"MySQL error\n";
70 nextTime += 1000*UPDATE_PERIOD;
72 useconds_t delay = nextTime - now;
73 if (delay < ((uint32_t) 1) << 31) usleep(delay);
74 else nextTime = now + 1000*UPDATE_PERIOD;