本文整理汇总了C++中Package::SerializeAsString方法的典型用法代码示例。如果您正苦于以下问题:C++ Package::SerializeAsString方法的具体用法?C++ Package::SerializeAsString怎么用?C++ Package::SerializeAsString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Package
的用法示例。
在下文中一共展示了Package::SerializeAsString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: general_replica
int general_replica(Package package, struct HostEntity &destination) {
package.set_replicano(3);
string str = package.SerializeAsString();
// cout << "socket_replica--------1" << endl;
// cout << "socket_replica--------before makeConnForReplica sock = "<< destination.sock << endl;
int sock = makeConnForReplica(destination); //reusable sockets creation
// cout << "socket_replica--------after makeConnForReplica sock = "<< destination.sock << endl;
// int sock = makeClientSocket("localhost", 50009, true);
// cout << "socket_replica--------2, sock = " << sock << endl;
// generalSend(destination.host, destination.port, sock, str.c_str(), 1);
// cout << "socket_replica--------2, sock = " << sock << endl;
// generalSendTCP(sock, str.c_str());
generalSendTo(destination.host.c_str(), destination.port, sock, str.c_str(),
str.size(), TCP);
// cout << "socket_replica--------3" << endl;
void *buff_return = (void*) malloc(sizeof(int32_t));
// int r = d3_svr_recv(sock, buff_return, sizeof(int32_t), 0, &recv_addr);
// int r = generalReveiveTCP(sock, buff_return, sizeof buff_return, 0);
struct sockaddr_in recvAddr;
// int r =generalReceive(sock, buff_return, sizeof(int32_t), recvAddr, 0, TCP);
int r = 0;
// cout << "socket_replica--------4" << endl;
//connect (int socket, struct sockaddr *addr, size_t length)
if (r < 0) {
cerr << "general_replica: got bad news from relica: " << r << endl;
}
}
示例2: c_zht_compare_and_swap_std
int c_zht_compare_and_swap_std(ZHTClient_c zhtClient, const char *key,
const char *seen_value, const char *new_value, char **value_queried) {
ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;
string sKey(key);
string sSeenValue(seen_value);
string sNewValue(new_value);
Package package;
package.set_virtualpath(sKey);
package.set_isdir(true);
package.set_replicano(5);
package.set_operation(5); // 5 for comapre and swap
if (!sSeenValue.empty()) {
package.set_realfullpath(sSeenValue);
}
if (!sNewValue.empty()) {
package.set_newfullpath(sNewValue);
}
string return_str;
int rc = zhtcppClient->compare_and_swap(package.SerializeAsString(),
return_str);
Package ret_pack;
ret_pack.ParseFromString(return_str);
strcpy(*value_queried, ret_pack.realfullpath().c_str());
return rc;
}
示例3: test_proto_buffer
/*
* to be removed.
*/
void test_proto_buffer(const char *key, const char *value) {
Package package;
package.set_virtualpath(key); //as key
package.set_isdir(true);
package.set_replicano(5);
package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
package.set_realfullpath(value);
fprintf(stderr, "package virtualpath is: %s\n",
package.virtualpath().c_str());
const char * realfullpath = package.realfullpath().c_str();
fprintf(stderr, "package realfullpath is: %s\n", realfullpath);
string str = package.SerializeAsString();
int len = strlen(str.c_str());
Package package2;
package2.ParseFromString(str);
string str2 = package2.SerializeAsString();
fprintf(stderr, "package2 virtualpath is: %s\n",
package2.virtualpath().c_str());
const char *realfullpath2 = package2.realfullpath().c_str();
fprintf(stderr, "package2 realfullpath is: %s\n", realfullpath2);
}
示例4: zht_remove
int Worker::zht_remove(string key) {
/*Package package;
package.set_virtualpath(key);
package.set_operation(2);
string str = package.SerializeAsString();
int index;
svrclient.str2Host(str, index);*/
int index = myhash(key.c_str(), svrclient.memberList.size());
if (index != selfIndex) {
Package package;
package.set_virtualpath(key);
package.set_operation(2);
string str = package.SerializeAsString();
pthread_mutex_lock(&msg_lock);
int ret = svrclient.remove(str);
pthread_mutex_unlock(&msg_lock);
return ret;
} else {
int ret = pmap->remove(key);
if (ret != 0) {
cerr << "DB Error: fail to remove :ret= " << ret << endl;
return -2;
} else
return 0; //succeed.
}
}
示例5: HB_insert
int32_t HB_insert(NoVoHT *map, Package &package) {
//int opt = package.operation();//opt not be used?
string value = package.SerializeAsString();
//int ret = db.set(package.virtualpath(), package_str); //virtualpath as key
// cout << "Insert to pmap...value = " << value << endl;
string key = package.virtualpath();
// cout<<"key:"<<key<<endl;
// cout<<"value:"<<value<<endl;
// cout<<"Insert: k-v ready. put..."<<endl;
int ret = map->put(key, value);
// cout << "end inserting, ret = " << ret << endl;
if (ret != 0) {
cerr << "insert error: ret = " << ret << endl;
return -3;
}
/*
cout << "String insted: " << package_str << endl;
*/
else
return 0;
}
示例6: removeMeta
int ZHTClient::removeMeta(string path) {
Package package;
package.set_operation(2); // 3 for insert, 1 for look up, 2 for remove
package.set_replicano(3); //5: original, 3 not original
package.set_virtualpath(path);
string str = package.SerializeAsString();
int sock = this->str2SockLRU(str, TCP);
reuseSock(sock);
// cout<<"sock = "<<sock<<endl;
struct HostEntity dest = this->str2Host(str);
sockaddr_in recvAddr;
int sentSize = generalSendTo(dest.host.data() ,dest.port, sock, str.c_str(), TCP);
// cout<<"remove sentSize "<< sentSize <<endl;
int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));
// generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0);
generalReceive(sock, (void*)ret_buf, sizeof(int32_t),recvAddr,0, TCP);
// generalSendTCP(sock, str.c_str());
// int32_t* ret = (int32_t*) malloc(sizeof(int32_t));
// generalReveiveTCP(sock, (void*) ret, sizeof(int32_t), 0);
int ret_1 = *(int32_t*) ret_buf;
// cout<<"remove got: "<< ret_1 <<endl;
// cout <<"Returned status: "<< *(int32_t*) ret<<endl;
// d3_closeConnection(sock);
free(ret_buf);
return ret_1;
}
示例7: HB_insert_cstr_
int32_t HB_insert_cstr_(map<char*, char*> &chmap, Package &package) {
string package_str = package.SerializeAsString();
char* value = (char*) malloc(package_str.length() * sizeof(char));
strcpy(value, package_str.c_str());
char* key = (char*) malloc(package.virtualpath().length() * sizeof(char));
// cout << "package.virtualpath().c_str()="<<package.virtualpath().c_str()<<endl;
strcpy(key, package.virtualpath().c_str());
// cout <<"after scrcpy, key = "<<key<<", key length = "<< strlen(key) <<endl;
// pair<map<char*, char*>::iterator, bool> ret;
// ret = chmap.insert(pair<char*, char*>(key, value));
// free(key);
// free(value);
if (chmap.insert(pair<char*, char*>(key, value)).second == false) {
cout
<< "HB_insert_cstr: insert failed, return -3, element exists, key = "
<< key << ", value = " << value << endl;
free(key);
free(value);
return -3;
} else {
cout << " HB_insert_cstr: insert succeeded. key = " << key
<< ", value = " << chmap.find(key)->second << endl;
free(key);
free(value);
return 0;
}
}
示例8: zht_insert
int Worker::zht_insert(string str) {
Package package;
package.ParseFromString(str);
package.set_operation(3);
str = package.SerializeAsString();
int index;
svrclient.str2Host(str, index);
if (index != selfIndex) { //cout << "NOOOOO..index = " << index << endl;
pthread_mutex_lock(&msg_lock);
int ret = svrclient.insert(str);
pthread_mutex_unlock(&msg_lock);
return ret;
} else {
string key = package.virtualpath(); //cout << "key = " << key << endl;
//pthread_mutex_lock(&zht_lock);
int ret = pmap->put(key, str);
//pthread_mutex_unlock(&zht_lock);
if (ret != 0) {
cerr << "insert error: key = " << key << " ret = " << ret << endl;
return -3;
} else
return 0;
}
}
示例9: zht_insert_meta
int zht_insert_meta(ZHTClient_c zhtClient, struct metadata * meta){
ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;
Package package;
string keyStr(meta->key);
//1. General infos on the file/storage
package.set_virtualpath(keyStr); //as key
package.set_isdir(false);
package.set_replicano(1);
package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
package.set_realfullpath(keyStr);
//2. General infos on the encoding
package.set_k(meta->k);
package.set_m(meta->m);
package.set_encodinglib(meta->encodingLib);
package.set_filesize(meta->fileSize);
package.set_bufsize(meta->bufsize);
//3. Chunks/Replicas locations
struct comTransfer * current = meta->loc->transfers;
Package_Location* location;
while(current != NULL){
location = package.add_location();
location->set_hostname(string(current->hostName));
location->set_port(current->port);
location->set_distantchunkname(string(current->distantChunkName));
current=current->next;
}
//4. Insertion
std::string serialized = package.SerializeAsString();
dbgprintf("Package Length:%i\n",serialized.size());
//TEST
Package package4;
package4.ParseFromString(serialized);
dbgprintf("Serialized: %s \n",serialized.c_str());
dbgprintf("key: %s \n",(package4.virtualpath()).c_str());
dbgprintf("k:%i,m:%i,encodinglib:%i,filesize:%i \n",package4.k(),package4.m(),package4.encodinglib(),package4.filesize());
for (int j = 0; j < package4.location_size(); j++) {
const Package_Location& location = package4.location(j);
dbgprintf("chunk:%s, port:%i\n",location.distantchunkname().c_str(), location.port());
}
//END TEST
int res = zhtcppClient->insert(serialized);
return res;
}
示例10: submittaskszht
void submittaskszht(ZHTClient &clientRet, string client_id)
{
int ret = 0;
Package package;
package.set_virtualpath(client_id);
package.set_operation(21);
cout << "Ok, what is the hell!\n" << endl;
for (map<uint32_t, NodeList>::iterator map_it = update_map_zht.begin(); map_it != update_map_zht.end(); ++map_it)
{
uint32_t index = map_it->first;
NodeList &update_list = map_it->second;
while (!update_list.empty())
{
int num_vector_count, per_vector_count;
string alltasks;
vector< vector<string> > tokenize_string = tokenize_1(update_list.front(), '$', '#', num_vector_count, per_vector_count);
//cout << " num_vector_count = " << num_vector_count << " per_vector_count = " << per_vector_count << endl;
for(int i = 0; i < per_vector_count; i++)
{
Package tmpPackage;
tmpPackage.ParseFromString(tokenize_string.at(0).at(i));
string key = tmpPackage.virtualpath();
alltasks.append(key); alltasks.append("\'\"");
} //cout << "Serve
package.set_realfullpath(alltasks);
string str = package.SerializeAsString();
ret += clientRet.svrtosvr(str, str.length(), index);
update_list.pop_front();
}
}
// Package package; string alltasks;
// package.set_virtualpath(client_id); // Here key is just the client ID
// package.set_operation(21);
// //package.set_operation(22);
// num_packages++;
//
// int num_tasks_this_package = max_tasks_per_package;
// int num_tasks_left = num_tasks - total_submitted1;
// if (num_tasks_left < max_tasks_per_package) {
// num_tasks_this_package = num_tasks_left;
// }
// for(int j = 0; j < num_tasks_this_package; j++) {
// int task_id = it->first; ++it;
//
// stringstream task_id_ss;
// task_id_ss << task_id << client_id; // Task ID + Client ID
// alltasks.append(task_id_ss.str()); alltasks.append("\'\""); // Task ID
// }
// total_submitted1 = total_submitted1 + num_tasks_this_package;
// package.set_realfullpath(alltasks);
// string str = package.SerializeAsString();
// //cout << "String size = " << str.size() << " str length = " << strlen(str.c_str());
// int32_t ret = clientRet.svrtosvr(str, str.length(), toserver);
}
示例11: remove
int ZHTClient::remove(string str) {
Package package;
package.ParseFromString(str);
char *c_str;
int size = str.length();
c_str = (char*) malloc((size + 5 + 1) * sizeof(char));
if (c_str == NULL) {
cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl;
exit(1);
}
int len = copystring(c_str, str);
if (package.virtualpath().empty()) //empty key not allowed.
return -1;
/*if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf!
package.set_realfullpath(" ");*/
package.set_operation(2); //1 for look up, 2 for remove, 3 for insert
package.set_replicano(3); //5: original, 3 not original
str = package.SerializeAsString();
int sock = this->str2SockLRU(str, TCP);
reuseSock(sock);
// cout<<"sock = "<<sock<<endl;
struct HostEntity dest = this->str2Host(str);
sockaddr_in recvAddr;
//pthread_mutex_lock(&msg_lock);
int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len,
TCP);
//pthread_mutex_unlock(&msg_lock);
//int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP);
// cout<<"remove sentSize "<< sentSize <<endl;
int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));
// generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0);
//pthread_mutex_lock(&msg_lock);
generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP);
//pthread_mutex_unlock(&msg_lock);
// generalSendTCP(sock, str.c_str());
// int32_t* ret = (int32_t*) malloc(sizeof(int32_t));
// generalReveiveTCP(sock, (void*) ret, sizeof(int32_t), 0);
int ret_1 = *(int32_t*) ret_buf;
// cout<<"remove got: "<< ret_1 <<endl;
//cout <<"Returned status: "<< *(int32_t*) ret<<endl;
// d3_closeConnection(sock);
free(ret_buf);
return ret_1;
}
示例12: HB_insert
int HB_insert(DB &db, Package package) {
//int opt = package.operation();//opt not be used?
string package_str = package.SerializeAsString();
int ret = db.set(package.virtualpath(), package_str); //virtualpath as key
if (ret == 0) {
return -3; //insert fail.
}
/*
cout << "String insted: " << package_str << endl;
*/
return 0;
}
示例13: c_zht_remove2_std
int c_zht_remove2_std(ZHTClient_c zhtClient, const char *key) {
ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;
string sKey(key);
Package package;
package.set_virtualpath(sKey);
package.set_operation(2); //1 for look up, 2 for remove, 3 for insert, 4 append
return zhtcppClient->remove(package.SerializeAsString());
}
示例14: svrtosvr
// general server to server send (for load information and task stealing)
int32_t ZHTClient::svrtosvr(string str, int size, int index) {
Package package;
package.ParseFromString(str);
if (package.virtualpath().empty()) //empty key not allowed.
{
return -1;
}
str = package.SerializeAsString();
char *c_str;
c_str = (char*) malloc((size + 5 + 1) * sizeof(char));
if (c_str == NULL) {
cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl;
exit(1);
}
int len = copystring(c_str, str);
if (package.virtualpath().empty()) //empty key not allowed.
{
return -1;
}
//cout << "C++ string len = " << str.length() << endl; cout << "C++ string: \n" << str << endl;
/*cout << "C string: " << endl;
for(int i = 0; i < len; i++) cout << c_str[i]; cout << endl;
return 1;*/
int sock = this->index2SockLRU(index, TCP);
reuseSock(sock);
struct HostEntity dest = this->index2Host(index);
sockaddr_in recvAddr; //cout << "going to acquire lock" << endl;
//pthread_mutex_lock(&msg_lock); //cout << "lock acquired" << endl;
//int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP);
//cout << "what is the fuck!" << dest.host.data() << endl;
int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len,
TCP); //cout << "svrsvr sent" << endl;
//pthread_mutex_unlock(&msg_lock);
int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));
//pthread_mutex_lock(&msg_lock);
generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP); //cout << "svrsvr recv" << endl;
//pthread_mutex_unlock(&msg_lock); //cout << "lock released" << endl;
int32_t ret_1 = *(int32_t*) ret_buf;
free(ret_buf);
//if(package.operation() == 14) {
// cout << "ZHTClient::svrtosvr num_task = " << ret_1 << endl;
//}
return ret_1;
}
示例15: c_zht_remove2
int c_zht_remove2(const char *key) {
string keyStr(key);
if (keyStr.empty()) //empty key not allowed.
return -1;
Package package;
package.set_virtualpath(keyStr);
package.set_operation(2); //1 for look up, 2 for remove, 3 for insert
return zhtClient.remove(package.SerializeAsString());
}