WIP: list_files end-to-end!

This commit is contained in:
Scott Lystig Fritchie 2015-06-23 17:08:15 +09:00
parent 6722b3c0f1
commit 73f71c406e
3 changed files with 59 additions and 2 deletions

View file

@ -77,6 +77,12 @@ message Mpb_EpochId {
required bytes epoch_csum = 2;
}
// file_info() type
message Mpb_FileInfo {
required uint64 file_size = 1;
required string file_name = 2;
}
//////////////////////////////////////////
//
// requests & responses
@ -161,7 +167,7 @@ message Mpb_ReadChunkResp {
optional Mpb_ChunkCSum csum = 3;
}
// read_chunk() request & response
// checksum_list() request & response
message Mpb_ChecksumListReq {
required string file = 1;
@ -172,6 +178,17 @@ message Mpb_ChecksumListResp {
optional bytes chunk = 2;
}
// list_files() request & response
message Mpb_ListFilesReq {
}
message Mpb_ListFilesResp {
required Mpb_GeneralStatusCode status = 1;
// Wow, I cannot use optional & repeated together?
repeated Mpb_FileInfo files = 2;
}
//////////////////////////////////////////
//
// request & response wrapper
@ -195,6 +212,7 @@ message Mpb_Request {
optional Mpb_WriteChunkReq write_chunk = 13;
optional Mpb_ReadChunkReq read_chunk = 14;
optional Mpb_ChecksumListReq checksum_list = 15;
optional Mpb_ListFilesReq list_files = 16;
}
message Mpb_Response {
@ -217,4 +235,5 @@ message Mpb_Response {
optional Mpb_WriteChunkResp write_chunk = 13;
optional Mpb_ReadChunkResp read_chunk = 14;
optional Mpb_ChecksumListResp checksum_list = 15;
optional Mpb_ListFilesResp list_files = 16;
}

View file

@ -41,7 +41,8 @@
append_chunk/6, append_chunk/7,
write_chunk/5, write_chunk/6,
read_chunk/4, read_chunk/5,
checksum_list/2, checksum_list/3
checksum_list/2, checksum_list/3,
list_files/1, list_files/2
]).
%% gen_server callbacks
@ -103,6 +104,12 @@ checksum_list(PidSpec, File) ->
checksum_list(PidSpec, File, Timeout) ->
send_sync(PidSpec, {checksum_list, File}, Timeout).
list_files(PidSpec) ->
list_files(PidSpec, ?DEFAULT_TIMEOUT).
list_files(PidSpec, Timeout) ->
send_sync(PidSpec, {list_files}, Timeout).
send_sync(PidSpec, Cmd, Timeout) ->
gen_server:call(PidSpec, {send_sync, Cmd}, Timeout).
@ -310,6 +317,28 @@ do_send_sync({checksum_list, File},
catch X:Y ->
Res = {bummer, {X, Y, erlang:get_stacktrace()}},
{Res, S#state{count=Count+1}}
end;
do_send_sync({list_files},
#state{sock=Sock, sock_id=Index, count=Count}=S) ->
try
ReqID = <<Index:64/big, Count:64/big>>,
Req = #mpb_listfilesreq{},
R1a = #mpb_request{req_id=ReqID,
list_files=Req},
Bin1a = machi_pb:encode_mpb_request(R1a),
ok = gen_tcp:send(Sock, Bin1a),
{ok, Bin1B} = gen_tcp:recv(Sock, 0),
case (catch machi_pb:decode_mpb_response(Bin1B)) of
#mpb_response{req_id=ReqID, list_files=R} when R /= undefined ->
Result = convert_list_files_resp(R),
{Result, S#state{count=Count+1}};
#mpb_response{req_id=ReqID, generic=G} when G /= undefined ->
#mpb_errorresp{code=Code, msg=Msg, extra=Extra} = G,
{{error, {Code, Msg, Extra}}, S#state{count=Count+1}}
end
catch X:Y ->
Res = {bummer, {X, Y, erlang:get_stacktrace()}},
{Res, S#state{count=Count+1}}
end.
convert_csum_req(none) ->
@ -350,3 +379,10 @@ convert_checksum_list_resp(#mpb_checksumlistresp{status='OK', chunk=Chunk}) ->
{ok, Chunk};
convert_checksum_list_resp(#mpb_checksumlistresp{status=Status}) ->
convert_general_status_code(Status).
convert_list_files_resp(#mpb_listfilesresp{status='OK', files=Files}) ->
FileInfo = [{Size, File} || #mpb_fileinfo{file_size=Size,
file_name=File} <- Files],
{ok, FileInfo};
convert_list_files_resp(#mpb_listfilesresp{status=Status}) ->
convert_general_status_code(Status).

View file

@ -82,6 +82,8 @@ smoke_test2() ->
{Ch, Fl, Off, Sz} <- Reads],
{ok, _} = ?C:checksum_list(Clnt, File1),
{ok, [{File1Size,File1}]} = ?C:list_files(Clnt),
true = is_integer(File1Size),
ok
after