LogCabin
|
00001 /* Copyright (c) 2012 Stanford University 00002 * 00003 * Permission to use, copy, modify, and distribute this software for any 00004 * purpose with or without fee is hereby granted, provided that the above 00005 * copyright notice and this permission notice appear in all copies. 00006 * 00007 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES 00008 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 00009 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR 00010 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00011 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00012 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00013 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00014 */ 00015 00016 #include "Core/Debug.h" 00017 #include "Core/ProtoBuf.h" 00018 #include "Tree/ProtoBuf.h" 00019 00020 namespace LogCabin { 00021 namespace Tree { 00022 namespace ProtoBuf { 00023 00024 namespace PC = LogCabin::Protocol::Client; 00025 00026 void 00027 readOnlyTreeRPC(const Tree& tree, 00028 const PC::ReadOnlyTree::Request& request, 00029 PC::ReadOnlyTree::Response& response) 00030 { 00031 Result result; 00032 if (request.has_condition()) { 00033 result = tree.checkCondition(request.condition().path(), 00034 request.condition().contents()); 00035 } 00036 if (result.status != Status::OK) { 00037 // condition does not match, skip 00038 } else if (request.has_list_directory()) { 00039 std::vector<std::string> children; 00040 result = tree.listDirectory(request.list_directory().path(), 00041 children); 00042 for (auto it = children.begin(); it != children.end(); ++it) 00043 response.mutable_list_directory()->add_child(*it); 00044 } else if (request.has_read()) { 00045 std::string contents; 00046 result = tree.read(request.read().path(), contents); 00047 response.mutable_read()->set_contents(contents); 00048 } else { 00049 PANIC("Unexpected request: %s", 00050 Core::ProtoBuf::dumpString(request).c_str()); 00051 } 00052 response.set_status(static_cast<PC::Status>(result.status)); 00053 if (result.status != Status::OK) 00054 response.set_error(result.error); 00055 } 00056 00057 void 00058 readWriteTreeRPC(Tree& tree, 00059 const PC::ReadWriteTree::Request& request, 00060 PC::ReadWriteTree::Response& response) 00061 { 00062 Result result; 00063 if (request.has_condition()) { 00064 result = tree.checkCondition(request.condition().path(), 00065 request.condition().contents()); 00066 } 00067 if (result.status != Status::OK) { 00068 // condition does not match, skip 00069 } else if (request.has_make_directory()) { 00070 result = tree.makeDirectory(request.make_directory().path()); 00071 } else if (request.has_remove_directory()) { 00072 result = tree.removeDirectory(request.remove_directory().path()); 00073 } else if (request.has_write()) { 00074 result = tree.write(request.write().path(), 00075 request.write().contents()); 00076 } else if (request.has_remove_file()) { 00077 result = tree.removeFile(request.remove_file().path()); 00078 } else { 00079 PANIC("Unexpected request: %s", 00080 Core::ProtoBuf::dumpString(request).c_str()); 00081 } 00082 response.set_status(static_cast<PC::Status>(result.status)); 00083 if (result.status != Status::OK) 00084 response.set_error(result.error); 00085 } 00086 00087 } // namespace LogCabin::Tree::ProtoBuf 00088 } // namespace LogCabin::Tree 00089 } // namespace LogCabin