dtcutils.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DTC_UTILS__
  17. #define __DTC_UTILS__
  18. #include <vector>
  19. #include <string>
  20. #include <sys/socket.h>
  21. #include <iostream>
  22. #include <netinet/in.h>
  23. #include <fcntl.h>
  24. #include <arpa/inet.h>
  25. /*此文件放置dtc的工具函数*/
  26. namespace dtc
  27. {
  28. namespace utils
  29. {
  30. /*************************************************
  31. 获取本机的ip tomchen
  32. **************************************************/
  33. inline std::string get_local_ip()
  34. {
  35. int iClientSockfd = socket(AF_INET, SOCK_DGRAM, 0);
  36. if (iClientSockfd < 0)
  37. return "0.0.0.0";
  38. struct sockaddr_in stINETAddr;
  39. stINETAddr.sin_addr.s_addr = inet_addr("192.168.0.1");
  40. stINETAddr.sin_family = AF_INET;
  41. stINETAddr.sin_port = htons(8888);
  42. int iCurrentFlag = fcntl(iClientSockfd, F_GETFL, 0);
  43. fcntl(iClientSockfd, F_SETFL, iCurrentFlag | FNDELAY);
  44. if (connect(iClientSockfd, (struct sockaddr *)&stINETAddr,
  45. sizeof(sockaddr)) != 0) {
  46. close(iClientSockfd);
  47. return "0.0.0.0";
  48. }
  49. struct sockaddr_in stINETAddrLocal;
  50. socklen_t iAddrLenLocal = sizeof(stINETAddrLocal);
  51. getsockname(iClientSockfd, (struct sockaddr *)&stINETAddrLocal,
  52. &iAddrLenLocal);
  53. close(iClientSockfd);
  54. return inet_ntoa(stINETAddrLocal.sin_addr);
  55. }
  56. /*************************************************
  57. 切割字符串strOri, 以_Ch为分隔符,结果为theVec
  58. **************************************************/
  59. inline void split_str(std::string strOri, char _Ch,
  60. std::vector<std::string> &theVec)
  61. {
  62. std::string::size_type nLastPos = 0;
  63. std::string strSub;
  64. std::string::size_type iIndex = strOri.find(_Ch);
  65. if (std::string::npos == iIndex) {
  66. theVec.push_back(strOri);
  67. return;
  68. }
  69. while (std::string::npos != iIndex) {
  70. strSub = strOri.substr(nLastPos, iIndex - nLastPos);
  71. nLastPos = iIndex + 1;
  72. iIndex = strOri.find(_Ch, nLastPos);
  73. theVec.push_back(strSub);
  74. }
  75. if (nLastPos != 0) {
  76. strSub = strOri.substr(nLastPos, strOri.length() - nLastPos);
  77. theVec.push_back(strSub);
  78. }
  79. }
  80. inline int get_bid()
  81. {
  82. char buf[1024];
  83. getcwd(buf, sizeof(buf));
  84. std::vector<std::string> pathVec;
  85. dtc::utils::split_str(std::string(buf), '/', pathVec);
  86. if (pathVec.size() < 5) {
  87. return 0;
  88. }
  89. std::string strAccessKey = pathVec[4];
  90. std::string strModuleId = strAccessKey.substr(0, 8);
  91. return atoi(strModuleId.c_str());
  92. }
  93. } // namespace utils
  94. } // namespace dtc
  95. #endif