bthreads_service.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2015 Baidu, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Authors: Ge,Jun (gejun@baidu.com)
  15. #include <ostream>
  16. #include "brpc/closure_guard.h" // ClosureGuard
  17. #include "brpc/controller.h" // Controller
  18. #include "brpc/builtin/common.h"
  19. #include "brpc/builtin/bthreads_service.h"
  20. namespace bthread {
  21. void print_task(std::ostream& os, bthread_t tid);
  22. }
  23. namespace brpc {
  24. void BthreadsService::default_method(::google::protobuf::RpcController* cntl_base,
  25. const ::brpc::BthreadsRequest*,
  26. ::brpc::BthreadsResponse*,
  27. ::google::protobuf::Closure* done) {
  28. ClosureGuard done_guard(done);
  29. Controller *cntl = static_cast<Controller*>(cntl_base);
  30. cntl->http_response().set_content_type("text/plain");
  31. butil::IOBufBuilder os;
  32. const std::string& constraint = cntl->http_request().unresolved_path();
  33. if (constraint.empty()) {
  34. os << "Use /bthreads/<bthread_id>";
  35. } else {
  36. char* endptr = NULL;
  37. bthread_t tid = strtoull(constraint.c_str(), &endptr, 10);
  38. if (*endptr == '\0' || *endptr == '/') {
  39. ::bthread::print_task(os, tid);
  40. } else {
  41. cntl->http_response().set_status_code(HTTP_STATUS_NOT_FOUND);
  42. cntl->SetFailed(ENOMETHOD, "path=%s is not a bthread id",
  43. constraint.c_str());
  44. }
  45. }
  46. os.move_to(cntl->response_attachment());
  47. }
  48. } // namespace brpc