|
@ -13,6 +13,7 @@ |
|
|
#include <netinet/in.h>
|
|
|
#include <netinet/in.h>
|
|
|
#include <string.h>
|
|
|
#include <string.h>
|
|
|
#include <stdio.h>
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include "commander.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main( |
|
|
int main( |
|
@ -99,88 +100,129 @@ int main( |
|
|
db.delete_table.clear(); |
|
|
db.delete_table.clear(); |
|
|
} |
|
|
} |
|
|
#endif
|
|
|
#endif
|
|
|
database run_db(database::create("/tmp/", 4096)); |
|
|
|
|
|
auto soc = socket(AF_INET, SOCK_DGRAM, 0); |
|
|
|
|
|
struct sockaddr_in addr; |
|
|
|
|
|
addr.sin_family = AF_INET; |
|
|
|
|
|
addr.sin_port = htons(20450); |
|
|
|
|
|
memset((void*)&addr.sin_addr, 0, sizeof(addr.sin_addr)); |
|
|
|
|
|
bind(soc,(struct sockaddr*)&addr,sizeof(addr)); |
|
|
|
|
|
|
|
|
|
|
|
do{ |
|
|
|
|
|
received_data recv; |
|
|
|
|
|
sending_data reply; |
|
|
|
|
|
sockaddr_in client; |
|
|
|
|
|
socklen_t packet_sz; |
|
|
|
|
|
size_t reply_size; |
|
|
|
|
|
recvfrom( |
|
|
|
|
|
soc, |
|
|
|
|
|
(void*)&recv, |
|
|
|
|
|
sizeof(received_data), |
|
|
|
|
|
MSG_WAITFORONE, |
|
|
|
|
|
(struct sockaddr*)&client, |
|
|
|
|
|
&packet_sz |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if(packet_sz < sizeof(db_op)) continue; |
|
|
|
|
|
|
|
|
|
|
|
switch (static_cast<db_op>(recv.op)) |
|
|
|
|
|
{ |
|
|
|
|
|
case db_op::version: |
|
|
|
|
|
reply.rep_id = recv.rep_id; |
|
|
|
|
|
reply.identifier.x = 1; |
|
|
|
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier.x); |
|
|
|
|
|
break; |
|
|
|
|
|
case db_op::read: |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CMD::commander cmd_args(argc, argv); |
|
|
|
|
|
|
|
|
|
|
|
std::string database_str = "/data/"; |
|
|
|
|
|
|
|
|
|
|
|
if(cmd_args.isFlagSet("-database")) |
|
|
|
|
|
{ |
|
|
|
|
|
database_str = cmd_args.getFlagValue("-database"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
size_t database_size = 4096; |
|
|
|
|
|
|
|
|
|
|
|
if(cmd_args.isFlagSet("-page-count")) |
|
|
|
|
|
{ |
|
|
|
|
|
try{ |
|
|
|
|
|
database_size = std::stoll(cmd_args.getFlagValue("-page-count")); |
|
|
|
|
|
} catch (...) { |
|
|
|
|
|
std::cerr << "Invalid page-count value" << std::endl; |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(cmd_args.isFlagSet("-create")) |
|
|
|
|
|
{ |
|
|
|
|
|
try{ |
|
|
|
|
|
database creat(database::create(database_str, database_size)); |
|
|
|
|
|
std::cout << "Created" << std::endl; |
|
|
|
|
|
return 0; |
|
|
|
|
|
} catch (...) { |
|
|
|
|
|
std::cerr << "Creating of " << database_str << " of " << database_size << " pages failed" << std::endl; |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
|
|
|
|
database run_db(database::open(database_str)); |
|
|
|
|
|
auto soc = socket(AF_INET, SOCK_DGRAM, 0); |
|
|
|
|
|
struct sockaddr_in addr; |
|
|
|
|
|
addr.sin_family = AF_INET; |
|
|
|
|
|
addr.sin_port = htons(20450); |
|
|
|
|
|
memset((void*)&addr.sin_addr, 0, sizeof(addr.sin_addr)); |
|
|
|
|
|
bind(soc,(struct sockaddr*)&addr,sizeof(addr)); |
|
|
|
|
|
|
|
|
|
|
|
do{ |
|
|
|
|
|
received_data recv; |
|
|
|
|
|
sending_data reply; |
|
|
|
|
|
sockaddr_in client; |
|
|
|
|
|
socklen_t packet_sz; |
|
|
|
|
|
size_t reply_size; |
|
|
|
|
|
recvfrom( |
|
|
|
|
|
soc, |
|
|
|
|
|
(void*)&recv, |
|
|
|
|
|
sizeof(received_data), |
|
|
|
|
|
MSG_WAITFORONE, |
|
|
|
|
|
(struct sockaddr*)&client, |
|
|
|
|
|
&packet_sz |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if(packet_sz < sizeof(db_op)) continue; |
|
|
|
|
|
|
|
|
|
|
|
switch (static_cast<db_op>(recv.op)) |
|
|
|
|
|
{ |
|
|
|
|
|
case db_op::version: |
|
|
reply.rep_id = recv.rep_id; |
|
|
reply.rep_id = recv.rep_id; |
|
|
auto req = run_db.read(recv.identifier); |
|
|
|
|
|
if(req.first != 0) |
|
|
|
|
|
|
|
|
reply.identifier.x = 1; |
|
|
|
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier.x); |
|
|
|
|
|
break; |
|
|
|
|
|
case db_op::read: |
|
|
{ |
|
|
{ |
|
|
reply.identifier = recv.identifier; |
|
|
|
|
|
reply.page = req.second; |
|
|
|
|
|
reply_size = sizeof(reply); |
|
|
|
|
|
|
|
|
reply.rep_id = recv.rep_id; |
|
|
|
|
|
auto req = run_db.read(recv.identifier); |
|
|
|
|
|
if(req.first != 0) |
|
|
|
|
|
{ |
|
|
|
|
|
reply.identifier = recv.identifier; |
|
|
|
|
|
reply.page = req.second; |
|
|
|
|
|
reply_size = sizeof(reply); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case db_op::write: |
|
|
{ |
|
|
{ |
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); |
|
|
|
|
|
|
|
|
reply.rep_id = recv.rep_id; |
|
|
|
|
|
try{ |
|
|
|
|
|
run_db.write(recv.identifier, recv.page); |
|
|
|
|
|
reply.identifier = recv.identifier; |
|
|
|
|
|
} catch (...) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
case db_op::write: |
|
|
|
|
|
{ |
|
|
|
|
|
reply.rep_id = recv.rep_id; |
|
|
|
|
|
try{ |
|
|
|
|
|
run_db.write(recv.identifier, recv.page); |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case db_op::remove: |
|
|
|
|
|
{ |
|
|
|
|
|
reply.rep_id = recv.rep_id; |
|
|
|
|
|
run_db.remove(recv.identifier); |
|
|
reply.identifier = recv.identifier; |
|
|
reply.identifier = recv.identifier; |
|
|
} catch (...) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); |
|
|
} |
|
|
} |
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
case db_op::remove: |
|
|
|
|
|
{ |
|
|
|
|
|
reply.rep_id = recv.rep_id; |
|
|
|
|
|
run_db.remove(recv.identifier); |
|
|
|
|
|
reply.identifier = recv.identifier; |
|
|
|
|
|
reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
std::cerr << "bad_request " << (uint32_t)static_cast<db_op>(recv.op) << std::endl; |
|
|
|
|
|
continue; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
sendto( |
|
|
|
|
|
soc, |
|
|
|
|
|
(void*)&reply, |
|
|
|
|
|
reply_size, |
|
|
|
|
|
0, |
|
|
|
|
|
(struct sockaddr*)&client, |
|
|
|
|
|
(socklen_t)sizeof(client) |
|
|
|
|
|
); |
|
|
|
|
|
//std::cerr << "reply to " << (uint32_t)static_cast<db_op>(recv.op) << std::endl;
|
|
|
|
|
|
}while(true); |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
std::cerr << "bad_request " << (uint32_t)static_cast<db_op>(recv.op) << std::endl; |
|
|
|
|
|
continue; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
sendto( |
|
|
|
|
|
soc, |
|
|
|
|
|
(void*)&reply, |
|
|
|
|
|
reply_size, |
|
|
|
|
|
0, |
|
|
|
|
|
(struct sockaddr*)&client, |
|
|
|
|
|
(socklen_t)sizeof(client) |
|
|
|
|
|
); |
|
|
|
|
|
//std::cerr << "reply to " << (uint32_t)static_cast<db_op>(recv.op) << std::endl;
|
|
|
|
|
|
}while(true); |
|
|
|
|
|
} catch (...) { |
|
|
|
|
|
std::cerr << "GoJDB crashed !" << std::endl; |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |