forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
SqlProxy.cpp
1 #include "stdinc.h"
2 #include "SqlProxy.h"
3 
4 namespace forest {
5 
6 
7 int main(int argc, char *argv[]){
8  uint32_t finTime;
9  ipa_t intIp, extIp;
10  if(argc != 8 ||
11  (sscanf(argv[7],"%d",&finTime) != 1) ||
12  (intIp = Np4d::ipAddress(argv[1])) == 0 ||
13  (extIp = Np4d::ipAddress(argv[2])) == 0)
14  fatal("usage: SqlProxy intIp extIp db host user pass runTime");
15 
16  if (extIp == Np4d::ipAddress("127.0.0.1")) extIp = Np4d::myIpAddress();
17  if (extIp == 0) fatal("can't retrieve default IP address");
18 
19  SqlProxy sp;
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);
22 }
23 
24 bool SqlProxy::init(ipa_t intIp, ipa_t extIp, char* db, char* host, char* user, char* pass) {
25  tcpSockInt = Np4d::streamSocket();
26  tcpSockExt = Np4d::streamSocket();
27  sqlSock = -1;
28  if(tcpSockInt < 0 || tcpSockExt < 0 ||
29  !Np4d::bind4d(tcpSockInt,intIp,30190) ||
30  !Np4d::bind4d(tcpSockExt,extIp,30191) ||
31  !Np4d::listen4d(tcpSockInt) || !Np4d::nonblock(tcpSockInt) ||
32  !Np4d::listen4d(tcpSockExt) || !Np4d::nonblock(tcpSockExt)) {
33  cerr << "failed to initialize external socket" << endl;
34  return false;
35  }
36  try {
37  sqlconn = new mysqlpp::Connection(db,host,user,pass);
38  } catch(const mysqlpp::Exception& er) {
39  fatal("cannot connect to mysql db");
40  }
41  return true;
42 }
43 
44 void SqlProxy::run(uint32_t runTime) {
45  uint32_t now,nextTime;
46  now = nextTime = Util::getTime();
47  while(now <= runTime) {
48  ipa_t avIp; ipp_t avPort;
49  sqlSock = Np4d::accept4d(tcpSockInt,avIp,avPort);
50  if(sqlSock <= 0) sqlSock = Np4d::accept4d(tcpSockExt,avIp,avPort);
51  if(sqlSock > 0) {
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;
55  try {
56  mysqlpp::Query q = sqlconn->query();
57  q << sqlStr;
58  q.storein(results);
59  if(results.size() > 0) {
60  string buf = (string)results[0].pass;
61  Np4d::sendBuf(sqlSock,(char*)buf.c_str(),buf.size()+1);
62  }
63  } catch(mysqlpp::Exception e) {
64  cerr << "MySQL error\n";
65  }
66  //send sql result back
67  close(sqlSock);
68  sqlSock = -1;
69  }
70  nextTime += 1000*UPDATE_PERIOD;
71  now = Misc::getTime();
72  useconds_t delay = nextTime - now;
73  if (delay < ((uint32_t) 1) << 31) usleep(delay);
74  else nextTime = now + 1000*UPDATE_PERIOD;
75  }
76 }
77 
78 } // ends namespace
79