?????????????newlisp???????MongoDB. ?????????MongoDB C driver??????????????????????????????????????????????????C++??????????????????????????MongoDB C++ driver. ???????extern "C" ????C??????
???????????????????MongoDB C++ driver??????-fPIC??????
?????????????db.h???
extern "C" void CheckOnline();
extern "C" void Init(char * mongo_uri);
????????db.cc???
#include "db.h"
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include "mongo_session_factory.h"
#include "mongo_session.h"
#include <mongo/bson/bsonobj.h>
#include <mongo/bson/bsonobjbuilder.h>
using namespace std;
class Config {
public:
string mongo_uri;
};
Config config;
void SetOffline(mongo::DBClientBase& con?? mongo::OID const& id) {
mongo::BSONObjBuilder condition;
condition.append("_id"?? id);
mongo::BSONObjBuilder update_field;
update_field.append("status"?? "offline");
mongo::BSONObjBuilder set_field;
set_field.append("$set"?? update_field.obj());
con.update("kaimei.display"?? mongo::Query(condition.obj())?? set_field.obj());
}
/**
* Check every sign status
*/
void CheckOnline() {
cout << config.mongo_uri << endl;
std::shared_ptr<MongoSession> session = MongoSessionFactory::GetSession(config.mongo_uri);
mongo::DBClientBase & con = session->get_db_client_base();
mongo::BSONObjBuilder condition;
std::unique_ptr<mongo::DBClientCursor> cursor = con.query("kaimei.display"?? mongo::Query(condition.obj()));
while (cursor->more()) {
mongo::BSONObj record = cursor->next();
uint64_t t1 = record.getField("last_active_time").numberLong();
timeval tv;
gettimeofday(&tv??NULL);
if (tv.tv_sec - t1 > 300) {
SetOffline(con?? record.getField("_id").OID());
}
}
}
void Init(char * mongo_uri) {
config.mongo_uri = mongo_uri;
}