From 3a15f8b9fdaac0c940c4c5fa34765716b8f0d4fc Mon Sep 17 00:00:00 2001 From: derekyuan Date: Tue, 11 Sep 2018 14:43:04 +0800 Subject: [PATCH] add Tars files --- .gitmodules | 20 +- Install.md | 215 +- Install.zh.md | 217 +- README.md | 16 +- README.zh.md | 13 + build/install.sh | 89 +- cpp | 1 + cpp/README.md | 34 - cpp/build/CMakeLists.txt | 34 - cpp/build/README.md | 14 - cpp/build/build.sh | 30 - .../CoroutineDemo/AServer/AServant.tars | 14 - .../CoroutineDemo/AServer/AServantImp.cpp | 56 - .../CoroutineDemo/AServer/AServantImp.h | 58 - .../CoroutineDemo/AServer/AServer.cpp | 58 - cpp/examples/CoroutineDemo/AServer/AServer.h | 51 - cpp/examples/CoroutineDemo/AServer/makefile | 12 - .../CoroutineDemo/BServer/AServant.tars | 14 - .../CoroutineDemo/BServer/BServant.tars | 15 - .../CoroutineDemo/BServer/BServantImp.cpp | 140 - .../CoroutineDemo/BServer/BServantImp.h | 62 - .../CoroutineDemo/BServer/BServer.cpp | 55 - cpp/examples/CoroutineDemo/BServer/BServer.h | 51 - cpp/examples/CoroutineDemo/BServer/makefile | 12 - cpp/examples/CoroutineDemo/README.md | 7 - .../CoroutineDemo/client/BServant.tars | 15 - cpp/examples/CoroutineDemo/client/main.cpp | 144 - cpp/examples/CoroutineDemo/client/makefile | 12 - .../CoroutineDemo/testCoro/BServant.tars | 15 - cpp/examples/CoroutineDemo/testCoro/main.cpp | 107 - cpp/examples/CoroutineDemo/testCoro/makefile | 12 - .../testParallelCoro/BServant.tars | 15 - .../CoroutineDemo/testParallelCoro/main.cpp | 276 -- .../CoroutineDemo/testParallelCoro/makefile | 12 - cpp/examples/HttpDemo/HttpClient/main.cpp | 175 - cpp/examples/HttpDemo/HttpClient/makefile | 16 - cpp/examples/HttpDemo/HttpServer/HttpImp.cpp | 49 - cpp/examples/HttpDemo/HttpServer/HttpImp.h | 51 - .../HttpDemo/HttpServer/HttpServer.cpp | 98 - cpp/examples/HttpDemo/HttpServer/HttpServer.h | 50 - cpp/examples/HttpDemo/HttpServer/makefile | 14 - .../PromiseDemo/AServer/AServant.tars | 27 - .../PromiseDemo/AServer/AServantImp.cpp | 252 -- .../PromiseDemo/AServer/AServantImp.h | 63 - cpp/examples/PromiseDemo/AServer/AServer.cpp | 57 - cpp/examples/PromiseDemo/AServer/AServer.h | 52 - .../PromiseDemo/AServer/BServant.tars | 25 - .../PromiseDemo/AServer/CServant.tars | 25 - cpp/examples/PromiseDemo/AServer/makefile | 12 - .../PromiseDemo/BServer/BServant.tars | 25 - .../PromiseDemo/BServer/BServantImp.cpp | 45 - .../PromiseDemo/BServer/BServantImp.h | 40 - cpp/examples/PromiseDemo/BServer/BServer.cpp | 57 - cpp/examples/PromiseDemo/BServer/BServer.h | 52 - cpp/examples/PromiseDemo/BServer/makefile | 11 - .../PromiseDemo/CServer/CServant.tars | 25 - .../PromiseDemo/CServer/CServantImp.cpp | 45 - .../PromiseDemo/CServer/CServantImp.h | 40 - cpp/examples/PromiseDemo/CServer/CServer.cpp | 57 - cpp/examples/PromiseDemo/CServer/CServer.h | 53 - cpp/examples/PromiseDemo/CServer/makefile | 11 - cpp/examples/PromiseDemo/Client/main.cpp | 138 - cpp/examples/PromiseDemo/Client/makefile | 13 - cpp/examples/PromiseDemo/README.md | 6 - cpp/examples/PushDemo/PushClient/Makefile | 13 - .../PushDemo/PushClient/TestRecvThread.cpp | 162 - .../PushDemo/PushClient/TestRecvThread.h | 28 - cpp/examples/PushDemo/PushClient/main.cpp | 30 - .../PushServer/TestPushServantImp.cpp | 55 - .../PushDemo/PushServer/TestPushServantImp.h | 43 - .../PushDemo/PushServer/TestPushServer.cpp | 83 - .../PushDemo/PushServer/TestPushServer.h | 41 - .../PushDemo/PushServer/TestPushThread.cpp | 67 - .../PushDemo/PushServer/TestPushThread.h | 31 - cpp/examples/PushDemo/PushServer/makefile | 17 - cpp/examples/PushDemo/README.md | 7 - .../HelloServer/AsyncClient/main.cpp | 80 - .../HelloServer/AsyncClient/makefile | 16 - .../HelloServer/Client/main.cpp | 62 - .../HelloServer/Client/makefile | 16 - .../QuickStartDemo/HelloServer/Hello.tars | 26 - .../QuickStartDemo/HelloServer/HelloImp.cpp | 42 - .../QuickStartDemo/HelloServer/HelloImp.h | 53 - .../HelloServer/HelloServer.cpp | 59 - .../QuickStartDemo/HelloServer/HelloServer.h | 50 - .../QuickStartDemo/HelloServer/makefile | 16 - .../ProxyServer/Client/main.cpp | 63 - .../ProxyServer/Client/makefile | 17 - .../QuickStartDemo/ProxyServer/Proxy.tars | 26 - .../QuickStartDemo/ProxyServer/ProxyImp.cpp | 83 - .../QuickStartDemo/ProxyServer/ProxyImp.h | 63 - .../ProxyServer/ProxyServer.cpp | 57 - .../QuickStartDemo/ProxyServer/ProxyServer.h | 52 - .../QuickStartDemo/ProxyServer/makefile | 17 - cpp/examples/QuickStartDemo/README.md | 7 - cpp/examples/README.md | 11 - cpp/examples/StressDemo/README.md | 7 - .../StressDemo/TarsStressClient/main.cpp | 134 - .../StressDemo/TarsStressClient/makefile | 16 - .../StressDemo/TarsStressClient/teststress.sh | 5 - .../StressDemo/TarsStressServer/Stress.tars | 26 - .../StressDemo/TarsStressServer/StressImp.cpp | 58 - .../StressDemo/TarsStressServer/StressImp.h | 57 - .../TarsStressServer/TarsStressServer.cpp | 57 - .../TarsStressServer/TarsStressServer.h | 53 - .../StressDemo/TarsStressServer/makefile | 16 - .../AdminRegistryServer/AdminReapThread.cpp | 112 - .../AdminRegistryServer/AdminReapThread.h | 74 - .../AdminRegistryServer/AdminRegistryImp.cpp | 908 ---- .../AdminRegistryServer/AdminRegistryImp.h | 453 -- .../AdminRegistryServer.cpp | 106 - .../AdminRegistryServer/AdminRegistryServer.h | 57 - .../AdminRegistryServer/CMakeLists.txt | 6 - cpp/framework/AdminRegistryServer/DbProxy.cpp | 953 ---- cpp/framework/AdminRegistryServer/DbProxy.h | 289 -- .../AdminRegistryServer/ExecuteTask.cpp | 626 --- .../AdminRegistryServer/ExecuteTask.h | 148 - cpp/framework/AdminRegistryServer/util.h | 26 - cpp/framework/AuthServer/AuthImp.cpp | 164 - cpp/framework/AuthServer/AuthImp.h | 43 - cpp/framework/AuthServer/AuthServer.cpp | 14 - cpp/framework/AuthServer/AuthServer.h | 23 - cpp/framework/AuthServer/CMakeLists.txt | 7 - cpp/framework/AuthServer/main.cpp | 26 - cpp/framework/CMakeLists.txt | 176 - cpp/framework/ConfigServer/CMakeLists.txt | 8 - cpp/framework/ConfigServer/ConfigImp.cpp | 970 ---- cpp/framework/ConfigServer/ConfigImp.h | 265 -- cpp/framework/ConfigServer/ConfigServer.cpp | 32 - cpp/framework/ConfigServer/ConfigServer.h | 39 - cpp/framework/ConfigServer/main.cpp | 42 - cpp/framework/LogServer/CMakeLists.txt | 7 - cpp/framework/LogServer/LogImp.cpp | 433 -- cpp/framework/LogServer/LogImp.h | 222 - cpp/framework/LogServer/LogServer.cpp | 116 - cpp/framework/LogServer/LogServer.h | 43 - cpp/framework/LogServer/main.cpp | 39 - cpp/framework/NodeServer/Activator.cpp | 465 -- cpp/framework/NodeServer/Activator.h | 209 - cpp/framework/NodeServer/BatchPatchThread.cpp | 222 - cpp/framework/NodeServer/BatchPatchThread.h | 114 - cpp/framework/NodeServer/CMakeLists.txt | 14 - cpp/framework/NodeServer/CommandAddFile.h | 194 - cpp/framework/NodeServer/CommandDestroy.h | 126 - cpp/framework/NodeServer/CommandLoad.h | 516 --- cpp/framework/NodeServer/CommandNotify.h | 100 - cpp/framework/NodeServer/CommandPatch.h | 665 --- cpp/framework/NodeServer/CommandStart.h | 480 -- cpp/framework/NodeServer/CommandStop.h | 269 -- cpp/framework/NodeServer/KeepAliveThread.cpp | 352 -- cpp/framework/NodeServer/KeepAliveThread.h | 105 - cpp/framework/NodeServer/NodeImp.cpp | 771 ---- cpp/framework/NodeServer/NodeImp.h | 208 - cpp/framework/NodeServer/NodeRollLogger.cpp | 107 - cpp/framework/NodeServer/NodeRollLogger.h | 120 - cpp/framework/NodeServer/NodeServer.cpp | 366 -- cpp/framework/NodeServer/NodeServer.h | 103 - cpp/framework/NodeServer/PlatformInfo.cpp | 116 - cpp/framework/NodeServer/PlatformInfo.h | 61 - cpp/framework/NodeServer/PropertyReporter.h | 63 - cpp/framework/NodeServer/RegistryProxy.h | 73 - cpp/framework/NodeServer/RemoveLogThread.cpp | 172 - cpp/framework/NodeServer/RemoveLogThread.h | 92 - cpp/framework/NodeServer/ReportMemThread.cpp | 108 - cpp/framework/NodeServer/ReportMemThread.h | 63 - cpp/framework/NodeServer/ServerCommand.h | 109 - cpp/framework/NodeServer/ServerFactory.cpp | 480 -- cpp/framework/NodeServer/ServerFactory.h | 201 - cpp/framework/NodeServer/ServerImp.cpp | 79 - cpp/framework/NodeServer/ServerImp.h | 77 - .../NodeServer/ServerLimitResource.cpp | 111 - .../NodeServer/ServerLimitResource.h | 77 - cpp/framework/NodeServer/ServerObject.cpp | 811 ---- cpp/framework/NodeServer/ServerObject.h | 526 --- .../NodeServer/SingleFileDownloader.cpp | 128 - .../NodeServer/SingleFileDownloader.h | 93 - cpp/framework/NodeServer/main.cpp | 253 -- cpp/framework/NodeServer/util.h | 27 - cpp/framework/NotifyServer/CMakeLists.txt | 6 - cpp/framework/NotifyServer/LoadDbThread.cpp | 129 - cpp/framework/NotifyServer/LoadDbThread.h | 152 - cpp/framework/NotifyServer/NotifyImp.cpp | 350 -- cpp/framework/NotifyServer/NotifyImp.h | 106 - cpp/framework/NotifyServer/NotifyServer.cpp | 55 - cpp/framework/NotifyServer/NotifyServer.h | 52 - cpp/framework/NotifyServer/main.cpp | 42 - cpp/framework/PatchServer/CMakeLists.txt | 8 - cpp/framework/PatchServer/PatchCache.cpp | 279 -- cpp/framework/PatchServer/PatchCache.h | 84 - cpp/framework/PatchServer/PatchImp.cpp | 283 -- cpp/framework/PatchServer/PatchImp.h | 93 - cpp/framework/PatchServer/PatchServer.cpp | 47 - cpp/framework/PatchServer/PatchServer.h | 49 - cpp/framework/PatchServer/main.cpp | 51 - cpp/framework/PropertyServer/CMakeLists.txt | 6 - .../PropertyServer/PropertyDbManager.cpp | 655 --- .../PropertyServer/PropertyDbManager.h | 136 - .../PropertyServer/PropertyHashMap.h | 213 - cpp/framework/PropertyServer/PropertyImp.cpp | 204 - cpp/framework/PropertyServer/PropertyImp.h | 111 - .../PropertyServer/PropertyReapThread.cpp | 454 -- .../PropertyServer/PropertyReapThread.h | 144 - .../PropertyServer/PropertyServer.cpp | 307 -- cpp/framework/PropertyServer/PropertyServer.h | 101 - cpp/framework/PropertyServer/main.cpp | 49 - .../QueryPropertyServer/CMakeLists.txt | 9 - cpp/framework/QueryPropertyServer/DbProxy.cpp | 902 ---- cpp/framework/QueryPropertyServer/DbProxy.h | 58 - .../QueryPropertyServer/DbThread.cpp | 161 - cpp/framework/QueryPropertyServer/DbThread.h | 73 - .../QueryPropertyServer/QueryDbThread.cpp | 158 - .../QueryPropertyServer/QueryDbThread.h | 78 - .../QueryPropertyServer/QueryImp.cpp | 143 - cpp/framework/QueryPropertyServer/QueryImp.h | 63 - cpp/framework/QueryPropertyServer/QueryItem.h | 43 - .../QueryPropertyServer/QueryServer.cpp | 242 - .../QueryPropertyServer/QueryServer.h | 124 - .../QueryPropertyServer/RequestDecoder.cpp | 502 --- .../QueryPropertyServer/RequestDecoder.h | 100 - cpp/framework/QueryStatServer/CMakeLists.txt | 9 - cpp/framework/QueryStatServer/DbProxy.cpp | 818 ---- cpp/framework/QueryStatServer/DbProxy.h | 58 - cpp/framework/QueryStatServer/DbThread.cpp | 163 - cpp/framework/QueryStatServer/DbThread.h | 72 - .../QueryStatServer/QueryDbThread.cpp | 158 - cpp/framework/QueryStatServer/QueryDbThread.h | 78 - cpp/framework/QueryStatServer/QueryImp.cpp | 143 - cpp/framework/QueryStatServer/QueryImp.h | 63 - cpp/framework/QueryStatServer/QueryItem.h | 43 - cpp/framework/QueryStatServer/QueryServer.cpp | 242 - cpp/framework/QueryStatServer/QueryServer.h | 124 - .../QueryStatServer/RequestDecoder.cpp | 501 --- .../QueryStatServer/RequestDecoder.h | 100 - cpp/framework/README.md | 18 - cpp/framework/RegistryServer/CMakeLists.txt | 7 - .../RegistryServer/CheckNodeThread.cpp | 96 - .../RegistryServer/CheckNodeThread.h | 82 - .../RegistryServer/CheckSettingState.cpp | 97 - .../RegistryServer/CheckSettingState.h | 83 - cpp/framework/RegistryServer/DbHandle.cpp | 1800 -------- cpp/framework/RegistryServer/DbHandle.h | 532 --- cpp/framework/RegistryServer/QueryImp.cpp | 208 - cpp/framework/RegistryServer/QueryImp.h | 107 - cpp/framework/RegistryServer/ReapThread.cpp | 153 - cpp/framework/RegistryServer/ReapThread.h | 103 - cpp/framework/RegistryServer/RegistryImp.cpp | 122 - cpp/framework/RegistryServer/RegistryImp.h | 163 - .../RegistryServer/RegistryProcThread.cpp | 139 - .../RegistryServer/RegistryProcThread.h | 157 - .../RegistryServer/RegistryServer.cpp | 127 - cpp/framework/RegistryServer/RegistryServer.h | 84 - cpp/framework/RegistryServer/main.cpp | 42 - cpp/framework/StatServer/CMakeLists.txt | 6 - cpp/framework/StatServer/ReapSSDThread.cpp | 451 -- cpp/framework/StatServer/ReapSSDThread.h | 141 - cpp/framework/StatServer/StatDbManager.cpp | 671 --- cpp/framework/StatServer/StatDbManager.h | 155 - cpp/framework/StatServer/StatHashMap.h | 109 - cpp/framework/StatServer/StatImp.cpp | 252 -- cpp/framework/StatServer/StatImp.h | 125 - cpp/framework/StatServer/StatServer.cpp | 325 -- cpp/framework/StatServer/StatServer.h | 101 - cpp/framework/StatServer/main.cpp | 41 - cpp/framework/conf/tars.default | 33 - cpp/framework/conf/tars.tarsconfig | 39 - cpp/framework/conf/tars.tarsjava.default | 44 - cpp/framework/conf/tars.tarslog | 40 - cpp/framework/conf/tars.tarsnotify | 49 - cpp/framework/conf/tars.tarspatch | 34 - cpp/framework/conf/tars.tarsproperty | 76 - cpp/framework/conf/tars.tarsstat | 61 - .../tarsAdminRegistry/conf/adminregistry.conf | 62 - .../deploy/tarsAdminRegistry/util/execute.sh | 22 - .../deploy/tarsAdminRegistry/util/start.sh | 2 - .../deploy/tarsAdminRegistry/util/stop.sh | 13 - cpp/framework/deploy/tars_install.sh | 29 - .../deploy/tarsconfig/conf/tarsconfig.conf | 55 - .../deploy/tarsconfig/util/execute.sh | 14 - cpp/framework/deploy/tarsconfig/util/start.sh | 2 - cpp/framework/deploy/tarsconfig/util/stop.sh | 13 - .../deploy/tarsnode/conf/tarsnode.conf | 66 - cpp/framework/deploy/tarsnode/util/execute.sh | 17 - cpp/framework/deploy/tarsnode/util/monitor.sh | 14 - cpp/framework/deploy/tarsnode/util/start.sh | 3 - cpp/framework/deploy/tarsnode/util/stop.sh | 14 - cpp/framework/deploy/tarsnode_install.sh | 21 - .../deploy/tarspatch/conf/rsync.conf | 10 - .../deploy/tarspatch/conf/tarspatch.conf | 48 - .../deploy/tarspatch/util/execute.sh | 14 - cpp/framework/deploy/tarspatch/util/init.sh | 33 - cpp/framework/deploy/tarspatch/util/start.sh | 2 - cpp/framework/deploy/tarspatch/util/stop.sh | 13 - .../tarsregistry/conf/tarsregistry.conf | 103 - cpp/framework/deploy/tarsregistry/util/env.sh | 2 - .../deploy/tarsregistry/util/execute.sh | 21 - .../deploy/tarsregistry/util/start.sh | 2 - .../deploy/tarsregistry/util/stop.sh | 13 - cpp/framework/patchclient/CMakeLists.txt | 6 - cpp/framework/patchclient/patchclient.cpp | 150 - cpp/framework/patchclient/tars_patch.cpp | 291 -- cpp/framework/patchclient/tars_patch.h | 194 - cpp/framework/protocol/AdminReg.tars | 493 -- cpp/framework/protocol/CMakeLists.txt | 5 - cpp/framework/protocol/Node.tars | 218 - cpp/framework/protocol/NodeDescriptor.tars | 147 - cpp/framework/protocol/Patch.tars | 75 - cpp/framework/protocol/Registry.tars | 137 - .../protocol/RegistryDescriptor.tars | 78 - cpp/framework/sql/db_tars.sql | 641 --- cpp/framework/sql/dump-sql.sh | 2 - cpp/framework/sql/exec-sql.sh | 6 - cpp/servant/CMakeLists.txt | 66 - cpp/servant/README.md | 11 - cpp/servant/jmem/jmem_hashmap.h | 2339 ---------- cpp/servant/jmem/jmem_hashmap_compact.h | 2421 ---------- cpp/servant/jmem/jmem_multi_hashmap.h | 2736 ------------ cpp/servant/jmem/jmem_policy.h | 201 - cpp/servant/jmem/jmem_queue.h | 172 - cpp/servant/jmem/jmem_rbtree.h | 2062 --------- cpp/servant/libservant/AdapterProxy.cpp | 815 ---- cpp/servant/libservant/AdminServant.cpp | 54 - cpp/servant/libservant/AppCache.cpp | 151 - cpp/servant/libservant/AppProtocol.cpp | 216 - cpp/servant/libservant/Application.cpp | 1221 ----- cpp/servant/libservant/AsyncProcThread.cpp | 113 - cpp/servant/libservant/BaseNotify.cpp | 64 - cpp/servant/libservant/CMakeLists.txt | 20 - cpp/servant/libservant/Communicator.cpp | 366 -- cpp/servant/libservant/CommunicatorEpoll.cpp | 500 --- cpp/servant/libservant/CoroutineScheduler.cpp | 910 ---- cpp/servant/libservant/EndpointInfo.cpp | 155 - cpp/servant/libservant/EndpointManager.cpp | 1935 -------- cpp/servant/libservant/Global.cpp | 53 - cpp/servant/libservant/NetworkUtil.cpp | 196 - cpp/servant/libservant/NotifyObserver.cpp | 195 - cpp/servant/libservant/ObjectProxy.cpp | 317 -- cpp/servant/libservant/ObjectProxyFactory.cpp | 77 - cpp/servant/libservant/PropertyReport.cpp | 123 - cpp/servant/libservant/Servant.cpp | 186 - cpp/servant/libservant/ServantHandle.cpp | 910 ---- cpp/servant/libservant/ServantHelper.cpp | 72 - cpp/servant/libservant/ServantProxy.cpp | 994 ----- .../libservant/ServantProxyFactory.cpp | 67 - cpp/servant/libservant/StatReport.cpp | 811 ---- cpp/servant/libservant/TarsConfig.cpp | 204 - cpp/servant/libservant/TarsCurrent.cpp | 341 -- cpp/servant/libservant/TarsLogger.cpp | 723 --- cpp/servant/libservant/TarsNodeF.cpp | 96 - cpp/servant/libservant/TarsNotify.cpp | 104 - cpp/servant/libservant/Transceiver.cpp | 668 --- cpp/servant/makefile/makefile.tars | 219 - cpp/servant/promise/exception_ptr.h | 235 - cpp/servant/promise/promise.h | 884 ---- cpp/servant/promise/promise_exception.h | 199 - cpp/servant/promise/tuple.h | 591 --- cpp/servant/promise/when_all.h | 333 -- cpp/servant/protocol/AdminF.tars | 32 - cpp/servant/protocol/Auth.tars | 115 - cpp/servant/protocol/BaseF.tars | 66 - cpp/servant/protocol/CMakeLists.txt | 65 - cpp/servant/protocol/ConfigF.tars | 127 - cpp/servant/protocol/EndpointF.tars | 41 - cpp/servant/protocol/LogF.tars | 63 - cpp/servant/protocol/NodeF.tars | 43 - cpp/servant/protocol/NotifyF.tars | 118 - cpp/servant/protocol/PropertyF.tars | 64 - cpp/servant/protocol/QueryF.tars | 86 - cpp/servant/protocol/StatF.tars | 92 - cpp/servant/script/create_http_server.sh | 52 - cpp/servant/script/create_tars_server.sh | 52 - cpp/servant/script/demo/DemoServant.tars | 10 - cpp/servant/script/demo/DemoServantImp.cpp | 19 - cpp/servant/script/demo/DemoServantImp.h | 35 - cpp/servant/script/demo/DemoServer.cpp | 43 - cpp/servant/script/demo/DemoServer.h | 34 - cpp/servant/script/demo/makefile | 17 - .../script/http_demo/DemoServantImp.cpp | 44 - cpp/servant/script/http_demo/DemoServantImp.h | 37 - cpp/servant/script/http_demo/DemoServer.cpp | 82 - cpp/servant/script/http_demo/DemoServer.h | 34 - cpp/servant/script/http_demo/makefile | 17 - cpp/servant/servant/AdapterProxy.h | 327 -- cpp/servant/servant/AdminServant.h | 75 - cpp/servant/servant/AppCache.h | 111 - cpp/servant/servant/AppProtocol.h | 514 --- cpp/servant/servant/Application.h | 452 -- cpp/servant/servant/AsyncProcThread.h | 79 - cpp/servant/servant/BaseNotify.h | 84 - cpp/servant/servant/Communicator.h | 309 -- cpp/servant/servant/CommunicatorEpoll.h | 329 -- cpp/servant/servant/CommunicatorFactory.h | 103 - cpp/servant/servant/CoroutineScheduler.h | 715 --- cpp/servant/servant/EndpointInfo.h | 255 -- cpp/servant/servant/EndpointManager.h | 688 --- cpp/servant/servant/Global.h | 239 - cpp/servant/servant/Message.h | 300 -- cpp/servant/servant/NetworkUtil.h | 64 - cpp/servant/servant/NotifyObserver.h | 123 - cpp/servant/servant/ObjectProxy.h | 344 -- cpp/servant/servant/ObjectProxyFactory.h | 102 - cpp/servant/servant/PropertyReport.h | 261 -- cpp/servant/servant/Servant.h | 299 -- cpp/servant/servant/ServantHandle.h | 203 - cpp/servant/servant/ServantHelper.h | 207 - cpp/servant/servant/ServantProxy.h | 704 --- cpp/servant/servant/ServantProxyFactory.h | 65 - cpp/servant/servant/StatReport.h | 374 -- cpp/servant/servant/TarsConfig.h | 154 - cpp/servant/servant/TarsCurrent.h | 308 -- cpp/servant/servant/TarsLogger.h | 879 ---- cpp/servant/servant/TarsNodeF.h | 78 - cpp/servant/servant/TarsNotify.h | 90 - cpp/servant/servant/Transceiver.h | 376 -- cpp/servant/tup/CMakeLists.txt | 5 - cpp/servant/tup/RequestF.tars | 47 - cpp/servant/tup/Tars.h | 1922 -------- cpp/servant/tup/TarsDisplayer.h | 371 -- cpp/servant/tup/TarsType.h | 248 -- cpp/servant/tup/tup.h | 686 --- cpp/test/CMakeLists.txt | 7 - cpp/test/README.md | 8 - cpp/test/testFramework/CMakeLists.txt | 8 - .../testAdminRegistry/CMakeLists.txt | 13 - .../testFramework/testAdminRegistry/main.cpp | 109 - cpp/test/testServant/CMakeLists.txt | 8 - .../testTarsAnalyzer/CMakeLists.txt | 12 - .../testServant/testTarsAnalyzer/main.cpp | 172 - .../testTarsAnalyzer/tarsAnalyzer.cpp | 1221 ----- .../testTarsAnalyzer/tarsAnalyzer.h | 442 -- .../testTarsAnalyzer/tupRequestF.h | 50 - cpp/test/testServant/testTup/CMakeLists.txt | 12 - cpp/test/testServant/testTup/UserInfo.h | 170 - cpp/test/testServant/testTup/UserInfo.tars | 17 - cpp/test/testServant/testTup/main.cpp | 213 - cpp/test/testUtil/CMakeLists.txt | 18 - cpp/test/testUtil/example_tc_autoptr.cpp | 132 - cpp/test/testUtil/example_tc_bitmap.cpp | 122 - cpp/test/testUtil/example_tc_cgi.cpp | 136 - cpp/test/testUtil/example_tc_common.cpp | 190 - cpp/test/testUtil/example_tc_config.cpp | 101 - cpp/test/testUtil/example_tc_encoder.cpp | 145 - cpp/test/testUtil/example_tc_file.cpp | 110 - cpp/test/testUtil/example_tc_file_lock.cpp | 78 - cpp/test/testUtil/example_tc_gzip.cpp | 50 - cpp/test/testUtil/example_tc_hashmap.cpp | 402 -- cpp/test/testUtil/example_tc_http.cpp | 636 --- cpp/test/testUtil/example_tc_http_async.cpp | 91 - cpp/test/testUtil/example_tc_lock.cpp | 90 - cpp/test/testUtil/example_tc_log.cpp | 150 - cpp/test/testUtil/example_tc_mem_chunk.cpp | 115 - cpp/test/testUtil/example_tc_mem_queue.cpp | 124 - cpp/test/testUtil/example_tc_mmap.cpp | 98 - .../testUtil/example_tc_multi_hashmap.cpp | 812 ---- cpp/test/testUtil/example_tc_mysql.cpp | 74 - cpp/test/testUtil/example_tc_option.cpp | 52 - cpp/test/testUtil/example_tc_pack.cpp | 109 - cpp/test/testUtil/example_tc_singleton.cpp | 67 - cpp/test/testUtil/example_tc_socket.cpp | 226 - cpp/test/testUtil/example_tc_thread.cpp | 91 - cpp/test/testUtil/example_tc_thread_lock.cpp | 96 - cpp/test/testUtil/example_tc_thread_pool.cpp | 152 - cpp/test/testUtil/example_tc_thread_queue.cpp | 116 - cpp/test/testUtil/log.config.conf | 64 - cpp/test/testUtil/template.config.conf | 55 - cpp/test/testUtil/test.html | 33 - cpp/thirdparty/thirdparty.sh | 1 - cpp/tools/CMakeLists.txt | 112 - cpp/tools/README.md | 19 - cpp/tools/pb2tarscpp/CMakeLists.txt | 13 - cpp/tools/pb2tarscpp/CppGenCallback.cpp | 146 - cpp/tools/pb2tarscpp/CppGenCallback.h | 24 - cpp/tools/pb2tarscpp/CppGenProxy.cpp | 126 - cpp/tools/pb2tarscpp/CppGenProxy.h | 23 - cpp/tools/pb2tarscpp/CppGenServant.cpp | 142 - cpp/tools/pb2tarscpp/CppGenServant.h | 22 - cpp/tools/pb2tarscpp/CppPbUtils.cpp | 39 - cpp/tools/pb2tarscpp/CppPbUtils.h | 16 - cpp/tools/pb2tarscpp/CppPlugin.cpp | 108 - cpp/tools/pb2tarscpp/CppPlugin.h | 37 - cpp/tools/tars2android/CMakeLists.txt | 1 - cpp/tools/tars2android/interface_analysis.cpp | 100 - cpp/tools/tars2android/interface_analysis.h | 47 - cpp/tools/tars2android/main.cpp | 159 - cpp/tools/tars2android/tars2android.cpp | 2020 --------- cpp/tools/tars2android/tars2android.h | 384 -- cpp/tools/tars2android/tars_filter.cpp | 61 - cpp/tools/tars2android/tars_filter.h | 39 - cpp/tools/tars2c/CMakeLists.txt | 1 - cpp/tools/tars2c/main.cpp | 103 - cpp/tools/tars2c/tars2c.cpp | 776 ---- cpp/tools/tars2c/tars2c.h | 348 -- cpp/tools/tars2cpp/CMakeLists.txt | 1 - cpp/tools/tars2cpp/main.cpp | 132 - cpp/tools/tars2cpp/tars2cpp.cpp | 2757 ------------ cpp/tools/tars2cpp/tars2cpp.h | 440 -- cpp/tools/tars2cs/CMakeLists.txt | 1 - cpp/tools/tars2cs/main.cpp | 122 - cpp/tools/tars2cs/tars2cs.cpp | 428 -- cpp/tools/tars2cs/tars2cs.h | 245 - cpp/tools/tars2node/CMakeLists.txt | 32 - cpp/tools/tars2node/code_generator.cpp | 87 - cpp/tools/tars2node/code_generator.h | 261 -- cpp/tools/tars2node/file_util.cpp | 33 - cpp/tools/tars2node/gen_js.cpp | 388 -- cpp/tools/tars2node/gen_js_dts.cpp | 231 - cpp/tools/tars2node/gen_proxy.cpp | 383 -- cpp/tools/tars2node/gen_proxy_dts.cpp | 173 - cpp/tools/tars2node/gen_server.cpp | 381 -- cpp/tools/tars2node/gen_server_dts.cpp | 149 - cpp/tools/tars2node/gen_server_imp.cpp | 123 - cpp/tools/tars2node/idl_scan.cpp | 175 - cpp/tools/tars2node/idl_util.cpp | 391 -- cpp/tools/tars2node/main.cpp | 184 - cpp/tools/tars2oc/CMakeLists.txt | 1 - cpp/tools/tars2oc/main.cpp | 122 - cpp/tools/tars2oc/tars2oc.cpp | 921 ---- cpp/tools/tars2oc/tars2oc.h | 324 -- cpp/tools/tars2php/CMakeLists.txt | 1 - cpp/tools/tars2php/main.cpp | 115 - cpp/tools/tars2php/tars2php.cpp | 346 -- cpp/tools/tars2php/tars2php.h | 193 - cpp/tools/tars2python/CMakeLists.txt | 1 - cpp/tools/tars2python/main.cpp | 105 - cpp/tools/tars2python/tars2python.cpp | 555 --- cpp/tools/tars2python/tars2python.h | 90 - cpp/tools/tarsgrammar/CMakeLists.txt | 68 - cpp/tools/tarsgrammar/tars.l | 390 -- cpp/tools/tarsgrammar/tars.y | 1110 ----- cpp/tools/tarsparse/CMakeLists.txt | 37 - cpp/tools/tarsparse/element.cpp | 339 -- cpp/tools/tarsparse/element.h | 861 ---- cpp/tools/tarsparse/parse.cpp | 603 --- cpp/tools/tarsparse/parse.h | 368 -- cpp/util/CMakeLists.txt | 12 - cpp/util/README.md | 1 - cpp/util/include/CMakeLists.txt | 3 - cpp/util/include/util/detail/tc_assert.h | 47 - .../include/util/detail/tc_checked_delete.h | 40 - .../util/detail/tc_shared_count_base.h | 68 - .../util/detail/tc_shared_count_impl.h | 71 - .../include/util/detail/tc_template_util.h | 164 - cpp/util/include/util/tc_atomic.h | 199 - cpp/util/include/util/tc_autoptr.h | 508 --- cpp/util/include/util/tc_base64.h | 93 - cpp/util/include/util/tc_bind.h | 409 -- cpp/util/include/util/tc_bind_detail.h | 2357 ---------- cpp/util/include/util/tc_bitmap.h | 250 -- cpp/util/include/util/tc_buffer.h | 229 - cpp/util/include/util/tc_buffer_pool.h | 148 - cpp/util/include/util/tc_callback.h | 512 --- cpp/util/include/util/tc_callback_detail.h | 66 - cpp/util/include/util/tc_cgi.h | 595 --- cpp/util/include/util/tc_clientsocket.h | 662 --- cpp/util/include/util/tc_common.h | 785 ---- cpp/util/include/util/tc_config.h | 534 --- cpp/util/include/util/tc_consistent_hash.h | 174 - .../include/util/tc_consistent_hash_new.h | 383 -- cpp/util/include/util/tc_dyn_object.h | 107 - .../include/util/tc_enable_shared_from_this.h | 80 - cpp/util/include/util/tc_encoder.h | 143 - cpp/util/include/util/tc_epoll_server.h | 1868 -------- cpp/util/include/util/tc_epoller.h | 140 - cpp/util/include/util/tc_eventfd.h | 65 - cpp/util/include/util/tc_ex.h | 94 - cpp/util/include/util/tc_fcontext.h | 31 - cpp/util/include/util/tc_fcontext_i386.h | 40 - cpp/util/include/util/tc_fcontext_x86_64.h | 49 - cpp/util/include/util/tc_fifo.h | 127 - cpp/util/include/util/tc_file.h | 289 -- cpp/util/include/util/tc_file_mutex.h | 162 - cpp/util/include/util/tc_functor.h | 999 ----- cpp/util/include/util/tc_gzip.h | 185 - cpp/util/include/util/tc_hash_fun.h | 198 - cpp/util/include/util/tc_hashmap.h | 1894 -------- cpp/util/include/util/tc_hashmap_compact.h | 2043 --------- cpp/util/include/util/tc_http.h | 1280 ------ cpp/util/include/util/tc_http2clientmgr.h | 58 - cpp/util/include/util/tc_http2session.h | 160 - cpp/util/include/util/tc_http_async.h | 475 -- cpp/util/include/util/tc_lock.h | 281 -- cpp/util/include/util/tc_logger.h | 1646 ------- cpp/util/include/util/tc_loki.h | 637 --- cpp/util/include/util/tc_loop_queue.h | 227 - cpp/util/include/util/tc_malloc_chunk.h | 709 --- cpp/util/include/util/tc_md5.h | 179 - cpp/util/include/util/tc_mem_chunk.h | 628 --- cpp/util/include/util/tc_mem_queue.h | 207 - cpp/util/include/util/tc_mem_vector.h | 346 -- cpp/util/include/util/tc_mmap.h | 181 - cpp/util/include/util/tc_monitor.h | 224 - cpp/util/include/util/tc_multi_hashmap.h | 2628 ----------- cpp/util/include/util/tc_mysql.h | 481 -- cpp/util/include/util/tc_nghttp2.h | 197 - cpp/util/include/util/tc_openssl.h | 151 - cpp/util/include/util/tc_option.h | 111 - cpp/util/include/util/tc_pack.h | 825 ---- cpp/util/include/util/tc_parsepara.h | 211 - cpp/util/include/util/tc_rbtree.h | 1967 -------- .../include/util/tc_readers_writer_data.h | 83 - cpp/util/include/util/tc_scoped_ptr.h | 83 - cpp/util/include/util/tc_sem_mutex.h | 166 - cpp/util/include/util/tc_sha.h | 177 - cpp/util/include/util/tc_shared_ptr.h | 246 - cpp/util/include/util/tc_shm.h | 174 - cpp/util/include/util/tc_singleton.h | 266 -- cpp/util/include/util/tc_socket.h | 544 --- cpp/util/include/util/tc_squeue.h | 265 -- cpp/util/include/util/tc_sslmgr.h | 87 - cpp/util/include/util/tc_tea.h | 175 - cpp/util/include/util/tc_thread.h | 187 - cpp/util/include/util/tc_thread_cond.h | 144 - cpp/util/include/util/tc_thread_mutex.h | 178 - cpp/util/include/util/tc_thread_pool.h | 362 -- cpp/util/include/util/tc_thread_queue.h | 289 -- cpp/util/include/util/tc_thread_rwlock.h | 114 - cpp/util/include/util/tc_timeout_queue.h | 422 -- cpp/util/include/util/tc_timeout_queue_map.h | 568 --- cpp/util/include/util/tc_timeout_queue_new.h | 339 -- cpp/util/include/util/tc_timeout_queue_noid.h | 221 - cpp/util/include/util/tc_timeprovider.h | 145 - cpp/util/src/CMakeLists.txt | 30 - cpp/util/src/tc_base64.cpp | 181 - cpp/util/src/tc_bitmap.cpp | 375 -- cpp/util/src/tc_buffer.cpp | 196 - cpp/util/src/tc_buffer_pool.cpp | 212 - cpp/util/src/tc_cgi.cpp | 873 ---- cpp/util/src/tc_clientsocket.cpp | 762 ---- cpp/util/src/tc_common.cpp | 757 ---- cpp/util/src/tc_config.cpp | 784 ---- cpp/util/src/tc_dyn_object.cpp | 85 - cpp/util/src/tc_encoder.cpp | 310 -- cpp/util/src/tc_epoll_server.cpp | 2394 ---------- cpp/util/src/tc_epoller.cpp | 93 - cpp/util/src/tc_ex.cpp | 62 - cpp/util/src/tc_fifo.cpp | 89 - cpp/util/src/tc_file.cpp | 514 --- cpp/util/src/tc_file_mutex.cpp | 126 - cpp/util/src/tc_gzip.cpp | 141 - cpp/util/src/tc_hashmap.cpp | 2592 ----------- cpp/util/src/tc_hashmap_compact.cpp | 2764 ------------ cpp/util/src/tc_http.cpp | 2159 --------- cpp/util/src/tc_http2clientmgr.cpp | 62 - cpp/util/src/tc_http2session.cpp | 455 -- cpp/util/src/tc_http_async.cpp | 517 --- cpp/util/src/tc_jump_i386_sysv_elf_gas.s | 65 - cpp/util/src/tc_jump_x86_64_sysv_elf_gas.s | 75 - cpp/util/src/tc_logger.cpp | 310 -- cpp/util/src/tc_make_i386_sysv_elf_gas.s | 70 - cpp/util/src/tc_make_x86_64_sysv_elf_gas.s | 67 - cpp/util/src/tc_malloc_chunk.cpp | 1608 ------- cpp/util/src/tc_md5.cpp | 318 -- cpp/util/src/tc_mem_chunk.cpp | 707 --- cpp/util/src/tc_mem_queue.cpp | 329 -- cpp/util/src/tc_mmap.cpp | 143 - cpp/util/src/tc_multi_hashmap.cpp | 3948 ----------------- cpp/util/src/tc_mysql.cpp | 499 --- cpp/util/src/tc_nghttp2.cpp | 244 - cpp/util/src/tc_openssl.cpp | 176 - cpp/util/src/tc_option.cpp | 85 - cpp/util/src/tc_pack.cpp | 443 -- cpp/util/src/tc_parsepara.cpp | 284 -- cpp/util/src/tc_rbtree.cpp | 3137 ------------- cpp/util/src/tc_sem_mutex.cpp | 210 - cpp/util/src/tc_sha.cpp | 810 ---- cpp/util/src/tc_shm.cpp | 95 - cpp/util/src/tc_socket.cpp | 630 --- cpp/util/src/tc_sslmgr.cpp | 187 - cpp/util/src/tc_tea.cpp | 729 --- cpp/util/src/tc_thread.cpp | 138 - cpp/util/src/tc_thread_cond.cpp | 104 - cpp/util/src/tc_thread_mutex.cpp | 223 - cpp/util/src/tc_thread_pool.cpp | 303 -- cpp/util/src/tc_thread_rwlock.cpp | 126 - cpp/util/src/tc_timeprovider.cpp | 146 - docs-en/tars_config.md | 121 - docs-en/tars_cpp_develop_specification.md | 177 - docs-en/tars_cpp_faq.md | 52 - docs-en/tars_cpp_future_promise.md | 92 - docs-en/tars_cpp_http_demo.md | 236 - docs-en/tars_cpp_quickstart.md | 518 --- docs-en/tars_cpp_server_thread.md | 73 - docs-en/tars_cpp_user_guide.md | 1359 ------ docs-en/tars_java_quickstart.md | 351 -- docs-en/tars_java_spring.md | 67 - docs-en/tars_java_spring_cloud.md | 176 - docs-en/tars_java_user_guide.md | 698 --- docs-en/tars_protobuf_cpp.md | 47 - docs-en/tars_push.md | 685 --- docs-en/tars_server_monitor.md | 53 - docs-en/tars_template.md | 642 --- docs-en/tars_tup.md | 595 --- docs/images/eureka-tars-java.png | Bin 33868 -> 0 bytes docs/images/tars-en.png | Bin 39204 -> 0 bytes docs/images/tars_auth_client.png | Bin 44932 -> 0 bytes docs/images/tars_auth_endpoint.png | Bin 37404 -> 0 bytes docs/images/tars_auth_server.png | Bin 47693 -> 0 bytes docs/images/tars_config_app1.png | Bin 29102 -> 0 bytes docs/images/tars_config_app2.png | Bin 14461 -> 0 bytes docs/images/tars_config_appconfig.png | Bin 32786 -> 0 bytes docs/images/tars_config_appconfig_result.png | Bin 21660 -> 0 bytes docs/images/tars_config_jiegoutu.png | Bin 27668 -> 0 bytes docs/images/tars_config_references.png | Bin 19207 -> 0 bytes docs/images/tars_config_server1.png | Bin 31761 -> 0 bytes docs/images/tars_config_server2.png | Bin 14986 -> 0 bytes docs/images/tars_config_server3_ref1.png | Bin 13157 -> 0 bytes docs/images/tars_config_server3_ref2.png | Bin 7938 -> 0 bytes docs/images/tars_config_set1.png | Bin 29344 -> 0 bytes docs/images/tars_config_set2.png | Bin 14650 -> 0 bytes docs/images/tars_config_table1.png | Bin 6598 -> 0 bytes docs/images/tars_config_table2.png | Bin 2706 -> 0 bytes docs/images/tars_cpp_quickstart_bushu1.png | Bin 51546 -> 0 bytes docs/images/tars_cpp_quickstart_bushu2.png | Bin 44939 -> 0 bytes docs/images/tars_cpp_quickstart_patch.png | Bin 32151 -> 0 bytes .../tars_cpp_quickstart_patchresult.png | Bin 24020 -> 0 bytes docs/images/tars_cpp_quickstart_upload.png | Bin 30297 -> 0 bytes docs/images/tars_cpp_third_protocol.png | Bin 19540 -> 0 bytes docs/images/tars_dye_en.png | Bin 45507 -> 0 bytes docs/images/tars_flow.PNG | Bin 56055 -> 0 bytes docs/images/tars_idc_en.png | Bin 23608 -> 0 bytes docs/images/tars_java_evn1.png | Bin 8538 -> 0 bytes docs/images/tars_java_evn2.png | Bin 13217 -> 0 bytes docs/images/tars_java_evn3.png | Bin 41775 -> 0 bytes docs/images/tars_java_quickstart_bushu1.png | Bin 49740 -> 0 bytes docs/images/tars_java_quickstart_bushu2.png | Bin 44973 -> 0 bytes docs/images/tars_java_quickstart_patch.png | Bin 32151 -> 0 bytes .../tars_java_quickstart_patchresult.png | Bin 24020 -> 0 bytes docs/images/tars_java_quickstart_upload.png | Bin 43441 -> 0 bytes docs/images/tars_junheng_en.png | Bin 19133 -> 0 bytes docs/images/tars_overload_en.png | Bin 18050 -> 0 bytes docs/images/tars_property.png | Bin 75977 -> 41060 bytes docs/images/tars_property_en.png | Bin 47069 -> 0 bytes docs/images/tars_push_deploy.PNG | Bin 26027 -> 0 bytes docs/images/tars_result.PNG | Bin 17185 -> 0 bytes docs/images/tars_rongcuo_en.png | Bin 17797 -> 0 bytes docs/images/tars_set_en.png | Bin 15993 -> 0 bytes docs/images/tars_ssl_endpoint.png | Bin 14771 -> 24110 bytes docs/images/tars_stat.png | Bin 65239 -> 204196 bytes docs/images/tars_stat_en.png | Bin 136789 -> 0 bytes docs/images/tars_tarslog_bushu.png | Bin 52546 -> 44054 bytes docs/images/tars_tarslog_bushu_en.png | Bin 80232 -> 45583 bytes docs/images/tars_tarslog_patch.png | Bin 30898 -> 44439 bytes docs/images/tars_tarslog_patch_en.png | Bin 46495 -> 49984 bytes docs/images/tars_tarsnotify_bushu.png | Bin 20791 -> 22009 bytes docs/images/tars_tarsnotify_bushu_en.png | Bin 37096 -> 21145 bytes docs/images/tars_tarsnotify_patch.png | Bin 31468 -> 33206 bytes docs/images/tars_tarsnotify_patch_en.png | Bin 46535 -> 34892 bytes docs/images/tars_tarsproperty_bushu.png | Bin 54290 -> 46324 bytes docs/images/tars_tarsproperty_bushu_en.png | Bin 83042 -> 48481 bytes docs/images/tars_tarsproperty_patch.png | Bin 31505 -> 43964 bytes docs/images/tars_tarsproperty_patch_en.png | Bin 48175 -> 31136 bytes docs/images/tars_tarsqueryproperty_bushu.png | Bin 56188 -> 47635 bytes .../tars_tarsqueryproperty_bushu_en.png | Bin 81066 -> 48998 bytes docs/images/tars_tarsqueryproperty_patch.png | Bin 31616 -> 44815 bytes .../tars_tarsqueryproperty_patch_en.png | Bin 48486 -> 45461 bytes docs/images/tars_tarsquerystat_bushu.png | Bin 55425 -> 44094 bytes docs/images/tars_tarsquerystat_bushu_en.png | Bin 81750 -> 47962 bytes docs/images/tars_tarsquerystat_patch.png | Bin 31266 -> 44053 bytes docs/images/tars_tarsquerystat_patch_en.png | Bin 47026 -> 30158 bytes docs/images/tars_tarsstat_bushu.png | Bin 51146 -> 44091 bytes docs/images/tars_tarsstat_bushu_en.png | Bin 76125 -> 46210 bytes docs/images/tars_tarsstat_patch.png | Bin 30638 -> 43886 bytes docs/images/tars_tarsstat_patch_en.png | Bin 58093 -> 29983 bytes docs/images/tars_template_table1.png | Bin 3598 -> 0 bytes docs/images/tars_top_en.png | Bin 52949 -> 0 bytes docs/images/tars_tupclass.png | Bin 56656 -> 0 bytes docs/images/tars_tupstruct.png | Bin 29934 -> 0 bytes docs/images/tars_web_index.png | Bin 25139 -> 0 bytes docs/images/tars_web_system.png | Bin 53656 -> 38249 bytes docs/images/tars_web_system_en.png | Bin 24709 -> 0 bytes docs/images/tars_web_system_index.png | Bin 16831 -> 38249 bytes docs/images/tars_web_system_index_en.png | Bin 42757 -> 44721 bytes docs/question_images/encoding_error.png | Bin 9273 -> 0 bytes docs/question_images/resin.png | Bin 11949 -> 0 bytes docs/tars_auth.md | 67 - docs/tars_config.md | 121 - docs/tars_cpp_develop_specification.md | 178 - docs/tars_cpp_faq.md | 52 - docs/tars_cpp_future_promise.md | 88 - docs/tars_cpp_http_demo.md | 237 - docs/tars_cpp_quickstart.md | 486 -- docs/tars_cpp_server_thread.md | 74 - docs/tars_cpp_user_guide.md | 1341 ------ docs/tars_java_faq.md | 6 - docs/tars_java_quickstart.md | 326 -- docs/tars_java_spring.md | 67 - docs/tars_java_spring_boot.md | 183 - docs/tars_java_spring_cloud.md | 177 - docs/tars_java_user_guide.md | 697 --- docs/tars_protobuf_cpp.md | 44 - docs/tars_push.md | 684 --- docs/tars_server_monitor.md | 53 - docs/tars_template.md | 642 --- docs/tars_tup.md | 601 --- framework | 1 + java | 1 + java/README.md | 18 - java/core/README.md | 0 java/core/client.pom.xml | 120 - java/core/pom.xml | 59 - java/core/server.pom.xml | 111 - .../com/qq/tars/client/ClientVersion.java | 32 - .../java/com/qq/tars/client/Communicator.java | 154 - .../qq/tars/client/CommunicatorConfig.java | 367 -- .../qq/tars/client/CommunicatorFactory.java | 67 - .../java/com/qq/tars/client/ObjectProxy.java | 212 - .../qq/tars/client/ObjectProxyFactory.java | 152 - .../java/com/qq/tars/client/ServantProxy.java | 30 - .../qq/tars/client/ServantProxyConfig.java | 330 -- .../qq/tars/client/ServantProxyFactory.java | 64 - .../client/cluster/DefaultLoadBalance.java | 90 - .../cluster/ServantInvokerAliveStat.java | 149 - .../cluster/ServantnvokerAliveChecker.java | 43 - .../qq/tars/client/rpc/InvokerComparator.java | 29 - .../com/qq/tars/client/rpc/ServantClient.java | 272 -- .../tars/client/rpc/ServantInvokeContext.java | 128 - .../qq/tars/client/rpc/ServantInvoker.java | 72 - .../client/rpc/ServantProtocolInvoker.java | 146 - .../ConsistentHashLoadBalance.java | 132 - .../rpc/loadbalance/DefaultLoadBalance.java | 100 - .../rpc/loadbalance/HashLoadBalance.java | 123 - .../rpc/loadbalance/LoadBalanceHelper.java | 180 - .../loadbalance/RoundRobinLoadBalance.java | 124 - .../rpc/tars/TarsCallbackFilterChain.java | 58 - .../client/rpc/tars/TarsCallbackWrapper.java | 124 - .../rpc/tars/TarsClientFilterChain.java | 64 - .../client/rpc/tars/TarsInvokeContext.java | 40 - .../qq/tars/client/rpc/tars/TarsInvoker.java | 192 - .../client/rpc/tars/TarsProtocolInvoker.java | 47 - .../client/support/ClientPoolManager.java | 93 - .../support/ClientThreadPoolManager.java | 70 - .../client/support/ServantCacheManager.java | 155 - .../com/qq/tars/client/util/ClientLogger.java | 36 - .../java/com/qq/tars/client/util/Pair.java | 78 - .../com/qq/tars/client/util/ParseTools.java | 153 - .../qq/tars/common/AbstractFilterChain.java | 54 - .../main/java/com/qq/tars/common/Filter.java | 14 - .../java/com/qq/tars/common/FilterChain.java | 9 - .../java/com/qq/tars/common/FilterKind.java | 5 - .../common/support/ClassLoaderManager.java | 42 - .../com/qq/tars/common/support/Endpoint.java | 182 - .../com/qq/tars/common/support/Holder.java | 59 - .../support/ScheduledExecutorManager.java | 156 - .../com/qq/tars/common/util/BeanAccessor.java | 221 - .../com/qq/tars/common/util/CommonUtils.java | 73 - .../java/com/qq/tars/common/util/Config.java | 214 - .../com/qq/tars/common/util/Constants.java | 85 - .../qq/tars/common/util/DyeingKeyCache.java | 53 - .../com/qq/tars/common/util/DyeingSwitch.java | 54 - .../com/qq/tars/common/util/FileUtil.java | 130 - .../java/com/qq/tars/common/util/HexUtil.java | 82 - .../java/com/qq/tars/common/util/Loader.java | 54 - .../com/qq/tars/common/util/StringUtils.java | 357 -- .../com/qq/tars/common/util/TableUtil.java | 58 - .../common/util/concurrent/TaskQueue.java | 120 - .../common/util/concurrent/TaskThread.java | 45 - .../util/concurrent/TaskThreadFactory.java | 46 - .../concurrent/TaskThreadPoolExecutor.java | 233 - .../qq/tars/protocol/annotation/Servant.java | 29 - .../protocol/annotation/ServantCodec.java | 31 - .../tars/protocol/tars/TarsInputStream.java | 912 ---- .../protocol/tars/TarsInputStreamExt.java | 63 - .../tars/protocol/tars/TarsOutputStream.java | 369 -- .../protocol/tars/TarsOutputStreamExt.java | 61 - .../qq/tars/protocol/tars/TarsStructBase.java | 95 - .../tars/annotation/TarsCallback.java | 31 - .../protocol/tars/annotation/TarsContext.java | 29 - .../protocol/tars/annotation/TarsHolder.java | 31 - .../protocol/tars/annotation/TarsMethod.java | 29 - .../tars/annotation/TarsMethodParameter.java | 29 - .../tars/annotation/TarsRouteKey.java | 13 - .../protocol/tars/annotation/TarsStruct.java | 29 - .../tars/annotation/TarsStructProperty.java | 33 - .../tars/exc/TarsDecodeException.java | 26 - .../tars/exc/TarsEncodeException.java | 25 - .../protocol/tars/support/TarsMethodInfo.java | 84 - .../tars/support/TarsMethodParameterInfo.java | 69 - .../protocol/tars/support/TarsStructInfo.java | 41 - .../tars/support/TarsStrutPropertyInfo.java | 75 - .../qq/tars/protocol/util/TarsDisplayer.java | 696 --- .../com/qq/tars/protocol/util/TarsHelper.java | 580 --- .../com/qq/tars/protocol/util/TarsUtil.java | 409 -- .../com/qq/tars/register/RegisterHandler.java | 33 - .../com/qq/tars/register/RegisterManager.java | 47 - .../com/qq/tars/rpc/common/InvokeContext.java | 38 - .../java/com/qq/tars/rpc/common/Invoker.java | 30 - .../com/qq/tars/rpc/common/LoadBalance.java | 39 - .../qq/tars/rpc/common/ProtocolInvoker.java | 34 - .../main/java/com/qq/tars/rpc/common/Url.java | 178 - .../rpc/common/exc/NoInvokerException.java | 37 - .../rpc/common/support/AbstractInvoker.java | 79 - .../util/concurrent/ConcurrentHashSet.java | 65 - .../com/qq/tars/rpc/exc/ClientException.java | 46 - .../rpc/exc/CommunicatorConfigException.java | 37 - .../tars/rpc/exc/NoConnectionException.java | 29 - .../tars/rpc/exc/NotConnectedException.java | 39 - .../tars/rpc/exc/NotImplementedException.java | 39 - .../tars/rpc/exc/ServerDecodeException.java | 29 - .../tars/rpc/exc/ServerEncodeException.java | 29 - .../com/qq/tars/rpc/exc/ServerException.java | 61 - .../tars/rpc/exc/ServerNoFuncException.java | 29 - .../rpc/exc/ServerNoServantException.java | 29 - .../rpc/exc/ServerQueueTimeoutException.java | 29 - .../rpc/exc/ServerResetGridException.java | 29 - .../tars/rpc/exc/ServerUnknownException.java | 29 - .../rpc/exc/ServiceNotFoundException.java | 22 - .../com/qq/tars/rpc/exc/TarsException.java | 38 - .../com/qq/tars/rpc/exc/TimeoutException.java | 38 - .../rpc/ext/ExtendedProtocolCodecWrapper.java | 55 - .../tars/rpc/ext/ExtendedProtocolFactory.java | 59 - .../java/com/qq/tars/rpc/ext/RequestInfo.java | 30 - .../com/qq/tars/rpc/ext/ResponseInfo.java | 26 - .../java/com/qq/tars/rpc/protocol/Codec.java | 44 - .../rpc/protocol/ServantProtocolFactory.java | 38 - .../qq/tars/rpc/protocol/ServantRequest.java | 46 - .../qq/tars/rpc/protocol/ServantResponse.java | 27 - .../rpc/protocol/ext/ExtendedServant.java | 26 - .../protocol/ext/ExtendedServantRequest.java | 28 - .../protocol/ext/ExtendedServantResponse.java | 38 - .../qq/tars/rpc/protocol/tars/TarsCodec.java | 548 --- .../rpc/protocol/tars/TarsCodecHelper.java | 482 -- .../tars/TarsServantProtocolFactory.java | 38 - .../rpc/protocol/tars/TarsServantRequest.java | 179 - .../protocol/tars/TarsServantResponse.java | 184 - .../protocol/tars/support/AnalystManager.java | 60 - .../tars/support/TarsAbstractCallback.java | 67 - .../rpc/protocol/tup/BasicClassTypeUtil.java | 333 -- .../protocol/tup/ObjectCreateException.java | 26 - .../rpc/protocol/tup/OldUniAttribute.java | 278 -- .../tars/rpc/protocol/tup/UniAttribute.java | 313 -- .../com/qq/tars/server/ServerVersion.java | 32 - .../qq/tars/server/apps/BaseAppContext.java | 175 - .../server/apps/DefaultAppContextEvent.java | 27 - .../server/apps/DefaultAppServantEvent.java | 28 - .../qq/tars/server/apps/XmlAppContext.java | 162 - .../qq/tars/server/common/ServerLogger.java | 49 - .../tars/server/common/XMLConfigElement.java | 128 - .../qq/tars/server/common/XMLConfigFile.java | 59 - .../server/config/ConfigurationException.java | 29 - .../server/config/ConfigurationManager.java | 119 - .../server/config/ServantAdapterConfig.java | 117 - .../qq/tars/server/config/ServerConfig.java | 345 -- .../java/com/qq/tars/server/core/Adapter.java | 22 - .../com/qq/tars/server/core/AppContext.java | 39 - .../qq/tars/server/core/AppContextEvent.java | 30 - .../tars/server/core/AppContextListener.java | 25 - .../tars/server/core/AppContextManager.java | 34 - .../qq/tars/server/core/AppServantEvent.java | 35 - .../com/qq/tars/server/core/AppService.java | 25 - .../com/qq/tars/server/core/AsyncContext.java | 91 - .../java/com/qq/tars/server/core/Context.java | 74 - .../qq/tars/server/core/ContextManager.java | 40 - .../server/core/ExtendedServantProcessor.java | 100 - .../qq/tars/server/core/ServantAdapter.java | 190 - .../tars/server/core/ServantHomeSkeleton.java | 121 - .../qq/tars/server/core/ServantProcessor.java | 40 - .../server/core/ServantThreadPoolManager.java | 75 - .../java/com/qq/tars/server/core/Server.java | 157 - .../core/ServerThreadPoolDispatcher.java | 139 - .../server/core/TarsServantProcessor.java | 267 -- .../server/core/TarsServerFilterChain.java | 31 - .../server/ha/ConnectionSessionListener.java | 57 - .../java/com/qq/tars/server/startup/Main.java | 50 - .../qq/tars/support/admin/AdminFServant.java | 37 - .../qq/tars/support/admin/CommandHandler.java | 22 - .../support/admin/CustemCommandHelper.java | 46 - .../support/admin/impl/AdminFServantImpl.java | 312 -- .../qq/tars/support/config/ConfigHelper.java | 148 - .../support/config/UpdateConfigCallback.java | 22 - .../tars/support/config/prx/ConfigInfo.java | 167 - .../qq/tars/support/config/prx/ConfigPrx.java | 248 -- .../support/config/prx/ConfigPrxCallback.java | 44 - .../support/config/prx/GetConfigListInfo.java | 167 - .../com/qq/tars/support/log/ArrayWriter.java | 105 - .../qq/tars/support/log/LogConfCacheMngr.java | 148 - .../java/com/qq/tars/support/log/LogItem.java | 109 - .../qq/tars/support/log/LogWorkThread.java | 54 - .../java/com/qq/tars/support/log/Logger.java | 689 --- .../qq/tars/support/log/LoggerFactory.java | 343 -- .../tars/support/log/LoggingOutputStream.java | 107 - .../qq/tars/support/log/ThrowableInfo.java | 59 - .../com/qq/tars/support/log/prx/LogInfo.java | 248 -- .../com/qq/tars/support/log/prx/LogPrx.java | 89 - .../tars/support/log/prx/LogPrxCallback.java | 31 - .../com/qq/tars/support/log/util/Utils.java | 116 - .../com/qq/tars/support/node/NodeHelper.java | 89 - .../qq/tars/support/node/prx/ServerFPrx.java | 77 - .../support/node/prx/ServerFPrxCallback.java | 31 - .../qq/tars/support/node/prx/ServerInfo.java | 133 - .../qq/tars/support/notify/NotifyHelper.java | 90 - .../tars/support/notify/prx/NOTIFYLEVEL.java | 55 - .../tars/support/notify/prx/NotifyInfo.java | 102 - .../tars/support/notify/prx/NotifyItem.java | 136 - .../qq/tars/support/notify/prx/NotifyKey.java | 119 - .../qq/tars/support/notify/prx/NotifyPrx.java | 121 - .../support/notify/prx/NotifyPrxCallback.java | 35 - .../tars/support/notify/prx/ReportInfo.java | 199 - .../tars/support/notify/prx/ReportType.java | 51 - .../com/qq/tars/support/om/OmConstants.java | 30 - .../java/com/qq/tars/support/om/OmLogger.java | 33 - .../com/qq/tars/support/om/OmServiceMngr.java | 82 - .../tars/support/om/ScheduledServiceMngr.java | 109 - .../qq/tars/support/om/ServerStatHelper.java | 68 - .../property/CommonPropertyPolicy.java | 204 - .../support/property/JvmPropertyPolicy.java | 160 - .../property/PropertyReportHelper.java | 178 - .../support/property/prx/PropertyFPrx.java | 58 - .../property/prx/PropertyFPrxCallback.java | 34 - .../support/property/prx/StatPropInfo.java | 101 - .../support/property/prx/StatPropMsgBody.java | 84 - .../support/property/prx/StatPropMsgHead.java | 222 - .../qq/tars/support/query/QueryHelper.java | 81 - .../qq/tars/support/query/prx/EndpointF.java | 277 -- .../qq/tars/support/query/prx/QueryFPrx.java | 224 - .../support/query/prx/QueryFPrxCallback.java | 42 - .../tars/support/stat/InvokeStatHelper.java | 98 - .../com/qq/tars/support/stat/ProxyStat.java | 106 - .../qq/tars/support/stat/ProxyStatBody.java | 125 - .../qq/tars/support/stat/ProxyStatHead.java | 158 - .../qq/tars/support/stat/ProxyStatUtils.java | 131 - .../com/qq/tars/support/stat/StatHelper.java | 106 - .../qq/tars/support/stat/prx/ProxyInfo.java | 82 - .../qq/tars/support/stat/prx/StatFPrx.java | 86 - .../support/stat/prx/StatFPrxCallback.java | 36 - .../tars/support/stat/prx/StatMicMsgBody.java | 174 - .../tars/support/stat/prx/StatMicMsgHead.java | 290 -- .../tars/support/stat/prx/StatSampleMsg.java | 214 - .../trace/TarsTraceZipkinConfiguration.java | 91 - .../support/trace/TraceCallbackFilter.java | 61 - .../tars/support/trace/TraceClientFilter.java | 113 - .../qq/tars/support/trace/TraceContext.java | 74 - .../qq/tars/support/trace/TraceManager.java | 51 - .../tars/support/trace/TraceServerFilter.java | 77 - .../com/qq/tars/support/trace/TraceUtil.java | 66 - ...SupportedSuchSampleEncodeingException.java | 10 - .../NotSupportedSuchSampleTypeException.java | 10 - java/core/src/main/resources/AdminF.tars | 32 - java/core/src/main/resources/ConfigF.tars | 119 - java/core/src/main/resources/EndpointF.tars | 40 - java/core/src/main/resources/LogF.tars | 63 - java/core/src/main/resources/QueryF.tars | 86 - java/core/src/main/resources/StatF.tars | 92 - java/distributedContext/README.md | 0 java/distributedContext/pom.xml | 16 - .../qq/tars/context/DistributedContext.java | 35 - .../tars/context/DistributedContextCodec.java | 59 - .../context/DistributedContextManager.java | 83 - .../qq/tars/context/exc/CodecException.java | 37 - .../com/qq/tars/context/impl/ContextItem.java | 46 - .../context/impl/DistributedContextImpl.java | 72 - java/examples/README.md | 0 java/examples/pom.xml | 31 - java/examples/quickstart-client/README.md | 0 java/examples/quickstart-client/pom.xml | 67 - .../quickstart/client/testapp/HelloPrx.java | 37 - .../client/testapp/HelloPrxCallback.java | 29 - .../com/qq/tars/quickstart/domain/Main.java | 54 - .../src/main/resources/hello.tars | 23 - .../src/main/webapp/WEB-INF/web.xml | 7 - java/examples/quickstart-server/README.md | 10 - java/examples/quickstart-server/pom.xml | 70 - .../quickstart/server/AppStartListener.java | 33 - .../server/testapp/HelloServant.java | 31 - .../server/testapp/impl/HelloServantImpl.java | 27 - .../TestApp.HelloJavaServer.config.conf | 51 - .../src/main/resources/hello.tars | 23 - .../src/main/resources/servants.xml | 11 - .../src/main/webapp/WEB-INF/servants.xml | 11 - .../src/main/webapp/WEB-INF/web.xml | 7 - java/examples/stress-server/README.md | 0 java/examples/stress-server/pom.xml | 70 - .../stress/server/test/StressServant.java | 33 - .../server/test/impl/StressServantImpl.java | 34 - .../src/main/resources/stress.tars | 25 - .../src/main/webapp/WEB-INF/servants.xml | 7 - .../src/main/webapp/WEB-INF/web.xml | 7 - java/examples/tars-spring-boot-server/pom.xml | 68 - .../server/QuickStartApplication.java | 29 - .../server/testapp/HelloServant.java | 29 - .../server/testapp/impl/HelloServantImpl.java | 29 - .../TestApp.HelloJavaServer.config.conf | 51 - .../src/main/resources/hello.tars | 23 - .../tars-spring-cloud-client/README.md | 0 .../examples/tars-spring-cloud-client/pom.xml | 44 - .../tars/springcloud/test/client/Client.java | 33 - .../test/client/ClientApplication.java | 33 - .../springcloud/test/client/HelloPrx.java | 28 - .../src/main/resources/application.yml | 10 - .../tars-spring-cloud-server/README.md | 0 .../examples/tars-spring-cloud-server/pom.xml | 65 - .../springcloud/test/server/Application.java | 32 - .../springcloud/test/server/HelloServant.java | 31 - .../test/server/HelloServantImpl.java | 29 - .../test/server/HelloServantImpl2.java | 26 - .../src/main/resources/application.yml | 14 - java/examples/tars-spring-server/pom.xml | 28 - .../quickstart/server/AppStartListener.java | 35 - .../server/testapp/HelloServant.java | 29 - .../server/testapp/impl/HelloServantImpl.java | 30 - .../src/main/resources/servants-spring.xml | 10 - java/net/README.md | 0 java/net/pom.xml | 23 - .../java/com/qq/tars/net/client/Callback.java | 26 - .../java/com/qq/tars/net/client/Client.java | 364 -- .../com/qq/tars/net/client/FutureImpl.java | 55 - .../com/qq/tars/net/client/ticket/Ticket.java | 105 - .../net/client/ticket/TicketListener.java | 24 - .../tars/net/client/ticket/TicketManager.java | 85 - .../java/com/qq/tars/net/core/IoBuffer.java | 176 - .../java/com/qq/tars/net/core/Processor.java | 22 - .../java/com/qq/tars/net/core/Request.java | 103 - .../java/com/qq/tars/net/core/Response.java | 71 - .../java/com/qq/tars/net/core/Server.java | 161 - .../java/com/qq/tars/net/core/Session.java | 96 - .../com/qq/tars/net/core/SessionEvent.java | 29 - .../com/qq/tars/net/core/SessionListener.java | 25 - .../com/qq/tars/net/core/SessionManager.java | 40 - .../com/qq/tars/net/core/nio/Acceptor.java | 38 - .../com/qq/tars/net/core/nio/Reactor.java | 188 - .../qq/tars/net/core/nio/SelectorManager.java | 151 - .../tars/net/core/nio/SessionManagerImpl.java | 115 - .../com/qq/tars/net/core/nio/TCPAcceptor.java | 85 - .../com/qq/tars/net/core/nio/TCPSession.java | 336 -- .../com/qq/tars/net/core/nio/UDPAcceptor.java | 70 - .../com/qq/tars/net/core/nio/UDPSession.java | 174 - .../com/qq/tars/net/core/nio/WorkThread.java | 107 - .../qq/tars/net/protocol/ProtocolDecoder.java | 29 - .../qq/tars/net/protocol/ProtocolEncoder.java | 29 - .../tars/net/protocol/ProtocolException.java | 37 - .../qq/tars/net/protocol/ProtocolFactory.java | 24 - .../main/java/com/qq/tars/net/util/Utils.java | 115 - java/pom.xml | 100 - java/protobuf/README.md | 13 - java/protobuf/pom.xml | 28 - .../tars/rpc/protocol/proto/MethodCache.java | 44 - .../tars/rpc/protocol/proto/ProtoCodec.java | 78 - java/spring/README.md | 8 - java/spring/pom.xml | 19 - java/spring/tars-spring-boot-starter/pom.xml | 46 - .../server/apps/SpringBootAppContext.java | 133 - .../TarsManageServiceConfiguration.java | 48 - .../tars/spring/TarsServerConfiguration.java | 38 - .../spring/annotation/EnableTarsServer.java | 29 - .../annotation/RemotePropertySource.java | 26 - .../spring/annotation/TarsHttpService.java | 27 - .../tars/spring/annotation/TarsListener.java | 29 - .../bean/CommunicatorBeanPostProcessor.java | 87 - .../spring/bean/ManageServiceListener.java | 33 - .../tars/spring/bean/PropertiesListener.java | 58 - .../bean/ServletContainerCustomizer.java | 76 - .../spring/bean/TarsServerStartLifecycle.java | 77 - .../main/resources/META-INF/spring.factories | 3 - .../tars-spring-cloud-starter/README.md | 0 java/spring/tars-spring-cloud-starter/pom.xml | 50 - .../eureka/EurekaRegisterHandler.java | 151 - .../eureka/TarsEurekaAutoConfiguration.java | 84 - .../eureka/TarsEurekaClientConfig.java | 24 - .../eureka/TarsEurekaHealthCheckHandler.java | 28 - .../register/eureka/TarsEurekaInstance.java | 91 - .../eureka/TarsStatusChangeListener.java | 46 - .../tars/server/apps/TarsStartLifecycle.java | 217 - .../com/qq/tars/spring/TarsConfiguration.java | 35 - .../annotation/EnableTarsConfiguration.java | 30 - .../bean/TarsClientBeanPostProcessor.java | 89 - .../spring/config/TarsClientProperties.java | 25 - .../spring/config/TarsServerProperties.java | 88 - .../main/resources/META-INF/spring.factories | 2 - java/spring/tars-spring/README.md | 0 java/spring/tars-spring/pom.xml | 55 - .../qq/tars/server/apps/SpringAppContext.java | 117 - .../qq/tars/spring/annotation/TarsClient.java | 53 - .../tars/spring/annotation/TarsServant.java | 38 - .../qq/tars/spring/config/ListenerConfig.java | 30 - .../qq/tars/spring/config/ServantConfig.java | 67 - .../config/TarsSpringConfigException.java | 24 - .../schema/TarsBeanDefinitionParser.java | 86 - .../spring/schema/TarsNamespaceHandler.java | 31 - .../main/resources/META-INF/spring.handlers | 1 - .../main/resources/META-INF/spring.schemas | 1 - .../src/main/resources/META-INF/tars.xsd | 42 - java/tools/README.md | 0 java/tools/pom.xml | 26 - java/tools/tars-maven-plugin/README.md | 0 java/tools/tars-maven-plugin/pom.xml | 219 - .../com/qq/tars/maven/gensrc/Servant.java | 48 - .../qq/tars/maven/gensrc/Tars2JavaConfig.java | 120 - .../qq/tars/maven/gensrc/Tars2JavaMojo.java | 901 ---- .../qq/tars/maven/gensrc/TarsBuildMojo.java | 588 --- .../qq/tars/maven/model/ClasspathElement.java | 37 - .../com/qq/tars/maven/model/Dependency.java | 67 - .../com/qq/tars/maven/model/Directory.java | 28 - .../com/qq/tars/maven/parse/TarsLexer.java | 2437 ---------- .../com/qq/tars/maven/parse/TarsParser.java | 3200 ------------- .../qq/tars/maven/parse/ast/TarsConst.java | 55 - .../tars/maven/parse/ast/TarsCustomType.java | 46 - .../com/qq/tars/maven/parse/ast/TarsEnum.java | 60 - .../qq/tars/maven/parse/ast/TarsInclude.java | 34 - .../tars/maven/parse/ast/TarsInterface.java | 51 - .../com/qq/tars/maven/parse/ast/TarsKey.java | 53 - .../qq/tars/maven/parse/ast/TarsMapType.java | 44 - .../tars/maven/parse/ast/TarsNamespace.java | 83 - .../tars/maven/parse/ast/TarsOperation.java | 59 - .../qq/tars/maven/parse/ast/TarsParam.java | 62 - .../maven/parse/ast/TarsPrimitiveType.java | 92 - .../com/qq/tars/maven/parse/ast/TarsRoot.java | 68 - .../qq/tars/maven/parse/ast/TarsStruct.java | 50 - .../maven/parse/ast/TarsStructMember.java | 77 - .../com/qq/tars/maven/parse/ast/TarsType.java | 72 - .../tars/maven/parse/ast/TarsVectorType.java | 57 - .../com/qq/tars/maven/script/Platform.java | 208 - .../qq/tars/maven/util/ArchiveEntryUtils.java | 124 - .../qq/tars/maven/util/FilePermission.java | 89 - .../tars/maven/util/FilePermissionUtils.java | 134 - .../com/qq/tars/maven/util/StringUtils.java | 369 -- .../qq/tars/maven/util/XMLConfigElement.java | 125 - .../com/qq/tars/maven/util/XMLConfigFile.java | 57 - .../com/qq/tars/maven/script/configTemplate | 39 - .../com/qq/tars/maven/script/servantTemplate | 13 - .../com/qq/tars/maven/script/unixBinTemplate | 27 - .../qq/tars/maven/script/windowsBinTemplate | 88 - nodejs | 2 +- php | 1 + php/README.md | 115 - php/TARS-PHP-FAQ.md | 39 - php/TARS-PHP-ISSUE.md | 20 - ...77\347\224\250\345\256\236\350\267\265.md" | 228 - ...50\203\275RPC\346\241\206\346\236\266.pdf" | Bin 3377722 -> 0 bytes php/dist/phptars-tars-client-0.1.0.zip | Bin 16740 -> 0 bytes php/dist/phptars-tars-client-0.1.1.zip | Bin 17323 -> 0 bytes php/dist/phptars-tars-client-0.1.2.zip | Bin 17338 -> 0 bytes php/dist/phptars-tars-client-0.1.3.zip | Bin 16865 -> 0 bytes php/dist/phptars-tars-client-0.1.4.zip | Bin 17055 -> 0 bytes php/dist/phptars-tars-config-0.1.0.zip | Bin 5229 -> 0 bytes php/dist/phptars-tars-config-0.1.1.zip | Bin 5229 -> 0 bytes php/dist/phptars-tars-config-0.1.2.zip | Bin 5233 -> 0 bytes php/dist/phptars-tars-config-0.1.3.zip | Bin 5248 -> 0 bytes php/dist/phptars-tars-deploy-0.1.0.zip | Bin 2098 -> 0 bytes php/dist/phptars-tars-log-0.1.0.zip | Bin 4554 -> 0 bytes php/dist/phptars-tars-log-0.1.1.zip | Bin 4554 -> 0 bytes php/dist/phptars-tars-log-0.1.2.zip | Bin 4557 -> 0 bytes php/dist/phptars-tars-log-0.1.3.zip | Bin 4575 -> 0 bytes php/dist/phptars-tars-monitor-0.1.0.zip | Bin 27476 -> 0 bytes php/dist/phptars-tars-monitor-0.1.1.zip | Bin 27497 -> 0 bytes php/dist/phptars-tars-monitor-0.1.2.zip | Bin 31426 -> 0 bytes php/dist/phptars-tars-monitor-0.1.3.zip | Bin 31487 -> 0 bytes php/dist/phptars-tars-registry-0.1.0.zip | Bin 13956 -> 0 bytes php/dist/phptars-tars-registry-0.1.1.zip | Bin 13959 -> 0 bytes php/dist/phptars-tars-report-0.1.0.zip | Bin 4805 -> 0 bytes php/dist/phptars-tars-server-0.1.0.zip | Bin 25964 -> 0 bytes php/dist/phptars-tars-server-0.1.1.zip | Bin 25966 -> 0 bytes php/dist/phptars-tars-server-0.1.2.zip | Bin 26392 -> 0 bytes php/dist/phptars-tars-server-0.1.3.zip | Bin 26408 -> 0 bytes php/dist/phptars-tars-server-0.1.4.zip | Bin 26958 -> 0 bytes php/dist/phptars-tars-server-0.1.5.zip | Bin 27091 -> 0 bytes php/dist/phptars-tars-utils-0.1.0.zip | Bin 5626 -> 0 bytes php/dist/phptars-tars2php-0.1.0.zip | Bin 27789 -> 0 bytes php/dist/phptars-tars2php-0.1.1.zip | Bin 27649 -> 0 bytes php/dist/phptars-tars2php-0.1.2.zip | Bin 27660 -> 0 bytes php/dist/tarsphp.json | 541 --- php/examples/tars-http-server/README.md | 218 - .../tars-http-server/scripts/tars2php.sh | 6 - .../src/component/Controller.php | 57 - .../tars-http-server/src/composer.json | 26 - .../tars-http-server/src/conf/ENVConf.php | 26 - .../src/controller/IndexController.php | 316 -- php/examples/tars-http-server/src/index.php | 21 - .../PHPServer/obj/TestTafServiceServant.php | 377 -- .../obj/classes/ComplicatedStruct.php | 47 - .../PHPServer/obj/classes/LotofTags.php | 68 - .../PHPServer/obj/classes/OutStruct.php | 44 - .../PHPServer/obj/classes/SimpleStruct.php | 37 - .../PHPTest/PHPServer/obj/tars/example.tars | 65 - .../tars-http-server/src/services.php | 28 - .../tars-http-server/tars/example.tars | 65 - .../tars-http-server/tars/tars.proto.php | 13 - .../tars/tarsclient.proto.php | 19 - php/examples/tars-tcp-server/README.md | 199 - .../tars-tcp-server/scripts/tars2php.sh | 6 - .../tars-tcp-server/src/composer.json | 26 - .../tars-tcp-server/src/conf/ENVConf.php | 26 - .../StressObj/StressServant.php | 70 - .../StressObj/tars/stress.tars | 26 - .../src/impl/PHPServerServantImpl.php | 256 -- php/examples/tars-tcp-server/src/index.php | 21 - .../PHPServer/obj/TestTafServiceServant.php | 102 - .../obj/classes/ComplicatedStruct.php | 47 - .../PHPServer/obj/classes/LotofTags.php | 68 - .../PHPServer/obj/classes/OutStruct.php | 44 - .../PHPServer/obj/classes/SimpleStruct.php | 37 - .../PHPTest/PHPServer/obj/tars/example.tars | 66 - php/examples/tars-tcp-server/src/services.php | 13 - .../tars-tcp-server/tars/example.tars | 66 - .../tars-tcp-server/tars/tars.proto.php | 19 - php/examples/tars-timer-server/README.md | 76 - .../tars-timer-server/src/composer.json | 25 - php/examples/tars-timer-server/src/index.php | 21 - .../PHPServer/obj/TestTafServiceServant.php | 377 -- .../obj/classes/ComplicatedStruct.php | 47 - .../PHPServer/obj/classes/LotofTags.php | 68 - .../PHPServer/obj/classes/OutStruct.php | 44 - .../PHPServer/obj/classes/SimpleStruct.php | 37 - .../PHPTest/PHPServer/obj/tars/example.tars | 64 - .../tars-timer-server/src/services.php | 12 - .../src/timer/TenSecondsTimer.php | 43 - .../tars-timer-server/tars/tars.proto.php | 13 - php/tars-client/README.md | 48 - php/tars-client/composer.json | 33 - php/tars-client/src/Code.php | 132 - php/tars-client/src/Communicator.php | 457 -- php/tars-client/src/CommunicatorConfig.php | 331 -- php/tars-client/src/Consts.php | 19 - php/tars-client/src/RequestPacket.php | 53 - php/tars-client/src/ResponsePacket.php | 28 - php/tars-client/src/TUPAPIWrapper.php | 527 --- .../PHPServer/obj/TestTafServiceServant.php | 359 -- .../obj/classes/ComplicatedStruct.php | 47 - .../PHPServer/obj/classes/LotofTags.php | 68 - .../PHPServer/obj/classes/OutStruct.php | 44 - .../PHPServer/obj/classes/SimpleStruct.php | 37 - .../PHPTest/PHPServer/obj/tars/example.tars | 64 - php/tars-client/tests/composer.json | 25 - php/tars-client/tests/testServant.php | 38 - php/tars-config/README.md | 25 - php/tars-config/composer.json | 27 - php/tars-config/src/ConfigServant.php | 230 - php/tars-config/src/classes/ConfigInfo.php | 58 - .../src/classes/GetConfigListInfo.php | 58 - php/tars-config/src/tars/ConfigF.tars | 126 - php/tars-config/tests/demo.php | 21 - php/tars-deploy/README.md | 20 - php/tars-deploy/composer.json | 26 - php/tars-deploy/src/Deploy.php | 127 - php/tars-extension/README.md | 201 - php/tars-extension/auto | 9 - php/tars-extension/auto7.sh | 11 - php/tars-extension/config.m4 | 24 - php/tars-extension/include/php7_wrapper.h | 114 - php/tars-extension/include/php_error.h | 36 - php/tars-extension/include/php_tupapi.h | 158 - php/tars-extension/include/tars_c.h | 466 -- php/tars-extension/include/ttars.h | 221 - php/tars-extension/include/tup_c.h | 172 - php/tars-extension/tars_c.c | 1883 -------- .../testcases/AllTypeStruct.php | 100 - php/tars-extension/testcases/NestedStruct.php | 79 - php/tars-extension/testcases/SimpleStruct.php | 36 - php/tars-extension/testcases/test.php | 1343 ------ .../testcases/testTARSClient.php | 848 ---- .../testcases/testTARSServer.php | 830 ---- php/tars-extension/tests/basic/bool.phpt | 41 - php/tars-extension/tests/basic/char_001.phpt | 41 - php/tars-extension/tests/basic/char_002.phpt | 39 - php/tars-extension/tests/basic/char_003.phpt | 39 - php/tars-extension/tests/basic/char_004.phpt | 39 - php/tars-extension/tests/basic/char_005.phpt | 39 - php/tars-extension/tests/basic/double.phpt | 42 - php/tars-extension/tests/basic/float.phpt | 44 - php/tars-extension/tests/basic/int32.phpt | 42 - php/tars-extension/tests/basic/int64.phpt | 42 - php/tars-extension/tests/basic/short.phpt | 40 - .../tests/basic/string_001.phpt | 42 - .../tests/basic/string_002.phpt | 42 - php/tars-extension/tests/basic/uint16.phpt | 42 - php/tars-extension/tests/basic/uint32.phpt | 42 - php/tars-extension/tests/basic/uint8.phpt | 42 - .../tests/basic/wup_exception.phpt | 30 - .../tests/combination/complicate_map.phpt | 69 - .../tests/combination/complicate_struct.phpt | 67 - .../tests/combination/complicate_vector.phpt | 69 - .../tests/combination/map_001.phpt | 48 - .../tests/combination/map_002.phpt | 51 - .../tests/combination/map_003.phpt | 45 - .../tests/combination/map_004.phpt | 45 - .../tests/combination/map_005.phpt | 48 - .../tests/combination/map_006.phpt | 47 - .../tests/combination/map_007.phpt | 41 - .../tests/combination/map_in_vector.phpt | 53 - .../tests/combination/struct_001.phpt | 49 - .../tests/combination/struct_002.phpt | 78 - .../tests/combination/vector_char.phpt | 46 - .../tests/combination/vector_empty.phpt | 42 - .../tests/combination/vector_in_map.phpt | 49 - .../tests/combination/vector_in_map2.phpt | 52 - .../tests/combination/vector_in_map3.phpt | 59 - .../tests/combination/vector_string.phpt | 46 - .../tests/include/AllTypeStruct.php | 100 - .../tests/include/NestedStruct.php | 79 - .../tests/include/SimpleStruct.php | 36 - php/tars-extension/tests/include/config.inc | 10 - .../tests/include/functions.php | 36 - php/tars-extension/tests/include/skipif.inc | 10 - php/tars-extension/ttars.c | 1883 -------- php/tars-extension/tup_c.c | 1057 ----- php/tars-extension/tupapi.c | 2141 --------- php/tars-log/README.md | 43 - php/tars-log/composer.json | 27 - php/tars-log/src/LogServant.php | 88 - php/tars-log/src/classes/LogInfo.php | 93 - php/tars-log/src/tars/LogF.tars | 63 - php/tars-log/tests/demo.php | 19 - php/tars-monitor/README.md | 75 - php/tars-monitor/composer.json | 32 - php/tars-monitor/src/PropertyFServant.php | 55 - php/tars-monitor/src/PropertyFWrapper.php | 91 - php/tars-monitor/src/StatFServant.php | 86 - php/tars-monitor/src/StatFWrapper.php | 257 -- .../src/cache/RedisStoreCache.php | 152 - .../src/cache/SwooleTableStoreCache.php | 138 - php/tars-monitor/src/classes/ProxyInfo.php | 23 - .../src/classes/StatMicMsgBody.php | 66 - .../src/classes/StatMicMsgHead.php | 93 - php/tars-monitor/src/classes/StatPropInfo.php | 30 - .../src/classes/StatPropMsgBody.php | 24 - .../src/classes/StatPropMsgHead.php | 72 - .../src/classes/StatSampleMsg.php | 79 - php/tars-monitor/src/client/Code.php | 132 - php/tars-monitor/src/client/CodeMonitor.php | 132 - php/tars-monitor/src/client/Communicator.php | 376 -- .../src/client/CommunicatorConfig.php | 319 -- .../src/client/CommunicatorMonitor.php | 141 - php/tars-monitor/src/client/Consts.php | 19 - php/tars-monitor/src/client/RequestPacket.php | 53 - .../src/client/RequestPacketMonitor.php | 53 - .../src/client/ResponsePacket.php | 28 - .../src/client/ResponsePacketMonitor.php | 28 - php/tars-monitor/src/client/TUPAPIWrapper.php | 527 --- .../src/client/TUPAPIWrapperMonitor.php | 527 --- .../src/contract/StoreCacheInterface.php | 77 - php/tars-monitor/src/tars/PropertyF.tars | 64 - php/tars-monitor/src/tars/StatF.tars | 92 - php/tars-monitor/tests/propertyFDemo.php | 49 - php/tars-monitor/tests/statFDemo.php | 41 - php/tars-registry/README.md | 51 - php/tars-registry/composer.json | 27 - php/tars-registry/src/EndpointF.php | 135 - php/tars-registry/src/QueryFServant.php | 173 - php/tars-registry/src/QueryFWrapper.php | 65 - php/tars-registry/src/RouteTable.php | 54 - php/tars-registry/src/client/CodeRegistry.php | 132 - .../src/client/CommunicatorRegistry.php | 392 -- .../src/client/RequestPacketRegistry.php | 53 - .../src/client/ResponsePacketRegistry.php | 28 - .../src/client/TUPAPIWrapperRegistry.php | 527 --- php/tars-registry/src/tars/EndpointF.tars | 38 - php/tars-registry/src/tars/QueryF.tars | 85 - php/tars-registry/tests/demo.php | 47 - php/tars-report/README.md | 36 - php/tars-report/composer.json | 26 - php/tars-report/src/NodeF.tars | 43 - php/tars-report/src/ServerF.php | 63 - php/tars-report/src/ServerFAsync.php | 63 - php/tars-report/src/ServerFSync.php | 92 - php/tars-report/src/ServerInfo.php | 44 - php/tars-server/README.md | 118 - php/tars-server/composer.json | 34 - php/tars-server/src/Code.php | 51 - php/tars-server/src/Consts.php | 31 - php/tars-server/src/cmd/Command.php | 42 - php/tars-server/src/cmd/CommandBase.php | 52 - php/tars-server/src/cmd/Restart.php | 28 - php/tars-server/src/cmd/Start.php | 74 - php/tars-server/src/cmd/Stop.php | 49 - php/tars-server/src/core/Event.php | 114 - php/tars-server/src/core/Request.php | 35 - php/tars-server/src/core/Response.php | 57 - php/tars-server/src/core/Server.php | 537 --- php/tars-server/src/protocol/HTTPProtocol.php | 39 - php/tars-server/src/protocol/JSONProtocol.php | 35 - php/tars-server/src/protocol/Protocol.php | 26 - .../src/protocol/ProtocolFactory.php | 27 - php/tars-server/src/protocol/TARSProtocol.php | 465 -- .../Acg.ACGRecomConfigServer.config.conf | 56 - php/tars-server/tests/ClientConf.php | 60 - .../tests/QDPHP.QDPCHttpServer.config.conf | 59 - php/tars-server/tests/test.php | 115 - php/tars-server/tests/testConf.php | 279 -- php/tars-utils/README.md | 66 - php/tars-utils/composer.json | 26 - php/tars-utils/src/Conf.php | 50 - php/tars-utils/src/Utils.php | 375 -- .../tests/QDPHP.TARSServer.config.conf | 64 - php/tars-utils/tests/test.php | 18 - php/tars2php/README.md | 206 - php/tars2php/composer.json | 21 - php/tars2php/src/tars2php.php | 2891 ------------ .../PHPServer/obj/TestTafServiceServant.php | 382 -- .../obj/classes/ComplicatedStruct.php | 47 - .../PHPServer/obj/classes/LotofTags.php | 68 - .../PHPServer/obj/classes/OutStruct.php | 44 - .../PHPServer/obj/classes/SimpleStruct.php | 37 - .../PHPTest/PHPServer/obj/tars/example.tars | 66 - php/tars2php/tests/example.tars | 66 - .../PHPServer/obj/TestTafServiceServant.php | 102 - .../obj/classes/ComplicatedStruct.php | 47 - .../PHPServer/obj/classes/LotofTags.php | 68 - .../PHPServer/obj/classes/OutStruct.php | 44 - .../PHPServer/obj/classes/SimpleStruct.php | 37 - .../PHPTest/PHPServer/obj/tars/example.tars | 66 - php/tars2php/tests/tars.proto.php | 19 - php/tars2php/tests/tars2php.sh | 5 - php/tars2php/tests/tarsclient.proto.php | 19 - plugins/README.md | 1 - plugins/java-protobuf-plugin/README.md | 144 - plugins/java-protobuf-plugin/build.gradle | 120 - .../gradle/wrapper/gradle-wrapper.jar | Bin 54708 -> 0 bytes .../gradle/wrapper/gradle-wrapper.properties | 5 - plugins/java-protobuf-plugin/gradlew | 172 - plugins/java-protobuf-plugin/gradlew.bat | 84 - .../src/java_plugin/cpp/JavaPlugin.cpp | 140 - .../cpp/JavaPrxCallbackGenerator.cpp | 347 -- .../cpp/JavaPrxCallbackGenerator.h | 39 - .../src/java_plugin/cpp/JavaPrxGenerator.cpp | 351 -- .../src/java_plugin/cpp/JavaPrxGenerator.h | 40 - .../java_plugin/cpp/JavaServantGenerator.cpp | 344 -- .../java_plugin/cpp/JavaServantGenerator.h | 39 - .../src/java_plugin/cpp/LogHelper.h | 42 - .../usage/multi/base.proto | 15 - .../usage/multi/service.proto | 9 - .../usage/single/flight.proto | 20 - tup | 1 + tup/README.md | 19 - tup/tup-c/Tars_c.c | 1806 -------- tup/tup-c/Tars_c.h | 457 -- tup/tup-c/demo/Test.tars | 43 - tup/tup-c/demo/main.c | 483 -- tup/tup-c/demo/makefile | 41 - tup/tup-c/tup_c.c | 944 ---- tup/tup-c/tup_c.h | 161 - tup/tup-csharp/.vs/TestTupConsole/v14/.suo | Bin 38400 -> 0 bytes tup/tup-csharp/Program.cs | 636 --- tup/tup-csharp/Properties/AssemblyInfo.cs | 36 - tup/tup-csharp/Test/A.cs | 67 - tup/tup-csharp/Test/B.cs | 78 - tup/tup-csharp/Test/Test.tars | 46 - tup/tup-csharp/Test/TestInfo.cs | 278 -- tup/tup-csharp/Test/TestTupSample1.cs | 278 -- tup/tup-csharp/Test/Test_const.cs | 31 - tup/tup-csharp/TestTupConsole.csproj | 78 - tup/tup-csharp/TestTupConsole.sln | 20 - tup/tup-csharp/makefile | 33 - tup/tup-csharp/tars/TarsDecodeException.cs | 30 - tup/tup-csharp/tars/TarsDisplayer.cs | 470 -- tup/tup-csharp/tars/TarsEncodeException.cs | 30 - tup/tup-csharp/tars/TarsInputStream.cs | 1330 ------ tup/tup-csharp/tars/TarsOutputStream.cs | 728 --- tup/tup-csharp/tars/TarsStruct.cs | 52 - tup/tup-csharp/tars/TarsUtil.cs | 516 --- tup/tup-csharp/tup/Const.cs | 36 - tup/tup-csharp/tup/RequestPacket.cs | 113 - tup/tup-csharp/tup/TarsUniPacket.cs | 180 - tup/tup-csharp/tup/UniAttribute.cs | 495 --- tup/tup-csharp/tup/UniPacket.cs | 215 - tup/tup-csharp/util/BasicClassTypeUtil.cs | 208 - tup/tup-csharp/util/ByteConverter.cs | 242 - tup/tup-csharp/util/HexUtil.cs | 141 - tup/tup-csharp/util/ObjectCreateException.cs | 29 - tup/tup-csharp/util/QTrace.cs | 117 - tup/tup-csharp/util/Utility.cs | 117 - tup/tup-oc/Examples/Adapters/MttHDVerifyRes.h | 31 - tup/tup-oc/Examples/Adapters/MttHDVerifyRes.m | 1310 ------ .../Examples/Adapters/MttLoginReq+Adapter.h | 34 - .../Examples/Adapters/MttLoginReq+Adapter.m | 79 - .../Adapters/MttPageConfigReq+Adapter.h | 31 - .../Adapters/MttPageConfigReq+Adapter.m | 115 - .../Adapters/MttReleaseInfo+Adapter.h | 34 - .../Adapters/MttReleaseInfo+Adapter.m | 96 - .../Examples/Adapters/MttSTStat+Adapter.h | 38 - .../Examples/Adapters/MttSTStat+Adapter.m | 66 - .../Examples/Adapters/MttSplashReq+Adapter.h | 31 - .../Examples/Adapters/MttSplashReq+Adapter.m | 42 - .../Examples/Adapters/MttUserBase+Adapter.h | 31 - .../Examples/Adapters/MttUserBase+Adapter.m | 44 - .../Examples/Adapters/MttVerifyReq+Adapter.h | 31 - .../Examples/Adapters/MttVerifyReq+Adapter.m | 37 - .../Examples/BusinessData/MttHDLoginInfo.h | 40 - .../Examples/BusinessData/MttHDLoginInfo.m | 155 - .../BusinessData/MttHDPageConfigInfo.h | 51 - .../BusinessData/MttHDPageConfigInfo.m | 163 - .../BusinessData/MttHDPersistentObject.h | 32 - .../BusinessData/MttHDPersistentObject.m | 51 - .../Examples/BusinessData/MttHDSplashInfo.h | 38 - .../Examples/BusinessData/MttHDSplashInfo.m | 104 - .../Examples/BusinessData/MttHDStatInfo.h | 144 - .../Examples/BusinessData/MttHDStatInfo.m | 406 -- .../Examples/BusinessData/MttHDVerifyInfo.h | 34 - .../Examples/BusinessData/MttHDVerifyInfo.m | 54 - .../Examples/BusinessData/MttHDVideoInfo.h | 88 - .../Examples/BusinessData/MttHDVideoInfo.m | 316 -- tup/tup-oc/Examples/MttHDWupAdapters.h | 27 - tup/tup-oc/Examples/MttHDWupBusinessData.h | 29 - tup/tup-oc/Examples/MttHDWupEnvironment.h | 61 - tup/tup-oc/Examples/MttHDWupEnvironment.m | 195 - tup/tup-oc/Examples/MttHDWupManager.h | 60 - tup/tup-oc/Examples/MttHDWupManager.m | 149 - tup/tup-oc/Examples/MttHDWupRMIServices.h | 25 - .../Protocol/MttBrokerSecurityRequest.h | 47 - .../Protocol/MttBrokerSecurityRequest.m | 64 - .../Protocol/MttBrokerSecurityResponse.h | 33 - .../Protocol/MttBrokerSecurityResponse.m | 52 - tup/tup-oc/Examples/Protocol/MttVideoApp.h | 80 - tup/tup-oc/Examples/Protocol/MttVideoApp.m | 229 - tup/tup-oc/Examples/Protocol/MttVideoReq.h | 39 - tup/tup-oc/Examples/Protocol/MttVideoReq.m | 65 - .../Examples/RMIAgents/MttConfigAgent.h | 39 - .../Examples/RMIAgents/MttConfigAgent.m | 84 - .../Examples/RMIAgents/MttCoolReadAgent.h | 34 - .../Examples/RMIAgents/MttCoolReadAgent.m | 62 - tup/tup-oc/Examples/RMIAgents/MttLoginAgent.h | 36 - tup/tup-oc/Examples/RMIAgents/MttLoginAgent.m | 83 - .../Examples/RMIAgents/MttSecurityAgent.h | 34 - .../Examples/RMIAgents/MttSecurityAgent.m | 64 - tup/tup-oc/Examples/RMIAgents/MttStatAgent.h | 33 - tup/tup-oc/Examples/RMIAgents/MttStatAgent.m | 69 - .../Examples/RMIAgents/MttVerifyAgent.h | 34 - .../Examples/RMIAgents/MttVerifyAgent.m | 69 - .../Examples/RMIServices/MttConfigService.h | 33 - .../Examples/RMIServices/MttConfigService.m | 124 - .../Examples/RMIServices/MttCoolReadService.h | 34 - .../Examples/RMIServices/MttCoolReadService.m | 126 - .../Examples/RMIServices/MttLoginService.h | 33 - .../Examples/RMIServices/MttLoginService.m | 90 - .../Examples/RMIServices/MttSecurityService.h | 34 - .../Examples/RMIServices/MttSecurityService.m | 64 - .../Examples/RMIServices/MttStatService.h | 32 - .../Examples/RMIServices/MttStatService.m | 62 - .../Examples/RMIServices/MttVerifyService.h | 32 - .../Examples/RMIServices/MttVerifyService.m | 74 - .../ASIHttpRequest/ASICacheDelegate.h | 119 - .../ASIHttpRequest/ASIHTTPRequest.h | 1020 ----- .../ASIHttpRequest/ASIHTTPRequestConfig.h | 59 - .../ASIHttpRequest/ASIHTTPRequestDelegate.h | 51 - .../ASIHttpRequest/ASIProgressDelegate.h | 54 - tup/tup-oc/tup/tup.xcodeproj/project.pbxproj | 1678 ------- .../contents.xcworkspacedata | 7 - tup/tup-oc/tup/tup/RMIModel/WupAgent.h | 50 - tup/tup-oc/tup/tup/RMIModel/WupAgent.m | 147 - .../tup/tup/RMIModel/WupAgentDelegate.h | 41 - tup/tup-oc/tup/tup/RMIModel/WupHttpRequest.h | 37 - tup/tup-oc/tup/tup/RMIModel/WupHttpRequest.m | 142 - tup/tup-oc/tup/tup/RMIModel/WupService.h | 37 - tup/tup-oc/tup/tup/RMIModel/WupService.m | 61 - .../tup/tup/Serializable/JceEnumHelper.h | 44 - .../tup/tup/Serializable/JceEnumHelper.m | 71 - .../tup/tup/Serializable/JceInputStream.h | 60 - .../tup/tup/Serializable/JceInputStream.m | 514 --- tup/tup-oc/tup/tup/Serializable/JceObject.h | 67 - tup/tup-oc/tup/tup/Serializable/JceObject.m | 161 - tup/tup-oc/tup/tup/Serializable/JceObjectV2.h | 83 - tup/tup-oc/tup/tup/Serializable/JceObjectV2.m | 294 -- .../tup/tup/Serializable/JceOutputStream.h | 48 - .../tup/tup/Serializable/JceOutputStream.m | 362 -- tup/tup-oc/tup/tup/Serializable/JceStream.h | 93 - tup/tup-oc/tup/tup/Serializable/JceStream.m | 109 - tup/tup-oc/tup/tup/Serializable/RequestF.h | 44 - tup/tup-oc/tup/tup/Serializable/RequestF.mm | 94 - .../tup/tup/Serializable/UniAttribute.h | 98 - .../tup/tup/Serializable/UniAttribute.m | 279 -- tup/tup-oc/tup/tup/Serializable/UniPacket.h | 42 - tup/tup-oc/tup/tup/Serializable/UniPacket.mm | 143 - .../tup/tup/WirelessUnifiedProtocol-Debug.h | 37 - .../tup/WirelessUnifiedProtocol-Prefix.pch | 24 - tup/tup-oc/tup/tup/WirelessUnifiedProtocol.h | 29 - .../tupTests/Protocol/Mtt/MttAcceptEncoding.h | 40 - .../tupTests/Protocol/Mtt/MttAcceptEncoding.m | 48 - .../tup/tupTests/Protocol/Mtt/MttAdsContent.h | 36 - .../tup/tupTests/Protocol/Mtt/MttAdsContent.m | 65 - .../tup/tupTests/Protocol/Mtt/MttAdsReq.h | 32 - .../tup/tupTests/Protocol/Mtt/MttAdsReq.m | 56 - .../tup/tupTests/Protocol/Mtt/MttAdsRsp.h | 35 - .../tup/tupTests/Protocol/Mtt/MttAdsRsp.m | 60 - .../Protocol/Mtt/MttBrokerContentInfo.h | 38 - .../Protocol/Mtt/MttBrokerContentInfo.m | 66 - .../Protocol/Mtt/MttBrokerDisplayInfo.h | 34 - .../Protocol/Mtt/MttBrokerDisplayInfo.m | 63 - .../Protocol/Mtt/MttBrokerFragRequest.h | 35 - .../Protocol/Mtt/MttBrokerFragRequest.m | 65 - .../Protocol/Mtt/MttBrokerFragResponse.h | 34 - .../Protocol/Mtt/MttBrokerFragResponse.m | 62 - .../tupTests/Protocol/Mtt/MttBrokerJSPDInfo.h | 30 - .../tupTests/Protocol/Mtt/MttBrokerJSPDInfo.m | 56 - .../Protocol/Mtt/MttBrokerJSRequest.h | 36 - .../Protocol/Mtt/MttBrokerJSRequest.m | 66 - .../Protocol/Mtt/MttBrokerPageRequest.h | 33 - .../Protocol/Mtt/MttBrokerPageRequest.m | 63 - .../tupTests/Protocol/Mtt/MttBrokerPicParam.h | 30 - .../tupTests/Protocol/Mtt/MttBrokerPicParam.m | 58 - .../Protocol/Mtt/MttBrokerPicRequest.h | 37 - .../Protocol/Mtt/MttBrokerPicRequest.m | 70 - .../Protocol/Mtt/MttBrokerPushRequest.h | 34 - .../Protocol/Mtt/MttBrokerPushRequest.m | 63 - .../Protocol/Mtt/MttBrokerPushResponse.h | 31 - .../Protocol/Mtt/MttBrokerPushResponse.m | 57 - .../tupTests/Protocol/Mtt/MttBrokerReqType.h | 41 - .../tupTests/Protocol/Mtt/MttBrokerReqType.m | 50 - .../Protocol/Mtt/MttBrokerRequestParam.h | 53 - .../Protocol/Mtt/MttBrokerRequestParam.m | 92 - .../Protocol/Mtt/MttBrokerSecurityRequest.h | 33 - .../Protocol/Mtt/MttBrokerSecurityRequest.m | 62 - .../Protocol/Mtt/MttBrokerSecurityResponse.h | 31 - .../Protocol/Mtt/MttBrokerSecurityResponse.m | 58 - .../Mtt/MttBrokerSimplifiedPushRequest.h | 44 - .../Mtt/MttBrokerSimplifiedPushRequest.m | 86 - .../Mtt/MttBrokerSimplifiedPushResponse.h | 36 - .../Mtt/MttBrokerSimplifiedPushResponse.m | 67 - .../Protocol/Mtt/MttBrokerSimplifiedRequest.h | 71 - .../Protocol/Mtt/MttBrokerSimplifiedRequest.m | 134 - .../Protocol/Mtt/MttBrokerTransferResponse.h | 42 - .../Protocol/Mtt/MttBrokerTransferResponse.m | 78 - .../Protocol/Mtt/MttBrokerUploadRequest.h | 42 - .../Protocol/Mtt/MttBrokerUploadRequest.m | 80 - .../tupTests/Protocol/Mtt/MttBrokerUserInfo.h | 46 - .../tupTests/Protocol/Mtt/MttBrokerUserInfo.m | 96 - .../tup/tupTests/Protocol/Mtt/MttBrokerWups.h | 32 - .../tup/tupTests/Protocol/Mtt/MttBrokerWups.m | 59 - .../tup/tupTests/Protocol/Mtt/MttBrowserReq.h | 33 - .../tup/tupTests/Protocol/Mtt/MttBrowserReq.m | 60 - .../tup/tupTests/Protocol/Mtt/MttBrowserRsp.h | 37 - .../tup/tupTests/Protocol/Mtt/MttBrowserRsp.m | 69 - .../tupTests/Protocol/Mtt/MttBrowserVersion.h | 43 - .../tupTests/Protocol/Mtt/MttBrowserVersion.m | 54 - .../tupTests/Protocol/Mtt/MttCheckPluginReq.h | 32 - .../tupTests/Protocol/Mtt/MttCheckPluginReq.m | 60 - .../tupTests/Protocol/Mtt/MttCityListReq.h | 29 - .../tupTests/Protocol/Mtt/MttCityListReq.m | 54 - .../tupTests/Protocol/Mtt/MttCityListRsp.h | 30 - .../tupTests/Protocol/Mtt/MttCityListRsp.m | 56 - .../tup/tupTests/Protocol/Mtt/MttConfInfo.h | 30 - .../tup/tupTests/Protocol/Mtt/MttConfInfo.m | 58 - .../tup/tupTests/Protocol/Mtt/MttConfRsp.h | 31 - .../tup/tupTests/Protocol/Mtt/MttConfRsp.m | 56 - .../tup/tupTests/Protocol/Mtt/MttConfigReq.h | 36 - .../tup/tupTests/Protocol/Mtt/MttConfigReq.m | 68 - .../tup/tupTests/Protocol/Mtt/MttConfigRsp.h | 40 - .../tup/tupTests/Protocol/Mtt/MttConfigRsp.m | 75 - .../tup/tupTests/Protocol/Mtt/MttConfirm.h | 32 - .../tup/tupTests/Protocol/Mtt/MttConfirm.m | 58 - .../Protocol/Mtt/MttContentEncoding.h | 40 - .../Protocol/Mtt/MttContentEncoding.m | 48 - .../tup/tupTests/Protocol/Mtt/MttDNSReq.h | 30 - .../tup/tupTests/Protocol/Mtt/MttDNSReq.m | 56 - .../tup/tupTests/Protocol/Mtt/MttDNSRsp.h | 29 - .../tup/tupTests/Protocol/Mtt/MttDNSRsp.m | 55 - .../tupTests/Protocol/Mtt/MttDirectPolicy.h | 40 - .../tupTests/Protocol/Mtt/MttDirectPolicy.m | 48 - .../tup/tupTests/Protocol/Mtt/MttDomainRsp.h | 35 - .../tup/tupTests/Protocol/Mtt/MttDomainRsp.m | 65 - .../tupTests/Protocol/Mtt/MttDomainRspNew.h | 33 - .../tupTests/Protocol/Mtt/MttDomainRspNew.m | 59 - .../tup/tupTests/Protocol/Mtt/MttDynamicReq.h | 32 - .../tup/tupTests/Protocol/Mtt/MttDynamicReq.m | 60 - .../tup/tupTests/Protocol/Mtt/MttDynamicRsp.h | 30 - .../tup/tupTests/Protocol/Mtt/MttDynamicRsp.m | 56 - .../tupTests/Protocol/Mtt/MttEDomainType.h | 83 - .../tupTests/Protocol/Mtt/MttEDomainType.m | 134 - .../tup/tupTests/Protocol/Mtt/MttEEntryType.h | 49 - .../tup/tupTests/Protocol/Mtt/MttEEntryType.m | 66 - .../tup/tupTests/Protocol/Mtt/MttEFASTKEY.h | 190 - .../tup/tupTests/Protocol/Mtt/MttEFASTKEY.m | 348 -- .../tupTests/Protocol/Mtt/MttEHotWordType.h | 47 - .../tupTests/Protocol/Mtt/MttEHotWordType.m | 62 - .../tupTests/Protocol/Mtt/MttELanguageType.h | 56 - .../tupTests/Protocol/Mtt/MttELanguageType.m | 80 - .../Protocol/Mtt/MttEPushServerType.h | 46 - .../Protocol/Mtt/MttEPushServerType.m | 60 - .../tupTests/Protocol/Mtt/MttERelayProtoVer.h | 40 - .../tupTests/Protocol/Mtt/MttERelayProtoVer.m | 48 - .../tup/tupTests/Protocol/Mtt/MttEServerVer.h | 54 - .../tup/tupTests/Protocol/Mtt/MttEServerVer.m | 76 - .../tup/tupTests/Protocol/Mtt/MttETPV.h | 31 - .../tup/tupTests/Protocol/Mtt/MttETPV.m | 59 - .../tup/tupTests/Protocol/Mtt/MttEUSESTAT.h | 44 - .../tup/tupTests/Protocol/Mtt/MttEUSESTAT.m | 56 - .../tupTests/Protocol/Mtt/MttEWaitPushType.h | 41 - .../tupTests/Protocol/Mtt/MttEWaitPushType.m | 50 - .../tup/tupTests/Protocol/Mtt/MttErrorCode.h | 41 - .../tup/tupTests/Protocol/Mtt/MttErrorCode.m | 50 - .../tup/tupTests/Protocol/Mtt/MttExtDomain.h | 31 - .../tup/tupTests/Protocol/Mtt/MttExtDomain.m | 56 - .../tup/tupTests/Protocol/Mtt/MttFKINFO.h | 32 - .../tup/tupTests/Protocol/Mtt/MttFKINFO.m | 58 - .../tup/tupTests/Protocol/Mtt/MttHotListReq.h | 31 - .../tup/tupTests/Protocol/Mtt/MttHotListReq.m | 59 - .../tup/tupTests/Protocol/Mtt/MttHotListRsp.h | 32 - .../tup/tupTests/Protocol/Mtt/MttHotListRsp.m | 59 - .../tupTests/Protocol/Mtt/MttHotWordInfo.h | 31 - .../tupTests/Protocol/Mtt/MttHotWordInfo.m | 59 - .../tupTests/Protocol/Mtt/MttHotWordsReq.h | 30 - .../tupTests/Protocol/Mtt/MttHotWordsReq.m | 56 - .../tupTests/Protocol/Mtt/MttHotWordsRsp.h | 30 - .../tupTests/Protocol/Mtt/MttHotWordsRsp.m | 58 - .../tup/tupTests/Protocol/Mtt/MttIPListReq.h | 31 - .../tup/tupTests/Protocol/Mtt/MttIPListReq.m | 57 - .../tup/tupTests/Protocol/Mtt/MttIPListRsp.h | 31 - .../tup/tupTests/Protocol/Mtt/MttIPListRsp.m | 58 - .../tup/tupTests/Protocol/Mtt/MttIcon.h | 30 - .../tup/tupTests/Protocol/Mtt/MttIcon.m | 58 - .../tup/tupTests/Protocol/Mtt/MttIconInfo.h | 31 - .../tup/tupTests/Protocol/Mtt/MttIconInfo.m | 59 - .../tup/tupTests/Protocol/Mtt/MttIconRsp.h | 31 - .../tup/tupTests/Protocol/Mtt/MttIconRsp.m | 57 - .../tupTests/Protocol/Mtt/MttImageQuality.h | 45 - .../tupTests/Protocol/Mtt/MttImageQuality.m | 58 - .../tup/tupTests/Protocol/Mtt/MttImageReq.h | 39 - .../tup/tupTests/Protocol/Mtt/MttImageReq.m | 72 - .../tup/tupTests/Protocol/Mtt/MttImageRsp.h | 29 - .../tup/tupTests/Protocol/Mtt/MttImageRsp.m | 53 - .../tup/tupTests/Protocol/Mtt/MttImgReq.h | 35 - .../tup/tupTests/Protocol/Mtt/MttImgReq.m | 62 - .../tup/tupTests/Protocol/Mtt/MttImgRsp.h | 32 - .../tup/tupTests/Protocol/Mtt/MttImgRsp.m | 59 - .../tup/tupTests/Protocol/Mtt/MttItemReq.h | 30 - .../tup/tupTests/Protocol/Mtt/MttItemReq.m | 54 - .../tup/tupTests/Protocol/Mtt/MttItemRsp.h | 31 - .../tup/tupTests/Protocol/Mtt/MttItemRsp.m | 57 - .../tup/tupTests/Protocol/Mtt/MttKeywordRsp.h | 30 - .../tup/tupTests/Protocol/Mtt/MttKeywordRsp.m | 56 - .../tup/tupTests/Protocol/Mtt/MttLPageReq.h | 29 - .../tup/tupTests/Protocol/Mtt/MttLPageReq.m | 53 - .../tup/tupTests/Protocol/Mtt/MttLPageRsp.h | 32 - .../tup/tupTests/Protocol/Mtt/MttLPageRsp.m | 61 - .../tup/tupTests/Protocol/Mtt/MttLoginReq.h | 47 - .../tup/tupTests/Protocol/Mtt/MttLoginReq.m | 84 - .../tup/tupTests/Protocol/Mtt/MttLoginRsp.h | 47 - .../tup/tupTests/Protocol/Mtt/MttLoginRsp.m | 88 - .../tupTests/Protocol/Mtt/MttMTTContentType.h | 46 - .../tupTests/Protocol/Mtt/MttMTTContentType.m | 60 - .../tup/tupTests/Protocol/Mtt/MttMethod.h | 42 - .../tup/tupTests/Protocol/Mtt/MttMethod.m | 52 - .../tup/tupTests/Protocol/Mtt/MttNaviRsp.h | 32 - .../tup/tupTests/Protocol/Mtt/MttNaviRsp.m | 58 - .../tup/tupTests/Protocol/Mtt/MttNavigation.h | 33 - .../tup/tupTests/Protocol/Mtt/MttNavigation.m | 64 - .../tupTests/Protocol/Mtt/MttPageBrowserReq.h | 39 - .../tupTests/Protocol/Mtt/MttPageBrowserReq.m | 74 - .../tupTests/Protocol/Mtt/MttPageBrowserRsp.h | 34 - .../tupTests/Protocol/Mtt/MttPageBrowserRsp.m | 64 - .../tupTests/Protocol/Mtt/MttPageConfigReq.h | 42 - .../tupTests/Protocol/Mtt/MttPageConfigReq.m | 77 - .../tupTests/Protocol/Mtt/MttPageConfigRsp.h | 54 - .../tupTests/Protocol/Mtt/MttPageConfigRsp.m | 96 - .../tup/tupTests/Protocol/Mtt/MttPageReq.h | 32 - .../tup/tupTests/Protocol/Mtt/MttPageReq.m | 58 - .../tup/tupTests/Protocol/Mtt/MttPageRsp.h | 35 - .../tup/tupTests/Protocol/Mtt/MttPageRsp.m | 65 - .../Protocol/Mtt/MttPerformanceInfo.h | 38 - .../Protocol/Mtt/MttPerformanceInfo.m | 68 - .../tup/tupTests/Protocol/Mtt/MttPicInfo.h | 30 - .../tup/tupTests/Protocol/Mtt/MttPicInfo.m | 58 - .../tup/tupTests/Protocol/Mtt/MttPicRsp.h | 31 - .../tup/tupTests/Protocol/Mtt/MttPicRsp.m | 57 - .../tup/tupTests/Protocol/Mtt/MttPing.h | 29 - .../tup/tupTests/Protocol/Mtt/MttPing.m | 54 - .../tupTests/Protocol/Mtt/MttPluginBootResp.h | 30 - .../tupTests/Protocol/Mtt/MttPluginBootResp.m | 55 - .../tup/tupTests/Protocol/Mtt/MttPluginInfo.h | 36 - .../tup/tupTests/Protocol/Mtt/MttPluginInfo.m | 66 - .../tupTests/Protocol/Mtt/MttPluginInfoNew.h | 43 - .../tupTests/Protocol/Mtt/MttPluginInfoNew.m | 92 - .../tup/tupTests/Protocol/Mtt/MttPluginRsp.h | 33 - .../tup/tupTests/Protocol/Mtt/MttPluginRsp.m | 59 - .../tupTests/Protocol/Mtt/MttPluginRspNew.h | 30 - .../tupTests/Protocol/Mtt/MttPluginRspNew.m | 56 - .../tupTests/Protocol/Mtt/MttPluginVersion.h | 31 - .../tupTests/Protocol/Mtt/MttPluginVersion.m | 60 - .../tupTests/Protocol/Mtt/MttPushAppReqList.h | 35 - .../tupTests/Protocol/Mtt/MttPushAppReqList.m | 63 - .../Protocol/Mtt/MttPushPortionFresh.h | 29 - .../Protocol/Mtt/MttPushPortionFresh.m | 55 - .../Protocol/Mtt/MttPushRespnoseContent.h | 38 - .../Protocol/Mtt/MttPushRespnoseContent.m | 63 - .../tupTests/Protocol/Mtt/MttPushSysNotify.h | 31 - .../tupTests/Protocol/Mtt/MttPushSysNotify.m | 57 - .../tup/tupTests/Protocol/Mtt/MttRefuse.h | 32 - .../tup/tupTests/Protocol/Mtt/MttRefuse.m | 58 - .../tupTests/Protocol/Mtt/MttRelayPageReq.h | 49 - .../tupTests/Protocol/Mtt/MttRelayPageReq.m | 88 - .../tupTests/Protocol/Mtt/MttRelayPageRsp.h | 33 - .../tupTests/Protocol/Mtt/MttRelayPageRsp.m | 60 - .../tup/tupTests/Protocol/Mtt/MttReply.h | 35 - .../tup/tupTests/Protocol/Mtt/MttReply.m | 61 - .../tup/tupTests/Protocol/Mtt/MttRequest.h | 39 - .../tup/tupTests/Protocol/Mtt/MttRequest.m | 64 - .../Protocol/Mtt/MttRequestFragment.h | 35 - .../Protocol/Mtt/MttRequestFragment.m | 61 - .../tupTests/Protocol/Mtt/MttSTImagePolicy.h | 29 - .../tupTests/Protocol/Mtt/MttSTImagePolicy.m | 53 - .../tup/tupTests/Protocol/Mtt/MttSTJSInfo.h | 32 - .../tup/tupTests/Protocol/Mtt/MttSTJSInfo.m | 58 - .../tup/tupTests/Protocol/Mtt/MttSTJSPDInfo.h | 30 - .../tup/tupTests/Protocol/Mtt/MttSTJSPDInfo.m | 56 - .../tup/tupTests/Protocol/Mtt/MttSTOperator.h | 36 - .../tup/tupTests/Protocol/Mtt/MttSTOperator.m | 66 - .../tupTests/Protocol/Mtt/MttSTOperatorRsp.h | 31 - .../tupTests/Protocol/Mtt/MttSTOperatorRsp.m | 55 - .../tup/tupTests/Protocol/Mtt/MttSTPV.h | 33 - .../tup/tupTests/Protocol/Mtt/MttSTPV.m | 59 - .../tupTests/Protocol/Mtt/MttSTSingleMD5.h | 29 - .../tupTests/Protocol/Mtt/MttSTSingleMD5.m | 54 - .../tupTests/Protocol/Mtt/MttSTSpeedStatis.h | 31 - .../tupTests/Protocol/Mtt/MttSTSpeedStatis.m | 55 - .../tup/tupTests/Protocol/Mtt/MttSTStat.h | 48 - .../tup/tupTests/Protocol/Mtt/MttSTStat.m | 89 - .../tup/tupTests/Protocol/Mtt/MttSTTime.h | 30 - .../tup/tupTests/Protocol/Mtt/MttSTTime.m | 54 - .../tup/tupTests/Protocol/Mtt/MttSTTotal.h | 36 - .../tup/tupTests/Protocol/Mtt/MttSTTotal.m | 62 - .../tup/tupTests/Protocol/Mtt/MttSearchInfo.h | 36 - .../tup/tupTests/Protocol/Mtt/MttSearchInfo.m | 73 - .../tup/tupTests/Protocol/Mtt/MttSearchReq.h | 30 - .../tup/tupTests/Protocol/Mtt/MttSearchReq.m | 56 - .../tup/tupTests/Protocol/Mtt/MttSearchRsp.h | 34 - .../tup/tupTests/Protocol/Mtt/MttSearchRsp.m | 62 - .../tupTests/Protocol/Mtt/MttSearchTypeInfo.h | 31 - .../tupTests/Protocol/Mtt/MttSearchTypeInfo.m | 58 - .../tupTests/Protocol/Mtt/MttSearchTypeReq.h | 30 - .../tupTests/Protocol/Mtt/MttSearchTypeReq.m | 56 - .../tupTests/Protocol/Mtt/MttSearchTypeRsp.h | 34 - .../tupTests/Protocol/Mtt/MttSearchTypeRsp.m | 60 - .../Protocol/Mtt/MttSecurityCheckType.h | 40 - .../Protocol/Mtt/MttSecurityCheckType.m | 48 - .../Protocol/Mtt/MttSoftAnalyseInfo.h | 34 - .../Protocol/Mtt/MttSoftAnalyseInfo.m | 66 - .../tupTests/Protocol/Mtt/MttSpecialValue.h | 41 - .../tupTests/Protocol/Mtt/MttSpecialValue.m | 50 - .../tup/tupTests/Protocol/Mtt/MttSplashReq.h | 35 - .../tup/tupTests/Protocol/Mtt/MttSplashReq.m | 65 - .../tup/tupTests/Protocol/Mtt/MttSplashRsp.h | 36 - .../tup/tupTests/Protocol/Mtt/MttSplashRsp.m | 68 - .../tup/tupTests/Protocol/Mtt/MttStatValue.h | 45 - .../tup/tupTests/Protocol/Mtt/MttStatValue.m | 58 - .../Protocol/Mtt/MttStaticConfigReq.h | 37 - .../Protocol/Mtt/MttStaticConfigReq.m | 69 - .../tup/tupTests/Protocol/Mtt/MttThirdUse.h | 33 - .../tup/tupTests/Protocol/Mtt/MttThirdUse.m | 61 - .../tup/tupTests/Protocol/Mtt/MttTipPushReq.h | 29 - .../tup/tupTests/Protocol/Mtt/MttTipPushReq.m | 55 - .../tup/tupTests/Protocol/Mtt/MttTipPushRsp.h | 35 - .../tup/tupTests/Protocol/Mtt/MttTipPushRsp.m | 66 - .../tup/tupTests/Protocol/Mtt/MttURLPV.h | 31 - .../tup/tupTests/Protocol/Mtt/MttURLPV.m | 55 - .../tup/tupTests/Protocol/Mtt/MttUpdateRsp.h | 33 - .../tup/tupTests/Protocol/Mtt/MttUpdateRsp.m | 63 - .../tup/tupTests/Protocol/Mtt/MttUserBase.h | 46 - .../tup/tupTests/Protocol/Mtt/MttUserBase.m | 98 - .../tup/tupTests/Protocol/Mtt/MttVerifyReq.h | 30 - .../tup/tupTests/Protocol/Mtt/MttVerifyReq.m | 56 - .../tup/tupTests/Protocol/Mtt/MttVerifyRsp.h | 32 - .../tup/tupTests/Protocol/Mtt/MttVerifyRsp.m | 58 - .../tupTests/Protocol/Mtt/MttVerifyRspExt.h | 32 - .../tupTests/Protocol/Mtt/MttVerifyRspExt.m | 58 - .../tupTests/Protocol/Mtt/MttWeatherInfo.h | 39 - .../tupTests/Protocol/Mtt/MttWeatherInfo.m | 75 - .../Protocol/Mtt/MttWeatherInfo4Mini.h | 33 - .../Protocol/Mtt/MttWeatherInfo4Mini.m | 63 - .../tup/tupTests/Protocol/Mtt/MttWeatherReq.h | 32 - .../tup/tupTests/Protocol/Mtt/MttWeatherReq.m | 58 - .../tup/tupTests/Protocol/Mtt/MttWeatherRsp.h | 32 - .../tup/tupTests/Protocol/Mtt/MttWeatherRsp.m | 59 - .../Protocol/Mtt/MttWeatherRsp4Mini.h | 32 - .../Protocol/Mtt/MttWeatherRsp4Mini.m | 59 - .../WirelessUnifiedProtocolTests-Info.plist | 22 - .../tupTests/WirelessUnifiedProtocolTests.h | 29 - .../tupTests/WirelessUnifiedProtocolTests.m | 48 - .../tup/tupTests/en.lproj/InfoPlist.strings | 2 - tup/tup-php/RequestF_tup.php | 151 - tup/tup-php/demo/Hello.tars | 43 - tup/tup-php/demo/Hello_tup.php | 173 - tup/tup-php/demo/demo.php | 279 -- tup/tup-php/makefile | 28 - tup/tup-php/tars.php | 1166 ----- tup/tup-php/tars_exception.php | 32 - tup/tup-php/tars_header.php | 150 - tup/tup-php/tup.php | 313 -- tup/tup-python/__init__.py | 0 tup/tup-python/jmain.py | 49 - tup/tup-python/makefile | 24 - tup/tup-python/tars/EndpointF.py | 69 - tup/tup-python/tars/QueryF.py | 276 -- tup/tup-python/tars/__TimeoutQueue.py | 300 -- tup/tup-python/tars/__adapterproxy.py | 703 --- tup/tup-python/tars/__async.py | 201 - tup/tup-python/tars/__init__.py | 0 tup/tup-python/tars/__logger.py | 103 - tup/tup-python/tars/__packet.py | 104 - tup/tup-python/tars/__rpc.py | 441 -- tup/tup-python/tars/__servantproxy.py | 358 -- tup/tup-python/tars/__tars.py | 546 --- tup/tup-python/tars/__trans.py | 575 --- tup/tup-python/tars/__tup.py | 118 - tup/tup-python/tars/__util.py | 252 -- tup/tup-python/tars/core.py | 91 - tup/tup-python/tars/exception.py | 36 - tup/tup-python/tars/tars/EndpointF.tars | 25 - tup/tup-python/tars/tars/QueryF.tars | 70 - tup/tup-python/tars/tars/__init__.py | 0 tup/tup-python/test_tars2python.py | 143 - tup/tup-python/wmain.py | 43 - tup/tup-windows-cpp/RequestF.h | 224 - tup/tup-windows-cpp/Tars.h | 1357 ------ tup/tup-windows-cpp/TarsDisplayer.h | 379 -- tup/tup-windows-cpp/TarsType.h | 221 - tup/tup-windows-cpp/stdint.h | 216 - tup/tup-windows-cpp/tup.h | 672 --- web | 1 + web/docs/API.md | 1236 ------ web/docs/README.md | 13 - web/pom.xml | 305 -- .../com/qq/common/BindingResultMessage.java | 37 - web/src/main/java/com/qq/common/Charsets.java | 22 - .../com/qq/common/CodeEnableException.java | 126 - .../qq/common/CodeEnableRuntimeException.java | 126 - .../main/java/com/qq/common/CodeEnabled.java | 25 - .../ExceptionMessageTemplateManager.java | 66 - .../qq/common/ExceptionMessageTempldate.java | 76 - .../java/com/qq/common/ExceptionResponse.java | 87 - .../qq/common/ExceptionResponseAttached.java | 70 - .../common/ResponseBodyMessageConverter.java | 72 - .../ServletRequestParameterException.java | 38 - .../StringFreeMarkerTemplateLoader.java | 52 - .../main/java/com/qq/common/TCPClient.java | 87 - .../java/com/qq/common/TimestampString.java | 42 - .../java/com/qq/common/WrappedController.java | 121 - .../qq/common/YesOrNoBooleanConverter.java | 42 - .../java/com/qq/tars/db/AdapterMapper.java | 48 - .../java/com/qq/tars/db/ConfigMapper.java | 87 - .../java/com/qq/tars/db/NotifyMapper.java | 33 - .../main/java/com/qq/tars/db/PatchMapper.java | 34 - .../java/com/qq/tars/db/ServerMapper.java | 73 - .../main/java/com/qq/tars/db/TaskMapper.java | 34 - .../java/com/qq/tars/db/TemplateMapper.java | 45 - .../java/com/qq/tars/entity/AdapterConf.java | 192 - .../java/com/qq/tars/entity/ConfigFile.java | 144 - .../com/qq/tars/entity/ConfigFileHistory.java | 92 - .../java/com/qq/tars/entity/ConfigRef.java | 50 - .../java/com/qq/tars/entity/ConfigRefExt.java | 50 - .../com/qq/tars/entity/ProfileTemplate.java | 82 - .../java/com/qq/tars/entity/ServerConf.java | 392 -- .../java/com/qq/tars/entity/ServerNotify.java | 92 - .../java/com/qq/tars/entity/ServerPatch.java | 92 - .../main/java/com/qq/tars/entity/Task.java | 50 - .../java/com/qq/tars/entity/TaskItem.java | 130 - .../exception/DBConnectTimeoutException.java | 42 - .../DBConnectTimeoutRuntimeException.java | 42 - .../exception/DBConsistencyException.java | 42 - .../DBConsistencyRuntimeException.java | 42 - .../qq/tars/exception/DBOtherException.java | 42 - .../exception/DBOtherRuntimeException.java | 42 - .../com/qq/tars/exception/DBSqlException.java | 42 - .../tars/exception/DBSqlRuntimeException.java | 42 - .../exception/DBUpdateNothingException.java | 42 - .../DBUpdateNothingRuntimeException.java | 42 - .../qq/tars/exception/DiskFullException.java | 42 - .../exception/DiskFullRuntimeException.java | 42 - .../qq/tars/exception/DiskOtherException.java | 42 - .../exception/DiskOtherRuntimeException.java | 42 - .../tars/exception/TARSRequestException.java | 46 - .../TARSRequestRuntimeException.java | 46 - .../qq/tars/generated/tars/AUTH_STATE.java | 42 - .../com/qq/tars/generated/tars/AUTH_TYPE.java | 36 - .../qq/tars/generated/tars/AdminRegPrx.java | 941 ---- .../generated/tars/AdminRegPrxCallback.java | 63 - .../generated/tars/ApplyTokenRequest.java | 72 - .../generated/tars/ApplyTokenResponse.java | 87 - .../com/qq/tars/generated/tars/AuthPrx.java | 78 - .../tars/generated/tars/AuthPrxCallback.java | 20 - .../qq/tars/generated/tars/AuthRequest.java | 98 - .../qq/tars/generated/tars/ConfigInfo.java | 147 - .../com/qq/tars/generated/tars/ConfigPrx.java | 233 - .../generated/tars/ConfigPrxCallback.java | 29 - .../generated/tars/DeleteTokenRequest.java | 72 - .../qq/tars/generated/tars/EMTaskCommand.java | 41 - .../tars/generated/tars/EMTaskItemStatus.java | 42 - .../qq/tars/generated/tars/EMTaskStatus.java | 43 - .../generated/tars/GetConfigListInfo.java | 151 - .../com/qq/tars/generated/tars/LoadInfo.java | 113 - .../com/qq/tars/generated/tars/NodeInfo.java | 192 - .../com/qq/tars/generated/tars/PatchInfo.java | 143 - .../qq/tars/generated/tars/PatchRequest.java | 237 - .../tars/generated/tars/PreparePatchInfo.java | 146 - .../generated/tars/PreparePatchRequest.java | 226 - .../qq/tars/generated/tars/ServerState.java | 40 - .../tars/generated/tars/ServerStateDesc.java | 158 - .../tars/generated/tars/ServerStateInfo.java | 149 - .../qq/tars/generated/tars/TaskItemReq.java | 216 - .../qq/tars/generated/tars/TaskItemRsp.java | 160 - .../com/qq/tars/generated/tars/TaskReq.java | 128 - .../com/qq/tars/generated/tars/TaskRsp.java | 140 - .../com/qq/tars/generated/tars/TokenKey.java | 98 - .../qq/tars/generated/tars/TokenRequest.java | 74 - .../qq/tars/generated/tars/TokenResponse.java | 90 - .../qq/tars/generated/tars/tarsErrCode.java | 46 - .../com/qq/tars/service/NotifyService.java | 53 - .../com/qq/tars/service/PatchService.java | 99 - .../com/qq/tars/service/ScheduleService.java | 37 - .../java/com/qq/tars/service/SetTriple.java | 95 - .../qq/tars/service/SystemConfigService.java | 57 - .../tars/service/adapter/AdapterService.java | 104 - .../tars/service/adapter/AddAdapterConf.java | 157 - .../service/adapter/UpdateAdapterConf.java | 111 - .../qq/tars/service/admin/AdminService.java | 189 - .../qq/tars/service/admin/CommandResult.java | 72 - .../qq/tars/service/admin/CommandTarget.java | 58 - .../qq/tars/service/config/AddConfigFile.java | 115 - .../qq/tars/service/config/ConfigService.java | 318 -- .../tars/service/config/UpdateConfigFile.java | 47 - .../service/expand/ExpandPreviewServer.java | 124 - .../qq/tars/service/expand/ExpandServer.java | 82 - .../service/expand/ExpandServerPreview.java | 122 - .../service/expand/ExpandServerService.java | 226 - .../monitor/TARSPropertyMonitorCondition.java | 132 - .../monitor/TARSPropertyMonitorDataRow.java | 100 - .../monitor/TARSPropertyMonitorService.java | 255 -- .../monitor/TARSStatMonitorCondition.java | 141 - .../monitor/TARSStatMonitorDataRow.java | 180 - .../monitor/TARSStatMonitorService.java | 300 -- .../qq/tars/service/server/DeployAdapter.java | 145 - .../qq/tars/service/server/DeployServer.java | 138 - .../qq/tars/service/server/ServerService.java | 160 - .../qq/tars/service/server/UpdateServer.java | 182 - .../com/qq/tars/service/task/AddTask.java | 42 - .../com/qq/tars/service/task/AddTaskItem.java | 52 - .../qq/tars/service/task/TaskItemResp.java | 132 - .../com/qq/tars/service/task/TaskResp.java | 59 - .../com/qq/tars/service/task/TaskService.java | 113 - .../qq/tars/service/template/AddTemplate.java | 54 - .../service/template/TemplateService.java | 116 - .../tars/service/template/UpdateTemplate.java | 67 - .../com/qq/tars/service/tree/TreeNode.java | 87 - .../com/qq/tars/service/tree/TreeService.java | 109 - .../java/com/qq/tars/tools/CodecUtils.java | 50 - .../main/java/com/qq/tars/tools/DateTime.java | 46 - .../java/com/qq/tars/tools/SystemUtils.java | 81 - .../com/qq/tars/validate/Application.java | 41 - .../tars/validate/ApplicationValidator.java | 36 - .../com/qq/tars/validate/ConfigFileName.java | 41 - .../validate/ConfigFileNameValidator.java | 37 - .../com/qq/tars/validate/DeployServer.java | 40 - .../tars/validate/DeployServerValidator.java | 37 - .../java/com/qq/tars/validate/EnableSet.java | 20 - .../java/com/qq/tars/validate/EndPoint.java | 20 - .../java/com/qq/tars/validate/ObjName.java | 41 - .../qq/tars/validate/ObjNameValidator.java | 36 - .../java/com/qq/tars/validate/ServerName.java | 41 - .../qq/tars/validate/ServerNameValidator.java | 37 - .../java/com/qq/tars/validate/SetArea.java | 41 - .../qq/tars/validate/SetAreaValidator.java | 36 - .../java/com/qq/tars/validate/SetGroup.java | 41 - .../qq/tars/validate/SetGroupValidator.java | 36 - .../java/com/qq/tars/validate/SetName.java | 41 - .../qq/tars/validate/SetNameValidator.java | 36 - .../main/java/com/qq/tars/validate/Value.java | 42 - .../com/qq/tars/validate/ValueValidator.java | 35 - .../tars/web/controller/DeployController.java | 97 - .../tars/web/controller/ExpandController.java | 108 - .../tars/web/controller/IndexController.java | 36 - .../web/controller/MonitorController.java | 58 - .../tars/web/controller/TaskController.java | 90 - .../tars/web/controller/TraceController.java | 57 - .../tars/web/controller/TreeController.java | 48 - .../controller/adapter/AdapterConfView.java | 170 - .../controller/adapter/AdapterController.java | 87 - .../web/controller/auth/AuthController.java | 112 - .../controller/config/ConfigController.java | 279 -- .../config/ConfigFileHistoryView.java | 70 - .../web/controller/config/ConfigFileView.java | 120 - .../controller/notify/NotifyController.java | 58 - .../controller/notify/ServerNotifyView.java | 70 - .../web/controller/patch/PatchController.java | 50 - .../web/controller/patch/ServerPatchView.java | 70 - .../controller/patch/UploadController.java | 117 - .../web/controller/server/ServerConfView.java | 270 -- .../controller/server/ServerController.java | 172 - .../template/ProfileTemplateView.java | 70 - .../template/TemplateController.java | 92 - .../qq/tars/web/filter/WrapRequestFilter.java | 40 - .../web/interceptor/VariablesInterceptor.java | 61 - web/src/main/resources/app.config.properties | 21 - .../conf-dozer/dozer-mapper-base.xml | 50 - .../resources/conf-mybatis/mybatis-base.xml | 44 - .../conf-mybatis/mybatis-mapper-adapter.xml | 104 - .../conf-mybatis/mybatis-mapper-config.xml | 269 -- .../conf-mybatis/mybatis-mapper-notify.xml | 37 - .../conf-mybatis/mybatis-mapper-patch.xml | 38 - .../conf-mybatis/mybatis-mapper-server.xml | 242 - .../conf-mybatis/mybatis-mapper-task.xml | 46 - .../conf-mybatis/mybatis-mapper-template.xml | 81 - .../resources/conf-spring/spring-base.xml | 27 - .../conf-spring/spring-context-aop.xml | 28 - .../conf-spring/spring-context-bean.xml | 51 - .../conf-spring/spring-context-dao.xml | 27 - .../conf-spring/spring-context-datasource.xml | 49 - .../conf-spring/spring-context-mvc.xml | 67 - web/src/main/resources/frame.zip | Bin 695062 -> 0 bytes web/src/main/resources/log4j.properties | 19 - web/src/main/resources/tars.conf | 19 - web/src/main/resources/tars/AdminReg.tars | 464 -- web/src/main/resources/tars/Auth.tars | 116 - web/src/main/resources/tars/ConfigF.tars | 119 - .../main/resources/tars/NodeDescriptor.tars | 147 - web/src/main/webapp/WEB-INF/web.xml | 91 - web/src/main/webapp/chart_test.html | 77 - web/src/main/webapp/css/bootstrap.min.css | 5 - .../webapp/css/bootstrap_datepicker.min.css | 7 - .../main/webapp/css/bootstrap_table.min.css | 1 - web/src/main/webapp/css/style.css | 38 - web/src/main/webapp/css/zTreeStyle.css | 94 - .../fonts/glyphicons-halflings-regular.eot | Bin 20335 -> 0 bytes .../fonts/glyphicons-halflings-regular.svg | 229 - .../fonts/glyphicons-halflings-regular.ttf | Bin 41280 -> 0 bytes .../fonts/glyphicons-halflings-regular.woff | Bin 23320 -> 0 bytes web/src/main/webapp/history_list.html | 203 - web/src/main/webapp/img/complete.gif | Bin 2207 -> 0 bytes web/src/main/webapp/img/line_conn.gif | Bin 45 -> 0 bytes web/src/main/webapp/img/loading.gif | Bin 381 -> 0 bytes web/src/main/webapp/img/loading2.gif | Bin 8060 -> 0 bytes web/src/main/webapp/img/metro.gif | Bin 4679 -> 0 bytes web/src/main/webapp/img/metro.png | Bin 5283 -> 0 bytes web/src/main/webapp/img/sprite.png | Bin 218 -> 0 bytes web/src/main/webapp/js/bootstrap.min.js | 7 - .../webapp/js/bootstrap_datepicker.min.js | 8 - web/src/main/webapp/js/bootstrap_table.min.js | 8 - web/src/main/webapp/js/chart.min.js | 14 - web/src/main/webapp/js/common.js | 332 -- web/src/main/webapp/js/index.js | 21 - web/src/main/webapp/js/jquery.min.js | 5 - .../main/webapp/js/jquery.ztree.core.min.js | 74 - .../webapp/js/jquery.ztree.excheck.min.js | 34 - web/src/main/webapp/js/npm.js | 13 - web/src/main/webapp/js/server_list.js | 905 ---- web/src/main/webapp/js/service_cfg.js | 576 --- web/src/main/webapp/js/tars_tree.js | 154 - web/src/main/webapp/js/template.js | 2 - web/src/main/webapp/js/token_list.js | 146 - web/src/main/webapp/js/tree_search.js | 233 - web/src/main/webapp/monitor.html | 369 -- web/src/main/webapp/pages/exception.ftl | 27 - web/src/main/webapp/pages/include/header.ftl | 30 - web/src/main/webapp/pages/include/sider.ftl | 25 - web/src/main/webapp/pages/include/sider2.ftl | 32 - web/src/main/webapp/pages/index.ftl | 143 - web/src/main/webapp/pages/op_manage.ftl | 70 - web/src/main/webapp/pages/trace.ftl | 78 - web/src/main/webapp/property_monitor.html | 324 -- web/src/main/webapp/pub_manage.html | 360 -- web/src/main/webapp/server_deploy.html | 369 -- web/src/main/webapp/server_expand.html | 395 -- web/src/main/webapp/server_list.html | 382 -- web/src/main/webapp/server_offline.html | 152 - web/src/main/webapp/service_cfg.html | 239 - web/src/main/webapp/template_manage.html | 323 -- web/src/main/webapp/token_list.html | 112 - web/src/main/webapp/v2/index.html | 1 - .../main/webapp/v2/static/css/app.a9e65.css | 5 - .../v2/static/fonts/Lato-Bold.1870e.woff2 | Bin 10156 -> 0 bytes .../v2/static/fonts/Lato-Bold.530a2.woff | Bin 21324 -> 0 bytes .../v2/static/fonts/Lato-Bold.624f2.ttf | Bin 21236 -> 0 bytes .../v2/static/fonts/Lato-Bold.ea734.eot | Bin 21466 -> 0 bytes .../v2/static/fonts/Lato-Light.493f8.woff | Bin 21036 -> 0 bytes .../v2/static/fonts/Lato-Light.502bf.woff2 | Bin 10016 -> 0 bytes .../v2/static/fonts/Lato-Light.a3eaa.ttf | Bin 20948 -> 0 bytes .../v2/static/fonts/Lato-Light.d10a5.eot | Bin 21192 -> 0 bytes .../v2/static/fonts/Lato-Regular.25295.ttf | Bin 21352 -> 0 bytes .../v2/static/fonts/Lato-Regular.6d8f7.woff2 | Bin 10116 -> 0 bytes .../v2/static/fonts/Lato-Regular.9966c.woff | Bin 21440 -> 0 bytes .../v2/static/fonts/Lato-Regular.e0ede.eot | Bin 21588 -> 0 bytes .../webapp/v2/static/img/Lato-Bold.70b17.svg | 1 - .../webapp/v2/static/img/Lato-Light.f5206.svg | 1 - .../v2/static/img/Lato-Regular.cd5ac.svg | 1 - .../webapp/v2/static/img/iconfont.4980b.svg | 111 - web/src/main/webapp/v2/static/js/app.efa50.js | 1 - .../webapp/v2/static/js/manifest.64ab7.js | 1 - .../main/webapp/v2/static/js/vendor.de9d9.js | 12 - 2271 files changed, 140 insertions(+), 340459 deletions(-) create mode 160000 cpp delete mode 100644 cpp/README.md delete mode 100644 cpp/build/CMakeLists.txt delete mode 100644 cpp/build/README.md delete mode 100755 cpp/build/build.sh delete mode 100644 cpp/examples/CoroutineDemo/AServer/AServant.tars delete mode 100644 cpp/examples/CoroutineDemo/AServer/AServantImp.cpp delete mode 100644 cpp/examples/CoroutineDemo/AServer/AServantImp.h delete mode 100644 cpp/examples/CoroutineDemo/AServer/AServer.cpp delete mode 100644 cpp/examples/CoroutineDemo/AServer/AServer.h delete mode 100644 cpp/examples/CoroutineDemo/AServer/makefile delete mode 100644 cpp/examples/CoroutineDemo/BServer/AServant.tars delete mode 100644 cpp/examples/CoroutineDemo/BServer/BServant.tars delete mode 100644 cpp/examples/CoroutineDemo/BServer/BServantImp.cpp delete mode 100644 cpp/examples/CoroutineDemo/BServer/BServantImp.h delete mode 100644 cpp/examples/CoroutineDemo/BServer/BServer.cpp delete mode 100644 cpp/examples/CoroutineDemo/BServer/BServer.h delete mode 100644 cpp/examples/CoroutineDemo/BServer/makefile delete mode 100644 cpp/examples/CoroutineDemo/README.md delete mode 100644 cpp/examples/CoroutineDemo/client/BServant.tars delete mode 100644 cpp/examples/CoroutineDemo/client/main.cpp delete mode 100644 cpp/examples/CoroutineDemo/client/makefile delete mode 100644 cpp/examples/CoroutineDemo/testCoro/BServant.tars delete mode 100644 cpp/examples/CoroutineDemo/testCoro/main.cpp delete mode 100644 cpp/examples/CoroutineDemo/testCoro/makefile delete mode 100644 cpp/examples/CoroutineDemo/testParallelCoro/BServant.tars delete mode 100644 cpp/examples/CoroutineDemo/testParallelCoro/main.cpp delete mode 100644 cpp/examples/CoroutineDemo/testParallelCoro/makefile delete mode 100644 cpp/examples/HttpDemo/HttpClient/main.cpp delete mode 100644 cpp/examples/HttpDemo/HttpClient/makefile delete mode 100644 cpp/examples/HttpDemo/HttpServer/HttpImp.cpp delete mode 100644 cpp/examples/HttpDemo/HttpServer/HttpImp.h delete mode 100644 cpp/examples/HttpDemo/HttpServer/HttpServer.cpp delete mode 100644 cpp/examples/HttpDemo/HttpServer/HttpServer.h delete mode 100644 cpp/examples/HttpDemo/HttpServer/makefile delete mode 100644 cpp/examples/PromiseDemo/AServer/AServant.tars delete mode 100644 cpp/examples/PromiseDemo/AServer/AServantImp.cpp delete mode 100644 cpp/examples/PromiseDemo/AServer/AServantImp.h delete mode 100644 cpp/examples/PromiseDemo/AServer/AServer.cpp delete mode 100644 cpp/examples/PromiseDemo/AServer/AServer.h delete mode 100644 cpp/examples/PromiseDemo/AServer/BServant.tars delete mode 100644 cpp/examples/PromiseDemo/AServer/CServant.tars delete mode 100644 cpp/examples/PromiseDemo/AServer/makefile delete mode 100644 cpp/examples/PromiseDemo/BServer/BServant.tars delete mode 100644 cpp/examples/PromiseDemo/BServer/BServantImp.cpp delete mode 100644 cpp/examples/PromiseDemo/BServer/BServantImp.h delete mode 100644 cpp/examples/PromiseDemo/BServer/BServer.cpp delete mode 100644 cpp/examples/PromiseDemo/BServer/BServer.h delete mode 100644 cpp/examples/PromiseDemo/BServer/makefile delete mode 100644 cpp/examples/PromiseDemo/CServer/CServant.tars delete mode 100644 cpp/examples/PromiseDemo/CServer/CServantImp.cpp delete mode 100644 cpp/examples/PromiseDemo/CServer/CServantImp.h delete mode 100644 cpp/examples/PromiseDemo/CServer/CServer.cpp delete mode 100644 cpp/examples/PromiseDemo/CServer/CServer.h delete mode 100644 cpp/examples/PromiseDemo/CServer/makefile delete mode 100644 cpp/examples/PromiseDemo/Client/main.cpp delete mode 100644 cpp/examples/PromiseDemo/Client/makefile delete mode 100644 cpp/examples/PromiseDemo/README.md delete mode 100644 cpp/examples/PushDemo/PushClient/Makefile delete mode 100644 cpp/examples/PushDemo/PushClient/TestRecvThread.cpp delete mode 100644 cpp/examples/PushDemo/PushClient/TestRecvThread.h delete mode 100644 cpp/examples/PushDemo/PushClient/main.cpp delete mode 100644 cpp/examples/PushDemo/PushServer/TestPushServantImp.cpp delete mode 100644 cpp/examples/PushDemo/PushServer/TestPushServantImp.h delete mode 100644 cpp/examples/PushDemo/PushServer/TestPushServer.cpp delete mode 100644 cpp/examples/PushDemo/PushServer/TestPushServer.h delete mode 100644 cpp/examples/PushDemo/PushServer/TestPushThread.cpp delete mode 100644 cpp/examples/PushDemo/PushServer/TestPushThread.h delete mode 100644 cpp/examples/PushDemo/PushServer/makefile delete mode 100644 cpp/examples/PushDemo/README.md delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/AsyncClient/main.cpp delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/AsyncClient/makefile delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/Client/main.cpp delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/Client/makefile delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/Hello.tars delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/HelloImp.cpp delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/HelloImp.h delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/HelloServer.cpp delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/HelloServer.h delete mode 100644 cpp/examples/QuickStartDemo/HelloServer/makefile delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/Client/main.cpp delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/Client/makefile delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/Proxy.tars delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/ProxyImp.cpp delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/ProxyImp.h delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.h delete mode 100644 cpp/examples/QuickStartDemo/ProxyServer/makefile delete mode 100644 cpp/examples/QuickStartDemo/README.md delete mode 100644 cpp/examples/README.md delete mode 100644 cpp/examples/StressDemo/README.md delete mode 100644 cpp/examples/StressDemo/TarsStressClient/main.cpp delete mode 100644 cpp/examples/StressDemo/TarsStressClient/makefile delete mode 100644 cpp/examples/StressDemo/TarsStressClient/teststress.sh delete mode 100644 cpp/examples/StressDemo/TarsStressServer/Stress.tars delete mode 100644 cpp/examples/StressDemo/TarsStressServer/StressImp.cpp delete mode 100644 cpp/examples/StressDemo/TarsStressServer/StressImp.h delete mode 100644 cpp/examples/StressDemo/TarsStressServer/TarsStressServer.cpp delete mode 100644 cpp/examples/StressDemo/TarsStressServer/TarsStressServer.h delete mode 100644 cpp/examples/StressDemo/TarsStressServer/makefile delete mode 100644 cpp/framework/AdminRegistryServer/AdminReapThread.cpp delete mode 100644 cpp/framework/AdminRegistryServer/AdminReapThread.h delete mode 100644 cpp/framework/AdminRegistryServer/AdminRegistryImp.cpp delete mode 100644 cpp/framework/AdminRegistryServer/AdminRegistryImp.h delete mode 100644 cpp/framework/AdminRegistryServer/AdminRegistryServer.cpp delete mode 100644 cpp/framework/AdminRegistryServer/AdminRegistryServer.h delete mode 100644 cpp/framework/AdminRegistryServer/CMakeLists.txt delete mode 100644 cpp/framework/AdminRegistryServer/DbProxy.cpp delete mode 100644 cpp/framework/AdminRegistryServer/DbProxy.h delete mode 100644 cpp/framework/AdminRegistryServer/ExecuteTask.cpp delete mode 100644 cpp/framework/AdminRegistryServer/ExecuteTask.h delete mode 100644 cpp/framework/AdminRegistryServer/util.h delete mode 100644 cpp/framework/AuthServer/AuthImp.cpp delete mode 100644 cpp/framework/AuthServer/AuthImp.h delete mode 100644 cpp/framework/AuthServer/AuthServer.cpp delete mode 100644 cpp/framework/AuthServer/AuthServer.h delete mode 100644 cpp/framework/AuthServer/CMakeLists.txt delete mode 100644 cpp/framework/AuthServer/main.cpp delete mode 100644 cpp/framework/CMakeLists.txt delete mode 100644 cpp/framework/ConfigServer/CMakeLists.txt delete mode 100644 cpp/framework/ConfigServer/ConfigImp.cpp delete mode 100644 cpp/framework/ConfigServer/ConfigImp.h delete mode 100644 cpp/framework/ConfigServer/ConfigServer.cpp delete mode 100644 cpp/framework/ConfigServer/ConfigServer.h delete mode 100644 cpp/framework/ConfigServer/main.cpp delete mode 100644 cpp/framework/LogServer/CMakeLists.txt delete mode 100644 cpp/framework/LogServer/LogImp.cpp delete mode 100644 cpp/framework/LogServer/LogImp.h delete mode 100644 cpp/framework/LogServer/LogServer.cpp delete mode 100644 cpp/framework/LogServer/LogServer.h delete mode 100644 cpp/framework/LogServer/main.cpp delete mode 100644 cpp/framework/NodeServer/Activator.cpp delete mode 100644 cpp/framework/NodeServer/Activator.h delete mode 100644 cpp/framework/NodeServer/BatchPatchThread.cpp delete mode 100644 cpp/framework/NodeServer/BatchPatchThread.h delete mode 100644 cpp/framework/NodeServer/CMakeLists.txt delete mode 100644 cpp/framework/NodeServer/CommandAddFile.h delete mode 100644 cpp/framework/NodeServer/CommandDestroy.h delete mode 100644 cpp/framework/NodeServer/CommandLoad.h delete mode 100644 cpp/framework/NodeServer/CommandNotify.h delete mode 100644 cpp/framework/NodeServer/CommandPatch.h delete mode 100644 cpp/framework/NodeServer/CommandStart.h delete mode 100644 cpp/framework/NodeServer/CommandStop.h delete mode 100644 cpp/framework/NodeServer/KeepAliveThread.cpp delete mode 100644 cpp/framework/NodeServer/KeepAliveThread.h delete mode 100644 cpp/framework/NodeServer/NodeImp.cpp delete mode 100644 cpp/framework/NodeServer/NodeImp.h delete mode 100644 cpp/framework/NodeServer/NodeRollLogger.cpp delete mode 100644 cpp/framework/NodeServer/NodeRollLogger.h delete mode 100644 cpp/framework/NodeServer/NodeServer.cpp delete mode 100644 cpp/framework/NodeServer/NodeServer.h delete mode 100644 cpp/framework/NodeServer/PlatformInfo.cpp delete mode 100644 cpp/framework/NodeServer/PlatformInfo.h delete mode 100644 cpp/framework/NodeServer/PropertyReporter.h delete mode 100644 cpp/framework/NodeServer/RegistryProxy.h delete mode 100644 cpp/framework/NodeServer/RemoveLogThread.cpp delete mode 100644 cpp/framework/NodeServer/RemoveLogThread.h delete mode 100644 cpp/framework/NodeServer/ReportMemThread.cpp delete mode 100644 cpp/framework/NodeServer/ReportMemThread.h delete mode 100644 cpp/framework/NodeServer/ServerCommand.h delete mode 100644 cpp/framework/NodeServer/ServerFactory.cpp delete mode 100644 cpp/framework/NodeServer/ServerFactory.h delete mode 100644 cpp/framework/NodeServer/ServerImp.cpp delete mode 100644 cpp/framework/NodeServer/ServerImp.h delete mode 100644 cpp/framework/NodeServer/ServerLimitResource.cpp delete mode 100644 cpp/framework/NodeServer/ServerLimitResource.h delete mode 100644 cpp/framework/NodeServer/ServerObject.cpp delete mode 100644 cpp/framework/NodeServer/ServerObject.h delete mode 100644 cpp/framework/NodeServer/SingleFileDownloader.cpp delete mode 100644 cpp/framework/NodeServer/SingleFileDownloader.h delete mode 100644 cpp/framework/NodeServer/main.cpp delete mode 100644 cpp/framework/NodeServer/util.h delete mode 100644 cpp/framework/NotifyServer/CMakeLists.txt delete mode 100644 cpp/framework/NotifyServer/LoadDbThread.cpp delete mode 100644 cpp/framework/NotifyServer/LoadDbThread.h delete mode 100644 cpp/framework/NotifyServer/NotifyImp.cpp delete mode 100644 cpp/framework/NotifyServer/NotifyImp.h delete mode 100644 cpp/framework/NotifyServer/NotifyServer.cpp delete mode 100644 cpp/framework/NotifyServer/NotifyServer.h delete mode 100644 cpp/framework/NotifyServer/main.cpp delete mode 100644 cpp/framework/PatchServer/CMakeLists.txt delete mode 100644 cpp/framework/PatchServer/PatchCache.cpp delete mode 100644 cpp/framework/PatchServer/PatchCache.h delete mode 100644 cpp/framework/PatchServer/PatchImp.cpp delete mode 100644 cpp/framework/PatchServer/PatchImp.h delete mode 100644 cpp/framework/PatchServer/PatchServer.cpp delete mode 100644 cpp/framework/PatchServer/PatchServer.h delete mode 100644 cpp/framework/PatchServer/main.cpp delete mode 100644 cpp/framework/PropertyServer/CMakeLists.txt delete mode 100644 cpp/framework/PropertyServer/PropertyDbManager.cpp delete mode 100644 cpp/framework/PropertyServer/PropertyDbManager.h delete mode 100644 cpp/framework/PropertyServer/PropertyHashMap.h delete mode 100644 cpp/framework/PropertyServer/PropertyImp.cpp delete mode 100644 cpp/framework/PropertyServer/PropertyImp.h delete mode 100644 cpp/framework/PropertyServer/PropertyReapThread.cpp delete mode 100644 cpp/framework/PropertyServer/PropertyReapThread.h delete mode 100644 cpp/framework/PropertyServer/PropertyServer.cpp delete mode 100644 cpp/framework/PropertyServer/PropertyServer.h delete mode 100644 cpp/framework/PropertyServer/main.cpp delete mode 100644 cpp/framework/QueryPropertyServer/CMakeLists.txt delete mode 100644 cpp/framework/QueryPropertyServer/DbProxy.cpp delete mode 100644 cpp/framework/QueryPropertyServer/DbProxy.h delete mode 100644 cpp/framework/QueryPropertyServer/DbThread.cpp delete mode 100644 cpp/framework/QueryPropertyServer/DbThread.h delete mode 100644 cpp/framework/QueryPropertyServer/QueryDbThread.cpp delete mode 100644 cpp/framework/QueryPropertyServer/QueryDbThread.h delete mode 100644 cpp/framework/QueryPropertyServer/QueryImp.cpp delete mode 100644 cpp/framework/QueryPropertyServer/QueryImp.h delete mode 100644 cpp/framework/QueryPropertyServer/QueryItem.h delete mode 100644 cpp/framework/QueryPropertyServer/QueryServer.cpp delete mode 100644 cpp/framework/QueryPropertyServer/QueryServer.h delete mode 100644 cpp/framework/QueryPropertyServer/RequestDecoder.cpp delete mode 100644 cpp/framework/QueryPropertyServer/RequestDecoder.h delete mode 100644 cpp/framework/QueryStatServer/CMakeLists.txt delete mode 100644 cpp/framework/QueryStatServer/DbProxy.cpp delete mode 100644 cpp/framework/QueryStatServer/DbProxy.h delete mode 100644 cpp/framework/QueryStatServer/DbThread.cpp delete mode 100644 cpp/framework/QueryStatServer/DbThread.h delete mode 100644 cpp/framework/QueryStatServer/QueryDbThread.cpp delete mode 100644 cpp/framework/QueryStatServer/QueryDbThread.h delete mode 100644 cpp/framework/QueryStatServer/QueryImp.cpp delete mode 100644 cpp/framework/QueryStatServer/QueryImp.h delete mode 100644 cpp/framework/QueryStatServer/QueryItem.h delete mode 100644 cpp/framework/QueryStatServer/QueryServer.cpp delete mode 100644 cpp/framework/QueryStatServer/QueryServer.h delete mode 100644 cpp/framework/QueryStatServer/RequestDecoder.cpp delete mode 100644 cpp/framework/QueryStatServer/RequestDecoder.h delete mode 100644 cpp/framework/README.md delete mode 100644 cpp/framework/RegistryServer/CMakeLists.txt delete mode 100644 cpp/framework/RegistryServer/CheckNodeThread.cpp delete mode 100644 cpp/framework/RegistryServer/CheckNodeThread.h delete mode 100644 cpp/framework/RegistryServer/CheckSettingState.cpp delete mode 100644 cpp/framework/RegistryServer/CheckSettingState.h delete mode 100644 cpp/framework/RegistryServer/DbHandle.cpp delete mode 100644 cpp/framework/RegistryServer/DbHandle.h delete mode 100644 cpp/framework/RegistryServer/QueryImp.cpp delete mode 100644 cpp/framework/RegistryServer/QueryImp.h delete mode 100644 cpp/framework/RegistryServer/ReapThread.cpp delete mode 100644 cpp/framework/RegistryServer/ReapThread.h delete mode 100644 cpp/framework/RegistryServer/RegistryImp.cpp delete mode 100644 cpp/framework/RegistryServer/RegistryImp.h delete mode 100644 cpp/framework/RegistryServer/RegistryProcThread.cpp delete mode 100644 cpp/framework/RegistryServer/RegistryProcThread.h delete mode 100644 cpp/framework/RegistryServer/RegistryServer.cpp delete mode 100644 cpp/framework/RegistryServer/RegistryServer.h delete mode 100644 cpp/framework/RegistryServer/main.cpp delete mode 100644 cpp/framework/StatServer/CMakeLists.txt delete mode 100644 cpp/framework/StatServer/ReapSSDThread.cpp delete mode 100644 cpp/framework/StatServer/ReapSSDThread.h delete mode 100644 cpp/framework/StatServer/StatDbManager.cpp delete mode 100644 cpp/framework/StatServer/StatDbManager.h delete mode 100644 cpp/framework/StatServer/StatHashMap.h delete mode 100644 cpp/framework/StatServer/StatImp.cpp delete mode 100644 cpp/framework/StatServer/StatImp.h delete mode 100644 cpp/framework/StatServer/StatServer.cpp delete mode 100644 cpp/framework/StatServer/StatServer.h delete mode 100644 cpp/framework/StatServer/main.cpp delete mode 100644 cpp/framework/conf/tars.default delete mode 100644 cpp/framework/conf/tars.tarsconfig delete mode 100644 cpp/framework/conf/tars.tarsjava.default delete mode 100644 cpp/framework/conf/tars.tarslog delete mode 100644 cpp/framework/conf/tars.tarsnotify delete mode 100644 cpp/framework/conf/tars.tarspatch delete mode 100644 cpp/framework/conf/tars.tarsproperty delete mode 100644 cpp/framework/conf/tars.tarsstat delete mode 100644 cpp/framework/deploy/tarsAdminRegistry/conf/adminregistry.conf delete mode 100644 cpp/framework/deploy/tarsAdminRegistry/util/execute.sh delete mode 100644 cpp/framework/deploy/tarsAdminRegistry/util/start.sh delete mode 100644 cpp/framework/deploy/tarsAdminRegistry/util/stop.sh delete mode 100644 cpp/framework/deploy/tars_install.sh delete mode 100644 cpp/framework/deploy/tarsconfig/conf/tarsconfig.conf delete mode 100644 cpp/framework/deploy/tarsconfig/util/execute.sh delete mode 100644 cpp/framework/deploy/tarsconfig/util/start.sh delete mode 100644 cpp/framework/deploy/tarsconfig/util/stop.sh delete mode 100644 cpp/framework/deploy/tarsnode/conf/tarsnode.conf delete mode 100644 cpp/framework/deploy/tarsnode/util/execute.sh delete mode 100644 cpp/framework/deploy/tarsnode/util/monitor.sh delete mode 100644 cpp/framework/deploy/tarsnode/util/start.sh delete mode 100644 cpp/framework/deploy/tarsnode/util/stop.sh delete mode 100644 cpp/framework/deploy/tarsnode_install.sh delete mode 100644 cpp/framework/deploy/tarspatch/conf/rsync.conf delete mode 100644 cpp/framework/deploy/tarspatch/conf/tarspatch.conf delete mode 100644 cpp/framework/deploy/tarspatch/util/execute.sh delete mode 100644 cpp/framework/deploy/tarspatch/util/init.sh delete mode 100644 cpp/framework/deploy/tarspatch/util/start.sh delete mode 100644 cpp/framework/deploy/tarspatch/util/stop.sh delete mode 100644 cpp/framework/deploy/tarsregistry/conf/tarsregistry.conf delete mode 100644 cpp/framework/deploy/tarsregistry/util/env.sh delete mode 100644 cpp/framework/deploy/tarsregistry/util/execute.sh delete mode 100644 cpp/framework/deploy/tarsregistry/util/start.sh delete mode 100644 cpp/framework/deploy/tarsregistry/util/stop.sh delete mode 100644 cpp/framework/patchclient/CMakeLists.txt delete mode 100644 cpp/framework/patchclient/patchclient.cpp delete mode 100644 cpp/framework/patchclient/tars_patch.cpp delete mode 100644 cpp/framework/patchclient/tars_patch.h delete mode 100644 cpp/framework/protocol/AdminReg.tars delete mode 100644 cpp/framework/protocol/CMakeLists.txt delete mode 100644 cpp/framework/protocol/Node.tars delete mode 100644 cpp/framework/protocol/NodeDescriptor.tars delete mode 100644 cpp/framework/protocol/Patch.tars delete mode 100644 cpp/framework/protocol/Registry.tars delete mode 100644 cpp/framework/protocol/RegistryDescriptor.tars delete mode 100644 cpp/framework/sql/db_tars.sql delete mode 100644 cpp/framework/sql/dump-sql.sh delete mode 100644 cpp/framework/sql/exec-sql.sh delete mode 100644 cpp/servant/CMakeLists.txt delete mode 100644 cpp/servant/README.md delete mode 100644 cpp/servant/jmem/jmem_hashmap.h delete mode 100644 cpp/servant/jmem/jmem_hashmap_compact.h delete mode 100644 cpp/servant/jmem/jmem_multi_hashmap.h delete mode 100644 cpp/servant/jmem/jmem_policy.h delete mode 100644 cpp/servant/jmem/jmem_queue.h delete mode 100644 cpp/servant/jmem/jmem_rbtree.h delete mode 100644 cpp/servant/libservant/AdapterProxy.cpp delete mode 100644 cpp/servant/libservant/AdminServant.cpp delete mode 100644 cpp/servant/libservant/AppCache.cpp delete mode 100644 cpp/servant/libservant/AppProtocol.cpp delete mode 100644 cpp/servant/libservant/Application.cpp delete mode 100644 cpp/servant/libservant/AsyncProcThread.cpp delete mode 100644 cpp/servant/libservant/BaseNotify.cpp delete mode 100644 cpp/servant/libservant/CMakeLists.txt delete mode 100644 cpp/servant/libservant/Communicator.cpp delete mode 100644 cpp/servant/libservant/CommunicatorEpoll.cpp delete mode 100644 cpp/servant/libservant/CoroutineScheduler.cpp delete mode 100644 cpp/servant/libservant/EndpointInfo.cpp delete mode 100644 cpp/servant/libservant/EndpointManager.cpp delete mode 100644 cpp/servant/libservant/Global.cpp delete mode 100644 cpp/servant/libservant/NetworkUtil.cpp delete mode 100644 cpp/servant/libservant/NotifyObserver.cpp delete mode 100644 cpp/servant/libservant/ObjectProxy.cpp delete mode 100644 cpp/servant/libservant/ObjectProxyFactory.cpp delete mode 100644 cpp/servant/libservant/PropertyReport.cpp delete mode 100644 cpp/servant/libservant/Servant.cpp delete mode 100644 cpp/servant/libservant/ServantHandle.cpp delete mode 100644 cpp/servant/libservant/ServantHelper.cpp delete mode 100644 cpp/servant/libservant/ServantProxy.cpp delete mode 100644 cpp/servant/libservant/ServantProxyFactory.cpp delete mode 100644 cpp/servant/libservant/StatReport.cpp delete mode 100644 cpp/servant/libservant/TarsConfig.cpp delete mode 100644 cpp/servant/libservant/TarsCurrent.cpp delete mode 100644 cpp/servant/libservant/TarsLogger.cpp delete mode 100644 cpp/servant/libservant/TarsNodeF.cpp delete mode 100644 cpp/servant/libservant/TarsNotify.cpp delete mode 100644 cpp/servant/libservant/Transceiver.cpp delete mode 100644 cpp/servant/makefile/makefile.tars delete mode 100644 cpp/servant/promise/exception_ptr.h delete mode 100644 cpp/servant/promise/promise.h delete mode 100644 cpp/servant/promise/promise_exception.h delete mode 100644 cpp/servant/promise/tuple.h delete mode 100644 cpp/servant/promise/when_all.h delete mode 100644 cpp/servant/protocol/AdminF.tars delete mode 100644 cpp/servant/protocol/Auth.tars delete mode 100644 cpp/servant/protocol/BaseF.tars delete mode 100644 cpp/servant/protocol/CMakeLists.txt delete mode 100644 cpp/servant/protocol/ConfigF.tars delete mode 100644 cpp/servant/protocol/EndpointF.tars delete mode 100644 cpp/servant/protocol/LogF.tars delete mode 100644 cpp/servant/protocol/NodeF.tars delete mode 100644 cpp/servant/protocol/NotifyF.tars delete mode 100644 cpp/servant/protocol/PropertyF.tars delete mode 100644 cpp/servant/protocol/QueryF.tars delete mode 100644 cpp/servant/protocol/StatF.tars delete mode 100755 cpp/servant/script/create_http_server.sh delete mode 100644 cpp/servant/script/create_tars_server.sh delete mode 100644 cpp/servant/script/demo/DemoServant.tars delete mode 100644 cpp/servant/script/demo/DemoServantImp.cpp delete mode 100644 cpp/servant/script/demo/DemoServantImp.h delete mode 100644 cpp/servant/script/demo/DemoServer.cpp delete mode 100644 cpp/servant/script/demo/DemoServer.h delete mode 100644 cpp/servant/script/demo/makefile delete mode 100644 cpp/servant/script/http_demo/DemoServantImp.cpp delete mode 100644 cpp/servant/script/http_demo/DemoServantImp.h delete mode 100644 cpp/servant/script/http_demo/DemoServer.cpp delete mode 100644 cpp/servant/script/http_demo/DemoServer.h delete mode 100644 cpp/servant/script/http_demo/makefile delete mode 100644 cpp/servant/servant/AdapterProxy.h delete mode 100644 cpp/servant/servant/AdminServant.h delete mode 100644 cpp/servant/servant/AppCache.h delete mode 100644 cpp/servant/servant/AppProtocol.h delete mode 100644 cpp/servant/servant/Application.h delete mode 100644 cpp/servant/servant/AsyncProcThread.h delete mode 100644 cpp/servant/servant/BaseNotify.h delete mode 100644 cpp/servant/servant/Communicator.h delete mode 100644 cpp/servant/servant/CommunicatorEpoll.h delete mode 100644 cpp/servant/servant/CommunicatorFactory.h delete mode 100644 cpp/servant/servant/CoroutineScheduler.h delete mode 100644 cpp/servant/servant/EndpointInfo.h delete mode 100644 cpp/servant/servant/EndpointManager.h delete mode 100644 cpp/servant/servant/Global.h delete mode 100644 cpp/servant/servant/Message.h delete mode 100644 cpp/servant/servant/NetworkUtil.h delete mode 100644 cpp/servant/servant/NotifyObserver.h delete mode 100644 cpp/servant/servant/ObjectProxy.h delete mode 100644 cpp/servant/servant/ObjectProxyFactory.h delete mode 100644 cpp/servant/servant/PropertyReport.h delete mode 100644 cpp/servant/servant/Servant.h delete mode 100644 cpp/servant/servant/ServantHandle.h delete mode 100644 cpp/servant/servant/ServantHelper.h delete mode 100644 cpp/servant/servant/ServantProxy.h delete mode 100644 cpp/servant/servant/ServantProxyFactory.h delete mode 100644 cpp/servant/servant/StatReport.h delete mode 100644 cpp/servant/servant/TarsConfig.h delete mode 100644 cpp/servant/servant/TarsCurrent.h delete mode 100644 cpp/servant/servant/TarsLogger.h delete mode 100644 cpp/servant/servant/TarsNodeF.h delete mode 100644 cpp/servant/servant/TarsNotify.h delete mode 100644 cpp/servant/servant/Transceiver.h delete mode 100644 cpp/servant/tup/CMakeLists.txt delete mode 100644 cpp/servant/tup/RequestF.tars delete mode 100644 cpp/servant/tup/Tars.h delete mode 100644 cpp/servant/tup/TarsDisplayer.h delete mode 100644 cpp/servant/tup/TarsType.h delete mode 100644 cpp/servant/tup/tup.h delete mode 100644 cpp/test/CMakeLists.txt delete mode 100644 cpp/test/README.md delete mode 100644 cpp/test/testFramework/CMakeLists.txt delete mode 100644 cpp/test/testFramework/testAdminRegistry/CMakeLists.txt delete mode 100644 cpp/test/testFramework/testAdminRegistry/main.cpp delete mode 100644 cpp/test/testServant/CMakeLists.txt delete mode 100644 cpp/test/testServant/testTarsAnalyzer/CMakeLists.txt delete mode 100644 cpp/test/testServant/testTarsAnalyzer/main.cpp delete mode 100644 cpp/test/testServant/testTarsAnalyzer/tarsAnalyzer.cpp delete mode 100644 cpp/test/testServant/testTarsAnalyzer/tarsAnalyzer.h delete mode 100644 cpp/test/testServant/testTarsAnalyzer/tupRequestF.h delete mode 100644 cpp/test/testServant/testTup/CMakeLists.txt delete mode 100644 cpp/test/testServant/testTup/UserInfo.h delete mode 100644 cpp/test/testServant/testTup/UserInfo.tars delete mode 100644 cpp/test/testServant/testTup/main.cpp delete mode 100644 cpp/test/testUtil/CMakeLists.txt delete mode 100644 cpp/test/testUtil/example_tc_autoptr.cpp delete mode 100644 cpp/test/testUtil/example_tc_bitmap.cpp delete mode 100644 cpp/test/testUtil/example_tc_cgi.cpp delete mode 100644 cpp/test/testUtil/example_tc_common.cpp delete mode 100644 cpp/test/testUtil/example_tc_config.cpp delete mode 100644 cpp/test/testUtil/example_tc_encoder.cpp delete mode 100644 cpp/test/testUtil/example_tc_file.cpp delete mode 100644 cpp/test/testUtil/example_tc_file_lock.cpp delete mode 100644 cpp/test/testUtil/example_tc_gzip.cpp delete mode 100644 cpp/test/testUtil/example_tc_hashmap.cpp delete mode 100644 cpp/test/testUtil/example_tc_http.cpp delete mode 100644 cpp/test/testUtil/example_tc_http_async.cpp delete mode 100644 cpp/test/testUtil/example_tc_lock.cpp delete mode 100644 cpp/test/testUtil/example_tc_log.cpp delete mode 100644 cpp/test/testUtil/example_tc_mem_chunk.cpp delete mode 100644 cpp/test/testUtil/example_tc_mem_queue.cpp delete mode 100644 cpp/test/testUtil/example_tc_mmap.cpp delete mode 100644 cpp/test/testUtil/example_tc_multi_hashmap.cpp delete mode 100644 cpp/test/testUtil/example_tc_mysql.cpp delete mode 100644 cpp/test/testUtil/example_tc_option.cpp delete mode 100644 cpp/test/testUtil/example_tc_pack.cpp delete mode 100644 cpp/test/testUtil/example_tc_singleton.cpp delete mode 100644 cpp/test/testUtil/example_tc_socket.cpp delete mode 100644 cpp/test/testUtil/example_tc_thread.cpp delete mode 100644 cpp/test/testUtil/example_tc_thread_lock.cpp delete mode 100644 cpp/test/testUtil/example_tc_thread_pool.cpp delete mode 100644 cpp/test/testUtil/example_tc_thread_queue.cpp delete mode 100644 cpp/test/testUtil/log.config.conf delete mode 100644 cpp/test/testUtil/template.config.conf delete mode 100644 cpp/test/testUtil/test.html delete mode 100644 cpp/thirdparty/thirdparty.sh delete mode 100644 cpp/tools/CMakeLists.txt delete mode 100644 cpp/tools/README.md delete mode 100644 cpp/tools/pb2tarscpp/CMakeLists.txt delete mode 100644 cpp/tools/pb2tarscpp/CppGenCallback.cpp delete mode 100644 cpp/tools/pb2tarscpp/CppGenCallback.h delete mode 100644 cpp/tools/pb2tarscpp/CppGenProxy.cpp delete mode 100644 cpp/tools/pb2tarscpp/CppGenProxy.h delete mode 100644 cpp/tools/pb2tarscpp/CppGenServant.cpp delete mode 100644 cpp/tools/pb2tarscpp/CppGenServant.h delete mode 100644 cpp/tools/pb2tarscpp/CppPbUtils.cpp delete mode 100644 cpp/tools/pb2tarscpp/CppPbUtils.h delete mode 100644 cpp/tools/pb2tarscpp/CppPlugin.cpp delete mode 100644 cpp/tools/pb2tarscpp/CppPlugin.h delete mode 100644 cpp/tools/tars2android/CMakeLists.txt delete mode 100644 cpp/tools/tars2android/interface_analysis.cpp delete mode 100644 cpp/tools/tars2android/interface_analysis.h delete mode 100644 cpp/tools/tars2android/main.cpp delete mode 100644 cpp/tools/tars2android/tars2android.cpp delete mode 100644 cpp/tools/tars2android/tars2android.h delete mode 100644 cpp/tools/tars2android/tars_filter.cpp delete mode 100644 cpp/tools/tars2android/tars_filter.h delete mode 100644 cpp/tools/tars2c/CMakeLists.txt delete mode 100644 cpp/tools/tars2c/main.cpp delete mode 100644 cpp/tools/tars2c/tars2c.cpp delete mode 100644 cpp/tools/tars2c/tars2c.h delete mode 100644 cpp/tools/tars2cpp/CMakeLists.txt delete mode 100644 cpp/tools/tars2cpp/main.cpp delete mode 100644 cpp/tools/tars2cpp/tars2cpp.cpp delete mode 100644 cpp/tools/tars2cpp/tars2cpp.h delete mode 100644 cpp/tools/tars2cs/CMakeLists.txt delete mode 100644 cpp/tools/tars2cs/main.cpp delete mode 100644 cpp/tools/tars2cs/tars2cs.cpp delete mode 100644 cpp/tools/tars2cs/tars2cs.h delete mode 100644 cpp/tools/tars2node/CMakeLists.txt delete mode 100644 cpp/tools/tars2node/code_generator.cpp delete mode 100644 cpp/tools/tars2node/code_generator.h delete mode 100644 cpp/tools/tars2node/file_util.cpp delete mode 100644 cpp/tools/tars2node/gen_js.cpp delete mode 100644 cpp/tools/tars2node/gen_js_dts.cpp delete mode 100644 cpp/tools/tars2node/gen_proxy.cpp delete mode 100644 cpp/tools/tars2node/gen_proxy_dts.cpp delete mode 100644 cpp/tools/tars2node/gen_server.cpp delete mode 100644 cpp/tools/tars2node/gen_server_dts.cpp delete mode 100644 cpp/tools/tars2node/gen_server_imp.cpp delete mode 100644 cpp/tools/tars2node/idl_scan.cpp delete mode 100644 cpp/tools/tars2node/idl_util.cpp delete mode 100644 cpp/tools/tars2node/main.cpp delete mode 100644 cpp/tools/tars2oc/CMakeLists.txt delete mode 100644 cpp/tools/tars2oc/main.cpp delete mode 100644 cpp/tools/tars2oc/tars2oc.cpp delete mode 100644 cpp/tools/tars2oc/tars2oc.h delete mode 100644 cpp/tools/tars2php/CMakeLists.txt delete mode 100644 cpp/tools/tars2php/main.cpp delete mode 100644 cpp/tools/tars2php/tars2php.cpp delete mode 100644 cpp/tools/tars2php/tars2php.h delete mode 100644 cpp/tools/tars2python/CMakeLists.txt delete mode 100644 cpp/tools/tars2python/main.cpp delete mode 100644 cpp/tools/tars2python/tars2python.cpp delete mode 100644 cpp/tools/tars2python/tars2python.h delete mode 100644 cpp/tools/tarsgrammar/CMakeLists.txt delete mode 100644 cpp/tools/tarsgrammar/tars.l delete mode 100644 cpp/tools/tarsgrammar/tars.y delete mode 100644 cpp/tools/tarsparse/CMakeLists.txt delete mode 100644 cpp/tools/tarsparse/element.cpp delete mode 100644 cpp/tools/tarsparse/element.h delete mode 100644 cpp/tools/tarsparse/parse.cpp delete mode 100644 cpp/tools/tarsparse/parse.h delete mode 100644 cpp/util/CMakeLists.txt delete mode 100644 cpp/util/README.md delete mode 100644 cpp/util/include/CMakeLists.txt delete mode 100644 cpp/util/include/util/detail/tc_assert.h delete mode 100644 cpp/util/include/util/detail/tc_checked_delete.h delete mode 100644 cpp/util/include/util/detail/tc_shared_count_base.h delete mode 100644 cpp/util/include/util/detail/tc_shared_count_impl.h delete mode 100644 cpp/util/include/util/detail/tc_template_util.h delete mode 100644 cpp/util/include/util/tc_atomic.h delete mode 100644 cpp/util/include/util/tc_autoptr.h delete mode 100644 cpp/util/include/util/tc_base64.h delete mode 100644 cpp/util/include/util/tc_bind.h delete mode 100644 cpp/util/include/util/tc_bind_detail.h delete mode 100644 cpp/util/include/util/tc_bitmap.h delete mode 100644 cpp/util/include/util/tc_buffer.h delete mode 100644 cpp/util/include/util/tc_buffer_pool.h delete mode 100644 cpp/util/include/util/tc_callback.h delete mode 100644 cpp/util/include/util/tc_callback_detail.h delete mode 100644 cpp/util/include/util/tc_cgi.h delete mode 100644 cpp/util/include/util/tc_clientsocket.h delete mode 100644 cpp/util/include/util/tc_common.h delete mode 100644 cpp/util/include/util/tc_config.h delete mode 100644 cpp/util/include/util/tc_consistent_hash.h delete mode 100644 cpp/util/include/util/tc_consistent_hash_new.h delete mode 100644 cpp/util/include/util/tc_dyn_object.h delete mode 100644 cpp/util/include/util/tc_enable_shared_from_this.h delete mode 100644 cpp/util/include/util/tc_encoder.h delete mode 100644 cpp/util/include/util/tc_epoll_server.h delete mode 100644 cpp/util/include/util/tc_epoller.h delete mode 100644 cpp/util/include/util/tc_eventfd.h delete mode 100644 cpp/util/include/util/tc_ex.h delete mode 100644 cpp/util/include/util/tc_fcontext.h delete mode 100644 cpp/util/include/util/tc_fcontext_i386.h delete mode 100644 cpp/util/include/util/tc_fcontext_x86_64.h delete mode 100644 cpp/util/include/util/tc_fifo.h delete mode 100644 cpp/util/include/util/tc_file.h delete mode 100644 cpp/util/include/util/tc_file_mutex.h delete mode 100644 cpp/util/include/util/tc_functor.h delete mode 100644 cpp/util/include/util/tc_gzip.h delete mode 100644 cpp/util/include/util/tc_hash_fun.h delete mode 100644 cpp/util/include/util/tc_hashmap.h delete mode 100644 cpp/util/include/util/tc_hashmap_compact.h delete mode 100644 cpp/util/include/util/tc_http.h delete mode 100644 cpp/util/include/util/tc_http2clientmgr.h delete mode 100644 cpp/util/include/util/tc_http2session.h delete mode 100644 cpp/util/include/util/tc_http_async.h delete mode 100644 cpp/util/include/util/tc_lock.h delete mode 100644 cpp/util/include/util/tc_logger.h delete mode 100644 cpp/util/include/util/tc_loki.h delete mode 100644 cpp/util/include/util/tc_loop_queue.h delete mode 100644 cpp/util/include/util/tc_malloc_chunk.h delete mode 100644 cpp/util/include/util/tc_md5.h delete mode 100644 cpp/util/include/util/tc_mem_chunk.h delete mode 100644 cpp/util/include/util/tc_mem_queue.h delete mode 100644 cpp/util/include/util/tc_mem_vector.h delete mode 100644 cpp/util/include/util/tc_mmap.h delete mode 100644 cpp/util/include/util/tc_monitor.h delete mode 100644 cpp/util/include/util/tc_multi_hashmap.h delete mode 100644 cpp/util/include/util/tc_mysql.h delete mode 100644 cpp/util/include/util/tc_nghttp2.h delete mode 100644 cpp/util/include/util/tc_openssl.h delete mode 100644 cpp/util/include/util/tc_option.h delete mode 100644 cpp/util/include/util/tc_pack.h delete mode 100644 cpp/util/include/util/tc_parsepara.h delete mode 100644 cpp/util/include/util/tc_rbtree.h delete mode 100644 cpp/util/include/util/tc_readers_writer_data.h delete mode 100644 cpp/util/include/util/tc_scoped_ptr.h delete mode 100644 cpp/util/include/util/tc_sem_mutex.h delete mode 100644 cpp/util/include/util/tc_sha.h delete mode 100644 cpp/util/include/util/tc_shared_ptr.h delete mode 100644 cpp/util/include/util/tc_shm.h delete mode 100644 cpp/util/include/util/tc_singleton.h delete mode 100644 cpp/util/include/util/tc_socket.h delete mode 100644 cpp/util/include/util/tc_squeue.h delete mode 100644 cpp/util/include/util/tc_sslmgr.h delete mode 100644 cpp/util/include/util/tc_tea.h delete mode 100644 cpp/util/include/util/tc_thread.h delete mode 100644 cpp/util/include/util/tc_thread_cond.h delete mode 100644 cpp/util/include/util/tc_thread_mutex.h delete mode 100644 cpp/util/include/util/tc_thread_pool.h delete mode 100644 cpp/util/include/util/tc_thread_queue.h delete mode 100644 cpp/util/include/util/tc_thread_rwlock.h delete mode 100644 cpp/util/include/util/tc_timeout_queue.h delete mode 100644 cpp/util/include/util/tc_timeout_queue_map.h delete mode 100644 cpp/util/include/util/tc_timeout_queue_new.h delete mode 100644 cpp/util/include/util/tc_timeout_queue_noid.h delete mode 100644 cpp/util/include/util/tc_timeprovider.h delete mode 100644 cpp/util/src/CMakeLists.txt delete mode 100644 cpp/util/src/tc_base64.cpp delete mode 100644 cpp/util/src/tc_bitmap.cpp delete mode 100644 cpp/util/src/tc_buffer.cpp delete mode 100644 cpp/util/src/tc_buffer_pool.cpp delete mode 100644 cpp/util/src/tc_cgi.cpp delete mode 100644 cpp/util/src/tc_clientsocket.cpp delete mode 100644 cpp/util/src/tc_common.cpp delete mode 100644 cpp/util/src/tc_config.cpp delete mode 100644 cpp/util/src/tc_dyn_object.cpp delete mode 100644 cpp/util/src/tc_encoder.cpp delete mode 100644 cpp/util/src/tc_epoll_server.cpp delete mode 100644 cpp/util/src/tc_epoller.cpp delete mode 100644 cpp/util/src/tc_ex.cpp delete mode 100644 cpp/util/src/tc_fifo.cpp delete mode 100644 cpp/util/src/tc_file.cpp delete mode 100644 cpp/util/src/tc_file_mutex.cpp delete mode 100644 cpp/util/src/tc_gzip.cpp delete mode 100644 cpp/util/src/tc_hashmap.cpp delete mode 100644 cpp/util/src/tc_hashmap_compact.cpp delete mode 100644 cpp/util/src/tc_http.cpp delete mode 100644 cpp/util/src/tc_http2clientmgr.cpp delete mode 100644 cpp/util/src/tc_http2session.cpp delete mode 100644 cpp/util/src/tc_http_async.cpp delete mode 100644 cpp/util/src/tc_jump_i386_sysv_elf_gas.s delete mode 100644 cpp/util/src/tc_jump_x86_64_sysv_elf_gas.s delete mode 100644 cpp/util/src/tc_logger.cpp delete mode 100644 cpp/util/src/tc_make_i386_sysv_elf_gas.s delete mode 100644 cpp/util/src/tc_make_x86_64_sysv_elf_gas.s delete mode 100644 cpp/util/src/tc_malloc_chunk.cpp delete mode 100644 cpp/util/src/tc_md5.cpp delete mode 100644 cpp/util/src/tc_mem_chunk.cpp delete mode 100644 cpp/util/src/tc_mem_queue.cpp delete mode 100644 cpp/util/src/tc_mmap.cpp delete mode 100644 cpp/util/src/tc_multi_hashmap.cpp delete mode 100644 cpp/util/src/tc_mysql.cpp delete mode 100644 cpp/util/src/tc_nghttp2.cpp delete mode 100644 cpp/util/src/tc_openssl.cpp delete mode 100644 cpp/util/src/tc_option.cpp delete mode 100644 cpp/util/src/tc_pack.cpp delete mode 100644 cpp/util/src/tc_parsepara.cpp delete mode 100644 cpp/util/src/tc_rbtree.cpp delete mode 100644 cpp/util/src/tc_sem_mutex.cpp delete mode 100644 cpp/util/src/tc_sha.cpp delete mode 100644 cpp/util/src/tc_shm.cpp delete mode 100644 cpp/util/src/tc_socket.cpp delete mode 100644 cpp/util/src/tc_sslmgr.cpp delete mode 100644 cpp/util/src/tc_tea.cpp delete mode 100644 cpp/util/src/tc_thread.cpp delete mode 100644 cpp/util/src/tc_thread_cond.cpp delete mode 100644 cpp/util/src/tc_thread_mutex.cpp delete mode 100644 cpp/util/src/tc_thread_pool.cpp delete mode 100644 cpp/util/src/tc_thread_rwlock.cpp delete mode 100644 cpp/util/src/tc_timeprovider.cpp delete mode 100644 docs-en/tars_config.md delete mode 100644 docs-en/tars_cpp_develop_specification.md delete mode 100644 docs-en/tars_cpp_faq.md delete mode 100644 docs-en/tars_cpp_future_promise.md delete mode 100644 docs-en/tars_cpp_http_demo.md delete mode 100644 docs-en/tars_cpp_quickstart.md delete mode 100644 docs-en/tars_cpp_server_thread.md delete mode 100644 docs-en/tars_cpp_user_guide.md delete mode 100644 docs-en/tars_java_quickstart.md delete mode 100644 docs-en/tars_java_spring.md delete mode 100644 docs-en/tars_java_spring_cloud.md delete mode 100644 docs-en/tars_java_user_guide.md delete mode 100644 docs-en/tars_protobuf_cpp.md delete mode 100644 docs-en/tars_push.md delete mode 100644 docs-en/tars_server_monitor.md delete mode 100644 docs-en/tars_template.md delete mode 100644 docs-en/tars_tup.md delete mode 100644 docs/images/eureka-tars-java.png delete mode 100644 docs/images/tars-en.png delete mode 100644 docs/images/tars_auth_client.png delete mode 100644 docs/images/tars_auth_endpoint.png delete mode 100644 docs/images/tars_auth_server.png delete mode 100644 docs/images/tars_config_app1.png delete mode 100644 docs/images/tars_config_app2.png delete mode 100644 docs/images/tars_config_appconfig.png delete mode 100644 docs/images/tars_config_appconfig_result.png delete mode 100644 docs/images/tars_config_jiegoutu.png delete mode 100644 docs/images/tars_config_references.png delete mode 100644 docs/images/tars_config_server1.png delete mode 100644 docs/images/tars_config_server2.png delete mode 100644 docs/images/tars_config_server3_ref1.png delete mode 100644 docs/images/tars_config_server3_ref2.png delete mode 100644 docs/images/tars_config_set1.png delete mode 100644 docs/images/tars_config_set2.png delete mode 100644 docs/images/tars_config_table1.png delete mode 100644 docs/images/tars_config_table2.png delete mode 100644 docs/images/tars_cpp_quickstart_bushu1.png delete mode 100644 docs/images/tars_cpp_quickstart_bushu2.png delete mode 100644 docs/images/tars_cpp_quickstart_patch.png delete mode 100644 docs/images/tars_cpp_quickstart_patchresult.png delete mode 100644 docs/images/tars_cpp_quickstart_upload.png delete mode 100644 docs/images/tars_cpp_third_protocol.png delete mode 100644 docs/images/tars_dye_en.png delete mode 100644 docs/images/tars_flow.PNG delete mode 100644 docs/images/tars_idc_en.png delete mode 100644 docs/images/tars_java_evn1.png delete mode 100644 docs/images/tars_java_evn2.png delete mode 100644 docs/images/tars_java_evn3.png delete mode 100644 docs/images/tars_java_quickstart_bushu1.png delete mode 100644 docs/images/tars_java_quickstart_bushu2.png delete mode 100644 docs/images/tars_java_quickstart_patch.png delete mode 100644 docs/images/tars_java_quickstart_patchresult.png delete mode 100644 docs/images/tars_java_quickstart_upload.png delete mode 100644 docs/images/tars_junheng_en.png delete mode 100644 docs/images/tars_overload_en.png delete mode 100644 docs/images/tars_property_en.png delete mode 100644 docs/images/tars_push_deploy.PNG delete mode 100644 docs/images/tars_result.PNG delete mode 100644 docs/images/tars_rongcuo_en.png delete mode 100644 docs/images/tars_set_en.png delete mode 100644 docs/images/tars_stat_en.png delete mode 100644 docs/images/tars_template_table1.png delete mode 100644 docs/images/tars_top_en.png delete mode 100644 docs/images/tars_tupclass.png delete mode 100644 docs/images/tars_tupstruct.png delete mode 100644 docs/images/tars_web_index.png delete mode 100644 docs/images/tars_web_system_en.png delete mode 100644 docs/question_images/encoding_error.png delete mode 100644 docs/question_images/resin.png delete mode 100644 docs/tars_auth.md delete mode 100644 docs/tars_config.md delete mode 100644 docs/tars_cpp_develop_specification.md delete mode 100644 docs/tars_cpp_faq.md delete mode 100644 docs/tars_cpp_future_promise.md delete mode 100644 docs/tars_cpp_http_demo.md delete mode 100644 docs/tars_cpp_quickstart.md delete mode 100644 docs/tars_cpp_server_thread.md delete mode 100644 docs/tars_cpp_user_guide.md delete mode 100644 docs/tars_java_faq.md delete mode 100644 docs/tars_java_quickstart.md delete mode 100644 docs/tars_java_spring.md delete mode 100644 docs/tars_java_spring_boot.md delete mode 100644 docs/tars_java_spring_cloud.md delete mode 100644 docs/tars_java_user_guide.md delete mode 100644 docs/tars_protobuf_cpp.md delete mode 100644 docs/tars_push.md delete mode 100644 docs/tars_server_monitor.md delete mode 100644 docs/tars_template.md delete mode 100644 docs/tars_tup.md create mode 160000 framework create mode 160000 java delete mode 100644 java/README.md delete mode 100644 java/core/README.md delete mode 100644 java/core/client.pom.xml delete mode 100644 java/core/pom.xml delete mode 100644 java/core/server.pom.xml delete mode 100644 java/core/src/main/java/com/qq/tars/client/ClientVersion.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/Communicator.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/CommunicatorConfig.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/CommunicatorFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/ObjectProxy.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/ObjectProxyFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/ServantProxy.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/ServantProxyConfig.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/ServantProxyFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/cluster/DefaultLoadBalance.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/cluster/ServantInvokerAliveStat.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/cluster/ServantnvokerAliveChecker.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/InvokerComparator.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/ServantClient.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/ServantInvokeContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/ServantInvoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/ServantProtocolInvoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/loadbalance/ConsistentHashLoadBalance.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/loadbalance/DefaultLoadBalance.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/loadbalance/HashLoadBalance.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/loadbalance/LoadBalanceHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/loadbalance/RoundRobinLoadBalance.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/tars/TarsCallbackFilterChain.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/tars/TarsCallbackWrapper.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/tars/TarsClientFilterChain.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/tars/TarsInvokeContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/tars/TarsInvoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/rpc/tars/TarsProtocolInvoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/support/ClientPoolManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/support/ClientThreadPoolManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/support/ServantCacheManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/util/ClientLogger.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/util/Pair.java delete mode 100644 java/core/src/main/java/com/qq/tars/client/util/ParseTools.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/AbstractFilterChain.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/Filter.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/FilterChain.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/FilterKind.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/support/ClassLoaderManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/support/Endpoint.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/support/Holder.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/support/ScheduledExecutorManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/BeanAccessor.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/CommonUtils.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/Config.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/Constants.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/DyeingKeyCache.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/DyeingSwitch.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/FileUtil.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/HexUtil.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/Loader.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/StringUtils.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/TableUtil.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/concurrent/TaskQueue.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/concurrent/TaskThread.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/concurrent/TaskThreadFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/common/util/concurrent/TaskThreadPoolExecutor.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/annotation/Servant.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/annotation/ServantCodec.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/TarsInputStream.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/TarsInputStreamExt.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/TarsOutputStream.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/TarsOutputStreamExt.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/TarsStructBase.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsHolder.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsMethod.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsMethodParameter.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsRouteKey.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsStruct.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/annotation/TarsStructProperty.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/exc/TarsDecodeException.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/exc/TarsEncodeException.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/support/TarsMethodInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/support/TarsMethodParameterInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/support/TarsStructInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/tars/support/TarsStrutPropertyInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/util/TarsDisplayer.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/util/TarsHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/protocol/util/TarsUtil.java delete mode 100644 java/core/src/main/java/com/qq/tars/register/RegisterHandler.java delete mode 100644 java/core/src/main/java/com/qq/tars/register/RegisterManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/InvokeContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/Invoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/LoadBalance.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/ProtocolInvoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/Url.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/exc/NoInvokerException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/support/AbstractInvoker.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/common/util/concurrent/ConcurrentHashSet.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ClientException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/CommunicatorConfigException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/NoConnectionException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/NotConnectedException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/NotImplementedException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerDecodeException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerEncodeException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerNoFuncException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerNoServantException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerQueueTimeoutException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerResetGridException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServerUnknownException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/ServiceNotFoundException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/TarsException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/exc/TimeoutException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/ext/ExtendedProtocolCodecWrapper.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/ext/ExtendedProtocolFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/ext/RequestInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/ext/ResponseInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/Codec.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/ServantProtocolFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/ServantRequest.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/ServantResponse.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/ext/ExtendedServant.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/ext/ExtendedServantRequest.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/ext/ExtendedServantResponse.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/TarsCodec.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/TarsCodecHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/TarsServantProtocolFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/TarsServantRequest.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/TarsServantResponse.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/support/AnalystManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tars/support/TarsAbstractCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tup/BasicClassTypeUtil.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tup/ObjectCreateException.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tup/OldUniAttribute.java delete mode 100644 java/core/src/main/java/com/qq/tars/rpc/protocol/tup/UniAttribute.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/ServerVersion.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/apps/BaseAppContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/apps/DefaultAppContextEvent.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/apps/DefaultAppServantEvent.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/apps/XmlAppContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/common/ServerLogger.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/common/XMLConfigElement.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/common/XMLConfigFile.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/config/ConfigurationException.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/config/ConfigurationManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/config/ServantAdapterConfig.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/config/ServerConfig.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/Adapter.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AppContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AppContextEvent.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AppContextListener.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AppContextManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AppServantEvent.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AppService.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/AsyncContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/Context.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ContextManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ExtendedServantProcessor.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ServantAdapter.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ServantHomeSkeleton.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ServantProcessor.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ServantThreadPoolManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/Server.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/ServerThreadPoolDispatcher.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/TarsServantProcessor.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/core/TarsServerFilterChain.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/ha/ConnectionSessionListener.java delete mode 100644 java/core/src/main/java/com/qq/tars/server/startup/Main.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/admin/AdminFServant.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/admin/CommandHandler.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/admin/CustemCommandHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/admin/impl/AdminFServantImpl.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/config/ConfigHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/config/UpdateConfigCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/config/prx/ConfigInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/config/prx/ConfigPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/config/prx/ConfigPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/config/prx/GetConfigListInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/ArrayWriter.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/LogConfCacheMngr.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/LogItem.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/LogWorkThread.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/Logger.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/LoggerFactory.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/LoggingOutputStream.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/ThrowableInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/prx/LogInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/prx/LogPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/prx/LogPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/log/util/Utils.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/node/NodeHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/node/prx/ServerFPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/node/prx/ServerFPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/node/prx/ServerInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/NotifyHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/NOTIFYLEVEL.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/NotifyInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/NotifyItem.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/NotifyKey.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/NotifyPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/NotifyPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/ReportInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/notify/prx/ReportType.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/om/OmConstants.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/om/OmLogger.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/om/OmServiceMngr.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/om/ScheduledServiceMngr.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/om/ServerStatHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/CommonPropertyPolicy.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/JvmPropertyPolicy.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/PropertyReportHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/prx/PropertyFPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/prx/PropertyFPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/prx/StatPropInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/prx/StatPropMsgBody.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/property/prx/StatPropMsgHead.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/query/QueryHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/query/prx/EndpointF.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/query/prx/QueryFPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/query/prx/QueryFPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/InvokeStatHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/ProxyStat.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/ProxyStatBody.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/ProxyStatHead.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/ProxyStatUtils.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/StatHelper.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/prx/ProxyInfo.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/prx/StatFPrx.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/prx/StatFPrxCallback.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/prx/StatMicMsgBody.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/prx/StatMicMsgHead.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/stat/prx/StatSampleMsg.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TarsTraceZipkinConfiguration.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TraceCallbackFilter.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TraceClientFilter.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TraceContext.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TraceManager.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TraceServerFilter.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/TraceUtil.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/exc/NotSupportedSuchSampleEncodeingException.java delete mode 100644 java/core/src/main/java/com/qq/tars/support/trace/exc/NotSupportedSuchSampleTypeException.java delete mode 100644 java/core/src/main/resources/AdminF.tars delete mode 100644 java/core/src/main/resources/ConfigF.tars delete mode 100644 java/core/src/main/resources/EndpointF.tars delete mode 100644 java/core/src/main/resources/LogF.tars delete mode 100644 java/core/src/main/resources/QueryF.tars delete mode 100644 java/core/src/main/resources/StatF.tars delete mode 100644 java/distributedContext/README.md delete mode 100644 java/distributedContext/pom.xml delete mode 100644 java/distributedContext/src/main/java/com/qq/tars/context/DistributedContext.java delete mode 100644 java/distributedContext/src/main/java/com/qq/tars/context/DistributedContextCodec.java delete mode 100644 java/distributedContext/src/main/java/com/qq/tars/context/DistributedContextManager.java delete mode 100644 java/distributedContext/src/main/java/com/qq/tars/context/exc/CodecException.java delete mode 100644 java/distributedContext/src/main/java/com/qq/tars/context/impl/ContextItem.java delete mode 100644 java/distributedContext/src/main/java/com/qq/tars/context/impl/DistributedContextImpl.java delete mode 100644 java/examples/README.md delete mode 100644 java/examples/pom.xml delete mode 100644 java/examples/quickstart-client/README.md delete mode 100644 java/examples/quickstart-client/pom.xml delete mode 100644 java/examples/quickstart-client/src/main/java/com/qq/tars/quickstart/client/testapp/HelloPrx.java delete mode 100644 java/examples/quickstart-client/src/main/java/com/qq/tars/quickstart/client/testapp/HelloPrxCallback.java delete mode 100644 java/examples/quickstart-client/src/main/java/com/qq/tars/quickstart/domain/Main.java delete mode 100644 java/examples/quickstart-client/src/main/resources/hello.tars delete mode 100644 java/examples/quickstart-client/src/main/webapp/WEB-INF/web.xml delete mode 100644 java/examples/quickstart-server/README.md delete mode 100644 java/examples/quickstart-server/pom.xml delete mode 100644 java/examples/quickstart-server/src/main/java/com/qq/tars/quickstart/server/AppStartListener.java delete mode 100644 java/examples/quickstart-server/src/main/java/com/qq/tars/quickstart/server/testapp/HelloServant.java delete mode 100644 java/examples/quickstart-server/src/main/java/com/qq/tars/quickstart/server/testapp/impl/HelloServantImpl.java delete mode 100644 java/examples/quickstart-server/src/main/resources/TestApp.HelloJavaServer.config.conf delete mode 100644 java/examples/quickstart-server/src/main/resources/hello.tars delete mode 100644 java/examples/quickstart-server/src/main/resources/servants.xml delete mode 100644 java/examples/quickstart-server/src/main/webapp/WEB-INF/servants.xml delete mode 100644 java/examples/quickstart-server/src/main/webapp/WEB-INF/web.xml delete mode 100644 java/examples/stress-server/README.md delete mode 100644 java/examples/stress-server/pom.xml delete mode 100644 java/examples/stress-server/src/main/java/com/qq/tars/stress/server/test/StressServant.java delete mode 100644 java/examples/stress-server/src/main/java/com/qq/tars/stress/server/test/impl/StressServantImpl.java delete mode 100644 java/examples/stress-server/src/main/resources/stress.tars delete mode 100644 java/examples/stress-server/src/main/webapp/WEB-INF/servants.xml delete mode 100644 java/examples/stress-server/src/main/webapp/WEB-INF/web.xml delete mode 100644 java/examples/tars-spring-boot-server/pom.xml delete mode 100644 java/examples/tars-spring-boot-server/src/main/java/com/qq/tars/quickstart/server/QuickStartApplication.java delete mode 100644 java/examples/tars-spring-boot-server/src/main/java/com/qq/tars/quickstart/server/testapp/HelloServant.java delete mode 100644 java/examples/tars-spring-boot-server/src/main/java/com/qq/tars/quickstart/server/testapp/impl/HelloServantImpl.java delete mode 100644 java/examples/tars-spring-boot-server/src/main/resources/TestApp.HelloJavaServer.config.conf delete mode 100644 java/examples/tars-spring-boot-server/src/main/resources/hello.tars delete mode 100644 java/examples/tars-spring-cloud-client/README.md delete mode 100644 java/examples/tars-spring-cloud-client/pom.xml delete mode 100644 java/examples/tars-spring-cloud-client/src/main/java/com/qq/tars/springcloud/test/client/Client.java delete mode 100644 java/examples/tars-spring-cloud-client/src/main/java/com/qq/tars/springcloud/test/client/ClientApplication.java delete mode 100644 java/examples/tars-spring-cloud-client/src/main/java/com/qq/tars/springcloud/test/client/HelloPrx.java delete mode 100644 java/examples/tars-spring-cloud-client/src/main/resources/application.yml delete mode 100644 java/examples/tars-spring-cloud-server/README.md delete mode 100644 java/examples/tars-spring-cloud-server/pom.xml delete mode 100644 java/examples/tars-spring-cloud-server/src/main/java/com/qq/tars/springcloud/test/server/Application.java delete mode 100644 java/examples/tars-spring-cloud-server/src/main/java/com/qq/tars/springcloud/test/server/HelloServant.java delete mode 100644 java/examples/tars-spring-cloud-server/src/main/java/com/qq/tars/springcloud/test/server/HelloServantImpl.java delete mode 100644 java/examples/tars-spring-cloud-server/src/main/java/com/qq/tars/springcloud/test/server/HelloServantImpl2.java delete mode 100644 java/examples/tars-spring-cloud-server/src/main/resources/application.yml delete mode 100644 java/examples/tars-spring-server/pom.xml delete mode 100644 java/examples/tars-spring-server/src/main/java/com/qq/tars/quickstart/server/AppStartListener.java delete mode 100644 java/examples/tars-spring-server/src/main/java/com/qq/tars/quickstart/server/testapp/HelloServant.java delete mode 100644 java/examples/tars-spring-server/src/main/java/com/qq/tars/quickstart/server/testapp/impl/HelloServantImpl.java delete mode 100644 java/examples/tars-spring-server/src/main/resources/servants-spring.xml delete mode 100644 java/net/README.md delete mode 100644 java/net/pom.xml delete mode 100644 java/net/src/main/java/com/qq/tars/net/client/Callback.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/client/Client.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/client/FutureImpl.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/client/ticket/Ticket.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/client/ticket/TicketListener.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/client/ticket/TicketManager.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/IoBuffer.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/Processor.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/Request.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/Response.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/Server.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/Session.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/SessionEvent.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/SessionListener.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/SessionManager.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/Acceptor.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/Reactor.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/SelectorManager.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/SessionManagerImpl.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/TCPAcceptor.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/TCPSession.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/UDPAcceptor.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/UDPSession.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/core/nio/WorkThread.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/protocol/ProtocolDecoder.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/protocol/ProtocolEncoder.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/protocol/ProtocolException.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/protocol/ProtocolFactory.java delete mode 100644 java/net/src/main/java/com/qq/tars/net/util/Utils.java delete mode 100644 java/pom.xml delete mode 100644 java/protobuf/README.md delete mode 100644 java/protobuf/pom.xml delete mode 100644 java/protobuf/src/main/java/com/qq/tars/rpc/protocol/proto/MethodCache.java delete mode 100644 java/protobuf/src/main/java/com/qq/tars/rpc/protocol/proto/ProtoCodec.java delete mode 100644 java/spring/README.md delete mode 100644 java/spring/pom.xml delete mode 100644 java/spring/tars-spring-boot-starter/pom.xml delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/server/apps/SpringBootAppContext.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/TarsManageServiceConfiguration.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/TarsServerConfiguration.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/annotation/EnableTarsServer.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/annotation/RemotePropertySource.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/annotation/TarsHttpService.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/annotation/TarsListener.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/bean/CommunicatorBeanPostProcessor.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/bean/ManageServiceListener.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/bean/PropertiesListener.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/bean/ServletContainerCustomizer.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/java/com/qq/tars/spring/bean/TarsServerStartLifecycle.java delete mode 100644 java/spring/tars-spring-boot-starter/src/main/resources/META-INF/spring.factories delete mode 100644 java/spring/tars-spring-cloud-starter/README.md delete mode 100644 java/spring/tars-spring-cloud-starter/pom.xml delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/register/eureka/EurekaRegisterHandler.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/register/eureka/TarsEurekaAutoConfiguration.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/register/eureka/TarsEurekaClientConfig.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/register/eureka/TarsEurekaHealthCheckHandler.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/register/eureka/TarsEurekaInstance.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/register/eureka/TarsStatusChangeListener.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/server/apps/TarsStartLifecycle.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/spring/TarsConfiguration.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/spring/annotation/EnableTarsConfiguration.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/spring/bean/TarsClientBeanPostProcessor.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/spring/config/TarsClientProperties.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/java/com/qq/tars/spring/config/TarsServerProperties.java delete mode 100644 java/spring/tars-spring-cloud-starter/src/main/resources/META-INF/spring.factories delete mode 100644 java/spring/tars-spring/README.md delete mode 100644 java/spring/tars-spring/pom.xml delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/server/apps/SpringAppContext.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/annotation/TarsClient.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/annotation/TarsServant.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/config/ListenerConfig.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/config/ServantConfig.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/config/TarsSpringConfigException.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/schema/TarsBeanDefinitionParser.java delete mode 100644 java/spring/tars-spring/src/main/java/com/qq/tars/spring/schema/TarsNamespaceHandler.java delete mode 100644 java/spring/tars-spring/src/main/resources/META-INF/spring.handlers delete mode 100644 java/spring/tars-spring/src/main/resources/META-INF/spring.schemas delete mode 100644 java/spring/tars-spring/src/main/resources/META-INF/tars.xsd delete mode 100644 java/tools/README.md delete mode 100644 java/tools/pom.xml delete mode 100644 java/tools/tars-maven-plugin/README.md delete mode 100644 java/tools/tars-maven-plugin/pom.xml delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/gensrc/Servant.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/gensrc/Tars2JavaConfig.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/gensrc/Tars2JavaMojo.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/gensrc/TarsBuildMojo.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/model/ClasspathElement.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/model/Dependency.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/model/Directory.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/TarsLexer.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/TarsParser.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsConst.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsCustomType.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsEnum.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsInclude.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsInterface.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsKey.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsMapType.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsNamespace.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsOperation.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsParam.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsPrimitiveType.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsRoot.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsStruct.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsStructMember.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsType.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/parse/ast/TarsVectorType.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/script/Platform.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/util/ArchiveEntryUtils.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/util/FilePermission.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/util/FilePermissionUtils.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/util/StringUtils.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/util/XMLConfigElement.java delete mode 100644 java/tools/tars-maven-plugin/src/main/java/com/qq/tars/maven/util/XMLConfigFile.java delete mode 100644 java/tools/tars-maven-plugin/src/main/resources/com/qq/tars/maven/script/configTemplate delete mode 100644 java/tools/tars-maven-plugin/src/main/resources/com/qq/tars/maven/script/servantTemplate delete mode 100644 java/tools/tars-maven-plugin/src/main/resources/com/qq/tars/maven/script/unixBinTemplate delete mode 100644 java/tools/tars-maven-plugin/src/main/resources/com/qq/tars/maven/script/windowsBinTemplate create mode 160000 php delete mode 100644 php/README.md delete mode 100644 php/TARS-PHP-FAQ.md delete mode 100644 php/TARS-PHP-ISSUE.md delete mode 100644 "php/TARS-PHP\344\275\277\347\224\250\345\256\236\350\267\265.md" delete mode 100644 "php/TARS-PHP\357\274\232PHP\346\236\204\345\273\272\351\253\230\346\200\247\350\203\275RPC\346\241\206\346\236\266.pdf" delete mode 100644 php/dist/phptars-tars-client-0.1.0.zip delete mode 100644 php/dist/phptars-tars-client-0.1.1.zip delete mode 100644 php/dist/phptars-tars-client-0.1.2.zip delete mode 100644 php/dist/phptars-tars-client-0.1.3.zip delete mode 100644 php/dist/phptars-tars-client-0.1.4.zip delete mode 100644 php/dist/phptars-tars-config-0.1.0.zip delete mode 100644 php/dist/phptars-tars-config-0.1.1.zip delete mode 100644 php/dist/phptars-tars-config-0.1.2.zip delete mode 100644 php/dist/phptars-tars-config-0.1.3.zip delete mode 100644 php/dist/phptars-tars-deploy-0.1.0.zip delete mode 100644 php/dist/phptars-tars-log-0.1.0.zip delete mode 100644 php/dist/phptars-tars-log-0.1.1.zip delete mode 100644 php/dist/phptars-tars-log-0.1.2.zip delete mode 100644 php/dist/phptars-tars-log-0.1.3.zip delete mode 100644 php/dist/phptars-tars-monitor-0.1.0.zip delete mode 100644 php/dist/phptars-tars-monitor-0.1.1.zip delete mode 100644 php/dist/phptars-tars-monitor-0.1.2.zip delete mode 100644 php/dist/phptars-tars-monitor-0.1.3.zip delete mode 100644 php/dist/phptars-tars-registry-0.1.0.zip delete mode 100644 php/dist/phptars-tars-registry-0.1.1.zip delete mode 100644 php/dist/phptars-tars-report-0.1.0.zip delete mode 100644 php/dist/phptars-tars-server-0.1.0.zip delete mode 100644 php/dist/phptars-tars-server-0.1.1.zip delete mode 100644 php/dist/phptars-tars-server-0.1.2.zip delete mode 100644 php/dist/phptars-tars-server-0.1.3.zip delete mode 100644 php/dist/phptars-tars-server-0.1.4.zip delete mode 100644 php/dist/phptars-tars-server-0.1.5.zip delete mode 100644 php/dist/phptars-tars-utils-0.1.0.zip delete mode 100644 php/dist/phptars-tars2php-0.1.0.zip delete mode 100644 php/dist/phptars-tars2php-0.1.1.zip delete mode 100644 php/dist/phptars-tars2php-0.1.2.zip delete mode 100644 php/dist/tarsphp.json delete mode 100644 php/examples/tars-http-server/README.md delete mode 100755 php/examples/tars-http-server/scripts/tars2php.sh delete mode 100644 php/examples/tars-http-server/src/component/Controller.php delete mode 100644 php/examples/tars-http-server/src/composer.json delete mode 100644 php/examples/tars-http-server/src/conf/ENVConf.php delete mode 100644 php/examples/tars-http-server/src/controller/IndexController.php delete mode 100644 php/examples/tars-http-server/src/index.php delete mode 100644 php/examples/tars-http-server/src/servant/PHPTest/PHPServer/obj/TestTafServiceServant.php delete mode 100644 php/examples/tars-http-server/src/servant/PHPTest/PHPServer/obj/classes/ComplicatedStruct.php delete mode 100644 php/examples/tars-http-server/src/servant/PHPTest/PHPServer/obj/classes/LotofTags.php delete mode 100644 php/examples/tars-http-server/src/servant/PHPTest/PHPServer/obj/classes/OutStruct.php delete mode 100644 php/examples/tars-http-server/src/servant/PHPTest/PHPServer/obj/classes/SimpleStruct.php delete mode 100644 php/examples/tars-http-server/src/servant/PHPTest/PHPServer/obj/tars/example.tars delete mode 100644 php/examples/tars-http-server/src/services.php delete mode 100644 php/examples/tars-http-server/tars/example.tars delete mode 100644 php/examples/tars-http-server/tars/tars.proto.php delete mode 100644 php/examples/tars-http-server/tars/tarsclient.proto.php delete mode 100644 php/examples/tars-tcp-server/README.md delete mode 100755 php/examples/tars-tcp-server/scripts/tars2php.sh delete mode 100644 php/examples/tars-tcp-server/src/composer.json delete mode 100644 php/examples/tars-tcp-server/src/conf/ENVConf.php delete mode 100644 php/examples/tars-tcp-server/src/cservant/Test/TarsStressServer/StressObj/StressServant.php delete mode 100644 php/examples/tars-tcp-server/src/cservant/Test/TarsStressServer/StressObj/tars/stress.tars delete mode 100644 php/examples/tars-tcp-server/src/impl/PHPServerServantImpl.php delete mode 100644 php/examples/tars-tcp-server/src/index.php delete mode 100644 php/examples/tars-tcp-server/src/servant/PHPTest/PHPServer/obj/TestTafServiceServant.php delete mode 100644 php/examples/tars-tcp-server/src/servant/PHPTest/PHPServer/obj/classes/ComplicatedStruct.php delete mode 100644 php/examples/tars-tcp-server/src/servant/PHPTest/PHPServer/obj/classes/LotofTags.php delete mode 100644 php/examples/tars-tcp-server/src/servant/PHPTest/PHPServer/obj/classes/OutStruct.php delete mode 100644 php/examples/tars-tcp-server/src/servant/PHPTest/PHPServer/obj/classes/SimpleStruct.php delete mode 100644 php/examples/tars-tcp-server/src/servant/PHPTest/PHPServer/obj/tars/example.tars delete mode 100644 php/examples/tars-tcp-server/src/services.php delete mode 100644 php/examples/tars-tcp-server/tars/example.tars delete mode 100644 php/examples/tars-tcp-server/tars/tars.proto.php delete mode 100644 php/examples/tars-timer-server/README.md delete mode 100644 php/examples/tars-timer-server/src/composer.json delete mode 100644 php/examples/tars-timer-server/src/index.php delete mode 100644 php/examples/tars-timer-server/src/servant/PHPTest/PHPServer/obj/TestTafServiceServant.php delete mode 100644 php/examples/tars-timer-server/src/servant/PHPTest/PHPServer/obj/classes/ComplicatedStruct.php delete mode 100644 php/examples/tars-timer-server/src/servant/PHPTest/PHPServer/obj/classes/LotofTags.php delete mode 100644 php/examples/tars-timer-server/src/servant/PHPTest/PHPServer/obj/classes/OutStruct.php delete mode 100644 php/examples/tars-timer-server/src/servant/PHPTest/PHPServer/obj/classes/SimpleStruct.php delete mode 100644 php/examples/tars-timer-server/src/servant/PHPTest/PHPServer/obj/tars/example.tars delete mode 100644 php/examples/tars-timer-server/src/services.php delete mode 100644 php/examples/tars-timer-server/src/timer/TenSecondsTimer.php delete mode 100644 php/examples/tars-timer-server/tars/tars.proto.php delete mode 100644 php/tars-client/README.md delete mode 100644 php/tars-client/composer.json delete mode 100644 php/tars-client/src/Code.php delete mode 100644 php/tars-client/src/Communicator.php delete mode 100644 php/tars-client/src/CommunicatorConfig.php delete mode 100644 php/tars-client/src/Consts.php delete mode 100644 php/tars-client/src/RequestPacket.php delete mode 100644 php/tars-client/src/ResponsePacket.php delete mode 100644 php/tars-client/src/TUPAPIWrapper.php delete mode 100644 php/tars-client/tests/PHPTest/PHPServer/obj/TestTafServiceServant.php delete mode 100644 php/tars-client/tests/PHPTest/PHPServer/obj/classes/ComplicatedStruct.php delete mode 100644 php/tars-client/tests/PHPTest/PHPServer/obj/classes/LotofTags.php delete mode 100644 php/tars-client/tests/PHPTest/PHPServer/obj/classes/OutStruct.php delete mode 100644 php/tars-client/tests/PHPTest/PHPServer/obj/classes/SimpleStruct.php delete mode 100644 php/tars-client/tests/PHPTest/PHPServer/obj/tars/example.tars delete mode 100644 php/tars-client/tests/composer.json delete mode 100644 php/tars-client/tests/testServant.php delete mode 100644 php/tars-config/README.md delete mode 100644 php/tars-config/composer.json delete mode 100644 php/tars-config/src/ConfigServant.php delete mode 100644 php/tars-config/src/classes/ConfigInfo.php delete mode 100644 php/tars-config/src/classes/GetConfigListInfo.php delete mode 100644 php/tars-config/src/tars/ConfigF.tars delete mode 100644 php/tars-config/tests/demo.php delete mode 100644 php/tars-deploy/README.md delete mode 100644 php/tars-deploy/composer.json delete mode 100644 php/tars-deploy/src/Deploy.php delete mode 100644 php/tars-extension/README.md delete mode 100644 php/tars-extension/auto delete mode 100644 php/tars-extension/auto7.sh delete mode 100644 php/tars-extension/config.m4 delete mode 100644 php/tars-extension/include/php7_wrapper.h delete mode 100644 php/tars-extension/include/php_error.h delete mode 100644 php/tars-extension/include/php_tupapi.h delete mode 100644 php/tars-extension/include/tars_c.h delete mode 100644 php/tars-extension/include/ttars.h delete mode 100644 php/tars-extension/include/tup_c.h delete mode 100644 php/tars-extension/tars_c.c delete mode 100644 php/tars-extension/testcases/AllTypeStruct.php delete mode 100644 php/tars-extension/testcases/NestedStruct.php delete mode 100644 php/tars-extension/testcases/SimpleStruct.php delete mode 100644 php/tars-extension/testcases/test.php delete mode 100644 php/tars-extension/testcases/testTARSClient.php delete mode 100644 php/tars-extension/testcases/testTARSServer.php delete mode 100644 php/tars-extension/tests/basic/bool.phpt delete mode 100644 php/tars-extension/tests/basic/char_001.phpt delete mode 100644 php/tars-extension/tests/basic/char_002.phpt delete mode 100644 php/tars-extension/tests/basic/char_003.phpt delete mode 100644 php/tars-extension/tests/basic/char_004.phpt delete mode 100644 php/tars-extension/tests/basic/char_005.phpt delete mode 100644 php/tars-extension/tests/basic/double.phpt delete mode 100644 php/tars-extension/tests/basic/float.phpt delete mode 100644 php/tars-extension/tests/basic/int32.phpt delete mode 100644 php/tars-extension/tests/basic/int64.phpt delete mode 100644 php/tars-extension/tests/basic/short.phpt delete mode 100644 php/tars-extension/tests/basic/string_001.phpt delete mode 100644 php/tars-extension/tests/basic/string_002.phpt delete mode 100644 php/tars-extension/tests/basic/uint16.phpt delete mode 100644 php/tars-extension/tests/basic/uint32.phpt delete mode 100644 php/tars-extension/tests/basic/uint8.phpt delete mode 100644 php/tars-extension/tests/basic/wup_exception.phpt delete mode 100644 php/tars-extension/tests/combination/complicate_map.phpt delete mode 100644 php/tars-extension/tests/combination/complicate_struct.phpt delete mode 100644 php/tars-extension/tests/combination/complicate_vector.phpt delete mode 100644 php/tars-extension/tests/combination/map_001.phpt delete mode 100644 php/tars-extension/tests/combination/map_002.phpt delete mode 100644 php/tars-extension/tests/combination/map_003.phpt delete mode 100644 php/tars-extension/tests/combination/map_004.phpt delete mode 100644 php/tars-extension/tests/combination/map_005.phpt delete mode 100644 php/tars-extension/tests/combination/map_006.phpt delete mode 100644 php/tars-extension/tests/combination/map_007.phpt delete mode 100644 php/tars-extension/tests/combination/map_in_vector.phpt delete mode 100644 php/tars-extension/tests/combination/struct_001.phpt delete mode 100644 php/tars-extension/tests/combination/struct_002.phpt delete mode 100644 php/tars-extension/tests/combination/vector_char.phpt delete mode 100644 php/tars-extension/tests/combination/vector_empty.phpt delete mode 100644 php/tars-extension/tests/combination/vector_in_map.phpt delete mode 100644 php/tars-extension/tests/combination/vector_in_map2.phpt delete mode 100644 php/tars-extension/tests/combination/vector_in_map3.phpt delete mode 100644 php/tars-extension/tests/combination/vector_string.phpt delete mode 100644 php/tars-extension/tests/include/AllTypeStruct.php delete mode 100644 php/tars-extension/tests/include/NestedStruct.php delete mode 100644 php/tars-extension/tests/include/SimpleStruct.php delete mode 100644 php/tars-extension/tests/include/config.inc delete mode 100644 php/tars-extension/tests/include/functions.php delete mode 100644 php/tars-extension/tests/include/skipif.inc delete mode 100644 php/tars-extension/ttars.c delete mode 100644 php/tars-extension/tup_c.c delete mode 100644 php/tars-extension/tupapi.c delete mode 100644 php/tars-log/README.md delete mode 100644 php/tars-log/composer.json delete mode 100644 php/tars-log/src/LogServant.php delete mode 100644 php/tars-log/src/classes/LogInfo.php delete mode 100644 php/tars-log/src/tars/LogF.tars delete mode 100644 php/tars-log/tests/demo.php delete mode 100644 php/tars-monitor/README.md delete mode 100644 php/tars-monitor/composer.json delete mode 100644 php/tars-monitor/src/PropertyFServant.php delete mode 100644 php/tars-monitor/src/PropertyFWrapper.php delete mode 100644 php/tars-monitor/src/StatFServant.php delete mode 100644 php/tars-monitor/src/StatFWrapper.php delete mode 100644 php/tars-monitor/src/cache/RedisStoreCache.php delete mode 100644 php/tars-monitor/src/cache/SwooleTableStoreCache.php delete mode 100644 php/tars-monitor/src/classes/ProxyInfo.php delete mode 100644 php/tars-monitor/src/classes/StatMicMsgBody.php delete mode 100644 php/tars-monitor/src/classes/StatMicMsgHead.php delete mode 100644 php/tars-monitor/src/classes/StatPropInfo.php delete mode 100644 php/tars-monitor/src/classes/StatPropMsgBody.php delete mode 100644 php/tars-monitor/src/classes/StatPropMsgHead.php delete mode 100644 php/tars-monitor/src/classes/StatSampleMsg.php delete mode 100644 php/tars-monitor/src/client/Code.php delete mode 100644 php/tars-monitor/src/client/CodeMonitor.php delete mode 100644 php/tars-monitor/src/client/Communicator.php delete mode 100644 php/tars-monitor/src/client/CommunicatorConfig.php delete mode 100644 php/tars-monitor/src/client/CommunicatorMonitor.php delete mode 100644 php/tars-monitor/src/client/Consts.php delete mode 100644 php/tars-monitor/src/client/RequestPacket.php delete mode 100644 php/tars-monitor/src/client/RequestPacketMonitor.php delete mode 100644 php/tars-monitor/src/client/ResponsePacket.php delete mode 100644 php/tars-monitor/src/client/ResponsePacketMonitor.php delete mode 100644 php/tars-monitor/src/client/TUPAPIWrapper.php delete mode 100644 php/tars-monitor/src/client/TUPAPIWrapperMonitor.php delete mode 100644 php/tars-monitor/src/contract/StoreCacheInterface.php delete mode 100644 php/tars-monitor/src/tars/PropertyF.tars delete mode 100644 php/tars-monitor/src/tars/StatF.tars delete mode 100644 php/tars-monitor/tests/propertyFDemo.php delete mode 100644 php/tars-monitor/tests/statFDemo.php delete mode 100644 php/tars-registry/README.md delete mode 100644 php/tars-registry/composer.json delete mode 100644 php/tars-registry/src/EndpointF.php delete mode 100644 php/tars-registry/src/QueryFServant.php delete mode 100644 php/tars-registry/src/QueryFWrapper.php delete mode 100644 php/tars-registry/src/RouteTable.php delete mode 100644 php/tars-registry/src/client/CodeRegistry.php delete mode 100644 php/tars-registry/src/client/CommunicatorRegistry.php delete mode 100644 php/tars-registry/src/client/RequestPacketRegistry.php delete mode 100644 php/tars-registry/src/client/ResponsePacketRegistry.php delete mode 100644 php/tars-registry/src/client/TUPAPIWrapperRegistry.php delete mode 100644 php/tars-registry/src/tars/EndpointF.tars delete mode 100644 php/tars-registry/src/tars/QueryF.tars delete mode 100644 php/tars-registry/tests/demo.php delete mode 100644 php/tars-report/README.md delete mode 100644 php/tars-report/composer.json delete mode 100644 php/tars-report/src/NodeF.tars delete mode 100644 php/tars-report/src/ServerF.php delete mode 100644 php/tars-report/src/ServerFAsync.php delete mode 100644 php/tars-report/src/ServerFSync.php delete mode 100644 php/tars-report/src/ServerInfo.php delete mode 100644 php/tars-server/README.md delete mode 100644 php/tars-server/composer.json delete mode 100644 php/tars-server/src/Code.php delete mode 100644 php/tars-server/src/Consts.php delete mode 100644 php/tars-server/src/cmd/Command.php delete mode 100644 php/tars-server/src/cmd/CommandBase.php delete mode 100644 php/tars-server/src/cmd/Restart.php delete mode 100644 php/tars-server/src/cmd/Start.php delete mode 100644 php/tars-server/src/cmd/Stop.php delete mode 100644 php/tars-server/src/core/Event.php delete mode 100644 php/tars-server/src/core/Request.php delete mode 100644 php/tars-server/src/core/Response.php delete mode 100644 php/tars-server/src/core/Server.php delete mode 100644 php/tars-server/src/protocol/HTTPProtocol.php delete mode 100644 php/tars-server/src/protocol/JSONProtocol.php delete mode 100644 php/tars-server/src/protocol/Protocol.php delete mode 100644 php/tars-server/src/protocol/ProtocolFactory.php delete mode 100644 php/tars-server/src/protocol/TARSProtocol.php delete mode 100644 php/tars-server/tests/Acg.ACGRecomConfigServer.config.conf delete mode 100644 php/tars-server/tests/ClientConf.php delete mode 100644 php/tars-server/tests/QDPHP.QDPCHttpServer.config.conf delete mode 100644 php/tars-server/tests/test.php delete mode 100644 php/tars-server/tests/testConf.php delete mode 100644 php/tars-utils/README.md delete mode 100644 php/tars-utils/composer.json delete mode 100644 php/tars-utils/src/Conf.php delete mode 100644 php/tars-utils/src/Utils.php delete mode 100644 php/tars-utils/tests/QDPHP.TARSServer.config.conf delete mode 100644 php/tars-utils/tests/test.php delete mode 100644 php/tars2php/README.md delete mode 100644 php/tars2php/composer.json delete mode 100644 php/tars2php/src/tars2php.php delete mode 100644 php/tars2php/tests/client/PHPTest/PHPServer/obj/TestTafServiceServant.php delete mode 100644 php/tars2php/tests/client/PHPTest/PHPServer/obj/classes/ComplicatedStruct.php delete mode 100644 php/tars2php/tests/client/PHPTest/PHPServer/obj/classes/LotofTags.php delete mode 100644 php/tars2php/tests/client/PHPTest/PHPServer/obj/classes/OutStruct.php delete mode 100644 php/tars2php/tests/client/PHPTest/PHPServer/obj/classes/SimpleStruct.php delete mode 100644 php/tars2php/tests/client/PHPTest/PHPServer/obj/tars/example.tars delete mode 100644 php/tars2php/tests/example.tars delete mode 100644 php/tars2php/tests/server/PHPTest/PHPServer/obj/TestTafServiceServant.php delete mode 100644 php/tars2php/tests/server/PHPTest/PHPServer/obj/classes/ComplicatedStruct.php delete mode 100644 php/tars2php/tests/server/PHPTest/PHPServer/obj/classes/LotofTags.php delete mode 100644 php/tars2php/tests/server/PHPTest/PHPServer/obj/classes/OutStruct.php delete mode 100644 php/tars2php/tests/server/PHPTest/PHPServer/obj/classes/SimpleStruct.php delete mode 100644 php/tars2php/tests/server/PHPTest/PHPServer/obj/tars/example.tars delete mode 100644 php/tars2php/tests/tars.proto.php delete mode 100644 php/tars2php/tests/tars2php.sh delete mode 100644 php/tars2php/tests/tarsclient.proto.php delete mode 100644 plugins/README.md delete mode 100644 plugins/java-protobuf-plugin/README.md delete mode 100644 plugins/java-protobuf-plugin/build.gradle delete mode 100644 plugins/java-protobuf-plugin/gradle/wrapper/gradle-wrapper.jar delete mode 100644 plugins/java-protobuf-plugin/gradle/wrapper/gradle-wrapper.properties delete mode 100755 plugins/java-protobuf-plugin/gradlew delete mode 100644 plugins/java-protobuf-plugin/gradlew.bat delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaPlugin.cpp delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaPrxCallbackGenerator.cpp delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaPrxCallbackGenerator.h delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaPrxGenerator.cpp delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaPrxGenerator.h delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaServantGenerator.cpp delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/JavaServantGenerator.h delete mode 100644 plugins/java-protobuf-plugin/src/java_plugin/cpp/LogHelper.h delete mode 100644 plugins/java-protobuf-plugin/usage/multi/base.proto delete mode 100644 plugins/java-protobuf-plugin/usage/multi/service.proto delete mode 100644 plugins/java-protobuf-plugin/usage/single/flight.proto create mode 160000 tup delete mode 100644 tup/README.md delete mode 100644 tup/tup-c/Tars_c.c delete mode 100644 tup/tup-c/Tars_c.h delete mode 100644 tup/tup-c/demo/Test.tars delete mode 100644 tup/tup-c/demo/main.c delete mode 100644 tup/tup-c/demo/makefile delete mode 100644 tup/tup-c/tup_c.c delete mode 100644 tup/tup-c/tup_c.h delete mode 100644 tup/tup-csharp/.vs/TestTupConsole/v14/.suo delete mode 100644 tup/tup-csharp/Program.cs delete mode 100644 tup/tup-csharp/Properties/AssemblyInfo.cs delete mode 100644 tup/tup-csharp/Test/A.cs delete mode 100644 tup/tup-csharp/Test/B.cs delete mode 100644 tup/tup-csharp/Test/Test.tars delete mode 100644 tup/tup-csharp/Test/TestInfo.cs delete mode 100644 tup/tup-csharp/Test/TestTupSample1.cs delete mode 100644 tup/tup-csharp/Test/Test_const.cs delete mode 100644 tup/tup-csharp/TestTupConsole.csproj delete mode 100644 tup/tup-csharp/TestTupConsole.sln delete mode 100644 tup/tup-csharp/makefile delete mode 100644 tup/tup-csharp/tars/TarsDecodeException.cs delete mode 100644 tup/tup-csharp/tars/TarsDisplayer.cs delete mode 100644 tup/tup-csharp/tars/TarsEncodeException.cs delete mode 100644 tup/tup-csharp/tars/TarsInputStream.cs delete mode 100644 tup/tup-csharp/tars/TarsOutputStream.cs delete mode 100644 tup/tup-csharp/tars/TarsStruct.cs delete mode 100644 tup/tup-csharp/tars/TarsUtil.cs delete mode 100644 tup/tup-csharp/tup/Const.cs delete mode 100644 tup/tup-csharp/tup/RequestPacket.cs delete mode 100644 tup/tup-csharp/tup/TarsUniPacket.cs delete mode 100644 tup/tup-csharp/tup/UniAttribute.cs delete mode 100644 tup/tup-csharp/tup/UniPacket.cs delete mode 100644 tup/tup-csharp/util/BasicClassTypeUtil.cs delete mode 100644 tup/tup-csharp/util/ByteConverter.cs delete mode 100644 tup/tup-csharp/util/HexUtil.cs delete mode 100644 tup/tup-csharp/util/ObjectCreateException.cs delete mode 100644 tup/tup-csharp/util/QTrace.cs delete mode 100644 tup/tup-csharp/util/Utility.cs delete mode 100644 tup/tup-oc/Examples/Adapters/MttHDVerifyRes.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttHDVerifyRes.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttLoginReq+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttLoginReq+Adapter.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttPageConfigReq+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttPageConfigReq+Adapter.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttReleaseInfo+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttReleaseInfo+Adapter.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttSTStat+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttSTStat+Adapter.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttSplashReq+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttSplashReq+Adapter.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttUserBase+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttUserBase+Adapter.m delete mode 100644 tup/tup-oc/Examples/Adapters/MttVerifyReq+Adapter.h delete mode 100644 tup/tup-oc/Examples/Adapters/MttVerifyReq+Adapter.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDLoginInfo.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDLoginInfo.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDPageConfigInfo.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDPageConfigInfo.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDPersistentObject.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDPersistentObject.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDSplashInfo.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDSplashInfo.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDStatInfo.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDStatInfo.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDVerifyInfo.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDVerifyInfo.m delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDVideoInfo.h delete mode 100644 tup/tup-oc/Examples/BusinessData/MttHDVideoInfo.m delete mode 100644 tup/tup-oc/Examples/MttHDWupAdapters.h delete mode 100644 tup/tup-oc/Examples/MttHDWupBusinessData.h delete mode 100644 tup/tup-oc/Examples/MttHDWupEnvironment.h delete mode 100644 tup/tup-oc/Examples/MttHDWupEnvironment.m delete mode 100644 tup/tup-oc/Examples/MttHDWupManager.h delete mode 100644 tup/tup-oc/Examples/MttHDWupManager.m delete mode 100644 tup/tup-oc/Examples/MttHDWupRMIServices.h delete mode 100644 tup/tup-oc/Examples/Protocol/MttBrokerSecurityRequest.h delete mode 100644 tup/tup-oc/Examples/Protocol/MttBrokerSecurityRequest.m delete mode 100644 tup/tup-oc/Examples/Protocol/MttBrokerSecurityResponse.h delete mode 100644 tup/tup-oc/Examples/Protocol/MttBrokerSecurityResponse.m delete mode 100644 tup/tup-oc/Examples/Protocol/MttVideoApp.h delete mode 100644 tup/tup-oc/Examples/Protocol/MttVideoApp.m delete mode 100644 tup/tup-oc/Examples/Protocol/MttVideoReq.h delete mode 100644 tup/tup-oc/Examples/Protocol/MttVideoReq.m delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttConfigAgent.h delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttConfigAgent.m delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttCoolReadAgent.h delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttCoolReadAgent.m delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttLoginAgent.h delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttLoginAgent.m delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttSecurityAgent.h delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttSecurityAgent.m delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttStatAgent.h delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttStatAgent.m delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttVerifyAgent.h delete mode 100644 tup/tup-oc/Examples/RMIAgents/MttVerifyAgent.m delete mode 100644 tup/tup-oc/Examples/RMIServices/MttConfigService.h delete mode 100644 tup/tup-oc/Examples/RMIServices/MttConfigService.m delete mode 100644 tup/tup-oc/Examples/RMIServices/MttCoolReadService.h delete mode 100644 tup/tup-oc/Examples/RMIServices/MttCoolReadService.m delete mode 100644 tup/tup-oc/Examples/RMIServices/MttLoginService.h delete mode 100644 tup/tup-oc/Examples/RMIServices/MttLoginService.m delete mode 100644 tup/tup-oc/Examples/RMIServices/MttSecurityService.h delete mode 100644 tup/tup-oc/Examples/RMIServices/MttSecurityService.m delete mode 100644 tup/tup-oc/Examples/RMIServices/MttStatService.h delete mode 100644 tup/tup-oc/Examples/RMIServices/MttStatService.m delete mode 100644 tup/tup-oc/Examples/RMIServices/MttVerifyService.h delete mode 100644 tup/tup-oc/Examples/RMIServices/MttVerifyService.m delete mode 100644 tup/tup-oc/tup/Dependencies/ASIHttpRequest/ASICacheDelegate.h delete mode 100644 tup/tup-oc/tup/Dependencies/ASIHttpRequest/ASIHTTPRequest.h delete mode 100644 tup/tup-oc/tup/Dependencies/ASIHttpRequest/ASIHTTPRequestConfig.h delete mode 100644 tup/tup-oc/tup/Dependencies/ASIHttpRequest/ASIHTTPRequestDelegate.h delete mode 100644 tup/tup-oc/tup/Dependencies/ASIHttpRequest/ASIProgressDelegate.h delete mode 100644 tup/tup-oc/tup/tup.xcodeproj/project.pbxproj delete mode 100644 tup/tup-oc/tup/tup.xcodeproj/project.xcworkspace/contents.xcworkspacedata delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupAgent.h delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupAgent.m delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupAgentDelegate.h delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupHttpRequest.h delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupHttpRequest.m delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupService.h delete mode 100644 tup/tup-oc/tup/tup/RMIModel/WupService.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceEnumHelper.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceEnumHelper.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceInputStream.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceInputStream.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceObject.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceObject.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceObjectV2.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceObjectV2.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceOutputStream.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceOutputStream.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceStream.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/JceStream.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/RequestF.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/RequestF.mm delete mode 100644 tup/tup-oc/tup/tup/Serializable/UniAttribute.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/UniAttribute.m delete mode 100644 tup/tup-oc/tup/tup/Serializable/UniPacket.h delete mode 100644 tup/tup-oc/tup/tup/Serializable/UniPacket.mm delete mode 100644 tup/tup-oc/tup/tup/WirelessUnifiedProtocol-Debug.h delete mode 100644 tup/tup-oc/tup/tup/WirelessUnifiedProtocol-Prefix.pch delete mode 100644 tup/tup-oc/tup/tup/WirelessUnifiedProtocol.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAcceptEncoding.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAcceptEncoding.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAdsContent.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAdsContent.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAdsReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAdsReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAdsRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttAdsRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerContentInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerContentInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerDisplayInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerDisplayInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerFragRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerFragRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerFragResponse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerFragResponse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerJSPDInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerJSPDInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerJSRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerJSRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPageRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPageRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPicParam.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPicParam.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPicRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPicRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPushRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPushRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPushResponse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerPushResponse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerReqType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerReqType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerRequestParam.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerRequestParam.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSecurityRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSecurityRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSecurityResponse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSecurityResponse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSimplifiedPushRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSimplifiedPushRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSimplifiedPushResponse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSimplifiedPushResponse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSimplifiedRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerSimplifiedRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerTransferResponse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerTransferResponse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerUploadRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerUploadRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerUserInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerUserInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerWups.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrokerWups.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrowserReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrowserReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrowserRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrowserRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrowserVersion.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttBrowserVersion.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttCheckPluginReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttCheckPluginReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttCityListReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttCityListReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttCityListRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttCityListRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfigReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfigReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfigRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfigRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfirm.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttConfirm.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttContentEncoding.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttContentEncoding.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDNSReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDNSReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDNSRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDNSRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDirectPolicy.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDirectPolicy.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDomainRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDomainRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDomainRspNew.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDomainRspNew.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDynamicReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDynamicReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDynamicRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttDynamicRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEDomainType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEDomainType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEEntryType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEEntryType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEFASTKEY.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEFASTKEY.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEHotWordType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEHotWordType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttELanguageType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttELanguageType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEPushServerType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEPushServerType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttERelayProtoVer.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttERelayProtoVer.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEServerVer.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEServerVer.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttETPV.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttETPV.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEUSESTAT.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEUSESTAT.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEWaitPushType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttEWaitPushType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttErrorCode.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttErrorCode.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttExtDomain.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttExtDomain.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttFKINFO.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttFKINFO.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotListReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotListReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotListRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotListRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotWordInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotWordInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotWordsReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotWordsReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotWordsRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttHotWordsRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIPListReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIPListReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIPListRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIPListRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIcon.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIcon.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIconInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIconInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIconRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttIconRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImageQuality.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImageQuality.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImageReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImageReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImageRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImageRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImgReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImgReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImgRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttImgRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttItemReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttItemReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttItemRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttItemRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttKeywordRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttKeywordRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLPageReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLPageReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLPageRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLPageRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLoginReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLoginReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLoginRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttLoginRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttMTTContentType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttMTTContentType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttMethod.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttMethod.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttNaviRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttNaviRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttNavigation.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttNavigation.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageBrowserReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageBrowserReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageBrowserRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageBrowserRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageConfigReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageConfigReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageConfigRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageConfigRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPageRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPerformanceInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPerformanceInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPicInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPicInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPicRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPicRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPing.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPing.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginBootResp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginBootResp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginInfoNew.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginInfoNew.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginRspNew.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginRspNew.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginVersion.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPluginVersion.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushAppReqList.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushAppReqList.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushPortionFresh.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushPortionFresh.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushRespnoseContent.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushRespnoseContent.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushSysNotify.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttPushSysNotify.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRefuse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRefuse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRelayPageReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRelayPageReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRelayPageRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRelayPageRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttReply.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttReply.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRequest.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRequest.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRequestFragment.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttRequestFragment.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTImagePolicy.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTImagePolicy.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTJSInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTJSInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTJSPDInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTJSPDInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTOperator.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTOperator.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTOperatorRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTOperatorRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTPV.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTPV.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTSingleMD5.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTSingleMD5.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTSpeedStatis.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTSpeedStatis.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTStat.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTStat.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTTime.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTTime.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTTotal.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSTTotal.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchTypeInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchTypeInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchTypeReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchTypeReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchTypeRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSearchTypeRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSecurityCheckType.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSecurityCheckType.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSoftAnalyseInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSoftAnalyseInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSpecialValue.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSpecialValue.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSplashReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSplashReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSplashRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttSplashRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttStatValue.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttStatValue.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttStaticConfigReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttStaticConfigReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttThirdUse.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttThirdUse.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttTipPushReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttTipPushReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttTipPushRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttTipPushRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttURLPV.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttURLPV.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttUpdateRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttUpdateRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttUserBase.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttUserBase.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttVerifyReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttVerifyReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttVerifyRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttVerifyRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttVerifyRspExt.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttVerifyRspExt.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherInfo.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherInfo.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherInfo4Mini.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherInfo4Mini.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherReq.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherReq.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherRsp.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherRsp.m delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherRsp4Mini.h delete mode 100644 tup/tup-oc/tup/tupTests/Protocol/Mtt/MttWeatherRsp4Mini.m delete mode 100644 tup/tup-oc/tup/tupTests/WirelessUnifiedProtocolTests-Info.plist delete mode 100644 tup/tup-oc/tup/tupTests/WirelessUnifiedProtocolTests.h delete mode 100644 tup/tup-oc/tup/tupTests/WirelessUnifiedProtocolTests.m delete mode 100644 tup/tup-oc/tup/tupTests/en.lproj/InfoPlist.strings delete mode 100644 tup/tup-php/RequestF_tup.php delete mode 100644 tup/tup-php/demo/Hello.tars delete mode 100644 tup/tup-php/demo/Hello_tup.php delete mode 100644 tup/tup-php/demo/demo.php delete mode 100644 tup/tup-php/makefile delete mode 100644 tup/tup-php/tars.php delete mode 100644 tup/tup-php/tars_exception.php delete mode 100644 tup/tup-php/tars_header.php delete mode 100644 tup/tup-php/tup.php delete mode 100644 tup/tup-python/__init__.py delete mode 100644 tup/tup-python/jmain.py delete mode 100644 tup/tup-python/makefile delete mode 100644 tup/tup-python/tars/EndpointF.py delete mode 100644 tup/tup-python/tars/QueryF.py delete mode 100644 tup/tup-python/tars/__TimeoutQueue.py delete mode 100644 tup/tup-python/tars/__adapterproxy.py delete mode 100644 tup/tup-python/tars/__async.py delete mode 100644 tup/tup-python/tars/__init__.py delete mode 100644 tup/tup-python/tars/__logger.py delete mode 100644 tup/tup-python/tars/__packet.py delete mode 100644 tup/tup-python/tars/__rpc.py delete mode 100644 tup/tup-python/tars/__servantproxy.py delete mode 100644 tup/tup-python/tars/__tars.py delete mode 100644 tup/tup-python/tars/__trans.py delete mode 100644 tup/tup-python/tars/__tup.py delete mode 100644 tup/tup-python/tars/__util.py delete mode 100644 tup/tup-python/tars/core.py delete mode 100644 tup/tup-python/tars/exception.py delete mode 100644 tup/tup-python/tars/tars/EndpointF.tars delete mode 100644 tup/tup-python/tars/tars/QueryF.tars delete mode 100644 tup/tup-python/tars/tars/__init__.py delete mode 100644 tup/tup-python/test_tars2python.py delete mode 100644 tup/tup-python/wmain.py delete mode 100644 tup/tup-windows-cpp/RequestF.h delete mode 100644 tup/tup-windows-cpp/Tars.h delete mode 100644 tup/tup-windows-cpp/TarsDisplayer.h delete mode 100644 tup/tup-windows-cpp/TarsType.h delete mode 100644 tup/tup-windows-cpp/stdint.h delete mode 100644 tup/tup-windows-cpp/tup.h create mode 160000 web delete mode 100644 web/docs/API.md delete mode 100644 web/docs/README.md delete mode 100644 web/pom.xml delete mode 100644 web/src/main/java/com/qq/common/BindingResultMessage.java delete mode 100644 web/src/main/java/com/qq/common/Charsets.java delete mode 100644 web/src/main/java/com/qq/common/CodeEnableException.java delete mode 100644 web/src/main/java/com/qq/common/CodeEnableRuntimeException.java delete mode 100644 web/src/main/java/com/qq/common/CodeEnabled.java delete mode 100644 web/src/main/java/com/qq/common/ExceptionMessageTemplateManager.java delete mode 100644 web/src/main/java/com/qq/common/ExceptionMessageTempldate.java delete mode 100644 web/src/main/java/com/qq/common/ExceptionResponse.java delete mode 100644 web/src/main/java/com/qq/common/ExceptionResponseAttached.java delete mode 100644 web/src/main/java/com/qq/common/ResponseBodyMessageConverter.java delete mode 100644 web/src/main/java/com/qq/common/ServletRequestParameterException.java delete mode 100644 web/src/main/java/com/qq/common/StringFreeMarkerTemplateLoader.java delete mode 100644 web/src/main/java/com/qq/common/TCPClient.java delete mode 100644 web/src/main/java/com/qq/common/TimestampString.java delete mode 100644 web/src/main/java/com/qq/common/WrappedController.java delete mode 100644 web/src/main/java/com/qq/common/YesOrNoBooleanConverter.java delete mode 100644 web/src/main/java/com/qq/tars/db/AdapterMapper.java delete mode 100644 web/src/main/java/com/qq/tars/db/ConfigMapper.java delete mode 100644 web/src/main/java/com/qq/tars/db/NotifyMapper.java delete mode 100644 web/src/main/java/com/qq/tars/db/PatchMapper.java delete mode 100644 web/src/main/java/com/qq/tars/db/ServerMapper.java delete mode 100644 web/src/main/java/com/qq/tars/db/TaskMapper.java delete mode 100644 web/src/main/java/com/qq/tars/db/TemplateMapper.java delete mode 100644 web/src/main/java/com/qq/tars/entity/AdapterConf.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ConfigFile.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ConfigFileHistory.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ConfigRef.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ConfigRefExt.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ProfileTemplate.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ServerConf.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ServerNotify.java delete mode 100644 web/src/main/java/com/qq/tars/entity/ServerPatch.java delete mode 100644 web/src/main/java/com/qq/tars/entity/Task.java delete mode 100644 web/src/main/java/com/qq/tars/entity/TaskItem.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBConnectTimeoutException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBConnectTimeoutRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBConsistencyException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBConsistencyRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBOtherException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBOtherRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBSqlException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBSqlRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBUpdateNothingException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DBUpdateNothingRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DiskFullException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DiskFullRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DiskOtherException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/DiskOtherRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/TARSRequestException.java delete mode 100644 web/src/main/java/com/qq/tars/exception/TARSRequestRuntimeException.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AUTH_STATE.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AUTH_TYPE.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AdminRegPrx.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AdminRegPrxCallback.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ApplyTokenRequest.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ApplyTokenResponse.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AuthPrx.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AuthPrxCallback.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/AuthRequest.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ConfigInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ConfigPrx.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ConfigPrxCallback.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/DeleteTokenRequest.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/EMTaskCommand.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/EMTaskItemStatus.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/EMTaskStatus.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/GetConfigListInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/LoadInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/NodeInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/PatchInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/PatchRequest.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/PreparePatchInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/PreparePatchRequest.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ServerState.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ServerStateDesc.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/ServerStateInfo.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TaskItemReq.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TaskItemRsp.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TaskReq.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TaskRsp.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TokenKey.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TokenRequest.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/TokenResponse.java delete mode 100644 web/src/main/java/com/qq/tars/generated/tars/tarsErrCode.java delete mode 100644 web/src/main/java/com/qq/tars/service/NotifyService.java delete mode 100644 web/src/main/java/com/qq/tars/service/PatchService.java delete mode 100644 web/src/main/java/com/qq/tars/service/ScheduleService.java delete mode 100644 web/src/main/java/com/qq/tars/service/SetTriple.java delete mode 100644 web/src/main/java/com/qq/tars/service/SystemConfigService.java delete mode 100644 web/src/main/java/com/qq/tars/service/adapter/AdapterService.java delete mode 100644 web/src/main/java/com/qq/tars/service/adapter/AddAdapterConf.java delete mode 100644 web/src/main/java/com/qq/tars/service/adapter/UpdateAdapterConf.java delete mode 100644 web/src/main/java/com/qq/tars/service/admin/AdminService.java delete mode 100644 web/src/main/java/com/qq/tars/service/admin/CommandResult.java delete mode 100644 web/src/main/java/com/qq/tars/service/admin/CommandTarget.java delete mode 100644 web/src/main/java/com/qq/tars/service/config/AddConfigFile.java delete mode 100644 web/src/main/java/com/qq/tars/service/config/ConfigService.java delete mode 100644 web/src/main/java/com/qq/tars/service/config/UpdateConfigFile.java delete mode 100644 web/src/main/java/com/qq/tars/service/expand/ExpandPreviewServer.java delete mode 100644 web/src/main/java/com/qq/tars/service/expand/ExpandServer.java delete mode 100644 web/src/main/java/com/qq/tars/service/expand/ExpandServerPreview.java delete mode 100644 web/src/main/java/com/qq/tars/service/expand/ExpandServerService.java delete mode 100644 web/src/main/java/com/qq/tars/service/monitor/TARSPropertyMonitorCondition.java delete mode 100644 web/src/main/java/com/qq/tars/service/monitor/TARSPropertyMonitorDataRow.java delete mode 100644 web/src/main/java/com/qq/tars/service/monitor/TARSPropertyMonitorService.java delete mode 100644 web/src/main/java/com/qq/tars/service/monitor/TARSStatMonitorCondition.java delete mode 100644 web/src/main/java/com/qq/tars/service/monitor/TARSStatMonitorDataRow.java delete mode 100644 web/src/main/java/com/qq/tars/service/monitor/TARSStatMonitorService.java delete mode 100644 web/src/main/java/com/qq/tars/service/server/DeployAdapter.java delete mode 100644 web/src/main/java/com/qq/tars/service/server/DeployServer.java delete mode 100644 web/src/main/java/com/qq/tars/service/server/ServerService.java delete mode 100644 web/src/main/java/com/qq/tars/service/server/UpdateServer.java delete mode 100644 web/src/main/java/com/qq/tars/service/task/AddTask.java delete mode 100644 web/src/main/java/com/qq/tars/service/task/AddTaskItem.java delete mode 100644 web/src/main/java/com/qq/tars/service/task/TaskItemResp.java delete mode 100644 web/src/main/java/com/qq/tars/service/task/TaskResp.java delete mode 100644 web/src/main/java/com/qq/tars/service/task/TaskService.java delete mode 100644 web/src/main/java/com/qq/tars/service/template/AddTemplate.java delete mode 100644 web/src/main/java/com/qq/tars/service/template/TemplateService.java delete mode 100644 web/src/main/java/com/qq/tars/service/template/UpdateTemplate.java delete mode 100644 web/src/main/java/com/qq/tars/service/tree/TreeNode.java delete mode 100644 web/src/main/java/com/qq/tars/service/tree/TreeService.java delete mode 100644 web/src/main/java/com/qq/tars/tools/CodecUtils.java delete mode 100644 web/src/main/java/com/qq/tars/tools/DateTime.java delete mode 100644 web/src/main/java/com/qq/tars/tools/SystemUtils.java delete mode 100644 web/src/main/java/com/qq/tars/validate/Application.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ApplicationValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ConfigFileName.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ConfigFileNameValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/DeployServer.java delete mode 100644 web/src/main/java/com/qq/tars/validate/DeployServerValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/EnableSet.java delete mode 100644 web/src/main/java/com/qq/tars/validate/EndPoint.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ObjName.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ObjNameValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ServerName.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ServerNameValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/SetArea.java delete mode 100644 web/src/main/java/com/qq/tars/validate/SetAreaValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/SetGroup.java delete mode 100644 web/src/main/java/com/qq/tars/validate/SetGroupValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/SetName.java delete mode 100644 web/src/main/java/com/qq/tars/validate/SetNameValidator.java delete mode 100644 web/src/main/java/com/qq/tars/validate/Value.java delete mode 100644 web/src/main/java/com/qq/tars/validate/ValueValidator.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/DeployController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/ExpandController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/IndexController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/MonitorController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/TaskController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/TraceController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/TreeController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/adapter/AdapterConfView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/adapter/AdapterController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/auth/AuthController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/config/ConfigController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/config/ConfigFileHistoryView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/config/ConfigFileView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/notify/NotifyController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/notify/ServerNotifyView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/patch/PatchController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/patch/ServerPatchView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/patch/UploadController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/server/ServerConfView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/server/ServerController.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/template/ProfileTemplateView.java delete mode 100644 web/src/main/java/com/qq/tars/web/controller/template/TemplateController.java delete mode 100644 web/src/main/java/com/qq/tars/web/filter/WrapRequestFilter.java delete mode 100644 web/src/main/java/com/qq/tars/web/interceptor/VariablesInterceptor.java delete mode 100644 web/src/main/resources/app.config.properties delete mode 100644 web/src/main/resources/conf-dozer/dozer-mapper-base.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-base.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-adapter.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-config.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-notify.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-patch.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-server.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-task.xml delete mode 100644 web/src/main/resources/conf-mybatis/mybatis-mapper-template.xml delete mode 100644 web/src/main/resources/conf-spring/spring-base.xml delete mode 100644 web/src/main/resources/conf-spring/spring-context-aop.xml delete mode 100644 web/src/main/resources/conf-spring/spring-context-bean.xml delete mode 100644 web/src/main/resources/conf-spring/spring-context-dao.xml delete mode 100644 web/src/main/resources/conf-spring/spring-context-datasource.xml delete mode 100644 web/src/main/resources/conf-spring/spring-context-mvc.xml delete mode 100644 web/src/main/resources/frame.zip delete mode 100644 web/src/main/resources/log4j.properties delete mode 100644 web/src/main/resources/tars.conf delete mode 100644 web/src/main/resources/tars/AdminReg.tars delete mode 100644 web/src/main/resources/tars/Auth.tars delete mode 100644 web/src/main/resources/tars/ConfigF.tars delete mode 100644 web/src/main/resources/tars/NodeDescriptor.tars delete mode 100644 web/src/main/webapp/WEB-INF/web.xml delete mode 100644 web/src/main/webapp/chart_test.html delete mode 100644 web/src/main/webapp/css/bootstrap.min.css delete mode 100644 web/src/main/webapp/css/bootstrap_datepicker.min.css delete mode 100644 web/src/main/webapp/css/bootstrap_table.min.css delete mode 100644 web/src/main/webapp/css/style.css delete mode 100644 web/src/main/webapp/css/zTreeStyle.css delete mode 100644 web/src/main/webapp/fonts/glyphicons-halflings-regular.eot delete mode 100644 web/src/main/webapp/fonts/glyphicons-halflings-regular.svg delete mode 100644 web/src/main/webapp/fonts/glyphicons-halflings-regular.ttf delete mode 100644 web/src/main/webapp/fonts/glyphicons-halflings-regular.woff delete mode 100644 web/src/main/webapp/history_list.html delete mode 100644 web/src/main/webapp/img/complete.gif delete mode 100644 web/src/main/webapp/img/line_conn.gif delete mode 100644 web/src/main/webapp/img/loading.gif delete mode 100644 web/src/main/webapp/img/loading2.gif delete mode 100644 web/src/main/webapp/img/metro.gif delete mode 100644 web/src/main/webapp/img/metro.png delete mode 100644 web/src/main/webapp/img/sprite.png delete mode 100644 web/src/main/webapp/js/bootstrap.min.js delete mode 100644 web/src/main/webapp/js/bootstrap_datepicker.min.js delete mode 100644 web/src/main/webapp/js/bootstrap_table.min.js delete mode 100644 web/src/main/webapp/js/chart.min.js delete mode 100644 web/src/main/webapp/js/common.js delete mode 100644 web/src/main/webapp/js/index.js delete mode 100644 web/src/main/webapp/js/jquery.min.js delete mode 100644 web/src/main/webapp/js/jquery.ztree.core.min.js delete mode 100644 web/src/main/webapp/js/jquery.ztree.excheck.min.js delete mode 100644 web/src/main/webapp/js/npm.js delete mode 100644 web/src/main/webapp/js/server_list.js delete mode 100644 web/src/main/webapp/js/service_cfg.js delete mode 100644 web/src/main/webapp/js/tars_tree.js delete mode 100644 web/src/main/webapp/js/template.js delete mode 100644 web/src/main/webapp/js/token_list.js delete mode 100644 web/src/main/webapp/js/tree_search.js delete mode 100644 web/src/main/webapp/monitor.html delete mode 100644 web/src/main/webapp/pages/exception.ftl delete mode 100644 web/src/main/webapp/pages/include/header.ftl delete mode 100644 web/src/main/webapp/pages/include/sider.ftl delete mode 100644 web/src/main/webapp/pages/include/sider2.ftl delete mode 100644 web/src/main/webapp/pages/index.ftl delete mode 100644 web/src/main/webapp/pages/op_manage.ftl delete mode 100644 web/src/main/webapp/pages/trace.ftl delete mode 100644 web/src/main/webapp/property_monitor.html delete mode 100644 web/src/main/webapp/pub_manage.html delete mode 100644 web/src/main/webapp/server_deploy.html delete mode 100644 web/src/main/webapp/server_expand.html delete mode 100644 web/src/main/webapp/server_list.html delete mode 100644 web/src/main/webapp/server_offline.html delete mode 100644 web/src/main/webapp/service_cfg.html delete mode 100644 web/src/main/webapp/template_manage.html delete mode 100644 web/src/main/webapp/token_list.html delete mode 100644 web/src/main/webapp/v2/index.html delete mode 100644 web/src/main/webapp/v2/static/css/app.a9e65.css delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Bold.1870e.woff2 delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Bold.530a2.woff delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Bold.624f2.ttf delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Bold.ea734.eot delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Light.493f8.woff delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Light.502bf.woff2 delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Light.a3eaa.ttf delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Light.d10a5.eot delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Regular.25295.ttf delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Regular.6d8f7.woff2 delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Regular.9966c.woff delete mode 100644 web/src/main/webapp/v2/static/fonts/Lato-Regular.e0ede.eot delete mode 100644 web/src/main/webapp/v2/static/img/Lato-Bold.70b17.svg delete mode 100644 web/src/main/webapp/v2/static/img/Lato-Light.f5206.svg delete mode 100644 web/src/main/webapp/v2/static/img/Lato-Regular.cd5ac.svg delete mode 100644 web/src/main/webapp/v2/static/img/iconfont.4980b.svg delete mode 100644 web/src/main/webapp/v2/static/js/app.efa50.js delete mode 100644 web/src/main/webapp/v2/static/js/manifest.64ab7.js delete mode 100644 web/src/main/webapp/v2/static/js/vendor.de9d9.js diff --git a/.gitmodules b/.gitmodules index 2f3461134..fa7c64dba 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,21 @@ +[submodule "framework"] + path = framework + url = https://github.com/TarsCloud/TarsFramework +[submodule "cpp"] + path = cpp + url = https://github.com/TarsCloud/TarsCpp +[submodule "java"] + path = java + url = https://github.com/TarsCloud/TarsJava [submodule "nodejs"] path = nodejs - url = https://github.com/tars-node/Tars.js.git + url = https://github.com/tars-node/Tars.js +[submodule "php"] + path = php + url = https://github.com/TarsPHP/TarsPHP +[submodule "tup"] + path = tup + url = https://github.com/TarsCloud/TarsTup +[submodule "web"] + path = web + url = https://github.com/TarsCloud/TarsWeb diff --git a/Install.md b/Install.md index 49d2ec991..c9497dd94 100644 --- a/Install.md +++ b/Install.md @@ -20,11 +20,10 @@ gcc version: | 4.8.2 or laterã€glibc-devel(Dependent c++ framewo bison version: | 2.5 or later(Dependent c++ framework tools) flex version: | 2.5 or later(Dependent c++ framework tools) cmake version: | 2.8.8 or later(Dependent c++ framework tools) -resin version: | 4.0.49 or later(Dependent web management system) -Java JDK version: | JDK1.6 or later; For web management system,(JDK 1.8 or later) -Maven version: | 2.2.1 or later web management systemã€dependency of java framework) mysql version: | 4.1.17 or later(dependency of framework running) rapidjson version: | 1.0.2 or later(dependency of C++ framework) +nvm version: | 0.33.11 or later(Dependent web management system) +node version: | 8.11.3 or later(Dependent web management system) Hardware requirements: a machine running Linux. @@ -56,15 +55,7 @@ make make install ``` -## 1.3. Install resin -resin is the **recommended** running environment for Tars management system.(JDK is needed) -``` -cd /usr/local/ -tar zxvf resin-4.0.49.tar.gz -ln -s resin-4.0.49 resin -``` - -## 1.4. Install mysql +## 1.3. Install mysql Before installation, check whether ncurses and zlib have been installed. Execute these commands if not exist: ``` yum install ncurses-devel @@ -184,119 +175,29 @@ show slave status\G; # 2. Install develop environment for Tars ## 2.1. Install develop environment for web management system For linux: -Download JDK, unzip and install. - -Configure environment. -``` -vim /etc/profile -``` -Add following contents to /etc/profile: -``` -export JAVA_HOME=${jdk source dir} -CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar -PATH=$JAVA_HOME/bin:$PATH -export PATH JAVA_HOME CLASSPATH -``` -Execute command: -``` -source /etc/profile -``` -Test: -``` -java -version -``` -Install maven -Download maven, unzip and install: +Nvm script installation provided by the official website -Configure environment variables -``` -vim /etc/profile -``` -Add these lines: -``` -export MAVEN_HOME=${maven source dir} -export PATH=$PATH:$MAVEN_HOME/bin -``` -Execute command +Execute the following command ``` -source /etc/profile +wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash +source ~/.bashrc ``` -Test -``` -mvn -v -``` - -## 2.2. Install develop environment for java language framework -Environment configuration for JDK and maven is similar to that of web management system. - -**Attention: Make sure that your machine can access to internet, then modify file setting.xml from conf directory in the installed path of maven.** - -Download tars code, change to java source directory, and install: +Install node and process manager pm2 for node applications with load function ``` -mvn clean install -mvn clean install -f core/client.pom.xml -mvn clean install -f core/server.pom.xml - +nvm install v8.11.3 +npm install -g pm2 --registry=https://registry.npm.taobao.org ``` -Build web project -Use IDE or maven to create a maven web project, use eclipse as example, choose File -> New -> Project -> Maven Project -> maven-archetype-webapp,then enter groupId, artifactId,when finished use eclipse to import, the directory architecture is as following: -``` -├── pom.xml -└── src - ├── main - │ ├── java - │ │ └── tars - │ ├── resources - │ └── webapp - └── test - ├── java - └── resources -``` - - -Add dependency configuration in Maven - -```xml - - com.tencent.tars - tars-server - 1.4.0 - jar - -``` +## 2.2. Install develop environment for C++ +Download TarsFramework source -Plugin dependency configuration - -```xml - - com.tencent.tars - tars-maven-plugin - 1.4.0 - - - - ${basedir}/src/main/resources/hello.tars - - UTF-8 - true - ${basedir}/src/main/java - UTF-8 - com.qq.tars.quickstart.server. - - - -``` - -## 2.3. Install develop environment for C++ -Download tars source, change to directory `cpp/thirdparty`, execute `thirdparty.sh` script to download rapidjson. - -Then change to cpp/build. +Then change to build. ``` -cd {$source_folder}/cpp/build +cd {$source_folder}/build chmod u+x build.sh +./build.sh prepare ./build.sh all ``` **Be care that the default mysql lib path which Tars use is /usr/local/mysql/ . @@ -317,16 +218,16 @@ chown ${normal user}:${normal user} ./tars/ installation ``` -cd {$source_folder}/cpp/build +cd {$source_folder}/build ./build.sh install or make install ``` **The default install path is /usr/local/tars/cpp。** **If you want to install on different path:** ``` -**modify build/CMakeLists.txt** -**modify TARS_PATH in servant/makefile/makefile.tars** -**modify DEMO_PATH in servant/script/create_tars_server.sh** +**modify tarscpp/CMakeLists.txt** +**modify TARS_PATH in tarscpp/servant/makefile/makefile.tars** +**modify DEMO_PATH in tarscpp/servant/script/create_tars_server.sh** ``` # 3. Initialize the db environment for Tars @@ -340,7 +241,7 @@ flush privileges; **Attention: Modify ${'localhost'} to real hostname from /etc/hosts.** ## 3.2. Create DB -Search the ip in the script under `cpp/framework/sql`,and replace with the above ip. +Search the ip in the script under `framework/sql`,and replace with the above ip. ``` sed -i "s/192.168.2.131/${your machine ip}/g" `grep 192.168.2.131 -rl ./*` @@ -375,7 +276,7 @@ tarsAdminRegistry, tarsregistry, tarsnode, tarsconfig, tarspatch The basic general services: tarsstat, tarsproperty,tarsnotify, tarslog,tarsquerystat,tarsqueryproperty ``` -First get the core services package, change to cpp/build directory and input: +First get the core services package, change to build directory and input: ``` make framework-tar ``` @@ -481,58 +382,20 @@ Configure a checking crontab for tarsnode,ensuring it's always alive: ## 4.3. Install web management system ->** Install tars java framework first, see section 2.2** - > The name of the directory where management system source code in is **web** -Modify the configuration file, save it to the path web/src/main/resources/ -- app.config.properties - Modify DB configuration - -```ini -# db_tars -tarsweb.datasource.tars.addr={$your_db_ip}:3306 -tarsweb.datasource.tars.user=tars -tarsweb.datasource.tars.pswd=tars2015 - -# Public package store path -upload.tgz.path=/usr/local/app/patchs/tars.upload/ -``` - -- tars.conf - Substitute registry1.tars.com,registry2.tars.com with real ip, concatenate multiple address with colon. - -```xml - - - #proxy config - - #address - locator = tars.tarsregistry.QueryObj@tcp -h registry1.tars.com -p 17890:tars.tarsregistry.QueryObj@tcp -h registry2.tars.com -p 17890 - sync-invoke-timeout = 30000 - #timeout(ms) - max-invoke-timeout = 30000 - #interval for refresh endpoint(ms) - refresh-endpoint-interval = 60000 - #invoke between modules [optional] - stat = tars.tarsstat.StatObj - #async thread - asyncthread = 3 - modulename = tars.system - - - -``` - -Package: execute following command in web directory, then tars.war will be created in web/target. +Modify the configuration file and change the IP address in the configuration file to the local IP address, as follows: ``` -mvn clean package +cd web +sed -i 's/db.tars.com/${your_machine_ip}/g' config/webConf.js +sed -i 's/registry.tars.com/${your_machine_ip}/g' config/tars.conf ``` -Web patch -Put tars.war into /usr/local/resin/webapps/ +Install web management page dependencies, start web ``` -cp ./target/tars.war /usr/local/resin/webapps/ +cd web +npm install --registry=https://registry.npm.taobao.org +npm run prd ``` Create log directory @@ -540,25 +403,7 @@ Create log directory mkdir -p /data/log/tars ``` -Modify conf/resin.xml in Resin directory - -Modify the default configuration -```xml - - - -``` -to -```xml - - - -``` - -Launch resin -/usr/local/resin/bin/resin.sh start - -Visit the website, input ${your machine ip}:8080 into browser: +Visit the website, input ${your machine ip}:3000 into browser: ![tars](docs/images/tars_web_system_index_en.png) diff --git a/Install.zh.md b/Install.zh.md index d11c8d6b8..c08227ec6 100644 --- a/Install.zh.md +++ b/Install.zh.md @@ -19,11 +19,10 @@ gcc版本: | 4.8.2åŠä»¥ä¸Šç‰ˆæœ¬ã€glibc-devel(c++è¯­è¨€æ¡†æž¶ä¾ bison工具版本: | 2.5åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆc++语言框架ä¾èµ–) flex工具版本: | 2.5åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆc++语言框架ä¾èµ–) cmake版本: | 2.8.8åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆc++语言框架ä¾èµ–) -resin版本: | 4.0.49åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆweb管ç†ç³»ç»Ÿä¾èµ–) -Java JDK版本: | java语言框架(最低1.6),web管ç†ç³»ç»Ÿï¼ˆæœ€ä½Ž1.8) -Maven版本: | 2.2.1åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆweb管ç†ç³»ç»Ÿã€java语言框架ä¾èµ–) mysql版本: | 4.1.17åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆæ¡†æž¶è¿è¡Œä¾èµ–) rapidjson版本: | 1.0.2版本(c++语言框架ä¾èµ–) +nvm版本: | 0.33.11åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆweb管ç†ç³»ç»Ÿä¾èµ–) +node版本: | 8.11.3åŠä»¥ä¸Šç‰ˆæœ¬ï¼ˆweb管ç†ç³»ç»Ÿä¾èµ–) è¿è¡ŒæœåŠ¡å™¨è¦æ±‚:1å°æ™®é€šå®‰è£…linux系统的机器å³å¯ã€‚ @@ -54,16 +53,7 @@ make make install(如果make install失败,一般是æƒé™ä¸å¤Ÿï¼Œåˆ‡æ¢root进行安装) ``` -## 1.3. resinå®‰è£…ä»‹ç» -resin是Tars管ç†ç³»ç»Ÿ**推è**çš„è¿è¡ŒçŽ¯å¢ƒï¼ˆå¦‚果没有安装java jdk,先需è¦å®‰è£…,具体å¯ä»¥å‚è§2.1章节)。 -> 以resin-4.0.49,安装在/usr/local/resin下为例 -``` -cd /usr/local/ -tar zxvf resin-4.0.49.tar.gz -ln -s resin-4.0.49 resin -``` - -## 1.4. mysql å®‰è£…ä»‹ç» +## 1.3. mysql å®‰è£…ä»‹ç» å®‰è£…å‰ï¼Œç¡®å®šç³»ç»Ÿæ˜¯å¦å®‰è£…了ncursesã€zlib,若没有,å¯ä»¥æ‰§è¡Œï¼š ``` yum install ncurses-devel @@ -190,121 +180,28 @@ show slave status\G; ## 2.1. web管ç†ç³»ç»Ÿå¼€å‘环境安装 以linux环境为例: -下载java jdk,解压安装 - -é…置环境环境 -``` -vim /etc/profile -``` -添加如下内容: -``` -export JAVA_HOME=${jdk source dir} -CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar -PATH=$JAVA_HOME/bin:$PATH -export PATH JAVA_HOME CLASSPATH -``` -执行命令 -``` -source /etc/profile -``` -测试 -``` -java -version -``` - -maven安装 -下载maven,解压安装 -é…置环境å˜é‡ -``` -vim /etc/profile -``` -添加如下内容: -``` -export MAVEN_HOME=${maven source dir} -export PATH=$PATH:$MAVEN_HOME/bin -``` -执行命令 -``` -source /etc/profile -``` -测试 -``` -mvn -v -``` -**注æ„,如有需è¦ï¼Œä¿®æ”¹æœ¬åœ°ä»“库的ä½ç½®å’Œmavençš„æº** - -## 2.2. java语言框架开å‘环境安装 - -java jdkå’Œmaven环境é…置与上é¢web管ç†ç³»ç»Ÿçš„类似 +以官网æ供的nvm脚本安装 -**注æ„,java框架安装å‰ï¼Œéœ€è¦ä¿è¯æœºå™¨èƒ½è”网,åŒæ—¶ä¿®æ”¹maven安装目录下的conf目录的setting.xml,尽é‡ä½¿ç”¨å›½å†…çš„mavené•œåƒï¼Œå…·ä½“æ“作å¯ä»¥ç½‘上查看** - -下载tarsæºç ï¼Œè¿›å…¥javaæºç ç›®å½•ï¼Œå¹¶å®‰è£…在本地仓库 +执行以下命令: ``` -mvn clean install -mvn clean install -f core/client.pom.xml -mvn clean install -f core/server.pom.xml - +wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash +source ~/.bashrc ``` -构建web工程项目 -通过IDE或者maven创建一个maven web项目, -这里以eclipse为例,File -> New -> Project -> Maven Project -> maven-archetype-webapp,å†è¾“å…¥groupIdã€artifactId,生æˆå®Œæˆä¹‹åŽå¯ä»¥é€šè¿‡eclipse进行导入,目录结构如下 +node和带有负载功能的node应用的进程管ç†å™¨pm2安装 ``` -├── pom.xml -└── src - ├── main - │ ├── java - │ │ └── tars - │ ├── resources - │ └── webapp - └── test - ├── java - └── resources -``` - - -增加Mavenä¾èµ–é…ç½® -使用Tars框架时,需è¦ä¾èµ–框架æ供的jar包ä¾èµ–,以åŠå·¥å…·æ’件。在工程项目pom.xml文件中增加如下ä¾èµ–é…置。 -框架ä¾èµ–é…ç½® -```xml - - com.tencent.tars - tars-server - 1.4.0 - jar - -``` - -æ’件ä¾èµ–é…ç½® - -```xml - - com.tencent.tars - tars-maven-plugin - 1.4.0 - - - - ${basedir}/src/main/resources/hello.tars - - UTF-8 - true - ${basedir}/src/main/java - UTF-8 - com.qq.tars.quickstart.server. - - - +nvm install v8.11.3 +npm install -g pm2 --registry=https://registry.npm.taobao.org ``` -## 2.3. c++ å¼€å‘环境安装 -下载tarsæºç ï¼Œé¦–先进入cpp/thirdparty目录,执行thirdparty.sh脚本,下载ä¾èµ–çš„rapidjson +## 2.2. c++ å¼€å‘环境安装 +下载TarsFrameworkæºç  -然åŽè¿›å…¥cpp/buildæºç ç›®å½• +然åŽè¿›å…¥buildæºç ç›®å½• ``` -cd {$source_folder}/cpp/build +cd {$source_folder}/build chmod u+x build.sh +./build.sh prepare ./build.sh all ``` @@ -325,16 +222,16 @@ chown ${普通用户}:${普通用户} ./tars/ 安装 ``` -cd {$source_folder}/cpp/build +cd {$source_folder}/build ./build.sh install或者make install ``` **默认的安装路径为/usr/local/tars/cpp。** **如è¦ä¿®æ”¹å®‰è£…路径:** ``` -**需è¦ä¿®æ”¹build目录下CMakeLists.txt文件中的安装路径。** -**需è¦ä¿®æ”¹servant/makefile/makefile.tars文件中的TARS_PATH的路径** -**需è¦ä¿®æ”¹servant/script/create_tars_server.sh文件中的DEMO_PATH的路径** +**需è¦ä¿®æ”¹tarscpp目录下CMakeLists.txt文件中的安装路径。** +**需è¦ä¿®æ”¹tarscpp/servant/makefile/makefile.tars文件中的TARS_PATH的路径** +**需è¦ä¿®æ”¹tarscpp/servant/script/create_tars_server.sh文件中的DEMO_PATH的路径** ``` # 3. Tarsæ•°æ®åº“环境åˆå§‹åŒ– @@ -348,7 +245,7 @@ flush privileges; **注æ„${主机å}需è¦ä¿®æ”¹æˆè‡ªèº«æœºå™¨çš„å称,å¯ä»¥é€šè¿‡æŸ¥çœ‹/etc/hosts ## 3.2. 创建数æ®åº“ -sql脚本在cpp/framework/sql目录下,修改部署的ipä¿¡æ¯ +sql脚本在framework/sql目录下,修改部署的ipä¿¡æ¯ ``` sed -i "s/192.168.2.131/${your machine ip}/g" `grep 192.168.2.131 -rl ./*` sed -i "s/db.tars.com/${your machine ip}/g" `grep db.tars.com -rl ./*` @@ -390,7 +287,7 @@ tars_property是æœåŠ¡å±žæ€§ç›‘控数æ®å­˜å‚¨çš„æ•°æ®åº“ï¼› 通过管ç†å¹³å°éƒ¨ç½²çš„普通基础æœåŠ¡ï¼štarsstat, tarsproperty,tarsnotify, tarslog,tarsquerystat,tarsqueryproperty ``` -首先准备第一ç§æœåŠ¡çš„安装包,在cpp/build/目录下输入: +首先准备第一ç§æœåŠ¡çš„安装包,在build/目录下输入: ``` make framework-tar ``` @@ -517,57 +414,20 @@ chmod u+x tarsnode_install.sh ## 4.3. 安装web管ç†ç³»ç»Ÿ ->**执行之å‰å…ˆå®‰è£…tars java语言框架到本地仓库,å‚è§2.2章节** >管ç†ç³»ç»Ÿæºä»£ç ç›®å½•å称为**web** -修改é…置文件,文件存放的路径在web/src/main/resources/目录下。 -- app.config.properties - 修改为实际的数æ®åº“é…ç½® - -```ini -# æ•°æ®åº“(db_tars) -tarsweb.datasource.tars.addr={$your_db_ip}:3306 -tarsweb.datasource.tars.user=tars -tarsweb.datasource.tars.pswd=tars2015 - -# å‘布包存储路径 -upload.tgz.path=/usr/local/app/patchs/tars.upload/ +修改é…置文件,将é…置文件中的ip地å€ä¿®æ”¹ä¸ºæœ¬æœºip地å€ï¼Œå¦‚下: ``` - -- tars.conf - 替æ¢registry1.tars.com,registry2.tars.com为实际IP。å¯ä»¥åªé…置一个地å€ï¼Œå¤šä¸ªåœ°å€ç”¨å†’å·â€œ:â€è¿žæŽ¥ - -```xml - - - #proxy需è¦çš„é…ç½® - - #åœ°å€ - locator = tars.tarsregistry.QueryObj@tcp -h registry1.tars.com -p 17890:tars.tarsregistry.QueryObj@tcp -h registry2.tars.com -p 17890 - sync-invoke-timeout = 30000 - #最大超时时间(毫秒) - max-invoke-timeout = 30000 - #刷新端å£æ—¶é—´é—´éš”(毫秒) - refresh-endpoint-interval = 60000 - #模å—间调用[å¯é€‰] - stat = tars.tarsstat.StatObj - #网络异步回调线程个数 - asyncthread = 3 - modulename = tars.system - - - +cd web +sed -i 's/db.tars.com/${your_machine_ip}/g' config/webConf.js +sed -i 's/registry.tars.com/${your_machine_ip}/g' config/tars.conf ``` -打包,在web目录下执行命令,会在web/target目录下生æˆtars.war +安装web管ç†é¡µé¢ä¾èµ–,å¯åŠ¨web ``` -mvn clean package -``` - -webå‘布 -å°†tars.war放置到/usr/local/resin/webapps/中 -``` -cp ./target/tars.war /usr/local/resin/webapps/ +cd web +npm install --registry=https://registry.npm.taobao.org +npm run prd ``` 创建日志目录 @@ -575,25 +435,8 @@ cp ./target/tars.war /usr/local/resin/webapps/ mkdir -p /data/log/tars ``` -修改Resin安装目录下的conf/resin.xmlé…置文件 -将默认的é…ç½® -```xml - - - -``` -修改为 -```xml - - - -``` - -å¯åŠ¨resin -/usr/local/resin/bin/resin.sh start - 访问站点 -æµè§ˆå™¨è¾“å…¥${your machine ip}:8080,å³å¯çœ‹åˆ°ï¼Œå¦‚下: +æµè§ˆå™¨è¾“å…¥${your machine ip}:3000,å³å¯çœ‹åˆ°ï¼Œå¦‚下: ![tars](docs/images/tars_web_system_index.png) diff --git a/README.md b/README.md index 160babec7..b4e2879e3 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ For now it supports following languages: - Java - Nodejs - PHP +- Go ## Installation @@ -32,7 +33,20 @@ If you are familar to Tars, refer to scripts in directory [build](build), may so ## Performance -See [ docs/tars_performce.md](docs/tars_performce.md). +See [tars_performce.md](docs/tars_performce.md). + +## Submodule + +Directory |Features +------------------|---------------- +framework |Source code implementation of C++ language framework basic service +cpp |C++ language framework rpc source code implementation +java |java language framework rpc source code implementation +go |go language framework rpc source code implementation +nodejs |nodejs language framework rpc source code implementation +php |php language framework rpc source code implementation +tup |source code implementation of tup group protocol in each language +web |manage tars web source implementation ## Developer's documentation diff --git a/README.zh.md b/README.zh.md index fdb7bc75d..06768b950 100644 --- a/README.zh.md +++ b/README.zh.md @@ -36,6 +36,19 @@ Tars详细介ç»å‚è§[Introduction.md](Introduction.md)。 å‚è§[ docs/tars_performce.md](docs/tars_performce.md)。 +## å­é¡¹ç›® + +目录å称 |功能 +------------------|---------------- +framework |C++语言框架基础æœåŠ¡çš„æºç å®žçŽ° +cpp |C++语言框架rpcçš„æºç å®žçŽ° +java |java语言框架rpcçš„æºç å®žçŽ° +go |go语言框架rpcçš„æºç å®žçŽ° +nodejs |nodejs语言框架rpcçš„æºç å®žçŽ° +php |php语言框架rpcçš„æºç å®žçŽ° +tup |tup组包åè®®å„个语言的æºç å®žçŽ° +web |管ç†tarsçš„webæºç å®žçŽ° + ## å¼€å‘技术文档 å‚è§[docs](docs)目录。 diff --git a/build/install.sh b/build/install.sh index 07969a6b1..298f8eb30 100644 --- a/build/install.sh +++ b/build/install.sh @@ -24,45 +24,6 @@ make make install cd - -##°²×°java jdk -tar zxvf jdk-8u111-linux-x64.tar.gz -echo "export JAVA_HOME=${PWD_DIR}/jdk1.8.0_111" >> /etc/profile -echo "CLASSPATH=\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> /etc/profile -echo "PATH=\$JAVA_HOME/bin:\$PATH" >> /etc/profile -echo "export PATH JAVA_HOME CLASSPATH" >> /etc/profile - -source /etc/profile - -java -version - -##°²×°maven -tar zxvf apache-maven-3.3.9-bin.tar.gz -echo "export MAVEN_HOME=${PWD_DIR}/apache-maven-3.3.9/" >> /etc/profile -echo "export PATH=\$PATH:\$MAVEN_HOME/bin" >> /etc/profile - -source /etc/profile - -mvn -v - -##°²×°resin - -cp resin-4.0.49.tar.gz /usr/local/ -cd /usr/local/ -tar zxvf resin-4.0.49.tar.gz -cd resin-4.0.49 -./configure --prefix=/usr/local/resin-4.0.49 -make -make install -cd ${PWD_DIR} -ln -s /usr/local/resin-4.0.49 /usr/local/resin - -##°²×°rapidjson -yum install -y git - -git clone https://github.com/Tencent/rapidjson.git - -cp -r ./rapidjson ../cpp/thirdparty/ - ## °²×°mysql yum install -y ncurses-devel yum install -y zlib-devel @@ -77,14 +38,15 @@ if [ ! -n "$MysqlIncludePath" ] ln -s /usr/local/mysql-5.6.26 /usr/local/mysql cd - else - ## ¸ù¾Ýmysql ¿â·¾¶ ÅäÖà ÉèÖÃcpp/build/CMakeLists.txt - sed -i "s@/usr/local/mysql/include@${MysqlIncludePath}@g" ../cpp/build/CMakeLists.txt - sed -i "s@/usr/local/mysql/lib@${MysqlLibPath}@g" ../cpp/build/CMakeLists.txt + ## ¸ù¾Ýmysql ¿â·¾¶ ÅäÖà ÉèÖÃframework/CMakeLists.txt tarscpp/CMakeList.txt + sed -i "s@/usr/local/mysql/include@${MysqlIncludePath}@g" ../framework/CMakeLists.txt + sed -i "s@/usr/local/mysql/lib@${MysqlLibPath}@g" ../framework/CMakeLists.txt + sed -i "s@/usr/local/mysql/include@${MysqlIncludePath}@g" ../framework/tarscpp/CMakeLists.txt + sed -i "s@/usr/local/mysql/lib@${MysqlLibPath}@g" ../framework/tarscpp/CMakeLists.txt fi - yum install -y perl cd /usr/local/mysql useradd mysql @@ -120,16 +82,14 @@ cd - echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf ldconfig - -##°²×°javaÓïÑÔ¿ò¼Ü -cd ../java/ -mvn clean install -mvn clean install -f core/client.pom.xml -mvn clean install -f core/server.pom.xml +##ÏÂÔØC++»ù´¡·þÎñ¿ò¼Ü +yum install -y git +cd ../ +git submodule update --init --recursive framework cd - ##°²×°c++ÓïÑÔ¿ò¼Ü -cd ../cpp/build/ +cd ../framework/build/ chmod u+x build.sh ./build.sh all ./build.sh install @@ -141,7 +101,7 @@ mysql -uroot -proot@appinside -e "grant all on *.* to 'tars'@'localhost' identif mysql -uroot -proot@appinside -e "grant all on *.* to 'tars'@'${MachineName}' identified by 'tars2015' with grant option;" mysql -uroot -proot@appinside -e "flush privileges;" -cd ../cpp/framework/sql/ +cd ../framework/sql/ sed -i "s/192.168.2.131/${MachineIp}/g" `grep 192.168.2.131 -rl ./*` sed -i "s/db.tars.com/${MachineIp}/g" `grep db.tars.com -rl ./*` chmod u+x exec-sql.sh @@ -149,7 +109,7 @@ chmod u+x exec-sql.sh cd - ##´ò°ü¿ò¼Ü»ù´¡·þÎñ -cd ../cpp/build/ +cd ../framework/build/ make framework-tar make tarsstat-tar @@ -162,7 +122,7 @@ cd - ##°²×°ºËÐÄ»ù´¡·þÎñ mkdir -p /usr/local/app/tars/ -cd ../cpp/build/ +cd ../framework/build/ cp framework.tgz /usr/local/app/tars/ cd /usr/local/app/tars tar xzfv framework.tgz @@ -177,19 +137,22 @@ chmod u+x tars_install.sh ./tarspatch/util/init.sh -##°²×°web¹ÜÀíϵͳ -cd ${PWD_DIR} -cd ../web/ -sed -i "s/db.tars.com/${MachineIp}/g" `grep db.tars.com -rl ./src/main/resources/*` -sed -i "s/registry1.tars.com/${MachineIp}/g" `grep registry1.tars.com -rl ./src/main/resources/*` -sed -i "s/registry2.tars.com/${MachineIp}/g" `grep registry2.tars.com -rl ./src/main/resources/*` +##°²×°nodejs»·¾³ +wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash +source ~/.bashrc +nvm install v8.11.3 -mvn clean package -cp ./target/tars.war /usr/local/resin/webapps/ +##°²×°web¹ÜÀíϵͳ +cd ../ +git submodule update --init --recursive web +cd web/ +npm install -g pm2 --registry=https://registry.npm.taobao.org +sed -i "s/registry.tars.com/${MachineIp}/g" `grep registry1.tars.com -rl ./config/*` +sed -i "s/db.tars.com/${MachineIp}/g" `grep db.tars.com -rl ./config/*` +npm install --registry=https://registry.npm.taobao.org +npm run prd cd - mkdir -p /data/log/tars/ -cp ./conf/resin.xml /usr/local/resin/conf/ -/usr/local/resin/bin/resin.sh start diff --git a/cpp b/cpp new file mode 160000 index 000000000..09367c9ab --- /dev/null +++ b/cpp @@ -0,0 +1 @@ +Subproject commit 09367c9abbd067a731ff52da74211b650ffd63c7 diff --git a/cpp/README.md b/cpp/README.md deleted file mode 100644 index 95e9fe918..000000000 --- a/cpp/README.md +++ /dev/null @@ -1,34 +0,0 @@ -该工程是Tars框架C++语言的æºä»£ç  - -目录å称 |功能 -------------------|---------------- -build |C++语言框架编译和安装的目录 -framework |C++语言框架基础æœåŠ¡çš„æºç å®žçŽ° -servant |C++语言框架rpcçš„æºç å®žçŽ° -tools |C++语言框架IDL工具的æºç å®žçŽ° -util |C++语言框架基础工具库的æºç å®žçŽ° -examples |C++语言框架的示例代ç ï¼ŒåŒ…括:快速入门示例ã€promise编程入门示例ã€åŽ‹æµ‹ç¨‹åºç¤ºä¾‹ -test |C++语言框架å„ä¸ªéƒ¨åˆ†çš„æµ‹è¯•ç¨‹åº -doc |文档说明 -thirdparty |ä¾èµ–第三方组件或者库的存放目录,目å‰æ¡†æž¶åŸºç¡€æœåŠ¡QueryStatServer用到了rapidjson,需è¦ä¸‹è½½æ”¾åˆ°è¿™ä¸ªç›®å½• - -ä¾èµ–环境 - -软件 |软件è¦æ±‚ -------|-------- -linux内核版本: | 2.6.18åŠä»¥ä¸Šç‰ˆæœ¬ -gcc版本: | 4.1.2åŠä»¥ä¸Šç‰ˆæœ¬ã€glibc-devel -bison工具版本: | 2.5åŠä»¥ä¸Šç‰ˆæœ¬ -flex工具版本: | 2.5åŠä»¥ä¸Šç‰ˆæœ¬ -cmake版本: | 2.8.8åŠä»¥ä¸Šç‰ˆæœ¬ -mysql版本: | 4.1.17åŠä»¥ä¸Šç‰ˆæœ¬ -rapidjson版本: | 1.0.2版本 - -编译安装 - -``` -cd ./build -./build.sh all -./build.sh install -``` -详细安装å‚è§Install.md \ No newline at end of file diff --git a/cpp/build/CMakeLists.txt b/cpp/build/CMakeLists.txt deleted file mode 100644 index d0232e14d..000000000 --- a/cpp/build/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -project(tars_cpp) - -set(CMAKE_VERBOSE_MAKEFILE off) - -set(MYSQL_DIR_INC "/usr/local/mysql/include") -set(MYSQL_DIR_LIB "/usr/local/mysql/lib") - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O2 -Wall -Wno-deprecated") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Wall -Wno-deprecated") - -#set(CMAKE_BUILD_TYPE "Debug") - -set(TARS_VERSION "1.1.0") -add_definitions(-DTARS_VERSION="${TARS_VERSION}") -set(TARS_SSL 0) -add_definitions(-DTARS_SSL=${TARS_SSL}) -set(TARS_HTTP2 0) -add_definitions(-DTARS_HTTP2=${TARS_HTTP2}) - -set(INSTALL_PREFIX "/usr/local/tars/cpp") - -set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX}) - -add_subdirectory(util) -add_subdirectory(tools) - -set(TARS2CPP "${tools_BINARY_DIR}/tars2cpp/tars2cpp") - -add_subdirectory(servant) -add_subdirectory(framework) - -add_subdirectory(test) diff --git a/cpp/build/README.md b/cpp/build/README.md deleted file mode 100644 index 865d0df26..000000000 --- a/cpp/build/README.md +++ /dev/null @@ -1,14 +0,0 @@ -Tars构建工程 - -编译 -``` -build.sh all -``` -æ¸…ç† -``` -build.sh cleanall -``` -安装 -``` -build.sh install -``` \ No newline at end of file diff --git a/cpp/build/build.sh b/cpp/build/build.sh deleted file mode 100755 index 08772fc5b..000000000 --- a/cpp/build/build.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/sh - -ARGS=$1 - -if [ $# -lt 1 ]; then - ARGS="help" -fi - -BASEPATH=$(cd `dirname $0`; pwd) - -case $ARGS in - all) - cd $BASEPATH; cp CMakeLists.txt ../; cmake ..; make - ;; - cleanall) - cd $BASEPATH; make clean; rm -rf CMakeFiles/ CMakeCache.txt Makefile util/ tools/ servant/ framework/ test/ cmake_install.cmake *.tgz install_manifest.txt - ;; - install) - cd $BASEPATH; make install - ;; - help|*) - echo "Usage:" - echo "$0 help: view help info." - echo "$0 all: build all target" - echo "$0 install: install framework" - echo "$0 cleanall: remove all temp file" - ;; -esac - - diff --git a/cpp/examples/CoroutineDemo/AServer/AServant.tars b/cpp/examples/CoroutineDemo/AServer/AServant.tars deleted file mode 100644 index 5487126d1..000000000 --- a/cpp/examples/CoroutineDemo/AServer/AServant.tars +++ /dev/null @@ -1,14 +0,0 @@ - -module Test -{ - -interface AServant -{ - int test(); - - int testInt(int iIn,out int iOut); - - int testStr(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/CoroutineDemo/AServer/AServantImp.cpp b/cpp/examples/CoroutineDemo/AServer/AServantImp.cpp deleted file mode 100644 index f2b162ec0..000000000 --- a/cpp/examples/CoroutineDemo/AServer/AServantImp.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AServantImp.h" -#include "AServer.h" - -using namespace std; - -////////////////////////////////////////////////////// -void AServantImp::initialize() -{ - //initialize servant here: - //... -} - -////////////////////////////////////////////////////// -void AServantImp::destroy() -{ - //destroy servant here: - //... -} - - -int AServantImp::test(tars::TarsCurrentPtr current) -{ - return 0; -} - -tars::Int32 AServantImp::testInt(tars::Int32 iIn,tars::Int32 &iOut,tars::TarsCurrentPtr current) -{ - - iOut = iIn; - - return 0; -} - -tars::Int32 AServantImp::testStr(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - - sOut = sIn; - - return 0; -} diff --git a/cpp/examples/CoroutineDemo/AServer/AServantImp.h b/cpp/examples/CoroutineDemo/AServer/AServantImp.h deleted file mode 100644 index ebfbf88c9..000000000 --- a/cpp/examples/CoroutineDemo/AServer/AServantImp.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _AServantImp_H_ -#define _AServantImp_H_ - -#include "servant/Application.h" -#include "AServant.h" - -/** - * - * - */ -class AServantImp : public Test::AServant -{ -public: - /** - * - */ - virtual ~AServantImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual int test(tars::TarsCurrentPtr current); - - tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,tars::TarsCurrentPtr current); - - tars::Int32 testStr(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - -private: - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/CoroutineDemo/AServer/AServer.cpp b/cpp/examples/CoroutineDemo/AServer/AServer.cpp deleted file mode 100644 index e08d1a55b..000000000 --- a/cpp/examples/CoroutineDemo/AServer/AServer.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AServer.h" -#include "AServantImp.h" - -using namespace std; - -AServer g_app; - -///////////////////////////////////////////////////////////////// -void -AServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".AServantObj"); -} -///////////////////////////////////////////////////////////////// -void -AServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// -int main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/CoroutineDemo/AServer/AServer.h b/cpp/examples/CoroutineDemo/AServer/AServer.h deleted file mode 100644 index db2f022b3..000000000 --- a/cpp/examples/CoroutineDemo/AServer/AServer.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _AServer_H_ -#define _AServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class AServer : public Application -{ -public: - /** - * - **/ - virtual ~AServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); - -}; - -extern AServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/CoroutineDemo/AServer/makefile b/cpp/examples/CoroutineDemo/AServer/makefile deleted file mode 100644 index 9922db3a3..000000000 --- a/cpp/examples/CoroutineDemo/AServer/makefile +++ /dev/null @@ -1,12 +0,0 @@ - -#----------------------------------------------------------------------- -APP := Test -TARGET := AServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/CoroutineDemo/BServer/AServant.tars b/cpp/examples/CoroutineDemo/BServer/AServant.tars deleted file mode 100644 index 5487126d1..000000000 --- a/cpp/examples/CoroutineDemo/BServer/AServant.tars +++ /dev/null @@ -1,14 +0,0 @@ - -module Test -{ - -interface AServant -{ - int test(); - - int testInt(int iIn,out int iOut); - - int testStr(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/CoroutineDemo/BServer/BServant.tars b/cpp/examples/CoroutineDemo/BServer/BServant.tars deleted file mode 100644 index 7c83e510f..000000000 --- a/cpp/examples/CoroutineDemo/BServer/BServant.tars +++ /dev/null @@ -1,15 +0,0 @@ - -module Test -{ - -interface BServant -{ - int test(); - - int testCoroSerial(string sIn, out string sOut); - - int testCoroParallel(string sIn, out string sOut); - -}; - -}; diff --git a/cpp/examples/CoroutineDemo/BServer/BServantImp.cpp b/cpp/examples/CoroutineDemo/BServer/BServantImp.cpp deleted file mode 100644 index e09bde32f..000000000 --- a/cpp/examples/CoroutineDemo/BServer/BServantImp.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "BServantImp.h" -#include "BServer.h" -#include "servant/Application.h" -#include "servant/Communicator.h" - -using namespace std; -using namespace tars; - - -////////////////////////////////////////////////////// -void BServantImp::initialize() -{ - //initialize servant here: - //... - _pPrx = Application::getCommunicator()->stringToProxy("Test.AServer.AServantObj"); -} -////////////////////////////////////////////////////// -void BServantImp::destroy() -{ -} - -class AServantCoroCallback : public AServantCoroPrxCallback -{ -public: - virtual ~AServantCoroCallback(){} - - virtual void callback_testInt(tars::Int32 ret, tars::Int32 iOut) - { - _iRet = ret; - _iOut = iOut; - } - virtual void callback_testInt_exception(tars::Int32 ret) - { - _iException = ret; - } - - virtual void callback_testStr(tars::Int32 ret, const std::string& sOut) - { - _iRet = ret; - _sOut = sOut; - } - virtual void callback_testStr_exception(tars::Int32 ret) - { - _iException = ret; - } - -public: - int _iException; - int _iRet; - int _iOut; - string _sOut; -}; -typedef tars::TC_AutoPtr AServantCoroCallbackPtr; - -int BServantImp::test(tars::TarsCurrentPtr current) { return 0;} - -tars::Int32 BServantImp::testCoroSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - try - { - int iRet = -1; - - int iIn = 5; - int iOut = 0; - - iRet = _pPrx->testInt(iIn, iOut); - - if(iRet == 0) - { - string sRet(""); - - iRet = _pPrx->testStr(sIn, sRet); - - if(iRet == 0) - { - sOut = sRet; - } - } - - return iRet; - } - catch(exception &ex) - { - TLOGERROR("BServantImp::testCoroSerial exception:" << ex.what() << endl); - } - - return -1; -} - -tars::Int32 BServantImp::testCoroParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - try - { - int iRet = -1; - - int iIn = 5; - - CoroParallelBasePtr sharedPtr = new CoroParallelBase(2); - - AServantCoroCallbackPtr cb1 = new AServantCoroCallback(); - cb1->setCoroParallelBasePtr(sharedPtr); - _pPrx->coro_testInt(cb1, iIn); - - AServantCoroCallbackPtr cb2 = new AServantCoroCallback(); - cb2->setCoroParallelBasePtr(sharedPtr); - _pPrx->coro_testStr(cb2, sIn); - - coroWhenAll(sharedPtr); - - if(cb1->_iRet == 0 && cb2->_iRet == 0) - { - sOut = cb2->_sOut; - iRet = 0; - } - - return iRet; - } - catch(exception &ex) - { - TLOGERROR("BServantImp::testCoroParallel exception:" << ex.what() << endl); - } - - return -1; -} diff --git a/cpp/examples/CoroutineDemo/BServer/BServantImp.h b/cpp/examples/CoroutineDemo/BServer/BServantImp.h deleted file mode 100644 index b20d57726..000000000 --- a/cpp/examples/CoroutineDemo/BServer/BServantImp.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServantImp_H_ -#define _BServantImp_H_ - -#include "servant/Application.h" -#include "BServant.h" -#include "AServant.h" - -using namespace Test; - -/** - * - * - */ -class BServantImp : public Test::BServant -{ -public: - /** - * - */ - virtual ~BServantImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual int test(tars::TarsCurrentPtr current); - - tars::Int32 testCoroSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - - tars::Int32 testCoroParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - -private: - AServantPrx _pPrx; - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/CoroutineDemo/BServer/BServer.cpp b/cpp/examples/CoroutineDemo/BServer/BServer.cpp deleted file mode 100644 index 73b2f868e..000000000 --- a/cpp/examples/CoroutineDemo/BServer/BServer.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "BServer.h" -#include "BServantImp.h" - -using namespace std; - -BServer g_app; - -///////////////////////////////////////////////////////////////// -void BServer::initialize() -{ - //initialize application here: - //... - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".BServantObj"); -} -///////////////////////////////////////////////////////////////// -void BServer::destroyApp() -{ - //destroy application here: - //... -} - -int main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/CoroutineDemo/BServer/BServer.h b/cpp/examples/CoroutineDemo/BServer/BServer.h deleted file mode 100644 index d11225796..000000000 --- a/cpp/examples/CoroutineDemo/BServer/BServer.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServer_H_ -#define _BServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class BServer : public Application -{ -public: - /** - * - **/ - virtual ~BServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); - -}; - -extern BServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/CoroutineDemo/BServer/makefile b/cpp/examples/CoroutineDemo/BServer/makefile deleted file mode 100644 index 4c9555b90..000000000 --- a/cpp/examples/CoroutineDemo/BServer/makefile +++ /dev/null @@ -1,12 +0,0 @@ - -#----------------------------------------------------------------------- -APP := Test -TARGET := BServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/CoroutineDemo/README.md b/cpp/examples/CoroutineDemo/README.md deleted file mode 100644 index d8d528389..000000000 --- a/cpp/examples/CoroutineDemo/README.md +++ /dev/null @@ -1,7 +0,0 @@ -该工程是Tars åç¨‹ç¼–ç¨‹ç¤ºä¾‹çš„ä»£ç  - - -目录å称 |功能 ------------------|---------------- -client/BServer/AServer | å程编程的示例程åºï¼Œclient访问BServer,BServer用å程方å¼åŽ»å¹¶è¡Œå’Œä¸²è¡Œè®¿é—®AServer -testCoro/testParallelCoro | å程编程的示例程åºï¼Œè‡ªå®šä¹‰æˆ–者继承框架的å程类 diff --git a/cpp/examples/CoroutineDemo/client/BServant.tars b/cpp/examples/CoroutineDemo/client/BServant.tars deleted file mode 100644 index 7c83e510f..000000000 --- a/cpp/examples/CoroutineDemo/client/BServant.tars +++ /dev/null @@ -1,15 +0,0 @@ - -module Test -{ - -interface BServant -{ - int test(); - - int testCoroSerial(string sIn, out string sOut); - - int testCoroParallel(string sIn, out string sOut); - -}; - -}; diff --git a/cpp/examples/CoroutineDemo/client/main.cpp b/cpp/examples/CoroutineDemo/client/main.cpp deleted file mode 100644 index 9f7a92ea5..000000000 --- a/cpp/examples/CoroutineDemo/client/main.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "BServant.h" -#include "servant/Communicator.h" -#include "util/tc_thread_pool.h" -#include - -using namespace std; -using namespace Test; -using namespace tars; - -class Test1 -{ -public: - Test1(const string &sStr); - - ~Test1(); - - void queryResult(int iFlag, int iExecuteNum); - -private: - Communicator _comm; - BServantPrx _prx; -}; - -Test1::Test1(const string &sStr) -{ - _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 10.208.139.242 -p 17890 -t 10000"); - _comm.setProperty("stat", "tars.tarsstat.StatObj"); - _comm.stringToProxy(sStr, _prx); -} - -Test1::~Test1() -{ - -} - -void Test1::queryResult(int iFlag, int iExecuteNum) -{ - string sIn(10,'a'); - string sOut(""); - - tars::Int32 count = 0; - unsigned long sum = 0; - - time_t _iTime=TC_TimeProvider::getInstance()->getNowMs(); - - for(int i=0; itestCoroSerial(sIn, sOut); - } - else - { - ret = _prx->testCoroParallel(sIn, sOut); - } - - if(ret == 0) - { - ++sum; - ++count; - if(count == iExecuteNum) - { - cout << "pthread id: " << pthread_self() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl; - _iTime=TC_TimeProvider::getInstance()->getNowMs(); - count = 0; - } - } - } - catch(TC_Exception &e) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl; - } - } - cout << "succ:" << sum << endl; - cout << "sOut:" << sOut << endl; -} - -int main(int argc,char ** argv) -{ - if(argc != 5) - { - cout << "usage: " << argv[0] << " sObj ThreadNum CallTimes CallMode" << endl; - return -1; - } - - string s = string(argv[1]); - - Test1 test1(s); - - try - { - tars::Int32 threads = TC_Common::strto(string(argv[2])); - - TC_ThreadPool tp; - tp.init(threads); - tp.start(); - - tars::Int32 times = TC_Common::strto(string(argv[3])); - tars::Int32 callMode = TC_Common::strto(string(argv[4])); - - for(int i = 0; i - -using namespace std; -using namespace Test; -using namespace tars; - -//继承框架的å程类 -class TestCoroutine : public Coroutine -{ -public: - TestCoroutine(int iNum, const string &sObj); - - ~TestCoroutine() {} - - void handle(); - -private: - int _num; - string _sObj; - Communicator _comm; - BServantPrx _prx; -}; - -TestCoroutine::TestCoroutine(int iNum, const string &sObj) -: _num(iNum) -, _sObj(sObj) -{ - _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 10.208.139.242 -p 17890 -t 10000"); - _comm.stringToProxy(_sObj, _prx); -} - -void TestCoroutine::handle() -{ - string sIn(32,'a'); - string sOut(""); - unsigned long sum = 0; - - for(int i = 0; i < _num; i++) - { - try - { - int iRet = _prx->testCoroSerial(sIn, sOut); - if(iRet == 0) - { - ++sum; - } - - sOut = ""; - iRet = _prx->testCoroParallel(sIn, sOut); - if(iRet == 0) - { - ++sum; - } - } - catch(TC_Exception &e) - { - cout << "i: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "i: " << i << "unknown exception." << endl; - } - } - cout << "succ:" << sum <(string(argv[1])); - - string sObj = string(argv[2]); - - TestCoroutine testCoro(iNum, sObj); - - testCoro.setCoroInfo(10, 128, 128*1024); - - testCoro.start(); - - testCoro.getThreadControl().join(); - - return 0; -} diff --git a/cpp/examples/CoroutineDemo/testCoro/makefile b/cpp/examples/CoroutineDemo/testCoro/makefile deleted file mode 100644 index 432ad4939..000000000 --- a/cpp/examples/CoroutineDemo/testCoro/makefile +++ /dev/null @@ -1,12 +0,0 @@ - -#----------------------------------------------------------------------- -APP := Test -TARGET := testCoro -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/CoroutineDemo/testParallelCoro/BServant.tars b/cpp/examples/CoroutineDemo/testParallelCoro/BServant.tars deleted file mode 100644 index 7c83e510f..000000000 --- a/cpp/examples/CoroutineDemo/testParallelCoro/BServant.tars +++ /dev/null @@ -1,15 +0,0 @@ - -module Test -{ - -interface BServant -{ - int test(); - - int testCoroSerial(string sIn, out string sOut); - - int testCoroParallel(string sIn, out string sOut); - -}; - -}; diff --git a/cpp/examples/CoroutineDemo/testParallelCoro/main.cpp b/cpp/examples/CoroutineDemo/testParallelCoro/main.cpp deleted file mode 100644 index aaeb77adb..000000000 --- a/cpp/examples/CoroutineDemo/testParallelCoro/main.cpp +++ /dev/null @@ -1,276 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "BServant.h" -#include "servant/Communicator.h" -#include "servant/CoroutineScheduler.h" -#include - -using namespace std; -using namespace Test; -using namespace tars; - -class BServantCoroCallback : public BServantCoroPrxCallback -{ -public: - virtual ~BServantCoroCallback(){} - - virtual void callback_testCoroSerial(tars::Int32 ret, const std::string &sOut) // override - { - _iRet = ret; - _sOut = sOut; - } - virtual void callback_testCoroSerial_exception(tars::Int32 ret) // override - { - _iException = ret; - } - - virtual void callback_testCoroParallel(tars::Int32 ret, const std::string &sOut) // override - { - _iRet = ret; - _sOut = sOut; - } - - virtual void callback_testCoroParallel_exception(tars::Int32 ret) // override - { - _iException = ret; - } - -public: - int _iException; - int _iRet; - int _iOut; - string _sOut; -}; -typedef tars::TC_AutoPtr BServantCoroCallbackPtr; - -//自定义å程类 -class CoroutineClass : public TC_Thread -{ -public: - /** - * 构造函数 - */ - CoroutineClass(); - - /** - * æžæž„函数 - */ - virtual ~CoroutineClass(); - - /** - * 返回0,代表æˆåŠŸï¼Œ-1,表示失败 - */ - int registerFunc(const vector< tars::TC_Callback > &vFunc); - - /** - * 线程åˆå§‹åŒ– - */ - virtual void initialize() {} - - /** - * 线程处ç†æ–¹æ³• - */ - virtual void run(); - - /** - * åœæ­¢çº¿ç¨‹ - */ - void terminate(); - -protected: - /** - * 线程已ç»å¯åŠ¨, 进入具体å程处ç†å‰è°ƒç”¨ - */ - virtual void startCoro() {} - - /** - * 线程马上è¦é€€å‡ºæ—¶è°ƒç”¨ - */ - virtual void stopCoro() {} - - /** - * 具体的处ç†é€»è¾‘ - */ - virtual void handleCoro(); - -protected: - CoroutineScheduler *_coroSched; - uint32_t _iPoolSize; - size_t _iStackSize; - vector< tars::TC_Callback > _vFunc; -}; - -CoroutineClass::CoroutineClass() -: _coroSched(NULL) -, _iPoolSize(1024) -, _iStackSize(128*1024) -{ -} - -CoroutineClass::~CoroutineClass() -{ - if(isAlive()) - { - terminate(); - - getThreadControl().join(); - } -} - -int CoroutineClass::registerFunc(const vector< tars::TC_Callback > &vFunc) -{ - if(vFunc.size() > _iPoolSize || vFunc.size() <= 0) - { - return -1; - } - - _vFunc = vFunc; - - return 0; -} - -void CoroutineClass::run() -{ - initialize(); - - startCoro(); - - handleCoro(); - - stopCoro(); -} - -void CoroutineClass::terminate() -{ - if(_coroSched) - { - _coroSched->terminate(); - } -} - -void CoroutineClass::handleCoro() -{ - _coroSched = new CoroutineScheduler(); - - _coroSched->init(_iPoolSize, _iStackSize); - - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - - assert(pSptd != NULL); - - pSptd->_sched = _coroSched; - - for(size_t i = 0; i < _vFunc.size(); ++i) - { - _coroSched->createCoroutine(_vFunc[i]); - } - - _coroSched->run(); - - delete _coroSched; - _coroSched = NULL; -} - -//////////////////////////////////////////// -//继承框架的å程类 -class TestCoroutine : public Coroutine -{ -public: - TestCoroutine(int iNum, const string &sObj); - - ~TestCoroutine() {} - - void handle(); - -private: - int _num; - string _sObj; - Communicator _comm; - BServantPrx _prx; -}; - -TestCoroutine::TestCoroutine(int iNum, const string &sObj) -: _num(iNum) -, _sObj(sObj) -{ - _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 10.208.139.242 -p 17890 -t 10000"); - _comm.stringToProxy(_sObj, _prx); -} - -void TestCoroutine::handle() -{ - string sIn(32,'a'); - unsigned long sum = 0; - - for(int i = 0; i < _num; i++) - { - try - { - CoroParallelBasePtr sharedPtr = new CoroParallelBase(2); - - BServantCoroCallbackPtr cb1 = new BServantCoroCallback(); - cb1->setCoroParallelBasePtr(sharedPtr); - _prx->coro_testCoroSerial(cb1, sIn); - - BServantCoroCallbackPtr cb2 = new BServantCoroCallback(); - cb2->setCoroParallelBasePtr(sharedPtr); - _prx->coro_testCoroParallel(cb2, sIn); - - coroWhenAll(sharedPtr); - - cout << "ret1:" << cb1->_sOut << "|ret2:" << cb2->_sOut << endl; - ; - if(cb1->_iRet == 0 && cb2->_iRet == 0 && cb1->_iException == 0 && cb2->_iException == 0) - { - ++sum; - } - } - catch(TC_Exception &e) - { - cout << "i: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "i: " << i << "unknown exception." << endl; - } - - } - cout << "succ:" << sum <(string(argv[1])); - - string sObj = string(argv[2]); - - TestCoroutine testCoro(iNum, sObj); - - testCoro.setCoroInfo(10, 128, 128*1024); - - testCoro.start(); - - testCoro.getThreadControl().join(); - - return 0; -} diff --git a/cpp/examples/CoroutineDemo/testParallelCoro/makefile b/cpp/examples/CoroutineDemo/testParallelCoro/makefile deleted file mode 100644 index 432ad4939..000000000 --- a/cpp/examples/CoroutineDemo/testParallelCoro/makefile +++ /dev/null @@ -1,12 +0,0 @@ - -#----------------------------------------------------------------------- -APP := Test -TARGET := testCoro -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/HttpDemo/HttpClient/main.cpp b/cpp/examples/HttpDemo/HttpClient/main.cpp deleted file mode 100644 index 764e48222..000000000 --- a/cpp/examples/HttpDemo/HttpClient/main.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include "util/tc_http.h" -#include "util/tc_common.h" -#include "util/tc_clientsocket.h" -#include "util/tc_thread_pool.h" -#include "tup/Tars.h" -#include "tup/tup.h" -#include "util/tc_timeprovider.h" -using namespace std; -using namespace tars; -using namespace tup; - -int doRequest(TC_HttpRequest& stHttp,TC_TCPClient&tcpClient, TC_HttpResponse &stHttpRsp, int iTimeout) -{ - string sSendBuffer = stHttp.encode(); - - int iRet = tcpClient.send(sSendBuffer.c_str(), sSendBuffer.length()); - if(iRet != TC_ClientSocket::EM_SUCCESS) - { - return iRet; - } - - stHttpRsp.reset(); - - string sBuffer; - - char *sTmpBuffer = new char[10240]; - size_t iRecvLen = 10240; - - while(true) - { - iRecvLen = 10240; - - iRet = tcpClient.recv(sTmpBuffer, iRecvLen); - - if(iRet == TC_ClientSocket::EM_SUCCESS) - sBuffer.append(sTmpBuffer, iRecvLen); - - switch(iRet) - { - case TC_ClientSocket::EM_SUCCESS: - if(stHttpRsp.incrementDecode(sBuffer)) - { - delete []sTmpBuffer; - return TC_ClientSocket::EM_SUCCESS; - } - continue; - case TC_ClientSocket::EM_CLOSE: - delete []sTmpBuffer; - stHttpRsp.incrementDecode(sBuffer); - return TC_ClientSocket::EM_SUCCESS; - default: - delete []sTmpBuffer; - return iRet; - } - } - - assert(true); - - return 0; -} - -void th_dohandle(int excut_num, int iSplit) -{ - tars::Int32 count = 0; - unsigned long sum = 0; - unsigned long id = 1; - int64_t _iTime = TC_TimeProvider::getInstance()->getNowMs(); - - TC_HttpRequest stHttpReq; - stHttpReq.setCacheControl("no-cache"); - - string sServer1("http://10.120.129.226:10024/"); - - TC_TCPClient tcpClient1; - tcpClient1.init("10.120.129.226", 10024, 3000); - - int iRet = 0; - - for (int i = 0; igetNowMs() - _iTime <<"(ms)"<< endl; - _iTime=TC_TimeProvider::getInstance()->getNowMs(); - count = 0; - } - } - else - { - cout <<"pthread id: " << pthread_self()<< "|iRet:"<(string(argv[1])); - TC_ThreadPool tp; - tp.init(threads); - tp.start(); - cout << "init tp succ" << endl; - tars::Int32 times = TC_Common::strto(string(argv[2])); - - int iSplit = 1; - if(argc == 4) - { - iSplit = TC_Common::strto(string(argv[3])); - } - auto fwrapper3 = std::bind(&th_dohandle, times, iSplit); - for(int i = 0; i &buffer) -{ - TC_HttpRequest request; - vector v = current->getRequestBuffer(); - string sBuf; - sBuf.assign(&v[0],v.size()); - request.decode(sBuf); - TC_HttpResponse rsp; - string s="hello"; - rsp.setResponse(s.c_str(),s.size()); - rsp.encode(buffer); - - return 0; -} diff --git a/cpp/examples/HttpDemo/HttpServer/HttpImp.h b/cpp/examples/HttpDemo/HttpServer/HttpImp.h deleted file mode 100644 index 4e330b102..000000000 --- a/cpp/examples/HttpDemo/HttpServer/HttpImp.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _HttpImp_H_ -#define _HttpImp_H_ - -#include "servant/Application.h" - -/** - * - * - */ -class HttpImp : public Servant -{ -public: - /** - * - */ - virtual ~HttpImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - int doRequest(TarsCurrentPtr current, vector &buffer); - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/HttpDemo/HttpServer/HttpServer.cpp b/cpp/examples/HttpDemo/HttpServer/HttpServer.cpp deleted file mode 100644 index 4f2a05ee8..000000000 --- a/cpp/examples/HttpDemo/HttpServer/HttpServer.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "HttpServer.h" -#include "HttpImp.h" - -using namespace std; - -HttpServer g_app; - -///////////////////////////////////////////////////////////////// -struct HttpProtocol -{ - /** - * ½âÎöhttpÇëÇó - * @param in - * @param out - * - * @return int - */ - static int parseHttp(string &in, string &out) - { - try - { - //ÅжÏÇëÇóÊÇ·ñÊÇHTTPÇëÇó - bool b = TC_HttpRequest ::checkRequest(in.c_str(), in.length()); - //ÍêÕûµÄHTTPÇëÇó - if(b) - { - out = in; - in = ""; - //TLOGDEBUG("out size: " << out.size() << endl); - return TC_EpollServer::PACKET_FULL; - } - else - { - return TC_EpollServer::PACKET_LESS; - } - } - catch(exception &ex) - { - return TC_EpollServer::PACKET_ERR; - } - - return TC_EpollServer::PACKET_LESS; //±íʾÊÕµ½µÄ°ü²»ÍêÈ« - } - -}; - -void -HttpServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj"); - addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&HttpProtocol::parseHttp); -} -///////////////////////////////////////////////////////////////// -void -HttpServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/HttpDemo/HttpServer/HttpServer.h b/cpp/examples/HttpDemo/HttpServer/HttpServer.h deleted file mode 100644 index 7e9492ef9..000000000 --- a/cpp/examples/HttpDemo/HttpServer/HttpServer.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _HttpServer_H_ -#define _HttpServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class HttpServer : public Application -{ -public: - /** - * - **/ - virtual ~HttpServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -}; - -extern HttpServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/HttpDemo/HttpServer/makefile b/cpp/examples/HttpDemo/HttpServer/makefile deleted file mode 100644 index a505838fc..000000000 --- a/cpp/examples/HttpDemo/HttpServer/makefile +++ /dev/null @@ -1,14 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := TestApp -TARGET := HttpServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += -LIB += - -#-----------------------------------------------------------------------i - -include /usr/local/tars/cpp/makefile/makefile.tars diff --git a/cpp/examples/PromiseDemo/AServer/AServant.tars b/cpp/examples/PromiseDemo/AServer/AServant.tars deleted file mode 100644 index 1255254dd..000000000 --- a/cpp/examples/PromiseDemo/AServer/AServant.tars +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module Test -{ - -interface AServant -{ - int queryResultSerial(string sIn, out string sOut); - - int queryResultParallel(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/PromiseDemo/AServer/AServantImp.cpp b/cpp/examples/PromiseDemo/AServer/AServantImp.cpp deleted file mode 100644 index bcf4ca608..000000000 --- a/cpp/examples/PromiseDemo/AServer/AServantImp.cpp +++ /dev/null @@ -1,252 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AServantImp.h" -#include "AServer.h" -#include "servant/Application.h" -#include "servant/Communicator.h" - -using namespace std; -using namespace tars; - -////////////////////////////////////////////////////// -class BServantCallback : public BServantPrxCallback -{ - -public: - BServantCallback(TarsCurrentPtr ¤t) - : _current(current) - {} - - BServantCallback(TarsCurrentPtr ¤t, const promise::Promise &promise) - : _current(current) - , _promise(promise) - {} - - void callback_queryResult(tars::Int32 ret, const std::string &sOut) - { - if(ret == 0) - { - _promise.setValue(sOut); - } - else - { - handExp("callback_queryResult", ret); - } - } - void callback_queryResult_exception(tars::Int32 ret) - { - handExp("callback_queryResult_exception", ret); - } - -private: - void handExp(const std::string &sFuncName, tars::Int32 ret) - { - string s("sFuncName:"); - s += sFuncName; - s += "|ret:"; - s += TC_Common::tostr(ret); - - _promise.setException(promise::copyException(s)); - - TLOGDEBUG("ServerPrxCallback handExp:" << s << endl); - } - -private: - - TarsCurrentPtr _current; - promise::Promise _promise; -}; -////////////////////////////////////////////////////// -class CServantCallback : public CServantPrxCallback -{ - -public: - CServantCallback(TarsCurrentPtr ¤t) - : _current(current) - {} - - CServantCallback(TarsCurrentPtr ¤t, const promise::Promise &promise) - : _current(current) - , _promise(promise) - {} - - void callback_queryResult(tars::Int32 ret, const std::string &sOut) - { - if(ret == 0) - { - _promise.setValue(sOut); - } - else - { - handExp("callback_queryResult", ret); - } - } - void callback_queryResult_exception(tars::Int32 ret) - { - handExp("callback_queryResult_exception", ret); - } - -private: - void handExp(const std::string &sFuncName, tars::Int32 ret) - { - string s("sFuncName:"); - s += sFuncName; - s += "|ret:"; - s += TC_Common::tostr(ret); - - _promise.setException(promise::copyException(s)); - - TLOGDEBUG("ServerPrxCallback handExp:" << s << endl); - } - -private: - - TarsCurrentPtr _current; - promise::Promise _promise; -}; -////////////////////////////////////////////////////// -promise::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) -{ - promise::Promise promise; - - Test::BServantPrxCallbackPtr cb = new BServantCallback(current, promise); - - prx->async_queryResult(cb, sIn); - - return promise.getFuture(); -} -////////////////////////////////////////////////////// -promise::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) -{ - promise::Promise promise; - - Test::CServantPrxCallbackPtr cb = new CServantCallback(current, promise); - - prx->async_queryResult(cb, sIn); - - return promise.getFuture(); -} -////////////////////////////////////////////////////// -promise::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future& future) -{ - std::string sResult(""); - std::string sException(""); - try - { - sResult = future.get(); - - return sendCReq(prx, sResult, current); - } - catch (exception& e) - { - TLOGDEBUG("Exception:" << e.what() << endl); - sException = e.what(); - } - - promise::Promise promise; - promise.setValue(sException); - - return promise.getFuture(); -} -////////////////////////////////////////////////////// -int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future& future) -{ - int ret = 0; - std::string sResult(""); - try - { - sResult = future.get(); - } - catch (exception& e) - { - ret = -1; - sResult = e.what(); - - TLOGDEBUG("Exception:" << e.what() << endl); - } - - AServant::async_response_queryResultSerial(current, ret, sResult); - - return 0; -} -////////////////////////////////////////////////////// -int handleBCRspAndReturnClient(TarsCurrentPtr current, const promise::Future, promise::Future > >& allFuture) -{ - int ret = 0; - std::string sResult(""); - try - { - promise::Tuple, promise::Future > tupleFuture = allFuture.get(); - - std::string sResult1 = tupleFuture.get<0>().get(); - std::string sResult2 = tupleFuture.get<1>().get(); - - sResult = sResult1; - sResult += "|"; - sResult += sResult2; - } - catch (exception& e) - { - ret = -1; - sResult = e.what(); - - TLOGDEBUG("Exception:" << e.what() << endl); - } - - AServant::async_response_queryResultParallel(current, ret, sResult); - - return 0; -} -////////////////////////////////////////////////////// -void AServantImp::initialize() -{ - //initialize servant here: - //... - _pPrxB = Application::getCommunicator()->stringToProxy("Test.BServer.BServantObj"); - _pPrxC = Application::getCommunicator()->stringToProxy("Test.CServer.CServantObj"); -} -////////////////////////////////////////////////////// -void AServantImp::destroy() -{ -} -////////////////////////////////////////////////////// -tars::Int32 AServantImp::queryResultSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - current->setResponse(false); - - promise::Future f = sendBReq(_pPrxB, sIn, current); - - f.then(tars::TC_Bind(&handleBRspAndSendCReq, _pPrxC, current)).then(tars::TC_Bind(&handleCRspAndReturnClient, current)); - - return 0; -} -////////////////////////////////////////////////////// -tars::Int32 AServantImp::queryResultParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - current->setResponse(false); - - promise::Future f1 = sendBReq(_pPrxB, sIn, current); - - promise::Future f2 = sendCReq(_pPrxC, sIn, current); - - promise::Future, promise::Future > > f_all = promise::whenAll(f1, f2); - - f_all.then(tars::TC_Bind(&handleBCRspAndReturnClient, current)); - - return 0; -} - diff --git a/cpp/examples/PromiseDemo/AServer/AServantImp.h b/cpp/examples/PromiseDemo/AServer/AServantImp.h deleted file mode 100644 index 0d2c19175..000000000 --- a/cpp/examples/PromiseDemo/AServer/AServantImp.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServantImp_H_ -#define _BServantImp_H_ - -#include "servant/Application.h" -#include "AServant.h" -#include "BServant.h" -#include "CServant.h" -#include "promise/promise.h" -//#include "promise/tuple.h" - -#include "promise/when_all.h" - -using namespace Test; - -/////////////////////////////////// -promise::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); - -promise::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future& future); - -promise::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); - -int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future& future); - -/////////////////////////////////// -int handleBCRspAndReturnClient(TarsCurrentPtr current, const promise::Future, promise::Future > >& allFuture); - -/////////////////////////////////// -class AServantImp : public Test::AServant -{ -public: - - virtual ~AServantImp() {} - - virtual void initialize(); - - virtual void destroy(); - - tars::Int32 queryResultSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - - tars::Int32 queryResultParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - -private: - BServantPrx _pPrxB; - CServantPrx _pPrxC; -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/PromiseDemo/AServer/AServer.cpp b/cpp/examples/PromiseDemo/AServer/AServer.cpp deleted file mode 100644 index 09dbf2ca3..000000000 --- a/cpp/examples/PromiseDemo/AServer/AServer.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AServer.h" -#include "AServantImp.h" - -using namespace std; - -AServer g_app; - -///////////////////////////////////////////////////////////////// -void AServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".AServantObj"); -} -///////////////////////////////////////////////////////////////// -void AServer::destroyApp() -{ - //destroy application here: - //... -} - -///////////////////////////////////////////////////////////////// -int main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/PromiseDemo/AServer/AServer.h b/cpp/examples/PromiseDemo/AServer/AServer.h deleted file mode 100644 index 9e6238a32..000000000 --- a/cpp/examples/PromiseDemo/AServer/AServer.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServer_H_ -#define _BServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class AServer : public Application -{ -public: - /** - * - **/ - virtual ~AServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -protected: - bool cmdprofile(const string& command, const string& params, string& result); -}; - -extern AServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/PromiseDemo/AServer/BServant.tars b/cpp/examples/PromiseDemo/AServer/BServant.tars deleted file mode 100644 index 4f498e63e..000000000 --- a/cpp/examples/PromiseDemo/AServer/BServant.tars +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module Test -{ - -interface BServant -{ - int queryResult(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/PromiseDemo/AServer/CServant.tars b/cpp/examples/PromiseDemo/AServer/CServant.tars deleted file mode 100644 index d8dd8adab..000000000 --- a/cpp/examples/PromiseDemo/AServer/CServant.tars +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module Test -{ - -interface CServant -{ - int queryResult(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/PromiseDemo/AServer/makefile b/cpp/examples/PromiseDemo/AServer/makefile deleted file mode 100644 index 9922db3a3..000000000 --- a/cpp/examples/PromiseDemo/AServer/makefile +++ /dev/null @@ -1,12 +0,0 @@ - -#----------------------------------------------------------------------- -APP := Test -TARGET := AServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/PromiseDemo/BServer/BServant.tars b/cpp/examples/PromiseDemo/BServer/BServant.tars deleted file mode 100644 index 4f498e63e..000000000 --- a/cpp/examples/PromiseDemo/BServer/BServant.tars +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module Test -{ - -interface BServant -{ - int queryResult(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/PromiseDemo/BServer/BServantImp.cpp b/cpp/examples/PromiseDemo/BServer/BServantImp.cpp deleted file mode 100644 index ffa2eccca..000000000 --- a/cpp/examples/PromiseDemo/BServer/BServantImp.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "BServantImp.h" -#include "BServer.h" -#include "servant/Application.h" -#include "servant/Communicator.h" - -using namespace std; -using namespace tars; - -////////////////////////////////////////////////////// -void BServantImp::initialize() -{ - //initialize servant here: - //... -} -////////////////////////////////////////////////////// -void BServantImp::destroy() -{ -} -////////////////////////////////////////////////////// -tars::Int32 BServantImp::queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - sOut = "[sResult:"; - sOut += sIn; - sOut += "]"; - - return 0; -} - - diff --git a/cpp/examples/PromiseDemo/BServer/BServantImp.h b/cpp/examples/PromiseDemo/BServer/BServantImp.h deleted file mode 100644 index 1ca696dc8..000000000 --- a/cpp/examples/PromiseDemo/BServer/BServantImp.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServantImp_H_ -#define _BServantImp_H_ - -#include "servant/Application.h" -#include "BServant.h" -#include "promise/promise.h" - -using namespace Test; - -class BServantImp : public Test::BServant -{ -public: - - virtual ~BServantImp() {} - - virtual void initialize(); - - virtual void destroy(); - - tars::Int32 queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/PromiseDemo/BServer/BServer.cpp b/cpp/examples/PromiseDemo/BServer/BServer.cpp deleted file mode 100644 index d203ec552..000000000 --- a/cpp/examples/PromiseDemo/BServer/BServer.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "BServer.h" -#include "BServantImp.h" - -using namespace std; - -BServer g_app; - -///////////////////////////////////////////////////////////////// -void BServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".BServantObj"); -} -///////////////////////////////////////////////////////////////// -void BServer::destroyApp() -{ - //destroy application here: - //... -} - -///////////////////////////////////////////////////////////////// -int main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/PromiseDemo/BServer/BServer.h b/cpp/examples/PromiseDemo/BServer/BServer.h deleted file mode 100644 index 7038cd8b6..000000000 --- a/cpp/examples/PromiseDemo/BServer/BServer.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServer_H_ -#define _BServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class BServer : public Application -{ -public: - /** - * - **/ - virtual ~BServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -protected: - bool cmdprofile(const string& command, const string& params, string& result); -}; - -extern BServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/PromiseDemo/BServer/makefile b/cpp/examples/PromiseDemo/BServer/makefile deleted file mode 100644 index 156f400b0..000000000 --- a/cpp/examples/PromiseDemo/BServer/makefile +++ /dev/null @@ -1,11 +0,0 @@ -#----------------------------------------------------------------------- - -APP := Test -TARGET := BServer -CONFIG := -STRIP_FLAG:= N -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/PromiseDemo/CServer/CServant.tars b/cpp/examples/PromiseDemo/CServer/CServant.tars deleted file mode 100644 index d8dd8adab..000000000 --- a/cpp/examples/PromiseDemo/CServer/CServant.tars +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module Test -{ - -interface CServant -{ - int queryResult(string sIn, out string sOut); -}; - -}; diff --git a/cpp/examples/PromiseDemo/CServer/CServantImp.cpp b/cpp/examples/PromiseDemo/CServer/CServantImp.cpp deleted file mode 100644 index 7e55a9fb9..000000000 --- a/cpp/examples/PromiseDemo/CServer/CServantImp.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "CServantImp.h" -#include "CServer.h" -#include "servant/Application.h" -#include "servant/Communicator.h" - -using namespace std; -using namespace tars; - -////////////////////////////////////////////////////// -void CServantImp::initialize() -{ - //initialize servant here: - //... -} -////////////////////////////////////////////////////// -void CServantImp::destroy() -{ -} -////////////////////////////////////////////////////// -tars::Int32 CServantImp::queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - sOut = "[sResult:"; - sOut += sIn; - sOut += "]"; - - return 0; -} - - diff --git a/cpp/examples/PromiseDemo/CServer/CServantImp.h b/cpp/examples/PromiseDemo/CServer/CServantImp.h deleted file mode 100644 index 1191b56ed..000000000 --- a/cpp/examples/PromiseDemo/CServer/CServantImp.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServantImp_H_ -#define _BServantImp_H_ - -#include "servant/Application.h" -#include "CServant.h" -#include "promise/promise.h" - -using namespace Test; - -class CServantImp : public Test::CServant -{ -public: - - virtual ~CServantImp() {} - - virtual void initialize(); - - virtual void destroy(); - - tars::Int32 queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/PromiseDemo/CServer/CServer.cpp b/cpp/examples/PromiseDemo/CServer/CServer.cpp deleted file mode 100644 index 9d8827bd9..000000000 --- a/cpp/examples/PromiseDemo/CServer/CServer.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "CServer.h" -#include "CServantImp.h" - -using namespace std; - -CServer g_app; - -///////////////////////////////////////////////////////////////// -void CServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".CServantObj"); -} -///////////////////////////////////////////////////////////////// -void CServer::destroyApp() -{ - //destroy application here: - //... -} - -///////////////////////////////////////////////////////////////// -int main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/PromiseDemo/CServer/CServer.h b/cpp/examples/PromiseDemo/CServer/CServer.h deleted file mode 100644 index 79cb745e3..000000000 --- a/cpp/examples/PromiseDemo/CServer/CServer.h +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _BServer_H_ -#define _BServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * * - * **/ -class CServer : public Application -{ -public: - /** - * - **/ - virtual ~CServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -protected: - bool cmdprofile(const string& command, const string& params, string& result); -}; - -extern CServer g_app; - -//////////////////////////////////////////// -#endif - diff --git a/cpp/examples/PromiseDemo/CServer/makefile b/cpp/examples/PromiseDemo/CServer/makefile deleted file mode 100644 index 303c3f3bf..000000000 --- a/cpp/examples/PromiseDemo/CServer/makefile +++ /dev/null @@ -1,11 +0,0 @@ -#----------------------------------------------------------------------- -APP := Test -TARGET := CServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/examples/PromiseDemo/Client/main.cpp b/cpp/examples/PromiseDemo/Client/main.cpp deleted file mode 100644 index 83ca95fb9..000000000 --- a/cpp/examples/PromiseDemo/Client/main.cpp +++ /dev/null @@ -1,138 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AServant.h" -#include "servant/Communicator.h" -#include "util/tc_thread_pool.h" -#include - -using namespace std; -using namespace Test; -using namespace tars; - -class Test1 -{ -public: - Test1(const string &sStr); - - ~Test1(); - - void queryResult(int iFlag, int iExecuteNum); - -private: - Communicator _comm; - AServantPrx prx; -}; - -Test1::Test1(const string &sStr) -{ - _comm.setProperty("locator", "tars.tarsregistry.QueryObj@ tcp -h 10.208.139.242 -p 17890 -t 10000 "); - _comm.stringToProxy(sStr, prx); -} - -Test1::~Test1() -{ - -} - -void Test1::queryResult(int iFlag, int iExecuteNum) -{ - string sIn(10,'a'); - string sOut(""); - - tars::Int32 count = 0; - unsigned long sum = 0; - - time_t _iTime=TC_TimeProvider::getInstance()->getNowMs(); - - for(int i=0; iqueryResultSerial(sIn, sOut); - } - else - { - ret = prx->queryResultParallel(sIn, sOut); - } - - if(ret == 0) - { - ++sum; - ++count; - if(count == iExecuteNum) - { - cout << "pthread id: " << pthread_self() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl; - _iTime=TC_TimeProvider::getInstance()->getNowMs(); - count = 0; - } - } - } - catch(TC_Exception &e) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl; - } - } - cout << "succ:" << sum << endl; - cout << "sOut:" << sOut << endl; -} - -int main(int argc,char ** argv) -{ - if(argc != 5) - { - cout << "usage: " << argv[0] << " sObj ThreadNum CallTimes CallMode" << endl; - return -1; - } - - string s = string(argv[1]); - - Test1 test1(s); - try - { - tars::Int32 threads = TC_Common::strto(string(argv[2])); - TC_ThreadPool tp; - tp.init(threads); - tp.start(); - tars::Int32 times = TC_Common::strto(string(argv[3])); - tars::Int32 callMode = TC_Common::strto(string(argv[4])); - - for(int i = 0; i -#include - -/* - å“应包解ç å‡½æ•°ï¼Œæ ¹æ®ç‰¹å®šæ ¼å¼è§£ç ä»ŽæœåŠ¡ç«¯æ”¶åˆ°çš„æ•°æ®ï¼Œè§£æžä¸ºResponsePacket -*/ -static size_t pushResponse(const char* recvBuffer, size_t length, list& done) -{ - size_t pos = 0; - while (pos < length) - { - unsigned int len = length - pos; - if(len < sizeof(unsigned int)) - { - break; - } - - unsigned int iHeaderLen = ntohl(*(unsigned int*)(recvBuffer + pos)); - - //åšä¸€ä¸‹ä¿æŠ¤,长度大于M - if (iHeaderLen > 100000 || iHeaderLen < sizeof(unsigned int)) - { - throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen)); - } - - //包没有接收全 - if (len < iHeaderLen) - { - break; - } - else - { - ResponsePacket rsp; - rsp.iRequestId = ntohl(*((unsigned int *)(recvBuffer + pos + sizeof(unsigned int)))); - rsp.sBuffer.resize(iHeaderLen - 2*sizeof(unsigned int)); - ::memcpy(&rsp.sBuffer[0], recvBuffer + pos + 2*sizeof(unsigned int), iHeaderLen - 2*sizeof(unsigned int)); - - pos += iHeaderLen; - - done.push_back(rsp); - } - } - - return pos; -} -/* - 请求包编ç å‡½æ•°ï¼Œæœ¬å‡½æ•°çš„打包格å¼ä¸º - 整个包长度(字节)+iRequestId(字节)+包内容 -*/ -static void pushRequest(const RequestPacket& request, string& buff) -{ - unsigned int net_bufflength = htonl(request.sBuffer.size()+8); - unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength); - - buff = ""; - for (int i = 0; i<4; ++i) - { - buff += *bufflengthptr++; - } - - unsigned int netrequestId = htonl(request.iRequestId); - unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId); - - for (int i = 0; i<4; ++i) - { - buff += *netrequestIdptr++; - } - - string tmp; - tmp.assign((const char*)(&request.sBuffer[0]), request.sBuffer.size()); - buff+=tmp; -} - -static void printResult(int iRequestId, const string &sResponseStr) -{ - cout << "request id: " << iRequestId << endl; - cout << "response str: " << sResponseStr << endl; -} -static void printPushInfo(const string &sResponseStr) -{ - cout << "push message: " << sResponseStr << endl; -} - -int TestPushCallBack::onDispatch(ReqMessagePtr msg) -{ - if(msg->request.sFuncName == "printResult") - { - string sRet; - cout << "sBuffer: " << msg->response.sBuffer.size() << endl; - sRet.assign(&(msg->response.sBuffer[0]), msg->response.sBuffer.size()); - printResult(msg->request.iRequestId, sRet); - return 0; - } - else if(msg->response.iRequestId == 0) - { - string sRet; - sRet.assign(&(msg->response.sBuffer[0]), msg->response.sBuffer.size()); - printPushInfo(sRet); - return 0; - } - else - { - cout << "no match func!" <(sObjName+"@"+sObjHost); - - ProxyProtocol prot; - prot.requestFunc = pushRequest; - prot.responseFunc = pushResponse; - - _prx->tars_set_protocol(prot); -} - -void RecvThread::terminate() -{ - _bTerminate = true; - { - tars::TC_ThreadLock::Lock sync(*this); - notifyAll(); - } -} - -void RecvThread::run(void) -{ - TestPushCallBackPtr cbPush = new TestPushCallBack(); - _prx->tars_set_push_callback(cbPush); - - string buf("heartbeat"); - - while(!_bTerminate) - { - { - try - { - TestPushCallBackPtr cb = new TestPushCallBack(); - _prx->rpc_call_async(_prx->tars_gen_requestid(), "printResult", buf.c_str(), buf.length(), cb); - } - catch(TarsException& e) - { - cout << "TarsException: " << e.what() << endl; - } - catch(...) - { - cout << "unknown exception" << endl; - } - } - - { - TC_ThreadLock::Lock sync(*this); - timedWait(5000); - } - } -} diff --git a/cpp/examples/PushDemo/PushClient/TestRecvThread.h b/cpp/examples/PushDemo/PushClient/TestRecvThread.h deleted file mode 100644 index d8a33ade7..000000000 --- a/cpp/examples/PushDemo/PushClient/TestRecvThread.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __TEST_RECV_THREAD_H -#define __TEST_RECV_THREAD_H - -#include "servant/Application.h" - -class TestPushCallBack : public ServantProxyCallback -{ -public: - virtual int onDispatch(ReqMessagePtr msg); -}; -typedef tars::TC_AutoPtr TestPushCallBackPtr; - -class RecvThread : public TC_Thread, public TC_ThreadLock -{ -public: - RecvThread(); - - virtual void run(); - - void terminate(); -private: - bool _bTerminate; - - Communicator _comm; - - ServantPrx _prx; -}; -#endif diff --git a/cpp/examples/PushDemo/PushClient/main.cpp b/cpp/examples/PushDemo/PushClient/main.cpp deleted file mode 100644 index 613f24536..000000000 --- a/cpp/examples/PushDemo/PushClient/main.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "servant/Application.h" -#include "TestRecvThread.h" -#include - -using namespace std; -using namespace tars; - -int main(int argc,char**argv) -{ - try - { - RecvThread thread; - thread.start(); - - int c; - while((c = getchar()) != 'q'); - - thread.terminate(); - thread.getThreadControl().join(); - } - catch(std::exception&e) - { - cerr<<"std::exception:"<& response) -{ - //ä¿å­˜å®¢æˆ·ç«¯çš„ä¿¡æ¯ï¼Œä»¥ä¾¿å¯¹å®¢æˆ·ç«¯è¿›è¡Œpushæ¶ˆæ¯ - (PushUser::mapMutex).lock(); - map::iterator it = PushUser::pushUser.find(current->getIp()); - if(it == PushUser::pushUser.end()) - { - PushUser::pushUser.insert(map::value_type(current->getIp(), current)); - LOG->debug() << "connect ip: " << current->getIp() << endl; - } - (PushUser::mapMutex).unlock(); - //返回给客户端它自己请求的数æ®åŒ…,å³åŽŸåŒ…返回 - const vector& request = current->getRequestBuffer(); - response = request; - - return 0; -} -//客户端关闭到æœåŠ¡ç«¯çš„连接,或者æœåŠ¡ç«¯å‘现客户端长时间未å‘é€åŒ…过æ¥ï¼Œç„¶åŽè¶…过60s就关闭连接 -//调用的方法 -int TestPushServantImp::doClose(TarsCurrentPtr current) -{ - (PushUser::mapMutex).lock(); - map::iterator it = PushUser::pushUser.find(current->getIp()); - if(it != PushUser::pushUser.end()) - { - PushUser::pushUser.erase(it); - LOG->debug() << "close ip: " << current->getIp() << endl; - } - (PushUser::mapMutex).unlock(); - - return 0; -} - - diff --git a/cpp/examples/PushDemo/PushServer/TestPushServantImp.h b/cpp/examples/PushDemo/PushServer/TestPushServantImp.h deleted file mode 100644 index 5b46f8329..000000000 --- a/cpp/examples/PushDemo/PushServer/TestPushServantImp.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef _TestPushServantImp_H_ -#define _TestPushServantImp_H_ - -#include "servant/Application.h" -//#include "TestPushServant.h" - -/** - * - * - */ -class TestPushServantImp : public tars::Servant -{ -public: - /** - * - */ - virtual ~TestPushServantImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual int test(tars::TarsCurrentPtr current) { return 0;}; - - - //é‡è½½Servantçš„doRequest方法 - int doRequest(tars::TarsCurrentPtr current, vector& response); - - //é‡è½½Servantçš„doClose方法 - int doClose(tars::TarsCurrentPtr current); - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/PushDemo/PushServer/TestPushServer.cpp b/cpp/examples/PushDemo/PushServer/TestPushServer.cpp deleted file mode 100644 index d8a3dd951..000000000 --- a/cpp/examples/PushDemo/PushServer/TestPushServer.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "TestPushServer.h" -#include "TestPushServantImp.h" - -using namespace std; - -TestPushServer g_app; - -///////////////////////////////////////////////////////////////// - -static int parse(string &in, string &out) -{ - if(in.length() < sizeof(unsigned int)) - { - return TC_EpollServer::PACKET_LESS; - } - - unsigned int iHeaderLen; - - memcpy(&iHeaderLen, in.c_str(), sizeof(unsigned int)); - - iHeaderLen = ntohl(iHeaderLen); - - if(iHeaderLen < (unsigned int)(sizeof(unsigned int))|| iHeaderLen > 1000000) - { - return TC_EpollServer::PACKET_ERR; - } - - if((unsigned int)in.length() < iHeaderLen) - { - return TC_EpollServer::PACKET_LESS; - } - - out = in.substr(0, iHeaderLen); - - in = in.substr(iHeaderLen); - - return TC_EpollServer::PACKET_FULL; -} - - -void -TestPushServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".TestPushServantObj"); - - addServantProtocol("Test.TestPushServer.TestPushServantObj", parse); - - pushThread.start(); - -} -///////////////////////////////////////////////////////////////// -void -TestPushServer::destroyApp() -{ - //destroy application here: - //... - pushThread.terminate(); - pushThread.getThreadControl().join(); - -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/PushDemo/PushServer/TestPushServer.h b/cpp/examples/PushDemo/PushServer/TestPushServer.h deleted file mode 100644 index e44f8daf0..000000000 --- a/cpp/examples/PushDemo/PushServer/TestPushServer.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef _TestPushServer_H_ -#define _TestPushServer_H_ - -#include -#include "servant/Application.h" -#include "TestPushThread.h" - - -using namespace tars; - -/** - * - **/ -class TestPushServer : public Application -{ -public: - /** - * - **/ - virtual ~TestPushServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); - - private: - //用于push消æ¯çš„线程 - PushInfoThread pushThread; - -}; - -extern TestPushServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/PushDemo/PushServer/TestPushThread.cpp b/cpp/examples/PushDemo/PushServer/TestPushThread.cpp deleted file mode 100644 index 5c9edd4de..000000000 --- a/cpp/examples/PushDemo/PushServer/TestPushThread.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "TestPushThread.h" -#include - -map PushUser::pushUser; -TC_ThreadMutex PushUser::mapMutex; - - -void PushInfoThread::terminate(void) -{ - _bTerminate = true; - { - tars::TC_ThreadLock::Lock sync(*this); - notifyAll(); - } -} - -void PushInfoThread::setPushInfo(const string &sInfo) -{ - unsigned int iBuffLength = htonl(sInfo.size()+8); - unsigned char * pBuff = (unsigned char*)(&iBuffLength); - - _sPushInfo = ""; - for (int i = 0; i<4; ++i) - { - _sPushInfo += *pBuff++; - } - - unsigned int iRequestId = htonl(_iId); - unsigned char * pRequestId = (unsigned char*)(&iRequestId); - - for (int i = 0; i<4; ++i) - { - _sPushInfo += *pRequestId++; - } - - _sPushInfo += sInfo; -} -//定期å‘客户pushæ¶ˆæ¯ -void PushInfoThread::run(void) -{ - time_t iNow; - - setPushInfo("hello world"); - - while (!_bTerminate) - { - iNow = TC_TimeProvider::getInstance()->getNow(); - - if(iNow - _tLastPushTime > _tInterval) - { - _tLastPushTime = iNow; - - (PushUser::mapMutex).lock(); - for(map::iterator it = (PushUser::pushUser).begin(); it != (PushUser::pushUser).end(); ++it) - { - (it->second)->sendResponse(_sPushInfo.c_str(), _sPushInfo.size()); - LOG->debug() << "sendResponse: " << _sPushInfo.size() < pushUser; - static TC_ThreadMutex mapMutex; -}; - -class PushInfoThread : public TC_Thread, public TC_ThreadLock -{ -public: - PushInfoThread():_bTerminate(false),_tLastPushTime(0),_tInterval(10),_iId(0){} - - virtual void run(); - - void terminate(); - - void setPushInfo(const string &sInfo); - -private: - bool _bTerminate; - time_t _tLastPushTime; - time_t _tInterval; - unsigned int _iId; - string _sPushInfo; -}; -#endif diff --git a/cpp/examples/PushDemo/PushServer/makefile b/cpp/examples/PushDemo/PushServer/makefile deleted file mode 100644 index 7464e8883..000000000 --- a/cpp/examples/PushDemo/PushServer/makefile +++ /dev/null @@ -1,17 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := Test -TARGET := TestPushServer -CONFIG := -STRIP_FLAG := N -TARS2CPP_FLAG:= - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- - -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/cpp/examples/PushDemo/README.md b/cpp/examples/PushDemo/README.md deleted file mode 100644 index 264d3a849..000000000 --- a/cpp/examples/PushDemo/README.md +++ /dev/null @@ -1,7 +0,0 @@ -该工程是Tarså¿«é€Ÿå…¥é—¨ç¤ºä¾‹çš„ä»£ç  - - -目录å称 |功能 ------------------|---------------- -HelloServer | å¼€å‘快速入门的示例 -ProxyServer | 中转代ç†æœåŠ¡ç¤ºä¾‹ï¼Œä½œä¸ºHelloServer的代ç†æœåŠ¡ \ No newline at end of file diff --git a/cpp/examples/QuickStartDemo/HelloServer/AsyncClient/main.cpp b/cpp/examples/QuickStartDemo/HelloServer/AsyncClient/main.cpp deleted file mode 100644 index 638677145..000000000 --- a/cpp/examples/QuickStartDemo/HelloServer/AsyncClient/main.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include "servant/Communicator.h" -#include "Hello.h" - -using namespace std; -using namespace TestApp; -using namespace tars; - -class HelloCallBack : public HelloPrxCallback -{ -public: - HelloCallBack(){} - - virtual ~HelloCallBack(){} - - virtual void callback_testHello(tars::Int32 ret, const std::string& sRsp) - { - cout<<"callback_testHello ret:"<< ret << "|sRsp:" << sRsp <async_testHello(cb, sReq); - cout<<" sReq:"< -#include "servant/Communicator.h" -#include "Hello.h" - -using namespace std; -using namespace TestApp; -using namespace tars; - -int main(int argc,char ** argv) -{ - Communicator comm; - - try - { - HelloPrx prx; - comm.stringToProxy("TestApp.HelloServer.HelloObj@tcp -h 10.120.129.226 -p 20001" , prx); - - try - { - string sReq("hello"); - string sRsp(""); - - int iRet = prx->testHello(sReq, sRsp); - cout<<"iRet:"<(ServerConfig::Application + "." + ServerConfig::ServerName + ".HelloObj"); -} -///////////////////////////////////////////////////////////////// -void -HelloServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/QuickStartDemo/HelloServer/HelloServer.h b/cpp/examples/QuickStartDemo/HelloServer/HelloServer.h deleted file mode 100644 index f35886b08..000000000 --- a/cpp/examples/QuickStartDemo/HelloServer/HelloServer.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _HelloServer_H_ -#define _HelloServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class HelloServer : public Application -{ -public: - /** - * - **/ - virtual ~HelloServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -}; - -extern HelloServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/QuickStartDemo/HelloServer/makefile b/cpp/examples/QuickStartDemo/HelloServer/makefile deleted file mode 100644 index 56961c42f..000000000 --- a/cpp/examples/QuickStartDemo/HelloServer/makefile +++ /dev/null @@ -1,16 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := TestApp -TARGET := HelloServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- - -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/cpp/examples/QuickStartDemo/ProxyServer/Client/main.cpp b/cpp/examples/QuickStartDemo/ProxyServer/Client/main.cpp deleted file mode 100644 index b204c49a4..000000000 --- a/cpp/examples/QuickStartDemo/ProxyServer/Client/main.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include "servant/Communicator.h" -#include "Proxy.h" - -using namespace std; -using namespace TestApp; -using namespace tars; - -int main(int argc,char ** argv) -{ - Communicator comm; - - try - { - ProxyPrx prx; - comm.stringToProxy("TestApp.ProxyServer.ProxyObj@tcp -h 10.208.139.242 -p 10007" , prx); - - try - { - string sReq("hello"); - string sRsp(""); - - int iRet = prx->testProxy(sReq, sRsp); - - cout<<"iRet:"<stringToProxy("TestApp.HelloServer.HelloObj"); -} - -////////////////////////////////////////////////////// -void ProxyImp::destroy() -{ -} - -////////////////////////////////////////////////////// -tars::Int32 ProxyImp::test(tars::TarsCurrentPtr current) { return 0;} - -////////////////////////////////////////////////////// -tars::Int32 ProxyImp::testProxy(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - try - { - current->setResponse(false); - - TestApp::HelloPrxCallbackPtr cb = new HelloCallback(current); - - _prx->tars_set_timeout(3000)->async_testHello(cb,sIn); - } - catch(std::exception &ex) - { - current->setResponse(true); - - TLOGERROR("ProxyImp::testProxy ex:" << ex.what() << endl); - } - - return 0; -} diff --git a/cpp/examples/QuickStartDemo/ProxyServer/ProxyImp.h b/cpp/examples/QuickStartDemo/ProxyServer/ProxyImp.h deleted file mode 100644 index 9c924bdba..000000000 --- a/cpp/examples/QuickStartDemo/ProxyServer/ProxyImp.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROXY_IMP_H_ -#define __PROXY_IMP_H_ - -#include "servant/Application.h" -#include "Hello.h" -#include "Proxy.h" - -using namespace TestApp; - -/** - * - * - */ -class ProxyImp : public TestApp::Proxy -{ -public: - /** - * - */ - virtual ~ProxyImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual tars::Int32 test(tars::TarsCurrentPtr current); - - /** - * - */ - virtual tars::Int32 testProxy(const std::string& sReq, std::string &sRsp, tars::TarsCurrentPtr current); - -private: - HelloPrx _prx; - -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp b/cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp deleted file mode 100644 index 58def1291..000000000 --- a/cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ProxyServer.h" -#include "ProxyImp.h" - -using namespace std; - -ProxyServer g_app; - -///////////////////////////////////////////////////////////////// -void ProxyServer::initialize() -{ - //initialize application here: - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".ProxyObj"); - -} -///////////////////////////////////////////////////////////////// -void ProxyServer::destroyApp() -{ - //destroy application here: - //... -} - -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.h b/cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.h deleted file mode 100644 index 2e6d3a42f..000000000 --- a/cpp/examples/QuickStartDemo/ProxyServer/ProxyServer.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROXY_SERVER_H_ -#define __PROXY_SERVER_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class ProxyServer : public Application -{ -public: - /** - * - **/ - virtual ~ProxyServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -protected: - -}; - -extern ProxyServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/QuickStartDemo/ProxyServer/makefile b/cpp/examples/QuickStartDemo/ProxyServer/makefile deleted file mode 100644 index 713820135..000000000 --- a/cpp/examples/QuickStartDemo/ProxyServer/makefile +++ /dev/null @@ -1,17 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := TestApp -TARGET := ProxyServer -CONFIG := -STRIP_FLAG:= N -TARS2CPP_FLAG:= - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- -include /home/tarsproto/TestApp/HelloServer/HelloServer.mk -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/cpp/examples/QuickStartDemo/README.md b/cpp/examples/QuickStartDemo/README.md deleted file mode 100644 index 264d3a849..000000000 --- a/cpp/examples/QuickStartDemo/README.md +++ /dev/null @@ -1,7 +0,0 @@ -该工程是Tarså¿«é€Ÿå…¥é—¨ç¤ºä¾‹çš„ä»£ç  - - -目录å称 |功能 ------------------|---------------- -HelloServer | å¼€å‘快速入门的示例 -ProxyServer | 中转代ç†æœåŠ¡ç¤ºä¾‹ï¼Œä½œä¸ºHelloServer的代ç†æœåŠ¡ \ No newline at end of file diff --git a/cpp/examples/README.md b/cpp/examples/README.md deleted file mode 100644 index 0c83b6a84..000000000 --- a/cpp/examples/README.md +++ /dev/null @@ -1,11 +0,0 @@ -该工程是Tarså…¥é—¨ç¤ºä¾‹çš„ä»£ç  - - -目录å称 |功能 ------------------|---------------- -QuickStartDemo | å¼€å‘快速入门的示例 -PromiseDemo | promise编程的示例 -HttpDemo | httpæœåŠ¡ç«¯çš„示例 -CoroutineDemo | å程的示例 -StressDemo | tars c++åŽ‹æµ‹ä»£ç  -PushDemo | tars push 模å¼demo \ No newline at end of file diff --git a/cpp/examples/StressDemo/README.md b/cpp/examples/StressDemo/README.md deleted file mode 100644 index 6325b3e86..000000000 --- a/cpp/examples/StressDemo/README.md +++ /dev/null @@ -1,7 +0,0 @@ -该工程是Tarså…¥é—¨ç¤ºä¾‹çš„ä»£ç  - - -目录å称 |功能 ------------------|---------------- -TarsStressServer | Tars性能压测æœåŠ¡ç«¯çš„ç¨‹åº -TarsStressClient | Tarsæ€§èƒ½åŽ‹æµ‹å®¢æˆ·ç«¯çš„ç¨‹åº diff --git a/cpp/examples/StressDemo/TarsStressClient/main.cpp b/cpp/examples/StressDemo/TarsStressClient/main.cpp deleted file mode 100644 index 6b0f081d1..000000000 --- a/cpp/examples/StressDemo/TarsStressClient/main.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "Stress.h" -#include "servant/Communicator.h" -#include "util/tc_thread_pool.h" -#include - -using namespace std; -using namespace Test; -using namespace tars; - -class Test1 -{ -public: - Test1(const string &sStr); - Test1(){} - ~Test1(); - void dohandle(int excut_num,int size); -private: - Communicator _comm; - StressPrx prx; -}; - -Test1::Test1(const string &sStr) -{ -// _comm.setProperty("locator","tars.tarsregistry.QueryObj@tcp -h 172.27.194.147 -p 17890 -t 50000"); - _comm.setProperty("locator","tars.tarsregistry.QueryObj@tcp -h 10.120.129.226 -p 17890 -t 10000"); - _comm.setProperty("stat", "tars.tarsstat.StatObj"); - _comm.stringToProxy(sStr, prx); -} - -Test1::~Test1() -{ - -} - -void Test1::dohandle(int excut_num,int size) -{ - string s(size,'a'); - - tars::Int32 count = 0; - unsigned long sum = 0; - - time_t _iTime=TC_TimeProvider::getInstance()->getNowMs(); - - for(int i=0; itars_set_timeout(15000)->testStr(s,ret); - if(ret.size() == s.size()) - { - ++sum; - ++count; - } - - if(count == excut_num) - { - cout << "pthread id: " << pthread_self() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl; - count = 0; - } - } - catch(TC_Exception &e) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl; - } - } - cout << "succ:" << sum <(string(argv[1])); - TC_ThreadPool tp; - tp.init(threads); - tp.start(); - - cout << "init tp succ" << endl; - - tars::Int32 times = TC_Common::strto(string(argv[2])); - tars::Int32 size = TC_Common::strto(string(argv[4])); - cout << "times:" << times << endl; - - for(int i = 0; igetNowMs()-_time<getNowMs(); - _num=0; - } - - return 0; -} diff --git a/cpp/examples/StressDemo/TarsStressServer/StressImp.h b/cpp/examples/StressDemo/TarsStressServer/StressImp.h deleted file mode 100644 index 85eee1ce7..000000000 --- a/cpp/examples/StressDemo/TarsStressServer/StressImp.h +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _StressImp_H_ -#define _StressImp_H_ - -#include "servant/Application.h" -#include "Stress.h" - -/** - * - * - */ -class StressImp : public Test::Stress -{ -public: - /** - * - */ - virtual ~StressImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual int test(tars::TarsCurrentPtr current); - - - tars::Int32 testStr(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); -private: - int _num; - int64_t _time; -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/examples/StressDemo/TarsStressServer/TarsStressServer.cpp b/cpp/examples/StressDemo/TarsStressServer/TarsStressServer.cpp deleted file mode 100644 index a077043bc..000000000 --- a/cpp/examples/StressDemo/TarsStressServer/TarsStressServer.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "TarsStressServer.h" -#include "StressImp.h" - -using namespace std; - -TarsStressServer g_app; - -///////////////////////////////////////////////////////////////// -void TarsStressServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".StressObj"); -} -///////////////////////////////////////////////////////////////// -void TarsStressServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// - -int main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/examples/StressDemo/TarsStressServer/TarsStressServer.h b/cpp/examples/StressDemo/TarsStressServer/TarsStressServer.h deleted file mode 100644 index 07eac29c9..000000000 --- a/cpp/examples/StressDemo/TarsStressServer/TarsStressServer.h +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _TarsStressServer_H_ -#define _TarsStressServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class TarsStressServer : public Application -{ -public: - /** - * - **/ - virtual ~TarsStressServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); - -protected: - -}; - -extern TarsStressServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/examples/StressDemo/TarsStressServer/makefile b/cpp/examples/StressDemo/TarsStressServer/makefile deleted file mode 100644 index 03b473272..000000000 --- a/cpp/examples/StressDemo/TarsStressServer/makefile +++ /dev/null @@ -1,16 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := Test -TARGET := TarsStressServer -CONFIG := -STRIP_FLAG:= N -TARS2CPP_FLAG:= - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- - -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/cpp/framework/AdminRegistryServer/AdminReapThread.cpp b/cpp/framework/AdminRegistryServer/AdminReapThread.cpp deleted file mode 100644 index 3023a4fc4..000000000 --- a/cpp/framework/AdminRegistryServer/AdminReapThread.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AdminReapThread.h" - -extern TC_Config * g_pconf; - -AdminReapThread::AdminReapThread() -: _terminate(false) -, _timeout(150) -{ -} - -AdminReapThread::~AdminReapThread() -{ - if (isAlive()) - { - terminate(); - notifyAll(); - getThreadControl().join(); - } -} - - -int AdminReapThread::init() -{ - TLOGDEBUG("begin AdminReapThread init"<((*g_pconf).get("/tars/reap", "10")); - //最å°å€¼ä¿æŠ¤ - _updateInterval = _updateInterval < 5 ? 5 : _updateInterval; - - //管ç†ä¸»æŽ§å¿ƒè·³è¶…时时间 - _timeout = TC_Common::strto((*g_pconf)["/tars/reap"]); - _timeout = _timeout < 5 ? 5 : _timeout; - - //是å¦å…³é—­æ›´æ–°ç®¡ç†ä¸»æŽ§å¿ƒè·³æ—¶é—´,一般需è¦è¿ç§»æ—¶ï¼Œè®¾ç½®æ­¤é¡¹ä¸ºY - _heartBeatOff = (*g_pconf).get("/tars/reap", "N") == "Y"?true:false; - - _db.updateRegistryInfo2Db(_heartBeatOff); - _db.loadIPPhysicalGroupInfo(); - - TLOGDEBUG("AdminReapThread init ok."<getNow(); - time_t tLastQueryServer = 0; - time_t tNow; - while(!_terminate) - { - try - { - tNow = TNOW; - //更新心跳 - if(tNow - tLastUpdateTime >= _updateInterval) - { - tLastUpdateTime = tNow; - _db.updateRegistryInfo2Db(_heartBeatOff); - _db.loadIPPhysicalGroupInfo(); - } - - //轮询心跳超时的主控 - if(tNow - tLastQueryServer >= _timeout) - { - tLastQueryServer = tNow; - _db.checkRegistryTimeout(_timeout); - } - - TC_ThreadLock::Lock lock(*this); - timedWait(100); //ms - } - catch(exception & ex) - { - TLOGERROR("AdminReapThread exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("AdminReapThread unknown exception:" << endl); - } - } -} - - - diff --git a/cpp/framework/AdminRegistryServer/AdminReapThread.h b/cpp/framework/AdminRegistryServer/AdminReapThread.h deleted file mode 100644 index 5da2dc77f..000000000 --- a/cpp/framework/AdminRegistryServer/AdminReapThread.h +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __AMINREAP_THREAD_H__ -#define __AMINREAP_THREAD_H__ - -#include -#include "util/tc_thread.h" -#include "DbProxy.h" - -using namespace tars; - -/** - * 用于执行定时æ“作的线程类 - */ -class AdminReapThread : public TC_Thread, public TC_ThreadLock -{ -public: - /* - * åˆå§‹åŒ–函数 - */ - AdminReapThread(); - - /* - * æžæž„函数 - */ - ~AdminReapThread(); - - /* - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * åˆå§‹åŒ– - */ - int init(); - - /** - * 轮询函数 - */ - virtual void run(); - -protected: - //线程结æŸæ ‡å¿— - bool _terminate; - - //æ•°æ®åº“æ“作 - DbProxy _db; - - //心跳更新时间间隔 - int _updateInterval; - - //æœåŠ¡å¿ƒè·³è¶…时时间 - int _timeout; - - //æœåŠ¡å¿ƒè·³åŒ…开关 - bool _heartBeatOff; -}; - -#endif diff --git a/cpp/framework/AdminRegistryServer/AdminRegistryImp.cpp b/cpp/framework/AdminRegistryServer/AdminRegistryImp.cpp deleted file mode 100644 index f41b6a4ad..000000000 --- a/cpp/framework/AdminRegistryServer/AdminRegistryImp.cpp +++ /dev/null @@ -1,908 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AdminRegistryImp.h" -#include "ExecuteTask.h" -#include "servant/Application.h" - -extern TC_Config * g_pconf; - -void AdminRegistryImp::initialize() -{ - TLOGDEBUG("begin AdminRegistryImp init"<getCommunicator()->stringToProxy("tars.tarspatch.PatchObj"); - - TLOGDEBUG("AdminRegistryImp init ok."<addTaskReq(taskReq); - - return 0; -} - -int AdminRegistryImp::getTaskRsp(const string &taskNo, TaskRsp &taskRsp, tars::TarsCurrentPtr current) -{ - //ä¼˜å…ˆä»Žå†…å­˜ä¸­èŽ·å– - bool ret = ExecuteTask::getInstance()->getTaskRsp(taskNo, taskRsp); - if (ret) - { - // TLOGDEBUG("AdminRegistryImp::getTaskRsp taskNo:" << taskNo << " from running time, ret:" << ret < &taskRsp, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("AdminRegistryImp::getTaskHistory application:" << application << "|serverName:" << serverName < &info, tars::TarsCurrentPtr current) -{ - return _db.setTaskItemInfo(itemNo, info); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////////// -/// -vector AdminRegistryImp::getAllApplicationNames(string & result, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("AdminRegistryImp::getAllApplicationNames enter"< AdminRegistryImp::getAllNodeNames(string & result, tars::TarsCurrentPtr current) -{ - map mNodes = _db.getActiveNodeList(result); - map::iterator it; - vector vNodes; - - TLOGDEBUG("AdminRegistryImp::getAllNodeNames enter" <first); - } - - return vNodes; -} - -int AdminRegistryImp::getNodeVesion(const string &nodeName, string &version, string & result, tars::TarsCurrentPtr current) -{ - try - { - TLOGDEBUG("AdminRegistryImp::getNodeVesion enter" <(g_pconf->get("/tars/nodeinfo","3000")); - nodePrx->tars_set_timeout(timeout)->tars_ping(); - - result = "succ"; - TLOGDEBUG("AdminRegistryImp::pingNode name:"<getIp()<<":"<getPort()<shutdown(result); - } - catch(TarsException & ex) - { - TLOGERROR( "AdminRegistryImp::shutdownNode '"<<( name + "' exception:" + ex.what())< > AdminRegistryImp::getAllServerIds(string & result, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("AdminRegistryImp::getAllServerIds:"<getIp()<<":"<getPort()<getIp() << ":" << current->getPort() < server; - server = _db.getServers(application, serverName, nodeName,true); - if(server.size() == 0) - { - TLOGERROR("AdminRegistryImp::getServerState "<<(" '" + application + "." + serverName + "_" + nodeName + "' no config")<setResponse(false); - - NodePrxCallbackPtr callback = new GetServerStateCallbackImp(nodePrx, application, serverName, nodeName, state, current); - nodePrx->async_getStateInfo(callback, application, serverName); - } - catch(TarsException & e ) - { - current->setResponse(true); - string s = e.what(); - if(s.find("server function mismatch exception") != string::npos) - { - ServerState stateInNode = nodePrx->getState(application, serverName, result); - state.presentStateInNode = etos(stateInNode); - state.processId = nodePrx->getServerPid(application, serverName, result); - } - TLOGERROR("AdminRegistryImp::getServerState "<<"'" + application + "." + serverName + "_" + nodeName<<"|"<< e.what() <getIp() << ":" << current->getPort() <getIp() << ":" << current->getPort() < server; - server = _db.getServers(application, serverName, nodeName, true); - - //判断是å¦ä¸ºdns éždnsæ‰éœ€è¦åˆ°nodeå¯åŠ¨æœåŠ¡ - if(server.size() != 0 && server[0].serverType == "tars_dns") - { - TLOGINFO(" '" + application + "." + serverName + "_" + nodeName + "' is tars_dns server"<getIp() << ":" << current->getPort() <setResponse(false); - NodePrxCallbackPtr callback = new StartServerCallbackImp(application, serverName, nodeName, current); - nodePrx->async_startServer(callback, application, serverName); - } - - return iRet; - } - catch(TarsSyncCallTimeoutException& tex) - { - current->setResponse(true); - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - TLOGERROR("AdminRegistryImp::startServer '"<<(application + "." + serverName + "_" + nodeName+ "' TarsSyncCallTimeoutException:" + tex.what())<setResponse(true); - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - TLOGERROR("AdminRegistryImp::startServer '"<<(application + "." + serverName + "_" + nodeName + "' TarsNodeNotRegistryException:" + re.what())<setResponse(true); - result = string(__FUNCTION__) + " '" + application + "." + serverName + "_" + nodeName - + "' TarsException:" + ex.what(); - TLOGERROR("AdminRegistryImp::startServer '"<<(application + "." + serverName + "_" + nodeName + "' TarsException:" + ex.what())<getIp() << ":" << current->getPort() < server; - server = _db.getServers(application, serverName, nodeName, true); - - //判断是å¦ä¸ºdns éždnsæ‰éœ€è¦åˆ°nodeå¯åŠ¨æœåŠ¡ - if(server.size() != 0 && server[0].serverType == "tars_dns") - { - TLOGINFO("AdminRegistryImp::stopServer '" + application + "." + serverName + "_" + nodeName + "' is tars_dns server"<getIp() << ":" << current->getPort() << "|" << iRet <getIp() << ":" << current->getPort() <setResponse(false); - NodePrxCallbackPtr callback = new StopServerCallbackImp(application, serverName, nodeName, current); - nodePrx->async_stopServer(callback, application, serverName); - } - - return iRet; - } - catch(TarsSyncCallTimeoutException& tex) - { - current->setResponse(true); - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - TLOGERROR("AdminRegistryImp::stopServer '"<<(application + "." + serverName + "_" + nodeName+ "' TarsSyncCallTimeoutException:" + tex.what())<setResponse(true); - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - TLOGERROR("AdminRegistryImp::stopServer '"<<(application + "." + serverName + "_" + nodeName+ "' TarsNodeNotRegistryException:" + re.what())<setResponse(true); - TLOGERROR("AdminRegistryImp::stopServer '"<<(application + "." + serverName + "_" + nodeName+ "' TarsException:" + ex.what())<getIp() << ":" << current->getPort() < server; - server = _db.getServers(application, serverName, nodeName, true); - - //判断是å¦ä¸ºdns éždnsæ‰éœ€è¦åˆ°nodeåœæ­¢ã€å¯åŠ¨æœåŠ¡ - if(server.size() != 0 && server[0].serverType == "tars_dns") - { - isDnsServer = true; - } - else - { - NodePrx nodePrx = _db.getNodePrx(nodeName); - iRet = nodePrx->stopServer(application, serverName, result); - } - TLOGDEBUG("AdminRegistryImp::restartServer stop|"<< application << "." << serverName << "_" << nodeName << "|" << current->getIp() << ":" << current->getPort() <getIp() << ":" << current->getPort() <startServer(application, serverName, result); - } - } - catch(TarsSyncCallTimeoutException& tex) - { - result = "AdminRegistryImp::restartServer '" + application + "." + serverName + "_" + nodeName - + "' TarsSyncCallTimeoutException:" + tex.what(); - - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - } - catch(TarsNodeNotRegistryException& re) - { - result = "AdminRegistryImp::restartServer '" + application + "." + serverName + "_" + nodeName - + "' TarsNodeNotRegistryException:" + re.what(); - - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - } - catch(TarsException & ex) - { - result += "AdminRegistryImp::restartServer '" + application + "." + serverName + "_" + nodeName - + "' TarsException:" + ex.what(); - iRet = EM_TARS_UNKNOWN_ERR; - } - TLOGERROR( result << endl); - } - return iRet; -} - -int AdminRegistryImp::notifyServer(const string & application, const string & serverName, const string & nodeName, const string &command, string &result, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("AdminRegistryImp::notifyServer: " << application << "." << serverName << "_" << nodeName << "|" << current->getIp() << ":" << current->getPort() <setResponse(false); - NodePrxCallbackPtr callback = new NotifyServerCallbackImp(current); - nodePrx->async_notifyServer(callback, application, serverName, command); - return EM_TARS_SUCCESS; - } - catch(TarsSyncCallTimeoutException& tex) - { - current->setResponse(true); - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - TLOGERROR("AdminRegistryImp::notifyServer '"<<(application + "." + serverName + "_" + nodeName - + "' TarsSyncCallTimeoutException:" + tex.what())<setResponse(true); - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - TLOGERROR("AdminRegistryImp::notifyServer '"<<(application + "." + serverName + "_" + nodeName - + "' TarsNodeNotRegistryException:" + re.what())<setResponse(true); - TLOGERROR("AdminRegistryImp::notifyServer '"<<(application + "." + serverName + "_" + nodeName - + "' TarsException:" + ex.what())<"]; - - TLOGDEBUG("AdminRegistryImp::batchPatch " - << reqPro.appname + "." + reqPro.servername + "_" + reqPro.nodename << "|" - << reqPro.binname << "|" - << reqPro.version << "|" - << reqPro.user << "|" - << reqPro.servertype << "|" - << reqPro.patchobj << "|" - << reqPro.md5 <<"|" - << reqPro.ostype - << endl); - - //获å–patch包的文件信æ¯å’Œmd5值 - string patchFile; - string md5; - int iRet = _db.getInfoByPatchId(reqPro.version, patchFile, md5); - if (iRet != 0) - { - TLOGERROR("AdminRegistryImp::batchPatch"<< ", get patch tgz error:" << reqPro.version << endl); - return EM_TARS_GET_PATCH_FILE_ERR; - } - - //让tarspatch准备å‘布包 - iRet = _patchPrx->preparePatchFile(reqPro.appname, reqPro.servername, patchFile); - if (iRet != 0) - { - TLOGERROR("AdminRegistryImp::batchPatch"<< ", prepare patch file error:" << patchFile << endl); - return EM_TARS_PREPARE_ERR; - } - - reqPro.md5 = md5; - - iRet = EM_TARS_UNKNOWN_ERR; - int defaultTime = 3000; - NodePrx proxy; - try - { - proxy = _db.getNodePrx(reqPro.nodename); - int timeout = TC_Common::strto(g_pconf->get("/tars/nodeinfo","10000")); - - current->setResponse(false); - NodePrxCallbackPtr callback = new PatchProCallbackImp(reqPro, proxy, defaultTime, current); - proxy->tars_set_timeout(timeout)->async_patchPro(callback, reqPro); - } - catch(TarsSyncCallTimeoutException& tex) - { - current->setResponse(true); - result = tex.what(); - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - TLOGERROR("AdminRegistryImp::batchPatch " << reqPro.appname + "." + reqPro.servername + "_" + reqPro.nodename <<"|ret."<setResponse(true); - result = re.what(); - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - TLOGERROR("AdminRegistryImp::batchPatch "<< reqPro.appname + "." + reqPro.servername + "_" + reqPro.nodename <<"|ret."<setResponse(true); - result = ex.what(); - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - TLOGERROR("AdminRegistryImp::batchPatch "<< reqPro.appname + "." + reqPro.servername + "_" + reqPro.nodename <<"|ret."<setResponse(true); - result = "Unknown Exception"; - TLOGERROR("AdminRegistryImp::batchPatch " << reqPro.appname + "." + reqPro.servername + "_" + reqPro.nodename <<"|ret."<getIp() << ":" << current->getPort() <setResponse(false); - NodePrxCallbackPtr callback = new GetPatchPercentCallbackImp(application, serverName, nodeName, current); - nodePrx->async_getPatchPercent(callback, application, serverName); - } - catch(TarsSyncCallTimeoutException& tex) - { - current->setResponse(true); - TLOGERROR( "AdminRegistryImp::getPatchPercent: " << application + "." + serverName + "_" + nodeName << "|TarsSyncCallTimeoutException:" << tex.what() << endl); - result = tex.what(); - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - } - catch(TarsNodeNotRegistryException& rex) - { - current->setResponse(true); - iRet = EM_TARS_NODE_NOT_REGISTRY_ERR; - TLOGERROR("AdminRegistryImp::getPatchPercent: '" <<(application + "." + serverName + "_" + nodeName + " Caller:" + current->getIp() + ":" + TC_Common::tostr(current->getPort()) - + "' TarsNodeNotRegistryException:" + rex.what())<setResponse(true); - iRet = EM_TARS_UNKNOWN_ERR; - TLOGERROR("AdminRegistryImp::getPatchPercent: '" <<(application + "." + serverName + "_" + nodeName + " Caller:" + current->getIp() + ":" + TC_Common::tostr(current->getPort()) - + "' TarsException:" + ex.what())<loadServer(application, serverName, result); - } - catch(TarsSyncCallTimeoutException& tex) - { - TLOGERROR("AdminRegistryImp::loadServer: "<< application + "." + serverName + "_" + nodeName << "|Exception:" << tex.what() << endl); - return EM_TARS_CALL_NODE_TIMEOUT_ERR; - } - catch(TarsNodeNotRegistryException& rex) - { - TLOGERROR("AdminRegistryImp::loadServer: '"<<(" '" + application + "." + serverName + "_" + nodeName - + "' exception:" + rex.what())< server; - server = _db.getServers(application, serverName, nodeName, true); - if(server.size() > 0) - { - profileTemplate = server[0].profile; - iRet = 0; - } - } - TLOGDEBUG( "AdminRegistryImp::getServerProfileTemplate get " << application<<"."<getIp(); - return 0; -} - -int AdminRegistryImp::gridPatchServer(const vector &gridDescList, vector &gridFailDescList, std::string & resultDesc, tars::TarsCurrentPtr current) -{ - TLOGDEBUG(__FUNCTION__ << "|gridDescList size:" << gridDescList.size() << endl); - - int iRet = 0; - - try - { - gridFailDescList.clear(); - - for(size_t i = 0; i < gridDescList.size(); ++i) - { - if(gridDescList[i].application != "" && gridDescList[i].servername != "" && gridDescList[i].nodename != "") - { - string status(""); - - if(gridDescList[i].status == NORMAL) - { - status = "NORMAL"; - } - else if(gridDescList[i].status == GRID) - { - status = "GRID"; - } - else - { - status = "NO_FLOW"; - } - - int ret = _db.gridPatchServer(gridDescList[i].application, gridDescList[i].servername, gridDescList[i].nodename, status); - - if(ret < 0) - { - gridFailDescList.push_back(gridDescList[i]); - iRet = -1; - TLOGERROR(__FUNCTION__ << "|app:" << gridDescList[i].application << "|servername:" << gridDescList[i].servername << "|node:" << gridDescList[i].nodename << "|state:" << status << "|ret:" << ret << endl); - DLOG<<__FUNCTION__ << "|app:" << gridDescList[i].application << "|servername:" << gridDescList[i].servername << "|node:" << gridDescList[i].nodename << "|state:" << status << "|ret:" << ret << endl; - } - else - { - TLOGDEBUG(__FUNCTION__ << "|app:" << gridDescList[i].application << "|servername:" << gridDescList[i].servername << "|node:" << gridDescList[i].nodename << "|state:" << status << "|ret:" << ret << endl); - DLOG<<__FUNCTION__ << "|app:" << gridDescList[i].application << "|servername:" << gridDescList[i].servername << "|node:" << gridDescList[i].nodename << "|state:" << status << "|ret:" << ret << endl; - } - } - else - { - TLOGERROR(__FUNCTION__ << "|app:" << gridDescList[i].application << "|servername:" << gridDescList[i].servername << "|node:" << gridDescList[i].nodename << endl); - DLOG<<__FUNCTION__ << "|app:" << gridDescList[i].application << "|servername:" << gridDescList[i].servername << "|node:" << gridDescList[i].nodename << endl; - } - } - - return iRet; - } - catch(exception & ex) - { - iRet = EM_TARS_UNKNOWN_ERR; - resultDesc = string(__FUNCTION__) + "|exception:" + ex.what(); - TLOGERROR(resultDesc << endl); - DLOG<tars_timeout(_defaultTime); - - AdminReg::async_response_batchPatch(_current, ret, result); -} - -void PatchProCallbackImp::callback_patchPro_exception(tars::Int32 ret) -{ - int iRet = EM_TARS_UNKNOWN_ERR; - _nodePrx->tars_timeout(_defaultTime); - - if(ret == tars::TARSSERVERQUEUETIMEOUT || ret == tars::TARSASYNCCALLTIMEOUT) - { - iRet = EM_TARS_CALL_NODE_TIMEOUT_ERR; - } - - AdminReg::async_response_batchPatch(_current, iRet, ""); - TLOGDEBUG("PatchProCallbackImp::callback_patchPro_exception: |exception ret."<getIp() << ":" << _current->getPort() << "|" << ret <getIp() << ":" << _current->getPort() << "|" << ret <getIp() << ":" << _current->getPort() << "|" << ret <getIp() << ":" << _current->getPort() << "|" << ret <getIp() << ":" << _current->getPort() << "|" << ret << "|" << result <getIp() << ":" << _current->getPort() << "|" << ret <getState(_application, _serverName, resultRef); - _state.presentStateInNode = etos(stateInNode); - _state.processId = _nodePrx->getServerPid(_application, _serverName, resultRef); - TLOGWARN("GetServerStateCallbackImp::callback_getStateInfo_exception '" + _application + "." + _serverName + "_" + _nodeName<<"|"<< resultRef <getState(_application, _serverName, result); - _state.presentStateInNode = etos(stateInNode); - _state.processId = _nodePrx->getServerPid(_application, _serverName, result); - TLOGERROR("GetServerStateCallbackImp::callback_getStateInfo_exception '" + _application + "." + _serverName + "_" + _nodeName<<"|"<< result < - */ - virtual int getTaskHistory(const string & application, const string & serverName, const string & command, vector &taskRsp, tars::TarsCurrentPtr current); - - /** - * è®¾ç½®ä»»åŠ¡çŠ¶æ€ - * - * @param itemNo - * @param startTime - * @param endTime - * @param status - * @param log - * @param current - * - * @return int - */ - virtual int setTaskItemInfo(const string & itemNo, const map &info, tars::TarsCurrentPtr current); - - /***********application****************/ - /** - * å¸è½½æœåŠ¡ - * - * @param application - * @param serverName - * @param nodeName - * @param current - * - * @return int - */ - virtual int undeploy(const string & application, const string & serverName, const string & nodeName, const string &user, string &log, tars::TarsCurrentPtr current); - - /** - * 获å–application列表 - * - * @param null - * @param out result : 结果æè¿° - * - * @return application列表 - */ - virtual vector getAllApplicationNames(string &result, tars::TarsCurrentPtr current); - - - /***********node****************/ - - /** - * 获å–node列表 - * - * @param null - * @param out result : 结果æè¿° - * - * @return node 列表 - */ - virtual vector getAllNodeNames(string &result, tars::TarsCurrentPtr current); - - /** - * 获å–node版本 - * @param name nodeå称 - * @param version node版本 - * @param out result 结果æè¿° - * @return 0-æˆåŠŸ others-失败 - */ - virtual int getNodeVesion(const string &nodeName, string &version, string & result, tars::TarsCurrentPtr current); - - /** - * ping node - * - * @param name: node id - * @param out result : 结果æè¿° - * - * @return : true-ping通;false-ä¸é€š - */ - virtual bool pingNode(const string & name, string &result, tars::TarsCurrentPtr current); - - /** - * åœæ­¢ node - * - * @param name: node id - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int shutdownNode(const string & name, string &result, tars::TarsCurrentPtr current); - - /** - * 获å–server列表 - * - * @param name: null - * @param out result : 结果æè¿° - * - * @return: server列表åŠç›¸å…³ä¿¡æ¯ - */ - virtual vector > getAllServerIds(string &result, tars::TarsCurrentPtr current); - - /** - * 获å–特定serverçŠ¶æ€ - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeNmae : node id - * @param out state : çŠ¶æ€ - * @param out result : 结果æè¿° - * - * @return : 处ç†ç»“æžœ - */ - virtual int getServerState(const string & application, const string & serverName, const string & nodeName, ServerStateDesc &state, string &result, tars::TarsCurrentPtr current); - - /** - * 获å–特定ip所属group - * - * @param sting: ip - * @param out int : group id - * @param out result : 结果æè¿° - * - * @return : 处ç†ç»“æžœ - */ - - virtual int getGroupId(const string & ip,int &groupId, string &result, tars::TarsCurrentPtr current); - - /** - * å¯åŠ¨ç‰¹å®šserver - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int startServer(const string & application, const string & serverName, const string & nodeName, string &result, tars::TarsCurrentPtr current); - - /** - * åœæ­¢ç‰¹å®šserver - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int stopServer(const string & application, const string & serverName, const string & nodeName, string &result, tars::TarsCurrentPtr current); - - /** - * é‡å¯ç‰¹å®šserver - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int restartServer(const string & application, const string & serverName, const string & nodeName, string &result, tars::TarsCurrentPtr current); - - /** - * 通知æœåŠ¡ - * @param application - * @param serverName - * @param nodeName - * @param command - * @param result - * @param current - * - * @return int - */ - virtual int notifyServer(const string & application, const string & serverName, const string & nodeName, const string &command, string &result, tars::TarsCurrentPtr current); - - /** - * 批é‡å‘布 - * - * @param PatchRequest : å‘布请求 - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int batchPatch(const tars::PatchRequest & req, string &result, tars::TarsCurrentPtr current); - - /** - * å‘布æˆåŠŸ - * - * @param req - * @param result - * @param current - * - * @return int - */ - virtual int updatePatchLog(const string &application, const string & serverName, const string & nodeName, const string & patchId, const string & user, const string &patchType, bool succ, tars::TarsCurrentPtr current); - - /** - * 获å–æœåŠ¡å‘布进度 - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @param nodeName :node id - * @out tPatchInfo :å‘布百分比 - * @return :0-æˆåŠŸ others-失败 - */ - virtual int getPatchPercent(const string &application, const string &serverName,const string & nodeName, PatchInfo &tPatchInfo, tars::TarsCurrentPtr current); - - /** - * 加载特定server - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int loadServer(const string & application, const string & serverName, const string & nodeName, string &result, tars::TarsCurrentPtr current); - - /** - * 获å–ç›¸åº”æ¨¡æ¿ - * - * @param profileName: 模æ¿å称 - * @param out profileTemplate: 模æ¿å†…容 - * @param out resultDesc: 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int getProfileTemplate(const std::string & profileName,std::string &profileTemplate, std::string & resultDesc, tars::TarsCurrentPtr current); - - /** - * 获å–务æœç›¸åº”æ¨¡æ¿ - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out profileTemplate: 模æ¿å†…容 - * @param out resultDesc: 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - virtual int getServerProfileTemplate(const string & application, const string & serverName, const string & nodeName,std::string &profileTemplate, std::string & resultDesc, tars::TarsCurrentPtr current); - - /** - * node通过接å£èŽ·å–连接上主控的node ip - * @param sNodeIp: node çš„ip - * - * @return 0-æˆåŠŸ others-失败 - */ - virtual int getClientIp(std::string &sClientIp,tars::TarsCurrentPtr current); - - virtual int gridPatchServer(const vector &gridDescList, vector &gridFailDescList, std::string & resultDesc, tars::TarsCurrentPtr current); -protected: - - //æ•°æ®åº“æ“作类对象 - DbProxy _db; - - PatchPrx _patchPrx; -}; - -class PatchProCallbackImp: public NodePrxCallback -{ -public: - PatchProCallbackImp(const tars::PatchRequest& req, const NodePrx& nodePrx, int defaultTime, tars::TarsCurrentPtr current) - : _reqPro(req) - , _nodePrx(nodePrx) - , _defaultTime(defaultTime) - , _current(current) - { - } - - virtual void callback_patchPro(tars::Int32 ret, const std::string& result); - virtual void callback_patchPro_exception(tars::Int32 ret); - -private: - - tars::PatchRequest _reqPro; - NodePrx _nodePrx; - int _defaultTime; - tars::TarsCurrentPtr _current; -}; - - -class StartServerCallbackImp: public NodePrxCallback -{ -public: - StartServerCallbackImp(string application, string serverName, string nodeName, tars::TarsCurrentPtr current) - : _application(application) - , _serverName(serverName) - , _nodeName(nodeName) - , _current(current) - { - } - - virtual void callback_startServer(tars::Int32 ret, const std::string& result); - virtual void callback_startServer_exception(tars::Int32 ret); - -private: - string _application; - string _serverName; - string _nodeName; - tars::TarsCurrentPtr _current; -}; - -class StopServerCallbackImp: public NodePrxCallback -{ -public: - StopServerCallbackImp(string application, string serverName, string nodeName, tars::TarsCurrentPtr current) - : _application(application) - , _serverName(serverName) - , _nodeName(nodeName) - , _current(current) - { - } - - virtual void callback_stopServer(tars::Int32 ret, const std::string& result); - virtual void callback_stopServer_exception(tars::Int32 ret); - -private: - string _application; - string _serverName; - string _nodeName; - tars::TarsCurrentPtr _current; -}; - -class NotifyServerCallbackImp: public NodePrxCallback -{ -public: - NotifyServerCallbackImp(tars::TarsCurrentPtr current) - : _current(current) - { - } - - virtual void callback_notifyServer(tars::Int32 ret, const std::string& result); - virtual void callback_notifyServer_exception(tars::Int32 ret); - -private: - tars::TarsCurrentPtr _current; -}; - -class GetServerStateCallbackImp: public NodePrxCallback -{ -public: - GetServerStateCallbackImp(const NodePrx& nodePrx, string application, string serverName, string nodeName, const ServerStateDesc& state, tars::TarsCurrentPtr current) - : _nodePrx(nodePrx) - , _application(application) - , _serverName(serverName) - , _nodeName(nodeName) - , _state(state) - , _current(current) - { - } - - virtual void callback_getStateInfo(tars::Int32 ret, const tars::ServerStateInfo& info, const std::string& result); - virtual void callback_getStateInfo_exception(tars::Int32 ret); -private: - NodePrx _nodePrx; - string _application; - string _serverName; - string _nodeName; - ServerStateDesc _state; - tars::TarsCurrentPtr _current; -}; - - -class GetPatchPercentCallbackImp: public NodePrxCallback -{ -public: - GetPatchPercentCallbackImp(string application, string serverName, string nodeName, tars::TarsCurrentPtr current) - : _application(application) - , _serverName(serverName) - , _nodeName(nodeName) - , _current(current) - { - } - - virtual void callback_getPatchPercent(tars::Int32 ret, const tars::PatchInfo& tPatchInfo); - virtual void callback_getPatchPercent_exception(tars::Int32 ret); - -private: - string _application; - string _serverName; - string _nodeName; - tars::TarsCurrentPtr _current; -}; - - -#endif diff --git a/cpp/framework/AdminRegistryServer/AdminRegistryServer.cpp b/cpp/framework/AdminRegistryServer/AdminRegistryServer.cpp deleted file mode 100644 index 6a1a51534..000000000 --- a/cpp/framework/AdminRegistryServer/AdminRegistryServer.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "AdminRegistryServer.h" -#include "AdminRegistryImp.h" - -TC_Config * g_pconf; -AdminRegistryServer g_app; - -extern TC_Config * g_pconf; - -//内部版本 -const string SERVER_VERSION = "B002"; - -void AdminRegistryServer::initialize() -{ - TLOGDEBUG("AdminRegistryServer::initialize..." << endl); - - try - { - string size = Application::getCommunicator()->getProperty("timeout-queue-size", ""); - if(size.empty()) - { - Application::getCommunicator()->setProperty("timeout-queue-size","100"); - } - - loadServantEndpoint(); - - //轮询线程 - _reapThread.init(); - _reapThread.start(); - - //ä¾›admin client访问的对象 - string adminObj = g_pconf->get("/tars/objname", ""); - if(adminObj != "") - { - addServant(adminObj); - } - } - catch(TC_Exception & ex) - { - TLOGERROR("RegistryServer initialize exception:" << ex.what() << endl); - cerr << "RegistryServer initialize exception:" << ex.what() << endl; - exit(-1); - } - - TLOGDEBUG("RegistryServer::initialize OK!" << endl); -} - -int AdminRegistryServer::loadServantEndpoint() -{ - map mapAdapterServant; - mapAdapterServant = ServantHelperManager::getInstance()->getAdapterServant(); - - map::iterator iter; - for(iter = mapAdapterServant.begin(); iter != mapAdapterServant.end(); iter++ ) - { - TC_Endpoint ep = getEpollServer()->getBindAdapter(iter->first)->getEndpoint(); - - _mapServantEndpoint[iter->second] = ep.toString(); - - TLOGDEBUG("registry obj: " << iter->second << " = " << ep.toString() < getServantEndpoint() { return _mapServantEndpoint; } - -private: - int loadServantEndpoint(); - -protected: - - //用于执行定时æ“作的线程对象 - AdminReapThread _reapThread; - - //对象-适é…器 列表 - map _mapServantEndpoint; -}; - -extern AdminRegistryServer g_app; -extern const string SERVER_VERSION; diff --git a/cpp/framework/AdminRegistryServer/CMakeLists.txt b/cpp/framework/AdminRegistryServer/CMakeLists.txt deleted file mode 100644 index ed28f9d6f..000000000 --- a/cpp/framework/AdminRegistryServer/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(MODULE "tarsAdminRegistry") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/bin) - -#complice_module(${MODULE} "AdminReg.tars") -complice_module(${MODULE}) diff --git a/cpp/framework/AdminRegistryServer/DbProxy.cpp b/cpp/framework/AdminRegistryServer/DbProxy.cpp deleted file mode 100644 index 9a2355982..000000000 --- a/cpp/framework/AdminRegistryServer/DbProxy.cpp +++ /dev/null @@ -1,953 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbProxy.h" -#include "AdminRegistryServer.h" -#include "util/tc_parsepara.h" -#include "ExecuteTask.h" - -TC_ThreadLock DbProxy::_mutex; - -map DbProxy::_mapNodePrxCache; - -TC_ThreadLock DbProxy::_NodePrxLock; -vector >DbProxy::_serverGroupRule; -//key-ip, value-ç»„ç¼–å· -map DbProxy::_serverGroupCache; - - -int DbProxy::init(TC_Config *pconf) -{ - try - { - - TC_DBConf tcDBConf; - tcDBConf.loadFromMap(pconf->getDomainMap("/tars/db")); - _mysqlReg.init(tcDBConf); - } - catch (TC_Config_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - - return 0; -} - -int DbProxy::addTaskReq(const TaskReq &taskReq) -{ - try - { - for (size_t i = 0; i < taskReq.taskItemReq.size(); i++) - { - map > data; - data["task_no"] = make_pair(TC_Mysql::DB_STR, taskReq.taskNo); - data["item_no"] = make_pair(TC_Mysql::DB_STR, taskReq.taskItemReq[i].itemNo); - data["application"] = make_pair(TC_Mysql::DB_STR, taskReq.taskItemReq[i].application); - data["server_name"] = make_pair(TC_Mysql::DB_STR, taskReq.taskItemReq[i].serverName); - data["node_name"] = make_pair(TC_Mysql::DB_STR, taskReq.taskItemReq[i].nodeName); - data["command"] = make_pair(TC_Mysql::DB_STR, taskReq.taskItemReq[i].command); - data["parameters"] = make_pair(TC_Mysql::DB_STR, TC_Parsepara(taskReq.taskItemReq[i].parameters).tostr()); - - _mysqlReg.insertRecord("t_task_item", data); - } - - { - map > data; - data["task_no"] = make_pair(TC_Mysql::DB_STR, taskReq.taskNo); - data["serial"] = make_pair(TC_Mysql::DB_INT, TC_Common::tostr(taskReq.serial)); - data["create_time"] = make_pair(TC_Mysql::DB_INT, "now()"); - data["user_name"] = make_pair(TC_Mysql::DB_STR, taskReq.userName); - - _mysqlReg.insertRecord("t_task", data); - } - } - catch (exception &ex) - { - TLOGERROR("DbProxy::addTaskReq exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -int DbProxy::getTaskRsp(const string &taskNo, TaskRsp &taskRsp) -{ - - try - { - string sql = "select * from t_task as t1, t_task_item as t2 where t1.task_no=t2.task_no and t2.task_no='" - + _mysqlReg.escapeString(taskNo) + "'"; - - tars::TC_Mysql::MysqlData item = _mysqlReg.queryRecord(sql); - if (item.size() == 0) - { - TLOGERROR("DbProxy::getTaskRsp 't_task' not task: " << taskNo << endl); - return -1; - } - - taskRsp.taskNo = item[0]["task_no"]; - taskRsp.serial = TC_Common::strto(item[0]["serial"]); - taskRsp.userName = item[0]["user_name"]; - - - for (unsigned i = 0; i < item.size(); i++) - { - TaskItemRsp rsp; - rsp.startTime = item[i]["start_time"]; - rsp.endTime = item[i]["end_time"]; - rsp.status = (EMTaskItemStatus)TC_Common::strto(item[i]["status"]); - rsp.statusInfo = etos(rsp.status); - - rsp.req.taskNo = item[i]["task_no"]; - rsp.req.itemNo = item[i]["item_no"]; - rsp.req.application = item[i]["application"]; - rsp.req.serverName = item[i]["server_name"]; - rsp.req.nodeName = item[i]["node_name"]; - rsp.req.setName = item[i]["set_name"]; - rsp.req.command = item[i]["command"]; - rsp.req.parameters = TC_Parsepara(item[i]["parameters"]).toMap(); - - taskRsp.taskItemRsp.push_back(rsp); - } - - ExecuteTask::getInstance()->checkTaskRspStatus(taskRsp); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("DbProxy::getTaskRsp exception"< &taskRsp) -{ - string sql = "select t1.`create_time`, t1.`serial`, t1.`user_name`, t2.* from t_task as t1, t_task_item as t2 where t1.task_no=t2.task_no and t2.application='" - + _mysqlReg.escapeString(application) + "' and t2.server_name='" - + _mysqlReg.escapeString(serverName) + "' and t2.command='" - + _mysqlReg.escapeString(command) + "' order by create_time desc, task_no"; - - try - { - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - TLOGDEBUG("DbProxy::getTaskHistory size:" << res.size() << ", sql:" << sql << endl); - for (unsigned i = 0; i < res.size(); i++) - { - string taskNo = res[i]["task_no"]; - - //获å–Item - TaskItemRsp itemRsp; - itemRsp.startTime = res[i]["start_time"]; - itemRsp.endTime = res[i]["end_time"]; - itemRsp.status = (EMTaskItemStatus)TC_Common::strto(res[i]["status"]); - itemRsp.statusInfo= etos(itemRsp.status); - - itemRsp.req.taskNo = res[i]["task_no"]; - itemRsp.req.itemNo = res[i]["item_no"]; - itemRsp.req.application = res[i]["application"]; - itemRsp.req.serverName = res[i]["server_name"]; - itemRsp.req.nodeName = res[i]["node_name"]; - itemRsp.req.setName = res[i]["set_name"]; - itemRsp.req.command = res[i]["command"]; - itemRsp.req.parameters = TC_Parsepara(res[i]["parameters"]).toMap(); - - if (taskRsp.empty() || taskRsp[taskRsp.size() - 1].taskNo != taskNo) - { - //æ–°çš„TaskRsp - TaskRsp rsp; - rsp.taskNo = res[i]["task_no"]; - rsp.serial = TC_Common::strto(res[i]["serial"]); - rsp.userName = res[i]["user_name"]; - - rsp.taskItemRsp.push_back(itemRsp); - - taskRsp.push_back(rsp); - } - else - { - taskRsp.back().taskItemRsp.push_back(itemRsp); - } - } - } - catch (exception &ex) - { - TLOGERROR("DbProxy::getTaskHistory exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -int DbProxy::setTaskItemInfo(const string & itemNo, const map &info) -{ - string where = " where item_no='" + itemNo + "'"; - try - { - map > data; - data["item_no"] = make_pair(TC_Mysql::DB_STR, itemNo); - map::const_iterator it = info.find("start_time"); - if (it != info.end()) - { - data["start_time"] = make_pair(TC_Mysql::DB_STR, it->second); - } - it = info.find("end_time"); - if (it != info.end()) - { - data["end_time"] = make_pair(TC_Mysql::DB_STR, it->second); - } - it = info.find("status"); - if (it != info.end()) - { - data["status"] = make_pair(TC_Mysql::DB_INT, it->second); - } - it = info.find("log"); - if (it != info.end()) - { - data["log"] = make_pair(TC_Mysql::DB_STR, it->second); - } - - _mysqlReg.updateRecord("t_task_item", data, where); - } - catch (exception &ex) - { - TLOGERROR("DbProxy::setTaskItemInfo exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -int DbProxy::undeploy(const string & application, const string & serverName, const string & nodeName, const string &user, string &log) -{ - string where = "where application='" + application + "' and server_name='" + serverName + "' and node_name='" +nodeName + "'"; - - try - { - - _mysqlReg.deleteRecord("t_server_conf", where); - - _mysqlReg.deleteRecord("t_adapter_conf", where); - - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("DbProxy::undeploy exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -map DbProxy::getActiveNodeList(string& result) -{ - map mapNodeList; - try - { - string sql = - "select node_name, node_obj from t_node_info " - "where present_state='active'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - TLOGDEBUG("DbProxy::getActiveNodeList (present_state='active') affected:" << res.size() << endl); - for (unsigned i = 0; i < res.size(); i++) - { - mapNodeList[res[i]["node_name"]] = res[i]["node_obj"]; - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("DbProxy::getActiveNodeList exception: " < 0) - { - version = res[0]["tars_version"]; - return 0; - - } - result = "node_name(" + nodeName + ") int table t_node_info not exist"; - } - catch (TC_Mysql_Exception& ex) - { - result = string(__FUNCTION__) + " exception: " + ex.what(); - TLOGERROR(result << endl); - } - return -1; -} - -int DbProxy::updateServerState(const string& app, const string& serverName, const string& nodeName, - const string& stateFields, tars::ServerState state, int processId) -{ - try - { - int64_t iStart = TC_TimeProvider::getInstance()->getNowMs(); - if (stateFields != "setting_state" && stateFields != "present_state") - { - TLOGDEBUG(app << "." << serverName << "_" << nodeName - << " not supported fields:" << stateFields << endl); - return -1; - } - - string sProcessIdSql = (stateFields == "present_state" ? - (", process_id = " + TC_Common::tostr(processId) + " ") : ""); - - string sql = - "update t_server_conf " - "set " + stateFields + " = '" + etos(state) + "' " + sProcessIdSql + - "where application='" + _mysqlReg.escapeString(app) + "' " - " and server_name='" + _mysqlReg.escapeString(serverName) + "' " - " and node_name='" + _mysqlReg.escapeString(nodeName) + "' "; - - _mysqlReg.execute(sql); - TLOGDEBUG(__FUNCTION__ << " " << app << "." << serverName << "_" << nodeName - << " affected:" << _mysqlReg.getAffectedRows() - << "|cost:" << (TC_TimeProvider::getInstance()->getNowMs() - iStart) << endl); - return 0; - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << app << "." << serverName << "_" << nodeName - << " exception: " << ex.what() << endl); - return -1; - } -} - -int DbProxy::gridPatchServer(const string& app, const string& servername, const string& nodename, const string& status) -{ - try - { - string sql("update t_server_conf"); - sql += " set grid_flag='"; - sql += status; - sql += "' where application='"; - sql += _mysqlReg.escapeString(app); - sql += "' and server_name='"; - sql += _mysqlReg.escapeString(servername); - sql += "' and node_name='"; - sql += _mysqlReg.escapeString(nodename); - sql += "'"; - - int64_t iStart = TC_TimeProvider::getInstance()->getNowMs(); - - _mysqlReg.execute(sql); - - TLOGDEBUG(__FUNCTION__ << "|app:" << app << "|server:" << servername << "|node:" << nodename - << "|affected:" << _mysqlReg.getAffectedRows() << "|cost:" << (TC_TimeProvider::getInstance()->getNowMs() - iStart) << endl); - - return 0; - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << "|app:" << app << "|server:" << servername << "|node:" << nodename - << "|exception: " << ex.what() << endl); - return -1; - } -} - -vector DbProxy::getServers(const string& app, const string& serverName, const string& nodeName, bool withDnsServer) -{ - string sql; - vector vServers; - unsigned num = 0; - int64_t iStart = TC_TimeProvider::getInstance()->getNowMs(); - - try - { - //server详细é…ç½® - string sCondition; - sCondition += "server.node_name='" + _mysqlReg.escapeString(nodeName) + "'"; - if (app != "") sCondition += " and server.application='" + _mysqlReg.escapeString(app) + "' "; - if (serverName != "") sCondition += " and server.server_name='" + _mysqlReg.escapeString(serverName) + "' "; - if (withDnsServer == false) sCondition += " and server.server_type !='tars_dns' "; //ä¸èŽ·å–dnsæœåŠ¡ - -// " allow_ip, max_connections, servant, queuecap, queuetimeout,protocol,handlegroup,shmkey,shmcap," - - sql = - "select server.application, server.server_name, server.node_name, base_path, " - " exe_path, setting_state, present_state, adapter_name, thread_num, async_thread_num, endpoint," - " profile,template_name, " - " allow_ip, max_connections, servant, queuecap, queuetimeout,protocol,handlegroup," - " patch_version, patch_time, patch_user, " - " server_type, start_script_path, stop_script_path, monitor_script_path,config_center_port ," - " enable_set, set_name, set_area, set_group " - "from t_server_conf as server " - " left join t_adapter_conf as adapter using(application, server_name, node_name) " - "where " + sCondition; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - num = res.size(); - //对应server在vector的下标 - map mapAppServerTemp; - - //获å–模版profile内容 - map mapProfile; - - //分拆数æ®åˆ°serverçš„ä¿¡æ¯ç»“构里 - for (unsigned i = 0; i < res.size(); i++) - { - string sServerId = res[i]["application"] + "." + res[i]["server_name"] - + "_" + res[i]["node_name"]; - - if (mapAppServerTemp.find(sServerId) == mapAppServerTemp.end()) - { - //serveré…ç½® - ServerDescriptor server; - server.application = res[i]["application"]; - server.serverName = res[i]["server_name"]; - server.nodeName = res[i]["node_name"]; - server.basePath = res[i]["base_path"]; - server.exePath = res[i]["exe_path"]; - server.settingState = res[i]["setting_state"]; - server.presentState = res[i]["present_state"]; - server.patchVersion = res[i]["patch_version"]; - server.patchTime = res[i]["patch_time"]; - server.patchUser = res[i]["patch_user"]; - server.profile = res[i]["profile"]; - server.serverType = res[i]["server_type"]; - server.startScript = res[i]["start_script_path"]; - server.stopScript = res[i]["stop_script_path"]; - server.monitorScript = res[i]["monitor_script_path"]; - server.configCenterPort = TC_Common::strto(res[i]["config_center_port"]); - - server.setId = ""; - if (TC_Common::lower(res[i]["enable_set"]) == "y") - { - server.setId = res[i]["set_name"] + "." + res[i]["set_area"] + "." + res[i]["set_group"]; - } - - //获å–父模版profile内容 - if (mapProfile.find(res[i]["template_name"]) == mapProfile.end()) - { - string sResult; - mapProfile[res[i]["template_name"]] = getProfileTemplate(res[i]["template_name"], sResult); - } - - TC_Config tParent, tProfile; - tParent.parseString(mapProfile[res[i]["template_name"]]); - tProfile.parseString(server.profile); - int iDefaultAsyncThreadNum = 3; - if (server.serverType == "tars_nodejs") - { - iDefaultAsyncThreadNum = 0; - } - int iConfigAsyncThreadNum = TC_Common::strto(TC_Common::trim(res[i]["async_thread_num"])); - iDefaultAsyncThreadNum = iConfigAsyncThreadNum > iDefaultAsyncThreadNum ? iConfigAsyncThreadNum : iDefaultAsyncThreadNum; - server.asyncThreadNum = TC_Common::strto(tProfile.get("/tars/application/client", TC_Common::tostr(iDefaultAsyncThreadNum))); - tParent.joinConfig(tProfile, true); - server.profile = tParent.tostr(); - - mapAppServerTemp[sServerId] = vServers.size(); - vServers.push_back(server); - } - - //adapteré…ç½® - AdapterDescriptor adapter; - adapter.adapterName = res[i]["adapter_name"]; - if (adapter.adapterName == "") - { - //adapter没é…置,left join åŽä¸º NULL,ä¸æ”¾åˆ°adapters map - continue; - } - - adapter.threadNum = res[i]["thread_num"]; - adapter.endpoint = res[i]["endpoint"]; - adapter.maxConnections = TC_Common::strto(res[i]["max_connections"]); - adapter.allowIp = res[i]["allow_ip"]; - adapter.servant = res[i]["servant"]; - adapter.queuecap = TC_Common::strto(res[i]["queuecap"]); - adapter.queuetimeout = TC_Common::strto(res[i]["queuetimeout"]); - adapter.protocol = res[i]["protocol"]; - adapter.handlegroup = res[i]["handlegroup"]; - - vServers[mapAppServerTemp[sServerId]].adapters[adapter.adapterName] = adapter; - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << app << "." << serverName << "_" << nodeName - << " exception: " << ex.what() << "|" << sql << endl); - return vServers; - } - catch (TC_Config_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << app << "." << serverName << "_" << nodeName - << " TC_Config_Exception exception: " << ex.what() << endl); - throw TarsException(string("TC_Config_Exception exception: ") + ex.what()); - } - - TLOGDEBUG(app << "." << serverName << "_" << nodeName - << " getServers affected:" << num - << "|cost:" << (TC_TimeProvider::getInstance()->getNowMs() - iStart) << endl); - - return vServers; - -} - -int DbProxy::getNodeList(const string& app, const string& serverName, vector& nodeNames) -{ - int ret = 0; - nodeNames.clear(); - try - { - string sql = "select node_name from t_server_conf where application='" + _mysqlReg.escapeString(app) + "' and server_name='" + _mysqlReg.escapeString(serverName) + "'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - - for (unsigned i = 0; i < res.size(); i++) - { - nodeNames.push_back(res[i]["node_name"]); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - ret = -1; - } - return ret; -} - - -string DbProxy::getProfileTemplate(const string& sTemplateName, string& sResultDesc) -{ - map mapRecursion; - return getProfileTemplate(sTemplateName, mapRecursion, sResultDesc); -} - -string DbProxy::getProfileTemplate(const string& sTemplateName, map& mapRecursion, string& sResultDesc) -{ - try - { - string sql = "select template_name, parents_name, profile from t_profile_template " - "where template_name='" + _mysqlReg.escapeString(sTemplateName) + "'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - - if (res.size() == 0) - { - sResultDesc += "(" + sTemplateName + ":template not found)"; - return ""; - } - - TC_Config confMyself, confParents; - confMyself.parseString(res[0]["profile"]); - //mapRecursion用于é¿å…é‡å¤ç»§æ‰¿ - mapRecursion[res[0]["template_name"]] = 1; - - if (res[0]["parents_name"] != "" && mapRecursion.find(res[0]["parents_name"]) == mapRecursion.end()) - { - confParents.parseString(getProfileTemplate(res[0]["parents_name"], mapRecursion, sResultDesc)); - confMyself.joinConfig(confParents, false); - } - sResultDesc += "(" + sTemplateName + ":OK)"; - - TLOGDEBUG(__FUNCTION__ << " " << sTemplateName << " " << sResultDesc << endl); - - return confMyself.tostr(); - } - catch (TC_Mysql_Exception& ex) - { - sResultDesc += "(" + sTemplateName + ":" + ex.what() + ")"; - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - catch (TC_Config_Exception& ex) - { - sResultDesc += "(" + sTemplateName + ":" + ex.what() + ")"; - TLOGERROR(__FUNCTION__ << " TC_Config_Exception exception: " << ex.what() << endl); - } - - return ""; -} - -vector DbProxy::getAllApplicationNames(string& result) -{ - vector vApps; - try - { - string sql = "select distinct application from t_server_conf"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - TLOGDEBUG(__FUNCTION__ << " affected:" << res.size() << endl); - - for (unsigned i = 0; i < res.size(); i++) - { - vApps.push_back(res[i]["application"]); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - return vApps; - } - - return vApps; - -} - -vector > DbProxy::getAllServerIds(string& result) -{ - vector > vServers; - try - { - string sql = - "select application, server_name, node_name, setting_state, present_state,server_type from t_server_conf"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - TLOGDEBUG(__FUNCTION__ << " affected:" << res.size() << endl); - - for (unsigned i = 0; i < res.size(); i++) - { - vector server; - server.push_back(res[i]["application"] + "." + res[i]["server_name"] + "_" + res[i]["node_name"]); - server.push_back(res[i]["setting_state"]); - server.push_back(res[i]["present_state"]); - server.push_back(res[i]["server_type"]); - vServers.push_back(server); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - return vServers; - } - - return vServers; - -} -int DbProxy::getGroupId(const string& ip) -{ - bool bFind = false; - int iGroupId = -1; - string sOrder; - string sAllowIpRule; - string sDennyIpRule; - vector > vServerGroupInfo; - try - { - { - TC_ThreadLock::Lock lock(_mutex); - map::iterator it = _serverGroupCache.find(ip); - if (it != _serverGroupCache.end()) - { - return it->second; - } - vServerGroupInfo = _serverGroupRule; - } - - for (unsigned i = 0; i < vServerGroupInfo.size(); i++) - { - iGroupId = TC_Common::strto(vServerGroupInfo[i]["group_id"]); - sOrder = vServerGroupInfo[i]["ip_order"]; - sAllowIpRule = vServerGroupInfo[i]["allow_ip_rule"]; - sDennyIpRule = vServerGroupInfo[i]["denny_ip_rule"]; - vector vAllowIp = TC_Common::sepstr(sAllowIpRule, ",|;"); - vector vDennyIp = TC_Common::sepstr(sDennyIpRule, ",|;"); - if (sOrder == "allow_denny") - { - if (TC_Common::matchPeriod(ip, vAllowIp)) - { - bFind = true; - break; - } - } - else if (sOrder == "denny_allow") - { - if (TC_Common::matchPeriod(ip, vDennyIp)) - { - //在ä¸å…许的ip列表中则ä¸å±žäºŽæœ¬è¡Œæ‰€å¯¹åº”组 继续匹é…查找 - continue; - } - if (TC_Common::matchPeriod(ip, vAllowIp)) - { - bFind = true; - break; - } - } - } - - if (bFind == true) - { - TC_ThreadLock::Lock lock(_mutex); - _serverGroupCache[ip] = iGroupId; - - TLOGINFO("get groupId succ|ip|" << ip - << "|group_id|" << iGroupId << "|ip_order|" << sOrder - << "|allow_ip_rule|" << sAllowIpRule - << "|denny_ip_rule|" << sDennyIpRule - << "|ServerGroupCache|" << TC_Common::tostr(_serverGroupCache) << endl); - - return iGroupId; - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - catch (exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << ex.what() << endl); - } - return -1; -} - -NodePrx DbProxy::getNodePrx(const string& nodeName) -{ - try - { - TC_ThreadLock::Lock lock(_NodePrxLock); - - if (_mapNodePrxCache.find(nodeName) != _mapNodePrxCache.end()) - { - return _mapNodePrxCache[nodeName]; - } - - string sql = - "select node_obj " - "from t_node_info " - "where node_name='" + _mysqlReg.escapeString(nodeName) + "' and present_state='active'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - TLOGDEBUG(__FUNCTION__ << " '" << nodeName << "' affected:" << res.size() << endl); - - if (res.size() == 0) - { - throw TarsNodeNotRegistryException("node '" + nodeName + "' not registered or heartbeart timeout,please check for it"); - } - - NodePrx nodePrx; - g_app.getCommunicator()->stringToProxy(res[0]["node_obj"], nodePrx); - - _mapNodePrxCache[nodeName] = nodePrx; - - return nodePrx; - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << nodeName << " exception: " << ex.what() << endl); - throw TarsNodeNotRegistryException(string("get node record from db error:") + ex.what()); - } - catch (tars::TarsException& ex) - { - TLOGERROR(__FUNCTION__ << " " << nodeName << " exception: " << ex.what() << endl); - throw ex; - } - -} - -int DbProxy::checkRegistryTimeout(unsigned uTimeout) -{ - try - { - string sql = - "update t_registry_info " - "set present_state='inactive' " - "where last_heartbeat < date_sub(now(), INTERVAL " + tars::TC_Common::tostr(uTimeout) + " SECOND)"; - - _mysqlReg.execute(sql); - TLOGDEBUG(__FUNCTION__ << " (" << uTimeout << "s) affected:" << _mysqlReg.getAffectedRows() << endl); - - return _mysqlReg.getAffectedRows(); - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - return -1; - } - -} - -int DbProxy::updateRegistryInfo2Db(bool bRegHeartbeatOff) -{ - if (bRegHeartbeatOff) - { - TLOGDEBUG("updateRegistryInfo2Db not need to update reigstry status !" << endl); - return 0; - } - - map::iterator iter; - map mapServantEndpoint = g_app.getServantEndpoint(); - if (mapServantEndpoint.size() == 0) - { - TLOGERROR("fatal error, get registry servant failed!" << endl); - return -1; - } - - try - { - string sql = "replace into t_registry_info (locator_id, servant, endpoint, last_heartbeat, present_state, tars_version) " - "values "; - - string sVersion = TARS_VERSION; - sVersion += "_"; - sVersion += SERVER_VERSION; - - for (iter = mapServantEndpoint.begin(); iter != mapServantEndpoint.end(); iter++) - { - TC_Endpoint locator; - locator.parse(iter->second); - - sql += (iter == mapServantEndpoint.begin() ? string("") : string(", ")) + - "('" + locator.getHost() + ":" + TC_Common::tostr(locator.getPort()) + "', " - "'" + iter->first + "', '" + iter->second + "', now(), 'active', " + - "'" + _mysqlReg.escapeString(sVersion) + "')"; - } - - _mysqlReg.execute(sql); - TLOGDEBUG(__FUNCTION__ << " affected:" << _mysqlReg.getAffectedRows() << endl); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - return -1; - } - catch (exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -int DbProxy::loadIPPhysicalGroupInfo() -{ - try - { - string sql = "select group_id,ip_order,allow_ip_rule,denny_ip_rule,group_name from t_server_group_rule " - "order by group_id"; - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - TLOGDEBUG(__FUNCTION__ << " get server group from db, records affected:" << res.size() << endl); - - - TC_ThreadLock::Lock lock(_mutex); - _serverGroupRule.clear(); - _serverGroupRule = res.data(); - - _serverGroupCache.clear(); //è§„åˆ™æ”¹å˜ æ¸…é™¤ä»¥å‰ç¼“å­˜ - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - catch (exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << ex.what() << endl); - } - return -1; -} - -int DbProxy::getInfoByPatchId(const string &patchId, string &patchFile, string &md5) -{ - try - { - string sql = "select tgz, md5 from t_server_patchs where id=" + patchId; - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sql); - - if (res.size() == 0) - { - TLOGERROR(__FUNCTION__ << " get patch tgz, md5 from db error, no records! patchId=" << patchId << endl); - return -1; - } - - patchFile = res[0]["tgz"]; - md5 = res[0]["md5"]; - - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - catch (exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << ex.what() << endl); - } - return -1; -} - -int DbProxy::updatePatchByPatchId(const string &application, const string & serverName, const string & nodeName, const string & patchId, const string & user, const string &patchType, bool succ) -{ - try - { - string sql = "update t_server_patchs set publish='1',publish_user='" + user - + "',publish_time=now(),lastuser='"+ user +"' where id=" + patchId; - - _mysqlReg.execute(sql); - - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl); - } - catch (exception& ex) - { - TLOGERROR(__FUNCTION__ << " " << ex.what() << endl); - } - return -1; -} - diff --git a/cpp/framework/AdminRegistryServer/DbProxy.h b/cpp/framework/AdminRegistryServer/DbProxy.h deleted file mode 100644 index b4e0c8e03..000000000 --- a/cpp/framework/AdminRegistryServer/DbProxy.h +++ /dev/null @@ -1,289 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_PROXY_H__ -#define __DB_PROXY_H__ - -#include "util/tc_common.h" -#include "util/tc_config.h" -#include "util/tc_monitor.h" -#include "util/tc_mysql.h" -#include "util/tc_file.h" -#include "Node.h" -#include "servant/TarsLogger.h" -#include "AdminReg.h" - -using namespace tars; -/** - * 主控获å–nodeä¿¡æ¯å¼‚常 - */ -struct TarsNodeNotRegistryException : public TarsException -{ - TarsNodeNotRegistryException(const string &buffer) : TarsException(buffer){}; - TarsNodeNotRegistryException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsNodeNotRegistryException() throw(){}; -}; - -/** - * æ•°æ®åº“æ“作类 - */ -class DbProxy -{ -public: - /** - * 构造函数 - */ - DbProxy(){} - - /** - * åˆå§‹åŒ– - * @param pconf é…置文件 - * @return 0-æˆåŠŸ others-失败 - */ - int init(TC_Config *pconf); - - /** - * 获å–特定node idçš„å¯¹è±¡ä»£ç† - * @param nodeName : node id - * @return : 对象代ç†çš„智能指针 - */ - NodePrx getNodePrx(const string & nodeName); - - /** - * 增加异步任务 - * - * @param taskNo - * @param taskReq - * @param serial - * - * @return string - */ - int addTaskReq(const TaskReq &taskReq); - - /** - * èŽ·å– - * - * @param taskNo - * - * @return TaskRsp - */ - int getTaskRsp(const string &taskNo, TaskRsp &taskRsp); - - /** - * 获å–历å²çš„Taskä¿¡æ¯ - * - * @param application - * @param serverName - * @param command - * - * @return vector - */ - int getTaskHistory(const string & application, const string & serverName, const string & command, vector &taskRsp); - - /** - * 设置task itemä¿¡æ¯ - * - * @param itemNo - * @param startTime - * @param endTime - * @param status - * @param log - * @param current - * - * @return int - */ - int setTaskItemInfo(const string & itemNo, const map &info); - - /** - * 下线æœåŠ¡ - * - * @param application - * @param serverName - * @param nodeName - * - * @return int - */ - int undeploy(const string & application, const string & serverName, const string & nodeName, const string &user, string &log); - - /** - * 获å–活动node列表endpointä¿¡æ¯ - * @param out result 结果æè¿° - * @return map : 对应id nodeçš„obj - */ - map getActiveNodeList(string & result); - - /** - * 获å–node版本 - * @param name nodeå称 - * @param version node版本 - * @param out result 结果æè¿° - * @return 0-æˆåŠŸ others-失败 - */ - int getNodeVersion(const string &nodeName, string &version, string & result); - - /** - * 获å–在该node部署的server列表 - * - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * - * @return serverä¿¡æ¯åˆ—表 - */ - vector getServers(const string & app, const string & serverName, const string & nodeName,bool withDnsServer = false); - - /** - * æ ¹æ®åº”用和æœåŠ¡å,获å–部署的node列表 - */ - int getNodeList(const string & app, const string & serverName, vector& nodeNames); - - /** - * 获å–serverçš„é…ç½®æ¨¡æ¿ - * @param sTemplateName 模æ¿å称 - * @param sResultDesc 结果æè¿° - * @return 模æ¿å†…容 - */ - string getProfileTemplate(const string & sTemplateName, string & sResultDesc); - - /** - * æ ¹æ®patchId获å–patch tgz包 - * - * @param patchId - * - * @return string - */ - int getInfoByPatchId(const string &patchId, string &patchFile, string &md5); - - /** - * æ›´æ–°å‘布æˆåŠŸä¿¡æ¯ - * - * @param patchId - * - * @return int - */ - int updatePatchByPatchId(const string &application, const string & serverName, const string & nodeName, const string & patchId, const string & user, const string &patchType, bool succ); - -protected: - /** - * 获å–serverçš„é…ç½®æ¨¡æ¿ - * @param sTemplateName 模æ¿å称 - * @param mapRecursion è¢«é€’å½’åˆ°çš„æ¨¡æ¿ - * @param sResultDesc 结果æè¿° - * @return 模æ¿å†…容 - */ - string getProfileTemplate(const string & sTemplateName, map & mapRecursion, string & sResultDesc); - -public: - /** - * æ›´æ–°serverçŠ¶æ€ - * - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param stateFields: 更新状æ€å­—段å - * @param state : serverçŠ¶æ€ - * - * @return serverä¿¡æ¯åˆ—表 - */ - int updateServerState(const string & app, const string & serverName, const string & nodeName, - const string & stateFields, tars::ServerState state, int processId = -1); - - - /** - * 获å–application列表 - * @param null - * @param out reuslt - * @return application列表 - */ - vector getAllApplicationNames(string & result); - - - /** - * 获å–server列表 - * @param null - * @return node 列表 - */ - vector > getAllServerIds(string & result); - - - /** - * 设置serverå‘布版本 - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param version : server 版本 - * @param user : å‘布者 - */ - int setPatchInfo(const string & app, const string & serverName, const string & nodeName, - const string & version, const string & user); - - /** - * 自动伸缩时调用的ç°åº¦å‘å¸ƒæŽ¥å£ - * - * @param app: 应用 - * @param servername: server å - * @param nodename : node id - * @param status : æµé‡çŠ¶æ€,NORMAL-正常æµé‡,NO_FLOW-æ— æµé‡ - * - * @return : 0-æˆåŠŸ others-失败 - */ - int gridPatchServer(const string & app, const string & servername, const string & nodename, const string &status); - - /** - * 轮询数æ®åº“,将心跳超时的registry设为ä¸å­˜æ´» - * @param iTiemout 超时时间 - * @return - */ - int checkRegistryTimeout(unsigned uTimeout); - - /** - * æ›´æ–°registryä¿¡æ¯åˆ°db - */ - int updateRegistryInfo2Db(bool bRegHeartbeatOff); - - - /** - * æ ¹æ®ip获å–组id - * @return int <0 失败 其它正常 - */ - int getGroupId(const string& ip); - - /** - * 加载IP物ç†åˆ†ç»„ä¿¡æ¯ - */ - int loadIPPhysicalGroupInfo(); - -// int getServerInfo(const tars::srvRequestInfo & info,vector& vServerInfo); -protected: - //mysql连接对象 - tars::TC_Mysql _mysqlReg; - - //node节点代ç†åˆ—表 - static map _mapNodePrxCache; - static TC_ThreadLock _NodePrxLock; - - //匹é…åˆ†ç»„ä¿¡æ¯ - static vector > _serverGroupRule; - - //用于åˆå§‹åŒ–ä¿æŠ¤ - static TC_ThreadLock _mutex; - - //åˆ†ç»„ä¿¡æ¯ - static map _serverGroupCache; - static map _groupNameIDCache; -}; - -#endif diff --git a/cpp/framework/AdminRegistryServer/ExecuteTask.cpp b/cpp/framework/AdminRegistryServer/ExecuteTask.cpp deleted file mode 100644 index 52c8ad38d..000000000 --- a/cpp/framework/AdminRegistryServer/ExecuteTask.cpp +++ /dev/null @@ -1,626 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ExecuteTask.h" -#include "servant/Application.h" -#include "util/tc_timeprovider.h" - -extern TC_Config * g_pconf; - -TaskList::TaskList(const TaskReq &taskReq) -: _taskReq(taskReq) -{ - _adminPrx = CommunicatorFactory::getInstance()->getCommunicator()->stringToProxy(g_pconf->get("/tars/objname", "")); - - _taskRsp.taskNo = _taskReq.taskNo; - _taskRsp.serial = _taskReq.serial; - _taskRsp.userName = _taskReq.userName; - - for (size_t i=0; i < taskReq.taskItemReq.size(); i++) - { - TaskItemRsp rsp; - rsp.req = taskReq.taskItemReq[i]; - rsp.startTime = ""; - rsp.endTime = ""; - rsp.status = EM_I_NOT_START; - rsp.statusInfo= etos(rsp.status); - - _taskRsp.taskItemRsp.push_back(rsp); - } - - _createTime = TC_TimeProvider::getInstance()->getNow(); -} - - -TaskRsp TaskList::getTaskRsp() -{ - TC_LockT lock(*this); - - return _taskRsp; -} - - -void TaskList::setRspInfo(size_t index, bool start, EMTaskItemStatus status) -{ - TaskItemRsp rsp; - map info; - { - TC_LockT lock(*this); - - if (start) - { - _taskRsp.taskItemRsp[index].startTime = TC_Common::now2str("%Y-%m-%d %H:%M:%S"); - info["start_time"] = _taskRsp.taskItemRsp[index].startTime; - } - else - { - _taskRsp.taskItemRsp[index].endTime = TC_Common::now2str("%Y-%m-%d %H:%M:%S"); - info["end_time"] = _taskRsp.taskItemRsp[index].endTime; - } - - _taskRsp.taskItemRsp[index].status = status; - info["status"] = TC_Common::tostr(_taskRsp.taskItemRsp[index].status); - - _taskRsp.taskItemRsp[index].statusInfo = etos(status); - - rsp = _taskRsp.taskItemRsp[index]; - } - - try - { - _adminPrx->setTaskItemInfo(rsp.req.itemNo, info); - } - catch (exception &ex) - { -// log = ex.what(); - TLOGERROR("TaskList::setRspInfo error:" << ex.what() << endl); - } -} - -void TaskList::setRspLog(size_t index, const string &log) -{ - TaskItemRsp rsp; - map info; - { - TC_LockT lock(*this); - - _taskRsp.taskItemRsp[index].executeLog = log; - - rsp = _taskRsp.taskItemRsp[index]; - - info["log"] = _taskRsp.taskItemRsp[index].executeLog; - } - - try - { - _adminPrx->setTaskItemInfo(rsp.req.itemNo, info); - } - catch (exception &ex) - { -// log = ex.what(); - TLOGERROR("TaskList::setRspLog error:" << ex.what() << endl); - } -} - -EMTaskItemStatus TaskList::start(const TaskItemReq &req, string &log) -{ - int ret = -1; - try - { - ret = _adminPrx->startServer(req.application, req.serverName, req.nodeName, log); - if (ret == 0) - return EM_I_SUCCESS; - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::executeSingleTask startServer error:" << log << endl); - } - - return EM_I_FAILED; -} - -EMTaskItemStatus TaskList::restart(const TaskItemReq &req, string &log) -{ - int ret = -1; - try - { - ret = _adminPrx->restartServer(req.application, req.serverName, req.nodeName, log); - if (ret == 0) - return EM_I_SUCCESS; - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::restartServer error:" << log << endl); - } - - return EM_I_FAILED; -} - -EMTaskItemStatus TaskList::undeploy(const TaskItemReq &req, string &log) -{ - int ret = -1; - try - { - ret = _adminPrx->undeploy(req.application, req.serverName, req.nodeName, req.userName, log); - if (ret == 0) - return EM_I_SUCCESS; - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::undeploy error:" << log << endl); - } - - return EM_I_FAILED; -} - -EMTaskItemStatus TaskList::stop(const TaskItemReq &req, string &log) -{ - int ret = -1; - try - { - ret = _adminPrx->stopServer(req.application, req.serverName, req.nodeName, log); - if (ret == 0) - return EM_I_SUCCESS; - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::stop error:" << log << endl); - } - - return EM_I_FAILED; -} - -string TaskList::get(const string &name, const map ¶meters) -{ - map::const_iterator it = parameters.find(name); - if (it == parameters.end()) - { - return ""; - } - return it->second; -} - -EMTaskItemStatus TaskList::patch(const TaskItemReq &req, string &log) -{ - - try - { - - int ret = EM_TARS_UNKNOWN_ERR; - - TLOGDEBUG("TaskList::patch:" << TC_Common::tostr(req.parameters.begin(), req.parameters.end()) << endl); - - string patchId = get("patch_id", req.parameters); - string patchType = get("patch_type", req.parameters); - - tars::PatchRequest patchReq; - patchReq.appname = req.application; - patchReq.servername = req.serverName; - patchReq.nodename = req.nodeName; - patchReq.version = patchId; - patchReq.user = req.userName; - - try - { - ret = _adminPrx->batchPatch(patchReq, log); - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::patch batchPatch error:" << log << endl); - return EM_I_FAILED; - } - - if (ret != EM_TARS_SUCCESS) - { - log = "batchPatch err:" + log; - return EM_I_FAILED; - } - - while (true) - { - PatchInfo pi; - - try - { - ret = _adminPrx->getPatchPercent(req.application, req.serverName, req.nodeName, pi); - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::patch getPatchPercent error, ret:" << ret << endl); - } - - if (ret != 0) - { - _adminPrx->updatePatchLog(req.application, req.serverName, req.nodeName, patchId, req.userName, patchType, false); - TLOGERROR("TaskList::patch getPatchPercent error, ret:" << ret << endl); - return EM_I_FAILED; - } - - if(pi.iPercent == 100 && pi.bSucc) - { - _adminPrx->updatePatchLog(req.application, req.serverName, req.nodeName, patchId, req.userName, patchType, true); - TLOGDEBUG("TaskList::patch getPatchPercent ok, percent:" << pi.iPercent << "%" << endl); - break; - } - - TLOGDEBUG("TaskList::patch getPatchPercent percent:" << pi.iPercent << "%, succ:" << pi.bSucc << endl); - - sleep(1); - } - - } - catch (exception &ex) - { - TLOGERROR("TaskList::patch error:" << ex.what() << endl); - return EM_I_FAILED; - } - return EM_I_SUCCESS; -} - -EMTaskItemStatus TaskList::gridPatchServer(const TaskItemReq &req, string &log) -{ - int ret = -1; - try - { - TLOGDEBUG("TaskList::grid_server:" << TC_Common::tostr(req.parameters.begin(), req.parameters.end()) << endl); - - string status = get("grid_status", req.parameters); - - vector gridDescList; - - tars::ServerGridDesc serverGridDesc; - serverGridDesc.application = req.application; - serverGridDesc.servername = req.serverName; - serverGridDesc.nodename = req.nodeName; - - if(TC_Common::strto(status) == tars::NORMAL) - serverGridDesc.status = tars::NORMAL; - else if(TC_Common::strto(status) == tars::NO_FLOW) - serverGridDesc.status = tars::NO_FLOW; - else if(TC_Common::strto(status) == tars::GRID) - serverGridDesc.status = tars::GRID; - else - serverGridDesc.status = tars::NORMAL; - - gridDescList.push_back(serverGridDesc); - - vector gridFailDescList; - ret = _adminPrx->gridPatchServer(gridDescList, gridFailDescList,log); - if (ret == 0) - return EM_I_SUCCESS; - } - catch (exception &ex) - { - log = ex.what(); - TLOGERROR("TaskList::gridPatchServer error:" << log << endl); - } - - return EM_I_FAILED; -} -EMTaskItemStatus TaskList::executeSingleTask(size_t index, const TaskItemReq &req) -{ - TLOGDEBUG("TaskList::executeSingleTask: taskNo=" << req.taskNo - << ",application=" << req.application - << ",serverName=" << req.serverName - << ",nodeName=" << req.nodeName - << ",setName=" << req.setName - << ",command=" << req.command << endl); - - EMTaskItemStatus ret = EM_I_FAILED; - string log; - if (req.command == "stop") - { - ret = stop(req, log); - } - else if (req.command == "start") - { - ret = start(req, log); - } - else if (req.command == "restart") - { - ret = restart(req, log); - } - else if (req.command == "patch_tars") - { - ret = patch(req, log); - if (ret == EM_I_SUCCESS && get("bak_flag", req.parameters) != "1") - { - //ä¸æ˜¯å¤‡æœº, 需è¦é‡å¯ - ret = restart(req, log); - } - } - else if (req.command == "undeploy_tars") - { - ret = undeploy(req, log); - } - else if (req.command == "grid_server") - { - ret = gridPatchServer(req,log); - } - else - { - ret = EM_I_FAILED; - log = "command not support!"; - TLOGDEBUG("TaskList::executeSingleTask command not support!" << endl); - } - - setRspLog(index, log); - - return ret; -} - -////////////////////////////////////////////////////////////////////////////// -TaskListSerial::TaskListSerial(const TaskReq &taskReq) -: TaskList(taskReq) -{ - _pool.init(1); - _pool.start(); -} - -void TaskListSerial::execute() -{ - TLOGDEBUG("TaskListSerial::execute" << endl); - - auto cmd = std::bind(&TaskListSerial::doTask, this); - _pool.exec(cmd); -} - -void TaskListSerial::doTask() -{ - size_t len = 0; - { - TC_LockT lock(*this); - len = _taskReq.taskItemReq.size(); - } - - EMTaskItemStatus status = EM_I_SUCCESS; - - for (size_t i=0; i < len; i++) - { - TaskItemReq req; - - setRspInfo(i, true, EM_I_RUNNING); - { - TC_LockT lock(*this); - - req = _taskReq.taskItemReq[i]; - } - - if (EM_I_SUCCESS == status) - { - status = executeSingleTask(i, req); - } - else - { - //上一个任务ä¸æˆåŠŸ, åŽç»­çš„任务都cancel掉 - status = EM_I_CANCEL; - } - - setRspInfo(i, false, status); - } -} - -////////////////////////////////////////////////////////////////////////////// -TaskListParallel::TaskListParallel(const TaskReq &taskReq) -: TaskList(taskReq) -{ - //最大并行线程数 - size_t num = taskReq.taskItemReq.size() > 10 ? 10 : taskReq.taskItemReq.size(); - _pool.init(num); - _pool.start(); -} - -void TaskListParallel::execute() -{ - TLOGDEBUG("TaskListParallel::execute" << endl); - - TC_LockT lock(*this); - - for (size_t i=0; i < _taskReq.taskItemReq.size(); i++) - { - auto cmd = std::bind(&TaskListParallel::doTask, this, _taskReq.taskItemReq[i], i); - _pool.exec(cmd); - } -} - -void TaskListParallel::doTask(TaskItemReq req, size_t index) -{ - setRspInfo(index, true, EM_I_RUNNING); - - //do work - TLOGDEBUG("TaskListParallel::executeTask: taskNo=" << req.taskNo - << ",application=" << req.application - << ",serverName=" << req.serverName - << ",setName=" << req.setName - << ",command=" << req.command << endl); - - EMTaskItemStatus status = executeSingleTask(index, req); - - setRspInfo(index, false, status); -} - -///////////////////////////////////////////////////////////////////////////// - -ExecuteTask::ExecuteTask() -{ - _terminate = false; - start(); -} - -ExecuteTask::~ExecuteTask() -{ - terminate(); -} - -void ExecuteTask::terminate() -{ - _terminate = true; - TC_LockT lock(*this); - notifyAll(); -} - -void ExecuteTask::run() -{ - const time_t diff = 2*60;//2分钟 - while (!_terminate) - { - { - TC_ThreadLock::Lock lock(*this); - map::iterator it = _task.begin(); - while (it != _task.end()) - { - if(TC_TimeProvider::getInstance()->getNow() - it->second->getCreateTime() > diff) - { - TLOGDEBUG("==============ExecuteTask::run, delete old task, taskNo=" << it->first << endl); - TaskList *tmp = it->second; - _task.erase(it++); - delete tmp; - } - else - { - ++it; - } - } - } - - { - TC_LockT lock(*this); - timedWait(5*1000); - } - } -} - - -int ExecuteTask::addTaskReq(const TaskReq &taskReq) -{ - TLOGDEBUG("ExecuteTask::addTaskReq" << - ", taskNo=" << taskReq.taskNo << - ", size=" << taskReq.taskItemReq.size() << - ", serial=" << taskReq.serial << - ", userName="<< taskReq.userName << endl); - - TaskList *p = NULL; - if (taskReq.serial) - { - p = new TaskListSerial(taskReq); - } - else - { - p = new TaskListParallel(taskReq); - } - - { - TC_ThreadLock::Lock lock(*this); - - _task[taskReq.taskNo] = p; - - //删除历å²æ•°æ®, 当然最好是在定时独立åš, 放在这里主è¦æ˜¯é€”方便 - map::iterator it = _task.begin(); - while (it != _task.end()) - { - static time_t diff = 24 *60 *60;//1天 - //大于两天数æ®ä»»åŠ¡å°±å¹²æŽ‰ - if(TC_TimeProvider::getInstance()->getNow() - it->second->getCreateTime() > diff) - { - TaskList *temp = it->second; - - _task.erase(it++); - - delete temp; - temp = NULL; - } - else - { - ++it; - } - } - } - - p->execute(); - - return 0; -} - -bool ExecuteTask::getTaskRsp(const string &taskNo, TaskRsp &taskRsp) -{ - TLOGDEBUG("ExecuteTask::getTaskRsp, taskNo=" << taskNo << endl); - - TC_ThreadLock::Lock lock(*this); - - map::iterator it = _task.find(taskNo); - - if( it == _task.end()) - { - return false; - } - - taskRsp = (it->second)->getTaskRsp(); - - ExecuteTask::getInstance()->checkTaskRspStatus(taskRsp); - - return true; -} - - -void ExecuteTask::checkTaskRspStatus(TaskRsp &taskRsp) -{ - size_t not_start = 0; - size_t running = 0; - size_t success = 0; - size_t failed = 0; - size_t cancel = 0; - - for (unsigned i = 0; i < taskRsp.taskItemRsp.size(); i++) - { - TaskItemRsp &rsp = taskRsp.taskItemRsp[i]; - - switch (rsp.status) - { - case EM_I_NOT_START: - ++not_start; - break; - case EM_I_RUNNING: - ++running; - break; - case EM_I_SUCCESS: - ++success; - break; - case EM_I_FAILED: - ++failed; - break; - case EM_I_CANCEL: - ++cancel; - break; - } - } - - if (not_start == taskRsp.taskItemRsp.size()) taskRsp.status = EM_T_NOT_START; - else if (running == taskRsp.taskItemRsp.size()) taskRsp.status = EM_T_RUNNING; - else if (success == taskRsp.taskItemRsp.size()) taskRsp.status = EM_T_SUCCESS; - else if (failed == taskRsp.taskItemRsp.size()) taskRsp.status = EM_T_FAILED; - else if (cancel == taskRsp.taskItemRsp.size()) taskRsp.status = EM_T_CANCEL; - else taskRsp.status = EM_T_PARIAL; - -} - - - diff --git a/cpp/framework/AdminRegistryServer/ExecuteTask.h b/cpp/framework/AdminRegistryServer/ExecuteTask.h deleted file mode 100644 index 2a06c1015..000000000 --- a/cpp/framework/AdminRegistryServer/ExecuteTask.h +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __EXECUTE_TASK_H__ -#define __EXECUTE_TASK_H__ - -#include -#include "util/tc_thread_pool.h" -#include "util/tc_singleton.h" -#include "AdminReg.h" - -using namespace tars; - -class TaskList : public TC_ThreadMutex -{ -public: - /** - * 构造函数 - */ - TaskList(const TaskReq &taskReq); - - /** - * 得到返回任务 - */ - TaskRsp getTaskRsp(); - - - virtual void execute() = 0; - /** - * 创建时间 - */ - time_t getCreateTime() { return _createTime; } - -protected: - - //è®¾ç½®åº”ç­”ä¿¡æ¯ - void setRspInfo(size_t index, bool start, EMTaskItemStatus status); - - //设置应答的log - void setRspLog(size_t index, const string &log); - - EMTaskItemStatus executeSingleTask(size_t index, const TaskItemReq &req); - - EMTaskItemStatus start (const TaskItemReq &req, string &log); - EMTaskItemStatus restart (const TaskItemReq &req, string &log); - EMTaskItemStatus stop (const TaskItemReq &req, string &log); - EMTaskItemStatus patch (const TaskItemReq &req, string &log); - EMTaskItemStatus undeploy(const TaskItemReq &req, string &log); - EMTaskItemStatus gridPatchServer(const TaskItemReq &req, string &log); - string get(const string &name, const map ¶meters); - -protected: - - //线程池 - TC_ThreadPool _pool; - //请求任务 - TaskReq _taskReq; - //返回任务 - TaskRsp _taskRsp; - - AdminRegPrx _adminPrx; - - time_t _createTime; -}; - -class TaskListSerial : public TaskList -{ -public: - TaskListSerial(const TaskReq &taskReq); - - virtual void execute(); - -protected: - void doTask(); -}; - -class TaskListParallel : public TaskList -{ -public: - TaskListParallel(const TaskReq &taskReq); - - virtual void execute(); - -protected: - void doTask(TaskItemReq req, size_t index); -}; - -class ExecuteTask : public TC_Singleton, public TC_ThreadLock, public TC_Thread -{ -public: - - ExecuteTask(); - - ~ExecuteTask(); - - virtual void run(); - - void terminate(); - - /** - * 添加任务请求 - * - * @param taskList - * @param serial - * @param current - * - * @return string - */ - int addTaskReq(const TaskReq &taskReq); - - /** - * 获å–ä»»åŠ¡çŠ¶æ€ - * - * @param taskIdList : 任务列表id - * - * @return ä»»åŠ¡çŠ¶æ€ - */ - bool getTaskRsp(const string &taskNo, TaskRsp &taskRsp); - - /** - * 检查taskçš„çŠ¶æ€ - * - * @param taskRsp - */ - void checkTaskRspStatus(TaskRsp &taskRsp); - -protected: - - map _task; - - //线程结æŸæ ‡å¿— - bool _terminate; -}; - -#endif diff --git a/cpp/framework/AdminRegistryServer/util.h b/cpp/framework/AdminRegistryServer/util.h deleted file mode 100644 index cd7500e9e..000000000 --- a/cpp/framework/AdminRegistryServer/util.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __UTIL_H_ -#define __UTIL_H_ - -#include "util/tc_common.h" -#include "servant/tars_logger.h" - -#define FILE_FUN __FILE__<<":"<<__FUNCTION__<<":"<<__LINE__<<"|" -#define FILE_FUN_STR TC_Common::tostr(__FILE__)+":"+TC_Common::tostr(__FUNCTION__)+":"+TC_Common::tostr(__LINE__)+"|" - -#endif diff --git a/cpp/framework/AuthServer/AuthImp.cpp b/cpp/framework/AuthServer/AuthImp.cpp deleted file mode 100644 index 9bd71a04c..000000000 --- a/cpp/framework/AuthServer/AuthImp.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include "AuthImp.h" -#include "AuthServer.h" -#include "servant/TarsLogger.h" - -using namespace tars; - -std::string tableName = "t_auth_info"; - -AuthImp::AuthImp() : _mysql(NULL) -{ -} - -AuthImp::~AuthImp() -{ - delete _mysql; -} - -void AuthImp::initialize() -{ - assert (!_mysql); - _mysql = new TC_Mysql(); - - extern TC_Config * g_pconf; - - TC_DBConf dbConf; - dbConf.loadFromMap(g_pconf->getDomainMap("/tars/db")); - _mysql->init(dbConf); - - std::string createTbl = (*g_pconf)["/tars/"]; - _mysql->execute(createTbl); -} - -int AuthImp::authProcess(const AuthRequest& request, TarsCurrentPtr current) -{ - string sSql("select token from "); - sSql += tableName; - sSql += " where application ='"; - sSql += request.sKey.sApplication; - sSql += "' and server ='"; - sSql += request.sKey.sServer; - sSql += "' and objname ='"; - sSql += request.sKey.sObjName; - sSql += "';"; - - TC_Mysql::MysqlData result; - try { - result = _mysql->queryRecord(sSql); - } - catch (const TC_Mysql_Exception& e) { - TLOGDEBUG("exp : " << e.what() << endl); - return AUTH_ERROR; - } - - assert (result.size() <= 1); - if (result.size() == 0) - { - return AUTH_WRONG_OBJ; - } - else - { - string token = result[0]["token"]; - TLOGDEBUG("got token from mysql " << token << endl); - if (token != request.sToken) - { - TLOGERROR("wrong token " << request.sToken << endl); - return AUTH_WRONG_TOKEN; - } - } - - return AUTH_SUCC; -} - - -vector AuthImp::getTokens(const tars::TokenRequest& request,tars::TarsCurrentPtr current) -{ - vector rsp; - for (vector::const_iterator it(request.vObjName.begin()); it != request.vObjName.end(); ++ it) - { - string sSql; - sSql += "select application, server, token from " + tableName + " where objname = '"; - sSql += *it; - sSql += "';"; - - TC_Mysql::MysqlData result; - try { - result = _mysql->queryRecord(sSql); - } - catch (const TC_Mysql_Exception& e) { - TLOGDEBUG("queryRecord exp " << e.what() << endl); - continue; - } - - tars::TokenResponse tmp; - tmp.sObjName = *it; - for (size_t i = 0; i < result.size(); ++ i) - { - // app.server - std::string key = result[i]["application"] + "." + result[i]["server"]; - tmp.mTokens[key] = result[i]["token"]; - } - rsp.push_back(tmp); - } - - return rsp; -} - - -#if 0 - id | int(11) | NO | PRI | NULL | auto_increment | - | application | varchar(128) | YES | MUL | | | - | server | varchar(128) | YES | | NULL | | - | objname | varchar(128) | YES | | | | - | token | varchar(128) | NO | | | | -#endif -tars::ApplyTokenResponse AuthImp::applyToken(const tars::ApplyTokenRequest& request,tars::TarsCurrentPtr current) -{ - // gen secret key; - char token[32] = ""; - TC_Common::getRandomHexChars(token, 16); - std::string sToken(token); - - std::ostringstream sql; - sql << "insert into " + tableName + " (application, server, objname, token) values('"; - sql << request.sKey.sApplication << "','" << request.sKey.sServer << "','" << request.sKey.sObjName << "','"; - sql << sToken << "');"; - - try { - string sSql = sql.str(); - TLOGDEBUG("exec insert:" << sSql << endl); - _mysql->execute(sSql); - } - catch (const TC_Mysql_Exception& e) { - TLOGDEBUG("applySecret exp " << e.what() << endl); - sToken.clear(); - } - - tars::ApplyTokenResponse rsp; - rsp.sKey = request.sKey; - rsp.sToken = sToken; - return rsp; -} - -int AuthImp::deleteToken(const tars::DeleteTokenRequest& request,tars::TarsCurrentPtr current) -{ - //DELETE FROM tbl_name - // WHERE which rows to delete; - - std::ostringstream sql; - sql << "delete from " + tableName + " where application='"; - sql << request.sKey.sApplication << "' and server='" << request.sKey.sServer << "' and objname='" << request.sKey.sObjName << "';"; - - try { - string sSql = sql.str(); - TLOGDEBUG("exec delete :" << sSql << endl); - _mysql->execute(sSql); - } - catch (const TC_Mysql_Exception& e) { - TLOGDEBUG("delete exp " << e.what() << endl); - return -1; - } - - return 0; -} - diff --git a/cpp/framework/AuthServer/AuthImp.h b/cpp/framework/AuthServer/AuthImp.h deleted file mode 100644 index fe8ed36a7..000000000 --- a/cpp/framework/AuthServer/AuthImp.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef _AUTHIMP_H_ -#define _AUTHIMP_H_ - -#include "Auth.h" -#include "util/tc_common.h" -#include "util/tc_config.h" -#include "util/tc_mysql.h" - -class AuthImp : public tars::Auth -{ -public: - AuthImp(); - ~AuthImp(); - - /** - * ³õʼ»¯ - * - * @return int - */ - virtual void initialize(); - - /** - * Í˳ö - */ - virtual void destroy() {} - - /** - * process - */ - virtual int authProcess(const tars::AuthRequest & request,tars::TarsCurrentPtr current); - virtual vector getTokens(const tars::TokenRequest& request,tars::TarsCurrentPtr current); - virtual tars::ApplyTokenResponse applyToken(const tars::ApplyTokenRequest& request,tars::TarsCurrentPtr current); - virtual int deleteToken(const tars::DeleteTokenRequest& request,tars::TarsCurrentPtr current); - -private: - /** - * mysql - */ - TC_Mysql* _mysql; -}; - -#endif - diff --git a/cpp/framework/AuthServer/AuthServer.cpp b/cpp/framework/AuthServer/AuthServer.cpp deleted file mode 100644 index 0c797002b..000000000 --- a/cpp/framework/AuthServer/AuthServer.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "AuthServer.h" -#include "AuthImp.h" - -void AuthServer::initialize() -{ - //Ôö¼Ó¶ÔÏó - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".AuthObj"); -} - -void AuthServer::destroyApp() -{ - cout << "AuthServer::destroyApp ok" << endl; -} - diff --git a/cpp/framework/AuthServer/AuthServer.h b/cpp/framework/AuthServer/AuthServer.h deleted file mode 100644 index 40fb1c068..000000000 --- a/cpp/framework/AuthServer/AuthServer.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _AUTH_SERVER_H_ -#define _AUTH_SERVER_H_ - -#include "servant/Application.h" - -using namespace tars; - -class AuthServer : public Application -{ -protected: - /** - * ³õʼ»¯, Ö»»á½ø³Ìµ÷ÓÃÒ»´Î - */ - virtual void initialize(); - - /** - * Îö¹¹, ÿ¸ö½ø³Ì¶¼»áµ÷ÓÃÒ»´Î - */ - virtual void destroyApp(); -}; - -#endif - diff --git a/cpp/framework/AuthServer/CMakeLists.txt b/cpp/framework/AuthServer/CMakeLists.txt deleted file mode 100644 index 3f8721f73..000000000 --- a/cpp/framework/AuthServer/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ - -set(MODULE "tarsauth") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}) - -complice_module(${MODULE}) - diff --git a/cpp/framework/AuthServer/main.cpp b/cpp/framework/AuthServer/main.cpp deleted file mode 100644 index 02dbd99ac..000000000 --- a/cpp/framework/AuthServer/main.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include "AuthServer.h" - -using namespace tars; - -TC_Config * g_pconf; - -int main(int argc, char *argv[]) -{ - try - { - AuthServer app; - g_pconf = & app.getConfig(); - app.main(argc, argv); - - app.waitForShutdown(); - } - catch(exception &ex) - { - cerr<< ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/CMakeLists.txt b/cpp/framework/CMakeLists.txt deleted file mode 100644 index 1d1e9d50a..000000000 --- a/cpp/framework/CMakeLists.txt +++ /dev/null @@ -1,176 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -project(framework) - -include_directories(${util_SOURCE_DIR}/include) -include_directories(${servant_SOURCE_DIR}) -include_directories(${MYSQL_DIR_INC}) - -link_libraries(tarsservant tarsutil) -link_libraries(${MYSQL_DIR_LIB}/libmysqlclient.a) -link_libraries(pthread z dl rt) - -macro(complice_module MODULE) - - include_directories(${PROJECT_SOURCE_DIR}/protocol) - include_directories(${servant_SOURCE_DIR}/servant) - - aux_source_directory(. DIR_SRCS) - - add_executable(${MODULE} ${DIR_SRCS}) - add_dependencies(${MODULE} FRAMEWORK-PROTOCOL) - -endmacro() - -#调用tars2cpp, 生æˆtars对应的文件 - macro(complice_tars OUT_DEPENDS_LIST) - set(DEPENDS_LIST) - - set(SERVANT_PROTOCOL "${servant_SOURCE_DIR}/protocol") - - set(CLEAN_LIST) - - set(CURRENT_PROTOCOL_DIR ${PROJECT_SOURCE_DIR}/protocol) - - FILE(GLOB SRC_LIST "${CURRENT_PROTOCOL_DIR}/*.tars") - - foreach (FILE ${SRC_LIST}) - - #设置tars文件æœç´¢è·¯å¾„ - set(INCLUDE_STRING "--include=\"${SERVANT_PROTOCOL}\"") - - get_filename_component(NAME_WE ${FILE} NAME_WE) - - # 生æˆtars文件 - set(TARS_IN ${FILE}) - set(TARS_H ${NAME_WE}.h) - - add_custom_command( - OUTPUT ${CURRENT_PROTOCOL_DIR}/${TARS_H} - WORKING_DIRECTORY ${CURRENT_PROTOCOL_DIR} - DEPENDS tars2cpp - COMMAND ${TARS2CPP} --with-tars ${INCLUDE_STRING} ${TARS_IN} - COMMENT "${TARS2CPP} --with-tars ${INCLUDE_STRING} ${TARS_IN}") - - list(APPEND DEPENDS_LIST ${CURRENT_PROTOCOL_DIR}/${TARS_H}) - - #设置需è¦æ¸…除的文件 - list(APPEND CLEAN_LIST ${CURRENT_PROTOCOL_DIR}/${NAME_WE}.h) - - endforeach () - - set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") - - set(OUT_DEPENDS_LIST ${DEPENDS_LIST}) -endmacro() - -add_subdirectory(protocol) -add_subdirectory(ConfigServer) -add_subdirectory(NodeServer) -add_subdirectory(NotifyServer) -add_subdirectory(PatchServer) -add_subdirectory(AdminRegistryServer) -add_subdirectory(RegistryServer) -add_subdirectory(PropertyServer) -add_subdirectory(LogServer) -add_subdirectory(patchclient) -add_subdirectory(StatServer) -add_subdirectory(QueryStatServer) -add_subdirectory(QueryPropertyServer) -add_subdirectory(AuthServer) -############################################################################################ -# 打包deploy, 用于部署 - -set(DEPENDS_LIST) -list(APPEND DEPENDS_LIST tarsnode) -list(APPEND DEPENDS_LIST tarsregistry) -list(APPEND DEPENDS_LIST tarsAdminRegistry) -list(APPEND DEPENDS_LIST tarspatch) -list(APPEND DEPENDS_LIST tarsconfig) - -#set(FRAMEWORK-TGZ "${CMAKE_BINARY_DIR}/framework-tmp.tgz") -set(FRAMEWORK-TGZ "${CMAKE_BINARY_DIR}/framework.tgz") - -#需è¦åŽ‹ç¼©çš„文件 -set(MODULES "tars_install.sh") -set(MODULES "${MODULES} tarsnode_install.sh") - -foreach (MODULE ${DEPENDS_LIST}) - set(MODULES "${MODULES} ${MODULE}") -endforeach () - -#打包脚本 -SET(RUN_DEPLOY_COMMAND_FILE "${PROJECT_BINARY_DIR}/run-deploy-framework.cmake") -FILE(WRITE ${RUN_DEPLOY_COMMAND_FILE} "#deploy framework-tar\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/deploy/\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND cp -rf ${PROJECT_SOURCE_DIR}/deploy/tars_install.sh .\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND cp -rf ${PROJECT_SOURCE_DIR}/deploy/tarsnode_install.sh .\n") -foreach (MODULE ${DEPENDS_LIST}) - FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND cp -rf ${PROJECT_SOURCE_DIR}/deploy/${MODULE} .\n") -endforeach () -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} ")\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/deploy/\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND tar czfv ${FRAMEWORK-TGZ} ${MODULES}\n") -#FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND mv ${FRAMEWORK-TGZ} ${CMAKE_BINARY_DIR}/framework.tgz\n") -FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} ")\n") - -#执行命令 -add_custom_command(OUTPUT ${FRAMEWORK-TGZ} - COMMAND ${CMAKE_COMMAND} -P ${RUN_DEPLOY_COMMAND_FILE} - COMMENT "call ${RUN_DEPLOY_COMMAND_FILE}") - -add_custom_target(framework-tar - DEPENDS ${FRAMEWORK-TGZ} ${DEPENDS_LIST}) - -############################################################################################ -# 打包tarsnotify tarsstat tarsproperty tarslog tarsauth -set(TARSQUERYSTAT tarsquerystat) -set(TARSQUERYPROPERTY tarsqueryproperty) - -set(DEPENDS_LIST) -list(APPEND DEPENDS_LIST tarsnotify) -list(APPEND DEPENDS_LIST tarsstat) -list(APPEND DEPENDS_LIST tarsproperty) -list(APPEND DEPENDS_LIST tarslog) -list(APPEND DEPENDS_LIST tarsquerystat) -list(APPEND DEPENDS_LIST tarsqueryproperty) -list(APPEND DEPENDS_LIST tarsauth) - - -foreach (MODULE ${DEPENDS_LIST}) - - #set(MODULE-TGZ "${CMAKE_BINARY_DIR}/${MODULE}-tmp.tgz") - set(MODULE-TGZ "${CMAKE_BINARY_DIR}/${MODULE}.tgz") - - #打包脚本 - SET(RUN_DEPLOY_COMMAND_FILE "${PROJECT_BINARY_DIR}/run-deploy-${MODULE}.cmake") - FILE(WRITE ${RUN_DEPLOY_COMMAND_FILE} "#deploy ${MODULE}-tar\n") - FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(\n") - FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/deploy/\n") - FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND tar czfv ${MODULE-TGZ} ${MODULE}\n") - #FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "COMMAND mv ${MODULE-TGZ} ${CMAKE_BINARY_DIR}/${MODULE}.tgz\n") - FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} ")\n") - - add_custom_command(OUTPUT ${MODULE-TGZ} - WORKING_DIRECTORY ${PROJECT_BINARY_DIR} - COMMAND ${CMAKE_COMMAND} -P ${RUN_DEPLOY_COMMAND_FILE} - COMMENT "call ${RUN_DEPLOY_COMMAND_FILE}") - - add_custom_target(${MODULE}-tar DEPENDS ${MODULE-TGZ} ${MODULE}) -endforeach () - -############################################################################################ - - - - - - - - - - - - diff --git a/cpp/framework/ConfigServer/CMakeLists.txt b/cpp/framework/ConfigServer/CMakeLists.txt deleted file mode 100644 index 49846b4f6..000000000 --- a/cpp/framework/ConfigServer/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ - -set(MODULE "tarsconfig") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/bin) - -complice_module(${MODULE}) - - diff --git a/cpp/framework/ConfigServer/ConfigImp.cpp b/cpp/framework/ConfigServer/ConfigImp.cpp deleted file mode 100644 index 749ebd258..000000000 --- a/cpp/framework/ConfigServer/ConfigImp.cpp +++ /dev/null @@ -1,970 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ConfigImp.h" -#include "ConfigServer.h" - -extern TC_Config * g_pconf; - -#define CHECKLIMIT(a,b,c,f) do{\ - if(IsLimited((a),(b),(c),(f)))\ - {\ - return 0;\ - }}while(0) - -using namespace tars; - -map > ConfigImp::_loadConfigLimited; - -void ConfigImp::loadconf() -{ - TC_DBConf tcDBConf; - tcDBConf.loadFromMap(g_pconf->getDomainMap("/tars/db")); - _mysqlConfig.init(tcDBConf); - - _limitInterval = TC_Common::strto(g_pconf->get("/tars/limit", "10")); - _limitInterval = _limitInterval <= 0 ? 10 : _limitInterval; - _interval = TC_Common::strto(g_pconf->get("/tars/limit", "1")); - _interval = _interval <= 0 ? 1 : _interval; -} - -void ConfigImp::initialize() -{ - loadconf(); -} - -int ConfigImp::ListConfig(const string &app, const string &server, vector &vf,tars::TarsCurrentPtr current) -{ - try - { - string host = current->getIp(); - - TLOGDEBUG("ConfigImp::ListConfig app:" << app << "|server:" << server << "|host:" << host << endl); - - //查ip对应é…ç½® - string sNULL(""); - string sSql = - "select filename from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(app+"."+server) + "' " - "and host='" + _mysqlConfig.escapeString(host) +"' " - "and level=" + TC_Common::tostr(eLevelIpServer) + " " - "and set_name ='" + _mysqlConfig.escapeString(sNULL) +"' " - "and set_area ='" + _mysqlConfig.escapeString(sNULL) +"' " - "and set_group ='" + _mysqlConfig.escapeString(sNULL) +"' "; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::ListConfig sql:" << sSql << "|res.size:" << res.size() << endl); - - for(unsigned i=0; i vtSetDivisions = TC_Common::sepstr(sSetDivision, "."); - if(vtSetDivisions.size() != 3) - { - return false; - } - - sSetName = vtSetDivisions[0]; - sSetArea = vtSetDivisions[1]; - sSetGroup = vtSetDivisions[2]; - - return true; -} - -int ConfigImp::ListConfigByInfo(const ConfigInfo& configInfo, vector &vf,tars::TarsCurrentPtr current) -{ - try - { - string sHost = configInfo.host.empty() ? current->getIp() : configInfo.host; - - TLOGDEBUG("ConfigImp::ListConfigByInfo app:" << configInfo.appname << "|server:" << configInfo.servername << "|set:" << configInfo.setdivision << "|host:" << sHost << endl); - - string sCondition; - if(!configInfo.setdivision.empty()) - { - string sSetName,sSetArea,sSetGroup; - if(getSetInfo(sSetName,sSetArea,sSetGroup,configInfo.setdivision)) - { - sCondition += " and set_name='" +_mysqlConfig.escapeString(sSetName)+"' "; - sCondition += " and set_area='" +_mysqlConfig.escapeString(sSetArea)+"' "; - sCondition += " and set_group='"+_mysqlConfig.escapeString(sSetGroup)+"' "; - } - else - { - TLOGERROR("ConfigImp::ListConfigByInfo setdivision is invalid|setdivision:" << configInfo.setdivision << endl); - return -1; - } - } - else//兼容没有setä¿¡æ¯çš„业务 - { - string sNULL(""); - sCondition += " and set_name='" + _mysqlConfig.escapeString(sNULL) +"' "; - sCondition += " and set_area='" + _mysqlConfig.escapeString(sNULL) +"' "; - sCondition += " and set_group='" + _mysqlConfig.escapeString(sNULL) +"' "; - } - - //查ip对应é…ç½® - string sSql = - "select filename from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(configInfo.appname + "." + configInfo.servername) + "' " - "and host='" + _mysqlConfig.escapeString(sHost) + "' " - "and level=" + TC_Common::tostr(eLevelIpServer) + sCondition; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::ListConfigByInfo sql:" << sSql << "|res.size:" << res.size() << endl); - - for(unsigned i=0; igetIp(),configInfo.filename); - - if(configInfo.bAppOnly || configInfo.servername.empty())//应用级é…置或者set级é…ç½® - { - iRet = loadAppConfigByInfo(configInfo,config,current); - } - else - { - iRet = loadConfigByHost(configInfo,config,current); - } - - return iRet; -} - -int ConfigImp::checkConfigByInfo(const ConfigInfo & configInfo, string &result,tars::TarsCurrentPtr current) -{ - TLOGDEBUG("ConfigImp::checkConfigByInfo app:" << configInfo.appname << "|server:" << configInfo.servername << "|filename:" << configInfo.filename << "|setdivsion:" << configInfo.setdivision << endl); - - int iRet = loadConfigByHost(configInfo,result,current); - if (iRet != 0) - { - TLOGERROR("ConfigImp::checkConfigByInfo loadConfigByHost fail." << endl); - return -1; - } - - try - { - TC_Config conf; - conf.parseString(result); - } - catch(exception &ex) - { - result = ex.what(); - TLOGERROR("ConfigImp::checkConfigByInfo exception:" << ex.what() << endl); - return -1; - } - - return 0; -} - -int ConfigImp::loadConfig(const std::string& app, const std::string& server, const std::string& fileName, string &config, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("ConfigImp::loadConfig app:" << app << "|server:" << server << "|fileName:" << fileName << "|host:" << current->getIp() << endl); - - CHECKLIMIT(app,server,current->getIp(),fileName); - - if(!server.empty()) - { - return loadConfigByHost(app + "." + server, fileName, current->getIp(), config, current); - } - else - { - return loadAppConfig(app, fileName, config, current); - } -} - -int ConfigImp::loadConfigByHost(const std::string& appServerName, const std::string& fileName, const string &host, string &config, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("ConfigImp::loadConfigByHost appServerName:" << appServerName << "|fileName:" << fileName << "|host:" << host << endl); - - int iRet = 0; - config = ""; - - //多é…置文件的分割符 - string sSep = "\r\n\r\n"; - try - { - string sAllServerConfig(""); - int iAllConfigId = 0; - - //查公有é…ç½® - string sNULL(""); - string sSql = - "select id,config from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(appServerName) + "' " - "and filename='" + _mysqlConfig.escapeString(fileName) + "' " - "and level=" + TC_Common::tostr(eLevelAllServer) + " " - "and set_name ='" + _mysqlConfig.escapeString(sNULL) + "' " - "and set_area ='" + _mysqlConfig.escapeString(sNULL) + "' " - "and set_group ='" + _mysqlConfig.escapeString(sNULL) + "' "; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadConfigByHost sql:" << sSql << "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - sAllServerConfig = res[0]["config"]; - iAllConfigId = TC_Common::strto(res[0]["id"]); - } - - vector referenceIds; - - iRet = getReferenceIds(iAllConfigId,referenceIds); - - TLOGDEBUG("ConfigImp::loadConfigByHost referenceIds.size:" << referenceIds.size() << endl); - - if(iRet == 0) - { - for(size_t i = 0; i < referenceIds.size();i++) - { - int iRefId = referenceIds[i]; - string refConfig(""); - - iRet = loadConfigByPK(iRefId,refConfig); - if(iRet == 0 && (!refConfig.empty())) - { - if(config.empty()) - { - config += refConfig; - } - else - { - config += sSep + refConfig; - } - } - } - } - - //添加é…置本身 - if(!sAllServerConfig.empty()) - { - if(config.empty()) - { - config += sAllServerConfig; - } - else - { - config += sSep + sAllServerConfig; - } - } - - - string sIpServerConfig = ""; - int iIpConfigId = 0; - - sSql = - "select id,config from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(appServerName) + "' " - "and filename='" + _mysqlConfig.escapeString(fileName) + "' " - "and host='" + _mysqlConfig.escapeString(host) + "' " - "and level=" + TC_Common::tostr(eLevelIpServer) + " " - "and set_name ='" + _mysqlConfig.escapeString(sNULL) + "' " - "and set_area ='" + _mysqlConfig.escapeString(sNULL) + "' " - "and set_group ='" + _mysqlConfig.escapeString(sNULL) + "' "; - - res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadConfigByHost sql:" << sSql << "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - sIpServerConfig = res[0]["config"]; - iIpConfigId = TC_Common::strto(res[0]["id"]); - } - - iRet = getReferenceIds(iIpConfigId,referenceIds); - - TLOGDEBUG("ConfigImp::loadConfigByHost referenceIds.size:" << referenceIds.size() << endl); - - if(iRet == 0) - { - for(size_t i = 0; i < referenceIds.size(); i++) - { - int iRefId = referenceIds[i]; - string refConfig(""); - - iRet = loadConfigByPK(iRefId,refConfig); - if(iRet == 0 && (!refConfig.empty()) ) - { - if(config.empty()) - { - config += refConfig; - } - else - { - config += sSep + refConfig; - } - } - } - } - - //添加é…置本身 - if(!sIpServerConfig.empty()) - { - if(config.empty()) - { - config += sIpServerConfig; - } - else - { - config += sSep + sIpServerConfig; - } - } - } - catch(TC_Mysql_Exception & ex) - { - config = ex.what(); - TLOGERROR("ConfigImp::loadConfigByHost exception:" << ex.what() << endl); - iRet = -1; - } - catch(...) - { - TLOGERROR("ConfigImp::loadConfigByHost unknown exception." << endl); - iRet = -1; - } - - return iRet; -} - -int ConfigImp::checkConfig(const std::string& appServerName, const std::string& fileName, const string &host, string &result, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("ConfigImp::checkConfig appServerName:" << appServerName << "|fileName:" << fileName << "|host:" << host << endl); - - int ret = loadConfigByHost(appServerName, fileName, host, result, current); - if (ret != 0) - { - TLOGDEBUG("ConfigImp::checkConfig loadConfigByHost fail." << endl); - return -1; - } - - try - { - TC_Config conf; - conf.parseString(result); - } - catch(exception &ex) - { - TLOGDEBUG("ConfigImp::checkConfig exception:" << ex.what() << endl); - result = ex.what(); - return -1; - } - - return 0; -} - -///////////////////////////////////////////////private -int ConfigImp::loadConfigByPK(int iConfigId, string &sConfig) -{ - int iRet = 0 ; - //按照建立引用的先åŽè¿”回 - try - { - string sSql = - "select config from t_config_files where id=" + TC_Common::tostr(iConfigId) +" order by id"; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadConfigByPK sql:" << sSql << "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - sConfig = res[0]["config"]; - } - } - catch(TC_Mysql_Exception & ex) - { - sConfig = ex.what(); - TLOGERROR("ConfigImp::loadConfigByPK exception:" << ex.what() << endl); - return -1; - } - catch(...) - { - TLOGERROR("ConfigImp::loadConfigByPK unknown exception." << endl); - return -1; - } - - return iRet; -} - -int ConfigImp::loadRefConfigByPK(int iConfigId, const string& setdivision,string &sConfig) -{ - int iRet = 0 ; - //按照建立引用的先åŽè¿”回 - try - { - string sSql = - "select server_name,filename,config from t_config_files where id=" + TC_Common::tostr(iConfigId) +" order by id"; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadRefConfigByPK sql:" << sSql << "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - string sAppConfig = res[0]["config"]; - string sFileName = res[0]["filename"]; - string sServerName = res[0]["server_name"]; - - string sSetName(""); - string sSetArea(""); - string sSetGroup(""); - string sSetConfig(""); - - //查询该é…置是å¦æœ‰seté…ç½®ä¿¡æ¯ - if(getSetInfo(sSetName,sSetArea,sSetGroup,setdivision)) - { - string sCondition; - sCondition += " and set_name='" +_mysqlConfig.escapeString(sSetName)+"' "; - sCondition += " and set_area='" +_mysqlConfig.escapeString(sSetArea)+"' "; - sCondition += " and set_group='" +_mysqlConfig.escapeString(sSetGroup)+"' "; - - string sSql = - "select config from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(sServerName) + "' " - "and filename='" + _mysqlConfig.escapeString(sFileName) + "' " - "and level=" + TC_Common::tostr(eLevelApp) + sCondition; - - TC_Mysql::MysqlData resSet = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadRefConfigByPK sql:" << sSql << "|resSet.size:" << resSet.size() << endl); - - if(resSet.size() == 1) - { - sSetConfig = resSet[0]["config"]; - } - } - - sConfig = mergeConfig(sAppConfig,sSetConfig); - } - } - catch(TC_Mysql_Exception & ex) - { - sConfig = ex.what(); - TLOGERROR("ConfigImp::loadRefConfigByPK exception: "< &referenceIds) -{ - int iRet = 0 ; - - referenceIds.clear(); - - string sSql = - "select reference_id from t_config_references where config_id=" + TC_Common::tostr(iConfigId); - - TLOGDEBUG("ConfigImp::getReferenceIds sql:" << sSql << endl); - - try - { - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::getReferenceIds res.size:" << res.size() << endl); - - for(size_t i = 0 ; i < res.size(); i++) - { - int iRefId = TC_Common::strto(res[i]["reference_id"]); - referenceIds.push_back(iRefId); - } - } - catch(TC_Mysql_Exception & ex) - { - TLOGERROR("ConfigImp::getReferenceIds exception:" << ex.what() << endl); - return -1; - } - catch(...) - { - TLOGERROR("ConfigImp::getReferenceIds unknown exception" << endl); - return -1; - } - - return iRet; -} - - -int ConfigImp::loadAppConfig(const std::string& appName, const std::string& fileName, string &config, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("ConfigImp::loadAppConfig appName:" << appName << "|fileName:" << fileName << endl); - - int iRet = 0 ; - config = ""; - - try - { - string sNULL; - //查公有é…ç½® - string sSql = - "select id,config from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(appName) + "' " - "and filename='" + _mysqlConfig.escapeString(fileName) + "' " - "and level=" + TC_Common::tostr(eLevelApp) + " " - "and set_name ='" + _mysqlConfig.escapeString(sNULL) + "' " - "and set_area ='" + _mysqlConfig.escapeString(sNULL) + "' " - "and set_group ='" + _mysqlConfig.escapeString(sNULL) + "' "; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadAppConfig sql:" << sSql << "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - config = res[0]["config"]; - } - } - catch(TC_Mysql_Exception & ex) - { - config = ex.what(); - TLOGERROR("ConfigImp::loadAppConfig exception:" << ex.what() << endl); - iRet = -1; - } - catch(...) - { - TLOGERROR("ConfigImp::loadAppConfig unknown exception." << endl); - iRet = -1; - } - - return iRet; - -} - -int ConfigImp::loadConfigByHost(const ConfigInfo & configInfo, string &config, tars::TarsCurrentPtr current) -{ - string sHost = configInfo.host.empty() ? current->getIp() : configInfo.host; - - TLOGDEBUG("ConfigImp::loadConfigByHost app:" << configInfo.appname << "|server:" << configInfo.servername << "|filename:" << configInfo.filename - << "|host:" << sHost << "|setdivision:" <(res[0]["id"]); - } - - //查æœåŠ¡é…置的引用é…ç½® - vector referenceIds; - iRet = getReferenceIds(iAllConfigId,referenceIds); - - TLOGDEBUG("ConfigImp::loadConfigByHost referenceIds.size:" << referenceIds.size() << endl); - - if(iRet == 0) - { - for(size_t i = 0; i < referenceIds.size();i++) - { - int iRefId = referenceIds[i]; - string refConfig(""); - - //此处获å–çš„æœåŠ¡é…置的引用é…ç½®ä¿¡æ¯ - iRet = loadRefConfigByPK(iRefId,configInfo.setdivision,refConfig); - if(iRet == 0 && (!refConfig.empty())) - { - sTemp = mergeConfig(sTemp,refConfig); - } - } - } - - //添加æœåŠ¡é…置本身 - config = mergeConfig(sTemp,sAllServerConfig); - - sTemp.clear(); - - string sIpServerConfig(""); - int iIpConfigId = 0; - - //查看节点级é…ç½® - sSql = - "select id,config from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(configInfo.appname + "." + configInfo.servername) + "' " - "and filename='" + _mysqlConfig.escapeString(configInfo.filename) + "' " - "and host='" + _mysqlConfig.escapeString(sHost) + "' " - "and level=" + TC_Common::tostr(eLevelIpServer); - - res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::loadConfigByHost sql:" << sSql<< "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - sIpServerConfig = res[0]["config"]; - iIpConfigId = TC_Common::strto(res[0]["id"]); - } - - iRet = getReferenceIds(iIpConfigId, referenceIds); - - TLOGDEBUG("ConfigImp::loadConfigByHost referenceIds.size:" << referenceIds.size() << endl); - - if(iRet == 0) - { - string refAppConfig(""); - string refSetConfig(""); - for(size_t i = 0; i < referenceIds.size();i++) - { - int iRefId = referenceIds[i]; - string refConfig(""); - - //此处获å–的节点é…置的引用é…ç½®ä¿¡æ¯ - iRet = loadRefConfigByPK(iRefId,configInfo.setdivision,refConfig); - if(iRet == 0 && (!refConfig.empty())) - { - sTemp = mergeConfig(sTemp,refConfig); - } - } - } - - config = mergeConfig(config,mergeConfig(sTemp, sIpServerConfig)); - - } - catch(TC_Mysql_Exception & ex) - { - config = ex.what(); - TLOGERROR("ConfigImp::loadConfigByHost exception:" << ex.what() << endl); - iRet = -1; - } - catch(...) - { - TLOGERROR("ConfigImp::loadConfigByHost unknown exception" << endl); - iRet = -1; - } - - return iRet; -} - -int ConfigImp::loadAppConfigByInfo(const ConfigInfo & configInfo, string &config, tars::TarsCurrentPtr current) -{ - int iRet = 0; - config=""; - try - { - string sSql = - "select id,config from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(configInfo.appname) + "' " - "and filename='" + _mysqlConfig.escapeString(configInfo.filename) + "' " - "and level=" + TC_Common::tostr(eLevelApp); - - string sNULL(""); - string sCondition(""); - sCondition += " and set_name='" + _mysqlConfig.escapeString(sNULL) +"' "; - sCondition += " and set_area='" + _mysqlConfig.escapeString(sNULL) +"' "; - sCondition += " and set_group='" + _mysqlConfig.escapeString(sNULL) +"' "; - - //先获å–appé…ç½® - string sSqlAppNoSet = sSql + sCondition; - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSqlAppNoSet); - - TLOGDEBUG("ConfigImp::loadAppConfigByInfo sSqlAppNoSet:" << sSqlAppNoSet << "|res.size:" << res.size() << endl); - - string sAppConfig(""); - - if(res.size() == 1) - { - sAppConfig = res[0]["config"]; - } - - //å†èŽ·å–app下有setä¿¡æ¯çš„é…ç½® - string sSetConfig; - if(!configInfo.setdivision.empty())//存在setä¿¡æ¯ - { - string sSetName,sSetArea,sSetGroup; - - if(getSetInfo(sSetName,sSetArea,sSetGroup,configInfo.setdivision)) - { - string sCondition; - sCondition += " and set_name='" +_mysqlConfig.escapeString(sSetName)+"' "; - sCondition += " and set_area='" +_mysqlConfig.escapeString(sSetArea)+"' "; - sCondition += " and set_group='" +_mysqlConfig.escapeString(sSetGroup)+"' "; - - string sSqlSet = sSql + sCondition; - res = _mysqlConfig.queryRecord(sSqlSet); - - TLOGDEBUG("ConfigImp::loadAppConfigByInfo sSqlSet:" << sSqlSet << "|res.size:" << res.size() << endl); - - if(res.size() == 1) - { - sSetConfig = res[0]["config"]; - } - } - else - { - TLOGERROR("ConfigImp::loadAppConfigByInfo setdivision is invalid|configInfo.setdivision:" << configInfo.setdivision << endl); - return -1; - } - } - - config = mergeConfig(sAppConfig,sSetConfig); - - } - catch(TC_Mysql_Exception & ex) - { - config = ex.what(); - TLOGERROR("ConfigImp::loadAppConfigByInfo exception:" << ex.what() << endl); - iRet = -1; - } - catch(...) - { - TLOGERROR("ConfigImp::loadAppConfigByInfo unknown exception" << endl); - iRet = -1; - } - - return iRet; -} - -string ConfigImp::mergeConfig(const string& sLowConf,const string& sHighConf) -{ - - if(sLowConf.empty()) - { - return sHighConf; - } - else if(sHighConf.empty()) - { - return sLowConf; - } - else - { - return sLowConf + "\r\n\r\n" + sHighConf; - } -} - -//频率é™åˆ¶ -bool ConfigImp::IsLimited(const std::string & app, const std::string & server, const std::string & sIp,const string& sFile) -{ - const time_t INTERVAL = 1 * 60; - bool bLimited = false; - - { - ServerKey sKey = {app, server, sIp,sFile}; - - static TC_ThreadLock s_mutex; - - TC_ThreadLock::Lock lock(s_mutex); - - map >::iterator it = _loadConfigLimited.find(sKey); - if(it != _loadConfigLimited.end()) - { - ///1分钟内最多å…许拉å–10次 - if((TNOW - it->second.first) <= (INTERVAL*_interval)) - { - if(it->second.second <= _limitInterval) - { - it->second.second++; - } - else - { - bLimited = true; - TLOGERROR("ConfigImp::IsLimited app:" << app << "|server:" << server << "|sIp:" << sIp << "|sFile:" << sFile - << "|out of limit,now:" << it->second.second << "|limitInterval:" << _limitInterval << endl); - } - } - //å·²ç»è¶…过1分钟了 - else - { - _loadConfigLimited.erase(sKey); - } - } - else - { - _loadConfigLimited.insert(std::make_pair(sKey,std::make_pair(TNOW,1))); - } - } - - return bLimited; -} - -int ConfigImp::ListAllConfigByInfo(const tars::GetConfigListInfo & configInfo, vector &vf, tars::TarsCurrentPtr current) -{ - CHECKLIMIT(configInfo.appname,configInfo.servername,current->getIp(),""); - - try - { - if(configInfo.bAppOnly) - { - //查ip对应é…ç½® - string sSql = "select distinct filename from t_config_files " - "where server_name = '" + _mysqlConfig.escapeString(configInfo.appname) + "' " - "and level=" + TC_Common::tostr(eLevelApp); - - TLOGDEBUG("ConfigImp::ListAllConfigByInfo sql:" << sSql << endl); - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::ListAllConfigByInfo sql:" << sSql << "|res:" << res.size() << endl); - - for(unsigned i=0; i(eLevelIpServer); - } - - if(!configInfo.setdivision.empty()) - { - string sSetName,sSetArea,sSetGroup; - if(getSetInfo(sSetName,sSetArea,sSetGroup,configInfo.setdivision)) - { - sCondition += " and set_name='" +_mysqlConfig.escapeString(sSetName)+"' "; - sCondition += " and set_area='" +_mysqlConfig.escapeString(sSetArea)+"' "; - sCondition += " and set_group='" +_mysqlConfig.escapeString(sSetGroup)+"' "; - } - else - { - TLOGERROR("ConfigImp::ListAllConfigByInfo setdivision is invalid:" << configInfo.setdivision << endl); - return -1; - } - } - else//兼容没有setä¿¡æ¯çš„业务 - { - string sNULL; - sCondition += " and set_name='" + _mysqlConfig.escapeString(sNULL) +"' "; - sCondition += " and set_area='" + _mysqlConfig.escapeString(sNULL) +"' "; - sCondition += " and set_group='" + _mysqlConfig.escapeString(sNULL) +"' "; - } - - sSql += sCondition; - - TLOGDEBUG("ConfigImp::ListAllConfigByInfo sql:" << sSql << endl); - - TC_Mysql::MysqlData res = _mysqlConfig.queryRecord(sSql); - - TLOGDEBUG("ConfigImp::ListAllConfigByInfo sql:" << sSql << "|res:" << res.size() << endl); - - for(unsigned i=0; i &vf,tars::TarsCurrentPtr current); - - /** - * 加载é…置文件 - * param app :应用 - * param server: serverå - * param filename: é…置文件å - * - * return : é…置文件内容 - */ - virtual int loadConfig(const std::string & app, const std::string & server, const std::string & filename, string &config, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®ip获å–é…ç½® - * @param appServerName - * @param filename - * @param host - * @param config - * - * @return int - */ - virtual int loadConfigByHost(const string &appServerName, const string &filename, const string &host, string &config, tars::TarsCurrentPtr current); - - /** - * - * @param appServerName - * @param filename - * @param host - * @param current - * - * @return int - */ - virtual int checkConfig(const string &appServerName, const string &filename, const string &host, string &result, tars::TarsCurrentPtr current); - - /** - * 获å–é…置文件列表 - * param configInfo ConfigInfo - * param vf: é…置文件å - * - * return : é…置文件内容 - */ - virtual int ListConfigByInfo(const ConfigInfo& configInfo, vector &vf,tars::TarsCurrentPtr current); - - /** - * 加载é…置文件 - * param configInfo ConfigInfo - * param config: é…置文件内容 - * - * return : - */ - virtual int loadConfigByInfo(const ConfigInfo & configInfo, string &config,tars::TarsCurrentPtr current); - - /** - * - * @param configInfo ConfigInfo - * - * @return int - */ - virtual int checkConfigByInfo(const ConfigInfo & configInfo, string &result,tars::TarsCurrentPtr current); - - /** - * 获å–æœåŠ¡çš„所有é…置文件列表, - * @param configInfo 支æŒæ‹‰å–应用é…置列表,æœåŠ¡é…置列表,机器é…置列表 - * @param[out] vf 获å–到的文件å称列表 - * @return int 0: æˆåŠŸ, -1:失败 - **/ - virtual int ListAllConfigByInfo(const tars::GetConfigListInfo & configInfo, vector &vf, tars::TarsCurrentPtr current); - -protected: - - /** - * 加载本serverçš„é…置文件 - */ - void loadconf(); - -private: - - /** - * - * 查询configId对应的é…ç½® - * - * @param iConfigId - * @param sConfig - * - * @return int 0,æˆåŠŸæˆ–者无此config,表示应用级é…ç½® - * 2,表示set应用é…ç½® - */ - int loadConfigByPK(int iConfigId, string &sConfig); - - /** - * - * 查询configId对应的é…置信æ¯ï¼ŒåŒæ—¶ä¼šæŸ¥çœ‹è¯¥ - * - * @param iConfigId - * @param setdivision setåˆ†ç»„ä¿¡æ¯ - * @param sConfig - * - * @return int 0,æˆåŠŸ - * - */ - int loadRefConfigByPK(int iConfigId, const string& setdivision,string &sConfig); - - /** - * - * 获å–iConfigId对应的引用id列表 - * - * @param iConfigId - * @param referenceIds - * - * @return int - */ - int getReferenceIds(int iConfigId,vector &referenceIds); - - /** - * 获å–应用级公共é…ç½® - * @param appName 应用å称 - * @param fileName 文件å称 - * @out param 获å–到的é…置文件内容 - * - * @return int - */ - int loadAppConfig(const std::string& appName, const std::string& fileName, string &config, tars::TarsCurrentPtr current); - - /** - * 获å–应用级公共é…ç½® - * @param configInfo ConfigInfo - * @out param 获å–到的é…置文件内容 - * - * @return int - */ - int loadAppConfigByInfo(const ConfigInfo & configInfo, string &config, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®ip获å–é…ç½® - * @param configInfo ConfigInfoï¼›configInfo中指定ip,如果没有则从current获å–当å‰è¯·æ±‚çš„ip - * @param config - * - * @return int - */ - int loadConfigByHost(const ConfigInfo & configInfo, string &config, tars::TarsCurrentPtr current); - - /** - * 获å–setåˆ†ç»„ä¿¡æ¯ - * @out param sSetName setå称 - * @out param sSetArea set地区å称 - * @out param sSetGroup set分组å称 - * @param sSetDivision setä¿¡æ¯:sSetName.sSetArea.sSetGroupæ ¼å¼ - * - * @return bool setä¿¡æ¯æ­£ç¡®åˆ™è¿”回true - */ - bool getSetInfo(string& sSetName,string& sSetArea,string& sSetGroup,const string& sSetDivision); - - /** - * åˆå¹¶ä¸¤ä¸ªé…置文件,如果有冲çªé¡¹,则sHighConf优先级比sLowConf高 - * - * @param sLowConf 应用å称 - * @param sHighConf 应用å称 - * - * @ return string åˆå¹¶çš„结果 - */ - string mergeConfig(const string& sLowConf,const string& sHighConf); - - /** - * 通过对app+server + ip + file进行é™åˆ¶åŠ è½½é…置文件的频率 - * - */ - bool IsLimited(const std::string & app, const std::string & server, const std::string & sIp,const string& sFile); - -protected: - TC_Mysql _mysqlConfig; - - static map > _loadConfigLimited; - - //时间间隔 - int _interval; - - ///æ¯åˆ†é’Ÿé™åˆ¶ä¸ªæ•° - int _limitInterval; -}; - -#endif - diff --git a/cpp/framework/ConfigServer/ConfigServer.cpp b/cpp/framework/ConfigServer/ConfigServer.cpp deleted file mode 100644 index fcb276c52..000000000 --- a/cpp/framework/ConfigServer/ConfigServer.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ConfigServer.h" -#include "ConfigImp.h" - -void ConfigServer::initialize() -{ - //滚动日志也打å°æ¯«ç§’ - TarsRollLogger::getInstance()->logger()->modFlag(TC_DayLogger::HAS_MTIME); - - //增加对象 - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".ConfigObj"); -} - -void ConfigServer::destroyApp() -{ -} - diff --git a/cpp/framework/ConfigServer/ConfigServer.h b/cpp/framework/ConfigServer/ConfigServer.h deleted file mode 100644 index 75b24f938..000000000 --- a/cpp/framework/ConfigServer/ConfigServer.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __CONFIG_SERVER_H_ -#define __CONFIG_SERVER_H_ - -#include "servant/Application.h" - -using namespace tars; - -class ConfigServer : public Application -{ -protected: - /** - * åˆå§‹åŒ–, 进程åªä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžæž„, 进程退出时会调用一次 - */ - virtual void destroyApp(); -}; - -#endif - diff --git a/cpp/framework/ConfigServer/main.cpp b/cpp/framework/ConfigServer/main.cpp deleted file mode 100644 index 133eb9841..000000000 --- a/cpp/framework/ConfigServer/main.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ConfigServer.h" -#include - -using namespace tars; - -TC_Config * g_pconf; - -int main(int argc, char *argv[]) -{ - try - { - ConfigServer app; - g_pconf = & app.getConfig(); - app.main(argc, argv); - - app.waitForShutdown(); - } - catch(exception &ex) - { - cerr<< ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/LogServer/CMakeLists.txt b/cpp/framework/LogServer/CMakeLists.txt deleted file mode 100644 index a479a7c1f..000000000 --- a/cpp/framework/LogServer/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ - -set(MODULE "tarslog") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/) - -complice_module(${MODULE}) - diff --git a/cpp/framework/LogServer/LogImp.cpp b/cpp/framework/LogServer/LogImp.cpp deleted file mode 100644 index 82515e719..000000000 --- a/cpp/framework/LogServer/LogImp.cpp +++ /dev/null @@ -1,433 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "LogImp.h" - -GlobeInfo g_globe; - -TC_DayLogger& GlobeInfo::makeDayLogger(const string &app, const string &server, const string &logname, const string &format,const string& ip) -{ - string sLogPath = _log_path + "/" + app + "/" + server + "/" + logname; - - TLOGDEBUG("GlobeInfo::makeDayLogger sLogPath:" << sLogPath << "|format:" << format << endl); - - DLOG << "GlobeInfo::makeDayLogger sLogPath:" << sLogPath << "|format:" << format << endl; - - TC_DayLogger *p = new TC_DayLogger(); - p->init(sLogPath, format); - p->setupThread(&_group); - - //所有标识都ä¸è¦ - p->modFlag(0xffff, false); - - _loggers[logname][ip] = p; - - _formats[logname][ip] = format; - - return (*p); -} - -TC_DayLogger& GlobeInfo::makeDayLogger(const LogInfo & info, const string &logname, const string &format,const string& ip) -{ - string sLogPath = getRealLogName(info); - - //获å–é…ç½®ä¸­çš„è®°å½•æ–¹å¼ - string sFormat = format; - string sLogType = info.sLogType; - string sKey = info.appname + "." +info.servername + "." + info.sFilename; - map::iterator it = _mLogType.find(sKey); - if(it != _mLogType.end()) - { - //æœåŠ¡ç«¯çš„é…置优先级最高 - sLogType = it->second; - //ä½¿ç”¨è§„èŒƒæ ¼å¼ - sFormat = ""; - } - - TLOGDEBUG("GlobeInfo::makeDayLogger sLogPath:" << sLogPath << "|app:" << info.appname << "|server:" << info.servername << "|filename:" << info.sFilename - << "|format:" << format << "|setdivision:" << info.setdivision << "|bHasSufix:" << info.bHasSufix << "|bHasAppNamePrefix:" << info.bHasAppNamePrefix - << "|sConcatStr" << info.sConcatStr << "|sSepar" << info.sSepar << "|sLogType:" << sLogType << endl); - - DLOG << "GlobeInfo::makeDayLogger sLogPath:" << sLogPath << "|app:" << info.appname << "|server:" << info.servername << "|filename:" << info.sFilename - << "|format:" << format << "|setdivision:" << info.setdivision << "|bHasSufix:" << info.bHasSufix << "|bHasAppNamePrefix:" << info.bHasAppNamePrefix - << "|sConcatStr" << info.sConcatStr << "|sSepar" << info.sSepar << "|sLogType:" << sLogType <init(sLogPath, format,info.bHasSufix,info.sConcatStr,getTarsLogType(sFormat,sLogType)); - p->setSeparator(info.sSepar); - p->enableSqareWrapper(info.bHasSquareBracket); - - p->setupThread(&_group); - - //所有标识都ä¸è¦ - p->modFlag(0xffff, false); - - _loggers[logname][ip] = p; - - _formats[logname][ip] = format; - return (*p); -} - -TarsLogTypePtr GlobeInfo::getTarsLogType(const string& sFormat,const string& sCutType) -{ - TarsLogTypePtr logTypePtr = NULL; - if(sCutType != "") - { - string sType = TC_Common::lower(sCutType); - string::size_type pos = string::npos; - if((pos = sType.find("day")) != string::npos) - { - int n = 1; - if(sType.substr(0,pos) != "" && TC_Common::isdigit(sType.substr(0,pos)) == true) - { - n = TC_Common::strto(sType.substr(0,pos)); - } - - string format = (sFormat=="") ? TarsLogByDay::FORMAT : sFormat; - logTypePtr = new TarsLogByDay(format,n); - } - else if((pos = sType.find("hour")) != string::npos) - { - int n = 1; - if(sType.substr(0,pos) != "" && TC_Common::isdigit(sType.substr(0,pos)) == true) - { - n = TC_Common::strto(sType.substr(0,pos)); - } - - string format = (sFormat=="") ? TarsLogByHour::FORMAT : sFormat; - logTypePtr = new TarsLogByHour(format,n); - } - else if((pos = sType.find("minute")) != string::npos) - { - int n = 10;//支æŒ5,10分钟 - if(sType.substr(0,pos) != "" && TC_Common::isdigit(sType.substr(0,pos)) == true) - { - n = TC_Common::strto(sType.substr(0,pos)); - } - - string format = (sFormat=="") ? TarsLogByMinute::FORMAT : sFormat; - logTypePtr = new TarsLogByMinute(format,n); - } - } - - return logTypePtr; -} - -string GlobeInfo::getRealLogName(const LogInfo & info) -{ - string sRealLogname; - if(info.bHasAppNamePrefix) - { - sRealLogname = info.sFilename.empty() ? (info.appname +"." + info.servername) : \ - (info.appname +"." + info.servername + info.sConcatStr + info.sFilename); - } - else - { - sRealLogname = info.sFilename; - } - - string setDivision = getSetGoodFormat(info.setdivision); - - string sLogPath = _log_path + "/" + info.appname + "/" + setDivision + "/" + info.servername + "/" + sRealLogname; - - return sLogPath; -} - -string GlobeInfo::getLogName(const LogInfo & info) -{ - string sLogname = TC_Common::tostr(info.bHasSufix) + info.sConcatStr + TC_Common::tostr(info.bHasAppNamePrefix) + info.sConcatStr +\ - TC_Common::tostr(info.bHasSquareBracket) + info.sConcatStr + info.sSepar + info.sConcatStr +\ - info.setdivision + info.sConcatStr + info.appname + info.sConcatStr + info.servername + info.sConcatStr + info.sFilename + info.sConcatStr + info.sLogType; - - return sLogname; -} - -bool GlobeInfo::HasSameFormat(const string& logname,const string& format,string& ip) -{ - map::iterator itFormat = _formats[logname].begin(); - bool bHasFormat = false; //是å¦æœ‰åˆ›å»ºè¿‡çš„format - for(; itFormat != _formats[logname].end(); itFormat++) - { - if(itFormat->second == format) //找到创建过的format - { - bHasFormat = true; - ip = itFormat->first; //为创建过相åŒformat的节点ip - break; - } - } - - return bHasFormat; -} - -bool GlobeInfo::IsLoggerAttached(const string& logname,const string& sExcludeIp,const TC_DayLogger* pLogger) -{ - map::iterator itLogger = _loggers[logname].begin(); - bool bIsAttached = false; //是å¦æœ‰ip在用该logger实例 - - for(; itLogger != _loggers[logname].end(); itLogger++) - { - if(itLogger->first != sExcludeIp && itLogger->second == pLogger) - { - bIsAttached = true; - TLOGDEBUG("GlobeInfo::IsLoggerAttached first:" << itLogger->first<< endl); - break; - } - } - - return bIsAttached; -} - -TC_DayLogger& GlobeInfo::getLogger(const LogInfo & info,const string& ip) -{ - string format = info.sFormat; - if(binary_search(_vHourlist.begin(), _vHourlist.end(), (info.appname + "." + info.servername)) != false) - { - format = "%Y%m%d%H"; - } - - string setDivision = getSetGoodFormat(info.setdivision); - - if(!TC_File::isFileExistEx((_log_path + "/" + info.appname + "/" + setDivision + "/" + info.servername), S_IFDIR)) - { - TC_File::makeDirRecursive(_log_path + "/" + info.appname + "/" + setDivision + "/" + info.servername); - } - - string logname = getLogName(info); - - TLOGDEBUG("GlobeInfo::getLogger logname:" << logname << "|format:" << format << "|setDivision:" << setDivision << endl); - - Lock lock(*this); - - map >::iterator itLogName = _loggers.find(logname); - if( itLogName == _loggers.end()) //没有创建过的log - { - return makeDayLogger(info, logname, format, ip); - } - - map::iterator itIp = itLogName->second.find(ip); - if(itIp == itLogName->second.end()) //没有创建过的ip节点 - { - string sSameIp(""); - - if(HasSameFormat(logname,format,sSameIp)) //有创建过的format,把新的节点ip划分到åŒç»„ - { - _loggers[logname][ip] = _loggers[logname][sSameIp];//使用具有相åŒformatçš„logger实例 - _formats[logname][ip] = format; - - return (*(_loggers[logname][ip])); - } - else //没有创建过该format,新建立一个logname下的format组 - { - return makeDayLogger(info, logname, format,ip); - } - - } - else //该logname下的这个ip节点创建过 - { - if(_formats[logname][ip] != format) //该ip节点的日志格å¼å˜åŒ–了 - { - string sSameIp(""); - TC_DayLogger* pOldLogger = _loggers[logname][ip]; - if(HasSameFormat(logname,format,sSameIp))//å·²ç»æœ‰è¯¥æ ¼å¼ï¼Œå½’å…¥åŒç»„ - { - _loggers[logname][ip] = _loggers[logname][sSameIp]; - _formats[logname][ip] = format; - - if(pOldLogger) //删除旧的loger - { - pOldLogger->unSetupThread(); - delete pOldLogger; - pOldLogger = NULL; - } - - return (*(_loggers[logname][ip])); - } - else - { - _loggers[logname][ip] = NULL; - _formats[logname][ip] = ""; - if(!IsLoggerAttached(logname,ip,pOldLogger))//å¯èƒ½æœ‰å¤šä¸ªèŠ‚点引用这个loger,这里需è¦åˆ¤æ–­ - { - pOldLogger->unSetupThread(); - delete pOldLogger; - pOldLogger = NULL; - } - - return makeDayLogger(info, logname, format,ip); - } - } - } - - //没有改å˜æ ¼å¼ - return (*(_loggers[logname][ip])); - -} - -TC_DayLogger& GlobeInfo::getLogger(const string &app, const string &server, const string &file, const string &sformat,const string& ip) -{ - if(!TC_File::isFileExistEx((_log_path + "/" + app + "/" + server), S_IFDIR)) - { - TC_File::makeDirRecursive(_log_path + "/" + app + "/" + server); - } - - string format = sformat; - if(binary_search(_vHourlist.begin(),_vHourlist.end(),(app + "." + server)) != false) - { - format = "%Y%m%d%H"; - } - - string logname = file.empty() ? (app + "." + server) : (app + "." + server + "_" + file); - - TLOGDEBUG("GlobeInfo::getLogger logname:" << logname << "|format:" << format << endl); - - Lock lock(*this); - - map >::iterator itLogName = _loggers.find(logname); - if( itLogName == _loggers.end()) //没有创建过的log - { - return makeDayLogger(app, server, logname, format,ip); - } - - map::iterator itIp = itLogName->second.find(ip); - if(itIp == itLogName->second.end()) //没有创建过的ip节点 - { - string sSameIp(""); - if(HasSameFormat(logname,format,sSameIp)) //有创建过的format,把新的节点ip划分到åŒç»„ - { - _loggers[logname][ip] = _loggers[logname][sSameIp];//使用具有相åŒformatçš„logger实例 - _formats[logname][ip] = format; - - return (*(_loggers[logname][ip])); - } - else //没有创建过该format,新建立一个logname下的format组 - { - return makeDayLogger(app, server, logname, format,ip); - } - - } - else //该logname下的这个ip节点创建过 - { - if(_formats[logname][ip] != format) //该ip节点的日志格å¼å˜åŒ–了 - { - string sSameIp(""); - TC_DayLogger* pOldLogger = _loggers[logname][ip]; - if(HasSameFormat(logname,format,sSameIp))//å·²ç»æœ‰è¯¥æ ¼å¼ï¼Œå½’å…¥åŒç»„ - { - _loggers[logname][ip] = _loggers[logname][sSameIp]; - _formats[logname][ip] = format; - - if(pOldLogger) //删除旧的loger - { - pOldLogger->unSetupThread(); - delete pOldLogger; - pOldLogger = NULL; - } - - return (*(_loggers[logname][ip])); - } - else - { - _loggers[logname][ip] = NULL; - _formats[logname][ip] = ""; - if(!IsLoggerAttached(logname,ip,pOldLogger))//å¯èƒ½æœ‰å¤šä¸ªèŠ‚点引用这个loger,这里需è¦åˆ¤æ–­ - { - pOldLogger->unSetupThread(); - delete pOldLogger; - pOldLogger = NULL; - } - - return makeDayLogger(app, server, logname, format,ip); - } - } - } - - //没有改å˜æ ¼å¼ - return (*(_loggers[logname][ip])); -} - -string GlobeInfo::getSetGoodFormat(const string& sSetDivision) -{ - vector vtSetDivisions = TC_Common::sepstr(sSetDivision,"."); - if(vtSetDivisions.size() != 3) - { - return ""; - } - else - { - if(vtSetDivisions[2] == "*") - { - return string(vtSetDivisions[0] + vtSetDivisions[1]); - } - else - { - return string(vtSetDivisions[0] + vtSetDivisions[1] + vtSetDivisions[2]); - } - } -} - - -void GlobeInfo::update(const vector &vHourlist, const map &mLogType) -{ - Lock lock(*this); - - _vHourlist = vHourlist; - - _mLogType = mLogType; -} - -////////////////////////////////////////// -// -void LogImp::initialize() -{ - -} - -void LogImp::logger(const string &app, const string &server, const string &file, const string &format, const vector &buffer, tars::TarsCurrentPtr current) -{ - TC_DayLogger &dl = g_globe.getLogger(app, server, file, format,current->getIp()); - - //记录日志 - for(size_t i = 0; i < buffer.size(); i++) - { - if(g_globe._bIpPrefix) - { - dl.any() << current->getIp() << "|" << buffer[i]; - } - else - { - dl.any() << buffer[i]; - } - } -} - -void LogImp::loggerbyInfo(const LogInfo & info,const vector & buffer,tars::TarsCurrentPtr current) -{ - TC_DayLogger &dl = g_globe.getLogger(info,current->getIp()); - - //记录日志 - for(size_t i = 0; i < buffer.size(); i++) - { - if(g_globe._bIpPrefix) - { - dl.any() << current->getIp() << info.sSepar << buffer[i]; - } - else - { - dl.any() << buffer[i]; - } - } -} diff --git a/cpp/framework/LogServer/LogImp.h b/cpp/framework/LogServer/LogImp.h deleted file mode 100644 index 83348ef6b..000000000 --- a/cpp/framework/LogServer/LogImp.h +++ /dev/null @@ -1,222 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __LOG_IMP_H_ -#define __LOG_IMP_H_ - -#include "servant/LogF.h" -#include "util/tc_common.h" -#include "util/tc_file.h" -#include "util/tc_logger.h" -#include "util/tc_monitor.h" -#include "LogServer.h" - -using namespace tars; - -/** - * å…¨å±€ä¿¡æ¯ - */ -struct GlobeInfo : public TC_ThreadLock -{ -public: - /** - * 获å–按天日志 - * @param app - * @param server - * @param file - * @param format - * @param ip æœåŠ¡æ‰€åœ¨èŠ‚点ip - * - * @return TC_DayLogger& - */ - TC_DayLogger& getLogger(const string &app, const string &server, const string &file, const string &format,const string& ip); - - /** - * 获å–按天日志 - * @param info 详情å‚看LogInfo - * @param ip æœåŠ¡æ‰€åœ¨èŠ‚点ip - * - * @return TC_DayLogger& - */ - TC_DayLogger& getLogger(const LogInfo & info,const string& ip); - - /** - * æ›´æ–°_vHourlistå’Œ_mLogTypeé…ç½® - */ - void update(const vector &vHourlist, const map &mLogType); - -protected: - /** - * 生æˆæŒ‰å¤©æ—¥å¿— - * @param info 详情å‚看LogInfo - * @param logname - * @param format - * @param ip æœåŠ¡æ‰€åœ¨èŠ‚点ip - * - * @return TC_DayLogger& - */ - TC_DayLogger& makeDayLogger(const LogInfo & info, const string &logname, const string &format,const string& ip); - - /** - * 生æˆæŒ‰å¤©æ—¥å¿— - * @param app - * @param server - * @param logname - * @param format - * @param ip æœåŠ¡æ‰€åœ¨èŠ‚点ip - * - * @return TC_DayLogger& - */ - TC_DayLogger& makeDayLogger(const string &app, const string &server, const string &logname, const string &format,const string& ip); - - /** - * 判断相应的logname下是å¦æœ‰æŒ‡å®šçš„format - * @param logname ç”±file.empty() ? (app + "." + server) : (app + "." + server + "_" + file)æ–¹å¼ç»„æˆ - * @param format - * @param[out] ip 如果有指定的format存在,则返回该format对应的节点ip - * - * @return bool - */ - bool HasSameFormat(const string& logname,const string& format,string& ip); - - /** - * 判断在logname下,除了节点sExcludeIp外,是å¦è¿˜æœ‰å…¶å®ƒèŠ‚点在使用pLoger指定的logger实例 - * @param logname - * @param sExcludeIp - * @param pLogger - * - * @return bool - */ - bool IsLoggerAttached(const string& logname,const string& sExcludeIp,const TC_DayLogger* pLoger); - -private: - /** - * 获å–setå®Œæ•´æ ¼å¼ - * @param sSetDivision set分组信æ¯,例如:mtt.s.1 mtt.s.* - * - * @return 规整åŽçš„æ ¼å¼ï¼Œä¾‹å¦‚:mtt.s.* 去掉"*"符å·ï¼Œè¿”回mttsï¼›mtts1则原样返回 - */ - string getSetGoodFormat(const string& sSetDivision); - - /** - * 从LogInfo中获å–相关信æ¯å¾—到标识一个logger对象的字符串 - * @param info - */ - string getLogName(const LogInfo & info); - - /** - * 从LogInfo中获å–日志文件å称,包括路径å在内 - * @param info - */ - string getRealLogName(const LogInfo & info); - - TarsLogTypePtr getTarsLogType(const string& sFormat, const string& sCutType); - -public: - /** - * 写日志线程 - */ - TC_LoggerThreadGroup _group; - - /** - * 写日志路径 - */ - string _log_path; - - /** - * logger对象:logname,ip,logger - */ - map > _loggers; - - /** - * loggeræ ¼å¼:logname,ip,format - */ - map > _formats; - - /** - * 按å°æ—¶è®°å½•çš„é…ç½® - */ - vector _vHourlist; - - /** - * 按时间方å¼é…ç½® - * key:日志文件全å: appname.servername.filename - * value:时间格å¼å­—符串:hour,2hour,5minute,10minute - */ - map _mLogType; - - /** - *是å¦æ‰“å°å®¢æˆ·ç«¯ip - */ - bool _bIpPrefix; - -}; - -extern GlobeInfo g_globe; - -/** - * log实现 - */ -class LogImp : public Log -{ -public: - /** - * - */ - LogImp(){}; - - /** - * - */ - ~LogImp(){}; - - /** - * åˆå§‹åŒ– - * - * @return int - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy() {}; - - /** - * 输出日志信æ¯åˆ°æŒ‡å®šæ–‡ä»¶ - * @param app 业务å称 - * @param server æœåŠ¡å称 - * @param file 日志文件å称 - * @param format æ—¥å¿—è¾“å‡ºæ ¼å¼ - * @param buffer 日志内容 - * - * - */ - void logger(const string &app, const string &server, const string &file, const string &format, const vector &buffer, tars::TarsCurrentPtr current); - /** - * 获å–æ•°æ® - * @param info - * @param buffer - * - */ - void loggerbyInfo(const LogInfo & info,const vector & buffer,tars::TarsCurrentPtr current); - -private: - -}; - -#endif - diff --git a/cpp/framework/LogServer/LogServer.cpp b/cpp/framework/LogServer/LogServer.cpp deleted file mode 100644 index 69a7ae82c..000000000 --- a/cpp/framework/LogServer/LogServer.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "LogServer.h" -#include "util/tc_logger.h" -#include "LogImp.h" - -void LogServer::initialize() -{ - string s; - loadLogFormat("","",s); - - //日志路径 - g_globe._log_path = _conf["/tars/log"]; - - //å¯åŠ¨å†™çº¿ç¨‹ - g_globe._group.start(TC_Common::strto(_conf["/tars/log"])); - - string prefix = TC_Common::lower(_conf.get("/tars/log","true")); - g_globe._bIpPrefix = (prefix == "true") ? true : false; - - //增加对象 - addServant(ServerConfig::Application + "." + ServerConfig::ServerName +".LogObj"); - - TARS_ADD_ADMIN_CMD_NORMAL("reloadLogFormat", LogServer::loadLogFormat); -} - -bool LogServer::loadLogFormat(const string& command, const string& params, string& result) -{ - TLOGDEBUG("LogServer::loadLogFormat command:" << command << "|params:" << params << endl); - - try - { - TC_Config conf; - - conf.parseFile(ServerConfig::ConfigFile); - - vector vHourlist; - - map mLogType; - - try - { - string sHour = conf["/tars/log/format"]; - - vHourlist = TC_Common::sepstr(sHour,"|;,"); - - sort(vHourlist.begin(),vHourlist.end()); - - unique(vHourlist.begin(),vHourlist.end()); - - result = "loadLogFormat succ:" + sHour; - - TLOGDEBUG("LogServer::loadLogFormat result:" << result << endl); - - DLOG<< "LogServer::loadLogFormat result:" << result << endl; - - //hour=app.server.file|app2.server2.file2 - map mType; - if(conf.getDomainMap("/tars/log/logtype", mType)) - { - map::iterator it = mType.begin(); - while(it != mType.end()) - { - vector vList = TC_Common::sepstr(it->second,"|;,"); - for(size_t i = 0;i < vList.size();i++) - { - //app.server.file = hour - mLogType[vList[i]] = it->first; - - TLOGDEBUG("LogServer::loadLogFormat " << vList[i] << "|" << it->first << endl); - - DLOG<<"LogServer::loadLogFormat " << vList[i] << "|" << it->first << endl; - } - it++; - } - } - - g_globe.update(vHourlist, mLogType); - - } - catch(exception& e) - { - result += e.what(); - TLOGERROR("LogServer::loadLogFormat command:" << command << "|params:" << params << "|result:" << result << endl); - } - - return true; - } - catch(exception &e) - { - result += e.what(); - TLOGERROR("LogServer::loadLogFormat command:" << command << "|params:" << params << "|result:" << result << endl); - } - - return false; -} - -void LogServer::destroyApp() -{ - TLOGDEBUG("LogServer::destroyApp ok" << endl); -} - diff --git a/cpp/framework/LogServer/LogServer.h b/cpp/framework/LogServer/LogServer.h deleted file mode 100644 index 450981df7..000000000 --- a/cpp/framework/LogServer/LogServer.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __LOG_SERVER_H_ -#define __LOG_SERVER_H_ - -#include "servant/Application.h" - -using namespace tars; - -class LogServer : public Application -{ -protected: - /** - * åˆå§‹åŒ–, 进程会调用一次 - */ - virtual void initialize(); - - /** - * æžå¤Ÿ, 进程退出时会调用一次 - */ - virtual void destroyApp(); - -private: - - bool loadLogFormat(const string& command, const string& params, string& result); -}; - -#endif - diff --git a/cpp/framework/LogServer/main.cpp b/cpp/framework/LogServer/main.cpp deleted file mode 100644 index cee3f2097..000000000 --- a/cpp/framework/LogServer/main.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "LogServer.h" -#include - -using namespace tars; - -LogServer g_app; - -int main(int argc, char *argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/NodeServer/Activator.cpp b/cpp/framework/NodeServer/Activator.cpp deleted file mode 100644 index d3004f1e2..000000000 --- a/cpp/framework/NodeServer/Activator.cpp +++ /dev/null @@ -1,465 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include "Activator.h" -#include "NodeServer.h" - -static pid_t *childpid = NULL; /* ptr to array allocated at run-time */ -#define SHELL "/bin/sh" - -pid_t Activator::activate(const string& strExePath, const string& strPwdPath, const string& strRollLogPath, const vector& vOptions, vector& vEnvs) -{ - TC_ThreadLock::Lock lock(*this); - addActivatingRecord(); - if (isActivatingLimited() == true) - { - TLOGERROR("Activator::activate The server " << strExePath << " activating is limited! it will not auto start until after " + TC_Common::tostr(_punishInterval) + " seconds" << endl); - } - - if (strExePath.empty()) - { - throw runtime_error("The server executable path is empty."); - } - - if (TC_File::isFileExistEx(strExePath) && !TC_File::canExecutable(strExePath)) - { - TC_File::setExecutable(strExePath, true); - } - vector vArgs; - vArgs.push_back(strExePath); - vArgs.insert(vArgs.end(), vOptions.begin(), vOptions.end()); - - TLOGDEBUG("Activator::activate activating server [exepath: " << strExePath << ", args: " << TC_Common::tostr(vArgs) << "]" << endl); - - int argc = static_cast(vArgs.size()); - char **argv = static_cast(malloc((argc + 1) * sizeof(char *))); - int i = 0; - for (vector::const_iterator p = vArgs.begin(); p != vArgs.end(); ++p, ++i) - { - assert(i < argc); - argv[i] = strdup(p->c_str()); - } - assert(i == argc); - argv[argc] = 0; - - const char *pwdCStr = strPwdPath.c_str(); - - pid_t pid = fork(); - if (pid == -1) - { - TLOGDEBUG("Activator::activate "<< strPwdPath << "|fork child process catch exception|errno=" << errno << endl); - throw runtime_error("fork child process catch exception"); - } - - if (pid == 0) - { - int maxFd = static_cast(sysconf(_SC_OPEN_MAX)); - for (int fd = 3; fd < maxFd; ++fd) - { - close(fd); - } - - //server stdcout 日志在滚动日志显示 - if (_redirectPath != "") - { - TC_File::makeDirRecursive(TC_File::extractFilePath(_redirectPath)); - if ((freopen64(_redirectPath.c_str(), "ab", stdout)) != NULL && (freopen64(_redirectPath.c_str(), "ab", stderr)) != NULL) - { - cout << argv[0] << " redirect stdout and stderr to " << _redirectPath << endl; - } - else - { - //é‡å®šå‘失败 直接退出 - exit(0); - //cout << argv[0]<<" cannot redirect stdout and stderr to log file" << _redirectPath << "|errno=" < mResult; - if (doScript(strServerId, strStartScript, strResult, mResult) == false) - { - throw runtime_error("run script file " + strStartScript + " error :" + strResult); - } - - pid_t pid = -1; - if (!strMonitorScript.empty() && TC_File::isFileExistEx(strMonitorScript)) - { - string s; - mResult.clear(); - if (doScript(strServerId, strMonitorScript, s, mResult) == false) - { - throw runtime_error("run script " + strMonitorScript + " error :" + s); - } - } - - return pid; -} - -int Activator::deactivate(int pid) -{ - if (pid != 0) - { - return sendSignal(pid, SIGKILL); - } - - return -1; -} - -int Activator::deactivateAndGenerateCore(int pid) -{ - if (pid != 0) - { - return sendSignal(pid, SIGABRT); - } - - return -1; -} - -int Activator::sendSignal(int pid, int signal) const -{ - assert(pid); - int ret = ::kill(static_cast(pid), signal); - if (ret != 0 && errno != ESRCH) - { - TLOGERROR("Activator::activate send signal " << signal << " to pid " << pid << " catch exception|" << errno << endl); - return -1; - } - - return ret; -} - -bool Activator::isActivatingLimited() -{ - unsigned uLen = _activingRecord.size(); - - //惩罚时间段内ç¦æ­¢å¯åŠ¨ - if (_limited == true && uLen > 0 && TNOW - _activingRecord[uLen - 1] < _punishInterval) - { - return true; - } - - return false; -} -void Activator::addActivatingRecord() -{ - time_t tNow = TNOW; - unsigned uLen = _activingRecord.size(); - - if (uLen > 0) - { - if (tNow - _activingRecord[uLen - 1] < _timeInterval) - { - _curCount++; - } - - //完æˆæƒ©ç½šæ—¶é—´,自动解除惩罚 - if (_limited == true && tNow - _activingRecord[uLen - 1] > _punishInterval * 1.5) - { - _curCount = 0; - _limited = false; - _activingRecord.clear(); - } - - if (_curCount > _maxCount) - { - _limited = true; - } - } - _activingRecord.push_back(tNow); -} - -bool Activator::doScript(const string& strServerId, const string& strScript, string& strResult, map& mResult, const string& sEndMark) -{ - TLOGINFO("Activator::activate doScript " << strScript << " begin----" << endl); - - if (!TC_File::isFileExistEx(strScript)) - { - strResult = "The script file: " + strScript + " is not exist"; - return false; - } - - string sRealEndMark = sEndMark; - if (sRealEndMark == "") - { - sRealEndMark = "end-" + TC_File::extractFileName(strScript); - } - - TLOGINFO("Activator::activate doScript " << strScript << " endMark----" << sRealEndMark << endl); - - if (!TC_File::canExecutable(strScript)) - { - TC_File::setExecutable(strScript, true); - } - - string sRedirect = ""; - if (_redirectPath != "") - { - sRedirect = " 2>&1 >>" + _redirectPath; - } - - string sCmd = strScript + sRedirect + " " + strServerId + " &"; - FILE *fp = popen2(sCmd.c_str(), "r"); - if (fp == NULL) - { - strResult = "popen script file: " + strScript + " error "; - return false; - } - - //使文件å˜æˆéžé˜»å¡žæ–¹å¼è®¿é—® - int flags; - if ((flags = fcntl(fileno(fp), F_GETFL, 0)) < 0) - { - strResult = "fcntl get error.script file: " + strScript; - fflush(fp); - pclose2(fp); - return false; - } - flags |= O_NONBLOCK; - if (fcntl(fileno(fp), F_SETFL, flags) < 0) - { - strResult = "fcntl set error.script file: " + strScript; - fflush(fp); - pclose2(fp); - - return false; - } - //strResult = ""; - - int c; - time_t tNow = TNOW; - while (TNOW - 2 < tNow) - { - usleep(200000); - while ((c = fgetc(fp)) != EOF) - { - strResult += (char)c; - } - if (sRealEndMark == "" || strResult.find(sRealEndMark) != string::npos) - { - TLOGINFO("Activator::doScript "<< sCmd << "|sEndMark " << sRealEndMark << " finded|" << strResult << endl); - break; - } - } - - strResult = TC_Common::replace(strResult, "\n", "\r\n"); - LOG->info() << "Activator::doScript "<< sCmd << " result:" << strResult << endl; - - fflush(fp); - pclose2(fp); - - mResult = parseScriptResult(strServerId, strResult); - return true; -} - -map Activator::parseScriptResult(const string& strServerId, const string& strResult) -{ - map mResult; - vector vResult = TC_Common::sepstr(strResult, "\n"); - for (unsigned i = 0; i < vResult.size(); i++) - { - string::size_type pos = vResult[i].find('='); - if (pos != string::npos) - { - string sName = vResult[i].substr(0, pos); - string sValue = vResult[i].substr(pos + 1); - sName = TC_Common::lower(TC_Common::trim(sName)); - sValue = TC_Common::trim(sValue); - mResult[sName] = sValue; - if (sName == "notify") - { - g_app.reportServer(strServerId, sValue); - } - } - } - - return mResult; -} - -FILE* Activator::popen2(const char *cmdstring, const char *type) -{ - int i, pfd[2]; - pid_t pid; - FILE *fp; - /*only allow "r" or "w" */ - if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) - { - errno = EINVAL; /* required by POSIX.2 */ - return (NULL); - } - int maxfd = static_cast(sysconf(_SC_OPEN_MAX)); - if (childpid == NULL) - { /* first time through */ - /* allocate zeroed out array for child pids */ - childpid = new pid_t[maxfd]; - memset(childpid, 0, sizeof(pid_t) * maxfd); - //char** envArray = new char*[envCount]; - if (childpid == NULL) - { - return (NULL); - } - } - - if (pipe(pfd) < 0) - { - return (NULL); /* errno set by pipe() */ - } - - if ((pid = fork()) < 0) - { - return (NULL); /* errno set by fork() */ - } - else if (pid == 0) - { /* child */ - if (*type == 'r') - { - close(pfd[0]); - dup2(pfd[1], STDOUT_FILENO); - dup2(pfd[1], STDERR_FILENO); - close(pfd[1]); - } - else - { - close(pfd[1]); - dup2(pfd[0], STDIN_FILENO); - close(pfd[0]); - } - /* close all descriptors opened in parent process*/ - for (i = 3; i < maxfd; i++) - { - if (i != pfd[0] && i != pfd[1]) - { - close(i); - } - } - execl(SHELL, "sh", "-c", cmdstring, (char *)0); - _exit(127); - } - /* parent */ - if (*type == 'r') - { - close(pfd[1]); - if ((fp = fdopen(pfd[0], type)) == NULL) - { - return (NULL); - } - } - else - { - close(pfd[0]); - if ((fp = fdopen(pfd[1], type)) == NULL) - { - return (NULL); - } - } - childpid[fileno(fp)] = pid; /* remember child pid for this fd */ - return (fp); -} - -int Activator::pclose2(FILE *fp) -{ - int fd, stat; - pid_t pid; - - if (childpid == NULL) - { - return (-1); /* popen() has never been called */ - } - fd = fileno(fp); - if ((pid = childpid[fd]) == 0) - { - return (-1); /* fp wasn't opened by popen() */ - } - childpid[fd] = 0; - if (fclose(fp) == EOF) - { - return (-1); - } - - while (waitpid(pid, &stat, 0) < 0) - { - if (errno != EINTR) - { - return (-1); /* error other than EINTR from waitpid() */ - } - } - return (stat); /* return child's termination status */ -} diff --git a/cpp/framework/NodeServer/Activator.h b/cpp/framework/NodeServer/Activator.h deleted file mode 100644 index abe3b3ee5..000000000 --- a/cpp/framework/NodeServer/Activator.h +++ /dev/null @@ -1,209 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __ACTIVATOR_H_ -#define __ACTIVATOR_H_ -#include "Node.h" -#include -#include "util/tc_file.h" -#include "util/tc_monitor.h" -#include - -using namespace tars; -using namespace std; -//////////////////////////////////////////////////// -// - -//用æ¥æ ‡å¿—è„šæœ¬ç»“æŸ -///////////////////////////////////////////////////////// -// 环境å˜é‡ -struct EnvVal : std::unary_function -{ - string operator()(const std::string& value) - { - string::size_type pos = value.find("="); - if(pos == string::npos || pos >= value.size() - 1) - { - return value; - } - - string v = value.substr(pos + 1); - assert(v.size()); - - string::size_type start = 0; - string::size_type finish; - - //å®æ›¿æ¢ - while((start = v.find("$", start)) != string::npos && start < v.size() - 1) - { - string var; - if(v[start + 1] == '{') - { - finish = v.find("}"); - if(finish == string::npos) - { - break; - } - var = v.substr(start + 2, finish - start - 2); - } - else - { - finish = start + 1; - while((isalnum(v[finish]) || v[finish] == '_') && finish < v.size()) - { - ++finish; - } - var = v.substr(start + 1, finish - start - 1); - --finish; - } - - char* p = getenv(var.c_str()); - string str = p ? string(p) : ""; - - v.replace(start, finish - start + 1, str); - - start += str.size(); - } - - //此处需è¦é©¬ä¸Šè®¾ç½®ï¼Œå¦åˆ™ä»¥ä¸Šå®æ›¿æ¢ä¸­èŽ·å–的环境å˜é‡ä¸ºç©º - setenv((value.substr(0, pos)).c_str(), v.c_str(), true); - - return value.substr(0, pos) + "=" + v; - } -}; - -class Activator : public TC_ThreadLock, public TC_HandleBase -{ -public: - /** - * 构造æœåŠ¡ - * iTimeInterval秒内最多进行iMaxCount次å¯åŠ¨ã€‚ 达到最大å¯åŠ¨æ¬¡æ•°ä»å¤±è´¥åŽiPunishInterval秒é‡è¯•å¯åŠ¨ä¸€æ¬¡ - * @param iTimeInterval - * @param iMaxCount - * @param iPunishInterval 惩罚时间间隔 - * - */ - Activator(int iTimeInterval,int iMaxCount,int iPunishInterval) - : _maxCount(iMaxCount) - , _timeInterval(iTimeInterval) - , _punishInterval(iPunishInterval) - , _termSignal(false) - , _redirectPath("") - { - clearRunntimeData(); - }; - - ~Activator() - { - }; - - /** - * å¯åŠ¨æœåŠ¡ - * - * @param strExePath å¯æ‰§è¡Œæ–‡ä»¶è·¯å¾„ - * @param strPwdPath 当å‰è·¯å¾„ - * @param strRollLogPath 日志路径 - * @param vOptions å¯åŠ¨å‚æ•° - * @param vEnvs 环境å˜é‡ - * @return pid_t 生æˆå­è¿›ç¨‹id - * - */ - pid_t activate(const string& strExePath, const string& strPwdPath, const string &strRollLogPath, const vector& vOptions, vector& vEnvs); - - /** - * 脚本å¯åŠ¨æœåŠ¡ - * - * @param sServerId æœåŠ¡id - * @param strStartScript 脚本路径 - * @param strMonitorScript 脚本路径 - * @param sResult 执行结果 - * @return pid_t 生æˆå­è¿›ç¨‹id - * - */ - pid_t activate(const string &strServerId, const string& strStartScript, const string &strMonitorScript, string &strResult); - - /** - * åœæ­¢æœåŠ¡ - * - * @param pid 进程id - * @return int 0 æˆåŠŸ 其它失败 - */ - int deactivate( int pid ); - - /** - * åœæ­¢æœåŠ¡ 并生æˆcore文件 - * - * @param pid 进程id - * @return int 0 æˆåŠŸ 其它失败 - */ - int deactivateAndGenerateCore( int pid ); - - /** - * å‘é€ä¿¡å· - * - * @param pid 进程id - * @param signal ä¿¡å· - * @return int 0 æˆåŠŸ 其它失败 - */ - int sendSignal( int pid, int signal ) const; -public: - - bool isActivatingLimited (); //å¯åŠ¨é™åˆ¶,用æ¥é˜²æ­¢é—®é¢˜æœåŠ¡ä¸æ–­é‡å¯å½±å“其它æœåŠ¡ - - void addActivatingRecord(); - - //è¿è¡Œè„šæœ¬ - bool doScript(const string &sServerId, const string &strScript, string &strResult, map &mResult,const string &sEndMark = ""); - - map parseScriptResult(const string &strServerId,const string &strResult); - - void setRedirectPath(const string& sRedirectpath) {_redirectPath = sRedirectpath;} - - void clearRunntimeData() - { - _activingRecord.clear(); - _limited = false; - _curCount = 0; - }; - - void setLimitInfo(int iTimeInterval,int iMaxCount,int iPunishInterval) - { - _timeInterval = iTimeInterval; - _maxCount = iMaxCount; - _punishInterval = iPunishInterval; - clearRunntimeData(); - } - -private: - int pclose2(FILE *fp); - FILE* popen2(const char *cmdstring, const char *type); - -private: - vector _activingRecord; //è¿è¡Œæ—¶ - bool _limited; //是å¦å¯åŠ¨å—é™ï¼Œè¿è¡Œæ—¶ - int _curCount; //当å‰å¯åŠ¨æ¬¡æ•°ï¼Œè¿è¡Œæ—¶ - int _maxCount; //最大å¯åŠ¨æ¬¡æ•°,é…ç½® - int _timeInterval; //时间,å•ä½åˆ†é’Ÿ,é…ç½® - int _punishInterval; //惩罚å—é™æ—¶é—´é—´éš”,å•ä½åˆ†é’Ÿ,é…ç½® - -private: - bool _termSignal; //éžtarsæœåŠ¡è„šæœ¬è¿è¡Œè¶…æ—¶åœæ­¢ä¿¡å· - string _redirectPath; //标准输出和错误输出é‡å®šå‘目录 -}; - -typedef TC_AutoPtr ActivatorPtr; -#endif - diff --git a/cpp/framework/NodeServer/BatchPatchThread.cpp b/cpp/framework/NodeServer/BatchPatchThread.cpp deleted file mode 100644 index a1e94325a..000000000 --- a/cpp/framework/NodeServer/BatchPatchThread.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_md5.h" -#include "BatchPatchThread.h" -#include "ServerFactory.h" -#include "CommandPatch.h" -#include "NodeRollLogger.h" -#include "util.h" - -using namespace tars; - -BatchPatch::BatchPatch() -{ - -} - -BatchPatch::~BatchPatch() -{ - terminate(); -} - -void BatchPatch::start(int iNum) -{ - for (int i = 0; i < iNum; i++) - { - BatchPatchThread * t = new BatchPatchThread(this); - t->setPath(_downloadPath); - t->start(); - - _runners.push_back(t); - } -} - -void BatchPatch::terminate() -{ - for (size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->terminate(); - } - } - - for (size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->getThreadControl().join(); - } - } - -} - -void BatchPatch::push_back(const tars::PatchRequest & request, ServerObjectPtr serverPtr) -{ - { - TC_ThreadLock::Lock LockQueue(_queueMutex); - - if (_patchIng.count(request.appname + request.servername) == 1) - { - std::string sException = "reduplicate patch request:" + request.version + "," + request.user; - - NODE_LOG("patchPro")->debug() << FILE_FUN<< request.appname + "." + request.servername << "|" << sException << endl; - throw TC_Exception(sException); - } - - if (!serverPtr) - { - NODE_LOG("patchPro")->error() << FILE_FUN<< "server null " <& item) -{ - TC_ThreadLock::Lock LockQueue(_queueMutex); - - bool bRet=false; - - bRet=_patchQueue.pop_front(item); - - if(bRet) - { - _patchIng.erase(item.first.appname+item.first.servername); - } - - return bRet; -} - -void BatchPatch::timedWait(int millsecond) -{ - TC_ThreadLock::Lock lock(_queueMutex); - - _queueMutex.timedWait(millsecond); -} - -BatchPatchThread::BatchPatchThread(BatchPatch * patch) : _batchPatch(patch) -{ - _shutDown = false; -} - -BatchPatchThread::~BatchPatchThread() -{ - terminate(); -} - -void BatchPatchThread::terminate() -{ - _shutDown = true; - - if (isAlive()) - { - getThreadControl().join(); - } -} - -void BatchPatchThread::doPatchRequest(const tars::PatchRequest & request, ServerObjectPtr server) -{ - NODE_LOG("patchPro")->debug() << FILE_FUN - << request.appname + "." + request.servername + "_" + request.nodename << "|" - << request.groupname << "|" - << request.version << "|" - << request.user << "|" - << request.servertype << "|" - << request.patchobj << "|" - << request.md5 << "|" - << request.ostype << endl; - - - //设置å‘å¸ƒçŠ¶æ€ - try - { - std::string sError; - - if (!server) - { - NODE_LOG("patchPro")->error() <debug() <error() << FILE_FUN<< request.appname + "." + request.servername << "|" << request.md5 << "|canExecute error:" << sError << endl; - return ; - } - - if (command.execute(sError) == 0) - { - NODE_LOG("patchPro")->debug() <error() <error() << FILE_FUN<< request.appname + "." + request.servername << "|" << request.md5 << "|Exception:" << e.what() << endl; - } - catch (...) - { - NODE_LOG("patchPro")->error() < item; - if (_batchPatch->pop_front(item)) - { - if (!(item.second)) - { - NODE_LOG("patchPro")->error() <timedWait(2000); - } - } - catch (exception& e) - { - NODE_LOG("patchPro")->error()<error()<& item); - - /** - * 设置下载暂存目录 - */ - void setPath(const std::string & sDownloadPath) - { - _downloadPath = sDownloadPath; - } -private: - tars::TC_ThreadLock _queueMutex; - - TC_ThreadQueue > _patchQueue; - - std::set _patchIng; - - std::vector _runners; - - std::string _downloadPath; -}; - - -class BatchPatchThread : public TC_Thread -{ -public: - BatchPatchThread(BatchPatch * patch); - - ~BatchPatchThread(); - - virtual void run(); - - void terminate(); - -public: - /** - * 设置下载暂存目录 - */ - void setPath(const std::string & sDownloadPath) - { - _downloadPath = sDownloadPath; - } - - /** - * 执行å‘布å•ä¸ªè¯·æ±‚ - */ - void doPatchRequest(const tars::PatchRequest & request, ServerObjectPtr server); - -protected: - BatchPatch * _batchPatch; - - std::string _downloadPath; //文件下载目录 - -protected: - bool _shutDown; -}; - -#endif diff --git a/cpp/framework/NodeServer/CMakeLists.txt b/cpp/framework/NodeServer/CMakeLists.txt deleted file mode 100644 index c7028c9b6..000000000 --- a/cpp/framework/NodeServer/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ - -include_directories(${PROJECT_SOURCE_DIR}/patchclient) - -link_libraries(patch) - -set(MODULE "tarsnode") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/bin) - -#complice_module(${MODULE} "Node.tars") -complice_module(${MODULE}) - -#tarsnodeä¾èµ–patch的客户端 -add_dependencies(${MODULE} patch) diff --git a/cpp/framework/NodeServer/CommandAddFile.h b/cpp/framework/NodeServer/CommandAddFile.h deleted file mode 100644 index 562a4280a..000000000 --- a/cpp/framework/NodeServer/CommandAddFile.h +++ /dev/null @@ -1,194 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __ADDFILE_COMMAND_H_ -#define __ADDFILE_COMMAND_H_ - -#include "ServerCommand.h" - -/** - *自configæœåŠ¡ä¸‹è½½æ–‡ä»¶ - * - */ -class CommandAddFile : public ServerCommand -{ - -public: - CommandAddFile(const ServerObjectPtr &pServerObjectPtr,const string &sFile,bool bByNode = false); - ExeStatus canExecute(string &sResult); - int execute(string &sResult); -private: - int getScriptFile(string &sResult); - bool _byNode; - string _file; - string _serverId; - ServerDescriptor _desc; - StatExChangePtr _statExChange; - ServerObjectPtr _serverObjectPtr; -}; - -////////////////////////////////////////////////////////////// -// -inline CommandAddFile::CommandAddFile(const ServerObjectPtr &pServerObjectPtr,const string &sFile,bool bByNode) -:_byNode(bByNode) -,_file(sFile) -,_serverObjectPtr(pServerObjectPtr) -{ - _desc = _serverObjectPtr->getServerDescriptor(); - _serverId = _serverObjectPtr->getServerId(); -} - -////////////////////////////////////////////////////////////////////////////// -// -inline ServerCommand::ExeStatus CommandAddFile::canExecute(string &sResult) -{ - - TC_ThreadRecLock::Lock lock( *_serverObjectPtr ); - - TLOGDEBUG("CommandAddFile::canExecute "<<_desc.application<< "." << _desc.serverName << " beging addFilesing------"<< endl); - ServerObject::InternalServerState eState = _serverObjectPtr->getInternalState(); - - if(_desc.application == "" || _desc.serverName == "" ) - { - TLOGERROR("CommandAddFile::canExecute app or server name is empty"<getExePath()+"/"+_file; - } - _file = TC_File::simplifyDirectory(TC_Common::trim(_file)); - - string sFilePath = TC_File::extractFilePath(_file); - - string sFileName = TC_File::extractFileName(_file); - //创建目录 - if(!TC_File::makeDirRecursive(sFilePath ) ) - { - - TLOGERROR("CommandAddFile::execute cannot create dir:"<isScriptFile(sFileName) == true) - { - bRet = getScriptFile(sResult); - } - else - { - TarsRemoteConfig tTarsRemoteConfig; - tTarsRemoteConfig.setConfigInfo(Application::getCommunicator(), ServerConfig::Config,_desc.application, _desc.serverName, sFilePath,_desc.setId); -// tTarsRemoteConfig.setConfigInfo(ServerConfig::Config,_desc.application, _desc.serverName, sFilePath,_desc.setId); - bRet = tTarsRemoteConfig.addConfig(sFileName,sResult); - g_app.reportServer(_serverObjectPtr->getServerId(),sResult); - } - return bRet; - } - catch(exception &e) - { - TLOGERROR("CommandAddFile::execute get file: "<<_file<<" from configServer fail."<stringToProxy(ServerConfig::Config); - string sStream; - int ret; - if(_desc.setId.empty()) - { - ret = pPtr->loadConfig(_desc.application, _desc.serverName, sFileName, sStream); - } - else - { - struct ConfigInfo confInfo; - confInfo.appname = _desc.application; - confInfo.servername = _desc.serverName; - confInfo.filename = sFileName; - confInfo.setdivision = _desc.setId; - ret = pPtr->loadConfigByInfo(confInfo,sStream); - } - - if (ret != 0) - { - sResult = FILE_FUN_STR+"[fail] get remote file :" + sFileName; - TLOGERROR(sResult<decodeMacro(sStream); - string sTem; - string::size_type pos = sStream.find('\n'); - while(pos != string::npos) - { - sTem = sTem + TC_Common::trim(sStream.substr(0,pos)," \r\t") +"\n"; - sStream = sStream.substr(pos+1); - pos = sStream.find('\n'); - } - sStream = sTem + sStream; - string sEndMark = "echo 'end-"+sFileName+"'"; - if(sStream.find(sEndMark) == string::npos) - { - sStream = sStream + "\n"+sEndMark ; //tarsnode 生æˆçš„脚本都以 'echo 'end filename'' ç»“æŸ - } - if(TC_File::isFileExist(_file) && TC_File::load2str(_file) != sStream) - { - TC_File::copyFile(_file,_file+".bak"); - } - ofstream out(_file.c_str()); - if(!out.good()) - { - TLOGERROR("CommandAddFile::getScriptFile cannot create file: "<<(_file + " erro:"+ strerror(errno))<getServerId(),sResult); - TLOGDEBUG(sResult<getServerDescriptor(); -} -////////////////////////////////////////////////////////////// -// -inline ServerCommand::ExeStatus CommandDestroy::canExecute(string &sResult) -{ - TC_ThreadRecLock::Lock lock( *_serverObjectPtr ); - - NODE_LOG("destroyServer")->debug()<getInternalState(); - - if (eState != ServerObject::Inactive) - { - sResult = FILE_FUN_STR+"server state is not Inactive. the curent state is " + _serverObjectPtr->toStringState( eState ); - NODE_LOG("destroyServer")->debug()<getServerDir(); - string sServerId = _serverObjectPtr->getServerId(); - - //删除æœåŠ¡æ—¥å¿—目录 - string sLogPath = _serverObjectPtr->getLogPath(); - if (sLogPath.empty()) - {//使用默认路径 - sLogPath = "/usr/local/app/tars/app_log/"; - } - - vector vtServerNames = TC_Common::sepstr(sServerId,"."); - if (vtServerNames.size() != 2) - { - NODE_LOG("destroyServer")->error() <push_back(sLogPath); - - //异步删除data下文件或者å­ç›®å½• - if(TC_File::isFileExistEx(sServerDir, S_IFDIR)) - { - g_RemoveLogThread->push_back(sServerDir); - NODE_LOG("destroyServer")->error()<error()<error()<error()<& macro, const string& value); - - /** - * 获å–serveré…置文件 - * @return int - */ - void getRemoteConf(); - -private: - bool _byNode; - NodeInfo _nodeInfo; - ServerDescriptor _desc; - ServerObjectPtr _serverObjectPtr; - StatExChangePtr _statExChange; - -private: - string _serverDir; //æœåŠ¡æ•°æ®ç›®å½• - string _confPath; //æœåŠ¡é…置文件目录 - string _confFile; //æœåŠ¡é…置文件目录 _strConfPath/conf - string _exePath; //æœåŠ¡å¯æ‰§è¡Œç¨‹åºç›®å½•ã€‚å¯ä¸ªæ€§è®¾å®š,一般为_strServerDir/bin - string _exeFile; //一般为_strExePath+_strServerName å¯ä¸ªæ€§æŒ‡å®š - string _logPath; //æœåŠ¡æ—¥å¿—目录 - string _libPath; //动æ€åº“目录 一般为_desc.basePath/lib - string _serverType; //æœåŠ¡ç±»åž‹ -private: - string _startScript; //å¯åŠ¨è„šæœ¬ - string _stopScript; //åœæ­¢è„šæœ¬ - string _monitorScript; //监控脚本 -}; - -////////////////////////////////////////////////////////////// -// -CommandLoad::CommandLoad(const ServerObjectPtr& pServerObjectPtr, const NodeInfo& tNodeInfo, bool bByNode) -: _byNode(bByNode) -, _nodeInfo(tNodeInfo) -, _serverObjectPtr(pServerObjectPtr) -{ - _desc = _serverObjectPtr->getServerDescriptor(); -} - -////////////////////////////////////////////////////////////// -// -inline ServerCommand::ExeStatus CommandLoad::canExecute(string& sResult) -{ - - TC_ThreadRecLock::Lock lock(*_serverObjectPtr); - - TLOGDEBUG("CommandLoad::canExecute " << _desc.application << "." << _desc.serverName << "|" << _desc.setId << "| beging loaded------|" << endl); - - ServerObject::InternalServerState eState = _serverObjectPtr->getInternalState(); - - if (_desc.application == "" || _desc.serverName == "") - { - TLOGDEBUG("CommandLoad::canExecute app or server name is empty"<< endl); - return DIS_EXECUTABLE; - } - - if (_serverObjectPtr->toStringState(eState).find("ing") != string::npos && eState != ServerObject::Activating) - { - TLOGDEBUG("CommandLoad::canExecute cannot loading the config, the server state is "<<_serverObjectPtr->toStringState(eState)<< endl); - return DIS_EXECUTABLE; - } - - //设当å‰çŠ¶æ€ä¸ºæ­£åœ¨loading - _statExChange = new StatExChange(_serverObjectPtr, ServerObject::Loading, eState); - - return EXECUTABLE; -} - - -////////////////////////////////////////////////////////////// -// -inline int CommandLoad::execute(string& sResult) -{ - //åˆå§‹åŒ–æœåŠ¡ç›®å½•ä¿¡æ¯ - _serverDir = TC_Common::trim(_desc.basePath); - _exePath = TC_Common::trim(_desc.exePath); - _serverType = TC_Common::lower(TC_Common::trim(_desc.serverType)); - - //è‹¥serverDirä¸åˆæ³•é‡‡ç”¨é»˜è®¤è·¯å¾„ - if (_serverDir.empty() || TC_File::isAbsolute(_serverDir) == false) - { - _serverDir = TC_File::simplifyDirectory(_nodeInfo.dataDir + "/" + _desc.application + "." + _desc.serverName); - } - - //è‹¥exePathä¸åˆæ³•é‡‡ç”¨é»˜è®¤è·¯å¾„ - //注æ„javaæœåŠ¡å¯åŠ¨æ–¹å¼ç‰¹æ®Š å¯æ‰§è¡Œæ–‡ä»¶ä¸ºjava é¡»ç‰¹æ®Šå¤„ç† - if (_exePath.empty()) - { - _exePath = _serverDir + "/bin/"; - if (_serverType == "tars_java") - { - _exeFile = "java"; - } - else - { - _exeFile = _exePath + _desc.serverName; - } - } - else if (TC_File::isAbsolute(_exePath) == false) - { - //此时_desc.exePath为手工指定,手工指定时_desc.exePath为文件 所以路径è¦æ‰£é™¤å¯æ‰§è¡Œæ–‡ä»¶å - //而且å¯æ‰§è¡Œæ–‡ä»¶åå¯ä»¥ä¸ç­‰äºŽ_strServerName 用æ¥ç”¨åŒä¸€å¯æ‰§è¡Œæ–‡ä»¶ï¼Œä¸åŒé…ç½®å¯åŠ¨å¤šä¸ªæœåŠ¡ - _exeFile = _serverDir + "/bin/" + _exePath; - _exePath = TC_File::extractFilePath(_exeFile); - } - else - { - //此时_desc.exePath为手工指定,手工指定时_desc.exePath为文件 所以路径è¦æ‰£é™¤å¯æ‰§è¡Œæ–‡ä»¶å - //而且å¯æ‰§è¡Œæ–‡ä»¶åå¯ä»¥ä¸ç­‰äºŽ_strServerName 用æ¥ç”¨åŒä¸€å¯æ‰§è¡Œæ–‡ä»¶ï¼Œä¸åŒé…ç½®å¯åŠ¨å¤šä¸ªæœåŠ¡ - _exeFile = _desc.exePath; - _exePath = _serverType == "tars_java" ? _serverDir + "/bin/" : TC_File::extractFilePath(_desc.exePath); - } - - _exeFile = TC_File::simplifyDirectory(_exeFile); - - _exePath = TC_File::simplifyDirectory(_exePath) + "/"; - - //å¯åŠ¨è„šæœ¬å¤„ç† - _startScript = TC_Common::trim(_desc.startScript); - if (!_startScript.empty() && TC_File::isAbsolute(_startScript) == false) - { - _startScript = _exePath + _startScript; - } - - //åœæ­¢è„šæœ¬å¤„ç† - _stopScript = TC_Common::trim(_desc.stopScript); - if (!_stopScript.empty() && TC_File::isAbsolute(_stopScript) == false) - { - _stopScript = _exePath + _stopScript; - } - - //ç›‘æŽ§è„šæœ¬å¤„ç† - _monitorScript = TC_Common::trim(_desc.monitorScript); - if (!_monitorScript.empty() && TC_File::isAbsolute(_monitorScript) == false) - { - _monitorScript = _exePath + _monitorScript; - } - - _startScript = TC_File::simplifyDirectory(_startScript); - _stopScript = TC_File::simplifyDirectory(_stopScript); - _monitorScript = TC_File::simplifyDirectory(_monitorScript); - - //创建é…ç½®lib文件目录 - _libPath = _nodeInfo.dataDir + "/lib/"; - - //获å–æœåŠ¡æ¡†æž¶é…置文件 - _confPath = _serverDir + "/conf/"; - - _confFile = _confPath + _desc.application + "." + _desc.serverName + ".config.conf"; - - TLOGDEBUG("CommandLoad::execute"<< _serverType << "," - << "exe_path=" << _exePath << "," - << "exe_file=" << _exeFile << "," - << "start_script=" << _startScript << "," - << "stop_script=" << _stopScript << "," - << "monitor_script="<< _monitorScript<< "," - << "config_file=" << _confFile << endl); - - //创建目录 - if (!TC_File::makeDirRecursive(_exePath)) - { - - TLOGERROR("CommandLoad::execute cannot create dir: "<<(_exePath + " erro:" + strerror(errno))< m; - - m["node"] = ServerConfig::Application + "." + ServerConfig::ServerName + ".ServerObj@" + g_app.getAdapterEndpoint("ServerAdapter").toString(); - tConf.insertDomainParam("/tars/application/server", m, true); - m.clear(); - - map::const_reverse_iterator itAdapters; - for (itAdapters = _desc.adapters.rbegin(); itAdapters != _desc.adapters.rend(); itAdapters++) - { - TLOGINFO("CommandLoad::updateConfigFile get adapter " << itAdapters->first << endl); - if (LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - _desc.display(LOG->info()); - } - - if (itAdapters->first == "") - { - continue; - } - - tEp.parse(itAdapters->second.endpoint); - m["endpoint"] = tEp.toString(); - m["allow"] = itAdapters->second.allowIp; - m["queuecap"] = TC_Common::tostr(itAdapters->second.queuecap); - m["queuetimeout"] = TC_Common::tostr(itAdapters->second.queuetimeout); - m["maxconns"] = TC_Common::tostr(itAdapters->second.maxConnections); - m["threads"] = TC_Common::tostr(itAdapters->second.threadNum); - m["servant"] = TC_Common::tostr(itAdapters->second.servant); - m["protocol"] = itAdapters->second.protocol == "" ? "tars" : itAdapters->second.protocol; - m["handlegroup"] = itAdapters->second.handlegroup == "" ? itAdapters->first : itAdapters->second.handlegroup; - - tConf.insertDomainParam("/tars/application/server/" + itAdapters->first, m, true); - } - - //获å–本地socket - TC_Endpoint tLocalEndpoint; - uint16_t p = tEp.getPort(); - if (p == 0) - { - try - { - //原始é…置文件中有admin端å£äº†, 直接使用 - TC_Config conf; - conf.parseFile(_confFile); - TC_Endpoint ep; - ep.parse(conf.get("/tars/application/server")); - p = ep.getPort(); - } - catch (exception& ex) - { - //éšæœºåˆ†é…ä¸€ä¸ªç«¯å£ - TC_Socket t; - t.createSocket(); - t.bind("127.0.0.1", 0); - string d; - t.getSockName(d, p); - t.close(); - } - - } - - tLocalEndpoint.setPort(p); - tLocalEndpoint.setHost("127.0.0.1"); - tLocalEndpoint.setTcp(true); - tLocalEndpoint.setTimeout(3000); - - //需è¦å®æ›¿æ¢éƒ¨åˆ†é…ç½® - TC_Config tConfMacro; - map mMacro; - mMacro.clear(); - mMacro["locator"] = Application::getCommunicator()->getProperty("locator"); - - //>>修改æˆä»Žä¸»æŽ§èŽ·å–locatoråœ°å€ - vector activeEp; - vector inactiveEp; - QueryFPrx queryProxy = AdminProxy::getInstance()->getQueryProxy(); - int iRet = 0; - try - { - iRet = queryProxy->findObjectById4All(AdminProxy::getInstance()->getQueryProxyName(), activeEp, inactiveEp); - TLOGDEBUG("CommandLoad::updateConfigFile " << _serverObjectPtr->getServerId() << "|iRet|" << iRet << "|" << activeEp.size() << "|" << inactiveEp.size() << endl); - } - catch (exception& e) - { //获å–主控地å€å¼‚常时,ä»ä½¿ç”¨node中的locator - TLOGERROR("CommandLoad::updateConfigFile:get registry locator excetpion:" << e.what() << "|" << _serverObjectPtr->getServerId() << endl); - iRet = -1; - } - catch (...) - { - TLOGERROR("CommandLoad::updateConfigFile:get registry locator unknown exception|" << _serverObjectPtr->getServerId() << endl); - iRet = -1; - } - - if (iRet == 0 && activeEp.size() > 0) - { - string sLocator = AdminProxy::getInstance()->getQueryProxyName() + "@"; - for (size_t i = 0; i < activeEp.size(); ++i) - { - string sSingleAddr = "tcp -h " + activeEp[i].host + " -p " + TC_Common::tostr(activeEp[i].port); - sLocator += sSingleAddr + ":"; - } - sLocator = sLocator.substr(0, sLocator.length() - 1); - mMacro["locator"] = sLocator; - TLOGDEBUG("CommandLoad::updateConfigFile:" << _serverObjectPtr->getServerId() << "|locator|" << sLocator << endl); - } - - mMacro["modulename"] = _desc.application + "." + _desc.serverName; - mMacro["app"] = _desc.application; - mMacro["server"] = _desc.serverName; - mMacro["serverid"] = _serverObjectPtr->getServerId(); - mMacro["localip"] = g_app.getAdapterEndpoint("ServerAdapter").getHost(); - mMacro["exe"] = TC_File::simplifyDirectory(_exeFile); - mMacro["basepath"] = TC_File::simplifyDirectory(_exePath) + "/"; - mMacro["datapath"] = TC_File::simplifyDirectory(_serverDir) + "/data/"; - mMacro["logpath"] = ServerConfig::LogPath; - mMacro["local"] = tLocalEndpoint.toString(); - - mMacro["mainclass"] = "com.qq." + TC_Common::lower(_desc.application) + "." + TC_Common::lower(_desc.serverName) + "." + _desc.serverName; - mMacro["config-center-port"] = TC_Common::tostr(_desc.configCenterPort); - - mMacro["setdivision"] = _desc.setId; - - mMacro["enableset"] = "n"; - if (!mMacro["setdivision"].empty()) - { - mMacro["enableset"] = "y"; - } - else - { - mMacro["setdivision"] = "NULL"; - } - - mMacro["asyncthread"] = TC_Common::tostr(_desc.asyncThreadNum); - - //创建目录 - TC_File::makeDirRecursive(mMacro["basepath"]); - TC_File::makeDirRecursive(mMacro["datapath"]); - TC_File::makeDirRecursive(_logPath + "/" + _desc.application + "/" + _desc.serverName + "/"); - - //åˆå¹¶ä¸¤ç±»é…ç½® - _serverObjectPtr->setMacro(mMacro); - - string strProfile = _serverObjectPtr->decodeMacro(_desc.profile); - tConfMacro.parseString(strProfile); - tConf.joinConfig(tConfMacro, true); - - string sStream = TC_Common::replace(tConf.tostr(), "\\s", " "); - string sConfigFileBak = _confFile + ".bak"; - if (TC_File::isFileExist(_confFile) && TC_File::load2str(_confFile) != sStream) - { - TC_File::copyFile(_confFile, sConfigFileBak); - } - - ofstream configfile(_confFile.c_str()); - if (!configfile.good()) - { - TLOGERROR("CommandLoad::updateConfigFile cannot create configuration file: " << _confFile << endl); - return -1; - } - - configfile << sStream; - configfile.close(); - - _logPath = tConf.get("/tars/application/server", ""); - - _serverObjectPtr->setJvmParams(tConf.get("/tars/application/server", "")); - _serverObjectPtr->setMainClass(tConf.get("/tars/application/server", "")); - _serverObjectPtr->setClassPath(tConf.get("/tars/application/server", "")); - _serverObjectPtr->setEnv(tConf.get("/tars/application/server", "")); - _serverObjectPtr->setHeartTimeout(TC_Common::strto(tConf.get("/tars/application/server", ""))); - - _serverObjectPtr->setRedirectPath(tConf.get("/tars/application/", "")); - - _serverObjectPtr->setBackupFileNames(tConf.get("/tars/application/server", "classes/autoconf")); - - bool bEn = (TC_Common::lower(tConf.get("/tars/application/server", "y")) == "y") ? true : false; - - _serverObjectPtr->setEnSynState(bEn); - - _serverObjectPtr->setExeFile(_exeFile); - _serverObjectPtr->setConfigFile(_confFile); - _serverObjectPtr->setExePath(_exePath); - _serverObjectPtr->setLogPath(_logPath); - _serverObjectPtr->setLibPath(_libPath); - _serverObjectPtr->setServerDir(_serverDir); - _serverObjectPtr->setNodeInfo(_nodeInfo); - - //å–åˆå¹¶åŽé…ç½® - TC_Endpoint localEp; - localEp.parse(tConf.get("/tars/application/server")); - _serverObjectPtr->setLocalEndpoint(localEp); - - _serverObjectPtr->setServerType(_serverType); - _serverObjectPtr->setScript(_startScript, _stopScript, _monitorScript); - _serverObjectPtr->setLoaded(true); - - return 0; - } - catch (exception& e) - { - sResult = e.what(); - TLOGERROR("CommandLoad::updateConfigFile "<getServerId() == "tars.tarsconfig") - { - return ; - } - string sResult; - try - { - ConfigPrx pPtr = Application::getCommunicator()->stringToProxy(ServerConfig::Config); - vector vf; - int ret; - - if (_desc.setId.empty()) - { - ret = pPtr->ListConfig(_desc.application, _desc.serverName, vf); - } - else - { - struct ConfigInfo confInfo; - confInfo.appname = _desc.application; - confInfo.servername = _desc.serverName; - confInfo.setdivision = _desc.setId; - ret = pPtr->ListConfigByInfo(confInfo, vf); - } - - if (ret != 0) - { - TLOGERROR("CommandLoad::getRemoteConf [fail] get remote file list"<< endl); - g_app.reportServer(_serverObjectPtr->getServerId(), sResult); - } - - for (unsigned i = 0; i < vf.size(); i++) - { - //è„šæœ¬æ‹‰å– éœ€è¦å®æ›¿æ¢ - if (_serverObjectPtr->isScriptFile(vf[i]) == true) - { - _serverObjectPtr->getRemoteScript(vf[i]); - continue; - } - - //éžtarsæœåŠ¡é…置文件需è¦nodeæ‹‰å– tarsæœåŠ¡é…ç½®æœåŠ¡å¯åŠ¨æ—¶è‡ªå·±æ‹‰å– - if (_serverObjectPtr->isTarsServer() != true) - { - - TarsRemoteConfig tTarsRemoteConfig; - tTarsRemoteConfig.setConfigInfo(Application::getCommunicator(),ServerConfig::Config,_desc.application, _desc.serverName, _exePath,_desc.setId); - tTarsRemoteConfig.addConfig(vf[i], sResult); - g_app.reportServer(_serverObjectPtr->getServerId(), sResult); - } - } - } - catch (exception& e) - { - TLOGERROR("CommandLoad::getRemoteConf " << e.what() << endl); - } -} - -#endif diff --git a/cpp/framework/NodeServer/CommandNotify.h b/cpp/framework/NodeServer/CommandNotify.h deleted file mode 100644 index 2572de519..000000000 --- a/cpp/framework/NodeServer/CommandNotify.h +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __NOTIFY_COMMAND_H_ -#define __NOTIFY_COMMAND_H_ - -#include "ServerCommand.h" -#include "NodeRollLogger.h" - -/** - * 通知æœåŠ¡ - * - */ -class CommandNotify : public ServerCommand -{ -public: - CommandNotify(const ServerObjectPtr &pServerObjectPtr,const string &sMsg,bool bByNode = false); - - ExeStatus canExecute(string &sResult); - - int execute(string &sResult); - -private: - bool _byNode; - string _msg; - ServerDescriptor _desc; - ServerObjectPtr _serverObjectPtr; -}; - -////////////////////////////////////////////////////////////// -// -inline CommandNotify::CommandNotify(const ServerObjectPtr &pServerObjectPtr,const string &sMsg,bool bByNode) -:_byNode(bByNode) -,_msg(sMsg) -,_serverObjectPtr(pServerObjectPtr) -{ - _desc = _serverObjectPtr->getServerDescriptor(); -} -////////////////////////////////////////////////////////////// -// -inline ServerCommand::ExeStatus CommandNotify::canExecute(string &sResult) -{ - NODE_LOG("notify")->debug()<getInternalState(); - - if ( eState == ServerObject::Inactive) - { - sResult = "server state is Inactive. "; - NODE_LOG("notify")->debug()<debug()<getLocalEndpoint().toString(); - pAdminPrx = Application::getCommunicator()->stringToProxy(sAdminPrx); - sResult = pAdminPrx->notify(_msg); - } - catch (exception& e) - { - NODE_LOG("notify")->debug()< 0) - { - _serverObjectPtr->setPatchPercent((int)(pos*100.0/fi.size)); - } - } - -private: - ServerObjectPtr _serverObjectPtr; -}; - -////////////////////////////////////////////////////////////// -// -inline CommandPatch::CommandPatch(const ServerObjectPtr & server, const std::string & sDownloadPath, const tars::PatchRequest & request) -: _serverObjectPtr(server), _patchRequest(request) -{ - _localTgzBasePath = sDownloadPath + "/BatchPatchingLoad"; - _localExtractBasePath = sDownloadPath + "/BatchPatching"; -} - -inline string CommandPatch::getOsType() -{ - string defaultOs = g_pconf->get("/tars/node", "suse"); - string ret = defaultOs; - - vector vKey = g_pconf->getDomainKey("/tars/node/os_pattern"); - NODE_LOG("patchPro")->debug() << FILE_FUN<< vKey.size() << endl; - for(size_t i = 0; i < vKey.size(); i++) - { - string key = vKey[i]; - string paths = g_pconf->get("/tars/node/os_pattern<" + key + ">", ""); - NODE_LOG("patchPro")->debug() < vPath = TC_Common::sepstr(paths,":"); - vector::iterator it = vPath.begin(); - for(; it != vPath.end(); it ++) - { - string path = *it; - if(TC_File::isFileExist(path)) - { - ret = key; - NODE_LOG("patchPro")->debug() << FILE_FUN<< path << " is exist, ret:" << ret << endl; - return ret; - } - else - { - NODE_LOG("patchPro")->debug() << FILE_FUN<< path << " not exist" << endl; - } - } - } - - return ret; -} - -inline bool CommandPatch::checkOsType() -{ - string checkStr = g_pconf->get("/tars/node", "n"); - if(checkStr == "Y" || checkStr == "y" || checkStr == "yes" || checkStr == "true") - { - return true; - } - else - { - return false; - } -} - - -inline ServerCommand::ExeStatus CommandPatch::canExecute(string & sResult) -{ - ServerObject::InternalServerState eState = _serverObjectPtr->getLastState(); - - ServerObject::InternalServerState eCurrentState =_serverObjectPtr->getInternalState(); - - NODE_LOG("patchPro")->debug()<< FILE_FUN <<_patchRequest.appname+"."+_patchRequest.servername<<"|sResult:"<toStringState(eCurrentState)<<"|last state :"<< _serverObjectPtr->toStringState(eState)<< endl; - - _statExChange = new StatExChange(_serverObjectPtr, ServerObject::BatchPatching, eState); - - _serverObjectPtr->setPatchVersion(_patchRequest.version); - _serverObjectPtr->setPatchPercent(0); - _serverObjectPtr->setPatchResult(""); - - return EXECUTABLE; -} - -inline int CommandPatch::updatePatchResult(string & sResult) -{ - try - { - NODE_LOG("patchPro")->debug() <getRegistryProxy(); - if (!proxy) - { - sResult = "patch succ but update version and user fault, get registry proxy fail"; - - NODE_LOG("patchPro")->error() <updatePatchResult(patch); - NODE_LOG("patchPro")->debug() <error() <error() <debug() <debug() <debug() <debug() <error() <stringToProxy(_patchRequest.patchobj); - proxy->tars_timeout(60000); - - int downloadRet = SingleFileDownloader::download(proxy, sRemoteTgzFile, dtask.sLocalTgzFile, eventPtr, sResult); - if(downloadRet != 0) - { - //返回ç é”™å¼€ä¸€ä¸‹ - iRet = downloadRet - 100; - returned = true; - } - } - catch (std::exception & ex) - { - sResult = "download from patch exception," + string(ex.what()); - iRet = -3; - NODE_LOG("patchPro")->error() <error() <debug() <error() <error() <error() <error() <error() <getConfigFile()); - string packageFormat= conf.get("/tars/application/server","war"); - string cmd,sLocalTgzFile_bak; - if (_serverObjectPtr->getServerType() == "tars_java") //如果是tars_java,使用war 方法 - { - //NODE_LOG("patchPro")->error() <<"tgz file name"< files; - tars::TC_File::listDirectory(sLocalExtractPach, files, true); - if(files.empty()) - { - sResult = cmd + ",error!"; - NODE_LOG("patchPro")->error() <debug() << FILE_FUN << "unzip:" << cmd <debug() <error() << FILE_FUN << sResult << endl; - iRet = -7; - } - } - } - - //对于tars_nodejs需è¦å…ˆåˆ é™¤exepath下的文件 - if (iRet == 0) - { - if (_serverObjectPtr->getServerType() == "tars_nodejs") - { - if(TC_File::removeFile(_serverObjectPtr->getExePath(),true) != 0 || !TC_File::makeDirRecursive(_serverObjectPtr->getExePath())) - { - iRet = -8; //iRetCode = EM_ITEM_PERMISSION; - NODE_LOG("patchPro")->error() <getExePath()<<"|"<getServerType() == "tars_nodejs") - { - TC_File::copyFile(sLocalExtractPach + "/" + sServerName+ "/" + sServerName, _serverObjectPtr->getExePath(), true); - } - else - { - //如果出错,这里会抛异常 - if(packageFormat!="jar") - { - TC_File::copyFile(sLocalExtractPach + "/" + sServerName, _serverObjectPtr->getExePath(), true); - } - else - { - // TC_File::copyFile( sLocalTgzFile_bak , _serverObjectPtr->getExePath(), true); - string cpCmd =" cp -f "+sLocalTgzFile_bak+" "+_serverObjectPtr->getExePath()+";"; - system(cpCmd.c_str()); - } - } - - //设置å‘布状æ€åˆ°ä¸»æŽ§ - iRet = updatePatchResult(sResult); - if(0 != iRet) - { - NODE_LOG("patchPro")->error() <debug() <debug() <error() <error() <setPatchResult(sResult, iRet == 0); - - //设置å‘布结果,如果是å‘布异常了这里也设置,主è¦æ˜¯è®¾ç½®è¿›åº¦ä¸º100%,方便å‰ç«¯åˆ¤æ–­ - _serverObjectPtr->setPatched(true); - - //å‘布åŽï¼Œcore频率é™åˆ¶é‡æ–°è®¡ç®— - _serverObjectPtr->resetCoreInfo(); - - g_app.reportServer(_patchRequest.appname + "." + _patchRequest.servername, sResult); - - return iRet; -} - -inline int CommandPatch::backupfiles(std::string &sResult) -{ - try - { - if (_serverObjectPtr->getServerType() == "tars_java") //如果是javaæœåŠ¡çš„è¯éœ€è¦æ¸…空目录,备份目录 - { - int maxbaknum = 5; - string srcPath = "/usr/local/app/tars/tarsnode/data/" + _patchRequest.appname + "." + _patchRequest.servername + "/"; - string destPath = "/usr/local/app/tars/tarsnode/tmp/" + _patchRequest.appname + "." + _patchRequest.servername + "/"; - - if (!TC_File::isFileExistEx(srcPath, S_IFDIR)) //ä¸å­˜åœ¨è¿™ä¸ªç›®å½•ï¼Œå…ˆåˆ›å»º - { - NODE_LOG("patchPro")->debug() <= 1; i--) - { - string destPathBak = + "bin_bak" + TC_Common::tostr(i) + "/"; - if(!TC_File::isFileExistEx(destPathBak,S_IFDIR)) //ä¸å­˜åœ¨è¿™ä¸ªç›®å½•ï¼Œå¯ä»¥ç»§ç»­å¾ªçŽ¯ - { - NODE_LOG("patchPro")->debug() <bak2 - { - string newFile = destPath + "bin_bak" + TC_Common::tostr(i+1) + "/"; - if(TC_File::isFileExistEx(newFile,S_IFDIR) && TC_File::removeFile(newFile, true) != 0) - { - NODE_LOG("patchPro")->error() <debug() <debug() <error() <debug() <debug() <debug() <debug() < vFileNames = TC_Common::sepstr(_serverObjectPtr->getBackupFileNames(), ";|"); - for(vector::size_type i = 0;i< vFileNames.size();i++) - { - string sBackupSrc = destPathBak + vFileNames[i]; - string sBackupDest = existFile + vFileNames[i]; - if (TC_File::isFileExistEx(sBackupSrc,S_IFDIR)) - { - if (!TC_File::isFileExistEx(sBackupDest,S_IFDIR)) - { - if(!TC_File::makeDirRecursive(TC_File::simplifyDirectory(sBackupDest))) - { - NODE_LOG("patchPro")->error() <debug() <error() <error() <getExeFile(); - _logPath = _serverObjectPtr->getLogPath(); - _desc = _serverObjectPtr->getServerDescriptor(); -} -////////////////////////////////////////////////////////////// -// -inline ServerCommand::ExeStatus CommandStart::canExecute(string& sResult) -{ - - TC_ThreadRecLock::Lock lock(*_serverObjectPtr); - - NODE_LOG("startServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " beging activate------|byRegistry|" << _byNode << endl; - - ServerObject::InternalServerState eState = _serverObjectPtr->getInternalState(); - - if (_byNode) - { - _serverObjectPtr->setEnabled(true); - } - else if (!_serverObjectPtr->isEnabled()) - { - sResult = "server is disabled"; - NODE_LOG("startServer")->debug() << FILE_FUN << sResult << endl; - return DIS_EXECUTABLE; - } - - if (eState == ServerObject::Active || eState == ServerObject::Activating) - { - _serverObjectPtr->synState(); - sResult = "server is already " + _serverObjectPtr->toStringState(eState); - NODE_LOG("startServer")->debug() << FILE_FUN << sResult << endl; - return NO_NEED_EXECUTE; - } - - if (eState != ServerObject::Inactive) - { - sResult = "server state is not Inactive. the curent state is " + _serverObjectPtr->toStringState(eState); - NODE_LOG("startServer")->debug() << FILE_FUN << sResult << endl; - return DIS_EXECUTABLE; - } - - if (TC_File::isAbsolute(_exeFile) && - !TC_File::isFileExistEx(_exeFile) && - _serverObjectPtr->getStartScript().empty() && - _serverObjectPtr->getServerType() != "tars_nodejs" && _serverObjectPtr->getServerType() != "tars_php") - { - _serverObjectPtr->setPatched(false); - sResult = "The server exe patch " + _exeFile + " is not exist."; - NODE_LOG("startServer")->debug() << FILE_FUN << sResult << endl; - return DIS_EXECUTABLE; - } - - _serverObjectPtr->setPatched(true); - - _serverObjectPtr->setState(ServerObject::Activating, false); //此时ä¸é€šçŸ¥regisrty。checkpidæˆåŠŸåŽå†é€šçŸ¥ - - return EXECUTABLE; -} - -inline bool CommandStart::startByScript(string& sResult) -{ - pid_t iPid = -1; - bool bSucc = false; - string sStartScript = _serverObjectPtr->getStartScript(); - string sMonitorScript = _serverObjectPtr->getMonitorScript(); - - string sServerId = _serverObjectPtr->getServerId(); - iPid = _serverObjectPtr->getActivator()->activate(sServerId, sStartScript, sMonitorScript, sResult); - - vector vtServerName = TC_Common::sepstr(sServerId, "."); - if (vtServerName.size() != 2) - { - sResult = sResult + "|failed to get pid for " + sServerId + ",server id error"; - NODE_LOG("startServer")->error() << FILE_FUN << sResult << endl; - throw runtime_error(sResult); - } - - //默认使用å¯åŠ¨è„šæœ¬è·¯å¾„ - string sFullExeFileName = TC_File::extractFilePath(sStartScript) + vtServerName[1]; - if (!TC_File::isFileExist(sFullExeFileName)) - { - NODE_LOG("startServer")->error() << FILE_FUN << "file name " << sFullExeFileName << " error, use exe file" << endl; - - //使用exe路径 - sFullExeFileName = _serverObjectPtr->getExeFile(); - if (!TC_File::isFileExist(sFullExeFileName)) - { - sResult = sResult + "|failed to get full exe file name|" + sFullExeFileName; - NODE_LOG("startServer")->error() << FILE_FUN << sResult << endl; - throw runtime_error(sResult); - } - } - time_t tNow = TNOW; - int iStartWaitInterval = START_WAIT_INTERVAL; - try - { - //æœåŠ¡å¯åŠ¨,超时时间自己定义的情况 - TC_Config conf; - conf.parseFile(_serverObjectPtr->getConfigFile()); - iStartWaitInterval = TC_Common::strto(conf.get("/tars/application/server", "3000")) / 1000; - if (iStartWaitInterval < START_WAIT_INTERVAL) - { - iStartWaitInterval = START_WAIT_INTERVAL; - } - if (iStartWaitInterval > 60) - { - iStartWaitInterval = 60; - } - - } - catch (...) - { - } - - string sPidFile = "/usr/local/app/tars/tarsnode/util/" + sServerId + ".pid"; - string sGetServerPidScript = "ps -ef | grep -v 'grep' |grep -iE ' " + sFullExeFileName + " '| awk '{print $2}' > " + sPidFile; - - while ((TNOW - iStartWaitInterval) < tNow) - { - //注æ„:由于是守护进程,ä¸è¦å¯¹sytem返回值进行判断,始终waitä¸åˆ°å­è¿›ç¨‹ - system(sGetServerPidScript.c_str()); - string sPid = TC_Common::trim(TC_File::load2str(sPidFile)); - if (TC_Common::isdigit(sPid)) - { - iPid = TC_Common::strto(sPid); - _serverObjectPtr->setPid(iPid); - if (_serverObjectPtr->checkPid() == 0) - { - bSucc = true; - break; - } - } - - NODE_LOG("startServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " activating usleep " << int(iStartWaitInterval) << endl; - usleep(START_SLEEP_INTERVAL); - } - - if (_serverObjectPtr->checkPid() != 0) - { - sResult = sResult + "|get pid for server[" + sServerId + "],pid is not digit"; - NODE_LOG("startServer")->error() << FILE_FUN << sResult << endl; - if (TC_File::isFileExist(sPidFile)) - { - TC_File::removeFile(sPidFile, false); - } - throw runtime_error(sResult); - } - - if (TC_File::isFileExist(sPidFile)) - { - TC_File::removeFile(sPidFile, false); - } - return bSucc; -} - -inline bool CommandStart::startByScriptPHP(string& sResult) -{ - //生æˆå¯åŠ¨shell脚本 - string sConfigFile = _serverObjectPtr->getConfigFile(); - string sLogPath = _serverObjectPtr->getLogPath(); - string sServerDir = _serverObjectPtr->getServerDir(); - string sLibPath = _serverObjectPtr->getLibPath(); - string sExePath = _serverObjectPtr->getExePath(); - string sLogRealPath = sLogPath + _desc.application +"/" + _desc.serverName + "/" ; - string sLogRealPathFile = sLogRealPath +_serverObjectPtr->getServerId() +".log"; - - TC_Config conf; - conf.parseFile(sConfigFile); - string phpexecPath = ""; - string entrance = ""; - try{ - phpexecPath = TC_Common::strto(conf["/tars/application/server"]); - entrance = TC_Common::strto(conf["/tars/application/server"]); - } catch (...){} - entrance = entrance=="" ? sServerDir+"/bin/src/index.php" : entrance; - - std::ostringstream osStartStcript; - osStartStcript << "#!/bin/sh" << std::endl; - osStartStcript << "if [ ! -d \"" << sLogRealPath << "\" ];then mkdir -p \"" << sLogRealPath << "\"; fi" << std::endl; - osStartStcript << phpexecPath << " " << entrance <<" --config=" << sConfigFile << " start >> " << sLogRealPathFile << " 2>&1 " << std::endl; - osStartStcript << "echo \"end-tars_start.sh\"" << std::endl; - - TC_File::save2file(sExePath + "/tars_start.sh", osStartStcript.str()); - TC_File::setExecutable(sExePath + "/tars_start.sh", true); - - pid_t iPid = -1; - bool bSucc = false; - - string sStartScript = _serverObjectPtr->getStartScript(); - sStartScript = sStartScript=="" ? _serverObjectPtr->getExePath() + "/tars_start.sh" : sStartScript; - string sMonitorScript = _serverObjectPtr->getMonitorScript(); - string sServerId = _serverObjectPtr->getServerId(); - iPid = _serverObjectPtr->getActivator()->activate(sServerId, sStartScript, sMonitorScript, sResult); - - - vector vtServerName = TC_Common::sepstr(sServerId, "."); - if (vtServerName.size() != 2) - { - sResult = sResult + "|failed to get pid for " + sServerId + ",server id error"; - NODE_LOG("startServer")->error() << FILE_FUN << sResult << endl; - throw runtime_error(sResult); - } - - //默认使用å¯åŠ¨è„šæœ¬è·¯å¾„ - string sFullExeFileName = TC_File::extractFilePath(sStartScript) + vtServerName[1]; - time_t tNow = TNOW; - int iStartWaitInterval = START_WAIT_INTERVAL; - try - { - //æœåŠ¡å¯åŠ¨,超时时间自己定义的情况 - TC_Config conf; - conf.parseFile(_serverObjectPtr->getConfigFile()); - iStartWaitInterval = TC_Common::strto(conf.get("/tars/application/server", "3000")) / 1000; - if (iStartWaitInterval < START_WAIT_INTERVAL) - { - iStartWaitInterval = START_WAIT_INTERVAL; - } - if (iStartWaitInterval > 60) - { - iStartWaitInterval = 60; - } - - } - catch (...) - { - } - - string sPidFile = "/usr/local/app/tars/tarsnode/util/" + sServerId + ".pid"; - string sGetServerPidScript = "ps -ef | grep -v 'grep' |grep -iE ' " + _desc.application+"."+_desc.serverName + "'|grep master| awk '{print $2}' > " + sPidFile; - - while ((TNOW - iStartWaitInterval) < tNow) - { - //注æ„:由于是守护进程,ä¸è¦å¯¹sytem返回值进行判断,始终waitä¸åˆ°å­è¿›ç¨‹ - system(sGetServerPidScript.c_str()); - string sPid = TC_Common::trim(TC_File::load2str(sPidFile)); - if (TC_Common::isdigit(sPid)) - { - iPid = TC_Common::strto(sPid); - _serverObjectPtr->setPid(iPid); - if (_serverObjectPtr->checkPid() == 0) - { - bSucc = true; - break; - } - } - - NODE_LOG("startServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " activating usleep " << int(iStartWaitInterval) << endl; - usleep(START_SLEEP_INTERVAL); - } - - if (_serverObjectPtr->checkPid() != 0) - { - sResult = sResult + "|get pid for server[" + sServerId + "],pid is not digit"; - NODE_LOG("startServer")->error() << FILE_FUN << sResult << endl; - if (TC_File::isFileExist(sPidFile)) - { - TC_File::removeFile(sPidFile, false); - } - throw runtime_error(sResult); - } - - if (TC_File::isFileExist(sPidFile)) - { - TC_File::removeFile(sPidFile, false); - } - return bSucc; -} - -inline bool CommandStart::startNormal(string& sResult) -{ - pid_t iPid = -1; - bool bSucc = false; - string sRollLogFile; - vector vEnvs; - vector vOptions; - string sConfigFile = _serverObjectPtr->getConfigFile(); - string sLogPath = _serverObjectPtr->getLogPath(); - string sServerDir = _serverObjectPtr->getServerDir(); - string sLibPath = _serverObjectPtr->getLibPath(); - string sExePath = _serverObjectPtr->getExePath(); - - //环境å˜é‡è®¾ç½® - vector vecEnvs = TC_Common::sepstr(_serverObjectPtr->getEnv(), ";|"); - for (vector::size_type i = 0; i < vecEnvs.size(); i++) - { - vEnvs.push_back(vecEnvs[i]); - } - - { - vEnvs.push_back("LD_LIBRARY_PATH=$LD_LIBRARY_PATH:" + sExePath + ":" + sLibPath); - } - - //生æˆå¯åŠ¨è„šæœ¬ - std::ostringstream osStartStcript; - osStartStcript << "#!/bin/sh" << std::endl; - for (vector::size_type i = 0; i < vEnvs.size(); i++) - { - osStartStcript << "export " << vEnvs[i] << std::endl; - } - - osStartStcript << std::endl; - if (_serverObjectPtr->getServerType() == "tars_java") - { - //javaæœåŠ¡ - string sClassPath = _serverObjectPtr->getClassPath(); - string sJarList = sExePath + "/classes"; - vector vJarList; - TC_File::scanDir(sClassPath, vJarList); - - sJarList = sJarList + ":" + sClassPath + "/*"; - - vOptions.push_back("-Dconfig=" + sConfigFile); - string s = _serverObjectPtr->getJvmParams() + " -cp " + sJarList + " " + _serverObjectPtr->getMainClass(); - vector v = TC_Common::sepstr(s, " \t"); - vOptions.insert(vOptions.end(), v.begin(), v.end()); - - osStartStcript << _exeFile << " " << TC_Common::tostr(vOptions) << endl; - } - else if (_serverObjectPtr->getServerType() == "tars_nodejs") - { - vOptions.push_back(sExePath + "/tars_nodejs/node-agent/bin/node-agent"); - string s = sExePath + "/src/ -c " + sConfigFile; - vector v = TC_Common::sepstr(s," \t"); - vOptions.insert(vOptions.end(), v.begin(), v.end()); - - //对于tars_nodejs类型需è¦ä¿®æ”¹ä¸‹_exeFile - _exeFile = sExePath+"/tars_nodejs/node"; - - osStartStcript<getServerType() == "tars_php") - { - vOptions.push_back("--config=" + sConfigFile); - osStartStcript << _exeFile << " " << TC_Common::tostr(vOptions) << " &" << endl; - } - else - { - //c++æœåŠ¡ - vOptions.push_back("--config=" + sConfigFile); - osStartStcript << _exeFile << " " << TC_Common::tostr(vOptions) << " &" << endl; - } - - //ä¿å­˜å¯åŠ¨æ–¹å¼åˆ°bin目录供手工å¯åŠ¨ - TC_File::save2file(sExePath + "/tars_start.sh", osStartStcript.str()); - TC_File::setExecutable(sExePath + "/tars_start.sh", true); - - if (sLogPath != "") - { - sRollLogFile = sLogPath + "/" + _desc.application + "/" + _desc.serverName + "/" + _desc.application + "." + _desc.serverName + ".log"; - } - - const string sPwdPath = sLogPath != "" ? sLogPath : sServerDir; //pwd patch 统一设置至log目录 以便core文件å‘现 删除 - iPid = _serverObjectPtr->getActivator()->activate(_exeFile, sPwdPath, sRollLogFile, vOptions, vEnvs); - if (iPid == 0) //child process - { - return false; - } - - _serverObjectPtr->setPid(iPid); - - bSucc = (_serverObjectPtr->checkPid() == 0) ? true : false; - - return bSucc; -} -////////////////////////////////////////////////////////////// -// -inline int CommandStart::execute(string& sResult) -{ - int64_t startMs = TC_TimeProvider::getInstance()->getNowMs(); - try - { - bool bSucc = false; - //set stdout & stderr - _serverObjectPtr->getActivator()->setRedirectPath(_serverObjectPtr->getRedirectPath()); - - if( TC_Common::lower(TC_Common::trim(_desc.serverType)) == "tars_php" ){ - - bSucc = startByScriptPHP(sResult); - - }else if (!_serverObjectPtr->getStartScript().empty() || _serverObjectPtr->isTarsServer() == false){ - - bSucc = startByScript(sResult); - } - else - { - bSucc = startNormal(sResult); - } - - if (bSucc == true) - { - _serverObjectPtr->synState(); - _serverObjectPtr->setLastKeepAliveTime(TNOW); - _serverObjectPtr->setLimitInfoUpdate(true); - _serverObjectPtr->setStarted(true); - _serverObjectPtr->setStartTime(TNOWMS); - NODE_LOG("startServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << "_" << _desc.nodeName << " start succ " << sResult << ", use:" << (TC_TimeProvider::getInstance()->getNowMs() - startMs) << " ms" << endl; - return 0; - } - } - catch (exception& e) - { - sResult = e.what(); - } - catch (const std::string& e) - { - sResult = e; - } - catch (...) - { - sResult = "catch unkwon exception"; - } - - NODE_LOG("startServer")->error() << FILE_FUN << _desc.application << "." << _desc.serverName << " start failed :" << sResult << ", use:" << (TC_TimeProvider::getInstance()->getNowMs() - startMs) << " ms" << endl; - - _serverObjectPtr->setPid(0); - _serverObjectPtr->setState(ServerObject::Inactive); - _serverObjectPtr->setStarted(false); - - return -1; -} -#endif diff --git a/cpp/framework/NodeServer/CommandStop.h b/cpp/framework/NodeServer/CommandStop.h deleted file mode 100644 index 089931617..000000000 --- a/cpp/framework/NodeServer/CommandStop.h +++ /dev/null @@ -1,269 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __STOP_COMMAND_H_ -#define __STOP_COMMAND_H_ - -#include "ServerCommand.h" - -class CommandStop : public ServerCommand -{ -public: - enum - { - STOP_WAIT_INTERVAL = 2, /*æœåŠ¡å…³é—­ç­‰å¾…时间 秒*/ - STOP_SLEEP_INTERVAL = 100000, /*微妙*/ - }; -public: - CommandStop(const ServerObjectPtr& pServerObjectPtr, bool bUseAdmin = true, bool bByNode = false, bool bGenerateCore = false); - - ExeStatus canExecute(string& sResult); - - int execute(string& sResult); - -private: - ServerObjectPtr _serverObjectPtr; - bool _useAdmin; //是å¦ä½¿ç”¨ç®¡ç†ç«¯å£åœæœåŠ¡ - bool _byNode; - bool _generateCore; - ServerDescriptor _desc; -}; - -////////////////////////////////////////////////////////////// -// -inline CommandStop::CommandStop(const ServerObjectPtr& pServerObjectPtr, bool bUseAdmin, bool bByNode, bool bGenerateCore) -: _serverObjectPtr(pServerObjectPtr) -, _useAdmin(bUseAdmin) -, _byNode(bByNode) -, _generateCore(bGenerateCore) -{ - _desc = _serverObjectPtr->getServerDescriptor(); -} -////////////////////////////////////////////////////////////// -// -inline ServerCommand::ExeStatus CommandStop::canExecute(string& sResult) -{ - TC_ThreadRecLock::Lock lock(*_serverObjectPtr); - - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " beging deactivate------|byRegistry|" << _byNode << endl; - - ServerObject::InternalServerState eState = _serverObjectPtr->getInternalState(); - - if (_byNode) - { - _serverObjectPtr->setEnabled(false); - } - - if (eState == ServerObject::Inactive) - { - _serverObjectPtr->synState(); - sResult = "server state is Inactive. "; - NODE_LOG("stopServer")->debug() << FILE_FUN << sResult << endl; - return NO_NEED_EXECUTE; - } - - if (eState == ServerObject::Destroying) - { - sResult = "server state is Destroying. "; - NODE_LOG("stopServer")->debug() << FILE_FUN << sResult << endl; - return DIS_EXECUTABLE; - } - - if (eState == ServerObject::Patching || eState == ServerObject::BatchPatching) - { - PatchInfo tPatchInfo; - _serverObjectPtr->getPatchPercent(tPatchInfo); - //状æ€ä¸ºPathingæ—¶ 10秒无更新æ‰å…许stop - std::string sPatchType = eState == ServerObject::BatchPatching ? "BatchPatching" : "Patching"; - time_t iTimes = eState == ServerObject::BatchPatching ? 10 * 60 : 10; - if (TNOW - tPatchInfo.iModifyTime < iTimes) - { - sResult = "server is " + sPatchType + " " + TC_Common::tostr(tPatchInfo.iPercent) + "%, please try again later....."; - NODE_LOG("stopServer")->debug() << FILE_FUN << "CommandStop::canExecute|" << sResult << endl; - return DIS_EXECUTABLE; - } - else - { - NODE_LOG("stopServer")->debug() << FILE_FUN << "server is patching " << tPatchInfo.iPercent << "% ,and no modify info for " << TNOW - tPatchInfo.iModifyTime << "s" << endl; - } - } - - _serverObjectPtr->setState(ServerObject::Deactivating); - - return EXECUTABLE; -} - -////////////////////////////////////////////////////////////// -// -inline int CommandStop::execute(string& sResult) -{ - bool needWait = false; - try - { - pid_t pid = _serverObjectPtr->getPid(); - NODE_LOG("stopServer")->debug() << FILE_FUN << "pid:" << pid << endl; - if (pid != 0) - { - string f = "/proc/" + TC_Common::tostr(pid) + "/status"; - NODE_LOG("stopServer")->debug() << FILE_FUN << "print the server status :" << f << "|" << TC_File::load2str(f) << endl; - } - - string sStopScript = _serverObjectPtr->getStopScript(); - //é…置了脚本或者éžtarsæœåŠ¡ - if( TC_Common::lower(TC_Common::trim(_desc.serverType)) == "tars_php" ){ - //生æˆåœæ­¢shell脚本 - string sConfigFile = _serverObjectPtr->getConfigFile(); - string sLogPath = _serverObjectPtr->getLogPath(); - string sServerDir = _serverObjectPtr->getServerDir(); - string sLibPath = _serverObjectPtr->getLibPath(); - string sExePath = _serverObjectPtr->getExePath(); - string sLogRealPath = sLogPath + _desc.application +"/" + _desc.serverName + "/" ; - string sLogRealPathFile = sLogRealPath +_serverObjectPtr->getServerId() +".log"; - - TC_Config conf; - conf.parseFile(sConfigFile); - string phpexecPath = ""; - string entrance = ""; - try{ - phpexecPath = TC_Common::strto(conf["/tars/application/server"]); - entrance = TC_Common::strto(conf["/tars/application/server"]); - } catch (...){} - entrance = entrance=="" ? sServerDir+"/bin/src/index.php" : entrance; - - std::ostringstream osStopStcript; - osStopStcript << "#!/bin/sh" << std::endl; - osStopStcript << "if [ ! -d \"" << sLogRealPath << "\" ];then mkdir -p \"" << sLogRealPath << "\"; fi" << std::endl; - osStopStcript << phpexecPath << " " << entrance <<" --config=" << sConfigFile << " stop >> " << sLogRealPathFile << " 2>&1 " << std::endl; - osStopStcript << "echo \"end-tars_stop.sh\"" << std::endl; - - TC_File::save2file(sExePath + "/tars_stop.sh", osStopStcript.str()); - TC_File::setExecutable(sExePath + "/tars_stop.sh", true); - - sStopScript = sStopScript=="" ? _serverObjectPtr->getExePath() + "/tars_stop.sh" : sStopScript; - - map mResult; - string sServerId = _serverObjectPtr->getServerId(); - _serverObjectPtr->getActivator()->doScript(sServerId, sStopScript, sResult, mResult); - needWait = true; - - }else if (!sStopScript.empty() || _serverObjectPtr->isTarsServer() == false) { - map mResult; - string sServerId = _serverObjectPtr->getServerId(); - _serverObjectPtr->getActivator()->doScript(sServerId, sStopScript, sResult, mResult); - needWait = true; - } - else - { - if (_useAdmin) - { - AdminFPrx pAdminPrx; //æœåŠ¡ç®¡ç†ä»£ç† - string sAdminPrx = "AdminObj@" + _serverObjectPtr->getLocalEndpoint().toString(); - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName - << " call " << sAdminPrx << endl; - pAdminPrx = Application::getCommunicator()->stringToProxy(sAdminPrx); - pAdminPrx->async_shutdown(NULL); - needWait = true; - } - } - - } - catch (exception& e) - { - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " shut down result:|" << e.what() << "|use kill -9 for check" << endl; - pid_t pid = _serverObjectPtr->getPid(); - _serverObjectPtr->getActivator()->deactivate(pid); - } - catch (...) - { - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " shut down fail:|" << "|use kill -9 for check" << endl; - pid_t pid = _serverObjectPtr->getPid(); - _serverObjectPtr->getActivator()->deactivate(pid); - } - - //等待STOP_WAIT_INTERVAL秒 - time_t tNow = TNOW; - int iStopWaitInterval = STOP_WAIT_INTERVAL; - try - { - //æœåŠ¡åœæ­¢,超时时间自己定义的情况 - TC_Config conf; - conf.parseFile(_serverObjectPtr->getConfigFile()); - iStopWaitInterval = TC_Common::strto(conf["/tars/application/server"]) / 1000; - - if (iStopWaitInterval < STOP_WAIT_INTERVAL) - { - iStopWaitInterval = STOP_WAIT_INTERVAL; - } - - if (iStopWaitInterval > 60) - { - iStopWaitInterval = 60; - } - - } - catch (...) - { - } - - if (needWait) - { - while (TNOW - iStopWaitInterval < tNow) - { - if (_serverObjectPtr->checkPid() != 0) //如果pidå·²ç»ä¸å­˜åœ¨ - { - _serverObjectPtr->setPid(0); - _serverObjectPtr->setState(ServerObject::Inactive); - _serverObjectPtr->setStarted(false); - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << "server already stop" << endl; - return 0; - } - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " deactivating usleep " << int(STOP_SLEEP_INTERVAL) << endl; - usleep(STOP_SLEEP_INTERVAL); - } - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << " shut down timeout ( used " << iStopWaitInterval << "'s), use kill -9" << endl; - } - - //ä»ç„¶å¤±è´¥ã€‚用kill -9,å†ç­‰å¾…STOP_WAIT_INTERVAL秒。 - pid_t pid = _serverObjectPtr->getPid(); - if (_generateCore == true) - { - _serverObjectPtr->getActivator()->deactivateAndGenerateCore(pid); - } - else - { - _serverObjectPtr->getActivator()->deactivate(pid); - } - - tNow = TNOW; - while (TNOW - STOP_WAIT_INTERVAL < tNow) - { - if (_serverObjectPtr->checkPid() != 0) //如果pidå·²ç»ä¸å­˜åœ¨ - { - _serverObjectPtr->setPid(0); - _serverObjectPtr->setState(ServerObject::Inactive); - _serverObjectPtr->setStarted(false); - return 0; - } - usleep(STOP_SLEEP_INTERVAL); - } - sResult = "server pid " + TC_Common::tostr(pid) + " is still exist"; - - NODE_LOG("stopServer")->debug() << FILE_FUN << _desc.application << "." << _desc.serverName << ", " << sResult << endl; - - return -1; -} - -#endif diff --git a/cpp/framework/NodeServer/KeepAliveThread.cpp b/cpp/framework/NodeServer/KeepAliveThread.cpp deleted file mode 100644 index af4f14a91..000000000 --- a/cpp/framework/NodeServer/KeepAliveThread.cpp +++ /dev/null @@ -1,352 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "KeepAliveThread.h" -#include "RegistryProxy.h" -#include "NodeRollLogger.h" -#include "util/tc_timeprovider.h" -#include "util.h" - -KeepAliveThread::KeepAliveThread() -: _terminate(false) -, _registryPrx(NULL) -{ - _runTime = time(0); - _nodeInfo = _platformInfo.getNodeInfo(); - _heartTimeout = TC_Common::strto(g_pconf->get("/tars/node/keepalive", "10")); - _monitorInterval = TC_Common::strto(g_pconf->get("/tars/node/keepalive", "2")); - - _monitorIntervalMs = TC_Common::strto(g_pconf->get("/tars/node/keepalive", "10")); - - _synInterval = TC_Common::strto(g_pconf->get("/tars/node/keepalive", "60")); - _synStatBatch = g_pconf->get("/tars/node/keepalive", "Y"); - _monitorInterval = _monitorInterval > 10 ? 10 : (_monitorInterval < 1 ? 1 : _monitorInterval); - -} - -KeepAliveThread::~KeepAliveThread() -{ - terminate(); -} - -void KeepAliveThread::terminate() -{ - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << endl; - - _terminate = true; - - if (isAlive()) - { - _lock.notifyAll(); - getThreadControl().join(); - } - -} - -bool KeepAliveThread::timedWait(int millsecond) -{ - TC_ThreadLock::Lock lock(_lock); - - if (_terminate) - { - return true; - } - - return _lock.timedWait(millsecond); -} - -void KeepAliveThread::run() -{ - bool bLoaded = false; - bool bRegistered = false; - - while (!_terminate) - { - int64_t startMs = TC_TimeProvider::getInstance()->getNowMs(); - - try - { - //获å–ä¸»æŽ§ä»£ç† - if (!_registryPrx) - { - _registryPrx = AdminProxy::getInstance()->getRegistryProxy(); - if (!_registryPrx) - { - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "RegistryProxy init fail ! retry it after " + TC_Common::tostr(_monitorInterval) + " second" << endl; - } - } - - //注册nodeä¿¡æ¯ - if (_registryPrx && bRegistered == false) - { - bRegistered = registerNode(); - } - - //加载æœåŠ¡ - if (bLoaded == false) - { - bLoaded = loadAllServers(); - } - - //检查æœåŠ¡çš„limité…置是å¦éœ€è¦æ›´æ–° - if (bLoaded) - { - ServerFactory::getInstance()->setAllServerResourceLimit(); - } - - //检查æœåŠ¡ - checkAlive(); - - // 上报nodeçŠ¶æ€ - if (reportAlive() != 0) - { - bRegistered = false; //registryæœåŠ¡é‡å¯ 需è¦é‡æ–°æ³¨å†Œ - } - } - catch (exception& e) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch exception|" << e.what() << endl; - } - catch (...) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch unkown exception|" << endl; - } - - { - int64_t useMs = (TC_TimeProvider::getInstance()->getNowMs() - startMs); - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "run use:" << useMs << " ms" << endl; - } - - timedWait(ServerFactory::getInstance()->getMinMonitorIntervalMs()); - } -} - -bool KeepAliveThread::registerNode() -{ - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "registerNode begin===============|node name|" << _nodeInfo.nodeName << endl; - try - { - int iRet = _registryPrx->registerNode(_nodeInfo.nodeName, _nodeInfo, _platformInfo.getLoadInfo()); - - if (iRet == 0) - { - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "register node succ" << endl; - return true; - } - } - catch (exception& e) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "KeepAliveThread::registerNode catch exception|" << e.what() << endl; - } - - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "register node fail" << endl; - - return false; -} - -bool KeepAliveThread::loadAllServers() -{ - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "load server begin===============|node name|" << _nodeInfo.nodeName << endl; - - /** - * 由于加载失败或者node下没有部署æœåŠ¡ï¼Œè¿™é‡Œå°±ä¼šä¸€ç›´åŽ»è®¿é—®ä¸»æŽ§ - * 增加这个é™åˆ¶ï¼Œå¦‚果超过5次失败,则ä¸åŽ»åŠ è½½ï¼Œ10分钟é‡è¯•10次 - * 如果之åŽæœ‰æ–°æœåŠ¡éƒ¨ç½²ï¼Œä¼šè‡ªåŠ¨æ·»åŠ åˆ°node缓存中,ä¸å½±å“监控æµç¨‹ - */ - static int iFailNum = 0; - if (iFailNum >= 5) - { - static time_t tLastLoadTime = 0; - if ((TNOW - tLastLoadTime) < 600) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "load server fail" << endl; - return false; - } - tLastLoadTime = TNOW; - iFailNum = 0; - } - - try - { - if (ServerFactory::getInstance()->loadServer()) - { - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "load server succ" << endl; - return true; - } - } - catch (exception& e) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch exception|" << e.what() << endl; - } - - iFailNum++; - - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "load server fail" << endl; - - return false; -} - -int KeepAliveThread::reportAlive() -{ - try - { - static time_t tReport; - time_t tNow = TNOW; - if (tNow - tReport > _heartTimeout) - { - tReport = tNow; - - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "node keep alive ----------------------------------------------------|" << time(0) << "|" << pthread_self() << '\n' << endl; - - int iRet = _registryPrx->keepAlive(_nodeInfo.nodeName, _platformInfo.getLoadInfo()); - - return iRet; - } - } - catch (exception& e) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "KeepAliveThread::reportAlive catch exception|" << e.what() << endl; - } - - return 0; -} - -int KeepAliveThread::synStat() -{ - try - { - int iRet = -1; - - vector v; - _stat.swap(v); - - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "node syn stat size|" << v.size() << "|" << _synStatBatch << "|" << pthread_self() << endl; - - if (v.size() > 0) - { - iRet = _registryPrx->updateServerBatch(v); - } - - return iRet; - } - catch (exception& e) - { - string s = e.what(); - if (s.find("server function mismatch exception") != string::npos) - { - _synStatBatch = "N"; - } - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch exception|" << s << endl; - } - - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "fail,set SynStatBatch = " << _synStatBatch << endl; - - return -1; -} - -void KeepAliveThread::checkAlive() -{ - int64_t startMs = TC_TimeProvider::getInstance()->getNowMs(); - - static time_t tSyn = 0; - bool bNeedSynServerState = false; - - time_t tNow = TNOW; - if (tNow - tSyn > _synInterval) - { - tSyn = tNow; - bNeedSynServerState = true; - } - - string sServerId; - - _stat.clear(); - - map mmServerList = ServerFactory::getInstance()->getAllServers(); - map::const_iterator it = mmServerList.begin(); - for (; it != mmServerList.end(); it++) - { - map::const_iterator p = it->second.begin(); - for (; p != it->second.end(); p++) - { - try - { - sServerId = it->first + "." + p->first; - ServerObjectPtr pServerObjectPtr = p->second; - if (!pServerObjectPtr) - { - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << sServerId << "|=NULL|" << endl; - continue; - } - - pServerObjectPtr->doMonScript(); - - if (TNOW - _runTime < ServantHandle::HEART_BEAT_INTERVAL * 5) - { - //等待心跳包 - continue; - } - - //corelimit checking - pServerObjectPtr->checkCoredumpLimit(); - - //(checkServer时,上报Server所å ç”¨çš„内存给主控) - pServerObjectPtr->checkServer(_heartTimeout); - - //cache åŒæ­¥server çŠ¶æ€ - if (bNeedSynServerState == true) - { - ServerInfo tServerInfo; - tServerInfo.application = it->first; - tServerInfo.serverName = p->first; - ServerDescriptor tServerDescriptor = pServerObjectPtr->getServerDescriptor(); - tServerDescriptor.settingState = pServerObjectPtr->isEnabled() == true ? "active" : "inactive"; - g_serverInfoHashmap.set(tServerInfo, tServerDescriptor); - - ServerStateInfo tServerStateInfo; - tServerStateInfo.serverState = (pServerObjectPtr->IsEnSynState() ? pServerObjectPtr->getState() : tars::Inactive); - tServerStateInfo.processId = pServerObjectPtr->getPid(); - tServerStateInfo.nodeName = _nodeInfo.nodeName; - tServerStateInfo.application = it->first; - tServerStateInfo.serverName = p->first; - - if (TC_Common::lower(_synStatBatch) == "y") - { - _stat.push_back(tServerStateInfo); - } - else - { - pServerObjectPtr->asyncSynState(); - } - } - } - catch (exception& e) - { - NODE_LOG("KeepAliveThread")->error() << FILE_FUN << sServerId << " catch exception|" << e.what() << endl; - } - } - } - - if (bNeedSynServerState) - { - synStat(); - } - - { - int64_t useMs = (TC_TimeProvider::getInstance()->getNowMs() - startMs); - NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "checkAlive use:" << useMs << " ms" << endl; - } -} diff --git a/cpp/framework/NodeServer/KeepAliveThread.h b/cpp/framework/NodeServer/KeepAliveThread.h deleted file mode 100644 index fe5c991a0..000000000 --- a/cpp/framework/NodeServer/KeepAliveThread.h +++ /dev/null @@ -1,105 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __KEEP_ALIVE_THREAD_H_ -#define __KEEP_ALIVE_THREAD_H_ -#include "Node.h" -#include -#include "ServerFactory.h" -#include "util/tc_monitor.h" -#include "util/tc_thread.h" -#include "PlatformInfo.h" - -using namespace tars; -using namespace std; - -class KeepAliveThread : public TC_Thread//,public TC_ThreadLock -{ -public: - /** - * 构造函数 - */ - KeepAliveThread(); - - /** - * æžæž„函数 - */ - ~KeepAliveThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - -protected: - - virtual void run(); - - /** - * å¾€registry注册nodeä¿¡æ¯ - */ - bool registerNode(); - - /** - * 获å–本node在registryé…ç½®æœåŠ¡ä¿¡æ¯ - */ - bool loadAllServers(); - - /** - * 上报registry node当å‰çŠ¶æ€ - * @return int éž0失败 - */ - int reportAlive(); - - /** - * 检查node上serverçŠ¶æ€ - */ - void checkAlive(); - - /** - * 与registry åŒæ­¥node上所属æœåŠ¡çŠ¶æ€ - */ - int synStat(); - - /** - * 等待 - */ - bool timedWait(int millsecond); - -protected: - - NodeInfo _nodeInfo; - PlatformInfo _platformInfo; - - bool _terminate; - time_t _runTime; //nodeè¿è¡Œæ—¶é—´ - int _heartTimeout; //业务心跳超时时间(s) - int _monitorInterval; //监控server状æ€çš„间隔时间(s) - int _monitorIntervalMs; //新的监控状æ€é—´éš”,改æˆæ¯«ç§’ - int _synInterval; //åŒæ­¥ä¸Žregisrty server状æ€çš„间隔时间(s) - string _synStatBatch; //批é‡åŒæ­¥ - - RegistryPrx _registryPrx; - -private: - - vector _stat; //æœåŠ¡çŠ¶æ€åˆ—表 - TC_ThreadLock _lock; -}; - - -#endif - diff --git a/cpp/framework/NodeServer/NodeImp.cpp b/cpp/framework/NodeServer/NodeImp.cpp deleted file mode 100644 index 97d14b6bb..000000000 --- a/cpp/framework/NodeServer/NodeImp.cpp +++ /dev/null @@ -1,771 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_clientsocket.h" -#include "BatchPatchThread.h" -#include "NodeImp.h" -#include "RegistryProxy.h" -#include "NodeServer.h" -#include "CommandStart.h" -#include "CommandNotify.h" -#include "CommandStop.h" -#include "CommandDestroy.h" -#include "CommandAddFile.h" -#include "CommandPatch.h" -#include "NodeRollLogger.h" -#include "AdminReg.h" -#include "util/tc_timeprovider.h" - -extern BatchPatch * g_BatchPatchThread; - -void NodeImp::initialize() -{ - try - { - TLOGDEBUG("initialize NodeImp" << endl); - - _nodeInfo = _platformInfo.getNodeInfo(); - _downloadPath = _platformInfo.getDownLoadDir(); - } - catch ( exception& e ) - { - TLOGERROR(FILE_FUN << e.what() << endl); - exit( 0 ); - } -} - -int NodeImp::destroyServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current ) -{ - int64_t startMs = TC_TimeProvider::getInstance()->getNowMs(); - - int iRet = EM_TARS_SUCCESS; - if ( g_app.isValid(current->getIp()) == false ) - { - result += " erro:ip "+ current->getIp()+" is invalid"; - iRet = EM_TARS_INVALID_IP_ERR; - NODE_LOG("destroyServer")->error() << FILE_FUN << "access_log|" << iRet << "|" << (TC_TimeProvider::getInstance()->getNowMs() - startMs) - << "|" << application << "." << serverName << "|" << result << endl; - return iRet; - } - - result = " ["+application + "." + serverName+"] "; - NODE_LOG("destroyServer")->debug() << result << endl; - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName ); - if ( pServerObjectPtr ) - { - string s; - CommandDestroy command(pServerObjectPtr); - iRet = command.doProcess(current, s, false); - if ( iRet == 0 && ServerFactory::getInstance()->eraseServer(application, serverName) == 0) - { - pServerObjectPtr = NULL; - result = result+"succ:" + s; - } - else - { - result = FILE_FUN_STR+"error:"+s; - } - } - else - { - result += FILE_FUN_STR+"server is not exist"; - iRet = -2; - } - - NODE_LOG("destroyServer")->error() << FILE_FUN << "access_log|" << iRet << "|" << (TC_TimeProvider::getInstance()->getNowMs() - startMs) - << "|" << application << "|" << serverName << "|" << result << endl; - return iRet; -} - -int NodeImp::patchPro(const tars::PatchRequest & req, string & result, TarsCurrentPtr current) -{ - NODE_LOG("patchPro")->debug() << FILE_FUN - << req.appname + "." + req.servername + "_" + req.nodename << "|" - << req.groupname << "|" - << req.version << "|" - << req.user << "|" - << req.servertype << "|" - << req.patchobj << "|" - << req.md5 << "|" - << req.ostype << endl; - - if ( g_app.isValid(current->getIp()) == false ) - { - result +=FILE_FUN_STR+ " erro:ip "+ current->getIp()+" is invalid"; - NODE_LOG("patchPro")->error()<loadServer(req.appname, req.servername, true, sError); - if (!server) - { - result =FILE_FUN_STR+ "load " + req.appname + "." + req.servername + "|" + sError; - NODE_LOG("patchPro")->error() <getInternalState(); - - //去掉å¯åŠ¨ä¸­ä¸èƒ½å‘布的é™åˆ¶ - if (server->toStringState(eState).find("ing") != string::npos && eState != ServerObject::Activating) - { - result = FILE_FUN_STR+"cannot patch the server ,the server state is " + server->toStringState(eState); - NODE_LOG("patchPro")->error() <error() <error() <debug() <toStringState(eState) << endl; - server->setLastState(eState); - server->setState(ServerObject::BatchPatching); - //将百分比åˆå§‹åŒ–为0,防止上次patch结果影å“本次patch进度 - server->setPatchPercent(0); - - NODE_LOG("patchPro")->debug()<push_back(req,server); - } - catch (std::exception & ex) - { - NODE_LOG("patchPro")->error() <error() <debug() <getServer( application, serverName ); - if ( pServerObjectPtr ) - { - string s; - CommandAddFile command(pServerObjectPtr,file); - int iRet = command.doProcess(current,s); - if ( iRet == 0) - { - result = result+"succ:" + s; - } - else - { - result = FILE_FUN_STR+"error:"+s; - } - return iRet; - } - - result += FILE_FUN_STR+"server is not exist"; - - return -1; -} - -string NodeImp::getName( TarsCurrentPtr current ) -{ - TLOGDEBUG(FILE_FUN << endl); - - _nodeInfo = _platformInfo.getNodeInfo(); - - return _nodeInfo.nodeName; -} - -LoadInfo NodeImp::getLoad( TarsCurrentPtr current ) -{ - TLOGDEBUG(FILE_FUN<< endl); - - return _platformInfo.getLoadInfo(); -} - -int NodeImp::shutdown( string &result,TarsCurrentPtr current ) -{ - try - { - result = FILE_FUN_STR; - - TLOGDEBUG( result << endl); - if ( g_app.isValid(current->getIp()) == false ) - { - result += "erro:ip "+ current->getIp()+" is invalid"; - TLOGERROR(FILE_FUN << result << endl); - return -1; - } - Application::terminate(); - return 0; - } - catch ( exception& e ) - { - TLOGERROR( "NodeImp::shutdown catch exception :" << e.what() << endl); - } - catch ( ... ) - { - TLOGERROR( "NodeImp::shutdown catch unkown exception" << endl); - } - return -1; -} - -int NodeImp::stopAllServers( string &result,TarsCurrentPtr current ) -{ - try - { - if ( g_app.isValid(current->getIp()) == false ) - { - result = FILE_FUN_STR+" erro:ip "+ current->getIp()+" is invalid"; - TLOGERROR( result << endl); - return -1; - } - map mmServerList; - mmServerList.clear(); - mmServerList = ServerFactory::getInstance()->getAllServers(); - for ( map::const_iterator it = mmServerList.begin(); it != mmServerList.end(); it++ ) - { - for ( map::const_iterator p = it->second.begin(); p != it->second.end(); p++ ) - { - ServerObjectPtr pServerObjectPtr = p->second; - if ( pServerObjectPtr ) - { - result =result+"stop server ["+it->first+"."+ p->first+"] "; - TLOGDEBUG(FILE_FUN << result << endl); - - bool bByNode = true; - string s; - CommandStop command(pServerObjectPtr,true,bByNode); - int iRet = command.doProcess(s); - if (iRet == 0) - { - result = result+"succ:"+s+"\n"; - } - else - { - result = result+"error:"+s+"\n"; - } - } - } - } - return 0; - } - catch ( exception& e ) - { - TLOGERROR( FILE_FUN<<" catch exception :" << e.what() << endl); - result += e.what(); - } - catch ( ... ) - { - TLOGERROR( FILE_FUN<<" catch unkown exception" << endl); - result += "catch unkown exception"; - } - TLOGDEBUG("stop succ"<getIp()) == false ) - { - result += " erro:ip "+ current->getIp()+" is invalid"; - TLOGERROR(FILE_FUN << result << endl); - return -1; - } - - string s; - - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->loadServer(application,serverName,false,s); - if (!pServerObjectPtr) - { - result =result+" error::cannot load server description.\n"+s; - return EM_TARS_LOAD_SERVICE_DESC_ERR; - } - result = result + " succ" + s; - return 0; - } - catch ( exception& e ) - { - TLOGERROR( FILE_FUN<<" catch exception :" << e.what() << endl); - result += e.what(); - } - catch ( ... ) - { - TLOGERROR( FILE_FUN<<" catch unkown exception" << endl); - result += "catch unkown exception"; - } - return -1; -} - -int NodeImp::startServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current ) -{ - int iRet = EM_TARS_UNKNOWN_ERR; - try - { - result = string(__FUNCTION__)+ " ["+application + "." + serverName+"] from "+ current->getIp()+" "; - NODE_LOG("startServer")->debug()<getIp()) == false ) - { - result += " erro:ip "+ current->getIp()+" is invalid"; - NODE_LOG("startServer")->error()<debug()<loadServer( application, serverName,true,s); - NODE_LOG("startServer")->debug()<debug()<getServer( application, serverName ); - if (pServerObjectPtr) - { - pServerObjectPtr->setEnabled(true); - } - - result += "error::cannot load server description from regisrty.\n" + s; - iRet = EM_TARS_LOAD_SERVICE_DESC_ERR; - } - catch ( exception& e ) - { - NODE_LOG("startServer")->error() << FILE_FUN << " catch exception:" << e.what() << endl; - result += e.what(); - } - catch ( ... ) - { - NODE_LOG("startServer")->error() << FILE_FUN <<" catch unkown exception" << endl; - result += "catch unkown exception"; - } - return iRet; -} - -int NodeImp::stopServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current ) -{ - int iRet = EM_TARS_UNKNOWN_ERR; - try - { - result = string(__FUNCTION__)+" ["+application + "." + serverName+"] from "+ current->getIp()+" "; - - NODE_LOG("stopServer")->debug() <getServer( application, serverName ); - if ( pServerObjectPtr ) - { - string s; - bool bByNode = true; - CommandStop command(pServerObjectPtr,true, bByNode); - iRet = command.doProcess(current,s); - if (iRet == 0 ) - { - result = result+"succ:"+s; - } - else - { - result = result+"error:"+s; - } - NODE_LOG("stopServer")->debug()<error() << FILE_FUN<<" catch exception :" << e.what() << endl; - result += e.what(); - } - catch ( ... ) - { - NODE_LOG("stopServer")->error() << FILE_FUN<<" catch unkown exception" << endl; - result += "catch unkown exception"; - } - return EM_TARS_UNKNOWN_ERR; -} - -int NodeImp::notifyServer( const string& application, const string& serverName, const string &sMsg, string &result, TarsCurrentPtr current ) -{ - try - { - result = string(__FUNCTION__)+" ["+application + "." + serverName+"] '" + sMsg + "' "; - TLOGDEBUG(result<getIp()) == false ) - { - result += " erro:ip "+ current->getIp()+" is invalid"; - TLOGERROR("NodeImp::notifyServer " << result << endl); - return EM_TARS_INVALID_IP_ERR; - } - - if ( application == "tars" && serverName == "tarsnode" ) - { - AdminFPrx pAdminPrx; //æœåŠ¡ç®¡ç†ä»£ç† - pAdminPrx = Application::getCommunicator()->stringToProxy("AdminObj@"+ServerConfig::Local); - result = pAdminPrx->notify(sMsg); - return 0; - } - - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName ); - if ( pServerObjectPtr ) - { - string s; - CommandNotify command(pServerObjectPtr,sMsg); - int iRet = command.doProcess(s); - if (iRet == 0 ) - { - result = result + "succ:" + s; - } - else - { - result = result + "error:" + s; - } - return iRet; - } - - result += "server is not exist"; - } - catch ( exception& e ) - { - TLOGERROR( "NodeImp::notifyServer catch exception :" << e.what() << endl); - result += e.what(); - } - catch ( ... ) - { - TLOGERROR( "NodeImp::notifyServer catch unkown exception" << endl); - result += "catch unkown exception"; - } - return -1; -} - -int NodeImp::getServerPid( const string& application, const string& serverName,string &result,TarsCurrentPtr current ) -{ - result = string(__FUNCTION__)+" ["+application + "." + serverName+"] "; - TLOGDEBUG("NodeImp::getServerPid " <getServer( application, serverName ); - if ( pServerObjectPtr ) - { - result += "succ"; - return pServerObjectPtr->getPid(); - } - result += "server not exist"; - return -1; -} - -ServerState NodeImp::getSettingState( const string& application, const string& serverName,string &result, TarsCurrentPtr current ) -{ - result = string(__FUNCTION__)+" ["+application + "." + serverName+"] "; - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName ); - if ( pServerObjectPtr ) - { - result += "succ"; - return pServerObjectPtr->isEnabled()==true?tars::Active:tars::Inactive; - } - result += "server not exist"; - TLOGERROR( "NodeImp::getServerPid" <<" "<getServer( application, serverName ); - if ( pServerObjectPtr ) - { - result += "succ"; - return pServerObjectPtr->getState(); - } - result += "server not exist"; - TLOGERROR("NodeImp::getState " << result << endl); - return tars::Inactive; -} - -tars::Int32 NodeImp::getStateInfo(const std::string & application,const std::string & serverName,tars::ServerStateInfo &info,std::string &result,tars::TarsCurrentPtr current) -{ - result = string(__FUNCTION__)+" ["+application + "." + serverName+"] "; - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName ); - if ( pServerObjectPtr ) - { - result += "succ"; - - info.serverState = pServerObjectPtr->getState(); - info.processId = pServerObjectPtr->getPid(); - info.settingState = pServerObjectPtr->isEnabled()==true?tars::Active:tars::Inactive; - - TLOGDEBUG("NodeImp::getStateInfo " <getServer( application, serverName ); - if ( pServerObjectPtr ) - { - result += "succ"; - pServerObjectPtr->synState(); - return 0; - } - result += "server not exist"; - TLOGERROR("NodeImp::synState "<getServer( application, serverName ); - if ( pServerObjectPtr ) - { - result += "succ"; - NODE_LOG("getPatchPercent")->debug()<getPatchPercent(tPatchInfo); - } - - result += "server not exist"; - NODE_LOG("getPatchPercent")->error() << FILE_FUN <<" "<< result<< endl; - return EM_TARS_LOAD_SERVICE_DESC_ERR; - } - catch ( exception& e ) - { - NODE_LOG("getPatchPercent")->error() << FILE_FUN << " catch exception :" << e.what() << endl; - result += e.what(); - } - catch ( ... ) - { - NODE_LOG("getPatchPercent")->error() << FILE_FUN <<" catch unkown exception" << endl; - result += "catch unkown exception"; - } - - return EM_TARS_UNKNOWN_ERR; - -} - -tars::Int32 NodeImp::delCache(const string &sFullCacheName, const std::string &sBackupPath, const std::string & sKey, std :: string &result, TarsCurrentPtr current) -{ - try - { - TLOGDEBUG("NodeImp::delCache "<(sKey); - } - TLOGDEBUG("NodeImp::delCache|key=" << key << endl); - - int shmid = shmget(key, 0, 0666); - if (shmid == -1) - { - result = "failed to shmget " + sBasePath + "|key=" + TC_Common::tostr(key); - //如果获å–失败则认为共享内存已ç»åˆ é™¤,直接返回æˆåŠŸ - TLOGDEBUG("NodeImp::delCache"<< "|" << sFullCacheName << "|" << result << endl); - return 0; - } - - shmid_ds *pShmInfo= new shmid_ds; - int iRet = shmctl(shmid, IPC_STAT, pShmInfo); - if (iRet != 0) - { - result = "failed to shmctl " + sBasePath + "|key=" + TC_Common::tostr(key) + "|ret=" + TC_Common::tostr(iRet); - delete pShmInfo; - pShmInfo = NULL; - throw runtime_error(result); - } - - if (pShmInfo->shm_nattch>=1) - { - result = "current attach count >= 1,please check it |" + sBasePath + "|key=" +TC_Common::tostr(key)+"|attch_count=" + TC_Common::tostr(pShmInfo->shm_nattch); - delete pShmInfo; - pShmInfo = NULL; - throw runtime_error(result); - }; - - delete pShmInfo; - pShmInfo = NULL; - - int ret =shmctl(shmid, IPC_RMID, NULL); - if (ret !=0) - { - result = "failed to rm share memory|key=" + TC_Common::tostr(key) + "|shmid=" + TC_Common::tostr(shmid) + "|ret=" + TC_Common::tostr(ret); - throw runtime_error(result); - } - - TLOGDEBUG("NodeImp::delCache success delete cache:" << sFullCacheName <<", no need backup it"<< endl); - - return 0; - } - catch ( exception& e ) - { - result = TC_Common::tostr(__FILE__)+":"+TC_Common::tostr(__FUNCTION__) + ":"+ TC_Common::tostr(__LINE__) + "|" + e.what(); - TLOGERROR("NodeImp::delCache " << result << endl); - TARS_NOTIFY_ERROR(result); - } - - return -1; -} - -tars::Int32 NodeImp::getUnusedShmKeys(tars::Int32 count,vector &shm_keys,tars::TarsCurrentPtr current) { - int ret = 0; - for(size_t i = 0; i < 256 && shm_keys.size() < (size_t)count; i++) - { - key_t key = ftok(ServerConfig::BasePath.c_str(), i); - if(key == -1) - { - TLOGDEBUG("NodeImp::getUnusedShmKeys create an key failed, i=" << i << endl); - } - else - { - int shmid = shmget(key, 0, 0666); - if(shmid == -1) - { - TLOGDEBUG("NodeImp::getUnusedShmKeys find an key:" << key << " 0x" << keyToStr(key) << endl); - shm_keys.push_back(key); - } - else - { - TLOGDEBUG("NodeImp::getUnusedShmKeys key: " << key << " 0x" << keyToStr(key) << " is busy"<< endl); - } - } - } - - return ret; -} - -string NodeImp::keyToStr(key_t key_value) -{ - char buf[32]; - snprintf(buf, 32, "%08x", key_value); - string s(buf); - return s; -} -/*********************************************************************/ - - - - - diff --git a/cpp/framework/NodeServer/NodeImp.h b/cpp/framework/NodeServer/NodeImp.h deleted file mode 100644 index 0ef6a9329..000000000 --- a/cpp/framework/NodeServer/NodeImp.h +++ /dev/null @@ -1,208 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __NODE_IMP_H_ -#define __NODE_IMP_H_ -#include "Node.h" -#include -#include "PlatformInfo.h" -#include "Activator.h" -#include "KeepAliveThread.h" -#include "tars_patch.h" - -using namespace tars; -using namespace std; - -class NodeImp : public Node -{ -public: - /** - * 构造函数 - */ - NodeImp() - { - } - - /** - * æžæž„函数 - */ - ~NodeImp() - { - } - - /** - * åˆå§‹åŒ– - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy() - { - } - - /** - * 销æ¯æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - virtual int destroyServer( const string& application, const string& serverName, string &result,TarsCurrentPtr current ); - - /** - * 增强的å‘å¸ƒæŽ¥å£ - * pushRequest æ’å…¥å‘布请求到队列 - * @param req å‘布请求 - * @return int 0æˆåŠŸ 其它失败 - */ - int patchPro(const tars::PatchRequest & req, string & result, TarsCurrentPtr current); - - /** - * 加载指定文件 - * @param out result 失败说明 - * @return int 0æˆåŠŸ éž0失败 - */ - virtual int addFile(const string &application,const string &serverName,const string &file, string &result, TarsCurrentPtr current); - - /** - * 获å–nodeå称 - * @return string - */ - virtual string getName( TarsCurrentPtr current ) ; - - /** - * 获å–node上负载 - * @return LoadInfo - */ - virtual tars::LoadInfo getLoad( TarsCurrentPtr current ) ; - - /** - * 关闭node - * @return int - */ - virtual int shutdown( string &result, TarsCurrentPtr current ); - - /** - * 关闭nodes上所有æœåŠ¡ - * @return int - */ - virtual int stopAllServers( string &result,TarsCurrentPtr current ); - - /** - * 载入指定æœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - virtual int loadServer( const string& application, const string& serverName, string &result, TarsCurrentPtr current ); - - /** - * å¯åŠ¨æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - virtual int startServer( const string& application, const string& serverName, string &result, TarsCurrentPtr current ) ; - - /** - * åœæ­¢æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - virtual int stopServer( const string& application, const string& serverName, string &result, TarsCurrentPtr current ) ; - - /** - * 通知æœåŠ¡ - * @param application - * @param serverName - * @param result - * @param current - * - * @return int - */ - virtual int notifyServer( const string& application, const string& serverName, const string &command, string &result, TarsCurrentPtr current ); - - /** - * 获å–指定æœåŠ¡pidè¿›ç¨‹å· - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int getServerPid( const string& application, const string& serverName, string &result, TarsCurrentPtr current); - - /** - * 获å–指定æœåŠ¡registryè®¾ç½®çš„çŠ¶æ€ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - virtual ServerState getSettingState( const string& application, const string& serverName, string &result, TarsCurrentPtr current ) ; - - /** - * 获å–指定æœåŠ¡çŠ¶æ€ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return ServerState - */ - virtual ServerState getState( const string& application, const string& serverName, string &result, TarsCurrentPtr current ) ; - - /** - * 获å–指定æœåŠ¡åœ¨nodeçš„ä¿¡æ¯ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return ServerState - */ - virtual int getStateInfo(const std::string & application,const std::string & serverName,tars::ServerStateInfo &info,std::string &result,tars::TarsCurrentPtr current); - - /** - * åŒæ­¥æŒ‡å®šæœåŠ¡çŠ¶æ€ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - virtual int synState( const string& application, const string& serverName, string &result, TarsCurrentPtr current ) ; - - /** - * å‘布æœåŠ¡è¿›åº¦ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @out tPatchInfo ä¸‹è½½ä¿¡æ¯ - * @return int - */ - virtual int getPatchPercent( const string& application, const string& serverName, PatchInfo &tPatchInfo, TarsCurrentPtr current); - - virtual tars::Int32 delCache(const std::string & sFullCacheName, const std::string &sBackupPath, const std::string & sKey, std::string &result,TarsCurrentPtr current); - - virtual tars::Int32 getUnusedShmKeys(tars::Int32 count,vector &shm_keys,tars::TarsCurrentPtr current); - -private: - string keyToStr(key_t key_value); - -private: - - string _downloadPath; //文件下载目录 - NodeInfo _nodeInfo; //nodeä¿¡æ¯ - PlatformInfo _platformInfo; //å¹³å°ä¿¡æ¯ - RegistryPrx _registryPrx; //ä¸»æŽ§ä»£ç† - -}; - -typedef TC_AutoPtr NodeImpPtr; - -#endif - diff --git a/cpp/framework/NodeServer/NodeRollLogger.cpp b/cpp/framework/NodeServer/NodeRollLogger.cpp deleted file mode 100644 index 2fc296cd5..000000000 --- a/cpp/framework/NodeServer/NodeRollLogger.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NodeRollLogger.h" -#include "servant/Communicator.h" - - -///////////////////////////////////////////////////////////////////////////////////// -RollLoggerManager::RollLoggerManager() -: _maxSize(1024 * 1024 * 50), _maxNum(10) -{ -} - -RollLoggerManager::~RollLoggerManager() -{ - - map::iterator it = _loggers.begin(); - while(it != _loggers.end()) - { - delete it->second; - ++it; - } - _loggers.clear(); -} - -void RollLoggerManager::setLogInfo(const string &sApp, const string &sServer, const string &sLogpath, int iMaxSize, int iMaxNum, const CommunicatorPtr &comm, const string &sLogObj) -//void RollLoggerManager::setLogInfo(const string &sApp, const string &sServer, const string &sLogpath, int iMaxSize, int iMaxNum, const string &sLogObj) -{ - _app = sApp; - _server = sServer; - _logpath = sLogpath; - - _maxSize = iMaxSize; - _maxNum = iMaxNum; - _comm = comm; - _logObj = sLogObj; - //生æˆç›®å½• - TC_File::makeDirRecursive(_logpath + "/" + _app + "/" + _server); - - _local.start(1); -} - - -void RollLoggerManager::sync(RollLogger *pRollLogger, bool bSync) -{ - if(bSync) - { - pRollLogger->unSetupThread(); - } - else - { - pRollLogger->setupThread(&_local); - } -} - -/* -void NodeRollLogger::enableDyeing(bool bEnable, const string& sDyeingKey) -{ - _logger.getRoll()->enableDyeing(bEnable, sDyeingKey); -} -*/ - -RollLoggerManager::RollLogger* RollLoggerManager::logger(const string &sFile) -{ - Lock lock(*this); - map::iterator it = _loggers.find(sFile); - if( it == _loggers.end()) - { - RollLogger *p = new RollLogger(); - //p->modFlag(RollLogger::HAS_MTIME); - initRollLogger(p, sFile, "%Y%m%d"); - _loggers[sFile] = p; - return p; - } - - return it->second; -} - -void RollLoggerManager::initRollLogger(RollLogger *pRollLogger, const string &sFile, const string &sFormat) -{ - - //åˆå§‹åŒ–本地循环日志 - pRollLogger->init(_logpath + "/" + _app + "/" + _server + "/" + _app + "." + _server + "_" + sFile, _maxSize, _maxNum); - pRollLogger->modFlag(TC_DayLogger::HAS_TIME, false); - pRollLogger->modFlag(TC_DayLogger::HAS_TIME|TC_DayLogger::HAS_LEVEL|TC_DayLogger::HAS_PID, true); - - //设置为异步 - sync(pRollLogger, false); - - - //è®¾ç½®æŸ“è‰²æ—¥å¿—ä¿¡æ¯ - pRollLogger->getWriteT().setDyeingLogInfo(_app, _server, _logpath, _maxSize, _maxNum, _comm, _logObj); -// pRollLogger->getWriteT().setDyeingLogInfo(_app, _server, _logpath, _maxSize, _maxNum, _logObj); -} diff --git a/cpp/framework/NodeServer/NodeRollLogger.h b/cpp/framework/NodeServer/NodeRollLogger.h deleted file mode 100644 index 9beddff14..000000000 --- a/cpp/framework/NodeServer/NodeRollLogger.h +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __NODE_ROLL_LOGGER_H__ -#define __NODE_ROLL_LOGGER_H__ - -#include "util/tc_logger.h" -#include "servant/TarsLogger.h" - -/** - * node使用的日志,主è¦æ˜¯ä¸ºäº†æ”¯æŒå¤šä¸ªæ»šåŠ¨æ—¥å¿— - */ - -/////////////////////////////////////////////////////////////////////////////// - -/** - * 本地日志帮助类, å•ä»¶ - * 循环日志å•ä»¶æ˜¯æ°¸ç”Ÿä¸æ­»çš„, ä¿è¯ä»»ä½•åœ°æ–¹éƒ½å¯ä»¥ä½¿ç”¨ - * 当该对象æžå¤Ÿä»¥åŽ, 则直接coutå‡ºæ¥ - */ -class RollLoggerManager : public TC_ThreadLock, public TC_Singleton -{ -public: - enum - { - NONE_LOG = 1, /**所有的log都ä¸å†™*/ - ERROR_LOG = 2, /**写错误log*/ - WARN_LOG = 3, /**写错误,警告log*/ - DEBUG_LOG = 4, /**写错误,警告,调试log*/ - INFO_LOG = 5 /**写错误,警告,调试,Info log*/ - }; -public: - - typedef TC_Logger RollLogger; - - RollLoggerManager(); - - ~RollLoggerManager(); - - /** - * è®¾ç½®æœ¬åœ°ä¿¡æ¯ - * @param app, 业务å称 - * @param server, æœåŠ¡å称 - * @param logpath, 日志路径 - * @param iMaxSize, 文件最大大å°,字节 - * @param iMaxNum, 文件最大数 - */ - void setLogInfo(const string &sApp, const string &sServer, const string &sLogpath, int iMaxSize = 1024*1024*50, int iMaxNum = 10, const CommunicatorPtr &comm=NULL, const string &sLogObj=""); - - /** - * 设置åŒæ­¥å†™æ—¥å¿— - * - * @param bSync - */ - void sync(RollLogger *pRollLogger, bool bSync = true); - - /** - * 获å–循环日志 - * - * @return RollLogger - */ - RollLogger *logger(const string &sFile); - -private: - void initRollLogger(RollLogger *pRollLogger, const string &sFile, const string &sFormat); - -protected: - - /** - * 应用 - */ - string _app; - - /** - * æœåŠ¡å称 - */ - string _server; - - /** - * 日志路径 - */ - string _logpath; - - int _maxSize; - int _maxNum; - CommunicatorPtr _comm; - string _logObj; - - /** - * 本地线程组 - */ - TC_LoggerThreadGroup _local; - - /** - * 远程日志 - */ - map _loggers; - -}; - - -/** - * 循环日志 - */ -#define NODE_LOG(x) (RollLoggerManager::getInstance()->logger(x)) -#endif - diff --git a/cpp/framework/NodeServer/NodeServer.cpp b/cpp/framework/NodeServer/NodeServer.cpp deleted file mode 100644 index b4fa039e1..000000000 --- a/cpp/framework/NodeServer/NodeServer.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NodeServer.h" -#include "NodeImp.h" -#include "ServerImp.h" -#include "RegistryProxy.h" -#include "NodeRollLogger.h" - -string NodeServer::g_sNodeIp; - -BatchPatch *g_BatchPatchThread; -RemoveLogManager *g_RemoveLogThread; - -void NodeServer::initialize() -{ - //滚动日志也打å°æ¯«ç§’ - TarsRollLogger::getInstance()->logger()->modFlag(TC_DayLogger::HAS_MTIME); - - //node使用的循环日志åˆå§‹åŒ– - RollLoggerManager::getInstance()->setLogInfo(ServerConfig::Application, - ServerConfig::ServerName, ServerConfig::LogPath, - ServerConfig::LogSize, ServerConfig::LogNum, _communicator, - ServerConfig::Log); - - initRegistryObj(); - - addConfig("tarsnode.conf"); - - //增加对象 - string sNodeObj = ServerConfig::Application + "." + ServerConfig::ServerName + ".NodeObj"; - string sServerObj = ServerConfig::Application + "." + ServerConfig::ServerName + ".ServerObj"; - - addServant(sNodeObj); - addServant(sServerObj); - - TLOGDEBUG("NodeServer::initialize ServerAdapter " << (getAdapterEndpoint("ServerAdapter")).toString() << endl); - TLOGDEBUG("NodeServer::initialize NodeAdapter " << (getAdapterEndpoint("NodeAdapter")).toString() << endl); - - g_sNodeIp = getAdapterEndpoint("NodeAdapter").getHost(); - - if (!ServerFactory::getInstance()->loadConfig()) - { - TLOGERROR("NodeServer::initialize ServerFactory loadConfig failure" << endl); - } - - //查看æœåŠ¡desc - TARS_ADD_ADMIN_CMD_PREFIX("tars.serverdesc", NodeServer::cmdViewServerDesc); - TARS_ADD_ADMIN_CMD_PREFIX("reloadconfig", NodeServer::cmdReLoadConfig); - - initHashMap(); - - //å¯åŠ¨KeepAliveThread - _keepAliveThread = new KeepAliveThread(); - _keepAliveThread->start(); - - TLOGDEBUG("NodeServer::initialize |KeepAliveThread start" << endl); - - _reportMemThread = new ReportMemThread(); - _reportMemThread->start(); - - TLOGDEBUG("NodeServer::initialize |_reportMemThread start" << endl); - - //å¯åŠ¨æ‰¹é‡å‘布线程 - PlatformInfo plat; - int iThreads = TC_Common::strto(g_pconf->get("/tars/node", "10")); - - _batchPatchThread = new BatchPatch(); - _batchPatchThread->setPath(plat.getDownLoadDir()); - _batchPatchThread->start(iThreads); - - g_BatchPatchThread = _batchPatchThread; - - TLOGDEBUG("NodeServer::initialize |BatchPatchThread start(" << iThreads << ")" << endl); - - _removeLogThread = new RemoveLogManager(); - _removeLogThread->start(iThreads); - - g_RemoveLogThread = _removeLogThread; - - TLOGDEBUG("NodeServer::initialize |RemoveLogThread start(" << iThreads << ")" << endl); -} - -void NodeServer::initRegistryObj() -{ - string sLocator = Application::getCommunicator()->getProperty("locator"); - vector vtLocator = TC_Common::sepstr(sLocator, "@"); - TLOGDEBUG("locator:" << sLocator << endl); - if (vtLocator.size() == 0) - { - TLOGERROR("NodeServer::initRegistryObj failed to parse locator" << endl); - exit(1); - } - - vector vObj = TC_Common::sepstr(vtLocator[0], "."); - if (vObj.size() != 3) - { - TLOGERROR("NodeServer::initRegistryObj failed to parse locator" << endl); - exit(1); - } - - //获å–主控åå­—çš„å‰ç¼€ - string sObjPrefix(""); - string::size_type pos = vObj[2].find("QueryObj"); - if (pos != string::npos) - { - sObjPrefix = vObj[2].substr(0, pos); - } - - AdminProxy::getInstance()->setRegistryObjName("tars.tarsregistry." + sObjPrefix + "RegistryObj", - "tars.tarsregistry." + sObjPrefix + "QueryObj"); - - TLOGDEBUG("NodeServer::initRegistryObj RegistryObj:" << ("tars.tarsregistry." + sObjPrefix + "RegistryObj") << endl); - TLOGDEBUG("NodeServer::initRegistryObj QueryObj:" << ("tars.tarsregistry." + sObjPrefix + "QueryObj") << endl); -} - -void NodeServer::initHashMap() -{ - TLOGDEBUG("NodeServer::initHashMap " << endl); - - string sFile = ServerConfig::DataPath + "/" + g_pconf->get("/tars/node/hashmap", "__tarsnode_servers"); - string sPath = TC_File::extractFilePath(sFile); - int iMinBlock = TC_Common::strto(g_pconf->get("/tars/node/hashmap", "500")); - int iMaxBlock = TC_Common::strto(g_pconf->get("/tars/node/hashmap", "500")); - float iFactor = TC_Common::strto(g_pconf->get("/tars/node/hashmap", "1")); - int iSize = TC_Common::toSize(g_pconf->get("/tars/node/hashmap"), 1024 * 1024 * 10); - - if (!TC_File::makeDirRecursive(sPath)) - { - TLOGDEBUG("NodeServer::initHashMap cannot create hashmap file " << sPath << endl); - exit(0); - } - - try - { - g_serverInfoHashmap.initDataBlockSize(iMinBlock, iMaxBlock, iFactor); - g_serverInfoHashmap.initStore(sFile.c_str(), iSize); - - TLOGDEBUG("NodeServer::initHashMap init hash map succ" << endl); - } - catch (TC_HashMap_Exception& e) - { - TC_File::removeFile(sFile, false); - runtime_error(e.what()); - } -} - -TC_Endpoint NodeServer::getAdapterEndpoint(const string& name) const -{ - TLOGINFO("NodeServer::getAdapterEndpoint:" << name << endl); - - TC_EpollServerPtr pEpollServerPtr = Application::getEpollServer(); - assert(pEpollServerPtr); - - TC_EpollServer::BindAdapterPtr pBindAdapterPtr = pEpollServerPtr->getBindAdapter(name); - assert(pBindAdapterPtr); - - return pBindAdapterPtr->getEndpoint(); -} - -bool NodeServer::cmdViewServerDesc(const string& command, const string& params, string& result) -{ - TLOGINFO("NodeServer::cmdViewServerDesc" << command << " " << params << endl); - - vector v = TC_Common::sepstr(params, "."); - if (v.size() != 2) - { - result = "invalid params:" + params; - return false; - } - - string application = v[0]; - string serverName = v[1]; - - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer(application, serverName); - if (pServerObjectPtr) - { - ostringstream os; - pServerObjectPtr->getServerDescriptor().display(os); - result = os.str(); - return false; - } - - result = "server " + params + " not exist"; - - return true; -} - -bool NodeServer::cmdReLoadConfig(const string& command, const string& params, string& result) -{ - TLOGDEBUG("NodeServer::cmdReLoadConfig " << endl); - - bool bRet = false; - - if (addConfig("tarsnode.conf")) - { - bRet = ServerFactory::getInstance()->loadConfig(); - } - - string s = bRet ? "OK" : "failure"; - - result = "cmdReLoadConfig " + s; - - return bRet; -} - -void NodeServer::destroyApp() -{ - if (_keepAliveThread) - { - delete _keepAliveThread; - _keepAliveThread = NULL; - } - - if (_reportMemThread) - { - delete _reportMemThread; - _reportMemThread = NULL; - } - - if (_batchPatchThread) - { - delete _batchPatchThread; - _batchPatchThread = NULL; - } - - if (_removeLogThread) - { - delete _removeLogThread; - _removeLogThread = NULL; - } - - TLOGDEBUG("NodeServer::destroyApp "<< pthread_self() << endl); -} - -string tostr(const set& setStr) -{ - string str = "{"; - set::iterator it = setStr.begin(); - int i = 0; - for (; it != setStr.end(); it++, i++) - { - if (i > 0) - { - str += "," + *it; - } - else - { - str += *it; - } - } - str += "}"; - - return str; -} - -bool NodeServer::isValid(const string& ip) -{ - static time_t g_tTime = 0; - static set g_ipSet; - - time_t tNow = TNOW; - - static TC_ThreadLock g_tMutex; - - TC_ThreadLock::Lock lock(g_tMutex); - if (tNow - g_tTime > 60) - { - string objs = g_pconf->get("/tars/node", "tars.tarsregistry.AdminRegObj:tars.tarsAdminRegistry.AdminRegObj"); - string ips = g_pconf->get("/tars/node", "172.25.38.208:172.25.38.208"); - - TLOGDEBUG("NodeServer::isValid objs:" << objs << "|ips:" << ips << endl); - - vector vObj = TC_Common::sepstr(objs, ":"); - - vector vIp = TC_Common::sepstr(ips, ":"); - for (size_t i = 0; i < vIp.size(); i++) - { - g_ipSet.insert(vIp[i]); - TLOGDEBUG(ips << "g_ipSet insert ip:" << vIp[i] << endl); - } - - for (size_t i = 0; i < vObj.size(); i++) - { - set tempSet; - string obj = vObj[i]; - try - { - - QueryFPrx queryPrx = Application::getCommunicator()->stringToProxy(obj); - vector vActiveEp, vInactiveEp; - queryPrx->tars_endpointsAll(vActiveEp, vInactiveEp); - - for (unsigned i = 0; i < vActiveEp.size(); i++) - { - tempSet.insert(vActiveEp[i].host()); - } - - for (unsigned i = 0; i < vInactiveEp.size(); i++) - { - tempSet.insert(vInactiveEp[i].host()); - } - - TLOGDEBUG("NodeServer::isValid "<< obj << "|tempSet.size():" << tempSet.size() << "|" << tostr(tempSet) << endl); - } - catch (exception& e) - { - TLOGERROR("NodeServer::isValid catch error: " << e.what() << endl); - } - catch (...) - { - TLOGERROR("NodeServer::isValid catch error: " << endl); - } - - if (tempSet.size() > 0) - { - g_ipSet.insert(tempSet.begin(), tempSet.end()); - } - } - - TLOGDEBUG("NodeServer::isValid g_ipSet.size():" << g_ipSet.size() << "|" << tostr(g_ipSet) << endl); - g_tTime = tNow; - } - - if (g_ipSet.count(ip) > 0) - { - return true; - } - - if (g_sNodeIp == ip) - { - return true; - } - - return false; -} - -void NodeServer::reportServer(const string& sServerId, const string& sResult) -{ - try - { - //上报到notify - NotifyPrx pNotifyPrx = Application::getCommunicator()->stringToProxy(ServerConfig::Notify); - if (pNotifyPrx && sResult != "") - { - pNotifyPrx->async_reportServer(NULL, sServerId, "", sResult); - } - } - catch (exception& ex) - { - TLOGERROR("NodeServer::reportServer error:" << ex.what() << endl); - } -} diff --git a/cpp/framework/NodeServer/NodeServer.h b/cpp/framework/NodeServer/NodeServer.h deleted file mode 100644 index a9d897541..000000000 --- a/cpp/framework/NodeServer/NodeServer.h +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __NODE_SERVER_H_ -#define __NODE_SERVER_H_ - -#include "servant/Application.h" -#include "KeepAliveThread.h" -#include "ReportMemThread.h" -#include "QueryF.h" -#include "BatchPatchThread.h" -#include "RemoveLogThread.h" -#include "util.h" - -using namespace tars; - -const string NODE_VERSION="B001"; - -class NodeServer; - -extern NodeServer g_app; - -class NodeServer : public Application -{ -public: - /** - * 获å–Adapter Endpoint - */ - TC_Endpoint getAdapterEndpoint(const string& name ) const; - - /** - * 添加Config - * @param filename - */ - bool addConfig(const string &filename){return Application::addConfig(filename);} - - /* - * æƒé™æŽ§åˆ¶ - */ - bool isValid(const string& ip); - - /* - * 上报string至notify - */ - void reportServer(const string &sServerId,const string &sResult); - -public: - /* - * 管ç†æŒ‡ä»¤ - */ - bool cmdViewServerDesc(const string& command, const string& params, string& result); - /** - *@brief 加载æœåŠ¡é…置文件,并使之生效 - * - */ - bool cmdReLoadConfig(const string& command, const string& params, string& result); - -protected: - - /** - * åˆå§‹åŒ–, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžå¤Ÿ, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void destroyApp(); - - /** - * åˆå§‹åŒ–hashmap - */ - void initHashMap(); -private: - /** - *åˆå§‹åŒ–主控objåå­— - */ - void initRegistryObj(); -private: - KeepAliveThread * _keepAliveThread; - ReportMemThread * _reportMemThread; - - BatchPatch * _batchPatchThread; - RemoveLogManager * _removeLogThread; - - static string g_sNodeIp; -}; -#endif - - diff --git a/cpp/framework/NodeServer/PlatformInfo.cpp b/cpp/framework/NodeServer/PlatformInfo.cpp deleted file mode 100644 index 855e34253..000000000 --- a/cpp/framework/NodeServer/PlatformInfo.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PlatformInfo.h" -#include "util/tc_clientsocket.h" -#include "NodeServer.h" - -NodeInfo PlatformInfo::getNodeInfo() const -{ - NodeInfo tNodeInfo; - tNodeInfo.nodeName = getNodeName(); - tNodeInfo.dataDir = getDataDir(); - TC_Endpoint tEndPoint = g_app.getAdapterEndpoint("NodeAdapter"); - tNodeInfo.nodeObj = ServerConfig::Application + "." + ServerConfig::ServerName + ".NodeObj@" + tEndPoint.toString(); - tNodeInfo.endpointIp = tEndPoint.getHost(); - tNodeInfo.endpointPort = tEndPoint.getPort(); - tNodeInfo.timeOut = tEndPoint.getTimeout(); - tNodeInfo.version = TARS_VERSION+string("_")+NODE_VERSION; - - return tNodeInfo; -} - -LoadInfo PlatformInfo::getLoadInfo() const -{ - LoadInfo info; - info.avg1 = -1.0f; - info.avg5 = -1.0f; - info.avg15 = -1.0f; - - double loadAvg[3]; - if ( getloadavg( loadAvg, 3 ) != -1 ) - { - info.avg1 = static_cast( loadAvg[0] ); - info.avg5 = static_cast( loadAvg[1] ); - info.avg15 = static_cast( loadAvg[2] ); - } - - return info; -} - -string PlatformInfo::getNodeName() const -{ - return ServerConfig::LocalIp; -} - -string PlatformInfo::getDataDir() const -{ - string sDataDir; - sDataDir = ServerConfig::DataPath; - - if ( TC_File::isAbsolute(sDataDir) == false) - { - char cwd[PATH_MAX]; - if ( getcwd( cwd, PATH_MAX ) == NULL ) - { - TLOGERROR("PlatformInfo::getDataDir cannot get the current directory:\n" << endl); - exit( 0 ); - } - sDataDir = string(cwd) + '/' + sDataDir; - } - - sDataDir = TC_File::simplifyDirectory(sDataDir); - if ( sDataDir[sDataDir.length() - 1] == '/' ) - { - sDataDir = sDataDir.substr( 0, sDataDir.length() - 1 ); - } - - return sDataDir; -} - -string PlatformInfo::getDownLoadDir() const -{ - string sDownLoadDir = ""; - try - { - sDownLoadDir = g_app.getConfig().get("/tars/node",""); - if(sDownLoadDir == "") - { - string sDataDir = getDataDir(); - string::size_type pos = sDataDir.find_last_of("/"); - if(pos != string::npos) - { - sDownLoadDir = sDataDir.substr(0,pos)+"/tmp/download/"; - } - } - - sDownLoadDir = TC_File::simplifyDirectory(sDownLoadDir); - if(!TC_File::makeDirRecursive( sDownLoadDir )) - { - TLOGERROR("getDownLoadDir property `tars/node' is not set and cannot create dir:"< -#include "util/tc_config.h" -#include -#include "servant/Application.h" - -using namespace tars; -using namespace std; - -extern TC_Config* g_pconf; - -class PlatformInfo -{ -public: - /** - * 获å–nodeçš„ç›¸å…³ä¿¡æ¯ - */ - NodeInfo getNodeInfo() const; - - /** - * 获å–nodeçš„æ‰€åœ¨æœºå™¨çš„è´Ÿè½½ä¿¡æ¯ - */ - LoadInfo getLoadInfo() const; - - /** - * 获å–nodeçš„å称 - */ - string getNodeName() const; - - /** - * 获å–nodeçš„æ•°æ®ç›®å½• - */ - string getDataDir() const; - - /** - * 获å–文件下载目录 - */ - string getDownLoadDir() const; -}; - -#endif - diff --git a/cpp/framework/NodeServer/PropertyReporter.h b/cpp/framework/NodeServer/PropertyReporter.h deleted file mode 100644 index 3b7259064..000000000 --- a/cpp/framework/NodeServer/PropertyReporter.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PropertyReporter_H_ -#define __PropertyReporter_H_ - - -#include "servant/Application.h" - -//eg1: REPORT_COUNT("socket_error", 1); -//eg2: REPORT_SUM("GB_consume", 100); - - -//define reporter template -#define REPORT_INSTALL \ -template \ -PropertyReportPtr creatReport(const string & master, string msg, REPORTFUNC reportFunc) \ -{ \ - PropertyReportPtr srp = Application::getCommunicator()->getStatReport()->getPropertyReport(msg); \ - if ( !srp ) \ - { \ - srp = Application::getCommunicator()->getStatReport()->createPropertyReport(msg, reportFunc); \ - srp->setMasterName(master);\ - } \ - return srp; \ -}; - - -//count report -#define REPORT_COUNT(master, str,value) \ -PropertyReportPtr countReporter = creatReport(master, str, PropertyReport::count()); \ -countReporter->report(value); - - -//sum report -#define REPORT_SUM(master, str,value) \ -PropertyReportPtr sumReporter = creatReport(master, str, PropertyReport::sum()); \ -sumReporter->report(value); - -//max report -#define REPORT_MAX(master, str,value) \ -PropertyReportPtr maxReporter = creatReport(master, str, PropertyReport::max()); \ -maxReporter->report(value); - -//set a reporter -REPORT_INSTALL; - - -#endif // __PropertyReporter_H_ - diff --git a/cpp/framework/NodeServer/RegistryProxy.h b/cpp/framework/NodeServer/RegistryProxy.h deleted file mode 100644 index 9c5eb2f17..000000000 --- a/cpp/framework/NodeServer/RegistryProxy.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REGISTRY_PROXY_H_ -#define __REGISTRY_PROXY_H_ - -#include "Node.h" -#include "ServerFactory.h" -#include "util/tc_config.h" -#include "QueryF.h" -#include "servant/Communicator.h" -#include "util/tc_singleton.h" -#include "NodeServer.h" - -using namespace tars; -using namespace std; - -class AdminProxy; - -class AdminProxy : public TC_Singleton -{ -public: - RegistryPrx getRegistryProxy() - { - RegistryPrx pRistryPrx; - string sRegistry = g_app.getConfig().get("/tars/node",_registryProxyName); - - Application::getCommunicator()->stringToProxy(sRegistry, pRistryPrx); - - return pRistryPrx; - } - - QueryFPrx getQueryProxy() - { - QueryFPrx pQueryPrx; - string sQuery = g_app.getConfig().get("/tars/node", _queryProxyName); - - Application::getCommunicator()->stringToProxy(sQuery, pQueryPrx); - - return pQueryPrx; - } - - inline void setRegistryObjName(const string &sRegistryProxyName, const string &sQueryProxyName) - { - _registryProxyName = sRegistryProxyName; - _queryProxyName = sQueryProxyName; - } - - inline string& getRegistryProxyName(){ return _registryProxyName; } - - inline string& getQueryProxyName() { return _queryProxyName; } - -private: - - string _registryProxyName; - string _queryProxyName; -}; -#endif - - diff --git a/cpp/framework/NodeServer/RemoveLogThread.cpp b/cpp/framework/NodeServer/RemoveLogThread.cpp deleted file mode 100644 index e5857107c..000000000 --- a/cpp/framework/NodeServer/RemoveLogThread.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_md5.h" -#include "RemoveLogThread.h" -#include "ServerFactory.h" -#include "NodeRollLogger.h" -#include "util/tc_timeprovider.h" -#include "util.h" - -using namespace tars; - -RemoveLogManager::RemoveLogManager() -{ -} - -RemoveLogManager::~RemoveLogManager() -{ - terminate(); -} - -void RemoveLogManager::start(int iNum) -{ - for (int i = 0; i < iNum; i++) - { - RemoveLogThread * t = new RemoveLogThread(this); - t->start(); - - _runners.push_back(t); - } -} - -void RemoveLogManager::terminate() -{ - for (size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->terminate(); - } - } - - for (size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->getThreadControl().join(); - } - } - - _queueMutex.notifyAll(); -} - -int RemoveLogManager::push_back(const string& logPath) -{ - { - TC_ThreadLock::Lock LockQueue(_queueMutex); - - if (_reqSet.count(logPath) == 1) - { - return -1; - } - - _reqQueue.push_back(logPath); - _reqSet.insert(logPath); - } - - return 0; -} - -bool RemoveLogManager::pop_front(string& logPath) -{ - TC_ThreadLock::Lock LockQueue(_queueMutex); - - bool bRet=false; - - bRet=_reqQueue.pop_front(logPath); - - if(bRet) - { - _reqSet.erase(logPath); - } - - return bRet; -} - -void RemoveLogManager::timedWait(int millsecond) -{ - TC_ThreadLock::Lock lock(_queueMutex); - - _queueMutex.timedWait(millsecond); -} - -///////////////////////////////////////// -RemoveLogThread::RemoveLogThread(RemoveLogManager * manager) -: _manager(manager) -, _shutDown(false) -{ -} - -RemoveLogThread::~RemoveLogThread() -{ - terminate(); -} - -void RemoveLogThread::terminate() -{ - _shutDown = true; - - if (isAlive()) - { - getThreadControl().join(); - } -} - -void RemoveLogThread::run() -{ - while (!_shutDown) - { - try - { - string sLogPath; - if (_manager->pop_front(sLogPath)) - { - int64_t startMs = TC_TimeProvider::getInstance()->getNowMs(); - if (TC_File::isFileExistEx(sLogPath, S_IFDIR)) - { - int ret = TC_File::removeFile(sLogPath,true); - if (ret == 0) - { - NODE_LOG("RemoveLogThread")->debug() <getNowMs() - startMs) << endl; - } - else - { - NODE_LOG("RemoveLogThread")->error()<getNowMs() - startMs) << endl; - } - } - else - { - NODE_LOG("RemoveLogThread")->debug()<getNowMs() - startMs) << endl; - } - } - else - { - _manager->timedWait(2000); - } - - } - catch (exception& e) - { - NODE_LOG("RemoveLogThread")->error()<error()< _reqQueue; - - std::set _reqSet; //ç”¨äºŽåŽ»é‡ - - std::vector _runners; -}; - -class RemoveLogThread : public TC_Thread -{ -public: - RemoveLogThread(RemoveLogManager * manager); - - ~RemoveLogThread(); - - virtual void run(); - - void terminate(); - -protected: - RemoveLogManager * _manager; - - bool _shutDown; -}; - -#endif diff --git a/cpp/framework/NodeServer/ReportMemThread.cpp b/cpp/framework/NodeServer/ReportMemThread.cpp deleted file mode 100644 index f5e4353c6..000000000 --- a/cpp/framework/NodeServer/ReportMemThread.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ReportMemThread.h" -#include "RegistryProxy.h" -#include "NodeRollLogger.h" -#include "util/tc_timeprovider.h" -#include "util.h" - -ReportMemThread::ReportMemThread( ) -{ - _shutDown = false; - //用旧é…ç½® - _monitorInterval = TC_Common::strto(g_pconf->get("/tars/node/keepalive","2")); - _monitorInterval = _monitorInterval>10?10:(_monitorInterval<1?1:_monitorInterval); -} - -ReportMemThread::~ReportMemThread() -{ - terminate(); -} - -void ReportMemThread::terminate() -{ - NODE_LOG("ReportMemThread")->debug()<error()<error()< mmServerList = ServerFactory::getInstance()->getAllServers(); - map::const_iterator it = mmServerList.begin(); - for(;it != mmServerList.end(); it++) - { - map::const_iterator p = it->second.begin(); - for(;p != it->second.end(); p++) - { - try - { - sServerId = it->first+"."+p->first; - ServerObjectPtr pServerObjectPtr = p->second; - if(!pServerObjectPtr) - { - NODE_LOG("ReportMemThread")->debug()<reportMemProperty(); - - } - catch(exception &e) - { - NODE_LOG("ReportMemThread")->error()< -#include "ServerFactory.h" -#include "util/tc_monitor.h" -#include "util/tc_thread.h" - -using namespace tars; -using namespace std; - -class ReportMemThread : public TC_Thread//,public TC_ThreadLock -{ -public: - /** - * 构造函数 - */ - ReportMemThread(); - - /** - * æžæž„函数 - */ - ~ReportMemThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - -protected: - - virtual void run(); - - void report(); - - bool timedWait(int millsecond); - -protected: - - bool _shutDown; - int _monitorInterval; - TC_ThreadLock _lock; -}; - - -#endif - diff --git a/cpp/framework/NodeServer/ServerCommand.h b/cpp/framework/NodeServer/ServerCommand.h deleted file mode 100644 index 33c6a87d6..000000000 --- a/cpp/framework/NodeServer/ServerCommand.h +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __SERVER_COMMAND_H_ -#define __SERVER_COMMAND_H_ - -#include "ServerObject.h" -#include "NodeServer.h" - -class StatExChange; - -typedef TC_AutoPtr StatExChangePtr; - -class ServerCommand : public TC_HandleBase -{ -public: - enum ExeStatus - { - EXECUTABLE, //å¯ä»¥è¿è¡Œ - DIS_EXECUTABLE, //ä¸å¯ä»¥è¿è¡Œ - NO_NEED_EXECUTE //ä¸éœ€è¦è¿è¡Œ - }; -public: - virtual ~ServerCommand(){} - - virtual ExeStatus canExecute(string &sResult){ return EXECUTABLE;} - - virtual int execute(string &sResult) = 0; - - virtual int doProcess() - { - string s ; - return doProcess(s); - } - - virtual int doProcess(string &sResult) - { - ExeStatus t = canExecute(sResult); - if(t == DIS_EXECUTABLE) - { - return -1; - } - else if(t == NO_NEED_EXECUTE) - { - return 0; - } - - return execute(sResult); - } - - virtual int doProcess(const TarsCurrentPtr current,string &sResult, bool bValid=true) - { - if( bValid && g_app.isValid(current->getIp()) == false ) - { - sResult = " erro:ip "+ current->getIp()+" is invalid"; - return -1; - } - - return doProcess(sResult); - } -}; - - -class StatExChange : public TC_HandleBase -{ -public: - - /** - * 构造函数 - * @param pServerObjectPtr ServerObject指针 - * @param eState1 状æ€1,构造时设置pServerObjectPtr 状æ€ä¸ºstate1 - * @param eState2 状æ€2,æžæž„时设置pServerObjectPtr 状æ€ä¸ºstate2 - */ - StatExChange( const ServerObjectPtr& pServerObjectPtr, ServerObject::InternalServerState eState1, ServerObject::InternalServerState eState2 ) - { - _serverObjectPtr = pServerObjectPtr; - _state1 = eState1; - _state2 = eState2; - _serverObjectPtr->setState( _state1 ); - } - - /** - * æžå¤Ÿ - */ - ~StatExChange() - { - _serverObjectPtr->setState( _state2 ); - } - -private: - ServerObject::InternalServerState _state1; - ServerObject::InternalServerState _state2; - ServerObjectPtr _serverObjectPtr; -}; - -#endif diff --git a/cpp/framework/NodeServer/ServerFactory.cpp b/cpp/framework/NodeServer/ServerFactory.cpp deleted file mode 100644 index ad3c6b68f..000000000 --- a/cpp/framework/NodeServer/ServerFactory.cpp +++ /dev/null @@ -1,480 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ServerFactory.h" -#include "RegistryProxy.h" -#include "CommandLoad.h" -#include "NodeRollLogger.h" - -HashMap g_serverInfoHashmap; - -ServerFactory::ServerFactory() -: _bReousceLimitConfChanged(false) -, _iMinMonitorIntervalMs(60000) -, _bReportLoadInfo(true) -{ -} - -ServerObjectPtr ServerFactory::getServer( const string& application, const string& serverName ) -{ - TLOGINFO("ServerFactory::getServer: "<::const_iterator p1 = _mmServerList.find( application ); - if ( p1 != _mmServerList.end() ) - { - map::const_iterator p2 = p1->second.find( serverName ); - if ( p2 != p1->second.end() && p2->second ) - { - return p2->second; - } - } - return NULL; -} - -int ServerFactory::eraseServer( const string& application, const string& serverName ) -{ - Lock lock( *this ); - - map::const_iterator p1 = _mmServerList.find( application ); - if ( p1 == _mmServerList.end() ) - { - return 0; - } - - map::const_iterator p2 = p1->second.find( serverName ); - if ( p2 == p1->second.end() ) - { - return 0; - } - - _mmServerList[application].erase( serverName ); - if ( p1->second.empty() ) - { - _mmServerList.erase( application ); - } - - return 0; -} - -ServerObjectPtr ServerFactory::loadServer( const string& application, const string& serverName,bool enableCache,string& result) -{ - ServerObjectPtr pServerObjectPtr; - vector vServerDescriptor; - - vServerDescriptor = getServerFromRegistry(application,serverName,result); - - //å…¨é‡åŠ è½½å¤±è´¥åŽä»Žcache里é¢è¯»å– - if(vServerDescriptor.size() < 1 && enableCache == true) - { - vServerDescriptor = getServerFromCache(application,serverName,result); - } - - if(vServerDescriptor.size() < 1) - { - result += " cannot load server description from regisrty "; - return NULL; - } - - for(unsigned i = 0; i < vServerDescriptor.size(); i++) - { - pServerObjectPtr = createServer(vServerDescriptor[i],result); - } - - return pServerObjectPtr; -} - -vector ServerFactory::getServerFromRegistry( const string& application, const string& serverName, string& result) -{ - TLOGDEBUG("ServerFactory::getServerFromRegistry"< vServerDescriptor; - try - { - RegistryPrx _pRegistryPrx = AdminProxy::getInstance()->getRegistryProxy(); - - if(!_pRegistryPrx) - { - TLOGERROR("ServerFactory::getServerFromRegistry cann't get the proxy of registry. "<getServers( application, serverName, _tPlatformInfo.getNodeName()); - //清空cache - if( vServerDescriptor.size()> 0 && application == "" && serverName == "") - { - g_serverInfoHashmap.clear(); - TLOGINFO("ServerFactory::getServerFromRegistry hashmap clear ok "< ServerFactory::getServerFromCache( const string& application, const string& serverName, string& result) -{ - TLOGDEBUG("ServerFactory::getServerFromCache: "<< application <<"."< vServerDescriptor; - if(application != "" && serverName != "") - { - tServerInfo.application = application; - tServerInfo.serverName = serverName; - if(g_serverInfoHashmap.get(tServerInfo,tServerDescriptor) == TC_HashMap::RT_OK) - { - vServerDescriptor.push_back(tServerDescriptor); - } - } - else - { - HashMap::lock_iterator it = g_serverInfoHashmap.beginSetTime(); - while(it != g_serverInfoHashmap.end()) - { - ServerInfo tServerInfo; - ServerDescriptor tServerDescriptor; - int ret = it->get(tServerInfo,tServerDescriptor); - if(ret != TC_HashMap::RT_OK) - { - result =result + "\n hashmap erro:"+TC_Common::tostr(ret); - TLOGERROR("ServerFactory::getServerFromCache "<::const_iterator p1 = _mmServerList.find( application ); - if ( p1 != _mmServerList.end() ) - { - map::const_iterator p2 = p1->second.find( serverName ); - if ( p2 != p1->second.end() && p2->second ) - { - p2->second->setServerDescriptor(tDesc); - CommandLoad command(p2->second,_tPlatformInfo.getNodeInfo()); - int iRet = command.doProcess(result); - if( iRet == 0) - { - return p2->second; - } - else - { - return NULL; - } - } - } - - ServerObjectPtr pServerObjectPtr = new ServerObject(tDesc); - CommandLoad command(pServerObjectPtr,_tPlatformInfo.getNodeInfo()); - int iRet = command.doProcess(result); - if(iRet ==0) - { - ServerObject::ServerLimitInfo tInfo; - loadLimitInfo(application, pServerObjectPtr->getServerId(), tInfo); - pServerObjectPtr->setServerLimitInfo(tInfo); - - if(tInfo.iMonitorIntervalMs < _iMinMonitorIntervalMs) - { - NODE_LOG("KeepAliveThread")->debug() <" << tInfo.iMonitorIntervalMs << "|" << pServerObjectPtr->getServerId() << endl; - _iMinMonitorIntervalMs = tInfo.iMonitorIntervalMs; - } - - if (!tInfo.bReportLoadInfo) - { - NODE_LOG("KeepAliveThread")->debug() <getServerId()< ServerFactory::getAllServers() -{ - Lock lock( *this ); - return _mmServerList; -} - -bool ServerFactory::loadConfig() -{ - try - { - //首先å–默认é…ç½® - string sDefault="/tars/app_conf/default"; - map m = g_pconf->getDomainMap(sDefault); - parseLimitInfo(m, _defaultLimitInfo,true); - NODE_LOG("KeepAliveThread")->debug()< v = g_pconf->getDomainVector("/tars/app_conf/"); - - map& appConfig = _mAppCoreConfig.getWriterData(); - for(size_t i = 0; i < v.size(); i++) - { - string path = "/tars/app_conf/" + v[i]; - TLOGDEBUG("ServerFactory::loadConfig path:" << path << endl); - map m = g_pconf->getDomainMap(path); - if(v[i] == "default") - { - continue; - } - else - { - ServerObject::ServerLimitInfo limitInfo; - parseLimitInfo(m, limitInfo,false); - - limitInfo.bCloseCore = false; - limitInfo.eCoreType = ServerObject::EM_AUTO_LIMIT; - - vector apps = TC_Common::sepstr(g_pconf->get(string("/tars/app_conf<") +v[i]+ ">",""),"|"); - for(size_t i = 0; i < apps.size(); i++) - { - string app = apps[i]; - appConfig[app] = limitInfo; - } - } - } - _mAppCoreConfig.swap(); - - vector vCloseCoreSrvs = TC_Common::sepstr(g_pconf->get("/tars/app_conf",""),"|"); - vector::const_iterator it = vCloseCoreSrvs.begin(); - - map& serverConfig = _mServerCoreConfig.getWriterData(); - for(;it != vCloseCoreSrvs.end();it++) - { - ServerObject::ServerLimitInfo tInfo = _defaultLimitInfo; - tInfo.bCloseCore = true; - tInfo.eCoreType = ServerObject::EM_MANUAL_LIMIT; - - serverConfig[*it] = tInfo; - TLOGDEBUG("ServerFactory::loadConfig Load ClosecoreLimit:" << *it <& confMap, ServerObject::ServerLimitInfo& limitInfo,bool bDefault) -{ - map::const_iterator it = confMap.find("maxstopcount"); - if(it != confMap.end()) - { - limitInfo.iMaxExcStopCount = TC_Common::strto(it->second); - } - else - { - limitInfo.iMaxExcStopCount=(bDefault ? 3 :_defaultLimitInfo.iMaxExcStopCount); - } - - it = confMap.find("coretimeinterval"); - if(it != confMap.end()) - { - limitInfo.iCoreLimitTimeInterval = TC_Common::strto(it->second); - } - else - { - limitInfo.iCoreLimitTimeInterval = (bDefault ? 5 : _defaultLimitInfo.iCoreLimitTimeInterval); - } - - it = confMap.find("coretimeexpired"); - if(it != confMap.end()) - { - limitInfo.iCoreLimitExpiredTime = TC_Common::strto(it->second); - } - else - { - limitInfo.iCoreLimitExpiredTime =(bDefault ? 30 :_defaultLimitInfo.iCoreLimitExpiredTime); - } - - it = confMap.find("corelimitenable"); - if(it != confMap.end()) - { - limitInfo.bEnableCoreLimit = it->second == "true"; - } - else - { - limitInfo.bEnableCoreLimit = (bDefault ? false :_defaultLimitInfo.bEnableCoreLimit); - } - - it = confMap.find("monitorIntervalMs"); - if(it != confMap.end()) - { - limitInfo.iMonitorIntervalMs = TC_Common::strto(it->second); - } - else - { - limitInfo.iMonitorIntervalMs =(bDefault ? 1000 : _defaultLimitInfo.iMonitorIntervalMs); - } - - it = confMap.find("ActivatorMaxCount"); - if(it != confMap.end()) - { - limitInfo.iActivatorMaxCount = TC_Common::strto(it->second); - } - else - { - limitInfo.iActivatorMaxCount =(bDefault ? 10 : _defaultLimitInfo.iActivatorMaxCount); - } - - it = confMap.find("ActivatorTimeInterval"); - if(it != confMap.end()) - { - limitInfo.iActivatorTimeInterval = TC_Common::strto(it->second); - } - else - { - limitInfo.iActivatorTimeInterval =(bDefault ? 60 : _defaultLimitInfo.iActivatorTimeInterval); - } - - it = confMap.find("ActivatorPunishInterval"); - if(it != confMap.end()) - { - limitInfo.iActivatorPunishInterval = TC_Common::strto(it->second); - } - else - { - limitInfo.iActivatorPunishInterval =(bDefault ? 600 : _defaultLimitInfo.iActivatorPunishInterval); - } - - it=confMap.find("reportLoadInfoenable"); - if (it!=confMap.end()) - { - limitInfo.bReportLoadInfo=TC_Common::strto(it->second)==1 ? true : false; - } - else - { - limitInfo.bReportLoadInfo=(bDefault ? true : _defaultLimitInfo.bReportLoadInfo); - } - -} - -void ServerFactory::setAllServerResourceLimit() -{ - Lock lock( *this ); - - //有更新æ‰åŠ è½½ - if(_bReousceLimitConfChanged) - { - - map::const_iterator it = _mmServerList.begin(); - for(;it != _mmServerList.end(); it++) - { - map::const_iterator p = it->second.begin(); - for(;p != it->second.end(); p++) - { - string sAppId = it->first; - string sServerId = it->first+"."+p->first; - ServerObjectPtr pServerObjectPtr = p->second; - if(!pServerObjectPtr) - { - continue; - } - - ServerObject::ServerLimitInfo tInfo; - loadLimitInfo(sAppId, sServerId, tInfo); - if(tInfo.iMonitorIntervalMs < _iMinMonitorIntervalMs) - { - NODE_LOG("KeepAliveThread")->debug()<" << tInfo.iMonitorIntervalMs << "|" << sServerId << endl; - _iMinMonitorIntervalMs = tInfo.iMonitorIntervalMs; - } - - TLOGDEBUG("ServerFactory::setAllServerResourceLimit setAllServerResourceLimit|" << sServerId <<"|"<setServerLimitInfo(tInfo); - - } - } - _bReousceLimitConfChanged = false; - } -} - -void ServerFactory::loadLimitInfo(const string& sAppId, const string& sServerId, ServerObject::ServerLimitInfo& tInfo) { - - map& appConfig = _mAppCoreConfig.getReaderData(); - map& serverConfig = _mServerCoreConfig.getReaderData(); - - if(serverConfig.count(sServerId) == 1) - { - tInfo = serverConfig[sServerId]; - } - else if(appConfig.count(sAppId) == 1) - { - tInfo = appConfig[sAppId]; - } - else - { - tInfo = _defaultLimitInfo; - tInfo.bCloseCore = false; - tInfo.eCoreType = ServerObject::EM_AUTO_LIMIT; - } - -} - - diff --git a/cpp/framework/NodeServer/ServerFactory.h b/cpp/framework/NodeServer/ServerFactory.h deleted file mode 100644 index f26890c97..000000000 --- a/cpp/framework/NodeServer/ServerFactory.h +++ /dev/null @@ -1,201 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __SERVER_FACTORY_H_ -#define __SERVER_FACTORY_H_ -#include "Node.h" -#include -#include "ServerObject.h" -#include "util/tc_common.h" -#include "jmem/jmem_hashmap.h" -#include "util/tc_file.h" -#include "util/tc_singleton.h" -#include "util/tc_readers_writer_data.h" - -using namespace tars; -using namespace std; - -typedef TarsHashMap HashMap; - -extern HashMap g_serverInfoHashmap; - -typedef map ServerGroup; - -class ServerFactory : public TC_Singleton, public TC_ThreadLock -{ -public: - /** - * 构造函数 - */ - ServerFactory(); - - /** - * æžæž„函数 - */ - ~ServerFactory() - { - }; - - /** - * 创建指定æœåŠ¡ - * @param ServerDescriptor æœåŠ¡ä¿¡æ¯ - * @return ServerObjectPtr - */ - ServerObjectPtr createServer( const ServerDescriptor& tDesc) - { - string result; - return createServer(tDesc,result); - } - - /** - * 创建指定æœåŠ¡ - * @param ServerDescriptor æœåŠ¡ä¿¡æ¯ - * @param result 结果 - * @return ServerObjectPtr - */ - ServerObjectPtr createServer( const ServerDescriptor& tDesc,string& result); - - /** - * 删除指定æœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int eraseServer( const string& application, const string& serverName ); - - /** - * 获å–指定æœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return ServerObjectPtr æœåŠ¡ä¸å­˜åœ¨è¿”回NULL - */ - ServerObjectPtr getServer( const string& application, const string& serverName ); - - /** - * loadæœåŠ¡ è‹¥application - * serverName为空load所有æœåŠ¡ï¼Œåªè¿”回最åŽä¸€ä¸ªload对象。 - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return ServerObjectPtr æœåŠ¡ä¸å­˜åœ¨è¿”回NULL - */ - ServerObjectPtr loadServer( const string& application="", const string& serverName="",bool enableCache = true) - { - string result; - return loadServer(application,serverName,enableCache,result); - } - - /** - * loadæœåŠ¡ è‹¥application - * serverName为空load所有æœåŠ¡ï¼Œåªè¿”回最åŽä¸€ä¸ªload对象。 - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @param result 结果 - * @return ServerObjectPtr æœåŠ¡ä¸å­˜åœ¨è¿”回NULL - */ - ServerObjectPtr loadServer( const string& application, const string& serverName,bool enableCache, string& result); - - /** - * getæœåŠ¡ è‹¥application - * serverName为空获å–所有æœåŠ¡ï¼Œåªè¿”回最åŽä¸€ä¸ªload对象。 - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return vector æœåŠ¡ä¸å­˜åœ¨è¿”回NULL - */ - vector getServerFromRegistry( const string& application, const string& serverName, string& result); - - /** - * getæœåŠ¡ è‹¥application - * serverName为空获å–所有æœåŠ¡ï¼Œåªè¿”回最åŽä¸€ä¸ªload对象。 - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return vector æœåŠ¡ä¸å­˜åœ¨è¿”回NULL - */ - vector getServerFromCache( const string& application, const string& serverName, string& result); - - /** - * 获å–node上æœåŠ¡æ‰€æœ‰æœåŠ¡ - * @return map - */ - map getAllServers(); - - /** - *@brief 加载æœåŠ¡core属性的é…置文件 - *@return bool - */ - bool loadConfig(); - - /** - *@brief 设置node上所有æœåŠ¡çš„core属性,如果node更新过coreé…置,则ä¸éœ€è¦è®¾ç½® - * - */ - void setAllServerResourceLimit(); - - /** - * 获å–最å°çš„监控周期 - */ - int32_t getMinMonitorIntervalMs() - { - return _iMinMonitorIntervalMs; - } - - /** - * 是å¦ä¸ŠæŠ¥èŠ‚ç‚¹è´Ÿè½½ä¿¡æ¯ - */ - bool getReportLoadInfo() {return _bReportLoadInfo;} - -private: - - /** - * 解æžnode管ç†æœåŠ¡çš„limit资æºé…ç½® - */ - void parseLimitInfo(const map& confMap, ServerObject::ServerLimitInfo& limitInfo, bool bDefault); - - /** - * 获å–æœåŠ¡è¿›ç¨‹çš„limit资æºé…ç½® - */ - void loadLimitInfo(const string& sAppId, const string& sServerId, ServerObject::ServerLimitInfo& tInfo); - -private: - //> - map _mmServerList; - - PlatformInfo _tPlatformInfo; - -private: - - // - TC_ReadersWriterData > _mServerCoreConfig; - - // - TC_ReadersWriterData > _mAppCoreConfig; - - //é…置是å¦æœ‰æ›´æ–° - bool _bReousceLimitConfChanged; - - //最大æœåŠ¡å¼‚常åœæ­¢ä¸ªæ•°ï¼Œè¶…过é™åˆ¶åˆ™è‡ªåŠ¨å±è”½coredump - ServerObject::ServerLimitInfo _defaultLimitInfo; - - //最å°çš„监控周期 - int32_t _iMinMonitorIntervalMs; - - //是å¦ä¸ŠæŠ¥èŠ‚点负载信æ¯,默认上报 - bool _bReportLoadInfo; -}; - - -#endif - - diff --git a/cpp/framework/NodeServer/ServerImp.cpp b/cpp/framework/NodeServer/ServerImp.cpp deleted file mode 100644 index eacdba04d..000000000 --- a/cpp/framework/NodeServer/ServerImp.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ServerImp.h" -#include "util.h" - -int ServerImp::keepAlive( const tars::ServerInfo& serverInfo, tars::TarsCurrentPtr current ) -{ - try - { - string sApp = serverInfo.application; - string sName = serverInfo.serverName; - - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( sApp, sName ); - if(pServerObjectPtr) - { - TLOGDEBUG("ServerImp::keepAlive server " << serverInfo.application << "." << serverInfo.serverName << " keep alive"<< endl); - - pServerObjectPtr->keepAlive(serverInfo.pid,serverInfo.adapter); - - return 0; - } - - TLOGDEBUG("ServerImp::keepAlive server " << serverInfo.application << "." << serverInfo.serverName << " is not exist"<< endl); - } - catch ( exception& e ) - { - TLOGERROR( "ServerImp::keepAlive catch exception :" << e.what() << endl); - } - catch ( ... ) - { - TLOGERROR("ServerImp::keepAlive unkown exception catched" << endl); - } - - return -1; -} - -int ServerImp::reportVersion( const string &app,const string &serverName,const string &version,tars::TarsCurrentPtr current) -{ - try - { - TLOGDEBUG("ServerImp::reportVersion|server|" << app << "." << serverName << "|version|" << version<< endl); - - ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( app, serverName ); - if(pServerObjectPtr) - { - pServerObjectPtr->setVersion(version); - - return 0; - } - - TLOGDEBUG("ServerImp::reportVersion server " << app << "." << serverName << " is not exist"<< endl); - } - catch ( exception& e ) - { - TLOGERROR("ServerImp::reportVersion catch exception :" << e.what() << endl); - } - catch ( ... ) - { - TLOGERROR("ServerImp::reportVersion unkown exception catched" << endl); - } - - return -1; -} - - diff --git a/cpp/framework/NodeServer/ServerImp.h b/cpp/framework/NodeServer/ServerImp.h deleted file mode 100644 index 61517b0fb..000000000 --- a/cpp/framework/NodeServer/ServerImp.h +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __SERVER_IMP_H_ -#define __SERVER_IMP_H_ -#include "Node.h" -#include -#include "ServerFactory.h" -#include "util/tc_common.h" - - - -using namespace tars; -using namespace std; - -class ServerImp : public ServerF -{ -public: - /** - * 构造函数 - */ - ServerImp() - { - } - - - /** - * æžæž„函数 - */ - virtual ~ServerImp() - { - } - - /** - * åˆå§‹åŒ– - */ - virtual void initialize() - { - }; - - /** - * 退出 - */ - virtual void destroy() - { - }; - - /** - * 上报心跳 - */ - virtual int keepAlive( const tars::ServerInfo& serverInfo, tars::TarsCurrentPtr current ) ; - - /** - * 上报tars版本 - */ - virtual int reportVersion( const string &app,const string &serverName,const string &version,tars::TarsCurrentPtr current) ; - -private: -}; - -typedef TC_AutoPtr ServerImpPtr; - -#endif - diff --git a/cpp/framework/NodeServer/ServerLimitResource.cpp b/cpp/framework/NodeServer/ServerLimitResource.cpp deleted file mode 100644 index 460a757b3..000000000 --- a/cpp/framework/NodeServer/ServerLimitResource.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ServerLimitResource.h" -#include "util/tc_timeprovider.h" -#include "servant/TarsLogger.h" -#include "NodeRollLogger.h" -#include "util.h" - -ServerLimitResource::ServerLimitResource( int iMaxCount,int iTimeInterval/*分钟*/ ,int iExpiredTime/*分钟*/,const string& app,const string& servername) -: _maxExcStopCount(iMaxCount) -, _coreLimitTimeInterval(iTimeInterval*60) -, _coreLimitExpiredTime(iExpiredTime*60) -, _appName(app) -, _serverName(servername) -{ - resetRunTimeData(); -} - -void ServerLimitResource::resetRunTimeData() -{ - time_t tNow = TNOW; - _closeCore = false; - _curExcStopCount = 0; - _excStopRecordTime = tNow; - _enableCoreLimitTime = 0; -} - -void ServerLimitResource::setLimitCheckInfo(int iMaxCount,int iTimeInterval,int iExpiredTime) -{ - TC_ThreadLock::Lock lock(*this); - _maxExcStopCount = (iMaxCount>0)?iMaxCount:1; - _coreLimitTimeInterval = (iTimeInterval>0?iTimeInterval:1)*60; - _coreLimitExpiredTime = (iExpiredTime>0?iExpiredTime:1)*60; -} - -void ServerLimitResource::addExcStopRecord() -{ - time_t tNow = TNOW; - - TC_ThreadLock::Lock lock(*this); - //在å…许的时间段内æ‰æ£€æŸ¥æ˜¯å¦å¢žåŠ è®¡æ•°å’Œå…³é—­core属性 - NODE_LOG("core")->debug() << FILE_FUN<< (_appName+"."+_serverName) << "|before|" << tNow << "|" << _excStopRecordTime - << "|" << _coreLimitTimeInterval << "|" << _curExcStopCount << "|" << _closeCore << "|" << _maxExcStopCount << endl; - if((tNow - _excStopRecordTime) < _coreLimitTimeInterval) - { - _curExcStopCount++; - if(!_closeCore && (_curExcStopCount >= _maxExcStopCount)) - { - _closeCore = true; - _enableCoreLimitTime = tNow; - } - } - else - { - //é‡æ–°è®¡æ•° - _curExcStopCount = 1; - _excStopRecordTime = tNow; - } - NODE_LOG("core")->debug() << FILE_FUN << (_appName+"."+_serverName) << "|after|" << tNow << "|" << _excStopRecordTime - << "|" << _coreLimitTimeInterval << "|" << _curExcStopCount << "|" << _closeCore << "|" << _maxExcStopCount << endl; -} - -int ServerLimitResource::IsCoreLimitNeedClose(bool& bClose) -{ - time_t tNow = TNOW; - - int ret = 0; - if(_closeCore) - { - if((tNow -_enableCoreLimitTime) < _coreLimitExpiredTime) - { - bClose = true; - ret = 1; - } - //如果coreå±è”½è¿‡æœŸï¼Œåˆ™é‡æ–°æ‰“å¼€ - else - { - TC_ThreadLock::Lock lock(*this); - NODE_LOG("core")->debug() <debug() < -#include -#include "util/tc_monitor.h" -#include "util/tc_autoptr.h" - -using namespace tars; -using namespace std; - -class ServerLimitResource : public TC_ThreadLock, public TC_HandleBase -{ -public: - - ServerLimitResource(int iMaxCount=3,int iTimeInterval=5/*分钟*/,int iExpiredTime=30/*分钟*/,const string& app="",const string& servername=""); - - ~ServerLimitResource(){} - -public: - void setLimitCheckInfo(int iMaxCount,int iTimeInterval,int iExpiredTime); - /** - * @brief æœåŠ¡å¼‚常é‡å¯è®°å½• - * - *1 如果在_coreLimitTimeInterval时间段内,则增加异常个数 - *2 判断异常个数,如果在时间段内达到coreé™åˆ¶_maxExcStopCount,则å±è”½core属性_closeCore - * 并记录当å‰æ—¶é—´æˆ³_enableCoreLimitTime - */ - void addExcStopRecord(); - /** - *@brief 检查是å¦éœ€è¦å…³é—­æˆ–者打开core - * - *1 如果æœåŠ¡core被关闭,则先检查被关闭的时间戳,如果过期,且本æœåŠ¡æ²¡æœ‰é…ç½®manual关闭,则打开core属性 - *2 检查core开关,如果true则关闭core,则异步通知æœåŠ¡å…³é—­ï¼Œå¦åˆ™æ‰“开开关 - * - */ - int IsCoreLimitNeedClose(bool& bClose); - - //é‡ç½®å†…部的è¿è¡Œæ—¶æ•°æ® - void resetRunTimeData(); - -private: - - //字段分为两类,è¿è¡Œæ—¶æ•°æ®å’Œé…ç½®æ•°æ® - bool _closeCore; //å±è”½core开关, è¿è¡Œæ—¶ - int _curExcStopCount; //当å‰æœåŠ¡å¼‚常åœæ­¢ä¸ªæ•°, è¿è¡Œæ—¶ - int _maxExcStopCount; //最大æœåŠ¡å¼‚常åœæ­¢ä¸ªæ•°ï¼Œé…ç½® - time_t _excStopRecordTime; //第一次异常åœæ­¢æ—¶é—´, è¿è¡Œæ—¶ - int32_t _coreLimitTimeInterval; //时间间隔内coreçš„é™åˆ¶ï¼Œå•ä½æ˜¯åˆ†é’Ÿï¼Œé…ç½® - time_t _enableCoreLimitTime; //上一次关闭core的时间, è¿è¡Œæ—¶ - int32_t _coreLimitExpiredTime; //core关闭的æŒç»­æ—¶é—´,å•ä½ä¸ºåˆ†é’Ÿï¼Œé…ç½® - - string _appName; //应用å - string _serverName; //æœåŠ¡å -}; - -typedef TC_AutoPtr ServerLimitResourcePtr; - -#endif - - diff --git a/cpp/framework/NodeServer/ServerObject.cpp b/cpp/framework/NodeServer/ServerObject.cpp deleted file mode 100644 index 7cb8bcb9d..000000000 --- a/cpp/framework/NodeServer/ServerObject.cpp +++ /dev/null @@ -1,811 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ServerObject.h" -#include "RegistryProxy.h" -#include "util/tc_clientsocket.h" -#include "servant/Communicator.h" -#include "NodeServer.h" -#include "CommandStart.h" -#include "CommandNotify.h" -#include "CommandStop.h" -#include "CommandDestroy.h" -#include "CommandAddFile.h" -#include "NodeRollLogger.h" - -ServerObject::ServerObject( const ServerDescriptor& tDesc) -: _tarsServer(true) -, _loaded(false) -, _patched(true) -, _noticed(false) -, _noticeFailTimes(0) -, _enSynState(true) -, _pid(0) -, _state(ServerObject::Inactive) -, _limitStateUpdated(false) -, _started(false) -{ - //60秒内最多å¯åŠ¨10次,达到10次å¯åŠ¨ä»å¤±è´¥åŽ,æ¯éš”600秒å†é‡è¯•ä¸€æ¬¡ - _activatorPtr = new Activator(60,10,600); - - //æœåŠ¡ç›¸å…³ä¿®æ”¹é›†ä¸­æ”¾åˆ°setServerDescriptor函数中 - setServerDescriptor(tDesc); - - _serviceLimitResource = new ServerLimitResource(5,10,60,_application,_serverName); -} - -void ServerObject::setServerDescriptor( const ServerDescriptor& tDesc ) -{ - TLOGDEBUG("ServerObject::setServerDescriptor "<< tDesc.application << "." << tDesc.serverName <::const_iterator itAdapters; - for( itAdapters = _desc.adapters.begin(); itAdapters != _desc.adapters.end(); itAdapters++) - { - _adapterKeepAliveTime[itAdapters->first] = now; - } - _adapterKeepAliveTime["AdminAdapter"] = now; - setLastKeepAliveTime(now); -} - -bool ServerObject::isAutoStart() -{ - Lock lock(*this); - TLOGDEBUG( "ServerObject::isAutoStart "<< _application << "." << _serverName <<"|"<<_enabled<<"|"<<_loaded<<"|"<<_patched<<"|"<debug()<isActivatingLimited()) - { - if(_activatorPtr->isActivatingLimited()) - { - TLOGDEBUG("ServerObject::isAutoStart " << _application << "." << _serverName <<" not allow to restart in limited state"<getRegistryProxy()->updateServer( _nodeInfo.nodeName, _application, _serverName, tServerStateInfo); - } - else - { - AdminProxy::getInstance()->getRegistryProxy()->async_updateServer(NULL, _nodeInfo.nodeName, _application, _serverName, tServerStateInfo); - } - - //日志 - stringstream ss; - tServerStateInfo.displaySimple(ss); - NODE_LOG("synState")->debug()<getRegistryProxy()->async_updateServer( NULL, _nodeInfo.nodeName, _application, _serverName, tServerStateInfo); - - //日志 - stringstream ss; - tServerStateInfo.displaySimple(ss); - NODE_LOG("synState")->debug()<sendSignal(_pid, 0); - if (iRet == 0) - { - NODE_LOG("KeepAliveThread")->debug() <error() <::iterator it1 = _adapterKeepAliveTime.begin(); - if(adapter.empty()) - { - while (it1 != _adapterKeepAliveTime.end() ) - { - it1->second = t; - it1++; - } - return; - } - map::iterator it2 = _adapterKeepAliveTime.find(adapter); - if( it2 != _adapterKeepAliveTime.end()) - { - it2->second = t; - } - else - { - TLOGERROR("ServerObject::setLastKeepAliveTime "<::const_iterator it = _adapterKeepAliveTime.find(adapter); - if( it != _adapterKeepAliveTime.end()) - { - return it->second; - } - return -1; -} - - -bool ServerObject::isTimeOut(int iTimeout) -{ - Lock lock(*this); - time_t now = TNOW; - if(now - _keepAliveTime > iTimeout) - { - TLOGERROR("ServerObject::isTimeOut server time out "<|"<::const_iterator it = _adapterKeepAliveTime.begin(); - while (it != _adapterKeepAliveTime.end() ) - { - if(now - it->second > iTimeout) - { - TLOGERROR("ServerObject::isTimeOut server "<< it->first<<" time out "<second<<"|>|"<= 100)//默认为100ms,_timeout - { - NODE_LOG("KeepAliveThread")->debug()<|"<<100<debug()<<"server start time out "<getRegistryProxy()->async_reportVersion(NULL,_application, _serverName, _nodeInfo.nodeName, version); - - }catch(...) - { - _noticed = false; - } -} - -void ServerObject::setPid(pid_t pid) -{ - Lock lock(*this); - if (pid == _pid) - { - return; - } - NODE_LOG("startServer")->debug()<99?99:iPercent; - _patchInfo.iModifyTime = TNOW; -} - -void ServerObject::setPatchResult(const string &sPatchResult,const bool bSucc) -{ - Lock lock(*this); - _patchInfo.sResult = sPatchResult; - _patchInfo.bSucc = bSucc; -} - -void ServerObject::setPatchVersion(const string &sVersion) -{ - Lock lock(*this); - _patchInfo.sVersion = sVersion; -} - -string ServerObject::getPatchVersion() -{ - Lock lock(*this); - return _patchInfo.sVersion; -} - -int ServerObject::getPatchPercent(PatchInfo &tPatchInfo) -{ - TLOGDEBUG("ServerObject::getPatchPercent"<< _application << "_" << _serverName << "|"<< _serverId<< endl); - Lock lock(*this); - TLOGDEBUG("ServerObject::getPatchPercent "<< _application << "_" << _serverName << "|"<< _serverId << "get lock" << endl); - - tPatchInfo = _patchInfo; - //是å¦æ­£åœ¨å‘布 - tPatchInfo.bPatching = (_state == ServerObject::Patching ||_state==ServerObject::BatchPatching)?true:false; - - if (tPatchInfo.bSucc == true || _state == ServerObject::Patching || _state == ServerObject::BatchPatching) - { - TLOGDEBUG("ServerObject::getPatchPercent "<< _desc.application - << "|" << _desc.serverName << "|succ:" << (tPatchInfo.bSucc?"true":"false") << "|" << toStringState(_state) << "|" << _patchInfo.iPercent << "%|" << _patchInfo.sResult << endl); - return 0; - } - - TLOGERROR("ServerObject::getPatchPercent "<< _desc.application - << "|" << _desc.serverName << "|succ:" << (tPatchInfo.bSucc?"true":"false") << "|" << toStringState(_state) << "|" << _patchInfo.iPercent << "%|" << _patchInfo.sResult << endl); - return -1; -} - -string ServerObject::decodeMacro(const string& value) const -{ - string tmp = value; - map::const_iterator it = _macro.begin(); - while(it != _macro.end()) - { - tmp = TC_Common::replace(tmp, "${" + it->first + "}", it->second); - ++it; - } - return tmp; -} - -void ServerObject::setMacro(const map& mMacro) -{ - Lock lock(*this); - map::const_iterator it1 = mMacro.begin(); - for(;it1!= mMacro.end();++it1) - { - map::iterator it2 = _macro.find(it1->first); - - if(it2 != _macro.end()) - { - it2->second = it1->second; - } - else - { - _macro[it1->first] = it1->second; - } - } -} - -void ServerObject::setScript(const string &sStartScript,const string &sStopScript,const string &sMonitorScript ) -{ - - _startScript = TC_File::simplifyDirectory(TC_Common::trim(sStartScript)); - _stopScript = TC_File::simplifyDirectory(TC_Common::trim(sStopScript)); - _monitorScript = TC_File::simplifyDirectory(TC_Common::trim(sMonitorScript)); -} - -bool ServerObject::getRemoteScript(const string &sFileName) -{ - string sFile = TC_File::simplifyDirectory(TC_Common::trim(sFileName)); - if(!sFile.empty()) - { - CommandAddFile commands(this,sFile); - commands.doProcess(); //拉å–脚本 - return true; - } - return false; -} - -void ServerObject::doMonScript() -{ - try - { - string sResult; - time_t tNow = TNOW; - if(TC_File::isAbsolute(_monitorScript) == true) //监控脚本 - { - if(_state == ServerObject::Activating||tNow - _keepAliveTime > ServantHandle::HEART_BEAT_INTERVAL) - { - map mResult; - _activatorPtr->doScript(_serverId,_monitorScript,sResult,mResult); - if(mResult.find("pid") != mResult.end() && TC_Common::isdigit(mResult["pid"]) == true) - { - TLOGDEBUG("ServerObject::doMonScript "<< _serverId << "|"<< mResult["pid"] << endl); - keepAlive(TC_Common::strto(mResult["pid"])); - } - } - } - } - catch (exception &e) - { - TLOGERROR("ServerObject::doMonScript error:" << e.what() << endl); - } -} - -void ServerObject::checkServer(int iTimeout)//checkServer时对æœåŠ¡æ‰€å ç”¨çš„内存上报到主控 -{ - try - { - string sResult; - NODE_LOG("KeepAliveThread")->debug() <debug()<debug()<debug()<debug()<debug() < 0?_timeout:iTimeout; - if( _state != ServerObject::Inactive && isTimeOut(iRealTimeout)) - { - sResult = "[alarm] zombie process,no keep alive msg for " + TC_Common::tostr(iRealTimeout) + " seconds"; - NODE_LOG("KeepAliveThread")->debug()<debug() <debug() <addExcStopRecord(); - } - } - } - catch(exception &ex) - { - NODE_LOG("KeepAliveThread")->error()<error()<debug()<>改æˆä¸ŠæŠ¥ç‰©ç†å†…å­˜ - vector vtStatm = TC_Common::sepstr(stream, " "); - if (vtStatm.size() < 2) - { - NODE_LOG("ReportMemThread")->error() <(stream) * 4); - NODE_LOG("ReportMemThread")->debug()<(stream)*4<<")OK."<error()<error()<error() << FILE_FUN << " ex:" << ex.what() << endl; - TLOGERROR(FILE_FUN << " ex:" << ex.what() << endl); - } - catch(...) - { - NODE_LOG("ReportMemThread")->error() << FILE_FUN << " unknown error" << endl; - TLOGERROR(FILE_FUN << "unknown ex." << endl); - } -} - -void ServerObject::checkCoredumpLimit() -{ - if(_state != ServerObject::Active || !_limitStateInfo.bEnableCoreLimit) - { - TLOGINFO(FILE_FUN<<"checkCoredumpLimit:server is inactive or disable corelimit"<IsCoreLimitNeedClose(bNeedClose); - - bool bNeedUpdate = (iRet==2)?true:false; - - if(_limitStateUpdated && bNeedClose) - { - _limitStateUpdated = setServerCoreLimit(true)?false:true;//设置æˆåŠŸåŽå†å±è”½æ›´æ–° - _limitStateInfo.bCloseCore = bNeedClose; - } - else if(bNeedUpdate && !bNeedClose) - { - setServerCoreLimit(false); - _limitStateInfo.bCloseCore = bNeedClose; - } - } - else - { - if(_limitStateUpdated) - { - _limitStateUpdated = setServerCoreLimit(_limitStateInfo.bCloseCore)?false:true; //设置æˆåŠŸåŽå†å±è”½æ›´æ–° - } - } -} - -bool ServerObject::setServerCoreLimit(bool bCloseCore) -{ - string sResult = "succ"; - const string sCmd = (bCloseCore)?("tars.closecore yes"):("tars.closecore no"); - - CommandNotify command(this,sCmd); - int iRet = command.doProcess(sResult); - - NODE_LOG("KeepAliveThread")->debug()<debug() << FILE_FUN << _serverId << "|" << _limitStateInfo.str() << endl; - - _serviceLimitResource->setLimitCheckInfo(tInfo.iMaxExcStopCount,tInfo.iCoreLimitTimeInterval,tInfo.iCoreLimitExpiredTime); - - _activatorPtr->setLimitInfo(tInfo.iActivatorTimeInterval, tInfo.iActivatorMaxCount, tInfo.iActivatorPunishInterval); - - _limitStateUpdated = true; -} - -//é‡ç½®coreè®¡æ•°ä¿¡æ¯ -void ServerObject::resetCoreInfo() -{ - _serviceLimitResource->resetRunTimeData(); -} - -void ServerObject::setStarted(bool bStarted) -{ - Lock lock(*this); - _started=bStarted; -} - -void ServerObject::setStartTime(int64_t iStartTime) -{ - Lock lock(*this); - _startTime = iStartTime; -} \ No newline at end of file diff --git a/cpp/framework/NodeServer/ServerObject.h b/cpp/framework/NodeServer/ServerObject.h deleted file mode 100644 index 9006c8971..000000000 --- a/cpp/framework/NodeServer/ServerObject.h +++ /dev/null @@ -1,526 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __SERVER_OBJECT_H_ -#define __SERVER_OBJECT_H_ - -#include "Node.h" -#include "Registry.h" -#include -#include "Activator.h" -#include "util/tc_file.h" -#include "util/tc_config.h" -#include "servant/AdminF.h" -#include "servant/NodeF.h" -#include "servant/TarsLogger.h" -#include "PlatformInfo.h" -#include "PropertyReporter.h" -#include "ServerLimitResource.h" - -using namespace tars; -using namespace std; - -class ServerObject : public TC_ThreadRecLock, public TC_HandleBase -{ -public: - enum InternalServerState - { - Inactive, - Activating, - Active, - Deactivating, - Destroying, - Destroyed, - Loading, - Patching, - BatchPatching, - AddFilesing, - }; - - enum EM_CoreType - { - EM_AUTO_LIMIT, - EM_MANUAL_LIMIT - }; - - /** - *@brief æœåŠ¡è¿›ç¨‹çš„limit资æºçŠ¶æ€ï¼Œæ¯”如coreå±žæ€§èµ„æº - * - *æœåŠ¡çš„corelimitåˆå§‹çŠ¶æ€é»˜è®¤ä¸ºEM_AUTO_LIMIT+false - */ - struct ServerLimitInfo - { - bool bEnableCoreLimit; //资æºå±žæ€§å¼€å…³ - bool bCloseCore; //core属性的状æ€ï¼Œå±è”½:true,打开:false - EM_CoreType eCoreType; //æœåŠ¡core的当å‰å±è”½æ–¹å¼ - int iMaxExcStopCount; //最大æœåŠ¡å¼‚常åœæ­¢ä¸ªæ•° - int32_t iCoreLimitTimeInterval; //时间间隔内coreçš„é™åˆ¶ï¼Œå•ä½æ˜¯åˆ†é’Ÿ - int32_t iCoreLimitExpiredTime; //core关闭的æŒç»­æ—¶é—´,å•ä½ä¸ºåˆ†é’Ÿ - int32_t iMonitorIntervalMs; //keepalive监控时间间隔 - - int iActivatorMaxCount; //最大å¯åŠ¨æ¬¡æ•° - int iActivatorTimeInterval; //时间 - int iActivatorPunishInterval; //惩罚å—é™æ—¶é—´é—´éš” - bool bReportLoadInfo; //是å¦ä¸ŠæŠ¥èŠ‚点负载信æ¯,默认上报 - - ServerLimitInfo() - : bEnableCoreLimit(false) - , bCloseCore(false) - , eCoreType(EM_AUTO_LIMIT) - , iMaxExcStopCount(3) - , iCoreLimitTimeInterval(5) - , iCoreLimitExpiredTime(30) - , iMonitorIntervalMs(1000) - , iActivatorMaxCount(10) - , iActivatorTimeInterval(60) - , iActivatorPunishInterval(600) - , bReportLoadInfo(true) - { - } - - ostream& displaySimple(ostream& _os, int _level=0) const - { - tars::TarsDisplayer _ds(_os, _level); - _ds.displaySimple(bEnableCoreLimit, true); - _ds.displaySimple(bCloseCore, true); - _ds.displaySimple(eCoreType, true); - _ds.displaySimple(iMaxExcStopCount, true); - _ds.displaySimple(iCoreLimitTimeInterval, true); - _ds.displaySimple(iCoreLimitExpiredTime, true); - _ds.displaySimple(iMonitorIntervalMs, true); - - _ds.displaySimple(iActivatorMaxCount, true); - _ds.displaySimple(iActivatorTimeInterval, true); - _ds.displaySimple(iActivatorPunishInterval, false); - - _ds.displaySimple(bReportLoadInfo, true); - return _os; - } - - string str() - { - stringstream ss; - displaySimple(ss); - return ss.str(); - } - }; - -public: - - /** - * 构造函数 - * @param tDesc serveræ述结构 - */ - ServerObject( const ServerDescriptor& tDesc); - - /** - * æžå¤Ÿ - */ - ~ServerObject() { }; - - /** - * 是å¦æ˜¯tarsæœåŠ¡ - * - * @return bool - */ - bool isTarsServer() {return _tarsServer;} - - /** - * 是å¦æ˜¯enable - * - * @return bool - */ - bool isEnabled() {return _enabled;} - - /** - * æœåŠ¡æ˜¯å¦è‡ªåŠ¨å¯åŠ¨ - *,用æ¥é˜²æ­¢é—®é¢˜æœåŠ¡ä¸æ–­é‡å¯å½±å“其它æœåŠ¡ - * @return bool - */ - bool isAutoStart() ; - - /** - * 设置æœåŠ¡enable - * @param enable - */ - void setEnabled(bool enable){_enabled = enable; } - - /** - * 设置æœåŠ¡å·²patch - * @param enable - */ - void setPatched(bool bPatched) - { - _patched = bPatched; - _patchInfo.iPercent = 100; - } - - /** - *设置æœåŠ¡å·²load - * @param enable - */ - void setLoaded(bool bLoaded){_loaded = bLoaded;} - - /** - *是å¦å…许åŒæ­¥æœåŠ¡çš„状æ€åˆ°ä¸»æŽ§ä¸­å¿ƒï¼Œåœ¨æŸäº›æƒ…况下,æœåŠ¡å‘布了,但是还ä¸æƒ³å¯¹å¤–æä¾›æœåŠ¡ - *则å¯ä»¥è®¾ç½®è¿™ä¸ªå€¼ - *@param bEn - */ - void setEnSynState(bool bEn) { _enSynState = bEn;} - - bool IsEnSynState(){ return _enSynState;} - -public: - - /** - * 验è¯server对应pid是å¦å­˜åœ¨ - * @return int - */ - int checkPid(); - - /** - * 设置server对应pid - */ - void setPid( pid_t pid ); - - /** - * 获å–server对应pid - */ - - int getPid() { return _pid; } - - /** - * 获å–server对应本地socket - */ - - TC_Endpoint getLocalEndpoint() { return _localEndpoint; } - - /** - * 设置server对应本地socket - */ - - void setLocalEndpoint(const TC_Endpoint &tLocalEndpoint) { _localEndpoint = tLocalEndpoint; } - - /** - * 获å–serverä¸ŠæŠ¥ä¿¡æ¯ - * @para string obj - * @para pid_t pid上报pid - * @return void - */ - void keepAlive( pid_t pid, const string &adapter=""); - - /** - * å–çš„server最近keepAlive时间 - * @return int - */ - int getLastKeepAliveTime(const string &adapter=""); - - /** - * 设置server最近keepAlive时间 - * @return void - */ - void setLastKeepAliveTime(int t,const string &adapter=""); - - /** - * æœåŠ¡æ˜¯å¦å·²timeout - * @param iTimeout - * @return bool - */ - bool isTimeOut(int iTimeout); - - /* - * 是å¦å·²ç»é€šè¿‡commandStartå¯åŠ¨æˆåŠŸ - */ - bool isStarted() {return _started;} - -public: - - /** - * è·ŸregistryåŒæ­¥server当å‰çŠ¶æ€ - * @return void - */ - void synState(); - - /** - * 异步跟registryåŒæ­¥server当å‰çŠ¶æ€ - * @return void - */ - void asyncSynState(); - - /** - * 设置server当å‰çŠ¶æ€ - * @param eState - * @param bSynState 是å¦åŒæ­¥server状æ€åˆ°registry - * @return void - */ - void setState( InternalServerState eState, bool bSynState=true); - - /** - *设置server上一次状æ€ï¼Œå½“server状æ€å‘生å˜åŒ–时调用 - */ - void setLastState(InternalServerState eState); - - /** - * 获å–æœåŠ¡çŠ¶æ€ - * - * @return tars::ServerState - */ - tars::ServerState getState(); - - /** - * 获å–serverå†…éƒ¨çŠ¶æ€ - * @return void - */ - InternalServerState getInternalState() { return _state; } - - /** - *获å–serverä¸Šä¸€æ¬¡å†…éƒ¨çŠ¶æ€ - *@return InternalServerState - */ - InternalServerState getLastState() {return _lastState;} - - /** - *转æ¢æŒ‡å®šserver内部状æ€ä¸ºå­—ç¬¦ä¸²å½¢å¼ - * @para eState - * @return string - */ - string toStringState( InternalServerState eState ) const; - - /** - *转æ¢æŒ‡å®šserver内部状æ€ä¸ºone::ServerStateå½¢å¼ consider - * @para eState - * @return ServerState - */ - ServerState toServerState( InternalServerState eState ) const; - - /** - *监控serverçŠ¶æ€ - * @para iTimeout 心跳超时时间 - * @return void - */ - void checkServer(int iTimeout); - - /** - * 属性上报å•ç‹¬å‡ºæ¥ - */ - void reportMemProperty(); - - /** - *设置脚本文件 - * @para tConf 脚本文件 - * @return void - */ - string decodeMacro(const string& value) const ; - -public: - - /** - *是å¦ä¸ºè„šæœ¬æ–‡ä»¶ - * @para sFileName 文件å - * @return bool - */ - bool isScriptFile(const string &sFileName); - - /** - * 拉å–脚本文件 - * @para sFileName 文件å - * @return bool - */ - bool getRemoteScript(const string &sFileName); - - /** - *执行监控脚本文件 - * @para sFileName 文件å - * @return void - */ - void doMonScript(); - -public: - /** - *s设置下载百分比 - * @para iPercent 文件已下载百分比 - * @return bool - */ - void setPatchPercent(const int iPercent); - - /** - *s设置下载结果 - * @para sPatchResult - * @return bool - */ - void setPatchResult(const string &sPatchResult,const bool bSucc = false); - - /** - *s设置下载版本 - * @para sPatchResult - * @return bool - */ - void setPatchVersion(const string &sVersion); - - /** - * 获å–æ­£åœ¨ä¸‹è½½çš„ç‰ˆæœ¬å· - * @return string - */ - string getPatchVersion(); - - /** - * 获å–ä¸‹è½½ä¿¡æ¯ - * @para tPatchInfo - * @return int - */ - int getPatchPercent(PatchInfo &tPatchInfo); - -public: - ServerDescriptor getServerDescriptor() { return _desc; } - ActivatorPtr getActivator() { return _activatorPtr; } - string getExePath(){return _exePath;} - string getExeFile(){return _exeFile;} - string getConfigFile(){return _confFile;} - string getLogPath(){return _logPath;} - string getLibPath(){return _libPath;} - string getServerDir(){return _serverDir;} - string getServerId(){return _serverId;} - string getServerType(){return _serverType;} - string getStartScript() {return _startScript;} - string getStopScript() {return _stopScript;} - string getMonitorScript() {return _monitorScript;} - string getEnv() { return _env; } - string getRedirectPath() {return _redirectPath;} - - //javaæœåŠ¡ - string getJvmParams() {return _jvmParams;} - string getMainClass() {return _mainClass;} - string getClassPath() {return _classPath;} - string getBackupFileNames(){return _backupFiles;} - - void setServerDescriptor( const ServerDescriptor& tDesc ); - void setVersion( const string &version ); - void setExeFile(const string &sExeFile){_exeFile = sExeFile;} - void setExePath(const string &sExePath){_exePath = sExePath;} - void setConfigFile(const string &sConfFile){_confFile = sConfFile;} - void setLogPath(const string &sLogPath){_logPath = sLogPath;} - void setLibPath(const string &sLibPath){_libPath = sLibPath;} - void setServerDir(const string &sServerDir){_serverDir = sServerDir;} - void setNodeInfo(const NodeInfo &tNodeInfo){_nodeInfo = tNodeInfo;} - void setServerType( const string &sType ){ _serverType = TC_Common::lower(TC_Common::trim(sType));_serverType == "not_tars"?_tarsServer = false:_tarsServer=true;} - void setMacro(const map& mMacro); - void setScript(const string &sStartScript,const string &sStopScript,const string &sMonitorScript); - - void setEnv(const string & sEnv) { _env = sEnv; } - void setHeartTimeout(int iTimeout) { _timeout = iTimeout; } - - //javaæœåŠ¡ - void setJvmParams(const string &sJvmParams){_jvmParams = sJvmParams;} - void setMainClass(const string &sMainClass){_mainClass = TC_Common::trim(sMainClass);} - void setClassPath(const string &sClassPath){_classPath = sClassPath;} - void setRedirectPath(const string& sRedirectpath) {_redirectPath = sRedirectpath;} - void setBackupFileNames(const string& sFileNames){_backupFiles = sFileNames;} - - //é‡ç½®coreè®¡æ•°ä¿¡æ¯ - void resetCoreInfo(); - - //是å¦å·²ç»é€šè¿‡commandStartå¯åŠ¨æˆåŠŸ - void setStarted(bool bStarted); - - //设置å¯åŠ¨çš„时间,作为checkpid系统延迟判断的起点 - void setStartTime(int64_t iStartTime); - - //判断是å¦å¯åŠ¨è¶…æ—¶ - bool isStartTimeOut(); -public: - /** - * auto check routine - */ - void checkCoredumpLimit(); - - /** - * server restart and set limitupdate state - */ - void setLimitInfoUpdate(bool bUpdate); - - void setServerLimitInfo(const ServerLimitInfo& tInfo); - - bool setServerCoreLimit(bool bCloseCore); - -private: - bool _tarsServer; //是å¦tarsæœåŠ¡ - string _serverType; //æœåŠ¡ç±»åž‹ tars_cpp tars_java not_tars - -private: - bool _enabled; //æœåŠ¡æ˜¯å¦æœ‰æ•ˆ - bool _loaded; //æœåŠ¡é…置是å¦å·²æˆåŠŸåŠ è½½ - bool _patched; //æœåŠ¡å¯æ‰§è¡Œç¨‹åºæ˜¯å¦å·²æˆåŠŸä¸‹è½½ - bool _noticed; //æœåŠ¡å½“å‰çŠ¶æ€æ˜¯å¦å·²é€šçŸ¥registry。 - unsigned _noticeFailTimes; //åŒæ­¥æœåŠ¡çŠ¶æ€åˆ°registry连续失败次数 - bool _enSynState; //是å¦å…许åŒæ­¥æœåŠ¡çš„状æ€åˆ°ä¸»æŽ§ - -private: - string _application; //æœåŠ¡æ‰€å±žapp - string _serverName; //æœåŠ¡å称 - string _serverId; //æœåŠ¡id为App.ServerName.Ipå½¢å¼ - -private: - string _jvmParams; //javaæœåŠ¡jvm - string _mainClass; //javaæœåŠ¡main class - string _classPath; //javaæœåŠ¡ class path - string _redirectPath; //标准输出和错误输出é‡å®šå‘目录 - -private: - string _startScript; //å¯åŠ¨è„šæœ¬ - string _stopScript; //åœæ­¢è„šæœ¬ - string _monitorScript; //监控脚本 - -private: - string _serverDir; //æœåŠ¡æ•°æ®ç›®å½• - string _confFile; //æœåŠ¡é…置文件目录 - string _exePath; //一般为_serverDir+"/bin" å¯ä¸ªæ€§æŒ‡å®š - string _exeFile; //一般为_exePath+_serverName å¯ä¸ªæ€§æŒ‡å®š - string _logPath; //æœåŠ¡æ—¥å¿—目录 - string _libPath; //动æ€åº“目录 一般为_desc.basePath/lib - map _macro; //æœåŠ¡å® - -private: - PatchInfo _patchInfo; //ä¸‹è½½ä¿¡æ¯ - -private: - pid_t _pid; //æœåŠ¡è¿›ç¨‹å· - string _version; //TARS版本 - NodeInfo _nodeInfo; //æœåŠ¡æ‰€åœ¨nodeä¿¡æ¯ - TC_Endpoint _localEndpoint; //本地socket - ServerDescriptor _desc; //æœåŠ¡æ述,主è¦ç”¨äºŽç”Ÿæˆé…置文件等 - ActivatorPtr _activatorPtr; //用于å¯åŠ¨ã€åœæ­¢æœåŠ¡ - time_t _keepAliveTime; //æœåŠ¡æœ€è¿‘上报时间 - map _adapterKeepAliveTime; //å„adapter最近上报时间,用æ¥åˆ¤æ–­adapter是å¦ä¸ŠæŠ¥è¶…æ—¶ - - InternalServerState _state; //当å‰æœåŠ¡çŠ¶æ€ - InternalServerState _lastState; //上一次æœåŠ¡çŠ¶æ€ - - int _timeout; //心跳超时时间 - string _env; //环境å˜é‡å­—符串 - string _backupFiles; //针对javaæœåŠ¡å‘布时bin目录下需è¦ä¿ç•™çš„文件;å¯ä»¥ç”¨;|æ¥åˆ†éš” - -private: - bool _limitStateUpdated; //æœåŠ¡çš„limité…置是å¦æœ‰æ›´æ–°ï¼Œé‡å¯ä¹Ÿç®—æ›´æ–° - ServerLimitInfo _limitStateInfo; //通过nodeé…置设置的资æºä¿¡æ¯ - ServerLimitResourcePtr _serviceLimitResource; - bool _started; //是å¦å·²ç»é€šè¿‡commandStartå¯åŠ¨æˆåŠŸ - int64_t _startTime; //å¯åŠ¨çš„时间,作为checkpid系统延迟判断的起点 -}; - -typedef TC_AutoPtr ServerObjectPtr; - -#endif - diff --git a/cpp/framework/NodeServer/SingleFileDownloader.cpp b/cpp/framework/NodeServer/SingleFileDownloader.cpp deleted file mode 100644 index f2899cfa4..000000000 --- a/cpp/framework/NodeServer/SingleFileDownloader.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "SingleFileDownloader.h" -#include "servant/TarsLogger.h" -#include "util.h" - -DownloadTaskFactory* DownloadTaskFactory::_instance = new DownloadTaskFactory(); - -int SingleFileDownloader::download(const PatchPrx &patchPrx, const string &remoteFile, const string &localFile, const DownloadEventPtr &pPtr, std::string & sResult) -{ - - vector vFiles; - int ret = 0; - - for( int i = 0; i < 2; i ++) - { - try - { - ret = patchPrx->listFileInfo(remoteFile, vFiles); - break; - } - catch(TarsException& ex) - { - TLOGERROR("SingleFileDownloader::download " << (remoteFile + " TarsException " + ex.what())<< endl); - } - } - - //1表示是文件 - if(ret != 1) - { - sResult = remoteFile + " is not an file"; - return -1; - } - - if(vFiles.size() < 1) - { - sResult = remoteFile + " not exist"; - return -2; - } - - tars::FileInfo fileInfo = vFiles[0]; - - FILE *fp = fopen(localFile.c_str(), "wb"); - if (!fp) - { - sResult = localFile + " can not write"; - return -3; - } - - //循环下载文件到本地 - vector buffer; - int pos = 0; - int downloadRet = 0; - bool fileEnded = false; - while (true) - { - buffer.clear(); - - //最多å°è¯•ä¸¤æ¬¡ - for( int i = 0; i < 2; i ++) - { - try - { - downloadRet = patchPrx->download(remoteFile, pos, buffer); - break; - } - catch(TarsException& ex) - { - TLOGERROR("SingleFileDownloader::download "<< (remoteFile + " TarsException " + ex.what()) << endl); - } - } - - if (downloadRet < 0) - { - TLOGERROR("SingleFileDownloader::download " << "|downloadRet:" << downloadRet << "|remoteFile:" << remoteFile << endl); - sResult = remoteFile + " download from tarspatch error " + TC_Common::tostr(downloadRet); - ret = downloadRet - 100; - break; - } - else if (downloadRet == 0) - { - size_t r = fwrite((void*)&buffer[0], 1, buffer.size(), fp); - if (r == 0) - { - TLOGERROR("SingleFileDownloader::download fwrite file '" + localFile + "' error!" << endl); - sResult = "fwrite file '" + localFile + "' error!"; - ret = -5; - break; - } - pos += r; - if(pPtr) - { - pPtr->onDownloading(fileInfo, pos); - } - } - else if (downloadRet == 1) - { - TLOGDEBUG("SingleFileDownloader::download load succ " << remoteFile << "|pos:"< -#include "util/tc_monitor.h" -#include "util/tc_hash_fun.h" - -class DownloadEvent : public TC_HandleBase -{ -public: - virtual void onDownloading(const FileInfo &fi, int pos) = 0; -}; - -typedef TC_AutoPtr DownloadEventPtr; - - -//下载任务 -struct DownloadTask -{ - string sLocalTgzFile; -}; - -inline bool operator<(const DownloadTask&l, const DownloadTask&r) -{ - if(l.sLocalTgzFile != r.sLocalTgzFile) - { - return (l.sLocalTgzFile < r.sLocalTgzFile); - } - - return false; -} - -#define POOLSIZE 10 - -class DownloadTaskFactory -{ - /**定义hash处ç†å™¨*/ - using hash_functor = std::function; - -public: - - tars::TC_ThreadRecLock* getRecLock(const DownloadTask& task) - { - size_t hashcode = _hashf(task.sLocalTgzFile); - size_t index = hashcode % POOLSIZE; - return &_lockPool[index]; - } - - static DownloadTaskFactory& getInstance() - { - return *_instance; - } - -private: - DownloadTaskFactory() - : _hashf(tars::hash()) - { - } - -private: - tars::TC_ThreadRecLock _lockPool[POOLSIZE]; - hash_functor _hashf; - static DownloadTaskFactory* _instance; -}; - -//从patch上下载å•ä¸ªæ–‡ä»¶ -class SingleFileDownloader -{ -public: - static int download(const PatchPrx &patchPrx, const string &remoteFile, const string &localFile, const DownloadEventPtr &pPtr, std::string & sResult); - -private: -}; -////////////////////////////////////////////////////////////// -#endif - diff --git a/cpp/framework/NodeServer/main.cpp b/cpp/framework/NodeServer/main.cpp deleted file mode 100644 index 21e9a097e..000000000 --- a/cpp/framework/NodeServer/main.cpp +++ /dev/null @@ -1,253 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NodeServer.h" -#include "servant/Communicator.h" -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_config.h" -#include - -using namespace std; -using namespace tars; - -NodeServer g_app; -TC_Config* g_pconf; - -static string outfill(const string& s, char c = ' ', int n = 29) -{ - return (s + string(abs(n - (int)s.length()), c)); -} - -bool getConfig(const string &sLocator,const string &sRegistryObj,string &sNodeId,string &sLocalIp, string &sConfigFile, bool bCloseCout) -{ - try - { - //string sLocalIp; - string sResult; - string sTemplate; - TC_Config tConf; - CommunicatorFactory::getInstance()->getCommunicator()->setProperty("locator",sLocator); - RegistryPrx pRegistryPrx = CommunicatorFactory::getInstance()->getCommunicator()->stringToProxy(sRegistryObj); - if( sLocalIp.empty() && pRegistryPrx->getClientIp(sLocalIp) != 0) - { - cerr<<"cannot get localip: " <getNodeTemplate(sNodeId,sTemplate); - if(TC_Common::trim(sTemplate) == "" ) - { - cerr << "cannot get node Template nodeid:"<"); - if (sCloseCoutValue == "1") - { - sTemplate = TC_Common::replace(sTemplate, "closecout=1", "closecout=0"); - tConf.parseString(sTemplate); - } - else if (sCloseCoutValue.empty()) - { - map m; - m["closecout"] = "0"; - tConf.insertDomainParam("/tars/application/server", m,true); - } - else - { - cerr << "failed to set closeout value" << endl; - } - } - - string sConfigPath = TC_File::extractFilePath(sConfigFile); - if(!TC_File::makeDirRecursive( sConfigPath )) - { - cerr<<"cannot create dir: " <"]; - - ServantPrx prx = CommunicatorFactory::getInstance()->getCommunicator()->stringToProxy(nodeObj); - prx->tars_ping(); -} - -void parseConfig(int argc, char *argv[]) -{ - - TC_Option tOp;//consider - tOp.decode(argc, argv); - - if (tOp.hasParam("nodeversion")) - { - cout << "Node:" << TARS_VERSION << "_" << NODE_VERSION << endl; - exit(0); - } - - if (tOp.hasParam("monitor")) - { - try - { - string configFile = tOp.getValue("config"); - - monitorNode(configFile); - } - catch (exception &ex) - { - cout << "failed:" << ex.what() << endl; - exit(-1); - } - exit(0); - return; - } - - string sLocator = tOp.getValue("locator"); - string sNodeId = tOp.getValue("nodeid"); - string sConfigFile = tOp.getValue("config"); - string sRegistryObj = tOp.getValue("registryObj"); - string sLocalIp = tOp.getValue("localip"); - - if(sConfigFile == "") - { - cerr << endl; - cerr <<"start server with locator config, for example: "< mOp = tOp.getMulti(); - for(map::const_iterator it = mOp.begin(); it != mOp.end(); ++it) - { - cout << outfill( it->first)<< it->second << endl; - } - -} - -int main( int argc, char* argv[] ) -{ - try - { - bool bNoDaemon = false; - for (int i = 1; i < argc; ++i) - { - if (::strcmp(argv[i], "--monitor") == 0) - { - bNoDaemon = true; - break; - } - } - - if (!bNoDaemon) - { - TC_Common::daemon(); - } - - parseConfig(argc,argv); - - g_pconf = &g_app.getConfig(); - - g_app.main( argc, argv ); - g_app.waitForShutdown(); - } - catch ( exception& ex ) - { - cout<< ex.what() << endl; - } - catch ( ... ) - { - cout<< "main unknow exception cathed" << endl; - } - - return 0; -} diff --git a/cpp/framework/NodeServer/util.h b/cpp/framework/NodeServer/util.h deleted file mode 100644 index bf6b93d2b..000000000 --- a/cpp/framework/NodeServer/util.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _UTIL_H -#define _UTIL_H - -#include "util/tc_common.h" - -#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__) - -#define FILE_FUN __FILENAME__<<":"<<__FUNCTION__<<":"<<__LINE__<<"|" -#define FILE_FUN_STR TC_Common::tostr(__FILENAME__)+":"+TC_Common::tostr(__FUNCTION__)+":"+TC_Common::tostr(__LINE__)+"|" - -#endif diff --git a/cpp/framework/NotifyServer/CMakeLists.txt b/cpp/framework/NotifyServer/CMakeLists.txt deleted file mode 100644 index 9fd0c566c..000000000 --- a/cpp/framework/NotifyServer/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ - -set(MODULE "tarsnotify") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/) - -complice_module(${MODULE}) diff --git a/cpp/framework/NotifyServer/LoadDbThread.cpp b/cpp/framework/NotifyServer/LoadDbThread.cpp deleted file mode 100644 index 0ed9c6675..000000000 --- a/cpp/framework/NotifyServer/LoadDbThread.cpp +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "LoadDbThread.h" -#include "NotifyServer.h" - -LoadDbThread::LoadDbThread() -: _interval(30) -, _terminate(false) -{ - -} - -LoadDbThread::~LoadDbThread() -{ - if(isAlive()) - { - terminate(); - getThreadControl().join(); - } -} - -void LoadDbThread::terminate() -{ - _terminate = true; - - TC_ThreadLock::Lock lock(*this); - - notifyAll(); -} - -void LoadDbThread::init() -{ - try - { - map mDbConf = g_pconf->getDomainMap("/tars/db"); - assert(!mDbConf.empty()); - - TC_DBConf tcDBConf; - tcDBConf.loadFromMap(mDbConf); - - _mysql.init(tcDBConf); - - TLOGDEBUG("LoadDbThread::init init mysql conf succ." << endl); - } - catch(exception &ex) - { - TLOGERROR("LoadDbThread::init ex:" << ex.what() << endl); - FDLOG("EX") << "LoadDbThread::init ex:" << ex.what() << endl; - } -} - -void LoadDbThread::run() -{ - size_t iLastTime = 0; - size_t iNow = TNOW; - - while (!_terminate) - { - if(iNow - iLastTime >= _interval) - { - loadData(); - - iLastTime = iNow; - } - - { - TC_ThreadLock::Lock lock(*this); - timedWait(1000); - } - } -} - -void LoadDbThread::loadData() -{ - try - { - TC_Mysql::MysqlData mysqlData; - map &mTmep = _data.getWriter(); - mTmep.clear(); - size_t iOffset(0); - - do - { - string sSql("select application, set_name, set_area, set_group, server_name, node_name from t_server_conf limit 1000 offset "); - sSql = sSql + TC_Common::tostr(iOffset) +";"; - - mysqlData = _mysql.queryRecord(sSql); - - for (size_t i = 0; i < mysqlData.size(); i++) - { - string sValue = mysqlData[i]["set_name"] +"."+ mysqlData[i]["set_area"]+ "." + mysqlData[i]["set_group"]; - if (mysqlData[i]["set_name"].empty()) - { - continue; - } - - string sKey = mysqlData[i]["application"] + "." + mysqlData[i]["server_name"] + mysqlData[i]["node_name"]; - mTmep.insert(map::value_type(sKey, sValue)); - } - - iOffset += mysqlData.size(); - - } while (iOffset % 1000 == 0); - - _data.swap(); - - TLOGDEBUG("LoadDbThread::loadData load data finish, _mSetApp size:" << mTmep.size() << endl); - - } - catch (exception &ex) - { - TLOGERROR("LoadDbThread::loadData exception:" << ex.what() << endl); - FDLOG("EX") << "LoadDbThread::loadData exception:" << ex.what() << endl; - } -} diff --git a/cpp/framework/NotifyServer/LoadDbThread.h b/cpp/framework/NotifyServer/LoadDbThread.h deleted file mode 100644 index 43db14cd8..000000000 --- a/cpp/framework/NotifyServer/LoadDbThread.h +++ /dev/null @@ -1,152 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __LOAD_DB_THREAD_H_ -#define __LOAD_DB_THREAD_H_ - -#include "util/tc_thread.h" -#include "util/tc_monitor.h" -#include "util/tc_mysql.h" -#include "servant/TarsLogger.h" -//#include "NotifyServer.h" - -using namespace std; -using namespace tars; - -/** - * Ë«buff»º´æ - */ -template -class DataWrapper -{ -public: - DataWrapper() - :_swap(true) - { - } - - ~DataWrapper() - { - } - - T &getWriter() - { - if (_swap) - { - return _a; - } - else - { - return _b; - } - } - - T &getReader() - { - if (_swap) - { - return _b; - } - else - { - return _a; - } - } - - void swap() - { - _swap = !_swap; - } - -private: - bool _swap; - T _a; - T _b; -}; - -/** - * ´Ódb¼ÓÔØsetÏà¹ØÐÅÏ¢µÄÏß³ÌÀà - */ -class LoadDbThread : public TC_Thread, public tars::TC_ThreadLock -{ -public: - typedef DataWrapper< map > Data; - - /** - * ¹¹Ô캯Êý - */ - LoadDbThread(); - - /** - * Îö¹¹º¯Êý - */ - ~LoadDbThread(); - - /** - * ³õʼ»¯ - */ - void init(); - - /** - * ÔËÐÐ - */ - void run(); - - /** - * Í£Ö¹ - */ - void terminate(); - - /** - * »ñÈ¡setÐÅÏ¢ - */ - string getSetName(const string &sAppName) - { - map &mData = _data.getReader(); - map::iterator it = mData.find(sAppName); - - if (it != mData.end()) - { - return it->second; - } - - return ""; - - } - -private: - - /** - * ¼ÓÔØdbÊý¾Ý - */ - void loadData(); - -private: - //¼ÓÔØdbÊý¾ÝµÄʱ¼ä¼ä¸ô - size_t _interval; - - //Í£Ö¹ - bool _terminate; - - //·ÃÎÊÊý¾Ý¿â - TC_Mysql _mysql; - - //Ë«buff»º´æ - Data _data; -}; - - -#endif diff --git a/cpp/framework/NotifyServer/NotifyImp.cpp b/cpp/framework/NotifyServer/NotifyImp.cpp deleted file mode 100644 index 71bd04488..000000000 --- a/cpp/framework/NotifyServer/NotifyImp.cpp +++ /dev/null @@ -1,350 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NotifyImp.h" -#include "jmem/jmem_hashmap.h" -#include "NotifyServer.h" - -extern TC_Config * g_pconf; -extern TarsHashMap * g_notifyHash; - -void NotifyImp::loadconf() -{ - TC_DBConf tcDBConf; - tcDBConf.loadFromMap(g_pconf->getDomainMap("/tars/db")); - - _mysqlConfig.init(tcDBConf); - - _sql = (*g_pconf)["/tars/"]; - _maxPageSize = TC_Common::strto((*g_pconf)["/tars/hash"]); - _maxPageNum = TC_Common::strto((*g_pconf)["/tars/hash"]); - - map m = g_pconf->getDomainMap("/tars/filter"); - for(map::iterator it= m.begin();it != m.end();it++) - { - vector vFilters = TC_Common::sepstr(it->second,";|"); - _setFilter[it->first].insert(vFilters.begin(),vFilters.end()); - } - -} - -void NotifyImp::initialize() -{ - loadconf(); -} - -bool NotifyImp::IsdbTableExist(const string& sTbName) -{ - try - { - TC_Mysql::MysqlData tTotalRecord = _mysqlConfig.queryRecord("show tables like '%"+sTbName+"%'"); - - TLOGINFO("NotifyImp::IsdbTableExist show tables like '%" + sTbName + "%|affected:" << tTotalRecord.size() << endl); - - if (tTotalRecord.size() > 0) - { - return true; - } - else - { - TLOGERROR("NotifyImp::IsdbTableExist sTbName:" << sTbName << "|doesn't exist" << endl); - return false; - } - - } - catch(TC_Mysql_Exception& ex) - { - TLOGERROR("NotifyImp::IsdbTableExist exception:" << ex.what() << endl); - return false; - } - catch(...) - { - TLOGERROR("NotifyImp::IsdbTableExist unknown exception." << endl); - return false; - } -} - -void NotifyImp::creatTb(const string &sTbName) -{ - try - { - if (!IsdbTableExist(sTbName)) - { - string sSql = TC_Common::replace(_sql, "${TABLE}",sTbName); - - _mysqlConfig.execute(sSql); - - TLOGDEBUG("NotifyImp::creatTb sSql:" << sSql << endl); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("NotifyImp::creatTb exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("NotifyImp::IsdbTableExist unknown exception." << endl); - } -} - -bool NotifyImp::IsNeedFilte(const string& sServerName,const string& sResult) -{ - if(_setFilter.find(sServerName) != _setFilter.end()) - { - set& setFilter = _setFilter.find(sServerName)->second; - for(set::iterator it = setFilter.begin(); it != setFilter.end();it++) - { - if(sResult.find(*it) != string::npos) - { - return true; - } - } - } - - const string sDefault = "default"; - if(_setFilter.find(sDefault) != _setFilter.end()) - { - set& setFilter = _setFilter.find(sDefault)->second; - for(set::iterator it = setFilter.begin(); it != setFilter.end();it++) - { - if(sResult.find(*it) != string::npos) - { - return true; - } - } - } - - return false; -} - -void NotifyImp::reportServer(const string& sServerName, const string& sThreadId, const string& sResult, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("NotifyImp::reportServer sServerName:" << sServerName << "|ip:" << current->getIp() << "|sThreadId:" << sThreadId << "|sResult:" << sResult << endl); - //DLOG << "NotifyImp::reportServer sServerName:" << sServerName << "|ip:" << current->getIp() << "|sThreadId:" << sThreadId << "|sResult:" << sResult << endl; - - if(IsNeedFilte(sServerName,sResult)) - { - TLOGWARN("NotifyImp::reportServer sServerName:" << sServerName << "|ip:" << current->getIp() << "|sThreadId:" << sThreadId << "|sResult:" << sResult <<"|filted"<< endl); - return; - } - - ReportInfo info; - info.sApp = sServerName; - info.sServer = sServerName; - - vector vModule = TC_Common::sepstr(sServerName,"."); - if(vModule.size() >= 2) - { - info.sApp = vModule[0]; - info.sServer = vModule[1]; - } - - info.eType = REPORT; - info.sSet = g_app.getLoadDbThread()->getSetName(sServerName + current->getIp()); - info.sMessage = sResult; - info.sThreadId = sThreadId; - - reportNotifyInfo(info, current); - - return; -} - -void NotifyImp::notifyServer(const string& sServerName, NOTIFYLEVEL level, const string& sMessage, tars::TarsCurrentPtr current) -{ - ReportInfo info; - info.sApp = sServerName; - info.sServer = sServerName; - - - vector vModule = TC_Common::sepstr(sServerName, "."); - if (vModule.size() >= 2) - { - info.sApp = vModule[0]; - info.sServer = vModule[1]; - } - - info.eType = NOTIFY; - info.sSet = g_app.getLoadDbThread()->getSetName(sServerName + current->getIp()); - info.sMessage = sMessage; - info.eLevel = level; - - reportNotifyInfo(info, current); - - return; -} - -int NotifyImp::getNotifyInfo(const tars::NotifyKey & stKey,tars::NotifyInfo &stInfo,tars::TarsCurrentPtr current) -{ - int iRet = g_notifyHash->get(stKey, stInfo); - - ostringstream os; - stKey.displaySimple(os); - os << "|"; - stInfo.displaySimple(os); - - TLOGDEBUG("NotifyImp::getNotifyInfo iRet:" << iRet << "|os:" << os.str() << endl); - - return iRet; -} - -void NotifyImp::reportNotifyInfo(const tars::ReportInfo & info, tars::TarsCurrentPtr current) -{ - switch (info.eType) - { - case (REPORT): - { - TLOGDEBUG("NotifyImp::reportNotifyInfo reportServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|ip:" << current->getIp() - << "|sThreadId:" << info.sThreadId << "|sMessage:" << info.sMessage << endl); - - // DLOG << "NotifyImp::reportNotifyInfo reportServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|ip:" << current->getIp() - // << "|sThreadId:" << info.sThreadId << "|sMessage:" << info.sMessage << endl; - - if (IsNeedFilte(info.sApp + info.sServer, info.sMessage)) - { - TLOGWARN("NotifyImp::reportNotifyInfo reportServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|ip:" << current->getIp() - << "|sThreadId:" << info.sThreadId << "|sMessage:" << info.sMessage << "|filted" << endl); - - return; - } - - string sql; - TC_Mysql::RECORD_DATA rd; - - rd["application"] = make_pair(TC_Mysql::DB_STR, info.sApp); - rd["server_name"] = make_pair(TC_Mysql::DB_STR, info.sServer); - rd["container_name"] = make_pair(TC_Mysql::DB_STR, info.sContainer); - rd["server_id"] = make_pair(TC_Mysql::DB_STR, info.sApp +"."+ info.sServer + "_" + current->getIp()); - rd["node_name"] = make_pair(TC_Mysql::DB_STR, current->getIp()); - rd["thread_id"] = make_pair(TC_Mysql::DB_STR, info.sThreadId); - - if (!info.sSet.empty()) - { - vector v = TC_Common::sepstr(info.sSet, "."); - if (v.size() != 3 || (v.size() == 3 && (v[0] == "*" || v[1] == "*"))) - { - TLOGERROR("NotifyImp::reportNotifyInfo bad set name:" << info.sSet << endl); - } - else - { - rd["set_name"] = make_pair(TC_Mysql::DB_STR, v[0]); - rd["set_area"] = make_pair(TC_Mysql::DB_STR, v[1]); - rd["set_group"] = make_pair(TC_Mysql::DB_STR, v[2]); - } - - } - else - { - rd["set_name"] = make_pair(TC_Mysql::DB_STR, ""); - rd["set_area"] = make_pair(TC_Mysql::DB_STR, ""); - rd["set_group"] = make_pair(TC_Mysql::DB_STR, ""); - } - - rd["result"] = make_pair(TC_Mysql::DB_STR, info.sMessage); - rd["notifytime"] = make_pair(TC_Mysql::DB_INT, "now()"); - string sTable = "t_server_notifys"; - try - { - _mysqlConfig.insertRecord(sTable, rd); - } - catch (TC_Mysql_Exception& ex) - { - string err = string(ex.what()); - if (std::string::npos != err.find("doesn't exist")) - { - creatTb(sTable); - } - else - { - string sInfo = string("insert2Db exception") + "|" + ServerConfig::LocalIp + "|" + ServerConfig::Application + "." + ServerConfig::ServerName; - TARS_NOTIFY_ERROR(sInfo); - } - TLOGERROR("NotifyImp::reportNotifyInfo insert2Db exception:" << ex.what() << endl); - } - catch (exception& ex) - { - TLOGERROR("NotifyImp::reportNotifyInfo insert2Db exception:" << ex.what() << endl); - string sInfo = string("insert2Db exception") + "|" + ServerConfig::LocalIp + "|" + ServerConfig::Application + "." + ServerConfig::ServerName; - //TARS_NOTIFY_ERROR(sInfo); - } - } - case (NOTIFY): - { - TLOGDEBUG("NotifyImp::reportNotifyInfo notifyServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|ip:" << current->getIp() - << "|eLevel:" << tars::etos(info.eLevel) << "|sMessage:" << info.sMessage << endl); - - if (info.eLevel == NOTIFYERROR) - { - // FDLOG("NOTIFYERROR") << "NotifyImp::reportNotifyInfo notifyServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|ip:" << current->getIp() - // << "|eLevel:" << tars::etos(info.eLevel) << "|sMessage:" << info.sMessage << endl; - } - else - { - // DLOG << "NotifyImp::reportNotifyInfo notifyServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|ip:" << current->getIp() - // << "|eLevel:" << tars::etos(info.eLevel) << "|sMessage:" << info.sMessage << endl; - } - - string sServerId = info.sApp + info.sServer + "_" + current->getIp(); - - NotifyKey stKey0; - stKey0.name = info.sApp + info.sServer; - stKey0.ip = current->getIp(); - stKey0.page = 0; - - NotifyInfo stInfo0; - - NotifyItem stItem; - stItem.sTimeStamp = TC_Common::now2str("%Y-%m-%d %H:%M:%S"); - stItem.sServerId = sServerId; - stItem.iLevel = info.eLevel; - stItem.sMessage = info.sMessage; - - int iRet; - iRet = g_notifyHash->get(stKey0, stInfo0); - TLOGDEBUG("get " << sServerId << " page " << stKey0.page << " info return :" << iRet << endl); - if (iRet == TC_HashMap::RT_LOAD_DATA_ERR) - { - return; - } - - if (stInfo0.notifyItems.size() < _maxPageSize) - { - stInfo0.notifyItems.push_back(stItem); - iRet = g_notifyHash->set(stKey0, stInfo0); - TLOGDEBUG("NotifyImp::reportNotifyInfo set sServerId:" << sServerId << "|page:" << stKey0.page << "|iRet:" << iRet << endl); - return; - } - - //0页置æ¢å‡ºåŽ» - NotifyKey stKeyReplPage = stKey0; - stKeyReplPage.page = (stInfo0.nextpage + 1) % _maxPageNum; - if (stKeyReplPage.page == 0) - { - stKeyReplPage.page = 1; - } - iRet = g_notifyHash->set(stKeyReplPage, stInfo0); - - //修改0页 - stInfo0.nextpage = stKeyReplPage.page; - stInfo0.notifyItems.clear(); - stInfo0.notifyItems.push_back(stItem); - iRet = g_notifyHash->set(stKey0, stInfo0); - } - default: - break; - } - - return; -} diff --git a/cpp/framework/NotifyServer/NotifyImp.h b/cpp/framework/NotifyServer/NotifyImp.h deleted file mode 100644 index fd7b7f65b..000000000 --- a/cpp/framework/NotifyServer/NotifyImp.h +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef NOTIFY_I_H -#define NOTIFY_I_H - -#include "NotifyF.h" -#include "util/tc_common.h" -#include "util/tc_config.h" -#include "util/tc_mysql.h" -#include "servant/TarsLogger.h" -#include "LoadDbThread.h" - -using namespace tars; - -class NotifyImp : public Notify -{ -public: - /** - * åˆå§‹åŒ– - * - * @return int - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy() {}; - - /** - * report - * @param sServerName - * @param sThreadId - * @param sResult - * @param current - */ - virtual void reportServer(const string& sServerName, const string& sThreadId, const string& sResult, tars::TarsCurrentPtr current); - - /** - * notify - * @param sServerName - * @param sThreadId - * @param sCommand - * @param sResult - * @param current - */ - virtual void notifyServer(const string& sServerName, NOTIFYLEVEL level, const string& sMessage, tars::TarsCurrentPtr current); - - /** - * get notify info - */ - virtual tars::Int32 getNotifyInfo(const tars::NotifyKey & stKey,tars::NotifyInfo &stInfo,tars::TarsCurrentPtr current); - - /* - *reportNotifyInfo - *@param info - */ - virtual void reportNotifyInfo(const tars::ReportInfo & info, tars::TarsCurrentPtr current); - -protected: - - void loadconf(); - -private: - - bool IsdbTableExist(const string& sTbName); - - void creatTb(const string &sTbName); - - bool IsNeedFilte(const string& sServerName,const string& sResult); - -protected: - TC_Mysql _mysqlConfig; - string _sql; //创建表 - size_t _maxPageSize; - size_t _maxPageNum; - /* - * 按模å—é…置过滤规则 - * key=default 表示全局规则 - * key=app.server具体模å—规则 - * 模å—规则匹é…失败æ‰åŒ¹é…全局规则 - - - default=r1|r2 - MTT.HelloServer=r3|r4 - - - */ - map > _setFilter; -}; - -#endif diff --git a/cpp/framework/NotifyServer/NotifyServer.cpp b/cpp/framework/NotifyServer/NotifyServer.cpp deleted file mode 100644 index 14759d894..000000000 --- a/cpp/framework/NotifyServer/NotifyServer.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NotifyServer.h" -#include "NotifyImp.h" -#include "jmem/jmem_hashmap.h" - -TarsHashMap * g_notifyHash; - -void NotifyServer::initialize() -{ - //增加对象 - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".NotifyObj"); - - //åˆå§‹åŒ–hash - g_notifyHash = new TarsHashMap(); - - g_notifyHash->initDataBlockSize(TC_Common::strto((*g_pconf)["/tars/hash"]), - TC_Common::strto((*g_pconf)["/tars/hash"]), - TC_Common::strto((*g_pconf)["/tars/hash"])); - - size_t iSize = TC_Common::toSize((*g_pconf)["/tars/hash"], 1024 * 1024 * 10); - if (iSize > 1024 * 1024 * 100) - iSize = 1024 * 1024 * 100; - char* data = new char[iSize]; - g_notifyHash->create(data, iSize); - - _loadDbThread = new LoadDbThread(); - _loadDbThread->init(); - _loadDbThread->start(); -} - -void NotifyServer::destroyApp() -{ - if(_loadDbThread != NULL) - { - delete _loadDbThread; - _loadDbThread = NULL; - } - - TLOGDEBUG("NotifyServer::destroyApp ok" << endl); -} diff --git a/cpp/framework/NotifyServer/NotifyServer.h b/cpp/framework/NotifyServer/NotifyServer.h deleted file mode 100644 index 94e388aeb..000000000 --- a/cpp/framework/NotifyServer/NotifyServer.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __NOTIFY_SERVER_H_ -#define __NOTIFY_SERVER_H_ - -#include "servant/Application.h" -#include "LoadDbThread.h" - -using namespace tars; - -class NotifyServer : public Application -{ -public: - /** - * åˆå§‹åŒ–, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžæž„, æ¯ä¸ªè¿›ç¨‹éƒ½ä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void destroyApp(); - - /** - * 获å–加载db的线程类 - */ - inline LoadDbThread * getLoadDbThread() { return _loadDbThread; } - -private: - - LoadDbThread *_loadDbThread; -}; - -extern NotifyServer g_app; -extern TC_Config * g_pconf; - -#endif - diff --git a/cpp/framework/NotifyServer/main.cpp b/cpp/framework/NotifyServer/main.cpp deleted file mode 100644 index 1e3e720df..000000000 --- a/cpp/framework/NotifyServer/main.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NotifyServer.h" -#include - -using namespace tars; - -TC_Config * g_pconf; -NotifyServer g_app; - -int main(int argc, char *argv[]) -{ - try - { - g_pconf = &g_app.getConfig(); - g_app.main(argc, argv); - - g_app.waitForShutdown(); - } - catch(exception &ex) - { - cerr<< ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/PatchServer/CMakeLists.txt b/cpp/framework/PatchServer/CMakeLists.txt deleted file mode 100644 index b9474a3d1..000000000 --- a/cpp/framework/PatchServer/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ - -set(MODULE "tarspatch") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/bin) - -#complice_module(${MODULE} "Patch.tars") -complice_module(${MODULE}) - diff --git a/cpp/framework/PatchServer/PatchCache.cpp b/cpp/framework/PatchServer/PatchCache.cpp deleted file mode 100644 index 5f7c6ff2d..000000000 --- a/cpp/framework/PatchServer/PatchCache.cpp +++ /dev/null @@ -1,279 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include -#include "PatchCache.h" -#include "PatchServer.h" - -using namespace tars; - -int PatchCache::load(const std::string & sFile, std::pair & mem) -{ - TLOGDEBUG("PatchCache::load sFile:" << sFile << endl); - - struct MemState * cur = NULL; - - TC_ThreadLock::Lock lock(_mutex); - - try - { - struct stat st; - memset(&st, 0, sizeof(struct stat)); - - if (lstat(sFile.c_str(), &st) != 0) - { - TLOGERROR("PatchCache::load sFile:" << sFile << "|lstat file error:" << strerror(errno) << endl); - return -1; - } - - if ((size_t)st.st_size > _MemMax || (size_t)st.st_size < _MemMin) - { - TLOGERROR("PatchCache::load sFile:" << sFile << "|invalid file size must(" << _MemMax << ">" << st.st_size << "<" << _MemMin << ")" << endl); - return -1; - } - - //查看是å¦å·²ç»åŠ è½½å…¥å†…å­˜ - std::map::iterator it = _mapFiles.find(sFile); - if (it != _mapFiles.end()) - { - cur = it->second; - - if ((cur->MemLoad == EM_LOADING || cur->MemLoad == EM_RELOAD))//错误都为1çŠ¶æ€ - { - TLOGERROR("PatchCache::load sFile:" << sFile << "|loading file now|MemLoad:" << cur->MemLoad << "|MemCount:" << cur->MemCount << "|FileName:" << cur->FileName << endl); - - //这里是由于内存分é…ä¸è¶³ï¼Œå¯¼è‡´å‡ºçŽ°loadingçš„çŠ¶æ€ - _mapFiles.erase(sFile); - - return -1; - } - - if (cur->FileSize == (size_t)st.st_size && cur->FileTime == st.st_mtime && cur->FileInode == st.st_ino) - { - mem.first = cur->MemBuf; - mem.second = cur->FileSize; - cur->MemTime = time(NULL); - cur->MemCount++; - - TLOGDEBUG("PatchCache::load sFile:" << sFile << "|reuse file in mem" << endl); - return 0; - } - else - { - cur->FileSize = st.st_size; - cur->FileInode = st.st_ino; - cur->FileTime = st.st_mtime; - cur->MemLoad = EM_RELOAD; - - mem.first = cur->MemBuf; - mem.second = st.st_size; - - TLOGDEBUG("PatchCache::load sFile:" << sFile << "|need reload file" << endl); - } - } - else - { - if (__getMem(sFile, cur) == -1) - { - TLOGERROR("PatchCache::load sFile:" << sFile << "|get free mem fault" << endl); - return -1; - } - - cur->FileName = sFile; - cur->FileSize = st.st_size; - cur->FileInode = st.st_ino; - cur->FileTime = st.st_mtime; - - mem.first = cur->MemBuf; - mem.second = cur->FileSize; - - if (_mapFiles.find(sFile) == _mapFiles.end()) - { - _mapFiles.insert(make_pair(sFile,cur)); - } - } - } - catch (std::exception & ex) - { - TLOGERROR("PatchCache::load sFile:" << sFile << "|exception:" << ex.what() << endl); - return -1; - } - catch (...) - { - TLOGERROR("PatchCache::load sFile:" << sFile << "|unknown Exception" << endl); - return -1; - } - - if (__loadFile(sFile, cur->MemBuf, cur->FileSize) == 0) - { - cur->MemCount++; - cur->MemTime = time(NULL); - cur->MemLoad = EM_LOADED; - - return 0; - } - else - { - cur->MemTime = 0; - cur->MemLoad = EM_NLOAD; - return -1; - } - - return -1; -} - -int PatchCache::release(const std::string & sFile) -{ - TLOGDEBUG("PatchCache::release sFile:" << sFile << endl); - - try - { - TC_ThreadLock::Lock lock(_mutex); - - std::map::iterator it = _mapFiles.find(sFile); - if (it == _mapFiles.end()) - { - TLOGERROR("PatchCache::release Find '" << sFile << "' fault" << endl); - return -1; - } - - it->second->MemCount--; - - TLOGDEBUG("PatchCache::release sFile:" << sFile << "|count:" << it->second->MemCount << endl); - return 0; - } - catch (std::exception & ex) - { - TLOGERROR("PatchCache::release sFile:" << sFile << "|exception:" << ex.what() << endl); - } - catch (...) - { - TLOGERROR("PatchCache::release sFile:" << sFile << "|unknown Exception" << endl); - } - - return -1; -} - -int PatchCache::__getMem(const std::string & sFile, struct MemState *& cur) -{ - TLOGDEBUG("PatchCache::__getMem sFile:" << sFile << endl); - - cur = NULL; - - for (size_t i = 0; i < _vecMems.size(); i++) - { - struct MemState * st = _vecMems[i]; - - TLOGDEBUG("PatchCache::__getMem sFile:" << sFile << "|MemLoad:"<MemLoad << "|MemCount:" << st->MemCount << "|MemTime:" << st->MemTime << "|FileName:" << st->FileName << endl); - - if ((st->MemLoad == EM_NLOAD || st->MemLoad == EM_LOADED) && st->MemCount == 0 && st->MemTime + g_app.getExpireTime() <= time(NULL)) - { - _mapFiles.erase(_vecMems[i]->FileName); - - cur = _vecMems[i]; - break; - } - } - - if (cur == NULL && _vecMems.size() < _MemNum) - { - cur = new MemState(); - cur->MemBuf = (char *)malloc(_MemMax);//从这里开始新建 - - _vecMems.push_back(cur); - - _mapFiles.insert(std::make_pair(sFile, cur)); - } - - if (cur != NULL) - { - cur->MemCount = 0; - cur->MemSize = _MemMax; - cur->MemLoad = EM_LOADING; - - if (cur->MemBuf == NULL)//有å¯èƒ½å†…存分é…失败 - { - //从vector删除新的分é…内存失败的元素 - _vecMems.erase(remove(_vecMems.begin(),_vecMems.end(),cur)); - - TLOGERROR("PatchCache::__getMem sFile:" << sFile << "|cur->MemBuf NULL" << endl); - - return -1; - } - - return 0; - } - - TLOGERROR("PatchCache::__getMem sFile:" << sFile << "|not enough mem" << endl); - - return -1; -} - -int PatchCache::__loadFile(const std::string & sFile, char * szBuff, size_t sizeLen) -{ - TLOGDEBUG("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << endl); - - if(szBuff == NULL) - { - TLOGERROR("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << "|szBuff NULL"<< endl); - - return -4; - } - - FILE * fp = fopen(sFile.c_str(), "r"); - if (fp == NULL) - { - TLOGERROR("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << "|open file error:" << strerror(errno) << endl); - return -1; - } - - //从指定ä½ç½®å¼€å§‹è¯»æ•°æ® - if (fseek(fp, 0, SEEK_SET) == -1) - { - fclose(fp); - - TLOGERROR("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << "|fseek error:" << strerror(errno) << endl); - return -2; - } - - //开始读å–文件 - size_t r = fread(szBuff, 1, sizeLen, fp); - if (r > 0) - { - //æˆåŠŸè¯»å–rå­—èŠ‚æ•°æ® - fclose(fp); - TLOGDEBUG("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << "|r:" << r << endl); - return 0; - } - else - { - //到文件末尾了 - if (feof(fp)) - { - fclose(fp); - TLOGDEBUG("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << "|to tail ok" << endl); - return 1; - } - - //读å–文件出错了 - TLOGERROR("PatchCache::__loadFile sFile:" << sFile << "|sizeLen:" << sizeLen << "|r:" << r << "|error:" << strerror(errno) << endl); - fclose(fp); - return -3; - } - -} - diff --git a/cpp/framework/PatchServer/PatchCache.h b/cpp/framework/PatchServer/PatchCache.h deleted file mode 100644 index e0ae68573..000000000 --- a/cpp/framework/PatchServer/PatchCache.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PATCH_CACHE_H_ -#define __PATCH_CACHE_H_ - -#include -#include -#include -#include "util/tc_monitor.h" - -const size_t SIZE_BUMEM_MIN = 1 * 1024 * 1024; -const size_t SIZE_BUMEM_MAX = 100 * 1024 * 1024; - -class PatchCache -{ -private: - enum FileLoad - { - EM_NLOAD = 0, - EM_LOADING = 1, - EM_RELOAD = 2, - EM_LOADED = 3 - }; - - struct MemState - { - string FileName; //文件路径 - size_t FileSize; //æ–‡ä»¶å¤§å° - size_t FileInode; //Inodeç´¢å¼•å· - time_t FileTime; //文件创建时间 - - char * MemBuf; //该å—å†…å­˜çš„èµ·å§‹åœ°å€ - size_t MemSize; //该å—å†…å­˜çš„å¤§å° - size_t MemCount; //指å‘该å—内存的数目 - time_t MemTime; //该å—内存最åŽæ“纵时间 - FileLoad MemLoad; //该å—内存已ç»åŠ è½½æˆåŠŸ - }; -public: - void setMemOption(const size_t MemMax, const size_t MemMin, const size_t MemNum) - { - _MemMax = MemMax > SIZE_BUMEM_MAX ? SIZE_BUMEM_MAX : MemMax; - _MemMin = MemMin < SIZE_BUMEM_MIN ? SIZE_BUMEM_MIN : MemMin; - _MemNum = MemNum; - } - - int load(const std::string & sFile, std::pair & mem); - - int release(const std::string & sFile); - -private: - int __loadFile(const std::string & sFile, char * szBuff, size_t sizeLen); - - int __getMem (const std::string & sFile, struct MemState *& cur); - -private: - tars::TC_ThreadLock _mutex; - - std::vector _vecMems; - - std::map _mapFiles; - - size_t _MemMax; - - size_t _MemMin; - - size_t _MemNum; -}; - -#endif - diff --git a/cpp/framework/PatchServer/PatchImp.cpp b/cpp/framework/PatchServer/PatchImp.cpp deleted file mode 100644 index fb06e8712..000000000 --- a/cpp/framework/PatchServer/PatchImp.cpp +++ /dev/null @@ -1,283 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_file.h" -#include "util/tc_config.h" -#include "util/tc_md5.h" -#include "servant/TarsLogger.h" -#include "PatchImp.h" -#include "PatchCache.h" -#include "PatchServer.h" - -extern PatchCache g_PatchCache; - -struct LIST -{ - LIST(const string& dir, vector &vf) : _dir(dir), _vf(vf) - { - } - - void operator()(const string &file) - { - //普通文件æ‰åŒæ­¥, 连接文件有效 - if(tars::TC_File::isFileExistEx(file, S_IFREG)) - { - FileInfo fi; - fi.path = file.substr(_dir.length()); - fi.size = tars::TC_File::getFileSize(file); - fi.canExec = tars::TC_File::canExecutable(file); - fi.md5 = tars::TC_MD5::md5file(file); - - _vf.push_back(fi); - - TLOGDEBUG("LIST path:" << fi.path << "|file:" << file << "|size:" << fi.size << "|exec:" << (fi.canExec?"true":"false") << endl); - } - } - - string _dir; - vector &_vf; -}; - -PatchImp::PatchImp() -: _size(1024*1024) -{ -} - -void PatchImp::initialize() -{ - try - { - _directory = (*g_conf)["/tars"]; - _uploadDirectory = (*g_conf)["/tars"]; - _size = TC_Common::toSize(g_conf->get("/tars", "1M"), 1024*1024); - } - catch(exception &ex) - { - TLOGDEBUG("PatchImp::initialize directory must not be empty." << endl); - exit(0); - } - - if(!tars::TC_File::isAbsolute(_directory)) - { - TLOGDEBUG("PatchImp::initialize directory must be absolute directory path." << endl); - exit(0); - } - - TLOGDEBUG("PatchImp::initialize patch dirtectory:" << _directory << "|uploadDirectory:" << _uploadDirectory << "|size:" << _size << endl); -} - - -/************************************************************************************************** - ** 对外接å£ï¼Œæ™®é€šä¸‹è½½æŽ¥å£ - ** - **/ -int PatchImp::listFileInfo(const string &path, vector & vf, TarsCurrentPtr current) -{ - TLOGDEBUG("PatchImp::listFileInfo ip:" << current->getIp() << "|path:" << path << endl); - - string dir = tars::TC_File::simplifyDirectory(_directory + "/" + path); - - int ret = __listFileInfo(dir, vf); - - stringstream ss; - tars::TarsDisplayer ds(ss); - ds.displaySimple(vf, false); - - TLOGDEBUG("PatchImp::listFileInfo ip:" << current->getIp() << "|path:" << path << "|dir:" << dir << "|str:" << ss.str() << endl); - - return ret; -} - -int PatchImp::download(const string & file, int pos, vector & vb, TarsCurrentPtr current) -{ - TLOGDEBUG("PatchImp::download ip:" << current->getIp() << "|file:" << file << "|pos:" << pos << endl); - - string path = tars::TC_File::simplifyDirectory(_directory + "/" + file); - - int iRet = -1; - - if (iRet < 0) - { - iRet = __downloadFromMem (path, pos, vb); - } - - if (iRet < 0) - { - iRet = __downloadFromFile(path, pos, vb); - } - - return iRet; -} - -int PatchImp::preparePatchFile(const string &app, const string &serverName, const string &patchFile, TarsCurrentPtr current) -{ - string upfile = _uploadDirectory + "/" + app + "/" + serverName + "/" + patchFile; - string dstDirectory = _directory + "/TARSBatchPatching/" + app + "/" + serverName; - string dstfile = dstDirectory + "/" + app +"." + serverName + ".tgz"; - - TLOGDEBUG("PatchImp::preparePatchFile upfile:" << upfile << "|dstfile:" << dstfile << endl); - - bool succ = TC_File::makeDirRecursive(dstDirectory); - if (!succ) - { - TLOGERROR("PatchImp::preparePatchFile makeDirRecursive path:" << dstDirectory << "|error!" << endl); - return -2; - } - - if (!TC_File::isFileExist(upfile)) - { - TLOGERROR("PatchImp::preparePatchFile isFileExist file:" << upfile << "|not exist!" << endl); - return -1; - } - - TC_File::copyFile(upfile, dstfile, true); - - return 0; -} - -/************************************************************************************************** - * å‘布æœåŠ¡å†…部使用函数 - */ -int PatchImp::__listFileInfo(const string &path, vector &vf) -{ - TLOGDEBUG("PatchImp::__listFileInfo patch:" << path << endl); - - if (path.find("..") != string::npos) - { - return -1; - } - - if(!tars::TC_File::isFileExistEx(path, S_IFDIR)) - { - //是文件 - FileInfo fi; - fi.path = tars::TC_File::extractFileName(path); - fi.size = tars::TC_File::getFileSize(path); - fi.canExec = tars::TC_File::canExecutable(path); - fi.md5 = tars::TC_MD5::md5file(path); - - vf.push_back(fi); - - return 1; - } - else - { - //目录 - vector files; - - tars::TC_File::listDirectory(path, files, true); - - for_each(files.begin(), files.end(), LIST(path, vf)); - - return 0; - } - - return 0; -} - -int PatchImp::__downloadFromMem (const string & file, size_t pos, vector & vb) -{ - TLOGDEBUG("PatchImp::__downloadFromMem file:" << file << "|pos:" << pos << "|size:" << _size << endl); - - pair mem; - if (g_PatchCache.load(file, mem) != 0) - { - TLOGERROR("PatchImp::__downloadFromMem file:" << file << "|pos:" << pos << "|LoadFile error" << endl); - return -1; - } - - if (pos >= mem.second) - { - TLOGDEBUG("PatchImp::__downloadFromMem file:" << file << "|pos:" << pos << "|to tail ok" << endl); - - g_PatchCache.release(file); - - return 1; - } - - const size_t sizeBuf = mem.second - pos >= _size ? _size : mem.second - pos; - - TLOGDEBUG("PatchImp::__downloadFromMem file:" << file << "|pos:" << pos << "|sizeBuf:" << sizeBuf << endl); - - vb.resize(sizeBuf); - - memcpy((char *)&vb[0], mem.first + pos, sizeBuf); - - g_PatchCache.release(file); - - return 0; -} - - -int PatchImp::__downloadFromFile(const string & file, size_t pos, vector & vb) -{ - TLOGDEBUG("PatchImp::__downloadFromFile file:" << file << "|pos:" << pos << "|size:" << _size << endl); - - FILE * fp = fopen(file.c_str(), "rb"); - if (fp == NULL) - { - TLOGERROR("PatchImp::__downloadFromFile file:" << file << "|pos:" << pos << "|open file error:" << strerror(errno) << endl); - return -1; - } - - //从指定ä½ç½®å¼€å§‹è¯»æ•°æ® - if (fseek(fp, pos, SEEK_SET) == -1) - { - fclose(fp); - - TLOGERROR("PatchImp::__downloadFromFile file:" << file << "|pos:" << pos << "|fseek error:" << strerror(errno) << endl); - return -2; - } - - //开始读å–文件 - vb.resize(_size); - size_t r = fread((void*)(&vb[0]), 1, _size, fp); - if (r > 0) - { - //æˆåŠŸè¯»å–rå­—èŠ‚æ•°æ® - vb.resize(r); - - fclose(fp); - - TLOGDEBUG("PatchImp::__downloadFromFile file:" << file << "|pos:" << pos << "|r:" << r << endl); - - return 0; - } - else - { - //到文件末尾了 - if (feof(fp)) - { - fclose(fp); - - TLOGDEBUG("PatchImp::__downloadFromFile file:" << file << "|pos:" << pos << "|to tail ok" << endl); - - return 1; - } - - //读å–文件出错了 - TLOGERROR("PatchImp::__downloadFromFile file:" << file << "|pos:" << pos << "|r:" << r << "|error:" << strerror(errno) << endl); - - fclose(fp); - - return -3; - } - -} - - - - diff --git a/cpp/framework/PatchServer/PatchImp.h b/cpp/framework/PatchServer/PatchImp.h deleted file mode 100644 index 2e80b84d9..000000000 --- a/cpp/framework/PatchServer/PatchImp.h +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PATCH_IMP_H_ -#define __PATCH_IMP_H_ - -#include "Patch.h" - -using namespace tars; - -class PatchImp : public Patch -{ -public: - /** - * - */ - PatchImp(); - - /** - * åˆå§‹åŒ– - * - * @return int - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy() {}; - - /** - * 获å–è·¯å¾„ä¸‹æ‰€æœ‰æ–‡ä»¶åˆ—è¡¨ä¿¡æ¯ - * @param path, 目录路径, 相对_directory的路径, ä¸èƒ½æœ‰.. - * @param vector, æ–‡ä»¶åˆ—è¡¨ä¿¡æ¯ - * @return int - */ - int listFileInfo(const string &path, vector &vf, TarsCurrentPtr current); - - /** - * 下载文件 - * @param file, 文件完全路径 - * @param pos, 从什么ä½ç½®å¼€å§‹ä¸‹è½½ - * @return vector, 文件内容 - */ - int download(const string &file, int pos, vector &vb, TarsCurrentPtr current); - - /** - * 准备好需è¦patch的文件,å°†å‘布的文件从上传目录å¤åˆ¶åˆ°å‘布目录 - * @param app, 应用å - * @param serverName, æœåŠ¡å - * @param patchFile, 需è¦å‘布的文件å - * @return int, 0: æˆåŠŸ, <0: 失败 - */ - int preparePatchFile(const string &app, const string &serverName, const string &patchFile, TarsCurrentPtr current); - -protected: - int __listFileInfo(const string &path, vector &vf); - - int __downloadFromMem (const string & file, size_t pos, vector & vb); - - int __downloadFromFile(const string & file, size_t pos, vector & vb); - -protected: - /** - * 目录 - */ - string _directory; - - /** - * 上传的目录 - */ - string _uploadDirectory; - - /** - * æ¯æ¬¡åŒæ­¥å¤§å° - */ - size_t _size; -}; - -#endif diff --git a/cpp/framework/PatchServer/PatchServer.cpp b/cpp/framework/PatchServer/PatchServer.cpp deleted file mode 100644 index 659aca885..000000000 --- a/cpp/framework/PatchServer/PatchServer.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PatchServer.h" -#include "PatchImp.h" -#include "PatchCache.h" - -PatchCache g_PatchCache; - -void PatchServer::initialize() -{ - //增加对象 - addServant(ServerConfig::Application + "." + ServerConfig::ServerName +".PatchObj"); - - size_t memMax = TC_Common::toSize(g_conf->get("/tars", "100M"), 1024*1024); - size_t memMin = TC_Common::toSize(g_conf->get("/tars", "1M"), 1024*1024); - size_t memNum = TC_Common::strto(g_conf->get("/tars", "10")); - - g_PatchCache.setMemOption(memMax, memMin, memNum); - - int _expireTime = TC_Common::strto(g_conf->get("/tars", "30")); - - TLOGDEBUG("PatchServer::initialize memMax:" << memMax << "|memMin:" << memMin << "|memNum:" << memNum << "|expireTime:" << _expireTime << endl); -} - -void PatchServer::destroyApp() -{ - TLOGDEBUG("PatchServer::destroyApp ok" << endl); -} - -int PatchServer::getExpireTime() const -{ - return _expireTime; -} diff --git a/cpp/framework/PatchServer/PatchServer.h b/cpp/framework/PatchServer/PatchServer.h deleted file mode 100644 index 377886687..000000000 --- a/cpp/framework/PatchServer/PatchServer.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PATCH_SERVER_H_ -#define __PATCH_SERVER_H_ - -#include "servant/Application.h" -#include "Patch.h" - -using namespace tars; - -class PatchServer : public Application -{ -protected: - /** - * åˆå§‹åŒ–, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžå¤Ÿ, 进程退出时会调用一次 - */ - virtual void destroyApp(); - -public: - int getExpireTime() const; - -private: - //文件的内存缓存的过期时间 - int _expireTime; -}; - -extern TC_Config *g_conf; -extern PatchServer g_app; - -#endif diff --git a/cpp/framework/PatchServer/main.cpp b/cpp/framework/PatchServer/main.cpp deleted file mode 100644 index d1b12a75e..000000000 --- a/cpp/framework/PatchServer/main.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PatchServer.h" -#include - -using namespace std; - -TC_Config *g_conf; -PatchServer g_app; -/** - * 说明 - * 1 需è¦é…置一个路径 - * 2 如果å­ç›®å½•æ˜¯link目录, 则ä¸è¿›å…¥link目录é历 - * 3 如果文件是link, 则读å–link的文件 - * 4 如果åŒæ­¥çš„文件是link目录, 则进入link目录é历, 下é¢çš„规则如1 - * @param argc - * @param argv - * - * @return int - */ -int main(int argc, char *argv[]) -{ - try - { - g_conf = &g_app.getConfig(); - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/PropertyServer/CMakeLists.txt b/cpp/framework/PropertyServer/CMakeLists.txt deleted file mode 100644 index b39a690d2..000000000 --- a/cpp/framework/PropertyServer/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ - -set(MODULE "tarsproperty") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/) - -complice_module(${MODULE}) diff --git a/cpp/framework/PropertyServer/PropertyDbManager.cpp b/cpp/framework/PropertyServer/PropertyDbManager.cpp deleted file mode 100644 index f9a7f39b4..000000000 --- a/cpp/framework/PropertyServer/PropertyDbManager.cpp +++ /dev/null @@ -1,655 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PropertyDbManager.h" -#include "PropertyReapThread.h" -#include "util/tc_config.h" -#include "PropertyServer.h" -#include "PropertyHashMap.h" -/////////////////////////////////////////////////////////// -// - -PropertyDbManager::PropertyDbManager() -: _terminate(false) -{ - TLOGDEBUG("begin PropertyDbManager init" << endl); - - _sql = (*g_pconf)["/tars"]; - _sqlStatus = g_pconf->get("/tars", ""); - _reapSql = g_pconf->getDomainMap("/tars/reapSql"); - _tbNamePre = g_pconf->get("/tars/db","t_property_realtime_"); - _maxInsertCount = TC_Common::strto(g_pconf->get("/tars/reapSql","1000")); - _enableWeighted = TC_Common::strto(g_pconf->get("/tars/","0")); - - if (_sqlStatus == "") - { - _sqlStatus = "CREATE TABLE `t_ecstatus` ( " - " `id` int(11) NOT NULL auto_increment, " - " `appname` varchar(64) NOT NULL default '', " - " `action` tinyint(4) NOT NULL default '0', " - " `checkint` smallint(6) NOT NULL default '10', " - " `lasttime` varchar(16) NOT NULL default '', " - " PRIMARY KEY (`appname`,`action`), " - " UNIQUE KEY `id` (`id`) " - " ) ENGINE=HEAP DEFAULT CHARSET=gbk"; - } - - string sCutType = g_pconf->get("/tars/reapSql","hour"); - if (sCutType == "day") - { - _cutType = CUT_BY_DAY; - } - else if (sCutType == "minute") - { - _cutType = CUT_BY_MINUTE; - } - else - { - _cutType = CUT_BY_HOUR; - } - - size_t iInsertThreadByMachine = TC_Common::strto(g_pconf->get("/tars/reapSql","4")); - size_t iInsertThreadByDB = TC_Common::strto(g_pconf->get("/tars/reapSql","2")); - - TLOGDEBUG("PropertyDbManager init iInsertThreadByMachine:" << iInsertThreadByMachine << "|iInsertThreadByDB:" << iInsertThreadByDB << endl); - - vector vDb =g_pconf->getDomainVector("/tars/multidb"); - - _dbNumber = vDb.size(); - - TLOGDEBUG("PropertyDbManager init multidb size:" <<_dbNumber << endl); - - //map mIp; - map > mIp; - vector vIp; - string sIp(""); - string sIpAndPort(""); - - for (int i=0; i< _dbNumber; i++) - { - TC_DBConf tConf; - tConf.loadFromMap(g_pconf->getDomainMap("/tars/multidb/" + vDb[i])); - _sTbNamePre.push_back(g_pconf->get("/tars/multidb/" + vDb[i] + "", - "t_propert_0" + TC_Common::tostr(i) + "_")); - - sIp = tConf._host; - - sIpAndPort = "ip:"; - sIpAndPort += tConf._host; - sIpAndPort += "|port:"; - sIpAndPort += TC_Common::tostr(tConf._port); - - _ipAndPort.push_back(sIpAndPort); - - map >::const_iterator iter = mIp.find(sIp); - - if(iter == mIp.end()) - { - pair tempPair; - tempPair.first = 0; - tempPair.second = 0; - - //mIp.insert(map::value_type(sIp, 0)); - mIp.insert(map >::value_type(sIp, tempPair)); - - _dbIpNum++; - - vector vIndex; - vIndex.push_back(i); - - _ipHasDbInfo.insert(map >::value_type(sIp, vIndex)); - } - else - { - _ipHasDbInfo[sIp].push_back(i); - } - - vIp.push_back(sIp); - - //默认值为1 - _dbWeighted.push_back(TC_Common::strto(g_pconf->get("/tars/multidb/" + vDb[i] + "","1"))); - - TC_Mysql *pMysql = new TC_Mysql(); - pMysql->init(tConf); - _ssdThreadMysql.push_back(pMysql); - } - - if(iInsertThreadByMachine < _dbIpNum) - { - _dbIpNum = iInsertThreadByMachine; - } - - TLOGDEBUG("PropertyDbManager init insert DB threadnum:" << _dbIpNum << endl); - - size_t iDbOneMachine = 999999; - map >::iterator m_iter = _ipHasDbInfo.begin(); - while(m_iter != _ipHasDbInfo.end()) - { - vector &vDb = m_iter->second; - - if(vDb.size() < iDbOneMachine) - { - iDbOneMachine = vDb.size(); - } - ++m_iter; - } - - TLOGDEBUG("PropertyDbManager init iDbOneMachine:" << iDbOneMachine << endl); - - if(iInsertThreadByDB > iDbOneMachine) - { - iInsertThreadByDB = iDbOneMachine; - } - - _oneDbHasThreadNum = iInsertThreadByDB; - - TLOGDEBUG("PropertyDbManager init iInsertThreadByDB:" << iInsertThreadByDB << endl); - - size_t iIndex = 0; - map >::iterator map_it = mIp.begin(); - - while(map_it != mIp.end()) - { - map_it->second.first = iIndex; - map_it->second.second = 0; - ++iIndex; - - if(iIndex == _dbIpNum) - { - iIndex = 0; - } - - ++map_it; - } - - for(size_t i = 0; i < vIp.size(); ++i) - { - map >::iterator iter = mIp.find(vIp[i]); - if(iter != mIp.end()) - { - _dbToIp.insert(map::value_type(i, (iter->second.first * iInsertThreadByDB + iter->second.second))); - - iter->second.second += 1; - - if(iter->second.second == iInsertThreadByDB) - { - iter->second.second = 0; - } - } - else - { - TLOGERROR("PropertyDbManager init DbToIp error ip:" << vIp[i] << endl); - } - } - - map::iterator mDbToIpIter = _dbToIp.begin(); - while(mDbToIpIter != _dbToIp.end()) - { - TLOGDEBUG("PropertyDbManager init index:" << mDbToIpIter->first << "|thread index:" << mDbToIpIter->second << endl); - ++mDbToIpIter; - } - - map >::iterator m_iter1 = _ipHasDbInfo.begin(); - while(m_iter1 != _ipHasDbInfo.end()) - { - vector &vDb = m_iter1->second; - - FDLOG("PropertyPool") << "statsec ip:" << ServerConfig::LocalIp << "|ip:" << m_iter1->first << "|DbNum:" << vDb.size() << endl; - - ++m_iter1; - } - - TLOGDEBUG("PropertyDbManager init ok." << endl); -} -/////////////////////////////////////////////////////////// -PropertyDbManager::~PropertyDbManager() -{ - vector::iterator it = _ssdThreadMysql.begin(); - while(it != _ssdThreadMysql.end()) - { - if(*it) - { - delete *it; - } - ++it; - } -} -/////////////////////////////////////////////////////////// -size_t PropertyDbManager::getDbToIpIndex(size_t iIndex) -{ - map::const_iterator iter = _dbToIp.find(iIndex); - if(iter != _dbToIp.end()) - { - return iter->second; - } - return -1; -} -/////////////////////////////////////////////////////////// -bool PropertyDbManager::hasEnableWeighted() -{ - return _enableWeighted; -} -/////////////////////////////////////////////////////////// -string PropertyDbManager::getIpAndPort(size_t iDbIndex) -{ - assert(iDbIndex < _ipAndPort.size()); - - return _ipAndPort[iDbIndex]; -} -/////////////////////////////////////////////////////////// -void PropertyDbManager::getDbWeighted(int& iGcd,int& iMaxW,vector& vDbWeighted) -{ - vDbWeighted = _dbWeighted; - - int gcd = 0; - int iMax = 0; - for(size_t i = 0;i < vDbWeighted.size(); i++) - { - gcd = getGcd(gcd,vDbWeighted[i]); - - if(vDbWeighted[i] > iMax) - { - iMax = vDbWeighted[i]; - } - } - - iGcd = gcd; - - iMaxW = iMax; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::getGcd (int a, int b) -{ - int c; - while(a != 0) - { - c = a; - a = b%a; - b = c; - } - - return b; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::insert2Db(const PropertyMsg &mPropMsg, const string &sDate, const string &sFlag, int iOldWriteNum, int &iNowWriteNum, const string &sTbNamePre, TC_Mysql *pMysql) -{ - int iCount = 0; - int iHasWriteNum = iOldWriteNum; - ostringstream osSql; - string strValues; - - iNowWriteNum = 0; - - string strTbName = (sTbNamePre !=""?sTbNamePre:_tbNamePre) + TC_Common::replace(sDate,"-","") + sFlag.substr(0,_cutType*2); - try - { - creatTable(strTbName,pMysql); - - for(map::const_iterator it = mPropMsg.begin(); it != mPropMsg.end(); it++ ) - { - if(_terminate) - { - return -1; - } - - if(iHasWriteNum > 0) - { - --iHasWriteNum; - continue; - } - - const PropHead& head = it->first; - const PropBody& body = it->second; - //组织sqlè¯­å¥ - for(size_t i = 0; i < body.vInfo.size(); i++) - { - strValues = " ('"; - strValues += sDate; - strValues += "','"; - strValues += sFlag; - strValues += "','"; - strValues += pMysql->escapeString(head.moduleName); - strValues += "','"; - strValues += pMysql->escapeString(head.ip); - strValues += "','"; - strValues += pMysql->escapeString(head.propertyName); - strValues += "','"; - strValues += pMysql->escapeString(head.setName); - strValues += "','"; - strValues += pMysql->escapeString(head.setArea); - strValues += "','"; - strValues += pMysql->escapeString(head.setID); - strValues += "','"; - strValues += pMysql->escapeString(body.vInfo[i].policy); - strValues += "','"; - - //如果是平å‡å€¼ - if(body.vInfo[i].policy == "Avg") - { - vector sTmp = TC_Common::sepstr(body.vInfo[i].value,"="); - double avg = 0; - if(2 == sTmp.size() && TC_Common::strto(sTmp[1]) != 0) - { - avg = TC_Common::strto(sTmp[0])/TC_Common::strto(sTmp[1]); - } - else - { - avg = TC_Common::strto(sTmp[0]); - } - - - strValues += pMysql->escapeString(TC_Common::tostr(avg)); - strValues += "') "; - } - else - { - strValues += pMysql->escapeString(body.vInfo[i].value); - strValues += "') "; - } - - if ( iCount == 0 ) - { - osSql.str(""); - osSql << "insert ignore into " + strTbName + " (f_date,f_tflag,master_name,master_ip,property_name,set_name,set_area,set_id,policy,value) values "; - osSql << strValues; - } - else - { - osSql<< "," + strValues; - } - iNowWriteNum++; - iCount ++; - } - if ( iCount >= _maxInsertCount ) - { - usleep(100); - pMysql->execute(osSql.str()); - - TLOGDEBUG("insert " << strTbName << " affected:" << iCount << endl); - - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|insert " << strTbName << "|insert affected:" << iCount << "|mysql affected:" << pMysql->getAffectedRows() << endl; - - iCount = 0; - } - } - - if ( iCount != 0 ) - { - pMysql->execute(osSql.str()); - TLOGDEBUG("insert " << strTbName << " affected:" << iCount << endl); - FDLOG("PropertyPool") << "statsec ip:" << ServerConfig::LocalIp << "|insert " << strTbName << "|insert affected:" << iCount << "|mysql affected:" << pMysql->getAffectedRows() << endl; - } - } - catch (TC_Mysql_Exception& ex) - { - string err = string (ex.what()); - if (std::string::npos == err.find( "Duplicate")) - { - creatTable(strTbName,pMysql); - } - //因为会输出1åƒæ¡è®°å½•ï¼Œè¿™é‡Œåšæˆªå– - TLOGERROR("insert2Db exception: " << err.substr(0,64) << endl); - return 1; - } - catch (exception& ex) - { - TLOGERROR("insert2Db exception: " << ex.what() << endl); - return 1; - } - return 0; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::creatTable(const string &sTbName, TC_Mysql *_pstMql) -{ - string daytime = sTbName.substr(0, sTbName.length() -2 ); // 去掉å°æ—¶ - int iRet = 0; - int hour = 0; - - for (hour=0; hour < 24; hour++) - { - char buf[3]; - snprintf(buf,sizeof(buf),"%.2d",hour); - string sName = daytime + string(buf); - iRet |= creatTb(sName, _pstMql); - } - - return iRet; -} -/////////////////////////////////////////////////////////// -bool PropertyDbManager::hasdbTableExist(const string& sTbName,TC_Mysql *pMysql) -{ - try - { - TC_Mysql::MysqlData tTotalRecord = pMysql->queryRecord("show tables like '%"+sTbName+"%'"); - TLOGINFO(__FUNCTION__<<"|show tables like '%"+sTbName+"%|affected:"< 0) - { - return true; - } - else - { - return false; - } - - } - catch(TC_Mysql_Exception& ex) - { - TLOGERROR("exception: " << ex.what() << endl); - return false; - } -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::creatTb(const string &sTbName,TC_Mysql *pMysql) -{ - try - { - - if (!hasdbTableExist(sTbName,pMysql)) - { - string sSql = TC_Common::replace(_sql, "${TABLE}",sTbName); - TLOGINFO(sSql << endl); - pMysql->execute(sSql); - } - - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("PropertyDbManager::creatTb exception: " << ex.what() << endl); - return 1; - } -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::creatEscTb(const string &sTbName, const string& sSql , TC_Mysql *pMysql) -{ - try - { - if (!hasdbTableExist(sTbName,pMysql)) - { - TLOGDEBUG(sSql << endl); - pMysql->execute(sSql); - - } - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("exception: " << ex.what() << endl); - return 1; - } -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::updateEcsStatus(const string &sLastTime,const string &sTbNamePre,TC_Mysql *pMysql) -{ - try - { - string sAppName = sTbNamePre != ""?sTbNamePre:_tbNamePre; - string sCondition = "where appname='"+sAppName+"' and action =0"; - - TC_Mysql::RECORD_DATA rd; - rd["lasttime"] = make_pair(TC_Mysql::DB_STR, sLastTime); - - int iRet = pMysql->updateRecord("t_ecstatus", rd,sCondition); - - TLOGDEBUG("PropertyDbManager::updateEcsStatus iRet: " <getLastSQL()<< endl); - if (iRet == 0 ) - { - rd["appname"] = make_pair(TC_Mysql::DB_STR, sAppName); - rd["checkint"] = make_pair(TC_Mysql::DB_INT, TC_Common::tostr(g_app.getInserInterv())); - rd["lasttime"] = make_pair(TC_Mysql::DB_STR, sLastTime); - iRet = pMysql->replaceRecord("t_ecstatus", rd); - } - if (iRet != 1) - { - TLOGDEBUG("PropertyDbManager::updateEcsStatus erro: ret:" << iRet<<"\n"<getLastSQL()<< endl); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("PropertyDbManager::updateEcsStatus exception: " << ex.what() << endl); - creatEscTb("t_ecstatus", _sqlStatus, pMysql); - return 1; - } - return 0; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::insert2MultiDbs(int iIndex, const PropertyMsg &propertymsg, const string &sDate, const string &sFlag) -{ - try - { - string sIp = ServerConfig::LocalIp; - - string sLastTime = sDate + " " + sFlag; - - string sTbNamePre = ""; - TC_Mysql * pMysql = NULL; - - sTbNamePre = _sTbNamePre[iIndex]; - - pMysql = _ssdThreadMysql[iIndex]; - - if (checkLastTime(sLastTime, sTbNamePre + sIp, pMysql) == 0) //=0,没有记录表示: æ•°æ®åº“记录的时间,比当å‰å½“å‰æ—¶é—´å° - { - TLOGDEBUG("begin insert to db " << getIpAndPort(iIndex) << endl); - - int iOldWrite = 0; - int iNowWrite = 0; - - int64_t iBegin = tars::TC_TimeProvider::getInstance()->getNowMs(); - - if(insert2Db(propertymsg, sDate, sFlag, iOldWrite, iNowWrite, sTbNamePre, pMysql) != 0) - { - if(_terminate) - { - return -1; - } - - iOldWrite = iNowWrite; - iNowWrite = 0; - - if(insert2Db(propertymsg ,sDate, sFlag, iOldWrite, iNowWrite, sTbNamePre, pMysql) != 0 ) - { - if(_terminate) - { - return -1; - } - - string sMsg("insert2Db_"); - sMsg += getIpAndPort(iIndex); - - sendAlarmSMS(sMsg); - } - else - { - if(updateEcsStatus(sLastTime, sTbNamePre + sIp, pMysql) != 0) - { - string sMsg("updateEcsStatus_"); - sMsg += getIpAndPort(iIndex); - - sendAlarmSMS(sMsg); - } - } - } - else - { - if(updateEcsStatus(sLastTime, sTbNamePre + sIp, pMysql) != 0) - { - string sMsg("updateEcsStatus_"); - sMsg += getIpAndPort(iIndex); - - sendAlarmSMS(sMsg); - } - } - - int64_t iEnd = tars::TC_TimeProvider::getInstance()->getNowMs(); - - TLOGDEBUG("insert|" << iIndex << "|" << getIpAndPort(iIndex) << "|" << sDate << "|" << sFlag << "|" << propertymsg.size() << "|" << (iEnd - iBegin) << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|insert|dbIndex:" << iIndex << "|" << getIpAndPort(iIndex) << "|date:" << sDate << "|tflag:" << sFlag - << "|records:" << propertymsg.size() << "|timecost(ms):" << (iEnd - iBegin) << "|iBegin(ms):" << iBegin << "|iEnd(ms):" << iEnd << endl; - } - } - catch(TC_Mysql_Exception& ex) - { - TLOGERROR("PropertyDbManager::insert2MultiDbs TC_Mysql_Exception: " << ex.what() << endl); - } - catch(exception& ex) - { - TLOGERROR("PropertyDbManager::insert2MultiDbs exception: " << ex.what() << endl); - } - - return 0; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::sendAlarmSMS(const string &sMsg) -{ - string errInfo = " ERROR:" + ServerConfig::LocalIp + "|" + sMsg + ":统计入库失败,请åŠæ—¶å¤„ç†!"; - TARS_NOTIFY_ERROR(errInfo); - - TLOGERROR("TARS_NOTIFY_ERROR " << errInfo << endl); - - return 0; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::getDbNumber() -{ - return _dbNumber; -} -/////////////////////////////////////////////////////////// -int PropertyDbManager::checkLastTime(const string &sLastTime,const string &sTbNamePre,TC_Mysql *pMysql) -{ - int iRet = 0; - try - { - string sAppName = sTbNamePre != "" ? sTbNamePre : _tbNamePre; - string sCondition = "where appname='"; - sCondition += sAppName; - sCondition += "' and lasttime >= '"; - sCondition += sLastTime; - sCondition += "'" ; - - iRet = pMysql->getRecordCount("t_ecstatus", sCondition); - - TLOGDEBUG("StatDbManager::checkLastTime iRet: " <getLastSQL()<< endl); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("StatDbManager::checkLastTime exception: " << ex.what() << endl); - creatEscTb("t_ecstatus", _sqlStatus, pMysql); - return 0; - } - - return iRet; -} diff --git a/cpp/framework/PropertyServer/PropertyDbManager.h b/cpp/framework/PropertyServer/PropertyDbManager.h deleted file mode 100644 index 88e25724f..000000000 --- a/cpp/framework/PropertyServer/PropertyDbManager.h +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_MANAGER_H_ -#define __DB_MANAGER_H_ - -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "servant/TarsLogger.h" -#include "jmem/jmem_hashmap.h" -#include "servant/PropertyF.h" -#include "PropertyHashMap.h" - -using namespace tars; - -class PropertyDbManager : public TC_Singleton,public TC_ThreadMutex -{ -public: - enum CutType - { - CUT_BY_DAY = 0, - CUT_BY_HOUR = 1, - CUT_BY_MINUTE = 2, - }; - -public: - PropertyDbManager(); - - ~PropertyDbManager(); - -public: - - int creatTb(const string &strTbName,TC_Mysql *pMysql = NULL); - - int creatTable(const string &sTbName, TC_Mysql *_pstMql); - - int creatEscTb(const string &sTbName, const string& sSql , TC_Mysql *pMysql); - - int insert2Db(const PropertyMsg &mPropMsg,const string &sDate,const string &sFlag, int iOldWriteNum, int &iNowWriteNum,const string &sTbNamePre = "",TC_Mysql *pMysql = NULL); - - int updateEcsStatus(const string &sLastTime,const string &sTbNamePre = "",TC_Mysql *pMysql = NULL); - - int checkLastTime(const string &sLastTime,const string &sTbNamePre,TC_Mysql *pMysql); - - int insert2MultiDbs(int iIndex, const PropertyMsg &propertymsg, const string &sDate, const string &sFlag); - - int sendAlarmSMS(const string &sMsg); - - int getDbNumber(); - - int genRandOrder(); - - /** - * 获å–å„个dbçš„æƒé‡å€¼ï¼Œå¹¶è¿”回所有æƒé‡çš„最大公约数值和最大æƒé‡ - * @param iGcd - * @param iMaxW - * @param vDbWeighted - */ - void getDbWeighted(int& iGcd,int& iMaxW,vector& vDbWeighted); - - bool hasEnableWeighted(); - - bool hasdbTableExist(const string& sTbName,TC_Mysql *pMysql); - - size_t getDbToIpIndex(size_t iIndex); - - size_t getDbIpNum() { return _dbIpNum; } - - size_t getInsertDbThreadNum() { return _dbIpNum * _oneDbHasThreadNum; } - - string getIpAndPort(size_t iDbIndex); - - map >& getIpHasDbInfo() { return _ipHasDbInfo; } - - void setTerminateFlag(bool bFlag) { _terminate = bFlag; } - -private: - - /** - *Greatest Common Divisor - *最大公约数 - */ - int getGcd (int a, int b); - -private: - - - bool _terminate; //入库时,åœæ­¢çš„控制开关 - string _sql; //创建表 - string _sqlStatus; //创建表t_ecstatus - string _tbNamePre; //表å‰ç¼€ - int _maxInsertCount; //一次最大æ’å…¥æ¡æ•° - CutType _cutType; //分表类型 - int _tableInterval; - map _reapSql; //定时执行sql - - vector _ssdThreadMysql; // reapthread中使用。 - vector _sTbNamePre; - int _dbNumber; - vector _randOrder; - - //å„个dbçš„æƒé‡å€¼ - vector _dbWeighted; - bool _enableWeighted; - - //多线程入库使用 - size_t _dbIpNum; - size_t _oneDbHasThreadNum; - map _dbToIp; - map > _ipHasDbInfo; - //打å°æ—¥å¿—使用 - vector _ipAndPort; - - -}; - -#endif - - diff --git a/cpp/framework/PropertyServer/PropertyHashMap.h b/cpp/framework/PropertyServer/PropertyHashMap.h deleted file mode 100644 index 72e6668d1..000000000 --- a/cpp/framework/PropertyServer/PropertyHashMap.h +++ /dev/null @@ -1,213 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROPERTY_HASHMAP_H_ -#define __PROPERTY_HASHMAP_H_ - -#include "util/tc_common.h" -#include "jmem/jmem_hashmap.h" -#include "servant/PropertyF.h" -#include "servant/TarsLogger.h" - -using namespace tars; - -typedef StatPropMsgBody PropBody; -typedef StatPropMsgHead PropHead; -typedef TarsHashMap PropHashMap; - -typedef std::map, __gnu_cxx::__pool_alloc > > PropertyMsg; - -class PropertyHashMap : public PropHashMap -{ -public: - - - /** - * å¢žåŠ æ•°æ® - * @param Key - * @param Value - * - * @return int - */ - int add(const PropHead &head, const StatPropMsgBody &body) - { - int ret = TC_HashMap::RT_OK; - tars::TarsOutputStream osk; - head.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - { - TC_LockT lock(ThreadLockPolicy::mutex()); - string sv; - time_t t = 0; - ret = this->_t.get(sk, sv,t); - if ( ret < 0 || ret == TC_HashMap::RT_ONLY_KEY || ret == TC_HashMap::RT_NO_DATA) - { - - tars::TarsOutputStream osv; - body.writeTo(osv); - string stemp(osv.getBuffer(), osv.getLength()); - vector vtData; - return this->_t.set(sk, stemp,true,vtData); - } - - //读å–到数æ®äº†, 解包 - if (ret == TC_HashMap::RT_OK) - { - tars::TarsInputStream is; - is.setBuffer(sv.c_str(), sv.length()); - - PropBody stBody; - stBody.readFrom(is); - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - ostringstream os; - head.displaySimple(os); - os<< " "< mSumib; - - for (size_t i=0; i< stBody.vInfo.size(); i++) - { - mSumib.insert(make_pair(stBody.vInfo[i].policy, stBody.vInfo[i].value)); - } - - for (size_t i=0; i< body.vInfo.size(); i++) - { - string sPolicy = body.vInfo[i].policy; - string inValue = body.vInfo[i].value; - map::iterator it = mSumib.find(sPolicy); - if(it != mSumib.end()) - { - if (sPolicy == "Count") - { - long long count = TC_Common::strto(it->second) + TC_Common::strto(inValue); - it->second = TC_Common::tostr(count); - } - else if (sPolicy == "Sum") - { - long long sum = TC_Common::strto(it->second) + TC_Common::strto(inValue); - it->second = TC_Common::tostr(sum); - } - else if (sPolicy == "Min") - { - long long s = TC_Common::strto(it->second); - long long in = TC_Common::strto(inValue); - long long min = s < in ? s : in; - it->second = TC_Common::tostr(min); - } - else if (sPolicy == "Max") - { - long long s = TC_Common::strto(it->second); - long long in = TC_Common::strto(inValue); - long long max = s > in ? s : in; - it->second = TC_Common::tostr(max); - } - else if (sPolicy == "Distr") - { - vector fields; - vector fieldIn; - fields = TC_Common::sepstr(it->second, ","); - fieldIn= TC_Common::sepstr(inValue, ","); - string tmpValue = ""; - for (size_t k=0; k sTmp = TC_Common::sepstr(fields[k], "|"); - vector inTmp = TC_Common::sepstr(fieldIn[k], "|"); - long long tmp = TC_Common::strto(sTmp[1]) + TC_Common::strto(inTmp[1]); - sTmp[1] = TC_Common::tostr(tmp); - fields[k] = sTmp[0]+ "|" +sTmp[1]; - - if (k==0) - { - tmpValue = fields[k]; - } - else - { - tmpValue = tmpValue + "," + fields[k]; - } - } - it->second = tmpValue; - } - else if(sPolicy == "Avg") - { - //double avg = (TC_Common::strto(sValue) + TC_Common::strto(inValue))/2; - vector sTmp = TC_Common::sepstr(it->second,"="); - vector inTmp = TC_Common::sepstr(inValue,"="); - - //总值求和 - double tmpValueSum = TC_Common::strto(sTmp[0]) + TC_Common::strto(inTmp[0]); - //新版本平å‡å€¼å¸¦æœ‰è®°å½•æ•°,记录求和 - long tmpCntSum = (2 == inTmp.size()?(TC_Common::strto(inTmp[1])):1) + - (2 == sTmp.size()?(TC_Common::strto(sTmp[1])):1); - - it->second = TC_Common::tostr(tmpValueSum) + "=" + TC_Common::tostr(tmpCntSum); - } - } - else - { - mSumib.insert(make_pair(body.vInfo[i].policy, body.vInfo[i].value)); - } - } - - stBody.vInfo.clear(); - - map::iterator it = mSumib.begin(); - while(it != mSumib.end()) - { - StatPropInfo tempProp; - tempProp.policy = it->first; - tempProp.value = it->second; - - stBody.vInfo.push_back(tempProp); - - ++it; - } - - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - ostringstream os; - head.displaySimple(os); - os<< " "< osv; - stBody.writeTo(osv); - string stemp(osv.getBuffer(), osv.getLength()); - vector vtData; - ret = this->_t.set(sk, stemp,true,vtData); - } - } - return ret; - } -}; - - -extern PropertyHashMap g_hashmap; - - - - - - -#endif - - diff --git a/cpp/framework/PropertyServer/PropertyImp.cpp b/cpp/framework/PropertyServer/PropertyImp.cpp deleted file mode 100644 index fff1b72be..000000000 --- a/cpp/framework/PropertyServer/PropertyImp.cpp +++ /dev/null @@ -1,204 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PropertyImp.h" -#include "PropertyServer.h" - -/////////////////////////////////////////////////////////// -TC_ThreadMutex PropertyImpThreadData::_mutex; -pthread_key_t PropertyImpThreadData::_key = 0; -size_t PropertyImpThreadData::_no=0; - -/////////////////////////////////////////////////////////// -PropertyImpThreadData::PropertyImpThreadData() -: _threadIndex(0) -{ -} -void PropertyImpThreadData::destructor(void* p) -{ - PropertyImpThreadData * pSptd = (PropertyImpThreadData*)p; - if(pSptd) - { - delete pSptd; - pSptd = NULL; - } -} -PropertyImpThreadData * PropertyImpThreadData::getData() -{ - if(_key == 0) - { - TC_LockT lock(_mutex); - if(_key == 0) - { - int iRet = ::pthread_key_create(&_key, PropertyImpThreadData::destructor); - - if (iRet != 0) - { - TLOGERROR("PropertyImpThreadData pthread_key_create fail:"<< errno << ":" << strerror(errno) << endl); - return NULL; - } - } - } - - PropertyImpThreadData * pSptd = (PropertyImpThreadData*)pthread_getspecific(_key); - - if(!pSptd) - { - TC_LockT lock(_mutex); - - pSptd = new PropertyImpThreadData(); - pSptd->_threadIndex = _no; - ++_no; - - int iRet = pthread_setspecific(_key, (void *)pSptd); - - assert(iRet == 0); - } - return pSptd; -} -/////////////////////////////////////////////////////////// -void PropertyImp::initialize() -{ - -} - -int PropertyImp::reportPropMsg(const map& propMsg, tars::TarsCurrentPtr current ) -{ - TLOGINFO("PropertyImp::reportPropMsg size:" << propMsg.size() << endl); - - handlePropMsg(propMsg, current); - - return 0; -} - -int PropertyImp::handlePropMsg(const map &propMsg, tars::TarsCurrentPtr current) -{ - for ( map::const_iterator it = propMsg.begin(); it != propMsg.end(); it++ ) - { - - const StatPropMsgHead &head = it->first; - const StatPropMsgBody &body = it->second; - - PropHead tHead; - tHead.moduleName = head.moduleName; - tHead.propertyName = head.propertyName; - tHead.setName = head.setName; - tHead.setArea = head.setArea; - tHead.setID = head.setID; - tHead.ip = current->getIp(); - - size_t iIndex = 0; - PropertyImpThreadData * td = PropertyImpThreadData::getData(); - - if(td) - { - iIndex = td->_threadIndex; - } - - dump2file(); - - int iBufferIndex = g_app.getSelectBufferIndex(); - - if(_lastBufferIndex != iBufferIndex) - { - if(_lastBufferIndex != -1) - { - TLOGDEBUG("PropertyImp::handlePropMsg iIndex:" << iIndex << "|iBufferIndex:" << iBufferIndex << "|_lastBufferIndex:" << _lastBufferIndex << endl); - - map > >& mBuffer = g_app.getBuffer(); - map > >::iterator iter = mBuffer.find(_lastBufferIndex); - iter->second[iIndex].first = TNOWMS; - iter->second[iIndex].second = 1; - } - - _lastBufferIndex = iBufferIndex; - } - - string sKey = tHead.moduleName; - sKey += tHead.ip; - - int iHashKey = _hashf(sKey) % g_app.getBuffNum(); - - PropertyHashMap *pHashMap = g_app.getHashMapBuff(iBufferIndex, iHashKey); - - ////////////////////////////////////////////////////////////////////////////////////// - - float rate = (pHashMap->getMapHead()._iUsedChunk) * 1.0/pHashMap->allBlockChunkCount(); - - if(rate >0.9) - { - pHashMap->expand(pHashMap->getMapHead()._iMemSize * 2); - TLOGERROR("PropertyImp::handlePropMsg hashmap expand to " << pHashMap->getMapHead()._iMemSize << endl); - } - - int iRet = pHashMap->add(tHead, body); - if(iRet != TC_HashMap::RT_OK ) - { - TLOGERROR("PropertyImp::handlePropMsg add hashmap recourd iRet:" << iRet << endl); - } - - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - ostringstream os; - os.str(""); - head.displaySimple(os); - body.displaySimple(os); - TLOGINFO("ret|"<getNow(); - time_t tTimeInterv = g_app.getInserInterv() * 60;//second - - static time_t g_tLastDumpTime = 0; - - if(g_tLastDumpTime == 0) - { - g_app.getTimeInfo(g_tLastDumpTime,g_sDate,g_sFlag); - } - - if(tTimeNow - g_tLastDumpTime > tTimeInterv) - { - static TC_ThreadLock g_mutex; - TC_ThreadLock::Lock lock( g_mutex ); - if(tTimeNow - g_tLastDumpTime > tTimeInterv) - { - g_app.getTimeInfo(g_tLastDumpTime,g_sDate,g_sFlag); - - int iSelectBuffer = g_app.getSelectBufferIndex(); - iSelectBuffer = !iSelectBuffer; - - g_app.setSelectBufferIndex(iSelectBuffer); - - TLOGDEBUG("PropertyImp::dump2file select buffer:" << iSelectBuffer << "|TimeInterv:" << tTimeInterv << "|now:" << tTimeNow << "|last:" << g_tLastDumpTime << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|PropertyImp::dump2file select buffer:" << iSelectBuffer << "|TimeInterv:" << tTimeInterv << "|now:" << tTimeNow << "|last:" << g_tLastDumpTime << endl; - } - } -} - - - - - diff --git a/cpp/framework/PropertyServer/PropertyImp.h b/cpp/framework/PropertyServer/PropertyImp.h deleted file mode 100644 index d6a4b1722..000000000 --- a/cpp/framework/PropertyServer/PropertyImp.h +++ /dev/null @@ -1,111 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROPERTY_IMP_H_ -#define __PROPERTY_IMP_H_ - -#include -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "util/tc_hash_fun.h" -#include "jmem/jmem_hashmap.h" -#include "servant/PropertyF.h" -#include "PropertyHashMap.h" - -using namespace tars; - -class PropertyImpThreadData : public TC_ThreadPool::ThreadData -{ -public: - static TC_ThreadMutex _mutex; //å…¨å±€äº’æ–¥é” - static pthread_key_t _key; //线程ç§æœ‰æ•°æ®key - static size_t _no; - - /** - * 构造函数 - */ - PropertyImpThreadData(); - - /** - * æ•°æ®èµ„æºé‡Šæ”¾ - * @param p - */ - static void destructor(void* p); - - /** - * 获å–线程数æ®ï¼Œæ²¡æœ‰çš„è¯ä¼šè‡ªåŠ¨åˆ›å»º - * @return ServantProxyThreadData* - */ - static PropertyImpThreadData * getData(); - -public: - size_t _threadIndex; -}; - -class PropertyImp : public PropertyF,public TC_ThreadLock -{ -public: - - using hash_functor = std::function; - /** - * - */ - PropertyImp() - : _lastBufferIndex(-1) - , _hashf(tars::hash()) - { - } - - /** - * æžå¤Ÿå‡½æ•° - */ - ~PropertyImp() { } - - /** - * åˆå§‹åŒ– - * - * @return int - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy() { } - - /** - * ä¸ŠæŠ¥æ€§å±žä¿¡æ¯ - * @param statmsg, ä¸ŠæŠ¥ä¿¡æ¯ - * @return int, 返回0表示æˆåŠŸ - */ - virtual int reportPropMsg(const map& propMsg, tars::TarsCurrentPtr current ); - -private: - int handlePropMsg(const map &propMsg, tars::TarsCurrentPtr current); - - void dump2file(); - -private: - - int _lastBufferIndex; - hash_functor _hashf; - -}; - -#endif - - diff --git a/cpp/framework/PropertyServer/PropertyReapThread.cpp b/cpp/framework/PropertyServer/PropertyReapThread.cpp deleted file mode 100644 index ee2a0b612..000000000 --- a/cpp/framework/PropertyServer/PropertyReapThread.cpp +++ /dev/null @@ -1,454 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PropertyReapThread.h" -#include "util/tc_config.h" -#include "PropertyServer.h" -#include "PropertyDbManager.h" - -//////////////////////////////////////////////////////////////// -ReapSSDProcThread::ReapSSDProcThread(PropertyReapThread * proc) -: _bTerminate(false) -, _proc(proc) -{ -} - -ReapSSDProcThread::~ReapSSDProcThread() -{ - if(isAlive()) - { - terminate(); - getThreadControl().join(); - } - _queue.clear(); -} - -void ReapSSDProcThread::terminate() -{ - _bTerminate = true; - - _queue.notifyT(); -} - -void ReapSSDProcThread::put(QueueItem data) -{ - if(!_bTerminate) - { - _queue.push_back(data); - } -} - -bool ReapSSDProcThread::pop(QueueItem & data) -{ - return _queue.pop_front(data, 1000); -} - -int ReapSSDProcThread::sendAlarmSMS(const string &sMsg) -{ - string errInfo = " ERROR:" + ServerConfig::LocalIp + "_" + sMsg; - TARS_NOTIFY_ERROR(errInfo); - - return 0; -} - -void ReapSSDProcThread::run() -{ - string sDate1(""); - string sFlag1(""); - string sDate2(""); - string sFlag2(""); - - while (!_bTerminate) - { - try - { - sDate1 = ""; - sFlag1 = ""; - sDate2 = ""; - sFlag2 = ""; - - QueueItem item; - - if(pop(item)) - { - if(item._statmsg != NULL) - { - int64_t iBegin = TNOWMS; - int64_t iEnd = 0; - - PropertyDbManager::getInstance()->insert2MultiDbs(item._index, *item._statmsg, item._date, item._tflag); - - iEnd = TNOWMS; - - TLOGDEBUG("ReapSSDProcThread::run stat ip:" << ServerConfig::LocalIp << "|dbIndex:" << item._index << "|" << PropertyDbManager::getInstance()->getIpAndPort(item._index) - << "|date:" << item._date << "|tflag:" << item._tflag << "|timecost(ms):" << (iEnd - iBegin) << "|iBegin:" << iBegin << "|iEnd:" << iEnd << endl); - - FDLOG("PropertyPool") << "ReapSSDProcThread::run stat ip:" << ServerConfig::LocalIp << "|dbIndex:" << item._index << "|" << PropertyDbManager::getInstance()->getIpAndPort(item._index) - << "|date:" << item._date << "|tflag:" << item._tflag << "|timecost(ms):" << (iEnd - iBegin) << "|iBegin:" << iBegin << "|iEnd:" << iEnd << endl; - - if((iEnd - iBegin)/1000 > g_app.getInserInterv() * 40) - { - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|ReapSSDProcThread::run timeout 8 minute." << endl; - string sMsg("stat ip:"); - sMsg += ServerConfig::LocalIp; - sMsg += " ReapSSDProcThread::run write db:"; - sMsg += PropertyDbManager::getInstance()->getIpAndPort(item._index); - sMsg += "|timeout "; - sMsg += TC_Common::tostr(g_app.getInserInterv() - 2); - sMsg += " Minute."; - - sendAlarmSMS(sMsg); - } - - delete item._statmsg; - item._statmsg = NULL; - } - else - { - TLOGERROR("ReapSSDProcThread::run item._statmsg == NULL." << endl); - } - } - } - catch(exception& e) - { - TLOGERROR("ReapSSDProcThread::run exception:" << e.what() << endl); - FDLOG("PropertyPool") << "ReapSSDProcThread::run exception:" << e.what() << endl; - } - } -} -////////////////////////////////////////////////////////////// -PropertyReapThread::PropertyReapThread() -: _terminate(false) -, _curWeight(0) -, _lastSq(-1) -{ - TLOGDEBUG("PropertyReapThread begin ok." << endl); -} - -PropertyReapThread::~PropertyReapThread() -{ - if (isAlive()) - { - terminate(); - - getThreadControl().join(); - } -} - -void PropertyReapThread::terminate() -{ - TLOGDEBUG("PropertyReapThread terminate." << endl); - - _terminate = true; - - TC_ThreadLock::Lock lock(*this); - - notifyAll(); -} - -void PropertyReapThread::run() -{ - int iInsertDataNum = PropertyDbManager::getInstance()->getInsertDbThreadNum(); - - for(int i = 0; i < iInsertDataNum; ++i) - { - ReapSSDProcThread *r = new ReapSSDProcThread(this); - - r->start(); - - _runners.push_back(r); - } - - string sDate,sTime; - - int dbNumber = PropertyDbManager::getInstance()->getDbNumber(); - - string sRandOrder; - - uint64_t iTotalNum = 0; - - int iLastIndex = -1; - - TLOGDEBUG("propertypool ip:" << ServerConfig::LocalIp << "|PropertyReapThread::run iInsertDataThread:" << iInsertDataNum << "|dbNumber:" << dbNumber << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|PropertyReapThread::run iInsertDataThread:" << iInsertDataNum << "|dbNumber:" << dbNumber << endl; - - while (!_terminate) - { - try - { - //åŒbuffer中一个buffer入库 - int iBufferIndex = !(g_app.getSelectBufferIndex()); - int64_t iInterval = 1000; - if(iBufferIndex != iLastIndex && g_app.getSelectBuffer(iBufferIndex, iInterval)) - { - iLastIndex = iBufferIndex; - - iTotalNum = 0; - - vector vAllPropertyMsg; - for(int iStatIndex = 0; iStatIndex < dbNumber; ++iStatIndex) - { - vAllPropertyMsg.push_back(new PropertyMsg()); - } - - int64_t tBegin = TNOWMS; - - getDataFromBuffer(iBufferIndex, vAllPropertyMsg, iTotalNum); - - int64_t tEnd = TNOWMS; - - TLOGDEBUG("propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|PropertyReapThread::run getDataFromBuffer timecost(ms):" << (tEnd - tBegin) << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|PropertyReapThread::run getDataFromBuffer timecost(ms):" << (tEnd - tBegin) << endl; - - TLOGDEBUG("propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|PropertyReapThread::run insert begin _vAllStatMsg.size:" << vAllPropertyMsg.size() << "|record num:" << iTotalNum << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|PropertyReapThread::run insert begin _vAllStatMsg.size:" << vAllPropertyMsg.size() << "|record num:" << iTotalNum << endl; - - if(iTotalNum <= 0) - { - for(int iStatIndex = 0; iStatIndex < dbNumber; ++iStatIndex) - { - delete vAllPropertyMsg[iStatIndex]; - } - - vAllPropertyMsg.clear(); - } - else - { - - string sFile=""; - string sDate=""; - string sFlag=""; - time_t time=0; - g_app.getTimeInfo(time,sDate,sFlag); - - //size_t iSize = vAllStatMsg.size(); - - QueueItem item; - int iInsertThreadIndex = 0; - sRandOrder = g_app.getRandOrder(); - - if (sRandOrder == "") - { - sRandOrder = "0"; - } - - map >& mIpHasDbInfo = PropertyDbManager::getInstance()->getIpHasDbInfo(); - map >::iterator m_iter = mIpHasDbInfo.begin(); - - while(m_iter != mIpHasDbInfo.end()) - { - vector &vDb = m_iter->second; - - for(size_t i = 0; i < vDb.size(); ++i) - { - int k = (i + TC_Common::strto(sRandOrder)) % vDb.size(); - - item._index = vDb[k]; - item._date = sDate; - item._tflag = sFlag; - item._statmsg = vAllPropertyMsg[item._index]; - - iInsertThreadIndex = PropertyDbManager::getInstance()->getDbToIpIndex(vDb[k]); - - assert(iInsertThreadIndex >= 0); - - _runners[iInsertThreadIndex]->put(item); - } - - ++m_iter; - } - - if(_terminate) - { - break; - } - } - - for(int k = 0; k < g_app.getBuffNum(); ++k) - { - PropertyHashMap *pHashMap = g_app.getHashMapBuff(iBufferIndex, k); - pHashMap->clear(); - } - - TLOGDEBUG("propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|PropertyReapThread::run insert record num:" << iTotalNum << "|tast patch finished." << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|PropertyReapThread::run insert record num:" << iTotalNum << "|tast patch finished." << endl; - } - - } - catch(exception& ex) - { - TLOGERROR("PropertyReapThread::run exception:"<< ex.what() << endl); - } - catch(...) - { - TLOGERROR("PropertyReapThread::run ReapSSDThread unkonw exception catched" << endl); - } - - TC_ThreadLock::Lock lock(*this); - timedWait(REAP_INTERVAL); - } - - TLOGDEBUG("PropertyReapThread run setTerminateFlag true." << endl); - - PropertyDbManager::getInstance()->setTerminateFlag(true); - - for(size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->terminate(); - - _runners[i]->getThreadControl().join(); - } - } - - for(size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]) - { - delete _runners[i]; - _runners[i] = NULL; - } - } - - TLOGDEBUG("PropertyReapThread run terminate." << endl); -} - -int PropertyReapThread::getIndexWithWeighted(int iMaxDb,int iGcd,int iMaxW,const vector& vDbWeight) -{ - while (true){ - - _lastSq = (_lastSq + 1) % iMaxDb; - - if (_lastSq == 0) - { - _curWeight = _curWeight - iGcd; - if (_curWeight <= 0) - { - _curWeight = iMaxW; - if(_curWeight == 0) - { - return 0; - } - } - } - - if (vDbWeight[_lastSq] >= _curWeight) - { - return _lastSq; - } - } -} -void PropertyReapThread::getDataFromBuffer(int iIndex, vector &vAllPropertyMsg, uint64_t &iTotalNum) -{ - TLOGDEBUG("PropertyReapThread::getDataFromBuffer iIndex:" << iIndex << "|begin..." << endl); - - try - { - int iCount = 0,dbSeq=0; - - //获å–db个数 - int dbNumber = PropertyDbManager::getInstance()->getDbNumber(); - - vector vDbWeight; - int iGcd = 0,iMaxW = 0; - - PropertyDbManager::getInstance()->getDbWeighted(iGcd,iMaxW,vDbWeight); - - bool bEnable = PropertyDbManager::getInstance()->hasEnableWeighted(); - - for(int k = 0; k < g_app.getBuffNum(); ++k) - { - if(_terminate) - { - break; - } - - PropertyHashMap *pHashMap = g_app.getHashMapBuff(iIndex, k); - - if(pHashMap->size() == 0) - { - continue ; - } - - FDLOG("PropertyPool") << "property ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iIndex << "|PropertyReapThread::getData load hashmap k:" << k << endl; - - PropertyHashMap::lock_iterator it = pHashMap->beginSetTime(); - while ( it != pHashMap->end() ) - { - if(_terminate) - { - break; - } - - PropHead head; - PropBody body; - int ret = it->get( head, body ); - if ( ret < 0 ) - { - ++it; - continue; - } - - if (dbNumber > 0) - { - if(bEnable)//按æƒé‡å…¥åº“ - { - dbSeq = getIndexWithWeighted(dbNumber,iGcd,iMaxW,vDbWeight); - TLOGINFO("PropertyReapThread::getIndexWithWeighted |" << dbSeq << endl); - } - else - { - dbSeq = iCount % dbNumber; - } - - (*(vAllPropertyMsg[dbSeq]))[head] = body; - } - - iCount++; - - ++it; - } - - } - - iTotalNum = iCount; - - TLOGDEBUG("PropertyReapThread::getDataFromBuffer Buffer Index:" << iIndex << "|get total size:" << iCount << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iIndex << "|PropertyReapThread::getData get total size:" << iCount << "|end..." << endl; - } - catch (exception& ex) - { - TLOGERROR("PropertyReapThread::getDataFromBuffer exception:" << ex.what() << endl); - FDLOG("PropertyPool") << "propertypool ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iIndex << "|PropertyReapThread::getData exception:" << ex.what() << endl; - - string sMsg("PropertyReapThread::getDataFromBuffer Buffer Index:"); - sMsg += TC_Common::tostr(iIndex); - sMsg += " exception:"; - sMsg += ex.what(); - sendAlarmSMS(sMsg); - } -} - -int PropertyReapThread::sendAlarmSMS(const string &sMsg) -{ - string errInfo = " ERROR:" + ServerConfig::LocalIp + "_" + sMsg; - TARS_NOTIFY_ERROR(errInfo); - - return 0; -} - - diff --git a/cpp/framework/PropertyServer/PropertyReapThread.h b/cpp/framework/PropertyServer/PropertyReapThread.h deleted file mode 100644 index c6a75b5ce..000000000 --- a/cpp/framework/PropertyServer/PropertyReapThread.h +++ /dev/null @@ -1,144 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REAP_THREAD_H__ -#define __REAP_THREAD_H__ -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "servant/PropertyF.h" -#include "util/tc_common.h" -#include "servant/TarsLogger.h" -#include "PropertyImp.h" - - -using namespace tars; - -extern TC_Config* g_pconf; - -//////////////////////////////////////// -class QueueItem -{ -public: - size_t _index; - string _date; - string _tflag; - PropertyMsg *_statmsg; - - QueueItem() - : _index(0) - , _date("") - , _tflag("") - , _statmsg(NULL) - {} -}; -//////////////////////////////////////// -class PropertyReapThread; -//////////////////////////////////////// -/** - * å‘æ•°æ®åº“æ’入数æ®çš„线程类 - */ -class ReapSSDProcThread : public TC_Thread -{ -public: - enum - { - TIME_INTERVAL = 5000//更新业务线程时间 - }; - ReapSSDProcThread(PropertyReapThread * proc); - - ~ReapSSDProcThread(); - - void terminate(); - - virtual void run(); - - void put(QueueItem data); - - bool pop(QueueItem & data); - - int sendAlarmSMS(const string &sMsg); - -private: - bool _bTerminate; - - PropertyReapThread * _proc; - - TC_ThreadQueue _queue; -}; -////////////////////////////////////////////// -/** - * 用于执行定时æ“作的线程类 - */ -class PropertyReapThread : public TC_Thread, public TC_ThreadLock -{ -public: - /** - * å®šä¹‰å¸¸é‡ - */ - enum - { - REAP_INTERVAL = 3000, - }; - - /** - * 构造 - */ - PropertyReapThread(); - - /** - * æžå¤Ÿ - */ - ~PropertyReapThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * 轮询函数 - */ - virtual void run(); - -private: - /* - * 从buffer中å–æ•°æ® - */ - void getDataFromBuffer(int iIndex, vector &vAllStatSecMsg, uint64_t &iTotalNum); - - int sendAlarmSMS(const string &sMsg); - - /** - * 通过æƒé‡è½®è¯¢è°ƒåº¦ç®—法获å–è¦æ’入数æ®çš„db index - * @param iMaxDb db个数 - * @param iGcd 所有æƒé‡çš„最大公约数 - * @param iMaxW 最大æƒé‡å€¼ - * @param vDbWeight 所有dbçš„æƒé‡å€¼ - * - * @return int - */ - int getIndexWithWeighted(int iMaxDb,int iGcd,int iMaxW,const vector& vDbWeight); - -private: - bool _terminate; - int _curWeight; - int _lastSq; - vector _runners; -}; - -#endif diff --git a/cpp/framework/PropertyServer/PropertyServer.cpp b/cpp/framework/PropertyServer/PropertyServer.cpp deleted file mode 100644 index a0ef47796..000000000 --- a/cpp/framework/PropertyServer/PropertyServer.cpp +++ /dev/null @@ -1,307 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PropertyServer.h" -#include "PropertyImp.h" -#include "servant/AppCache.h" - -void PropertyServer::initialize() -{ - try - { - //关闭远程日志 - TarsTimeLogger::getInstance()->enableRemote("", false); - TarsTimeLogger::getInstance()->enableRemote("PropertyPool", true); - - //增加对象 - addServant( ServerConfig::Application + "." + ServerConfig::ServerName +".PropertyObj" ); - - _insertInterval = TC_Common::strto(g_pconf->get("/tars/hashmap","5")); - if(_insertInterval < 5) - { - _insertInterval = 5; - } - - //获å–业务线程个数 - string sAdapter = ServerConfig::Application + "." + ServerConfig::ServerName + ".PropertyObjAdapter"; - string sHandleNum = "/tars/application/server/"; - sHandleNum += sAdapter; - sHandleNum += ""; - - int iHandleNum = TC_Common::strto(g_pconf->get(sHandleNum, "50")); - vector > vec; - vec.resize(iHandleNum); - - initHashMap(); - - string s(""); - _selectBuffer = getSelectBufferFromFlag(s); - - - for(size_t i =0; i < vec.size(); ++i) - { - vec[i].first = 0; - vec[i].second = 0; - } - _buffer[_selectBuffer] = vec; - - - for(size_t i =0; i < vec.size(); ++i) - { - vec[i].first = 0; - vec[i].second = 1; - } - _buffer[!_selectBuffer] = vec; - - TLOGDEBUG("PropertyServer::initialize iHandleNum:" << iHandleNum<< endl); - FDLOG("PropertyPool") << "PropertyServer::initialize iHandleNum:" << iHandleNum << endl; - - - TLOGDEBUG("PropertyServer::initialize iSelectBuffer:" << _selectBuffer<< endl); - FDLOG("PropertyPool") << "PropertyServer::initialize iSelectBuffer:" << _selectBuffer << endl; - - - _randOrder = AppCache::getInstance()->get("RandOrder"); - TLOGDEBUG("PropertyServer::initialize randorder:" << _randOrder << endl); - - _reapThread = new PropertyReapThread(); - - _reapThread->start(); - - TARS_ADD_ADMIN_CMD_PREFIX("tars.tarsproperty.randorder", PropertyServer::cmdSetRandOrder); - } - catch ( exception& ex ) - { - TLOGERROR("PropertyServer::initialize catch exception:" << ex.what() << endl); - exit( 0 ); - } - catch ( ... ) - { - TLOGERROR("PropertyServer::initialize unknow exception catched" << endl); - exit( 0 ); - } -} -string PropertyServer::getRandOrder(void) -{ - return _randOrder; -} - -string PropertyServer::getClonePath(void) -{ - return _clonePath; -} - -int PropertyServer::getInserInterv(void) -{ - return _insertInterval; -} - -bool PropertyServer::getSelectBuffer(int iIndex, int64_t iInterval) -{ - int64_t iNow = TNOWMS; - bool bFlag = true; - vector > &vBuffer = _buffer[iIndex]; - - for(vector >::size_type i=0; i != vBuffer.size(); i++) - { - if(vBuffer[i].second != 1) - { - //TLOGDEBUG("getSelectBuffer return false|i:" << i << "|ret:" << vBuffer[i].second << endl); - return false; - } - } - - for(vector >::size_type i=0; i != vBuffer.size(); i++) - { - if((iNow - vBuffer[i].first) < iInterval) - { - bFlag = false; - } - } - - if(bFlag) - { - for(vector >::size_type i=0; i != vBuffer.size(); i++) - { - vBuffer[i].second = 0; - } - - TLOGDEBUG("PropertyServer::getSelectBuffer getSelectBuffer end return true" << endl); - return true; - } - - TLOGDEBUG("PropertyServer::getSelectBuffer getSelectBuffer end return false" << endl); - return false; -} - -int PropertyServer::getSelectBufferFromFlag(const string& sFlag) -{ - if(sFlag.length()!=0) - { - return (TC_Common::strto(sFlag.substr(2,2))/_insertInterval)%2; - } - else - { - time_t tTime=0; - string sDate="",flag=""; - getTimeInfo(tTime,sDate,flag); - return (TC_Common::strto(flag.substr(2,2))/_insertInterval)%2; - } -} - -bool PropertyServer::cmdSetRandOrder(const string& command, const string& params, string& result) -{ - try - { - TLOGINFO("PropertyServer::cmdSetRandOrder " << command << " " << params << endl); - - _randOrder = params; - - result = "set RandOrder [" + _randOrder + "] ok"; - - AppCache::getInstance()->set("RandOrder",_randOrder); - } - catch (exception &ex) - { - result = ex.what(); - } - return true; -} - -void PropertyServer::initHashMap() -{ - TLOGDEBUG("PropertyServer::initHashMap begin" << endl); - - int iHashMapNum = TC_Common::strto(g_pconf->get("/tars/hashmap","3")); - - TLOGDEBUG("PropertyServer::initHashMap iHashMapNum:" << iHashMapNum << endl); - - _buffNum = iHashMapNum; - - _hashmap = new PropertyHashMap *[2]; - - for(int k = 0; k < 2; ++k) - { - _hashmap[k] = new PropertyHashMap[iHashMapNum](); - } - - int iMinBlock = TC_Common::strto(g_pconf->get("/tars/hashmap","128")); - int iMaxBlock = TC_Common::strto(g_pconf->get("/tars/hashmap","256")); - float iFactor = TC_Common::strto(g_pconf->get("/tars/hashmap","2")); - int iSize = TC_Common::toSize(g_pconf->get("/tars/hashmap"), 1024*1024*256); - - - _clonePath = ServerConfig::DataPath + "/" + g_pconf->get("/tars/hashmap","clone"); - - if(!TC_File::makeDirRecursive(_clonePath)) - { - TLOGERROR("cannot create hashmap file " << _clonePath << endl); - exit(0); - } - - TLOGDEBUG("PropertyServer::initHashMap init multi hashmap begin..." << endl); - - for(int i = 0; i < 2; ++i) - { - for(int k = 0; k < iHashMapNum; ++k) - { - string sFileConf("/tars/hashmapget(sFileConf, sFileDefault); - - string sPath = TC_File::extractFilePath(sHashMapFile); - - if(!TC_File::makeDirRecursive(sPath)) - { - TLOGERROR("cannot create hashmap file " << sPath << endl); - exit(0); - } - - try - { - TLOGINFO("initDataBlockSize size: " << iMinBlock << ", " << iMaxBlock << ", " << iFactor << endl); - - _hashmap[i][k].initDataBlockSize(iMinBlock,iMaxBlock,iFactor); - - if(TC_File::isFileExist(sHashMapFile)) - { - iSize = TC_File::getFileSize(sHashMapFile); - } - _hashmap[i][k].initStore( sHashMapFile.c_str(), iSize ); - - TLOGINFO("\n" << _hashmap[i][k].desc() << endl); - } - catch(TC_HashMap_Exception &e) - { - TC_File::removeFile(sHashMapFile,false); - throw runtime_error(e.what()); - } - - } - } - - TLOGDEBUG("PropertyServer::initHashMap init multi hashmap end..." << endl); -} - -void PropertyServer::destroyApp() -{ - if(_reapThread) - { - delete _reapThread; - _reapThread = NULL; - } - - for(int i = 0; i < 2; ++i) - { - if(_hashmap[i]) - { - delete [] _hashmap[i]; - } - } - - if(_hashmap) - { - delete [] _hashmap; - } - - TLOGDEBUG("PropertyServer::destroyApp ok" << endl); -} - -void PropertyServer::getTimeInfo(time_t &tTime,string &sDate,string &sFlag) -{ - //3G统计è¦æ±‚ - //loadIntev å•ä½ä¸ºåˆ†é’Ÿ - string sTime,sHour,sMinute; - time_t t = TNOW; - t = (t/(_insertInterval*60))*_insertInterval*60; //è¦æ±‚必须为Intevæ•´æ•°å€ - tTime = t; - t = (t%3600 == 0?t-60:t); //è¦æ±‚å°†9点写作0860 - sTime = TC_Common::tm2str(t,"%Y%m%d%H%M"); - sDate = sTime.substr(0,8); - sHour = sTime.substr(8,2); - sMinute = sTime.substr(10,2); - sFlag = sHour + (sMinute=="59"?"60":sMinute); //è¦æ±‚å°†9点写作0860 -} - diff --git a/cpp/framework/PropertyServer/PropertyServer.h b/cpp/framework/PropertyServer/PropertyServer.h deleted file mode 100644 index 4cc50efec..000000000 --- a/cpp/framework/PropertyServer/PropertyServer.h +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROPERTY_SERVER_H_ -#define __PROPERTY_SERVER_H_ - -#include "servant/Application.h" -#include "PropertyReapThread.h" - -using namespace tars; - -class PropertyServer : public Application -{ - -public: - - PropertyServer() - : _reapThread(NULL) - , _hashmap(NULL) - {} - - void getTimeInfo(time_t &tTime,string &sDate,string &sFlag); - - bool cmdSetRandOrder(const string& command, const string& params, string& result); - - string getRandOrder(void); - - string getClonePath(void); - - int getInserInterv(void); - - map > >& getBuffer(){return _buffer;} - - bool getSelectBuffer(int iIndex, int64_t iInterval); - - int getSelectBufferFromFlag(const string& sFlag); - - int getSelectBufferIndex() { return _selectBuffer; } - - void setSelectBufferIndex(int iIndex) { _selectBuffer = iIndex; } - - PropertyHashMap * getHashMapBuff(int iIndex, int iBuffer) { return &(_hashmap[iIndex][iBuffer]); } - - int getBuffNum() { return _buffNum; } - -protected: - /** - * åˆå§‹åŒ–, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžå¤Ÿ, æ¯ä¸ªè¿›ç¨‹éƒ½ä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void destroyApp(); - -private: - - void initHashMap(); -private: - - PropertyReapThread* _reapThread; - - // éšæœºå…¥åº“开关 - string _randOrder; - - //æ•°æ®æ¢å­˜ç›®å½• - string _clonePath; - - //æ•°æ®åº“æ’入间隔,å•ä½åˆ†é’Ÿ - int _insertInterval; - - //åŒbuffer机制 - map > > _buffer; - - int _selectBuffer; - - PropertyHashMap **_hashmap; - - int _buffNum; - -}; - -extern PropertyServer g_app; -extern TC_Config* g_pconf; - -#endif - diff --git a/cpp/framework/PropertyServer/main.cpp b/cpp/framework/PropertyServer/main.cpp deleted file mode 100644 index fb185d3fd..000000000 --- a/cpp/framework/PropertyServer/main.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "PropertyServer.h" -#include - -using namespace std; - -TC_Config* g_pconf; - -PropertyServer g_app; - -/** - * @param argc - * @param argv - * - * @return int - */ -int main( int argc, char* argv[] ) -{ - try - { - g_pconf = &g_app.getConfig(); - g_app.main( argc, argv ); - LOG->debug() << "main init succ" << endl; - g_app.waitForShutdown(); - } - catch ( exception& ex ) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/QueryPropertyServer/CMakeLists.txt b/cpp/framework/QueryPropertyServer/CMakeLists.txt deleted file mode 100644 index d13a2b44a..000000000 --- a/cpp/framework/QueryPropertyServer/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set(MODULE "tarsqueryproperty") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/) - -complice_module(${MODULE}) - - - -#FILE(command 'rm -rf ${EXECUTABLE_OUTPUT_PATH}/tarsquerystat') diff --git a/cpp/framework/QueryPropertyServer/DbProxy.cpp b/cpp/framework/QueryPropertyServer/DbProxy.cpp deleted file mode 100644 index f42260cce..000000000 --- a/cpp/framework/QueryPropertyServer/DbProxy.cpp +++ /dev/null @@ -1,902 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbProxy.h" -#include - -/////////////////////////////////////////////////////////// -string tFlagInc(const string& stflag); - -string dateInc(const string& sDate); - -void selectLastMinTime(const string& sUid, int iThread , const string& tbname, const TC_DBConf& tcDbInfo, string & ret, QueryParam &queryParam); - -void query(int iThread, const TC_DBConf & conf, map& mSqlPart, map > &result, string &sRes, QueryParam &queryParam,string &sPolicy); - -DbProxy::DbProxy() -{ -} - -DbProxy::~DbProxy() -{ -} - -int DbProxy::createRespHead(const vector &res, const string& sLasttime, string& result, bool bDbCountFlag) -{ - int iRet = 0; - string sRes; - - //检查查询返回值,如果一个线程失败,就返回失败。 - for(size_t i=0; i< res.size(); i++) - { - sRes += res[i] ; - if ( res[i][4] != '0' && iRet == 0) - { - iRet = -1; - } - } - - //int total = bDbCountFlag ? g_app.getDbNumber() : g_app.getDbNumber(); - int total = g_app.getDbNumber(); - result = "Ret:"; - result += TC_Common::tostr(iRet); - result += "\nlasttime:"; - result += sLasttime; - result += "\nActiveDb:"; - result += TC_Common::tostr(res.size()); - result += "\nTotalDb:"; - result += TC_Common::tostr(total); - result += "\n"; - result += sRes; - - return iRet; -} -int DbProxy::createRespData(const string& sUid, const map& mSqlPart, const vector > >& vDataList, const string& sHead, string &result, string& sPolicy) -{ - // 组åˆå¤šçº¿ç¨‹ç»“æžœ - //map firstç”±goupbyç”Ÿæˆ - //map second ç”±indexç”Ÿæˆ - int64_t tStart = TNOWMS; - vector > >::const_iterator dataItr = vDataList.begin(); - map > mStatData; - map >::iterator _it; - - for(size_t i = 0; dataItr != vDataList.end(); dataItr++, i++) - { - TLOGDEBUG(sUid << "sum["<size() << endl); - for(map >::const_iterator it = dataItr->begin(); it != dataItr->end(); it++) - { - _it = mStatData.find(it->first); - if (_it != mStatData.end()) - { - const vector &number1 = it->second; - vector &number2 = _it->second; - // 相åŒkey的值 求和,number1å’Œnumber1的大å°æ˜¯ä¸€æ ·çš„ - for (size_t j=0; jfirst] = it->second; - } - - } - -// dataItr->clear(); - } - - string groupField(""); - - string sTemp(""); -// int iLineNum = 0; - - for(_it = mStatData.begin(); _it != mStatData.end(); _it++) - { - string sKey = _it->first; - vector &vValue = _it->second; - - if(sPolicy == "Avg") - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - iResult += vValue[i]; - } - - TLOGDEBUG(sUid << "iResult:" << iResult << "|size:" << vValue.size() << endl); - - if(vValue.size() == 0) - { - iResult = 0; - } - else - { - iResult = iResult / vValue.size(); - } - - TLOGDEBUG(sUid << "Policy Avg iResult:" << iResult << "|size:" << vValue.size() << endl); - - vValue.clear(); - vValue.push_back(iResult); - } - else if(sPolicy == "Max") - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - if(i == 0) - { - iResult = vValue[i]; - } - - if(iResult < vValue[i]) - { - iResult = vValue[i]; - } - } - - TLOGDEBUG(sUid << "Policy Max iResult:" << iResult << "|size:" << vValue.size() << endl); - - vValue.clear(); - vValue.push_back(iResult); - } - else if(sPolicy == "Min") - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - if(i == 0) - { - iResult = vValue[i]; - } - - if(iResult > vValue[i]) - { - iResult = vValue[i]; - } - } - - TLOGDEBUG(sUid << "Policy Min iResult:" << iResult << "|size:" << vValue.size() << endl); - - vValue.clear(); - vValue.push_back(iResult); - } - else - { - string::size_type position; - - if((position =sKey.find("Avg")) != string::npos) - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - iResult += vValue[i]; - } - - TLOGDEBUG(sUid << "Avg iResult:" << iResult << "|size:" << vValue.size() << endl); - - if(vValue.size() == 0) - { - iResult = 0; - } - else - { - iResult = iResult / vValue.size(); - } - - vValue.clear(); - vValue.push_back(iResult); - } - else if((position =sKey.find("Min")) != string::npos) - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - if(i == 0) - { - iResult = vValue[i]; - } - - if(iResult > vValue[i]) - { - iResult = vValue[i]; - } - } - - TLOGDEBUG(sUid << "Min iResult:" << iResult << "|size:" << vValue.size() << endl); - - vValue.clear(); - vValue.push_back(iResult); - } - else if((position =sKey.find("Max")) != string::npos) - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - if(i == 0) - { - iResult = vValue[i]; - } - - if(iResult < vValue[i]) - { - iResult = vValue[i]; - } - } - - TLOGDEBUG(sUid << "Max iResult:" << iResult << "|size:" << vValue.size() << endl); - - vValue.clear(); - vValue.push_back(iResult); - } - else - { - double iResult = 0; - for(size_t i = 0; i < vValue.size(); ++i) - { - iResult += vValue[i]; - } - - TLOGDEBUG(sUid << "Sum iResult:" << iResult << "|size:" << vValue.size() << endl); - - vValue.clear(); - vValue.push_back(iResult); - } - } - - } - - - - result += sHead + "linecount:" + TC_Common::tostr(mStatData.size()) + "\n"; - - //把 查询结果转æ¢æˆä¸€è¡Œä¸€è¡Œçš„串 - /* - * input :groupby, f_date, f_tflag - * input : index, succ_count, timeout_count - *all map > - *string =>> f_date, f_tflag - *vector =>> succ_count, timeout_count - */ - _it = mStatData.begin(); - while(_it != mStatData.end()) - { - string valueBuffer = ""; - vector::iterator valueIt = _it->second.begin(); - while(valueIt != _it->second.end()) // value is vector int, need transfer to string; - { - valueBuffer += TC_Common::tostr(*valueIt) + ","; - valueIt++; - } - - result += _it->first + ","; - result += valueBuffer + "\n"; - - _it++; - } - - TLOGDEBUG("result:"<getNowMs(); - - //int64_t tEnd = TNOWMS; - - TLOGDEBUG("DbProxy::createRespData "<< sUid << "createRespData size:"<< result.length() << "|timecost(ms):" << (tEnd-tStart) << endl); - return 0; -} - -/** - * 通过线程池进行并å‘查询 - */ -void DbProxy::queryData(map &mSqlPart, string &sResult, bool bDbCountFlag) -{ - - try - { - string sUid = mSqlPart.find("uid")->second; - - vector vActive; - - if(bDbCountFlag) - { - vActive = g_app.getActiveDbInfo(); - } - else - { - //vActive = g_app.getActiveDbInfo(); - } - - int iThreads = vActive.size(); - - //int iThreads = bDbCountFlag ? g_app.getDbNumber() : g_app.getDbNumber(); - if(iThreads > 0) - { - vector vPolicy(iThreads); - - vector res(iThreads); - - vector > > vDataList(iThreads); - - _queryParam._run_times = iThreads; - - //TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "all thread query data begin." << endl); - - int64_t tStart = TC_TimeProvider::getInstance()->getNowMs(); - - for(int i=0; i < iThreads; i++) - { - auto fwrapper = std::bind(&query, - i, - std::cref(vActive[i]), - std::ref(mSqlPart), - std::ref(vDataList[i]), - std::ref(res[i]), - std::ref(_queryParam), - std::ref(vPolicy[i])); - - if(bDbCountFlag) - { - g_app.getThreadPoolDb().exec(fwrapper); - } - else - { - //g_app.getThreadPoolDb().exec(fwrapper); - } - } - - //ç­‰å¾…çº¿ç¨‹ç»“æŸ - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "wait for all thread query data done." << endl); - - bool rc = true; - int ifail = 0; - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(6000); - } - - ++ifail; - - if(!rc) - { - if(ifail >= 10) - { - break; - } - } - } - - if(ifail >= 10) - { - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "wait for all thread query data timeout." << endl); - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - _queryParam._monitor.timedWait(1000); - } - } - } - - if(_queryParam._atomic.get() == _queryParam._run_times) - rc = true; - /*bool rc = false; - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(60000); - }*/ - - int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs(); - - if(rc) - { - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; - - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "all thread done return:" << _queryParam._run_result <<"|timecost(ms):" << (tEnd - tStart) << endl); - - // 返回ret code - string sHead; - - string sLasttime = getLastTime(mSqlPart); - - if(createRespHead(res, sLasttime, sHead, bDbCountFlag) != 0) - { - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; - - sResult = sHead; - TLOGERROR("DbProxy::queryData query error:" << sHead << endl); - return; - } - - createRespData(sUid, mSqlPart, vDataList, sHead, sResult,vPolicy[vPolicy.size()-1]); - } - else - { - sResult ="Ret:-1\nquery timeout\n"; - - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "Ret:-1|query timeout." << endl); - } - } - else - { - sResult ="Ret:-1\nno active db\n"; - } - } - catch (exception &ex) - { - TLOGERROR("DbProxy::queryData exception:" << ex.what() << endl); - sResult ="Ret:-1\n" + string(ex.what()) + "\n"; - } - - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; -} - -void query(int iThread, const TC_DBConf & conf, map& mSqlPart, map > &result, string &sRes, QueryParam &queryParam,string &sPolicy) -{ - string sUid = mSqlPart.find("uid")->second; - - TLOGDEBUG("queryData " << sUid << "thread iIndex:" << iThread << endl); - - int64_t tStart = TNOWMS; - try - { - //dateFrom =>> 20111120 - string dateFrom = mSqlPart["date1"]; - string dateTo = mSqlPart["date2"]; - - //tflagFrom =>> 2360 - string tflagFrom = mSqlPart["tflag1"]; - string tflagTo = mSqlPart["tflag2"]; - - // 输入tflag æ¡ä»¶æ£€æŸ¥ - if (dateFrom.length() != 8 || dateTo.length() != 8 || tflagFrom.length() != 4 || - tflagTo.length() != 4 || - TC_Common::isdigit(tflagFrom) == false || - TC_Common::isdigit(tflagTo) == false) - { - sRes += "ret:-1|iDb:" + TC_Common::tostr(iThread) + "|wrong tflag:" + tflagFrom + "-" + tflagTo + "\n"; - - TLOGERROR("query sUid:" << sUid << sRes << endl); - - queryParam._run_result = -1; - queryParam._atomic.inc(); - - if(queryParam._atomic.get() == queryParam._run_times) - { - TC_ThreadLock::Lock lock(queryParam._monitor); - queryParam._monitor.notifyAll(); - } - - return ; - } - - //groupCond =>> "where slave_name like 'MTTsh2.BrokerServer' and f_tflag >='0000' and f_tflag <='2360' and f_date = '20111120'" - string whereCond = mSqlPart["whereCond"]; - - string::size_type position; - if((position =whereCond.find("policy")) != string::npos) - { - string temp = whereCond.substr(position); - if((position =whereCond.find("Avg")) != string::npos) - { - sPolicy = "Avg"; - } - else if((position =whereCond.find("Max")) != string::npos) - { - sPolicy = "Max"; - } - else if((position =whereCond.find("Min")) != string::npos) - { - sPolicy = "Min"; - } - else - { - sPolicy = "NULL"; - } - } - - //groupCond =>> "group by f_date, f_tflag" - string groupCond = mSqlPart["groupCond"]; - - //sumField =>> "succ_count, timeout_count"; - string sumField = mSqlPart["sumField"]; - - //groupField =>> "f_date, f_tflag" - string groupField = mSqlPart["groupField"]; - - //selectCond =>> "succ_count, timeout_count, f_date, f_tflag" - string selectCond = sumField +"," + groupField; - - //日期格å¼20111019 - string::size_type pos = string::npos; - string tmpDate = "f_date"; - if ((pos = selectCond.find(tmpDate, 0)) != string::npos) - { - selectCond.replace(pos, tmpDate.length(), "DATE_FORMAT( f_date, '%Y%m%d') as f_date"); - } - - string sDbName = mSqlPart["dataid"]; - string ignoreKey(""); - - - - vector vGroupField = TC_Common::sepstr(groupField, ", "); - vector vSumField = TC_Common::sepstr(sumField, ", "); - - TC_Mysql tcMysql; - - TC_DBConf tcDbConf = conf; - /*if(bFlag) - { - tcDbConf = g_app.getDbInfo(iThread); - } - else - { - tcDbConf = g_app.getDbInfo(iThread); - }*/ - - tcDbConf._database = sDbName; - - tcMysql.init(tcDbConf); - - string sTbNamePre = tcDbConf._database + "_"; - - string sTbName(""); - string sSql(""); - //select range by f_date and f_tflag - for(string day = dateFrom; day <= dateTo; day = dateInc(day)) - { - for(string tflag = tflagFrom; tflag <= tflagTo && (tflag.substr(0,2) < "24"); tflag = tFlagInc(tflag)) - { - //table name:tars_2012060723 - sTbName = sTbNamePre + day + tflag.substr(0,2); - - sSql = "select " + selectCond + " from " + sTbName + " " + ignoreKey + whereCond + " order by null;"; - - tars::TC_Mysql::MysqlData res = tcMysql.queryRecord(sSql); - - TLOGINFO(sUid << "res.size:" << res.size() << "|sSql:" << sSql << endl); - - // result is key:value pair; - //sKey ç”±groupbyç”Ÿæˆ - //valueç”±indexç”Ÿæˆ - //int64_t t2Start = TNOWMS; - for(size_t iRow = 0; iRow < res.size(); iRow++) - { - string sKey = ""; - for(size_t j = 0; j < vGroupField.size(); j++) - { - sKey += sKey.empty()?"":","; - sKey += res[iRow][vGroupField[j]]; - } - - map >::iterator itResult = result.find(sKey); - if (itResult != result.end()) - { - vector& data = itResult->second; - for (size_t j = 0; j < vSumField.size() && j < data.size(); j++) - { - data.push_back(TC_Common::strto(res[iRow][vSumField[j]]));// 相åŒkey的值 求和 - } - } - else - { - vector& vRes = result[sKey]; - for(size_t j = 0; j < vSumField.size(); j++) - { - vRes.push_back( TC_Common::strto(res[iRow][vSumField[j]]));; - } - } - TLOGINFO("query iDb:" << iThread <<" {"<< sKey << ":" << TC_Common::tostr(result[sKey]) << "}" << endl); - } - - TLOGINFO("query iDb :" << iThread << " day:" << day <<" tflag:" << tflag << endl); - } - } //day - - sRes = "ret:0 iDb:" + TC_Common::tostr(iThread) + "\n"; - - //queryParam._atomic.inc(); - } - catch(TC_Mysql_Exception & ex) - { - sRes = "ret:-1|iDb:" + TC_Common::tostr(iThread) + string("|exception:") + ex.what() + "\n"; - TLOGERROR("query sUid:" << sUid << "query:" << sRes << endl); - - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - catch(exception & ex) - { - sRes = "ret:-1|iDb:" + TC_Common::tostr(iThread) + string("|exception:") + ex.what() + "\n"; - TLOGERROR("query sUid:" << sUid << "query:" << sRes << endl); - - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - int64_t tEnd = TNOWMS; - - TLOGDEBUG("query sUid:" << sUid << "exit query iDb:" << iThread <<"|timecost(ms):" << (tEnd - tStart) << "|res:" << sRes << endl); - - queryParam._atomic.inc(); - - if(queryParam._atomic.get() == queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(queryParam._monitor); - queryParam._monitor.notifyAll(); - } - - TLOGDEBUG("query sUid:" << sUid << "notify query finish." << endl); - } - - -} - -/////////////////////////////////////////////////////////////////////////////// -string tFlagInc(const string& stflag) -{ - int h = TC_Common::strto(stflag.substr(0,2)); - int m = TC_Common::strto(stflag.substr(2,2)); - - h += 1; - char buf[5]; - snprintf(buf,sizeof(buf),"%.2d%.2d",h,m); - - return string(buf); -} -/////////////////////////////////////////////////////////////////////////////// -string dateInc(const string& sDate) -{ - string ret("20991231"); // 返回大数 - - try - { - int year = TC_Common::strto(sDate.substr(0, 4)); - int mon = TC_Common::strto(sDate.substr(4, 2)); - int day = TC_Common::strto(sDate.substr(6, 2)); - - struct tm *p = NULL; - time_t timep; - struct tm tt = {0}; - - time(&timep); - p=localtime_r(&timep, &tt); - p->tm_mon = mon -1; - p->tm_mday = day +1; - p->tm_year = year -1900 ; - - timep = mktime(p); - ret = TC_Common::tm2str(timep, "%Y%m%d"); - } - catch(exception & ex) - { - TLOGERROR("DbProxy::dateInc exception:" << ex.what() << endl); - } - return ret; -} -/////////////////////////////////////////////////////////////////////////////// -void selectLastMinTime(const string& sUid, int iThread , const string& tbname, const TC_DBConf& tcDbInfo, string & ret, QueryParam &queryParam) -{ - string sId = sUid; - try - { - TC_Mysql tcMysql; - - //TC_DBConf tcDbConf = tcDbInfo; - - //tcDbConf._database = TC_Common::trimright(tbname, "_"); - //tcDbConf._database = tbname; - string sTbNamePre = tbname + ".t_ecstatus"; - - //TLOGDEBUG("selectLastMinTime database name:" << tcDbConf._database << "|tbname:" << tbname << endl); - - tcMysql.init(tcDbInfo); - - int interval = g_app.getInsertInterval(); - time_t now = TC_TimeProvider::getInstance()->getNow(); - - string sDate,sFlag; - // 排除历å²è¿‡æœŸæ•°æ® - string sTime,sHour,sMinute; - time_t t = (now - interval * 60 * 2); - interval = g_app.getInsertInterval(); - t = (t/(interval*60))*interval*60; //è¦æ±‚必须为loadIntevæ•´æ•°å€ - t = (t%3600 == 0?t-60:t); //è¦æ±‚å°†9点写作0860 - sTime = TC_Common::tm2str(t,"%Y%m%d%H%M"); - sDate = sTime.substr(0,8); - sHour = sTime.substr(8,2); - sMinute = sTime.substr(10,2); - sFlag = sHour + (sMinute=="59"?"60":sMinute); //è¦æ±‚å°†9点写作0860 - - string sLast = sDate + " " + sFlag; - - string sSql = "select min(lasttime) as lasttime from "+ sTbNamePre+" where appname like '" +"%' and lasttime > '" + sLast + "'" ; - - tars::TC_Mysql::MysqlData res = tcMysql.queryRecord(sSql); - - if (res.size() > 0) - { - TLOGINFO("selectLastTime" << sId << "sSql:" << sSql << "|lasttime:" << res[0]["lasttime"] << endl); - - ret = res[0]["lasttime"]; - } - else - { - ret = ""; - } - //queryParam._atomic.inc(); - } - catch(TC_Mysql_Exception & ex) - { - TLOGERROR("selectLastTime sUid="<< sId <<"exception:"<< ex.what() << endl); - ret = ""; - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - catch(exception& e) - { - TLOGERROR("selectLastTime sUid="<< sId <<"exception:"<< e.what() << endl); - ret = ""; - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - - queryParam._atomic.inc(); - if(queryParam._atomic.get() == queryParam._run_times) - { - TC_ThreadLock::Lock lock(queryParam._monitor); - queryParam._monitor.notifyAll(); - TLOGDEBUG("query sUid:" << sId << "notify checktime finish." << endl); - } -} -/////////////////////////////////////////////////////////////////////////////// -string DbProxy::getLastTime(const map& mSqlPart) -{ - string sUid = mSqlPart.find("uid")->second; - - string min = "99999999999"; // 求最å°çš„,åˆå§‹ä½¿ç”¨å¾ˆå¤§çš„æ•°æ® - //TLOGDEBUG("mSqlPart"<< mSqlPart.find("dataid")->second < vDbInfo = g_app.getAllActiveDbInfo(); - - int iThreads = vDbInfo.size(); - - if(iThreads > 0) - { - vector res(iThreads); - - _queryParam._run_times = iThreads; - - int64_t tStart = TC_TimeProvider::getInstance()->getNowMs(); - - for (int i=0; i< iThreads; i++) - { - const string tbname = mSqlPart.find("dataid")->second; - auto fwrapper = std::bind(&selectLastMinTime, - std::cref(sUid), - i, - std::cref(tbname), - std::cref(vDbInfo[i]), - std::ref(res[i]), - std::ref(_queryParam)); - - g_app.getThreadPoolTimeCheck().exec(fwrapper); - } - - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "wait for getLastTime done." << endl); - - bool rc = true; - int ifail = 0; - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(300); - } - - ++ifail; - - if(!rc) - { - if(ifail >= 10) - { - break; - } - } - } - - if(ifail >= 10) - { - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "wait for getLastTime timeout." << endl); - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - _queryParam._monitor.timedWait(1000); - } - } - } - - if(_queryParam._atomic.get() == _queryParam._run_times) - rc = true; - /*bool rc = false; - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(3000); - }*/ - - int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs(); - - - if(rc) - { - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "getLastTime all done|return:" << _queryParam._run_result <<"|timecost(ms):" << (tEnd-tStart) << endl); - - for(int i = 0; i < iThreads; ++i) - { - if(res[i] < min) - { - min = res[i]; - } - } - } - else - { - min = ""; - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "getLastTime timeout." << endl); - } - } - else - { - min = ""; - } - - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "final lasttime:" << min << endl); - } - catch (exception &ex) - { - TLOGERROR("DbProxy::getLastTime exception:" << ex.what() << endl); - min = ""; - } - - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; - - return min; -} - - -string DbProxy::makeResult(int iRet, const string& sRes) -{ - size_t act = g_app.getActiveDbInfo().size(); - int total = g_app.getDbInfo().size(); - string result = "Ret:" + TC_Common::tostr(iRet) + "\n" - + "ActiveDb:" + TC_Common::tostr(act) + "\n" - + "TotalDb:" + TC_Common::tostr(total) + "\n" - + sRes; - return result; -} diff --git a/cpp/framework/QueryPropertyServer/DbProxy.h b/cpp/framework/QueryPropertyServer/DbProxy.h deleted file mode 100644 index 3fa215a12..000000000 --- a/cpp/framework/QueryPropertyServer/DbProxy.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_PROXY_H_ -#define __DB_PROXY_H_ - -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "servant/TarsLogger.h" -#include "QueryServer.h" - -using namespace tars; - -class DbProxy -{ -public: - - DbProxy(); - - ~DbProxy(); - - void queryData(map& mSqlPart, string &sResult, bool bDbCountFlag); - - string getLastTime(const map& mSqlPart); - -private: - - int createRespHead(const vector &res, const string& sLasttime ,string& result, bool bDbCountFlag); - - int createRespData(const string& sUid, const map& mSqlPart, const vector > >& vDataList, const string& sHead, string &result, string& sPolicy); - - string makeResult(int iRet, const string& sRes); - -private: - QueryParam _queryParam; -}; - - -#endif - - diff --git a/cpp/framework/QueryPropertyServer/DbThread.cpp b/cpp/framework/QueryPropertyServer/DbThread.cpp deleted file mode 100644 index e8cd89586..000000000 --- a/cpp/framework/QueryPropertyServer/DbThread.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbThread.h" -#include "util/tc_config.h" -#include "QueryServer.h" - - -DBThread::DBThread() -: _bTerminate(false) -{ - TLOGDEBUG("DBThread init ok" << endl); -} - -DBThread::~DBThread() -{ - if (isAlive()) - { - terminate(); - - getThreadControl().join(); - } - TLOGDEBUG("DBThread terminate." << endl); -} - -void DBThread::terminate() -{ - _bTerminate = true; - - TC_ThreadLock::Lock lock(*this); - - notifyAll(); -} - -void DBThread::run() -{ - TLOGDEBUG("DBThread::run begin." << endl); - while (!_bTerminate) - { - try - { - tryConnect(); - } - catch ( exception& ex ) - { - TLOGERROR("DBThread::run exception:" << ex.what() << endl); - - } - catch (... ) - { - TLOGERROR("DBThread::run unkonw exception catched" << endl); - } - TC_ThreadLock::Lock lock(*this); - timedWait(REAP_INTERVAL); - } -} - -int DBThread::connect(MYSQL** pstMql,const TC_DBConf& tcConf) -{ - - *pstMql = mysql_init(NULL); - if (mysql_real_connect(*pstMql,tcConf._host.c_str(), tcConf._user.c_str(), tcConf._password.c_str(), tcConf._database.c_str(), tcConf._port, NULL, tcConf._flag) == NULL) - { - TLOGERROR("[TC_Mysql::connect]: mysql_real_connect: " + string(mysql_error(*pstMql)) + ", erron:" < vDbCountInfo = g_app.getDbInfo(); - - size_t iDbCountInfoSize = vDbCountInfo.size(); - - // vector vAtivedbInfo; - - vector vAtivedbCountInfo; - - - - for(size_t i = 0; i < vDbCountInfo.size(); i++) - { - TC_DBConf tcDBConf = vDbCountInfo[i]; - - //tcDBConf._database = "tars_stat"; - - MYSQL* _pstMql = NULL; - - int iRet = connect(&_pstMql,tcDBConf); - - if (_pstMql != NULL) - { - mysql_close(_pstMql); - _pstMql = NULL; - } - - if (iRet != 0) - { - TLOGERROR("DBThread::tryConnect new db host:" << tcDBConf._host << "|port:" << TC_Common::tostr(tcDBConf._port) << "|database:" << tcDBConf._database << endl); - string s("host"); - - s += tcDBConf._host; - s += "|port"; - s += TC_Common::tostr(tcDBConf._port); - s += "|database:"; - s += tcDBConf._database; - - sendAlarmSMS(s); - continue; - } - - vAtivedbCountInfo.push_back(vDbCountInfo[i]);; - } - - size_t iActiveDbCountInfoSize = vAtivedbCountInfo.size(); - - if(iDbCountInfoSize != iActiveDbCountInfoSize) - { - string sMsg = "statcount old db size:"; - sMsg += TC_Common::tostr(iDbCountInfoSize); - sMsg += "|statcount active db size:"; - sMsg += TC_Common::tostr(iActiveDbCountInfoSize); - sMsg += "|no equal."; - - sendAlarmSMS(sMsg); - } - - g_app.setActiveDb(vAtivedbCountInfo); - } - catch (exception& ex) - { - TLOGERROR("DBThread::tryConnect exception:"<< ex.what() << endl); - } -} - - - diff --git a/cpp/framework/QueryPropertyServer/DbThread.h b/cpp/framework/QueryPropertyServer/DbThread.h deleted file mode 100644 index 2fcf4e209..000000000 --- a/cpp/framework/QueryPropertyServer/DbThread.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_THREAD_H_ -#define __DB_THREAD_H_ - -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "util/tc_common.h" -#include "servant/TarsLogger.h" - - -using namespace tars; - -/** - * 用于执行定时æ“作的线程类 - */ -class DBThread : public TC_Thread, public TC_ThreadLock -{ -public: - /** - * å®šä¹‰å¸¸é‡ - */ - enum - { - REAP_INTERVAL = 30000 - }; - /** - * 构造 - */ - DBThread(); - - /** - * æžå¤Ÿ - */ - ~DBThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * 轮询函数 - */ - virtual void run(); - - void tryConnect(); - - int connect(MYSQL **_pstMql,const TC_DBConf& tcConf); - - void sendAlarmSMS(const string &sMsg); - -private: - bool _bTerminate; - -}; - -#endif diff --git a/cpp/framework/QueryPropertyServer/QueryDbThread.cpp b/cpp/framework/QueryPropertyServer/QueryDbThread.cpp deleted file mode 100644 index 6a4a72c57..000000000 --- a/cpp/framework/QueryPropertyServer/QueryDbThread.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryDbThread.h" -#include "QueryServer.h" -#include "DbProxy.h" - -///////////////////////////////////////////////////////////////////////// -QueryDbThread::QueryDbThread() -: _terminate(false) -{ - TLOGDEBUG("QueryDbThread init ok" << endl); -} - -QueryDbThread::~QueryDbThread() -{ - terminate(); - - TLOGDEBUG("QueryDbThread terminate." << endl); -} - -void QueryDbThread::start(int iThreadNum) -{ - for(int i = 0; i < iThreadNum; ++i) - { - HandleThreadRunner *r = new HandleThreadRunner(this); - - r->start(); - - _runners.push_back(r); - } -} - -void QueryDbThread::terminate() -{ - _terminate = true; - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if (_runners[i]->isAlive()) - { - _runners[i]->terminate(); - - _queue.notifyT(); - } - } - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->getThreadControl().join(); - } - } - - _queue.clear(); - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]) - { - delete _runners[i]; - _runners[i] = NULL; - } - } -} - -bool QueryDbThread::pop(QueryItem* &pItem) -{ - return _queue.pop_front(pItem, 2000); -} - -void QueryDbThread::put(QueryItem* pItem) -{ - if(!_terminate && pItem) - { - _queue.push_back(pItem); - } -} - -///////////////////////////////////////////////////////////////////////// -HandleThreadRunner::HandleThreadRunner(QueryDbThread* proc) -: _terminate(false) -, _proc(proc) -{ -} - -void HandleThreadRunner::terminate() -{ - _terminate = true; -} - -void HandleThreadRunner::run() -{ - string sRes(""); - DbProxy _dbproxy; - int64_t tStart = 0; - int64_t tEnd = 0; - - while (!_terminate) - { - QueryItem* pQueryItem = NULL; - - if(!_terminate && _proc->pop(pQueryItem)) - { - try - { - tStart = TNOWMS; - - _dbproxy.queryData(pQueryItem->mQuery, sRes, pQueryItem->bFlag); - - tEnd = TNOWMS; - - sRes += "endline\n"; - - pQueryItem->current->sendResponse(sRes.c_str(), sRes.length()); - - FDLOG("inout") << "HandleThreadRunner::run sUid:" << pQueryItem->sUid << "queryData timecost(ms):" << (tEnd - tStart) << endl; - } - catch(exception& ex) - { - TLOGERROR("HandleThreadRunner::run exception:" << ex.what() << endl); - - string sResult = "Ret:-1\n" + string(ex.what()) + "\nendline\n"; - pQueryItem->current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "HandleThreadRunner::run sUid:" << pQueryItem->sUid << "exception:" << ex.what() << endl; - } - catch(...) - { - TLOGERROR("HandleThreadRunner::run exception." << endl); - - string sResult = "Ret:-1\nunknown exception\nendline\n"; - pQueryItem->current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "HandleThreadRunner::run sUid:" << pQueryItem->sUid << "unknown exception." << endl; - } - - sRes = ""; - - delete pQueryItem; - pQueryItem = NULL; - } - } -} diff --git a/cpp/framework/QueryPropertyServer/QueryDbThread.h b/cpp/framework/QueryPropertyServer/QueryDbThread.h deleted file mode 100644 index 49496be1e..000000000 --- a/cpp/framework/QueryPropertyServer/QueryDbThread.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __QUERY_DB_BY_HASH_THREAD_H_ -#define __QUERY_DB_BY_HASH_THREAD_H_ - -#include -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "util/tc_common.h" -#include "util/tc_thread_queue.h" -#include "QueryItem.h" - -using namespace tars; -using namespace std; - -////////////////////////////////////////////////////////////////////////////// -class QueryDbThread; - -////////////////////////////////////////////////////////////////////////////// -class HandleThreadRunner : public TC_Thread -{ -public: - - HandleThreadRunner(QueryDbThread* proc); - - virtual void run(); - - void terminate(); -private: - - bool _terminate; - - QueryDbThread * _proc; - -}; -////////////////////////////////////////////////////////////////////////////// -class QueryDbThread -{ -public: - - QueryDbThread(); - - ~QueryDbThread(); - - void terminate(); - - void start(int iThreadNum); - - void put(QueryItem* pItem); - - bool pop(QueryItem* &pItem); - - size_t getQueueSize() { return _queue.size(); }; - -private: - bool _terminate; - - TC_ThreadQueue _queue; - - vector _runners; -}; - -#endif diff --git a/cpp/framework/QueryPropertyServer/QueryImp.cpp b/cpp/framework/QueryPropertyServer/QueryImp.cpp deleted file mode 100644 index b53839c03..000000000 --- a/cpp/framework/QueryPropertyServer/QueryImp.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryImp.h" -#include "RequestDecoder.h" -#include "QueryItem.h" -#include "servant/Application.h" - -using namespace std; - -////////////////////////////////////////////////////// -void QueryImp::initialize() -{ - //initialize servant here: - //... -} - -////////////////////////////////////////////////////// -void QueryImp::destroy() -{ - //destroy servant here: - //... -} - - -int QueryImp::doRequest(tars::TarsCurrentPtr current, vector& response) -{ - const vector& request = current->getRequestBuffer(); - string buf((const char*)(&request[0]), request.size()); - string sUid = TC_Common::tostr(g_app.genUid()) + "|"; - - FDLOG("inout") << "QueryImp::doRequest sUid:" << sUid << "doRequest input:" << buf << endl; - TLOGDEBUG(sUid << "QueryImp::doRequest sUid:" << sUid << "doRequest input:" << buf << endl); - - doQuery(sUid, buf, false, current); - - return 0; -} - -int QueryImp::doQuery(const string sUid, const string &sIn, bool bTarsProtocol, tars::TarsCurrentPtr current) -{ - try - { - size_t pos = sIn.rfind("}"); // find json end - - string s(""); - - if (pos != string::npos) - { - s = sIn.substr(0, pos+1); - } - else - { - throw TC_Exception("bad query string"); - } - - RequestDecoder decoder(s); - TLOGDEBUG("QueryImp::doQuery"<getNowMs(); - - string lasttime = _proxy.getLastTime(decoder.getSql()); - sResult += "lasttime:" + lasttime + "\n"; - sResult += "endline\n"; - - tEnd = TC_TimeProvider::getInstance()->getNowMs(); - - current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "QueryImp::doQuery time_check sUid:" << sUid << "send result succ,size:"<< sResult.length() << "|timecost(ms):" << (tEnd-tStart) << endl; - TLOGDEBUG("QueryImp::doQuery time_check sUid:" << sUid << "send result succ,size:"<< sResult.length() << "|timecost(ms):" << (tEnd-tStart) << endl); - } - - else if(ret == RequestDecoder::QUERY) - { - current->setResponse(false); - QueryItem * pItem = new QueryItem(); - pItem->sUid = sUid; - pItem->current = current; - pItem->mQuery = decoder.getSql(); - - map &mSqlPart = decoder.getSql(); - - map::iterator it; - for(it=mSqlPart.begin();it!=mSqlPart.end();it++) - { - TLOGDEBUG("QueryImp::mysql "<first<<"|"<second< vGroupField = TC_Common::sepstr(sGroupField, ", "); - - pItem->bFlag = true; - g_app.getThreadPoolQueryDb()->put(pItem); - - TLOGDEBUG("QueryImp::doQuery all dbcount." << endl); - } - else - { - TLOGERROR("QueryImp::doQuery " << sUid << "decode request failed\n" <sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "QueryImp::doQuery failed sUid:" << sUid << endl; - } - } - catch(exception &ex) - { - TLOGERROR("QueryImp::doQuery exception:" << ex.what() << endl); - string sResult = "Ret:-1\n" + string(ex.what()) + "\nendline\n"; - current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "QueryImp::doQuery exception sUid:" << sUid << endl; - } - return 0; -} diff --git a/cpp/framework/QueryPropertyServer/QueryImp.h b/cpp/framework/QueryPropertyServer/QueryImp.h deleted file mode 100644 index 4fb6c53ce..000000000 --- a/cpp/framework/QueryPropertyServer/QueryImp.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _QueryImp_H_ -#define _QueryImp_H_ - -#include "servant/Application.h" -#include "util/tc_common.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "DbProxy.h" - -/** - * - * - */ -class QueryImp : public tars::Servant -{ -public: - /** - * - */ - virtual ~QueryImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - - - virtual int doRequest(tars::TarsCurrentPtr current, vector& response); - - -private: - int doQuery(const string sUid, const string &sIn, bool bTarsProtocol, tars::TarsCurrentPtr current); - -private: - DbProxy _proxy; -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/framework/QueryPropertyServer/QueryItem.h b/cpp/framework/QueryPropertyServer/QueryItem.h deleted file mode 100644 index a44d0f68e..000000000 --- a/cpp/framework/QueryPropertyServer/QueryItem.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __QUERY_ITEM_H_ -#define __QUERY_ITEM_H_ - -#include -#include -#include "servant/Application.h" - -using namespace std; -using namespace tars; - -///////////////////////////////////////////////////////////////////// -class QueryItem -{ -public: - bool bFlag; - string sUid; - map mQuery; - tars::TarsCurrentPtr current; - - QueryItem() - : bFlag(false) - , sUid("") - , current(NULL) - {} -}; - -#endif diff --git a/cpp/framework/QueryPropertyServer/QueryServer.cpp b/cpp/framework/QueryPropertyServer/QueryServer.cpp deleted file mode 100644 index cafbba709..000000000 --- a/cpp/framework/QueryPropertyServer/QueryServer.cpp +++ /dev/null @@ -1,242 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryServer.h" -#include "QueryImp.h" - -using namespace std; - -QueryServer g_app; - -TC_Config * g_pconf; -///////////////////////////////////////////////////////////////// - -struct JsonProtocol -{ - - static int parse(string &in, string &out) - { - TLOGINFO("JsonProtocol parse:" << in << endl); - - string::size_type jsonEnd = in.find("}"); - - if (jsonEnd != string::npos ) - { - out = in; - in = ""; - return TC_EpollServer::PACKET_FULL; //返回1表示收到的包已ç»å®Œå…¨ - } - - return TC_EpollServer::PACKET_ERR; //返回-1表示收到包å议错误,框架会自动关闭当å‰è¿žæŽ¥ - } -}; - - - -void QueryServer::initialize() -{ - //initialize application here: - //... - vector v_dblist; - vector v_dbcountlist; - - g_pconf->getDomainVector("/tars/countdb", v_dbcountlist); - - size_t iDbNumber = v_dblist.size(); - - size_t iDbCountNumber = v_dbcountlist.size(); - - TLOGDEBUG("QueryServer::initialize stat dbstat size:" << iDbNumber << "|dbcount size:" << iDbCountNumber << endl); - - for(size_t i = 0; i < iDbCountNumber; i++) - { - TC_DBConf tcDBConf; - - string path= "/tars/countdb/" + v_dbcountlist[i]; - - tcDBConf.loadFromMap(g_pconf->getDomainMap(path)); - - _dbStatInfo.push_back(tcDBConf); - } - - _activeDbInfo = _dbStatInfo; - - _dBThread = new DBThread(); - - _dBThread->start(); - - _queryFlag.insert("f_date"); - _queryFlag.insert("f_tflag"); - _queryFlag.insert("master_name"); - _queryFlag.insert("slave_name"); - _queryFlag.insert("slave_ip"); - _queryFlag.insert("interface_name"); - - _insertInterval = TC_Common::strto(g_pconf->get("/tars","5")); - - size_t iDbThreadPoolSize = TC_Common::strto(g_pconf->get("/tars/threadpool","8")); - - size_t iTimeCheckPoolSize = TC_Common::strto(g_pconf->get("/tars/threadpool","8")); - - size_t iQueryDbPoolSize = TC_Common::strto(g_pconf->get("/tars/threadpool","4")); - - _timeCheck.init(iTimeCheckPoolSize); - - _timeCheck.start(); - - _poolDb.init(iDbThreadPoolSize); - - _poolDb.start(); - - _tpoolQueryDb = new QueryDbThread(); - - _tpoolQueryDb->start(iQueryDbPoolSize); - - vector vIpGroup = g_pconf->getDomainKey("/tars/notarsslavename"); - TLOGDEBUG("QueryServer::initialize vIpGroup size:" << vIpGroup.size() << endl); - for (unsigned i = 0; i < vIpGroup.size(); i++) - { - _notTarsSlaveName.insert(vIpGroup[i]); - TLOGDEBUG("QueryServer::initialize i:" << i << "|notarsslavename:" << vIpGroup[i] << endl); - } - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".NoTarsObj"); - addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".NoTarsObj", &JsonProtocol::parse); -} -///////////////////////////////////////////////////////////////// - -void QueryServer::setActiveDb(const vector &vDbInfo) -{ - TC_LockT lock(*this); - _activeDbInfo = vDbInfo; -} - -vector QueryServer::getAllActiveDbInfo() const -{ - TC_LockT lock(*this); - vector vDbInfo; - for(size_t i = 0; i < _activeDbInfo.size(); ++i) - { - vDbInfo.push_back(_activeDbInfo[i]); - } - - return vDbInfo; -} - -vector QueryServer::getDbInfo() const -{ - TC_LockT lock(*this); - return _dbStatInfo; -} - -vector QueryServer::getActiveDbInfo() const -{ - TC_LockT lock(*this); - return _activeDbInfo; -} - -uint32_t QueryServer::genUid() -{ - TC_LockT lock(*this); - - while (++_uniqId == 0); - - return _uniqId; -} - -string QueryServer::dumpDbInfo(const vector& vDbInfo) const -{ - ostringstream os; - - os <::const_iterator it = _queryFlag.find(sKey); - if(it != _queryFlag.end()) - { - return true; - } - return false; -} - -set& QueryServer::getNotTarsSlaveName() -{ - return _notTarsSlaveName; -} - -///////////////////////////////////////////////////////////////// -void -QueryServer::destroyApp() -{ - //destroy application here: - //... - if(_dBThread) - { - delete _dBThread; - _dBThread = NULL; - } - - if(_tpoolQueryDb) - { - delete _tpoolQueryDb; - _tpoolQueryDb = NULL; - } - - bool b = _poolDb.waitForAllDone(1000); - - TLOGDEBUG("QueryServer::destroyApp waitForAllDone _poolDb return:" << b << "|getJobNum:" << _poolDb.getJobNum() << endl); - - _poolDb.stop(); - - b = _timeCheck.waitForAllDone(1000); - - TLOGDEBUG("QueryServer::destroyApp waitForAllDone _timeCheck return:" << b << "|getJobNum:" << _timeCheck.getJobNum() << endl); - - _timeCheck.stop(); -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_pconf = & g_app.getConfig(); - g_app.main(argc, argv); - TarsTimeLogger::getInstance()->enableRemote("inout",false); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/framework/QueryPropertyServer/QueryServer.h b/cpp/framework/QueryPropertyServer/QueryServer.h deleted file mode 100644 index 41da8f260..000000000 --- a/cpp/framework/QueryPropertyServer/QueryServer.h +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _QueryServer_H_ -#define _QueryServer_H_ - -#include -#include -#include "servant/Application.h" -#include "util/tc_thread_mutex.h" -#include "util/tc_lock.h" -#include "util/tc_thread_pool.h" -#include "util/tc_atomic.h" -#include "DbThread.h" -#include "QueryDbThread.h" - -using namespace std; -using namespace tars; - -///////////////////////////////////////////////////////////////////// -class QueryParam -{ -public: - int _run_times; - int _run_result; - TC_Atomic _atomic; - TC_ThreadLock _monitor; - - QueryParam() - : _run_times(0) - , _run_result(0) - , _atomic(0) - {} -}; -///////////////////////////////////////////////////////////////////// -class QueryServer : public Application , public TC_ThreadMutex -{ -public: - /** - * - **/ - virtual ~QueryServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); - - int getInsertInterval() const { return _insertInterval; }; - - size_t getDbNumber() const { return _dbStatInfo.size(); } - - size_t getActiveDbSize() const { return _activeDbInfo.size(); } - - void setActiveDb( const vector &vDbInfo); - - vector getAllActiveDbInfo() const; - - vector getDbInfo() const; - - vector getActiveDbInfo() const; - - TC_DBConf getDbInfo(int iIndex) { return _dbStatInfo[iIndex]; } - - string dumpDbInfo(const vector& vDbInfo) const; - - uint32_t genUid(); - - TC_ThreadPool & getThreadPoolTimeCheck() { return _timeCheck; } - - TC_ThreadPool & getThreadPoolDb() { return _poolDb; } - - QueryDbThread * getThreadPoolQueryDb() { return _tpoolQueryDb; } - - bool searchQueryFlag(const string &sKey); - - //匹é…éžtars被调æœåŠ¡å - set& getNotTarsSlaveName(); - -private: - uint32_t _uniqId; //唯一id - - int _insertInterval; //查询最åŽå…¥åº“时间使用,入库耗时ä¸ä¼šè¶…过入库间隔,å•ä½ä¸ºåˆ†é’Ÿ - - DBThread * _dBThread; //定时检查数æ®åº“实例是å¦å­˜æ´» - - QueryDbThread *_tpoolQueryDb; //用于处ç†æ•°æ®åº“的查询æ“作 - - vector _dbStatInfo; //æ•°æ®åº“ä¿¡æ¯ - - vector _activeDbInfo; //存活的数æ®åº“ä¿¡æ¯ - - set _queryFlag; //æ•°æ®åº“字段 - - TC_ThreadPool _timeCheck; //处ç†timecheckæ“作的线程池 - - TC_ThreadPool _poolDb; //具体查询压缩维度åŽçš„æ•°æ®åº“实例数æ®çš„线程池 - - - set _notTarsSlaveName; //匹é…éžtars被调æœåŠ¡å -}; - -extern QueryServer g_app; -extern TC_Config * g_pconf; -//////////////////////////////////////////// -#endif diff --git a/cpp/framework/QueryPropertyServer/RequestDecoder.cpp b/cpp/framework/QueryPropertyServer/RequestDecoder.cpp deleted file mode 100644 index 8fd5b3903..000000000 --- a/cpp/framework/QueryPropertyServer/RequestDecoder.cpp +++ /dev/null @@ -1,502 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbProxy.h" -#include "util/tc_config.h" -#include "QueryServer.h" -#include "RequestDecoder.h" - -static string skey = ""; - - - -RequestDecoder::RequestDecoder(const string& input) -: _input(input) -, _slaveName("") -{ -} -RequestDecoder::~RequestDecoder() -{ - -} - -int RequestDecoder::generateVector(Document &d ,const string s,std::vector &v) -{ - - Value::ConstMemberIterator iter = d.FindMember(s.c_str()); - int i=0; - if (iter == d.MemberEnd()) - { - return 0; - } - else - { - if(iter->value.IsArray()) - { - for (Value::ConstValueIterator itr = iter->value.Begin(); itr != iter->value.End(); ++itr) - { - v.push_back(itr->GetString()); - ++i; - } - } - - else if (iter->value.IsString()) - { - v.push_back(iter->value.GetString()); - i=1; - } - - else - { - return 0; - } - - return i; - } - -} -int RequestDecoder::composeSqlPartition() -{ - vector vConditions; - int iRet=0; - - iRet=generateVector(_req,"filter",vConditions); - - if(iRet==0) - { - return -1; - } - - - string date1,date2; - - int ret = find_field(vConditions, "f_date", date1); - date1 = TC_Common::trim(date1, " ''"); - date1 = TC_Common::trim(date1, " "); - if (ret == EQ || ret ==-1) - { - - date2 = date1; - } - else //NOT EQ, find again - { - ret = find_field(vConditions, "f_date", date2); - date2 = TC_Common::trim(date2, " ''"); - date2 = TC_Common::trim(date2, " "); - if (date1 < date2) - { - - } - else - { - date1.swap(date2); - } - } - - _sql["date1"] = date1; - _sql["date2"] = date2; - //get date end - - //get hour - string tflag1,tflag2; - ret = find_field(vConditions, "f_tflag", tflag1); - tflag1 = TC_Common::trim(tflag1, " \"'"); - if (ret == EQ || ret == -1) - { - - tflag2 = tflag1; - } - else //NOT EQ, find again - { - ret = find_field(vConditions, "f_tflag", tflag2); - tflag2 = TC_Common::trim(tflag2, " \"'"); - if (tflag1 < tflag2) - { - - } - else - { - tflag1.swap(tflag2); - } - } - - _sql["tflag1"] = tflag1; - _sql["tflag2"] = tflag2; - //get hour end - - iRet=generateVector(_req,"filter",vConditions); - if (!vConditions.empty()) - { - string whereCond = ""; - vector::iterator it = vConditions.begin(); - while(it != vConditions.end()) - { - string &sTmp = *it; - string::size_type pos = sTmp.find("slave_name"); - if(pos != string::npos) - { - _slaveName = *it; - } - - whereCond += (whereCond.empty()?"":" and ") + *it ; - it++; - } - - string sWhere(""); - string::size_type ipos = whereCond.find("and istars="); - - if(ipos != string::npos) - { - - string spre = whereCond.substr(0, ipos); - - string snext = whereCond.substr(ipos+9); - - string::size_type ipos1 = snext.find("and"); - - if(ipos1 != string::npos) - { - string s = snext.substr(ipos1); - sWhere = spre; - sWhere += s; - - string sistars = whereCond.substr(ipos+4, (ipos + 9) + ipos1 - (ipos + 4)); - - string value(""); - - int flag = parseCondition(sistars, value); - - TLOGDEBUG("RequestDecoder::composeSqlPartition istars set|sistars:" << sistars << "|flag:" << flag << "|ipos:" << ipos << "|ipos1:" << ipos1 << "|snext:" << snext << endl); - - if(flag == EQ) - { - value = TC_Common::trim(value, " \"'"); - _sql["istars"] = value; - } - else if(flag == NE) - { - value = TC_Common::trim(value, " \"'"); - int iFlag = TC_Common::strto(value); - iFlag = !iFlag; - value = TC_Common::tostr(iFlag); - _sql["istars"] = value; - } - } - else - { - sWhere = spre; - - string sistars = whereCond.substr(ipos+4); - - string value(""); - - int flag = parseCondition(sistars, value); - - if(flag == EQ) - { - value = TC_Common::trim(value, " \"'"); - _sql["istars"] = value; - } - else if(flag == NE) - { - value = TC_Common::trim(value, " \"'"); - int iFlag = TC_Common::strto(value); - iFlag = !iFlag; - value = TC_Common::tostr(iFlag); - _sql["istars"] = value; - } - } - } - else - { - sWhere = whereCond; - } - - _sql["whereCond"] = " where " + sWhere; - - - } - - if(_slaveName != "") - { - string::size_type pos1 = _slaveName.find("'"); - if(pos1 != string::npos) - { - string::size_type pos2 = _slaveName.rfind("'"); - if(pos2 != pos1 && pos2 != string::npos) - { - _slaveName = _slaveName.substr(pos1, (pos2 - pos1 -1)); - } - } - } - - - - vConditions.clear(); - iRet=generateVector(_req,"groupby",vConditions); - - if (!vConditions.empty()) - { - string groupCond = ""; - string groupField = ""; - vector::iterator it = vConditions.begin(); - while(it != vConditions.end()) - { - groupCond += (groupCond.empty()?"":", ") + *it; - groupField += (groupField.empty()?"":", ") + *it; - it++; - } - _sql["groupCond"] = " group by " + groupCond; - _sql["groupField"] = groupField; - } - - string sumField = ""; - vConditions.clear(); - iRet=generateVector(_req,"indexs",vConditions); - if (vConditions.empty()) - { - sumField = " sum(succ_count) "; - } - else - { - vector::iterator it = vConditions.begin(); - while(it != vConditions.end()) - { - // sumField += string((sumField.empty()?"":", ")) + " sum(" + *it + ")" ; - sumField += string((sumField.empty()?"":", ")) + " " + *it + "" ; - it++; - } - } - - _sql["sumField"]= sumField; - return 0; -} - -int RequestDecoder::parseCondition(const string& sCond, string& value) -{ - string::size_type pos =0; - pos= sCond.find(">="); - if (pos != string::npos ) - { - value = sCond.substr(pos+2); - return GE; - } - - pos = sCond.find("<="); - if (pos != string::npos ) - { - value = sCond.substr(pos+2); - return LE; - } - - pos = sCond.find("!="); - if (pos != string::npos ) - { - value = sCond.substr(pos+2); - return NE; - } - pos = sCond.find("<"); - if (pos != string::npos ) - { - value = sCond.substr(pos+1); - return LT; - } - - pos = sCond.find(">"); - if (pos != string::npos ) - { - value = sCond.substr(pos+1); - return GT; - } - - pos = sCond.find("="); - if (pos != string::npos ) - { - value = sCond.substr(pos+1); - return EQ; - } - return 0; -} - -int RequestDecoder::find_field(vector& vCond, const string& field /*f_tflag, f_date*/, string& value ) -{ - vector::iterator it = vCond.begin(); - while(it != vCond.end()){ - - string::size_type pos = it->find(field); - - if(pos != string::npos) - { - string temp = *it; - vCond.erase(it); - return parseCondition(temp, value); - } - it++; - } - return -1; -} - -/* -name : indexs, filter, groupby -*/ -int RequestDecoder::decodeDataid() -{ - - string::size_type pos = _input.find("dataid"); - if (pos == string::npos) - { - return -1; - } - string::size_type endpos = _input.find(",", pos); - _sql["dataid"] = _input.substr(pos + 9, endpos - pos -10); - LOG->debug() << "decodeDataid: " << _sql["dataid"] <debug() << "decodeMethod: " << _sql["method"] < &vFields) -{ - - string::size_type pos1 = arr.find_first_of("["); - if(pos1 == string::npos) - { - LOG->debug() << ("paramh '" + arr + "' is invalid!" ); - return -1; - } - string::size_type pos2 = arr.find_first_of("]"); - if(pos2 == string::npos) - { - LOG->debug() << ("paramh '" + arr + "' is invalid!" ); - return -1; - } - vFields = TC_Common::sepstr(arr.substr(pos1, pos2 -pos1-1), ","); - vector::iterator it = vFields.begin(); - while(it != vFields.end()) - { - - LOG->debug() << "indexs bt " << *it << endl; - string s = TC_Common::trim(*it, "[]\"\""); - *it = s; - LOG->debug() << "indexs at " << *it << endl; - it++; - } - return 0; - - -} - - -map& RequestDecoder::getSql() -{ - return _sql; -} - - -int RequestDecoder::decode() -{ - - _req.Parse(_input.c_str()); - if (_req.HasParseError()) { - TLOGERROR("Decode error:"<<_req.GetParseError()<<"|"<<_input); - return -1; - } - - try - { - vector vConditions ; - generateVector(_req,"method",vConditions); - if(vConditions.size()) - { - _sql["method"] =vConditions[0]; - TLOGDEBUG("Decode"<<_sql["method"]<error() << "RequestDecoder::decode exception:"<< ex.what() << endl; - return -1; - } - return -1; -} - -//ä¼ å…¥sUid,供打å°ä½¿ç”¨ -int RequestDecoder::addUid(const string& sUid) -{ - _sql["uid"] = sUid; - return 0; -} - -string RequestDecoder::getLine(const char** ppChar) -{ - string sTmp; - - sTmp.reserve(512); - - while((**ppChar) != '\r' && (**ppChar) != '\n' && (**ppChar) != '\0') - { - sTmp.append(1, (**ppChar)); - (*ppChar)++; - } - - if((**ppChar) == '\r') - { - (*ppChar)++; /* pass the char '\n' */ - } - - (*ppChar)++; - - return sTmp; -} - diff --git a/cpp/framework/QueryPropertyServer/RequestDecoder.h b/cpp/framework/QueryPropertyServer/RequestDecoder.h deleted file mode 100644 index f21cdf01b..000000000 --- a/cpp/framework/QueryPropertyServer/RequestDecoder.h +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REQUEST_DECODE_H__ -#define __REQUEST_DECODE_H__ -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "servant/StatF.h" -#include "util/tc_common.h" -#include "servant/TarsLogger.h" -#include "util/tc_thread.h" -#include "../../thirdparty/rapidjson/include/rapidjson/document.h" -#include "../../thirdparty/rapidjson/include/rapidjson/writer.h" -#include "../../thirdparty/rapidjson/include/rapidjson/stringbuffer.h" - -using namespace rapidjson; - -class RequestDecoder -{ -public: - static string getLine(const char** ppChar); -public: - enum Op - { - SET, - ADD, - SUB, - EQ, - NE, - GT, - LT, - LE, - GE, - LIMIT, - }; - - enum METHOD - { - QUERY = 0, - TIMECHECK, - }; - RequestDecoder(const string& input); - - ~RequestDecoder(); - - int parseCondition(const string& sCond, string& value); - - /** - * 从vCond中解æžå‡ºfield对应的值 - * - */ - int find_field(vector& vCond, const string& field /*f_tflag, f_date*/, string& value ); - - int decode(); - - int decodeDataid(); - - int decodeMethod(); - - int decodeArray(const string& arr, vector &vFields); - - int addUid(const string& sUid); - - map& getSql(); - - int composeSqlPartition(); - - string getQuerySlaveName() { return _slaveName; } - - int generateVector(Document &d ,const string s,std::vector &v); - -private: - //待解æžçš„json请求串 - string _input; - - //解æžjson得到的数æ®åº“查询slave_name - string _slaveName; - - //解æžjson请求串得到的Dom 文件 - Document _req; - - //解æžjson得到的数æ®åº“查询æ¡ä»¶å€¼ - map _sql; -}; -#endif - diff --git a/cpp/framework/QueryStatServer/CMakeLists.txt b/cpp/framework/QueryStatServer/CMakeLists.txt deleted file mode 100644 index a8e939701..000000000 --- a/cpp/framework/QueryStatServer/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set(MODULE "tarsquerystat") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/) - -complice_module(${MODULE}) - - - -#FILE(command 'rm -rf ${EXECUTABLE_OUTPUT_PATH}/tarsquerystat') diff --git a/cpp/framework/QueryStatServer/DbProxy.cpp b/cpp/framework/QueryStatServer/DbProxy.cpp deleted file mode 100644 index b650ba252..000000000 --- a/cpp/framework/QueryStatServer/DbProxy.cpp +++ /dev/null @@ -1,818 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbProxy.h" -#include - -/////////////////////////////////////////////////////////// -string tFlagInc(const string& stflag); - -string dateInc(const string& sDate); - -void selectLastMinTime(const string& sUid, int iThread , const string& tbname, const TC_DBConf& tcDbInfo, string & ret, QueryParam &queryParam); - -void query(int iThread, const TC_DBConf & conf, map& mSqlPart, map > &result, string &sRes, QueryParam &queryParam); - -DbProxy::DbProxy() -{ -} - -DbProxy::~DbProxy() -{ -} - -int DbProxy::createRespHead(const vector &res, const string& sLasttime, string& result, bool bDbCountFlag) -{ - int iRet = 0; - string sRes; - - //检查查询返回值,如果一个线程失败,就返回失败。 - for(size_t i=0; i< res.size(); i++) - { - sRes += res[i] ; - if ( res[i][4] != '0' && iRet == 0) - { - iRet = -1; - } - } - - //int total = bDbCountFlag ? g_app.getDbNumber() : g_app.getDbNumber(); - int total = g_app.getDbNumber(); - result = "Ret:"; - result += TC_Common::tostr(iRet); - result += "\nlasttime:"; - result += sLasttime; - result += "\nActiveDb:"; - result += TC_Common::tostr(res.size()); - result += "\nTotalDb:"; - result += TC_Common::tostr(total); - result += "\n"; - result += sRes; - - return iRet; -} -int DbProxy::createRespData(const string& sUid, const map& mSqlPart, const vector > >& vDataList, const string& sHead, string &result) -{ - // 组åˆå¤šçº¿ç¨‹ç»“æžœ - //map firstç”±goupbyç”Ÿæˆ - //map second ç”±indexç”Ÿæˆ - int64_t tStart = TNOWMS; - vector > >::const_iterator dataItr = vDataList.begin(); - map > mStatData; - map >::iterator _it; - - for(size_t i = 0; dataItr != vDataList.end(); dataItr++, i++) - { - TLOGINFO(sUid << "sum["<size() << endl); - for(map >::const_iterator it = dataItr->begin(); it != dataItr->end(); it++) - { - _it = mStatData.find(it->first); - if (_it != mStatData.end()) - { - const vector &number1 = it->second; - vector &number2 = _it->second; - // 相åŒkey的值 求和,number1å’Œnumber1的大å°æ˜¯ä¸€æ ·çš„ - for (size_t j=0; jfirst] = it->second; - } - } - } - - int iIndex = -1; - size_t iGroupFieldSize = 0; - bool bTars = false; - string sIsTars(""); - string groupField(""); - - map::const_iterator const_it = mSqlPart.find("istars"); - if(const_it != mSqlPart.end()) - { - TLOGDEBUG("DbProxy::createRespData istars:" << const_it->second << endl); - - bTars = true; - - sIsTars = const_it->second; - - map::const_iterator const_it_inner = mSqlPart.find("groupField"); - if(const_it_inner != mSqlPart.end()) - { - groupField = const_it_inner->second; - - vector vGroupField = TC_Common::sepstr(groupField, ", "); - - iGroupFieldSize = vGroupField.size(); - - for(size_t i = 0; i < vGroupField.size(); ++i) - { - if(vGroupField[i] == "slave_name") - { - iIndex = i; - break; - } - } - } - - if(iIndex < 0) - { - bTars = false; - } - } - /*else - { - TLOGDEBUG("DbProxy::createRespData no set istars." << endl); - }*/ - - string sTemp(""); - int iLineNum = 0; - - //把 查询结果转æ¢æˆä¸€è¡Œä¸€è¡Œçš„串 - /* - * input :groupby, f_date, f_tflag - * input : index, succ_count, timeout_count - * all map > - * string =>> f_date, f_tflag - * vector =>> succ_count, timeout_count - */ - _it = mStatData.begin(); - while(_it != mStatData.end()) - { - bool bFilter = false; - if(bTars) - { - vector vGroupField = TC_Common::sepstr(_it->first, ",", true); - - if(sIsTars == "1") - { - set::iterator it; - it = g_app.getNotTarsSlaveName().find(vGroupField[iIndex]); - if(it != g_app.getNotTarsSlaveName().end()) - { - bFilter = true; - } - } - else - { - if(vGroupField.size() != iGroupFieldSize) - { - TLOGERROR("DbProxy::createRespData vGroupField.size:" << vGroupField.size() << "|iGroupFieldSize:" << iGroupFieldSize << "|key:" << _it->first << "|keyFiled:" << groupField << endl); - } - else if(vGroupField[iIndex] == "" || vGroupField[iIndex].size() == 0) - { - TLOGERROR("DbProxy::createRespData iIndex:" << iIndex << "|vGroupField[iIndex]:" << vGroupField[iIndex] << endl); - } - - set::iterator it; - it = g_app.getNotTarsSlaveName().find(vGroupField[iIndex]); - if(it == g_app.getNotTarsSlaveName().end()) - { - bFilter = true; - } - } - - if(!bFilter) - { - ++iLineNum; - - string valueBuffer = ""; - vector::iterator valueIt = _it->second.begin(); - while(valueIt != _it->second.end()) // value is vector int, need transfer to string; - { - valueBuffer += TC_Common::tostr(*valueIt); - valueBuffer += ","; - valueIt++; - } - - sTemp += _it->first; - sTemp += ","; - sTemp += valueBuffer; - sTemp += "\n"; - } - } - else - { - ++iLineNum; - - string valueBuffer = ""; - vector::iterator valueIt = _it->second.begin(); - while(valueIt != _it->second.end()) // value is vector int, need transfer to string; - { - valueBuffer += TC_Common::tostr(*valueIt); - valueBuffer += ","; - valueIt++; - } - - sTemp += _it->first; - sTemp += ","; - sTemp += valueBuffer; - sTemp += "\n"; - } - - _it++; - } - - result += sHead; - result += "linecount:"; - result += TC_Common::tostr(iLineNum); - result += "\n"; - result += sTemp; - - TLOGINFO("DbProxy::createRespData result:"< &mSqlPart, string &sResult, bool bDbCountFlag) -{ - try - { - string sUid = mSqlPart.find("uid")->second; - - vector vActive; - - if(bDbCountFlag) - { - vActive = g_app.getActiveDbInfo(); - } - else - { - //vActive = g_app.getActiveDbInfo(); - } - - int iThreads = vActive.size(); - - if(iThreads > 0) - { - vector res(iThreads); - - vector > > vDataList(iThreads); - - _queryParam._run_times = iThreads; - - //TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "all thread query data begin." << endl); - - int64_t tStart = TC_TimeProvider::getInstance()->getNowMs(); - - for(int i=0; i < iThreads; i++) - { - auto fwrapper = std::bind(&query, - i, - std::cref(vActive[i]), - std::ref(mSqlPart), - std::ref(vDataList[i]), - std::ref(res[i]), - std::ref(_queryParam)); - - if(bDbCountFlag) - { - g_app.getThreadPoolDb().exec(fwrapper); - } - else - { - //g_app.getThreadPoolDb().exec(fwrapper); - } - } - - //ç­‰å¾…çº¿ç¨‹ç»“æŸ - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "wait for all thread query data done." << endl); - - bool rc = true; - int ifail = 0; - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(6000); - } - - ++ifail; - - if(!rc) - { - if(ifail >= 10) - { - break; - } - } - } - - if(ifail >= 10) - { - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "wait for all thread query data timeout." << endl); - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - _queryParam._monitor.timedWait(1000); - } - } - } - - if(_queryParam._atomic.get() == _queryParam._run_times) - rc = true; - /*bool rc = false; - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(60000); - }*/ - - int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs(); - - if(rc) - { - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; - - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "all thread done return:" << _queryParam._run_result <<"|timecost(ms):" << (tEnd - tStart) << endl); - - // 返回ret code - string sHead; - - string sLasttime = getLastTime(mSqlPart); - - if(createRespHead(res, sLasttime, sHead, bDbCountFlag) != 0) - { - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; - - sResult = sHead; - TLOGERROR("DbProxy::queryData query error:" << sHead << endl); - return; - } - - createRespData(sUid, mSqlPart, vDataList, sHead, sResult); - } - else - { - sResult ="Ret:-1\nquery timeout\n"; - - TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "Ret:-1|query timeout." << endl); - } - } - else - { - sResult ="Ret:-1\nno active db\n"; - } - } - catch (exception &ex) - { - TLOGERROR("DbProxy::queryData exception:" << ex.what() << endl); - sResult ="Ret:-1\n" + string(ex.what()) + "\n"; - } - - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; -} - -void query(int iThread, const TC_DBConf & conf, map& mSqlPart, map > &result, string &sRes, QueryParam &queryParam) -{ - string sUid = mSqlPart.find("uid")->second; - - TLOGDEBUG("queryData " << sUid << "thread iIndex:" << iThread << endl); - - int64_t tStart = TNOWMS; - try - { - //dateFrom =>> 20111120 - string dateFrom = mSqlPart["date1"]; - string dateTo = mSqlPart["date2"]; - - //tflagFrom =>> 2360 - string tflagFrom = mSqlPart["tflag1"]; - string tflagTo = mSqlPart["tflag2"]; - - // 输入tflag æ¡ä»¶æ£€æŸ¥ - if (dateFrom.length() != 8 || dateTo.length() != 8 || tflagFrom.length() != 4 || - tflagTo.length() != 4 || - TC_Common::isdigit(tflagFrom) == false || - TC_Common::isdigit(tflagTo) == false) - { - sRes += "ret:-1|iDb:" + TC_Common::tostr(iThread) + "|wrong tflag:" + tflagFrom + "-" + tflagTo + "\n"; - - TLOGERROR("query sUid:" << sUid << sRes << endl); - - queryParam._run_result = -1; - queryParam._atomic.inc(); - - if(queryParam._atomic.get() == queryParam._run_times) - { - TC_ThreadLock::Lock lock(queryParam._monitor); - queryParam._monitor.notifyAll(); - } - - return ; - } - - //groupCond =>> "where slave_name like 'MTTsh2.BrokerServer' and f_tflag >='0000' and f_tflag <='2360' and f_date = '20111120'" - string whereCond = mSqlPart["whereCond"]; - - //groupCond =>> "group by f_date, f_tflag" - string groupCond = mSqlPart["groupCond"]; - - //sumField =>> "succ_count, timeout_count"; - string sumField = mSqlPart["sumField"]; - - //groupField =>> "f_date, f_tflag" - string groupField = mSqlPart["groupField"]; - - //selectCond =>> "succ_count, timeout_count, f_date, f_tflag" - string selectCond = sumField +"," + groupField; - - //日期格å¼20111019 - string::size_type pos = string::npos; - string tmpDate = "f_date"; - if ((pos = selectCond.find(tmpDate, 0)) != string::npos) - { - selectCond.replace(pos, tmpDate.length(), "DATE_FORMAT( f_date, '%Y%m%d') as f_date"); - } - - string sDbName = mSqlPart["dataid"]; - string ignoreKey(""); - - //ä¸ä½¿ç”¨ä¸»é”® - if(sDbName == "tars" || sDbName == "db_tarsstat") - { - ignoreKey = " IGNORE INDEX ( PRIMARY ) "; - } - - vector vGroupField = TC_Common::sepstr(groupField, ", "); - vector vSumField = TC_Common::sepstr(sumField, ", "); - - TC_Mysql tcMysql; - - TC_DBConf tcDbConf = conf; - /*if(bFlag) - { - tcDbConf = g_app.getDbInfo(iThread); - } - else - { - tcDbConf = g_app.getDbInfo(iThread); - }*/ - - tcDbConf._database = sDbName; - - tcMysql.init(tcDbConf); - - string sTbNamePre = tcDbConf._database + "_"; - - string sTbName(""); - string sSql(""); - //select range by f_date and f_tflag - for(string day = dateFrom; day <= dateTo; day = dateInc(day)) - { - for(string tflag = tflagFrom; tflag <= tflagTo && (tflag.substr(0,2) < "24"); tflag = tFlagInc(tflag)) - { - //table name:tars_2012060723 - sTbName = sTbNamePre + day + tflag.substr(0,2); - - sSql = "select " + selectCond + " from " + sTbName + " " + ignoreKey + whereCond + groupCond + " order by null;"; - - tars::TC_Mysql::MysqlData res = tcMysql.queryRecord(sSql); - - TLOGINFO(sUid << "res.size:" << res.size() << "|sSql:" << sSql << endl); - - // result is key:value pair; - //sKey ç”±groupbyç”Ÿæˆ - //valueç”±indexç”Ÿæˆ - //int64_t t2Start = TNOWMS; - for(size_t iRow = 0; iRow < res.size(); iRow++) - { - string sKey = ""; - for(size_t j = 0; j < vGroupField.size(); j++) - { - sKey += sKey.empty()?"":","; - sKey += res[iRow][vGroupField[j]]; - } - - map >::iterator itResult = result.find(sKey); - if (itResult != result.end()) - { - vector& data = itResult->second; - for (size_t j = 0; j < vSumField.size() && j < data.size(); j++) - { - data[j] += TC_Common::strto(res[iRow][vSumField[j]]);// 相åŒkey的值 求和 - } - } - else - { - vector& vRes = result[sKey]; - for(size_t j = 0; j < vSumField.size(); j++) - { - vRes.push_back( TC_Common::strto(res[iRow][vSumField[j]]));; - } - } - TLOGINFO("query iDb:" << iThread <<" {"<< sKey << ":" << TC_Common::tostr(result[sKey]) << "}" << endl); - } - - TLOGINFO("query iDb :" << iThread << " day:" << day <<" tflag:" << tflag << endl); - } - } //day - - sRes = "ret:0 iDb:" + TC_Common::tostr(iThread) + "\n"; - - //queryParam._atomic.inc(); - } - catch(TC_Mysql_Exception & ex) - { - sRes = "ret:-1|iDb:" + TC_Common::tostr(iThread) + string("|exception:") + ex.what() + "\n"; - TLOGERROR("query sUid:" << sUid << "query:" << sRes << endl); - - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - catch(exception & ex) - { - sRes = "ret:-1|iDb:" + TC_Common::tostr(iThread) + string("|exception:") + ex.what() + "\n"; - TLOGERROR("query sUid:" << sUid << "query:" << sRes << endl); - - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - int64_t tEnd = TNOWMS; - - TLOGDEBUG("query sUid:" << sUid << "exit query iDb:" << iThread <<"|timecost(ms):" << (tEnd - tStart) << "|res:" << sRes << endl); - - queryParam._atomic.inc(); - - if(queryParam._atomic.get() == queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(queryParam._monitor); - queryParam._monitor.notifyAll(); - } - - TLOGDEBUG("query sUid:" << sUid << "notify query finish." << endl); - } - - -} - -/////////////////////////////////////////////////////////////////////////////// -string tFlagInc(const string& stflag) -{ - int h = TC_Common::strto(stflag.substr(0,2)); - int m = TC_Common::strto(stflag.substr(2,2)); - - h += 1; - char buf[5]; - snprintf(buf,sizeof(buf),"%.2d%.2d",h,m); - - return string(buf); -} -/////////////////////////////////////////////////////////////////////////////// -string dateInc(const string& sDate) -{ - string ret("20991231"); // 返回大数 - - try - { - int year = TC_Common::strto(sDate.substr(0, 4)); - int mon = TC_Common::strto(sDate.substr(4, 2)); - int day = TC_Common::strto(sDate.substr(6, 2)); - - struct tm *p = NULL; - time_t timep; - struct tm tt = {0}; - - time(&timep); - p=localtime_r(&timep, &tt); - p->tm_mon = mon -1; - p->tm_mday = day +1; - p->tm_year = year -1900 ; - - timep = mktime(p); - ret = TC_Common::tm2str(timep, "%Y%m%d"); - } - catch(exception & ex) - { - TLOGERROR("DbProxy::dateInc exception:" << ex.what() << endl); - } - return ret; -} -/////////////////////////////////////////////////////////////////////////////// -void selectLastMinTime(const string& sUid, int iThread , const string& tbname, const TC_DBConf& tcDbInfo, string & ret, QueryParam &queryParam) -{ - string sId = sUid; - try - { - string sTbNamePre = tbname + ".t_ecstatus"; - - //TLOGDEBUG("selectLastMinTime database name:" << tcDbConf._database << "|tbname:" << tbname << endl); - - TC_Mysql tcMysql; - tcMysql.init(tcDbInfo); - - int interval = g_app.getInsertInterval(); - time_t now = TC_TimeProvider::getInstance()->getNow(); - - string sDate,sFlag; - // 排除历å²è¿‡æœŸæ•°æ® - string sTime,sHour,sMinute; - time_t t = (now - interval * 60 * 2); - interval = g_app.getInsertInterval(); - t = (t/(interval*60))*interval*60; //è¦æ±‚必须为loadIntevæ•´æ•°å€ - t = (t%3600 == 0?t-60:t); //è¦æ±‚å°†9点写作0860 - sTime = TC_Common::tm2str(t,"%Y%m%d%H%M"); - sDate = sTime.substr(0,8); - sHour = sTime.substr(8,2); - sMinute = sTime.substr(10,2); - sFlag = sHour + (sMinute=="59"?"60":sMinute); //è¦æ±‚å°†9点写作0860 - - string sLast = sDate + " " + sFlag; - - string sSql = "select min(lasttime) as lasttime from "+ sTbNamePre+" where appname like '" +"%' and lasttime > '" + sLast + "'" ; - - tars::TC_Mysql::MysqlData res = tcMysql.queryRecord(sSql); - - if (res.size() > 0) - { - TLOGINFO("selectLastTime" << sId << "sSql:" << sSql << "|lasttime:" << res[0]["lasttime"] << endl); - - ret = res[0]["lasttime"]; - } - else - { - ret = ""; - } - //queryParam._atomic.inc(); - } - catch(TC_Mysql_Exception & ex) - { - TLOGERROR("selectLastTime sUid="<< sId <<"exception:"<< ex.what() << endl); - ret = ""; - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - catch(exception& e) - { - TLOGERROR("selectLastTime sUid="<< sId <<"exception:"<< e.what() << endl); - ret = ""; - queryParam._run_result = -1; - //queryParam._atomic.inc(); - } - - queryParam._atomic.inc(); - if(queryParam._atomic.get() == queryParam._run_times) - { - TC_ThreadLock::Lock lock(queryParam._monitor); - queryParam._monitor.notifyAll(); - TLOGDEBUG("query sUid:" << sId << "notify checktime finish." << endl); - } -} -/////////////////////////////////////////////////////////////////////////////// -string DbProxy::getLastTime(const map& mSqlPart) -{ - string sUid = mSqlPart.find("uid")->second; - - string min = "99999999999"; // 求最å°çš„,åˆå§‹ä½¿ç”¨å¾ˆå¤§çš„æ•°æ® - //TLOGDEBUG("mSqlPart"<< mSqlPart.find("dataid")->second < vDbInfo = g_app.getAllActiveDbInfo(); - - int iThreads = vDbInfo.size(); - - if(iThreads > 0) - { - vector res(iThreads); - - _queryParam._run_times = iThreads; - - int64_t tStart = TC_TimeProvider::getInstance()->getNowMs(); - - for (int i=0; i< iThreads; i++) - { - const string tbname = mSqlPart.find("dataid")->second; - auto fwrapper = std::bind(&selectLastMinTime, - std::cref(sUid), - i, - std::cref(tbname), - std::cref(vDbInfo[i]), - std::ref(res[i]), - std::ref(_queryParam)); - - g_app.getThreadPoolTimeCheck().exec(fwrapper); - } - - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "wait for getLastTime done." << endl); - - bool rc = true; - int ifail = 0; - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(300); - } - - ++ifail; - - if(!rc) - { - if(ifail >= 10) - { - break; - } - } - } - - if(ifail >= 10) - { - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "wait for getLastTime timeout." << endl); - while(_queryParam._atomic.get() != _queryParam._run_times) - { - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - _queryParam._monitor.timedWait(1000); - } - } - } - - if(_queryParam._atomic.get() == _queryParam._run_times) - rc = true; - /*bool rc = false; - { - TC_ThreadLock::Lock lock(_queryParam._monitor); - rc = _queryParam._monitor.timedWait(3000); - }*/ - - int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs(); - - - if(rc) - { - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "getLastTime all done|return:" << _queryParam._run_result <<"|timecost(ms):" << (tEnd-tStart) << endl); - - for(int i = 0; i < iThreads; ++i) - { - if(res[i] < min) - { - min = res[i]; - } - } - } - else - { - min = ""; - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "getLastTime timeout." << endl); - } - } - else - { - min = ""; - } - - TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "final lasttime:" << min << endl); - } - catch (exception &ex) - { - TLOGERROR("DbProxy::getLastTime exception:" << ex.what() << endl); - min = ""; - } - - _queryParam._run_times = 0; - _queryParam._run_result = 0; - _queryParam._atomic = 0; - - return min; -} - - -string DbProxy::makeResult(int iRet, const string& sRes) -{ - size_t act = g_app.getActiveDbInfo().size(); - int total = g_app.getDbInfo().size(); - string result = "Ret:" + TC_Common::tostr(iRet) + "\n" - + "ActiveDb:" + TC_Common::tostr(act) + "\n" - + "TotalDb:" + TC_Common::tostr(total) + "\n" - + sRes; - return result; -} diff --git a/cpp/framework/QueryStatServer/DbProxy.h b/cpp/framework/QueryStatServer/DbProxy.h deleted file mode 100644 index 373a76843..000000000 --- a/cpp/framework/QueryStatServer/DbProxy.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_PROXY_H_ -#define __DB_PROXY_H_ - -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "servant/TarsLogger.h" -#include "QueryServer.h" - -using namespace tars; - -class DbProxy -{ -public: - - DbProxy(); - - ~DbProxy(); - - void queryData(map& mSqlPart, string &sResult, bool bDbCountFlag); - - string getLastTime(const map& mSqlPart); - -private: - - int createRespHead(const vector &res, const string& sLasttime ,string& result, bool bDbCountFlag); - - int createRespData(const string& sUid, const map& mSqlPart, const vector > > &vDataList, const string& sHead, string &result); - - string makeResult(int iRet, const string& sRes); - -private: - QueryParam _queryParam; -}; - - -#endif - - diff --git a/cpp/framework/QueryStatServer/DbThread.cpp b/cpp/framework/QueryStatServer/DbThread.cpp deleted file mode 100644 index 718956ac8..000000000 --- a/cpp/framework/QueryStatServer/DbThread.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbThread.h" -#include "util/tc_config.h" -#include "QueryServer.h" - - -DBThread::DBThread() -: _bTerminate(false) -{ - TLOGDEBUG("DBThread init ok" << endl); -} - -DBThread::~DBThread() -{ - if (isAlive()) - { - terminate(); - - getThreadControl().join(); - } - TLOGDEBUG("DBThread terminate." << endl); -} - -void DBThread::terminate() -{ - _bTerminate = true; - - TC_ThreadLock::Lock lock(*this); - - notifyAll(); -} - -void DBThread::run() -{ - TLOGDEBUG("DBThread::run begin." << endl); - while (!_bTerminate) - { - try - { - tryConnect(); - } - catch ( exception& ex ) - { - TLOGERROR("DBThread::run exception:" << ex.what() << endl); - - } - catch (... ) - { - TLOGERROR("DBThread::run unkonw exception catched" << endl); - } - TC_ThreadLock::Lock lock(*this); - timedWait(REAP_INTERVAL); - } -} - -int DBThread::connect(MYSQL** pstMql,const TC_DBConf& tcConf) -{ - - *pstMql = mysql_init(NULL); - if (mysql_real_connect(*pstMql,tcConf._host.c_str(), tcConf._user.c_str(), tcConf._password.c_str(), tcConf._database.c_str(), tcConf._port, NULL, tcConf._flag) == NULL) - { - TLOGERROR("[TC_Mysql::connect]: mysql_real_connect: " + string(mysql_error(*pstMql)) + ", erron:" < vDbInfo = g_app.getDbInfo(); - - // size_t iDbInfoSize = vDbInfo.size(); - - vector vDbCountInfo = g_app.getDbInfo(); - - size_t iDbCountInfoSize = vDbCountInfo.size(); - - - - vector vAtivedbCountInfo; - - - - for(size_t i = 0; i < vDbCountInfo.size(); i++) - { - TC_DBConf tcDBConf = vDbCountInfo[i]; - - - - MYSQL* _pstMql = NULL; - - int iRet = connect(&_pstMql,tcDBConf); - - if (_pstMql != NULL) - { - mysql_close(_pstMql); - _pstMql = NULL; - } - - if (iRet != 0) - { - TLOGERROR("DBThread::tryConnect new db host:" << tcDBConf._host << "|port:" << TC_Common::tostr(tcDBConf._port) << "|database:" << tcDBConf._database << endl); - string s("host"); - - s += tcDBConf._host; - s += "|port"; - s += TC_Common::tostr(tcDBConf._port); - s += "|database:"; - s += tcDBConf._database; - - sendAlarmSMS(s); - continue; - } - - vAtivedbCountInfo.push_back(vDbCountInfo[i]);; - } - - size_t iActiveDbCountInfoSize = vAtivedbCountInfo.size(); - - if(iDbCountInfoSize != iActiveDbCountInfoSize) - { - string sMsg = "statcount old db size:"; - sMsg += TC_Common::tostr(iDbCountInfoSize); - sMsg += "|statcount active db size:"; - sMsg += TC_Common::tostr(iActiveDbCountInfoSize); - sMsg += "|no equal."; - - sendAlarmSMS(sMsg); - } - - g_app.setActiveDb(vAtivedbCountInfo); - } - catch (exception& ex) - { - TLOGERROR("DBThread::tryConnect exception:"<< ex.what() << endl); - } -} - - - diff --git a/cpp/framework/QueryStatServer/DbThread.h b/cpp/framework/QueryStatServer/DbThread.h deleted file mode 100644 index 56766df56..000000000 --- a/cpp/framework/QueryStatServer/DbThread.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_THREAD_H_ -#define __DB_THREAD_H_ -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "util/tc_common.h" -#include "servant/TarsLogger.h" - - -using namespace tars; - -/** - * 用于执行定时æ“作的线程类 - */ -class DBThread : public TC_Thread, public TC_ThreadLock -{ -public: - /** - * å®šä¹‰å¸¸é‡ - */ - enum - { - REAP_INTERVAL = 30000 - }; - /** - * 构造 - */ - DBThread(); - - /** - * æžå¤Ÿ - */ - ~DBThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * 轮询函数 - */ - virtual void run(); - - void tryConnect(); - - int connect(MYSQL **_pstMql,const TC_DBConf& tcConf); - - void sendAlarmSMS(const string &sMsg); - -private: - bool _bTerminate; - -}; - -#endif diff --git a/cpp/framework/QueryStatServer/QueryDbThread.cpp b/cpp/framework/QueryStatServer/QueryDbThread.cpp deleted file mode 100644 index 6a4a72c57..000000000 --- a/cpp/framework/QueryStatServer/QueryDbThread.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryDbThread.h" -#include "QueryServer.h" -#include "DbProxy.h" - -///////////////////////////////////////////////////////////////////////// -QueryDbThread::QueryDbThread() -: _terminate(false) -{ - TLOGDEBUG("QueryDbThread init ok" << endl); -} - -QueryDbThread::~QueryDbThread() -{ - terminate(); - - TLOGDEBUG("QueryDbThread terminate." << endl); -} - -void QueryDbThread::start(int iThreadNum) -{ - for(int i = 0; i < iThreadNum; ++i) - { - HandleThreadRunner *r = new HandleThreadRunner(this); - - r->start(); - - _runners.push_back(r); - } -} - -void QueryDbThread::terminate() -{ - _terminate = true; - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if (_runners[i]->isAlive()) - { - _runners[i]->terminate(); - - _queue.notifyT(); - } - } - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->getThreadControl().join(); - } - } - - _queue.clear(); - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]) - { - delete _runners[i]; - _runners[i] = NULL; - } - } -} - -bool QueryDbThread::pop(QueryItem* &pItem) -{ - return _queue.pop_front(pItem, 2000); -} - -void QueryDbThread::put(QueryItem* pItem) -{ - if(!_terminate && pItem) - { - _queue.push_back(pItem); - } -} - -///////////////////////////////////////////////////////////////////////// -HandleThreadRunner::HandleThreadRunner(QueryDbThread* proc) -: _terminate(false) -, _proc(proc) -{ -} - -void HandleThreadRunner::terminate() -{ - _terminate = true; -} - -void HandleThreadRunner::run() -{ - string sRes(""); - DbProxy _dbproxy; - int64_t tStart = 0; - int64_t tEnd = 0; - - while (!_terminate) - { - QueryItem* pQueryItem = NULL; - - if(!_terminate && _proc->pop(pQueryItem)) - { - try - { - tStart = TNOWMS; - - _dbproxy.queryData(pQueryItem->mQuery, sRes, pQueryItem->bFlag); - - tEnd = TNOWMS; - - sRes += "endline\n"; - - pQueryItem->current->sendResponse(sRes.c_str(), sRes.length()); - - FDLOG("inout") << "HandleThreadRunner::run sUid:" << pQueryItem->sUid << "queryData timecost(ms):" << (tEnd - tStart) << endl; - } - catch(exception& ex) - { - TLOGERROR("HandleThreadRunner::run exception:" << ex.what() << endl); - - string sResult = "Ret:-1\n" + string(ex.what()) + "\nendline\n"; - pQueryItem->current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "HandleThreadRunner::run sUid:" << pQueryItem->sUid << "exception:" << ex.what() << endl; - } - catch(...) - { - TLOGERROR("HandleThreadRunner::run exception." << endl); - - string sResult = "Ret:-1\nunknown exception\nendline\n"; - pQueryItem->current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "HandleThreadRunner::run sUid:" << pQueryItem->sUid << "unknown exception." << endl; - } - - sRes = ""; - - delete pQueryItem; - pQueryItem = NULL; - } - } -} diff --git a/cpp/framework/QueryStatServer/QueryDbThread.h b/cpp/framework/QueryStatServer/QueryDbThread.h deleted file mode 100644 index 49496be1e..000000000 --- a/cpp/framework/QueryStatServer/QueryDbThread.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __QUERY_DB_BY_HASH_THREAD_H_ -#define __QUERY_DB_BY_HASH_THREAD_H_ - -#include -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "util/tc_common.h" -#include "util/tc_thread_queue.h" -#include "QueryItem.h" - -using namespace tars; -using namespace std; - -////////////////////////////////////////////////////////////////////////////// -class QueryDbThread; - -////////////////////////////////////////////////////////////////////////////// -class HandleThreadRunner : public TC_Thread -{ -public: - - HandleThreadRunner(QueryDbThread* proc); - - virtual void run(); - - void terminate(); -private: - - bool _terminate; - - QueryDbThread * _proc; - -}; -////////////////////////////////////////////////////////////////////////////// -class QueryDbThread -{ -public: - - QueryDbThread(); - - ~QueryDbThread(); - - void terminate(); - - void start(int iThreadNum); - - void put(QueryItem* pItem); - - bool pop(QueryItem* &pItem); - - size_t getQueueSize() { return _queue.size(); }; - -private: - bool _terminate; - - TC_ThreadQueue _queue; - - vector _runners; -}; - -#endif diff --git a/cpp/framework/QueryStatServer/QueryImp.cpp b/cpp/framework/QueryStatServer/QueryImp.cpp deleted file mode 100644 index b53839c03..000000000 --- a/cpp/framework/QueryStatServer/QueryImp.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryImp.h" -#include "RequestDecoder.h" -#include "QueryItem.h" -#include "servant/Application.h" - -using namespace std; - -////////////////////////////////////////////////////// -void QueryImp::initialize() -{ - //initialize servant here: - //... -} - -////////////////////////////////////////////////////// -void QueryImp::destroy() -{ - //destroy servant here: - //... -} - - -int QueryImp::doRequest(tars::TarsCurrentPtr current, vector& response) -{ - const vector& request = current->getRequestBuffer(); - string buf((const char*)(&request[0]), request.size()); - string sUid = TC_Common::tostr(g_app.genUid()) + "|"; - - FDLOG("inout") << "QueryImp::doRequest sUid:" << sUid << "doRequest input:" << buf << endl; - TLOGDEBUG(sUid << "QueryImp::doRequest sUid:" << sUid << "doRequest input:" << buf << endl); - - doQuery(sUid, buf, false, current); - - return 0; -} - -int QueryImp::doQuery(const string sUid, const string &sIn, bool bTarsProtocol, tars::TarsCurrentPtr current) -{ - try - { - size_t pos = sIn.rfind("}"); // find json end - - string s(""); - - if (pos != string::npos) - { - s = sIn.substr(0, pos+1); - } - else - { - throw TC_Exception("bad query string"); - } - - RequestDecoder decoder(s); - TLOGDEBUG("QueryImp::doQuery"<getNowMs(); - - string lasttime = _proxy.getLastTime(decoder.getSql()); - sResult += "lasttime:" + lasttime + "\n"; - sResult += "endline\n"; - - tEnd = TC_TimeProvider::getInstance()->getNowMs(); - - current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "QueryImp::doQuery time_check sUid:" << sUid << "send result succ,size:"<< sResult.length() << "|timecost(ms):" << (tEnd-tStart) << endl; - TLOGDEBUG("QueryImp::doQuery time_check sUid:" << sUid << "send result succ,size:"<< sResult.length() << "|timecost(ms):" << (tEnd-tStart) << endl); - } - - else if(ret == RequestDecoder::QUERY) - { - current->setResponse(false); - QueryItem * pItem = new QueryItem(); - pItem->sUid = sUid; - pItem->current = current; - pItem->mQuery = decoder.getSql(); - - map &mSqlPart = decoder.getSql(); - - map::iterator it; - for(it=mSqlPart.begin();it!=mSqlPart.end();it++) - { - TLOGDEBUG("QueryImp::mysql "<first<<"|"<second< vGroupField = TC_Common::sepstr(sGroupField, ", "); - - pItem->bFlag = true; - g_app.getThreadPoolQueryDb()->put(pItem); - - TLOGDEBUG("QueryImp::doQuery all dbcount." << endl); - } - else - { - TLOGERROR("QueryImp::doQuery " << sUid << "decode request failed\n" <sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "QueryImp::doQuery failed sUid:" << sUid << endl; - } - } - catch(exception &ex) - { - TLOGERROR("QueryImp::doQuery exception:" << ex.what() << endl); - string sResult = "Ret:-1\n" + string(ex.what()) + "\nendline\n"; - current->sendResponse(sResult.c_str(), sResult.length()); - - FDLOG("inout") << "QueryImp::doQuery exception sUid:" << sUid << endl; - } - return 0; -} diff --git a/cpp/framework/QueryStatServer/QueryImp.h b/cpp/framework/QueryStatServer/QueryImp.h deleted file mode 100644 index 4fb6c53ce..000000000 --- a/cpp/framework/QueryStatServer/QueryImp.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _QueryImp_H_ -#define _QueryImp_H_ - -#include "servant/Application.h" -#include "util/tc_common.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "DbProxy.h" - -/** - * - * - */ -class QueryImp : public tars::Servant -{ -public: - /** - * - */ - virtual ~QueryImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - - - virtual int doRequest(tars::TarsCurrentPtr current, vector& response); - - -private: - int doQuery(const string sUid, const string &sIn, bool bTarsProtocol, tars::TarsCurrentPtr current); - -private: - DbProxy _proxy; -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/framework/QueryStatServer/QueryItem.h b/cpp/framework/QueryStatServer/QueryItem.h deleted file mode 100644 index a44d0f68e..000000000 --- a/cpp/framework/QueryStatServer/QueryItem.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __QUERY_ITEM_H_ -#define __QUERY_ITEM_H_ - -#include -#include -#include "servant/Application.h" - -using namespace std; -using namespace tars; - -///////////////////////////////////////////////////////////////////// -class QueryItem -{ -public: - bool bFlag; - string sUid; - map mQuery; - tars::TarsCurrentPtr current; - - QueryItem() - : bFlag(false) - , sUid("") - , current(NULL) - {} -}; - -#endif diff --git a/cpp/framework/QueryStatServer/QueryServer.cpp b/cpp/framework/QueryStatServer/QueryServer.cpp deleted file mode 100644 index cafbba709..000000000 --- a/cpp/framework/QueryStatServer/QueryServer.cpp +++ /dev/null @@ -1,242 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryServer.h" -#include "QueryImp.h" - -using namespace std; - -QueryServer g_app; - -TC_Config * g_pconf; -///////////////////////////////////////////////////////////////// - -struct JsonProtocol -{ - - static int parse(string &in, string &out) - { - TLOGINFO("JsonProtocol parse:" << in << endl); - - string::size_type jsonEnd = in.find("}"); - - if (jsonEnd != string::npos ) - { - out = in; - in = ""; - return TC_EpollServer::PACKET_FULL; //返回1表示收到的包已ç»å®Œå…¨ - } - - return TC_EpollServer::PACKET_ERR; //返回-1表示收到包å议错误,框架会自动关闭当å‰è¿žæŽ¥ - } -}; - - - -void QueryServer::initialize() -{ - //initialize application here: - //... - vector v_dblist; - vector v_dbcountlist; - - g_pconf->getDomainVector("/tars/countdb", v_dbcountlist); - - size_t iDbNumber = v_dblist.size(); - - size_t iDbCountNumber = v_dbcountlist.size(); - - TLOGDEBUG("QueryServer::initialize stat dbstat size:" << iDbNumber << "|dbcount size:" << iDbCountNumber << endl); - - for(size_t i = 0; i < iDbCountNumber; i++) - { - TC_DBConf tcDBConf; - - string path= "/tars/countdb/" + v_dbcountlist[i]; - - tcDBConf.loadFromMap(g_pconf->getDomainMap(path)); - - _dbStatInfo.push_back(tcDBConf); - } - - _activeDbInfo = _dbStatInfo; - - _dBThread = new DBThread(); - - _dBThread->start(); - - _queryFlag.insert("f_date"); - _queryFlag.insert("f_tflag"); - _queryFlag.insert("master_name"); - _queryFlag.insert("slave_name"); - _queryFlag.insert("slave_ip"); - _queryFlag.insert("interface_name"); - - _insertInterval = TC_Common::strto(g_pconf->get("/tars","5")); - - size_t iDbThreadPoolSize = TC_Common::strto(g_pconf->get("/tars/threadpool","8")); - - size_t iTimeCheckPoolSize = TC_Common::strto(g_pconf->get("/tars/threadpool","8")); - - size_t iQueryDbPoolSize = TC_Common::strto(g_pconf->get("/tars/threadpool","4")); - - _timeCheck.init(iTimeCheckPoolSize); - - _timeCheck.start(); - - _poolDb.init(iDbThreadPoolSize); - - _poolDb.start(); - - _tpoolQueryDb = new QueryDbThread(); - - _tpoolQueryDb->start(iQueryDbPoolSize); - - vector vIpGroup = g_pconf->getDomainKey("/tars/notarsslavename"); - TLOGDEBUG("QueryServer::initialize vIpGroup size:" << vIpGroup.size() << endl); - for (unsigned i = 0; i < vIpGroup.size(); i++) - { - _notTarsSlaveName.insert(vIpGroup[i]); - TLOGDEBUG("QueryServer::initialize i:" << i << "|notarsslavename:" << vIpGroup[i] << endl); - } - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".NoTarsObj"); - addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".NoTarsObj", &JsonProtocol::parse); -} -///////////////////////////////////////////////////////////////// - -void QueryServer::setActiveDb(const vector &vDbInfo) -{ - TC_LockT lock(*this); - _activeDbInfo = vDbInfo; -} - -vector QueryServer::getAllActiveDbInfo() const -{ - TC_LockT lock(*this); - vector vDbInfo; - for(size_t i = 0; i < _activeDbInfo.size(); ++i) - { - vDbInfo.push_back(_activeDbInfo[i]); - } - - return vDbInfo; -} - -vector QueryServer::getDbInfo() const -{ - TC_LockT lock(*this); - return _dbStatInfo; -} - -vector QueryServer::getActiveDbInfo() const -{ - TC_LockT lock(*this); - return _activeDbInfo; -} - -uint32_t QueryServer::genUid() -{ - TC_LockT lock(*this); - - while (++_uniqId == 0); - - return _uniqId; -} - -string QueryServer::dumpDbInfo(const vector& vDbInfo) const -{ - ostringstream os; - - os <::const_iterator it = _queryFlag.find(sKey); - if(it != _queryFlag.end()) - { - return true; - } - return false; -} - -set& QueryServer::getNotTarsSlaveName() -{ - return _notTarsSlaveName; -} - -///////////////////////////////////////////////////////////////// -void -QueryServer::destroyApp() -{ - //destroy application here: - //... - if(_dBThread) - { - delete _dBThread; - _dBThread = NULL; - } - - if(_tpoolQueryDb) - { - delete _tpoolQueryDb; - _tpoolQueryDb = NULL; - } - - bool b = _poolDb.waitForAllDone(1000); - - TLOGDEBUG("QueryServer::destroyApp waitForAllDone _poolDb return:" << b << "|getJobNum:" << _poolDb.getJobNum() << endl); - - _poolDb.stop(); - - b = _timeCheck.waitForAllDone(1000); - - TLOGDEBUG("QueryServer::destroyApp waitForAllDone _timeCheck return:" << b << "|getJobNum:" << _timeCheck.getJobNum() << endl); - - _timeCheck.stop(); -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_pconf = & g_app.getConfig(); - g_app.main(argc, argv); - TarsTimeLogger::getInstance()->enableRemote("inout",false); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/framework/QueryStatServer/QueryServer.h b/cpp/framework/QueryStatServer/QueryServer.h deleted file mode 100644 index 41da8f260..000000000 --- a/cpp/framework/QueryStatServer/QueryServer.h +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _QueryServer_H_ -#define _QueryServer_H_ - -#include -#include -#include "servant/Application.h" -#include "util/tc_thread_mutex.h" -#include "util/tc_lock.h" -#include "util/tc_thread_pool.h" -#include "util/tc_atomic.h" -#include "DbThread.h" -#include "QueryDbThread.h" - -using namespace std; -using namespace tars; - -///////////////////////////////////////////////////////////////////// -class QueryParam -{ -public: - int _run_times; - int _run_result; - TC_Atomic _atomic; - TC_ThreadLock _monitor; - - QueryParam() - : _run_times(0) - , _run_result(0) - , _atomic(0) - {} -}; -///////////////////////////////////////////////////////////////////// -class QueryServer : public Application , public TC_ThreadMutex -{ -public: - /** - * - **/ - virtual ~QueryServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); - - int getInsertInterval() const { return _insertInterval; }; - - size_t getDbNumber() const { return _dbStatInfo.size(); } - - size_t getActiveDbSize() const { return _activeDbInfo.size(); } - - void setActiveDb( const vector &vDbInfo); - - vector getAllActiveDbInfo() const; - - vector getDbInfo() const; - - vector getActiveDbInfo() const; - - TC_DBConf getDbInfo(int iIndex) { return _dbStatInfo[iIndex]; } - - string dumpDbInfo(const vector& vDbInfo) const; - - uint32_t genUid(); - - TC_ThreadPool & getThreadPoolTimeCheck() { return _timeCheck; } - - TC_ThreadPool & getThreadPoolDb() { return _poolDb; } - - QueryDbThread * getThreadPoolQueryDb() { return _tpoolQueryDb; } - - bool searchQueryFlag(const string &sKey); - - //匹é…éžtars被调æœåŠ¡å - set& getNotTarsSlaveName(); - -private: - uint32_t _uniqId; //唯一id - - int _insertInterval; //查询最åŽå…¥åº“时间使用,入库耗时ä¸ä¼šè¶…过入库间隔,å•ä½ä¸ºåˆ†é’Ÿ - - DBThread * _dBThread; //定时检查数æ®åº“实例是å¦å­˜æ´» - - QueryDbThread *_tpoolQueryDb; //用于处ç†æ•°æ®åº“的查询æ“作 - - vector _dbStatInfo; //æ•°æ®åº“ä¿¡æ¯ - - vector _activeDbInfo; //存活的数æ®åº“ä¿¡æ¯ - - set _queryFlag; //æ•°æ®åº“字段 - - TC_ThreadPool _timeCheck; //处ç†timecheckæ“作的线程池 - - TC_ThreadPool _poolDb; //具体查询压缩维度åŽçš„æ•°æ®åº“实例数æ®çš„线程池 - - - set _notTarsSlaveName; //匹é…éžtars被调æœåŠ¡å -}; - -extern QueryServer g_app; -extern TC_Config * g_pconf; -//////////////////////////////////////////// -#endif diff --git a/cpp/framework/QueryStatServer/RequestDecoder.cpp b/cpp/framework/QueryStatServer/RequestDecoder.cpp deleted file mode 100644 index 7227067c6..000000000 --- a/cpp/framework/QueryStatServer/RequestDecoder.cpp +++ /dev/null @@ -1,501 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "DbProxy.h" -#include "util/tc_config.h" -#include "QueryServer.h" -#include "RequestDecoder.h" - -static string skey = ""; - - - -RequestDecoder::RequestDecoder(const string& input) -: _input(input) -, _slaveName("") -{ -} -RequestDecoder::~RequestDecoder() -{ - -} - -int RequestDecoder::generateVector(Document &d ,const string s,std::vector &v) -{ - - Value::ConstMemberIterator iter = d.FindMember(s.c_str()); - int i=0; - if (iter == d.MemberEnd()) - { - return 0; - } - else - { - if(iter->value.IsArray()) - { - for (Value::ConstValueIterator itr = iter->value.Begin(); itr != iter->value.End(); ++itr) - { - v.push_back(itr->GetString()); - ++i; - } - } - - else if (iter->value.IsString()) - { - v.push_back(iter->value.GetString()); - i=1; - } - - else - { - return 0; - } - - return i; - } - -} -int RequestDecoder::composeSqlPartition() -{ - vector vConditions; - int iRet=0; - - iRet=generateVector(_req,"filter",vConditions); - - if(iRet==0) - { - return -1; - } - - - string date1,date2; - - int ret = find_field(vConditions, "f_date", date1); - date1 = TC_Common::trim(date1, " ''"); - date1 = TC_Common::trim(date1, " "); - if (ret == EQ || ret ==-1) - { - - date2 = date1; - } - else //NOT EQ, find again - { - ret = find_field(vConditions, "f_date", date2); - date2 = TC_Common::trim(date2, " ''"); - date2 = TC_Common::trim(date2, " "); - if (date1 < date2) - { - - } - else - { - date1.swap(date2); - } - } - - _sql["date1"] = date1; - _sql["date2"] = date2; - //get date end - - //get hour - string tflag1,tflag2; - ret = find_field(vConditions, "f_tflag", tflag1); - tflag1 = TC_Common::trim(tflag1, " \"'"); - if (ret == EQ || ret == -1) - { - - tflag2 = tflag1; - } - else //NOT EQ, find again - { - ret = find_field(vConditions, "f_tflag", tflag2); - tflag2 = TC_Common::trim(tflag2, " \"'"); - if (tflag1 < tflag2) - { - - } - else - { - tflag1.swap(tflag2); - } - } - - _sql["tflag1"] = tflag1; - _sql["tflag2"] = tflag2; - //get hour end - - iRet=generateVector(_req,"filter",vConditions); - if (!vConditions.empty()) - { - string whereCond = ""; - vector::iterator it = vConditions.begin(); - while(it != vConditions.end()) - { - string &sTmp = *it; - string::size_type pos = sTmp.find("slave_name"); - if(pos != string::npos) - { - _slaveName = *it; - } - - whereCond += (whereCond.empty()?"":" and ") + *it ; - it++; - } - - string sWhere(""); - string::size_type ipos = whereCond.find("and istars="); - - if(ipos != string::npos) - { - - string spre = whereCond.substr(0, ipos); - - string snext = whereCond.substr(ipos+9); - - string::size_type ipos1 = snext.find("and"); - - if(ipos1 != string::npos) - { - string s = snext.substr(ipos1); - sWhere = spre; - sWhere += s; - - string sistars = whereCond.substr(ipos+4, (ipos + 9) + ipos1 - (ipos + 4)); - - string value(""); - - int flag = parseCondition(sistars, value); - - TLOGDEBUG("RequestDecoder::composeSqlPartition istars set|sistars:" << sistars << "|flag:" << flag << "|ipos:" << ipos << "|ipos1:" << ipos1 << "|snext:" << snext << endl); - - if(flag == EQ) - { - value = TC_Common::trim(value, " \"'"); - _sql["istars"] = value; - } - else if(flag == NE) - { - value = TC_Common::trim(value, " \"'"); - int iFlag = TC_Common::strto(value); - iFlag = !iFlag; - value = TC_Common::tostr(iFlag); - _sql["istars"] = value; - } - } - else - { - sWhere = spre; - - string sistars = whereCond.substr(ipos+4); - - string value(""); - - int flag = parseCondition(sistars, value); - - if(flag == EQ) - { - value = TC_Common::trim(value, " \"'"); - _sql["istars"] = value; - } - else if(flag == NE) - { - value = TC_Common::trim(value, " \"'"); - int iFlag = TC_Common::strto(value); - iFlag = !iFlag; - value = TC_Common::tostr(iFlag); - _sql["istars"] = value; - } - } - } - else - { - sWhere = whereCond; - } - - _sql["whereCond"] = " where " + sWhere; - - - } - - if(_slaveName != "") - { - string::size_type pos1 = _slaveName.find("'"); - if(pos1 != string::npos) - { - string::size_type pos2 = _slaveName.rfind("'"); - if(pos2 != pos1 && pos2 != string::npos) - { - _slaveName = _slaveName.substr(pos1, (pos2 - pos1 -1)); - } - } - } - - - - vConditions.clear(); - iRet=generateVector(_req,"groupby",vConditions); - - if (!vConditions.empty()) - { - string groupCond = ""; - string groupField = ""; - vector::iterator it = vConditions.begin(); - while(it != vConditions.end()) - { - groupCond += (groupCond.empty()?"":", ") + *it; - groupField += (groupField.empty()?"":", ") + *it; - it++; - } - _sql["groupCond"] = " group by " + groupCond; - _sql["groupField"] = groupField; - } - - string sumField = ""; - vConditions.clear(); - iRet=generateVector(_req,"indexs",vConditions); - if (vConditions.empty()) - { - sumField = " sum(succ_count) "; - } - else - { - vector::iterator it = vConditions.begin(); - while(it != vConditions.end()) - { - sumField += string((sumField.empty()?"":", ")) + " sum(" + *it + ")" ; - it++; - } - } - - _sql["sumField"]= sumField; - return 0; -} - -int RequestDecoder::parseCondition(const string& sCond, string& value) -{ - string::size_type pos =0; - pos= sCond.find(">="); - if (pos != string::npos ) - { - value = sCond.substr(pos+2); - return GE; - } - - pos = sCond.find("<="); - if (pos != string::npos ) - { - value = sCond.substr(pos+2); - return LE; - } - - pos = sCond.find("!="); - if (pos != string::npos ) - { - value = sCond.substr(pos+2); - return NE; - } - pos = sCond.find("<"); - if (pos != string::npos ) - { - value = sCond.substr(pos+1); - return LT; - } - - pos = sCond.find(">"); - if (pos != string::npos ) - { - value = sCond.substr(pos+1); - return GT; - } - - pos = sCond.find("="); - if (pos != string::npos ) - { - value = sCond.substr(pos+1); - return EQ; - } - return 0; -} - -int RequestDecoder::find_field(vector& vCond, const string& field /*f_tflag, f_date*/, string& value ) -{ - vector::iterator it = vCond.begin(); - while(it != vCond.end()){ - - string::size_type pos = it->find(field); - - if(pos != string::npos) - { - string temp = *it; - vCond.erase(it); - return parseCondition(temp, value); - } - it++; - } - return -1; -} - -/* -name : indexs, filter, groupby -*/ -int RequestDecoder::decodeDataid() -{ - - string::size_type pos = _input.find("dataid"); - if (pos == string::npos) - { - return -1; - } - string::size_type endpos = _input.find(",", pos); - _sql["dataid"] = _input.substr(pos + 9, endpos - pos -10); - LOG->debug() << "decodeDataid: " << _sql["dataid"] <debug() << "decodeMethod: " << _sql["method"] < &vFields) -{ - - string::size_type pos1 = arr.find_first_of("["); - if(pos1 == string::npos) - { - LOG->debug() << ("paramh '" + arr + "' is invalid!" ); - return -1; - } - string::size_type pos2 = arr.find_first_of("]"); - if(pos2 == string::npos) - { - LOG->debug() << ("paramh '" + arr + "' is invalid!" ); - return -1; - } - vFields = TC_Common::sepstr(arr.substr(pos1, pos2 -pos1-1), ","); - vector::iterator it = vFields.begin(); - while(it != vFields.end()) - { - - LOG->debug() << "indexs bt " << *it << endl; - string s = TC_Common::trim(*it, "[]\"\""); - *it = s; - LOG->debug() << "indexs at " << *it << endl; - it++; - } - return 0; - - -} - - -map& RequestDecoder::getSql() -{ - return _sql; -} - - -int RequestDecoder::decode() -{ - - _req.Parse(_input.c_str()); - if (_req.HasParseError()) { - TLOGERROR("Decode error:"<<_req.GetParseError()<<"|"<<_input); - return -1; - } - - try - { - vector vConditions ; - generateVector(_req,"method",vConditions); - if(vConditions.size()) - { - _sql["method"] =vConditions[0]; - TLOGDEBUG("Decode"<<_sql["method"]<error() << "RequestDecoder::decode exception:"<< ex.what() << endl; - return -1; - } - return -1; -} - -//ä¼ å…¥sUid,供打å°ä½¿ç”¨ -int RequestDecoder::addUid(const string& sUid) -{ - _sql["uid"] = sUid; - return 0; -} - -string RequestDecoder::getLine(const char** ppChar) -{ - string sTmp; - - sTmp.reserve(512); - - while((**ppChar) != '\r' && (**ppChar) != '\n' && (**ppChar) != '\0') - { - sTmp.append(1, (**ppChar)); - (*ppChar)++; - } - - if((**ppChar) == '\r') - { - (*ppChar)++; /* pass the char '\n' */ - } - - (*ppChar)++; - - return sTmp; -} - diff --git a/cpp/framework/QueryStatServer/RequestDecoder.h b/cpp/framework/QueryStatServer/RequestDecoder.h deleted file mode 100644 index f21cdf01b..000000000 --- a/cpp/framework/QueryStatServer/RequestDecoder.h +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REQUEST_DECODE_H__ -#define __REQUEST_DECODE_H__ -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "servant/StatF.h" -#include "util/tc_common.h" -#include "servant/TarsLogger.h" -#include "util/tc_thread.h" -#include "../../thirdparty/rapidjson/include/rapidjson/document.h" -#include "../../thirdparty/rapidjson/include/rapidjson/writer.h" -#include "../../thirdparty/rapidjson/include/rapidjson/stringbuffer.h" - -using namespace rapidjson; - -class RequestDecoder -{ -public: - static string getLine(const char** ppChar); -public: - enum Op - { - SET, - ADD, - SUB, - EQ, - NE, - GT, - LT, - LE, - GE, - LIMIT, - }; - - enum METHOD - { - QUERY = 0, - TIMECHECK, - }; - RequestDecoder(const string& input); - - ~RequestDecoder(); - - int parseCondition(const string& sCond, string& value); - - /** - * 从vCond中解æžå‡ºfield对应的值 - * - */ - int find_field(vector& vCond, const string& field /*f_tflag, f_date*/, string& value ); - - int decode(); - - int decodeDataid(); - - int decodeMethod(); - - int decodeArray(const string& arr, vector &vFields); - - int addUid(const string& sUid); - - map& getSql(); - - int composeSqlPartition(); - - string getQuerySlaveName() { return _slaveName; } - - int generateVector(Document &d ,const string s,std::vector &v); - -private: - //待解æžçš„json请求串 - string _input; - - //解æžjson得到的数æ®åº“查询slave_name - string _slaveName; - - //解æžjson请求串得到的Dom 文件 - Document _req; - - //解æžjson得到的数æ®åº“查询æ¡ä»¶å€¼ - map _sql; -}; -#endif - diff --git a/cpp/framework/README.md b/cpp/framework/README.md deleted file mode 100644 index 3ee18cf6c..000000000 --- a/cpp/framework/README.md +++ /dev/null @@ -1,18 +0,0 @@ -该工程是Tars框架的基础æœåŠ¡ï¼Œæ˜¯æ‰¿è½½TARSå„个语言的æœåŠ¡è¿è¡Œçš„基础框架。 - - -目录å称 |功能 -----------------------|---------------- -sql |创建TARS框架è¿è¡ŒçŽ¯å¢ƒçš„æ•°æ®åº“的脚本和sql工具 -conf |å„个基础æœåŠ¡çš„模版é…ç½® -protocol |定义å„个基础æœåŠ¡å®šä¹‰çš„通信接å£æ–‡ä»¶ -RegistryServer |åå­—æœåŠ¡è·¯ç”± -NodeServer |管ç†æœåŠ¡ -AdminRegistryServer |与å‰å°è¿›è¡Œäº¤äº’的接入管ç†æœåŠ¡ -PatchServer |å‘布æœåŠ¡ -ConfigServer |é…ç½®æœåŠ¡ -LogServer |日志æœåŠ¡ -StatServer |模调数æ®ç»Ÿè®¡æœåŠ¡ -PropertyServer |属性统计æœåŠ¡ -NotifyServer |异常上报统计æœåŠ¡ -deploy |核心基础æœåŠ¡çš„模版é…置和工具脚本 diff --git a/cpp/framework/RegistryServer/CMakeLists.txt b/cpp/framework/RegistryServer/CMakeLists.txt deleted file mode 100644 index cfd1abf89..000000000 --- a/cpp/framework/RegistryServer/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ - -set(MODULE "tarsregistry") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/bin) - -#complice_module(${MODULE} "Registry.tars") -complice_module(${MODULE}) diff --git a/cpp/framework/RegistryServer/CheckNodeThread.cpp b/cpp/framework/RegistryServer/CheckNodeThread.cpp deleted file mode 100644 index dace0d20d..000000000 --- a/cpp/framework/RegistryServer/CheckNodeThread.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "CheckNodeThread.h" - -extern TC_Config * g_pconf; - -CheckNodeThread::CheckNodeThread() -: _terminate(false) -, _nodeTimeout(60) -, _nodeTimeoutInterval(250) -{ -} - -CheckNodeThread::~CheckNodeThread() -{ - if (isAlive()) - { - terminate(); - notifyAll(); - getThreadControl().join(); - } -} - - -int CheckNodeThread::init() -{ - TLOGDEBUG("CheckNodeThread init"<((*g_pconf).get("/tars/reap", "250")); - _nodeTimeout = _nodeTimeout < 15 ? 15 : _nodeTimeout; - - _nodeTimeoutInterval = TC_Common::strto((*g_pconf).get("/tars/reap", "60")); - _nodeTimeoutInterval = _nodeTimeoutInterval < 15 ? 5 : _nodeTimeoutInterval; - - TLOGDEBUG("CheckNodeThread init ok."<getNow(); - //轮询心跳超时的node - if(tNow - tLastCheckNode >= _nodeTimeoutInterval) - { - _db.checkNodeTimeout(_nodeTimeout); - - tLastCheckNode = tNow; - } - - TC_ThreadLock::Lock lock(*this); - timedWait(1000); //ms - } - catch(exception & ex) - { - TLOGERROR("CheckNodeThread exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("CheckNodeThread unknown exception" << endl); - } - } -} - - - diff --git a/cpp/framework/RegistryServer/CheckNodeThread.h b/cpp/framework/RegistryServer/CheckNodeThread.h deleted file mode 100644 index c566ccd98..000000000 --- a/cpp/framework/RegistryServer/CheckNodeThread.h +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __CHECK_NODE_THREAD_H__ -#define __CHECK_NODE_THREAD_H__ - -#include -#include "util/tc_thread.h" -#include "DbHandle.h" - -using namespace tars; - -////////////////////////////////////////////////////// -/** - * 监控tarsnode超时的线程类 - */ -class CheckNodeThread : public TC_Thread, public TC_ThreadLock -{ -public: - - /** - * 构造函数 - */ - CheckNodeThread(); - - /** - * æžæž„函数 - */ - ~CheckNodeThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * åˆå§‹åŒ– - */ - int init(); - - /** - * 轮询函数 - */ - virtual void run(); - -protected: - /* - * 线程结æŸæ ‡å¿— - */ - bool _terminate; - - /* - * æ•°æ®åº“æ“作 - */ - CDbHandle _db; - - /* - * node心跳超时时间 - */ - int _nodeTimeout; - - /* - * 线程检查周期 - */ - int _nodeTimeoutInterval; - -}; - -#endif diff --git a/cpp/framework/RegistryServer/CheckSettingState.cpp b/cpp/framework/RegistryServer/CheckSettingState.cpp deleted file mode 100644 index 517ca6434..000000000 --- a/cpp/framework/RegistryServer/CheckSettingState.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "CheckSettingState.h" -#include "servant/TarsLogger.h" - -extern TC_Config * g_pconf; - -CheckSettingState::CheckSettingState() -: _terminate(false) -, _checkingInterval(10) -, _leastChangedTime(10*60) -{ -} - -CheckSettingState::~CheckSettingState() -{ - if (isAlive()) - { - terminate(); - notifyAll(); - getThreadControl().join(); - } -} - -int CheckSettingState::init() -{ - TLOGDEBUG("CheckSettingStateThread init"<((*g_pconf).get("/tars/reap", "10")); - _checkingInterval = _checkingInterval < 5 ? 5 : _checkingInterval; - - _leastChangedTime = TC_Common::strto((*g_pconf).get("/tars/reap", "600")); - _leastChangedTime = _leastChangedTime < 60 ? 60 : _leastChangedTime; - - TLOGDEBUG("CheckSettingStateThread init ok."<getNow(); - - //核查å„server在nodeçš„è®¾ç½®çŠ¶æ€ - if(tNow - tLastQueryServer >= _checkingInterval) - { - _db.checkSettingState(_leastChangedTime); - - //执行完æˆåŽå†èµ‹å½“å‰æ—¶é—´å€¼ - tLastQueryServer = TC_TimeProvider::getInstance()->getNow(); - } - - TC_ThreadLock::Lock lock(*this); - timedWait(100); //ms - } - catch(exception & ex) - { - TLOGERROR("CheckSettingState exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("CheckSettingState unknown exception." << endl); - } - } -} - diff --git a/cpp/framework/RegistryServer/CheckSettingState.h b/cpp/framework/RegistryServer/CheckSettingState.h deleted file mode 100644 index f1f388801..000000000 --- a/cpp/framework/RegistryServer/CheckSettingState.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __CHECKSETINGSTATE_H__ -#define __CHECKSETINGSTATE_H__ - -#include - -#include "util/tc_thread.h" -#include "Node.h" -#include "DbHandle.h" - -using namespace tars; - -////////////////////////////////////////////////////// -/** - * 用于执行定时æ“作的线程类 - */ -class CheckSettingState : public TC_Thread, public TC_ThreadLock -{ -public: - - /* - * 构造函数 - */ - CheckSettingState(); - - /* - * æžæž„函数 - */ - ~CheckSettingState(); - - /* - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * åˆå§‹åŒ– - */ - int init(); - - /** - * 线程执行函数 - */ - virtual void run(); - -private: - /* - * 线程结æŸæ ‡å¿— - */ - bool _terminate; - - /* - * æ•°æ®åº“æ“作 - */ - CDbHandle _db; - - /* - * 轮询server状æ€çš„间隔时间 - */ - int _checkingInterval; - - /* - * 增é‡æŸ¥è¯¢æœåŠ¡çŠ¶æ€çš„时间,å•ä½æ˜¯ç§’ - */ - int _leastChangedTime; -}; - -#endif diff --git a/cpp/framework/RegistryServer/DbHandle.cpp b/cpp/framework/RegistryServer/DbHandle.cpp deleted file mode 100644 index 2cf80931e..000000000 --- a/cpp/framework/RegistryServer/DbHandle.cpp +++ /dev/null @@ -1,1800 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include -#include "DbHandle.h" -#include "RegistryServer.h" - -TC_ReadersWriterData CDbHandle::_objectsCache; - -TC_ReadersWriterData > CDbHandle::_mapGroupPriority; - -std::map CDbHandle::_mapServantStatus; -TC_ThreadLock CDbHandle::_mapServantStatusLock; - -map CDbHandle::_mapNodePrxCache; -TC_ThreadLock CDbHandle::_NodePrxLock; - -//key-ip, value-ç»„ç¼–å· -TC_ReadersWriterData > CDbHandle::_groupIdMap; -//key-group_name, value-ç»„ç¼–å· -TC_ReadersWriterData > CDbHandle::_groupNameMap; - -TC_ReadersWriterData CDbHandle::_setDivisionCache; - -extern RegistryServer g_app; -extern TC_Config *g_pconf; - -int CDbHandle::init(TC_Config *pconf) -{ - try - { - TC_DBConf tcDBConf; - tcDBConf.loadFromMap(pconf->getDomainMap("/tars/db")); - string option = pconf->get("/tars/db", ""); - if (!option.empty() && option == "CLIENT_MULTI_STATEMENTS") - { - tcDBConf._flag = CLIENT_MULTI_STATEMENTS; - _enMultiSql = true; - TLOGDEBUG("CDbHandle::init tcDBConf._flag: " << option << endl); - } - _mysqlReg.init(tcDBConf); - } - catch (TC_Config_Exception& ex) - { - TLOGERROR("CDbHandle::init exception: " << ex.what() << endl); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::init exception: " << ex.what() << endl); - exit(0); - } - - return 0; -} - -int CDbHandle::registerNode(const string& name, const NodeInfo& ni, const LoadInfo& li) -{ - try - { - string sSelectSql = "select present_state, node_obj, template_name " - "from t_node_info " - "where node_name='" + _mysqlReg.escapeString(name) + "'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSelectSql); - TLOGDEBUG("CDbHandle::init select node affected:" << res.size() << endl); - - string sTemplateName; - if (res.size() != 0) - { - //åˆæ³•æ€§åˆ¤æ–­ï¼Œæ˜¯å¦å­˜åœ¨å字相åŒå·²ç»å­˜æ´»ä½†node objä¸åŒçš„注册节点 - if (res[0]["present_state"] == "active" && res[0]["node_obj"] != ni.nodeObj) - { - TLOGERROR("registery node :" << name << " error, other node has registered." << endl); - return 1; - } - //传递已é…置的模æ¿å - sTemplateName = res[0]["template_name"]; - } - - string sSql = "replace into t_node_info " - " (node_name, node_obj, endpoint_ip, endpoint_port, data_dir, load_avg1, load_avg5, load_avg15," - " last_reg_time, last_heartbeat, setting_state, present_state, tars_version, template_name)" - "values('" + _mysqlReg.escapeString(name) + "', '" + _mysqlReg.escapeString(ni.nodeObj) + "', " - " '" + _mysqlReg.escapeString(ni.endpointIp) + "'," - " '" + tars::TC_Common::tostr(ni.endpointPort) + "'," - " '" + _mysqlReg.escapeString(ni.dataDir) + "','" + tars::TC_Common::tostr(li.avg1) + "'," - " '" + tars::TC_Common::tostr(li.avg5) + "'," - " '" + tars::TC_Common::tostr(li.avg15) + "', now(), now(), 'active', 'active', " + - " '" + _mysqlReg.escapeString(ni.version) + "', '" + _mysqlReg.escapeString(sTemplateName) + "')"; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("registry node :" << name << " affected:" << _mysqlReg.getAffectedRows() << endl); - - NodePrx nodePrx; - g_app.getCommunicator()->stringToProxy(ni.nodeObj, nodePrx); - - TC_ThreadLock::Lock lock(_NodePrxLock); - _mapNodePrxCache[name] = nodePrx; - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::init " << name << " exception: " << ex.what() << endl); - return 2; - } - catch (tars::TarsException& ex) - { - TLOGERROR("CDbHandle::init " << name << " exception: " << ex.what() << endl); - return 3; - } - - return 0; -} - -int CDbHandle::destroyNode(const string& name) -{ - try - { - string sSql = "update t_node_info as node " - "left join t_server_conf as server using (node_name) " - "set node.present_state = 'inactive', server.present_state='inactive' " - "where node.node_name='" + _mysqlReg.escapeString(name) + "'"; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("CDbHandle::destroyNode " << name << " affected:" << _mysqlReg.getAffectedRows() << endl); - - TC_ThreadLock::Lock lock(_NodePrxLock); - _mapNodePrxCache.erase(name); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::destroyNode " << name << " exception: " << ex.what() << endl); - } - - return 0; -} - - -int CDbHandle::keepAlive(const string& name, const LoadInfo& li) -{ - try - { - int64_t iStart = TNOWMS; - string sSql = "update t_node_info " - "set last_heartbeat=now(), present_state='active'," - " load_avg1=" + tars::TC_Common::tostr(li.avg1) + "," - " load_avg5=" + tars::TC_Common::tostr(li.avg5) + "," - " load_avg15=" + tars::TC_Common::tostr(li.avg15) + " " - "where node_name='" + _mysqlReg.escapeString(name) + "'"; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("CDbHandle::keepAlive " << name << " affected:" << _mysqlReg.getAffectedRows() << "|cost:" << (TNOWMS - iStart) << endl); - - if (_mysqlReg.getAffectedRows() == 0) - { - return 1; - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::keepAlive " << name << " exception: " << ex.what() << endl); - return 2; - } - - return 0; -} - -map CDbHandle::getActiveNodeList(string& result) -{ - map mapNodeList; - try - { - string sSql = "select node_name, node_obj from t_node_info " - "where present_state='active'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - - TLOGDEBUG("CDbHandle::getActiveNodeList (present_state='active') affected:" << res.size() << endl); - - for (unsigned i = 0; i < res.size(); i++) - { - mapNodeList[res[i]["node_name"]] = res[i]["node_obj"]; - } - } - catch (TC_Mysql_Exception& ex) - { - result = string(__FUNCTION__) + " exception: " + ex.what(); - TLOGERROR(result << endl); - return mapNodeList; - } - - return mapNodeList; -} - -int CDbHandle::getNodeVersion(const string& nodeName, string& version, string& result) -{ - try - { - string sSql = "select tars_version from t_node_info " - "where node_name='" + _mysqlReg.escapeString(nodeName) + "'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - - TLOGDEBUG("CDbHandle::getNodeVersion (node_name='" << nodeName << "') affected:" << res.size() << endl); - - if (res.size() > 0) - { - version = res[0]["tars_version"]; - return 0; - - } - result = "node_name(" + nodeName + ") int table t_node_info not exist"; - } - catch (TC_Mysql_Exception& ex) - { - - TLOGERROR("CDbHandle::getNodeVersion exception:"< CDbHandle::getServers(const string& app, const string& serverName, const string& nodeName, bool withDnsServer) -{ - string sSql; - vector vServers; - unsigned num = 0; - int64_t iStart = TNOWMS; - - try - { - //server详细é…ç½® - string sCondition; - sCondition += "server.node_name='" + _mysqlReg.escapeString(nodeName) + "'"; - if (app != "") sCondition += " and server.application='" + _mysqlReg.escapeString(app) + "' "; - if (serverName != "") sCondition += " and server.server_name='" + _mysqlReg.escapeString(serverName) + "' "; - if (withDnsServer == false) sCondition += " and server.server_type !='tars_dns' "; //ä¸èŽ·å–dnsæœåŠ¡ - - sSql = "select server.application, server.server_name, server.node_name, base_path, " - " exe_path, setting_state, present_state, adapter_name, thread_num, async_thread_num, endpoint," - " profile,template_name, " - " allow_ip, max_connections, servant, queuecap, queuetimeout,protocol,handlegroup," - " patch_version, patch_time, patch_user, " - " server_type, start_script_path, stop_script_path, monitor_script_path,config_center_port ," - " enable_set, set_name, set_area, set_group " - "from t_server_conf as server " - " left join t_adapter_conf as adapter using(application, server_name, node_name) " - "where " + sCondition; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - num = res.size(); - //对应server在vector的下标 - map mapAppServerTemp; - - //获å–模版profile内容 - map mapProfile; - - //分拆数æ®åˆ°serverçš„ä¿¡æ¯ç»“构里 - for (unsigned i = 0; i < res.size(); i++) - { - string sServerId = res[i]["application"] + "." + res[i]["server_name"] - + "_" + res[i]["node_name"]; - - if (mapAppServerTemp.find(sServerId) == mapAppServerTemp.end()) - { - //serveré…ç½® - ServerDescriptor server; - server.application = res[i]["application"]; - server.serverName = res[i]["server_name"]; - server.nodeName = res[i]["node_name"]; - server.basePath = res[i]["base_path"]; - server.exePath = res[i]["exe_path"]; - server.settingState = res[i]["setting_state"]; - server.presentState = res[i]["present_state"]; - server.patchVersion = res[i]["patch_version"]; - server.patchTime = res[i]["patch_time"]; - server.patchUser = res[i]["patch_user"]; - server.profile = res[i]["profile"]; - server.serverType = res[i]["server_type"]; - server.startScript = res[i]["start_script_path"]; - server.stopScript = res[i]["stop_script_path"]; - server.monitorScript = res[i]["monitor_script_path"]; - server.configCenterPort = TC_Common::strto(res[i]["config_center_port"]); - - server.setId = ""; - if (TC_Common::lower(res[i]["enable_set"]) == "y") - { - server.setId = res[i]["set_name"] + "." + res[i]["set_area"] + "." + res[i]["set_group"]; - } - - //获å–父模版profile内容 - if (mapProfile.find(res[i]["template_name"]) == mapProfile.end()) - { - string sResult; - mapProfile[res[i]["template_name"]] = getProfileTemplate(res[i]["template_name"], sResult); - } - - TC_Config tParent, tProfile; - tParent.parseString(mapProfile[res[i]["template_name"]]); - tProfile.parseString(server.profile); - - int iDefaultAsyncThreadNum = 3; - - if("tars_nodejs" == server.serverType) - { - //tars_nodejs类型的业务需è¦è®¾ç½®è¿™ä¸ªå€¼ä¸º0 - iDefaultAsyncThreadNum = 0; - } - - int iConfigAsyncThreadNum = TC_Common::strto(TC_Common::trim(res[i]["async_thread_num"])); - iDefaultAsyncThreadNum = iConfigAsyncThreadNum > iDefaultAsyncThreadNum ? iConfigAsyncThreadNum : iDefaultAsyncThreadNum; - server.asyncThreadNum = TC_Common::strto(tProfile.get("/tars/application/client", TC_Common::tostr(iDefaultAsyncThreadNum))); - tParent.joinConfig(tProfile, true); - server.profile = tParent.tostr(); - - mapAppServerTemp[sServerId] = vServers.size(); - vServers.push_back(server); - } - - //adapteré…ç½® - AdapterDescriptor adapter; - adapter.adapterName = res[i]["adapter_name"]; - if (adapter.adapterName == "") - { - //adapter没é…置,left join åŽä¸º NULL,ä¸æ”¾åˆ°adapters map - continue; - } - - adapter.threadNum = res[i]["thread_num"]; - adapter.endpoint = res[i]["endpoint"]; - adapter.maxConnections = TC_Common::strto(res[i]["max_connections"]); - adapter.allowIp = res[i]["allow_ip"]; - adapter.servant = res[i]["servant"]; - adapter.queuecap = TC_Common::strto(res[i]["queuecap"]); - adapter.queuetimeout = TC_Common::strto(res[i]["queuetimeout"]); - adapter.protocol = res[i]["protocol"]; - adapter.handlegroup = res[i]["handlegroup"]; - - vServers[mapAppServerTemp[sServerId]].adapters[adapter.adapterName] = adapter; - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::getServers " << app << "." << serverName << "_" << nodeName - << " exception: " << ex.what() << "|" << sSql << endl); - return vServers; - } - catch (TC_Config_Exception& ex) - { - TLOGERROR("CDbHandle::getServers " << app << "." << serverName << "_" << nodeName - << " TC_Config_Exception exception: " << ex.what() << endl); - throw TarsException(string("TC_Config_Exception exception: ") + ex.what()); - } - - TLOGDEBUG(app << "." << serverName << "_" << nodeName << " getServers affected:" << num - << "|cost:" << (TNOWMS - iStart) << endl); - - return vServers; - -} - -string CDbHandle::getProfileTemplate(const string& sTemplateName, string& sResultDesc) -{ - map mapRecursion; - return getProfileTemplate(sTemplateName, mapRecursion, sResultDesc); -} - -string CDbHandle::getProfileTemplate(const string& sTemplateName, map& mapRecursion, string& sResultDesc) -{ - try - { - string sSql = "select template_name, parents_name, profile from t_profile_template " - "where template_name='" + _mysqlReg.escapeString(sTemplateName) + "'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - - if (res.size() == 0) - { - sResultDesc += "(" + sTemplateName + ":template not found)"; - return ""; - } - - TC_Config confMyself, confParents; - confMyself.parseString(res[0]["profile"]); - //mapRecursion用于é¿å…é‡å¤ç»§æ‰¿ - mapRecursion[res[0]["template_name"]] = 1; - - if (res[0]["parents_name"] != "" && mapRecursion.find(res[0]["parents_name"]) == mapRecursion.end()) - { - confParents.parseString(getProfileTemplate(res[0]["parents_name"], mapRecursion, sResultDesc)); - confMyself.joinConfig(confParents, false); - } - sResultDesc += "(" + sTemplateName + ":OK)"; - - TLOGDEBUG("CDbHandle::getProfileTemplate " << sTemplateName << " " << sResultDesc << endl); - - return confMyself.tostr(); - } - catch (TC_Mysql_Exception& ex) - { - sResultDesc += "(" + sTemplateName + ":" + ex.what() + ")"; - TLOGERROR("CDbHandle::getProfileTemplate exception: " << ex.what() << endl); - } - catch (TC_Config_Exception& ex) - { - sResultDesc += "(" + sTemplateName + ":" + ex.what() + ")"; - TLOGERROR("CDbHandle::getProfileTemplate TC_Config_Exception exception: " << ex.what() << endl); - } - - return ""; -} - -vector CDbHandle::getAllApplicationNames(string& result) -{ - vector vApps; - try - { - string sSql = "select distinct application from t_server_conf"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - TLOGDEBUG("CDbHandle::getAllApplicationNames affected:" << res.size() << endl); - - for (unsigned i = 0; i < res.size(); i++) - { - vApps.push_back(res[i]["application"]); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::getAllApplicationNames exception: " << ex.what() << endl); - return vApps; - } - - return vApps; -} - -vector > CDbHandle::getAllServerIds(string& result) -{ - vector > vServers; - try - { - string sSql = "select application, server_name, node_name, setting_state, present_state,server_type from t_server_conf"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - TLOGDEBUG("CDbHandle::getAllServerIds affected:" << res.size() << endl); - - for (unsigned i = 0; i < res.size(); i++) - { - vector server; - server.push_back(res[i]["application"] + "." + res[i]["server_name"] + "_" + res[i]["node_name"]); - server.push_back(res[i]["setting_state"]); - server.push_back(res[i]["present_state"]); - server.push_back(res[i]["server_type"]); - vServers.push_back(server); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::getAllServerIds exception: " << ex.what() << endl); - return vServers; - } - - return vServers; - -} -int CDbHandle::updateServerState(const string& app, const string& serverName, const string& nodeName, const string& stateFields, tars::ServerState state, int processId) -{ - try - { - int64_t iStart = TNOWMS; - if (stateFields != "setting_state" && stateFields != "present_state") - { - TLOGDEBUG(app << "." << serverName << "_" << nodeName - << " not supported fields:" << stateFields << endl); - return -1; - } - - string sProcessIdSql = (stateFields == "present_state" ? - (", process_id = " + TC_Common::tostr(processId) + " ") : ""); - - string sSql = "update t_server_conf " - "set " + stateFields + " = '" + etos(state) + "' " + sProcessIdSql + - "where application='" + _mysqlReg.escapeString(app) + "' " - " and server_name='" + _mysqlReg.escapeString(serverName) + "' " - " and node_name='" + _mysqlReg.escapeString(nodeName) + "' "; - - _mysqlReg.execute(sSql); - TLOGDEBUG("CDbHandle::updateServerState " << app << "." << serverName << "_" << nodeName - << " affected:" << _mysqlReg.getAffectedRows() - << "|cost:" << (TNOWMS - iStart) << endl); - - { - TC_ThreadLock::Lock lock(_mapServantStatusLock); - ServantStatusKey statusKey = { app, serverName, nodeName }; - _mapServantStatus[statusKey] = static_cast(state); - } - return 0; - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::updateServerState " << app << "." << serverName << "_" << nodeName - << " exception: " << ex.what() << endl); - return -1; - } -} - -int CDbHandle::updateServerStateBatch(const std::vector& vecStateInfo) -{ - const size_t sizeStep = 1000; - - for (std::vector::size_type i = 0; i < vecStateInfo.size();) - { - int iEnd = (i + sizeStep >= vecStateInfo.size()) ? vecStateInfo.size() : (i + sizeStep); - if (doUpdateServerStateBatch(vecStateInfo, i, iEnd) != 0) return -1; - i = iEnd; - } - - return 0; -} - -int CDbHandle::doUpdateServerStateBatch(const std::vector& vecStateInfo, const size_t sizeBegin, const size_t sizeEnd) -{ - std::map > > map_sort; - - { - TC_ThreadLock::Lock lock(_mapServantStatusLock); - for (std::vector::size_type i = sizeBegin; i < sizeEnd; i++) - { - ServantStatusKey statusKey = { vecStateInfo[i].application, vecStateInfo[i].serverName, vecStateInfo[i].nodeName }; - std::map::iterator it = _mapServantStatus.find(statusKey); - if (it != _mapServantStatus.end() && it->second == static_cast(vecStateInfo[i].serverState)) continue; - - map_sort[vecStateInfo[i].application][vecStateInfo[i].serverName].push_back(i); - } - } - - if (map_sort.empty()) - { - TLOGDEBUG("CDbHandle::doUpdateServerStateBatch vector size(" << vecStateInfo.size() << ") do nothing within the same state in cache" << endl); - return 0; - } - - std::string sCommand; - //新更新的状æ€ï¼Œç­‰è¯­å¥æ›´æ–°æˆåŠŸåŽèµ‹å€¼ - std::map updated_map; - try - { - int64_t iStart = TC_TimeProvider::getInstance()->getNowMs(); - - if (_enMultiSql) - { - std::string sPrefix = "UPDATE t_server_conf SET present_state='"; - for (std::map > >::iterator it_app = map_sort.begin(); it_app != map_sort.end(); it_app++) - { - for (std::map >::iterator it_svr = it_app->second.begin(); it_svr != it_app->second.end(); it_svr++) - { - for (std::vector::size_type i = 0; i < it_svr->second.size(); i++) - { - sCommand += (sCommand == "" ? "" : ";") + sPrefix + etos(vecStateInfo[it_svr->second[i]].serverState) - + "', process_id= " + TC_Common::tostr(vecStateInfo[it_svr->second[i]].processId) - + " WHERE application='" + _mysqlReg.escapeString(it_app->first) - + "' AND server_name='" + _mysqlReg.escapeString(it_svr->first) - + "' AND node_name='" + _mysqlReg.escapeString(vecStateInfo[it_svr->second[i]].nodeName) + "'"; - - ServantStatusKey statusKey = { it_app->first, it_svr->first, vecStateInfo[it_svr->second[i]].nodeName }; - updated_map[statusKey] = vecStateInfo[it_svr->second[i]].serverState; - } - } - } - } - else - { - std::string sPidCommand, sPreCommand; - for (std::map > >::iterator it_app = map_sort.begin(); it_app != map_sort.end(); it_app++) - { - sPidCommand += " WHEN '" + it_app->first + "' THEN CASE server_name "; - sPreCommand += " WHEN '" + it_app->first + "' THEN CASE server_name "; - - for (std::map >::iterator it_svr = it_app->second.begin(); it_svr != it_app->second.end(); it_svr++) - { - sPidCommand += " WHEN '" + it_svr->first + "' THEN CASE node_name "; - sPreCommand += " WHEN '" + it_svr->first + "' THEN CASE node_name "; - - for (std::vector::size_type i = 0; i < it_svr->second.size(); i++) - { - sPidCommand += " WHEN '" + _mysqlReg.escapeString(vecStateInfo[it_svr->second[i]].nodeName) + "' THEN " + TC_Common::tostr(vecStateInfo[it_svr->second[i]].processId); - sPreCommand += " WHEN '" + _mysqlReg.escapeString(vecStateInfo[it_svr->second[i]].nodeName) + "' THEN '" + etos(vecStateInfo[it_svr->second[i]].serverState) + "'"; - - ServantStatusKey statusKey = { it_app->first, it_svr->first, vecStateInfo[it_svr->second[i]].nodeName }; - updated_map[statusKey] = vecStateInfo[it_svr->second[i]].serverState; - - } - - sPidCommand += " ELSE process_id END"; - sPreCommand += " ELSE present_state END"; - } - - sPidCommand += " ELSE process_id END"; - sPreCommand += " ELSE present_state END"; - } - sCommand = "UPDATE t_server_conf SET process_id= CASE application " + sPidCommand + " ELSE process_id END, present_state= CASE application " + sPreCommand + " ELSE present_state END"; - } - - if (!sCommand.empty()) - { - _mysqlReg.execute(sCommand); - int iRows = 0; - if (_enMultiSql) - { - for (iRows = mysql_affected_rows(_mysqlReg.getMysql()); !mysql_next_result(_mysqlReg.getMysql()); - iRows += mysql_affected_rows(_mysqlReg.getMysql())) ; - } - else - { - iRows = mysql_affected_rows(_mysqlReg.getMysql()); - } - - if (iRows > 0) - { - TLOGDEBUG("CDbHandle::doUpdateServerStateBatch sql: " << sCommand << " vector:" << vecStateInfo.size() << " affected:" << iRows - << "|cost:" << (TNOWMS - iStart) << endl); - - TC_ThreadLock::Lock lock(_mapServantStatusLock); - _mapServantStatus.insert(updated_map.begin(), updated_map.end()); - } - } - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::doUpdateServerStateBatch exception: " << ex.what() << " sql:" << sCommand << endl); - return -1; - } - catch (exception& ex) - { - TLOGERROR("CDbHandle::doUpdateServerStateBatch " << ex.what() << endl); - return -1; - } - return -1; -} - -int CDbHandle::setPatchInfo(const string& app, const string& serverName, const string& nodeName, const string& version, const string& user) -{ - try - { - string sSql = "update t_server_conf " - "set patch_version = '" + _mysqlReg.escapeString(version) + "', " - " patch_user = '" + _mysqlReg.escapeString(user) + "', " - " patch_time = now() " - "where application='" + _mysqlReg.escapeString(app) + "' " - " and server_name='" + _mysqlReg.escapeString(serverName) + "' " - " and node_name='" + _mysqlReg.escapeString(nodeName) + "' "; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("CDbHandle::setPatchInfo " << app << "." << serverName << "_" << nodeName << " affected:" << _mysqlReg.getAffectedRows() << endl); - - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::setPatchInfo " << app << "." << serverName << "_" << nodeName << " exception: " << ex.what() << endl); - return -1; - } -} - -int CDbHandle::setServerTarsVersion(const string& app, const string& serverName, const string& nodeName, const string& version) -{ - try - { - - int64_t iStart = TNOWMS; - string sSql = "update t_server_conf " - "set tars_version = '" + _mysqlReg.escapeString(version) + "' " - "where application='" + _mysqlReg.escapeString(app) + "' " - " and server_name='" + _mysqlReg.escapeString(serverName) + "' " - " and node_name='" + _mysqlReg.escapeString(nodeName) + "' "; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("CDbHandle::setServerTarsVersion " << app << "." << serverName << "_" << nodeName - << " affected:" << _mysqlReg.getAffectedRows() - << "|cost:" << (TNOWMS - iStart) << endl); - - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::setServerTarsVersion " << app << "." << serverName << "_" << nodeName << " exception: " << ex.what() << endl); - return -1; - } -} - -NodePrx CDbHandle::getNodePrx(const string& nodeName) -{ - try - { - TC_ThreadLock::Lock lock(_NodePrxLock); - - if (_mapNodePrxCache.find(nodeName) != _mapNodePrxCache.end()) - { - return _mapNodePrxCache[nodeName]; - } - - string sSql = "select node_obj " - "from t_node_info " - "where node_name='" + _mysqlReg.escapeString(nodeName) + "' and present_state='active'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - TLOGDEBUG("CDbHandle::getNodePrx '" << nodeName << "' affected:" << res.size() << endl); - - if (res.size() == 0) - { - throw Tars("node '" + nodeName + "' not registered or heartbeart timeout,please check for it"); - } - - NodePrx nodePrx; - g_app.getCommunicator()->stringToProxy(res[0]["node_obj"], nodePrx); - - _mapNodePrxCache[nodeName] = nodePrx; - - return nodePrx; - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::getNodePrx " << nodeName << " exception: " << ex.what() << endl); - throw Tars(string("get node record from db error:") + ex.what()); - } - catch (tars::TarsException& ex) - { - TLOGERROR("CDbHandle::getNodePrx " << nodeName << " exception: " << ex.what() << endl); - throw ex; - } - -} - -int CDbHandle::checkNodeTimeout(unsigned uTimeout) -{ - try - { - //这里先检查下,记录下有哪些节点超时了,方便定ä½é—®é¢˜ - { - string sTmpSql = "select node_name from t_node_info where last_heartbeat < date_sub(now(), INTERVAL " + tars::TC_Common::tostr(uTimeout) + " SECOND)"; - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sTmpSql); - if (res.size() > 0) - { - TLOGDEBUG( "CDbHandle::checkNodeTimeout affected:" << tars::TC_Common::tostr(res.data()) << endl); - } - } - - int64_t iStart = TNOWMS; - - string sSql = "update t_node_info as node " - " left join t_server_conf as server using (node_name) " - "set node.present_state='inactive', server.present_state='inactive', server.process_id=0 " - "where last_heartbeat < date_sub(now(), INTERVAL " + tars::TC_Common::tostr(uTimeout) + " SECOND)"; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("CDbHandle::checkNodeTimeout (" << uTimeout << "s) affected:" << _mysqlReg.getAffectedRows() << "|cost:" << (TNOWMS - iStart) << endl); - - return _mysqlReg.getAffectedRows(); - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::checkNodeTimeout exception: " << ex.what() << endl); - return -1; - } - -} - -int CDbHandle::checkRegistryTimeout(unsigned uTimeout) -{ - try - { - string sSql = "update t_registry_info " - "set present_state='inactive' " - "where last_heartbeat < date_sub(now(), INTERVAL " + tars::TC_Common::tostr(uTimeout) + " SECOND)"; - - _mysqlReg.execute(sSql); - - TLOGDEBUG("CDbHandle::checkRegistryTimeout (" << uTimeout << "s) affected:" << _mysqlReg.getAffectedRows() << endl); - - return _mysqlReg.getAffectedRows(); - - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::checkRegistryTimeout exception: " << ex.what() << endl); - return -1; - } - -} - -int CDbHandle::checkSettingState(const int iCheckLeastChangedTime) -{ - try - { - TLOGDEBUG("CDbHandle::checkSettingState ____________________________________" << endl); - - string sSql = "select application, server_name, node_name, setting_state " - "from t_server_conf " - "where setting_state='active' " //检查应当å¯åŠ¨çš„ - "and server_type != 'tars_dns'" //仅用æ¥æä¾›dnsæœåŠ¡çš„除外 - "and registry_timestamp >='" + TC_Common::tm2str(TC_TimeProvider::getInstance()->getNow() - iCheckLeastChangedTime) + "'"; - - int64_t iStart = TNOWMS; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - - TLOGDEBUG("CDbHandle::checkSettingState setting_state='active' affected:" << res.size() << "|cost:" << (TNOWMS - iStart) << endl); - - NodePrx nodePrx; - for (unsigned i = 0; i < res.size(); i++) - { - string sResult; - TLOGDEBUG("checking [" << i << "]: " << res[i]["application"] << "." << res[i]["server_name"] << "_" << res[i]["node_name"] << endl); - try - { - nodePrx = getNodePrx(res[i]["node_name"]); - if (nodePrx) - { - if (nodePrx->getSettingState(res[i]["application"], res[i]["server_name"], sResult) != Active) - { - string sTempSql = "select application, server_name, node_name, setting_state " - "from t_server_conf " - "where setting_state='active' " - "and application = '" + res[i]["application"] + "' " - "and server_name = '" + res[i]["server_name"] + "' " - "and node_name = '" + res[i]["node_name"] + "'"; - - if (_mysqlReg.queryRecord(sTempSql).size() == 0) - { - TLOGDEBUG(res[i]["application"] << "." << res[i]["server_name"] << "_" << res[i]["node_name"] - << " not setting active,and not need restart" << endl); - continue; - } - - TLOGDEBUG(res[i]["application"] << "." << res[i]["server_name"] << "_" << res[i]["node_name"] << " not setting active, start it" << endl); - - int iRet = nodePrx->startServer(res[i]["application"], res[i]["server_name"], sResult); - - TLOGDEBUG("startServer ret=" << iRet << ",result=" << sResult << endl); - } - } - } - - catch (tars::TarsException& ex) - { - TLOGERROR("checking " << res[i]["application"] << "." << res[i]["server_name"] << "_" << res[i]["node_name"] - << "' exception: " << ex.what() << endl); - } - catch (exception& ex) - { - TLOGERROR("checking " << res[i]["application"] << "." << res[i]["server_name"] << "_" << res[i]["node_name"] << "' exception: " << ex.what() << endl); - } - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::checkSettingState exception: " << ex.what() << endl); - return -1; - } - TLOGDEBUG("CDbHandle::checkSettingState ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl); - - return 0; -} - -int CDbHandle::getGroupId(const string& ip) -{ - - map& groupIdMap = _groupIdMap.getReaderData(); - map::iterator it = groupIdMap.find(ip); - if (it != groupIdMap.end()) - { - return it->second; - } - - uint32_t uip = stringIpToInt(ip); - string ipStar = Ip2StarStr(uip); - it = groupIdMap.find(ipStar); - if (it != groupIdMap.end()) - { - return it->second; - } - - return -1; -} - -int CDbHandle::getGroupIdByName(const string& sGroupName) -{ - int iGroupId = -1; - try - { - if (sGroupName.empty()) - { - return iGroupId; - } - - map& groupNameMap = _groupNameMap.getReaderData(); - map::iterator it = groupNameMap.find(sGroupName); - if (it != groupNameMap.end()) - { - TLOGINFO("CDbHandle::getGroupIdByName: "<< sGroupName << "|" << it->second << endl); - return it->second; - } - } - catch (exception& ex) - { - TLOGERROR("CDbHandle::getGroupIdByName exception:" << ex.what() << endl); - } - catch (...) - { - TLOGERROR("CDbHandle::getGroupIdByName unknown exception" << endl); - } - - TLOGINFO("CDbHandle::getGroupIdByName " << sGroupName << "|" << endl); - return -1; -} - -int CDbHandle::loadIPPhysicalGroupInfo(bool fromInit) -{ - tars::TC_Mysql::MysqlData res; - try - { - string sSql = "select group_id,ip_order,allow_ip_rule,denny_ip_rule,group_name from t_server_group_rule " - "order by group_id"; - - res = _mysqlReg.queryRecord(sSql); - - TLOGDEBUG("CDbHandle::loadIPPhysicalGroupInfo get server group from db, records affected:" << res.size() << endl); - - load2GroupMap(res.data()); - } - catch (TC_Mysql_Exception& ex) - { - sendSqlErrorAlarmSMS(); - TLOGERROR("CDbHandle::loadIPPhysicalGroupInfo exception: " << ex.what() << endl); - if (fromInit) - { - //åˆå§‹åŒ–是出现异常,退出, å…«æˆæ˜¯æ•°æ®åº“æƒé™é—®é¢˜ - assert(0); - } - } - catch (exception& ex) - { - TLOGDEBUG("CDbHandle::loadIPPhysicalGroupInfo " << ex.what() << endl); - if (fromInit) - { - assert(0); - } - } - return -1; -} - -void CDbHandle::load2GroupMap(const vector >& serverGroupRule) -{ - map& groupIdMap = _groupIdMap.getWriterData(); - map& groupNameMap = _groupNameMap.getWriterData(); - groupIdMap.clear(); //è§„åˆ™æ”¹å˜ æ¸…é™¤ä»¥å‰ç¼“å­˜ - groupNameMap.clear(); - vector >::const_iterator it = serverGroupRule.begin(); - for (; it != serverGroupRule.end(); it++) - { - int groupId = TC_Common::strto(it->find("group_id")->second); - vector vIp = TC_Common::sepstr(it->find("allow_ip_rule")->second, "|"); - for (size_t j = 0; j < vIp.size(); j++) - { - groupIdMap[vIp[j]] = groupId; - } - - groupNameMap[it->find("group_name")->second] = groupId; - } - _groupIdMap.swap(); - _groupNameMap.swap(); - -} - - -int CDbHandle::loadGroupPriority(bool fromInit) -{ - std::map & mapPriority = _mapGroupPriority.getWriterData(); - mapPriority.clear(); - try - { - std::string s_command("SELECT id,group_list,station FROM t_group_priority ORDER BY list_order ASC"); - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(s_command); - TLOGDEBUG("CDbHandle::loadGroupPriority load group priority from db, records affected:" << res.size() << endl); - - for (unsigned int i = 0; i < res.size(); i++) - { - mapPriority[i].sGroupID = res[i]["id"]; - mapPriority[i].sStation = res[i]["station"]; - - std::vector vecGroupID = TC_Common::sepstr(res[i]["group_list"], "|,;", false); - std::copy(vecGroupID.begin(), vecGroupID.end(), std::inserter(mapPriority[i].setGroupID, mapPriority[i].setGroupID.begin())); - TLOGDEBUG("loaded groups priority to cache [" << mapPriority[i].sStation << "] group size:" << mapPriority[i].setGroupID.size() << endl); - } - - _mapGroupPriority.swap(); - - TLOGDEBUG("loaded groups priority to cache virtual group size:" << mapPriority.size() << endl); - } - catch (TC_Mysql_Exception& ex) - { - sendSqlErrorAlarmSMS(); - TLOGERROR("CDbHandle::loadGroupPriority exception: " << ex.what() << endl); - if (fromInit) - { - assert(0); - } - return -1; - } - catch (exception& ex) - { - TLOGDEBUG("CDbHandle::loadGroupPriority " << ex.what() << endl); - if (fromInit) - { - assert(0); - } - return -1; - } - - return 0; -} - - -int CDbHandle::computeInactiveRate() -{ - try - { - std::string sCommand("SELECT SUM(CASE present_state WHEN 'active' THEN 1 ELSE 0 END) AS active, " - "SUM(CASE present_state WHEN 'inactive' THEN 1 ELSE 0 END) AS inactive FROM t_node_info;"); - - int64_t iStart = TNOWMS; - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sCommand); - TLOGINFO(__FUNCTION__ << "|cost:" << (TNOWMS - iStart) << endl); - if (res.size() != 1) - { - TLOGDEBUG("loaded inactive rate failed, size(" << res.size() << ") ne 1" << endl); - return -1; - } - - int iActive = TC_Common::strto(res[0]["active"]); - int iInactive = TC_Common::strto(res[0]["inactive"]); - - if (iActive < 0 || iInactive < 0) - { - TLOGDEBUG("loaded inactive rate failed, active(" << iActive << ") inactive(" << iInactive << ")" << endl); - return -2; - } - - return (iInactive + iActive) <= 0 ? 0 : static_cast(iInactive / (iInactive + iActive) * 100); - } - catch (TC_Mysql_Exception& ex) - { - sendSqlErrorAlarmSMS(); - TLOGERROR("CDbHandle::computeInactiveRate exception: " << ex.what() << endl); - return -3; - } - catch (exception& ex) - { - TLOGERROR("CDbHandle::computeInactiveRate " << ex.what() << endl); - return -4; - } -} - -TC_Mysql::MysqlData CDbHandle::UnionRecord(TC_Mysql::MysqlData& data1, TC_Mysql::MysqlData& data2) -{ - TC_Mysql::MysqlData result; - - result.data() = data1.data(); - - vector >& vTmp = data2.data(); - vector >::iterator it = vTmp.begin(); - for (; it != vTmp.end(); it++) - { - result.data().push_back(*it); - } - - return result; -} - -int CDbHandle::loadObjectIdCache(const bool bRecoverProtect, const int iRecoverProtectRate, const int iLoadTimeInterval, const bool bLoadAll, bool fromInit) -{ - ObjectsCache objectsCache; - SetDivisionCache setDivisionCache; - std::map mapStatus; - - try - { - int64_t iStart = TNOWMS; - if (bLoadAll) - { - //加载分组信æ¯ä¸€å®šè¦åœ¨åŠ è½½æœåŠ¡ä¿¡æ¯ä¹‹å‰ - loadIPPhysicalGroupInfo(fromInit); - //åŠ è½½åŸŽå¸‚ä¿¡æ¯ - loadGroupPriority(fromInit); - } - - //加载存活serveråŠregistryåˆ—è¡¨ä¿¡æ¯ - string sSql1 = - "select adapter.servant,adapter.endpoint,server.enable_group,server.setting_state,server.present_state,server.application,server.server_name,server.node_name,server.enable_set,server.set_name,server.set_area,server.set_group,server.ip_group_name,server.bak_flag " - "from t_adapter_conf as adapter right join t_server_conf as server using (application, server_name, node_name)"; - - //增é‡åŠ è½½é€»è¾‘ - if (!bLoadAll) - { - string sInterval = TC_Common::tm2str(TC_TimeProvider::getInstance()->getNow() - iLoadTimeInterval); - sSql1 += " ,(select distinct application,server_name from t_server_conf"; - sSql1 += " where registry_timestamp >='" + sInterval + "'"; - sSql1 += " union select distinct application,server_name from t_adapter_conf"; - sSql1 += " where registry_timestamp >='" + sInterval + "') as tmp"; - sSql1 += " where server.application=tmp.application and server.server_name=tmp.server_name"; - } - - string sSql2 = "select servant, endpoint, enable_group, present_state as setting_state, present_state, tars_version as application, tars_version as server_name, tars_version as node_name,'N' as enable_set,'' as set_name,'' as set_area,'' as set_group ,'' " - "as ip_group_name , '' as bak_flag from t_registry_info order by endpoint"; - - tars::TC_Mysql::MysqlData res; - - { - tars::TC_Mysql::MysqlData res1 = _mysqlReg.queryRecord(sSql1); - - tars::TC_Mysql::MysqlData res2 = _mysqlReg.queryRecord(sSql2); - TLOGDEBUG("CDbHandle::loadObjectIdCache load " << (bLoadAll ? "all " : "") << "Active objects from db, records affected:" << (res1.size() + res2.size()) - << "|cost:" << (TNOWMS - iStart) << endl); - iStart = TNOWMS; - res = UnionRecord(res1, res2); - } - for (unsigned i = 0; i < res.size(); i++) - { - try - { - if (res[i]["servant"].empty() && res[i]["endpoint"].empty()) - { - TLOGDEBUG(res[i]["application"] << "-" << res[i]["server_name"] << "-" << res[i]["node_name"] << " NULL" << endl); - ServantStatusKey statusKey = { res[i]["application"], res[i]["server_name"], res[i]["node_name"] }; - mapStatus[statusKey] = (res[i]["setting_state"] == "active" && res[i]["present_state"] == "active") ? tars::Active : tars::Inactive; - continue; - } - - TC_Endpoint ep; - try - { - ep.parse(res[i]["endpoint"]); - } - catch (exception& ex) - { - TLOGERROR("CDbHandle::loadObjectIdCache " << ex.what() << endl); - continue; - } - - EndpointF epf; - epf.host = ep.getHost(); - epf.port = ep.getPort(); - epf.timeout = ep.getTimeout(); - - // 现在支æŒä¸‰ç§ç±»åž‹ï¼š0 UDP, 1 TCP, 2 SSL - // 所以istcp字段作为int类型使用 - if (!ep.isTcp()) - { - epf.istcp = EndpointInfo::UDP; - } - else - { - if (ep.isSSL()) - epf.istcp = EndpointInfo::SSL; - else - epf.istcp = EndpointInfo::TCP; - } - - epf.authType = ep.getAuthType(); - epf.grouprealid = getGroupId(epf.host); - string ip_group_name = res[i]["ip_group_name"]; - epf.grouprealid = ip_group_name.empty() ? getGroupId(epf.host) : getGroupIdByName(ip_group_name); - epf.groupworkid = TC_Common::lower(res[i]["enable_group"]) == "y" ? epf.grouprealid : -1; - if (TC_Common::lower(res[i]["enable_group"]) == "y" && epf.grouprealid == -1) - { - //记录查ä¸åˆ°åˆ†ç»„的组åå’Œip - FDLOG("group_id") << ip_group_name << "|" << epf.host << endl; - } - - epf.setId = ""; - epf.bakFlag = TC_Common::strto(res[i]["bak_flag"]); - - bool bSet = TC_Common::lower(res[i]["enable_set"]) == "y"; - if (bSet) - { - epf.setId = res[i]["set_name"] + "." + res[i]["set_area"] + "." + res[i]["set_group"]; - } - - TLOGDEBUG("CDbHandle::loadObjectIdCache :" << res[i]["servant"] << "." << epf.host << "|" << epf.grouprealid << "|" << epf.groupworkid << "|" << res[i]["setting_state"] << "|" << res[i]["present_state"] << endl); - - bool bActive = true; - - ServantStatusKey statusKey = { res[i]["application"], res[i]["server_name"], res[i]["node_name"] }; - - if ((res[i]["setting_state"] == "active" && res[i]["present_state"] == "active") - || res[i]["servant"] == "tars.tarsAdminRegistry.AdminRegObj") //如果是管ç†æœåŠ¡, 强制认为它是活的 - { - //存活列表 - objectsCache[res[i]["servant"]].vActiveEndpoints.push_back(epf); - mapStatus[statusKey] = tars::Active; - } - else - { - //éžå­˜æ´»åˆ—表 - objectsCache[res[i]["servant"]].vInactiveEndpoints.push_back(epf); - mapStatus[statusKey] = tars::Inactive; - bActive = false; - } - - if (bSet) - { - if (res[i]["set_name"].empty() || res[i]["set_area"].empty() || res[i]["set_group"].empty() || res[i]["set_name"] == "*" || res[i]["set_area"] == "*") - { - TLOGERROR("CDbHandle::loadObjectIdCache: " << res[i]["servant"] << "." << epf.host << "|set division invalid[" << res[i]["set_name"] << "." << res[i]["set_area"] << "." << res[i]["set_group"] << "]" << endl); - bSet = false; - } - } - - //setåˆ’åˆ†ä¿¡æ¯ - if (bSet) - { - //set区域 - string sSetArea = res[i]["set_name"] + "." + res[i]["set_area"]; - //set全称 - string sSetId = res[i]["set_name"] + "." + res[i]["set_area"] + "." + res[i]["set_group"]; - - SetServerInfo setServerInfo; - setServerInfo.bActive = bActive; - setServerInfo.epf = epf; - - setServerInfo.sSetId = sSetId; - setServerInfo.sSetArea = sSetArea; - - setDivisionCache[res[i]["servant"]][res[i]["set_name"]].push_back(setServerInfo); - TLOGINFO("CDbHandle::loadObjectIdCache " << res[i]["servant"] << "." << epf.host << "|" << sSetId << "|" << setServerInfo.bActive << endl); - } - else if (!bLoadAll) - { - //增é‡åŠ è½½,如果ä¸å¯ç”¨set也è¦èµ‹ä¸ªç©ºå€¼ï¼Œé˜²æ­¢æ›´æ–°ç¼“存时ä¸å½»åº• - map > mTemp = setDivisionCache[res[i]["servant"]]; - setDivisionCache[res[i]["servant"]] = mTemp; - } - } - catch (TC_EndpointParse_Exception& ex) - { - TLOGERROR("CDbHandle::loadObjectIdCache " << ex.what() << endl); - } - } - - //替æ¢åˆ°cache - int iRate = bRecoverProtect == true ? computeInactiveRate() : 0; - if (bRecoverProtect == true && iRate > iRecoverProtectRate && objectsCache.size() > 0) - { - TLOGDEBUG("CDbHandle::loadObjectIdCache now database recover protect valid, rate:" << iRate << ",iRecoverProtectRate:" << iRecoverProtectRate - << std::boolalpha << ",bRecoverProtect:" << bRecoverProtect << endl); - return -1; - } - - updateObjectsCache(objectsCache, bLoadAll); - updateStatusCache(mapStatus, bLoadAll); - updateDivisionCache(setDivisionCache, bLoadAll); - - TLOGDEBUG("loaded objects to cache size:" << objectsCache.size() << endl); - TLOGDEBUG("loaded server status to cache size:" << mapStatus.size() << endl); - TLOGDEBUG("loaded set server to cache size:" << setDivisionCache.size() << endl); - FDLOG() << "loaded objects to cache size:" << objectsCache.size() << endl; - FDLOG() << "loaded set server to cache size:" << setDivisionCache.size() << endl; - - TLOGDEBUG("CDbHandle::loadObjectIdCache parse " << (bLoadAll ? "all " : "") << "|cost:" << (TNOWMS - iStart) << endl); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::loadObjectIdCache exception: " << ex.what() << endl); - - sendSqlErrorAlarmSMS(); - if (fromInit) - { - //åˆå§‹åŒ–是出现异常,退出 - assert(0); - } - return -1; - } - catch (exception& ex) - { - TLOGDEBUG("CDbHandle::loadObjectIdCache " << ex.what() << endl); - if (fromInit) - { - //åˆå§‹åŒ–是出现异常,退出 - assert(0); - } - return -1; - } - - return 0; -} - -int CDbHandle::updateRegistryInfo2Db(bool bRegHeartbeatOff) -{ - if (bRegHeartbeatOff) - { - TLOGDEBUG("updateRegistryInfo2Db not need to update reigstry status !" << endl); - return 0; - } - - map::iterator iter; - map mapServantEndpoint = g_app.getServantEndpoint(); - if (mapServantEndpoint.size() == 0) - { - TLOGERROR("fatal error, get registry servant failed!" << endl); - return -1; - } - - try - { - string sSql = "replace into t_registry_info (locator_id, servant, endpoint, last_heartbeat, present_state, tars_version) " - "values "; - - TC_Endpoint locator; - locator.parse(mapServantEndpoint[(*g_pconf)["/tars/objname"]]); - - for (iter = mapServantEndpoint.begin(); iter != mapServantEndpoint.end(); iter++) - { - sSql += (iter == mapServantEndpoint.begin() ? string("") : string(", ")) + - "('" + locator.getHost() + ":" + TC_Common::tostr(locator.getPort()) + "', " - "'" + iter->first + "', '" + iter->second + "', now(), 'active', " + - "'" + _mysqlReg.escapeString(TARS_VERSION) + "')"; - } - _mysqlReg.execute(sSql); - TLOGDEBUG("CDbHandle::updateRegistryInfo2Db affected:" << _mysqlReg.getAffectedRows() << endl); - } - catch (TC_Mysql_Exception& ex) - { - sendSqlErrorAlarmSMS(); - TLOGERROR("CDbHandle::updateRegistryInfo2Db exception: " << ex.what() << endl); - return -1; - } - catch (exception& ex) - { - TLOGERROR("CDbHandle::updateRegistryInfo2Db exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -vector CDbHandle::findObjectById(const string& id) -{ - ObjectsCache::iterator it; - ObjectsCache& usingCache = _objectsCache.getReaderData(); - - if ((it = usingCache.find(id)) != usingCache.end()) - { - return it->second.vActiveEndpoints; - } - else - { - vector activeEp; - return activeEp; - } -} - - -int CDbHandle::findObjectById4All(const string& id, vector& activeEp, vector& inactiveEp) -{ - - TLOGDEBUG(__FUNCTION__ << " id: " << id << endl); - - ObjectsCache::iterator it; - ObjectsCache& usingCache = _objectsCache.getReaderData(); - - if ((it = usingCache.find(id)) != usingCache.end()) - { - activeEp = it->second.vActiveEndpoints; - inactiveEp = it->second.vInactiveEndpoints; - } - else - { - activeEp.clear(); - inactiveEp.clear(); - } - - return 0; -} - -vector CDbHandle::getEpsByGroupId(const vector& vecEps, const GroupUseSelect GroupSelect, int iGroupId, ostringstream& os) -{ - os << "|"; - vector vResult; - - for (unsigned i = 0; i < vecEps.size(); i++) - { - os << vecEps[i].host << ":" << vecEps[i].port << "(" << vecEps[i].groupworkid << ");"; - if (GroupSelect == ENUM_USE_WORK_GROUPID && vecEps[i].groupworkid == iGroupId) - { - vResult.push_back(vecEps[i]); - } - if (GroupSelect == ENUM_USE_REAL_GROUPID && vecEps[i].grouprealid == iGroupId) - { - vResult.push_back(vecEps[i]); - } - } - - return vResult; -} - -vector CDbHandle::getEpsByGroupId(const vector& vecEps, const GroupUseSelect GroupSelect, const set& setGroupID, ostringstream& os) -{ - os << "|"; - std::vector vecResult; - - for (std::vector::size_type i = 0; i < vecEps.size(); i++) - { - os << vecEps[i].host << ":" << vecEps[i].port << "(" << vecEps[i].groupworkid << ")"; - if (GroupSelect == ENUM_USE_WORK_GROUPID && setGroupID.count(vecEps[i].groupworkid) == 1) - { - vecResult.push_back(vecEps[i]); - } - if (GroupSelect == ENUM_USE_REAL_GROUPID && setGroupID.count(vecEps[i].grouprealid) == 1) - { - vecResult.push_back(vecEps[i]); - } - } - - return vecResult; -} - -int CDbHandle::findObjectByIdInSameGroup(const string& id, const string& ip, vector& activeEp, vector& inactiveEp, ostringstream& os) -{ - activeEp.clear(); - inactiveEp.clear(); - - int iClientGroupId = getGroupId(ip); - - os << "|(" << iClientGroupId << ")"; - - if (iClientGroupId == -1) - { - return findObjectById4All(id, activeEp, inactiveEp); - } - - ObjectsCache::iterator it; - ObjectsCache& usingCache = _objectsCache.getReaderData(); - - if ((it = usingCache.find(id)) != usingCache.end()) - { - activeEp = getEpsByGroupId(it->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupId, os); - inactiveEp = getEpsByGroupId(it->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupId, os); - - if (activeEp.size() == 0) //没有åŒç»„çš„endpoit,匹é…未å¯ç”¨åˆ†ç»„çš„æœåŠ¡ - { - activeEp = getEpsByGroupId(it->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os); - inactiveEp = getEpsByGroupId(it->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os); - } - if (activeEp.size() == 0) //没有åŒç»„çš„endpoit - { - activeEp = it->second.vActiveEndpoints; - inactiveEp = it->second.vInactiveEndpoints; - } - } - - return 0; -} - -int CDbHandle::findObjectByIdInGroupPriority(const std::string& sID, const std::string& sIP, std::vector& vecActive, std::vector& vecInactive, std::ostringstream& os) -{ - vecActive.clear(); - vecInactive.clear(); - - int iClientGroupID = getGroupId(sIP); - os << "|(" << iClientGroupID << ")"; - if (iClientGroupID == -1) - { - return findObjectById4All(sID, vecActive, vecInactive); - } - - ObjectsCache& usingCache = _objectsCache.getReaderData(); - ObjectsCache::iterator itObject = usingCache.find(sID); - if (itObject == usingCache.end()) return 0; - - //首先在åŒç»„中查找 - { - vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupID, os); - vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupID, os); - os << "|(In Same Group: " << iClientGroupID << " Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")"; - } - - //å¯ç”¨åˆ†ç»„,但åŒç»„中没有找到,在优先级åºåˆ—中查找 - std::map & mapPriority = _mapGroupPriority.getReaderData(); - for (std::map::iterator it = mapPriority.begin(); it != mapPriority.end() && vecActive.empty(); it++) - { - if (it->second.setGroupID.count(iClientGroupID) == 0) - { - os << "|(Not In Priority " << it->second.sGroupID << ")"; - continue; - } - vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, it->second.setGroupID, os); - vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, it->second.setGroupID, os); - os << "|(In Priority: " << it->second.sGroupID << " Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")"; - } - - //没有åŒç»„çš„endpoit,匹é…未å¯ç”¨åˆ†ç»„çš„æœåŠ¡ - if (vecActive.empty()) - { - vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os); - vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os); - os << "|(In No Grouop: Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")"; - } - - //在未分组的情况下也没有找到,返回全部地å€(此时基本上所有的æœåŠ¡éƒ½å·²æŒ‚掉) - if (vecActive.empty()) - { - vecActive = itObject->second.vActiveEndpoints; - vecInactive = itObject->second.vInactiveEndpoints; - os << "|(In All: Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")"; - } - - return 0; -} - -int CDbHandle::findObjectByIdInSameStation(const std::string& sID, const std::string& sStation, std::vector& vecActive, std::vector& vecInactive, std::ostringstream& os) -{ - vecActive.clear(); - vecInactive.clear(); - - //获得station所有组 - std::map & mapPriority = _mapGroupPriority.getReaderData(); - std::map::iterator itGroup = mapPriority.end(); - for (itGroup = mapPriority.begin(); itGroup != mapPriority.end(); itGroup++) - { - if (itGroup->second.sStation != sStation) continue; - - break; - } - - if (itGroup == mapPriority.end()) - { - os << "|not found station:" << sStation; - return -1; - } - - ObjectsCache& usingCache = _objectsCache.getReaderData(); - ObjectsCache::iterator itObject = usingCache.find(sID); - if (itObject == usingCache.end()) return 0; - - //查找对应所有组下的IPåœ°å€ - vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_REAL_GROUPID, itGroup->second.setGroupID, os); - vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_REAL_GROUPID, itGroup->second.setGroupID, os); - - return 0; -} - -int CDbHandle::findObjectByIdInSameSet(const string& sID, const vector& vtSetInfo, std::vector& vecActive, std::vector& vecInactive, std::ostringstream& os) -{ - string sSetName = vtSetInfo[0]; - string sSetArea = vtSetInfo[0] + "." + vtSetInfo[1]; - string sSetId = vtSetInfo[0] + "." + vtSetInfo[1] + "." + vtSetInfo[2]; - - SetDivisionCache& usingSetDivisionCache = _setDivisionCache.getReaderData(); - SetDivisionCache::iterator it = usingSetDivisionCache.find(sID); - if (it == usingSetDivisionCache.end()) - { - //此情况下没å¯åŠ¨set - TLOGINFO("CDbHandle::findObjectByIdInSameSet:" << __LINE__ << "|" << sID << " haven't start set|" << sSetId << endl); - return -1; - } - - map >::iterator setNameIt = it->second.find(sSetName); - if (setNameIt == (it->second).end()) - { - //此情况下没å¯åŠ¨set - TLOGINFO("CDbHandle::findObjectByIdInSameSet:" << __LINE__ << "|" << sID << " haven't start set|" << sSetId << endl); - return -1; - } - - if (vtSetInfo[2] == "*") - { - //检索通é…组和set组中的所有æœåŠ¡ - vector vServerInfo = setNameIt->second; - for (size_t i = 0; i < vServerInfo.size(); i++) - { - if (vServerInfo[i].sSetArea == sSetArea) - { - if (vServerInfo[i].bActive) - { - vecActive.push_back(vServerInfo[i].epf); - } - else - { - vecInactive.push_back(vServerInfo[i].epf); - } - } - } - - return (vecActive.empty() && vecInactive.empty()) ? -2 : 0; - } - else - { - - // 1.从指定set组中查找 - int iRet = findObjectByIdInSameSet(sSetId, setNameIt->second, vecActive, vecInactive, os); - if (iRet != 0 && vtSetInfo[2] != "*") - { - // 2. 步骤1中没找到,在通é…组里找 - string sWildSetId = vtSetInfo[0] + "." + vtSetInfo[1] + ".*"; - iRet = findObjectByIdInSameSet(sWildSetId, setNameIt->second, vecActive, vecInactive, os); - } - - return iRet; - } - - -} - -int CDbHandle::findObjectByIdInSameSet(const string& sSetId, const vector& vSetServerInfo, std::vector& vecActive, std::vector& vecInactive, std::ostringstream& os) -{ - for (size_t i = 0; i < vSetServerInfo.size(); ++i) - { - if (vSetServerInfo[i].sSetId == sSetId) - { - if (vSetServerInfo[i].bActive) - { - vecActive.push_back(vSetServerInfo[i].epf); - } - else - { - vecInactive.push_back(vSetServerInfo[i].epf); - } - } - } - - int iRet = (vecActive.empty() && vecInactive.empty()) ? -2 : 0; - return iRet; -} -int CDbHandle::getNodeTemplateName(const string nodeName, string& sTemplateName) -{ - try - { - string sSql = - "select template_name " - "from t_node_info " - "where node_name='" + _mysqlReg.escapeString(nodeName) + "'"; - - tars::TC_Mysql::MysqlData res = _mysqlReg.queryRecord(sSql); - - if (res.size() != 0) - { - sTemplateName = res[0]["template_name"]; - } - - TLOGDEBUG("CDbHandle::getNodeTemplateName '" << nodeName << "' affected:" << res.size() - << " get template_name:'" << sTemplateName << "'" << endl); - - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("CDbHandle::getNodeTemplateName exception: " << ex.what() << endl); - return -1; - } - - return 0; -} - -void CDbHandle::updateStatusCache(const std::map& mStatus, bool updateAll) -{ - TC_ThreadLock::Lock lock(_mapServantStatusLock); - if (updateAll) - { - //å…¨é‡æ›´æ–° - _mapServantStatus = mStatus; - } - else - { - std::map::const_iterator it = mStatus.begin(); - for (; it != mStatus.end(); it++) - { - _mapServantStatus[it->first] = it->second; - } - } -} - -void CDbHandle::updateObjectsCache(const ObjectsCache& objCache, bool updateAll) -{ - //å…¨é‡æ›´æ–° - if (updateAll) - { - _objectsCache.getWriterData() = objCache; - _objectsCache.swap(); - } - else - { - //用查询数æ®è¦†ç›–一下 - _objectsCache.getWriterData() = _objectsCache.getReaderData(); - ObjectsCache& tmpObjCache = _objectsCache.getWriterData(); - - ObjectsCache::const_iterator it = objCache.begin(); - for (; it != objCache.end(); it++) - { - //增é‡çš„时候加载的是æœåŠ¡çš„æ‰€æœ‰èŠ‚ç‚¹ï¼Œå› æ­¤è¿™é‡Œç›´æŽ¥æ›¿æ¢ - tmpObjCache[it->first] = it->second; - } - _objectsCache.swap(); - } -} - -void CDbHandle::updateDivisionCache(const SetDivisionCache& setDivisionCache, bool updateAll) -{ - //å…¨é‡æ›´æ–° - if (updateAll) - { - _setDivisionCache.getWriterData() = setDivisionCache; - _setDivisionCache.swap(); - } - else - { - _setDivisionCache.getWriterData() = _setDivisionCache.getReaderData(); - SetDivisionCache& tmpsetCache = _setDivisionCache.getWriterData(); - SetDivisionCache::const_iterator it = setDivisionCache.begin(); - for (; it != setDivisionCache.end(); it++) - { - //有setä¿¡æ¯æ‰æ›´æ–° - if (it->second.size() > 0) - { - tmpsetCache[it->first] = it->second; - } - else if (tmpsetCache.count(it->first)) - { - //这个æœåŠ¡çš„所有节点都没有å¯ç”¨set,删除缓存中的setä¿¡æ¯ - tmpsetCache.erase(it->first); - } - } - _setDivisionCache.swap(); - } -} -void CDbHandle::sendSqlErrorAlarmSMS() -{ - string errInfo = " ERROR:" + g_app.getAdapterEndpoint("QueryAdapter").getHost() + ":主控访问数æ®åº“异常,请åŠæ—¶å¤„ç†"; - TARS_NOTIFY_ERROR(errInfo); - - TLOGERROR("TARS_NOTIFY_ERROR " << errInfo << endl); -} - -uint32_t CDbHandle::stringIpToInt(const std::string& sip) -{ - string ip1, ip2, ip3, ip4; - uint32_t dip, p1, p2, p3; - dip = 0; - p1 = sip.find('.'); - p2 = sip.find('.', p1 + 1); - p3 = sip.find('.', p2 + 1); - ip1 = sip.substr(0, p1); - ip2 = sip.substr(p1 + 1, p2 - p1 - 1); - ip3 = sip.substr(p2 + 1, p3 - p2 - 1); - ip4 = sip.substr(p3 + 1, sip.size() - p3 - 1); - (((unsigned char *)&dip)[0]) = TC_Common::strto(ip1); - (((unsigned char *)&dip)[1]) = TC_Common::strto(ip2); - (((unsigned char *)&dip)[2]) = TC_Common::strto(ip3); - (((unsigned char *)&dip)[3]) = TC_Common::strto(ip4); - return htonl(dip); -} - -string CDbHandle::Ip2Str(uint32_t ip) -{ - char str[50]; - unsigned char *p = (unsigned char *)&ip; - sprintf(str, "%u.%u.%u.%u", p[3], p[2], p[1], p[0]); - return string(str); -} - -string CDbHandle::Ip2StarStr(uint32_t ip) -{ - char str[50]; - unsigned char *p = (unsigned char *)&ip; - sprintf(str, "%u.%u.%u.*", p[3], p[2], p[1]); - return string(str); -} diff --git a/cpp/framework/RegistryServer/DbHandle.h b/cpp/framework/RegistryServer/DbHandle.h deleted file mode 100644 index 8c1e8283f..000000000 --- a/cpp/framework/RegistryServer/DbHandle.h +++ /dev/null @@ -1,532 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_HANDLE_H__ -#define __DB_HANDLE_H__ - -#include "util/tc_common.h" -#include "util/tc_config.h" -#include "util/tc_monitor.h" -#include "util/tc_mysql.h" -#include "util/tc_file.h" -#include "jmem/jmem_hashmap.h" -#include "util/tc_readers_writer_data.h" -#include - -#include "Registry.h" -#include "Node.h" -#include "servant/TarsLogger.h" - -#define GROUPCACHEFILE "serverGroupCache.dat" -#define GROUPPROICACHEFILE "GroupPrioCache.dat" - -using namespace tars; -////////////////////////////////////////////////////// - -// -typedef map ObjectsCache; -typedef TarsHashMap FileHashMap; - -//_mapServantStatusçš„key -struct ServantStatusKey -{ - string application; - string serverName; - string nodeName; -}; - -inline bool operator<(const ServantStatusKey&l, const ServantStatusKey&r) -{ - if(l.application != r.application) return (l.application < r.application); - if(l.serverName != r.serverName) return (l.serverName < r.serverName); - if(l.nodeName != r.nodeName) return (l.nodeName < r.nodeName); - return false; -} - -////////////////////////////////////////////////////// -/** - * 主控获å–nodeä¿¡æ¯å¼‚常 - */ -struct Tars : public TarsException -{ - Tars(const string &buffer) : TarsException(buffer){}; - Tars(const string &buffer, int err) : TarsException(buffer, err){}; - ~Tars() throw(){}; -}; - -////////////////////////////////////////////////////// -/** - * æ•°æ®åº“æ“作类 - */ -class CDbHandle -{ -private: - struct GroupPriorityEntry - { - std::string sGroupID; - std::string sStation; - std::set setGroupID; - }; - - enum GroupUseSelect - { - ENUM_USE_WORK_GROUPID, - ENUM_USE_REAL_GROUPID - }; - - //set中æœåŠ¡çš„ä¿¡æ¯ - struct SetServerInfo - { - string sSetId; - string sSetArea; - bool bActive; - EndpointF epf; - }; - - // - typedef map > > SetDivisionCache; - -public: - /** - * 构造函数 - */ - CDbHandle() - : _enMultiSql(false) - { - } - - /** - * åˆå§‹åŒ– - * @param pconf é…置文件 - * @return 0-æˆåŠŸ others-失败 - */ - int init(TC_Config *pconf); - - /** - * 获å–特定node idçš„å¯¹è±¡ä»£ç† - * @param nodeName : node id - * @return : 对象代ç†çš„智能指针 - */ - NodePrx getNodePrx(const string & nodeName); - - /** - * ä¿å­˜node注册的session - * @param name nodeå称 - * @param ni nodeè¯¦ç»†ä¿¡æ¯ - * @param li nodeæœºå™¨è´Ÿè½½ä¿¡æ¯ - * @return 注册是å¦æˆåŠŸ - */ - int registerNode(const string & name, const NodeInfo & ni, const LoadInfo & li); - - /** - * æ›´æ–°node的状æ€ä¸ºinactive - * - * @param name nodeå称 - * - * @return 0-æˆåŠŸ others-失败 - */ - int destroyNode(const string & name); - - /** - * æ›´æ–°node心跳时间åŠæœºå™¨è´Ÿè½½ - * - * @param name nodeå称 - * @param load nodeæœºå™¨è´Ÿè½½ä¿¡æ¯ - * @return 0-æˆåŠŸ others-失败 - */ - int keepAlive(const string & name, const LoadInfo & li); - - /** - * 获å–活动node列表endpointä¿¡æ¯ - * @param out result 结果æè¿° - * @return map : 对应id nodeçš„obj - */ - map getActiveNodeList(string & result); - - /** - * 获å–node版本 - * @param name nodeå称 - * @param version node版本 - * @param out result 结果æè¿° - * @return 0-æˆåŠŸ others-失败 - */ - int getNodeVersion(const string &nodeName, string &version, string & result); - - /** - * 获å–在该node部署的server列表 - * - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * - * @return serverä¿¡æ¯åˆ—表 - */ - vector getServers(const string & app, const string & serverName, const string & nodeName, bool withDnsServer = false); - - /** - * 获å–serverçš„é…ç½®æ¨¡æ¿ - * @param sTemplateName 模æ¿å称 - * @param sResultDesc 结果æè¿° - * @return 模æ¿å†…容 - */ - string getProfileTemplate(const string & sTemplateName, string & sResultDesc); - -protected: - /** - * 获å–serverçš„é…ç½®æ¨¡æ¿ - * @param sTemplateName 模æ¿å称 - * @param mapRecursion è¢«é€’å½’åˆ°çš„æ¨¡æ¿ - * @param sResultDesc 结果æè¿° - * @return 模æ¿å†…容 - */ - string getProfileTemplate(const string & sTemplateName, map & mapRecursion, string & sResultDesc); - -public: - /** - * æ›´æ–°serverçŠ¶æ€ - * - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param stateFields: 更新状æ€å­—段å - * @param state : serverçŠ¶æ€ - * - * @return serverä¿¡æ¯åˆ—表 - */ - int updateServerState(const string & app, const string & serverName, const string & nodeName, const string & stateFields, tars::ServerState state, int processId = -1); - - /** - * é‡æ‰¹æ›´æ–°serverçŠ¶æ€ - * - * @param vecStateInfo: 批é‡serverçš„çŠ¶æ€ - * - * @return serverä¿¡æ¯åˆ—表 - */ - int updateServerStateBatch(const std::vector & vecStateInfo); - - /** æ ¹æ®id获å–对象 - * - * @param id 对象å称 - * - * @return 返回所有该对象的活动endpoint列表 - */ - vector findObjectById(const string & id); - - /** æ ¹æ®id获å–对象 - * - * @param id 对象å称 - * @out param activeEp 存活的列表 - * @out param inactiveEp éžå­˜æ´»çš„列表 - * - * @return 0-æˆåŠŸ others-失败 - */ - int findObjectById4All(const string & id, vector& activeEp, vector& inactiveEp); - - /** æ ¹æ®id获å–åŒç»„对象 - * - * @param id 对象å称 - * @param ip - * @out param activeEp 存活的列表 - * @out param inactiveEp éžå­˜æ´»çš„列表 - * @out param os 打å°æ—¥å¿—使用 - * - * @return 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameGroup(const string & id, const string & ip, vector& activeEp, vector& inactiveEp, ostringstream &os); - - /** æ ¹æ®id获å–优先级åºåˆ—中的对象 - * - * @param id 对象å称 - * @param ip - * @out param vecActive 存活的列表 - * @out param vecInactive éžå­˜æ´»çš„列表 - * @out param os 打å°æ—¥å¿—使用 - * - * @return 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInGroupPriority(const std::string &sID, const std::string &sIP, std::vector & vecActive, std::vector & vecInactive, std::ostringstream & os); - - /** æ ¹æ®id和归属地获å–全部对象 - * - * @param id 对象å称 - * @param sStation 归属地 - * @out param vecActive 存活的列表 - * @out param vecInactive éžå­˜æ´»çš„列表 - * @out param os 打å°æ—¥å¿—使用 - * - * @return 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameStation(const std::string &sID, const std::string & sStation, std::vector & vecActive, std::vector & vecInactive, std::ostringstream & os); - - /** æ ¹æ®idå’Œsetä¿¡æ¯èŽ·å–全部对象 - * - * @param sID 对象å称 - * @param vtSetInfo setä¿¡æ¯ - * @out param vecActive 存活的列表 - * @out param vecInactive éžå­˜æ´»çš„列表 - * @out param os 打å°æ—¥å¿—使用 - * - * @return 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameSet(const string &sID, const vector &vtSetInfo, std::vector & vecActive, std::vector & vecInactive, std::ostringstream & os); - - /** æ ¹æ®setId获å–全部对象 - * - * @param sSetId setå称 - * @param vSetServerInfo SetName下部署的æœåŠ¡ä¿¡æ¯ - * @out param vecActive 存活的列表 - * @out param vecInactive éžå­˜æ´»çš„列表 - * @out param os 打å°æ—¥å¿—使用 - * - * @return 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameSet(const string &sSetId, const vector& vSetServerInfo, std::vector & vecActive, std::vector & vecInactive, std::ostringstream & os); - - /** - * 获å–application列表 - * @param null - * @param out reuslt - * @return application列表 - */ - vector getAllApplicationNames(string & result); - - /** - * 获å–server列表 - * @param null - * @return node 列表 - */ - vector > getAllServerIds(string & result); - - /** - * 获å–åˆå¹¶çš„é…置文件 - * @param appServerName - * @param fileName - * @param host - * @param config - * @param resultDesc - * - * @return int - */ - int getConfig(const string &appServerName, const string &fileName, const string &host, string &config, std::string &resultDesc); - - /** - * 设置serverå‘布版本 - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param version : server 版本 - * @param user : å‘布者 - */ - int setPatchInfo(const string & app, const string & serverName, const string & nodeName, const string & version, const string & user); - - /** - * 设置serverçš„tars库版本 - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param version: server基于的tarsç‰ˆæœ¬å· - * - * @return 0-æˆåŠŸ others-失败 - */ - int setServerTarsVersion(const string & app, const string & serverName, const string & nodeName, const string & version); - - /** - * 轮询数æ®åº“,将心跳超时的节点åŠserver状æ€è®¾ä¸ºä¸å­˜æ´» - * @param iTiemout 超时时间 - * @return - */ - int checkNodeTimeout(unsigned uTimeout); - - /** - * 轮询数æ®åº“,将心跳超时的registry设为ä¸å­˜æ´» - * @param iTiemout 超时时间 - * @return - */ - int checkRegistryTimeout(unsigned uTimeout); - - /** - * 定时检查在数æ®åº“的设置状æ€ä¸ºâ€œactiveâ€çš„æœåŠ¡åœ¨Node节点上的状æ€ï¼š - * 如果æœåŠ¡åœ¨Node的设置状æ€ä¸æ˜¯â€œactiveâ€ï¼Œåˆ™é€šçŸ¥Node主动é‡å¯è¯¥æœåŠ¡ - * - * @param iCheckTimeInterval æ¯æ¬¡è½®è¯¢æœ€è¿‘更新的记录,å•ä½ä¸ºç§’ - * @return - */ - int checkSettingState(const int iCheckLeastChangedTime=3600); - - /** - * 在加载对象列表之å‰ï¼Œè®¡ç®—当å‰éžæ´»åŠ¨çŠ¶æ€çš„node的比率 - * @param NULL - * @return - */ - int computeInactiveRate(); - - /** - * 加载对象列表到内存 - * @param bRecoverProtect - * @param iRecoverProtectRate - * @param iLoadTimeInterval 加载最近iLoadTimeInterval秒内å˜åŒ–的记录 - * @param bLoadAll 是å¦åŠ è½½æ‰€æœ‰æœåŠ¡ - * @param bFirstLoad 是å¦æ˜¯ç¬¬ä¸€æ¬¡å…¨é‡åŠ è½½ - * @param fromInit 是å¦æ¥ç€åˆå§‹åŒ–的调用 - * @return - */ - int loadObjectIdCache(const bool bRecoverProtect, const int iRecoverProtectRate, const int iLoadTimeInterval=60, const bool bLoadAll=false, bool fromInit = false); - - /** - * 加载组优先级到内存 - * @param NULL - * @return - */ - int loadGroupPriority(bool fromInit); - - /** - * æ›´æ–°registryä¿¡æ¯åˆ°db - */ - int updateRegistryInfo2Db(bool bRegHeartbeatOff=false); - - /** - * 获å–对应node的模æ¿å称 - */ - int getNodeTemplateName(const string nodeName, string & sTemplateName); - - /** - * æ ¹æ®ip获å–组id - * @return int <0 失败 其它正常 - */ - int getGroupId(const string& ip); - - /** - * æ ¹æ®ç»„å获å–组id - * @return int <0 失败 其它正常 - */ - int getGroupIdByName(const string& sGroupName); - - /** - * 加载IP物ç†åˆ†ç»„ä¿¡æ¯ - */ - int loadIPPhysicalGroupInfo(bool fromInit); - - /** - * ipè½¬æ¢ - */ - static uint32_t stringIpToInt(const std::string& sip); - - /** - * ipè½¬æ¢ - */ - static string Ip2Str(uint32_t ip); - - /** - * ipè½¬æ¢ - */ - static string Ip2StarStr(uint32_t ip); - -protected: - - /** - * æ ¹æ®group id获å–Endpoint - */ - vector getEpsByGroupId(const vector & vecEps, const GroupUseSelect GroupSelect, int iGroupId, ostringstream &os); - - vector getEpsByGroupId(const vector & vecEps, const GroupUseSelect GroupSelect, const set & setGroupID, ostringstream & os); - - /** - * updateServerStateBatch的底层实现函数 - */ - int doUpdateServerStateBatch(const std::vector & vecStateInfo, const size_t sizeBegin, const size_t sizeEnd); - -private: - /** - * 更新缓存中的æœåŠ¡çŠ¶æ€å€¼ - * - * @param mStatus - * @param updateAll 是å¦å…¨éƒ¨æ›´æ–° - * @param bFirstLoad 是å¦æ˜¯ç¬¬ä¸€æ¬¡å…¨é‡åŠ è½½ - */ - void updateStatusCache(const std::map& mStatus,bool updateAll=false); - - /** - * 更新缓存中的æœåŠ¡ä¿¡æ¯ - * - * @param objCache - * @param updateAll 是å¦å…¨éƒ¨æ›´æ–° - * @param bFirstLoad 是å¦æ˜¯ç¬¬ä¸€æ¬¡å…¨é‡åŠ è½½ - */ - void updateObjectsCache(const ObjectsCache& objCache,bool updateAll=false); - - /** - * 更新缓存中的setä¿¡æ¯ - * - * @param objCache - * @param updateAll 是å¦å…¨éƒ¨æ›´æ–° - * @param bFirstLoad 是å¦æ˜¯ç¬¬ä¸€æ¬¡å…¨é‡åŠ è½½ - */ - void updateDivisionCache(const SetDivisionCache& setDivisionCache,bool updateAll=false); - - /** - * 对数æ®åº“查询结果执行è”åˆæ“作 - * - * @param data1 - * @param data2 - * - * @return è”åˆçš„结果 - */ - TC_Mysql::MysqlData UnionRecord(TC_Mysql::MysqlData& data1,TC_Mysql::MysqlData& data2); - - /** - * æ•°æ®åº“访问异常上报 - */ - void sendSqlErrorAlarmSMS(); - - /** - * 建立ip分组map - */ - void load2GroupMap(const vector >& serverGroupRule); - -protected: - - //是å¦å…许采用多æ¡è¯­å¥åŒæ—¶æ‰§è¡Œæ–¹å¼ - bool _enMultiSql; - - //mysql连接对象 - tars::TC_Mysql _mysqlReg; - - //node节点代ç†åˆ—表 - static map _mapNodePrxCache; - static TC_ThreadLock _NodePrxLock; - - //对象列表缓存 - static TC_ReadersWriterData _objectsCache; - - //set划分缓存 - static TC_ReadersWriterData _setDivisionCache; - - //优先级的åºåˆ— - static TC_ReadersWriterData > _mapGroupPriority; - - //servant状æ€è¡¨ - static std::map _mapServantStatus; - - //存在多线程更新_mapServantStatus,需è¦åŠ é” - static TC_ThreadLock _mapServantStatusLock; - - //åˆ†ç»„ä¿¡æ¯ - static TC_ReadersWriterData > _groupIdMap; - static TC_ReadersWriterData > _groupNameMap; - -}; - -#endif diff --git a/cpp/framework/RegistryServer/QueryImp.cpp b/cpp/framework/RegistryServer/QueryImp.cpp deleted file mode 100644 index b06a9707d..000000000 --- a/cpp/framework/RegistryServer/QueryImp.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "QueryImp.h" -#include "util/tc_clientsocket.h" - -extern TC_Config * g_pconf; - -void QueryImp::initialize() -{ - TLOGDEBUG("begin QueryImp init"< QueryImp::findObjectById(const string & id, tars::TarsCurrentPtr current) -{ - vector eps = _db.findObjectById(id); - - ostringstream os; - doDaylog(FUNID_findObjectById,id,eps,vector(),current,os); - - return eps; -} - -tars::Int32 QueryImp::findObjectById4Any(const std::string & id,vector &activeEp,vector &inactiveEp,tars::TarsCurrentPtr current) -{ - int iRet = _db.findObjectById4All(id, activeEp, inactiveEp); - - ostringstream os; - doDaylog(FUNID_findObjectById4Any,id,activeEp,inactiveEp,current,os); - - return iRet; -} - -int QueryImp::findObjectById4All(const std::string & id, vector &activeEp,vector &inactiveEp,tars::TarsCurrentPtr current) -{ - ostringstream os; - - int iRet = _db.findObjectByIdInGroupPriority(id,current->getIp(),activeEp, inactiveEp,os); - - doDaylog(FUNID_findObjectById4All,id,activeEp,inactiveEp,current,os); - - return iRet; -} - -int QueryImp::findObjectByIdInSameGroup(const std::string & id, vector &activeEp,vector &inactiveEp, tars::TarsCurrentPtr current) -{ - ostringstream os; - TLOGINFO(__FUNCTION__ << ":" << __LINE__ << "|" << id << "|" << current->getIp() << endl); - - int iRet = _db.findObjectByIdInGroupPriority(id, current->getIp(), activeEp, inactiveEp, os); - - doDaylog(FUNID_findObjectByIdInSameGroup,id,activeEp,inactiveEp,current,os); - - return iRet; -} - -Int32 QueryImp::findObjectByIdInSameStation(const std::string & id, const std::string & sStation, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current) -{ - ostringstream os; - - int iRet = _db.findObjectByIdInSameStation(id, sStation, activeEp, inactiveEp, os); - - doDaylog(FUNID_findObjectByIdInSameStation,id,activeEp,inactiveEp,current,os); - - return iRet; -} - -Int32 QueryImp::findObjectByIdInSameSet(const std::string & id,const std::string & setId,vector &activeEp,vector &inactiveEp, tars::TarsCurrentPtr current) -{ - vector vtSetInfo = TC_Common::sepstr(setId,"."); - - if (vtSetInfo.size()!=3 ||(vtSetInfo.size()==3&&(vtSetInfo[0]=="*"||vtSetInfo[1]=="*"))) - { - TLOGERROR("QueryImp::findObjectByIdInSameSet:|set full name error[" << id << "_" << setId <<"]|" << current->getIp() << endl); - return -1; - } - - ostringstream os; - int iRet = _db.findObjectByIdInSameSet(id, vtSetInfo, activeEp, inactiveEp, os); - if (-1 == iRet) - { - //未å¯åŠ¨set,å¯åŠ¨ip分组策略 - return findObjectByIdInSameGroup(id, activeEp, inactiveEp, current); - } - else if (-2 == iRet) - { - //å¯åŠ¨äº†set,但未找到任何æœåŠ¡èŠ‚点 - TLOGERROR("QueryImp::findObjectByIdInSameSet |no one server found for [" << id << "_" << setId <<"]|" << current->getIp() << endl); - return -1; - } - else if (-3 == iRet) - { - //å¯åŠ¨äº†set,但未找到任何地区set,严格上ä¸åº”该出现此类情形,é…置错误或主调设置错误会引起此类错误 - TLOGERROR("QueryImp::findObjectByIdInSameSet |no set area found [" << id << "_" << setId <<"]|" << current->getIp() << endl); - return -1; - } - - doDaylog(FUNID_findObjectByIdInSameSet,id,activeEp,inactiveEp,current,os,setId); - - return iRet; -} - -void QueryImp::doDaylog(const FUNID eFnId,const string& id,const vector &activeEp, const vector &inactiveEp, const tars::TarsCurrentPtr& current,const ostringstream& os,const string& sSetid) -{ - string sEpList; - for(size_t i = 0; i < activeEp.size(); i++) - { - if(0 != i) - { - sEpList += ";"; - } - sEpList += activeEp[i].host + ":" + TC_Common::tostr(activeEp[i].port); - } - - sEpList += "|"; - - for(size_t i = 0; i < inactiveEp.size(); i++) - { - if(0 != i) - { - sEpList += ";"; - } - sEpList += inactiveEp[i].host + ":" + TC_Common::tostr(inactiveEp[i].port); - } - - switch(eFnId) - { - case FUNID_findObjectById4All: - case FUNID_findObjectByIdInSameGroup: - { - FDLOG("query_idc") << eFunTostr(eFnId)<<"|"<getIp() << "|"<< current->getPort() << "|" << id << "|" <getIp() << "|"<< current->getPort() << "|" << id << "|" <getIp() << "|"<< current->getPort() << "|" << id << "|" < findObjectById(const string & id, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®id获å–所有对象,包括活动和éžæ´»åŠ¨å¯¹è±¡ - */ - virtual tars::Int32 findObjectById4Any(const std::string & id, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®id获å–对象所有endpoint列表 - */ - Int32 findObjectById4All(const std::string & id, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®id获å–对象åŒç»„endpoint列表 - */ - Int32 findObjectByIdInSameGroup(const std::string & id, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®id获å–对象指定归属地的endpoint列表 - */ - Int32 findObjectByIdInSameStation(const std::string & id, const std::string & sStation, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); - - /** - * æ ¹æ®id获å–对象åŒset endpoint列表 - */ - Int32 findObjectByIdInSameSet(const std::string & id,const std::string & setId,vector &activeEp,vector &inactiveEp, tars::TarsCurrentPtr current); - -private: - /** - * 打å°æŒ‰å¤©æ—¥å¿— - */ - void doDaylog(const FUNID eFnId,const string& id,const vector &activeEp, const vector &inactiveEp, const tars::TarsCurrentPtr& current,const std::ostringstream& os,const string& sSetid=""); - - /** - * 转化æˆå­—符串 - */ - string eFunTostr(const FUNID eFnId); - -protected: - - //æ•°æ®åº“æ“作 - CDbHandle _db; - -}; - -#endif diff --git a/cpp/framework/RegistryServer/ReapThread.cpp b/cpp/framework/RegistryServer/ReapThread.cpp deleted file mode 100644 index 033956a46..000000000 --- a/cpp/framework/RegistryServer/ReapThread.cpp +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ReapThread.h" - -extern TC_Config * g_pconf; - -ReapThread::ReapThread() -: _terminate(false) -, _loadObjectsInterval1(10) -, _leastChangedTime1(60) -, _loadObjectsInterval2(3600) -, _leastChangedTime2(30*60) -, _registryTimeout(150) -, _recoverProtect(true) -, _recoverProtectRate(30) -, _heartBeatOff(false) -{ -} - -ReapThread::~ReapThread() -{ - if (isAlive()) - { - terminate(); - notifyAll(); - getThreadControl().join(); - } -} - -int ReapThread::init() -{ - TLOGDEBUG("begin ReapThread init"<((*g_pconf).get("/tars/reap", "10")); - //第一阶段加载最近时间更新的记录,默认是60秒 - _leastChangedTime1 = TC_Common::strto((*g_pconf).get("/tars/reap", "150")); - - _loadObjectsInterval2 = TC_Common::strto((*g_pconf).get("/tars/reap", "300")); - - //主控心跳超时时间 - _registryTimeout = TC_Common::strto((*g_pconf)["/tars/reap"]); - - //是å¦å¯ç”¨DBæ¢å¤ä¿æŠ¤åŠŸèƒ½ - _recoverProtect = (*g_pconf).get("/tars/reap", "Y") == "N"?false:true; - - //å¯ç”¨DBæ¢å¤ä¿æŠ¤åŠŸèƒ½çŠ¶æ€ä¸‹æžé™å€¼ - _recoverProtectRate = TC_Common::strto((*g_pconf).get("/tars/reap", "30")); - - //是å¦å…³é—­æ›´æ–°ä¸»æŽ§å¿ƒè·³æ—¶é—´,一般需è¦è¿ç§»ä¸»æŽ§æœåŠ¡æ˜¯ï¼Œè®¾ç½®æ­¤é¡¹ä¸ºY - _heartBeatOff = (*g_pconf).get("/tars/reap", "N") == "Y"?true:false; - - //最å°å€¼ä¿æŠ¤ - _loadObjectsInterval1 = _loadObjectsInterval1 < 5 ? 5 : _loadObjectsInterval1; - - _registryTimeout = _registryTimeout < 5 ? 5 : _registryTimeout; - - _recoverProtectRate = _recoverProtectRate < 1 ? 30: _recoverProtectRate; - - _recoverProtect = false; - - //加载对象列表 - _db.loadObjectIdCache(_recoverProtect, _recoverProtectRate,0,true, true); - - TLOGDEBUG("ReapThread init ok."<getNow(); - - //å…¨é‡åŠ è½½æ—¶é—´ - time_t tLastLoadObjectsStep2 = TC_TimeProvider::getInstance()->getNow(); - - time_t tLastQueryServer = 0; - time_t tNow; - while(!_terminate) - { - try - { - tNow = TC_TimeProvider::getInstance()->getNow(); - - //加载对象列表 - if(tNow - tLastLoadObjectsStep1 >= _loadObjectsInterval1) - { - tLastLoadObjectsStep1 = tNow; - - _db.updateRegistryInfo2Db(_heartBeatOff); - - if(tNow - tLastLoadObjectsStep2 >= _loadObjectsInterval2) - { - tLastLoadObjectsStep2 = tNow; - //å…¨é‡åŠ è½½,_leastChangedTime2å‚数没有æ„义 - _db.loadObjectIdCache(_recoverProtect, _recoverProtectRate,_leastChangedTime2,true, false); - } - else - { - _db.loadObjectIdCache(_recoverProtect, _recoverProtectRate,_leastChangedTime1,false, false); - } - - } - - //轮询心跳超时的主控 - if(tNow - tLastQueryServer >= _registryTimeout) - { - tLastQueryServer = tNow; - _db.checkRegistryTimeout(_registryTimeout); - } - - - TC_ThreadLock::Lock lock(*this); - timedWait(100); //ms - } - catch(exception & ex) - { - TLOGERROR("ReapThread exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("ReapThread unknown exception:" << endl); - } - } -} - - - diff --git a/cpp/framework/RegistryServer/ReapThread.h b/cpp/framework/RegistryServer/ReapThread.h deleted file mode 100644 index f778f6498..000000000 --- a/cpp/framework/RegistryServer/ReapThread.h +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REAP_THREAD_H__ -#define __REAP_THREAD_H__ - -#include -#include "util/tc_thread.h" -#include "DbHandle.h" - -using namespace tars; - -////////////////////////////////////////////////////// -/** - * 用于执行定时æ“作的线程类 - */ -class ReapThread : public TC_Thread, public TC_ThreadLock -{ -public: - /** - * 构造函数 - */ - ReapThread(); - - /** - * æžæž„函数 - */ - ~ReapThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * åˆå§‹åŒ– - */ - int init(); - - /** - * 轮询函数 - */ - virtual void run(); - -protected: - /* - * 线程结æŸæ ‡å¿— - */ - bool _terminate; - - /* - * æ•°æ®åº“æ“作 - */ - CDbHandle _db; - - /* - * 加载对象列表的时间间隔,å•ä½æ˜¯ç§’ - * 第一阶段加载时间 consider - */ - int _loadObjectsInterval1; - int _leastChangedTime1; - - /* - * å…¨é‡åŠ è½½æ—¶é—´,å•ä½æ˜¯ç§’ - */ - int _loadObjectsInterval2; - int _leastChangedTime2; - - /* - * registry心跳超时时间 - */ - int _registryTimeout; - - /* - * 是å¦å¯ç”¨DBæ¢å¤ä¿æŠ¤åŠŸèƒ½ï¼Œé»˜è®¤ä¸ºæ‰“å¼€ - */ - bool _recoverProtect; - - /* - * å¯ç”¨DBæ¢å¤ä¿æŠ¤åŠŸèƒ½çŠ¶æ€ä¸‹æžé™å€¼ - */ - int _recoverProtectRate; - - /* - * 主控心跳时间更新开关 - */ - bool _heartBeatOff; -}; - -#endif diff --git a/cpp/framework/RegistryServer/RegistryImp.cpp b/cpp/framework/RegistryServer/RegistryImp.cpp deleted file mode 100644 index e19bad94d..000000000 --- a/cpp/framework/RegistryServer/RegistryImp.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include - -#include "RegistryImp.h" -#include "RegistryProcThread.h" -#include "RegistryServer.h" - -//åˆå§‹åŒ–é…ç½®db连接 -extern TC_Config * g_pconf; -extern RegistryServer g_app; - -void RegistryImp::initialize() -{ - TLOGDEBUG("begin RegistryImp init"<put(procInfo); - - return 0; -} - -vector RegistryImp::getServers(const std::string & app,const std::string & serverName,const std::string & nodeName,tars::TarsCurrentPtr current) -{ - return _db.getServers(app, serverName, nodeName); -} - -int RegistryImp::updateServer(const string & nodeName, const string & app, const string & serverName, const tars::ServerStateInfo & stateInfo, tars::TarsCurrentPtr current) -{ - return _db.updateServerState(app, serverName, nodeName, "present_state", stateInfo.serverState, stateInfo.processId); -} - -int RegistryImp::updateServerBatch(const std::vector & vecStateInfo, tars::TarsCurrentPtr current) -{ - return _db.updateServerStateBatch(vecStateInfo); -} - -int RegistryImp::destroy(const string & name, tars::TarsCurrentPtr current) -{ - return _db.destroyNode(name); -} - -int RegistryImp::reportVersion(const string & app, const string & serverName, const string & nodeName, const string & version, tars::TarsCurrentPtr current) -{ - RegistryProcInfo procInfo; - procInfo.appName = app; - procInfo.serverName = serverName; - procInfo.nodeName = nodeName; - procInfo.tarsVersion = version; - procInfo.cmd = EM_REPORTVERSION; - - //放入异步处ç†çº¿ç¨‹ä¸­ - g_app.getRegProcThread()->put(procInfo); - - return 0; -} - -int RegistryImp::getNodeTemplate(const std::string & nodeName,std::string &profileTemplate,tars::TarsCurrentPtr current) -{ - string sTemplateName; - int iRet = _db.getNodeTemplateName(nodeName, sTemplateName); - if(iRet != 0 || sTemplateName == "") - { - //默认模æ¿é…ç½® - sTemplateName = (*g_pconf)["/tars/nodeinfo"]; - } - - string sDesc; - profileTemplate = _db.getProfileTemplate(sTemplateName, sDesc); - - TLOGDEBUG(nodeName << " get sTemplateName:" << sTemplateName << " result:" << sDesc << endl); - - return 0; -} - -int RegistryImp::getClientIp(std::string &sClientIp,tars::TarsCurrentPtr current) -{ - sClientIp = current->getIp(); - TLOGDEBUG("RegistryImp::getClientIp ip: " << sClientIp << endl); - - return 0; -} - -int RegistryImp::updatePatchResult(const PatchResult & result, tars::TarsCurrentPtr current) -{ - TLOGDEBUG( "RegistryImp::updatePatchResult " << result.sApplication + "." + result.sServerName + "_" + result.sNodeName << "|V:" << result.sVersion << "|U:" << result.sUserName << endl); - - return _db.setPatchInfo(result.sApplication, result.sServerName, result.sNodeName, result.sVersion, result.sUserName); -} - diff --git a/cpp/framework/RegistryServer/RegistryImp.h b/cpp/framework/RegistryServer/RegistryImp.h deleted file mode 100644 index c73872a5a..000000000 --- a/cpp/framework/RegistryServer/RegistryImp.h +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REGISTRY_IMP_H__ -#define __REGISTRY_IMP_H__ - -#include "util/tc_common.h" -#include "util/tc_config.h" - -#include "Registry.h" -#include "DbHandle.h" - -using namespace tars; - -////////////////////////////////////////////////////// -/* - * æ供给node调用的接å£ç±» - */ -class RegistryImp: public Registry -{ -public: - /** - * 构造函数 - */ - RegistryImp(){}; - - /** - * åˆå§‹åŒ– - */ - virtual void initialize(); - - /** - ** 退出 - */ - virtual void destroy() {}; - - /** - * 获å–æ•°æ® - * @param k - * @param v - * - * @return int - */ - int get(int &i, tars::TarsCurrentPtr current); - - /** - * nodeå¯åŠ¨çš„时候往registry注册一个session - * - * @param name nodeå称 - * @param ni nodeè¯¦ç»†ä¿¡æ¯ - * @param li nodeæœºå™¨è´Ÿè½½ä¿¡æ¯ - * - * @return 注册是å¦æˆåŠŸ - */ - virtual int registerNode(const string & name, const NodeInfo & ni, const LoadInfo & li, tars::TarsCurrentPtr current); - - /** - * node上报心跳负载 - * - * @param name nodeå称 - * @param li nodeæœºå™¨è´Ÿè½½ä¿¡æ¯ - * - * @return å¿ƒè·³æŽ¥æ”¶çŠ¶æ€ - */ - virtual int keepAlive(const string& name, const LoadInfo & li, tars::TarsCurrentPtr current); - - /** - * 获å–在该node部署的server列表 - * - * @param name nodeå称 - * - * @return serverå称列表 - */ - virtual vector getServers(const std::string & app, const std::string & serverName, const std::string & nodeName, tars::TarsCurrentPtr current); - - /** - * æ›´æ–°serverçŠ¶æ€ - * - * @param nodeName : node id - * @param app: 应用 - * @param serverName: server å - * @param state : serverçŠ¶æ€ - * - * @return serverä¿¡æ¯åˆ—表 - */ - virtual int updateServer(const string & nodeName, const string & app, const string & serverName, const tars::ServerStateInfo & stateInfo, tars::TarsCurrentPtr current); - - /** - * é‡æ‰¹æ›´æ–°serverçŠ¶æ€ - * - * @param vecStateInfo : 批é‡serverçŠ¶æ€ - * - * @return serverä¿¡æ¯åˆ—表 - */ - virtual int updateServerBatch(const std::vector & vecStateInfo, tars::TarsCurrentPtr current); - - /** - * nodeåœæ­¢ï¼Œé‡Šæ”¾nodeçš„ä¼šè¯ - * - * @param name nodeå称 - */ - virtual int destroy(const string & name, tars::TarsCurrentPtr current); - - /** - * 上报serverçš„tars库版本 - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param version: server基于的tarsç‰ˆæœ¬å· - * - * @return 0-æˆåŠŸ others-失败 - */ - virtual int reportVersion(const string & app, const string & serverName, const string & nodeName, const string & version, tars::TarsCurrentPtr current); - - /** - * 获å–node的模æ¿é…ç½® - * @param nodeName: nodeå称 - * @param out profileTemplate: 对应模æ¿å†…容 - * - * @return 0-æˆåŠŸ others-失败 - */ - virtual tars::Int32 getNodeTemplate(const std::string & nodeName, std::string &profileTemplate, tars::TarsCurrentPtr current); - - /** - * node通过接å£èŽ·å–连接上主控的node ip - * @param sNodeIp: node çš„ip - * - * @return 0-æˆåŠŸ others-失败 - */ - virtual tars::Int32 getClientIp(std::string &sClientIp, tars::TarsCurrentPtr current); - - /** - * å‘布任务完æˆåŽï¼ŒUPDATE版本å·å’Œå‘布人 - * @param PatchResult: å‘布结果 - * - * @return 0-æˆåŠŸ othres-失败 - */ - virtual tars::Int32 updatePatchResult(const PatchResult & result, tars::TarsCurrentPtr current); - -protected: - - /* - * æ•°æ®åº“æ“作 - */ - CDbHandle _db; - -}; - - -#endif diff --git a/cpp/framework/RegistryServer/RegistryProcThread.cpp b/cpp/framework/RegistryServer/RegistryProcThread.cpp deleted file mode 100644 index ee68da4fd..000000000 --- a/cpp/framework/RegistryServer/RegistryProcThread.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "RegistryProcThread.h" - -using namespace tars; - -//åˆå§‹åŒ–æ•°æ®åº“实例用 -extern TC_Config * g_pconf; - -////////////////////////////////////////////////////// -RegistryProcThread::RegistryProcThread() -: _terminate(false) -{ - TLOGDEBUG("RegistryProcThread init ok"<isAlive()) - { - _runners[i]->terminate(); - - _queue.notifyT(); - } - } - - for (uint32_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->getThreadControl().join(); - } - } - - _queue.clear(); - -} - -void RegistryProcThread::start(int num) -{ - for (int i = 0; i < num; ++i) - { - RegistryProcThreadRunnerPtr r = new RegistryProcThreadRunner(this); - - r->start(); - - _runners.push_back(r); - } -} - -void RegistryProcThread::put(const RegistryProcInfo& info) -{ - if(!_terminate) - { - _queue.push_back(info); - } -} - -bool RegistryProcThread::pop(RegistryProcInfo& info) -{ - return _queue.pop_front(info,1000); -} - -////////////////////////////////////////////////////// -RegistryProcThreadRunner::RegistryProcThreadRunner(RegistryProcThread* proc) -: _terminate(false) -, _proc(proc) -{ - _db.init(g_pconf); -} - -void RegistryProcThreadRunner::terminate() -{ - _terminate = true; -} - -void RegistryProcThreadRunner::run() -{ - while (!_terminate) - { - try - { - RegistryProcInfo info; - if(_proc->pop(info) && !_terminate) - { - TLOGDEBUG("RegistryProcThreadRunner:run cmd:"< RegistryProcThreadRunnerPtr; - -////////////////////////////////////////////////////// -/** - * 处ç†å¼‚æ­¥æ“作的线程池对象 - */ -class RegistryProcThread : public TC_HandleBase -{ -public: - /** - * 构造函数 - */ - RegistryProcThread(); - - /** - * æžæž„函数 - */ - virtual ~RegistryProcThread(); - - /** - * åœæ­¢è¿è¡Œ - */ - void terminate(); - - /** - * æ’入请求 - */ - void put(const RegistryProcInfo& info); - - /** - * å–请求 - */ - bool pop(RegistryProcInfo& info); - - /** - * å¯åŠ¨å¤„ç†è¯·æ±‚线程 - */ - void start(int num=1); - - friend class RegistryProcThreadRunner; - -protected: - /* - * 线程结æŸæ ‡å¿— - */ - bool _terminate; - - /* - * 请求队列 - */ - TC_ThreadQueue _queue; - - /* - * çº¿ç¨‹å¯¹è±¡é›†åˆ - */ - vector _runners; -}; - -typedef TC_AutoPtr RegistryProcThreadPtr; - -////////////////////////////////////////////////////// -/** - * 处ç†å¼‚æ­¥æ“作的线程 - */ -class RegistryProcThreadRunner : public TC_Thread, public TC_HandleBase -{ -public: - /* - * 构造函数 - */ - RegistryProcThreadRunner(RegistryProcThread* proc); - - /* - * 线程执行函数 - */ - virtual void run(); - - /* - * åœæ­¢è¿è¡Œ - */ - void terminate(); - -private: - /* - * 线程结æŸæ ‡å¿— - */ - bool _terminate; - - /* - * 线程池对象指针 - */ - RegistryProcThread * _proc; - - /* - * 访问主控db的处ç†ç±» - */ - CDbHandle _db; -}; - -#endif diff --git a/cpp/framework/RegistryServer/RegistryServer.cpp b/cpp/framework/RegistryServer/RegistryServer.cpp deleted file mode 100644 index 15cf7fe0b..000000000 --- a/cpp/framework/RegistryServer/RegistryServer.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "RegistryServer.h" - -extern TC_Config *g_pconf; - -void RegistryServer::initialize() -{ - TLOGDEBUG("RegistryServer::initialize..." << endl); - - try - { - //加载registry对象的端å£ä¿¡æ¯ - loadServantEndpoint(); - - //ReapThreadåˆå§‹åŒ–时会用到 - TarsTimeLogger::getInstance()->initFormat("group_id", "%Y%m%d%H"); - TarsTimeLogger::getInstance()->enableRemote("group_id", true); - - //å…¨é‡å’Œå¢žé‡åŠ è½½è·¯ç”±ä¿¡æ¯çš„线程 - _reapThread.init(); - _reapThread.start(); - - //检查node超时的线程 - _checkNodeThread.init(); - _checkNodeThread.start(); - - //监控所有æœåŠ¡çŠ¶æ€çš„线程 - _checksetingThread.init(); - _checksetingThread.start(); - - //异步处ç†çº¿ç¨‹ - _registryProcThread = new RegistryProcThread(); - int num = TC_Common::strto(g_pconf->get("/tars/reap", "3")); - _registryProcThread->start(num); - - //ä¾›node访问的对象 - addServant((*g_pconf)["/tars/objname"]); - - //ä¾›tarsçš„æœåŠ¡èŽ·å–路由的对象 - addServant((*g_pconf)["/tars/objname"]); - - TarsTimeLogger::getInstance()->enableRemote("", false); - - TarsTimeLogger::getInstance()->initFormat("query_set", "%Y%m%d%H"); - TarsTimeLogger::getInstance()->enableRemote("query_set", false); - - TarsTimeLogger::getInstance()->initFormat("query_idc", "%Y%m%d%H"); - TarsTimeLogger::getInstance()->enableRemote("query_idc", false); - - TarsTimeLogger::getInstance()->initFormat("query", "%Y%m%d%H"); - TarsTimeLogger::getInstance()->enableRemote("query", false); - } - catch (TC_Exception& ex) - { - TLOGERROR("RegistryServer initialize exception:" << ex.what() << endl); - cerr << "RegistryServer initialize exception:" << ex.what() << endl; - exit(-1); - } - - TLOGDEBUG("RegistryServer::initialize OK!" << endl); -} - - -void RegistryServer::destroyApp() -{ - if (_registryProcThread) - { - _registryProcThread->terminate(); - } - - TLOGDEBUG("RegistryServer::destroyApp ok" << endl); -} - -RegistryProcThread* RegistryServer::getRegProcThread() -{ - return _registryProcThread.get(); -} - -int RegistryServer::loadServantEndpoint() -{ - map mapAdapterServant = ServantHelperManager::getInstance()->getAdapterServant(); - - map::iterator iter; - for (iter = mapAdapterServant.begin(); iter != mapAdapterServant.end(); iter++) - { - TC_Endpoint ep = getEpollServer()->getBindAdapter(iter->first)->getEndpoint(); - - _mapServantEndpoint[iter->second] = ep.toString(); - - TLOGDEBUG("registry obj: " << iter->second << " = " << ep.toString() << endl); - } - - return 0; -} - -TC_Endpoint RegistryServer::getAdapterEndpoint(const string& name) const -{ - TC_Endpoint locator; - - try - { - locator = getEpollServer()->getBindAdapter(name)->getEndpoint(); - } - catch (exception& ex) - { - TLOGERROR("RegistryServer::getAdapterEndpoint exception: " << ex.what() << endl); - } - - return locator; -} - - diff --git a/cpp/framework/RegistryServer/RegistryServer.h b/cpp/framework/RegistryServer/RegistryServer.h deleted file mode 100644 index 50f3b844a..000000000 --- a/cpp/framework/RegistryServer/RegistryServer.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/Application.h" -#include "RegistryImp.h" -#include "QueryImp.h" -#include "ReapThread.h" -#include "CheckNodeThread.h" -#include "CheckSettingState.h" -#include "RegistryProcThread.h" - -using namespace tars; - -////////////////////////////////////////////////////// -/** - * 主控æœåŠ¡ - */ -class RegistryServer : public Application -{ -public: - /** - * åˆå§‹åŒ–, æœåŠ¡å¯åŠ¨æ—¶ä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžæž„, æœåŠ¡é€€å‡ºæ—¶ä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void destroyApp(); - - /** - * 获å–registry对象的端å£ä¿¡æ¯ - */ - map getServantEndpoint() { return _mapServantEndpoint; } - - /** - * 加载registry对象的端å£ä¿¡æ¯ - */ - int loadServantEndpoint(); - - /** - * 获å–异步处ç†çº¿ç¨‹çš„对象指针 - */ - RegistryProcThread * getRegProcThread(); - - /** - * æ ¹æ®name获å–对应的ipã€ç«¯å£ç­‰ä¿¡æ¯ - */ - TC_Endpoint getAdapterEndpoint(const string& name ) const; - -protected: - - /* - * 用于执行定时æ“作的线程对象 - */ - ReapThread _reapThread; //å…¨é‡å’Œå¢žé‡åŠ è½½è·¯ç”±ä¿¡æ¯çš„线程 - - CheckNodeThread _checkNodeThread; //监控tarsnode超时的线程 - - CheckSettingState _checksetingThread; //监控所有æœåŠ¡çŠ¶æ€çš„线程 - - RegistryProcThreadPtr _registryProcThread; //处ç†å¿ƒè·³ã€ä¸ŠæŠ¥ç­‰çš„异步线程 - - /* - * 对象-适é…器 列表 - */ - map _mapServantEndpoint; - -}; - - diff --git a/cpp/framework/RegistryServer/main.cpp b/cpp/framework/RegistryServer/main.cpp deleted file mode 100644 index b52620219..000000000 --- a/cpp/framework/RegistryServer/main.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "RegistryServer.h" -#include - -using namespace tars; - -RegistryServer g_app; -TC_Config * g_pconf; - -int main(int argc, char *argv[]) -{ - try - { - g_pconf = & g_app.getConfig(); - g_app.main(argc, argv); - - g_app.waitForShutdown(); - } - catch(exception &ex) - { - cerr<< ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/StatServer/CMakeLists.txt b/cpp/framework/StatServer/CMakeLists.txt deleted file mode 100644 index 59fd9659f..000000000 --- a/cpp/framework/StatServer/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ - -set(MODULE "tarsstat") - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/deploy/${MODULE}/) - -complice_module(${MODULE}) diff --git a/cpp/framework/StatServer/ReapSSDThread.cpp b/cpp/framework/StatServer/ReapSSDThread.cpp deleted file mode 100644 index 4e5f5e816..000000000 --- a/cpp/framework/StatServer/ReapSSDThread.cpp +++ /dev/null @@ -1,451 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ReapSSDThread.h" -#include "util/tc_config.h" -#include "StatServer.h" - - -ReapSSDProcThread::ReapSSDProcThread(ReapSSDThread * proc) -: _terminate(false) -, _proc(proc) -{ -} - -ReapSSDProcThread::~ReapSSDProcThread() -{ - if(isAlive()) - { - terminate(); - getThreadControl().join(); - } - _queue.clear(); -} - -void ReapSSDProcThread::terminate() -{ - _terminate = true; - - _queue.notifyT(); -} - -void ReapSSDProcThread::put(QueueItem data) -{ - if(!_terminate) - { - _queue.push_back(data); - } -} - -bool ReapSSDProcThread::pop(QueueItem & data) -{ - return _queue.pop_front(data, 1000); -} - -int ReapSSDProcThread::sendAlarmSMS(const string &sMsg) -{ - string errInfo = " ERROR:" + ServerConfig::LocalIp + "_" + sMsg; - TARS_NOTIFY_ERROR(errInfo); - - return 0; -} - -void ReapSSDProcThread::run() -{ - string sDate1(""); - string sFlag1(""); - string sDate2(""); - string sFlag2(""); - - while (!_terminate) - { - try - { - sDate1 = ""; - sFlag1 = ""; - sDate2 = ""; - sFlag2 = ""; - - QueueItem item; - - if(pop(item)) - { - if(item._statmsg != NULL) - { - int64_t iBegin = TNOWMS; - int64_t iEnd = 0; - - StatDbManager::getInstance()->insert2MultiDbs(item._index, *item._statmsg, item._date, item._tflag); - - iEnd = TNOWMS; - - TLOGDEBUG("ReapSSDProcThread::run stat ip:" << ServerConfig::LocalIp << "|dbIndex:" << item._index << "|" << StatDbManager::getInstance()->getIpAndPort(item._index) - << "|date:" << item._date << "|tflag:" << item._tflag << "|timecost(ms):" << (iEnd - iBegin) << "|iBegin:" << iBegin << "|iEnd:" << iEnd << endl); - - FDLOG("CountStat") << "ReapSSDProcThread::run stat ip:" << ServerConfig::LocalIp << "|dbIndex:" << item._index << "|" << StatDbManager::getInstance()->getIpAndPort(item._index) - << "|date:" << item._date << "|tflag:" << item._tflag << "|timecost(ms):" << (iEnd - iBegin) << "|iBegin:" << iBegin << "|iEnd:" << iEnd << endl; - - if((iEnd - iBegin)/1000 > (g_app.getInserInterv() - 2) * 60) - { - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|ReapSSDProcThread::run timeout 8 minute." << endl; - string sMsg("stat ip:"); - sMsg += ServerConfig::LocalIp; - sMsg += " ReapSSDProcThread::run write db:"; - sMsg += StatDbManager::getInstance()->getIpAndPort(item._index); - sMsg += "|timeout "; - sMsg += TC_Common::tostr(g_app.getInserInterv() - 2); - sMsg += " Minute."; - - sendAlarmSMS(sMsg); - } - - delete item._statmsg; - item._statmsg = NULL; - } - else - { - TLOGERROR("ReapSSDProcThread::run item._statmsg == NULL." << endl); - } - } - } - catch(exception& e) - { - TLOGERROR("ReapSSDProcThread::run exception:" << e.what() << endl); - FDLOG("CountStat") << "ReapSSDProcThread::run exception:" << e.what() << endl; - } - } -} -////////////////////////////////////////////////////////////// -ReapSSDThread::ReapSSDThread() -: _terminate(false) -, _curWeight(0) -, _lastSq(-1) -{ - TLOGDEBUG("ReapSSDThread begin ok" << endl); -} - -ReapSSDThread::~ReapSSDThread() -{ - if (isAlive()) - { - terminate(); - - getThreadControl().join(); - } -} - -void ReapSSDThread::terminate() -{ - TLOGDEBUG("ReapSSDThread terminate." << endl); - - _terminate = true; - - TC_ThreadLock::Lock lock(*this); - - notifyAll(); -} - -void ReapSSDThread::run() -{ - int iInsertDataNum = StatDbManager::getInstance()->getDbIpNum(); - - for(int i = 0; i < iInsertDataNum; ++i) - { - ReapSSDProcThread *r = new ReapSSDProcThread(this); - - r->start(); - - _runners.push_back(r); - } - - string sDate,sTime; - - int dbNumber = StatDbManager::getInstance()->getDbNumber(); - - string sRandOrder; - - uint64_t iTotalNum = 0; - - int iLastIndex = -1; - - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|ReapSSDThread::run iInsertDataThread:" << iInsertDataNum << "|dbNumber:" << dbNumber << endl; - - while (!_terminate) - { - try - { - //åŒbuffer中一个buffer入库 - int iBufferIndex = !(g_app.getSelectBufferIndex()); - int64_t iInterval = 3; - if(iBufferIndex != iLastIndex && g_app.getSelectBuffer(iBufferIndex, iInterval)) - { - iLastIndex = iBufferIndex; - - iTotalNum = 0; - - vector vAllStatMsg; - for(int iStatIndex = 0; iStatIndex < dbNumber; ++iStatIndex) - { - vAllStatMsg.push_back(new StatMsg()); - } - - int64_t tBegin = TNOWMS; - - getDataFromBuffer(iBufferIndex, vAllStatMsg, iTotalNum); - - int64_t tEnd = TNOWMS; - - TLOGDEBUG("stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|ReapSSDThread::run getDataFromBuffer timecost(ms):" << (tEnd - tBegin) << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|ReapSSDThread::run getDataFromBuffer timecost(ms):" << (tEnd - tBegin) << endl; - - TLOGDEBUG("stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|ReapSSDThread::run insert begin _vAllStatMsg.size:" << vAllStatMsg.size() << "|record num:" << iTotalNum << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|ReapSSDThread::run insert begin _vAllStatMsg.size:" << vAllStatMsg.size() << "|record num:" << iTotalNum << endl; - - if(iTotalNum <= 0) - { - for(int iStatIndex = 0; iStatIndex < dbNumber; ++iStatIndex) - { - delete vAllStatMsg[iStatIndex]; - } - - vAllStatMsg.clear(); - } - else - { - - string sFile=""; - string sDate=""; - string sFlag=""; - time_t time=0; - g_app.getTimeInfo(time,sDate,sFlag); - - //size_t iSize = vAllStatMsg.size(); - - QueueItem item; - int iInsertThreadIndex = 0; - sRandOrder = g_app.getRandOrder(); - - if (sRandOrder == "") - { - sRandOrder = "0"; - } - - map >& mIpHasDbInfo = StatDbManager::getInstance()->getIpHasDbInfo(); - map >::iterator m_iter = mIpHasDbInfo.begin(); - - while(m_iter != mIpHasDbInfo.end()) - { - vector &vDb = m_iter->second; - - for(size_t i = 0; i < vDb.size(); ++i) - { - int k = (i + TC_Common::strto(sRandOrder)) % vDb.size(); - - item._index = vDb[k]; - item._date = sDate; - item._tflag = sFlag; - item._statmsg = vAllStatMsg[item._index]; - - iInsertThreadIndex = StatDbManager::getInstance()->getDbToIpIndex(vDb[k]); - - assert(iInsertThreadIndex >= 0); - - _runners[iInsertThreadIndex]->put(item); - } - - ++m_iter; - } - - if(_terminate) - { - break; - } - } - - for(int k = 0; k < g_app.getBuffNum(); ++k) - { - StatHashMap *pHashMap = g_app.getHashMapBuff(iBufferIndex, k); - pHashMap->clear(); - } - - TLOGDEBUG("stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|ReapSSDThread::run insert record num:" << iTotalNum << "|tast patch finished." << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iBufferIndex << "|ReapSSDThread::run insert record num:" << iTotalNum << "|tast patch finished." << endl; - } - - } - catch(exception& ex) - { - TLOGERROR("ReapSSDThread::run exception:"<< ex.what() << endl); - } - catch(...) - { - TLOGERROR("ReapSSDThread::run ReapSSDThread unkonw exception catched" << endl); - } - - TC_ThreadLock::Lock lock(*this); - timedWait(REAP_INTERVAL); - } - - TLOGDEBUG("ReapSSDThread run setITerminateFlag true." << endl); - - StatDbManager::getInstance()->setITerminateFlag(true); - - for(size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]->isAlive()) - { - _runners[i]->terminate(); - - _runners[i]->getThreadControl().join(); - } - } - - for(size_t i = 0; i < _runners.size(); ++i) - { - if(_runners[i]) - { - delete _runners[i]; - _runners[i] = NULL; - } - } - - TLOGDEBUG("ReapSSDThread run terminate." << endl); -} - -int ReapSSDThread::getIndexWithWeighted(int iMaxDb,int iGcd,int iMaxW,const vector& vDbWeight) -{ - while (true){ - - _lastSq = (_lastSq + 1) % iMaxDb; - - if (_lastSq == 0) - { - _curWeight = _curWeight - iGcd; - if (_curWeight <= 0) - { - _curWeight = iMaxW; - if(_curWeight == 0) - { - return 0; - } - } - } - - if (vDbWeight[_lastSq] >= _curWeight) - { - return _lastSq; - } - } -} -void ReapSSDThread::getDataFromBuffer(int iIndex, vector &vAllStatMsg, uint64_t &iTotalNum) -{ - TLOGDEBUG("ReapSSDThread::getDataFromBuffer iIndex:" << iIndex << "|begin..." << endl); - - try - { - int iCount = 0,dbSeq=0; - - //获å–db个数 - int dbNumber = StatDbManager::getInstance()->getDbNumber(); - - vector vDbWeight; - int iGcd = 0,iMaxW = 0; - - StatDbManager::getInstance()->getDbWeighted(iGcd,iMaxW,vDbWeight); - - bool bEnable = StatDbManager::getInstance()->IsEnableWeighted(); - - for(int k = 0; k < g_app.getBuffNum(); ++k) - { - if(_terminate) - { - break; - } - - StatHashMap *pHashMap = g_app.getHashMapBuff(iIndex, k); - - if(pHashMap->size() == 0) - { - continue ; - } - - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iIndex << "|ReapSSDThread::getData load hashmap k:" << k << endl; - - HashMap::lock_iterator it = pHashMap->beginSetTime(); - while ( it != pHashMap->end() ) - { - if(_terminate) - { - break; - } - - StatMicMsgHead head; - StatMicMsgBody body; - int ret = it->get( head, body ); - if ( ret < 0 ) - { - ++it; - continue; - } - - if (dbNumber > 0) - { - if(bEnable)//按æƒé‡å…¥åº“ - { - dbSeq = getIndexWithWeighted(dbNumber,iGcd,iMaxW,vDbWeight); - TLOGINFO("ReapSSDThread::getIndexWithWeighted |" << dbSeq << endl); - } - else - { - dbSeq = iCount % dbNumber; - } - - (*(vAllStatMsg[dbSeq]))[head] = body; - } - - iCount++; - - ++it; - } - - } - - iTotalNum = iCount; - - TLOGDEBUG("ReapSSDThread::getDataFromBuffer Buffer Index:" << iIndex << "|get total size:" << iCount << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iIndex << "|ReapSSDThread::getData get total size:" << iCount << "|end..." << endl; - } - catch (exception& ex) - { - TLOGERROR("ReapSSDThread::getDataFromBuffer exception:" << ex.what() << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|Buffer Index:" << iIndex << "|ReapSSDThread::getData exception:" << ex.what() << endl; - - string sMsg("ReapSSDThread::getDataFromBuffer Buffer Index:"); - sMsg += TC_Common::tostr(iIndex); - sMsg += " exception:"; - sMsg += ex.what(); - sendAlarmSMS(sMsg); - } -} - -int ReapSSDThread::sendAlarmSMS(const string &sMsg) -{ - string errInfo = " ERROR:" + ServerConfig::LocalIp + "_" + sMsg; - TARS_NOTIFY_ERROR(errInfo); - - return 0; -} - diff --git a/cpp/framework/StatServer/ReapSSDThread.h b/cpp/framework/StatServer/ReapSSDThread.h deleted file mode 100644 index 1424d6f93..000000000 --- a/cpp/framework/StatServer/ReapSSDThread.h +++ /dev/null @@ -1,141 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __REAP_SSD_THREAD_H_ -#define __REAP_SSD_THREAD_H_ - -#include -#include "util/tc_thread.h" -#include "util/tc_mysql.h" -#include "servant/StatF.h" -#include "util/tc_common.h" -#include "servant/TarsLogger.h" -#include "StatImp.h" -#include "StatDbManager.h" - -using namespace tars; - -//////////////////////////////////////// -class QueueItem -{ -public: - size_t _index; - string _date; - string _tflag; - StatMsg *_statmsg; - - QueueItem() - : _index(0) - , _date("") - , _tflag("") - , _statmsg(NULL) - {} -}; -//////////////////////////////////////// -class ReapSSDThread; -//////////////////////////////////////// -/** - * å‘æ•°æ®åº“æ’入数æ®çš„线程类 - */ -class ReapSSDProcThread : public TC_Thread -{ -public: - enum - { - TIME_INTERVAL = 5000//更新业务线程时间 - }; - ReapSSDProcThread(ReapSSDThread * proc); - - ~ReapSSDProcThread(); - - void terminate(); - - virtual void run(); - - void put(QueueItem data); - - bool pop(QueueItem & data); - - int sendAlarmSMS(const string &sMsg); - -private: - bool _terminate; - - ReapSSDThread * _proc; - - TC_ThreadQueue _queue; -}; -//////////////////////////////////////// -/** - * 用于执行定时æ“作的线程类 - */ -class ReapSSDThread : public TC_Thread,public TC_ThreadLock -{ -public: - /** - * å®šä¹‰å¸¸é‡ - */ - enum - { - REAP_INTERVAL = 5000, /**轮训入库间隔时间**/ - }; - /** - * 构造 - */ - ReapSSDThread(); - - /** - * æžå¤Ÿ - */ - ~ReapSSDThread(); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * 轮询函数 - */ - virtual void run(); - -private: - /* - * 从buffer中å–æ•°æ® - */ - void getDataFromBuffer(int iIndex, vector &vAllStatMsg, uint64_t &iTotalNum); - - int sendAlarmSMS(const string &sMsg); - - /** - * 通过æƒé‡è½®è¯¢è°ƒåº¦ç®—法获å–è¦æ’入数æ®çš„db index - * @param iMaxDb db个数 - * @param iGcd 所有æƒé‡çš„最大公约数 - * @param iMaxW 最大æƒé‡å€¼ - * @param vDbWeight 所有dbçš„æƒé‡å€¼ - * - * @return int - */ - int getIndexWithWeighted(int iMaxDb,int iGcd,int iMaxW,const vector& vDbWeight); - -private: - bool _terminate; - int _curWeight; - int _lastSq; - vector _runners; -}; - -#endif diff --git a/cpp/framework/StatServer/StatDbManager.cpp b/cpp/framework/StatServer/StatDbManager.cpp deleted file mode 100644 index eb080671f..000000000 --- a/cpp/framework/StatServer/StatDbManager.cpp +++ /dev/null @@ -1,671 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "StatDbManager.h" -#include "util/tc_config.h" -#include "StatServer.h" -#include "StatHashMap.h" - -/////////////////////////////////////////////////////////// -StatDbManager::StatDbManager() -: _terminate(false) -, _lastDayTimeTable("") -{ - TLOGDEBUG("begin StatDbManager init" << endl); - - _sql = (*g_pconf)["/tars"]; - _sqlStatus = g_pconf->get("/tars", ""); - _tbNamePre = g_pconf->get("/tars/db","t_stat_realtime_"); - _maxInsertCount = TC_Common::strto(g_pconf->get("/tars/reapSql","1000")); - - //默认ä¸ä½¿ç”¨æƒé‡ - _enableWeighted = TC_Common::strto(g_pconf->get("/tars/","0")); - - size_t iInsertDbThreaad = TC_Common::strto(g_pconf->get("/tars/reapSql","4")); - - if (_sqlStatus == "") - { - _sqlStatus = "CREATE TABLE `t_ecstatus` ( " - " `id` int(11) NOT NULL auto_increment, " - " `appname` varchar(64) NOT NULL default '', " - " `action` tinyint(4) NOT NULL default '0', " - " `checkint` smallint(6) NOT NULL default '10', " - " `lasttime` varchar(16) NOT NULL default '', " - " PRIMARY KEY (`appname`,`action`), " - " UNIQUE KEY `id` (`id`) " - " ) ENGINE=HEAP DEFAULT CHARSET=utf8"; - } - - string sCutType = g_pconf->get("/tars/reapSql","hour"); - if (sCutType == "day") - { - _eCutType = CUT_BY_DAY; - } - else if (sCutType == "minute") - { - _eCutType = CUT_BY_MINUTE; - } - else - { - _eCutType = CUT_BY_HOUR; - } - - vector vDb =g_pconf->getDomainVector("/tars/multidb"); - - _dbNumber = vDb.size(); - - TLOGDEBUG("StatDbManager init multidb size:" <<_dbNumber << endl); - - map mIp; - vector vIp; - string sIp(""); - string sIpAndPort(""); - - for (int i=0; i< _dbNumber; i++) - { - TC_DBConf tConf; - tConf.loadFromMap(g_pconf->getDomainMap("/tars/multidb/" + vDb[i])); - - _vsTbNamePre.push_back(g_pconf->get("/tars/multidb/" + vDb[i] + "", "t_stat_0" + TC_Common::tostr(i) + "_")); - - sIp = tConf._host; - - sIpAndPort = "ip:"; - sIpAndPort += tConf._host; - sIpAndPort += "|port:"; - sIpAndPort += TC_Common::tostr(tConf._port); - - _vIpAndPort.push_back(sIpAndPort); - - map::const_iterator iter = mIp.find(sIp); - if(iter == mIp.end()) - { - mIp.insert(map::value_type(sIp, 0)); - - _dbIpNum++; - - vector vIndex; - vIndex.push_back(i); - - _mIpHasDbInfo.insert(map >::value_type(sIp, vIndex)); - } - else - { - _mIpHasDbInfo[sIp].push_back(i); - } - - vIp.push_back(sIp); - - //默认值为1 - _vDbWeighted.push_back(TC_Common::strto(g_pconf->get("/tars/multidb/" + vDb[i] + "","1"))); - - TC_Mysql *pMysql = new TC_Mysql(); - pMysql->init(tConf); - - _vMysql.push_back(pMysql); - } - - //如果写db的线程å°äºŽdb ip的个数,则设置db ip的个数为写db的线程数 - if(iInsertDbThreaad < _dbIpNum) - { - _dbIpNum = iInsertDbThreaad; - } - - TLOGDEBUG("StatDbManager init insert DB threadnum:" << _dbIpNum << endl); - - //设置æ¯ä¸ªdb ip使用写db的线程下标,å³æ¯ä¸ªå†™db线程负责写哪些ipçš„dbæ•°æ® - size_t iIndex = 0; - map::iterator map_it = mIp.begin(); - while(map_it != mIp.end()) - { - map_it->second = iIndex; - ++iIndex; - - if(iIndex == _dbIpNum) - { - iIndex = 0; - } - - ++map_it; - } - - //设置æ¯ä¸ªdb实例使用写db的线程下标,å³æ¯ä¸ªå†™db线程负责写哪些dbå®žä¾‹çš„æ•°æ® - for(size_t i = 0; i < vIp.size(); ++i) - { - map::const_iterator iter = mIp.find(vIp[i]); - if(iter != mIp.end()) - { - _mDbToIp.insert(map::value_type(i, iter->second)); - } - else - { - TLOGERROR("DbManager init DbToIp error ip:" << vIp[i] << endl); - } - } - - map >::iterator m_iter = _mIpHasDbInfo.begin(); - while(m_iter != _mIpHasDbInfo.end()) - { - vector &vDb = m_iter->second; - - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|ip:" << m_iter->first << "|DbNum:" << vDb.size() << endl; - - ++m_iter; - } - - - TLOGDEBUG("StatDbManager init ok." << endl); -} -/////////////////////////////////////////////////////////// -StatDbManager::~StatDbManager() -{ - vector::iterator it = _vMysql.begin(); - while(it != _vMysql.end()) - { - if(*it) - { - delete *it; - } - ++it; - } -} -/////////////////////////////////////////////////////////// -size_t StatDbManager::getDbToIpIndex(size_t iIndex) -{ - map::const_iterator iter = _mDbToIp.find(iIndex); - if(iter != _mDbToIp.end()) - { - return iter->second; - } - return -1; -} -/////////////////////////////////////////////////////////// -bool StatDbManager::IsEnableWeighted() -{ - return _enableWeighted; -} -/////////////////////////////////////////////////////////// -string StatDbManager::getIpAndPort(size_t iDbIndex) -{ - assert(iDbIndex < _vIpAndPort.size()); - - return _vIpAndPort[iDbIndex]; -} -/////////////////////////////////////////////////////////// -void StatDbManager::getDbWeighted(int& iGcd,int& iMaxW,vector& vDbWeighted) -{ - vDbWeighted = _vDbWeighted; - - int gcd = 0; - int iMax = 0; - for(size_t i = 0;i < vDbWeighted.size(); i++) - { - gcd = getGcd(gcd,vDbWeighted[i]); - - if(vDbWeighted[i] > iMax) - { - iMax = vDbWeighted[i]; - } - } - - iGcd = gcd; - - iMaxW = iMax; -} -/////////////////////////////////////////////////////////// -int StatDbManager::getGcd (int a, int b) -{ - int c; - while(a != 0) - { - c = a; - a = b%a; - b = c; - } - - return b; -} -/////////////////////////////////////////////////////////// -int StatDbManager::genRandOrder() -{ - ostringstream os; - _vRandOrder.clear(); - srand((unsigned)time(NULL)); - int iValue = 0; - size_t i = 0; - while(1) - { - int iFound = 0; - iValue = rand()%(_dbNumber ); - for (i=0; i<_vRandOrder.size(); i++) - { - if (iValue == _vRandOrder[i]) - { - iFound = 1; - break; - } - } - if ( iFound == 0) - { - _vRandOrder.push_back(iValue); - } - else - { - continue; - } - if (_vRandOrder.size() == (size_t)_dbNumber ) - { - break; - } - } - - vector::iterator it = _vRandOrder.begin(); - while(it != _vRandOrder.end() ) - { - os << *it++ << " " ; - } - TLOGDEBUG("randorder: " << os.str() << endl); - return 0; - -} -/////////////////////////////////////////////////////////// -int StatDbManager::insert2Db(const StatMsg &statmsg, const string &sDate, const string &sFlag, const string &sTbNamePre, TC_Mysql *pMysql) -{ - int iCount = 0; - string sSql; - string strValues; - - string sTbName = (sTbNamePre != "" ? sTbNamePre : _tbNamePre); - sTbName += TC_Common::replace(sDate, "-", ""); - sTbName += sFlag.substr(0,_eCutType * 2); - - try - { - string sEscapeString = pMysql->escapeString(ServerConfig::LocalIp); - - creatTable(sTbName,pMysql); - - for (StatMsg::const_iterator it = statmsg.begin(); it != statmsg.end(); it++ ) - { - if(_terminate) - { - return -1; - } - - const StatMicMsgHead& head = it->first; - const StatMicMsgBody& body = it->second; - - string strIntervCount; - int iTemp = 0; - for (map::const_iterator it = body.intervalCount.begin();it != body.intervalCount.end();it++) - { - if (iTemp != 0) - { - strIntervCount += ","; - } - strIntervCount = strIntervCount; - strIntervCount += TC_Common::tostr(it->first); - strIntervCount += "|"; - strIntervCount += TC_Common::tostr(it->second); - iTemp++; - } - - int iAveTime = 0; - if ( body.count != 0 ) - { - iAveTime = body.totalRspTime/body.count; - iAveTime == 0?iAveTime=1:iAveTime=iAveTime; - } - - //组织sqlè¯­å¥ - strValues = " ('"; - strValues += sEscapeString; - strValues += "','"; - strValues += sDate; - strValues += "','"; - strValues += sFlag; - strValues += "','"; - strValues += pMysql->escapeString(head.masterName); - strValues += "','"; - strValues += pMysql->escapeString(head.slaveName); - strValues += "','"; - strValues += pMysql->escapeString(head.interfaceName); - strValues += "','"; - strValues += pMysql->escapeString(head.tarsVersion); - strValues += "','"; - strValues += pMysql->escapeString(head.masterIp); - strValues += "','"; - strValues += pMysql->escapeString(head.slaveIp); - strValues += "',"; - strValues += TC_Common::tostr( head.slavePort); - strValues += ","; - strValues += TC_Common::tostr( head.returnValue ); - strValues += ","; - strValues += TC_Common::tostr(body.count); - strValues += ","; - strValues += TC_Common::tostr(body.timeoutCount); - strValues += ","; - strValues += TC_Common::tostr(body.execCount); - strValues += ","; - strValues += TC_Common::tostr(body.totalRspTime); - strValues += ",'"; - strValues += strIntervCount; - strValues += "',"; - strValues += TC_Common::tostr(iAveTime); - strValues += ","; - strValues += TC_Common::tostr(body.maxRspTime); - strValues += ","; - strValues += TC_Common::tostr(body.minRspTime ) + ") "; - - if( iCount == 0 ) - { - sSql = "insert ignore into "; - sSql += sTbName; - sSql += " (source_id,f_date,f_tflag,master_name,slave_name,interface_name,tars_version,master_ip,slave_ip,slave_port,return_value, succ_count,timeout_count,exce_count,total_time,interv_count,ave_time,maxrsp_time, minrsp_time) values "; - sSql += strValues; - } - else - { - sSql += ","; - sSql += strValues; - } - - iCount ++; - if ( iCount >= _maxInsertCount ) - { - usleep(100); - pMysql->execute(sSql); - TLOGDEBUG("insert " << sTbName << " affected:" << iCount << endl); - - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|insert " << sTbName << "|insert affected:" << iCount << "|mysql affected:" << pMysql->getAffectedRows() << endl; - iCount = 0; - } - } - - if ( iCount != 0 ) - { - pMysql->execute(sSql); - TLOGDEBUG("insert " << sTbName << " affected:" << iCount << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|insert " << sTbName << "|insert affected:" << iCount << "|mysql affected:" << pMysql->getAffectedRows() << endl; - } - } - catch (TC_Mysql_Exception& ex) - { - string err = string (ex.what()); - if (std::string::npos == err.find( "Duplicate")) - { - creatTable(sTbName,pMysql); - } - //因为会输出1åƒæ¡è®°å½•ï¼Œè¿™é‡Œåšæˆªå– - TLOGERROR("insert2Db exception: " << err.substr(0,64) << endl); - return 1; - } - catch (exception& ex) - { - TLOGERROR("insert2Db exception: " << ex.what() << endl); - return 1; - } - return 0; -} -/////////////////////////////////////////////////////////// -int StatDbManager::creatTable(const string &sTbName, TC_Mysql *_pstMql) -{ - string daytime = sTbName.substr(0, sTbName.length()- 2); // 去掉å°æ—¶ - - if (_lastDayTimeTable == daytime) - { - TLOGDEBUG("StatDbManager::creatTable table " << sTbName << " exist." << endl); - return 0; - } - - int iRet = 0; - int hour = 0; - - for (hour=0; hour < 24; hour++) - { - char buf[3]; - snprintf(buf,sizeof(buf),"%.2d",hour); - string sName = daytime + string(buf); - iRet |= creatTb(sName, _pstMql); - } - - _lastDayTimeTable = daytime; - - return iRet; -} -/////////////////////////////////////////////////////////// -bool StatDbManager::IsdbTableExist(const string& sTbName,TC_Mysql *pMysql) -{ - try - { - TC_Mysql::MysqlData tTotalRecord = pMysql->queryRecord("show tables like '%"+sTbName+"%'"); - TLOGINFO(__FUNCTION__<<"|show tables like '%"+sTbName+"%|affected:"< 0) - { - return true; - } - else - { - return false; - } - - }catch(TC_Mysql_Exception& ex) - { - TLOGERROR("StatDbManager::IsdbTableExist exception: " << ex.what() << endl); - return false; - } -} -/////////////////////////////////////////////////////////// -int StatDbManager::creatTb(const string &sTbName,TC_Mysql *pMysql) -{ - try - { - if (!IsdbTableExist(sTbName,pMysql)) - { - string sSql = TC_Common::replace(_sql, "${TABLE}",sTbName); - TLOGINFO(sSql << endl); - pMysql->execute(sSql); - } - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("StatDbManager::creatTb exception: " << ex.what() << endl); - return 1; - } -} -/////////////////////////////////////////////////////////// -int StatDbManager::creatEscTb(const string &sTbName, const string& sSql , TC_Mysql *pMysql) -{ - try - { - if (!IsdbTableExist(sTbName,pMysql)) - { - TLOGDEBUG(sSql << endl); - pMysql->execute(sSql); - - } - return 0; - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("exception: " << ex.what() << endl); - return 1; - } -} -/////////////////////////////////////////////////////////// -int StatDbManager::updateEcsStatus(const string &sLastTime,const string &sTbNamePre,TC_Mysql *pMysql) -{ - try - { - string sAppName = sTbNamePre != "" ? sTbNamePre : _tbNamePre; - string sCondition = "where appname='"; - sCondition += sAppName; - sCondition += "' and action=0"; - - TC_Mysql::RECORD_DATA rd; - rd["lasttime"] = make_pair(TC_Mysql::DB_STR, sLastTime); - - int iRet = pMysql->updateRecord("t_ecstatus", rd,sCondition); - - TLOGDEBUG("StatDbManager::updateEcsStatus iRet: " <getLastSQL()<< endl); - - if (iRet == 0 ) - { - rd["appname"] = make_pair(TC_Mysql::DB_STR, sAppName); - rd["checkint"] = make_pair(TC_Mysql::DB_INT, TC_Common::tostr(g_app.getInserInterv())); - rd["lasttime"] = make_pair(TC_Mysql::DB_STR, sLastTime); - iRet = pMysql->replaceRecord("t_ecstatus", rd); - } - - if (iRet != 1) - { - TLOGERROR("StatDbManager::updateEcsStatus erro: ret:" << iRet<<" "<getLastSQL()<< endl); - } - } - catch (TC_Mysql_Exception& ex) - { - string sql = _sqlStatus; - TLOGERROR("StatDbManager::updateEcsStatus exception: " << ex.what() << endl); - creatEscTb("t_ecstatus", sql, pMysql); - return 1; - } - return 0; -} -/////////////////////////////////////////////////////////// -int StatDbManager::insert2MultiDbs(int iIndex, const StatMsg &statmsg, const string &sDate, const string &sFlag) -{ - try - { - string sIp = ServerConfig::LocalIp; - - string sLastTime = sDate + " " + sFlag; - - string sTbNamePre = ""; - TC_Mysql * pMysql = NULL; - - sTbNamePre = _vsTbNamePre[iIndex]; - - pMysql = _vMysql[iIndex]; - - if (checkLastTime(sLastTime, sTbNamePre + sIp, pMysql) == 0) //=0,没有记录表示: æ•°æ®åº“记录的时间,比当å‰å½“å‰æ—¶é—´å° - { - TLOGDEBUG("begin insert to db " << getIpAndPort(iIndex) << endl); - - int64_t iBegin = tars::TC_TimeProvider::getInstance()->getNowMs(); - - if(insert2Db(statmsg, sDate, sFlag, sTbNamePre, pMysql) != 0) - { - if(_terminate) - { - return -1; - } - - if(insert2Db(statmsg ,sDate, sFlag, sTbNamePre, pMysql) != 0 ) - { - if(_terminate) - { - return -1; - } - - string sMsg("insert2Db_"); - sMsg += getIpAndPort(iIndex); - - sendAlarmSMS(sMsg); - } - else - { - if(updateEcsStatus(sLastTime, sTbNamePre + sIp, pMysql) != 0) - { - string sMsg("updateEcsStatus_"); - sMsg += getIpAndPort(iIndex); - - sendAlarmSMS(sMsg); - } - } - } - else - { - if(updateEcsStatus(sLastTime, sTbNamePre + sIp, pMysql) != 0) - { - string sMsg("updateEcsStatus_"); - sMsg += getIpAndPort(iIndex); - - sendAlarmSMS(sMsg); - } - } - - int64_t iEnd = tars::TC_TimeProvider::getInstance()->getNowMs(); - - TLOGDEBUG("insert|" << iIndex << "|" << getIpAndPort(iIndex) << "|" << sDate << "|" << sFlag << "|" << statmsg.size() << "|" << (iEnd - iBegin) << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|insert|dbIndex:" << iIndex << "|" << getIpAndPort(iIndex) << "|date:" << sDate << "|tflag:" << sFlag - << "|records:" << statmsg.size() << "|timecost(ms):" << (iEnd - iBegin) << "|iBegin(ms):" << iBegin << "|iEnd(ms):" << iEnd << endl; - } - } - catch(TC_Mysql_Exception& ex) - { - TLOGERROR("StatDbManager::insert2MultiDbs TC_Mysql_Exception: " << ex.what() << endl); - } - catch(exception& ex) - { - TLOGERROR("StatDbManager::insert2MultiDbs exception: " << ex.what() << endl); - } - - return 0; -} - -/////////////////////////////////////////////////////////// -int StatDbManager::sendAlarmSMS(const string &sMsg) -{ - string errInfo = " ERROR:" + ServerConfig::LocalIp + "|" + sMsg + ":统计入库失败,请åŠæ—¶å¤„ç†!"; - TARS_NOTIFY_ERROR(errInfo); - - TLOGERROR("TARS_NOTIFY_ERROR " << errInfo << endl); - - return 0; -} -/////////////////////////////////////////////////////////// -int StatDbManager::getDbNumber() -{ - return _dbNumber; -} -/////////////////////////////////////////////////////////// -int StatDbManager::checkLastTime(const string &sLastTime,const string &sTbNamePre,TC_Mysql *pMysql) -{ - int iRet = 0; - try - { - string sAppName = sTbNamePre != "" ? sTbNamePre : _tbNamePre; - string sCondition = "where appname='"; - sCondition += sAppName; - sCondition += "' and lasttime >= '"; - sCondition += sLastTime; - sCondition += "'" ; - - iRet = pMysql->getRecordCount("t_ecstatus", sCondition); - - TLOGDEBUG("StatDbManager::checkLastTime iRet: " <getLastSQL()<< endl); - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("StatDbManager::checkLastTime exception: " << ex.what() << endl); - creatEscTb("t_ecstatus", _sqlStatus, pMysql); - return 0; - } - - return iRet; -} - diff --git a/cpp/framework/StatServer/StatDbManager.h b/cpp/framework/StatServer/StatDbManager.h deleted file mode 100644 index 4483ee596..000000000 --- a/cpp/framework/StatServer/StatDbManager.h +++ /dev/null @@ -1,155 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __DB_MANAGER_H_ -#define __DB_MANAGER_H_ - -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "servant/TarsLogger.h" -#include "jmem/jmem_hashmap.h" -#include "servant/StatF.h" -#include "StatHashMap.h" - -using namespace tars; - -class StatDbManager : public TC_Singleton,public TC_ThreadMutex -{ -public: - enum CutType - { - CUT_BY_DAY = 0, - CUT_BY_HOUR = 1, - CUT_BY_MINUTE = 2, - }; - -public: - /** - * - */ - StatDbManager(); - - ~StatDbManager(); - -public: - - int creatTb(const string &strTbName,TC_Mysql *pMysql = NULL); - - int creatTable(const string &sTbName, TC_Mysql *_pstMql); - - int creatEscTb(const string &sTbName, const string& sSql , TC_Mysql *pMysql); - - int insert2Db(const StatMsg &statmsg,const string &sDate,const string &sFlag,const string &sTbNamePre = "",TC_Mysql *pMysql = NULL); - - int updateEcsStatus(const string &sLastTime,const string &sTbNamePre = "",TC_Mysql *pMysql = NULL); - - int checkLastTime(const string &sLastTime,const string &sTbNamePre,TC_Mysql *pMysql); - - int insert2MultiDbs(int iIndex, const StatMsg &vStatmsg, const string &sDate, const string &sFlag); - - int sendAlarmSMS(const string &sMsg); - - int getDbNumber(); - - int genRandOrder(); - - /** - * 获å–å„个dbçš„æƒé‡å€¼ï¼Œå¹¶è¿”回所有æƒé‡çš„最大公约数值和最大æƒé‡ - * @param iGcd - * @param iMaxW - * @param vDbWeighted - */ - void getDbWeighted(int& iGcd,int& iMaxW,vector& vDbWeighted); - - bool IsEnableWeighted(); - - bool IsdbTableExist(const string& sTbName,TC_Mysql *pMysql); - - size_t getDbToIpIndex(size_t iIndex); - - size_t getDbIpNum() { return _dbIpNum; } - - string getIpAndPort(size_t iDbIndex); - - map >& getIpHasDbInfo() { return _mIpHasDbInfo; } - - void setITerminateFlag(bool bFlag) { _terminate = bFlag; } - -private: - - int getGcd (int a, int b); - -private: - - //入库时,åœæ­¢çš„控制开关 - bool _terminate; - - //上次创建数æ®è¡¨çš„时间 - string _lastDayTimeTable; - - //创建表 - string _sql; - - //创建表t_ecstatus - string _sqlStatus; - - //表å‰ç¼€ - string _tbNamePre; - - //一次最大æ’å…¥æ¡æ•° - int _maxInsertCount; - - //分表类型 - CutType _eCutType; - - //æ’入数æ®çš„mysql dbä¿¡æ¯ - vector _vMysql; - - //æ•°æ®è¡¨å‰ç¼€ - vector _vsTbNamePre; - - //db个数 - int _dbNumber; - - //éšæœºå€¼ - vector _vRandOrder; - - //å„个dbçš„æƒé‡å€¼ - vector _vDbWeighted; - bool _enableWeighted; - - //dbçš„ip个数 - size_t _dbIpNum; - - //设置æ¯ä¸ªdb实例使用写db的线程下标,å³æ¯ä¸ªå†™db线程负责写哪些dbå®žä¾‹çš„æ•°æ® - map _mDbToIp; - - // - map > _mIpHasDbInfo; - - //打å°æ—¥å¿—使用 - vector _vIpAndPort; - - -}; - -#endif - - diff --git a/cpp/framework/StatServer/StatHashMap.h b/cpp/framework/StatServer/StatHashMap.h deleted file mode 100644 index a74597a73..000000000 --- a/cpp/framework/StatServer/StatHashMap.h +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __STAT_HASH_MAP_H_ -#define __STAT_HASH_MAP_H_ - -#include "servant/StatF.h" -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "jmem/jmem_hashmap.h" -#include "util/tc_file.h" -#include "util/tc_config.h" -#include "servant/TarsLogger.h" - -using namespace tars; - -typedef TarsHashMap HashMap;//FileStorePolicy - -typedef std::map, __gnu_cxx::__pool_alloc > > StatMsg; - -class StatHashMap : public HashMap -{ -public: - /** - * å¢žåŠ æ•°æ® - * @param Key - * @param Value - * - * @return int - */ - int add(const tars::StatMicMsgHead &head, const tars::StatMicMsgBody &body) - { - StatMicMsgBody stBody; - int ret = TC_HashMap::RT_OK; - tars::TarsOutputStream osk; - head.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - string sv; - time_t t = 0; - - { - - TC_LockT lock(ThreadLockPolicy::mutex()); - ret = this->_t.get(sk, sv,t); - - if (ret < 0) - { - return -1; - } - - //读å–到数æ®äº†, 解包 - if (ret == TC_HashMap::RT_OK) - { - tars::TarsInputStream is; - is.setBuffer(sv.c_str(), sv.length()); - stBody.readFrom(is); - } - - stBody.count += body.count; - stBody.execCount += body.execCount; - stBody.timeoutCount += body.timeoutCount; - - for(map::const_iterator it = body.intervalCount.begin();it!= body.intervalCount.end();it++) - { - stBody.intervalCount[it->first] += it->second; - } - - stBody.totalRspTime += body.totalRspTime; - if(stBody.maxRspTime < body.maxRspTime) - { - stBody.maxRspTime = body.maxRspTime; - } - //éž0最å°å€¼ - if( stBody.minRspTime == 0 || (stBody.minRspTime > body.minRspTime && body.minRspTime != 0)) - { - stBody.minRspTime = body.minRspTime; - } - - tars::TarsOutputStream osv; - stBody.writeTo(osv); - string stemp(osv.getBuffer(), osv.getLength()); - vector vtData; - - TLOGINFO("StatHashMap::add sk.length:" << sk.length() << " v.length:" << stemp.length() << endl); - - ret = this->_t.set(sk, stemp, true, vtData); - } - - return ret; - } -}; - -#endif - - diff --git a/cpp/framework/StatServer/StatImp.cpp b/cpp/framework/StatServer/StatImp.cpp deleted file mode 100644 index 04d55ef9a..000000000 --- a/cpp/framework/StatServer/StatImp.cpp +++ /dev/null @@ -1,252 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "StatImp.h" -#include "StatServer.h" - -/////////////////////////////////////////////////////////// -TC_ThreadMutex StatImpThreadData::_mutex; -pthread_key_t StatImpThreadData::_key = 0; -size_t StatImpThreadData::_iNo=0; - -/////////////////////////////////////////////////////////// -StatImpThreadData::StatImpThreadData() -: _iThreadIndex(0) -{ -} -void StatImpThreadData::destructor(void* p) -{ - StatImpThreadData * pSptd = (StatImpThreadData*)p; - if(pSptd) - { - delete pSptd; - pSptd = NULL; - } -} - -StatImpThreadData * StatImpThreadData::getData() -{ - if(_key == 0) - { - TC_LockT lock(_mutex); - if(_key == 0) - { - int iRet = ::pthread_key_create(&_key, StatImpThreadData::destructor); - - if (iRet != 0) - { - TLOGERROR("StatImpThreadData pthread_key_create fail:"<< errno << ":" << strerror(errno) << endl); - return NULL; - } - } - } - - StatImpThreadData * pSptd = (StatImpThreadData*)pthread_getspecific(_key); - - if(!pSptd) - { - TC_LockT lock(_mutex); - - pSptd = new StatImpThreadData(); - pSptd->_iThreadIndex = _iNo; - ++_iNo; - - int iRet = pthread_setspecific(_key, (void *)pSptd); - - assert(iRet == 0); - } - return pSptd; -} - -////////////////////////////////////////////////////////// -void StatImp::initialize() -{ - StatImpThreadData * td = StatImpThreadData::getData(); - - if(td) - { - _threadIndex = td->_iThreadIndex; - } - else - { - _threadIndex = 0; - TLOGERROR("StatImp::initialize StatImpThreadData::getData error." << endl); - } -} - -/////////////////////////////////////////////////////////// -// -int StatImp::reportMicMsg( const map& statmsg,bool bFromClient, tars::TarsCurrentPtr current ) -{ - TLOGINFO("report---------------------------------access size:" << statmsg.size() << "|bFromClient:" <::const_iterator it = statmsg.begin(); it != statmsg.end(); it++ ) - { - StatMicMsgHead head = it->first; - const StatMicMsgBody &body = it->second; - - if(bFromClient) - { - head.masterIp = current->getIp(); //以å‰æ˜¯è‡ªå·±èŽ·å–主调ip,现在从proxy直接 - - head.slaveName = getSlaveName(head.slaveName); - } - else - { - head.slaveIp = current->getIp();//现在从proxy直接 - } - - string sMasterName = head.masterName; - string::size_type pos = sMasterName.find("@"); - if (pos != string::npos) - { - head.masterName = sMasterName.substr(0, pos); - head.tarsVersion = sMasterName.substr(pos+1); - } - - map::iterator it_vip; - it_vip = g_app.getVirtualMasterIp().find(getSlaveName(head.slaveName)); - if( it_vip != g_app.getVirtualMasterIp().end()) - { - head.masterIp = it_vip->second; //按 slaveNameæ¥åŒ¹é…,填入å‡çš„主调ip,å‡å°å…¥åº“æ•°æ®é‡ - } - - //如果ä¸æ˜¯info等级的日志级别,就别往里走了 - ostringstream os; - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - os.str(""); - head.displaySimple(os); - body.displaySimple(os); - } - - //三个数æ®éƒ½ä¸º0æ—¶ä¸å…¥åº“ - if(body.count == 0 && body.execCount == 0 && body.timeoutCount == 0) - { - TLOGINFO(os.str()<<"|zero"< &msg,tars::TarsCurrentPtr current ) -{ - TLOGINFO("sample---------------------------------access size:" << msg.size() << endl); - - for(unsigned i=0; igetIp(); - - ostringstream os; - sample.displaySimple(os); - FDLOG()< >& mBuffer = g_app.getBuffer(); - map >::iterator iter = mBuffer.find(iBufferIndex); - iter->second[iIndex] = TNOW; - - dump2file(); - - iBufferIndex = g_app.getSelectBufferIndex(); - - string sKey = head.slaveName; - sKey += head.masterName; - sKey += head.interfaceName; - sKey += head.masterIp; - sKey += head.slaveIp; - - int iHashKey = _hashf(sKey) % g_app.getBuffNum(); - - StatHashMap *pHashMap = g_app.getHashMapBuff(iBufferIndex, iHashKey); - - ////////////////////////////////////////////////////////////////////////////////////// - - float rate = (pHashMap->getMapHead()._iUsedChunk) * 1.0/pHashMap->allBlockChunkCount(); - - if(rate >0.9) - { - TLOGERROR("StatImp::addHashMap hashmap will full|_iMemSize:" << pHashMap->getMapHead()._iMemSize << endl); - FDLOG("HashMap")<<"StatImp::addHashMap hashmap will full|_iMemSize:" << pHashMap->getMapHead()._iMemSize << endl; - return -1; - } - - int iRet = pHashMap->add(head, body); - if(iRet != 0) - { - TLOGDEBUG("StatImp::addHashMap set g_hashmap recourd erro|" << iRet << endl); - return iRet; - } - return iRet; -} - -/////////////////////////////////////////////////////////// -string StatImp::getSlaveName(const string& sSlaveName) -{ - return sSlaveName; -} - -void StatImp::dump2file() -{ - static string g_sDate; - static string g_sFlag; - static time_t g_tLastDumpTime = 0; - - time_t tTimeNow = TC_TimeProvider::getInstance()->getNow(); - time_t tTimeInterv = g_app.getInserInterv() *60;//second - - if(g_tLastDumpTime == 0) - { - g_app.getTimeInfo(g_tLastDumpTime,g_sDate,g_sFlag); - } - - if(tTimeNow - g_tLastDumpTime > tTimeInterv) - { - static TC_ThreadLock g_mutex; - TC_ThreadLock::Lock lock( g_mutex ); - if(tTimeNow - g_tLastDumpTime > tTimeInterv) - { - g_app.getTimeInfo(g_tLastDumpTime,g_sDate,g_sFlag); - - int iSelectBuffer = g_app.getSelectBufferIndex(); - iSelectBuffer = !iSelectBuffer; - - g_app.setSelectBufferIndex(iSelectBuffer); - - TLOGDEBUG("StatImp::dump2file select buffer:" << iSelectBuffer << "|TimeInterv:" << tTimeInterv << "|now:" << tTimeNow << "|last:" << g_tLastDumpTime << endl); - FDLOG("CountStat") << "stat ip:" << ServerConfig::LocalIp << "|StatImp::dump2file select buffer:" << iSelectBuffer << "|TimeInterv:" << tTimeInterv << "|now:" << tTimeNow << "|last:" << g_tLastDumpTime << endl; - } - } -} diff --git a/cpp/framework/StatServer/StatImp.h b/cpp/framework/StatServer/StatImp.h deleted file mode 100644 index 955d6c7e2..000000000 --- a/cpp/framework/StatServer/StatImp.h +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __STAT_IMP_H_ -#define __STAT_IMP_H_ - -#include -#include "util/tc_common.h" -#include "util/tc_thread.h" -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_mysql.h" -#include "util/tc_config.h" -#include "util/tc_hash_fun.h" -#include "servant/TarsLogger.h" -#include "jmem/jmem_hashmap.h" -#include "servant/StatF.h" -#include "StatHashMap.h" - -using namespace tars; - -class StatImpThreadData : public TC_ThreadPool::ThreadData -{ -public: - static TC_ThreadMutex _mutex; //å…¨å±€äº’æ–¥é” - static pthread_key_t _key; //线程ç§æœ‰æ•°æ®key - static size_t _iNo; - - /** - * 构造函数 - */ - StatImpThreadData(); - - /** - * æ•°æ®èµ„æºé‡Šæ”¾ - * @param p - */ - static void destructor(void* p); - - /** - * 获å–线程数æ®ï¼Œæ²¡æœ‰çš„è¯ä¼šè‡ªåŠ¨åˆ›å»º - * @return ServantProxyThreadData* - */ - static StatImpThreadData * getData(); - -public: - size_t _iThreadIndex; -}; - -class StatImp : public StatF,public TC_ThreadLock -{ -public: - /** - * - */ - StatImp() - : _hashf(tars::hash()) - , _threadIndex(0) - { - }; - - ~StatImp() - { - }; - - /** - * åˆå§‹åŒ– - * - * @return int - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy() - { - }; - - /** - * 上报模å—é—´è°ƒç”¨ä¿¡æ¯ - * @param statmsg, ä¸ŠæŠ¥ä¿¡æ¯ - * @return int, 返回0表示æˆåŠŸ - */ - virtual int reportMicMsg( const map& statmsg, bool bFromClient, tars::TarsCurrentPtr current ); - - /** - * 上报模å—é—´è°ƒç”¨é‡‡æ ·ä¿¡æ¯ - * @param sample, ä¸ŠæŠ¥ä¿¡æ¯ - * @return int, 返回0表示æˆåŠŸ - */ - virtual int reportSampleMsg(const vector &msg,tars::TarsCurrentPtr current ); - - using hash_functor = std::function; - -protected: - - int addHashMap(const StatMicMsgHead &head, const StatMicMsgBody &body); - -private: - void dump2file(); - - string getSlaveName(const string& sSlaveName); - -private: - hash_functor _hashf; - size_t _threadIndex; -}; - -#endif - - diff --git a/cpp/framework/StatServer/StatServer.cpp b/cpp/framework/StatServer/StatServer.cpp deleted file mode 100644 index 8a17e9670..000000000 --- a/cpp/framework/StatServer/StatServer.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "StatServer.h" -#include "servant/AppCache.h" -#include "StatImp.h" - -void StatServer::initialize() -{ - try - { - //关闭远程日志 - TarsTimeLogger::getInstance()->enableRemote("", false); - - //增加对象 - addServant( ServerConfig::Application + "." + ServerConfig::ServerName +".StatObj" ); - - _iInsertInterval = TC_Common::strto(g_pconf->get("/tars/hashmap","5")); - if(_iInsertInterval < 5) - { - _iInsertInterval = 5; - } - - //获å–业务线程个数 - string sAdapter = ServerConfig::Application + "." + ServerConfig::ServerName + ".StatObjAdapter"; - string sHandleNum = "/tars/application/server/"; - sHandleNum += sAdapter; - sHandleNum += ""; - - int iHandleNum = TC_Common::strto(g_pconf->get(sHandleNum, "20")); - vector vec; - vec.resize(iHandleNum); - for(size_t i =0; i < vec.size(); ++i) - { - vec[i] = 0; - } - - _vBuffer[0]=vec; - _vBuffer[1]=vec; - - TLOGDEBUG("StatServer::initialize iHandleNum:" << iHandleNum<< endl); - - initHashMap(); - - string s(""); - _iSelectBuffer = getSelectBufferFromFlag(s); - - TLOGDEBUG("StatServer::initialize iSelectBuffer:" << _iSelectBuffer<< endl); - - vector vIpGroup = g_pconf->getDomainKey("/tars/masteripgroup"); - for (unsigned i = 0; i < vIpGroup.size(); i++) - { - vector vOneGroup = TC_Common::sepstr(vIpGroup[i], ";", true); - if (vOneGroup.size() < 2) - { - TLOGERROR("StatImp::initialize wrong masterip:" << vIpGroup[i] << endl); - continue; - } - _mVirtualMasterIp[vOneGroup[0]] = vOneGroup[1]; - } - - _sRandOrder = AppCache::getInstance()->get("RandOrder"); - TLOGDEBUG("StatImp::initialize randorder:" << _sRandOrder << endl); - - _pReapSSDThread = new ReapSSDThread(); - _pReapSSDThread->start(); - - TARS_ADD_ADMIN_CMD_PREFIX("tars.tarsstat.randorder", StatServer::cmdSetRandOrder); - } - catch ( exception& ex ) - { - TLOGERROR("StatServer::initialize catch exception:" << ex.what() << endl); - exit( 0 ); - } - catch ( ... ) - { - TLOGERROR("StatServer::initialize unknow exception catched" << endl); - exit( 0 ); - } -} - -map& StatServer::getVirtualMasterIp(void) -{ - return _mVirtualMasterIp; -} - -string StatServer::getRandOrder(void) -{ - return _sRandOrder; -} - -string StatServer::getClonePath(void) -{ - return _sClonePath; -} - -int StatServer::getInserInterv(void) -{ - return _iInsertInterval; -} - -bool StatServer::getSelectBuffer(int iIndex, int64_t iInterval) -{ - int64_t iNow = TNOW; - bool bFlag = true; - vector &vBuffer = _vBuffer[iIndex]; - - for(vector::size_type i=0; i != vBuffer.size(); i++) - { - //if(vBuffer[i] != 0 && (iNow - vBuffer[i]) > iInterval) - if((iNow - vBuffer[i]) < iInterval) - { - bFlag = false; - } - } - - if(bFlag) - { - return true; - } - - return false; -} -int StatServer::getSelectBufferFromFlag(const string& sFlag) -{ - if(sFlag.length()!=0) - { - return (TC_Common::strto(sFlag.substr(2,2))/_iInsertInterval)%2; - } - else - { - time_t tTime=0; - string sDate="",flag=""; - getTimeInfo(tTime,sDate,flag); - return (TC_Common::strto(flag.substr(2,2))/_iInsertInterval)%2; - } -} -bool StatServer::cmdSetRandOrder(const string& command, const string& params, string& result) -{ - try - { - TLOGINFO("StatServer::cmdSetRandOrder:" << command << " " << params << endl); - - _sRandOrder = params; - - result = "set RandOrder [" + _sRandOrder + "] ok"; - - AppCache::getInstance()->set("RandOrder",_sRandOrder); - } - catch (exception &ex) - { - result = ex.what(); - } - return true; -} - -void StatServer::initHashMap() -{ - TLOGDEBUG("StatServer::initHashMap begin" << endl); - - int iHashMapNum = TC_Common::strto(g_pconf->get("/tars/hashmap","1")); - - _iBuffNum = iHashMapNum; - - _hashmap = new StatHashMap *[2]; - - for(int k = 0; k < 2; ++k) - { - _hashmap[k] = new StatHashMap[iHashMapNum](); - } - - int iMinBlock = TC_Common::strto(g_pconf->get("/tars/hashmap","128")); - int iMaxBlock = TC_Common::strto(g_pconf->get("/tars/hashmap","256")); - float iFactor = TC_Common::strto(g_pconf->get("/tars/hashmap","2")); - int iSize = TC_Common::toSize(g_pconf->get("/tars/hashmap"), 1024*1024*256); - - _sClonePath = ServerConfig::DataPath + "/" + g_pconf->get("/tars/hashmap","clone"); - - if(!TC_File::makeDirRecursive(_sClonePath)) - { - TLOGERROR("cannot create hashmap file " << _sClonePath << endl); - exit(0); - } - - TLOGDEBUG("StatServer::initHashMap init multi hashmap begin..." << endl); - - char a[26]; - int iChar = 0; - for(int n = 0; n < 26; n++) - { - a[n] = 'a' + n; - } - - for(int i = 0; i < 2; ++i) - { - for(int k = 0; k < iHashMapNum; ++k) - { - string sFileConf("/tars/hashmapget(sFileConf, sFileDefault); - - string sPath = TC_File::extractFilePath(sHashMapFile); - - if(!TC_File::makeDirRecursive(sPath)) - { - TLOGERROR("cannot create hashmap file " << sPath << endl); - exit(0); - } - - try - { - TLOGINFO("initDataBlockSize size: " << iMinBlock << ", " << iMaxBlock << ", " << iFactor << endl); - - _hashmap[i][k].initDataBlockSize(iMinBlock,iMaxBlock,iFactor); - - if(TC_File::isFileExist(sHashMapFile)) - { - iSize = TC_File::getFileSize(sHashMapFile); - } - else - { - int fd = open(sHashMapFile.c_str(), O_CREAT|O_EXCL|O_RDWR, 0666); - if(fd == -1) - { - if(errno != EEXIST) - { - throw TC_Exception("open1 file '" + sHashMapFile + "' error", errno); - } - else - { - fd = open(sHashMapFile.c_str(), O_CREAT|O_RDWR, 0666); - if(fd == -1) - { - throw TC_Exception("open2 file '" + sHashMapFile + "' error", errno); - } - } - } - - lseek(fd, iSize-1, SEEK_SET); - write(fd,"\0",1); - if(fd != -1) - { - close(fd); - } - } - - key_t key = ftok(sHashMapFile.c_str(), a[iChar%26]); - - iChar++; - - TLOGDEBUG("init hash mem,shm key: 0x" << hex << key << dec << endl); - - //_hashmap[i][k].initStore( sHashMapFile.c_str(), iSize ); - _hashmap[i][k].initStore(key, iSize); - _hashmap[i][k].setAutoErase(false); - - TLOGINFO("\n" << _hashmap[i][k].desc() << endl); - } - catch(TC_HashMap_Exception &e) - { - TC_File::removeFile(sHashMapFile,false); - throw runtime_error(e.what()); - } - - } - } - - TLOGDEBUG("StatServer::initHashMap init multi hashmap end..." << endl); -} - -void StatServer::destroyApp() -{ - if(_pReapSSDThread) - { - delete _pReapSSDThread; - _pReapSSDThread = NULL; - } - - for(int i = 0; i < 2; ++i) - { - delete [] _hashmap[i]; - } - - delete [] _hashmap; - - TLOGDEBUG("StatServer::destroyApp ok" << endl); -} - -void StatServer::getTimeInfo(time_t &tTime,string &sDate,string &sFlag) -{ - string sTime,sHour,sMinute; - time_t t = TC_TimeProvider::getInstance()->getNow(); - t = (t/(_iInsertInterval*60))*_iInsertInterval*60; //è¦æ±‚必须为loadIntevæ•´æ•°å€ - tTime = t; - t = (t%3600 == 0?t-60:t); //è¦æ±‚å°†9点写作0860 - sTime = TC_Common::tm2str(t,"%Y%m%d%H%M"); - sDate = sTime.substr(0,8); - sHour = sTime.substr(8,2); - sMinute = sTime.substr(10,2); - sFlag = sHour + (sMinute=="59"?"60":sMinute); //è¦æ±‚å°†9点写作0860 -} - - diff --git a/cpp/framework/StatServer/StatServer.h b/cpp/framework/StatServer/StatServer.h deleted file mode 100644 index 600278145..000000000 --- a/cpp/framework/StatServer/StatServer.h +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __STAT_SERVER_H_ -#define __STAT_SERVER_H_ - -#include "servant/Application.h" -#include "servant/StatF.h" -#include "StatHashMap.h" -#include "ReapSSDThread.h" - -using namespace tars; - -class StatServer : public Application -{ -protected: - /** - * åˆå§‹åŒ–, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize(); - - /** - * æžå¤Ÿ, æ¯ä¸ªè¿›ç¨‹éƒ½ä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void destroyApp(); - -public: - - void getTimeInfo(time_t &tTime,string &sDate,string &sFlag); - - bool cmdSetRandOrder(const string& command, const string& params, string& result); - - //获å–主调虚拟ip映射 - map& getVirtualMasterIp(void); - - string getRandOrder(void); - - string getClonePath(void); - - int getInserInterv(void); - - map >& getBuffer(){ return _vBuffer; } - - bool getSelectBuffer(int iIndex, int64_t iInterval); - - int getSelectBufferFromFlag(const string& sFlag); - - int getSelectBufferIndex() { return _iSelectBuffer; } - - void setSelectBufferIndex(int iIndex) { _iSelectBuffer = iIndex; } - - StatHashMap * getHashMapBuff(int iIndex, int iBuffer) { return &(_hashmap[iIndex][iBuffer]); } - - int getBuffNum() { return _iBuffNum; } - -private: - void initHashMap(); - -private: - - ReapSSDThread* _pReapSSDThread; - - //主调虚拟ipé…ç½® - map _mVirtualMasterIp; - - // éšæœºå…¥åº“开关 - string _sRandOrder; - - //æ•°æ®æ¢å­˜ç›®å½• - string _sClonePath; - - //æ•°æ®åº“æ’入间隔,å•ä½åˆ†é’Ÿ - int _iInsertInterval; - - //åŒbuffer机制 - map > _vBuffer; - - int _iSelectBuffer; - - StatHashMap **_hashmap; - - int _iBuffNum; -}; - -extern TC_Config* g_pconf; -extern StatServer g_app; - -#endif diff --git a/cpp/framework/StatServer/main.cpp b/cpp/framework/StatServer/main.cpp deleted file mode 100644 index 92e8cd849..000000000 --- a/cpp/framework/StatServer/main.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "StatServer.h" -#include - -using namespace std; - -TC_Config* g_pconf; -StatServer g_app; - -int main( int argc, char* argv[] ) -{ - try - { - g_pconf = &g_app.getConfig(); - g_app.main( argc, argv ); - g_app.waitForShutdown(); - } - catch ( exception& ex ) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/conf/tars.default b/cpp/framework/conf/tars.default deleted file mode 100644 index ad084cf29..000000000 --- a/cpp/framework/conf/tars.default +++ /dev/null @@ -1,33 +0,0 @@ - - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - stat=tars.tarsstat.StatObj - property=tars.tarsproperty.PropertyObj - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - - diff --git a/cpp/framework/conf/tars.tarsconfig b/cpp/framework/conf/tars.tarsconfig deleted file mode 100644 index 716ec47db..000000000 --- a/cpp/framework/conf/tars.tarsconfig +++ /dev/null @@ -1,39 +0,0 @@ - - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - - - charset=utf8 - dbhost=db.tars.com - dbname=db_tars - dbpass=tars2015 - dbport=3306 - dbuser=tars - - \ No newline at end of file diff --git a/cpp/framework/conf/tars.tarsjava.default b/cpp/framework/conf/tars.tarsjava.default deleted file mode 100644 index 7ccdc2a40..000000000 --- a/cpp/framework/conf/tars.tarsjava.default +++ /dev/null @@ -1,44 +0,0 @@ - - - enableset=${enableset} - setdivision=${setdivision} - - asyncthread=${asyncthread} - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - stat=tars.tarsstat.StatObj - property=tars.tarsproperty.PropertyObj - report-interval=60000 - modulename=${modulename} - sample-rate=100000 - max-sample-count=50 - - - deactivating-timeout=3000 - logLevel=DEBUG - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - loglevel=DEBUG - logsize=15M - log=tars.tarslog.LogObj - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - mainclass=com.qq.cloud.tars.server.startup.Main - classpath=${basepath}/conf:${basepath}/lib - jvmparams=-Dcom.sun.management.jmxremote.ssl\=false -Dcom.sun.management.jmxremote.authenticate\=false -Xms2000m -Xmx2000m -Xmn1000m -Xss1000k -XX:PermSize\=128M -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction\=60 -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+CMSParallelRemarkEnabled -XX:+CMSScavengeBeforeRemark -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction\=0 -verbosegc -XX:+PrintGCDetails -XX:ErrorFile\=${logpath}/${app}/${server}/jvm_error.log - sessiontimeout=120000 - sessioncheckinterval=60000 - tcpnodelay=true - udpbuffersize=8192 - charsetname=UTF-8 - backupfiles=bak1;bak2;bak3;conf - - - \ No newline at end of file diff --git a/cpp/framework/conf/tars.tarslog b/cpp/framework/conf/tars.tarslog deleted file mode 100644 index f65677645..000000000 --- a/cpp/framework/conf/tars.tarslog +++ /dev/null @@ -1,40 +0,0 @@ - - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - stat=tars.tarsstat.StatObj - property=tars.tarsproperty.PropertyObj - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=100000000 - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=ERROR - - - - logpath=/usr/local/app/tars/remote_app_log - logthread=10 - - hour=xx - - - \ No newline at end of file diff --git a/cpp/framework/conf/tars.tarsnotify b/cpp/framework/conf/tars.tarsnotify deleted file mode 100644 index af73c2db3..000000000 --- a/cpp/framework/conf/tars.tarsnotify +++ /dev/null @@ -1,49 +0,0 @@ - - sql=CREATE TABLE `${TABLE}` ( `id` int(11) NOT NULL AUTO_INCREMENT, `application` varchar(128) DEFAULT '', `server_name` varchar(128) DEFAULT NULL, `container_name` varchar(128) DEFAULT '' , `node_name` varchar(128) NOT NULL DEFAULT '', `set_name` varchar(16) DEFAULT NULL, `set_area` varchar(16) DEFAULT NULL, `set_group` varchar(16) DEFAULT NULL, `server_id` varchar(100) DEFAULT NULL, `thread_id` varchar(20) DEFAULT NULL, `command` varchar(50) DEFAULT NULL, `result` text, `notifytime` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name` (`server_name`), KEY `servernoticetime_i_1` (`notifytime`), KEY `indx_1_server_id` (`server_id`), KEY `query_index` (`application`,`server_name`,`node_name`,`set_name`,`set_area`,`set_group`) ) ENGINE\=InnoDB DEFAULT CHARSET\=utf8 - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - - - charset=utf8 - dbhost=db.tars.com - dbpass=tars2015 - dbport=3306 - dbuser=tars - dbname=db_tars - - - min_block=50 - max_block=200 - factor=1.5 - file_path=./notify - file_size=50000000 - max_page_num=30 - max_page_size=20 - - \ No newline at end of file diff --git a/cpp/framework/conf/tars.tarspatch b/cpp/framework/conf/tars.tarspatch deleted file mode 100644 index 8fdc6e810..000000000 --- a/cpp/framework/conf/tars.tarspatch +++ /dev/null @@ -1,34 +0,0 @@ - - directory=/usr/local/app/patchs/tars - uploadDirectory=/usr/local/app/patchs/tars.upload - size=100M - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - - \ No newline at end of file diff --git a/cpp/framework/conf/tars.tarsproperty b/cpp/framework/conf/tars.tarsproperty deleted file mode 100644 index bfabaf12e..000000000 --- a/cpp/framework/conf/tars.tarsproperty +++ /dev/null @@ -1,76 +0,0 @@ - - sql=CREATE TABLE `${TABLE}` (`stattime` timestamp NOT NULL default CURRENT_TIMESTAMP,`f_date` date NOT NULL default '1970-01-01', `f_tflag` varchar(8) NOT NULL default '',`master_name` varchar(128) NOT NULL default '',`master_ip` varchar(16) default NULL,`property_name` varchar(100) default NULL,`set_name` varchar(15) NOT NULL default '',`set_area` varchar(15) NOT NULL default '',`set_id` varchar(15) NOT NULL default '',`policy` varchar(20) default NULL,`value` varchar(255) default NULL, KEY (`f_date`,`f_tflag`,`master_name`,`master_ip`,`property_name`,`policy`),KEY `IDX_MASTER_NAME` (`master_name`),KEY `IDX_MASTER_IP` (`master_ip`),KEY `IDX_TIME` (`stattime`)) ENGINE\=Innodb - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - - - logpath=/usr/local/app/tars/remote_app_log - logthread=10 - - - charset - dbhost=db.tars.com - dbname=tars - dbport=3306 - dbuser=tars - dbpass=tars2015 - - - - dbhost=db.tars.com - dbname=tars_property - tbname=tars_property_ - dbuser=tars - dbpass=tars2015 - dbport=3306 - charset=utf8 - - - dbhost=db.tars.com - dbname=tars_property - tbname=tars_property_ - dbuser=tars - dbpass=tars2015 - dbport=3306 - charset=utf8 - - - - factor=1.5 - file=hashmap.txt - insertInterval=5 - maxBlock=200 - minBlock=100 - size=10M - - - Interval=10 - sql=insert ignore into t_master_property select master_name, property_name, policy from ${TABLE} group by master_name, property_name, policy; - - \ No newline at end of file diff --git a/cpp/framework/conf/tars.tarsstat b/cpp/framework/conf/tars.tarsstat deleted file mode 100644 index 2269f8aeb..000000000 --- a/cpp/framework/conf/tars.tarsstat +++ /dev/null @@ -1,61 +0,0 @@ - - sql=CREATE TABLE `${TABLE}`( `stattime` timestamp NOT NULL default CURRENT_TIMESTAMP,`f_date` date NOT NULL default '1970-01-01', `f_tflag` varchar(8) NOT NULL default '',`source_id` varchar(15) default NULL,`master_name` varchar(64) default NULL,`slave_name` varchar(64) default NULL,`interface_name` varchar(64) default NULL,`tars_version` varchar(16) NOT NULL default '',`master_ip` varchar(15) default NULL,`slave_ip` varchar(21) default NULL,`slave_port` int(10) default NULL,`return_value` int(11) default NULL,`succ_count` int(10) unsigned default NULL,`timeout_count` int(10) unsigned default NULL,`exce_count` int(10) unsigned default NULL,`interv_count` varchar(128) default NULL,`total_time` bigint(20) unsigned default NULL,`ave_time` int(10) unsigned default NULL,`maxrsp_time` int(10) unsigned default NULL,`minrsp_time` int(10) unsigned default NULL,PRIMARY KEY (`source_id`,`f_date`,`f_tflag`,`master_name`,`slave_name`,`interface_name`,`master_ip`,`slave_ip`,`slave_port`,`return_value`,`tars_version`),KEY `IDX_TIME` (`stattime`),KEY `IDC_MASTER` (`master_name`),KEY `IDX_INTERFACENAME` (`interface_name`),KEY `IDX_FLAGSLAVE` (`f_tflag`,`slave_name`), KEY `IDX_SLAVEIP` (`slave_ip`),KEY `IDX_SLAVE` (`slave_name`),KEY `IDX_RETVALUE` (`return_value`),KEY `IDX_MASTER_IP` (`master_ip`),KEY `IDX_F_DATE` (`f_date`)) ENGINE\=MyISAM DEFAULT CHARSET\=utf8 - enWeighted=1 - useolddatabase=0 - time_out=600 - - enableset=${enableset} - setdivision=${setdivision} - - locator=${locator} - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=${asyncthread} - modulename=${modulename} - - - app=${app} - server=${server} - localip=${localip} - local=${local} - basepath=${basepath} - datapath=${datapath} - logpath=${logpath} - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - - - tars.tarsstat;1.1.1.1 - - - masterfile=hashmap_master.txt - slavefile=hashmap_slave.txt - insertInterval=5 - enableStatCount=0 - size=8M - countsize=1M - - - interval=5 - insertDbThreadNum=4 - - - - dbhost=db.tars.com - dbname=tars_stat - tbname=tars_stat_ - dbuser=tars - dbpass=tars2015 - dbport=3306 - charset=utf8 - - - \ No newline at end of file diff --git a/cpp/framework/deploy/tarsAdminRegistry/conf/adminregistry.conf b/cpp/framework/deploy/tarsAdminRegistry/conf/adminregistry.conf deleted file mode 100644 index f49553089..000000000 --- a/cpp/framework/deploy/tarsAdminRegistry/conf/adminregistry.conf +++ /dev/null @@ -1,62 +0,0 @@ - - - enableset=n - setdivision=NULL - - app=tars - server=tarsAdminRegistry - localip=192.168.2.131 - basepath=/usr/local/app/tars/bin - datapath=/usr/local/app/tars/tarsnode/data/tars.tarsAdminRegistry/data - logpath=/usr/local/app/tars/app_log - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=2000 - logLevel=DEBUG - - allow - endpoint=tcp -h registry.tars.com -p 12000 -t 60000 - handlegroup=tars.tarsAdminRegistry.AdminRegObjAdapter - maxconns=1024 - protocol=tars - queuecap=10000 - queuetimeout=60000 - servant=tars.tarsAdminRegistry.AdminRegObj - threads=5 - - - - locator=tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890 - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=3 - modulename=tars.tarsAdminRegistry - - - - AdminRegObjName=tars.tarsAdminRegistry.AdminRegObj - patchServerObj=tars.tarspatch.PatchObj - - - dbhost=db.tars.com - dbname=db_tars - dbuser=tars - dbpass=tars2015 - dbport=3306 - charset=utf8 - dbflag=CLIENT_MULTI_STATEMENTS - - - defaultTemplate=tars.tarsnode - sync_node_timeout=4000 - - - updateHeartInterval=15 - reigistryTimeout=150 - - diff --git a/cpp/framework/deploy/tarsAdminRegistry/util/execute.sh b/cpp/framework/deploy/tarsAdminRegistry/util/execute.sh deleted file mode 100644 index db32a96d2..000000000 --- a/cpp/framework/deploy/tarsAdminRegistry/util/execute.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -ulimit -a -bin="/usr/local/app/tars/tarsAdminRegistry/bin/tarsAdminRegistry" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsAdminRegistry" | grep -v "grep" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi -ulimit -c unlimited - -CONFIG=/usr/local/app/tars/tarsnode/data/tars.tarsAdminRegistry/conf/tars.tarsAdminRegistry.conf - -if [ ! -f $CONFIG ]; then - CONFIG=/usr/local/app/tars/tarsAdminRegistry/conf/adminregistry.conf -fi - -$bin --config=$CONFIG & - diff --git a/cpp/framework/deploy/tarsAdminRegistry/util/start.sh b/cpp/framework/deploy/tarsAdminRegistry/util/start.sh deleted file mode 100644 index b85cf7ad0..000000000 --- a/cpp/framework/deploy/tarsAdminRegistry/util/start.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -sh /usr/local/app/tars/tarsAdminRegistry/util/execute.sh diff --git a/cpp/framework/deploy/tarsAdminRegistry/util/stop.sh b/cpp/framework/deploy/tarsAdminRegistry/util/stop.sh deleted file mode 100644 index a28d2cc6b..000000000 --- a/cpp/framework/deploy/tarsAdminRegistry/util/stop.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -bin="/usr/local/app/tars/tarsAdminRegistry/bin/tarsAdminRegistry" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsAdminRegistry" | grep -v "grep"|grep -v "sh" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" - fi - diff --git a/cpp/framework/deploy/tars_install.sh b/cpp/framework/deploy/tars_install.sh deleted file mode 100644 index ab4a5bed8..000000000 --- a/cpp/framework/deploy/tars_install.sh +++ /dev/null @@ -1,29 +0,0 @@ - -if [ ! -d /usr/local/app/tars/app_log ]; then - mkdir -p /data/log/tars - mkdir -p /usr/local/app/tars - mkdir -p /data/tars/app_log - ln -s /data/tars/app_log /usr/local/app/tars/app_log -fi - -if [ ! -d /usr/local/app/tars/remote_app_log ]; then - mkdir -p /data/tars/remote_app_log - ln -s /data/tars/remote_app_log /usr/local/app/tars/remote_app_log -fi - -cd /usr/local/app/tars/ - -chmod +x tarsAdminRegistry/util/*.sh -chmod +x tarsconfig/util/*.sh -chmod +x tarsnode/util/*.sh -chmod +x tarspatch/util/*.sh -chmod +x tarsregistry/util/*.sh -chmod +x tarsregistry/util/*.sh - -tarsregistry/util/start.sh ; -tarsAdminRegistry/util/start.sh; -tarsnode/util/start.sh ; -tarsconfig/util/start.sh; -tarspatch/util/start.sh; - - diff --git a/cpp/framework/deploy/tarsconfig/conf/tarsconfig.conf b/cpp/framework/deploy/tarsconfig/conf/tarsconfig.conf deleted file mode 100644 index ee6319347..000000000 --- a/cpp/framework/deploy/tarsconfig/conf/tarsconfig.conf +++ /dev/null @@ -1,55 +0,0 @@ - - - enableset=n - setdivision=NULL - - node=tars.tarsnode.ServerObj@tcp -h 192.168.2.131 -p 19386 -t 60000 - app=tars - server=tarsconfig - localip=192.168.2.131 - local=tcp -h 127.0.0.1 -p 10001 -t 3000 - basepath=/usr/local/app/tars/bin - datapath=/usr/local/app/tars/tarsconfig/data - logpath=/usr/local/app/tars/app_log - logsize=100000000 - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - allow - endpoint=tcp -h 192.168.2.131 -p 10001 -t 60000 - handlegroup=tars.tarsconfig.ConfigObjAdapter - maxconns=10240 - protocol=tars - queuecap=10000 - queuetimeout=60000 - servant=tars.tarsconfig.ConfigObj - shmcap=0 - shmkey=0 - threads=10 - - - - locator=tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890 - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=3 - stat=tars.tarsstat.StatObj - property=tars.tarsproperty.PropertyObj - modulename=tars.tarsconfig - - - - charset=utf8 - dbhost=db.tars.com - dbname=db_tars - dbpass=tars2015 - dbport=3306 - dbuser=tars - - diff --git a/cpp/framework/deploy/tarsconfig/util/execute.sh b/cpp/framework/deploy/tarsconfig/util/execute.sh deleted file mode 100644 index 7e5a7c3ef..000000000 --- a/cpp/framework/deploy/tarsconfig/util/execute.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -ulimit -a -bin="/usr/local/app/tars/tarsconfig/bin/tarsconfig" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsconfig" | grep -v "grep" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi -ulimit -c unlimited -$bin --config=/usr/local/app/tars/tarsconfig/conf/tarsconfig.conf & diff --git a/cpp/framework/deploy/tarsconfig/util/start.sh b/cpp/framework/deploy/tarsconfig/util/start.sh deleted file mode 100644 index 814809d38..000000000 --- a/cpp/framework/deploy/tarsconfig/util/start.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -sh /usr/local/app/tars/tarsconfig/util/execute.sh diff --git a/cpp/framework/deploy/tarsconfig/util/stop.sh b/cpp/framework/deploy/tarsconfig/util/stop.sh deleted file mode 100644 index cc5a21654..000000000 --- a/cpp/framework/deploy/tarsconfig/util/stop.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -bin="/usr/local/app/tars/tarsconfig/bin/tarsconfig" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsconfig" | grep -v "grep"|grep -v "sh" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" - fi - diff --git a/cpp/framework/deploy/tarsnode/conf/tarsnode.conf b/cpp/framework/deploy/tarsnode/conf/tarsnode.conf deleted file mode 100644 index 7bc79794e..000000000 --- a/cpp/framework/deploy/tarsnode/conf/tarsnode.conf +++ /dev/null @@ -1,66 +0,0 @@ - - - enableset=n - setdivision=NULL - - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - stat=tars.tarsstat.StatObj - property=tars.tarsproperty.PropertyObj - modulename=tars.tarsnode - locator=tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890 - sync-invoke-timeout=6000 - asyncthread=3 - - - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - app=tars - server=tarsnode - localip=192.168.2.131 - local=tcp -h 127.0.0.1 -p 19385 -t 10000 - basepath=/usr/local/app/tars/tarsnode/data - datapath=/usr/local/app/tars/tarsnode/data - logpath=/usr/local/app/tars/app_log - logLevel=DEBUG - - endpoint=tcp -h 192.168.2.131 -p 19385 -t 60000 - allow - maxconns=1024 - threads=5 - queuecap=10000 - queuetimeout=4000 - servant=tars.tarsnode.NodeObj - - - endpoint=tcp -h 192.168.2.131 -p 19386 -t 60000 - allow - maxconns=1024 - threads=5 - queuecap=10000 - queuetimeout=4000 - servant=tars.tarsnode.ServerObj - - - - - registryObj=tars.tarsregistry.RegistryObj - - heartTimeout=60 - monitorInterval=2 - synStatInterval=300 - - - file=serversCache.dat - minBlock=500 - maxBlock=500 - factor=1 - size=10M - - - diff --git a/cpp/framework/deploy/tarsnode/util/execute.sh b/cpp/framework/deploy/tarsnode/util/execute.sh deleted file mode 100644 index b177867fa..000000000 --- a/cpp/framework/deploy/tarsnode/util/execute.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -export PATH=${PATH}:/usr/local/app/tars/bin:/usr/local/jdk/bin; -bin="/usr/local/app/tars/tarsnode/bin/tarsnode" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsnode" | grep -v "grep"|grep -v "sh" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi - -ulimit -c 409600 -ulimit -a - -$bin --locator="tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890" --config=/usr/local/app/tars/tarsnode/conf/tarsnode.conf & diff --git a/cpp/framework/deploy/tarsnode/util/monitor.sh b/cpp/framework/deploy/tarsnode/util/monitor.sh deleted file mode 100644 index 7d87eecd8..000000000 --- a/cpp/framework/deploy/tarsnode/util/monitor.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -cd /usr/local/app/tars/tarsnode/util - -bin="/usr/local/app/tars/tarsnode/bin/tarsnode" - -$bin --monitor --config=/usr/local/app/tars/tarsnode/conf/tarsnode.conf - -ex=$? - -if [ $ex -ne 0 ]; then -# echo "monitor:"$ex", restart tarsnode" - sh /usr/local/app/tars/tarsnode/util/start.sh -fi - diff --git a/cpp/framework/deploy/tarsnode/util/start.sh b/cpp/framework/deploy/tarsnode/util/start.sh deleted file mode 100644 index b9c7c002f..000000000 --- a/cpp/framework/deploy/tarsnode/util/start.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -cd /usr/local/app/tars/tarsnode/util -sh execute.sh > start.tmp 2>&1 diff --git a/cpp/framework/deploy/tarsnode/util/stop.sh b/cpp/framework/deploy/tarsnode/util/stop.sh deleted file mode 100644 index c3fb50abf..000000000 --- a/cpp/framework/deploy/tarsnode/util/stop.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -bin="/usr/local/app/tars/tarsnode/bin/tarsnode" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsnode" | grep -v "grep" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi - - diff --git a/cpp/framework/deploy/tarsnode_install.sh b/cpp/framework/deploy/tarsnode_install.sh deleted file mode 100644 index e9dbcd10f..000000000 --- a/cpp/framework/deploy/tarsnode_install.sh +++ /dev/null @@ -1,21 +0,0 @@ - -if [ ! -d /usr/local/app/tars/app_log ]; then - mkdir -p /data/log/tars - mkdir -p /usr/local/app/tars - mkdir -p /data/tars/app_log - ln -s /data/tars/app_log /usr/local/app/tars/app_log -fi - -if [ ! -d /usr/local/app/tars/remote_app_log ]; then - mkdir -p /data/tars/remote_app_log - ln -s /data/tars/remote_app_log /usr/local/app/tars/remote_app_log -fi - -cd /usr/local/app/tars/ - - -chmod +x tarsnode/util/*.sh - -./tarsnode/util/start.sh - - diff --git a/cpp/framework/deploy/tarspatch/conf/rsync.conf b/cpp/framework/deploy/tarspatch/conf/rsync.conf deleted file mode 100644 index 6ac141097..000000000 --- a/cpp/framework/deploy/tarspatch/conf/rsync.conf +++ /dev/null @@ -1,10 +0,0 @@ -[webrelease] - path = /usr/local/app/patchs/TmpPatch - readonly=false -[webrelease_patch] - path = /usr/local/app/patchs/tars - readonly=false -[webrelease_bak] - path = /data/tars.upload/tars.upload - readonly=false - diff --git a/cpp/framework/deploy/tarspatch/conf/tarspatch.conf b/cpp/framework/deploy/tarspatch/conf/tarspatch.conf deleted file mode 100644 index c8de4d872..000000000 --- a/cpp/framework/deploy/tarspatch/conf/tarspatch.conf +++ /dev/null @@ -1,48 +0,0 @@ - - directory=/usr/local/app/patchs/tars - uploadDirectory=/usr/local/app/patchs/tars.upload - size=1M - - enableset=n - setdivision=NULL - - node=tars.tarsnode.ServerObj@tcp -h 192.168.2.131 -p 19386 -t 60000 - app=tars - server=tarspatch - localip=192.168.2.131 - local=tcp -h 127.0.0.1 -p 10000 -t 3000 - basepath=/usr/local/app/tars/bin - datapath=/usr/local/app/tars/tarspatch/data - logpath=/usr/local/app/tars/app_log - logsize=10M - config=tars.tarsconfig.ConfigObj - notify=tars.tarsnotify.NotifyObj - deactivating-timeout=3000 - logLevel=DEBUG - - allow - endpoint=tcp -h 192.168.2.131 -p 10000 -t 6000 - handlegroup=tars.tarspatch.PatchObjAdapter - maxconns=20000 - protocol=tars - queuecap=10000 - queuetimeout=60000 - servant=tars.tarspatch.PatchObj - threads=3 - - - - locator=tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890 - sync-invoke-timeout=3000 - async-invoke-timeout=5000 - refresh-endpoint-interval=60000 - report-interval=60000 - sample-rate=100000 - max-sample-count=50 - asyncthread=3 - stat=tars.tarsstat.StatObj - property=tars.tarsproperty.PropertyObj - modulename=tars.tarspatch - - - diff --git a/cpp/framework/deploy/tarspatch/util/execute.sh b/cpp/framework/deploy/tarspatch/util/execute.sh deleted file mode 100644 index e95f07a08..000000000 --- a/cpp/framework/deploy/tarspatch/util/execute.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -ulimit -a -bin="/usr/local/app/tars/tarspatch/bin/tarspatch" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarspatch" | grep -v "grep" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi -ulimit -c unlimited -$bin --config=/usr/local/app/tars/tarspatch/conf/tarspatch.conf & diff --git a/cpp/framework/deploy/tarspatch/util/init.sh b/cpp/framework/deploy/tarspatch/util/init.sh deleted file mode 100644 index 70e10c558..000000000 --- a/cpp/framework/deploy/tarspatch/util/init.sh +++ /dev/null @@ -1,33 +0,0 @@ - -if [ ! -d /usr/local/app/patchs ]; then - mkdir -p /usr/local/app/patchs - mkdir -p /data/tars/patchs/tars - mkdir -p /data/tars/patchs/TmpPatch - mkdir -p /data/tars/patchs/tars.upload - ln -s /data/tars/patchs/tars /usr/local/app/patchs/tars - ln -s /data/tars/patchs/TmpPatch /usr/local/app/patchs/TmpPatch - ln -s /data/tars/patchs/tars.upload /usr/local/app/patchs/tars.upload -fi - -bin="rsync" - -PID=`ps -eopid,cmd | grep "$bin" | grep -v "grep" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi - -rsync --address=web.tars.com --daemon --config=/usr/local/app/tars/tarspatch/conf/rsync.conf & - -#mkdir -p /usr/local/app/patchs -#cd /usr/local/app/patchs -#mkdir -p /data/tars/patchs/tars -#mkdir -p /data/tars/patchs/TmpPatch -#mkdir -p /data/tars/patchs/tars.upload -#ln -s /data/tars/patchs/tars /usr/local/app/patchs/tars -#ln -s /data/tars/patchs/TmpPatch /usr/local/app/patchs/TmpPatch -#ln -s /data/tars/patchs/tars.upload /usr/local/app/patchs/tars.upload - diff --git a/cpp/framework/deploy/tarspatch/util/start.sh b/cpp/framework/deploy/tarspatch/util/start.sh deleted file mode 100644 index e5a9adebb..000000000 --- a/cpp/framework/deploy/tarspatch/util/start.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -sh /usr/local/app/tars/tarspatch/util/execute.sh diff --git a/cpp/framework/deploy/tarspatch/util/stop.sh b/cpp/framework/deploy/tarspatch/util/stop.sh deleted file mode 100644 index c975d2d66..000000000 --- a/cpp/framework/deploy/tarspatch/util/stop.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -bin="/usr/local/app/tars/tarspatch/bin/tarspatch" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarspatch" | grep -v "grep"|grep -v "sh" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" - fi - diff --git a/cpp/framework/deploy/tarsregistry/conf/tarsregistry.conf b/cpp/framework/deploy/tarsregistry/conf/tarsregistry.conf deleted file mode 100644 index 6b41c4119..000000000 --- a/cpp/framework/deploy/tarsregistry/conf/tarsregistry.conf +++ /dev/null @@ -1,103 +0,0 @@ - - - - locator = tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890 - sync-invoke-timeout=10000 - async-invoke-timeout=60000 - refresh-endpoint-interval = 60000 - stat = tars.tarsstat.StatObj - property = tars.tarsproperty.PropertyObj - report-interval = 60000 - asyncthread = 3 - modulename = tars.tarsregistry - timeout-queue-size = 100 - - - app = tars - server = tarsregistry - localip = 192.168.2.131 - basepath = /usr/local/app/tars/tarsregistry/data - datapath = /usr/local/app/tars/tarsregistry/data - logpath = /usr/local/app/tars/app_log - netthread= 2 - notify=tars.tarsnotify.NotifyObj - - endpoint = tcp -h 192.168.2.131 -p 17890 -t 10000 - allow = - maxconns = 10000 - threads = 5 - queuecap = 10000 - queuetimeout= 4000 - servant = tars.tarsregistry.QueryObj - - - endpoint = tcp -h 192.168.2.131 -p 17891 -t 30000 - allow = - maxconns = 2048 - threads = 5 - queuecap = 10000 - queuetimeout= 4000 - servant = tars.tarsregistry.RegistryObj - - - - - dbhost = db.tars.com - dbname = db_tars - dbuser = tars - dbpass = tars2015 - dbport = 3306 - charset = utf8 - dbflag = CLIENT_MULTI_STATEMENTS - - - #加载object间隔时间(s) - loadObjectsInterval = 30 - reigistryTimeout = 150 - #node心跳超时时间(s) - nodeTimeout = 250 - #轮询server状æ€çš„间隔时间(s) - queryInterval = 150 - - #第一阶段加载时间间隔,ä½æ˜¯ç§’ - loadObjectsInterval1 = 13 - #第一阶段加载最近时间更新的记录,默认是60秒 - LeastChangedTime1 = 600 - - #第二阶段(全é‡ï¼‰åŠ è½½æ—¶é—´é—´éš”,å•ä½æ˜¯ç§’ - loadObjectsInterval2 = 3601 - - #node心跳超时时间,å•ä½æ˜¯ç§’ - nodeTimeout = 250 - #主控心跳超时检测时间,å•ä½æ˜¯ç§’ - reigistryTimeout = 150 - - #æœåŠ¡çŠ¶æ€ç›‘控时间,å•ä½æ˜¯ç§’ - queryInterval = 150 - #æœåŠ¡çŠ¶æ€ç›‘控加载最近时间更新的记录,å•ä½æ˜¯ç§’ - querylesttime = 300 - #主控心跳关闭开关,默认å…许心跳检测,è¦è¿ç§»çš„时候设置次项为N - # heartbeatoff=Y - - asyncthread = 6 - - - #patch对象 - patchServerObj = tars.tarspatch.PatchObj - #查询列表对象 - QueryObjName = tars.tarsregistry.QueryObj - #node调用的Registry对象 - RegistryObjName = tars.tarsregistry.RegistryObj - - - min_block = 50 - max_block = 200 - factor = 1.2 - FilePath = /usr/local/app/tars/tarsregistry/util/objectCache.dat - FileSize = 8M - - - #node默认模æ¿å称 - defaultTemplate = tars.tarsnode - - diff --git a/cpp/framework/deploy/tarsregistry/util/env.sh b/cpp/framework/deploy/tarsregistry/util/env.sh deleted file mode 100644 index 24580d073..000000000 --- a/cpp/framework/deploy/tarsregistry/util/env.sh +++ /dev/null @@ -1,2 +0,0 @@ -export PATH=$PATH:/usr/local/app/gperftool32/bin -export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/app/gperftool32/lib/ diff --git a/cpp/framework/deploy/tarsregistry/util/execute.sh b/cpp/framework/deploy/tarsregistry/util/execute.sh deleted file mode 100644 index f5c4f6722..000000000 --- a/cpp/framework/deploy/tarsregistry/util/execute.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -ulimit -a -export PATH=${PATH}:/usr/local/tars/bin; -export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/app/gperftool/lib; -#export HEAPCHECK=strict -export HEAP_CHECK_DUMP_DIRECTORY=. -HEAP_PROFILE_MMAP=true -HEAP_PROFILE_MMAP_LOG=true -#export HEAPPROFILE=registry -bin="/usr/local/app/tars/tarsregistry/bin/tarsregistry" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsregistry" | grep -v "grep" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" -fi -ulimit -c unlimited -$bin --config=/usr/local/app/tars/tarsregistry/conf/tarsregistry.conf & diff --git a/cpp/framework/deploy/tarsregistry/util/start.sh b/cpp/framework/deploy/tarsregistry/util/start.sh deleted file mode 100644 index dbb45d08b..000000000 --- a/cpp/framework/deploy/tarsregistry/util/start.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -sh /usr/local/app/tars/tarsregistry/util/execute.sh diff --git a/cpp/framework/deploy/tarsregistry/util/stop.sh b/cpp/framework/deploy/tarsregistry/util/stop.sh deleted file mode 100644 index 2ef733b1e..000000000 --- a/cpp/framework/deploy/tarsregistry/util/stop.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -bin="/usr/local/app/tars/tarsregistry/bin/tarsregistry" - -PID=`ps -eopid,cmd | grep "$bin"| grep "tarsregistry" | grep -v "grep"|grep -v "sh" |awk '{print $1}'` - -echo $PID - -if [ "$PID" != "" ]; then - kill -9 $PID - echo "kill -9 $PID" - fi - diff --git a/cpp/framework/patchclient/CMakeLists.txt b/cpp/framework/patchclient/CMakeLists.txt deleted file mode 100644 index f030f9997..000000000 --- a/cpp/framework/patchclient/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ - -complice_module("patchclient") - -add_library(patch tars_patch.cpp) - -add_dependencies(patch FRAMEWORK-JCE) diff --git a/cpp/framework/patchclient/patchclient.cpp b/cpp/framework/patchclient/patchclient.cpp deleted file mode 100644 index 6104f7876..000000000 --- a/cpp/framework/patchclient/patchclient.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "tars_patch.h" -#include "util/tc_option.h" -#include "servant/Application.h" - -#include - -using namespace tars; - -void usage(int argc, char *argv[]) -{ - cout << "Usage:" << argv[0] << " --server=[server] --remote=[server-dir] --local=[local] [--remove]" << endl; - cout << "server: PatchObj@tcp -h ip -p port -t timeout" << endl; - cout << "remote: directory or file sync from server" << endl; - cout << "local: directory sync to local" << endl; - cout << "remove: remove local dir data" << endl; - exit(0); -} - -class PatchClientInterface : public TarsPatchNotifyInterface -{ -public: - virtual void onDownload(const FileInfo &fi) - { - cout << "------------------------------------" << endl; - cout << "downloading [" << fi.path << "] [" << fi.size*1.0/1024 << "K]" << endl; - } - virtual void onDownloading(const FileInfo &fi, size_t pos, const string &dest) - { - cout << "downloading [" << fi.path << " ->] [" << dest << "] [" << pos*100/fi.size << "/100%]" << endl; - } - virtual void onDownloadOK(const FileInfo &fi, const string &dest) - { - cout << "download ok [" << fi.path << "] -> [" << dest << "]" << endl; - } - virtual void onListFile() - { - cout << "listing files" << endl; - } - virtual void onListFileOK(const vector &vf) - { - cout << "list ok total [" << vf.size() << "] files" << endl; - } - virtual void onRemoveLocalDirectory(const string &sDir) - { - cout << "------------------------------------" << endl; - cout << "remove local directory [" << sDir << "]" << endl; - } - virtual void onRemoveLocalDirectoryOK(const string &sDir) - { - cout << "remove local directory ok [" << sDir << "]"<< endl; - cout << "------------------------------------" << endl; - } - virtual void onSetExecutable(const FileInfo &fi) - { - cout << "executable [" << fi.path << "]" << endl; - } - virtual void onDownloadAllOK(const vector &vf, time_t timeBegin, time_t timeEnd) - { - cout << "------------------------------------" << endl; - cout << "download all files succ." << endl; - } - - virtual void onReportTime(const string & sFile, const time_t timeBegin, const time_t timeEnd) - { - cout << "------------------------------------" << endl; - cout << "download '" << sFile << "', begin:" << timeBegin << ", end:" << timeEnd << endl; - } -}; - -typedef TC_AutoPtr PatchClientInterfacePtr; - -int main(int argc, char *argv[]) -{ - - if(argc < 2) - { - usage(argc, argv); - } - - try - { - - TC_Option option; - option.decode(argc, argv); - - //远程目录的相对路径 - string server = option.getValue("server"); - if(server.empty()) - { - cout << "server must not be empty." << endl; - return 0; - } - - CommunicatorFactory::getInstance()->getCommunicator()->setProperty("sync-invoke-timeout", "60000"); - PatchPrx pPtr; - CommunicatorFactory::getInstance()->getCommunicator()->stringToProxy(server, pPtr); - - //远程目录的相对路径 - string remote_dir = option.getValue("remote"); - if(remote_dir.empty()) - { - cout << "remote_dir must not be empty." << endl; - return 0; - } - - //本地åŒæ­¥è·¯å¾„ - string local_dir = option.getValue("local"); - if(local_dir.empty()) - { - cout << "local_dir must not be empty." << endl; - return 0; - } - - //是å¦å…ˆæ¸…空本地目录 - bool bRemove = option.hasParam("remove"); - - cout << "connecting to patch server" << endl; - TarsPatch op; - op.init(pPtr, remote_dir, local_dir); - cout << "connect ok to patch server" << endl; - op.setRemove(bRemove); - - PatchClientInterfacePtr p = new PatchClientInterface(); - op.download(p); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/framework/patchclient/tars_patch.cpp b/cpp/framework/patchclient/tars_patch.cpp deleted file mode 100644 index eba44aad6..000000000 --- a/cpp/framework/patchclient/tars_patch.cpp +++ /dev/null @@ -1,291 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_file.h" -#include "util/tc_option.h" -#include "util/tc_md5.h" -#include "servant/TarsLogger.h" -#include "tars_patch.h" - -#include - -namespace tars -{ -TarsPatch::TarsPatch() -: _remove(false) -{ -} - -void TarsPatch::checkLocalDir() -{ - if (!tars::TC_File::isFileExistEx(_localDir, S_IFDIR)) - { - throw TarsPatchException("local dir '" + _localDir + "' must be a directory"); - } -} - -void TarsPatch::init(const PatchPrx &patchPrx, const string &remote_dir, const string &local_dir, const bool bCheck) -{ - _patchPrx = patchPrx; - _remoteDir = remote_dir; - _localDir = local_dir; - - if (bCheck) - { - checkLocalDir(); - } -} - -void TarsPatch::setRemove(bool bRemove) -{ - _remove = bRemove; -} - -void TarsPatch::download(const TarsPatchNotifyInterfacePtr &pPtr) -{ - //记录下载本次æœåŠ¡çš„总的时间开始 - time_t timeBegin = TNOW; - - checkLocalDir(); - - if (_remove) - { - string tmp = TC_File::simplifyDirectory(_localDir);// + "/" + _remoteDir); - - if (pPtr) pPtr->onRemoveLocalDirectory(tmp); - - int ret = TC_File::removeFile(tmp, true); - if (ret != 0) - { - throw TarsPatchException("remove '" + tmp + "' error", errno); - } - - if (pPtr) - { - pPtr->onRemoveLocalDirectoryOK(tmp); - } - } - - if (pPtr) - { - pPtr->onListFile(); - } - - //获å–åŒæ­¥åˆ—表 - vector vf; - int ret = -1; - - int nRetryTime = 0; - -RETRY: - try - { - ret = _patchPrx->listFileInfo(_remoteDir, vf); - - } - catch(TarsException& ex) - { - //最多é‡è¯•ä¸‰æ¬¡ - if(nRetryTime < 3) - { - nRetryTime++; - goto RETRY; - } - TLOGERROR("TarsPatch::download load error|" << __FILE__ << "," << __LINE__ << "|ex:" << ex.what() << endl); - throw TarsPatchException(ex.what()); - } - - if (ret != 0 && ret != 1) - { - throw TarsPatchException("listFileInfo error"); - } - - if (pPtr) - { - pPtr->onListFileOK(vf); - } - - if (ret == 0) - { - //path是路径, 对æ¯ä¸ªæ–‡ä»¶ä¸‹è½½ - for (size_t i = 0; i < vf.size(); i++) - { - download(true, vf[i], pPtr); - } - } - else if (ret == 1) - { - //path是文件 - download(false, vf[0], pPtr); - } - - time_t timeEnd = TNOW; - - if (pPtr) - { - pPtr->onDownloadAllOK(vf, timeBegin, timeEnd); - } -} - -void TarsPatch::download(bool bDir, const FileInfo &fi, const TarsPatchNotifyInterfacePtr &pPtr) -{ - if (pPtr) - { - pPtr->onDownload(fi); - } - - //获å–本地文件目录 - string file_dir = _localDir; - - if (bDir) - { - file_dir = tars::TC_File::simplifyDirectory(_localDir + "/" + tars::TC_File::extractFilePath(fi.path)); - - if (!file_dir.empty()) - { - //创建本地目录 - if (!tars::TC_File::makeDirRecursive(file_dir)) - { - throw TarsPatchException("create directory '" + file_dir + "' failed!"); - } - } - else - { - file_dir = "."; - } - } - - //本地文件å - string file = tars::TC_File::simplifyDirectory(file_dir + "/" + tars::TC_File::extractFileName(fi.path)); - - FILE *fp = fopen(file.c_str(), "wb"); - if (!fp) - { - throw TarsPatchException("fopen file '" + file + "' error", errno); - } - - - time_t timeBegin = TNOW; - try - { - //循环下载文件到本地 - vector v; - int pos = 0; - while (true) - { - v.clear(); - int ret; - - int nRetryTime = 0; - - RETRY: - try - { - if (bDir) - { - ret = _patchPrx->download(tars::TC_File::simplifyDirectory(_remoteDir + "/" + fi.path), pos, v); - } - else - { - ret = _patchPrx->download(tars::TC_File::simplifyDirectory(_remoteDir), pos, v); - } - } - catch(TarsException& ex) - { - //最多é‡è¯•ä¸¤æ¬¡ - if(nRetryTime < 2) - { - nRetryTime++; - goto RETRY; - } - TLOGERROR("TarsPatch::download load error|" << __FILE__ << "," << __LINE__ << "|"<onDownloading(fi, pos, file); - } - else if (ret == 1) - { - TLOGERROR("TarsPatch::download load succ|" << __FILE__ << "," << __LINE__ << "|" << fi.path << "|" << fi.md5<< "|" << pos << endl); - break; - } - } - } - catch (...) - { - fclose(fp); - fp = NULL; - TLOGERROR("TarsPatch::download error|" << __FILE__ << "," << __LINE__ << "|" << fi.path << "|" << fi.md5 << endl); - throw; - } - - fclose(fp); - - if (fi.canExec) - { - int ret = tars::TC_File::setExecutable(file, true); - if (ret == 0) - { - if (pPtr) - { - pPtr->onSetExecutable(fi); - } - } - else - { - throw TarsPatchException("set file '" + file + "' executable error!"); - } - } - - //检查MD5值 - if (!fi.md5.empty()) - { - std::string smd5 = tars::TC_MD5::md5file(file); - if (smd5 != fi.md5) - { - TLOGERROR("TarsPatch::download " << __FILE__ << "," << __LINE__ << "|" << fi.path << "|" << fi.md5 << "|" << smd5 << endl); - throw TarsPatchException(fi.path + "'s md5 is not equal to the file in patch server!");; - } - } - - time_t timeEnd = TNOW; - if (pPtr) - { - pPtr->onReportTime(fi.path, timeBegin, timeEnd); - } - - if (pPtr) - { - pPtr->onDownloadOK(fi, file); - } -} - - -} - - diff --git a/cpp/framework/patchclient/tars_patch.h b/cpp/framework/patchclient/tars_patch.h deleted file mode 100644 index e339782cd..000000000 --- a/cpp/framework/patchclient/tars_patch.h +++ /dev/null @@ -1,194 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PATCH_H_ -#define __TARS_PATCH_H_ - -#include "Patch.h" - -namespace tars -{ -///////////////////////////////////////////////////////////////////////////////////// -/** - * patchæœåŠ¡å®¢æˆ·ç«¯åº“ - * 采用该库实现从patchæœåŠ¡åŒæ­¥æ–‡ä»¶åˆ°æœ¬åœ° - */ - -///////////////////////////////////////////////////////////////////////////////////// -/** - * 下载异常 - */ -struct TarsPatchException : public TC_Exception -{ - TarsPatchException(const string& buffer) - : TC_Exception(buffer) - { - } - - TarsPatchException(const string& buffer, int err) - : TC_Exception(buffer, err) - { - } - - ~TarsPatchException() throw() - { - } -}; - -/** - * ä¸‹è½½é€šçŸ¥æŽ¥å£ - */ -class TarsPatchNotifyInterface : public TC_HandleBase -{ -public: - /** - * 删除本地目录 - * @param sDir - */ - virtual void onRemoveLocalDirectory(const string& sDir) = 0; - - /** - * 删除本地目录æˆåŠŸ - * @param sDir - */ - virtual void onRemoveLocalDirectoryOK(const string& sDir) = 0; - - /** - * 开始获å–文件链表 - */ - virtual void onListFile() = 0; - - /** - * 获å–文件链表æˆåŠŸ - * @param vf - */ - virtual void onListFileOK(const vector& vf) = 0; - - /** - * 开始下载 - * @param fi - */ - virtual void onDownload(const FileInfo& fi) = 0; - - /** - * 下载文件æˆåŠŸ - * @param fi - * @param pos - * @param dest, ä¿å­˜æœ¬åœ°è·¯å¾„ - */ - virtual void onDownloading(const FileInfo& fi, size_t pos, const string& dest) = 0; - - /** - * 下载文件æˆåŠŸ - * @param fi - * @param dest, ä¿å­˜æœ¬åœ°è·¯å¾„ - */ - virtual void onDownloadOK(const FileInfo& fi, const string& dest) = 0; - - /** - * 设置å¯æ‰§è¡Œ - * @param fi - */ - virtual void onSetExecutable(const FileInfo& fi) = 0; - - /** - * 所有文件列表文件下载æˆåŠŸ - * @param vf - */ - virtual void onDownloadAllOK(const vector& vf, time_t timeBegin, time_t timeEnd) = 0; - - /** - * 上报下载文件所有的时间 - * @param file, beginTime, endTime - */ - virtual void onReportTime(const string& sFile, const time_t timeBegin, const time_t timeEnd) = 0; -}; - -typedef TC_AutoPtr TarsPatchNotifyInterfacePtr; - -/** - * 下载æ“作类 - */ -class TarsPatch -{ -public: - - /** - * 构造函数 - */ - TarsPatch(); - - /** - * åˆå§‹åŒ– - * @param _patchPrx, patchæœåŠ¡å™¨åœ°å€ - * @param remote_dir, 远程åŒæ­¥ç›®å½•, ä¸èƒ½æœ‰..ç¬¦å· - * @param local_dir, åŒæ­¥åˆ°æœ¬åœ°çš„目录 - */ - void init(const PatchPrx& patchPrx, const string& remote_dir, const string& local_dir, const bool bCheck = true); - - /** - * 设置是å¦åˆ é™¤æœ¬åœ°ç›®å½• - * @param bRemove - */ - void setRemove(bool bRemove); - - /** - * 下载, 失败抛出异常 - * - */ - void download(const TarsPatchNotifyInterfacePtr& pPtr); - -protected: - - /** - * 下载æŸä¸€ä¸ªæ–‡ä»¶ - * @param fi - */ - void download(bool bDir, const FileInfo& fi, const TarsPatchNotifyInterfacePtr& pPtr); - - /** - * 检查本地路径 - */ - void checkLocalDir(); - -protected: - - /** - * è¿œç¨‹åœ°å€ - */ - string _remoteDir; - - /** - * æœ¬åœ°åœ°å€ - */ - string _localDir; - - /** - * 是å¦åˆ é™¤æœ¬åœ°åŒæ­¥ç›®å½• - */ - bool _remove; - - /** - * patchæœåŠ¡å™¨ - */ - PatchPrx _patchPrx; -}; - -} - -#endif - - diff --git a/cpp/framework/protocol/AdminReg.tars b/cpp/framework/protocol/AdminReg.tars deleted file mode 100644 index 7ffe15631..000000000 --- a/cpp/framework/protocol/AdminReg.tars +++ /dev/null @@ -1,493 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NodeDescriptor.tars" - -module tars -{ - struct ServerStateDesc - { - 0 require string settingStateInReg; - 1 require string presentStateInReg; - 2 require string presentStateInNode; - 3 require int processId; - 4 require string patchVersion; - 5 require string patchTime; - 6 require string patchUser; - }; - - enum tarsErrCode - { - //请求的node节点没有注册或者节点心跳超时 - EM_TARS_NODE_NOT_REGISTRY_ERR = 1001, - //主控请求node节点超时 - EM_TARS_CALL_NODE_TIMEOUT_ERR = 1002, - //从主控数æ®åº“获å–æœåŠ¡çš„æè¿°ä¿¡æ¯é”™è¯¯ - EM_TARS_LOAD_SERVICE_DESC_ERR = 1003, - //æœåŠ¡å¯èƒ½å¤„在åœæ­¢æˆ–者å‘布过程中 - EM_TARS_SERVICE_STATE_ERR = 1004, - //åŒæ ·çš„请求已ç»åœ¨å¤„ç†äº† - EM_TARS_REQ_ALREADY_ERR = 1005, - //请求æ¥è‡ªéžæ³•çš„ip地å€,如并éžåˆæ³•çš„ä¸»æŽ§åœ°å€ - EM_TARS_INVALID_IP_ERR = 1006, - //å‚数错误 - EM_TARS_PARAMETER_ERR = 1007, - //éžtarsæ–¹é¢çš„问题,用于node判断业务机器问题等 - EM_TARS_OTHER_ERR = 1008, - //获å–patch file异常 - EM_TARS_GET_PATCH_FILE_ERR = 1009, - //调用patch server准备å‘布包失败 - EM_TARS_PREPARE_ERR = 1010, - //未知错误 - EM_TARS_UNKNOWN_ERR = -1, - //æˆåŠŸ - EM_TARS_SUCCESS = 0 - }; - - /** - * 管ç†å¹³å°è¿‡æ¥çš„命令 - */ - enum EMTaskCommand - { - EM_CMD_START, - EM_CMD_STOP, - EM_CMD_PATCH, - EM_CMD_UNINSTALL - }; - - /** - * ä»»åŠ¡çš„çŠ¶æ€ - */ - enum EMTaskStatus { - EM_T_NOT_START, - EM_T_RUNNING, - EM_T_SUCCESS, - EM_T_FAILED, - EM_T_CANCEL, - EM_T_PARIAL, - }; - - /** - * 任务ITEMçŠ¶æ€ - */ - enum EMTaskItemStatus - { - EM_I_NOT_START, - EM_I_RUNNING, - EM_I_SUCCESS, - EM_I_FAILED, - EM_I_CANCEL, - }; - - /** - * 异步任务 - */ - struct TaskItemReq - { - 0 optional string taskNo; - 1 optional string itemNo; - 2 optional string application; - 3 optional string serverName; - 4 optional string nodeName; - 5 optional string setName; - 6 optional string command; - 7 optional string userName; - 8 optional map parameters; - }; - - /** - * 任务当å‰çš„ä¿¡æ¯ - */ - struct TaskItemRsp - { - 0 optional TaskItemReq req; - 1 optional string startTime; - 2 optional string endTime; - 3 optional EMTaskItemStatus status; - 4 optional string statusInfo; - 5 optional string executeLog; - }; - - /** - * Task任务 - */ - struct TaskReq - { - 0 optional vector taskItemReq; - 1 optional string taskNo; - 2 optional bool serial; - 3 optional string userName; - }; - - struct TaskRsp - { - 0 optional vector taskItemRsp; - 1 optional string taskNo; - 2 optional bool serial; - 3 optional string userName; - 4 optional EMTaskStatus status; - }; - - enum ServerGridStatus - { - //正常å‘布æœåŠ¡ - NORMAL = 0, - //自动扩容时,ç°åº¦æœåŠ¡ï¼Œå³éƒ¨åˆ†æµé‡åˆ†åˆ°ç°åº¦èŠ‚点上 - GRID = 1, - //å‘布æœåŠ¡ï¼Œä½†æ˜¯å‘布åŽï¼Œæ²¡ç”¨æµé‡è¿‡æ¥ - NO_FLOW = 2 - }; - - struct ServerGridDesc - { - 0 require string application; - 1 require string servername; - 2 require string nodename; - 3 require ServerGridStatus status; - }; - - /** - * admin 管ç†æŽ¥å£ - */ - interface AdminReg - { - - /** - * 请求异步任务 - * - * @param taskList : 任务列表 - * @param serial: 串行 - * - * @return 0: æˆåŠŸ, <0:失败 - */ - int addTaskReq(TaskReq taskReq); - - /** - * 获å–ä»»åŠ¡çŠ¶æ€ - * - * @param taskIdList : 任务列表id - * - * @return ä»»åŠ¡çŠ¶æ€ - */ - int getTaskRsp(string taskNo, out TaskRsp taskRsp); - - /** - * 获å–æ ¹æ®åº”用, æœåŠ¡, 命令字获å–历å²ä»»åŠ¡çŠ¶æ€ - * - * @param application : 应用å - * @param serverName : æœåŠ¡å - * @param command : 命令字 - * - * @return TaskRsp - */ - int getTaskHistory(string application, string serverName, string command, out vector taskRsps); - - /** - * 设置task itemçš„ä¿¡æ¯(内部使用) - * @param itemNo, itemno - * @param startTime, 开始时间 - * @param endTime, 结æŸæ—¶é—´ - * @param status, çŠ¶æ€ - * @param log, 异常日志 - * @return int, 0: æˆåŠŸ, <0: 失败 - */ - int setTaskItemInfo(string itemNo, map info); - - ////////////////////////////////////////////////////////////////////////////////// - /** - * 获å–application列表 - * - * @param out result : 结果æè¿° - * - * @return application列表 - */ - vector getAllApplicationNames(out string result); - - /** - * 获å–node列表 - * - * @param out result : 结果æè¿° - * - * @return node 列表 - */ - vector getAllNodeNames(out string result); - - /** - * ping node - * - * @param name: node id - * @param out result : 结果æè¿° - * - * @return : true-ping通;false-ä¸é€š - */ - bool pingNode(string name, out string result); - - /** - * get node version - * - * @param name: node id - * @param out version :版本 - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - int getNodeVesion(string name, out string version, out string result); - - /** - * åœæ­¢ node - * - * @param name: node id - * @param out result : 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - int shutdownNode(string name, out string result); - - /***********server****************/ - - /** - * 获å–server列表 - * - * @param name: null - * @param out result : 结果æè¿° - * - * @return: server列表åŠç›¸å…³ä¿¡æ¯ - */ - vector > getAllServerIds(out string result); - - /** - * 获å–特定serverçŠ¶æ€ - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeNmae : node id - * @param out state : çŠ¶æ€ - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int getServerState(string application,string serverName, string nodeName, out ServerStateDesc state, out string result); - - /** - * 获å–æŸip所属group 用于机房部署 例如æŸip属于那个机房 - * - * @param nodeNmae : ip - * @param out int : group id - * @param out result : 结果æè¿° - * - * @return : 处ç†ç»“æžœ - */ - int getGroupId(string ip, out int groupId, out string result); - - /** - * å¯åŠ¨ç‰¹å®šserver - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int startServer(string application,string serverName, string nodeName, out string result); - - /** - * åœæ­¢ç‰¹å®šserver - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int stopServer(string application,string serverName, string nodeName, out string result); - - /** - * é‡å¯ç‰¹å®šserver - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int restartServer(string application,string serverName, string nodeName, out string result); - - /** - * 通知server - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param command : 命令 - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int notifyServer(string application,string serverName, string nodeName, string command, out string result); - - /** - * å‘布特定server - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param shutdown : 是å¦éœ€è¦åœæ­¢æœåŠ¡ - * @param version : patch的版本 - * @param user : å‘布者 - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ -// int patchServer(string application, string serverName, string nodeName, bool shutdown, string version, string user, out string result); - - /** - * å¸è½½æœåŠ¡ - * @param application - * @param serverName - * @param nodeName - * @return 0: æˆåŠŸ, <0: 失败 - */ - int undeploy(string application, string serverName, string nodeName, string user, out string log); - - /** - * 批é‡å‘布æœåŠ¡ - * - * @param vector: 批é‡å‘布的请求 - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int batchPatch(PatchRequest req, out string result); - - /** - * æ›´æ–°å‘å¸ƒçŠ¶æ€ - * - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int updatePatchLog(string application, string serverName, string nodeName, string patchId, string user, string patchType, bool succ); - - /** - * 预å‘布接å£ï¼Œå°†æŒ‡å®šç‰ˆæœ¬çš„二进制文件下载到node - * - * @param PreparePatchRequest: 预å‘布 - * @param out nodeList :å·²ç»æ交预å‘布任务的node列表 - * - * @return : 0 - */ -// int prepareBatchPatch(PreparePatchRequest req, out vector nodeList, out string result); - - /** - * 获å–预å‘å¸ƒçš„è¿›åº¦ä¿¡æ¯ - * specialNodeList:如果为空,管ç†æœåŠ¡æ ¹æ®application+serverName查询node列表,如果ä¸ä¸ºç©ºï¼Œç›´æŽ¥ä½¿ç”¨ - */ -// int getPreparePatchPercent(string application, string serverName, vector specialNodeList, out map patchInfos, out string result); - - /** - * å‘布特定文件 - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param sSrcFile : patchæœåŠ¡å™¨ä¸Šçš„路径 - * @param sDstFile : nodeæœåŠ¡å™¨ä¸Šçš„目标路径 - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ -// int patchSubborn(string application, string serverName, string nodeName, string sSrcFile, string sDstFile, out string result); - - /** - * 获å–æœåŠ¡å‘布进度 - * @param application : æœåŠ¡æ‰€å±žåº”用å - * @param serverName : æœåŠ¡å - * @param nodeName : node id - * @out tPatchInfo :å‘å¸ƒä¿¡æ¯ - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int getPatchPercent(string application,string serverName,string nodeName,out PatchInfo tPatchInfo); - - /** - * 加载特定server - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out result : 结果æè¿° - * - * @return : 返回值详è§tarsErrCode枚举值 - */ - int loadServer(string application, string serverName, string nodeName, out string result); - - /** - * 获å–ç›¸åº”æ¨¡æ¿ - * - * @param profileName: 模æ¿å称 - * @param out profileTemplate: 模æ¿å†…容 - * @param out resultDesc: 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - int getProfileTemplate(string profileName , out string profileTemplate, out string resultDesc); - - /** - * 获å–æœåŠ¡ç›¸åº”æ¨¡æ¿ - * - * @param application: 应用 - * @param serverName : serverå - * @param nodeName : node id - * @param out profileTemplate: 模æ¿å†…容 - * @param out resultDesc: 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - int getServerProfileTemplate(string application, string serverName, string nodeName, out string profileTemplate, out string resultDesc); - - /** - * node通过接å£èŽ·å–连接上主控的node ip - * @param sNodeIp: node çš„ip - * - * @return 0-æˆåŠŸ others-失败 - */ - int getClientIp(out string sClientIp); - - /** - * 获å–æŸä¸ªæˆ–则所有æœåŠ¡çš„setdå’Œidcåå­— - * @param info: æ ¹æ®è¾“å…¥å‚数获å–ä¿¡æ¯ - * @param out vServerInfo æœåŠ¡ä¿¡æ¯ - * - * @return 0-æˆåŠŸ others-失败 - */ -// int getServerInfo(srvRequestInfo info,out vector vServerInfo); - - /** - * 自动伸缩时调用的ç°åº¦å‘å¸ƒæŽ¥å£ - * - * @param gridDescList: ç°åº¦å‘å¸ƒçš„ä¿¡æ¯ - * @param gridFailDescList: ç°åº¦å‘å¸ƒèŠ‚ç‚¹å¤±è´¥çš„ä¿¡æ¯ - * @param out resultDesc: 结果æè¿° - * - * @return : 0-æˆåŠŸ others-失败 - */ - int gridPatchServer(vector gridDescList, out vector gridFailDescList, out string resultDesc); - - }; -}; - diff --git a/cpp/framework/protocol/CMakeLists.txt b/cpp/framework/protocol/CMakeLists.txt deleted file mode 100644 index d6faaad7e..000000000 --- a/cpp/framework/protocol/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set(OUT_DEPENDS_LIST) - -complice_tars(OUT_DEPENDS_LIST) - -add_custom_target(FRAMEWORK-PROTOCOL ALL DEPENDS ${OUT_DEPENDS_LIST}) diff --git a/cpp/framework/protocol/Node.tars b/cpp/framework/protocol/Node.tars deleted file mode 100644 index 0298a1fc7..000000000 --- a/cpp/framework/protocol/Node.tars +++ /dev/null @@ -1,218 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NodeDescriptor.tars" -#include "RegistryDescriptor.tars" -module tars -{ - - interface Node - { - - /** - * 销æ¯æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int 0æˆåŠŸ 其它失败 - */ - int destroyServer(string application,string serverName, out string result); - - /** - * patch指定æœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @param appDistrib 在patch server 上的app目录 - * @param shutdown 是å¦è¦æ±‚æœåŠ¡çŠ¶æ€ä¸ºdisactive - * @return int 0æˆåŠŸ 其它失败 - */ - // int patch(string application, string serverName, bool shutDown,string patchServer, out string result); - - /** - * 增强的å‘å¸ƒæŽ¥å£ - * pushRequest æ’å…¥å‘布请求到队列 - * @param req å‘布请求 - * @return int 0æˆåŠŸ 其它失败 - */ - int patchPro(PatchRequest req, out string result); - - /** - * 预å‘布将指定版本的tgz拉å–到本地 - * - */ - // int preparePatchPro(PreparePatchRequest req, out string result); - - /** - * å‘布æœåŠ¡è¿›åº¦ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @out tPatchInfoä¸‹è½½ä¿¡æ¯ - * @return int - */ - // int getPreparePatchPercent(string application,string serverName,out PreparePatchInfo prepareInfo); - - /** - * patch指定文件路径 - * @param srcFile 文件或者路径在patchæœåŠ¡å™¨ä¸Šçš„目录 - * @param dstFile 文件或者路径在nodeæœåŠ¡å™¨ä¸Šå­˜æ”¾çš„目录 - * @param patchServer patchæœåŠ¡å™¨ - * @return int 0æˆåŠŸ 其它失败 - */ - // int patchSubborn(string patchServer, string application, string serverName, string srcFile, string dstFile, out string result); - - /** - * 下载指定文件 - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @param file 文件å - * @param out result 失败说明 - * @return int 0æˆåŠŸ éž0失败 - */ - - int addFile(string application, string serverName, string file,out string result); - - /** - * 获å–nodeå称 - * @return string - */ - string getName(); - - /** - * 获å–node上负载 - * @return LoadInfo - */ - LoadInfo getLoad(); - - /** - * 关闭node - * @return int - */ - int shutdown(out string result); - - /** - * 关闭node上所有æœåŠ¡ - * @return int - */ - int stopAllServers(out string result); - - /** - * 载入指定æœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int loadServer(string application, string serverName, out string result); - - /** - * å¯åŠ¨æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int startServer(string application,string serverName, out string result); - - /** - * åœæ­¢æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int stopServer(string application,string serverName, out string result); - - /** - * é‡å¯æŒ‡å®šæœåŠ¡ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ -// int restartServer(string application,string serverName, out string result); - - /** - * 通知æœåŠ¡ - * @return int - */ - int notifyServer(string application, string serverName, string command, out string result); - - /** - * 获å–指定æœåŠ¡pidè¿›ç¨‹å· - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int getServerPid(string application,string serverName, out string result); - - /** - * 获å–指定æœåŠ¡registryè®¾ç½®çš„çŠ¶æ€ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - ServerState getSettingState(string application,string serverName, out string result); - - /** - * 获å–指定æœåŠ¡çŠ¶æ€ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return ServerState - */ - ServerState getState(string application,string serverName, out string result); - - /** - * 获å–指定æœåŠ¡åœ¨nodeä¿¡æ¯ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return ServerState - */ - int getStateInfo(string application,string serverName,out ServerStateInfo info,out string result); - - /** - * åŒæ­¥æœåŠ¡çŠ¶æ€ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @return int - */ - int synState(string application,string serverName, out string result); - - /** - * å‘布æœåŠ¡è¿›åº¦ - * @param application æœåŠ¡æ‰€å±žåº”用å - * @param serverName æœåŠ¡å - * @out tPatchInfoä¸‹è½½ä¿¡æ¯ - * @return int - */ - int getPatchPercent(string application,string serverName,out PatchInfo tPatchInfo); - - /** - *备份和删除cache的共享内存 - *@param sFullCacheName cacheæœåŠ¡å…¨å - *@param sBackupPath 备份路径 - *@param sKey 共享内存的key值,如果为空则通过路径和‘Dâ€™ç”Ÿæˆ - *@return int - */ - int delCache(string sFullCacheName,string sBackupPath, string sKey, out string result); - - /** - * 获å–机器没有使用的共享内存的key列表,æ¯å°æœºå™¨æœ€å¤šåˆ†é…256个key, -1分é…失败 - */ - int getUnusedShmKeys(int count, out vector shm_keys); - - /* - * é‡æ–°èŽ·å–部署在该节点的æœåŠ¡é…置文件 - */ -// int reloadServerConf(string application,string serverName, out string result); - }; - -}; - diff --git a/cpp/framework/protocol/NodeDescriptor.tars b/cpp/framework/protocol/NodeDescriptor.tars deleted file mode 100644 index 3cd19d60a..000000000 --- a/cpp/framework/protocol/NodeDescriptor.tars +++ /dev/null @@ -1,147 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - enum ServerState - { - Inactive, - Activating, - Active, - Deactivating, - Destroying, - Destroyed - }; - - - struct LoadInfo - { - //过去1分钟负载 - 0 require float avg1; - //过去5分钟负载 - 1 require float avg5; - //过去15分钟负载 - 2 require float avg15; - //1分钟内cpu使用率å‡å€¼ - 3 optional int avgCpu; - }; - - struct PatchInfo - { - //是å¦æ­£åœ¨å‘布 - 0 require bool bPatching = false; - //å‘布百分比 - 1 require int iPercent; - //最近百分比修改时间 - 2 require int iModifyTime; - //版本 - 3 require string sVersion; - //结果æè¿° - 4 require string sResult; - //å‘布结果 - 5 optional bool bSucc = false; - }; - - /** - * 预å‘å¸ƒçš„è¿›åº¦ä¿¡æ¯ - */ - struct PreparePatchInfo - { - //是å¦æ­£åœ¨å‘布 - 0 require bool bPreparePatching = false; - //å‘布百分比 - 1 require int iPercent; - //最近百分比修改时间 - 2 require int iModifyTime; - //版本 - 3 require string sVersion; - //结果æè¿° - 4 require string sResult; - //预å‘布结果, 0:æˆåŠŸï¼Œå…¶ä»–失败,失败原因è§sResult - 5 optional int ret = 0; - }; - - struct NodeInfo - { - 0 require string nodeName; - 1 require string nodeObj; - 2 require string endpointIp; - 3 require int endpointPort; - 4 require short timeOut; - 5 require string dataDir; - 6 optional string version; - //å¯åŠ¨tarsnode时,从start.tmp获å–,设置的core大å°,core file size,因为å¯èƒ½å€¼ä¸ºunlimited - 7 optional string coreFileSize; - //å¯åŠ¨tarsnode时,从start.tmp获å–,设置的open files - 8 optional int openFiles; - }; - - struct ServerStateInfo - { - //present state - 0 require ServerState serverState; - 1 require int processId; - 2 optional string nodeName; - 3 optional string application; - 4 optional string serverName; - //setting states - 5 optional ServerState settingState; - }; - - struct PatchRequest - { - 0 require string appname; - 1 require string servername; - 2 require string nodename; - 3 require string groupname; - 4 require string binname; - 5 require string version; - 6 require string user; - 7 require string servertype; - 8 require string patchobj; - 9 require string md5; - //æ“作系统类型,如:suseã€tlinuxç­‰ - 10 optional string ostype; - // 带路径的å‘布包文件å,如果未指定filepath,则node将根æ®appnameã€servernameã€versionã€ostype等按规则组装å‘布包文件路径; - // 若已指定filepath,则node从filepath下载å‘布包,nodeä¸æ£€æŸ¥filepathçš„åˆæ³•æ€§ã€‚ - 11 optional string filepath; - }; - - struct PreparePatchRequest - { - 0 require string appname; - 1 require string servername; - 2 require string groupname; - 3 require string version; - 4 require string user; - 5 require string servertype; - 6 require string patchobj; - 7 require string md5; - //æ“作系统类型,如:suseã€tlinuxç­‰ - 8 require string ostype; - //指定的node列表进行预å‘布。specialNodeList为空表示å‘布所有节点 - 9 require vector specialNodeList; - // 带路径的å‘布包文件å,如果未指定filepath,则node将根æ®appnameã€servernameã€versionã€ostype等按规则组装å‘布包文件路径; - // 若已指定filepath,则node从filepath下载å‘布包,nodeä¸æ£€æŸ¥filepathçš„åˆæ³•æ€§ã€‚ - 10 optional string filepath; - }; - - //定义主键,用于nodeå†…é˜Ÿåˆ—çš„åŽ»é‡ - key[PreparePatchRequest, appname, servername, version]; - - -}; - diff --git a/cpp/framework/protocol/Patch.tars b/cpp/framework/protocol/Patch.tars deleted file mode 100644 index 0a9720506..000000000 --- a/cpp/framework/protocol/Patch.tars +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - -struct FileInfo -{ - /** - * 文件路径, 相对listFileInfo中path的路径 - **/ - 0 require string path; - - /** - * 文件大å°, 字节数 - **/ - 1 require int size; - - /** - * 文件是å¦å¯æ‰§è¡Œ - */ - 2 require bool canExec; - - /** - * 该文件的MD5值 - */ - 3 optional string md5; -}; - -/** - * - * å‘布æœåŠ¡çš„æŽ¥å£ - * - **/ -interface Patch -{ - /** - * 获å–è·¯å¾„ä¸‹æ‰€æœ‰æ–‡ä»¶åˆ—è¡¨ä¿¡æ¯ - * @param path, 目录路径, 路径中ä¸èƒ½åŒ…å«.. - * @param vector, æ–‡ä»¶åˆ—è¡¨ä¿¡æ¯ - * @return int, 返回0表示path是目录, 1表示path是文件, -1表示路径错误 - */ - int listFileInfo(string path, out vector vf); - - /** - * 下载文件 - * @param file, 文件路径 - * @param pos, 从什么ä½ç½®å¼€å§‹ä¸‹è½½ - * @param vector, 文件内容 - * @param int, 0:读å–表示æˆåŠŸ, 1:读å–到文件末尾了, <0: 读å–失败 - */ - int download(string file, int pos, out vector vb); - - /** - * 准备好需è¦patch的文件 - * @param patchFile, 需è¦å‘布的文件å - * @return int, 0: æˆåŠŸ, <0: 失败 - */ - int preparePatchFile(string app, string serverName, string patchFile); -}; - -}; diff --git a/cpp/framework/protocol/Registry.tars b/cpp/framework/protocol/Registry.tars deleted file mode 100644 index d10a9eee7..000000000 --- a/cpp/framework/protocol/Registry.tars +++ /dev/null @@ -1,137 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "NodeDescriptor.tars" -#include "RegistryDescriptor.tars" - -module tars -{ - struct PatchResult - { - 0 require string sApplication; - 1 require string sServerName; - 2 require string sNodeName; - 3 require string sVersion; - 4 require string sUserName; - }; - - /** - * registryæŽ¥å£ - */ - interface Registry - { - /** - * nodeå¯åŠ¨çš„时候往registry注册一个session - * - * @param nodeName nodeå称 - * @param ni nodeè¯¦ç»†ä¿¡æ¯ - * @param li nodeæœºå™¨è´Ÿè½½ä¿¡æ¯ - * - * @return 注册是å¦æˆåŠŸ - */ - int registerNode(string nodeName, NodeInfo ni, LoadInfo li); - - - /** - * node上报心跳åŠæœºå™¨è´Ÿè½½ - * - * @param nodeName nodeå称 - * @param load nodeæœºå™¨è´Ÿè½½ä¿¡æ¯ - */ - int keepAlive(string nodeName, LoadInfo load); - - - /** - * 获å–在该node部署的server列表 - * - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * - * @return serverä¿¡æ¯åˆ—表 - */ - vector getServers(string app, string serverName, string nodeName); - - /** - * æ›´æ–°serverçŠ¶æ€ - * - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param state : server状æ€ä¿¡æ¯ - * - * @return 0-æˆåŠŸ others-失败 - */ - int updateServer(string app, string serverName, string nodeName, ServerStateInfo state); - - /** - * 批é‡æ›´æ–°serverçŠ¶æ€ - * - * @param state : server状æ€ä¿¡æ¯ - * - * @return 0-æˆåŠŸ others-失败 - */ - int updateServerBatch(vector vState); - - /** - * nodeåœæ­¢ï¼Œé‡Šæ”¾nodeçš„ä¼šè¯ - * - * @param nodeName nodeå称 - * - * @return 0-æˆåŠŸ others-失败 - */ - int destroy(string nodeName); - - /** - * 上报serverçš„tars库版本 - * @param app: 应用 - * @param serverName: server å - * @param nodeName : node id - * @param version: server基于的tarsç‰ˆæœ¬å· - * - * @return 0-æˆåŠŸ others-失败 - */ - int reportVersion(string app, string serverName, string nodeName, string version); - - /** - * 获å–node的模æ¿é…ç½® - * @param nodeName: nodeå称 - * @param out profileTemplate: 对应模æ¿å†…容 - * - * @return 0-æˆåŠŸ others-失败 - */ - int getNodeTemplate(string nodeName, out string profileTemplate); - - - /** - * node通过接å£èŽ·å–连接上主控的node ip - * @param sNodeIp: node çš„ip - * - * @return 0-æˆåŠŸ others-失败 - */ - int getClientIp(out string sClientIp); - - - /** - * å‘布æœåŠ¡å®Œæ¯•ä¹‹åŽç»Ÿä¸€UPDATEå‘布版本和å‘布人 - * - */ - int updatePatchResult(PatchResult result); - - }; - -}; - diff --git a/cpp/framework/protocol/RegistryDescriptor.tars b/cpp/framework/protocol/RegistryDescriptor.tars deleted file mode 100644 index 388370545..000000000 --- a/cpp/framework/protocol/RegistryDescriptor.tars +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "EndpointF.tars" - -module tars -{ - struct AdapterDescriptor - { - 0 require string adapterName; - 1 require string threadNum; - 2 require string endpoint; - 3 require int maxConnections; - 4 require string allowIp; - 5 require string servant; - 6 require int queuecap; - 7 require int queuetimeout; - 8 require int grid; //该字段已弃用,为平滑å‡çº§è€Œä¿ç•™å®šä¹‰ - 9 optional string protocol; - 10 optional string handlegroup; - 11 optional int shmkey = 0; //该字段用作共享内存key - 12 optional int shmcap = 0; //è¯¥å­—æ®µç”¨ä½œå…±äº«å†…å­˜å¤§å° - }; - - struct ServerDescriptor - { - 0 require string application; - 1 require string serverName; - 2 require string nodeName; - 3 require string basePath; - 4 require string exePath; - 5 require string settingState; - 6 require string presentState; - 7 require string profile; - 8 require string patchVersion; - 9 require string patchTime; - 10 require string patchUser; - 11 require map adapters; - 12 optional string serverType; - 13 optional string startScript; - 14 optional string stopScript; - 15 optional string monitorScript; - 16 optional int configCenterPort; - 17 optional int asyncThreadNum; - 18 optional string setId; - 19 optional string sJmx_port; //tarsjavaæœåŠ¡ä¸“用 - }; - - struct ObjectName - { - 0 require string ObjectId; - }; - - //在1.5.5版本中除去了(2 require vector vActiveEpGroupIdå’Œ3 require vector vInactiveEpGroupId) - struct ObjectItem - { - 0 require vector vActiveEndpoints; - 1 require vector vInactiveEndpoints; - }; - //主控idc分组信æ¯ç»“构,用æ¥ç¼“å­˜ä¿¡æ¯ - struct ServerGroupRule - { - 0 require vector< map > vGroupRuleInfo; - }; -}; diff --git a/cpp/framework/sql/db_tars.sql b/cpp/framework/sql/db_tars.sql deleted file mode 100644 index dd4862d77..000000000 --- a/cpp/framework/sql/db_tars.sql +++ /dev/null @@ -1,641 +0,0 @@ --- MySQL dump 10.13 Distrib 5.6.26, for Linux (x86_64) --- --- Host: localhost Database: db_tars --- ------------------------------------------------------ --- Server version 5.6.26-log - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - --- --- Table structure for table `t_adapter_conf` --- - -DROP TABLE IF EXISTS `t_adapter_conf`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_adapter_conf` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `application` varchar(50) DEFAULT '', - `server_name` varchar(128) DEFAULT '', - `node_name` varchar(50) DEFAULT '', - `adapter_name` varchar(100) DEFAULT '', - `registry_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `thread_num` int(11) DEFAULT '1', - `endpoint` varchar(128) DEFAULT '', - `max_connections` int(11) DEFAULT '1000', - `allow_ip` varchar(255) NOT NULL DEFAULT '', - `servant` varchar(128) DEFAULT '', - `queuecap` int(11) DEFAULT NULL, - `queuetimeout` int(11) DEFAULT NULL, - `posttime` datetime DEFAULT '0000-00-00 00:00:00', - `lastuser` varchar(30) DEFAULT NULL, - `protocol` varchar(64) DEFAULT 'tars', - `handlegroup` varchar(64) DEFAULT '', - PRIMARY KEY (`id`), - UNIQUE KEY `application` (`application`,`server_name`,`node_name`,`adapter_name`), - KEY `adapter_conf_endpoint_index` (`endpoint`), - KEY `index_regtime_1` (`registry_timestamp`) -) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_adapter_conf` --- - -LOCK TABLES `t_adapter_conf` WRITE; -/*!40000 ALTER TABLE `t_adapter_conf` DISABLE KEYS */; -INSERT INTO `t_adapter_conf` VALUES (1,'tars','tarspatch','192.168.2.131','tars.tarspatch.PatchObjAdapter','2015-11-28 16:16:55',10,'tcp -h 192.168.2.131 -t 60000 -p 10000',200000,'','tars.tarspatch.PatchObj',10000,60000,'2015-11-28 11:16:55','admin','tars',''),(2,'tars','tarsconfig','192.168.2.131','tars.tarsconfig.ConfigObjAdapter','2015-11-28 16:16:26',10,'tcp -h 192.168.2.131 -t 60000 -p 10001',200000,'','tars.tarsconfig.ConfigObj',10000,60000,'2015-11-28 11:16:26','admin','tars',''),(21,'tars','tarsnotify','192.168.2.131','tars.tarsnotify.NotifyObjAdapter','2015-11-29 02:48:13',5,'tcp -h 192.168.2.131 -t 60000 -p 10002',200000,'','tars.tarsnotify.NotifyObj',10000,60000,'2015-11-28 21:48:13','admin','tars',''); -/*!40000 ALTER TABLE `t_adapter_conf` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_ats_cases` --- - -DROP TABLE IF EXISTS `t_ats_cases`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_ats_cases` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `casename` varchar(20) DEFAULT NULL, - `retvalue` text, - `paramvalue` text, - `interfaceid` int(11) DEFAULT NULL, - `posttime` datetime DEFAULT NULL, - `lastuser` varchar(30) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_ats_cases` --- - -LOCK TABLES `t_ats_cases` WRITE; -/*!40000 ALTER TABLE `t_ats_cases` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_ats_cases` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_ats_interfaces` --- - -DROP TABLE IF EXISTS `t_ats_interfaces`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_ats_interfaces` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `objname` varchar(150) DEFAULT NULL, - `funcname` varchar(150) DEFAULT NULL, - `retype` text, - `paramtype` text, - `outparamtype` text, - `interfaceid` int(11) DEFAULT NULL, - `postime` datetime DEFAULT NULL, - `lastuser` varchar(30) DEFAULT NULL, - `request_charset` varchar(16) NOT NULL, - `response_charset` varchar(16) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `objname` (`objname`,`funcname`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_ats_interfaces` --- - -LOCK TABLES `t_ats_interfaces` WRITE; -/*!40000 ALTER TABLE `t_ats_interfaces` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_ats_interfaces` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_config_files` --- - -DROP TABLE IF EXISTS `t_config_files`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_config_files` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `server_name` varchar(128) DEFAULT '', - `set_name` varchar(16) NOT NULL DEFAULT '', - `set_area` varchar(16) NOT NULL DEFAULT '', - `set_group` varchar(16) NOT NULL DEFAULT '', - `host` varchar(20) NOT NULL DEFAULT '', - `filename` varchar(128) DEFAULT NULL, - `config` longtext, - `posttime` datetime DEFAULT NULL, - `lastuser` varchar(50) DEFAULT NULL, - `level` int(11) DEFAULT '2', - `config_flag` int(10) NOT NULL DEFAULT '0', - PRIMARY KEY (`id`), - UNIQUE KEY `application` (`server_name`,`filename`,`host`,`level`,`set_name`,`set_area`,`set_group`) -) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_config_files` --- - -LOCK TABLES `t_config_files` WRITE; -/*!40000 ALTER TABLE `t_config_files` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_config_files` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_config_history_files` --- - -DROP TABLE IF EXISTS `t_config_history_files`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_config_history_files` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `configid` int(11) DEFAULT NULL, - `reason` varchar(128) DEFAULT '', - `reason_select` varchar(20) NOT NULL DEFAULT '', - `content` longtext, - `posttime` datetime DEFAULT NULL, - `lastuser` varchar(50) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_config_history_files` --- - -LOCK TABLES `t_config_history_files` WRITE; -/*!40000 ALTER TABLE `t_config_history_files` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_config_history_files` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_config_references` --- - -DROP TABLE IF EXISTS `t_config_references`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_config_references` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `config_id` int(11) DEFAULT NULL, - `reference_id` int(11) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `config_id` (`config_id`,`reference_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_config_references` --- - -LOCK TABLES `t_config_references` WRITE; -/*!40000 ALTER TABLE `t_config_references` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_config_references` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_group_priority` --- - -DROP TABLE IF EXISTS `t_group_priority`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_group_priority` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(128) DEFAULT '', - `group_list` text, - `list_order` int(11) DEFAULT '0', - `station` varchar(128) NOT NULL DEFAULT '', - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_group_priority` --- - -LOCK TABLES `t_group_priority` WRITE; -/*!40000 ALTER TABLE `t_group_priority` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_group_priority` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_machine_tars_info` --- - -DROP TABLE IF EXISTS `t_machine_tars_info`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_machine_tars_info` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `application` varchar(100) NOT NULL DEFAULT '', - `server_name` varchar(100) NOT NULL DEFAULT '', - `app_server_name` varchar(50) NOT NULL DEFAULT '', - `node_name` varchar(50) NOT NULL DEFAULT '', - `location` varchar(255) NOT NULL DEFAULT '', - `machine_type` varchar(50) NOT NULL DEFAULT '', - `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `update_person` varchar(64) NOT NULL DEFAULT '', - PRIMARY KEY (`application`,`server_name`,`node_name`), - UNIQUE KEY `id` (`id`), - KEY `tmachine_i_2` (`node_name`,`server_name`) -) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_machine_tars_info` --- - -LOCK TABLES `t_machine_tars_info` WRITE; -/*!40000 ALTER TABLE `t_machine_tars_info` DISABLE KEYS */; -/*INSERT INTO `t_machine_tars_info` VALUES (2,'tars','tarsconfig','tars.tarsconfig','192.168.2.131','','','2015-08-07 16:04:03','admin'),(3,'tars','tarsnotify','tars.tarsnotify','192.168.2.131','','','2015-08-09 02:13:54','admin'),(1,'tars','tarspatch','tars.tarspatch','192.168.2.131','','','2015-08-07 15:57:50','admin');*/ -/*!40000 ALTER TABLE `t_machine_tars_info` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_node_info` --- - -DROP TABLE IF EXISTS `t_node_info`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_node_info` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `node_name` varchar(128) DEFAULT '', - `node_obj` varchar(128) DEFAULT '', - `endpoint_ip` varchar(16) DEFAULT '', - `endpoint_port` int(11) DEFAULT '0', - `data_dir` varchar(128) DEFAULT '', - `load_avg1` float DEFAULT '0', - `load_avg5` float DEFAULT '0', - `load_avg15` float DEFAULT '0', - `last_reg_time` datetime DEFAULT '1970-01-01 00:08:00', - `last_heartbeat` datetime DEFAULT '1970-01-01 00:08:00', - `setting_state` enum('active','inactive') DEFAULT 'inactive', - `present_state` enum('active','inactive') DEFAULT 'inactive', - `tars_version` varchar(128) NOT NULL DEFAULT '', - `template_name` varchar(128) NOT NULL DEFAULT '', - `modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `group_id` int(11) DEFAULT '-1', - PRIMARY KEY (`id`), - UNIQUE KEY `node_name` (`node_name`), - KEY `indx_node_info_1` (`last_heartbeat`) -) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_node_info` --- - -LOCK TABLES `t_node_info` WRITE; -/*!40000 ALTER TABLE `t_node_info` DISABLE KEYS */; -INSERT INTO `t_node_info` VALUES (67,'192.168.2.131','tars.tarsnode.NodeObj@tcp -h 192.168.2.131 -p 19385 -t 60000','192.168.2.131',19385,'/usr/local/app/tars/tarsnode/data',0,0.12,0.39,'2015-11-29 13:36:07','2015-11-29 22:15:46','active','active','1.0.1_B001','','2015-11-29 14:15:46',-1); -/*!40000 ALTER TABLE `t_node_info` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_profile_template` --- - -DROP TABLE IF EXISTS `t_profile_template`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_profile_template` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `template_name` varchar(128) DEFAULT '', - `parents_name` varchar(128) DEFAULT '', - `profile` text NOT NULL, - `posttime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', - `lastuser` varchar(30) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `template_name` (`template_name`) -) ENGINE=InnoDB AUTO_INCREMENT=366 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_profile_template` --- - -LOCK TABLES `t_profile_template` WRITE; -/*!40000 ALTER TABLE `t_profile_template` DISABLE KEYS */; -INSERT INTO `t_profile_template` VALUES (353,'tars.default','tars.default','\n \n #是å¦å¯ç”¨SET分组\n enableset=${enableset}\n #SET分组的全å.(mtt.s.1)\n setdivision=${setdivision}\n \n #地å€\n locator =${locator}\n #åŒæ­¥è°ƒç”¨è¶…时时间,缺çœ3s(毫秒)\n sync-invoke-timeout = 3000\n #异步超时时间,缺çœ5s(毫秒)\n async-invoke-timeout =5000\n #é‡æ–°èŽ·å–æœåŠ¡åˆ—表时间间隔(毫秒)\n refresh-endpoint-interval = 60000\n #模å—间调用æœåŠ¡[å¯é€‰]\n stat = tars.tarsstat.StatObj\n #属性上报æœåŠ¡[å¯é€‰]\n property = tars.tarsproperty.PropertyObj\n #上报间隔时间,默认60s(毫秒)\n report-interval = 60000\n #stat采样比1:n 例如sample-rate为1000æ—¶ 采样比为åƒåˆ†ä¹‹ä¸€\n sample-rate = 100000\n #1分钟内stat最大采样æ¡æ•°\n max-sample-count = 50\n\n #网络异步回调线程个数\n asyncthread = ${asyncthread}\n #模å—å称\n modulename = ${modulename}\n \n \n #定义所有绑定的IP\n \n #应用å称\n app = ${app}\n #æœåŠ¡å称\n server = ${server}\n #本地ip\n localip = ${localip}\n\n #本地管ç†å¥—接字[å¯é€‰]\n local = ${local}\n #æœåŠ¡çš„æ•°æ®ç›®å½•,å¯æ‰§è¡Œæ–‡ä»¶,é…置文件等\n basepath = ${basepath}\n #\n datapath = ${datapath}\n #日志路径\n logpath = ${logpath}\n #日志大å°\n logsize = 10M\n #日志数é‡\n # lognum = 10\n #é…置中心的地å€[å¯é€‰]\n config = tars.tarsconfig.ConfigObj\n #ä¿¡æ¯ä¸­å¿ƒçš„地å€[å¯é€‰]\n notify = tars.tarsnotify.NotifyObj\n #远程LogServer[å¯é€‰]\n log = tars.tarslog.LogObj\n #关闭æœåŠ¡æ—¶ç­‰å¾…时间\n deactivating-timeout = 3000\n #滚动日志等级默认值\n logLevel=DEBUG\n \n \n ','2016-09-22 17:36:01','admin'),(354,'tars.tarspatch','tars.default','\n \n \n log = tars.tarslog.LogObj\n logLevel = DEBUG\n \n \n\n directory=/usr/local/app/patchs/tars\n uploadDirectory=/usr/local/app/patchs/tars.upload\n size=1M\n','2015-08-07 22:06:46','admin'),(355,'tars.tarsconfig','tars.default','\n \n \n log = tars.tarslog.LogObj\n logLevel = DEBUG\n \n \n \n charset=utf8\n dbhost=db.tars.com\n dbname=db_tars\n dbpass=tars2015\n dbport=3306\n dbuser=tars\n\n \n','2015-08-07 22:05:24','admin'),(356,'tars.tarsnotify','tars.default','\n \n \n log = tars.tarslog.LogObj\n logLevel = DEBUG\n \n \n\n \n min_block=50 \n max_block=200 \n factor=1.5 \n file_path=./notify \n file_size=50000000 \n max_page_num=30 \n max_page_size=20 \n \n \n charset=utf8\n dbhost=db.tars.com\n dbname=db_tars\n dbpass=tars2015\n dbport=3306\n dbuser=tars\n dbname=db_tars\n\n \n\n sql=CREATE TABLE `${TABLE}` ( `id` int(11) NOT NULL AUTO_INCREMENT, `application` varchar(128) DEFAULT \'\', `server_name` varchar(128) DEFAULT NULL, `container_name` varchar(128) DEFAULT \'\' , `node_name` varchar(128) NOT NULL DEFAULT \'\', `set_name` varchar(16) DEFAULT NULL, `set_area` varchar(16) DEFAULT NULL, `set_group` varchar(16) DEFAULT NULL, `server_id` varchar(100) DEFAULT NULL, `thread_id` varchar(20) DEFAULT NULL, `command` varchar(50) DEFAULT NULL, `result` text, `notifytime` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name` (`server_name`), KEY `servernoticetime_i_1` (`notifytime`), KEY `indx_1_server_id` (`server_id`), KEY `query_index` (`application`,`server_name`,`node_name`,`set_name`,`set_area`,`set_group`) ) ENGINE\\=InnoDB DEFAULT CHARSET\\=utf8\n','2016-09-26 15:21:52','admin'),(359,'tars.tarsstat','tars.default','\n sql= CREATE TABLE `${TABLE}`( `stattime` timestamp NOT NULL default CURRENT_TIMESTAMP,`f_date` date NOT NULL default \'1970-01-01\', `f_tflag` varchar(8) NOT NULL default \'\',`source_id` varchar(15) NOT NULL default \'\',`master_name` varchar(128) NOT NULL default \'\',`slave_name` varchar(128) NOT NULL default \'\',`interface_name` varchar(128) NOT NULL default \'\',`tars_version` varchar(16) NOT NULL default \'\',`master_ip` varchar(15) NOT NULL default \'\',`slave_ip` varchar(21) NOT NULL default \'\',`slave_port` int(10) NOT NULL default 0,`return_value` int(11) NOT NULL default 0,`succ_count` int(10) unsigned default NULL,`timeout_count` int(10) unsigned default NULL,`exce_count` int(10) unsigned default NULL,`interv_count` varchar(128) default NULL,`total_time` bigint(20) unsigned default NULL,`ave_time` int(10) unsigned default NULL,`maxrsp_time` int(10) unsigned default NULL,`minrsp_time` int(10) unsigned default NULL,PRIMARY KEY (`source_id`,`f_date`,`f_tflag`,`master_name`,`slave_name`,`interface_name`,`master_ip`,`slave_ip`,`slave_port`,`return_value`,`tars_version`),KEY `IDX_TIME` (`stattime`),KEY `IDC_MASTER` (`master_name`),KEY `IDX_INTERFACENAME` (`interface_name`),KEY `IDX_FLAGSLAVE` (`f_tflag`,`slave_name`), KEY `IDX_SLAVEIP` (`slave_ip`),KEY `IDX_SLAVE` (`slave_name`),KEY `IDX_RETVALUE` (`return_value`),KEY `IDX_MASTER_IP` (`master_ip`),KEY `IDX_F_DATE` (`f_date`)) ENGINE\\=InnoDB DEFAULT CHARSET\\=utf8\n enWeighted=1\n \n \n tars.tarsstat;1.1.1.1\n \n \n masterfile=hashmap_master.txt\n slavefile=hashmap_slave.txt\n insertInterval=5\n enableStatCount=0\n size=8M\n countsize=1M\n \n \n interval=5\n insertDbThreadNum=4\n \n \n \n dbhost=db.tars.com\n dbname=tars_stat\n tbname=tars_stat_\n dbuser=tars\n dbpass=tars2015\n dbport=3306\n charset=utf8\n \n \n \n','2016-09-26 17:25:45','admin'),(365,'tars.tarsjava.default','tars.default','\n \n enableset=${enableset}\n setdivision=${setdivision}\n \n locator=${locator}\n sync-invoke-timeout=20000\n async-invoke-timeout=20000\n refresh-endpoint-interval=60000\n stat=tars.tarsstat.StatObj\n property=tars.tarsproperty.PropertyObj\n report-interval=60000\n modulename=${modulename}\n sample-rate=100000\n max-sample-count=50\n \n \n app=${app}\n server=${server}\n localip=${localip}\n local=${local}\n basepath=${basepath}\n datapath=${datapath}\n logpath=${logpath}\n loglevel=DEBUG\n logsize=15M\n log=tars.tarslog.LogObj\n config=tars.tarsconfig.ConfigObj\n notify=tars.tarsnotify.NotifyObj\n mainclass=com.qq.tars.server.startup.Main\n classpath=${basepath}/conf:${basepath}/WEB-INF/classes:${basepath}/WEB-INF/lib\n jvmparams=-Dcom.sun.management.jmxremote.ssl\\=false -Dcom.sun.management.jmxremote.authenticate\\=false -Xms2000m -Xmx2000m -Xmn1000m -Xss1000k -XX:PermSize\\=128M -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction\\=60 -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+CMSParallelRemarkEnabled -XX:+CMSScavengeBeforeRemark -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction\\=0 -verbosegc -XX:+PrintGCDetails -XX:ErrorFile\\=${logpath}/${app}/${server}/jvm_error.log\n sessiontimeout=120000\n sessioncheckinterval=60000\n tcpnodelay=true\n udpbuffersize=8192\n charsetname=UTF-8\n backupfiles=conf\n \n \n','2016-10-13 17:22:07','admin'),(358,'tars.tarsnode','tars.default','\n \n enableset=n \n setdivision=NULL\n \n modulename=tars.tarsnode\n locator=${locator}\n #缺çœ3s(毫秒)\n sync-invoke-timeout = 6000\n asyncthread=3\n \n \n app=tars\n server=tarsnode\n localip=${localip}\n local = tcp -h 127.0.0.1 -p 19385 -t 10000\n basepath=/usr/local/app/tars/tarsnode/data\n datapath=/usr/local/app/tars/tarsnode/data\n logpath= /usr/local/app/tars/app_log\n logLevel=DEBUG\n #é…置绑定端å£\n \n #监å¬IP地å€\n endpoint = tcp -h ${localip} -p 19385 -t 60000\n #å…许的IP地å€\n allow =\n #最大连接数\n maxconns = 1024\n #当å‰çº¿ç¨‹ä¸ªæ•°\n threads = 5\n #æµé‡é™åˆ¶\n queuecap = 10000\n #队列超时时间\n queuetimeout= 4000\n #处ç†å¯¹è±¡\n servant = tars.tarsnode.NodeObj\n \n\n \n #监å¬IP地å€\n endpoint = tcp -h ${localip} -p 19386 -t 60000\n #å…许的IP地å€\n allow =\n #最大连接数\n maxconns = 1024\n #当å‰çº¿ç¨‹ä¸ªæ•°\n threads = 5\n #æµé‡é™åˆ¶\n queuecap = 10000\n #队列超时时间\n queuetimeout= 4000\n #处ç†å¯¹è±¡\n servant = tars.tarsnode.ServerObj\n \n \n \n\n \n registryObj = ${registryObj}\n \n #业务心跳超时时间(s) \n heartTimeout = 60\n \n #监控server状æ€é—´éš”时间(s) \n monitorInterval = 2 \n \n #跟主控/本地cacheåŒæ­¥æœåŠ¡çŠ¶æ€é—´éš”时间(s) \n synStatInterval = 300\n \n \n \n file =serversCache.dat\n minBlock =500\n maxBlock =500\n factor =1\n size =10M\n \n \n','2015-08-07 22:04:39','admin'),(360,'tars.tarsproperty','tars.default','\n sql=CREATE TABLE `${TABLE}` (`stattime` timestamp NOT NULL default CURRENT_TIMESTAMP,`f_date` date NOT NULL default \'1970-01-01\', `f_tflag` varchar(8) NOT NULL default \'\',`master_name` varchar(128) NOT NULL default \'\',`master_ip` varchar(16) default NULL,`property_name` varchar(100) default NULL,`set_name` varchar(15) NOT NULL default \'\',`set_area` varchar(15) NOT NULL default \'\',`set_id` varchar(15) NOT NULL default \'\',`policy` varchar(20) default NULL,`value` varchar(255) default NULL, KEY (`f_date`,`f_tflag`,`master_name`,`master_ip`,`property_name`,`policy`),KEY `IDX_MASTER_NAME` (`master_name`),KEY `IDX_MASTER_IP` (`master_ip`),KEY `IDX_TIME` (`stattime`)) ENGINE=Innodb\n\n \n charset\n dbhost=db.tars.com\n dbname=tars\n dbport=3306\n dbuser=tars\n dbpass=tars2015\n \n \n \n dbhost=db.tars.com\n dbname=tars_property\n tbname=tars_property_\n dbuser=tars\n dbpass=tars2015\n dbport=3306\n charset=utf8\n \n \n dbhost=db.tars.com\n dbname=tars_property\n tbname=tars_property_\n dbuser=tars\n dbpass=tars2015\n dbport=3306\n charset=utf8\n \n \n \n factor=1.5\n file=hashmap.txt\n insertInterval=5\n maxBlock=200\n minBlock=100\n size=10M\n \n \n Interval=10\n sql=insert ignore into t_master_property select master_name, property_name, policy from ${TABLE} group by master_name, property_name, policy;\n \n','2015-08-25 12:15:52','admin'),(362,'tars.tarslog','tars.default','\n \n \n logLevel=ERROR\n \n \n \n logpath=/usr/local/app/tars/remote_app_log\n logthread=10 \n \n hour=xx\n \n \n','2016-10-13 17:31:36','admin'),(366,'tars.tarsquerystat','tars.default','\n\n\n dbhost=db.tars.com\n dbname=tars_stat\n tbname=tars_stat_\n dbuser=tars\n dbpass=tars2015\n dbport=3306\n charset=utf8\n \n\n','2017-01-04 17:13:40',NULL),(367,'tars.tarsqueryproperty','tars.default','\n\n\n dbhost=db.tars.com\n dbname=tars_property\n tbname=tars_property_\n dbuser=tars\n dbpass=tars2015\n dbport=3306\n charset=utf8\n \n\n','2017-01-04 17:14:01',NULL),('368', 'tars.tarsphp.default', 'tars.default', '\r\n \r\n enableset=${enableset}\r\n setdivision=${setdivision}\r\n \r\n locator=${locator}\r\n sync-invoke-timeout=20000\r\n async-invoke-timeout=20000\r\n refresh-endpoint-interval=60000\r\n stat=tars.tarsstat.StatObj\r\n property=tars.tarsproperty.PropertyObj\r\n report-interval=60000\r\n modulename=${modulename}\r\n sample-rate=100000\r\n max-sample-count=50\r\n \r\n \r\n app=${app}\r\n server=${server}\r\n localip=${localip}\r\n local=${local}\r\n basepath=${basepath}\r\n datapath=${datapath}\r\n logpath=${logpath}\r\n loglevel=DEBUG\r\n logsize=15M\r\n log=tars.tarslog.LogObj\r\n config=tars.tarsconfig.ConfigObj\r\n notify=tars.tarsnotify.NotifyObj\r\n logLevel=DEBUG\r\n php=/usr/bin/php\r\n buffer_output_size=12582912\r\n open_tcp_nodelay=1\r\n open_eof_check=0\r\n open_eof_split=0\r\n task_worker_num=1\r\n dispatch_mode=2\r\n daemonize=1\r\n \r\n \r\n\r\n', '2018-05-18 10:41:36', 'dpp'),(369,'tars.springboot','tars.tarsjava.default','\n\n<\n packageFormat=jar\n mainclass=-jar ${basepath}/${app}.${server}.jar\n \n\n','2017-01-04 17:14:01',NULL); -/*!40000 ALTER TABLE `t_profile_template` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_registry_info` --- - -DROP TABLE IF EXISTS `t_registry_info`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_registry_info` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `locator_id` varchar(128) NOT NULL DEFAULT '', - `servant` varchar(128) NOT NULL DEFAULT '', - `endpoint` varchar(128) NOT NULL DEFAULT '', - `last_heartbeat` datetime DEFAULT '1970-01-01 00:08:00', - `present_state` enum('active','inactive') DEFAULT 'inactive', - `tars_version` varchar(128) NOT NULL DEFAULT '', - `modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `enable_group` char(1) DEFAULT 'N', - PRIMARY KEY (`id`), - UNIQUE KEY `locator_id` (`locator_id`,`servant`) -) ENGINE=InnoDB AUTO_INCREMENT=456487 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_registry_info` --- - -LOCK TABLES `t_registry_info` WRITE; -/*!40000 ALTER TABLE `t_registry_info` DISABLE KEYS */; -INSERT INTO `t_registry_info` VALUES (456484,'192.168.2.131:17890','tars.tarsregistry.QueryObj','tcp -h 192.168.2.131 -p 17890 -t 10000','2015-11-29 22:16:16','active','1.0.1','2015-11-29 14:16:16','N'),(456485,'192.168.2.131:17890','tars.tarsregistry.RegistryObj','tcp -h 192.168.2.131 -p 17891 -t 30000','2015-11-29 22:16:16','active','1.0.1','2015-11-29 14:16:16','N'),(456486,'192.168.2.131:12000','tars.tarsAdminRegistry.AdminRegObj','tcp -h 192.168.2.131 -p 12000 -t 60000','2015-11-29 22:16:21','active','4.1.0.0_B002','2015-11-29 14:16:21','N'); -/*!40000 ALTER TABLE `t_registry_info` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_server_conf` --- - -DROP TABLE IF EXISTS `t_server_conf`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_server_conf` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `application` varchar(128) DEFAULT '', - `server_name` varchar(128) DEFAULT '', - `node_group` varchar(50) NOT NULL DEFAULT '', - `node_name` varchar(50) NOT NULL DEFAULT '', - `registry_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `base_path` varchar(128) DEFAULT '', - `exe_path` varchar(128) NOT NULL DEFAULT '', - `template_name` varchar(128) NOT NULL DEFAULT '', - `bak_flag` int(11) NOT NULL DEFAULT '0', - `setting_state` enum('active','inactive') NOT NULL DEFAULT 'inactive', - `present_state` enum('active','inactive','activating','deactivating','destroyed') NOT NULL DEFAULT 'inactive', - `process_id` int(11) NOT NULL DEFAULT '0', - `patch_version` varchar(128) NOT NULL DEFAULT '', - `patch_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', - `patch_user` varchar(128) NOT NULL DEFAULT '', - `tars_version` varchar(128) NOT NULL DEFAULT '', - `posttime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', - `lastuser` varchar(30) DEFAULT NULL, - `server_type` enum('tars_cpp','not_tars','tars_java', 'tars_nodejs','tars_php') NOT NULL DEFAULT 'tars_cpp', - `start_script_path` varchar(128) DEFAULT NULL, - `stop_script_path` varchar(128) DEFAULT NULL, - `monitor_script_path` varchar(128) DEFAULT NULL, - `enable_group` char(1) DEFAULT 'N', - `enable_set` char(1) NOT NULL DEFAULT 'N', - `set_name` varchar(16) DEFAULT NULL, - `set_area` varchar(16) DEFAULT NULL, - `set_group` varchar(64) DEFAULT NULL, - `ip_group_name` varchar(64) DEFAULT NULL, - `profile` text, - `config_center_port` int(11) NOT NULL DEFAULT '0', - `async_thread_num` int(11) DEFAULT '3', - `server_important_type` enum('0','1','2','3','4','5') DEFAULT '0', - `remote_log_reserve_time` varchar(32) NOT NULL DEFAULT '65', - `remote_log_compress_time` varchar(32) NOT NULL DEFAULT '2', - `remote_log_type` int(1) NOT NULL DEFAULT '0', - `grid_flag` varchar(16) NOT NULL DEFAULT 'NORMAL', - PRIMARY KEY (`id`), - UNIQUE KEY `application` (`application`,`server_name`,`node_name`), - KEY `node_name` (`node_name`), - KEY `index_i_3` (`setting_state`,`server_type`,`application`,`server_name`,`node_name`), - KEY `index_regtime` (`registry_timestamp`) -) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_server_conf` --- - -LOCK TABLES `t_server_conf` WRITE; -/*!40000 ALTER TABLE `t_server_conf` DISABLE KEYS */; -INSERT INTO `t_server_conf` VALUES (1,'tars','tarspatch','','192.168.2.131','2015-11-29 11:08:09','','/usr/local/app/tars/tarspatch/bin/tarspatch','tars.tarspatch',0,'active','active',9855,'','0000-00-00 00:00:00','','1.0.1','2015-08-07 23:57:50','admin','tars_cpp','','','','N','N','','','','','',0,3,'0','65','2',0,'NORMAL'),(2,'tars','tarsconfig','','192.168.2.131','2015-11-29 11:13:02','','/usr/local/app/tars/tarsconfig/bin/tarsconfig','tars.tarsconfig',0,'active','active',9815,'','0000-00-00 00:00:00','','1.0.1','2015-08-08 10:49:37','admin','tars_cpp','','','','N','N','','','','','',0,3,'0','65','2',0,'NORMAL'),(20,'tars','tarsnotify','','192.168.2.131','2015-11-29 11:13:02','','','tars.tarsnotify',0,'active','active',9838,'47','2015-11-28 22:26:40','admin','1.0.1','2015-11-28 21:48:13','admin','tars_cpp','','','','N','N','','','','','',0,3,'0','65','2',0,'NORMAL'); -/*!40000 ALTER TABLE `t_server_conf` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_server_group_relation` --- - -DROP TABLE IF EXISTS `t_server_group_relation`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_server_group_relation` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `application` varchar(90) NOT NULL DEFAULT '', - `server_group` varchar(50) DEFAULT '', - `server_name` varchar(50) DEFAULT '', - `create_time` datetime DEFAULT NULL, - `creator` varchar(30) DEFAULT '', - PRIMARY KEY (`id`), - KEY `f_unique` (`application`,`server_group`,`server_name`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_server_group_relation` --- - -LOCK TABLES `t_server_group_relation` WRITE; -/*!40000 ALTER TABLE `t_server_group_relation` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_server_group_relation` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_server_group_rule` --- - -DROP TABLE IF EXISTS `t_server_group_rule`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_server_group_rule` ( - `group_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `ip_order` enum('allow_denny','denny_allow') NOT NULL DEFAULT 'denny_allow', - `allow_ip_rule` text, - `denny_ip_rule` text, - `lastuser` varchar(50) DEFAULT NULL, - `modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `group_name` varchar(128) DEFAULT '', - `group_name_cn` varchar(128) DEFAULT '', - PRIMARY KEY (`group_id`), - UNIQUE KEY `group_name_index` (`group_name`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_server_group_rule` --- - -LOCK TABLES `t_server_group_rule` WRITE; -/*!40000 ALTER TABLE `t_server_group_rule` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_server_group_rule` ENABLE KEYS */; -UNLOCK TABLES; - - --- --- Table structure for table `t_server_patchs` --- - -DROP TABLE IF EXISTS `t_server_patchs`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_server_patchs` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `server` varchar(50) DEFAULT NULL, - `version` varchar(1000) DEFAULT '', - `tgz` text, - `update_text` varchar(255) DEFAULT NULL, - `reason_select` varchar(255) DEFAULT NULL, - `document_complate` varchar(30) DEFAULT NULL, - `is_server_group` tinyint(2) NOT NULL DEFAULT '0', - `publish` tinyint(3) DEFAULT NULL, - `publish_time` datetime DEFAULT NULL, - `publish_user` varchar(30) DEFAULT NULL, - `upload_time` datetime DEFAULT NULL, - `upload_user` varchar(30) DEFAULT NULL, - `posttime` datetime DEFAULT NULL, - `lastuser` varchar(30) DEFAULT NULL, - `is_release_version` enum('true','false') DEFAULT 'true', - `package_type` tinyint(4) DEFAULT '0', - `group_id` varchar(64) NOT NULL DEFAULT '', - `default_version` tinyint(4) DEFAULT '0', - `md5` varchar(40) DEFAULT NULL, - `svn_version` varchar(50) DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `server_patchs_server_index` (`server`), - KEY `index_patchs_i1` (`server`), - KEY `index_i_2` (`tgz`(50)) -) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_server_patchs` --- - -LOCK TABLES `t_server_patchs` WRITE; -/*!40000 ALTER TABLE `t_server_patchs` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_server_patchs` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_task` --- - -DROP TABLE IF EXISTS `t_task`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_task` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `task_no` varchar(40) DEFAULT NULL, - `serial` tinyint(1) DEFAULT NULL, - `user_name` varchar(20) DEFAULT NULL, - `create_time` timestamp NULL DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `f_task` (`task_no`) -) ENGINE=InnoDB AUTO_INCREMENT=139 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_task` --- - -LOCK TABLES `t_task` WRITE; -/*!40000 ALTER TABLE `t_task` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_task` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_task_item` --- - -DROP TABLE IF EXISTS `t_task_item`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_task_item` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `task_no` varchar(40) DEFAULT NULL, - `item_no` varchar(40) DEFAULT NULL, - `application` varchar(30) DEFAULT NULL, - `server_name` varchar(50) DEFAULT NULL, - `node_name` varchar(20) DEFAULT NULL, - `command` varchar(20) DEFAULT NULL, - `parameters` text, - `start_time` datetime DEFAULT NULL, - `end_time` datetime DEFAULT NULL, - `status` int(11) DEFAULT NULL, - `set_name` varchar(20) DEFAULT NULL, - `log` text, - PRIMARY KEY (`id`), - UNIQUE KEY `f_uniq` (`item_no`,`task_no`), - KEY `f_task_no` (`task_no`), - KEY `f_index` (`application`,`server_name`,`command`) -) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_task_item` --- - -LOCK TABLES `t_task_item` WRITE; -/*!40000 ALTER TABLE `t_task_item` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_task_item` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `t_web_release_conf` --- - -DROP TABLE IF EXISTS `t_web_release_conf`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `t_web_release_conf` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `server` varchar(100) NOT NULL DEFAULT '', - `path` varchar(200) NOT NULL DEFAULT '', - `server_dir` varchar(200) NOT NULL DEFAULT '', - `is_server_group` tinyint(2) NOT NULL DEFAULT '0', - `enable_batch` tinyint(2) NOT NULL DEFAULT '0', - `user` varchar(200) NOT NULL DEFAULT '*', - `posttime` datetime DEFAULT NULL, - `lastuser` varchar(60) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `server` (`server`,`is_server_group`), - KEY `web_release_conf_server_index` (`server`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `t_web_release_conf` --- - -LOCK TABLES `t_web_release_conf` WRITE; -/*!40000 ALTER TABLE `t_web_release_conf` DISABLE KEYS */; -/*!40000 ALTER TABLE `t_web_release_conf` ENABLE KEYS */; -UNLOCK TABLES; -/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; - -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; - --- Dump completed on 2016-11-24 14:28:02 diff --git a/cpp/framework/sql/dump-sql.sh b/cpp/framework/sql/dump-sql.sh deleted file mode 100644 index 7978fe121..000000000 --- a/cpp/framework/sql/dump-sql.sh +++ /dev/null @@ -1,2 +0,0 @@ -mysqldump -uroot -proot@appinside db_tars > db_tars.sql - diff --git a/cpp/framework/sql/exec-sql.sh b/cpp/framework/sql/exec-sql.sh deleted file mode 100644 index 543edddc3..000000000 --- a/cpp/framework/sql/exec-sql.sh +++ /dev/null @@ -1,6 +0,0 @@ -mysql -uroot -proot@appinside -e "create database db_tars" -mysql -uroot -proot@appinside -e "create database tars_stat" -mysql -uroot -proot@appinside -e "create database tars_property" - -mysql -uroot -proot@appinside db_tars < db_tars.sql - diff --git a/cpp/servant/CMakeLists.txt b/cpp/servant/CMakeLists.txt deleted file mode 100644 index 3ed2447d8..000000000 --- a/cpp/servant/CMakeLists.txt +++ /dev/null @@ -1,66 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -project(servant) - -include_directories(${util_SOURCE_DIR}/include) - -link_libraries(pthread) - -#调用tars2cpp, 生æˆtars对应的文件 -macro(complice_tars OUT_DEPENDS_LIST HEADER) - set(DEPENDS_LIST) - - FILE(GLOB SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.tars") - - set(CLEAN_LIST) - - #设置tars生æˆåŽ, cpp文件因为h文件的include路径 - set(INCLUDE) - if (NOT ${HEADER} STREQUAL "") - set(INCLUDE "--header=${HEADER}") - endif () - - foreach (FILE ${SRC_LIST}) - - get_filename_component(NAME_WE ${FILE} NAME_WE) - - # 生æˆtars文件 - set(TARS_IN ${FILE}) - set(TARS_H ${NAME_WE}.h) - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS tars2cpp - COMMAND ${TARS2CPP} --with-tars ${INCLUDE} ${TARS_IN} - COMMENT "${TARS2CPP} --with-tars ${INCLUDE} ${TARS_IN}") - - list(APPEND DEPENDS_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) - - #设置需è¦æ¸…除的文件 - get_filename_component(PATH ${FILE} PATH) - list(APPEND CLEAN_LIST ${PATH}/${NAME_WE}.h) - if (EXISTS ${PATH}/${NAME_WE}.cpp) - list(APPEND CLEAN_LIST ${PATH}/${NAME_WE}.cpp) - endif (EXISTS ${PATH}/${NAME_WE}.cpp) - - endforeach (FILE) - - set(OUT_DEPENDS_LIST ${DEPENDS_LIST}) - - set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") - -endmacro() - -add_subdirectory(protocol) -add_subdirectory(tup) -add_subdirectory(libservant) - - -install(DIRECTORY servant DESTINATION include) -install(DIRECTORY tup DESTINATION include) -install(DIRECTORY jmem DESTINATION include) -install(DIRECTORY promise DESTINATION include) -install(DIRECTORY script DESTINATION .) -install(DIRECTORY makefile DESTINATION .) - diff --git a/cpp/servant/README.md b/cpp/servant/README.md deleted file mode 100644 index 9ed60527e..000000000 --- a/cpp/servant/README.md +++ /dev/null @@ -1,11 +0,0 @@ -该工程是Tars框架C++语言的rpc实现的æºä»£ç  - -目录å称 |功能 -------------------|---------------- -wup |rpc框架的统一的通信åè®®(tars/wupåè®®)å’Œå议的编解ç å®žçŽ°æ–¹å¼ -protocol |rpc框架定义的底层通信的返回ç å’Œä¸Žæ¡†æž¶åŸºç¡€æœåŠ¡è¿›è¡Œäº¤äº’的通信接å£æ–‡ä»¶ -servant/libservant |rpc框架的æºç å®žçŽ° -jmem |基于tarså议的内存数æ®ç»“构组件的æºç å®žçŽ° -promise |基于promise异步编程的æºç å®žçŽ° -makefile |使用TARS框架C++语言的编译æºä»£ç çš„makefile实现 -script |生æˆTARSæœåŠ¡æ¨¡ç‰ˆä»£ç çš„脚本工具 diff --git a/cpp/servant/jmem/jmem_hashmap.h b/cpp/servant/jmem/jmem_hashmap.h deleted file mode 100644 index cf0c3e865..000000000 --- a/cpp/servant/jmem/jmem_hashmap.h +++ /dev/null @@ -1,2339 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _JMEM_HASHMAP_H -#define _JMEM_HASHMAP_H - -#include "util/tc_hashmap.h" -#include "util/tc_autoptr.h" -#include "jmem/jmem_policy.h" -#include "tup/Tars.h" - -namespace tars -{ - -/************************************************************************ - 基本说明如下: - 基于Tarså议的内存hashmap - 编解ç å‡ºé”™åˆ™æŠ›å‡ºTarsDecodeExceptionå’ŒTarsEncodeException - å¯ä»¥å¯¹é”策略和存储策略进行组åˆ, 例如: - 基于信å·é‡é”, 文件存储的hashmap: - TarsHashMap - 基于信å·é‡é”, 共享内存存储的hashmap - TarsHashMap - 基于线程é”, 内存存储的hashmap - TarsHashMap - - 使用上, ä¸åŒçš„组åˆ, åˆå§‹åŒ–函数ä¸å®Œå…¨ä¸€æ · - åˆå§‹åŒ–函数有: - SemLockPolicy::initLock(key_t) - ShmStorePolicy::initStore(key_t, size_t) - FileStorePolicy::initStore(const char *file, size_t) - ç­‰, 具体å‚è§jmem_policy.h - - *********************************************************************** - - 基本特性说明: - > 内存数æ®çš„map, æ ¹æ®æœ€åŽGet时间的顺åºæ·˜æ±°æ•°æ®; - > 支æŒç¼“写/dump到文件/在线备份; - > 支æŒä¸åŒå¤§å°å†…å­˜å—çš„é…ç½®, æ供内存的使用率; - > 支æŒå›žæ”¶åˆ°æŒ‡å®šç©ºé—²æ¯”率的空间; - > 支æŒä»…设置Keyçš„æ“作, å³æ•°æ®æ— value, åªæœ‰Key, 类似与stl::set; - > 支æŒè‡ªå®šä¹‰hash算法; - > hashæ•°å¯ä»¥æ ¹æ®å†…å­˜å—比率设置, 并优化有素数, æ高hash的散列性; - > 支æŒå‡ ç§æ–¹å¼çš„é历, 通常é历时需è¦å¯¹map加é”; - > 对于hashæ–¹å¼çš„é历, é历时å¯ä»¥ä¸éœ€è¦å¯¹map加é”, 推è使用; - > 支æŒè‡ªå®šä¹‰æ“作对象设置, å¯ä»¥éžå¸¸å¿«é€Ÿå®žçŽ°ç›¸å…³çš„接å£; - > 支æŒè‡ªåŠ¨ç¼–解ç , Keyå’ŒValue的结构都通过tars2cpp生æˆ; - > tarså议支æŒè‡ªåŠ¨æ‰©å±•å­—段, 因此该hashmap支æŒè‡ªåŠ¨æ‰©å±•å­—段(Keyå’ŒValue都必须是通过tarsç¼–ç çš„); - > map支æŒåªè¯»æ¨¡å¼, åªè¯»æ¨¡å¼ä¸‹set/erase/del等修改数æ®çš„æ“作ä¸èƒ½ä½¿ç”¨, get/回写/在线备份正常使用 - > 支æŒè‡ªåŠ¨æ·˜æ±°, setæ—¶, 内存满则自动淘汰, 在éžè‡ªåŠ¨æ·˜æ±°æ—¶, 内存满直接返回RT_READONLY - > 对于mmap文件, 支æŒè‡ªåŠ¨æ‰©å±•æ–‡ä»¶, å³å†…å­˜ä¸å¤Ÿäº†, å¯ä»¥è‡ªåŠ¨æ‰©å±•æ–‡ä»¶å¤§å°(注æ„hashçš„æ•°é‡ä¸å˜, 因此开始就需è¦è€ƒè™‘hashçš„æ•°é‡), 而且ä¸èƒ½è·¨JHashmap对象(å³ä¸¤ä¸ªhashmap对象访问åŒä¸€å—文件,通知一个hashmap扩展以åŽï¼Œå¦å¤–一个对象并ä¸çŸ¥é“扩展了) - - *********************************************************************** - - hashmap链说明: - hashmap链一共包括了如下几ç§é“¾è¡¨: - > Set时间链: 任何Setæ“作都会修改该链表, SetåŽæ•°æ®è¢«è®¾ç½®ä¸ºè„æ•°æ®, 且移动到Set链表头部; - > Get时间链: 任何Getæ“作都会修改该链表, 除éžé“¾è¡¨åªè¯», 注æ„Set链åŒæ—¶ä¹Ÿä¼šä¿®æ”¹Get链 - > Dirty时间链: dirty链是Set链的一部分, 用于回写数æ®ç”¨ - > Backup链:备份链是Get链的一部分, 当备份数æ®æ—¶, 顺ç€Get链从尾部到头部开始备份; - - *********************************************************************** - - 相关æ“作说明: - > å¯ä»¥è®¾ç½®mapåªè¯», 则所有写æ“作返回RT_READONLY, 此时Getæ“作ä¸ä¿®æ”¹é“¾è¡¨ - > å¯ä»¥è®¾ç½®çŸ¥å¦è‡ªåŠ¨æ·˜æ±°, 默认是自动淘汰的.如果ä¸è‡ªåŠ¨æ·˜æ±°,则setæ—¶,无内存空间返回:RT_NO_MEMORY - > å¯ä»¥æ›´æ”¹hash的算法, 调用setHashFunctorå³å¯ - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºå¹²å‡€, 此时移出到Dirty链表指Dirty尾部的下一个元素; - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºè„, 此时会移动到Set链表头部; - > æ¯ä¸ªæ•°æ®éƒ½æœ‰ä¸€ä¸ªä¸Šæ¬¡å›žå†™æ—¶é—´(SyncTime), 如果å¯åŠ¨å›žå†™æ“作, 则在一定时间内会回写; - > å¯ä»¥dump到文件或者从文件中load, 这个时候会对mapåŠ é” - > å¯ä»¥è°ƒç”¨erase批é‡æ·˜æ±°æ•°æ®ç›´åˆ°å†…存空闲率到一定比例 - > å¯ä»¥è°ƒç”¨sync进行数æ®å›žå†™, ä¿è¯ä¸€å®šæ—¶é—´å†…没有回写的数æ®ä¼šå›žå†™, map回写时间通过setSyncTime设置, 默认10分钟 - > å¯ä»¥setToDoFunctor设置æ“作类, 以下是æ“作触å‘的情况: - - *********************************************************************** - - ToDoFunctor的函数说明: - > 通常继承ToDoFunctor, 实现相关函数就å¯ä»¥äº†, å¯ä»¥å®žçŽ°ä»¥ä¸‹åŠŸèƒ½:Getæ•°æ®, 淘汰数æ®, 删除数æ®, 回写数æ®, å¤‡ä»½æ•°æ® - > ToDoFunctor::erase, 当调用map.eraseæ—¶, 该函数会被调用 - > ToDoFunctor::del, 当调用map.delæ—¶, 该函数会被调用, 注æ„删除时数æ®å¯èƒ½éƒ½ä¸åœ¨cache中; - > ToDoFunctor::sync, 当调用map.syncæ—¶, 会触å‘æ¯æ¡éœ€è¦å›žå†™çš„æ•°æ®è¯¥å‡½æ•°éƒ½è¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å›žå†™è¯·æ±‚; - > ToDoFunctor::backup, 当调用map.backupæ—¶, 会触å‘需è¦å¤‡ä»½çš„æ•°æ®è¯¥å‡½æ•°ä¼šè¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å¤‡ä»½è¯·æ±‚; - > ToDoFunctor::get, 当调用map.getæ—¶, 如果map中无数æ®, 则该函数被调用, 该函数从db中获å–æ•°æ®, 并返回RT_OK, 如果dbæ— æ•°æ®åˆ™è¿”回RT_NO_DATA; - > ToDoFunctor所有接å£è¢«è°ƒç”¨æ—¶, 都ä¸ä¼šå¯¹map加é”, å› æ­¤å¯ä»¥æ“作map - - *********************************************************************** - - mapçš„é‡è¦å‡½æ•°è¯´æ˜Ž: - > set, 设置数æ®åˆ°map中, 会更新set链表 - 如果满了, 且å¯ä»¥è‡ªåŠ¨æ·˜æ±°, 则根æ®Get链淘汰数æ®, 此时ToDoFunctorçš„sync会被调用 - 如果满了, 且å¯ä»¥ä¸èƒ½è‡ªåŠ¨æ·˜æ±°, 则返回RT_NO_MEMORY - - > get, 从map获å–æ•°æ®, 如果有数æ®, 则直接从map获å–æ•°æ®å¹¶è¿”回RT_OK; - 如果没有数æ®, 则调用ToDoFunctor::get函数, 此时get函数需è¦è¿”回RT_OK, åŒæ—¶ä¼šè®¾ç½®åˆ°map中, 并返回数æ®; - 如果没有数æ®, 则ToDoFunctor::get函数也无数æ®, 则需è¦è¿”回RT_NO_DATA, 此时åªä¼šæŠŠKey设置到map中, 并返回RT_ONLY_KEY; - 在上é¢æƒ…况下, 如果å†æœ‰get请求, 则ä¸å†è°ƒç”¨ToDoFunctor::get, 直接返回RT_ONLY_KEY; - - > del, 删除数æ®, 无论cache是å¦æœ‰æ•°æ®, ToDoFunctor::del都会被调用; - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«åˆ é™¤; - - > erase, 淘汰数æ®, åªæœ‰cache存在数æ®, ToDoFunctor::eraseæ‰ä¼šè¢«è°ƒç”¨ - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«æ·˜æ±°, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > erase(int radio), 批é‡æ·˜æ±°æ•°æ®, 直到空闲å—比率到达radio; - ToDoFunctor::erase会被调用; - åªæœ‰Key的记录也会被淘汰, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > sync: 缓写数æ®, 超时没有回写且是è„æ•°æ®éœ€è¦å›žå†™, 回写完毕åŽ, æ•°æ®ä¼šè‡ªåŠ¨è®¾ç½®ä¸ºå¹²å‡€æ•°æ®; - å¯ä»¥å¤šä¸ªçº¿ç¨‹æˆ–进程åŒæ—¶ç¼“写; - ToDoFunctor::sync会被调用; - åªæœ‰Key的记录, ToDoFunctor::syncä¸ä¼šè¢«è°ƒç”¨; - - > backup: 备份数æ®, 顺ç€é¡ºç€Get链从尾部到头部开始备份; - ToDoFunctor::backup会被调用; - åªæœ‰Key的记录, ToDoFunctor::backupä¸ä¼šè¢«è°ƒç”¨; - 由于备份游标åªæœ‰ä¸€ä¸ª, 因此多个进程åŒæ—¶å¤‡ä»½çš„时候数æ®å¯èƒ½ä¼šæ¯ä¸ªè¿›ç¨‹æœ‰ä¸€éƒ¨åˆ† - 如果备份程åºå¤‡ä»½åˆ°ä¸€åŠdown了, 则下次å¯åŠ¨å¤‡ä»½æ—¶ä¼šæŽ¥ç€ä¸Šæ¬¡çš„备份进行, 除éžå°†backup(true)调用备份 - - *********************************************************************** - - 返回值说明: - > 注æ„函数所有int的返回值, 如无特别说明, 请å‚è§TC_HashMap::RT_ - - *********************************************************************** - - é历说明: - > å¯ä»¥ç”¨lock_iterator对map进行以下几ç§é历, 在é历过程中其实对map加é”处ç†äº† - > end(): 迭代器尾部 - > begin(): 按照block区å—é历 - > rbegin():按照block区å—逆åºé历 - > beginSetTime(): 按照Set时间顺åºé历 - > rbeginSetTime(): 按照Set时间顺åºé历 - > beginGetTime(): 按照Get时间顺åºé历 - > rbeginGetTime(): 按照Get时间逆åºé历 - > beginDirty(): 按时间逆åºé历è„æ•°æ®é“¾(如果setClean, 则也å¯èƒ½åœ¨è„链表上) - > 其实回写数æ®é“¾æ˜¯è„æ•°æ®é‡çš„å­é›† - > 注æ„:lock_iterator一旦获å–, 就对map加é”了, 直到lock_iteratoræžå¤Ÿä¸ºæ­¢ - > - > å¯ä»¥ç”¨hash_iterator对map进行é历, é历过程中对map没有加é”, 推è使用 - > hashBegin(): 获å–hashé历迭代器 - > hashEnd(): hashé历尾部迭代器 - > 注æ„:hash_iterator对应的其实是一个hash桶链, æ¯æ¬¡èŽ·å–æ•°æ®å…¶å®žä¼šèŽ·å–桶链上é¢çš„æ‰€æœ‰æ•°æ® -*/ - -template class StorePolicy> -class TarsHashMap : public StorePolicy -{ -public: - /** - * 定义数æ®æ“作基类 - * 获å–,é历,删除,淘汰时都å¯ä»¥ä½¿ç”¨è¯¥æ“作类 - */ - class ToDoFunctor - { - public: - /** - * æ•°æ®è®°å½• - */ - struct DataRecord - { - K _key; - V _value; - bool _dirty; - time_t _iSyncTime; - - DataRecord() : _dirty(true), _iSyncTime(0) - { - } - }; - - /** - * æžå¤Ÿ - */ - virtual ~ToDoFunctor(){}; - - /** - * æ·˜æ±°æ•°æ® - * @param stDataRecord: è¢«æ·˜æ±°çš„æ•°æ® - */ - virtual void erase(const DataRecord &stDataRecord){}; - - /** - * åˆ é™¤æ•°æ® - * @param bExists: 是å¦å­˜åœ¨æ•°æ® - * @param stDataRecord: æ•°æ®, bExists==true时有效, å¦åˆ™åªæœ‰key有效 - */ - virtual void del(bool bExists, const DataRecord &stDataRecord){}; - - /** - * å›žå†™æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void sync(const DataRecord &stDataRecord){}; - - /** - * å¤‡ä»½æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void backup(const DataRecord &stDataRecord){}; - - /** - * 获å–æ•°æ®, 默认返回RT_NO_GET - * stDataRecord中_key有效, 其他数æ®éœ€è¦è¿”回 - * @param stDataRecord: 需è¦èŽ·å–çš„æ•°æ® - * - * @return int, 获å–到数æ®, 返回:TC_HashMap::RT_OK - * 没有数æ®,返回:TC_HashMap::RT_NO_DATA, - * 系统默认GET,返回:TC_HashMap::RT_NO_GET - * 其他,则返回:TC_HashMap::RT_LOAD_DATA_ERR - */ - virtual int get(DataRecord &stDataRecord) - { - return TC_HashMap::RT_NO_GET; - } - }; - - typedef typename ToDoFunctor::DataRecord DataRecord; - - /////////////////////////////////////////////////////////////////// - /** - * 自动é”, 用于迭代器 - */ - class JhmAutoLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmAutoLock(typename LockPolicy::Mutex &mutex) : _lock(mutex) - { - } - - protected: - //ä¸å®žçŽ° - JhmAutoLock(const JhmAutoLock &al); - JhmAutoLock &operator=(const JhmAutoLock &al); - - protected: - /** - * é” - */ - TC_LockT _lock; - }; - - typedef TC_AutoPtr JhmAutoLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmLockItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmLockItem(const TC_HashMap::HashMapLockItem &item) - : _item(item) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockItem(const JhmLockItem &item) - : _item(item._item) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockItem& - */ - JhmLockItem& operator=(const JhmLockItem &item) - { - if(this != &item) - { - _item = item._item; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmLockItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmLockItem& item) - { - return !((*this) == item); - } - - /** - * 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty() { return _item.isDirty(); } - - /** - * 是å¦åªæœ‰Key - * - * @return bool - */ - bool isOnlyKey() { return _item.isOnlyKey(); } - - /** - * 最åŽå›žå†™æ—¶é—´ - * - * @return time_t - */ - time_t getSyncTime() { return _item.getSyncTime(); } - - /** - * 获å–值 - * @return int - * TC_HashMap::RT_OK:æ•°æ®èŽ·å–OK - * 其他值, 异常 - */ - int get(K& k) - { - string sk; - int ret = _item.get(sk); - if(ret != TC_HashMap::RT_OK) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(sk.c_str(), sk.length()); - k.readFrom(is); - - return ret; - } - - /** - * 获å–值 - * @return int - * TC_HashMap::RT_OK:æ•°æ®èŽ·å–OK - * TC_HashMap::RT_ONLY_KEY: key有效, v无效为空 - * 其他值, 异常 - */ - int get(K& k, V& v) - { - string sk; - string sv; - int ret = _item.get(sk, sv); - if(ret != TC_HashMap::RT_OK && ret != TC_HashMap::RT_ONLY_KEY) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(sk.c_str(), sk.length()); - k.readFrom(is); - - if(ret != TC_HashMap::RT_ONLY_KEY) - { - is.setBuffer(sv.c_str(), sv.length()); - v.readFrom(is); - } - - return ret; - } - - protected: - TC_HashMap::HashMapLockItem _item; - }; - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmLockIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmLockIterator(const TC_HashMap::lock_iterator it, const JhmAutoLockPtr &lock) - : _it(it), _item(it._iItem), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockIterator(const JhmLockIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator=(const JhmLockIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - _lock = it._lock; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmLockIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmLockIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator++() - { - ++_it; - _item = JhmLockItem(_it._iItem); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator operator++(int) - { - JhmLockIterator jit(_it, _lock); - ++_it; - _item = JhmLockItem(_it._iItem); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem& - */ - JhmLockItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem* - */ - JhmLockItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_HashMap::lock_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmLockItem _item; - - /** - * é” - */ - JhmAutoLockPtr _lock; - }; - - typedef JhmLockIterator lock_iterator ; - - /////////////////////////////////////////////////////////////////// - /** - * é”, 用于éžé”迭代器 - * - */ - class JhmLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmLock(typename LockPolicy::Mutex &mutex) : _mutex(mutex) - { - } - - /** - * 获å–é” - * - * @return typename LockPolicy::Mutex - */ - typename LockPolicy::Mutex& mutex() - { - return _mutex; - } - protected: - //ä¸å®žçŽ° - JhmLock(const JhmLock &al); - JhmLock &operator=(const JhmLock &al); - - protected: - /** - * é” - */ - typename LockPolicy::Mutex &_mutex; - }; - - typedef TC_AutoPtr JhmLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmItem(const TC_HashMap::HashMapItem &item, const JhmLockPtr &lock) - : _item(item), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmItem(const JhmItem &item) - : _item(item._item), _lock(item._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmItem& - */ - JhmItem& operator=(const JhmItem &item) - { - if(this != &item) - { - _item = item._item; - _lock = item._lock; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmItem& item) - { - return !((*this) == item); - } - - /** - * 获å–当å‰hash桶的所有数é‡, 注æ„åªèŽ·å–有key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * 如果å议解ç æœ‰é—®é¢˜ä¹Ÿä¸èŽ·å– - * @param - */ - void get(vector > &v) - { - vector vtData; - - { - TC_LockT lock(_lock->mutex()); - _item.get(vtData); - } - - for(size_t i = 0; i < vtData.size(); i++) - { - pair pk; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - pk.first.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - pk.second.readFrom(is); - - v.push_back(pk); - } - catch(exception &ex) - { - } - } - } - - /** - * 设置当å‰æ¡¶ä¸‹çš„所有数æ®ä¸ºè„æ•°æ® - * - * @param - * - * @return int - */ - int setDirty() - { - { - TC_LockT lock(_lock->mutex()); - return _item.setDirty(); - } - } - - protected: - TC_HashMap::HashMapItem _item; - JhmLockPtr _lock; - }; - - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmIterator(const TC_HashMap::hash_iterator &it, const JhmLockPtr &lock) - : _it(it), _item(it._iItem, lock), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmIterator(const JhmIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmIterator& - */ - JhmIterator& operator=(const JhmIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmIterator& - */ - JhmIterator& operator++() - { - TC_LockT lock(_lock->mutex()); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmIterator& - */ - JhmIterator operator++(int) - { - TC_LockT lock(_lock->mutex()); - JhmIterator jit(_it, _lock); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem& - */ - JhmItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem* - */ - JhmItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_HashMap::hash_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmItem _item; - - /** - * é” - */ - JhmLockPtr _lock; - }; - - typedef JhmIterator hash_iterator ; - - //////////////////////////////////////////////////////////////////////////// - // - /** - * 构造函数 - */ - TarsHashMap() - { - _todo_of = NULL; - } - - /** - * åˆå§‹åŒ–æ•°æ®å—å¹³å‡å¤§å° - * 表示内存分é…的时候,会分é…n个最å°å—, n个(最å°å¿«*增长因å­ï¼‰, n个(最å°å¿«*增长因å­*增长因å­ï¼‰..., 直到nä¸ªæœ€å¤§å— - * n是hashmap自己计算出æ¥çš„ - * è¿™ç§åˆ†é…策略通常是你数æ®å¿«è®°å½•å˜é•¿æ¯”较多的使用, 便于节约内存,如果数æ®è®°å½•åŸºæœ¬ä¸æ˜¯å˜é•¿çš„, 那最å°å—=最大快,增长因å­=1å°±å¯ä»¥äº† - * @param iMinDataSize: 最å°æ•°æ®å—å¤§å° - * @param iMaxDataSize: 最大数æ®å—å¤§å° - * @param fFactor: å¢žé•¿å› å­ >= 1.0 - */ - void initDataBlockSize(size_t iMinDataSize, size_t iMaxDataSize, float fFactor) - { - this->_t.initDataBlockSize(iMinDataSize, iMaxDataSize, fFactor); - } - - /** - * 设置hash比率(设置chunkæ•°æ®å—/hash项比值, 默认是2) - * 有需è¦æ›´æ”¹å¿…须在create之å‰è°ƒç”¨ - * - * @param fRadio - */ - void initHashRadio(float fRadio) { this->_t.initHashRadio(fRadio);} - - /** - * 设置hashæ–¹å¼ - * @param hash_of - */ - void setHashFunctor(TC_HashMap::hash_functor hashf) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setHashFunctor(hashf); - } - - /** - * 获å–hashæ–¹å¼ - * - * @return TC_HashMap::hash_functor& - */ - TC_HashMap::hash_functor &getHashFunctor() { return this->_t.getHashFunctor(); } - - /** - * 设置淘汰æ“作类 - * @param erase_of - */ - void setToDoFunctor(ToDoFunctor *todo_of) { this->_todo_of = todo_of; } - - /** - * 获å–æ¯ç§å¤§å°å†…å­˜å—çš„å¤´éƒ¨ä¿¡æ¯ - * - * @return vector: ä¸åŒå¤§å°å†…å­˜å—å¤´éƒ¨ä¿¡æ¯ - */ - vector getBlockDetail() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getBlockDetail(); - } - - /** - * 所有block中chunk的个数 - * - * @return size_t - */ - size_t allBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.allBlockChunkCount(); - } - - /** - * æ¯ç§block中chunk的个数(ä¸åŒå¤§å°å†…å­˜å—的个数相åŒ) - * - * @return size_t - */ - vector singleBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.singleBlockChunkCount(); - } - - /** - * 获å–hash桶的个数 - * - * @return size_t - */ - size_t getHashCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getHashCount(); - } - - /** - * 元素的个数 - * - * @return size_t - */ - size_t size() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.size(); - } - - /** - * è„æ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t dirtyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.dirtyCount(); - } - - /** - * Onlyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t onlyKeyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.onlyKeyCount(); - } - - /** - * 设置æ¯æ¬¡æ·˜æ±°æ•°é‡ - * @param n - */ - void setEraseCount(size_t n) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseCount(n); - } - - /** - * 获å–æ¯æ¬¡æ·˜æ±°æ•°é‡ - * - * @return size_t - */ - size_t getEraseCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseCount(); - } - - /** - * 设置åªè¯» - * @param bReadOnly - */ - void setReadOnly(bool bReadOnly) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setReadOnly(bReadOnly); - } - - /** - * 是å¦åªè¯» - * - * @return bool - */ - bool isReadOnly() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isReadOnly(); - } - - /** - * 设置是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * @param bAutoErase - */ - void setAutoErase(bool bAutoErase) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setAutoErase(bAutoErase); - } - - /** - * 是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * - * @return bool - */ - bool isAutoErase() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isAutoErase(); - } - - /** - * è®¾ç½®æ·˜æ±°æ–¹å¼ - * TC_HashMap::ERASEBYGET - * TC_HashMap::ERASEBYSET - * @param cEraseMode - */ - void setEraseMode(char cEraseMode) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseMode(cEraseMode); - } - - /** - * 获å–æ·˜æ±°æ–¹å¼ - * - * @return bool - */ - char getEraseMode() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseMode(); - } - - /** - * å¤´éƒ¨ä¿¡æ¯ - * - * @return TC_HashMap::tagMapHead - */ - TC_HashMap::tagMapHead& getMapHead() { return this->_t.getMapHead(); } - - /** - * 设置回写时间(秒) - * @param iSyncTime - */ - void setSyncTime(time_t iSyncTime) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setSyncTime(iSyncTime); - } - - /** - * 获å–回写时间 - * - * @return time_t - */ - time_t getSyncTime() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getSyncTime(); - } - - /** - * dump到文件 - * @param sFile - * @param bDoClear: 是å¦æ¸…空 - * @return int - * TC_HashMap::RT_DUMP_FILE_ERR: dump到文件出错 - * TC_HashMap::RT_OK: dump到文件æˆåŠŸ - */ - int dump2file(const string &sFile, bool bDoClear = false) - { - TC_LockT lock(LockPolicy::mutex()); - int ret = this->_t.dump2file(sFile); - if(ret != TC_HashMap::RT_OK) - { - return ret; - } - - if(bDoClear) - this->_t.clear(); - - return ret; - } - - /** - * 从文件load - * @param sFile - * - * @return int - * TC_HashMap::RT_LOAL_FILE_ERR: load出错 - * TC_HashMap::RT_VERSION_MISMATCH_ERR: 版本ä¸ä¸€è‡´ - * TC_HashMap::RT_OK: loadæˆåŠŸ - */ - int load5file(const string &sFile) - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.load5file(sFile); - } - - /** - * 清空hash map - * 所有map中的数æ®éƒ½è¢«æ¸…空 - */ - void clear() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.clear(); - } - - /** - * 检查数æ®çŠ¶æ€ - * @param k - * - * @return int - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMap::RT_DIRTY_DATA: 是è„æ•°æ® - * TC_HashMap::RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const K &k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.checkDirty(sk); - } - - /** - * 设置为干净数æ®i, 修改SET/GET时间链, 会导致数æ®ä¸å›žå†™ - * @param k - * - * @return int - * TC_HashMap::RT_READONLY: åªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setClean(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setClean(sk); - } - - /** - * 设置为è„æ•°æ®, 修改SET/GET时间链, 会导致数æ®å›žå†™ - * @param k - * @return int - * TC_HashMap::RT_READONLY: åªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMap::RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirty(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setDirty(sk); - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * (如果没设置自定义Get函数,没有数æ®æ—¶è¿”回:RT_NO_DATA) - * @param k - * @param v - * @param iSyncTime:æ•°æ®ä¸Šæ¬¡å›žå†™çš„时间, 没有缓写则为0 - * - * @return int: - * TC_HashMap::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_HashMap::RT_READONLY: åªè¯»æ¨¡å¼ - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMap::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_HashMap::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const K& k, V &v, time_t &iSyncTime) - { - iSyncTime = 0; - int ret = TC_HashMap::RT_OK; - - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - string sv; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.get(sk, sv, iSyncTime); - } - - //读å–到数æ®äº†, 解包 - if(ret == TC_HashMap::RT_OK) - { - tars::TarsInputStream is; - is.setBuffer(sv.c_str(), sv.length()); - v.readFrom(is); - - return ret; - } - - if(ret != TC_HashMap::RT_NO_DATA || _todo_of == NULL) - { - return ret; - } - - //åªè¯»æ¨¡å¼ - if(isReadOnly()) - { - return TC_HashMap::RT_READONLY; - } - - //获å–函数 - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - ret = _todo_of->get(stDataRecord); - if(ret == TC_HashMap::RT_OK) - { - v = stDataRecord._value; - return this->set(stDataRecord._key, stDataRecord._value, stDataRecord._dirty); - } - else if(ret == TC_HashMap::RT_NO_GET) - { - return TC_HashMap::RT_NO_DATA; - } - else if(ret == TC_HashMap::RT_NO_DATA) - { - ret = this->set(stDataRecord._key); - if(ret == TC_HashMap::RT_OK) - { - return TC_HashMap::RT_ONLY_KEY; - } - return ret; - } - - return TC_HashMap::RT_LOAD_DATA_ERR; - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * - * @return int: - * TC_HashMap::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMap::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_HashMap::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const K& k, V &v) - { - time_t iSyncTime; - return get(k, v, iSyncTime); - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - template - int getHash(size_t h, vector > &vv, C c) - { - int ret = TC_HashMap::RT_OK; - - TC_LockT lock(LockPolicy::mutex()); - - this->_t.doUpdate(); - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMap::Block block(&this->_t, iAddr); - - while(block.getHead() != 0) - { - TC_HashMap::BlockData data; - ret = block.getBlockData(data); - if(ret == TC_HashMap::RT_OK) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - { - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - vv.push_back(make_pair(tk, tv)); - } - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - return TC_HashMap::RT_OK; - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - template - int getHash(size_t h, vector &vv, C c) - { - int ret = TC_HashMap::RT_OK; - - TC_LockT lock(LockPolicy::mutex()); - - this->_t.doUpdate(); - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMap::Block block(&this->_t, iAddr); - - while(block.getHead() != 0) - { - TC_HashMap::BlockData data; - ret = block.getBlockData(data); - if(ret == TC_HashMap::RT_OK) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - { - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - vv.push_back(stDataRecord); - } - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - return TC_HashMap::RT_OK; - } - - /** - * æ¢å¤æ•°æ® - * 对于block记录无法读å–çš„æ•°æ®è‡ªåŠ¨åˆ é™¤ - * @param bRepair: 是å¦ä¿®å¤ - * @return 返回删除的记录数 - */ - size_t recover(bool bRepair) - { - size_t c = this->_t.getHashCount(); - size_t e = 0; - for(size_t i = 0; i < c; i++) - { - TC_LockT lock(LockPolicy::mutex()); - - e += this->_t.recover(i, bRepair); - } - - return e; - } - - /** - * 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @param v: 值 - * @param bDirty: 是å¦æ˜¯è„æ•°æ® - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const K& k, const V& v, bool bDirty = true) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - tars::TarsOutputStream osv; - v.writeTo(osv); - string sv(osv.getBuffer(), osv.getLength()); - - int ret = TC_HashMap::RT_OK; - vector vtData; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(sk, sv, bDirty, vtData); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtData.size(); i++) - { - K tk; - V tv; - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - tk.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = vtData[i]._dirty; - stDataRecord._iSyncTime = vtData[i]._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * 仅设置Key, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - int ret = TC_HashMap::RT_OK; - vector vtData; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(sk, vtData); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtData.size(); i++) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - tk.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = vtData[i]._dirty; - stDataRecord._iSyncTime = vtData[i]._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * 无论cache是å¦æœ‰æ•°æ®,todoçš„del都被调用 - * - * @param k, 关键字 - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int del(const K& k) - { - int ret = TC_HashMap::RT_OK; - - TC_HashMap::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_HashMap::RT_OK && ret != TC_HashMap::RT_ONLY_KEY && ret != TC_HashMap::RT_NO_DATA) - { - return ret; - } - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - - if(ret == TC_HashMap::RT_OK) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._value.c_str(), data._value.length()); - v.readFrom(is); - - stDataRecord._value = v; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - } - - _todo_of->del((ret == TC_HashMap::RT_OK), stDataRecord); - } - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * cache有数æ®,todoçš„erase被调用 - * - * @param k, 关键字 - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int erase(const K& k) - { - int ret = TC_HashMap::RT_OK; - - TC_HashMap::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_HashMap::RT_OK) - { - return ret; - } - - if(_todo_of) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._value.c_str(), data._value.length()); - v.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - stDataRecord._value = v; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->erase(stDataRecord); - } - return ret; - } - - /** - * 强制删除数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * - * @param k, 关键字 - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int eraseByForce(const K& k) - { - int ret = TC_HashMap::RT_OK; - - TC_HashMap::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_HashMap::RT_OK) - { - return ret; - } - - return ret; - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - - /** - * æ ¹æ®hash,强制删除相åŒhash值的所有数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param c, 匹é…仿函数: bool operator()(K v); - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - template - int eraseHashByForce(size_t h, C c) - { - int ret = TC_HashMap::RT_OK; - - vector vDelKey; - - TC_LockT lock(LockPolicy::mutex()); - - this->_t.doUpdate(); - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMap::Block block(&this->_t, iAddr); - TC_HashMap::BlockData data; - - while(block.getHead() != 0) - { - ret = block.getBlockData(data); - if(ret == TC_HashMap::RT_OK) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - vDelKey.push_back(data._key); - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - for(size_t i=0; i_t.del(vDelKey[i], data); - if(ret != TC_HashMap::RT_OK) - { - return ret; - } - } - - return TC_HashMap::RT_OK; - } - - /** - * æ ¹æ®hash,强制删除相åŒhash值的所有数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param c, 匹é…仿函数: bool operator()(K v); - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - template - int eraseHashByForce(size_t h, C c, vector& vDelK) - { - int ret = TC_HashMap::RT_OK; - - vector vDelKey; - - TC_LockT lock(LockPolicy::mutex()); - - this->_t.doUpdate(); - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMap::Block block(&this->_t, iAddr); - TC_HashMap::BlockData data; - - while(block.getHead() != 0) - { - ret = block.getBlockData(data); - if(ret == TC_HashMap::RT_OK) - { - K tk; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - { - vDelKey.push_back(data._key); - vDelK.push_back(tk); - } - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - for(size_t i=0; i_t.del(vDelKey[i], data); - if(ret != TC_HashMap::RT_OK) - { - vDelK.resize(i); - return ret; - } - } - - return TC_HashMap::RT_OK; - } - - /** - * 淘汰数æ®, æ ¹æ®Get时间淘汰 - * 直到: 元素个数/chunks * 100 < radio,bCheckDirty 为true时,é‡åˆ°è„æ•°æ®åˆ™æ·˜æ±°ç»“æŸ - * @param radio: 共享内存chunks使用比例 0< radio < 100 - * @return int: - * TC_HashMap::RT_READONLY: mapåªè¯» - * TC_HashMap::RT_OK:淘汰完毕 - */ - int erase(int radio, bool bCheckDirty = false) - { - while(true) - { - int ret; - TC_HashMap::BlockData data; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.erase(radio, data, bCheckDirty); - if(ret == TC_HashMap::RT_OK || ret == TC_HashMap::RT_READONLY) - { - return ret; - } - - if(ret != TC_HashMap::RT_ERASE_OK) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->erase(stDataRecord); - } - } - return TC_HashMap::RT_OK; - } - - /** - * 回写å•æ¡è®°å½•, 如果记录ä¸å­˜åœ¨, 则ä¸åšä»»ä½•å¤„ç† - * @param k - * - * @return int - * TC_HashMap::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMap::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_HashMap::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int sync(const K& k) - { - V v; - time_t iSyncTime; - int ret = get(k, v, iSyncTime); - - if(ret == TC_HashMap::RT_OK) - { - bool bDirty = (checkDirty(k) == TC_HashMap::RT_DIRTY_DATA); - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - stDataRecord._value = v; - stDataRecord._dirty = bDirty; - stDataRecord._iSyncTime = iSyncTime; - - _todo_of->sync(stDataRecord); - } - } - - return ret; - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å…¨éƒ¨å›žå†™ - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - * - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_HashMap::RT_OK: 回写完毕了 - */ - int sync(time_t iNowTime) - { - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - while(true) - { - TC_HashMap::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_HashMap::RT_NEED_SYNC) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->sync(stDataRecord); - } - } - - return TC_HashMap::RT_OK; - } - - /** - *å°†è„æ•°æ®å°¾æŒ‡é’ˆèµ‹ç»™å›žå†™å°¾æŒ‡é’ˆ - */ - void sync() - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å›žå†™,åªå›žå†™ä¸€ä¸ªè„æ•°æ®ï¼Œç›®çš„是替代int sync(uint32_t iNowTime) - * 方法,把由业务控制æ¯æ¬¡å›žå†™æ•°æ®é‡ï¼Œä½¿ç”¨æ—¶åº”该先调用void sync() - * 注æ„:cæ¡ä»¶å¯¹è±¡æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_HashMapCompact::RT_OK: 回写完毕了 - * - * 示例: - * p->sync(); - * while(true) { - * int iRet = pthis->SyncOnce(tNow); - * if( iRet == TC_HashMapCompact::RT_OK ) - * break; - * } - */ - template - int syncOnce(uint32_t iNowTime, C c) - { - TC_HashMap::BlockData data; - - K tk; - tars::TarsInputStream is; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_HashMap::RT_NEED_SYNC) - { - return ret; - } - - if(_todo_of) - { - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(!c(tk)) - { - this->_t.setDirtyAfterSync(data._key); - return ret; - } - } - else - { - return ret; - } - } - - V tv; - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->sync(stDataRecord); - - return ret; - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å›žå†™,åªå›žå†™ä¸€ä¸ªè„æ•°æ®ï¼Œç›®çš„是替代int sync(time_t iNowTime) - * 方法,把由业务控制æ¯æ¬¡å›žå†™æ•°æ®é‡ï¼Œä½¿ç”¨æ—¶åº”该先调用void sync() - * - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_HashMap::RT_OK: 回写完毕了 - * - * 示例: - * p->sync(); - * while(true) { - * int iRet = pthis->SyncOnce(tNow); - * if( iRet == TC_HashMap::RT_OK ) - * break; - * } - */ - int syncOnce(time_t iNowTime) - { - - - TC_HashMap::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_HashMap::RT_NEED_SYNC) - { - return ret; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->sync(stDataRecord); - } - - return ret; - } - /** - * å¤‡ä»½æ•°æ® - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å¤‡ä»½ - * å¯ä»¥å¤šä¸ªçº¿ç¨‹/进程备份数æ®,åŒæ—¶å¤‡ä»½æ—¶bForceFromBegin设置为false效率更高 - * - * @param bForceFromBegin: 是å¦å¼ºåˆ¶é‡å¤´å¼€å§‹å¤‡ä»½, 通常为false - * @return int: - * TC_HashMap::RT_OK: 备份OK了 - */ - int backup(bool bForceFromBegin = false) - { - { - //开始准备备份 - TC_LockT lock(LockPolicy::mutex()); - this->_t.backup(bForceFromBegin); - } - - while(true) - { - TC_HashMap::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.backup(data); - if(ret == TC_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_HashMap::RT_NEED_BACKUP) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->backup(stDataRecord); - } - } - - return TC_HashMap::RT_OK; - } - - /** - * æè¿° - * - * @return string - */ - string desc() { return this->_t.desc(); } - - /////////////////////////////////////////////////////////////////////////////// - /** - * 尾部 - * - * @return lock_iterator - */ - lock_iterator end() - { - JhmAutoLockPtr jlock; - return JhmLockIterator(this->_t.end(), jlock); - } - - /** - * æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * @param k - */ - lock_iterator find(const K& k) - { - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.find(sk), jlock); - } - - /** - * blockæ­£åº - * - * @return lock_iterator - */ - lock_iterator begin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.begin(), jlock); - } - - /** - * blocké€†åº - * - * @return lock_iterator - */ - lock_iterator rbegin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbegin(), jlock); - } - - /** - * 以Set时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * - * @return lock_iterator - */ - lock_iterator beginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginSetTime(), jlock); - } - - /** - * Set时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Set-->最近Set - * - * @return lock_iterator - */ - lock_iterator rbeginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginSetTime(), jlock); - } - - /** - * 以Get时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Get-->最久Get - * - * @return lock_iterator - */ - lock_iterator beginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginGetTime(), jlock); - } - - /** - * Get时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Get-->最近Get - * - * @return lock_iterator - */ - lock_iterator rbeginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginGetTime(), jlock); - } - - /** - * 获å–è„链表尾部迭代器(最长时间没有Setçš„è„æ•°æ®) - * - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * å¯èƒ½å­˜åœ¨å¹²å‡€æ•°æ® - * - * @return lock_iterator - */ - lock_iterator beginDirty() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginDirty(), jlock); - } - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, ä¸éœ€è¦å¯¹mapåŠ é” - - /** - * æ ¹æ®hash桶é历 - * - * @return hash_iterator - */ - hash_iterator hashBegin() - { - JhmLockPtr jlock(new JhmLock(this->mutex())); - TC_LockT lock(LockPolicy::mutex()); - return JhmIterator(this->_t.hashBegin(), jlock); - } - - /** - * ç»“æŸ - * - * @return - */ - hash_iterator hashEnd() - { - JhmLockPtr jlock; - return JhmIterator(this->_t.hashEnd(), jlock); - } - - /** - * 获å–指定下标的hash_iterator - * @param iIndex - * - * @return hash_iterator - */ - hash_iterator hashIndex(size_t iIndex) - { - JhmLockPtr jlock(new JhmLock(this->mutex())); - TC_LockT lock(LockPolicy::mutex()); - return JhmIterator(this->_t.hashIndex(iIndex), jlock); - } - -protected: - - /** - * 删除数æ®çš„函数对象 - */ - ToDoFunctor *_todo_of; -}; - -} - -#endif diff --git a/cpp/servant/jmem/jmem_hashmap_compact.h b/cpp/servant/jmem/jmem_hashmap_compact.h deleted file mode 100644 index cc567df69..000000000 --- a/cpp/servant/jmem/jmem_hashmap_compact.h +++ /dev/null @@ -1,2421 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _JMEM_HASHMAP_COMPACT_H -#define _JMEM_HASHMAP_COMPACT_H - -#include "util/tc_hashmap_compact.h" -#include "util/tc_autoptr.h" -#include "jmem/jmem_policy.h" -#include "tup/Tars.h" - -namespace tars -{ -/************************************************************************ -é‡è¦è¯´æ˜Ž: -1 使用方å¼ä¸ŽTarsHashmap完全一致; -2 与JHashmap相比, 在64ä½os下é¢,æ¯ä¸ªchunk能够节约大概30个字节; -3 效率比JHashmapç¨æ…¢ä¸€ç‚¹,å¯ä»¥å¿½ç•¥ä¸è®¡; -4 æ¯ä¸ªchunk的大å°æœ€å¤§åªèƒ½æ˜¯64k(与实际数æ®å—大å°æ— å…³,实际数æ®å—å¯ä»¥å¤šä¸ªchunk拼接在一起) -************************************************************************/ - -/************************************************************************ - 基本说明如下: - 基于Tarså议的内存hashmap - 编解ç å‡ºé”™åˆ™æŠ›å‡ºTarsDecodeExceptionå’ŒTarsEncodeException - å¯ä»¥å¯¹é”策略和存储策略进行组åˆ, 例如: - 基于信å·é‡é”, 文件存储的hashmap: - TarsHashMap - 基于信å·é‡é”, 共享内存存储的hashmap - TarsHashMap - 基于线程é”, 内存存储的hashmap - TarsHashMap - - 使用上, ä¸åŒçš„组åˆ, åˆå§‹åŒ–函数ä¸å®Œå…¨ä¸€æ · - åˆå§‹åŒ–函数有: - SemLockPolicy::initLock(key_t) - ShmStorePolicy::initStore(key_t, size_t) - FileStorePolicy::initStore(const char *file, size_t) - ç­‰, 具体å‚è§jmem_policy.h - - *********************************************************************** - - 基本特性说明: - > 内存数æ®çš„map, æ ¹æ®æœ€åŽGet时间的顺åºæ·˜æ±°æ•°æ®; - > 支æŒç¼“写/dump到文件/在线备份; - > 支æŒä¸åŒå¤§å°å†…å­˜å—çš„é…ç½®, æ供内存的使用率; - > 支æŒå›žæ”¶åˆ°æŒ‡å®šç©ºé—²æ¯”率的空间; - > 支æŒä»…设置Keyçš„æ“作, å³æ•°æ®æ— value, åªæœ‰Key, 类似与stl::set; - > 支æŒè‡ªå®šä¹‰hash算法; - > hashæ•°å¯ä»¥æ ¹æ®å†…å­˜å—比率设置, 并优化有素数, æ高hash的散列性; - > 支æŒå‡ ç§æ–¹å¼çš„é历, 通常é历时需è¦å¯¹map加é”; - > 对于hashæ–¹å¼çš„é历, é历时å¯ä»¥ä¸éœ€è¦å¯¹map加é”, 推è使用; - > 支æŒè‡ªå®šä¹‰æ“作对象设置, å¯ä»¥éžå¸¸å¿«é€Ÿå®žçŽ°ç›¸å…³çš„接å£; - > 支æŒè‡ªåŠ¨ç¼–解ç , Keyå’ŒValue的结构都通过tars2cpp生æˆ; - > tarså议支æŒè‡ªåŠ¨æ‰©å±•å­—段, 因此该hashmap支æŒè‡ªåŠ¨æ‰©å±•å­—段(Keyå’ŒValue都必须是通过tarsç¼–ç çš„); - > map支æŒåªè¯»æ¨¡å¼, åªè¯»æ¨¡å¼ä¸‹set/erase/del等修改数æ®çš„æ“作ä¸èƒ½ä½¿ç”¨, get/回写/在线备份正常使用 - > 支æŒè‡ªåŠ¨æ·˜æ±°, setæ—¶, 内存满则自动淘汰, 在éžè‡ªåŠ¨æ·˜æ±°æ—¶, 内存满直接返回RT_READONLY - > 对于mmap文件, 支æŒè‡ªåŠ¨æ‰©å±•æ–‡ä»¶, å³å†…å­˜ä¸å¤Ÿäº†, å¯ä»¥è‡ªåŠ¨æ‰©å±•æ–‡ä»¶å¤§å°(注æ„hashçš„æ•°é‡ä¸å˜, 因此开始就需è¦è€ƒè™‘hashçš„æ•°é‡), 而且ä¸èƒ½è·¨JHashmap对象(å³ä¸¤ä¸ªhashmap对象访问åŒä¸€å—文件,通知一个hashmap扩展以åŽï¼Œå¦å¤–一个对象并ä¸çŸ¥é“扩展了) - - *********************************************************************** - - hashmap链说明: - hashmap链一共包括了如下几ç§é“¾è¡¨: - > Set时间链: 任何Setæ“作都会修改该链表, SetåŽæ•°æ®è¢«è®¾ç½®ä¸ºè„æ•°æ®, 且移动到Set链表头部; - > Get时间链: 任何Getæ“作都会修改该链表, 除éžé“¾è¡¨åªè¯», 注æ„Set链åŒæ—¶ä¹Ÿä¼šä¿®æ”¹Get链 - > Dirty时间链: dirty链是Set链的一部分, 用于回写数æ®ç”¨ - > Backup链:备份链是Get链的一部分, 当备份数æ®æ—¶, 顺ç€Get链从尾部到头部开始备份; - - *********************************************************************** - - 相关æ“作说明: - > å¯ä»¥è®¾ç½®mapåªè¯», 则所有写æ“作返回RT_READONLY, 此时Getæ“作ä¸ä¿®æ”¹é“¾è¡¨ - > å¯ä»¥è®¾ç½®çŸ¥å¦è‡ªåŠ¨æ·˜æ±°, 默认是自动淘汰的.如果ä¸è‡ªåŠ¨æ·˜æ±°,则setæ—¶,无内存空间返回:RT_NO_MEMORY - > å¯ä»¥æ›´æ”¹hash的算法, 调用setHashFunctorå³å¯ - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºå¹²å‡€, 此时移出到Dirty链表指Dirty尾部的下一个元素; - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºè„, 此时会移动到Set链表头部; - > æ¯ä¸ªæ•°æ®éƒ½æœ‰ä¸€ä¸ªä¸Šæ¬¡å›žå†™æ—¶é—´(SyncTime), 如果å¯åŠ¨å›žå†™æ“作, 则在一定时间内会回写; - > å¯ä»¥dump到文件或者从文件中load, 这个时候会对mapåŠ é” - > å¯ä»¥è°ƒç”¨erase批é‡æ·˜æ±°æ•°æ®ç›´åˆ°å†…存空闲率到一定比例 - > å¯ä»¥è°ƒç”¨sync进行数æ®å›žå†™, ä¿è¯ä¸€å®šæ—¶é—´å†…没有回写的数æ®ä¼šå›žå†™, map回写时间通过setSyncTime设置, 默认10分钟 - > å¯ä»¥setToDoFunctor设置æ“作类, 以下是æ“作触å‘的情况: - - *********************************************************************** - - ToDoFunctor的函数说明: - > 通常继承ToDoFunctor, 实现相关函数就å¯ä»¥äº†, å¯ä»¥å®žçŽ°ä»¥ä¸‹åŠŸèƒ½:Getæ•°æ®, 淘汰数æ®, 删除数æ®, 回写数æ®, å¤‡ä»½æ•°æ® - > ToDoFunctor::erase, 当调用map.eraseæ—¶, 该函数会被调用 - > ToDoFunctor::del, 当调用map.delæ—¶, 该函数会被调用, 注æ„删除时数æ®å¯èƒ½éƒ½ä¸åœ¨cache中; - > ToDoFunctor::sync, 当调用map.syncæ—¶, 会触å‘æ¯æ¡éœ€è¦å›žå†™çš„æ•°æ®è¯¥å‡½æ•°éƒ½è¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å›žå†™è¯·æ±‚; - > ToDoFunctor::backup, 当调用map.backupæ—¶, 会触å‘需è¦å¤‡ä»½çš„æ•°æ®è¯¥å‡½æ•°ä¼šè¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å¤‡ä»½è¯·æ±‚; - > ToDoFunctor::get, 当调用map.getæ—¶, 如果map中无数æ®, 则该函数被调用, 该函数从db中获å–æ•°æ®, 并返回RT_OK, 如果dbæ— æ•°æ®åˆ™è¿”回RT_NO_DATA; - > ToDoFunctor所有接å£è¢«è°ƒç”¨æ—¶, 都ä¸ä¼šå¯¹map加é”, å› æ­¤å¯ä»¥æ“作map - - *********************************************************************** - - mapçš„é‡è¦å‡½æ•°è¯´æ˜Ž: - > set, 设置数æ®åˆ°map中, 会更新set链表 - 如果满了, 且å¯ä»¥è‡ªåŠ¨æ·˜æ±°, 则根æ®Get链淘汰数æ®, 此时ToDoFunctorçš„sync会被调用 - 如果满了, 且å¯ä»¥ä¸èƒ½è‡ªåŠ¨æ·˜æ±°, 则返回RT_NO_MEMORY - - > get, 从map获å–æ•°æ®, 如果有数æ®, 则直接从map获å–æ•°æ®å¹¶è¿”回RT_OK; - 如果没有数æ®, 则调用ToDoFunctor::get函数, 此时get函数需è¦è¿”回RT_OK, åŒæ—¶ä¼šè®¾ç½®åˆ°map中, 并返回数æ®; - 如果没有数æ®, 则ToDoFunctor::get函数也无数æ®, 则需è¦è¿”回RT_NO_DATA, 此时åªä¼šæŠŠKey设置到map中, 并返回RT_ONLY_KEY; - 在上é¢æƒ…况下, 如果å†æœ‰get请求, 则ä¸å†è°ƒç”¨ToDoFunctor::get, 直接返回RT_ONLY_KEY; - - > del, 删除数æ®, 无论cache是å¦æœ‰æ•°æ®, ToDoFunctor::del都会被调用; - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«åˆ é™¤; - - > erase, 淘汰数æ®, åªæœ‰cache存在数æ®, ToDoFunctor::eraseæ‰ä¼šè¢«è°ƒç”¨ - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«æ·˜æ±°, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > erase(int radio), 批é‡æ·˜æ±°æ•°æ®, 直到空闲å—比率到达radio; - ToDoFunctor::erase会被调用; - åªæœ‰Key的记录也会被淘汰, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > sync: 缓写数æ®, 超时没有回写且是è„æ•°æ®éœ€è¦å›žå†™, 回写完毕åŽ, æ•°æ®ä¼šè‡ªåŠ¨è®¾ç½®ä¸ºå¹²å‡€æ•°æ®; - å¯ä»¥å¤šä¸ªçº¿ç¨‹æˆ–进程åŒæ—¶ç¼“写; - ToDoFunctor::sync会被调用; - åªæœ‰Key的记录, ToDoFunctor::syncä¸ä¼šè¢«è°ƒç”¨; - - > backup: 备份数æ®, 顺ç€é¡ºç€Get链从尾部到头部开始备份; - ToDoFunctor::backup会被调用; - åªæœ‰Key的记录, ToDoFunctor::backupä¸ä¼šè¢«è°ƒç”¨; - 由于备份游标åªæœ‰ä¸€ä¸ª, 因此多个进程åŒæ—¶å¤‡ä»½çš„时候数æ®å¯èƒ½ä¼šæ¯ä¸ªè¿›ç¨‹æœ‰ä¸€éƒ¨åˆ† - 如果备份程åºå¤‡ä»½åˆ°ä¸€åŠdown了, 则下次å¯åŠ¨å¤‡ä»½æ—¶ä¼šæŽ¥ç€ä¸Šæ¬¡çš„备份进行, 除éžå°†backup(true)调用备份 - - *********************************************************************** - - 返回值说明: - > 注æ„函数所有int的返回值, 如无特别说明, 请å‚è§TC_HashMapCompact::RT_ - - *********************************************************************** - - é历说明: - > å¯ä»¥ç”¨lock_iterator对map进行以下几ç§é历, 在é历过程中其实对map加é”处ç†äº† - > end(): 迭代器尾部 - > begin(): 按照block区å—é历 - > rbegin():按照block区å—逆åºé历 - > beginSetTime(): 按照Set时间顺åºé历 - > rbeginSetTime(): 按照Set时间顺åºé历 - > beginGetTime(): 按照Get时间顺åºé历 - > rbeginGetTime(): 按照Get时间逆åºé历 - > beginDirty(): 按时间逆åºé历è„æ•°æ®é“¾(如果setClean, 则也å¯èƒ½åœ¨è„链表上) - > 其实回写数æ®é“¾æ˜¯è„æ•°æ®é‡çš„å­é›† - > 注æ„:lock_iterator一旦获å–, 就对map加é”了, 直到lock_iteratoræžå¤Ÿä¸ºæ­¢ - > - > å¯ä»¥ç”¨hash_iterator对map进行é历, é历过程中对map没有加é”, 推è使用 - > hashBegin(): 获å–hashé历迭代器 - > hashEnd(): hashé历尾部迭代器 - > 注æ„:hash_iterator对应的其实是一个hash桶链, æ¯æ¬¡èŽ·å–æ•°æ®å…¶å®žä¼šèŽ·å–桶链上é¢çš„æ‰€æœ‰æ•°æ® -*/ - -template class StorePolicy> -class TarsHashMapCompact : public StorePolicy -{ -public: - /** - * 定义数æ®æ“作基类 - * 获å–,é历,删除,淘汰时都å¯ä»¥ä½¿ç”¨è¯¥æ“作类 - */ - class ToDoFunctor - { - public: - /** - * æ•°æ®è®°å½• - */ - struct DataRecord - { - K _key; - V _value; - bool _dirty; - uint32_t _iSyncTime; - uint32_t _expiret; - uint8_t _ver; - - - DataRecord() : _dirty(true), _iSyncTime(0) - { - } - }; - - /** - * æžå¤Ÿ - */ - virtual ~ToDoFunctor(){}; - - /** - * æ·˜æ±°æ•°æ® - * @param stDataRecord: è¢«æ·˜æ±°çš„æ•°æ® - */ - virtual void erase(const DataRecord &stDataRecord){}; - - /** - * åˆ é™¤æ•°æ® - * @param bExists: 是å¦å­˜åœ¨æ•°æ® - * @param stDataRecord: æ•°æ®, bExists==true时有效, å¦åˆ™åªæœ‰key有效 - */ - virtual void del(bool bExists, const DataRecord &stDataRecord){}; - - /** - * å›žå†™æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void sync(const DataRecord &stDataRecord){}; - - /** - * å¤‡ä»½æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void backup(const DataRecord &stDataRecord){}; - - /** - * 获å–æ•°æ®, 默认返回RT_NO_GET - * stDataRecord中_key有效, 其他数æ®éœ€è¦è¿”回 - * @param stDataRecord: 需è¦èŽ·å–çš„æ•°æ® - * - * @return int, 获å–到数æ®, 返回:TC_HashMapCompact::RT_OK - * 没有数æ®,返回:TC_HashMapCompact::RT_NO_DATA, - * 系统默认GET,返回:TC_HashMapCompact::RT_NO_GET - * 其他,则返回:TC_HashMapCompact::RT_LOAD_DATA_ERR - */ - virtual int get(DataRecord &stDataRecord) - { - return TC_HashMapCompact::RT_NO_GET; - } - }; - - typedef typename ToDoFunctor::DataRecord DataRecord; - - /////////////////////////////////////////////////////////////////// - /** - * 自动é”, 用于迭代器 - */ - class JhmAutoLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmAutoLock(typename LockPolicy::Mutex &mutex) : _lock(mutex) - { - } - - protected: - //ä¸å®žçŽ° - JhmAutoLock(const JhmAutoLock &al); - JhmAutoLock &operator=(const JhmAutoLock &al); - - protected: - /** - * é” - */ - TC_LockT _lock; - }; - - typedef TC_AutoPtr JhmAutoLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmLockItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmLockItem(const TC_HashMapCompact::HashMapLockItem &item) - : _item(item) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockItem(const JhmLockItem &item) - : _item(item._item) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockItem& - */ - JhmLockItem& operator=(const JhmLockItem &item) - { - if(this != &item) - { - _item = item._item; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmLockItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmLockItem& item) - { - return !((*this) == item); - } - - /** - * 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty() { return _item.isDirty(); } - - /** - * 是å¦åªæœ‰Key - * - * @return bool - */ - bool isOnlyKey() { return _item.isOnlyKey(); } - - /** - * 最åŽå›žå†™æ—¶é—´ - * - * @return uint32_t - */ - uint32_t getSyncTime() { return _item.getSyncTime(); } - - /** - * 获å–值 - * @return int - * TC_HashMapCompact::RT_OK:æ•°æ®èŽ·å–OK - * 其他值, 异常 - */ - int get(K& k) - { - string sk; - int ret = _item.get(sk); - if(ret != TC_HashMapCompact::RT_OK) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(sk.c_str(), sk.length()); - k.readFrom(is); - - return ret; - } - - /** - * 获å–值 - * @return int - * TC_HashMapCompact::RT_OK:æ•°æ®èŽ·å–OK - * TC_HashMapCompact::RT_ONLY_KEY: key有效, v无效为空 - * 其他值, 异常 - */ - int get(K& k, V& v) - { - string sk; - string sv; - int ret = _item.get(sk, sv); - if(ret != TC_HashMapCompact::RT_OK && ret != TC_HashMapCompact::RT_ONLY_KEY) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(sk.c_str(), sk.length()); - k.readFrom(is); - - if(ret != TC_HashMapCompact::RT_ONLY_KEY) - { - is.setBuffer(sv.c_str(), sv.length()); - v.readFrom(is); - } - - return ret; - } - - protected: - TC_HashMapCompact::HashMapLockItem _item; - }; - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmLockIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmLockIterator(const TC_HashMapCompact::lock_iterator it, const JhmAutoLockPtr &lock) - : _it(it), _item(it._iItem), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockIterator(const JhmLockIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator=(const JhmLockIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - _lock = it._lock; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmLockIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmLockIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator++() - { - ++_it; - _item = JhmLockItem(_it._iItem); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator operator++(int) - { - JhmLockIterator jit(_it, _lock); - ++_it; - _item = JhmLockItem(_it._iItem); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem& - */ - JhmLockItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem* - */ - JhmLockItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_HashMapCompact::lock_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmLockItem _item; - - /** - * é” - */ - JhmAutoLockPtr _lock; - }; - - typedef JhmLockIterator lock_iterator ; - - /////////////////////////////////////////////////////////////////// - /** - * é”, 用于éžé”迭代器 - * - */ - class JhmLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmLock(typename LockPolicy::Mutex &mutex) : _mutex(mutex) - { - } - - /** - * 获å–é” - * - * @return typename LockPolicy::Mutex - */ - typename LockPolicy::Mutex& mutex() - { - return _mutex; - } - protected: - //ä¸å®žçŽ° - JhmLock(const JhmLock &al); - JhmLock &operator=(const JhmLock &al); - - protected: - /** - * é” - */ - typename LockPolicy::Mutex &_mutex; - }; - - typedef TC_AutoPtr JhmLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmItem(const TC_HashMapCompact::HashMapItem &item, const JhmLockPtr &lock) - : _item(item), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmItem(const JhmItem &item) - : _item(item._item), _lock(item._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmItem& - */ - JhmItem& operator=(const JhmItem &item) - { - if(this != &item) - { - _item = item._item; - _lock = item._lock; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmItem& item) - { - return !((*this) == item); - } - - /** - * 获å–当å‰hash桶的所有数é‡, 注æ„åªèŽ·å–有key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * 如果å议解ç æœ‰é—®é¢˜ä¹Ÿä¸èŽ·å– - * @param - */ - void get(vector > &v) - { - vector vtData; - - { - TC_LockT lock(_lock->mutex()); - _item.get(vtData); - } - - for(size_t i = 0; i < vtData.size(); i++) - { - pair pk; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - pk.first.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - pk.second.readFrom(is); - - v.push_back(pk); - } - catch(exception &ex) - { - } - } - } - - /** - * 获å–当å‰hash桶的所有过期数æ®, 注æ„åªèŽ·å–有key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * 如果å议解ç æœ‰é—®é¢˜ä¹Ÿä¸èŽ·å– - * @param - */ - void getExpire(uint32_t t, vector > &v) - { - vector vtData; - - { - TC_LockT lock(_lock->mutex()); - _item.getExpire(t, vtData); - } - - for(size_t i = 0; i < vtData.size(); i++) - { - pair pk; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - pk.first.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - pk.second.readFrom(is); - - v.push_back(pk); - } - catch(exception &ex) - { - } - } - } - - /** - * 设置当å‰æ¡¶ä¸‹çš„所有数æ®ä¸ºè„æ•°æ® - * - * @param - * - * @return int - */ - int setDirty() - { - { - TC_LockT lock(_lock->mutex()); - return _item.setDirty(); - } - } - - protected: - TC_HashMapCompact::HashMapItem _item; - JhmLockPtr _lock; - }; - - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmIterator(const TC_HashMapCompact::hash_iterator &it, const JhmLockPtr &lock) - : _it(it), _item(it._iItem, lock), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmIterator(const JhmIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmIterator& - */ - JhmIterator& operator=(const JhmIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmIterator& - */ - JhmIterator& operator++() - { - TC_LockT lock(_lock->mutex()); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmIterator& - */ - JhmIterator operator++(int) - { - TC_LockT lock(_lock->mutex()); - JhmIterator jit(_it, _lock); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem& - */ - JhmItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem* - */ - JhmItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_HashMapCompact::hash_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmItem _item; - - /** - * é” - */ - JhmLockPtr _lock; - }; - - typedef JhmIterator hash_iterator ; - - //////////////////////////////////////////////////////////////////////////// - // - /** - * 构造函数 - */ - TarsHashMapCompact() - { - _todo_of = NULL; - } - - /** - * åˆå§‹åŒ–æ•°æ®å—å¹³å‡å¤§å° - * 表示内存分é…的时候,会分é…n个最å°å—, n个(最å°å¿«*增长因å­ï¼‰, n个(最å°å¿«*增长因å­*增长因å­ï¼‰..., 直到nä¸ªæœ€å¤§å— - * n是hashmap自己计算出æ¥çš„ - * è¿™ç§åˆ†é…策略通常是你数æ®å¿«è®°å½•å˜é•¿æ¯”较多的使用, 便于节约内存,如果数æ®è®°å½•åŸºæœ¬ä¸æ˜¯å˜é•¿çš„, 那最å°å—=最大快,增长因å­=1å°±å¯ä»¥äº† - * @param iMinDataSize: 最å°æ•°æ®å—å¤§å° - * @param iMaxDataSize: 最大数æ®å—å¤§å° - * @param fFactor: å¢žé•¿å› å­ >= 1.0 - */ - void initDataBlockSize(size_t iMinDataSize, size_t iMaxDataSize, float fFactor) - { - this->_t.initDataBlockSize(iMinDataSize, iMaxDataSize, fFactor); - } - - /** - * 设置hash比率(设置chunkæ•°æ®å—/hash项比值, 默认是2) - * 有需è¦æ›´æ”¹å¿…须在create之å‰è°ƒç”¨ - * - * @param fRadio - */ - void initHashRadio(float fRadio) { this->_t.initHashRadio(fRadio);} - - /** - * 设置hashæ–¹å¼ - * @param hash_of - */ - void setHashFunctor(TC_HashMapCompact::hash_functor hashf) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setHashFunctor(hashf); - } - - /** - * 获å–hashæ–¹å¼ - * - * @return TC_HashMapCompact::hash_functor& - */ - TC_HashMapCompact::hash_functor &getHashFunctor() { return this->_t.getHashFunctor(); } - - /** - * 设置淘汰æ“作类 - * @param erase_of - */ - void setToDoFunctor(ToDoFunctor *todo_of) { this->_todo_of = todo_of; } - - /** - * 获å–æ¯ç§å¤§å°å†…å­˜å—çš„å¤´éƒ¨ä¿¡æ¯ - * - * @return vector: ä¸åŒå¤§å°å†…å­˜å—å¤´éƒ¨ä¿¡æ¯ - */ - vector getBlockDetail() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getBlockDetail(); - } - - /** - * 所有block中chunk的个数 - * - * @return size_t - */ - size_t allBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.allBlockChunkCount(); - } - - /** - * æ¯ç§block中chunk的个数(ä¸åŒå¤§å°å†…å­˜å—的个数相åŒ) - * - * @return size_t - */ - vector singleBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.singleBlockChunkCount(); - } - - /** - * 获å–hash桶的个数 - * - * @return size_t - */ - size_t getHashCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getHashCount(); - } - - /** - * 元素的个数 - * - * @return size_t - */ - size_t size() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.size(); - } - - /** - * è„æ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t dirtyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.dirtyCount(); - } - - /** - * Onlyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t onlyKeyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.onlyKeyCount(); - } - - /** - * 设置æ¯æ¬¡æ·˜æ±°æ•°é‡ - * @param n - */ - void setEraseCount(size_t n) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseCount(n); - } - - /** - * 获å–æ¯æ¬¡æ·˜æ±°æ•°é‡ - * - * @return size_t - */ - size_t getEraseCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseCount(); - } - - /** - * 设置åªè¯» - * @param bReadOnly - */ - void setReadOnly(bool bReadOnly) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setReadOnly(bReadOnly); - } - - /** - * 是å¦åªè¯» - * - * @return bool - */ - bool isReadOnly() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isReadOnly(); - } - - /** - * 设置是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * @param bAutoErase - */ - void setAutoErase(bool bAutoErase) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setAutoErase(bAutoErase); - } - - /** - * 是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * - * @return bool - */ - bool isAutoErase() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isAutoErase(); - } - - /** - * è®¾ç½®æ·˜æ±°æ–¹å¼ - * TC_HashMapCompact::ERASEBYGET - * TC_HashMapCompact::ERASEBYSET - * @param cEraseMode - */ - void setEraseMode(char cEraseMode) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseMode(cEraseMode); - } - - /** - * 获å–æ·˜æ±°æ–¹å¼ - * - * @return bool - */ - char getEraseMode() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseMode(); - } - - /** - * å¤´éƒ¨ä¿¡æ¯ - * - * @return TC_HashMapCompact::tagMapHead - */ - TC_HashMapCompact::tagMapHead& getMapHead() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getMapHead(); - } - - /** - * 设置回写时间(秒) - * @param iSyncTime - */ - void setSyncTime(uint32_t iSyncTime) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setSyncTime(iSyncTime); - } - - /** - * 获å–回写时间 - * - * @return uint32_t - */ - uint32_t getSyncTime() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getSyncTime(); - } - - /** - * dump到文件 - * @param sFile - * @param bDoClear: 是å¦æ¸…空 - * @return int - * TC_HashMapCompact::RT_DUMP_FILE_ERR: dump到文件出错 - * TC_HashMapCompact::RT_OK: dump到文件æˆåŠŸ - */ - int dump2file(const string &sFile, bool bDoClear = false) - { - TC_LockT lock(LockPolicy::mutex()); - int ret = this->_t.dump2file(sFile); - if(ret != TC_HashMapCompact::RT_OK) - { - return ret; - } - - if(bDoClear) - this->_t.clear(); - - return ret; - } - - /** - * 从文件load - * @param sFile - * - * @return int - * TC_HashMapCompact::RT_LOAL_FILE_ERR: load出错 - * TC_HashMapCompact::RT_VERSION_MISMATCH_ERR: 版本ä¸ä¸€è‡´ - * TC_HashMapCompact::RT_OK: loadæˆåŠŸ - */ - int load5file(const string &sFile) - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.load5file(sFile); - } - - /** - * 清空hash map - * 所有map中的数æ®éƒ½è¢«æ¸…空 - */ - void clear() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.clear(); - } - - /** - * 检查数æ®çŠ¶æ€ - * @param k - * - * @return int - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMapCompact::RT_DIRTY_DATA: 是è„æ•°æ® - * TC_HashMapCompact::RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const K &k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.checkDirty(sk); - } - - /** - * 设置为干净数æ®i, 修改SET/GET时间链, 会导致数æ®ä¸å›žå†™ - * @param k - * - * @return int - * TC_HashMapCompact::RT_READONLY: åªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMapCompact::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setClean(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setClean(sk); - } - - /** - * 设置为è„æ•°æ®, 修改SET/GET时间链, 会导致数æ®å›žå†™ - * @param k - * @return int - * TC_HashMapCompact::RT_READONLY: åªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMapCompact::RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirty(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setDirty(sk); - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * (如果没设置自定义Get函数,没有数æ®æ—¶è¿”回:RT_NO_DATA) - * @param k - * @param v - * @param iSyncTime:æ•°æ®ä¸Šæ¬¡å›žå†™çš„时间, 没有缓写则为0 - * - * @return int: - * TC_HashMapCompact::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_HashMapCompact::RT_READONLY: åªè¯»æ¨¡å¼ - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMapCompact::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_HashMapCompact::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const K& k, V &v, uint32_t &iSyncTime, uint32_t& iExpireTime, uint8_t& iVersion) - { - iSyncTime = 0; - iExpireTime = 0; - iVersion = 1; - int ret = TC_HashMapCompact::RT_OK; - - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - string sv; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.get(sk, sv, iSyncTime, iExpireTime, iVersion); - } - - //读å–到数æ®äº†, 解包 - if(ret == TC_HashMapCompact::RT_OK) - { - tars::TarsInputStream is; - is.setBuffer(sv.c_str(), sv.length()); - v.readFrom(is); - - return ret; - } - - if(ret != TC_HashMapCompact::RT_NO_DATA || _todo_of == NULL) - { - return ret; - } - - //åªè¯»æ¨¡å¼ - if(isReadOnly()) - { - return TC_HashMapCompact::RT_READONLY; - } - - //获å–函数 - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - ret = _todo_of->get(stDataRecord); - if(ret == TC_HashMapCompact::RT_OK) - { - v = stDataRecord._value; - iExpireTime = stDataRecord._expiret; - return this->set(stDataRecord._key, stDataRecord._value, stDataRecord._expiret, stDataRecord._ver, stDataRecord._dirty); - } - else if(ret == TC_HashMapCompact::RT_NO_GET) - { - return TC_HashMapCompact::RT_NO_DATA; - } - else if(ret == TC_HashMapCompact::RT_NO_DATA) - { - iExpireTime = stDataRecord._expiret; - ret = this->set(stDataRecord._key); - if(ret == TC_HashMapCompact::RT_OK) - { - return TC_HashMapCompact::RT_ONLY_KEY; - } - return ret; - } - - return TC_HashMapCompact::RT_LOAD_DATA_ERR; - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * - * @return int: - * TC_HashMapCompact::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMapCompact::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_HashMapCompact::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const K& k, V &v) - { - uint32_t iSyncTime; - uint32_t iExpireTime; - uint8_t iVer; - return get(k, v, iSyncTime, iExpireTime, iVer); - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - template - int getHash(size_t h, vector > &vv, C c) - { - int ret = TC_HashMapCompact::RT_OK; - - TC_LockT lock(LockPolicy::mutex()); - - TC_HashMapCompact::FailureRecover check(&this->_t); - - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMapCompact::Block block(&this->_t, iAddr); - - while(block.getHead() != 0) - { - TC_HashMapCompact::BlockData data; - ret = block.getBlockData(data); - if(ret == TC_HashMapCompact::RT_OK) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - { - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - vv.push_back(make_pair(tk, tv)); - } - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - return TC_HashMapCompact::RT_OK; - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - template - int getHash(size_t h, vector &vv, C c) - { - int ret = TC_HashMapCompact::RT_OK; - - TC_LockT lock(LockPolicy::mutex()); - - TC_HashMapCompact::FailureRecover check(&this->_t); - - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMapCompact::Block block(&this->_t, iAddr); - - while(block.getHead() != 0) - { - TC_HashMapCompact::BlockData data; - ret = block.getBlockData(data); - if(ret == TC_HashMapCompact::RT_OK) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - { - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._ver = data._ver; - stDataRecord._dirty = data._dirty; - stDataRecord._expiret = data._expiret; - stDataRecord._iSyncTime = data._synct; - - vv.push_back(stDataRecord); - } - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - return TC_HashMapCompact::RT_OK; - } - - /** - * æ¢å¤æ•°æ® - * 对于block记录无法读å–çš„æ•°æ®è‡ªåŠ¨åˆ é™¤ - * @param bRepair: 是å¦ä¿®å¤ - * @return 返回删除的记录数 - */ - size_t recover(bool bRepair) - { - size_t c = this->_t.getHashCount(); - size_t e = 0; - for(size_t i = 0; i < c; i++) - { - TC_LockT lock(LockPolicy::mutex()); - - e += this->_t.recover(i, bRepair); - } - - return e; - } - - /** - * 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @param v: 值 - * @param bDirty: 是å¦æ˜¯è„æ•°æ® - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_HashMapCompact::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const K& k, const V& v, bool bDirty = true, uint32_t iExpireTime = 0, uint8_t iVersion = 0) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - tars::TarsOutputStream osv; - v.writeTo(osv); - string sv(osv.getBuffer(), osv.getLength()); - - int ret = TC_HashMapCompact::RT_OK; - vector vtData; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(sk, sv, iExpireTime, iVersion, bDirty, vtData); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtData.size(); i++) - { - K tk; - V tv; - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - tk.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = vtData[i]._dirty; - stDataRecord._iSyncTime = vtData[i]._synct; - stDataRecord._expiret = vtData[i]._expiret; - stDataRecord._ver = vtData[i]._ver; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * 仅设置Key, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_HashMapCompact::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const K& k, uint8_t iVersion = 0) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - int ret = TC_HashMapCompact::RT_OK; - vector vtData; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(sk, iVersion, vtData); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtData.size(); i++) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - tk.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = vtData[i]._dirty; - stDataRecord._iSyncTime = vtData[i]._synct; - stDataRecord._expiret = vtData[i]._expiret; - stDataRecord._ver = vtData[i]._ver; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * 无论cache是å¦æœ‰æ•°æ®,todoçš„del都被调用 - * - * @param k, 关键字 - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMapCompact::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int del(const K& k) - { - int ret = TC_HashMapCompact::RT_OK; - - TC_HashMapCompact::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_HashMapCompact::RT_OK && ret != TC_HashMapCompact::RT_ONLY_KEY && ret != TC_HashMapCompact::RT_NO_DATA) - { - return ret; - } - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - - if(ret == TC_HashMapCompact::RT_OK) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._value.c_str(), data._value.length()); - v.readFrom(is); - - stDataRecord._value = v; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - } - - _todo_of->del((ret == TC_HashMapCompact::RT_OK), stDataRecord); - } - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * cache有数æ®,todoçš„erase被调用 - * - * @param k, 关键字 - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMapCompact::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int erase(const K& k) - { - int ret = TC_HashMapCompact::RT_OK; - - TC_HashMapCompact::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_HashMapCompact::RT_OK) - { - return ret; - } - - if(_todo_of) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._value.c_str(), data._value.length()); - v.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - stDataRecord._value = v; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - - _todo_of->erase(stDataRecord); - } - return ret; - } - - /** - * 强制删除数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * - * @param k, 关键字 - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMapCompact::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int eraseByForce(const K& k) - { - int ret = TC_HashMapCompact::RT_OK; - - TC_HashMapCompact::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_HashMapCompact::RT_OK) - { - return ret; - } - - return ret; - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - - /** - * æ ¹æ®hash,强制删除相åŒhash值的所有数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param c, 匹é…仿函数: bool operator()(K v); - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMapCompact::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - template - int eraseHashByForce(size_t h, C c) - { - int ret = TC_HashMapCompact::RT_OK; - - vector vDelKey; - - TC_LockT lock(LockPolicy::mutex()); - - TC_HashMapCompact::FailureRecover check(&this->_t); - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMapCompact::Block block(&this->_t, iAddr); - TC_HashMapCompact::BlockData data; - - while(block.getHead() != 0) - { - ret = block.getBlockData(data); - if(ret == TC_HashMapCompact::RT_OK) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - vDelKey.push_back(data._key); - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - for(size_t i=0; i_t.del(vDelKey[i], data); - if(ret != TC_HashMapCompact::RT_OK) - { - return ret; - } - } - - return TC_HashMapCompact::RT_OK; - } - - /** - * æ ¹æ®key, 获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param vv - * @param c, 匹é…仿函数: bool operator()(K v); - * - * @return int, RT_OK - */ - - /** - * æ ¹æ®hash,强制删除相åŒhash值的所有数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h - * @param c, 匹é…仿函数: bool operator()(K v); - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_HashMapCompact::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - template - int eraseHashByForce(size_t h, C c, vector& vDelK) - { - int ret = TC_HashMapCompact::RT_OK; - - vector vDelKey; - - TC_LockT lock(LockPolicy::mutex()); - - TC_HashMapCompact::FailureRecover check(&this->_t); - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - - TC_HashMapCompact::Block block(&this->_t, iAddr); - TC_HashMapCompact::BlockData data; - - while(block.getHead() != 0) - { - ret = block.getBlockData(data); - if(ret == TC_HashMapCompact::RT_OK) - { - K tk; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(c(tk)) - { - vDelKey.push_back(data._key); - vDelK.push_back(tk); - } - } - catch(exception &ex) - { - } - } - if(!block.nextBlock()) - { - break; - } - } - - for(size_t i=0; i_t.del(vDelKey[i], data); - if(ret != TC_HashMapCompact::RT_OK) - { - vDelK.resize(i); - return ret; - } - } - - return TC_HashMapCompact::RT_OK; - } - - /** - * 淘汰数æ®, æ ¹æ®Get时间淘汰 - * 直到: 元素个数/chunks * 100 < radio,bCheckDirty 为true时,é‡åˆ°è„æ•°æ®åˆ™æ·˜æ±°ç»“æŸ - * @param radio: 共享内存chunks使用比例 0< radio < 100 - * @return int: - * TC_HashMapCompact::RT_READONLY: mapåªè¯» - * TC_HashMapCompact::RT_OK:淘汰完毕 - */ - int erase(int radio, bool bCheckDirty = false) - { - while(true) - { - int ret; - TC_HashMapCompact::BlockData data; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.erase(radio, data, bCheckDirty); - if(ret == TC_HashMapCompact::RT_OK || ret == TC_HashMapCompact::RT_READONLY) - { - return ret; - } - - if(ret != TC_HashMapCompact::RT_ERASE_OK) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - - _todo_of->erase(stDataRecord); - } - } - return TC_HashMapCompact::RT_OK; - } - - /** - * 回写å•æ¡è®°å½•, 如果记录ä¸å­˜åœ¨, 则ä¸åšä»»ä½•å¤„ç† - * @param k - * - * @return int - * TC_HashMapCompact::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_HashMapCompact::RT_ONLY_KEY:åªæœ‰Key - * TC_HashMapCompact::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_HashMapCompact::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int sync(const K& k) - { - V v; - uint32_t iSyncTime; - uint32_t iExpireTime; - uint8_t iVersion; - int ret = get(k, v, iSyncTime, iExpireTime, iVersion); - - if(ret == TC_HashMapCompact::RT_OK) - { - bool bDirty = (checkDirty(k) == TC_HashMapCompact::RT_DIRTY_DATA); - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - stDataRecord._value = v; - stDataRecord._dirty = bDirty; - stDataRecord._iSyncTime = iSyncTime; - stDataRecord._expiret = iExpireTime; - stDataRecord._ver = iVersion; - - _todo_of->sync(stDataRecord); - } - } - - return ret; - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å…¨éƒ¨å›žå†™ - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - * - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_HashMapCompact::RT_OK: 回写完毕了 - */ - int sync(uint32_t iNowTime) - { - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - while(true) - { - TC_HashMapCompact::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_HashMapCompact::RT_OK) - { - return ret; - } - - if(ret != TC_HashMapCompact::RT_NEED_SYNC) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - - _todo_of->sync(stDataRecord); - } - } - - return TC_HashMapCompact::RT_OK; - } - - /** - *å°†è„æ•°æ®å°¾æŒ‡é’ˆèµ‹ç»™å›žå†™å°¾æŒ‡é’ˆ - */ - void sync() - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å›žå†™,åªå›žå†™ä¸€ä¸ªè„æ•°æ®ï¼Œç›®çš„是替代int sync(uint32_t iNowTime) - * 方法,把由业务控制æ¯æ¬¡å›žå†™æ•°æ®é‡ï¼Œä½¿ç”¨æ—¶åº”该先调用void sync() - * 注æ„:cæ¡ä»¶å¯¹è±¡æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @param c: æ¡ä»¶ä»¿å‡½æ•°: bool operator()(K v); - * @return int: - * TC_HashMapCompact::RT_OK: 回写完毕了 - * - * 示例: - * p->sync(); - * while(true) { - * int iRet = pthis->SyncOnce(tNow); - * if( iRet == TC_HashMapCompact::RT_OK ) - * break; - * } - */ - template - int syncOnce(uint32_t iNowTime, C c) - { - TC_HashMapCompact::BlockData data; - - K tk; - tars::TarsInputStream is; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_HashMapCompact::RT_OK) - { - return ret; - } - - if(ret != TC_HashMapCompact::RT_NEED_SYNC) - { - return ret; - } - - if(_todo_of) - { - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - if(!c(tk)) - { - this->_t.setDirtyAfterSync(data._key); - return ret; - } - } - else - { - return ret; - } - } - - V tv; - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - - _todo_of->sync(stDataRecord); - - return ret; - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å›žå†™,åªå›žå†™ä¸€ä¸ªè„æ•°æ®ï¼Œç›®çš„是替代int sync(uint32_t iNowTime) - * 方法,把由业务控制æ¯æ¬¡å›žå†™æ•°æ®é‡ï¼Œä½¿ç”¨æ—¶åº”该先调用void sync() - * - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_HashMapCompact::RT_OK: 回写完毕了 - * - * 示例: - * p->sync(); - * while(true) { - * int iRet = pthis->SyncOnce(tNow); - * if( iRet == TC_HashMapCompact::RT_OK ) - * break; - * } - */ - int syncOnce(uint32_t iNowTime) - { - - - TC_HashMapCompact::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_HashMapCompact::RT_OK) - { - return ret; - } - - if(ret != TC_HashMapCompact::RT_NEED_SYNC) - { - return ret; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - - _todo_of->sync(stDataRecord); - } - - return ret; - } - /** - * å¤‡ä»½æ•°æ® - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å¤‡ä»½ - * å¯ä»¥å¤šä¸ªçº¿ç¨‹/进程备份数æ®,åŒæ—¶å¤‡ä»½æ—¶bForceFromBegin设置为false效率更高 - * - * @param bForceFromBegin: 是å¦å¼ºåˆ¶é‡å¤´å¼€å§‹å¤‡ä»½, 通常为false - * @return int: - * TC_HashMapCompact::RT_OK: 备份OK了 - */ - int backup(bool bForceFromBegin = false) - { - { - //开始准备备份 - TC_LockT lock(LockPolicy::mutex()); - this->_t.backup(bForceFromBegin); - } - - while(true) - { - TC_HashMapCompact::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.backup(data); - if(ret == TC_HashMapCompact::RT_OK) - { - return ret; - } - - if(ret != TC_HashMapCompact::RT_NEED_BACKUP) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - stDataRecord._expiret = data._expiret; - stDataRecord._ver = data._ver; - - _todo_of->backup(stDataRecord); - } - } - - return TC_HashMapCompact::RT_OK; - } - - - - /** - * æè¿° - * - * @return string - */ - string desc() { return this->_t.desc(); } - - /////////////////////////////////////////////////////////////////////////////// - /** - * 尾部 - * - * @return lock_iterator - */ - lock_iterator end() - { - JhmAutoLockPtr jlock; - return JhmLockIterator(this->_t.end(), jlock); - } - - /** - * æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * @param k - */ - lock_iterator find(const K& k) - { - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.find(sk), jlock); - } - - /** - * blockæ­£åº - * - * @return lock_iterator - */ - lock_iterator begin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.begin(), jlock); - } - - /** - * blocké€†åº - * - * @return lock_iterator - */ - lock_iterator rbegin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbegin(), jlock); - } - - /** - * 以Set时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * - * @return lock_iterator - */ - lock_iterator beginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginSetTime(), jlock); - } - - /** - * Set时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Set-->最近Set - * - * @return lock_iterator - */ - lock_iterator rbeginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginSetTime(), jlock); - } - - /** - * 以Get时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Get-->最久Get - * - * @return lock_iterator - */ - lock_iterator beginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginGetTime(), jlock); - } - - /** - * Get时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Get-->最近Get - * - * @return lock_iterator - */ - lock_iterator rbeginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginGetTime(), jlock); - } - - /** - * 获å–è„链表尾部迭代器(最长时间没有Setçš„è„æ•°æ®) - * - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * å¯èƒ½å­˜åœ¨å¹²å‡€æ•°æ® - * - * @return lock_iterator - */ - lock_iterator beginDirty() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginDirty(), jlock); - } - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, ä¸éœ€è¦å¯¹mapåŠ é” - - /** - * æ ¹æ®hash桶é历 - * - * @return hash_iterator - */ - hash_iterator hashBegin() - { - JhmLockPtr jlock(new JhmLock(this->mutex())); - TC_LockT lock(LockPolicy::mutex()); - return JhmIterator(this->_t.hashBegin(), jlock); - } - - /** - * ç»“æŸ - * - * @return - */ - hash_iterator hashEnd() - { - JhmLockPtr jlock; - return JhmIterator(this->_t.hashEnd(), jlock); - } - -protected: - - /** - * 删除数æ®çš„函数对象 - */ - ToDoFunctor *_todo_of; -}; - -} - -#endif diff --git a/cpp/servant/jmem/jmem_multi_hashmap.h b/cpp/servant/jmem/jmem_multi_hashmap.h deleted file mode 100644 index ed7d451a7..000000000 --- a/cpp/servant/jmem/jmem_multi_hashmap.h +++ /dev/null @@ -1,2736 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _JMEM_MULTI_HASHMAP_H -#define _JMEM_MULTI_HASHMAP_H - -#include "util/tc_multi_hashmap.h" -#include "util/tc_autoptr.h" -#include "jmem/jmem_policy.h" -#include "tup/Tars.h" - -namespace tars -{ - -/************************************************************************ - 基本说明如下: - 基于Tarså议的支æŒå¤škey的内存hashmap - 编解ç å‡ºé”™åˆ™æŠ›å‡ºTarsDecodeExceptionå’ŒTarsEncodeException - å¯ä»¥å¯¹é”策略和存储策略进行组åˆ, 例如: - 基于信å·é‡é”, 文件存储的多key hashmap: - TarsMultiHashMap - 基于信å·é‡é”, 共享内存存储的多key hashmap - TarsMultiHashMap - 基于线程é”, 内存存储的多key hashmap - TarsMultiHashMap - - 使用上, ä¸åŒçš„组åˆ, åˆå§‹åŒ–函数ä¸å®Œå…¨ä¸€æ · - åˆå§‹åŒ–函数有: - SemLockPolicy::initLock(key_t) - ShmStorePolicy::initStore(key_t, size_t) - FileStorePolicy::initStore(const char *file, size_t) - ç­‰, 具体å‚è§jmem_policy.h - - *********************************************************************** - - 基本特性说明: - > 内存数æ®çš„map, æ ¹æ®æœ€åŽGet时间的顺åºæ·˜æ±°æ•°æ®; - > 支æŒç¼“写/dump到文件/在线备份; - > 支æŒä¸åŒå¤§å°å†…å­˜å—çš„é…ç½®, æ供内存的使用率; - > 支æŒå›žæ”¶åˆ°æŒ‡å®šç©ºé—²æ¯”率的空间; - > 支æŒä»…设置Keyçš„æ“作, å³æ•°æ®æ— value, åªæœ‰Key, 类似与stl::set; - > 支æŒè‡ªå®šä¹‰hash算法; - > hashæ•°å¯ä»¥æ ¹æ®å†…å­˜å—比率设置, 并优化有素数, æ高hash的散列性; - > 支æŒå‡ ç§æ–¹å¼çš„é历, 通常é历时需è¦å¯¹map加é”; - > 对于hashæ–¹å¼çš„é历, é历时å¯ä»¥ä¸éœ€è¦å¯¹map加é”, 推è使用; - > 支æŒè‡ªå®šä¹‰æ“作对象设置, å¯ä»¥éžå¸¸å¿«é€Ÿå®žçŽ°ç›¸å…³çš„接å£; - > 支æŒè‡ªåŠ¨ç¼–解ç , Keyå’ŒValue的结构都通过tars2cpp生æˆ; - > tarså议支æŒè‡ªåŠ¨æ‰©å±•å­—段, 因此该hashmap支æŒè‡ªåŠ¨æ‰©å±•å­—段(Keyå’ŒValue都必须是通过tarsç¼–ç çš„); - > map支æŒåªè¯»æ¨¡å¼, åªè¯»æ¨¡å¼ä¸‹set/erase/del等修改数æ®çš„æ“作ä¸èƒ½ä½¿ç”¨, get/回写/在线备份正常使用 - > 支æŒè‡ªåŠ¨æ·˜æ±°, setæ—¶, 内存满则自动淘汰, 在éžè‡ªåŠ¨æ·˜æ±°æ—¶, 内存满直接返回RT_READONLY - > 对于mmap文件, 支æŒè‡ªåŠ¨æ‰©å±•æ–‡ä»¶, å³å†…å­˜ä¸å¤Ÿäº†, å¯ä»¥è‡ªåŠ¨æ‰©å±•æ–‡ä»¶å¤§å°(注æ„hashçš„æ•°é‡ä¸å˜, 因此开始就需è¦è€ƒè™‘hashçš„æ•°é‡), 而且ä¸èƒ½è·¨JHashmap对象(å³ä¸¤ä¸ªhashmap对象访问åŒä¸€å—文件,通知一个hashmap扩展以åŽï¼Œå¦å¤–一个对象并ä¸çŸ¥é“扩展了) - - *********************************************************************** - - hashmap链说明: - hashmap链一共包括了如下几ç§é“¾è¡¨: - > Set时间链: 任何Setæ“作都会修改该链表, SetåŽæ•°æ®è¢«è®¾ç½®ä¸ºè„æ•°æ®, 且移动到Set链表头部; - > Get时间链: 任何Getæ“作都会修改该链表, 除éžé“¾è¡¨åªè¯», 注æ„Set链åŒæ—¶ä¹Ÿä¼šä¿®æ”¹Get链 - > Dirty时间链: dirty链是Set链的一部分, 用于回写数æ®ç”¨ - > Backup链:备份链是Get链的一部分, 当备份数æ®æ—¶, 顺ç€Get链从尾部到头部开始备份; - - *********************************************************************** - - 相关æ“作说明: - > å¯ä»¥è®¾ç½®mapåªè¯», 则所有写æ“作返回RT_READONLY, 此时Getæ“作ä¸ä¿®æ”¹é“¾è¡¨ - > å¯ä»¥è®¾ç½®çŸ¥å¦è‡ªåŠ¨æ·˜æ±°, 默认是自动淘汰的.如果ä¸è‡ªåŠ¨æ·˜æ±°,则setæ—¶,无内存空间返回:RT_NO_MEMORY - > å¯ä»¥æ›´æ”¹hash的算法, 调用setHashFunctorå³å¯ - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºå¹²å‡€, 此时移出到Dirty链表指Dirty尾部的下一个元素; - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºè„, 此时会移动到Set链表头部; - > æ¯ä¸ªæ•°æ®éƒ½æœ‰ä¸€ä¸ªä¸Šæ¬¡å›žå†™æ—¶é—´(SyncTime), 如果å¯åŠ¨å›žå†™æ“作, 则在一定时间内会回写; - > å¯ä»¥dump到文件或者从文件中load, 这个时候会对mapåŠ é” - > å¯ä»¥è°ƒç”¨erase批é‡æ·˜æ±°æ•°æ®ç›´åˆ°å†…存空闲率到一定比例 - > å¯ä»¥è°ƒç”¨sync进行数æ®å›žå†™, ä¿è¯ä¸€å®šæ—¶é—´å†…没有回写的数æ®ä¼šå›žå†™, map回写时间通过setSyncTime设置, 默认10分钟 - > å¯ä»¥setToDoFunctor设置æ“作类, 以下是æ“作触å‘的情况: - - *********************************************************************** - - ToDoFunctor的函数说明: - > 通常继承ToDoFunctor, 实现相关函数就å¯ä»¥äº†, å¯ä»¥å®žçŽ°ä»¥ä¸‹åŠŸèƒ½:Getæ•°æ®, 淘汰数æ®, 删除数æ®, 回写数æ®, å¤‡ä»½æ•°æ® - > ToDoFunctor::erase, 当调用map.eraseæ—¶, 该函数会被调用 - > ToDoFunctor::del, 当调用map.delæ—¶, 该函数会被调用, 注æ„删除时数æ®å¯èƒ½éƒ½ä¸åœ¨cache中; - > ToDoFunctor::sync, 当调用map.syncæ—¶, 会触å‘æ¯æ¡éœ€è¦å›žå†™çš„æ•°æ®è¯¥å‡½æ•°éƒ½è¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å›žå†™è¯·æ±‚; - > ToDoFunctor::backup, 当调用map.backupæ—¶, 会触å‘需è¦å¤‡ä»½çš„æ•°æ®è¯¥å‡½æ•°ä¼šè¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å¤‡ä»½è¯·æ±‚; - > ToDoFunctor::get, 当调用map.getæ—¶, 如果map中无数æ®, 则该函数被调用, 该函数从db中获å–æ•°æ®, 并返回RT_OK, 如果dbæ— æ•°æ®åˆ™è¿”回RT_NO_DATA; - > ToDoFunctor所有接å£è¢«è°ƒç”¨æ—¶, 都ä¸ä¼šå¯¹map加é”, å› æ­¤å¯ä»¥æ“作map - - *********************************************************************** - - mapçš„é‡è¦å‡½æ•°è¯´æ˜Ž: - > set, 设置数æ®åˆ°map中, 会更新set链表 - 如果满了, 且å¯ä»¥è‡ªåŠ¨æ·˜æ±°, 则根æ®Get链淘汰数æ®, 此时ToDoFunctorçš„sync会被调用 - 如果满了, 且å¯ä»¥ä¸èƒ½è‡ªåŠ¨æ·˜æ±°, 则返回RT_NO_MEMORY - - > get, 从map获å–æ•°æ®, 如果有数æ®, 则直接从map获å–æ•°æ®å¹¶è¿”回RT_OK; - 如果没有数æ®, 则调用ToDoFunctor::get函数, 此时get函数需è¦è¿”回RT_OK, åŒæ—¶ä¼šè®¾ç½®åˆ°map中, 并返回数æ®; - 如果没有数æ®, 则ToDoFunctor::get函数也无数æ®, 则需è¦è¿”回RT_NO_DATA, 此时åªä¼šæŠŠKey设置到map中, 并返回RT_ONLY_KEY; - 在上é¢æƒ…况下, 如果å†æœ‰get请求, 则ä¸å†è°ƒç”¨ToDoFunctor::get, 直接返回RT_ONLY_KEY; - - > del, 删除数æ®, 无论cache是å¦æœ‰æ•°æ®, ToDoFunctor::del都会被调用; - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«åˆ é™¤; - - > erase, 淘汰数æ®, åªæœ‰cache存在数æ®, ToDoFunctor::eraseæ‰ä¼šè¢«è°ƒç”¨ - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«æ·˜æ±°, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > erase(int ratio), 批é‡æ·˜æ±°æ•°æ®, 直到空闲å—比率到达ratio; - ToDoFunctor::erase会被调用; - åªæœ‰Key的记录也会被淘汰, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > sync: 缓写数æ®, 超时没有回写且是è„æ•°æ®éœ€è¦å›žå†™, 回写完毕åŽ, æ•°æ®ä¼šè‡ªåŠ¨è®¾ç½®ä¸ºå¹²å‡€æ•°æ®; - å¯ä»¥å¤šä¸ªçº¿ç¨‹æˆ–进程åŒæ—¶ç¼“写; - ToDoFunctor::sync会被调用; - åªæœ‰Key的记录, ToDoFunctor::syncä¸ä¼šè¢«è°ƒç”¨; - - > backup: 备份数æ®, 顺ç€é¡ºç€Get链从尾部到头部开始备份; - ToDoFunctor::backup会被调用; - åªæœ‰Key的记录, ToDoFunctor::backupä¸ä¼šè¢«è°ƒç”¨; - 由于备份游标åªæœ‰ä¸€ä¸ª, 因此多个进程åŒæ—¶å¤‡ä»½çš„时候数æ®å¯èƒ½ä¼šæ¯ä¸ªè¿›ç¨‹æœ‰ä¸€éƒ¨åˆ† - 如果备份程åºå¤‡ä»½åˆ°ä¸€åŠdown了, 则下次å¯åŠ¨å¤‡ä»½æ—¶ä¼šæŽ¥ç€ä¸Šæ¬¡çš„备份进行, 除éžå°†backup(true)调用备份 - - *********************************************************************** - - 返回值说明: - > 注æ„函数所有int的返回值, 如无特别说明, 请å‚è§TC_Multi_HashMap::RT_ - - *********************************************************************** - - é历说明: - > å¯ä»¥ç”¨lock_iterator对map进行以下几ç§é历, 在é历过程中其实对map加é”处ç†äº† - > end(): 迭代器尾部 - > begin(): 按照block区å—é历 - > rbegin():按照block区å—逆åºé历 - > beginSetTime(): 按照Set时间顺åºé历 - > rbeginSetTime(): 按照Set时间顺åºé历 - > beginGetTime(): 按照Get时间顺åºé历 - > rbeginGetTime(): 按照Get时间逆åºé历 - > beginDirty(): 按时间逆åºé历è„æ•°æ®é“¾(如果setClean, 则也å¯èƒ½åœ¨è„链表上) - > 其实回写数æ®é“¾æ˜¯è„æ•°æ®é‡çš„å­é›† - > 注æ„:lock_iterator一旦获å–, 就对map加é”了, 直到lock_iteratoræžå¤Ÿä¸ºæ­¢ - > - > å¯ä»¥ç”¨hash_iterator对map进行é历, é历过程中对map没有加é”, 推è使用 - > hashBegin(): 获å–hashé历迭代器 - > hashEnd(): hashé历尾部迭代器 - > 注æ„:hash_iterator对应的其实是一个hash桶链, æ¯æ¬¡èŽ·å–æ•°æ®å…¶å®žä¼šèŽ·å–桶链上é¢çš„æ‰€æœ‰æ•°æ® -*/ - -template class StorePolicy> -class TarsMultiHashMap : public StorePolicy -{ -public: - /** - * 返回数æ®ç»“æž„ - */ - struct Value - { - MK _mkey; - UK _ukey; - V _value; - bool _dirty; - uint8_t _iVersion; // å–值范围为1-255,0为特殊值,表示ä¸æ£€æŸ¥ç‰ˆæœ¬ã€‚循环使用 - time_t _iSyncTime; - Value() : _dirty(true), _iVersion(1), _iSyncTime(0) - { - } - }; - /** - * 定义数æ®æ“作基类 - * 获å–,é历,删除,淘汰时都å¯ä»¥ä½¿ç”¨è¯¥æ“作类 - */ - class ToDoFunctor - { - public: - /** - * æ•°æ®è®°å½• - */ - typedef Value DataRecord; // 兼容è€ç‰ˆæœ¬çš„åå­— - - /** - * æžå¤Ÿ - */ - virtual ~ToDoFunctor(){}; - - /** - * æ·˜æ±°æ•°æ® - * @param stDataRecord: è¢«æ·˜æ±°çš„æ•°æ® - */ - virtual void erase(const DataRecord &stDataRecord){}; - - /** - * åˆ é™¤æ•°æ® - * @param bExists: 是å¦å­˜åœ¨æ•°æ® - * @param stDataRecord: æ•°æ®, bExists==true时有效, å¦åˆ™åªæœ‰key有效 - */ - virtual void del(bool bExists, const DataRecord &stDataRecord){}; - - /** - * å›žå†™æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void sync(const DataRecord &stDataRecord){}; - - /** - * å¤‡ä»½æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void backup(const DataRecord &stDataRecord){}; - - /** - * 获å–æ•°æ®, 默认返回RT_NO_GET - * stDataRecord中_key有效, 其他数æ®éœ€è¦è¿”回 - * @param stDataRecord: 需è¦èŽ·å–çš„æ•°æ® - * - * @return int, 获å–到数æ®, 返回:TC_Multi_HashMap::RT_OK - * 没有数æ®,返回:TC_Multi_HashMap::RT_NO_DATA, - * 系统默认GET,返回:TC_Multi_HashMap::RT_NO_GET - * 其他,则返回:TC_Multi_HashMap::RT_LOAD_DATA_ERR - */ - virtual int get(DataRecord &stDataRecord) - { - return TC_Multi_HashMap::RT_NO_GET; - } - - /** - * æ ¹æ®ä¸»key获å–æ•°æ®ï¼Œé»˜è®¤è¿”回RT_NO_GET - * mk,需è¦èŽ·å–æ•°æ®çš„主key - * vtRecordsï¼Œè¿”å›žçš„æ•°æ® - */ - virtual int get(MK mk, vector& vtRecords) - { - return TC_Multi_HashMap::RT_NO_GET; - } - }; - - /////////////////////////////////////////////////////////////////// - /** - * 自动é”, 用于迭代器 - */ - class JhmAutoLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmAutoLock(typename LockPolicy::Mutex &mutex) : _lock(mutex) - { - } - - protected: - //ä¸å®žçŽ° - JhmAutoLock(const JhmAutoLock &al); - JhmAutoLock &operator=(const JhmAutoLock &al); - - protected: - /** - * é” - */ - TC_LockT _lock; - }; - - typedef TC_AutoPtr JhmAutoLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmLockItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmLockItem(const TC_Multi_HashMap::HashMapLockItem &item) - : _item(item) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockItem(const JhmLockItem &item) - : _item(item._item) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockItem& - */ - JhmLockItem& operator=(const JhmLockItem &item) - { - if(this != &item) - { - _item = item._item; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmLockItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmLockItem& item) - { - return !((*this) == item); - } - - /** - * 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty() { return _item.isDirty(); } - - /** - * 是å¦åªæœ‰Key - * - * @return bool - */ - bool isOnlyKey() { return _item.isOnlyKey(); } - - /** - * 最åŽå›žå†™æ—¶é—´ - * - * @return time_t - */ - time_t getSyncTime() { return _item.getSyncTime(); } - - /** - * 获å–当å‰itemçš„key - * @param mk, 主key - * @param uk, 除主key外的è”åˆä¸»é”® - * @return int - * TC_Multi_HashMap::RT_OK:æ•°æ®èŽ·å–OK - * 其他值, 异常 - */ - int get(MK &mk, UK &uk) - { - string smk, suk; - int ret = _item.get(smk, suk); - if(ret != TC_Multi_HashMap::RT_OK) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(smk.c_str(), smk.length()); - mk.readFrom(is); - is.setBuffer(suk.c_str(), suk.length()); - uk.readFrom(is); - - return ret; - } - - /** - * 获å–值当å‰itemçš„value(å«key) - * @param v - * @return int - * TC_Multi_HashMap::RT_OK:æ•°æ®èŽ·å–OK - * TC_Multi_HashMap::RT_ONLY_KEY: key有效, v无效为空 - * 其他值, 异常 - */ - int get(Value &v) - { - TC_Multi_HashMap::Value hv; - int ret = _item.get(hv); - if(ret != TC_Multi_HashMap::RT_OK && ret != TC_Multi_HashMap::RT_ONLY_KEY) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(hv._mkey.c_str(), hv._mkey.length()); - v._mkey.readFrom(is); - is.setBuffer(hv._data._key.c_str(), hv._data._key.length()); - v._ukey.readFrom(is); - if(ret != TC_Multi_HashMap::RT_ONLY_KEY) - { - is.setBuffer(hv._data._value.c_str(), hv._data._value.length()); - v._value.readFrom(is); - } - - v._dirty = hv._data._dirty; - v._iVersion = hv._data._iVersion; - v._iSyncTime = hv._data._synct; - - return ret; - } - - protected: - TC_Multi_HashMap::HashMapLockItem _item; - }; - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmLockIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmLockIterator(const TC_Multi_HashMap::lock_iterator it, const JhmAutoLockPtr &lock) - : _it(it), _item(it._iItem), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockIterator(const JhmLockIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator=(const JhmLockIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - _lock = it._lock; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmLockIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmLockIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator++() - { - ++_it; - _item = JhmLockItem(_it._iItem); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator operator++(int) - { - JhmLockIterator jit(_it, _lock); - ++_it; - _item = JhmLockItem(_it._iItem); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem& - */ - JhmLockItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem* - */ - JhmLockItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_Multi_HashMap::lock_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmLockItem _item; - - /** - * é” - */ - JhmAutoLockPtr _lock; - }; - - typedef JhmLockIterator lock_iterator ; - - /////////////////////////////////////////////////////////////////// - /** - * é”, 用于éžé”迭代器 - * - */ - class JhmLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmLock(typename LockPolicy::Mutex &mutex) : _mutex(mutex) - { - } - - /** - * 获å–é” - * - * @return typename LockPolicy::Mutex - */ - typename LockPolicy::Mutex& mutex() - { - return _mutex; - } - protected: - //ä¸å®žçŽ° - JhmLock(const JhmLock &al); - JhmLock &operator=(const JhmLock &al); - - protected: - /** - * é” - */ - typename LockPolicy::Mutex &_mutex; - }; - - typedef TC_AutoPtr JhmLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmItem(const TC_Multi_HashMap::HashMapItem &item, const JhmLockPtr &lock) - : _item(item), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmItem(const JhmItem &item) - : _item(item._item), _lock(item._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmItem& - */ - JhmItem& operator=(const JhmItem &item) - { - if(this != &item) - { - _item = item._item; - _lock = item._lock; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmItem& item) - { - return !((*this) == item); - } - - /** - * 获å–当å‰hash桶的所有数æ®, 注æ„åªèŽ·å–有key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * 如果å议解ç æœ‰é—®é¢˜ä¹Ÿä¸èŽ·å– - * @param vs, - */ - void get(vector &vs) - { - vector vtData; - - { - TC_LockT lock(_lock->mutex()); - _item.get(vtData); - } - - for(size_t i = 0; i < vtData.size(); i++) - { - - try - { - Value v; - - tars::TarsInputStream is; - is.setBuffer(vtData[i]._mkey.c_str(), vtData[i]._mkey.length()); - v._mkey.readFrom(is); - - is.setBuffer(vtData[i]._data._key.c_str(), vtData[i]._data._key.length()); - v._ukey.readFrom(is); - - is.setBuffer(vtData[i]._data._value.c_str(), vtData[i]._data._value.length()); - v._value.readFrom(is); - - v._iVersion = vtData[i]._data._iVersion; - v._dirty = vtData[i]._data._dirty; - v._iSyncTime = vtData[i]._data._synct; - - vs.push_back(v); - } - catch(exception &ex) - { - } - } - } - - protected: - TC_Multi_HashMap::HashMapItem _item; - JhmLockPtr _lock; - }; - - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmIterator(const TC_Multi_HashMap::hash_iterator &it, const JhmLockPtr &lock) - : _it(it), _item(it._iItem, lock), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmIterator(const JhmIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmIterator& - */ - JhmIterator& operator=(const JhmIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmIterator& - */ - JhmIterator& operator++() - { - TC_LockT lock(_lock->mutex()); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmIterator& - */ - JhmIterator operator++(int) - { - TC_LockT lock(_lock->mutex()); - JhmIterator jit(_it, _lock); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem& - */ - JhmItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem* - */ - JhmItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_Multi_HashMap::hash_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmItem _item; - - /** - * é” - */ - JhmLockPtr _lock; - }; - - typedef JhmIterator hash_iterator ; - - //////////////////////////////////////////////////////////////////////////// - // - /** - * 构造函数 - */ - TarsMultiHashMap() - { - _todo_of = NULL; - } - - /** - * åˆå§‹åŒ–æ•°æ®å—å¹³å‡å¤§å° - * 表示内存分é…的时候,会分é…n个最å°å—, n个(最å°å¿«*增长因å­ï¼‰, n个(最å°å¿«*增长因å­*增长因å­ï¼‰..., 直到nä¸ªæœ€å¤§å— - * n是hashmap自己计算出æ¥çš„ - * è¿™ç§åˆ†é…策略通常是你数æ®å¿«è®°å½•å˜é•¿æ¯”较多的使用, 便于节约内存,如果数æ®è®°å½•åŸºæœ¬ä¸æ˜¯å˜é•¿çš„, 那最å°å—=最大快,增长因å­=1å°±å¯ä»¥äº† - * @param iMinDataSize: 最å°æ•°æ®å—å¤§å° - * @param iMaxDataSize: 最大数æ®å—å¤§å° - * @param fFactor: å¢žé•¿å› å­ >= 1.0 - */ - void initDataBlockSize(size_t iMinDataSize, size_t iMaxDataSize, float fFactor) - { - this->_t.initDataBlockSize(iMinDataSize, iMaxDataSize, fFactor); - } - - /** - * 设置hash比率(设置chunkæ•°æ®å—/hash项比值, 默认是2) - * 有需è¦æ›´æ”¹å¿…须在create之å‰è°ƒç”¨ - * - * @param fratio - */ - void initHashRatio(float fratio) { this->_t.initHashRatio(fratio);} - - /** - * åˆå§‹åŒ–chunk个数/主key hash个数, 默认是1, å«ä¹‰æ˜¯ä¸€ä¸ªä¸»key下é¢å¤§æ¦‚有多个æ¡æ•°æ® - * 有需è¦æ›´æ”¹å¿…须在create之å‰è°ƒç”¨ - * - * @param fratio - */ - void initMainKeyHashRatio(float fratio) { this->_t.initMainKeyHashRatio(fratio);} - - /** - * 设置hashæ–¹å¼ï¼Œè¿™ä¸ªhash函数将作为è”åˆä¸»é”®çš„hash函数 - * @param hash_of - */ - void setHashFunctor(TC_Multi_HashMap::hash_functor hashf) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setHashFunctor(hashf); - } - - /** - * 设置主keyçš„hashæ–¹å¼ï¼Œå¦‚æžœä¸è®¾ï¼Œä¸»key将使用上é¢çš„è”åˆä¸»é”®çš„hash函数 - * @param hash_of - */ - void setHashFunctorM(TC_Multi_HashMap::hash_functor hashf) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setHashFunctorM(hashf); - } - - /** - * 获å–hashæ–¹å¼ - * - * @return TC_Multi_HashMap::hash_functor& - */ - TC_Multi_HashMap::hash_functor &getHashFunctor() { return this->_t.getHashFunctor(); } - - /* 获å–主key hashæ–¹å¼ - * - * @return TC_Multi_HashMap::hash_functor& - */ - TC_Multi_HashMap::hash_functor &getHashFunctorM() { return this->_t.getHashFunctorM(); } - - /** - * 设置淘汰æ“作类 - * @param erase_of - */ - void setToDoFunctor(ToDoFunctor *todo_of) { this->_todo_of = todo_of; } - - /** - * 获å–æ¯ç§å¤§å°å†…å­˜å—çš„å¤´éƒ¨ä¿¡æ¯ - * - * @return vector: ä¸åŒå¤§å°å†…å­˜å—å¤´éƒ¨ä¿¡æ¯ - */ - vector getBlockDetail() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getBlockDetail(); - } - - /** - * 所有block中chunk的个数 - * - * @return size_t - */ - size_t allBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.allBlockChunkCount(); - } - - /** - * æ¯ç§block中chunk的个数(ä¸åŒå¤§å°å†…å­˜å—的个数相åŒ) - * - * @return size_t - */ - vector singleBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.singleBlockChunkCount(); - } - - /** - * 获å–hash桶的个数 - * - * @return size_t - */ - size_t getHashCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getHashCount(); - } - - /** - * 获å–主key hash桶个数 - */ - size_t getMainKeyHashCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getMainKeyHashCount(); - } - - /** - * 元素的个数 - * - * @return size_t - */ - size_t size() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.size(); - } - - /** - * è„æ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t dirtyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.dirtyCount(); - } - - /** - * 主键下Only keyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t onlyKeyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.onlyKeyCount(); - } - - /** - * 主key下Only keyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t onlyKeyCountM() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.onlyKeyCountM(); - } - - /** - * 设置æ¯æ¬¡æ·˜æ±°æ•°é‡ - * @param n - */ - void setEraseCount(size_t n) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseCount(n); - } - - /** - * 获å–æ¯æ¬¡æ·˜æ±°æ•°é‡ - * - * @return size_t - */ - size_t getEraseCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseCount(); - } - - /** - * 设置åªè¯» - * @param bReadOnly - */ - void setReadOnly(bool bReadOnly) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setReadOnly(bReadOnly); - } - - /** - * 是å¦åªè¯» - * - * @return bool - */ - bool isReadOnly() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isReadOnly(); - } - - /** - * 设置是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * @param bAutoErase - */ - void setAutoErase(bool bAutoErase) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setAutoErase(bAutoErase); - } - - /** - * 是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * - * @return bool - */ - bool isAutoErase() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isAutoErase(); - } - - /** - * è®¾ç½®æ·˜æ±°æ–¹å¼ - * TC_Multi_HashMap::ERASEBYGET - * TC_Multi_HashMap::ERASEBYSET - * @param cEraseMode - */ - void setEraseMode(char cEraseMode) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseMode(cEraseMode); - } - - /** - * 获å–æ·˜æ±°æ–¹å¼ - * - * @return bool - */ - char getEraseMode() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseMode(); - } - - /** - * å¤´éƒ¨ä¿¡æ¯ - * - * @return TC_Multi_HashMap::tagMapHead - */ - TC_Multi_HashMap::tagMapHead& getMapHead() { return this->_t.getMapHead(); } - - /** - * 设置回写时间间隔(秒) - * @param iSyncTime - */ - void setSyncTime(time_t iSyncTime) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setSyncTime(iSyncTime); - } - - /** - * 获å–回写时间 - * - * @return time_t - */ - time_t getSyncTime() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getSyncTime(); - } - - /** - * dump到文件 - * @param sFile - * @param bDoClear: 是å¦æ¸…空 - * @return int - * TC_Multi_HashMap::RT_DUMP_FILE_ERR: dump到文件出错 - * TC_Multi_HashMap::RT_OK: dump到文件æˆåŠŸ - */ - int dump2file(const string &sFile, bool bDoClear = false) - { - TC_LockT lock(LockPolicy::mutex()); - int ret = this->_t.dump2file(sFile); - if(ret != TC_Multi_HashMap::RT_OK) - { - return ret; - } - - if(bDoClear) - this->_t.clear(); - - return ret; - } - - /** - * 从文件load - * @param sFile - * - * @return int - * TC_Multi_HashMap::RT_LOAL_FILE_ERR: load出错 - * TC_Multi_HashMap::RT_VERSION_MISMATCH_ERR: 版本ä¸ä¸€è‡´ - * TC_Multi_HashMap::RT_OK: loadæˆåŠŸ - */ - int load5file(const string &sFile) - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.load5file(sFile); - } - - /** - * 清空hash map - * 所有map中的数æ®éƒ½è¢«æ¸…空 - */ - void clear() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.clear(); - } - - /** - * 检查数æ®è„çŠ¶æ€ - * @param mk, 主key - * @param uk, 除主key外的è”åˆä¸»é”® - * - * @return int - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_DIRTY_DATA: 是è„æ•°æ® - * TC_Multi_HashMap::RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const MK &mk, const UK &uk) - { - tars::TarsOutputStream mosk; - mk.writeTo(mosk); - string smk(mosk.getBuffer(), mosk.getLength()); - - tars::TarsOutputStream uosk; - uk.writeTo(uosk); - string suk(uosk.getBuffer(), uosk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.checkDirty(smk, suk); - } - - /** - * 检查主key下的数æ®è„状æ€ï¼Œåªè¦ä¸»key下任何一æ¡è®°å½•æ˜¯è„æ•°æ®å°±è¿”å›žè„ - * @param mk, 主key - * - * @return int - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_DIRTY_DATA: 是è„æ•°æ® - * TC_Multi_HashMap::RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const MK &mk) - { - tars::TarsOutputStream mosk; - mk.writeTo(mosk); - string smk(mosk.getBuffer(), mosk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.checkDirty(smk); - } - - /** - * 设置为干净数æ®i, 修改SET/GET时间链, 会导致数æ®ä¸å›žå†™ - * @param k - * - * @return int - * TC_Multi_HashMap::RT_READONLY: åªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setClean(const MK &mk, const UK &uk) - { - tars::TarsOutputStream mosk; - mk.writeTo(mosk); - string smk(mosk.getBuffer(), mosk.getLength()); - - tars::TarsOutputStream uosk; - uk.writeTo(uosk); - string suk(uosk.getBuffer(), uosk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setClean(smk, suk); - } - - /** - * 设置为è„æ•°æ®, 修改SET/GET时间链, 会导致数æ®å›žå†™ - * @param mk - * @param uk - * @return int - * TC_Multi_HashMap::RT_READONLY: åªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirty(const MK &mk, const UK &uk) - { - tars::TarsOutputStream mosk; - mk.writeTo(mosk); - string smk(mosk.getBuffer(), mosk.getLength()); - - tars::TarsOutputStream uosk; - uk.writeTo(uosk); - string suk(uosk.getBuffer(), uosk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setDirty(smk, suk); - } - - /** - * 设置数æ®å›žå†™æ—¶é—´ - * @param mk - * @param uk - * @param iSyncTime - * @return int - * TC_Multi_HashMap::RT_READONLY: åªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setSyncTime(const MK &mk, const UK &uk, time_t iSyncTime) - { - tars::TarsOutputStream mosk; - mk.writeTo(mosk); - string smk(mosk.getBuffer(), mosk.getLength()); - - tars::TarsOutputStream uosk; - uk.writeTo(uosk); - string suk(uosk.getBuffer(), uosk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setSyncTime(smk, suk, iSyncTime); - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * (如果没设置自定义Get函数,没有数æ®æ—¶è¿”回:RT_NO_DATA) - * @param mk - * @param uk - * @param v - * - * @return int: - * TC_Multi_HashMap::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_Multi_HashMap::RT_READONLY: åªè¯»æ¨¡å¼ - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_Multi_HashMap::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const MK &mk, const UK &uk, Value &v) - { - int ret = TC_Multi_HashMap::RT_OK; - - tars::TarsOutputStream mosk; - mk.writeTo(mosk); - string smk(mosk.getBuffer(), mosk.getLength()); - - tars::TarsOutputStream uosk; - uk.writeTo(uosk); - string suk(uosk.getBuffer(), uosk.getLength()); - - TC_Multi_HashMap::Value tv; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.get(smk, suk, tv); - } - - // 读å–到数æ®äº†, 解包 - if(ret == TC_Multi_HashMap::RT_OK) - { - v._mkey = mk; - v._ukey = uk; - v._dirty = tv._data._dirty; - v._iSyncTime = tv._data._synct; - v._iVersion = tv._data._iVersion; - - tars::TarsInputStream is; - is.setBuffer(tv._data._value.c_str(), tv._data._value.length()); - v._value.readFrom(is); - - return ret; - } - - if(ret != TC_Multi_HashMap::RT_NO_DATA || _todo_of == NULL) - { - return ret; - } - - //åªè¯»æ¨¡å¼ - if(isReadOnly()) - { - return TC_Multi_HashMap::RT_READONLY; - } - - // Hashmap中没有数æ®ï¼Œä»Žå¤–部获å–函数获å–æ•°æ® - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = mk; - stDataRecord._ukey = uk; - ret = _todo_of->get(stDataRecord); - if(ret == TC_Multi_HashMap::RT_OK) - { - v = stDataRecord; - // 设置到hashmap中 - return this->set(stDataRecord._mkey, stDataRecord._ukey, stDataRecord._value, 0, stDataRecord._dirty); - } - else if(ret == TC_Multi_HashMap::RT_NO_GET) - { - return TC_Multi_HashMap::RT_NO_DATA; - } - else if(ret == TC_Multi_HashMap::RT_NO_DATA) - { - // 没有数æ®ï¼Œä»¥only keyçš„å½¢å¼è®¾ç½®åˆ°hashmap中 - ret = this->set(stDataRecord._mkey, stDataRecord._ukey); - if(ret == TC_Multi_HashMap::RT_OK) - { - return TC_Multi_HashMap::RT_ONLY_KEY; - } - return ret; - } - - return TC_Multi_HashMap::RT_LOAD_DATA_ERR; - } - - /** - * ä»…æ ¹æ®ä¸»key获å–æ‰€æœ‰çš„æ•°æ® - * @param mk - * @param vs - * - * @return int: - * RT_NO_DATA: æ²¡æœ‰æ•°æ® - * RT_ONLY_KEY: åªæœ‰Key - * RT_PART_DATA: æ•°æ®ä¸å…¨ï¼Œåªæœ‰éƒ¨åˆ†æ•°æ® - * RT_OK: 获å–æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int get(const MK& mk, vector &vs) - { - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - int ret = TC_Multi_HashMap::RT_OK; - vector hvs; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.get(smk, hvs); - } - - if(ret == TC_Multi_HashMap::RT_OK) - { - for(size_t i = 0; i < hvs.size(); i ++) - { - Value v; - v._mkey = mk; - - tars::TarsInputStream is; - is.setBuffer(hvs[i]._data._key.c_str(), hvs[i]._data._key.length()); - v._ukey.readFrom(is); - - is.setBuffer(hvs[i]._data._value.c_str(), hvs[i]._data._value.length()); - v._value.readFrom(is); - - v._iVersion = hvs[i]._data._iVersion; - v._dirty = hvs[i]._data._dirty; - v._iSyncTime = hvs[i]._data._synct; - vs.push_back(v); - } - return ret; - } - - if(ret != TC_Multi_HashMap::RT_NO_DATA || _todo_of == NULL) - { - return ret; - } - - //åªè¯»æ¨¡å¼ - if(isReadOnly()) - { - return TC_Multi_HashMap::RT_READONLY; - } - - // Hashmap中没有数æ®ï¼Œä»Žå¤–部获å–函数获å–æ•°æ® - ret = _todo_of->get(mk, vs); - if(ret == TC_Multi_HashMap::RT_OK) - { - // 设置到hashmap中 - for(size_t i = 0; i < vs.size(); i ++) - { - ret = this->set(vs[i]._mkey, vs[i]._ukey, vs[i]._value, 0, vs[i]._dirty); - if(ret != TC_Multi_HashMap::RT_OK) - { - // 把设置进去的全部删除 - vector vtErased; - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.del(smk, vtErased); - } - return ret; - } - } - } - else if(ret == TC_Multi_HashMap::RT_NO_GET) - { - return TC_Multi_HashMap::RT_NO_DATA; - } - else if(ret == TC_Multi_HashMap::RT_NO_DATA) - { - // 没有数æ®ï¼Œä»¥only keyçš„å½¢å¼è®¾ç½®åˆ°hashmap中 - ret = this->set(mk); - if(ret == TC_Multi_HashMap::RT_OK) - { - return TC_Multi_HashMap::RT_ONLY_KEY; - } - return ret; - } - - return TC_Multi_HashMap::RT_LOAD_DATA_ERR; - } - - /** - * æ ¹æ®hash值获å–相åŒhashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h, h为è”åˆä¸»é”®(MK+UK)çš„hash - * @param vv - * @param c, 匹é…仿函数: bool operator()(MK, UK); - * - * @return int, RT_OK - */ - template - int getHash(uint32_t h, vector &vv, C c) - { - TC_LockT lock(LockPolicy::mutex()); - - size_t index = h % this->_t.getHashCount(); - size_t iAddr = this->_t.item(index)->_iBlockAddr; - TC_Multi_HashMap::lock_iterator it(&this->_t, iAddr, TC_Multi_HashMap::lock_iterator::IT_UKEY, TC_Multi_HashMap::lock_iterator::IT_NEXT); - - while(it != this->_t.end()) - { - Value v; - TC_Multi_HashMap::Value hv; - int ret = it->get(hv); - if(ret == TC_Multi_HashMap::RT_OK) - { - tars::TarsInputStream is; - is.setBuffer(hv._mkey.c_str(), hv._mkey.length()); - v._mkey.readFrom(is); - - is.setBuffer(hv._data._key.c_str(), hv._data._key.length()); - v._ukey.readFrom(is); - - if(c(v._mkey, v._ukey)) - { - is.setBuffer(hv._data._value.c_str(), hv._data._value.length()); - v._value.readFrom(is); - - v._iVersion = hv._data._iVersion; - v._dirty = hv._data._dirty; - v._iSyncTime = hv._data._synct; - vv.push_back(v); - } - } - it ++; - } - - return TC_Multi_HashMap::RT_OK; - } - - /** - * æ ¹æ®ä¸»key hash值获å–相åŒä¸»key hashå€¼çš„æ‰€æœ‰æ•°æ® - * 注æ„:c匹é…对象æ“作中, map是加é”çš„, 需è¦æ³¨æ„ - * @param h, h为主key(MK)çš„hash - * @param mv, 返回结果集,以主key分组 - * @param c, 匹é…仿函数: bool operator()(MK); - * - * @return int, RT_OK - */ - template - int getHashM(uint32_t h, map > &mv, C c) - { - int ret = TC_Multi_HashMap::RT_OK; - map > hmv; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.get(h, hmv); - } - - if(ret == TC_Multi_HashMap::RT_OK) - { - map >::iterator it = hmv.begin(); - while(it != hmv.end()) - { - MK mk; - tars::TarsInputStream is; - is.setBuffer(it->first.c_str(), it->first.length()); - mk.readFrom(is); - - if(c(mk)) - { - vector vs; - vector &hvs = it->second; - for(size_t i = 0; i < hvs.size(); i ++) - { - Value v; - v._mkey = mk; - - is.setBuffer(hvs[i]._data._key.c_str(), hvs[i]._data._key.length()); - v._ukey.readFrom(is); - - is.setBuffer(hvs[i]._data._value.c_str(), hvs[i]._data._value.length()); - v._value.readFrom(is); - - v._iVersion = hvs[i]._data._iVersion; - v._dirty = hvs[i]._data._dirty; - v._iSyncTime = hvs[i]._data._synct; - vs.push_back(v); - } - mv[mk] = vs; - } - it ++; - } - } - return ret; - } - - /** - * 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param mk: 主key - * @param uk: 除主key外的è”åˆä¸»é”® - * @param v: 值 - * @param iVersion: 设置的数æ®ç‰ˆæœ¬ï¼Œåº”该根æ®getçš„æ•°æ®ç‰ˆæœ¬æ¥è®¾ç½®ï¼Œ0表示ä¸æ£€æŸ¥ç‰ˆæœ¬ - * @param bDirty: 是å¦æ˜¯è„æ•°æ® - * @param bHead: æ•°æ®æ’入到主key链的头部还是尾部 - * @param eType: æ’入的数æ®çš„类型 - * PART_DATA: æ’入的数æ®æ˜¯ä¸å®Œæ•´çš„æ•°æ® - * FULL_DATA: æ’入的数æ®æ˜¯å®Œæ•´æ•°æ® - * AUTO_DATA: æ ¹æ®Cache已有数æ®ç±»åž‹å†³å®šæœ€ç»ˆæ•°æ®ç±»åž‹ï¼Œå¦‚果已有数æ®æ˜¯ä¸å®Œæ•´çš„,最终的数æ®ä¹Ÿæ˜¯ä¸å®Œæ•´çš„,如果已有数æ®æ˜¯å®Œæ•´çš„,最终数æ®ä¹Ÿæ˜¯å®Œæ•´çš„,如果Cache中没有数æ®ï¼Œæœ€ç»ˆæ•°æ®æ˜¯ä¸å®Œæ•´çš„ - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_Multi_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const MK &mk, const UK &uk, const V &v, uint8_t iVersion, - bool bDirty = true, TC_Multi_HashMap::DATATYPE eType = TC_Multi_HashMap::AUTO_DATA, bool bHead = true) - { - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - uk.writeTo(uos); - string suk(uos.getBuffer(), uos.getLength()); - - tars::TarsOutputStream vos; - v.writeTo(vos); - string sv(vos.getBuffer(), vos.getLength()); - - int ret = TC_Multi_HashMap::RT_OK; - vector vtErased; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(smk, suk, sv, iVersion, bDirty, eType, bHead, vtErased); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtErased.size(); i++) - { - MK emk; - UK euk; - V tv; - try - { - tars::TarsInputStream is; - is.setBuffer(vtErased[i]._mkey.c_str(), vtErased[i]._mkey.length()); - emk.readFrom(is); - - is.setBuffer(vtErased[i]._data._key.c_str(), vtErased[i]._data._key.length()); - euk.readFrom(is); - - is.setBuffer(vtErased[i]._data._value.c_str(), vtErased[i]._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = vtErased[i]._data._iVersion; - stDataRecord._dirty = vtErased[i]._data._dirty; - stDataRecord._iSyncTime = vtErased[i]._data._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * 仅设置Key, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param mk: 主key - * @param uk: 除主key外的è”åˆä¸»é”® - * @param bHead: æ•°æ®æ’入到主key链的头部还是尾部 - * @param eType: æ’入的数æ®çš„类型 - * PART_DATA: æ’入的数æ®æ˜¯ä¸å®Œæ•´çš„æ•°æ® - * FULL_DATA: æ’入的数æ®æ˜¯å®Œæ•´æ•°æ® - * AUTO_DATA: æ ¹æ®Cache已有数æ®ç±»åž‹å†³å®šæœ€ç»ˆæ•°æ®ç±»åž‹ï¼Œå¦‚果已有数æ®æ˜¯ä¸å®Œæ•´çš„,最终的数æ®ä¹Ÿæ˜¯ä¸å®Œæ•´çš„,如果已有数æ®æ˜¯å®Œæ•´çš„,最终数æ®ä¹Ÿæ˜¯å®Œæ•´çš„,如果Cache中没有数æ®ï¼Œæœ€ç»ˆæ•°æ®æ˜¯ä¸å®Œæ•´çš„ - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_Multi_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const MK &mk, const UK &uk, TC_Multi_HashMap::DATATYPE eType = TC_Multi_HashMap::AUTO_DATA, bool bHead = true) - { - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - uk.writeTo(uos); - string suk(uos.getBuffer(), uos.getLength()); - - int ret = TC_Multi_HashMap::RT_OK; - vector vtErased; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(smk, suk, eType, bHead, vtErased); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtErased.size(); i++) - { - MK emk; - UK euk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtErased[i]._mkey.c_str(), vtErased[i]._mkey.length()); - emk.readFrom(is); - - is.setBuffer(vtErased[i]._data._key.c_str(), vtErased[i]._data._key.length()); - euk.readFrom(is); - - is.setBuffer(vtErased[i]._data._value.c_str(), vtErased[i]._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = vtErased[i]._data._iVersion; - stDataRecord._dirty = vtErased[i]._data._dirty; - stDataRecord._iSyncTime = vtErased[i]._data._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * 仅设置主Key, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param mk: 主key - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_Multi_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const MK &mk) - { - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - int ret = TC_Multi_HashMap::RT_OK; - vector vtErased; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(smk, vtErased); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtErased.size(); i++) - { - MK emk; - UK euk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtErased[i]._mkey.c_str(), vtErased[i]._mkey.length()); - emk.readFrom(is); - - is.setBuffer(vtErased[i]._data._key.c_str(), vtErased[i]._data._key.length()); - euk.readFrom(is); - - is.setBuffer(vtErased[i]._data._value.c_str(), vtErased[i]._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = vtErased[i]._data._iVersion; - stDataRecord._dirty = vtErased[i]._data._dirty; - stDataRecord._iSyncTime = vtErased[i]._data._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * 批é‡è®¾ç½®æ•°æ®ï¼Œå†…å­˜ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±° - * 注æ„,此接å£é€šå¸¸ç”¨äºŽä»Žæ•°æ®åº“中å–出相åŒä¸»key下的一批数æ®å¹¶åŒæ­¥åˆ°Cache - * ä¸èƒ½æ»¥ç”¨æ­¤æŽ¥å£æ¥æ‰¹é‡æ’入数æ®ï¼Œè¦æ³¨æ„ä¿è¯Cache和数æ®åº“æ•°æ®çš„一致 - * @param vs, 批é‡æ•°æ®é›† - * @param eType: æ’入的数æ®çš„类型 - * PART_DATA: æ’入的数æ®æ˜¯ä¸å®Œæ•´çš„æ•°æ® - * FULL_DATA: æ’入的数æ®æ˜¯å®Œæ•´æ•°æ® - * AUTO_DATA: æ ¹æ®Cache已有数æ®ç±»åž‹å†³å®šæœ€ç»ˆæ•°æ®ç±»åž‹ï¼Œå¦‚果已有数æ®æ˜¯ä¸å®Œæ•´çš„,最终的数æ®ä¹Ÿæ˜¯ä¸å®Œæ•´çš„,如果已有数æ®æ˜¯å®Œæ•´çš„,最终数æ®ä¹Ÿæ˜¯å®Œæ•´çš„,如果Cache中没有数æ®ï¼Œæœ€ç»ˆæ•°æ®æ˜¯ä¸å®Œæ•´çš„ - * @param bHead, æ•°æ®æ’入到主key链的头部还是尾部 - * @param bForce, 是å¦å¼ºåˆ¶æ’入数æ®ï¼Œä¸ºfalse则表示如果数æ®å·²ç»å­˜åœ¨åˆ™ä¸æ›´æ–° - * - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_Multi_HashMap::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const vector &vs, TC_Multi_HashMap::DATATYPE eType = TC_Multi_HashMap::AUTO_DATA, bool bHead = true, bool bForce = true) - { - int ret = TC_Multi_HashMap::RT_OK; - vector vtSet, vtErased; - - for(size_t i = 0; i < vs.size(); i ++) - { - TC_Multi_HashMap::Value v; - - tars::TarsOutputStream mos; - vs[i]._mkey.writeTo(mos); - v._mkey.assign(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - vs[i]._ukey.writeTo(uos); - v._data._key.assign(uos.getBuffer(), uos.getLength()); - - tars::TarsOutputStream vos; - vs[i]._value.writeTo(vos); - v._data._value.assign(vos.getBuffer(), vos.getLength()); - - v._data._dirty = vs[i]._dirty; - v._data._iVersion = vs[i]._iVersion; - v._data._synct = vs[i]._iSyncTime; - - vtSet.push_back(v); - } - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(vtSet, eType, bHead, bForce, vtErased); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtErased.size(); i++) - { - MK emk; - UK euk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtErased[i]._mkey.c_str(), vtErased[i]._mkey.length()); - emk.readFrom(is); - - is.setBuffer(vtErased[i]._data._key.c_str(), vtErased[i]._data._key.length()); - euk.readFrom(is); - - is.setBuffer(vtErased[i]._data._value.c_str(), vtErased[i]._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = vtErased[i]._data._iVersion; - stDataRecord._dirty = vtErased[i]._data._dirty; - stDataRecord._iSyncTime = vtErased[i]._data._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * 无论cache是å¦æœ‰æ•°æ®,todoçš„del都被调用 - * - * @param mk, 主key - * @param uk, 除主key外的è”åˆä¸»é”® - * - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_Multi_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int del(const MK &mk, const UK &uk) - { - int ret = TC_Multi_HashMap::RT_OK; - - TC_Multi_HashMap::Value data; - - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - uk.writeTo(uos); - string suk(uos.getBuffer(), uos.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(smk, suk, data); - } - - if(ret != TC_Multi_HashMap::RT_OK && ret != TC_Multi_HashMap::RT_ONLY_KEY && ret != TC_Multi_HashMap::RT_NO_DATA) - { - return ret; - } - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - - stDataRecord._mkey = mk; - stDataRecord._ukey = uk; - - if(ret == TC_Multi_HashMap::RT_OK) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._data._value.c_str(), data._data._value.length()); - v.readFrom(is); - - stDataRecord._value = v; - stDataRecord._iVersion = data._data._iVersion; - stDataRecord._dirty = data._data._dirty; - stDataRecord._iSyncTime = data._data._synct; - } - - _todo_of->del((ret == TC_Multi_HashMap::RT_OK), stDataRecord); - } - return ret; - } - - /** - * 删除主keyä¸‹çš„æ‰€æœ‰æ•°æ® - * cache有数æ®,todoçš„erase被调用 - * - * @param mk, 主key - * - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_Multi_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int erase(const MK &mk) - { - int ret = TC_Multi_HashMap::RT_OK; - - vector vtErased; - - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(smk, vtErased); - } - - if(ret != TC_Multi_HashMap::RT_OK) - { - return ret; - } - - if(_todo_of) - { - for(size_t i = 0; i < vtErased.size(); i ++) - { - MK emk; - UK euk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtErased[i]._mkey.c_str(), vtErased[i]._mkey.length()); - emk.readFrom(is); - - is.setBuffer(vtErased[i]._data._key.c_str(), vtErased[i]._data._key.length()); - euk.readFrom(is); - - is.setBuffer(vtErased[i]._data._value.c_str(), vtErased[i]._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = vtErased[i]._data._iVersion; - stDataRecord._dirty = vtErased[i]._data._dirty; - stDataRecord._iSyncTime = vtErased[i]._data._synct; - - _todo_of->erase(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * åˆ é™¤æŒ‡å®šæ•°æ® - * cache有数æ®,todoçš„erase被调用 - * - * @param mk, 主key - * @param uk, 除主key外的è”åˆä¸»é”® - * - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_Multi_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int erase(const MK &mk, const UK &uk) - { - int ret = TC_Multi_HashMap::RT_OK; - - TC_Multi_HashMap::Value data; - - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - uk.writeTo(uos); - string suk(uos.getBuffer(), uos.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(smk, suk, data); - } - - if(ret != TC_Multi_HashMap::RT_OK) - { - return ret; - } - if(_todo_of) - { - MK emk; - UK euk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(data._mkey.c_str(), data._mkey.length()); - emk.readFrom(is); - - is.setBuffer(data._data._key.c_str(), data._data._key.length()); - euk.readFrom(is); - - is.setBuffer(data._data._value.c_str(), data._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = data._data._iVersion; - stDataRecord._dirty = data._data._dirty; - stDataRecord._iSyncTime = data._data._synct; - - _todo_of->erase(stDataRecord); - } - catch(exception &ex) - { - } - } - return ret; - } - - /** - * 强制删除数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * - * @param mk, 主key - * @param uk, 除主key外的è”åˆä¸»é”® - * - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_Multi_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int eraseByForce(const MK &mk, const UK &uk) - { - int ret = TC_Multi_HashMap::RT_OK; - - TC_Multi_HashMap::Value data; - - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - uk.writeTo(uos); - string suk(uos.getBuffer(), uos.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(smk, suk, data); - } - - if(ret != TC_Multi_HashMap::RT_OK) - { - return ret; - } - - return ret; - } - - /** - * 强制删除主key下的所有数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * - * @param mk, 主key - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_Multi_HashMap::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int eraseByForce(const MK &mk) - { - int ret = TC_Multi_HashMap::RT_OK; - - vector data; - - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(smk, data); - } - - if(ret != TC_Multi_HashMap::RT_OK) - { - return ret; - } - - return ret; - } - - /** - * 淘汰数æ®, æ ¹æ®Get时间淘汰 - * 直到: 元素个数/chunks * 100 < ratio,bCheckDirty 为true时,é‡åˆ°è„æ•°æ®åˆ™æ·˜æ±°ç»“æŸ - * @param ratio: 共享内存chunks使用比例 0< ratio < 100 - * @param bCheckDirty: 是å¦æ£€æŸ¥æ•°æ®è„状æ€ï¼Œå¦‚果检查则é‡åˆ°è„æ•°æ®ä¸æ·˜æ±° - - * @return int: - * TC_Multi_HashMap::RT_READONLY: mapåªè¯» - * TC_Multi_HashMap::RT_OK:淘汰完毕 - */ - int erase(int ratio, bool bCheckDirty = false) - { - while(true) - { - int ret; - vector vtErased; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.erase(ratio, vtErased, bCheckDirty); - if(ret == TC_Multi_HashMap::RT_OK || ret == TC_Multi_HashMap::RT_READONLY) - { - return ret; - } - - if(ret != TC_Multi_HashMap::RT_ERASE_OK) - { - continue; - } - } - - if(_todo_of) - { - for(size_t i = 0; i < vtErased.size(); i++) - { - MK emk; - UK euk; - V tv; - try - { - tars::TarsInputStream is; - is.setBuffer(vtErased[i]._mkey.c_str(), vtErased[i]._mkey.length()); - emk.readFrom(is); - - is.setBuffer(vtErased[i]._data._key.c_str(), vtErased[i]._data._key.length()); - euk.readFrom(is); - - is.setBuffer(vtErased[i]._data._value.c_str(), vtErased[i]._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = emk; - stDataRecord._ukey = euk; - stDataRecord._value = tv; - stDataRecord._iVersion = vtErased[i]._data._iVersion; - stDataRecord._dirty = vtErased[i]._data._dirty; - stDataRecord._iSyncTime = vtErased[i]._data._synct; - - _todo_of->erase(stDataRecord); - } - catch(exception &ex) - { - } - } - } - } - return TC_Multi_HashMap::RT_OK; - } - - /** - * 回写å•æ¡è®°å½•, 如果记录ä¸å­˜åœ¨, 则ä¸åšä»»ä½•å¤„ç† - * @param mk - * @param uk - * - * @return int - * TC_Multi_HashMap::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY:åªæœ‰Key - * TC_Multi_HashMap::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_Multi_HashMap::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int sync(const MK &mk, const UK &uk) - { - Value v; - int ret = get(mk, uk, v); - - if(ret == TC_Multi_HashMap::RT_OK) - { - if(_todo_of) - { - _todo_of->sync(v); - } - } - - return ret; - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å…¨éƒ¨å›žå†™ - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - * - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_Multi_HashMap::RT_OK: 回写完毕了 - */ - int sync(time_t iNowTime) - { - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - while(true) - { - TC_Multi_HashMap::Value data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_Multi_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_Multi_HashMap::RT_NEED_SYNC) - { - continue; - } - } - - if(_todo_of) - { - MK mk; - UK uk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._mkey.c_str(), data._mkey.length()); - mk.readFrom(is); - - is.setBuffer(data._data._key.c_str(), data._data._key.length()); - uk.readFrom(is); - - is.setBuffer(data._data._value.c_str(), data._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = mk; - stDataRecord._ukey = uk; - stDataRecord._value = tv; - stDataRecord._iVersion = data._data._iVersion; - stDataRecord._dirty = data._data._dirty; - stDataRecord._iSyncTime = data._data._synct; - - _todo_of->sync(stDataRecord); - } - } - - return TC_Multi_HashMap::RT_OK; - } - - /** - *å°†è„æ•°æ®å°¾æŒ‡é’ˆèµ‹ç»™å›žå†™å°¾æŒ‡é’ˆ - */ - void sync() - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å›žå†™,åªå›žå†™ä¸€ä¸ªè„æ•°æ®ï¼Œç›®çš„是替代int sync(time_t iNowTime) - * 方法,把由业务控制æ¯æ¬¡å›žå†™æ•°æ®é‡ï¼Œä½¿ç”¨æ—¶åº”该先调用void sync() - * - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_Multi_HashMap::RT_OK: 回写完毕了 - * - * 示例: - * p->sync(); - * while(true) { - * int iRet = pthis->SyncOnce(tNow); - * if( iRet == TC_Multi_HashMap::RT_OK ) - * break; - * } - */ - int syncOnce(time_t iNowTime) - { - TC_Multi_HashMap::Value data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_Multi_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_Multi_HashMap::RT_NEED_SYNC) - { - return ret; - } - } - - if(_todo_of) - { - MK mk; - UK uk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._mkey.c_str(), data._mkey.length()); - mk.readFrom(is); - - is.setBuffer(data._data._key.c_str(), data._data._key.length()); - uk.readFrom(is); - - is.setBuffer(data._data._value.c_str(), data._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = mk; - stDataRecord._ukey = uk; - stDataRecord._value = tv; - stDataRecord._iVersion = data._data._iVersion; - stDataRecord._dirty = data._data._dirty; - stDataRecord._iSyncTime = data._data._synct; - - _todo_of->sync(stDataRecord); - } - - return ret; - } - /** - * å¤‡ä»½æ•°æ® - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å¤‡ä»½ - * å¯ä»¥å¤šä¸ªçº¿ç¨‹/进程备份数æ®,åŒæ—¶å¤‡ä»½æ—¶bForceFromBegin设置为false效率更高 - * - * @param bForceFromBegin: 是å¦å¼ºåˆ¶é‡å¤´å¼€å§‹å¤‡ä»½, 通常为false - * @return int: - * TC_Multi_HashMap::RT_OK: 备份OK了 - */ - int backup(bool bForceFromBegin = false) - { - { - //开始准备备份 - TC_LockT lock(LockPolicy::mutex()); - this->_t.backup(bForceFromBegin); - } - - while(true) - { - TC_Multi_HashMap::Value data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.backup(data); - if(ret == TC_Multi_HashMap::RT_OK) - { - return ret; - } - - if(ret != TC_Multi_HashMap::RT_NEED_BACKUP) - { - continue; - } - } - - if(_todo_of) - { - MK mk; - UK uk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._mkey.c_str(), data._mkey.length()); - mk.readFrom(is); - - is.setBuffer(data._data._key.c_str(), data._data._key.length()); - uk.readFrom(is); - - is.setBuffer(data._data._value.c_str(), data._data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._mkey = mk; - stDataRecord._ukey = uk; - stDataRecord._value = tv; - stDataRecord._iVersion = data._data._iVersion; - stDataRecord._dirty = data._data._dirty; - stDataRecord._iSyncTime = data._data._synct; - - _todo_of->backup(stDataRecord); - } - } - - return TC_Multi_HashMap::RT_OK; - } - - /** - * æè¿° - * - * @return string - */ - string desc() { return this->_t.desc(); } - - /////////////////////////////////////////////////////////////////////////////// - /** - * 尾部 - * - * @return lock_iterator - */ - lock_iterator end() - { - JhmAutoLockPtr jlock; - return JhmLockIterator(this->_t.end(), jlock); - } - - /** - * æ ¹æ®è”åˆä¸»é”®(MK+UK)æŸ¥æ‰¾æ•°æ® - * @param mk - * @param uk - * @return lock_interator - * 返回end()表示没有查到 - */ - lock_iterator find(const MK &mk, const UK &uk) - { - tars::TarsOutputStream mos; - mk.writeTo(mos); - string smk(mos.getBuffer(), mos.getLength()); - - tars::TarsOutputStream uos; - uk.writeTo(uos); - string suk(uos.getBuffer(), uos.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.find(smk, suk), jlock); - } - - /** - * 查找主key下所有数æ®æ•°é‡ - * @param mk, 主key - * - * @return size_t, 主key下的记录数 - */ - size_t count(const MK &mk) - { - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.count(smk); - } - - /** - * æ ¹æ®ä¸»key查找第一个数æ®çš„ä½ç½® - * 与上é¢çš„count函数组åˆå¯ä»¥é历所有主keyä¸‹çš„æ•°æ® - * 也å¯ä»¥ç›´æŽ¥ä½¿ç”¨è¿­ä»£å™¨ï¼Œç›´åˆ°end - * @param mk, 主key - * @return lock_iterator, 返回end()è¡¨ç¤ºæ²¡æœ‰æ•°æ® - */ - lock_iterator find(const MK& mk) - { - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.find(smk), jlock); - } - - /** - * 判断主key是å¦å­˜åœ¨ - * @param mk, 主key - * - * @return int - * TC_Multi_HashMap::RT_OK, 主keyå­˜åœ¨ï¼Œä¸”æœ‰æ•°æ® - * TC_Multi_HashMap::RT_ONLY_KEY, 主keyå­˜åœ¨ï¼Œæ²¡æœ‰æ•°æ® - * TC_Multi_HashMap::RT_PART_DATA, 主key存在,里é¢çš„æ•°æ®å¯èƒ½ä¸å®Œæ•´ - * TC_Multi_HashMap::RT_NO_DATA, 主keyä¸å­˜åœ¨ - */ - int checkMainKey(const MK& mk) - { - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.checkMainKey(smk); - } - - /** - * 设置主key下数æ®çš„完整性 - * @param mk, 主key - * @param bFull, true为完整数æ®ï¼Œfalse为ä¸å®Œæ•´æ•°æ® - * - * @return - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setFullData(const MK &mk, bool bFull) - { - tars::TarsOutputStream os; - mk.writeTo(os); - string smk(os.getBuffer(), os.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setFullData(smk, bFull); - } - - /** - * 检查åblock,并å¯è¿›è¡Œä¿®å¤ - * @param bRepaire, 是å¦è¿›è¡Œä¿®å¤ - * - * @return size_t, 返回åæ•°æ®ä¸ªæ•° - */ - size_t checkBadBlock(bool bRepair) - { - size_t c = this->_t.getHashCount(); - size_t e = 0; - for(size_t i = 0; i < c; i++) - { - TC_LockT lock(LockPolicy::mutex()); - - e += this->_t.checkBadBlock(i, bRepair); - } - - return e; - } - - /** - * blockæ­£åº - * - * @return lock_iterator - */ - lock_iterator begin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.begin(), jlock); - } - - /** - * blocké€†åº - * - * @return lock_iterator - */ - lock_iterator rbegin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbegin(), jlock); - } - - /** - * 以Set时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * - * @return lock_iterator - */ - lock_iterator beginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginSetTime(), jlock); - } - - /** - * Set时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Set-->最近Set - * - * @return lock_iterator - */ - lock_iterator rbeginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginSetTime(), jlock); - } - - /** - * 以Get时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Get-->最久Get - * - * @return lock_iterator - */ - lock_iterator beginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginGetTime(), jlock); - } - - /** - * Get时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Get-->最近Get - * - * @return lock_iterator - */ - lock_iterator rbeginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginGetTime(), jlock); - } - - /** - * 获å–è„链表尾部迭代器(最长时间没有Setçš„è„æ•°æ®) - * - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * å¯èƒ½å­˜åœ¨å¹²å‡€æ•°æ® - * - * @return lock_iterator - */ - lock_iterator beginDirty() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginDirty(), jlock); - } - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, ä¸éœ€è¦å¯¹mapåŠ é” - - /** - * æ ¹æ®hash桶é历 - * - * @return hash_iterator - */ - hash_iterator hashBegin() - { - JhmLockPtr jlock(new JhmLock(this->mutex())); - return JhmIterator(this->_t.hashBegin(), jlock); - } - - /** - * ç»“æŸ - * - * @return - */ - hash_iterator hashEnd() - { - JhmLockPtr jlock; - return JhmIterator(this->_t.hashEnd(), jlock); - } - -protected: - - /** - * 删除数æ®çš„函数对象 - */ - ToDoFunctor *_todo_of; -}; - -} - -#endif diff --git a/cpp/servant/jmem/jmem_policy.h b/cpp/servant/jmem/jmem_policy.h deleted file mode 100644 index 5e2235c48..000000000 --- a/cpp/servant/jmem/jmem_policy.h +++ /dev/null @@ -1,201 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _JMEM_POLICY_H -#define _JMEM_POLICY_H - -#include "util/tc_thread_mutex.h" -#include "util/tc_sem_mutex.h" -#include "util/tc_shm.h" -#include "util/tc_mmap.h" - -namespace tars -{ -////////////////////////////////////////////////////////////////////// -// 存储策略: 内存, 共享内存, mmap(文件) - -/** - * 内存存储 - */ -template -class MemStorePolicy : public LockPolicy -{ -public: - /** - * åˆå§‹åŒ– - * @param pAddr: 指令队列空间的指针 - * @param iSize: 空间的指针 - */ - void create(void *pAddr, size_t iSize) - { - _t.create(pAddr,iSize); - } - - /** - * 连接上队列 - * @param pAddr: 指令队列空间的指针 - * @param iSize: 空间的指针 - */ - void connect(void *pAddr, size_t iSize) - { - _t.connect(pAddr,iSize); - } - -protected: - T _t; -}; - -/** - * 共享内存存储 - */ -template -class ShmStorePolicy : public LockPolicy -{ -public: - /** - * åˆå§‹åŒ–共享存储 - * @param iShmKey - * @param iSize - */ - void initStore(key_t iShmKey, size_t iSize) - { - _shm.init(iSize, iShmKey); - if(_shm.iscreate()) - { - _t.create(_shm.getPointer(), iSize); - } - else - { - _t.connect(_shm.getPointer(), iSize); - } - } - - /** - * 释放共享内存 - */ - void release() - { - _shm.del(); - } -protected: - TC_Shm _shm; - T _t; -}; - -/** - * 文件存储 - */ -template -class FileStorePolicy : public LockPolicy -{ -public: - /** - * åˆå§‹åŒ–文件 - * @param file, 文件路径 - * @param iSize, æ–‡ä»¶å¤§å° - */ - void initStore(const char *file, size_t iSize) - { - _file = file; - _mmap.mmap(file, iSize); - if(_mmap.iscreate()) - { - _t.create(_mmap.getPointer(), iSize); - } - else - { - _t.connect(_mmap.getPointer(), iSize); - } - } - - /** - * 扩展空间, ç›®å‰åªå¯¹hashmap有效 - */ - int expand(size_t iSize) - { - TC_LockT lock(LockPolicy::mutex()); - - TC_Mmap m(false); - m.mmap(_file.c_str(), iSize); - - int ret = _t.append(m.getPointer(), iSize); - - if(ret == 0) - { - _mmap.munmap(); - _mmap = m; - _mmap.setOwner(true); - } - else - { - m.munmap(); - } - - return ret; - } - -protected: - string _file; - TC_Mmap _mmap; - T _t; -}; - -////////////////////////////////////////////////////////////////////// -// é”ç­–ç•¥: æ— é”, 线程é”, è¿›ç¨‹é” - -/** - * æ— é” - */ -class EmptyLockPolicy -{ -public: - typedef TC_EmptyMutex Mutex; - Mutex &mutex() { return _mutex; } - -protected: - Mutex _mutex; -}; - -/** - * 线程é”ç­–ç•¥ - */ -class ThreadLockPolicy -{ -public: - typedef TC_ThreadMutex Mutex; - Mutex &mutex() { return _mutex; } - -protected: - Mutex _mutex; -}; - -/** - * 进程é”ç­–ç•¥ - */ -class SemLockPolicy -{ -public: - typedef TC_SemMutex Mutex; - void initLock(key_t iSemKey) { return _mutex.init(iSemKey); } - Mutex &mutex() { return _mutex; } - -protected: - Mutex _mutex; -}; - -} - -#endif diff --git a/cpp/servant/jmem/jmem_queue.h b/cpp/servant/jmem/jmem_queue.h deleted file mode 100644 index 0b5340e87..000000000 --- a/cpp/servant/jmem/jmem_queue.h +++ /dev/null @@ -1,172 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _JMEM_QUEUE_H -#define _JMEM_QUEUE_H - -#include "util/tc_mem_queue.h" -#include "jmem/jmem_policy.h" -#include "tup/Tars.h" - -namespace tars -{ - -/** - * 基于Tarså议的内存循环队列 - * 编解ç å‡ºé”™åˆ™æŠ›å‡ºTarsDecodeExceptionå’ŒTarsEncodeException - * å¯ä»¥å¯¹é”策略和存储策略进行组åˆ, 例如: - * 基于信å·é‡é”, 文件存储的队列: - * TarsQueue - * 基于信å·é‡é”, 共享内存存储的队列 - * TarsQueue - * 基于线程é”, 内存存储的队列 - * TarsQueue - * - * 使用上, ä¸åŒçš„组åˆ, åˆå§‹åŒ–函数ä¸å®Œå…¨ä¸€æ · - * åˆå§‹åŒ–函数有: - * SemLockPolicy::initLock(key_t) - * ShmStorePolicy::initStore(key_t, size_t) - * FileStorePolicy::initStore(const char *file, size_t) - * ç­‰, 具体å‚è§jmem_policy.h - */ - -template class StorePolicy> -class TarsQueue : public StorePolicy -{ -public: - /** - * 弹出一个元素 - * @param t - * - * @return bool,true:æˆåŠŸ, false:无元素 - */ - bool pop_front(T &t) - { - string s; - - { - TC_LockT lock(LockPolicy::mutex()); - if(!this->_t.pop_front(s)) - { - return false; - } - } - tars::TarsInputStream is; - is.setBuffer(s.c_str(), s.length()); - t.readFrom(is); - - return true; - } - - /** - * æ’入到队列 - * @param t - * - * @return bool, ture:æˆåŠŸ, false:队列满 - */ - bool push_back(const T &t) - { - tars::TarsOutputStream os; - t.writeTo(os); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.push_back(os.getBuffer(), os.getLength()); - } - - /** - * 是å¦æ»¡äº† - * @param t - * - * @return bool - */ - bool isFull(const T &t) - { - tars::TarsOutputStream os; - t.writeTo(os); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isFull(os.getLength()); - } - - /** - * 获å–ç©ºé—²çš„ç©ºé—´å¤§å° - */ - size_t getFreeSize() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getFreeSize(); - } - - - /** - * 队列是å¦æ»¡ - * @param : iSize, 输入数æ®å—长度 - * @return bool , true:满, false: éžæ»¡ - */ - bool isFull(size_t iSize) - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isFull(iSize); - } - - /** - * 队列是å¦ç©º - * @return bool , true: 满, false: éžæ»¡ - */ - bool isEmpty() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isEmpty(); - } - - /** - * 队列中元素个数, ä¸åŠ é”的情况下ä¸ä¿è¯ä¸€å®šæ­£ç¡® - * @return size_t, 元素个数 - */ - size_t elementCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.elementCount(); - } - - - /** - * 队列长度(字节), å°äºŽæ€»å­˜å‚¨åŒºé•¿åº¦(总存储区长度包å«äº†æŽ§åˆ¶å¿«) - * @return size_t : 队列长度 - */ - size_t queueSize() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.queueSize(); - } - - /** - * 共享内存长度 - * @return size_t : 共享内存长度 - */ - size_t memSize() const - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.memSize(); - }; - -}; - -} - -#endif diff --git a/cpp/servant/jmem/jmem_rbtree.h b/cpp/servant/jmem/jmem_rbtree.h deleted file mode 100644 index 492f12866..000000000 --- a/cpp/servant/jmem/jmem_rbtree.h +++ /dev/null @@ -1,2062 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _JMEM_RBTREE_H -#define _JMEM_RBTREE_H - -#include "util/tc_rbtree.h" -#include "util/tc_autoptr.h" -#include "jmem/jmem_policy.h" -#include "tup/Tars.h" - -namespace tars -{ - -/************************************************************************ - 基本说明如下: - 基于Tarså议的内存rbtree - 编解ç å‡ºé”™åˆ™æŠ›å‡ºTarsDecodeExceptionå’ŒTarsEncodeException - å¯ä»¥å¯¹é”策略和存储策略进行组åˆ, 例如: - 基于信å·é‡é”, 文件存储的rbtree: - TarsRBtree - 基于信å·é‡é”, 共享内存存储的rbtree - TarsRBTree - 基于线程é”, 内存存储的rbtree - TarsRBTree - - 使用上, ä¸åŒçš„组åˆ, åˆå§‹åŒ–函数ä¸å®Œå…¨ä¸€æ · - åˆå§‹åŒ–函数有: - SemLockPolicy::initLock(key_t) - ShmStorePolicy::initStore(key_t, size_t) - FileStorePolicy::initStore(const char *file, size_t) - ç­‰, 具体å‚è§jmem_policy.h - - *********************************************************************** - - 基本特性说明: - > 内存数æ®çº¢é»‘æ ‘, æ ¹æ®æœ€åŽGet时间的顺åºæ·˜æ±°æ•°æ®; - > 支æŒç¼“写/dump到文件/在线备份; - > 支æŒä¸åŒå¤§å°å†…å­˜å—çš„é…ç½®, æ供内存的使用率; - > 支æŒå›žæ”¶åˆ°æŒ‡å®šç©ºé—²æ¯”率的空间; - > 支æŒä»…设置Keyçš„æ“作, å³æ•°æ®æ— value, åªæœ‰Key, 类似与stl::set; - > 支æŒå‡ ç§æ–¹å¼çš„é历, 通常é历时需è¦å¯¹map加é”; - > 支æŒè‡ªå®šä¹‰æ“作对象设置, å¯ä»¥éžå¸¸å¿«é€Ÿå®žçŽ°ç›¸å…³çš„接å£; - > 支æŒè‡ªåŠ¨ç¼–解ç , Keyå’ŒValue的结构都通过tars2cpp生æˆ; - > tarså议支æŒè‡ªåŠ¨æ‰©å±•å­—段, 因此该hashmap支æŒè‡ªåŠ¨æ‰©å±•å­—段(Keyå’ŒValue都必须是通过tarsç¼–ç çš„); - > map支æŒåªè¯»æ¨¡å¼, åªè¯»æ¨¡å¼ä¸‹set/erase/del等修改数æ®çš„æ“作ä¸èƒ½ä½¿ç”¨, get/回写/在线备份正常使用 - > 支æŒè‡ªåŠ¨æ·˜æ±°, setæ—¶, 内存满则自动淘汰, 在éžè‡ªåŠ¨æ·˜æ±°æ—¶, 内存满直接返回RT_READONLY - > 对于mmap文件, 支æŒè‡ªåŠ¨æ‰©å±•æ–‡ä»¶, å³å†…å­˜ä¸å¤Ÿäº†, å¯ä»¥è‡ªåŠ¨æ‰©å±•æ–‡ä»¶å¤§å°(注æ„hashçš„æ•°é‡ä¸å˜, 因此开始就需è¦è€ƒè™‘hashçš„æ•°é‡), 而且ä¸èƒ½è·¨JRBTree对象(å³ä¸¤ä¸ªrbtree对象访问åŒä¸€å—文件,通知一个rbtree扩展以åŽï¼Œå¦å¤–一个对象并ä¸çŸ¥é“扩展了) - - *********************************************************************** - - rbtree链说明: - rbtree链一共包括了如下几ç§é“¾è¡¨: - > Set时间链: 任何Setæ“作都会修改该链表, SetåŽæ•°æ®è¢«è®¾ç½®ä¸ºè„æ•°æ®, 且移动到Set链表头部; - > Get时间链: 任何Getæ“作都会修改该链表, 除éžé“¾è¡¨åªè¯», 注æ„Set链åŒæ—¶ä¹Ÿä¼šä¿®æ”¹Get链 - > Dirty时间链: dirty链是Set链的一部分, 用于回写数æ®ç”¨ - > Backup链:备份链是Get链的一部分, 当备份数æ®æ—¶, 顺ç€Get链从尾部到头部开始备份; - - *********************************************************************** - - 相关æ“作说明: - > å¯ä»¥è®¾ç½®mapåªè¯», 则所有写æ“作返回RT_READONLY, 此时Getæ“作ä¸ä¿®æ”¹é“¾è¡¨ - > å¯ä»¥è®¾ç½®çŸ¥å¦è‡ªåŠ¨æ·˜æ±°, 默认是自动淘汰的.如果ä¸è‡ªåŠ¨æ·˜æ±°,则setæ—¶,无内存空间返回:RT_NO_MEMORY - > å¯ä»¥æ›´æ”¹key比较的算法, 调用setLessFunctorå³å¯ - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºå¹²å‡€, 此时移出到Dirty链表指Dirty尾部的下一个元素; - > å¯ä»¥å°†æŸæ¡æ•°æ®è®¾ç½®ä¸ºè„, 此时会移动到Set链表头部; - > æ¯ä¸ªæ•°æ®éƒ½æœ‰ä¸€ä¸ªä¸Šæ¬¡å›žå†™æ—¶é—´(SyncTime), 如果å¯åŠ¨å›žå†™æ“作, 则在一定时间内会回写; - > å¯ä»¥dump到文件或者从文件中load, 这个时候会对mapåŠ é” - > å¯ä»¥è°ƒç”¨erase批é‡æ·˜æ±°æ•°æ®ç›´åˆ°å†…存空闲率到一定比例 - > å¯ä»¥è°ƒç”¨sync进行数æ®å›žå†™, ä¿è¯ä¸€å®šæ—¶é—´å†…没有回写的数æ®ä¼šå›žå†™, map回写时间通过setSyncTime设置, 默认10分钟 - > å¯ä»¥setToDoFunctor设置æ“作类, 以下是æ“作触å‘的情况: - - *********************************************************************** - - ToDoFunctor的函数说明: - > 通常继承ToDoFunctor, 实现相关函数就å¯ä»¥äº†, å¯ä»¥å®žçŽ°ä»¥ä¸‹åŠŸèƒ½:Getæ•°æ®, 淘汰数æ®, 删除数æ®, 回写数æ®, å¤‡ä»½æ•°æ® - > ToDoFunctor::erase, 当调用map.eraseæ—¶, 该函数会被调用 - > ToDoFunctor::del, 当调用map.delæ—¶, 该函数会被调用, 注æ„删除时数æ®å¯èƒ½éƒ½ä¸åœ¨cache中; - > ToDoFunctor::sync, 当调用map.syncæ—¶, 会触å‘æ¯æ¡éœ€è¦å›žå†™çš„æ•°æ®è¯¥å‡½æ•°éƒ½è¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å›žå†™è¯·æ±‚; - > ToDoFunctor::backup, 当调用map.backupæ—¶, 会触å‘需è¦å¤‡ä»½çš„æ•°æ®è¯¥å‡½æ•°ä¼šè¢«è°ƒç”¨ä¸€æ¬¡, 在该函数中处ç†å¤‡ä»½è¯·æ±‚; - > ToDoFunctor::get, 当调用map.getæ—¶, 如果map中无数æ®, 则该函数被调用, 该函数从db中获å–æ•°æ®, 并返回RT_OK, 如果dbæ— æ•°æ®åˆ™è¿”回RT_NO_DATA; - > ToDoFunctor所有接å£è¢«è°ƒç”¨æ—¶, 都ä¸ä¼šå¯¹map加é”, å› æ­¤å¯ä»¥æ“作map - - *********************************************************************** - - mapçš„é‡è¦å‡½æ•°è¯´æ˜Ž: - > set, 设置数æ®åˆ°map中, 会更新set链表 - 如果满了, 且å¯ä»¥è‡ªåŠ¨æ·˜æ±°, 则根æ®Get链淘汰数æ®, 此时ToDoFunctorçš„sync会被调用 - 如果满了, 且å¯ä»¥ä¸èƒ½è‡ªåŠ¨æ·˜æ±°, 则返回RT_NO_MEMORY - - > get, 从map获å–æ•°æ®, 如果有数æ®, 则直接从map获å–æ•°æ®å¹¶è¿”回RT_OK; - 如果没有数æ®, 则调用ToDoFunctor::get函数, 此时get函数需è¦è¿”回RT_OK, åŒæ—¶ä¼šè®¾ç½®åˆ°map中, 并返回数æ®; - 如果没有数æ®, 则ToDoFunctor::get函数也无数æ®, 则需è¦è¿”回RT_NO_DATA, 此时åªä¼šæŠŠKey设置到map中, 并返回RT_ONLY_KEY; - 在上é¢æƒ…况下, 如果å†æœ‰get请求, 则ä¸å†è°ƒç”¨ToDoFunctor::get, 直接返回RT_ONLY_KEY; - - > del, 删除数æ®, 无论cache是å¦æœ‰æ•°æ®, ToDoFunctor::del都会被调用; - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«åˆ é™¤; - - > erase, 淘汰数æ®, åªæœ‰cache存在数æ®, ToDoFunctor::eraseæ‰ä¼šè¢«è°ƒç”¨ - 如果åªæœ‰Key, 则该数æ®ä¹Ÿä¼šè¢«æ·˜æ±°, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > erase(int radio), 批é‡æ·˜æ±°æ•°æ®, 直到空闲å—比率到达radio; - ToDoFunctor::erase会被调用; - åªæœ‰Key的记录也会被淘汰, 但是ToDoFunctor::eraseä¸ä¼šè¢«è°ƒç”¨; - - > sync: 缓写数æ®, 超时没有回写且是è„æ•°æ®éœ€è¦å›žå†™, 回写完毕åŽ, æ•°æ®ä¼šè‡ªåŠ¨è®¾ç½®ä¸ºå¹²å‡€æ•°æ®; - å¯ä»¥å¤šä¸ªçº¿ç¨‹æˆ–进程åŒæ—¶ç¼“写; - ToDoFunctor::sync会被调用; - åªæœ‰Key的记录, ToDoFunctor::syncä¸ä¼šè¢«è°ƒç”¨; - - > backup: 备份数æ®, 顺ç€é¡ºç€Get链从尾部到头部开始备份; - ToDoFunctor::backup会被调用; - åªæœ‰Key的记录, ToDoFunctor::backupä¸ä¼šè¢«è°ƒç”¨; - 由于备份游标åªæœ‰ä¸€ä¸ª, 因此多个进程åŒæ—¶å¤‡ä»½çš„时候数æ®å¯èƒ½ä¼šæ¯ä¸ªè¿›ç¨‹æœ‰ä¸€éƒ¨åˆ† - 如果备份程åºå¤‡ä»½åˆ°ä¸€åŠdown了, 则下次å¯åŠ¨å¤‡ä»½æ—¶ä¼šæŽ¥ç€ä¸Šæ¬¡çš„备份进行, 除éžå°†backup(true)调用备份 - - *********************************************************************** - - 返回值说明: - > 注æ„函数所有int的返回值, 如无特别说明, 请å‚è§TC_RBTree::RT_ - - *********************************************************************** - - é历说明: - > å¯ä»¥ç”¨lock_iterator对map进行以下几ç§é历, 在é历过程中其实对map加é”处ç†äº† - > beginSetTime(): 按照Set时间顺åºé历 - > rbeginSetTime(): 按照Set时间顺åºé历 - > beginGetTime(): 按照Get时间顺åºé历 - > rbeginGetTime(): 按照Get时间逆åºé历 - > beginDirty(): 按时间逆åºé历è„æ•°æ®é“¾(如果setClean, 则也å¯èƒ½åœ¨è„链表上) - > 其实回写数æ®é“¾æ˜¯è„æ•°æ®é‡çš„å­é›† - > 注æ„:lock_iterator一旦获å–, 就对map加é”了, 直到lock_iteratoræžå¤Ÿä¸ºæ­¢ - - lock_iterator与nolock_iterator的区别: - > map的函数如果返回lock_iteratoråŽ,则自动对map加é”了,直到lock_iterator对象æžå¤Ÿæ‰è‡ªåŠ¨è§£é” - > map的函数如果返回nolock_iterator, ä¸ä¼šå¯¹map加é”, åªä¼šåœ¨nolock_iterator对象++或者get的时候æ‰åŠ é” - > nolock_iterator的优势是å¯ä»¥é历mapæ—¶,无需对map加大é¢ç§¯é”,但是é历的效率会低一些 - > nolock_iterator对象getæ•°æ®æ—¶,务必判断get返回值,因为迭代器指å‘çš„æ•°æ®æœ‰å¯èƒ½å¤±æ•ˆ -*/ - -template class StorePolicy> -class TarsRBTree : public StorePolicy -{ -public: - - /** - * 定义数æ®æ“作基类 - * 获å–,é历,删除,淘汰时都å¯ä»¥ä½¿ç”¨è¯¥æ“作类 - */ - class ToDoFunctor - { - public: - /** - * æ•°æ®è®°å½• - */ - struct DataRecord - { - K _key; - V _value; - bool _dirty; - time_t _iSyncTime; - - DataRecord() : _dirty(true), _iSyncTime(0) - { - } - }; - - /** - * æžå¤Ÿ - */ - virtual ~ToDoFunctor(){}; - - /** - * æ·˜æ±°æ•°æ® - * @param stDataRecord: è¢«æ·˜æ±°çš„æ•°æ® - */ - virtual void erase(const DataRecord &stDataRecord){}; - - /** - * åˆ é™¤æ•°æ® - * @param bExists: 是å¦å­˜åœ¨æ•°æ® - * @param stDataRecord: æ•°æ®, bExists==true时有效, å¦åˆ™åªæœ‰key有效 - */ - virtual void del(bool bExists, const DataRecord &stDataRecord){}; - - /** - * å›žå†™æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void sync(const DataRecord &stDataRecord){}; - - /** - * å¤‡ä»½æ•°æ® - * @param stDataRecord: æ•°æ® - */ - virtual void backup(const DataRecord &stDataRecord){}; - - /** - * 获å–æ•°æ®, 默认返回RT_NO_GET - * stDataRecord中_key有效, 其他数æ®éœ€è¦è¿”回 - * @param stDataRecord: 需è¦èŽ·å–çš„æ•°æ® - * - * @return int, 获å–到数æ®, 返回:TC_RBTree::RT_OK - * 没有数æ®,返回:TC_RBTree::RT_NO_DATA, - * 系统默认GET,返回:TC_RBTree::RT_NO_GET - * 其他,则返回:TC_RBTree::RT_LOAD_DATA_ERR - */ - virtual int get(DataRecord &stDataRecord) - { - return TC_RBTree::RT_NO_GET; - } - }; - - /////////////////////////////////////////////////////////////////// - /** - * 自动é”, 用于迭代器 - */ - class JhmAutoLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmAutoLock(typename LockPolicy::Mutex &mutex) : _lock(mutex) - { - } - - protected: - //ä¸å®žçŽ° - JhmAutoLock(const JhmAutoLock &al); - JhmAutoLock &operator=(const JhmAutoLock &al); - - protected: - /** - * é” - */ - TC_LockT _lock; - }; - - typedef TC_AutoPtr JhmAutoLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmLockItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmLockItem(const TC_RBTree::RBTreeLockItem &item) - : _item(item) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockItem(const JhmLockItem &item) - : _item(item._item) - { - } - - /** - * - */ - JhmLockItem() - { - } - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockItem& - */ - JhmLockItem& operator=(const JhmLockItem &item) - { - if(this != &item) - { - _item = item._item; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmLockItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmLockItem& item) - { - return !((*this) == item); - } - - /** - * 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty() { return _item.isDirty(); } - - /** - * 是å¦åªæœ‰Key - * - * @return bool - */ - bool isOnlyKey() { return _item.isOnlyKey(); } - - /** - * 最åŽå›žå†™æ—¶é—´ - * - * @return time_t - */ - time_t getSyncTime() { return _item.getSyncTime(); } - - /** - * 获å–值 - * @return int - * TC_RBTree::RT_OK:æ•°æ®èŽ·å–OK - * 其他值, 异常 - */ - int get(K& k) - { - string sk; - int ret = _item.get(sk); - if(ret != TC_RBTree::RT_OK) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(sk.c_str(), sk.length()); - k.readFrom(is); - - return ret; - } - - /** - * 获å–值 - * @return int - * TC_RBTree::RT_OK:æ•°æ®èŽ·å–OK - * TC_RBTree::RT_ONLY_KEY: key有效, v无效为空 - * 其他值, 异常 - */ - int get(K& k, V& v) - { - string sk; - string sv; - int ret = _item.get(sk, sv); - if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY) - { - return ret; - } - - tars::TarsInputStream is; - is.setBuffer(sk.c_str(), sk.length()); - k.readFrom(is); - - if(ret != TC_RBTree::RT_ONLY_KEY) - { - is.setBuffer(sv.c_str(), sv.length()); - v.readFrom(is); - } - - return ret; - } - - protected: - TC_RBTree::RBTreeLockItem _item; - }; - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmLockIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmLockIterator(const TC_RBTree::lock_iterator it, const JhmAutoLockPtr &lock) - : _it(it), _item(it._iItem), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmLockIterator(const JhmLockIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * 构造 - */ - JhmLockIterator() - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator=(const JhmLockIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - _lock = it._lock; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmLockIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmLockIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator& operator++() - { - ++_it; - _item = JhmLockItem(_it._iItem); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmLockIterator& - */ - JhmLockIterator operator++(int) - { - JhmLockIterator jit(_it, _lock); - ++_it; - _item = JhmLockItem(_it._iItem); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem& - */ - JhmLockItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmLockItem* - */ - JhmLockItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_RBTree::lock_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmLockItem _item; - - /** - * é” - */ - JhmAutoLockPtr _lock; - }; - - typedef JhmLockIterator lock_iterator ; - - /////////////////////////////////////////////////////////////////// - /** - * é”, 用于éžé”迭代器 - * - */ - class JhmLock : public TC_HandleBase - { - public: - /** - * 构造 - * @param mutex - */ - JhmLock(typename LockPolicy::Mutex &mutex) : _mutex(mutex) - { - } - - /** - * 获å–é” - * - * @return typename LockPolicy::Mutex - */ - typename LockPolicy::Mutex& mutex() - { - return _mutex; - } - protected: - //ä¸å®žçŽ° - JhmLock(const JhmLock &al); - JhmLock &operator=(const JhmLock &al); - - protected: - /** - * é” - */ - typename LockPolicy::Mutex &_mutex; - }; - - typedef TC_AutoPtr JhmLockPtr; - - /////////////////////////////////////////////////////////////////// - /** - * æ•°æ®é¡¹ - */ - class JhmItem - { - public: - - /** - * 构造函数 - * @param item - */ - JhmItem(const TC_RBTree::RBTreeItem &item, const JhmLockPtr &lock) - : _item(item), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmItem(const JhmItem &item) - : _item(item._item), _lock(item._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmItem& - */ - JhmItem& operator=(const JhmItem &item) - { - if(this != &item) - { - _item = item._item; - _lock = item._lock; - } - - return (*this); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator==(const JhmItem& item) - { - return (_item == item._item); - } - - /** - * - * @param item - * - * @return bool - */ - bool operator!=(const JhmItem& item) - { - return !((*this) == item); - } - - /** - * 获å–当å‰æ•°æ® - * @param - * - * @return int - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key有效 - * TC_RBTree::RT_OK: æˆåŠŸ(keyå’Œvalue都有效) - * 其他返回值: 错误 - */ - int get(K &k, V &v) - { - int ret; - TC_RBTree::BlockData stData; - - { - TC_LockT lock(_lock->mutex()); - ret = _item.get(stData); - } - - if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY) - { - return ret; - } - - try - { - tars::TarsInputStream is; - is.setBuffer(stData._key.c_str(), stData._key.length()); - k.readFrom(is); - - if(ret == TC_RBTree::RT_OK) - { - is.setBuffer(stData._value.c_str(), stData._value.length()); - v.readFrom(is); - } - } - catch(exception &ex) - { - return TC_RBTree::RT_DECODE_ERR; - } - - return ret; - } - - protected: - TC_RBTree::RBTreeItem _item; - JhmLockPtr _lock; - }; - - - /////////////////////////////////////////////////////////////////// - /** - * 迭代器 - */ - struct JhmIterator - { - public: - - /** - * 构造 - * @param it - * @param lock - */ - JhmIterator(const TC_RBTree::nolock_iterator &it, const JhmLockPtr &lock) - : _it(it), _item(it._iItem, lock), _lock(lock) - { - } - - /** - * æ‹·è´æž„造 - * @param it - */ - JhmIterator(const JhmIterator &it) - : _it(it._it), _item(it._item), _lock(it._lock) - { - } - - /** - * å¤åˆ¶ - * @param it - * - * @return JhmIterator& - */ - JhmIterator& operator=(const JhmIterator &it) - { - if(this != &it) - { - _it = it._it; - _item = it._item; - } - - return (*this); - } - - /** - * - * @param it - * - * @return bool - */ - bool operator==(const JhmIterator& it) - { - return (_it == it._it && _item == it._item); - } - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const JhmIterator& it) - { - return !((*this) == it); - } - - /** - * å‰ç½®++ - * - * @return JhmIterator& - */ - JhmIterator& operator++() - { - TC_LockT lock(_lock->mutex()); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return (*this); - } - - /** - * åŽç½®++ - * - * @return JhmIterator& - */ - JhmIterator operator++(int) - { - TC_LockT lock(_lock->mutex()); - JhmIterator jit(_it, _lock); - ++_it; - _item = JhmItem(_it._iItem, _lock); - return jit; - } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem& - */ - JhmItem& operator*() { return _item; } - - /** - * 获å–æ•°æ®é¡¹ - * - * @return JhmItem* - */ - JhmItem* operator->() { return &_item; } - - protected: - - /** - * 迭代器 - */ - TC_RBTree::nolock_iterator _it; - - /** - * æ•°æ®é¡¹ - */ - JhmItem _item; - - /** - * é” - */ - JhmLockPtr _lock; - }; - - typedef JhmIterator nolock_iterator ; - - /** - * 默认的比较 - */ - struct RBTreeLess - { - bool operator()(const string &k1, const string &k2) - { - K tk1; - K tk2; - - tars::TarsInputStream is; - is.setBuffer(k1.c_str(), k1.length()); - tk1.readFrom(is); - - is.setBuffer(k2.c_str(), k2.length()); - tk2.readFrom(is); - - return tk1 < tk2; - } - }; - - //////////////////////////////////////////////////////////////////////////// - // - /** - * 构造函数 - */ - TarsRBTree() - { - _todo_of = NULL; - - this->_t.setLessFunctor(RBTreeLess()); - } - - /** - * åˆå§‹åŒ–æ•°æ®å—å¹³å‡å¤§å° - * 表示内存分é…的时候,会分é…n个最å°å—, n个(最å°å¿«*增长因å­ï¼‰, n个(最å°å¿«*增长因å­*增长因å­ï¼‰..., 直到nä¸ªæœ€å¤§å— - * n是hashmap自己计算出æ¥çš„ - * è¿™ç§åˆ†é…策略通常是你数æ®å¿«è®°å½•å˜é•¿æ¯”较多的使用, 便于节约内存,如果数æ®è®°å½•åŸºæœ¬ä¸æ˜¯å˜é•¿çš„, 那最å°å—=最大快,增长因å­=1å°±å¯ä»¥äº† - * @param iMinDataSize: 最å°æ•°æ®å—å¤§å° - * @param iMaxDataSize: 最大数æ®å—å¤§å° - * @param fFactor: å¢žé•¿å› å­ >= 1.0 - */ - void initDataBlockSize(size_t iMinDataSize, size_t iMaxDataSize, float fFactor) - { - this->_t.initDataBlockSize(iMinDataSize, iMaxDataSize, fFactor); - } - - /** - * 设置lessæ–¹å¼ - * @param lessf - */ - void setLessFunctor(TC_RBTree::less_functor lessf) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setLessFunctor(lessf); - } - - /** - * 获å–lessæ–¹å¼ - * - * @return TC_RBTree::less_functor& - */ - TC_RBTree::less_functor &getLessFunctor() { return this->_t.getLessFunctor(); } - - /** - * 设置淘汰æ“作类 - * @param erase_of - */ - void setToDoFunctor(ToDoFunctor *todo_of) { this->_todo_of = todo_of; } - - /** - * 获å–æ¯ç§å¤§å°å†…å­˜å—çš„å¤´éƒ¨ä¿¡æ¯ - * - * @return vector: ä¸åŒå¤§å°å†…å­˜å—å¤´éƒ¨ä¿¡æ¯ - */ - vector getBlockDetail() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getBlockDetail(); - } - - /** - * 所有block中chunk的个数 - * - * @return size_t - */ - size_t allBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.allBlockChunkCount(); - } - - /** - * æ¯ç§block中chunk的个数(ä¸åŒå¤§å°å†…å­˜å—的个数相åŒ) - * - * @return size_t - */ - vector singleBlockChunkCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.singleBlockChunkCount(); - } - - /** - * 元素的个数 - * - * @return size_t - */ - size_t size() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.size(); - } - - /** - * è„æ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t dirtyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.dirtyCount(); - } - - /** - * Onlyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t onlyKeyCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.onlyKeyCount(); - } - - /** - * 设置æ¯æ¬¡æ·˜æ±°æ•°é‡ - * @param n - */ - void setEraseCount(size_t n) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseCount(n); - } - - /** - * 获å–æ¯æ¬¡æ·˜æ±°æ•°é‡ - * - * @return size_t - */ - size_t getEraseCount() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseCount(); - } - - /** - * 设置åªè¯» - * @param bReadOnly - */ - void setReadOnly(bool bReadOnly) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setReadOnly(bReadOnly); - } - - /** - * 是å¦åªè¯» - * - * @return bool - */ - bool isReadOnly() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isReadOnly(); - } - - /** - * 设置是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * @param bAutoErase - */ - void setAutoErase(bool bAutoErase) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setAutoErase(bAutoErase); - } - - /** - * 是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * - * @return bool - */ - bool isAutoErase() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.isAutoErase(); - } - - /** - * è®¾ç½®æ·˜æ±°æ–¹å¼ - * TC_RBTree::ERASEBYGET - * TC_RBTree::ERASEBYSET - * @param cEraseMode - */ - void setEraseMode(char cEraseMode) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setEraseMode(cEraseMode); - } - - /** - * 获å–æ·˜æ±°æ–¹å¼ - * - * @return bool - */ - char getEraseMode() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getEraseMode(); - } - - /** - * å¤´éƒ¨ä¿¡æ¯ - * - * @return TC_RBTree::tagMapHead - */ - TC_RBTree::tagMapHead& getMapHead() { return this->_t.getMapHead(); } - - /** - * 设置回写时间(秒) - * @param iSyncTime - */ - void setSyncTime(time_t iSyncTime) - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.setSyncTime(iSyncTime); - } - - /** - * 获å–回写时间 - * - * @return time_t - */ - time_t getSyncTime() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.getSyncTime(); - } - - /** - * dump到文件 - * @param sFile - * @param bDoClear: 是å¦æ¸…空 - * @return int - * TC_RBTree::RT_DUMP_FILE_ERR: dump到文件出错 - * TC_RBTree::RT_OK: dump到文件æˆåŠŸ - */ - int dump2file(const string &sFile, bool bDoClear = false) - { - TC_LockT lock(LockPolicy::mutex()); - int ret = this->_t.dump2file(sFile); - if(ret != TC_RBTree::RT_OK) - { - return ret; - } - - if(bDoClear) - this->_t.clear(); - - return ret; - } - - /** - * 从文件load - * @param sFile - * - * @return int - * TC_RBTree::RT_LOAL_FILE_ERR: load出错 - * TC_RBTree::RT_VERSION_MISMATCH_ERR: 版本ä¸ä¸€è‡´ - * TC_RBTree::RT_OK: loadæˆåŠŸ - */ - int load5file(const string &sFile) - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.load5file(sFile); - } - - /** - * 清空hash map - * 所有map中的数æ®éƒ½è¢«æ¸…空 - */ - void clear() - { - TC_LockT lock(LockPolicy::mutex()); - return this->_t.clear(); - } - - /** - * 检查数æ®çŠ¶æ€ - * @param k - * - * @return int - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key - * TC_RBTree::RT_DIRTY_DATA: 是è„æ•°æ® - * TC_RBTree::RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const K &k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.checkDirty(sk); - } - - /** - * 设置为干净数æ®i, 修改SET/GET时间链, 会导致数æ®ä¸å›žå†™ - * @param k - * - * @return int - * TC_RBTree::RT_READONLY: åªè¯» - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key - * TC_RBTree::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setClean(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setClean(sk); - } - - /** - * 设置为è„æ•°æ®, 修改SET/GET时间链, 会导致数æ®å›žå†™ - * @param k - * @return int - * TC_RBTree::RT_READONLY: åªè¯» - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key - * TC_RBTree::RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirty(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - TC_LockT lock(LockPolicy::mutex()); - return this->_t.setDirty(sk); - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * (如果没设置自定义Get函数,没有数æ®æ—¶è¿”回:RT_NO_DATA) - * @param k - * @param v - * @param iSyncTime:æ•°æ®ä¸Šæ¬¡å›žå†™çš„时间, 没有缓写则为0 - * - * @return int: - * TC_RBTree::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_RBTree::RT_READONLY: åªè¯»æ¨¡å¼ - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key - * TC_RBTree::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_RBTree::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const K& k, V &v, time_t &iSyncTime) - { - iSyncTime = 0; - int ret = TC_RBTree::RT_OK; - - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - string sv; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.get(sk, sv, iSyncTime); - } - - //读å–到数æ®äº†, 解包 - if(ret == TC_RBTree::RT_OK) - { - tars::TarsInputStream is; - is.setBuffer(sv.c_str(), sv.length()); - v.readFrom(is); - - return ret; - } - - if(ret != TC_RBTree::RT_NO_DATA || _todo_of == NULL) - { - return ret; - } - - //åªè¯»æ¨¡å¼ - if(isReadOnly()) - { - return TC_RBTree::RT_READONLY; - } - - //获å–函数 - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - ret = _todo_of->get(stDataRecord); - if(ret == TC_RBTree::RT_OK) - { - v = stDataRecord._value; - return this->set(stDataRecord._key, stDataRecord._value, stDataRecord._dirty); - } - else if(ret == TC_RBTree::RT_NO_GET) - { - return TC_RBTree::RT_NO_DATA; - } - else if(ret == TC_RBTree::RT_NO_DATA) - { - ret = this->set(stDataRecord._key); - if(ret == TC_RBTree::RT_OK) - { - return TC_RBTree::RT_ONLY_KEY; - } - return ret; - } - - return TC_RBTree::RT_LOAD_DATA_ERR; - } - - /** - * 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * - * @return int: - * TC_RBTree::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key - * TC_RBTree::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_RBTree::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int get(const K& k, V &v) - { - time_t iSyncTime; - return get(k, v, iSyncTime); - } - - /** - * 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @param v: 值 - * @param bDirty: 是å¦æ˜¯è„æ•°æ® - * @return int: - * TC_RBTree::RT_READONLY: mapåªè¯» - * TC_RBTree::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_RBTree::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const K& k, const V& v, bool bDirty = true) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - tars::TarsOutputStream osv; - v.writeTo(osv); - string sv(osv.getBuffer(), osv.getLength()); - - int ret = TC_RBTree::RT_OK; - vector vtData; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(sk, sv, bDirty, vtData); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtData.size(); i++) - { - K tk; - V tv; - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - tk.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = vtData[i]._dirty; - stDataRecord._iSyncTime = vtData[i]._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * 仅设置Key, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @return int: - * TC_RBTree::RT_READONLY: mapåªè¯» - * TC_RBTree::RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * TC_RBTree::RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const K& k) - { - tars::TarsOutputStream osk; - k.writeTo(osk); - string sk(osk.getBuffer(), osk.getLength()); - - int ret = TC_RBTree::RT_OK; - vector vtData; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.set(sk, vtData); - } - - //æ“ä½œæ·˜æ±°æ•°æ® - if(_todo_of) - { - for(size_t i = 0; i < vtData.size(); i++) - { - K tk; - V tv; - - try - { - tars::TarsInputStream is; - is.setBuffer(vtData[i]._key.c_str(), vtData[i]._key.length()); - tk.readFrom(is); - - is.setBuffer(vtData[i]._value.c_str(), vtData[i]._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = vtData[i]._dirty; - stDataRecord._iSyncTime = vtData[i]._synct; - - _todo_of->sync(stDataRecord); - } - catch(exception &ex) - { - } - } - } - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * 无论cache是å¦æœ‰æ•°æ®,todoçš„del都被调用 - * - * @param k, 关键字 - * @return int: - * TC_RBTree::RT_READONLY: mapåªè¯» - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_RBTree::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int del(const K& k) - { - int ret = TC_RBTree::RT_OK; - - TC_RBTree::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_RBTree::RT_OK && ret != TC_RBTree::RT_ONLY_KEY && ret != TC_RBTree::RT_NO_DATA) - { - return ret; - } - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - - if(ret == TC_RBTree::RT_OK) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._value.c_str(), data._value.length()); - v.readFrom(is); - - stDataRecord._value = v; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - } - - _todo_of->del((ret == TC_RBTree::RT_OK), stDataRecord); - } - return ret; - } - - /** - * åˆ é™¤æ•°æ® - * cache有数æ®,todoçš„erase被调用 - * - * @param k, 关键字 - * @return int: - * TC_RBTree::RT_READONLY: mapåªè¯» - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_RBTree::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int erase(const K& k) - { - int ret = TC_RBTree::RT_OK; - - TC_RBTree::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_RBTree::RT_OK) - { - return ret; - } - - if(_todo_of) - { - V v; - tars::TarsInputStream is; - is.setBuffer(data._value.c_str(), data._value.length()); - v.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - stDataRecord._value = v; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->erase(stDataRecord); - } - return ret; - } - - /** - * 强制删除数æ®,ä¸è°ƒç”¨todoçš„erase被调用 - * - * @param k, 关键字 - * @return int: - * TC_RBTree::RT_READONLY: mapåªè¯» - * TC_RBTree::RT_NO_DATA: 没有当å‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key, 也删除了 - * TC_RBTree::RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int eraseByForce(const K& k) - { - int ret = TC_RBTree::RT_OK; - - TC_RBTree::BlockData data; - - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.del(sk, data); - } - - if(ret != TC_RBTree::RT_OK) - { - return ret; - } - - return ret; - } - - /** - * 淘汰数æ®, æ ¹æ®Get时间淘汰 - * 直到: 元素个数/chunks * 100 < radio,bCheckDirty 为true时,é‡åˆ°è„æ•°æ®åˆ™æ·˜æ±°ç»“æŸ - * @param radio: 共享内存chunks使用比例 0< radio < 100 - * @return int: - * TC_RBTree::RT_READONLY: mapåªè¯» - * TC_RBTree::RT_OK:淘汰完毕 - */ - int erase(int radio, bool bCheckDirty = false) - { - while(true) - { - int ret; - TC_RBTree::BlockData data; - - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.erase(radio, data, bCheckDirty); - if(ret == TC_RBTree::RT_OK || ret == TC_RBTree::RT_READONLY) - { - return ret; - } - - if(ret != TC_RBTree::RT_ERASE_OK) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->erase(stDataRecord); - } - } - return TC_RBTree::RT_OK; - } - - /** - * 回写å•æ¡è®°å½•, 如果记录ä¸å­˜åœ¨, 则ä¸åšä»»ä½•å¤„ç† - * @param k - * - * @return int - * TC_RBTree::RT_NO_DATA: æ²¡æœ‰æ•°æ® - * TC_RBTree::RT_ONLY_KEY:åªæœ‰Key - * TC_RBTree::RT_OK:获å–æ•°æ®æˆåŠŸ - * TC_RBTree::RT_LOAD_DATA_ERR: loadæ•°æ®å¤±è´¥ - * 其他返回值: 错误 - */ - int sync(const K& k) - { - V v; - time_t iSyncTime; - int ret = get(k, v, iSyncTime); - - if(ret == TC_RBTree::RT_OK) - { - bool bDirty = (checkDirty(k) == TC_RBTree::RT_DIRTY_DATA); - - if(_todo_of) - { - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = k; - stDataRecord._value = v; - stDataRecord._dirty = bDirty; - stDataRecord._iSyncTime = iSyncTime; - - _todo_of->sync(stDataRecord); - } - } - - return ret; - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å…¨éƒ¨å›žå†™ - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - * - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_RBTree::RT_OK: 回写完毕了 - */ - int sync(time_t iNowTime) - { - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - while(true) - { - TC_RBTree::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_RBTree::RT_OK) - { - return ret; - } - - if(ret != TC_RBTree::RT_NEED_SYNC) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->sync(stDataRecord); - } - } - - return TC_RBTree::RT_OK; - } - - /** - *å°†è„æ•°æ®å°¾æŒ‡é’ˆèµ‹ç»™å›žå†™å°¾æŒ‡é’ˆ - */ - void sync() - { - TC_LockT lock(LockPolicy::mutex()); - this->_t.sync(); - } - - /** - * å°†è„æ•°æ®ä¸”一定时间没有回写的数æ®å›žå†™,åªå›žå†™ä¸€ä¸ªè„æ•°æ®ï¼Œç›®çš„是替代int sync(time_t iNowTime) - * 方法,把由业务控制æ¯æ¬¡å›žå†™æ•°æ®é‡ï¼Œä½¿ç”¨æ—¶åº”该先调用void sync() - * - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime(setSyncTime)则需è¦å›žå†™ - - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * - * @param iNowTime: 回写到什么时间, 通常是当å‰æ—¶é—´ - * @return int: - * TC_RBTree::RT_OK: 回写完毕了 - * - * 示例: - * p->sync(); - * while(true) { - * int iRet = pthis->SyncOnce(tNow); - * if( iRet == TC_RBTree::RT_OK ) - * break; - * } - */ - int syncOnce(time_t iNowTime) - { - TC_RBTree::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.sync(iNowTime, data); - if(ret == TC_RBTree::RT_OK) - { - return ret; - } - - if(ret != TC_RBTree::RT_NEED_SYNC) - { - return ret; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->sync(stDataRecord); - } - - return ret; - } - /** - * å¤‡ä»½æ•°æ® - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å¤‡ä»½ - * å¯ä»¥å¤šä¸ªçº¿ç¨‹/进程备份数æ®,åŒæ—¶å¤‡ä»½æ—¶bForceFromBegin设置为false效率更高 - * - * @param bForceFromBegin: 是å¦å¼ºåˆ¶é‡å¤´å¼€å§‹å¤‡ä»½, 通常为false - * @return int: - * TC_RBTree::RT_OK: 备份OK了 - */ - int backup(bool bForceFromBegin = false) - { - { - //开始准备备份 - TC_LockT lock(LockPolicy::mutex()); - this->_t.backup(bForceFromBegin); - } - - while(true) - { - TC_RBTree::BlockData data; - - int ret; - { - TC_LockT lock(LockPolicy::mutex()); - ret = this->_t.backup(data); - if(ret == TC_RBTree::RT_OK) - { - return ret; - } - - if(ret != TC_RBTree::RT_NEED_BACKUP) - { - continue; - } - } - - if(_todo_of) - { - K tk; - V tv; - - tars::TarsInputStream is; - is.setBuffer(data._key.c_str(), data._key.length()); - tk.readFrom(is); - - is.setBuffer(data._value.c_str(), data._value.length()); - tv.readFrom(is); - - typename ToDoFunctor::DataRecord stDataRecord; - stDataRecord._key = tk; - stDataRecord._value = tv; - stDataRecord._dirty = data._dirty; - stDataRecord._iSyncTime = data._synct; - - _todo_of->backup(stDataRecord); - } - } - - return TC_RBTree::RT_OK; - } - - /** - * æè¿° - * - * @return string - */ - string desc() { return this->_t.desc(); } - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, 无需è¦å¯¹map加大é¢ç§¯é”, 但是éåŽ†æ•ˆçŽ‡æœ‰ä¸€å®šå½±å“ - // (åªåœ¨get以åŠè¿­ä»£å™¨++的时候加é”) - // 获å–的迭代器和数æ®ä¸ä¿è¯å®žæ—¶æœ‰æ•ˆ,å¯èƒ½å·²ç»è¢«åˆ é™¤äº†,获å–æ•°æ®æ—¶éœ€è¦åˆ¤æ–­æ•°æ®çš„åˆæ³•æ€§ - // 用迭代器getæ•°æ®æ—¶, 请务必检查返回值 - /** - * 尾部 - * - * @return nolock_iterator - */ - nolock_iterator nolock_end() - { - JhmLockPtr jlock; - return JhmIterator(this->_t.nolock_end(), jlock); - } - - /** - * é¡ºåº - * - * @return nolock_iterator - */ - nolock_iterator nolock_begin() - { - JhmLockPtr jlock(new JhmLock(this->mutex())); - return JhmIterator(this->_t.nolock_begin(), jlock); - } - - /** - * é€†åº - * - * @return nolock_iterator - */ - nolock_iterator nolock_rbegin() - { - JhmLockPtr jlock(new JhmLock(this->mutex())); - return JhmIterator(this->_t.nolock_rbegin(), jlock); - } - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, 需è¦å¯¹map加大é¢ç§¯é”(åŠè¿­ä»£å™¨å­˜åœ¨æœ‰æ•ˆèŒƒå›´å†…全部都加é”) - // 获å–çš„æ•°æ®ä»¥åŠè¿­ä»£å™¨éƒ½æ˜¯å®žæ—¶æœ‰æ•ˆ - - /** - * 尾部 - * - * @return lock_iterator - */ - lock_iterator end() - { - JhmAutoLockPtr jlock; - return JhmLockIterator(this->_t.end(), jlock); - } - - /** - * é¡ºåº - * - * @return lock_iterator - */ - lock_iterator begin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.begin(), jlock); - } - - /** - * é€†åº - * - * @return lock_iterator - */ - lock_iterator rbegin() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbegin(), jlock); - } - - /** - * 查找(++顺åº) - * - * @return lock_iterator - */ - lock_iterator find(const K &k) - { - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.find(sk), jlock); - } - - /** - * 查找(++逆åº) - * - * @return lock_iterator - */ - lock_iterator rfind(const K &k) - { - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rfind(sk), jlock); - } - - /** - * 返回查找关键字的下界 - * map中已ç»æ’入了1,2,3,4çš„è¯ï¼Œå¦‚æžœlower_bound(2)çš„è¯ï¼Œè¿”回的2,而upper-bound(2)çš„è¯ï¼Œè¿”回的就是3 - * @param k - * - * @return lock_iterator - */ - lock_iterator lower_bound(const K &k) - { - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.lower_bound(sk), jlock); - } - - /** - * 返回查找关键字的上界 - * map中已ç»æ’入了1,2,3,4çš„è¯ï¼Œå¦‚æžœlower_bound(2)çš„è¯ï¼Œè¿”回的2,而upper-bound(2)çš„è¯ï¼Œè¿”回的就是3 - * @param k - * - * @return lock_iterator - */ - lock_iterator upper_bound(const K &k) - { - tars::TarsOutputStream os; - k.writeTo(os); - string sk(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.upper_bound(sk), jlock); - } - - /** - * é¡ºåº - * - * @return lock_iterator - */ - pair equal_range(const K &k1, const K &k2) - { - tars::TarsOutputStream os; - k1.writeTo(os); - string sk1(os.getBuffer(), os.getLength()); - - os.reset(); - k2.writeTo(os); - string sk2(os.getBuffer(), os.getLength()); - - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - pair pit = this->_t.equal_range(sk1, sk2); - - pair p; - JhmLockIterator it1(pit.first, jlock); - JhmLockIterator it2(pit.second, jlock); - - p.first = it1; - p.second = it2; - - return p; - } - - ///////////////////////////////////////////////////////////////////////////// - /** - * 以Set时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * - * @return lock_iterator - */ - lock_iterator beginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginSetTime(), jlock); - } - - /** - * Set时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Set-->最近Set - * - * @return lock_iterator - */ - lock_iterator rbeginSetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginSetTime(), jlock); - } - - /** - * 以Get时间排åºçš„迭代器 - * 返回的迭代器++表示按照时间顺åº:最近Get-->最久Get - * - * @return lock_iterator - */ - lock_iterator beginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginGetTime(), jlock); - } - - /** - * Get时间链逆åºçš„迭代器 - * - * 返回的迭代器++表示按照时间顺åº:最久Get-->最近Get - * - * @return lock_iterator - */ - lock_iterator rbeginGetTime() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.rbeginGetTime(), jlock); - } - - /** - * 获å–è„链表尾部迭代器(最长时间没有Setçš„è„æ•°æ®) - * - * 返回的迭代器++表示按照时间顺åº:最近Set-->最久Set - * å¯èƒ½å­˜åœ¨å¹²å‡€æ•°æ® - * - * @return lock_iterator - */ - lock_iterator beginDirty() - { - JhmAutoLockPtr jlock(new JhmAutoLock(this->mutex())); - return JhmLockIterator(this->_t.beginDirty(), jlock); - } - -protected: - - /** - * 删除数æ®çš„函数对象 - */ - ToDoFunctor *_todo_of; -}; - -} - -#endif diff --git a/cpp/servant/libservant/AdapterProxy.cpp b/cpp/servant/libservant/AdapterProxy.cpp deleted file mode 100644 index 10a8bec51..000000000 --- a/cpp/servant/libservant/AdapterProxy.cpp +++ /dev/null @@ -1,815 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/AdapterProxy.h" -#include "servant/Communicator.h" -#include "servant/StatReport.h" -#include "servant/Application.h" -#include "servant/AdminF.h" -#include "servant/AppCache.h" -#include "servant/TarsLogger.h" -#include "tup/tup.h" -#include "servant/StatF.h" -#include "servant/StatReport.h" -#include "util/tc_nghttp2.h" -#include "util/tc_http2clientmgr.h" - - -namespace tars -{ - -TC_Atomic AdapterProxy::_idGen; - -AdapterProxy::AdapterProxy(ObjectProxy * pObjectProxy,const EndpointInfo &ep,Communicator* pCom) -: _communicator(pCom) -, _objectProxy(pObjectProxy) -, _endpoint(ep) -, _activeStateInReg(true) -, _activeStatus(true) -, _totalInvoke(0) -, _timeoutInvoke(0) -, _nextFinishInvokeTime(0) -, _frequenceFailInvoke(0) -, _frequenceFailTime(0) -, _nextRetryTime(0) -, _connTimeout(false) -, _connExc(false) -, _connExcCnt(0) -, _staticWeight(0) -, _timeoutLogFlag(false) -, _noSendQueueLimit(1000) -, _maxSampleCount(1000) -, _sampleRate(0) -, _id(_idGen.inc()) -{ - _timeoutQueue.reset(new TC_TimeoutQueueNew()); - - if(pObjectProxy->getCommunicatorEpoll()) - { - _noSendQueueLimit = pObjectProxy->getCommunicatorEpoll()->getNoSendQueueLimit(); - } - - if(_communicator) - { - _timeoutLogFlag = _communicator->getTimeoutLogFlag(); - } - - if (ep.type() == EndpointInfo::UDP) - { - _trans.reset(new UdpTransceiver(this, ep)); - } - else - { - _trans.reset(new TcpTransceiver(this, ep)); - } - - //åˆå§‹åŒ–statçš„headä¿¡æ¯ - initStatHead(); -} - -AdapterProxy::~AdapterProxy() -{ -} - -string AdapterProxy::getSlaveName(const string& sSlaveName) -{ - string::size_type pos = sSlaveName.find("."); - if (pos != string::npos) - { - pos = sSlaveName.find(".", pos + 1); - if (pos != string::npos) - { - return sSlaveName.substr(0, pos); - } - } - - return sSlaveName; -} - -void AdapterProxy::initStatHead() -{ - vector v; - if(!ClientConfig::SetDivision.empty() && - StatReport::divison2SetInfo(ClientConfig::SetDivision, v)) //主调(client)å¯ç”¨set - { - _statHead.masterName = StatReport::trimAndLimitStr(ClientConfig::ModuleName + "." + v[0] + v[1] + v[2] + "@" + ClientConfig::TarsVersion, StatReport::MAX_MASTER_NAME_LEN); - } - else - { - _statHead.masterName = StatReport::trimAndLimitStr(ClientConfig::ModuleName + "@" + ClientConfig::TarsVersion, StatReport::MAX_MASTER_NAME_LEN); - } - - const string sSlaveName = getSlaveName(_objectProxy->name()); - string sSlaveSet = _endpoint.setDivision(); - if(!sSlaveSet.empty() && - StatReport::divison2SetInfo(sSlaveSet, v)) //被调å¯ç”¨set - { - _statHead.slaveSetName = v[0]; - _statHead.slaveSetArea = v[1]; - _statHead.slaveSetID = v[2]; - _statHead.slaveName = StatReport::trimAndLimitStr(sSlaveName + "." + v[0] + v[1] + v[2], StatReport::MAX_MASTER_NAME_LEN); - } - else - { - _statHead.slaveName = StatReport::trimAndLimitStr(sSlaveName, StatReport::MAX_MASTER_NAME_LEN); - } - - _statHead.slaveIp = StatReport::trimAndLimitStr(_endpoint.host(), StatReport::MAX_MASTER_IP_LEN); - _statHead.slavePort = _endpoint.port(); - _statHead.returnValue = 0; -} - -int AdapterProxy::invoke(ReqMessage * msg) -{ - assert(_trans != NULL); - - TLOGINFO("[TARS][AdapterProxy::invoke objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << endl); - - //未å‘链表有长度é™åˆ¶ - if(_timeoutQueue->getSendListSize() >= _noSendQueueLimit) - { - TLOGERROR("[TARS][AdapterProxy::invoke fail,ReqInfoQueue.size > " << _noSendQueueLimit << ",objname:" << _objectProxy->name() <<",desc:"<< _endpoint.desc() << endl); - msg->eStatus = ReqMessage::REQ_EXC; - - finishInvoke(msg); - - return 0; - } - - //生æˆrequestid - //tars调用 而且 ä¸æ˜¯å•å‘调用 - if(!msg->bFromRpc) - { - msg->request.iRequestId = _objectProxy->generateId(); - } - -#if TARS_HTTP2 - if (getObjProxy()->getProtoName() == HTTP2) - { - msg->request.iRequestId = getId(); // session Id - } -#endif - - _objectProxy->getProxyProtocol().requestFunc(msg->request, msg->sReqData); - - //交给连接å‘é€æ•°æ®,连接连上,bufferä¸ä¸ºç©º,直接å‘é€æ•°æ®æˆåŠŸ - if(_timeoutQueue->sendListEmpty() && _trans->sendRequest(msg->sReqData.c_str(),msg->sReqData.size()) != Transceiver::eRetError) - { - TLOGINFO("[TARS][AdapterProxy::invoke push (send) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id:" << msg->request.iRequestId << endl); - - //请求å‘é€æˆåŠŸäº†ï¼Œå•å‘调用直接返回 - if(msg->eType == ReqMessage::ONE_WAY) - { - delete msg; - msg = NULL; - - return 0; - } - - bool bFlag = _timeoutQueue->push(msg, msg->request.iRequestId, msg->request.iTimeout + msg->iBeginTime); - if(!bFlag) - { - TLOGERROR("[TARS][AdapterProxy::invoke fail1 : insert timeout queue fail,queue size:" << _timeoutQueue->size() << ",objname" <<_objectProxy->name() << ",desc" << _endpoint.desc() <eStatus = ReqMessage::REQ_EXC; - - finishInvoke(msg); - } - } - else - { - TLOGINFO("[TARS][AdapterProxy::invoke push (no send) " << _objectProxy->name() << ", " << _endpoint.desc() << ",id " << msg->request.iRequestId <push(msg,msg->request.iRequestId, msg->request.iTimeout+msg->iBeginTime, false); - if(!bFlag) - { - TLOGERROR("[TARS][AdapterProxy::invoke fail2 : insert timeout queue fail,queue size:" << _timeoutQueue->size() << "," <<_objectProxy->name() << ", " << _endpoint.desc() <eStatus = ReqMessage::REQ_EXC; - - finishInvoke(msg); - } - } - - return 0; -} - -void AdapterProxy::doInvoke() -{ - if(_timeoutQueue->sendListEmpty()) - { - return ; - } - - while(!_timeoutQueue->sendListEmpty()) - { - ReqMessage * msg = NULL; - - _timeoutQueue->getSend(msg); - - int iRet = _trans->sendRequest(msg->sReqData.c_str(), msg->sReqData.size()); - - TLOGINFO("[TARS][AdapterProxy::doInvoke sendRequest objname:" << _objectProxy->name() << ",desc" << _endpoint.desc() << ",id " << msg->request.iRequestId << ",ret" << iRet << endl); - - //å‘é€å¤±è´¥ 返回 - if(iRet == Transceiver::eRetError) - { - TLOGINFO("[TARS][AdapterProxy::doInvoke fail,errono:" << iRet << endl); - return; - } - - //请求å‘é€æˆåŠŸäº† 处ç†é‡‡æ · - //... - - //å‘é€å®Œæˆ - _timeoutQueue->popSend(msg->eType == ReqMessage::ONE_WAY); - if(msg->eType == ReqMessage::ONE_WAY) - { - delete msg; - msg = NULL; - } - - //å‘é€bufferå·²ç»æ»¡äº† è¦è¿”回 - if(iRet == Transceiver::eRetFull) - { - return; - } - } -} - - -void AdapterProxy::finishInvoke(bool bFail) -{ - TLOGINFO("[TARS][AdapterProxy::finishInvoke(bool) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",bFail:" << bFail << endl); - - time_t now = TNOW; - - CheckTimeoutInfo& info = _objectProxy->checkTimeoutInfo(); - - //å¤„äºŽå¼‚å¸¸çŠ¶æ€ å·²ç»å±è”½ - if(!_activeStatus) - { - if(!bFail) - { - //é‡è¯•æˆåŠŸ,æ¢å¤æ­£å¸¸çŠ¶æ€ - _activeStatus = true; - - //连续失败次数清零 - _frequenceFailInvoke = 0; - - _nextFinishInvokeTime = now + info.checkTimeoutInterval; - - _frequenceFailInvoke = 0; - - _totalInvoke = 1; - - _timeoutInvoke = 0; - - _connTimeout = false; - - _connExc = false; - - TLOGINFO("[TARS][AdapterProxy::finishInvoke(bool), objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",retry ok" << endl); - } - else - { - //结点已ç»å±è”½ 过æ¥å¤±è´¥çš„包ä¸ç”¨å¤„ç† - TLOGINFO("[TARS][AdapterProxy::finishInvoke(bool), objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",retry fail" << endl); - } - return; - } - - ++_totalInvoke; - - if(bFail) - { - //调用失败 - - //失败次数+1 - ++_timeoutInvoke; - - //连续失败时间间隔é‡æ–°è®¡ç®— - if(0 == _frequenceFailInvoke) - { - _frequenceFailTime = now + info.minFrequenceFailTime; - } - //连续失败次数加1 - _frequenceFailInvoke++; - - - //检查是å¦åˆ°äº†è¿žç»­å¤±è´¥æ¬¡æ•°,且至少在5s以上 - if(_frequenceFailInvoke >= info.frequenceFailInvoke && now >= _frequenceFailTime) - { - //setInactive(); - _activeStatus = false; - _nextRetryTime = TNOW + _objectProxy->checkTimeoutInfo().tryTimeInterval; - - TLOGERROR("[TARS][AdapterProxy::finishInvoke(bool) objname:"<< _objectProxy->name() - << ",desc:" << _endpoint.desc() - << ",disable frequenceFail,freqtimeout:" << _frequenceFailInvoke - << ",timeout:"<< _timeoutInvoke - << ",total:" << _totalInvoke << endl); - return ; - } - } - else - { - _frequenceFailInvoke = 0; - } - - //判断一段时间内的超时比例 - if(now > _nextFinishInvokeTime) - { - _nextFinishInvokeTime = now + info.checkTimeoutInterval; - - if(bFail && _timeoutInvoke >= info.minTimeoutInvoke && _timeoutInvoke >= info.radio * _totalInvoke) - { - setInactive(); - TLOGERROR("[TARS][AdapterProxy::finishInvoke(bool) objname" << _objectProxy->name() - << ",desc:" << _endpoint.desc() - << ",disable radioFail,freqtimeout:" << _frequenceFailInvoke - << ",timeout:"<< _timeoutInvoke - << ",total:" << _totalInvoke << endl); - } - else - { - //æ¯ä¸€åˆ†é’Ÿæ¸…空一次 - _totalInvoke = 0; - - _timeoutInvoke = 0; - } - } -} - - -bool AdapterProxy::checkActive(bool bForceConnect) -{ - time_t now = TNOW; - - TLOGINFO("[TARS][AdapterProxy::checkActive objname:" << _objectProxy->name() - << ",desc:" << _endpoint.desc() - << ",_activeStatus:" << (_activeStatus ? "enable" : "disable") - << (bForceConnect? ",forceConnect" : "") - << ",freqtimeout:" << _frequenceFailInvoke - << ",timeout:" << _timeoutInvoke - << ",_connExcCnt:"<<_connExcCnt - << ",total:" << _totalInvoke << endl); - - _trans->checkTimeout(); - - //强制é‡è¯• - if(bForceConnect) - { - //强制é‡è¯• 肯定是无效结点 - assert(!_activeStatus); - - //有效的连接 - if(_trans->isConnecting() || _trans->hasConnected()) - { - return true; - } - - _nextRetryTime = now + _objectProxy->checkTimeoutInfo().tryTimeInterval; - - //连接没有建立或者连接无效, é‡æ–°å»ºç«‹è¿žæŽ¥ - if(!_trans->isValid()) - { - - try - { - _trans->reconnect(); - } - catch(exception &ex) - { - _activeStatus = false; - - _trans->close(); - - TLOGERROR("[TARS][AdapterProxy::checkActive connect ex:" << ex.what() << endl); - } - } - - return (_trans->hasConnected() || _trans->isConnecting()); - } - - //失效且没有到下次é‡è¯•æ—¶é—´, 直接返回ä¸å¯ç”¨ - if((!_activeStatus) && (now < _nextRetryTime) ) - { - TLOGINFO("[TARS][AdapterProxy::checkActive,not reach retry time ,objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() <checkTimeoutInfo().tryTimeInterval; - } - - //连接没有建立或者连接无效, é‡æ–°å»ºç«‹è¿žæŽ¥ - if(!_trans->isValid()) - { - try - { - _trans->reconnect(); - } - catch(exception &ex) - { - _activeStatus = false; - - _trans->close(); - - TLOGERROR("[TARS][AdapterProxy::checkActive connect ex:" << ex.what() << endl); - } - } - - return (_trans->hasConnected() || _trans->isConnecting()); -} - - -void AdapterProxy::setConTimeout(bool bConTimeout) -{ - if(bConTimeout != _connTimeout) - { - TLOGERROR("[TARS][AdapterProxy::setConTimeout desc:"<< _endpoint.desc() << " connect timeout status is:" << bConTimeout << endl); - _connTimeout = bConTimeout; - if(_connTimeout) - { - setInactive(); - } - } -} - -int AdapterProxy::getConTimeout() -{ - return _objectProxy->getConTimeout(); -} - -//å±è”½ç»“点 -void AdapterProxy::setInactive() -{ - _activeStatus = false; - - _nextRetryTime = TNOW + _objectProxy->checkTimeoutInfo().tryTimeInterval; - - _trans->close(); - - TLOGINFO("[TARS][AdapterProxy::setInactive objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",inactive" << endl); -} - -void AdapterProxy::finishInvoke(ResponsePacket & rsp) -{ - TLOGINFO("[TARS][AdapterProxy::finishInvoke(ResponsePacket) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() - << ",id:" << rsp.iRequestId << endl); - - ReqMessage * msg = NULL; - - //requestid 为0 是pushæ¶ˆæ¯ - if(rsp.iRequestId == 0) - { - if(!_objectProxy->getPushCallback()) - { - TLOGERROR("[TARS][AdapterProxy::finishInvoke(ResponsePacket),request id is 0, pushcallback is null, objname:" << _objectProxy->name() - << ",desc:" << _endpoint.desc() << endl); - return; - } - - msg = new ReqMessage(); - msg->eStatus = ReqMessage::REQ_RSP; - msg->eType = ReqMessage::ASYNC_CALL; - msg->bFromRpc = true; - msg->bPush = true; - msg->proxy = _objectProxy->getServantProxy(); - msg->pObjectProxy = _objectProxy; - msg->adapter = this; - msg->callback = _objectProxy->getPushCallback(); - } - else - { - //这里的队列中的å‘é€é“¾è¡¨ä¸­çš„æ•°æ®å¯èƒ½å·²ç»åœ¨timeout的时候删除了 - bool retErase = _timeoutQueue->erase(rsp.iRequestId, msg); - - //找ä¸åˆ°æ­¤è¯·æ±‚idä¿¡æ¯ - if (!retErase) - { - if(_timeoutLogFlag) - { - TLOGERROR("[TARS][AdapterProxy::finishInvoke(ResponsePacket) objname:"<< _objectProxy->name() << ",get req-ptr NULL,may be timeout,id:" << rsp.iRequestId - << ",desc:" << _endpoint.desc() << endl); - } - return ; - } - - assert(msg->eStatus == ReqMessage::REQ_REQ); - - msg->eStatus = ReqMessage::REQ_RSP; - } - - msg->response = rsp; - - finishInvoke(msg); -} - -void AdapterProxy::finishInvoke(ReqMessage * msg) -{ - assert(msg->eStatus != ReqMessage::REQ_REQ); - - TLOGINFO("[TARS][AdapterProxy::finishInvoke(ReqMessage) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << " ,id:" << msg->response.iRequestId << endl); - - //å•å‘调用 - if(msg->eType == ReqMessage::ONE_WAY) - { - delete msg; - msg = NULL; - return ; - } - - //stat 上报调用统计 - stat(msg); - - //超时å±è”½ç»Ÿè®¡,异常ä¸ç®—超时统计 - if(msg->eStatus != ReqMessage::REQ_EXC && !msg->bPush) - { - finishInvoke(msg->response.iRet != TARSSERVERSUCCESS); - } - - //åŒæ­¥è°ƒç”¨ï¼Œå”¤é†’ServantProxy线程 - if(msg->eType == ReqMessage::SYNC_CALL) - { - if(!msg->bCoroFlag) - { - assert(msg->pMonitor); - - TC_ThreadLock::Lock sync(*(msg->pMonitor)); - msg->pMonitor->notify(); - msg->bMonitorFin = true; - } - else - { - msg->sched->put(msg->iCoroId); - } - - return ; - } - - //异步调用 - if(msg->eType == ReqMessage::ASYNC_CALL) - { - if(!msg->bCoroFlag) - { - if(msg->callback->getNetThreadProcess()) - { - //å¦‚æžœæ˜¯æœ¬çº¿ç¨‹çš„å›žè°ƒï¼Œç›´æŽ¥æœ¬çº¿ç¨‹å¤„ç† - //比如获å–endpoint - ReqMessagePtr msgPtr = msg; - try - { - msg->callback->onDispatch(msgPtr); - } - catch(exception & e) - { - TLOGERROR("[TARS][AdapterProxy::finishInvoke(ReqMessage) ex:" << e.what() << ",line:" << __LINE__ << endl); - } - catch(...) - { - TLOGERROR("[TARS]AdapterProxy::finishInvoke(ReqMessage) ex:unknown,line:" << __LINE__ << endl); - } - } - else - { - //异步回调,放入回调处ç†çº¿ç¨‹ä¸­ - _objectProxy->getCommunicatorEpoll()->pushAsyncThreadQueue(msg); - } - } - else - { - CoroParallelBasePtr ptr = msg->callback->getCoroParallelBasePtr(); - if(ptr) - { - ptr->insert(msg); - if(ptr->checkAllReqReturn()) - { - msg->sched->put(msg->iCoroId); - } - } - else - { - TLOGERROR("[TARS][AdapterProxy::finishInvoke(ReqMessage) coro parallel callback error,obj:" << _objectProxy->name() << ",desc:" << _endpoint.desc() - << ",id:" << msg->response.iRequestId << endl); - delete msg; - msg = NULL; - } - } - return; - } - - assert(false); - - return; -} - - -void AdapterProxy::doTimeout() -{ - ReqMessage * msg; - while(_timeoutQueue->timeout(msg)) - { - TLOGINFO("[TARS][AdapterProxy::doTimeout objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id " << msg->request.iRequestId << endl); - - assert(msg->eStatus == ReqMessage::REQ_REQ); - - msg->eStatus = ReqMessage::REQ_TIME; - - //有å¯èƒ½æ˜¯å•å‘调用超时了 - if(msg->eType == ReqMessage::ONE_WAY) - { - delete msg; - msg = NULL; - continue; - } - - //如果是异步调用超时 - if(msg->eType == ReqMessage::ASYNC_CALL) - { - //_connExcCnt大于0说明是网络连接异常引起的超时 - msg->response.iRet = (_connExcCnt > 0 ? TARSPROXYCONNECTERR : TARSASYNCCALLTIMEOUT); - } - - finishInvoke(msg); - } -} - -void AdapterProxy::sample(ReqMessage * msg) -{ - auto iter = _sample.find(msg->request.sFuncName); - if(iter == _sample.end()) - { - vector vBuf; - auto result = _sample.insert(make_pair(msg->request.sFuncName,vBuf)); - assert(result.second); - iter = result.first; - } - else - { - if(iter->second.size() > _maxSampleCount) - return; - } - - StatSampleMsg sample; - sample.unid = msg->sampleKey._unid; - sample.depth = msg->sampleKey._depth; - sample.width = msg->sampleKey._width; - sample.parentWidth = msg->sampleKey._parentWidth; - sample.masterName = ClientConfig::ModuleName; - sample.slaveName = StatReport::trimAndLimitStr(_objectProxy->name(), StatReport::MAX_MASTER_NAME_LEN); - sample.interfaceName = msg->request.sFuncName; - sample.masterIp = ""; - sample.slaveIp = _endpoint.host(); - - iter->second.push_back(sample); -} - -void AdapterProxy::stat(ReqMessage * msg) -{ - TLOGINFO("[TARS][AdapterProxy::stat(ReqMessage) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id:" << msg->response.iRequestId << endl); - - if(msg->bPush) - { - return ; - } - - StatMicMsgBody body; - int64_t sptime = 0; - - msg->iEndTime = TNOWMS; - - //包体信æ¯. - if(msg->eStatus == ReqMessage::REQ_RSP && TARSSERVERSUCCESS == msg->response.iRet) - { - body.count = 1; - sptime = (msg->iEndTime >= msg->iBeginTime) ? (msg->iEndTime - msg->iBeginTime) : 10000; - - body.totalRspTime = body.minRspTime = body.maxRspTime = sptime; - } - else if(msg->eStatus == ReqMessage::REQ_TIME) - { - body.timeoutCount = 1; - } - else - { - body.execCount = 1; - } - - auto it = _statBody.find(msg->request.sFuncName); - if(it != _statBody.end()) - { - merge(body,it->second); - } - else - { - _communicator->getStatReport()->getIntervCount(body.maxRspTime, body); - _statBody[msg->request.sFuncName] = body; - } - - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - ostringstream os; - os.str(""); - _statHead.displaySimple(os); - os << " "; - _statBody[msg->request.sFuncName].displaySimple(os); - TLOGINFO("[TARS][AdapterProxy::stat(ReqMessage) display:" << os.str() << endl); - } -} - -void AdapterProxy::merge(const StatMicMsgBody& inBody,StatMicMsgBody& outBody/*out*/) -{ - outBody.count += inBody.count; - outBody.timeoutCount += inBody.timeoutCount; - outBody.execCount += inBody.execCount; - outBody.totalRspTime += inBody.totalRspTime; - - if (outBody.maxRspTime < inBody.maxRspTime ) - { - outBody.maxRspTime = inBody.maxRspTime; - } - - //éž0最å°å€¼ - if (outBody.minRspTime == 0 ||(outBody.minRspTime > inBody.minRspTime && inBody.minRspTime != 0)) - { - outBody.minRspTime = inBody.minRspTime; - } - - _communicator->getStatReport()->getIntervCount(inBody.maxRspTime, outBody); -} - -void AdapterProxy::doStat(map & mStatMicMsg) -{ - TLOGINFO("[TARS][AdapterProxy::doStat objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << endl); - - for (const auto& kv : _statBody) - { - _statHead.interfaceName = kv.first; - //有数æ®å°±æ”¾åˆ°mapé‡Œé¢ - if(kv.second.count != 0 || kv.second.timeoutCount != 0 || kv.second.execCount != 0) - { - //判断是å¦å·²ç»æœ‰ç›¸åŒçš„æ•°æ®äº†ï¼Œéœ€è¦æ±‡æ€» - auto it = mStatMicMsg.find(_statHead); - if(it != mStatMicMsg.end()) - { - merge(kv.second, it->second); - } - else - { - mStatMicMsg[_statHead] = kv.second; - } - } - } - - //æ¸…ç©ºæ•°æ® - _statBody.clear(); -} - -void AdapterProxy::addConnExc(bool bExc) -{ - if(bExc) - { - if(!_connExc && _connExcCnt++ >= _objectProxy->checkTimeoutInfo().maxConnectExc) - { - if(!_connExc) - { - TLOGERROR("[TARS][AdapterProxy::addConnExc desc:"<< _endpoint.desc() << ",connect exception status is true! (connect error)"<report("AdminServant::notify:" + command); - - return NotifyObserver::getInstance()->notify(command, current); -} - -/////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/AppCache.cpp b/cpp/servant/libservant/AppCache.cpp deleted file mode 100644 index 3a3c2b586..000000000 --- a/cpp/servant/libservant/AppCache.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/AppCache.h" -#include "servant/Communicator.h" - -namespace tars -{ - -////////////////////////////////////////////////////////////////////// -// 缓存 -void AppCache::setCacheInfo(const string &sFile,int32_t iSynInterval) -{ - try - { - TC_LockT lock(*this); - - string sPath = TC_File::extractFilePath(sFile); - - TC_File::makeDirRecursive(sPath); - - _file = sFile; - - _synInterval = iSynInterval; - - if (TC_File::isFileExistEx(_file)) - { - _fileCache.parseFile(_file); - } - - //如果是旧版本数æ®ï¼ˆæ— ç‰ˆæœ¬å·)ç›´æŽ¥æ¸…ç† - if (_fileCache.get(string(APPCACHE_ROOT_PATH)+"","") == "") - { - TC_Config tFileCache; - - _fileCache = tFileCache; - } - } - catch(exception &e) - { - TLOGERROR("[TARS][AppCache setCacheInfo ex:" << e.what() << "]" << endl); - } -} - -string AppCache::get(const string & sName, const string sDomain) -{ - if(_file.empty()) - { - return ""; - } - - try - { - TC_LockT lock(*this); - string sValue = _fileCache.get(string(APPCACHE_ROOT_PATH) + "/" + sDomain + "<" + sName + ">"); - return sValue; - } - catch(exception &e) - { - TLOGERROR("[TARS][AppCache get sName:" << sName << ",ex:" << e.what() << "]" << endl); - } - return ""; -} - -map AppCache::getDomainMap(const string &path) -{ - map m; - - if(_file.empty()) - { - return m; - } - - try - { - TC_LockT lock(*this); - m = _fileCache.getDomainMap(string(APPCACHE_ROOT_PATH) + "/" + path); - } - catch(exception &e) - { - TLOGERROR("[TARS][AppCache getDomainMap path:" << path << ",ex:" << e.what() << "]" << endl); - } - return m; -} - -int AppCache::set(const string &sName,const string &sValue,const string sDomain) -{ - if(_file.empty()) - { - return -1; - } - - try - { - TC_LockT lock(*this); - - map m; - m[sName] = sValue; - - TC_Config tConf; - tConf.insertDomainParam(string(APPCACHE_ROOT_PATH)+"/"+sDomain,m,true); - if(_lastSynTime == 0) //ç¬¬ä¸€æ¬¡å†™æ•°æ® æ‰“å°tarsversion - { - m.clear(); - m["tarsversion"] = ClientConfig::TarsVersion; - tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true); - } - - { - m.clear(); - m["modify"] = TC_Common::now2str("%Y-%m-%d %H:%M:%S"); - tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true); - } - - _fileCache.joinConfig(tConf,true); - - time_t now = TNOW; - if(_lastSynTime + _synInterval/1000 > now) - { - return 0; - } - _lastSynTime = now; - - TC_File::save2file(_file,_fileCache.tostr()); - - return 0; - } - catch(exception &e) - { - TLOGERROR("[TARS][AppCache set name:" << sName << ",value:" << sValue << ",ex:" << e.what() << "]" << endl); - } - - return -1; -} - -////////////////////////////////////////////////////////////////////// -} - diff --git a/cpp/servant/libservant/AppProtocol.cpp b/cpp/servant/libservant/AppProtocol.cpp deleted file mode 100644 index 68865526f..000000000 --- a/cpp/servant/libservant/AppProtocol.cpp +++ /dev/null @@ -1,216 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_epoll_server.h" -#include "servant/AppProtocol.h" -#include "tup/Tars.h" -#include - - -#if TARS_HTTP2 -#include "util/tc_nghttp2.h" -#include "util/tc_http2clientmgr.h" - -#define MAKE_NV(NAME, VALUE, VALUELEN) \ - { \ - (uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, VALUELEN, \ - NGHTTP2_NV_FLAG_NONE \ - } - -#define MAKE_NV2(NAME, VALUE) \ - { \ - (uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1, \ - NGHTTP2_NV_FLAG_NONE \ - } - -#define MAKE_STRING_NV(NAME, VALUE) {(uint8_t*)(NAME.data()), (uint8_t*)(VALUE.data()), NAME.size(), VALUE.size(), NGHTTP2_NV_FLAG_NONE}; -#endif - -namespace tars -{ - -//TARSServerçš„å议解æžå™¨ -int AppProtocol::parseAdmin(string &in, string &out) -{ - return parse(in, out); -} - -void ProxyProtocol::tarsRequest(const RequestPacket& request, string& buff) -{ - TarsOutputStream os; - - request.writeTo(os); - - tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength()); - - buff.clear(); - - buff.reserve(sizeof(tars::Int32) + os.getLength()); - - buff.append((const char*)&iHeaderLen, sizeof(tars::Int32)); - - buff.append(os.getBuffer(), os.getLength()); -} -//////////////////////////////////////////////////////////////////////////////////// -#if TARS_HTTP2 -// nghttp2读å–请求包体,准备å‘é€ -static ssize_t reqbody_read_callback(nghttp2_session *session, int32_t stream_id, - uint8_t *buf, size_t length, - uint32_t *data_flags, - nghttp2_data_source *source, - void *user_data) -{ - std::vector* body = (std::vector* )source->ptr; - if (body->empty()) - { - *data_flags |= NGHTTP2_DATA_FLAG_EOF; - return 0; - } - - ssize_t len = length > body->size() ? body->size() : length; - std::memcpy(buf, &(*body)[0], len); - - vector::iterator end = body->begin(); - std::advance(end, len); - body->erase(body->begin(), end); - - return len; -} - -size_t http1Response(const char* recvBuffer, size_t length, std::list& done) -{ - tars::TC_HttpResponse httpRsp; - bool ok = httpRsp.decode(std::string(recvBuffer, length)); - if(!ok) - return 0; - - ResponsePacket rsp; - rsp.status["status"] = httpRsp.getResponseHeaderLine(); - for (const auto& kv : httpRsp.getHeaders()) - { - // å“应的头部 - rsp.status[kv.first] = kv.second; - } - - std::string content(httpRsp.getContent()); - rsp.sBuffer.assign(content.begin(), content.end()); - done.push_back(rsp); - return httpRsp.getHeadLength() + httpRsp.getContentLength(); -} - -std::string encodeHttp2(RequestPacket& request, TC_NgHttp2* session) -{ - std::vector nva; - - const std::string method(":method"); - nghttp2_nv nv1 = MAKE_STRING_NV(method, request.sFuncName); - if (!request.sFuncName.empty()) - nva.push_back(nv1); - - const std::string path(":path"); - nghttp2_nv nv2 = MAKE_STRING_NV(path, request.sServantName); - if (!request.sServantName.empty()) - nva.push_back(nv2); - - for (std::map::const_iterator - it(request.context.begin()); - it != request.context.end(); - ++ it) - { - nghttp2_nv nv = MAKE_STRING_NV(it->first, it->second); - nva.push_back(nv); - } - - nghttp2_data_provider* pData = NULL; - nghttp2_data_provider data; - if (!request.sBuffer.empty()) - { - pData = &data; - data.source.ptr = (void*)&request.sBuffer; - data.read_callback = reqbody_read_callback; - } - - int32_t sid = nghttp2_submit_request(session->session(), - NULL, - &nva[0], - nva.size(), - pData, - NULL); - if (sid < 0) - { - cerr << "Fatal error: nghttp2_submit_request return " << sid << endl; - return ""; - } - - request.iRequestId = sid; - nghttp2_session_send(session->session()); - - // 交给tarså‘é€ - std::string out; - out.swap(session->sendBuffer()); - return out; -} - -// ENCODE function, called by network thread -void http2Request(const RequestPacket& request, std::string& out) -{ - TC_NgHttp2* session = Http2ClientSessionManager::getInstance()->getSession(request.iRequestId); - if (session->getState() == TC_NgHttp2::None) - { - session->Init(); - session->settings(); - } - - assert (session->getState() == TC_NgHttp2::Http2); - - out = encodeHttp2(const_cast(request), session); -} - -size_t http2Response(const char* recvBuffer, size_t length, list& done, void* userptr) -{ - const int sessionId = *(int*)&userptr; - TC_NgHttp2* session = Http2ClientSessionManager::getInstance()->getSession(sessionId); - assert (session->getState() == TC_NgHttp2::Http2); - - int readlen = nghttp2_session_mem_recv(session->session(), - (const uint8_t*)recvBuffer, - length); - - if (readlen < 0) - { - throw std::runtime_error("nghttp2_session_mem_recv return error"); - return 0; - } - - std::map::const_iterator it(session->_doneResponses.begin()); - for (; it != session->_doneResponses.end(); ++ it) - { - ResponsePacket rsp; - - rsp.iRequestId = it->second.streamId; - rsp.status = it->second.headers; - rsp.sBuffer.assign(it->second.body.begin(), it->second.body.end()); - - done.push_back(rsp); - } - - session->_doneResponses.clear(); - return readlen; -} - -#endif -} - diff --git a/cpp/servant/libservant/Application.cpp b/cpp/servant/libservant/Application.cpp deleted file mode 100644 index b2c25e529..000000000 --- a/cpp/servant/libservant/Application.cpp +++ /dev/null @@ -1,1221 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_option.h" -#include "util/tc_common.h" -#include "servant/TarsNodeF.h" -#include "servant/Application.h" -#include "servant/AppProtocol.h" -#include "servant/AdminServant.h" -#include "servant/ServantHandle.h" -#include "servant/BaseF.h" -#include "servant/AppCache.h" -#include "servant/NotifyObserver.h" - -#include -#include - -#if TARS_SSL -#include "util/tc_sslmgr.h" -#endif - - -namespace tars -{ - -static void sighandler( int sig_no ) -{ - Application::terminate(); -} - -std::string ServerConfig::Application; //应用å称 -std::string ServerConfig::ServerName; //æœåŠ¡å称,一个æœåŠ¡å称å«ä¸€ä¸ªæˆ–多个æœåŠ¡æ ‡è¯† -std::string ServerConfig::LocalIp; //本机IP -std::string ServerConfig::BasePath; //应用程åºè·¯å¾„,用于ä¿å­˜è¿œç¨‹ç³»ç»Ÿé…置的本地目录 -std::string ServerConfig::DataPath; //应用程åºè·¯å¾„ï¼Œç”¨äºŽæœ¬åœ°æ•°æ® -std::string ServerConfig::Local; //本地套接字 -std::string ServerConfig::Node; //本机nodeåœ°å€ -std::string ServerConfig::Log; //æ—¥å¿—ä¸­å¿ƒåœ°å€ -std::string ServerConfig::Config; //é…ç½®ä¸­å¿ƒåœ°å€ -std::string ServerConfig::Notify; //ä¿¡æ¯é€šçŸ¥ä¸­å¿ƒ -std::string ServerConfig::LogPath; //logpath -int ServerConfig::LogSize; //log大å°(字节) -int ServerConfig::LogNum; //log个数() -std::string ServerConfig::LogLevel; //log日志级别 -std::string ServerConfig::ConfigFile; //框架é…置文件路径 -int ServerConfig::ReportFlow; //是å¦æœåŠ¡ç«¯ä¸ŠæŠ¥æ‰€æœ‰æŽ¥å£statæµé‡ 0ä¸ä¸ŠæŠ¥ 1上报 (用于éžtarsåè®®æœåŠ¡æµé‡ç»Ÿè®¡) -int ServerConfig::IsCheckSet; //是å¦å¯¹æŒ‰ç…§set规则调用进行åˆæ³•æ€§æ£€æŸ¥ 0,ä¸æ£€æŸ¥ï¼Œ1检查 -bool ServerConfig::OpenCoroutine; //是å¦å¯ç”¨å程处ç†æ–¹å¼ -size_t ServerConfig::CoroutineMemSize; //å程å ç”¨å†…å­˜ç©ºé—´çš„æœ€å¤§å¤§å° -uint32_t ServerConfig::CoroutineStackSize; //æ¯ä¸ªå程的栈大å°(默认128k) - -static string outfill(const string& s, char c = ' ', int n = 29) -{ - return (s + string(abs(n - (int)s.length()), c)); -} - -#define OUT_LINE (outfill("", '-', 50)) -#define OUT_LINE_LONG (outfill("", '=', 50)) - -/////////////////////////////////////////////////////////////////////////////////////////// -TC_Config Application::_conf; -TC_EpollServerPtr Application::_epollServer = NULL; -CommunicatorPtr Application::_communicator = NULL; - -/////////////////////////////////////////////////////////////////////////////////////////// -Application::Application() -{ -} - -Application::~Application() -{ - terminate(); -} - -TC_Config& Application::getConfig() -{ - return _conf; -} - -TC_EpollServerPtr& Application::getEpollServer() -{ - return _epollServer; -} - -CommunicatorPtr& Application::getCommunicator() -{ - return _communicator; -} - -void Application::waitForQuit() -{ - int64_t iLastCheckTime = TNOW; - int64_t iNow = iLastCheckTime; - - unsigned int iNetThreadNum = _epollServer->getNetThreadNum(); - vector vNetThread = _epollServer->getNetThread(); - - for (size_t i = 0; i < iNetThreadNum; ++i) - { - vNetThread[i]->start(); - } - - _epollServer->debug("server netthread num : " + TC_Common::tostr(iNetThreadNum)); - - while(!_epollServer->isTerminate()) - { - { - TC_ThreadLock::Lock sync(*_epollServer); - _epollServer->timedWait(5000); - } - - iNow = TNOW; - - if(iNow - iLastCheckTime > REPORT_SEND_QUEUE_INTERVAL) - { - iLastCheckTime = iNow; - - size_t n = 0; - for(size_t i = 0;i < iNetThreadNum; ++i) - { - n = n + vNetThread[i]->getSendRspSize(); - } - - if(_epollServer->_pReportRspQueue) - { - _epollServer->_pReportRspQueue->report(n); - } - } - } - - if(_epollServer->isTerminate()) - { - for(size_t i = 0; i < iNetThreadNum; ++i) - { - vNetThread[i]->terminate(); - vNetThread[i]->getThreadControl().join(); - } - - _epollServer->stopThread(); - } -} - -void Application::waitForShutdown() -{ - waitForQuit(); - - destroyApp(); - - TarsRemoteNotify::getInstance()->report("stop", true); -} - -void Application::terminate() -{ - if(_epollServer) - { - _epollServer->terminate(); - } -} - -bool Application::cmdViewStatus(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdViewStatus:" << command << " " << params << endl); - - ostringstream os; - - os << OUT_LINE_LONG << endl; - - os << outfill("[proxy config]:") << endl; - - outClient(os); - - os << OUT_LINE << "\n" << outfill("[server config]:") << endl; - - outServer(os); - - os << OUT_LINE << endl; - - outAllAdapter(os); - - result = os.str(); - - return true; -} -bool Application::cmdCloseCoreDump(const string& command, const string& params, string& result) -{ - struct rlimit tlimit; - int ret=0; - ostringstream os; - - ret = getrlimit(RLIMIT_CORE,&tlimit); - if(ret != 0) - { - TLOGERROR("error: "<logger()->setLogLevel(level); - - if(ret == 0) - { - ServerConfig::LogLevel = TC_Common::upper(level); - - result = "set log level [" + level + "] ok"; - - AppCache::getInstance()->set("logLevel",level); - } - else - { - result = "set log level [" + level + "] error"; - } - - return true; -} - -bool Application::cmdEnableDayLog(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdEnableDayLog:" << command << " " << params << endl); - - vector vParams = TC_Common::sepstr(TC_Common::trim(params),"|"); - - size_t nNum = vParams.size(); - - if(!(nNum == 2 || nNum == 3)) - { - result = "usage: tars.enabledaylog {remote|local}|[logname]|{true|false}"; - return false; - } - - if((vParams[0] != "local" && vParams[0] != "remote")) - { - result = "usage: tars.enabledaylog {remote|local}|[logname]|{true|false}"; - return false; - } - - if(nNum == 2 && (vParams[1] != "true" && vParams[1] != "false")) - { - result = "usage: tars.enabledaylog {remote|local}|[logname]|{true|false}"; - return false; - } - - if(nNum == 3 && (vParams[2] != "true" && vParams[2] != "false")) - { - result = "usage: tars.enabledaylog {remote|local}|[logname]|{true|false}"; - return false; - } - - bool bEnable = true; - string sFile; - - - if(nNum == 2) - { - bEnable = (vParams[1] == "true")?true:false; - sFile = ""; - result = "set " + vParams[0] + " " + vParams[1] + " ok"; - } - else if(nNum == 3) - { - bEnable = (vParams[2] == "true")?true:false; - sFile = vParams[1]; - result = "set " + vParams[0] + " " + vParams[1] + " "+vParams[2] + " ok"; - } - - - if(vParams[0] == "local") - { - TarsTimeLogger::getInstance()->enableLocal(sFile,bEnable); - return true; - } - - if(vParams[0] == "remote") - { - TarsTimeLogger::getInstance()->enableRemote(sFile,bEnable); - return true; - } - - result = "usage: tars.enabledaylog {remote|local}|[logname]|{true|false}"; - return false; - -} - -bool Application::cmdLoadConfig(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdLoadConfig:" << command << " " << params << endl); - - string filename = TC_Common::trim(params); - - if (TarsRemoteConfig::getInstance()->addConfig(filename, result,false)) - { - TarsRemoteNotify::getInstance()->report(result); - - return true; - } - - TarsRemoteNotify::getInstance()->report(result); - - return true; -} - -bool Application::cmdConnections(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdConnections:" << command << " " << params << endl); - - ostringstream os; - - os << OUT_LINE_LONG << endl; - - map m = _epollServer->getListenSocketInfo(); - - for(map::const_iterator it = m.begin(); it != m.end(); ++it) - { - vector v = it->second->getConnStatus(); - - os << OUT_LINE << "\n" << outfill("[adater:" + it->second->getName() + "] [connections:" + TC_Common::tostr(v.size())+ "]") << endl; - - os << outfill("conn-uid", ' ', 15) - << outfill("ip:port", ' ', 25) - << outfill("last-time", ' ', 25) - << outfill("timeout", ' ', 10) << endl; - - for(size_t i = 0; i < v.size(); i++) - { - os << outfill(TC_Common::tostr(v[i].uid), ' ', 15) - << outfill(v[i].ip + ":" + TC_Common::tostr(v[i].port), ' ', 25) - << outfill(TC_Common::tm2str(v[i].iLastRefreshTime,"%Y-%m-%d %H:%M:%S"), ' ', 25) - << outfill(TC_Common::tostr(v[i].timeout), ' ', 10) << endl; - } - } - os << OUT_LINE_LONG << endl; - - result = os.str(); - - return true; -} - -bool Application::cmdViewVersion(const string& command, const string& params, string& result) -{ - result = "$" + string(TARS_VERSION) + "$"; - return true; -} - -bool Application::cmdLoadProperty(const string& command, const string& params, string& result) -{ - try - { - TLOGINFO("Application::cmdLoadProperty:" << command << " " << params << endl); - - //é‡æ–°è§£æžé…置文件 - _conf.parseFile(ServerConfig::ConfigFile); - - string sResult = ""; - - //加载通讯器属性 - _communicator->setProperty(_conf); - - _communicator->reloadProperty(sResult); - - //加载远程对象 - ServerConfig::Log = _conf.get("/tars/application/server"); - - TarsTimeLogger::getInstance()->setLogInfo(_communicator, ServerConfig::Log, ServerConfig::Application, ServerConfig::ServerName, ServerConfig::LogPath,setDivision()); - - ServerConfig::Config = _conf.get("/tars/application/server"); - - TarsRemoteConfig::getInstance()->setConfigInfo(_communicator, ServerConfig::Config, ServerConfig::Application, ServerConfig::ServerName, ServerConfig::BasePath,setDivision()); - - ServerConfig::Notify = _conf.get("/tars/application/server"); - - TarsRemoteNotify::getInstance()->setNotifyInfo(_communicator, ServerConfig::Notify, ServerConfig::Application, ServerConfig::ServerName, setDivision()); - - result = "loaded config items:\r\n" + sResult + - "log=" + ServerConfig::Log + "\r\n" + - "config=" + ServerConfig::Config + "\r\n" + - "notify=" + ServerConfig::Notify + "\r\n"; - } - catch (TC_Config_Exception & ex) - { - result = "load config " + ServerConfig::ConfigFile + " error:" + ex.what(); - } - catch (exception &ex) - { - result = ex.what(); - } - return true; -} - -bool Application::cmdViewAdminCommands(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdViewAdminCommands:" << command << " " << params << endl); - - result =result + NotifyObserver::getInstance()->viewRegisterCommand(); - - return true; -} - -bool Application::cmdSetDyeing(const string& command, const string& params, string& result) -{ - vector vDyeingParams = TC_Common::sepstr(params, " "); - - if(vDyeingParams.size() == 2 || vDyeingParams.size() == 3) - { - ServantHelperManager::getInstance()->setDyeing(vDyeingParams[0], vDyeingParams[1], vDyeingParams.size() == 3 ? vDyeingParams[2] : ""); - - result = "DyeingKey=" + vDyeingParams[0] + "\r\n" + - "DyeingServant=" + vDyeingParams[1] + "\r\n" + - "DyeingInterface=" + (vDyeingParams.size() == 3 ? vDyeingParams[2] : "") + "\r\n"; - } - else - { - result = "Invalid parameters.Should be: dyeingKey dyeingServant [dyeingInterface]"; - } - return true; -} - -bool Application::cmdCloseCout(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdCloseCout:" << command << " " << params << endl); - - string s = TC_Common::lower(TC_Common::trim(params)); - - if(s == "yes") - { - AppCache::getInstance()->set("closeCout","1"); - } - else - { - AppCache::getInstance()->set("closeCout","0"); - } - - result = "set closeCout [" + s + "] ok"; - - return true; -} - -bool Application::cmdReloadLocator(const string& command, const string& params, string& result) -{ - TLOGINFO("Application::cmdReloadLocator:" << command << " " << params << endl); - - string sPara = TC_Common::lower(TC_Common::trim(params)); - - bool bSucc(true); - if (sPara == "reload") - { - TC_Config reloadConf; - - reloadConf.parseFile(ServerConfig::ConfigFile); - string sLocator = reloadConf.get("/tars/application/client/", ""); - - TLOGINFO(__FUNCTION__ << "|" << __LINE__ << "|conf file:" << ServerConfig::ConfigFile << "\n" - << "|sLocator:" << sLocator << endl); - - if (sLocator.empty()) - { - bSucc = false; - result = "locator info is null."; - } - else - { - _communicator->setProperty("locator", sLocator); - _communicator->reloadLocator(); - result = sLocator + " set succ."; - } - - } - else - { - result = "please input right paras."; - bSucc = false; - } - - return bSucc; -} - -void Application::outAllAdapter(ostream &os) -{ - map m = _epollServer->getListenSocketInfo(); - - for(map::const_iterator it = m.begin(); it != m.end(); ++it) - { - outAdapter(os, ServantHelperManager::getInstance()->getAdapterServant(it->second->getName()),it->second); - - os << OUT_LINE << endl; - } -} - -bool Application::addConfig(const string &filename) -{ - string result; - - if (TarsRemoteConfig::getInstance()->addConfig(filename, result, false)) - { - TarsRemoteNotify::getInstance()->report(result); - - return true; - } - TarsRemoteNotify::getInstance()->report(result); - - return true; -} - -bool Application::addAppConfig(const string &filename) -{ - string result = ""; - - // true-åªèŽ·å–应用级别é…ç½® - if (TarsRemoteConfig::getInstance()->addConfig(filename, result, true)) - - { - TarsRemoteNotify::getInstance()->report(result); - - return true; - } - - TarsRemoteNotify::getInstance()->report(result); - - return true; -} - -void Application::setHandle(TC_EpollServer::BindAdapterPtr& adapter) -{ - adapter->setHandle(); -} - -void Application::main(int argc, char *argv[]) -{ - try - { -#if TARS_SSL - SSLManager::GlobalInit(); -#endif - TC_Common::ignorePipe(); - - //解æžé…置文件 - parseConfig(argc, argv); - - //åˆå§‹åŒ–Proxy部分 - initializeClient(); - - //åˆå§‹åŒ–Server部分 - initializeServer(); - - vector adapters; - - //ç»‘å®šå¯¹è±¡å’Œç«¯å£ - bindAdapter(adapters); - - //业务应用的åˆå§‹åŒ– - initialize(); - - //输出所有adapter - outAllAdapter(cout); - - //设置HandleGroup分组,å¯åŠ¨çº¿ç¨‹ - for (size_t i = 0; i < adapters.size(); ++i) - { - string name = adapters[i]->getName(); - - string groupName = adapters[i]->getHandleGroupName(); - - if(name != groupName) - { - TC_EpollServer::BindAdapterPtr ptr = _epollServer->getBindAdapter(groupName); - - if (!ptr) - { - throw runtime_error("[TARS][adater `" + name + "` setHandle to group `" + groupName + "` fail!"); - } - - } - setHandle(adapters[i]); - } - - //å¯åŠ¨ä¸šåŠ¡å¤„ç†çº¿ç¨‹ - _epollServer->startHandle(); - _epollServer->createEpoll(); - - cout << "\n" << outfill("[initialize server] ", '.') << " [Done]" << endl; - - cout << OUT_LINE_LONG << endl; - - //动æ€åŠ è½½é…置文件 - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_LOAD_CONFIG, Application::cmdLoadConfig); - - //动æ€è®¾ç½®æ»šåŠ¨æ—¥å¿—等级 - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_SET_LOG_LEVEL, Application::cmdSetLogLevel); - - //动æ€è®¾ç½®æŒ‰å¤©æ—¥å¿—等级 - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_SET_DAYLOG_LEVEL, Application::cmdEnableDayLog); - - //查看æœåŠ¡çŠ¶æ€ - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_VIEW_STATUS, Application::cmdViewStatus); - - //查看当å‰é“¾æŽ¥çŠ¶æ€ - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_CONNECTIONS, Application::cmdConnections); - - //查看编译的TARS版本 - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_VIEW_VERSION, Application::cmdViewVersion); - - //加载é…ç½®æ–‡ä»¶ä¸­çš„å±žæ€§ä¿¡æ¯ - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_LOAD_PROPERTY, Application::cmdLoadProperty); - - //查看æœåŠ¡æ”¯æŒçš„管ç†å‘½ä»¤ - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_VIEW_ADMIN_COMMANDS, Application::cmdViewAdminCommands); - - //è®¾ç½®æŸ“è‰²ä¿¡æ¯ - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_SET_DYEING, Application::cmdSetDyeing); - - //设置æœåŠ¡çš„core limit - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_CLOSE_CORE, Application::cmdCloseCoreDump); - - //é‡æ–°åŠ è½½locatorä¿¡æ¯ - TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_RELOAD_LOCATOR, Application::cmdReloadLocator); - - //上报版本 - TARS_REPORTVERSION(TARS_VERSION); - - //å‘é€å¿ƒè·³ç»™node, 表示å¯åŠ¨äº† - TARS_KEEPALIVE(""); - - //å‘é€ç»™notify表示æœåŠ¡å¯åŠ¨äº† - TarsRemoteNotify::getInstance()->report("restart"); - - //ctrl + c能够完美结æŸæœåŠ¡ - signal(SIGINT, sighandler); - - - if(_conf.get("/tars/application/server",AppCache::getInstance()->get("closeCout")) != "0") - { - // é‡å®šå‘stdinã€stdoutã€stderr - int fd = open("/dev/null", O_RDWR ); - if(fd != -1) - { - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - } - else - { - close(0); - close(1); - close(2); - } - } - } - catch (exception &ex) - { - TarsRemoteNotify::getInstance()->report("exit: " + string(ex.what())); - - cout << "[main exception]:" << ex.what() << endl; - - terminate(); - } - - //åˆå§‹åŒ–完毕åŽ, 日志å†ä¿®æ”¹ä¸ºå¼‚æ­¥ - TarsRollLogger::getInstance()->sync(false); -} - -void Application::parseConfig(int argc, char *argv[]) -{ - TC_Option op; - - op.decode(argc, argv); - - //直接输出编译的TARS版本 - if(op.hasParam("version")) - { - cout << "TARS:" << TARS_VERSION << endl; - exit(0); - } - - //加载é…置文件 - ServerConfig::ConfigFile = op.getValue("config"); - - if(ServerConfig::ConfigFile == "") - { - cerr << "start server with config, for example: " << argv[0] << " --config=config.conf" << endl; - - exit(0); - } - - _conf.parseFile(ServerConfig::ConfigFile); -} - -TC_EpollServer::BindAdapter::EOrder Application::parseOrder(const string &s) -{ - vector vtOrder = TC_Common::sepstr(s,";, \t", false); - - if(vtOrder.size() != 2) - { - cerr << "invalid order '" << TC_Common::tostr(vtOrder) << "'."<< endl; - - exit(0); - } - if((TC_Common::lower(vtOrder[0]) == "allow")&&(TC_Common::lower(vtOrder[1]) == "deny")) - { - return TC_EpollServer::BindAdapter::ALLOW_DENY; - } - if((TC_Common::lower(vtOrder[0]) == "deny")&&(TC_Common::lower(vtOrder[1]) == "allow")) - { - return TC_EpollServer::BindAdapter::DENY_ALLOW; - } - - cerr << "invalid order '" << TC_Common::tostr(vtOrder) << "'."<< endl; - - exit(0); -} - -void Application::initializeClient() -{ - cout << "\n" << OUT_LINE_LONG << endl; - - //åˆå§‹åŒ–通信器 - _communicator = CommunicatorFactory::getInstance()->getCommunicator(_conf); - - cout << outfill("[proxy config]:") << endl; - - //输出 - outClient(cout); -#if TARS_SSL - try { - string path = _conf.get("/tars/application/clientssl/", "./"); - if (path.empty() || path[path.length() - 1] != '/') - path += "/"; - - string ca = path + _conf.get("/tars/application/clientssl/"); - string cert = path + _conf.get("/tars/application/clientssl/"); - if (cert == path) cert.clear(); - string key = path + _conf.get("/tars/application/clientssl/"); - if (key == path) key.clear(); - - if (!SSLManager::getInstance()->AddCtx("client", ca, cert, key, false)) - cout << "failed add client cert " << ca << endl; - else - cout << "succ add client cert " << ca << endl; - } - catch(...) { - } -#endif -} - -void Application::outClient(ostream &os) -{ - os << outfill("locator") << _communicator->getProperty("locator") << endl; - os << outfill("sync-invoke-timeout") << _communicator->getProperty("sync-invoke-timeout") << endl; - os << outfill("async-invoke-timeout") << _communicator->getProperty("async-invoke-timeout") << endl; - os << outfill("refresh-endpoint-interval") << _communicator->getProperty("refresh-endpoint-interval") << endl; - os << outfill("stat") << _communicator->getProperty("stat") << endl; - os << outfill("property") << _communicator->getProperty("property") << endl; - os << outfill("report-interval") << _communicator->getProperty("report-interval") << endl; - os << outfill("sample-rate") << _communicator->getProperty("sample-rate") << endl; - os << outfill("max-sample-count") << _communicator->getProperty("max-sample-count") << endl; - os << outfill("netthread") << _communicator->getProperty("netthread") << endl; - os << outfill("recvthread") << _communicator->getProperty("recvthread") << endl; - os << outfill("asyncthread") << _communicator->getProperty("asyncthread") << endl; - os << outfill("modulename") << _communicator->getProperty("modulename") << endl; - os << outfill("enableset") << _communicator->getProperty("enableset") << endl; - os << outfill("setdivision") << _communicator->getProperty("setdivision") << endl; -} - -string Application::toDefault(const string &s, const string &sDefault) -{ - if(s.empty()) - { - return sDefault; - } - return s; -} -string Application::setDivision() -{ - bool bEnableSet = TC_Common::lower(_conf.get("/tars/application", "n"))=="y"?true:false;; - - string sSetDevision = bEnableSet?_conf.get("/tars/application", ""):""; - return sSetDevision; -} - -void Application::addServantProtocol(const string& servant, const TC_EpollServer::protocol_functor& protocol) -{ - string adapterName = ServantHelperManager::getInstance()->getServantAdapter(servant); - - if (adapterName == "") - { - throw runtime_error("[TARS]addServantProtocol fail, no found adapter for servant:" + servant); - } - getEpollServer()->getBindAdapter(adapterName)->setProtocol(protocol); -} - -void Application::addServantConnProtocol(const string& servant, const TC_EpollServer::conn_protocol_functor& protocol) -{ - string adapterName = ServantHelperManager::getInstance()->getServantAdapter(servant); - - if (adapterName.empty()) - { - throw runtime_error("[TAF]addServantConnProtocol fail, no found adapter for servant:" + servant); - } - - getEpollServer()->getBindAdapter(adapterName)->setConnProtocol(protocol); -} - -void Application::addServantOnClose(const string& servant, const TC_EpollServer::close_functor& cf) -{ - string adapterName = ServantHelperManager::getInstance()->getServantAdapter(servant); - - if (adapterName.empty()) - { - throw runtime_error("[TAF]setServantOnClose fail, no found adapter for servant:" + servant); - } - - getEpollServer()->getBindAdapter(adapterName)->setOnClose(cf); -} - - -void Application::initializeServer() -{ - cout << OUT_LINE << "\n" << outfill("[server config]:") << endl; - - ServerConfig::Application = toDefault(_conf.get("/tars/application/server"), "UNKNOWN"); - - //缺çœé‡‡ç”¨è¿›ç¨‹å称 - string exe = ""; - - try - { - exe = TC_File::extractFileName(TC_File::getExePath()); - } - catch(TC_File_Exception & ex) - { - //å–失败则使用ip代替进程å - exe = _conf.get("/tars/application/server"); - } - - ServerConfig::ServerName = toDefault(_conf.get("/tars/application/server"), exe); - ServerConfig::BasePath = toDefault(_conf.get("/tars/application/server"), ".") + "/"; - ServerConfig::DataPath = toDefault(_conf.get("/tars/application/server"), ".") + "/"; - ServerConfig::LogPath = toDefault(_conf.get("/tars/application/server"), ".") + "/"; - ServerConfig::LogSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server"), "52428800"), 52428800); - ServerConfig::LogNum = TC_Common::strto(toDefault(_conf.get("/tars/application/server"), "10")); - ServerConfig::LocalIp = _conf.get("/tars/application/server"); - ServerConfig::Local = _conf.get("/tars/application/server"); - ServerConfig::Node = _conf.get("/tars/application/server"); - ServerConfig::Log = _conf.get("/tars/application/server"); - ServerConfig::Config = _conf.get("/tars/application/server"); - ServerConfig::Notify = _conf.get("/tars/application/server"); - ServerConfig::ReportFlow = _conf.get("/tars/application/server")=="0"?0:1; - ServerConfig::IsCheckSet = _conf.get("/tars/application/server","1")=="0"?0:1; - ServerConfig::OpenCoroutine = TC_Common::strto(toDefault(_conf.get("/tars/application/server"), "0")); - ServerConfig::CoroutineMemSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server"), "1073741824"), 1073741824); - ServerConfig::CoroutineStackSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server"), "131072"), 131072); - - if(ServerConfig::LocalIp.empty()) - { - vector v = TC_Socket::getLocalHosts(); - - ServerConfig::LocalIp = "127.0.0.1"; - //获å–第一个éž127.0.0.1çš„IP - for(size_t i = 0; i < v.size(); i++) - { - if(v[i] != "127.0.0.1") - { - ServerConfig::LocalIp = v[i]; - break; - } - } - } - - //è¾“å‡ºä¿¡æ¯ - outServer(cout); - - string sNetThread = _conf.get("/tars/application/server", "1"); - unsigned int iNetThreadNum = TC_Common::strto(sNetThread); - - if(iNetThreadNum < 1) - { - iNetThreadNum = 1; - cout << OUT_LINE << "\nwarning:netThreadNum < 1." << endl; - } - - //网络线程的é…置数目ä¸èƒ½15个 - if(iNetThreadNum > 15) - { - iNetThreadNum = 15; - cout << OUT_LINE << "\nwarning:netThreadNum > 15." << endl; - } - - _epollServer = new TC_EpollServer(iNetThreadNum); - - //网络线程的内存池é…ç½® - { - size_t minBlockSize = TC_Common::strto(toDefault(_conf.get("/tars/application/server"), "1024")); // 1KB - size_t maxBlockSize = TC_Common::strto(toDefault(_conf.get("/tars/application/server"), "8388608")); // 8MB - size_t maxBytes = TC_Common::strto(toDefault(_conf.get("/tars/application/server"), "67108864")); // 64MB - _epollServer->setNetThreadBufferPoolInfo(minBlockSize, maxBlockSize, maxBytes); - } - - - //åˆå§‹åŒ–æœåŠ¡æ˜¯å¦å¯¹ç©ºé“¾æŽ¥è¿›è¡Œè¶…时检查 - bool bEnable = (_conf.get("/tars/application/server","0")=="1")?true:false; - - _epollServer->EnAntiEmptyConnAttack(bEnable); - _epollServer->setEmptyConnTimeout(TC_Common::strto(toDefault(_conf.get("/tars/application/server"), "3"))); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–本地文件cache - cout << OUT_LINE << "\n" << outfill("[set file cache ]") << "OK" << endl; - AppCache::getInstance()->setCacheInfo(ServerConfig::DataPath+ServerConfig::ServerName+".tarsdat",0); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–本地Log - cout << OUT_LINE << "\n" << outfill("[set roll logger] ") << "OK" << endl; - TarsRollLogger::getInstance()->setLogInfo(ServerConfig::Application, ServerConfig::ServerName, ServerConfig::LogPath, ServerConfig::LogSize, ServerConfig::LogNum, _communicator, ServerConfig::Log); - _epollServer->setLocalLogger(TarsRollLogger::getInstance()->logger()); - - //åˆå§‹åŒ–是日志为åŒæ­¥ - TarsRollLogger::getInstance()->sync(true); - - //设置日志级别 - string level = AppCache::getInstance()->get("logLevel"); - if(level.empty()) - { - level = _conf.get("/tars/application/server","DEBUG"); - } - - TarsRollLogger::getInstance()->logger()->setLogLevel(TC_Common::upper(level)); - - ServerConfig::LogLevel = TC_Common::upper(level); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–到LogServerä»£ç† - cout << OUT_LINE << "\n" << outfill("[set time logger] ") << "OK" << endl; - bool bLogStatReport = (_conf.get("/tars/application/server", "0") == "1") ? true : false; - TarsTimeLogger::getInstance()->setLogInfo(_communicator, ServerConfig::Log, ServerConfig::Application, ServerConfig::ServerName, ServerConfig::LogPath, setDivision(), bLogStatReport); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–到é…ç½®ä¸­å¿ƒä»£ç† - cout << OUT_LINE << "\n" << outfill("[set remote config] ") << "OK" << endl; - TarsRemoteConfig::getInstance()->setConfigInfo(_communicator, ServerConfig::Config, ServerConfig::Application, ServerConfig::ServerName, ServerConfig::BasePath,setDivision()); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–到信æ¯ä¸­å¿ƒä»£ç† - cout << OUT_LINE << "\n" << outfill("[set remote notify] ") << "OK" << endl; - TarsRemoteNotify::getInstance()->setNotifyInfo(_communicator, ServerConfig::Notify, ServerConfig::Application, ServerConfig::ServerName, setDivision()); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–到Nodeçš„ä»£ç† - cout << OUT_LINE << "\n" << outfill("[set node proxy]") << "OK" << endl; - TarsNodeFHelper::getInstance()->setNodeInfo(_communicator, ServerConfig::Node, ServerConfig::Application, ServerConfig::ServerName); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - //åˆå§‹åŒ–管ç†å¯¹è±¡ - cout << OUT_LINE << "\n" << outfill("[set admin adapter]") << "OK" << endl; - - if(!ServerConfig::Local.empty()) - { - ServantHelperManager::getInstance()->addServant("AdminObj"); - - ServantHelperManager::getInstance()->setAdapterServant("AdminAdapter", "AdminObj"); - - TC_EpollServer::BindAdapterPtr lsPtr = new TC_EpollServer::BindAdapter(_epollServer.get()); - - lsPtr->setName("AdminAdapter"); - - lsPtr->setEndpoint(ServerConfig::Local); - - lsPtr->setMaxConns(TC_EpollServer::BindAdapter::DEFAULT_MAX_CONN); - - lsPtr->setQueueCapacity(TC_EpollServer::BindAdapter::DEFAULT_QUEUE_CAP); - - lsPtr->setQueueTimeout(TC_EpollServer::BindAdapter::DEFAULT_QUEUE_TIMEOUT); - - lsPtr->setProtocolName("tars"); - - lsPtr->setProtocol(AppProtocol::parse); - - lsPtr->setHandleGroupName("AdminAdapter"); - - lsPtr->setHandleNum(1); - - lsPtr->setHandle(); - - _epollServer->bind(lsPtr); - } - - //队列å–å¹³å‡å€¼ - if(!_communicator->getProperty("property").empty()) - { - string sRspQueue(""); - sRspQueue += ServerConfig::Application; - sRspQueue += "."; - sRspQueue += ServerConfig::ServerName; - sRspQueue += ".sendrspqueue"; - - PropertyReportPtr p; - p = _communicator->getStatReport()->createPropertyReport(sRspQueue, PropertyReport::avg()); - - _epollServer->_pReportRspQueue = p.get(); - } - -#if TARS_SSL - try { - string path = _conf.get("/tars/application/serverssl/", "./"); - if (path.empty() || path[path.length() - 1] != '/') - path += "/"; - - string ca = path + _conf.get("/tars/application/serverssl/"); - if (ca == path) ca.clear(); - string cert = path + _conf.get("/tars/application/serverssl/"); - string key = path + _conf.get("/tars/application/serverssl/"); - bool verifyClient = (_conf.get("/tars/application/serverssl/", "0") == "0") ? false : true; - - if (!SSLManager::getInstance()->AddCtx("server", ca, cert, key, verifyClient)) - cout << "failed add server cert " << ca << endl; - else - cout << "succ add server cert " << ca << ", verifyClient " << verifyClient << endl; - } catch(...) { - } -#endif -} - -void Application::outServer(ostream &os) -{ - os << outfill("Application") << ServerConfig::Application << endl; - os << outfill("ServerName") << ServerConfig::ServerName << endl; - os << outfill("BasePath") << ServerConfig::BasePath << endl; - os << outfill("DataPath") << ServerConfig::DataPath << endl; - os << outfill("LocalIp") << ServerConfig::LocalIp << endl; - os << outfill("Local") << ServerConfig::Local << endl; - os << outfill("LogPath") << ServerConfig::LogPath << endl; - os << outfill("LogSize") << ServerConfig::LogSize << endl; - os << outfill("LogNum") << ServerConfig::LogNum << endl; - os << outfill("Log") << ServerConfig::Log << endl; - os << outfill("Node") << ServerConfig::Node << endl; - os << outfill("Config") << ServerConfig::Config << endl; - os << outfill("Notify") << ServerConfig::Notify << endl; - os << outfill("OpenCoroutine") << ServerConfig::OpenCoroutine << endl; - os << outfill("CoroutineMemSize") << ServerConfig::CoroutineMemSize << endl; - os << outfill("CoroutineStackSize") << ServerConfig::CoroutineStackSize << endl; - os << outfill("CloseCout") << TC_Common::tostr(_conf.get("/tars/application/server",AppCache::getInstance()->get("closeCout")) == "0"?0:1)<< endl; - os << outfill("netthread") << TC_Common::tostr(_conf.get("/tars/application/server","1")) << endl; - os << outfill("BackPacketBuffLimit") << TC_Common::strto(toDefault(_conf.get("/tars/application/server", "0"), "0")) << endl; - - string level = AppCache::getInstance()->get("logLevel"); - if(level.empty()) - { - level = _conf.get("/tars/application/server","DEBUG"); - } - os << outfill("logLevel") << level<< endl; - - os << outfill("ReportFlow") << ServerConfig::ReportFlow<< endl; -} - -void Application::bindAdapter(vector& adapters) -{ - size_t iBackPacketBuffLimit = TC_Common::strto(toDefault(_conf.get("/tars/application/server", "0"), "0")); - - string sPrefix = ServerConfig::Application + "." + ServerConfig::ServerName + "."; - - vector adapterName; - - map servantHandles; - - if (_conf.getDomainVector("/tars/application/server", adapterName)) - { - for (size_t i = 0; i < adapterName.size(); i++) - { - string servant = _conf.get("/tars/application/server/" + adapterName[i] + ""); - - checkServantNameValid(servant, sPrefix); - - ServantHelperManager::getInstance()->setAdapterServant(adapterName[i], servant); - - TC_EpollServer::BindAdapterPtr bindAdapter = new TC_EpollServer::BindAdapter(_epollServer.get()); - - - string sLastPath = "/tars/application/server/" + adapterName[i]; - - bindAdapter->setName(adapterName[i]); - - bindAdapter->setEndpoint(_conf[sLastPath + ""]); - - bindAdapter->setMaxConns(TC_Common::strto(_conf.get(sLastPath + "", "128"))); - - bindAdapter->setOrder(parseOrder(_conf.get(sLastPath + "","allow,deny"))); - - bindAdapter->setAllow(TC_Common::sepstr(_conf[sLastPath + ""], ";,", false)); - - bindAdapter->setDeny(TC_Common::sepstr(_conf.get(sLastPath + "",""), ";,", false)); - - bindAdapter->setQueueCapacity(TC_Common::strto(_conf.get(sLastPath + "", "1024"))); - - bindAdapter->setQueueTimeout(TC_Common::strto(_conf.get(sLastPath + "", "10000"))); - - bindAdapter->setProtocolName(_conf.get(sLastPath + "", "tars")); - - if (bindAdapter->isTarsProtocol()) - { - bindAdapter->setProtocol(AppProtocol::parse); - } - - bindAdapter->setHandleGroupName(_conf.get(sLastPath + "", adapterName[i])); - - bindAdapter->setHandleNum(TC_Common::strto(_conf.get(sLastPath + "", "0"))); - - bindAdapter->setBackPacketBuffLimit(iBackPacketBuffLimit); - - _epollServer->bind(bindAdapter); - - adapters.push_back(bindAdapter); - - //队列å–å¹³å‡å€¼ - if(!_communicator->getProperty("property").empty()) - { - PropertyReportPtr p; - p = _communicator->getStatReport()->createPropertyReport(bindAdapter->getName() + ".queue", PropertyReport::avg()); - bindAdapter->_pReportQueue = p.get(); - - p = _communicator->getStatReport()->createPropertyReport(bindAdapter->getName() + ".connectRate", PropertyReport::avg()); - bindAdapter->_pReportConRate = p.get(); - - p = _communicator->getStatReport()->createPropertyReport(bindAdapter->getName() + ".timeoutNum", PropertyReport::sum()); - bindAdapter->_pReportTimeoutNum = p.get(); - } - } - } -} - -void Application::checkServantNameValid(const string& servant, const string& sPrefix) -{ - if((servant.length() <= sPrefix.length()) || (servant.substr(0, sPrefix.length()) != sPrefix)) - { - ostringstream os; - - os << "Servant '" << servant << "' error: must be start with '" << sPrefix << "'"; - - TarsRemoteNotify::getInstance()->report("exit:" + os.str()); - - cout << os.str() << endl; - - terminate(); - - exit(-1); - } -} - -void Application::outAdapter(ostream &os, const string &v, TC_EpollServer::BindAdapterPtr lsPtr) -{ - os << outfill("name") << lsPtr->getName() << endl; - os << outfill("servant") << v << endl; - os << outfill("endpoint") << lsPtr->getEndpoint().toString() << endl; - os << outfill("maxconns") << lsPtr->getMaxConns() << endl; - os << outfill("queuecap") << lsPtr->getQueueCapacity() << endl; - os << outfill("queuetimeout") << lsPtr->getQueueTimeout() << "ms" << endl; - os << outfill("order") << (lsPtr->getOrder()==TC_EpollServer::BindAdapter::ALLOW_DENY?"allow,deny":"deny,allow") << endl; - os << outfill("allow") << TC_Common::tostr(lsPtr->getAllow()) << endl; - os << outfill("deny") << TC_Common::tostr(lsPtr->getDeny()) << endl; - os << outfill("queuesize") << lsPtr->getRecvBufferSize() << endl; - os << outfill("connections") << lsPtr->getNowConnection() << endl; - os << outfill("protocol") << lsPtr->getProtocolName() << endl; - os << outfill("handlegroup") << lsPtr->getHandleGroupName() << endl; - os << outfill("handlethread") << lsPtr->getHandleNum() << endl; -} -////////////////////////////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/AsyncProcThread.cpp b/cpp/servant/libservant/AsyncProcThread.cpp deleted file mode 100644 index f4e09f5d4..000000000 --- a/cpp/servant/libservant/AsyncProcThread.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/AsyncProcThread.h" -#include "servant/Communicator.h" -#include "servant/StatReport.h" -#include "servant/TarsLogger.h" - -namespace tars -{ - -AsyncProcThread::AsyncProcThread(size_t iQueueCap) -: _terminate(false) -{ - _msgQueue = new ReqInfoQueue(iQueueCap); -} - -AsyncProcThread::~AsyncProcThread() -{ - terminate(); - - if(_msgQueue) - { - delete _msgQueue; - _msgQueue = NULL; - } -} - -void AsyncProcThread::terminate() -{ - Lock lock(*this); - - _terminate = true; - - notifyAll(); -} - -void AsyncProcThread::push_back(ReqMessage * msg) -{ - bool bFlag = _msgQueue->push_back(msg); - - { - TC_ThreadLock::Lock lock(*this); - notify(); - } - - if(!bFlag) - { - TLOGERROR("[TARS][AsyncProcThread::push_back] async_queue full." << endl); - delete msg; - msg = NULL; - } -} - -void AsyncProcThread::run() -{ - while (!_terminate) - { - ReqMessage * msg = NULL; - - //异步请求回æ¥çš„å“åº”åŒ…å¤„ç† - if(_msgQueue->empty()) - { - TC_ThreadLock::Lock lock(*this); - timedWait(1000); - } - - if (_msgQueue->pop_front(msg)) - { - //从回调对象把线程ç§æœ‰æ•°æ®ä¼ é€’到回调线程中 - ServantProxyThreadData * pServantProxyThreadData = ServantProxyThreadData::getData(); - assert(pServantProxyThreadData != NULL); - - //把染色的消æ¯è®¾ç½®åœ¨çº¿ç¨‹ç§æœ‰æ•°æ®é‡Œé¢ - pServantProxyThreadData->_dyeing = msg->bDyeing; - pServantProxyThreadData->_dyeingKey = msg->sDyeingKey; - - if(msg->adapter) - { - snprintf(pServantProxyThreadData->_szHost, sizeof(pServantProxyThreadData->_szHost), "%s", msg->adapter->endpoint().desc().c_str()); - } - - try - { - ReqMessagePtr msgPtr = msg; - msg->callback->onDispatch(msgPtr); - } - catch (exception& e) - { - TLOGERROR("[TARS][AsyncProcThread exception]:" << e.what() << endl); - } - catch (...) - { - TLOGERROR("[TARS][AsyncProcThread exception.]" << endl); - } - } - } -} -///////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/BaseNotify.cpp b/cpp/servant/libservant/BaseNotify.cpp deleted file mode 100644 index 7358cfd95..000000000 --- a/cpp/servant/libservant/BaseNotify.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/BaseNotify.h" -#include "servant/NotifyObserver.h" - -namespace tars -{ - -BaseNotify::BaseNotify() -{ -} - -BaseNotify::~BaseNotify() -{ -} - -void BaseNotify::addAdminCommandPrefix(const string& command, TAdminFunc func) -{ - TC_LockT lock(*this); - - _procFunctors.insert(std::make_pair(command, func)); - - NotifyObserver::getInstance()->registerPrefix(command, this); -} - -void BaseNotify::addAdminCommandNormal(const string& command, TAdminFunc func) -{ - TC_LockT lock(*this); - - _procFunctors.insert(std::make_pair(command, func)); - - NotifyObserver::getInstance()->registerNotify(command, this); -} - -bool BaseNotify::notify(const string& cmd, const string& params, TarsCurrentPtr current, string& result) -{ - TC_LockT lock(*this); - - map::iterator it; - - it = _procFunctors.find(cmd); - - if (it != _procFunctors.end()) - { - return (it->second)(cmd, params, result); - } - return false; -} -////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/CMakeLists.txt b/cpp/servant/libservant/CMakeLists.txt deleted file mode 100644 index dddea680e..000000000 --- a/cpp/servant/libservant/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -include_directories(${PROJECT_SOURCE_DIR}) -set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) - -aux_source_directory(. DIR_SRCS) - -add_library(tarsservant ${DIR_SRCS}) - -target_link_libraries(tarsservant) - -if(${TARS_SSL} EQUAL 1) - target_link_libraries(tarsservant; crypto; ssl) -else() - target_link_libraries(tarsservant) -endif() - -add_dependencies(tarsservant COPY-SERVENT-TARS tarsframework) - -install(TARGETS tarsservant - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) diff --git a/cpp/servant/libservant/Communicator.cpp b/cpp/servant/libservant/Communicator.cpp deleted file mode 100644 index 2d6202c1d..000000000 --- a/cpp/servant/libservant/Communicator.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_file.h" -#include "servant/Communicator.h" -#include "servant/StatReport.h" -#include "servant/TarsLogger.h" - -namespace tars -{ - -////////////////////////////////////////////////////////////////////////////////////////////// - -string ClientConfig::LocalIp = "127.0.0.1"; - -string ClientConfig::ModuleName = "unknown"; - -set ClientConfig::SetLocalIp; - -bool ClientConfig::SetOpen = false; - -string ClientConfig::SetDivision = ""; - -string ClientConfig::TarsVersion = string(TARS_VERSION); - -////////////////////////////////////////////////////////////////////////////////////////////// - -Communicator::Communicator() -: _initialized(false) -, _terminating(false) -, _clientThreadNum(1) -, _statReport(NULL) -, _timeoutLogFlag(true) -, _minTimeout(100) -{ - memset(_communicatorEpoll,0,sizeof(_communicatorEpoll)); -} - -Communicator::Communicator(TC_Config& conf, const string& domain/* = CONFIG_ROOT_PATH*/) -: _initialized(false) -, _terminating(false) -, _timeoutLogFlag(true) -{ - setProperty(conf, domain); -} - -Communicator::~Communicator() -{ - terminate(); -} - -bool Communicator::isTerminating() -{ - return _terminating; -} - -void Communicator::setProperty(TC_Config& conf, const string& domain/* = CONFIG_ROOT_PATH*/) -{ - TC_LockT lock(*this); - - conf.getDomainMap(domain, _properties); - - string defaultValue = "dft"; - if ((defaultValue == getProperty("enableset", defaultValue)) - || (defaultValue == getProperty("setdivision", defaultValue))) - { - _properties["enableset"] = conf.get("/tars/application", "n"); - _properties["setdivision"] = conf.get("/tars/application", "NULL"); - } - - initClientConfig(); -} - -void Communicator::initClientConfig() -{ - ClientConfig::SetOpen = TC_Common::lower(getProperty("enableset", "n"))=="y"?true:false; - - if (ClientConfig::SetOpen) - { - ClientConfig::SetDivision = getProperty("setdivision"); - - vector vtSetDivisions = TC_Common::sepstr(ClientConfig::SetDivision,"."); - - string sWildCard = "*"; - - if (vtSetDivisions.size()!=3 - || vtSetDivisions[0]==sWildCard - || vtSetDivisions[1]==sWildCard) - { - //set分组åä¸å¯¹æ—¶é»˜è®¤æ²¡æœ‰æ‰“å¼€set分组 - ClientConfig::SetOpen = false; - setProperty("enableset","n"); - TLOGERROR( "[TARS][set division name error:" << ClientConfig::SetDivision << ", client failed to open set]" << endl); - } - } - - ClientConfig::LocalIp = getProperty("localip", ""); - - if (ClientConfig::SetLocalIp.empty()) - { - vector v = TC_Socket::getLocalHosts(); - for (size_t i = 0; i < v.size(); i++) - { - if (v[i] != "127.0.0.1" && ClientConfig::LocalIp.empty()) - { - ClientConfig::LocalIp = v[i]; - } - ClientConfig::SetLocalIp.insert(v[i]); - } - } - - //缺çœé‡‡ç”¨è¿›ç¨‹å称 - string exe = ""; - - try - { - exe = TC_File::extractFileName(TC_File::getExePath()); - } - catch(TC_File_Exception & ex) - { - //å–失败则使用ip代替进程å - exe = ClientConfig::LocalIp; - } - - ClientConfig::ModuleName = getProperty("modulename", exe); -} - -void Communicator::setProperty(const map& properties) -{ - TC_LockT lock(*this); - - _properties = properties; - - initClientConfig(); -} - -void Communicator::setProperty(const string& name, const string& value) -{ - TC_LockT lock(*this); - - _properties[name] = value; - - initClientConfig(); -} - -string Communicator::getProperty(const string& name, const string& dft/* = ""*/) -{ - TC_LockT lock(*this); - - map::iterator it = _properties.find(name); - - if (it != _properties.end()) - { - return it->second; - } - return dft; -} - -void Communicator::reloadLocator() -{ - for (size_t i = 0; i < _clientThreadNum; ++i) - { - _communicatorEpoll[i]->getObjectProxyFactory()->loadObjectLocator(); - } -} - -int Communicator::reloadProperty(string & sResult) -{ - for(size_t i = 0; i < _clientThreadNum; ++i) - { - _communicatorEpoll[i]->getObjectProxyFactory()->loadObjectLocator(); - } - - int iReportInterval = TC_Common::strto(getProperty("report-interval", "60000")); - - int iReportTimeout = TC_Common::strto(getProperty("report-timeout", "5000")); - - int iSampleRate = TC_Common::strto(getProperty("sample-rate", "1000")); - - int iMaxSampleCount = TC_Common::strto(getProperty("max-sample-count", "100")); - - int iMaxReportSize = TC_Common::strto(getProperty("max-report-size", "1400")); - - string statObj = getProperty("stat", ""); - - string propertyObj = getProperty("property", ""); - - StatFPrx statPrx = NULL; - - if (!statObj.empty()) - { - statPrx = stringToProxy(statObj); - } - - PropertyFPrx propertyPrx = NULL; - - if (!propertyObj.empty()) - { - propertyPrx = stringToProxy(propertyObj); - } - - string sSetDivision = ClientConfig::SetOpen?ClientConfig::SetDivision:""; - _statReport->setReportInfo(statPrx, propertyPrx, ClientConfig::ModuleName, ClientConfig::LocalIp, sSetDivision, iReportInterval, iSampleRate, iMaxSampleCount, iMaxReportSize, iReportTimeout); - - sResult = "locator=" + getProperty("locator", "") + "\r\n" + - "stat=" + statObj + "\r\n" + "property=" + propertyObj + "\r\n" + - "SetDivision=" + sSetDivision + "\r\n" + - "report-interval=" + TC_Common::tostr(iReportInterval) + "\r\n" + - "report-timeout=" + TC_Common::tostr(iReportTimeout) + "\r\n" + - "sample-rate=" + TC_Common::tostr(iSampleRate) + "\r\n" + - "max-sample-count=" + TC_Common::tostr(iMaxSampleCount) + "\r\n"; - - return 0; -} - -void Communicator::initialize() -{ - TC_LockT lock(*this); - - if (_initialized) - return; - - _initialized = true; - - _servantProxyFactory = new ServantProxyFactory(this); - - - //客户端网络线程 - _clientThreadNum = TC_Common::strto(getProperty("netthread","1")); - - if(0 == _clientThreadNum) - { - _clientThreadNum = 1; - } - else if(MAX_CLIENT_THREAD_NUM < _clientThreadNum) - { - _clientThreadNum = MAX_CLIENT_THREAD_NUM; - } - - //stat总是有对象, ä¿è¯getStat返回的对象总是有效 - _statReport = new StatReport(_clientThreadNum); - - for(size_t i = 0; i < _clientThreadNum; ++i) - { - _communicatorEpoll[i] = new CommunicatorEpoll(this, i); - _communicatorEpoll[i]->start(); - } - - //åˆå§‹åŒ–ç»Ÿè®¡ä¸ŠæŠ¥æŽ¥å£ - string statObj = getProperty("stat", ""); - - string propertyObj = getProperty("property", ""); - - string moduleName = getProperty("modulename", ""); - - int iReportInterval = TC_Common::strto(getProperty("report-interval", "60000")); - - int iReportTimeout = TC_Common::strto(getProperty("report-timeout", "5000")); - - int iSampleRate = TC_Common::strto(getProperty("sample-rate", "1000")); - - int iMaxSampleCount = TC_Common::strto(getProperty("max-sample-count", "100")); - - int iMaxReportSize = TC_Common::strto(getProperty("max-report-size", "1400")); - - _timeoutLogFlag = TC_Common::strto(getProperty("timeout-log-flag", "1")); - - _minTimeout = TC_Common::strto(getProperty("min-timeout", "100")); - if(_minTimeout < 1) - _minTimeout = 1; - - StatFPrx statPrx = NULL; - if (!statObj.empty()) - { - statPrx = stringToProxy(statObj); - } - - //上报Propertyä¿¡æ¯çš„ä»£ç† - PropertyFPrx propertyPrx = NULL; - if (!propertyObj.empty()) - { - propertyPrx = stringToProxy(propertyObj); - } - - string sSetDivision = ClientConfig::SetOpen?ClientConfig::SetDivision:""; - _statReport->setReportInfo(statPrx, propertyPrx, ClientConfig::ModuleName, ClientConfig::LocalIp, sSetDivision, iReportInterval, iSampleRate, iMaxSampleCount, iMaxReportSize, iReportTimeout); -} - - -vector Communicator::getEndpoint(const string & objName) -{ - ServantProxy * pServantProxy = getServantProxy(objName); - return pServantProxy->getEndpoint(); -} - -vector Communicator::getEndpoint4All(const string & objName) -{ - ServantProxy * pServantProxy = getServantProxy(objName); - return pServantProxy->getEndpoint4All(); -} - -void Communicator::terminate() -{ - { - TC_LockT lock(*this); - - _terminating = true; - } - - if(_initialized) - { - for(size_t i = 0; i < _clientThreadNum; ++i) - { - _communicatorEpoll[i]->terminate(); - _communicatorEpoll[i]->getThreadControl().join(); - //delete _communicatorEpoll[i]; - //_communicatorEpoll[i] = NULL; - } - - if(_statReport) - { - if (_statReport->isAlive()) - { - _statReport->terminate(); - _statReport->getThreadControl().join(); - } - delete _statReport; - _statReport = NULL; - } - } - -} - -ServantProxy * Communicator::getServantProxy(const string& objectName,const string& setName) -{ - Communicator::initialize(); - - return _servantProxyFactory->getServantProxy(objectName,setName); -} - -StatReport * Communicator::getStatReport() -{ - Communicator::initialize(); - - return _statReport; -} - -ServantProxyFactory* Communicator::servantProxyFactory() -{ - return _servantProxyFactory.get(); -} -/////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/CommunicatorEpoll.cpp b/cpp/servant/libservant/CommunicatorEpoll.cpp deleted file mode 100644 index b8b6bedb9..000000000 --- a/cpp/servant/libservant/CommunicatorEpoll.cpp +++ /dev/null @@ -1,500 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/CommunicatorEpoll.h" -#include "servant/Communicator.h" -#include "servant/TarsLogger.h" -#include "servant/StatReport.h" - -using namespace std; - -namespace tars -{ - -CommunicatorEpoll::CommunicatorEpoll(Communicator * pCommunicator,size_t netThreadSeq) -: _communicator(pCommunicator) -, _terminate(false) -, _nextTime(0) -, _nextStatTime(0) -, _objectProxyFactory(NULL) -, _asyncThreadNum(3) -, _asyncSeq(0) -, _netThreadSeq(netThreadSeq) -, _reportAsyncQueue(NULL) -, _noSendQueueLimit(1000) -, _waitTimeout(100) -, _timeoutCheckInterval(100) -{ - _ep.create(1024); - - _shutdown.createSocket(); - _ep.add(_shutdown.getfd(), 0, EPOLLIN); - - //ObjectProxyFactory 对象 - _objectProxyFactory = new ObjectProxyFactory(this); - - //异步线程数 - _asyncThreadNum = TC_Common::strto(pCommunicator->getProperty("asyncthread", "3")); - - if(_asyncThreadNum == 0) - { - _asyncThreadNum = 3; - } - - if(_asyncThreadNum > MAX_CLIENT_ASYNCTHREAD_NUM) - { - _asyncThreadNum = MAX_CLIENT_ASYNCTHREAD_NUM; - } - - //节点队列未å‘é€è¯·æ±‚的大å°é™åˆ¶ - _noSendQueueLimit = TC_Common::strto(pCommunicator->getProperty("nosendqueuelimit", "1000")); - if(_noSendQueueLimit < 1000) - { - _noSendQueueLimit = 1000; - } - - //å¼‚æ­¥é˜Ÿåˆ—çš„å¤§å° - size_t iAsyncQueueCap = TC_Common::strto(pCommunicator->getProperty("asyncqueuecap", "10000")); - if(iAsyncQueueCap < 10000) - { - iAsyncQueueCap = 10000; - } - - //epollwait的超时时间 - _waitTimeout = TC_Common::strto(pCommunicator->getProperty("epollwaittimeout", "100")); - if(_waitTimeout < 1) - { - _waitTimeout = 1; - } - - //检查超时请求的时间间隔,å•ä½:ms - _timeoutCheckInterval = TC_Common::strto(pCommunicator->getProperty("timeoutcheckinterval", "100")); - if(_timeoutCheckInterval < 1) - { - _timeoutCheckInterval = 1; - } - - //创建异步线程 - for(size_t i = 0; i < _asyncThreadNum; ++i) - { - _asyncThread[i] = new AsyncProcThread(iAsyncQueueCap); - _asyncThread[i]->start(); - } - - //åˆå§‹åŒ–请求的事件通知 - for(size_t i = 0; i < MAX_CLIENT_NOTIFYEVENT_NUM; ++i) - { - _notify[i].bValid = false; - } - - //异步队列数目上报 - string moduleName = pCommunicator->getProperty("modulename", ""); - if(!moduleName.empty()) - { - PropertyReportPtr asyncQueuePtr = pCommunicator->getStatReport()->createPropertyReport(moduleName + ".asyncqueue"+TC_Common::tostr(netThreadSeq), PropertyReport::avg()); - _reportAsyncQueue = asyncQueuePtr.get(); - } -} - -CommunicatorEpoll::~CommunicatorEpoll() -{ - for(size_t i = 0;i < _asyncThreadNum; ++i) - { - if(_asyncThread[i]) - { - if (_asyncThread[i]->isAlive()) - { - _asyncThread[i]->terminate(); - _asyncThread[i]->getThreadControl().join(); - } - - delete _asyncThread[i]; - _asyncThread[i] = NULL; - } - } - - if(_objectProxyFactory) - { - delete _objectProxyFactory; - _objectProxyFactory = NULL; - } -} - -void CommunicatorEpoll::terminate() -{ - _terminate = true; - //通知epollå“应 - _ep.mod(_shutdown.getfd(), 0, EPOLLOUT); -} - -ObjectProxy * CommunicatorEpoll::getObjectProxy(const string & sObjectProxyName,const string& setName) -{ - return _objectProxyFactory->getObjectProxy(sObjectProxyName,setName); -} - -void CommunicatorEpoll::addFd(int fd, FDInfo * info, uint32_t events) -{ - _ep.add(fd,(uint64_t)info,events); -} - -void CommunicatorEpoll::delFd(int fd, FDInfo * info, uint32_t events) -{ - _ep.del(fd,(uint64_t)info,events); -} - -void CommunicatorEpoll::notify(size_t iSeq,ReqInfoQueue * msgQueue) -{ - assert(iSeq < MAX_CLIENT_NOTIFYEVENT_NUM); - - if(_notify[iSeq].bValid) - { - _ep.mod(_notify[iSeq].notify.getfd(),(long long)&_notify[iSeq].stFDInfo, EPOLLIN); - assert(_notify[iSeq].stFDInfo.p == (void*)msgQueue); - } - else - { - _notify[iSeq].stFDInfo.iType = FDInfo::ET_C_NOTIFY; - _notify[iSeq].stFDInfo.p = (void*)msgQueue; - _notify[iSeq].stFDInfo.fd = _notify[iSeq].eventFd; - _notify[iSeq].stFDInfo.iSeq = iSeq; - _notify[iSeq].notify.createSocket(); - _notify[iSeq].bValid = true; - - _ep.add(_notify[iSeq].notify.getfd(),(long long)&_notify[iSeq].stFDInfo, EPOLLIN); - } -} - -void CommunicatorEpoll::notifyDel(size_t iSeq) -{ - assert(iSeq < MAX_CLIENT_NOTIFYEVENT_NUM); - if(_notify[iSeq].bValid && NULL != _notify[iSeq].stFDInfo.p) - { - _ep.mod(_notify[iSeq].notify.getfd(),(long long)&_notify[iSeq].stFDInfo, EPOLLIN); - } -} - - -void CommunicatorEpoll::handleInputImp(Transceiver * pTransceiver) -{ - //检查连接是å¦æœ‰é”™è¯¯ - if(pTransceiver->isConnecting()) - { - int iVal = 0; - socklen_t iLen = static_cast(sizeof(int)); - if (::getsockopt(pTransceiver->fd(), SOL_SOCKET, SO_ERROR, reinterpret_cast(&iVal), &iLen) == -1 || iVal) - { - pTransceiver->close(); - pTransceiver->getAdapterProxy()->addConnExc(true); - TLOGERROR("[TARS][CommunicatorEpoll::handleInputImp] connect error " - << pTransceiver->getAdapterProxy()->endpoint().desc() - << "," << pTransceiver->getAdapterProxy()->getObjProxy()->name() - << ",_connExcCnt=" << pTransceiver->getAdapterProxy()->ConnExcCnt() - << "," << strerror(iVal) << endl); - return; - } - - pTransceiver->setConnected(); - } - - list done; - if(pTransceiver->doResponse(done) > 0) - { - list::iterator it = done.begin(); - for (; it != done.end(); ++it) - { - pTransceiver->getAdapterProxy()->finishInvoke(*it); - } - } -} - -void CommunicatorEpoll::handleOutputImp(Transceiver * pTransceiver) -{ - //检查连接是å¦æœ‰é”™è¯¯ - if(pTransceiver->isConnecting()) - { - int iVal = 0; - socklen_t iLen = static_cast(sizeof(int)); - if (::getsockopt(pTransceiver->fd(), SOL_SOCKET, SO_ERROR, reinterpret_cast(&iVal), &iLen) == -1 || iVal) - { - pTransceiver->close(); - pTransceiver->getAdapterProxy()->addConnExc(true); - TLOGERROR("[TARS][CommunicatorEpoll::handleOutputImp] connect error " - << pTransceiver->getAdapterProxy()->endpoint().desc() - << "," << pTransceiver->getAdapterProxy()->getObjProxy()->name() - << ",_connExcCnt=" << pTransceiver->getAdapterProxy()->ConnExcCnt() - << "," << strerror(iVal) << endl); - return; - } - - pTransceiver->setConnected(); - } - - pTransceiver->doRequest(); -} - -void CommunicatorEpoll::handle(FDInfo * pFDInfo, uint32_t events) -{ - try - { - assert(pFDInfo != NULL); - - //队列有消æ¯é€šçŸ¥è¿‡æ¥ - if(FDInfo::ET_C_NOTIFY == pFDInfo->iType) - { - ReqInfoQueue * pInfoQueue=(ReqInfoQueue*)pFDInfo->p; - ReqMessage * msg = NULL; - - try - { - while(pInfoQueue->pop_front(msg)) - { - //线程退出了 - if(ReqMessage::THREAD_EXIT == msg->eType) - { - assert(pInfoQueue->empty()); - - delete msg; - msg = NULL; - - _ep.del(_notify[pFDInfo->iSeq].notify.getfd(),(long long)&_notify[pFDInfo->iSeq].stFDInfo, EPOLLIN); - - delete pInfoQueue; - pInfoQueue = NULL; - - _notify[pFDInfo->iSeq].stFDInfo.p = NULL; - _notify[pFDInfo->iSeq].notify.close(); - _notify[pFDInfo->iSeq].bValid = false; - - return; - } - - try - { - msg->pObjectProxy->invoke(msg); - } - catch(exception & e) - { - TLOGERROR("[TARS]CommunicatorEpoll::handle exp:"<p; - - //先收包 - if (events & EPOLLIN) - { - try - { - handleInputImp(pTransceiver); - } - catch(exception & e) - { - TLOGERROR("[TARS]CommunicatorEpoll::handle exp:"<close(); - } - catch(exception & e) - { - TLOGERROR("[TARS]CommunicatorEpoll::handle exp:"< iNow) - { - return; - } - - //æ¯_timeoutCheckInterval检查一次 - _nextTime = iNow + _timeoutCheckInterval; - - for(size_t i = 0; i < _objectProxyFactory->getObjNum(); ++i) - { - const vector & vAdapterProxy=_objectProxyFactory->getObjectProxy(i)->getAdapters(); - for(size_t iAdapter=0;iAdapterdoTimeout(); - } - _objectProxyFactory->getObjectProxy(i)->doTimeout(); - } -} - -void CommunicatorEpoll::doStat() -{ - int64_t iNow = TNOW; - if(_nextStatTime > iNow) - return; - - //10s上报一次 - _nextStatTime = iNow + 10; - - //异步队列长度上报 - if(_reportAsyncQueue) - { - size_t n = 0; - for(size_t i = 0;i < _asyncThreadNum; ++i) - { - n = n + _asyncThread[i]->getSize(); - } - _reportAsyncQueue->report(n); - } - - StatReport::MapStatMicMsg mStatMicMsg; - - for(size_t i = 0;i < _objectProxyFactory->getObjNum(); ++i) - { - const vector & vAdapterProxy = _objectProxyFactory->getObjectProxy(i)->getAdapters(); - for(size_t iAdapter = 0;iAdapter < vAdapterProxy.size(); ++iAdapter) - { - vAdapterProxy[iAdapter]->doStat(mStatMicMsg); - } - } - - //有数æ®æ‰ä¸ŠæŠ¥ - if(mStatMicMsg.size() > 0) - { - StatReport::MapStatMicMsg* pmStatMicMsg = new StatReport::MapStatMicMsg(mStatMicMsg); - _communicator->getStatReport()->report(_netThreadSeq,pmStatMicMsg); - } -} - -void CommunicatorEpoll::pushAsyncThreadQueue(ReqMessage * msg) -{ - //å…ˆä¸è€ƒè™‘æ¯ä¸ªçº¿ç¨‹é˜Ÿåˆ—æ•°ç›®ä¸ä¸€è‡´çš„情况 - _asyncThread[_asyncSeq]->push_back(msg); - _asyncSeq ++; - - if(_asyncSeq == _asyncThreadNum) - { - _asyncSeq = 0; - } -} - -void CommunicatorEpoll::run() -{ - TLOGDEBUG("CommunicatorEpoll::run id:"<_netThreadSeq = (int)_netThreadSeq; - - while (!_terminate) - { - try - { - int iTimeout = ((_waitTimeout < _timeoutCheckInterval) ? _waitTimeout : _timeoutCheckInterval); - - int num = _ep.wait(iTimeout); - - if(_terminate) - { - break; - } - - //先处ç†epoll的网络事件 - for (int i = 0; i < num; ++i) - { - const epoll_event& ev = _ep.get(i); - uint64_t data = ev.data.u64; - - if(data == 0) - { - continue; //dataéžæŒ‡é’ˆ, 退出循环 - } - - handle((FDInfo*)data, ev.events); - } - - //处ç†è¶…时请求 - doTimeout(); - - //æ•°æ®ä¸ŠæŠ¥ - doStat(); - } - catch (exception& e) - { - TLOGERROR("[TARS][CommunicatorEpoll:run exception:" << e.what() << "]" << endl); - } - catch (...) - { - TLOGERROR("[TARS][CommunicatorEpoll:run exception.]" << endl); - } - } -} - -////////////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/CoroutineScheduler.cpp b/cpp/servant/libservant/CoroutineScheduler.cpp deleted file mode 100644 index 15abdacb8..000000000 --- a/cpp/servant/libservant/CoroutineScheduler.cpp +++ /dev/null @@ -1,910 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "servant/CoroutineScheduler.h" -#include "servant/TarsLogger.h" -#include "servant/ServantHandle.h" - -namespace tars -{ -//////////////////////////////////////////////////////// -std::size_t pagesize() -{ - static std::size_t size = ::sysconf( _SC_PAGESIZE); - return size; -} - -rlimit stacksize_limit_() -{ - rlimit limit; - - const int result = ::getrlimit( RLIMIT_STACK, & limit); - assert( 0 == result); - - return limit; -} - -rlimit stacksize_limit() -{ - static rlimit limit = stacksize_limit_(); - return limit; -} - -std::size_t page_count( std::size_t stacksize) -{ - return static_cast< std::size_t >( std::ceil(static_cast< float >(stacksize) / pagesize() ) ); -} - -bool standard_stack_allocator::is_stack_unbound() -{ - return RLIM_INFINITY == stacksize_limit().rlim_max; -} - -std::size_t standard_stack_allocator::default_stacksize() -{ - std::size_t size = 8 * minimum_stacksize(); - if ( is_stack_unbound() ) return size; - - assert( maximum_stacksize() >= minimum_stacksize() ); - return maximum_stacksize() == size ? size : (std::min)( size, maximum_stacksize() ); -} - -std::size_t standard_stack_allocator::minimum_stacksize() -{ - return 8 * 1024 + sizeof(fcontext_t) + 15; -} - -std::size_t standard_stack_allocator::maximum_stacksize() -{ - assert( ! is_stack_unbound() ); - return static_cast< std::size_t >( stacksize_limit().rlim_max); -} - -int standard_stack_allocator::allocate( stack_context & ctx, std::size_t size) -{ - assert( minimum_stacksize() <= size); - assert( is_stack_unbound() || ( maximum_stacksize() >= size) ); - - const std::size_t pages( page_count( size) + 1); - const std::size_t size_( pages * pagesize() ); - assert( 0 < size && 0 < size_); - - void * limit = ::mmap( 0, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); - - if (limit == (void *) -1) - { - TLOGERROR("[TARS][[standard_stack_allocator::allocate memory failed]" << endl); - return -1; - } - - std::memset( limit, '\0', size_); - - const int result( ::mprotect( limit, pagesize(), PROT_NONE) ); - assert( 0 == result); - - ctx.size = size_; - ctx.sp = static_cast< char * >( limit) + ctx.size; - - return 0; -} - -void standard_stack_allocator::deallocate( stack_context & ctx) -{ - assert( ctx.sp); - assert( minimum_stacksize() <= ctx.size); - assert( is_stack_unbound() || ( maximum_stacksize() >= ctx.size) ); - - void * limit = static_cast< char * >( ctx.sp) - ctx.size; - - ::munmap( limit, ctx.size); -} - -//////////////////////////////////////////////////////// -CoroutineInfo::CoroutineInfo() -: _prev(NULL) -, _next(NULL) -, _main(true) -, _scheduler(NULL) -, _uid(0) -, _eStatus(CORO_FREE) -, _ctx_to(NULL) -{ -} - -CoroutineInfo::CoroutineInfo(CoroutineScheduler* scheduler) -: _prev(NULL) -, _next(NULL) -, _main(false) -, _scheduler(scheduler) -, _uid(0) -, _eStatus(CORO_FREE) -, _ctx_to(NULL) -{ -} - -CoroutineInfo::CoroutineInfo(CoroutineScheduler* scheduler, uint32_t iUid, stack_context stack_ctx) -: _prev(NULL) -, _next(NULL) -, _main(false) -, _scheduler(scheduler) -, _uid(iUid) -, _eStatus(CORO_FREE) -, _stack_ctx(stack_ctx) -, _ctx_to(NULL) -{ -} - -CoroutineInfo::~CoroutineInfo() -{ -} - -void CoroutineInfo::registerFunc(const std::function& callback) -{ - _callback = callback; - - _init_func.coroFunc = CoroutineInfo::corotineProc; - - _init_func.args = this; - - _ctx_to = make_fcontext(_stack_ctx.sp, _stack_ctx.size, CoroutineInfo::corotineEntry); - - jump_fcontext(&_ctx_from, _ctx_to, (intptr_t)this, false); -} - -void CoroutineInfo::setStackContext(stack_context stack_ctx) -{ - _stack_ctx = stack_ctx; -} - -void CoroutineInfo::corotineEntry(intptr_t q) -{ - CoroutineInfo *coro = (CoroutineInfo*)q; - - Func func = coro->_init_func.coroFunc; - void* args = coro->_init_func.args; - - jump_fcontext(coro->_ctx_to, &(coro->_ctx_from), 0, false); - - try - { - func(args); - } - catch(std::exception &ex) - { - TLOGERROR("[TARS][CoroutineInfo::corotineEntry exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("[TARS][CoroutineInfo::corotineEntry unknown exception." << endl); - } -} - -void CoroutineInfo::corotineProc(void * args) -{ - CoroutineInfo *coro = (CoroutineInfo*)args; - - try - { - std::function cb = coro->_callback; - - cb(); - } - catch(std::exception &ex) - { - TLOGERROR("[TARS][CoroutineInfo::corotineProc exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("[TARS][CoroutineInfo::corotineProc unknown exception." << endl); - } - - CoroutineScheduler* scheduler = coro->getScheduler(); - scheduler->decUsedSize(); - scheduler->moveToFreeList(coro); - - scheduler->switchCoro(coro, &(scheduler->getMainCoroutine())); - TLOGERROR("[TARS][CoroutineInfo::corotineProc no come." << endl); -} - -////////////////////////////////////////////////////////////// -CoroutineScheduler::CoroutineScheduler() -: _terminal(false) -, _poolSize(1000) -, _stackSize(128*1024) -, _currentSize(0) -, _usedSize(0) -, _uniqId(0) -, _handle(NULL) -, _currentCoro(NULL) -, _all_coro(NULL) -{ - _all_coro = new CoroutineInfo*[_poolSize+1]; - for(size_t i = 0; i <= _poolSize; ++i) - { - _all_coro[i] = NULL; - } - - CoroutineInfo::CoroutineHeadInit(&_active); - CoroutineInfo::CoroutineHeadInit(&_avail); - CoroutineInfo::CoroutineHeadInit(&_inactive); - CoroutineInfo::CoroutineHeadInit(&_timeout); - CoroutineInfo::CoroutineHeadInit(&_free); -} - -CoroutineScheduler::~CoroutineScheduler() -{} - -void CoroutineScheduler::init(uint32_t iPoolSize, size_t iStackSize) -{ - if(iPoolSize <= 0) - { - TLOGERROR("[TARS][[CoroutineScheduler::init iPoolSize <= 0." << endl); - return ; - } - - _terminal = false; - _poolSize = iPoolSize; - _stackSize = iStackSize; - - if(_poolSize <= 100) - { - _currentSize = _poolSize; - } - else - { - _currentSize = 100; - } - - if(_all_coro != NULL) - { - delete [] _all_coro; - _all_coro = new CoroutineInfo*[_poolSize+1]; - for(size_t i = 0; i <= _poolSize; ++i) - { - _all_coro[i] = NULL; - } - } - - _usedSize = 0; - _uniqId = 0; - - int iSucc = 0; - for(size_t i = 0; i < _currentSize; ++i) - { - CoroutineInfo *coro = new CoroutineInfo(this); - - stack_context s_ctx; - int ret = _alloc.allocate(s_ctx, iStackSize); - if(ret != 0) - { - TLOGERROR("[TARS][CoroutineScheduler::init iPoolSize:" << iPoolSize << "|iStackSize:" << iStackSize << "|i:" << i << endl); - - delete coro; - coro = NULL; - break; - } - - coro->setStackContext(s_ctx); - - uint32_t iId = generateId(); - coro->setUid(iId); - coro->setStatus(CORO_FREE); - - //_free.push_front(coro); - - //_all.insert(make_pair(coro->getUid(), coro)); - - _all_coro[iId] = coro; - - CoroutineInfo::CoroutineAddTail(coro, &_free); - - ++iSucc; - } - - _currentSize = iSucc; - - _mainCoro.setUid(0); - _mainCoro.setStatus(CORO_FREE); - - _currentCoro = &_mainCoro; - - TLOGDEBUG("[TARS][CoroutineScheduler::init iPoolSize:" << _poolSize << "|iCurrentSize:" << _currentSize << "|iStackSize:" << _stackSize << endl); -} - -int CoroutineScheduler::increaseCoroPoolSize() -{ - int iInc = ((_poolSize - _currentSize) > 100) ? 100 : (_poolSize - _currentSize); - if(iInc <= 0) - { - TLOGERROR("[TARS][CoroutineScheduler::increaseCoroPoolSize full iPoolSize:" << _poolSize << "|iCurrentSize:" << _currentSize << endl); - return -1; - } - - int iSucc = 0; - for(int i = 0; i < iInc; ++i) - { - CoroutineInfo *coro = new CoroutineInfo(this); - uint32_t iId = generateId(); - coro->setUid(iId); - coro->setStatus(CORO_FREE); - - stack_context s_ctx; - int ret = _alloc.allocate(s_ctx, _stackSize); - if(ret != 0) - { - TLOGERROR("[TARS][CoroutineScheduler::increaseCoroPoolSize iPoolSize:" << _poolSize << "|iStackSize:" << _stackSize << "|i:" << i << endl); - - delete coro; - coro = NULL; - break; - } - - coro->setStackContext(s_ctx); - - _all_coro[iId] = coro; - - CoroutineInfo::CoroutineAddTail(coro, &_free); - - ++iSucc; - } - - if(iSucc == 0) - { - TLOGERROR("[TARS][CoroutineScheduler::increaseCoroPoolSize cannot create iInc:" << iInc << "|iPoolSize:" << _poolSize << endl); - return -1; - } - - _currentSize += iSucc; - - return 0; -} - -uint32_t CoroutineScheduler::createCoroutine(const std::function &callback) -{ - if(_usedSize >= _currentSize || CoroutineInfo::CoroutineHeadEmpty(&_free)) - { - int iRet = increaseCoroPoolSize(); - - if(iRet != 0) - return 0; - } - - CoroutineInfo *coro = _free._next; - assert(coro != NULL); - - CoroutineInfo::CoroutineDel(coro); - - _usedSize++; - - coro->setStatus(CORO_AVAIL); - - CoroutineInfo::CoroutineAddTail(coro, &_avail); - - coro->registerFunc(callback); - - return coro->getUid(); -} - -void CoroutineScheduler::run() -{ - while(!_terminal) - { - if(CoroutineInfo::CoroutineHeadEmpty(&_avail) && CoroutineInfo::CoroutineHeadEmpty(&_active)) - { - TC_ThreadLock::Lock lock(_monitor); - - if(_activeCoroQueue.size() <= 0) - { - _monitor.timedWait(1000); - } - } - - wakeupbytimeout(); - - wakeupbyself(); - - wakeup(); - - if(!CoroutineInfo::CoroutineHeadEmpty(&_active)) - { - int iLoop = 100; - while(iLoop > 0 && !CoroutineInfo::CoroutineHeadEmpty(&_active)) - { - CoroutineInfo *coro = _active._next; - - assert(coro != NULL); - - switchCoro(&_mainCoro, coro); - - --iLoop; - } - - } - - if(!CoroutineInfo::CoroutineHeadEmpty(&_avail)) - { - CoroutineInfo *coro = _avail._next; - - assert(coro != NULL); - - switchCoro(&_mainCoro, coro); - - } - - if(_usedSize == 0) - break; - } - - destroy(); -} - -void CoroutineScheduler::tars_run() -{ - if(!_terminal) - { - wakeupbytimeout(); - - wakeupbyself(); - - wakeup(); - - if(!CoroutineInfo::CoroutineHeadEmpty(&_active)) - { - int iLoop = 100; - while(iLoop > 0 && !CoroutineInfo::CoroutineHeadEmpty(&_active)) - { - CoroutineInfo *coro = _active._next; - - assert(coro != NULL); - - switchCoro(&_mainCoro, coro); - - --iLoop; - } - - } - - if(!CoroutineInfo::CoroutineHeadEmpty(&_avail)) - { - int iLoop = 100; - while(iLoop > 0 && !CoroutineInfo::CoroutineHeadEmpty(&_avail)) - { - CoroutineInfo *coro = _avail._next; - - assert(coro != NULL); - - switchCoro(&_mainCoro, coro); - - --iLoop; - } - - } - } -} - -void CoroutineScheduler::yield(bool bFlag) -{ - if(bFlag) - { - putbyself(_currentCoro->getUid()); - } - - moveToInactive(_currentCoro); - switchCoro(_currentCoro, &_mainCoro); -} - -void CoroutineScheduler::sleep(int iSleepTime) -{ - int64_t iNow = TNOWMS; - int64_t iTimeout = iNow + (iSleepTime >= 0 ? iSleepTime : -iSleepTime); - - _timeoutCoroId.insert(make_pair(iTimeout, _currentCoro->getUid())); - - moveToTimeout(_currentCoro); - switchCoro(_currentCoro, &_mainCoro); -} - -void CoroutineScheduler::putbyself(uint32_t iCoroId) -{ - if(!_terminal) - { - _needActiveCoroId.push_back(iCoroId); - } -} - -void CoroutineScheduler::wakeupbyself() -{ - if(!_terminal) - { - if(_needActiveCoroId.size() > 0) - { - list::iterator it = _needActiveCoroId.begin(); - while(it != _needActiveCoroId.end()) - { - CoroutineInfo *coro = _all_coro[*it]; - - assert(coro != NULL); - - moveToAvail(coro); - - ++it; - } - _needActiveCoroId.clear(); - } - } -} - -void CoroutineScheduler::put(uint32_t iCoroId) -{ - if(!_terminal) - { - _activeCoroQueue.push_back(iCoroId); - - if(_handle) - { - _handle->notifyFilter(); - } - else - { - TC_ThreadLock::Lock lock(_monitor); - - _monitor.notifyAll(); - } - } -} - -void CoroutineScheduler::wakeup() -{ - if(!_terminal) - { - if(_activeCoroQueue.size() > 0) - { - - TC_ThreadQueue >::queue_type coroIds; - _activeCoroQueue.swap(coroIds); - - TC_ThreadQueue >::queue_type::iterator it = coroIds.begin(); - - TC_ThreadQueue >::queue_type::iterator itEnd = coroIds.end(); - - while(it != itEnd) - { - CoroutineInfo *coro = _all_coro[*it]; - - assert(coro != NULL); - - moveToActive(coro); - - ++it; - } - - } - } -} - -void CoroutineScheduler::wakeupbytimeout() -{ - if(!_terminal) - { - if(_timeoutCoroId.size() > 0) - { - int64_t iNow = TNOWMS; - while(true) - { - multimap::iterator it = _timeoutCoroId.begin(); - - if(it == _timeoutCoroId.end() || it->first > iNow) - break; - - CoroutineInfo *coro = _all_coro[it->second]; - - assert(coro != NULL); - - moveToActive(coro); - - _timeoutCoroId.erase(it); - } - - } - } -} - -void CoroutineScheduler::terminate() -{ - _terminal = true; - - if(_handle) - { - _handle->notifyFilter(); - } - else - { - TC_ThreadLock::Lock lock(_monitor); - - _monitor.notifyAll(); - } -} - -uint32_t CoroutineScheduler::generateId() -{ - while (++_uniqId < 1); - - assert(_uniqId <= _poolSize); - - return _uniqId; -} - -void CoroutineScheduler::switchCoro(CoroutineInfo *from, CoroutineInfo *to) -{ - _currentCoro = to; - - jump_fcontext(from->getCtx(), to->getCtx(), 0, false); -} - -void CoroutineScheduler::moveToActive(CoroutineInfo *coro, bool bFlag) -{ - if(coro->getStatus() == CORO_INACTIVE) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_ACTIVE); - CoroutineInfo::CoroutineAddTail(coro, &_active); - } - else if(coro->getStatus() == CORO_TIMEOUT) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_ACTIVE); - CoroutineInfo::CoroutineAddTail(coro, &_active); - } - else - { - TLOGERROR("[TARS][CoroutineScheduler::moveToActive ERROR|iCoroId:" << coro->getUid() << "|tyep:" << coro->getStatus() << endl); - } -} - -void CoroutineScheduler::moveToAvail(CoroutineInfo *coro) -{ - if(coro->getStatus() == CORO_INACTIVE) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_AVAIL); - CoroutineInfo::CoroutineAddTail(coro, &_avail); - } - else - { - TLOGERROR("[TARS][CoroutineScheduler::moveToAvail ERROR:|iCoroId:" << coro->getUid() << "|tyep:" << coro->getStatus() << endl); - } -} - -void CoroutineScheduler::moveToInactive(CoroutineInfo *coro) -{ - if(coro->getStatus() == CORO_ACTIVE) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_INACTIVE); - CoroutineInfo::CoroutineAddTail(coro, &_inactive); - } - else if(coro->getStatus() == CORO_AVAIL) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_INACTIVE); - CoroutineInfo::CoroutineAddTail(coro, &_inactive); - } - else - { - TLOGERROR("[TARS][CoroutineScheduler::moveToInactive ERROR|iCoroId:" << coro->getUid() << "|tyep:" << coro->getStatus() << endl); - } -} - -void CoroutineScheduler::moveToTimeout(CoroutineInfo *coro) -{ - if(coro->getStatus() == CORO_ACTIVE) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_TIMEOUT); - CoroutineInfo::CoroutineAddTail(coro, &_timeout); - } - else if(coro->getStatus() == CORO_AVAIL) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_TIMEOUT); - CoroutineInfo::CoroutineAddTail(coro, &_timeout); - } - else - { - TLOGERROR("[TARS][CoroutineScheduler::moveToTimeout ERROR|iCoroId:" << coro->getUid() << "|tyep:" << coro->getStatus() << endl); - } -} - -void CoroutineScheduler::moveToFreeList(CoroutineInfo *coro) -{ - if(coro->getStatus() == CORO_ACTIVE) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_FREE); - CoroutineInfo::CoroutineAddTail(coro, &_free); - } - else if(coro->getStatus() == CORO_AVAIL) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_FREE); - CoroutineInfo::CoroutineAddTail(coro, &_free); - } - else if(coro->getStatus() == CORO_INACTIVE) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_FREE); - CoroutineInfo::CoroutineAddTail(coro, &_free); - } - else if(coro->getStatus() == CORO_TIMEOUT) - { - CoroutineInfo::CoroutineDel(coro); - coro->setStatus(CORO_FREE); - CoroutineInfo::CoroutineAddTail(coro, &_free); - } - else - { - TLOGERROR("[TARS][CoroutineScheduler::moveToFreeList ERROR: already free|iCoroId:" << coro->getUid() << "|tyep:" << coro->getStatus() << endl); - } -} - -void CoroutineScheduler::destroy() -{ - if(_all_coro) - { - for(size_t i = 1; i <= _poolSize; i++) - { - if(_all_coro[i]) - { - _alloc.deallocate(_all_coro[i]->getStackContext()); - } - } - delete [] _all_coro; - } -} -///////////////////////////////////////////////////////// -Coroutine::Coroutine() -: _coroSched(NULL) -, _num(1) -, _maxNum(128) -, _stackSize(128*1024) -{ -} - -Coroutine::~Coroutine() -{ - if(isAlive()) - { - terminate(); - - getThreadControl().join(); - } -} - -void Coroutine::setCoroInfo(uint32_t iNum, uint32_t iMaxNum, size_t iStackSize) -{ - _maxNum = (iMaxNum > 0 ? iMaxNum : 1); - _num = (iNum > 0 ? (iNum <= _maxNum ? iNum : _maxNum) : 1); - _stackSize = (iStackSize >= pagesize() ? iStackSize : pagesize()); -} - -void Coroutine::run() -{ - initialize(); - - handleCoro(); - - destroy(); -} - -void Coroutine::terminate() -{ - if(_coroSched) - { - _coroSched->terminate(); - } -} - -void Coroutine::handleCoro() -{ - _coroSched = new CoroutineScheduler(); - - _coroSched->init(_maxNum, _stackSize); - - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - - assert(pSptd != NULL); - - pSptd->_sched = _coroSched; - - for(uint32_t i = 0; i < _num; ++i) - { - _coroSched->createCoroutine(std::bind(&Coroutine::coroEntry, this)); - } - - _coroSched->run(); - - delete _coroSched; - _coroSched = NULL; -} - -void Coroutine::coroEntry(Coroutine *pCoro) -{ - try - { - pCoro->handle(); - } - catch(exception &ex) - { - TLOGERROR("[TARS][[Coroutine::coroEntry exception:" << ex.what() << "]" << endl); - } - catch(...) - { - TLOGERROR("[TARS][[Coroutine::coroEntry unknown exception]" << endl); - } -} - -uint32_t Coroutine::createCoroutine(const std::function &coroFunc) -{ - if(_coroSched) - { - return _coroSched->createCoroutine(coroFunc); - } - else - { - TLOGERROR("[TARS][[Coroutine::createCoroutine coro sched no init]" << endl); - } - return -1; -} - -void Coroutine::yield() -{ - if(_coroSched) - { - _coroSched->yield(); - } - else - { - throw CoroutineException("[TARS][[Coroutine::yield coro sched no init]"); - } -} - -void Coroutine::Sleep(int iSleepTime) -{ - if(_coroSched) - { - _coroSched->sleep(iSleepTime); - } - else - { - throw CoroutineException("[TARS][[Coroutine::yield coro sched no init]"); - } -} - -} diff --git a/cpp/servant/libservant/EndpointInfo.cpp b/cpp/servant/libservant/EndpointInfo.cpp deleted file mode 100644 index b7426a5c2..000000000 --- a/cpp/servant/libservant/EndpointInfo.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/EndpointInfo.h" -#include "servant/TarsLogger.h" - -namespace tars -{ -EndpointInfo::EndpointInfo() -: _port(0) -, _grid(0) -, _qos(0) -, _type(TCP) -, _weight(-1) -, _weighttype(0) -, _authType(0) -{ - _setDivision.clear(); - memset(&_addr,0,sizeof(struct sockaddr_in)); -} - -EndpointInfo::EndpointInfo(const string& host, uint16_t port, EndpointInfo::EType type, int32_t grid, const string & setDivision, int qos, int weight, unsigned int weighttype, int authType) -: _host(host) -, _port(port) -, _grid(grid) -, _qos(qos) -, _type(type) -, _setDivision(setDivision) -, _weight(weight) -, _weighttype(weighttype) -, _authType(authType) -{ - try - { - if(_weighttype == 0) - { - _weight = -1; - } - else - { - if(_weight == -1) - { - _weight = 100; - } - - _weight = (_weight > 100 ? 100 : _weight); - } - - NetworkUtil::getAddress(_host, _port, _addr); - - _cmpDesc = createCompareDesc(); - - _desc = createDesc(); - } - catch (...) - { - TLOGERROR("[ERROR:getAddress fail:" << _host << ":" << _port << "]" << endl); - } -} - -string EndpointInfo::createCompareDesc() -{ - ostringstream os; - if (_type == EndpointInfo::UDP) { os << "udp:"; } - if (_type == EndpointInfo::TCP) { os << "tcp:"; } - if (_type == EndpointInfo::SSL) { os << "ssl:"; } - os << _grid << ":" << _host << ":" << _port - << ":" << _setDivision << ":" << _qos << ":" << _weight << ":" << _weighttype << ":" << _authType; - - return os.str(); -} - -string EndpointInfo::createDesc() const -{ - ostringstream os; - - if (_type == EndpointInfo::TCP) - os << "tcp"; - else if (_type == EndpointInfo::UDP) - os << "udp"; - else - os << "ssl"; - - os << " -h " << host(); - os << " -p " << port(); - if(0 != _grid) - os << " -g " << _grid; - - if (!_setDivision.empty()) - { - os << " -s " << _setDivision; - } - - if(0 != _qos) - os << " -q " << _qos; - if(0 != _authType) - os << " -e " << _authType; - - return os.str(); -} - -bool EndpointInfo::operator == (const EndpointInfo& r) const -{ - return (_cmpDesc == r._cmpDesc); -} - -bool EndpointInfo::operator < (const EndpointInfo& r) const -{ - return (_cmpDesc < r._cmpDesc); -} - -string EndpointInfo::host() const -{ - return string(_host); -} - -int32_t EndpointInfo::grid() const -{ - return _grid; -} - -uint16_t EndpointInfo::port() const -{ - return _port; -} - -const struct sockaddr_in& EndpointInfo::addr() const -{ - return _addr; -} - -EndpointInfo::EType EndpointInfo::type() const -{ - return _type; -} - -const string& EndpointInfo::setDivision() const -{ - return _setDivision; -} -/////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/EndpointManager.cpp b/cpp/servant/libservant/EndpointManager.cpp deleted file mode 100644 index dcdca000a..000000000 --- a/cpp/servant/libservant/EndpointManager.cpp +++ /dev/null @@ -1,1935 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/EndpointManager.h" -#include "servant/ObjectProxy.h" -#include "servant/TarsLogger.h" -#include "servant/AppCache.h" -#include "servant/Application.h" -#include "servant/StatReport.h" - -namespace tars -{ -///////////////////////////////////////////////////////////////////////////// -QueryEpBase::QueryEpBase(Communicator * pComm, bool bFirstNetThread,bool bInterfaceReq) -: _communicator(pComm) -, _firstNetThread(bFirstNetThread) -, _interfaceReq(bInterfaceReq) -, _direct(false) -, _objName("") -, _invokeSetId("") -, _locator("") -, _valid(false) -, _weightType(E_LOOP) -, _requestRegistry(false) -, _requestTimeout(0) -, _timeoutInterval(5*1000) -, _refreshTime(0) -, _refreshInterval(60*1000) -, _activeEmptyInterval(10*1000) -, _failInterval(2*1000) -, _manyFailInterval(30*1000) -, _failTimesLimit(3) -, _failTimes(0) -{ - setNoDelete(true); -} - -void QueryEpBase::callback_findObjectById4All(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp) -{ - TLOGINFO("[TARS][callback_findObjectById4All _objName:" << _objName << "|ret:" << ret - << ",active:" << activeEp.size() - << ",inactive:" << inactiveEp.size() << "]" << endl); - - doEndpoints(activeEp,inactiveEp,ret); -} - -void QueryEpBase::callback_findObjectById4All_exception(tars::Int32 ret) -{ - TLOGERROR("[TARS][callback_findObjectById4All_exception _objName:" << _objName << "|ret:" << ret << "]" << endl); - - doEndpointsExp(ret); -} - -void QueryEpBase::callback_findObjectById4Any(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp) -{ - TLOGINFO("[TARS][callback_findObjectById4Any _objName:" << _objName << "|ret:" << ret - << ",active:" << activeEp.size() - << ",inactive:" << inactiveEp.size() << "]" << endl); - - doEndpoints(activeEp,inactiveEp,ret); -} - -void QueryEpBase::callback_findObjectById4Any_exception(tars::Int32 ret) -{ - TLOGERROR("[TARS][callback_findObjectById4Any_exception _objName:" << _objName << "|ret:" << ret << "]" << endl); - - doEndpointsExp(ret); -} - -void QueryEpBase::callback_findObjectByIdInSameGroup(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp) -{ - TLOGINFO("[TARS][callback_findObjectByIdInSameGroup _objName:" << _objName << "|ret:"< &activeEp, const vector & inactiveEp) -{ - TLOGINFO("[TARS][callback_findObjectByIdInSameSet _objName:" << _objName << "|ret:" << ret - << ",active:" << activeEp.size() - << ",inactive:" << inactiveEp.size() << "]" << endl); - - doEndpoints(activeEp,inactiveEp,ret); -} - -void QueryEpBase::callback_findObjectByIdInSameSet_exception( Int32 ret) -{ - TLOGERROR("[TARS][callback_findObjectByIdInSameSet_exception _objName:" << _objName << "|ret:" << ret << "]" << endl); - - doEndpointsExp(ret); -} - -void QueryEpBase::callback_findObjectByIdInSameStation( Int32 ret, const vector &activeEp, const vector &inactiveEp) -{ - TLOGINFO("[TARS][callback_findObjectByIdInSameStation _objName:" << _objName << "|ret:" << ret - << ",active:" << activeEp.size() - << ",inactive:" << inactiveEp.size() << "]" << endl); - - doEndpoints(activeEp,inactiveEp,ret); -} - -void QueryEpBase::callback_findObjectByIdInSameStation_exception( Int32 ret) -{ - TLOGERROR("[TARS][callback_findObjectByIdInSameStation_exception _objName:" << _objName << "|ret:" << ret << "]" << endl); - - doEndpointsExp(ret); -} - -int QueryEpBase::setLocatorPrx(QueryFPrx prx) -{ - _queryFPrx = prx; - - return 0; -} - -bool QueryEpBase::init(const string & sObjName,const string & sLocator,const string& setName) -{ - TLOGINFO("[TARS][QueryEpBase::init sObjName:" << sObjName << ",sLocator:" << sLocator << ",setName:" << setName << "]" << endl); - - _locator = sLocator; - - _invokeSetId = setName; - - setObjName(sObjName); - - return true; -} - -void QueryEpBase::setObjName(const string & sObjName) -{ - string sEndpoints(""); - string sInactiveEndpoints(""); - - string::size_type pos = sObjName.find_first_of('@'); - - if (pos != string::npos) - { - //[直接连接]指定æœåŠ¡çš„IP和端å£åˆ—表 - - _objName = sObjName.substr(0,pos); - - sEndpoints = sObjName.substr(pos + 1); - - _direct = true; - - _valid = true; - } - else - { - //[间接连接]通过registry查询æœåŠ¡ç«¯çš„IP和端å£åˆ—表 - - _direct = false; - _valid = false; - - _objName = sObjName; - - if(_locator.find_first_not_of('@') == string::npos) - { - TLOGERROR("[TARS][QueryEpBase::setObjName locator is not valid,_locator:" << _locator << "]" << endl); - throw TarsRegistryException("locator is not valid,_locator:" + _locator); - } - - _queryFPrx = _communicator->stringToProxy(_locator); - - string sLocatorKey = _locator; - - //如果å¯ç”¨set,则获å–按set分组的缓存 - if(ClientConfig::SetOpen) - { - sLocatorKey += "_" + ClientConfig::SetDivision; - } - - string objName = _objName + string(_invokeSetId.empty() ? "" : ":") + _invokeSetId; - - //[间接连接]第一次使用cache,如果是接å£çº§è¯·æ±‚则ä¸ä»Žç¼“å­˜è¯»å– - if(!_interfaceReq) - { - sEndpoints = AppCache::getInstance()->get(objName,sLocatorKey); - sInactiveEndpoints = AppCache::getInstance()->get("inactive_" + objName, sLocatorKey); - } - } - - setEndpoints(sEndpoints, _activeEndpoints); - setEndpoints(sInactiveEndpoints, _inactiveEndpoints); - - if(_activeEndpoints.size() > 0) - { - _valid = true; - } - - if(_activeEndpoints.size() > 0 || _inactiveEndpoints.size() > 0) - { - notifyEndpoints(_activeEndpoints, _inactiveEndpoints, true); - } -} - -void QueryEpBase::setEndpoints(const string & sEndpoints, set & setEndpoints) -{ - if(sEndpoints == "") - { - return ; - } - - bool bSameWeightType = true; - bool bFirstWeightType = true; - unsigned int iWeightType = 0; - - vector vEndpoints = TC_Common::sepstr(sEndpoints, ":", false); - - for (size_t i = 0; i < vEndpoints.size(); ++i) - { - try - { - TC_Endpoint ep(vEndpoints[i]); - - EndpointInfo::EType type; - if (ep.isSSL()) - type = EndpointInfo::SSL; - else if (ep.isTcp()) - type = EndpointInfo::TCP; - else - type = EndpointInfo::UDP; - - string sSetDivision; - - //解æžsetåˆ†ç»„ä¿¡æ¯ - { - string sep = " -s "; - size_t pos = vEndpoints[i].rfind(sep); - if (pos != string::npos) - { - sSetDivision = TC_Common::trim(vEndpoints[i].substr(pos+sep.size())); - - size_t endPos = sSetDivision.find(" "); - - if (endPos != string::npos) - { - sSetDivision = sSetDivision.substr(0, endPos); - } - } - } - - if(bFirstWeightType) - { - bFirstWeightType = false; - iWeightType = ep.getWeightType(); - } - else - { - if(ep.getWeightType() != iWeightType) - { - bSameWeightType = false; - } - } - - EndpointInfo epi(ep.getHost(), ep.getPort(), type, ep.getGrid(), sSetDivision, ep.getQos(), ep.getWeight(), ep.getWeightType(), ep.getAuthType()); - - setEndpoints.insert(epi); - } - catch (...) - { - TLOGERROR("[TARS][QueryEpBase::setEndpoints parse error,objname:" << _objName << ",endpoint:" << vEndpoints[i] << "]" << endl); - } - } - - if(bSameWeightType) - { - if(iWeightType == 1) - { - _weightType = E_STATIC_WEIGHT; - } - else - { - _weightType = E_LOOP; - } - } - else - { - _weightType = E_LOOP; - } -} - -void QueryEpBase::refreshReg(GetEndpointType type, const string & sName) -{ - if(_direct) - { - return; - } - - int64_t iNow = TNOWMS; - //æ­£åœ¨è¯·æ±‚çŠ¶æ€ è€Œä¸”è¯·æ±‚è¶…æ—¶äº†ï¼Œæˆ–è€…ç¬¬ä¸€æ¬¡ - if(_requestRegistry && _requestTimeout < iNow) - { - doEndpointsExp(0); - } - - //如果是间接连接,通过registry定时查询æœåŠ¡åˆ—表 - //æ­£åœ¨è¯·æ±‚çŠ¶æ€ è€Œä¸”è¯·æ±‚è¶…æ—¶äº† - //éžè¯·æ±‚çŠ¶æ€ åˆ°äº†ä¸‹ä¸€ä¸ªåˆ·æ–°æ—¶é—´äº† - if( (!_requestRegistry) && (_refreshTime <= iNow)) - { - _requestRegistry = true; - - //一定时间ä¸å›žè°ƒå°±ç®—超时了 - _requestTimeout = iNow + _timeoutInterval; - - TLOGINFO("[TARS][QueryEpBase::refresh,"<<_objName<<"]"< activeEp; - vector inactiveEp; - int iRet = 0; - switch(type) - { - case E_ALL: - { - iRet = _queryFPrx->findObjectById4Any(_objName,activeEp,inactiveEp); - break; - } - case E_STATION: - { - iRet = _queryFPrx->findObjectByIdInSameStation(_objName,sName,activeEp,inactiveEp); - } - case E_SET: - { - iRet = _queryFPrx->findObjectByIdInSameSet(_objName,sName,activeEp,inactiveEp); - break; - } - case E_DEFAULT: - default: - { - if(ClientConfig::SetOpen || !_invokeSetId.empty()) - { - //指定set调用时,指定set的优先级最高 - string setId = _invokeSetId.empty()?ClientConfig::SetDivision:_invokeSetId; - iRet = _queryFPrx->findObjectByIdInSameSet(_objName,setId,activeEp,inactiveEp); - } - else - { - iRet = _queryFPrx->findObjectByIdInSameGroup(_objName,activeEp,inactiveEp); - } - break; - } - } - doEndpoints(activeEp, inactiveEp, iRet, true); - } - else - { - switch(type) - { - case E_ALL: - { - _queryFPrx->async_findObjectById4Any(this,_objName); - break; - } - case E_STATION: - { - _queryFPrx->async_findObjectByIdInSameStation(this,_objName,sName); - break; - } - case E_SET: - { - _queryFPrx->async_findObjectByIdInSameSet(this,_objName,sName); - break; - } - case E_DEFAULT: - default: - { - if(ClientConfig::SetOpen || !_invokeSetId.empty()) - { - //指定set调用时,指定set的优先级最高 - string setId = _invokeSetId.empty()?ClientConfig::SetDivision:_invokeSetId; - _queryFPrx->async_findObjectByIdInSameSet(this,_objName,setId); - } - else - { - _queryFPrx->async_findObjectByIdInSameGroup(this,_objName); - } - break; - } - }//end switch - } - } - catch(TC_Exception & ex) - { - TLOGERROR("[TARS]QueryEpBase::refreshReg obj:"<<_objName<<"exception:"<& activeEp, const vector& inactiveEp, int iRet, bool bSync) -{ - if(iRet != 0) - { - doEndpointsExp(iRet); - return ; - } - - _failTimes = 0; - _requestRegistry = false; - - int64_t iNow = TNOWMS; - - //有返回æˆåŠŸçš„结点,按照正常的频率 - //如果返回空列表或者返回失败 2s去刷新一次 - //接å£è¯·æ±‚ä¸»æŽ§çš„æ–¹å¼ ä¸ç®¡æ˜¯ä¸æ˜¯ç©ºéƒ½è¦åŽ»åˆ·æ–° - if(activeEp.empty() && (!_interfaceReq) ) - { - _refreshTime = iNow + _activeEmptyInterval; - - //如果registry返回ActiveæœåŠ¡åˆ—表为空,ä¸åšæ›´æ–° - TLOGERROR("[TARS][QueryEpBase::doEndpoints, callback activeEps is empty,objname:"<< _objName << "]" << endl); - return; - } - else - { - _refreshTime = iNow + _refreshInterval; - } - - bool bNeedNotify = false; - bool bSameWeightType = true; - bool bFirstWeightType = true; - int iWeightType = 0; - - set sActiveEndpoints; - set sInactiveEndpoints; - set activeEps; - set inactiveEps; - - //生æˆactive set 用于比较 - for (uint32_t i = 0; i < activeEp.size(); ++i) - { - if(bFirstWeightType) - { - bFirstWeightType = false; - iWeightType = activeEp[i].weightType; - } - else - { - if(activeEp[i].weightType != iWeightType) - { - bSameWeightType = false; - } - } - - - // tars istcpæ„æ€å’Œè¿™é‡Œæžšä¸¾å€¼å¯¹åº” - EndpointInfo::EType type = EndpointInfo::EType(activeEp[i].istcp); - EndpointInfo ep(activeEp[i].host, activeEp[i].port, type, activeEp[i].grid, activeEp[i].setId, activeEp[i].qos, activeEp[i].weight, activeEp[i].weightType, activeEp[i].authType); - - activeEps.insert(ep); - } - - //生æˆinactive set 用于比较 - for (uint32_t i = 0; i < inactiveEp.size(); ++i) - { - // tars istcpæ„æ€å’Œè¿™é‡Œæžšä¸¾å€¼å¯¹åº” - EndpointInfo::EType type = EndpointInfo::EType(inactiveEp[i].istcp); - EndpointInfo ep(inactiveEp[i].host, inactiveEp[i].port, type, inactiveEp[i].grid, inactiveEp[i].setId, inactiveEp[i].qos, inactiveEp[i].weight, inactiveEp[i].weightType, inactiveEp[i].authType); - - inactiveEps.insert(ep); - } - - if(bSameWeightType) - { - if(iWeightType == 1) - { - _weightType = E_STATIC_WEIGHT; - } - else - { - _weightType = E_LOOP; - } - } - else - { - _weightType = E_LOOP; - } - - if(activeEps != _activeEndpoints) - { - bNeedNotify = true; - _activeEndpoints = activeEps; - - if(_firstNetThread) - { - setEndPointToCache(false); - } - } - - if(inactiveEps != _inactiveEndpoints) - { - bNeedNotify = true; - _inactiveEndpoints = inactiveEps; - - if(_firstNetThread) - { - setEndPointToCache(true); - } - } - - if(bNeedNotify) - { - notifyEndpoints(_activeEndpoints, _inactiveEndpoints, bSync); - } - - if(!_valid) - { - _valid = true; - doNotify(); - } -} - -void QueryEpBase::doEndpointsExp(int iRet) -{ - _failTimes++; - _requestRegistry = false; - - int64_t iNow = TNOWMS; - - //频率控制获å–主控失败 2秒钟å†æ›´æ–° - _refreshTime = iNow + _failInterval; - - //获å–主控连续失败3次就等30så†æ›´æ–°ä¸€æ¬¡ - //连续失败 强制设æˆæ•°æ®æ˜¯æœ‰æ•ˆçš„ - if(_failTimes > _failTimesLimit) - { - if(!_valid) - { - _valid = true; - doNotify(); - } - _refreshTime = iNow + _manyFailInterval; - } -} - -void QueryEpBase::setEndPointToCache(bool bInactive) -{ - //如果是接å£çº§è¯·æ±‚则ä¸ç¼“存到文件 - if(_interfaceReq) - { - return; - } - - string sEndpoints; - - set doEndpoints; - if(!bInactive) - { - doEndpoints = _activeEndpoints; - } - else - { - doEndpoints = _inactiveEndpoints; - } - - set::iterator iter; - iter = doEndpoints.begin(); - - for (; iter != doEndpoints.end(); ++iter) - { - //这里的超时时间 åªæ˜¯å¯¹æœåŠ¡ç«¯æœ‰æ•ˆã€‚这里的值无效。所以默认用3000了 - TC_Endpoint ep(iter->host(), iter->port(), 3000, iter->type(), iter->grid(), iter->qos(), iter->weight(), iter->getWeightType()); - ep.setAuthType(iter->authType()); - - if (!sEndpoints.empty()) - { - sEndpoints += ":"; - } - - sEndpoints += ep.toString(); - - if (!iter->setDivision().empty()) - { - sEndpoints += " -s " + iter->setDivision(); - } - } - - //如果å¯ç”¨set,则按set分组ä¿å­˜ - string sLocatorKey = _locator; - if(ClientConfig::SetOpen) - { - sLocatorKey += "_" + ClientConfig::SetDivision; - } - string objName = _objName + string(_invokeSetId.empty() ? "" : ":") + _invokeSetId; - if(bInactive) - { - AppCache::getInstance()->set("inactive_"+objName,sEndpoints,sLocatorKey); - } - else - { - AppCache::getInstance()->set(objName,sEndpoints,sLocatorKey); - } - - TLOGINFO("[TARS][setEndPointToCache,obj:" << _objName << ",invokeSetId:" << _invokeSetId << ",endpoint:" << sEndpoints << "]" << endl); -} - -///////////////////////////////////////////////////////////////////////////// -EndpointManager::EndpointManager(ObjectProxy * pObjectProxy, Communicator* pComm, const string & sObjName, bool bFirstNetThread,const string& setName) -: QueryEpBase(pComm, bFirstNetThread, false) -,_objectProxy(pObjectProxy) -,_lastRoundPosition(0) -,_update(true) -,_updateWeightInterval(60) -,_lastSWeightPosition(0) -,_consistentHashWeight(E_TC_CONHASH_KETAMAHASH) -,_consistentHash(E_TC_CONHASH_KETAMAHASH) -{ - setNetThreadProcess(true); - init(sObjName,_communicator->getProperty("locator"),setName); -} - -EndpointManager::~EndpointManager() -{ - map::iterator iterAdapter; - for(iterAdapter = _allProxys.begin();iterAdapter != _allProxys.end();iterAdapter++) - { - if(iterAdapter->second) - { - delete iterAdapter->second; - iterAdapter->second = NULL; - } - } -} - -void EndpointManager::notifyEndpoints(const set & active, const set & inactive, bool bSync) -{ - set::const_iterator iter; - map::iterator iterAdapter; - pair::iterator,bool> result; - - _activeProxys.clear(); - _regProxys.clear(); - - //æ›´æ–°active - iter = active.begin(); - for(;iter != active.end();++iter) - { - if(!_direct && _weightType == E_STATIC_WEIGHT && iter->weight() <= 0) - { - continue; - } - - iterAdapter = _allProxys.find(iter->desc()); - if(iterAdapter == _allProxys.end()) - { - AdapterProxy* ap = new AdapterProxy(_objectProxy, *iter, _communicator); - - result = _allProxys.insert(make_pair(iter->desc(),ap)); - assert(result.second); - - iterAdapter = result.first; - - _vAllProxys.push_back(ap); - } - - //该节点在主控的状æ€ä¸ºactive - iterAdapter->second->setActiveInReg(true); - - _activeProxys.push_back(iterAdapter->second); - - _regProxys.insert(make_pair(iter->desc(),iterAdapter->second)); - - //设置该节点的é™æ€æƒé‡å€¼ - iterAdapter->second->setWeight(iter->weight()); - } - - //æ›´æ–°inactive - iter = inactive.begin(); - for(;iter != inactive.end();++iter) - { - if(!_direct && _weightType == E_STATIC_WEIGHT && iter->weight() <= 0) - { - continue; - } - - iterAdapter = _allProxys.find(iter->desc()); - if(iterAdapter == _allProxys.end()) - { - AdapterProxy* ap = new AdapterProxy(_objectProxy, *iter, _communicator); - - result = _allProxys.insert(make_pair(iter->desc(),ap)); - assert(result.second); - - iterAdapter = result.first; - - _vAllProxys.push_back(ap); - } - - //该节点在主控的状æ€ä¸ºinactive - iterAdapter->second->setActiveInReg(false); - - _regProxys.insert(make_pair(iter->desc(),iterAdapter->second)); - - //设置该节点的é™æ€æƒé‡å€¼ - iterAdapter->second->setWeight(iter->weight()); - } - - //_vRegProxys 需è¦æŒ‰é¡ºåºæ¥ é‡æŽ’ - _vRegProxys.clear(); - iterAdapter = _regProxys.begin(); - for(;iterAdapter != _regProxys.end();++iterAdapter) - { - _vRegProxys.push_back(iterAdapter->second); - } - - _update = true; -} - -void EndpointManager::doNotify() -{ - _objectProxy->doInvoke(); -} - -bool EndpointManager::selectAdapterProxy(ReqMessage * msg,AdapterProxy * & pAdapterProxy) -{ - pAdapterProxy = NULL; - //刷新主控 - refreshReg(E_DEFAULT,""); - - //æ— æ•ˆçš„æ•°æ® è¿”å›žtrue - if(!_valid) - { - return true; - } - - //如果有hash,则先使用hashç­–ç•¥ - if (msg->bHash) - { - pAdapterProxy = getHashProxy(msg->iHashCode, msg->bConHash); - - return false; - } - - if(_weightType == E_STATIC_WEIGHT) - { - //æƒé‡æ¨¡å¼ - bool bStaticWeighted = false; - if(_weightType == E_STATIC_WEIGHT || msg->eType == ReqMessage::ONE_WAY) - bStaticWeighted = true; - - pAdapterProxy = getWeightedProxy(bStaticWeighted); - } - else - { - //æ™®é€šè½®è¯¢æ¨¡å¼ - pAdapterProxy = getNextValidProxy(); - } - - return false; -} - -AdapterProxy * EndpointManager::getNextValidProxy() -{ - if (_activeProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getNextValidProxy activeEndpoints is empty][obj:"<<_objName<<"]" << endl); - return NULL; - } - - vector conn; - - for(size_t i=0;i<_activeProxys.size();i++) - { - ++_lastRoundPosition; - if(_lastRoundPosition >= _activeProxys.size()) - _lastRoundPosition = 0; - - if(_activeProxys[_lastRoundPosition]->checkActive()) - { - return _activeProxys[_lastRoundPosition]; - } - - if(!_activeProxys[_lastRoundPosition]->isConnTimeout() && - !_activeProxys[_lastRoundPosition]->isConnExc()) - conn.push_back(_activeProxys[_lastRoundPosition]); - } - - if(conn.size() > 0) - { - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; -} - -AdapterProxy* EndpointManager::getHashProxy(int64_t hashCode, bool bConsistentHash) -{ - if(_weightType == E_STATIC_WEIGHT) - { - if(bConsistentHash) - { - return getConHashProxyForWeight(hashCode, true); - } - else - { - return getHashProxyForWeight(hashCode, true, _hashStaticRouterCache); - } - } - else - { - if(bConsistentHash) - { - return getConHashProxyForNormal(hashCode); - } - else - { - return getHashProxyForNormal(hashCode); - } - } -} - -AdapterProxy* EndpointManager::getHashProxyForWeight(int64_t hashCode, bool bStatic, vector &vRouterCache) -{ - if(_vRegProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getHashProxyForWeight _vRegProxys is empty], bStatic:" << bStatic << endl); - return NULL; - } - - if(checkHashStaticWeightChange(bStatic)) - { - int64_t iBegin = TNOWMS; - - updateHashProxyWeighted(bStatic); - - int64_t iEnd = TNOWMS; - - TLOGINFO("[TARS][EndpointManager::getHashProxyForWeight update bStatic:" << bStatic << "|_objName:" << _objName << "|timecost(ms):" << (iEnd - iBegin) << endl); - } - - if(vRouterCache.size() > 0) - { - size_t hash = ((int64_t)hashCode) % vRouterCache.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= vRouterCache.size()) - { - hash = hash % vRouterCache.size(); - } - - size_t iIndex = vRouterCache[hash]; - - if(iIndex >= _vRegProxys.size()) - { - iIndex = iIndex % _vRegProxys.size(); - } - - //被hash到的节点在主控是activeçš„æ‰èµ°åœ¨æµç¨‹ - if (_vRegProxys[iIndex]->isActiveInReg() && _vRegProxys[iIndex]->checkActive()) - { - return _vRegProxys[iIndex]; - } - else - { - if(_activeProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getHashProxyForWeight _activeEndpoints is empty], bStatic:" << bStatic << endl); - return NULL; - } - - //在active节点中å†æ¬¡hash - vector thisHash = _activeProxys; - vector conn; - - do - { - hash = ((int64_t)hashCode) % thisHash.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= thisHash.size()) - { - hash = hash % thisHash.size(); - } - - if (thisHash[hash]->checkActive()) - { - return thisHash[hash]; - } - if(!thisHash[hash]->isConnTimeout() && - !thisHash[hash]->isConnExc()) - { - conn.push_back(thisHash[hash]); - } - thisHash.erase(thisHash.begin() + hash); - } - while(!thisHash.empty()); - - if(conn.size() > 0) - { - hash = ((int64_t)hashCode) % conn.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= conn.size()) - { - hash = hash % conn.size(); - } - - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy *adapterProxy = conn[hash]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())]; - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; - } - } - - return getHashProxyForNormal(hashCode); -} - - -AdapterProxy* EndpointManager::getConHashProxyForWeight(int64_t hashCode, bool bStatic) -{ - if(_vRegProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getConHashProxyForWeight _vRegProxys is empty], bStatic:" << bStatic << endl); - return NULL; - } - - if(checkConHashChange(bStatic, _lastConHashWeightProxys)) - { - int64_t iBegin = TNOWMS; - - updateConHashProxyWeighted(bStatic, _lastConHashWeightProxys, _consistentHashWeight); - - int64_t iEnd = TNOWMS; - - TLOGINFO("[TARS][EndpointManager::getConHashProxyForWeight update bStatic:" << bStatic << "|_objName:" << _objName << "|timecost(ms):" << (iEnd - iBegin) << endl); - } - - if(_consistentHashWeight.size() > 0) - { - unsigned int iIndex = 0; - - // 通过一致性hashå–到对应的节点 - _consistentHashWeight.getIndex(hashCode, iIndex); - - if(iIndex >= _vRegProxys.size()) - { - iIndex = iIndex % _vRegProxys.size(); - } - - //被hash到的节点在主控是activeçš„æ‰èµ°åœ¨æµç¨‹ - if (_vRegProxys[iIndex]->isActiveInReg() && _vRegProxys[iIndex]->checkActive()) - { - return _vRegProxys[iIndex]; - } - else - { - if(_activeProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getConHashProxyForWeight _activeEndpoints is empty], bStatic:" << bStatic << endl); - return NULL; - } - - //在active节点中å†æ¬¡hash - vector thisHash = _activeProxys; - vector conn; - size_t hash = 0; - - do - { - hash = ((int64_t)hashCode) % thisHash.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= thisHash.size()) - { - hash = hash % thisHash.size(); - } - - if (thisHash[hash]->checkActive()) - { - return thisHash[hash]; - } - if(!thisHash[hash]->isConnTimeout() && - !thisHash[hash]->isConnExc()) - { - conn.push_back(thisHash[hash]); - } - thisHash.erase(thisHash.begin() + hash); - } - while(!thisHash.empty()); - - if(conn.size() > 0) - { - hash = ((int64_t)hashCode) % conn.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= conn.size()) - { - hash = hash % conn.size(); - } - - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy *adapterProxy = conn[hash]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())]; - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; - } - } - - return getHashProxyForNormal(hashCode); -} - -bool EndpointManager::checkHashStaticWeightChange(bool bStatic) -{ - if(bStatic) - { - if(_lastHashStaticProxys.size() != _vRegProxys.size()) - { - return true; - } - - for(size_t i = 0; i < _vRegProxys.size(); i++) - { - if(_lastHashStaticProxys[i]->getWeight() != _vRegProxys[i]->getWeight() || _lastHashStaticProxys[i]->endpoint().desc() != _vRegProxys[i]->endpoint().desc()) - { - return true; - } - } - } - - return false; -} - -bool EndpointManager::checkConHashChange(bool bStatic, const vector &vLastConHashProxys) -{ - if(vLastConHashProxys.size() != _vRegProxys.size()) - { - return true; - } - - for(size_t i = 0; i < _vRegProxys.size(); i++) - { - if(bStatic) - { - if(vLastConHashProxys[i]->getWeight() != _vRegProxys[i]->getWeight() || vLastConHashProxys[i]->endpoint().desc() != _vRegProxys[i]->endpoint().desc()) - { - return true; - } - } - else - { - if(vLastConHashProxys[i]->endpoint().desc() != _vRegProxys[i]->endpoint().desc()) - { - return true; - } - } - } - - return false; -} - -void EndpointManager::updateHashProxyWeighted(bool bStatic) -{ - if(_vRegProxys.size() <= 0) - { - TLOGERROR("[TARS][EndpointManager::updateHashProxyWeighted _vRegProxys is empty], bStatic:" << bStatic << endl); - return ; - } - - if(bStatic) - { - _lastHashStaticProxys = _vRegProxys; - _hashStaticRouterCache.clear(); - } - - vector vRegProxys; - vector vIndex; - for(size_t i = 0; i < _vRegProxys.size(); ++i) - { - if(_vRegProxys[i]->getWeight() > 0) - { - vRegProxys.push_back(_vRegProxys[i]); - vIndex.push_back(i); - } - } - - if(vRegProxys.size() <= 0) - { - TLOGERROR("[TARS][EndpointManager::updateHashProxyWeighted vRegProxys is empty], bStatic:" << bStatic << endl); - return ; - } - - size_t iHashStaticWeightSize = vRegProxys.size(); - map mIdToWeight; - multimap mWeightToId; - size_t iMaxR = 0; - size_t iMaxRouterR = 0; - - size_t iMaxWeight = vRegProxys[0]->getWeight(); - size_t iMinWeight = vRegProxys[0]->getWeight(); - size_t iTempWeight = 0; - - for(size_t i = 1;i < iHashStaticWeightSize; i++) - { - iTempWeight = vRegProxys[i]->getWeight(); - - if(iTempWeight > iMaxWeight) - { - iMaxWeight = iTempWeight; - } - - if(iTempWeight < iMinWeight) - { - iMinWeight = iTempWeight; - } - } - - if(iMinWeight > 0) - { - iMaxR = iMaxWeight / iMinWeight; - - if(iMaxR < iMinWeightLimit) - iMaxR = iMinWeightLimit; - - if(iMaxR > iMaxWeightLimit) - iMaxR = iMaxWeightLimit; - } - else - { - iMaxR = 1; - iMaxWeight = 1; - } - - for(size_t i = 0;i < iHashStaticWeightSize; i++) - { - int iWeight = (vRegProxys[i]->getWeight() * iMaxR) / iMaxWeight; - - if(iWeight > 0) - { - iMaxRouterR += iWeight; - - mIdToWeight.insert(map::value_type(vIndex[i], iWeight)); - - mWeightToId.insert(make_pair(iWeight, vIndex[i])); - } - else - { - if(bStatic) - { - _hashStaticRouterCache.push_back(vIndex[i]); - } - } - - TLOGINFO("[TARS]EndpointManager::updateHashProxyWeighted bStatic:" << bStatic << "|_objName:" << _objName << "|endpoint:" << vRegProxys[i]->endpoint().desc() << "|iWeight:" << vRegProxys[i]->getWeight() << "|iWeightR:" << iWeight << "|iIndex:" << vIndex[i] << endl); - } - - for(size_t i = 0; i < iMaxRouterR; i++) - { - bool bFirst = true; - multimap mulTemp; - multimap::reverse_iterator mIter = mWeightToId.rbegin(); - while(mIter != mWeightToId.rend()) - { - if(bFirst) - { - bFirst = false; - if(bStatic) - { - _hashStaticRouterCache.push_back(mIter->second); - } - - mulTemp.insert(make_pair((mIter->first - iMaxRouterR + mIdToWeight[mIter->second]), mIter->second)); - } - else - { - mulTemp.insert(make_pair((mIter->first + mIdToWeight[mIter->second]), mIter->second)); - } - - mIter++; - } - - mWeightToId.clear(); - mWeightToId.swap(mulTemp); - } -} - -void EndpointManager::updateConHashProxyWeighted(bool bStatic, vector &vLastConHashProxys, TC_ConsistentHashNew &conHash) -{ - if(_vRegProxys.size() <= 0) - { - TLOGERROR("[TARS][EndpointManager::updateHashProxyWeighted _vRegProxys is empty], bStatic:" << bStatic << endl); - return ; - } - - vLastConHashProxys = _vRegProxys; - conHash.clear(); - - for(size_t i = 0; i < _vRegProxys.size(); ++i) - { - int iWeight = (bStatic ? (_vRegProxys[i]->getWeight()) : 100); - if(iWeight > 0) - { - iWeight = iWeight / 4; - if(iWeight <= 0) - { - iWeight = 1; - } - conHash.addNode(_vRegProxys[i]->endpoint().desc(), i, iWeight); - } - } - - conHash.sortNode(); -} - -AdapterProxy* EndpointManager::getHashProxyForNormal(int64_t hashCode) -{ - if(_vRegProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getHashProxyForNormal _vRegProxys is empty]" << endl); - return NULL; - } - - // 1 _vRegProxys从客户端å¯åŠ¨ä¹‹åŽï¼Œå°±ä¸ä¼šå†æ”¹å˜ï¼Œé™¤éžæœ‰èŠ‚点增加 - // 2 如果有增加节点,则_vRegProxys顺åºä¼šé‡æ–°æŽ’åº,之å‰çš„hashä¼šæ”¹å˜ - // 3 节点下线åŽï¼Œéœ€è¦ä¸‹æ¬¡å¯åŠ¨å®¢æˆ·ç«¯åŽ,_vRegProxys内容æ‰ä¼šç”Ÿæ•ˆ - size_t hash = ((int64_t)hashCode) % _vRegProxys.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= _vRegProxys.size()) - { - hash = hash % _vRegProxys.size(); - } - - //被hash到的节点在主控是activeçš„æ‰èµ°åœ¨æµç¨‹ - if (_vRegProxys[hash]->isActiveInReg() && _vRegProxys[hash]->checkActive()) - { - return _vRegProxys[hash]; - } - else - { - if(_activeProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getHashProxyForNormal _activeEndpoints is empty]" << endl); - return NULL; - } - - //在active节点中å†æ¬¡hash - vector thisHash = _activeProxys; - vector conn; - - do - { - hash = ((int64_t)hashCode) % thisHash.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= thisHash.size()) - { - hash = hash % thisHash.size(); - } - - if (thisHash[hash]->checkActive()) - { - return thisHash[hash]; - } - if(!thisHash[hash]->isConnTimeout() && - !thisHash[hash]->isConnExc()) - { - conn.push_back(thisHash[hash]); - } - thisHash.erase(thisHash.begin() + hash); - } - while(!thisHash.empty()); - - if(conn.size() > 0) - { - hash = ((int64_t)hashCode) % conn.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= conn.size()) - { - hash = hash % conn.size(); - } - - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy *adapterProxy = conn[hash]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())]; - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; - } -} - -AdapterProxy* EndpointManager::getConHashProxyForNormal(int64_t hashCode) -{ - if(_vRegProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getConHashProxyForNormal _vRegProxys is empty]" << endl); - return NULL; - } - - if(checkConHashChange(false, _lastConHashProxys)) - { - int64_t iBegin = TNOWMS; - - updateConHashProxyWeighted(false, _lastConHashProxys, _consistentHash); - - int64_t iEnd = TNOWMS; - - TLOGINFO("[TARS][EndpointManager::getConHashProxyForNormal update _objName:" << _objName << "|timecost(ms):" << (iEnd - iBegin) << endl); - } - - if(_consistentHash.size() > 0) - { - unsigned int iIndex = 0; - - // 通过一致性hashå–到对应的节点 - _consistentHash.getIndex(hashCode, iIndex); - - if(iIndex >= _vRegProxys.size()) - { - iIndex = iIndex % _vRegProxys.size(); - } - - //被hash到的节点在主控是activeçš„æ‰èµ°åœ¨æµç¨‹ - if (_vRegProxys[iIndex]->isActiveInReg() && _vRegProxys[iIndex]->checkActive()) - { - return _vRegProxys[iIndex]; - } - else - { - if(_activeProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getConHashProxyForNormal _activeEndpoints is empty]" << endl); - return NULL; - } - - //在active节点中å†æ¬¡hash - vector thisHash = _activeProxys; - vector conn; - size_t hash = 0; - - do - { - hash = ((int64_t)hashCode) % thisHash.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= thisHash.size()) - { - hash = hash % thisHash.size(); - } - - if (thisHash[hash]->checkActive()) - { - return thisHash[hash]; - } - if(!thisHash[hash]->isConnTimeout() && - !thisHash[hash]->isConnExc()) - { - conn.push_back(thisHash[hash]); - } - thisHash.erase(thisHash.begin() + hash); - } - while(!thisHash.empty()); - - if(conn.size() > 0) - { - hash = ((int64_t)hashCode) % conn.size(); - - //这里åšåˆ¤æ–­çš„原因是:32ä½ç³»ç»Ÿä¸‹ï¼Œå¦‚æžœhashCode为负值,hashç»è¿‡ä¸Šé¢çš„计算会是一个超大值,导致越界 - if(hash >= conn.size()) - { - hash = hash % conn.size(); - } - - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy *adapterProxy = conn[hash]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())]; - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; - } - } - - return getHashProxyForNormal(hashCode); -} - -AdapterProxy* EndpointManager::getWeightedProxy(bool bStaticWeighted) -{ - return getWeightedForNormal(bStaticWeighted); -} - -AdapterProxy* EndpointManager::getWeightedForNormal(bool bStaticWeighted) -{ - if (_activeProxys.empty()) - { - TLOGERROR("[TARS][EndpointManager::getWeightedForNormal activeEndpoints is empty][obj:"<<_objName<<"]" << endl); - return NULL; - } - - int64_t iNow = TNOW; - if(_lastBuildWeightTime <= iNow) - { - updateProxyWeighted(); - - if(!_first) - { - _lastBuildWeightTime = iNow + _updateWeightInterval; - } - else - { - _first = false; - _lastBuildWeightTime = iNow + _updateWeightInterval + 5; - } - } - - bool bEmpty = false; - int iActiveSize = _activeWeightProxy.size(); - - if(iActiveSize > 0) - { - size_t iProxyIndex = 0; - set sConn; - - if(_staticRouterCache.size() > 0) - { - for(size_t i = 0;i < _staticRouterCache.size(); i++) - { - ++_lastSWeightPosition; - - if(_lastSWeightPosition >= _staticRouterCache.size()) - _lastSWeightPosition = 0; - - iProxyIndex = _staticRouterCache[_lastSWeightPosition]; - - if(_activeWeightProxy[iProxyIndex]->checkActive()) - { - return _activeWeightProxy[iProxyIndex]; - } - - if(!_activeWeightProxy[iProxyIndex]->isConnTimeout() && - !_activeWeightProxy[iProxyIndex]->isConnExc()) - { - sConn.insert(_activeWeightProxy[iProxyIndex]); - } - } - } - else - { - bEmpty = true; - } - - if(!bEmpty) - { - if(sConn.size() > 0) - { - vector conn; - set::iterator it_conn = sConn.begin(); - while(it_conn != sConn.end()) - { - conn.push_back(*it_conn); - ++it_conn; - } - - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeWeightProxy[((uint32_t)rand() % iActiveSize)]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; - } - } - - vector conn; - - for(size_t i=0;i<_activeProxys.size();i++) - { - ++_lastRoundPosition; - if(_lastRoundPosition >= _activeProxys.size()) - _lastRoundPosition = 0; - - if(_activeProxys[_lastRoundPosition]->checkActive()) - { - return _activeProxys[_lastRoundPosition]; - } - - if(!_activeProxys[_lastRoundPosition]->isConnTimeout() && - !_activeProxys[_lastRoundPosition]->isConnExc()) - conn.push_back(_activeProxys[_lastRoundPosition]); - } - - if(conn.size() > 0) - { - //都有问题, éšæœºé€‰æ‹©ä¸€ä¸ªæ²¡æœ‰connect超时或者链接异常的å‘é€ - AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())]; - - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - return adapterProxy; - } - - //所有adapter都有问题 选ä¸åˆ°ç»“点,éšæœºæ‰¾ä¸€ä¸ªé‡è¯• - AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())]; - //该proxyå¯èƒ½å·²ç»è¢«å±è”½,需é‡æ–°è¿žä¸€æ¬¡ - adapterProxy->checkActive(true); - - return NULL; -} - -void EndpointManager::updateProxyWeighted() -{ - size_t iWeightProxySize = _activeProxys.size(); - - if(iWeightProxySize <= 0) - { - TLOGERROR("[TARS]EndpointManager::updateProxyWeighted _objName:" << _objName << "|_activeProxys.size() <= 0" << endl); - return ; - } - - vector vProxy; - - for(size_t i = 0; i < _activeProxys.size(); ++i) - { - if(_activeProxys[i]->getWeight() > 0) - { - vProxy.push_back(_activeProxys[i]); - } - } - - iWeightProxySize = vProxy.size(); - - if(iWeightProxySize <= 0) - { - TLOGERROR("[TARS]EndpointManager::updateProxyWeighted _objName:" << _objName << "|vProxy.size() <= 0" << endl); - return ; - } - - if(_update) - { - _activeWeightProxy = vProxy; - - updateStaticWeighted(); - } - - _update = false; -} - -void EndpointManager::updateStaticWeighted() -{ - size_t iWeightProxySize = _activeWeightProxy.size(); - vector vWeight; - - vWeight.resize(iWeightProxySize); - - for(size_t i = 0; i < iWeightProxySize; i++) - { - vWeight[i] = _activeWeightProxy[i]->getWeight(); - } - - dispatchEndpointCache(vWeight); -} - -void EndpointManager::dispatchEndpointCache(const vector &vWeight) -{ - if(vWeight.size() <= 0) - { - TLOGERROR("EndpointManager::dispatchEndpointCache vWeight.size() < 0" << endl); - return ; - } - - size_t iWeightProxySize = vWeight.size(); - map mIdToWeight; - multimap mWeightToId; - size_t iMaxR = 0; - size_t iMaxRouterR = 0; - size_t iMaxWeight = 0; - size_t iMinWeight = 0; - size_t iTotalCapacty = 0; - size_t iTempWeight = 0; - - for(size_t i = 0; i < vWeight.size(); ++i) - { - iTotalCapacty += vWeight[i]; - } - - _staticRouterCache.clear(); - _lastSWeightPosition = 0; - _staticRouterCache.reserve(iTotalCapacty+100); - - iMaxWeight = vWeight[0]; - iMinWeight = vWeight[0]; - - for(size_t i = 1;i < iWeightProxySize; i++) - { - iTempWeight = vWeight[i]; - - if(iTempWeight > iMaxWeight) - { - iMaxWeight = iTempWeight; - } - - if(iTempWeight < iMinWeight) - { - iMinWeight = iTempWeight; - } - } - - if(iMinWeight > 0) - { - iMaxR = iMaxWeight / iMinWeight; - - if(iMaxR < iMinWeightLimit) - iMaxR = iMinWeightLimit; - - if(iMaxR > iMaxWeightLimit) - iMaxR = iMaxWeightLimit; - } - else - { - iMaxR = 1; - iMaxWeight = 1; - } - - for(size_t i = 0;i < iWeightProxySize; i++) - { - int iWeight = (vWeight[i] * iMaxR) / iMaxWeight; - - if(iWeight > 0) - { - iMaxRouterR += iWeight; - - mIdToWeight.insert(map::value_type(i, iWeight)); - - mWeightToId.insert(make_pair(iWeight, i)); - } - else - { - _staticRouterCache.push_back(i); - } - - TLOGINFO("[TARS]EndpointManager::dispatchEndpointCache _objName:" << _objName << "|endpoint:" << _activeWeightProxy[i]->endpoint().desc() << "|iWeightR:" << iWeight << endl); - } - - for(size_t i = 0; i < iMaxRouterR; i++) - { - bool bFirst = true; - multimap mulTemp; - multimap::reverse_iterator mIter = mWeightToId.rbegin(); - while(mIter != mWeightToId.rend()) - { - if(bFirst) - { - bFirst = false; - - _staticRouterCache.push_back(mIter->second); - - mulTemp.insert(make_pair((mIter->first - iMaxRouterR + mIdToWeight[mIter->second]), mIter->second)); - } - else - { - mulTemp.insert(make_pair((mIter->first + mIdToWeight[mIter->second]), mIter->second)); - } - - mIter++; - } - - mWeightToId.clear(); - mWeightToId.swap(mulTemp); - } -} - -///////////////////////////////////////////////////////////////////////////// -EndpointThread::EndpointThread(Communicator* pComm, const string & sObjName, GetEndpointType type, const string & sName, bool bFirstNetThread) -: QueryEpBase(pComm,bFirstNetThread,true) -, _type(type) -, _name(sName) -{ - init(sObjName,_communicator->getProperty("locator")); -} - -void EndpointThread::getEndpoints(vector &activeEndPoint, vector &inactiveEndPoint) -{ - //直连调用这个接å£æ— æ•ˆ - if(_direct) - { - return ; - } - - { - - TC_ThreadLock::Lock lock(_lock); - - refreshReg(_type,_name); - - activeEndPoint = _activeEndPoint; - inactiveEndPoint = _inactiveEndPoint; - } -} - -void EndpointThread::getTCEndpoints(vector &activeEndPoint, vector &inactiveEndPoint) -{ - //直连调用这个接å£æ— æ•ˆ - if(_direct) - { - return ; - } - - { - - TC_ThreadLock::Lock lock(_lock); - - refreshReg(_type,_name); - - activeEndPoint = _activeTCEndPoint; - inactiveEndPoint = _inactiveTCEndPoint; - } -} - -void EndpointThread::notifyEndpoints(const set & active, const set & inactive, bool bSync) -{ - if(!bSync) - { - TC_ThreadLock::Lock lock(_lock); - - update(active, inactive); - } - else - { - update(active, inactive); - } -} - -void EndpointThread::update(const set & active, const set & inactive) -{ - _activeEndPoint.clear(); - _inactiveEndPoint.clear(); - - _activeTCEndPoint.clear(); - _inactiveTCEndPoint.clear(); - - set::iterator iter= active.begin(); - for(;iter != active.end(); ++iter) - { - TC_Endpoint ep(iter->host(), iter->port(), 3000, iter->type(), iter->grid()); - - _activeTCEndPoint.push_back(ep); - - _activeEndPoint.push_back(*iter); - } - - iter = inactive.begin(); - for(;iter != inactive.end(); ++iter) - { - TC_Endpoint ep(iter->host(), iter->port(), 3000, iter->type(), iter->grid()); - - _inactiveTCEndPoint.push_back(ep); - - _inactiveEndPoint.push_back(*iter); - } -} -///////////////////////////////////////////////////////////////////////////// -EndpointManagerThread::EndpointManagerThread(Communicator * pComm,const string & sObjName) -:_communicator(pComm) -,_objName(sObjName) -{ -} - -EndpointManagerThread::~EndpointManagerThread() -{ - map::iterator iter; - for(iter=_info.begin();iter != _info.end();iter++) - { - if(iter->second) - { - delete iter->second; - iter->second = NULL; - } - } -} - -void EndpointManagerThread::getEndpoint(vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_DEFAULT,""); - - pThread->getEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getEndpointByAll(vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_ALL,""); - - pThread->getEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getEndpointBySet(const string sName, vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_SET,sName); - - pThread->getEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getEndpointByStation(const string sName, vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_STATION,sName); - - pThread->getEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getTCEndpoint(vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_DEFAULT,""); - - pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getTCEndpointByAll(vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_ALL,""); - - pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getTCEndpointBySet(const string sName, vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_SET,sName); - - pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint); -} - -void EndpointManagerThread::getTCEndpointByStation(const string sName, vector &activeEndPoint, vector &inactiveEndPoint) -{ - EndpointThread * pThread = getEndpointThread(E_STATION,sName); - - pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint); -} - -EndpointThread * EndpointManagerThread::getEndpointThread(GetEndpointType type,const string & sName) -{ - TC_ThreadLock::Lock lock(_lock); - - string sAllName = TC_Common::tostr((int)type) + ":" + sName; - - map::iterator iter; - iter = _info.find(sAllName); - if(iter != _info.end()) - { - return iter->second; - } - - EndpointThread * pThread = new EndpointThread(_communicator, _objName, type, sName); - _info[sAllName] = pThread; - - return pThread; -} - -} diff --git a/cpp/servant/libservant/Global.cpp b/cpp/servant/libservant/Global.cpp deleted file mode 100644 index 4388e7f09..000000000 --- a/cpp/servant/libservant/Global.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/Global.h" -#include "servant/BaseF.h" - -namespace tars -{ - -////////////////////////////////////////////////////////////////////////// -void TarsException::throwException(int ret, const string& desc) -{ - switch (ret) - { - case tars::TARSSERVERSUCCESS: - break; - case TARSSERVERDECODEERR: - throw TarsServerDecodeException("server decode exception: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - break; - case TARSSERVERENCODEERR: - throw TarsServerEncodeException("server encode exception: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - break; - case TARSSERVERNOFUNCERR: - throw TarsServerNoFuncException("server function mismatch exception: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - break; - case TARSSERVERNOSERVANTERR: - throw TarsServerNoServantException("server servant mismatch exception: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - break; - case TARSSERVERQUEUETIMEOUT: - throw TarsServerQueueTimeoutException("server queue timeout exception: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - break; - case TARSPROXYCONNECTERR: - throw TarsServerQueueTimeoutException("server connection lost: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - break; - default: - throw TarsServerUnknownException("server unknown exception: ret:" + TC_Common::tostr(ret) + " msg:"+ desc); - } -} -//////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/NetworkUtil.cpp b/cpp/servant/libservant/NetworkUtil.cpp deleted file mode 100644 index ed8aada2b..000000000 --- a/cpp/servant/libservant/NetworkUtil.cpp +++ /dev/null @@ -1,196 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/NetworkUtil.h" -#include "servant/Global.h" -#include "util/tc_epoller.h" - -#include -#include -#include - -using namespace std; -using namespace tars; - -int NetworkUtil::createSocket(bool udp, bool isLocal/* = false*/) -{ - int fd; - - if (udp) - { - fd = socket((isLocal ? PF_LOCAL : PF_INET), SOCK_DGRAM, IPPROTO_UDP); - } - else - { - fd = socket((isLocal ? PF_LOCAL : PF_INET), SOCK_STREAM, IPPROTO_TCP); - } - - if (fd == INVALID_SOCKET) - { - ostringstream os; - os << "createSocket ex:(" << errorToString(errno) << ")" << __FILE__ << ":" << __LINE__; - throw TarsNetSocketException(os.str()); - } - - if(!udp) - { - setTcpNoDelay(fd); - - setKeepAlive(fd); - } - - return fd; -} - -void NetworkUtil::closeSocketNoThrow(int fd) -{ - int error = errno; - close(fd); - errno = error; -} - -void NetworkUtil::setBlock(int fd, bool block) -{ - if (block) - { - int flags = fcntl(fd, F_GETFL); - flags &= ~O_NONBLOCK; - if (fcntl(fd, F_SETFL, flags) == SOCKET_ERROR) - { - closeSocketNoThrow(fd); - ostringstream os; - os << "setBlock ex:(" << errorToString(errno) << ")" << __FILE__ << ":" << __LINE__; - throw TarsNetSocketException(os.str()); - } - } - else - { - int flags = fcntl(fd, F_GETFL); - flags |= O_NONBLOCK; - if (fcntl(fd, F_SETFL, flags) == SOCKET_ERROR) - { - closeSocketNoThrow(fd); - ostringstream os; - os << "setBlock ex:(" << errorToString(errno) << ")" << __FILE__ << ":" << __LINE__; - throw TarsNetSocketException(os.str()); - } - } -} - -void NetworkUtil::setTcpNoDelay(int fd) -{ - int flag = 1; - if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, int(sizeof(int))) == SOCKET_ERROR) - { - closeSocketNoThrow(fd); - ostringstream os; - os << "setTcpNoDelay ex:(" << errorToString(errno) << ")" << __FILE__ << ":" << __LINE__; - throw TarsNetSocketException(os.str()); - } -} - -void NetworkUtil::setKeepAlive(int fd) -{ - int flag = 1; - if(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&flag, int(sizeof(int))) == SOCKET_ERROR) - { - closeSocketNoThrow(fd); - ostringstream os; - os << "setKeepAlive ex:(" << errorToString(errno) << ")" << __FILE__ << ":" << __LINE__; - throw TarsNetSocketException(os.str()); - } -} - -void NetworkUtil::doBind(int fd, struct sockaddr_in& addr) -{ - if(bind(fd, reinterpret_cast(&addr), int(sizeof(addr))) == SOCKET_ERROR) - { - closeSocketNoThrow(fd); - ostringstream os; - os << "doBind ex:(" << errorToString(errno) << ")" << __FILE__ << ":" << __LINE__; - throw TarsNetSocketException(os.str()); - } - socklen_t len = static_cast(sizeof(addr)); - getsockname(fd, reinterpret_cast(&addr), &len); -} - -bool NetworkUtil::doConnect(int fd, const struct sockaddr_in& addr) -{ - bool bConnected = false; - - int iRet = ::connect(fd, (struct sockaddr*)(&addr), int(sizeof(addr))); - - if (iRet == 0) - { - bConnected = true; - } - else if (iRet == -1 && errno != EINPROGRESS) - { - ::close(fd); - throw TarsNetConnectException(strerror(errno)); - } - - return bConnected; -} - -void NetworkUtil::getAddress(const string& host, int port, struct sockaddr_in& addr) -{ - memset(&addr, 0, sizeof(struct sockaddr_in)); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = inet_addr(host.c_str()); - - if(addr.sin_addr.s_addr == INADDR_NONE) - { - struct addrinfo* info = 0; - int retry = 5; - - struct addrinfo hints = { 0 }; - hints.ai_family = PF_INET; - - int rs = 0; - do - { - rs = getaddrinfo(host.c_str(), 0, &hints, &info); - } - while(info == 0 && rs == EAI_AGAIN && --retry >= 0); - - if(rs != 0) - { - ostringstream os; - os << "DNSException ex:(" << errorToString(errno) << ")" << rs << ":" << host << ":" << __FILE__ << ":" << __LINE__; - if(info != NULL) - { - freeaddrinfo(info); - } - throw TarsNetSocketException(os.str()); - } - - assert(info != NULL); - assert(info->ai_family == PF_INET); - struct sockaddr_in* sin = reinterpret_cast(info->ai_addr); - - addr.sin_addr.s_addr = sin->sin_addr.s_addr; - freeaddrinfo(info); - } -} - -string NetworkUtil::errorToString(int error) -{ - return strerror(error); -} - - diff --git a/cpp/servant/libservant/NotifyObserver.cpp b/cpp/servant/libservant/NotifyObserver.cpp deleted file mode 100644 index 668493e73..000000000 --- a/cpp/servant/libservant/NotifyObserver.cpp +++ /dev/null @@ -1,195 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/NotifyObserver.h" -#include "servant/BaseNotify.h" - -namespace tars -{ - -NotifyObserver::NotifyObserver() -{ -} - -NotifyObserver::~NotifyObserver() -{ -} - -void NotifyObserver::registerObject(const string& command, BaseNotify* obj, map >& target) -{ - TC_LockT lock(*this); - - target[command].insert(obj); -} - -void NotifyObserver::unregisterObject(const string& command, BaseNotify* obj, map >& target) -{ - TC_LockT lock(*this); - - map >::iterator it; - - it = target.find(command); - - if (it != target.end()) - { - set::iterator sit = it->second.find(obj); - - if (sit != it->second.end()) - { - it->second.erase(sit); - } - if (it->second.empty()) - { - target.erase(it); - } - } -} - -void NotifyObserver::registerNotify(const string& command, BaseNotify* obj) -{ - registerObject(command, obj, _notifys); -} - -void NotifyObserver::unregisterNotify(const string& command, BaseNotify* obj) -{ - unregisterObject(command, obj, _notifys); -} - -void NotifyObserver::registerPrefix(const string& command, BaseNotify* obj) -{ - registerObject(command, obj, _prefix); -} - -void NotifyObserver::unregisterPrefix(const string& command, BaseNotify* obj) -{ - unregisterObject(command, obj, _prefix); -} - -string NotifyObserver::notify(const string& command, TarsCurrentPtr current) -{ - TC_LockT lock(*this); - - string str = TC_Common::trim(command); - - string name = str; - - string params = ""; - - string::size_type pos = str.find_first_of(" "); - - if (pos != string::npos) - { - name = str.substr(0, pos); - - params = str.substr(pos + 1); - } - - ostringstream os; - - map >::iterator it = _prefix.find(name); - - if (it != _prefix.end()) - { - set& sbn = it->second; - - os << "[notify prefix object num:" << sbn.size() << "]" << endl; - - int i = 0; - - for (set::iterator sit = sbn.begin(); sit != sbn.end(); ++sit) - { - string result = ""; - - if ((*sit)->notify(name, params, current, result)) - { - os << "[" << ++i << "]:" << result << endl; - } - else - { - os << "[notify break by server]:" << endl; - os << result << endl; - - return os.str(); - } - } - } - - it = _notifys.find(name); - - if (it != _notifys.end()) - { - set& sbn = it->second; - - os << "[notify servant object num:" << sbn.size() << "]" << endl; - - int i = 0; - - for (set::iterator sit = sbn.begin(); sit != sbn.end(); ++sit) - { - string result = ""; - - if ((*sit)->notify(name, params, current, result)) - { - os << "[" << ++i << "]:" << result << endl; - } - else - { - os << "[notify break.]" << endl; - os << result << endl; - - return os.str(); - } - } - } - return os.str(); -} - -string NotifyObserver::viewRegisterCommand() -{ - TC_LockT lock(*this); - - ostringstream os; - - map >::iterator it = _prefix.begin(); - - set command; - - while (it != _prefix.end()) - { - command.insert(it->first); - ++it; - } - - it = _notifys.begin(); - - while (it != _notifys.end()) - { - command.insert(it->first); - ++it; - } - - set::const_iterator p = command.begin(); - - while(p != command.end()) - { - os<<*p<<"\r\n"; - ++p; - } - - return os.str(); -} -/////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/ObjectProxy.cpp b/cpp/servant/libservant/ObjectProxy.cpp deleted file mode 100644 index e9ff44585..000000000 --- a/cpp/servant/libservant/ObjectProxy.cpp +++ /dev/null @@ -1,317 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/ObjectProxy.h" -#include "servant/Communicator.h" -#include "servant/Global.h" -#include "servant/EndpointManager.h" -#include "servant/AppCache.h" -#include "util/tc_common.h" -#include "util/tc_clientsocket.h" -#include "servant/TarsLogger.h" - -namespace tars -{ -//////////////////////////////////////////////////////////////////////////////////////////// -ObjectProxy::ObjectProxy(CommunicatorEpoll * pCommunicatorEpoll, const string & sObjectProxyName,const string& setName) -: _communicatorEpoll(pCommunicatorEpoll) -, _invokeSetId(setName) -, _isInvokeBySet(false) -, _id(0) -, _hasSetProtocol(false) -, _conTimeout(1000) -, _servantProxy(NULL) -{ - string::size_type pos = sObjectProxyName.find_first_of('@'); - - if(pos != string::npos) - { - _name = sObjectProxyName.substr(0,pos); - } - else - { - _name = sObjectProxyName; - //å¯ç”¨set或者指定set调用 - if(ClientConfig::SetOpen || !_invokeSetId.empty()) - { - //指定set调用时,指定set的优先级最高 - _invokeSetId = _invokeSetId.empty()?ClientConfig::SetDivision:_invokeSetId; - _isInvokeBySet = true; - } - } - - _proxyProtocol.requestFunc = ProxyProtocol::tarsRequest; - _proxyProtocol.responseFunc = ProxyProtocol::tarsResponse; - _protoName = "tars"; - - _endpointManger.reset(new EndpointManager(this, _communicatorEpoll->getCommunicator(), sObjectProxyName, pCommunicatorEpoll->isFirstNetThread(), setName)); - -} - -ObjectProxy::~ObjectProxy() -{ -} - -void ObjectProxy::initialize() -{ -} - -int ObjectProxy::loadLocator() -{ - if(_endpointManger->getDirectProxy()) - { - //直接连接 - return 0; - } - - string locator = _communicatorEpoll->getCommunicator()->getProperty("locator"); - - if (locator.find_first_not_of('@') == string::npos) - { - TLOGERROR("[Locator is not valid:" << locator << "]" << endl); - - return -1; - } - - QueryFPrx prx = _communicatorEpoll->getCommunicator()->stringToProxy(locator); - - _endpointManger->setLocatorPrx(prx); - - return 0; -} - -void ObjectProxy::setProxyProtocol(const ProxyProtocol& protocol, const std::string& name) -{ - if(_hasSetProtocol) - { - return ; - } - - _hasSetProtocol = true; - _proxyProtocol = protocol; - _protoName = name; -} - -ProxyProtocol& ObjectProxy::getProxyProtocol() -{ - return _proxyProtocol; -} - -const std::string& ObjectProxy::getProtoName() const -{ - return _protoName; -} - -void ObjectProxy::setSocketOpt(int level, int optname, const void *optval, socklen_t optlen) -{ - SocketOpt socketOpt; - - socketOpt.level = level; - socketOpt.optname = optname; - socketOpt.optval = optval; - socketOpt.optlen = optlen; - - _socketOpts.push_back(socketOpt); -} - -vector& ObjectProxy::getSocketOpt() -{ - return _socketOpts; -} - -void ObjectProxy::setPushCallbacks(const ServantProxyCallbackPtr& cb) -{ - _pushCallback = cb; -} - -ServantProxyCallbackPtr ObjectProxy::getPushCallback() -{ - return _pushCallback; -} - -void ObjectProxy::invoke(ReqMessage * msg) -{ - TLOGINFO("[TARS][ObjectProxy::invoke, objname:" << _name << ", begin...]" << endl); - - //选择一个远程æœåŠ¡çš„Adapteræ¥è°ƒç”¨ - AdapterProxy * pAdapterProxy = NULL; - bool bFirst = _endpointManger->selectAdapterProxy(msg, pAdapterProxy); - - if(bFirst) - { - //判断是å¦è¯·æ±‚过主控 - bool bRet = _reqTimeoutQueue.push(msg,msg->request.iTimeout+msg->iBeginTime); - - assert(bRet); - - //把数æ®ç¼“存在objé‡Œé¢ - TLOGINFO("[TARS][ObjectProxy::invoke, objname:" << _name << ", select adapter proxy not valid (have not inovoke reg)]" << endl); - - return; - } - - if(!pAdapterProxy) - { - TLOGERROR("[TARS][ObjectProxy::invoke, objname:"<< _name << ", selectAdapterProxy is null]"<response.iRet = TARSADAPTERNULL; - - doInvokeException(msg); - - return ; - } - - msg->adapter = pAdapterProxy; - pAdapterProxy->invoke(msg); -} - -void ObjectProxy::doInvoke() -{ - TLOGINFO("[TARS][ObjectProxy::doInvoke, objname:" << _name << ", begin...]" << endl); - - while(!_reqTimeoutQueue.empty()) - { - TLOGINFO("[TARS][ObjectProxy::doInvoke, objname:" << _name << ", pop...]" << endl); - - ReqMessage * msg = NULL; - _reqTimeoutQueue.pop(msg); - - assert(msg != NULL); - - //选择一个远程æœåŠ¡çš„Adapteræ¥è°ƒç”¨ - AdapterProxy * pAdapterProxy = NULL; - _endpointManger->selectAdapterProxy(msg,pAdapterProxy); - - if(!pAdapterProxy) - { - //这里肯定是请求过主控 - TLOGERROR("[TARS][ObjectProxy::doInvoke, objname:" << _name << ", selectAdapterProxy is null]" << endl); - - msg->response.iRet = TARSADAPTERNULL; - - doInvokeException(msg); - - return; - } - - msg->adapter = pAdapterProxy; - pAdapterProxy->invoke(msg); - } -} - -const vector & ObjectProxy::getAdapters() const -{ - return _endpointManger->getAdapters(); -} - -void ObjectProxy::doInvokeException(ReqMessage * msg) -{ - TLOGINFO("[TARS][ObjectProxy::doInvokeException, objname:" << _name << "]" << endl); - - //å•å‘调用出现异常直接删除请求 - if(msg->eType == ReqMessage::ONE_WAY) - { - delete msg; - return; - } - - //标识请求异常 - msg->eStatus = ReqMessage::REQ_EXC; - - if(msg->eType == ReqMessage::SYNC_CALL) - { - if(!msg->bCoroFlag) - { - assert(msg->pMonitor); - - TC_ThreadLock::Lock sync(*(msg->pMonitor)); - - msg->pMonitor->notify(); - msg->bMonitorFin = true; - } - else - { - msg->sched->put(msg->iCoroId); - } - - return; - } - - if(msg->callback) - { - if(!msg->bCoroFlag) - { - if(msg->callback->getNetThreadProcess()) - { - ReqMessagePtr msgPtr = msg; - //å¦‚æžœæ˜¯æœ¬çº¿ç¨‹çš„å›žè°ƒï¼Œç›´æŽ¥æœ¬çº¿ç¨‹å¤„ç† - //比如获å–endpoint - try - { - msg->callback->onDispatch(msgPtr); - } - catch(exception & e) - { - TLOGERROR("[TARS]ObjectProxy::doInvokeException exp:" << e.what() << " ,line:" << __LINE__ << endl); - } - catch(...) - { - TLOGERROR("[TARS]ObjectProxy::doInvokeException exp:unknown line:|" << __LINE__ << endl); - } - } - else - { - //异步回调,放入回调处ç†çº¿ç¨‹ä¸­ - _communicatorEpoll->pushAsyncThreadQueue(msg); - } - } - else - { - CoroParallelBasePtr ptr = msg->callback->getCoroParallelBasePtr(); - if(ptr) - { - ptr->insert(msg); - if(ptr->checkAllReqReturn()) - { - msg->sched->put(msg->iCoroId); - } - } - else - { - TLOGERROR("[TARS]ObjectProxy::doInvokeException coro parallel callback error, objname:" << _name << endl); - delete msg; - } - } - } -} - -void ObjectProxy::doTimeout() -{ - TLOGINFO("[TARS][ObjectProxy::doInvokeException, objname:" << _name << "]" << endl); - - ReqMessage * reqInfo = NULL; - while(_reqTimeoutQueue.timeout(reqInfo)) - { - TLOGERROR("[TARS][ObjectProxy::doTimeout, objname:" << _name << ", queue timeout error]" << endl); - - reqInfo->response.iRet = TARSINVOKETIMEOUT; - - doInvokeException(reqInfo); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/ObjectProxyFactory.cpp b/cpp/servant/libservant/ObjectProxyFactory.cpp deleted file mode 100644 index fb21029f0..000000000 --- a/cpp/servant/libservant/ObjectProxyFactory.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/ObjectProxyFactory.h" - -namespace tars -{ - -ObjectProxyFactory::ObjectProxyFactory(CommunicatorEpoll * pCommunicatorEpoll) -: _communicatorEpoll(pCommunicatorEpoll) -, _objNum(0) -{ -} - -ObjectProxyFactory::~ObjectProxyFactory() -{ - for(size_t i = 0; i < _vObjectProxys.size(); i++) - { - if(_vObjectProxys[i]) - { - delete _vObjectProxys[i]; - _vObjectProxys[i] = NULL; - } - } -} - -ObjectProxy * ObjectProxyFactory::getObjectProxy(const string& sObjectProxyName,const string& setName) -{ - TC_LockT lock(*this); - - string tmpObjName = sObjectProxyName + ":" + setName; - map::iterator it = _objectProxys.find(tmpObjName); - if(it != _objectProxys.end()) - { - return it->second; - } - - ObjectProxy * pObjectProxy = new ObjectProxy(_communicatorEpoll, sObjectProxyName,setName); - - pObjectProxy->initialize(); - - _objectProxys[tmpObjName] = pObjectProxy; - //_objectProxys.insert(make_pair(tmpObjName,pObjectProxy)); - - _vObjectProxys.push_back(pObjectProxy); - - _objNum++; - - return pObjectProxy; -} - -int ObjectProxyFactory::loadObjectLocator() -{ - TC_LockT lock(*this); - - for (size_t i = 0; i < _objNum; i++) - { - _vObjectProxys[i]->loadLocator(); - } - - return 0; -} -/////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/PropertyReport.cpp b/cpp/servant/libservant/PropertyReport.cpp deleted file mode 100644 index 35122efd7..000000000 --- a/cpp/servant/libservant/PropertyReport.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/PropertyReport.h" -#include "util/tc_common.h" - -namespace tars -{ - -string PropertyReport::sum::get() -{ - string s = TC_Common::tostr(_d); - - clear(); - - return s; -} - -string PropertyReport::avg::get() -{ - if(_count == 0) - { - return "0"; - } - string s = TC_Common::tostr(static_cast(_sum)/_count); - - clear(); - - return s; -} - -PropertyReport::distr::distr(const vector& range) -{ - _range = range; - - std::sort(_range.begin(), _range.end()); - - _range.erase(unique(_range.begin(), _range.end()),_range.end()); - - _result.resize(_range.size()); -} - -void PropertyReport::distr::set(int o) -{ - vector::iterator it = std::upper_bound(_range.begin(), _range.end(), o); - - if (it != _range.end()) - { - size_t n = it - _range.begin(); - - ++_result[n]; - } -} - -string PropertyReport::distr::get() -{ - string s = ""; - - for(unsigned i = 0; i < _range.size(); ++i) - { - if (i != 0) - { - s += ","; - } - s = s + TC_Common::tostr(_range[i]) + "|" + TC_Common::tostr(_result[i]); - } - for(unsigned i = 0; i < _result.size(); ++i) - { - _result[i] = 0; - } - return s; -} - -string PropertyReport::max::get() -{ - string s = TC_Common::tostr(_d); - - clear(); - - return s; -} - -string PropertyReport::min::get() -{ - string s = TC_Common::tostr(_d); - - clear(); - - return s; -} - -void PropertyReport::min::set(int o) -{ - //éž0最å°å€¼ - if(_d == 0 ||(_d > o && o != 0)) - { - _d = o; - } -} - -string PropertyReport::count::get() -{ - string s = TC_Common::tostr(_d); - - clear(); - - return s; -} -/////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/Servant.cpp b/cpp/servant/libservant/Servant.cpp deleted file mode 100644 index ffd7320c8..000000000 --- a/cpp/servant/libservant/Servant.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/Servant.h" -#include "servant/BaseF.h" -#include "servant/Application.h" -#include "servant/AppCache.h" -#include "servant/TarsLogger.h" - -#include - -namespace tars -{ - -TC_ThreadMutex CallbackThreadData::_mutex; -pthread_key_t CallbackThreadData::_key = 0; - -Servant::Servant():_handle(NULL) -{ -} - -Servant::~Servant() -{ -} - -void Servant::setName(const string &name) -{ - _name = name; -} - -string Servant::getName() const -{ - return _name; -} - -void Servant::setHandle(TC_EpollServer::Handle* handle) -{ - _handle = handle; -} - -TC_EpollServer::Handle* Servant::getHandle() -{ - return _handle; -} - -int Servant::dispatch(TarsCurrentPtr current, vector &buffer) -{ - int ret = TARSSERVERUNKNOWNERR; - - if (current->getFuncName() == "tars_ping") - { - TLOGINFO("[TARS][Servant::dispatch] tars_ping ok from [" << current->getIp() << ":" << current->getPort() << "]" << endl); - - ret = TARSSERVERSUCCESS; - } - else if (!current->getBindAdapter()->isTarsProtocol()) - { - TC_LockT lock(*this); - - ret = doRequest(current, buffer); - } - else - { - TC_LockT lock(*this); - - ret = onDispatch(current, buffer); - } - return ret; -} - -TC_ThreadQueue& Servant::getResponseQueue() -{ - return _asyncResponseQueue; -} - -/////////////////////////////////////////////////////////////////////////// -ServantCallback::ServantCallback(const string& type, const ServantPtr& servant, const TarsCurrentPtr& current) -: _servant(servant) -, _current(current) -{ - ServantProxyCallback::setType(type); -} - -int ServantCallback::onDispatch(ReqMessagePtr msg) -{ - _servant->getResponseQueue().push_back(msg); - - _servant->getHandle()->notifyFilter(); - - return 0; -} - -const ServantPtr& ServantCallback::getServant() -{ - return _servant; -} - -const TarsCurrentPtr& ServantCallback::getCurrent() -{ - return _current; -} - -/////////////////////////////////////////////////////////////////////////// -CallbackThreadData::CallbackThreadData():_contextValid(false) -{ -} - -void CallbackThreadData::destructor(void* p) -{ - CallbackThreadData * pCbtd = (CallbackThreadData*)p; - if(pCbtd) - delete pCbtd; -} - -CallbackThreadData * CallbackThreadData::getData() -{ - if(_key == 0) - { - TC_LockT lock(_mutex); - if(_key == 0) - { - int iRet = ::pthread_key_create(&_key, CallbackThreadData::destructor); - - if (iRet != 0) - { - TLOGERROR("[TARS][CallbackThreadData pthread_key_create fail:" << errno << ":" << strerror(errno) << "]" << endl); - return NULL; - } - } - } - - CallbackThreadData * pCbtd = (CallbackThreadData*)pthread_getspecific(_key); - - if(!pCbtd) - { - TC_LockT lock(_mutex); - - pCbtd = new CallbackThreadData(); - - int iRet = pthread_setspecific(_key, (void *)pCbtd); - - assert(iRet == 0); - } - return pCbtd; -} - - -void CallbackThreadData::setResponseContext(const map & context) -{ - _contextValid = true; - if(context.empty()) - { - _responseContext.clear(); - } - else - { - _responseContext = context; - } -} - -map & CallbackThreadData::getResponseContext() -{ - return _responseContext; -} - -void CallbackThreadData::delResponseContext() -{ - _contextValid = false; - _responseContext.clear(); -} - -//////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/ServantHandle.cpp b/cpp/servant/libservant/ServantHandle.cpp deleted file mode 100644 index fc0d3ea34..000000000 --- a/cpp/servant/libservant/ServantHandle.cpp +++ /dev/null @@ -1,910 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_thread_pool.h" -#include "util/tc_timeprovider.h" -#include "servant/ServantHandle.h" -#include "servant/Application.h" -#include "servant/ServantHelper.h" -#include "servant/AppProtocol.h" -#include "servant/BaseF.h" -#include "servant/TarsNodeF.h" - -namespace tars -{ - -///////////////////////////////////////////////////////////////////////// -// -ServantHandle::ServantHandle() -: _coroSched(NULL) -{ - -} - -ServantHandle::~ServantHandle() -{ - map::iterator it = _servants.begin(); - - while(it != _servants.end()) - { - try - { - it->second->destroy(); - } - catch(exception &ex) - { - TLOGERROR("[TARS]ServantHandle::destroy error:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("[TARS]ServantHandle::destroy unknown exception error" << endl); - } - ++it; - } - - if(_coroSched != NULL) - { - delete _coroSched; - _coroSched = NULL; - } -} - -void ServantHandle::run() -{ - initialize(); - - if(!ServerConfig::OpenCoroutine) - { - handleImp(); - } - else - { - unsigned int iThreadNum = getEpollServer()->getLogicThreadNum(); - - size_t iCoroutineNum = (ServerConfig::CoroutineMemSize > ServerConfig::CoroutineStackSize) ? (ServerConfig::CoroutineMemSize / (ServerConfig::CoroutineStackSize * iThreadNum) ) : 1; - if(iCoroutineNum < 1) - iCoroutineNum = 1; - - startHandle(); - - _coroSched = new CoroutineScheduler(); - _coroSched->init(iCoroutineNum, ServerConfig::CoroutineStackSize); - _coroSched->setHandle(this); - - _coroSched->createCoroutine(std::bind(&ServantHandle::handleRequest, this)); - - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - - assert(pSptd != NULL); - - pSptd->_sched = _coroSched; - - while (!getEpollServer()->isTerminate()) - { - _coroSched->tars_run(); - } - - _coroSched->terminate(); - - _coroSched->destroy(); - - stopHandle(); - } -} - -void ServantHandle::handleRequest() -{ - bool bYield = false; - while (!getEpollServer()->isTerminate()) - { - bool bServerReqEmpty = false; - - { - TC_ThreadLock::Lock lock(_handleGroup->monitor); - - if (allAdapterIsEmpty() && allFilterIsEmpty()) - { - if(_coroSched->getResponseCoroSize() > 0) - { - bServerReqEmpty = true; - } - else - { - _handleGroup->monitor.timedWait(3000); - } - } - } - - //上报心跳 - heartbeat(); - - //为了实现所有主逻辑的å•çº¿ç¨‹åŒ–,在æ¯æ¬¡å¾ªçŽ¯ä¸­ç»™ä¸šåŠ¡å¤„ç†è‡ªæœ‰æ¶ˆæ¯çš„机会 - handleAsyncResponse(); - - handleCustomMessage(true); - - if(bServerReqEmpty) - { - _coroSched->yield(); - - continue; - } - - bYield = false; - - TC_EpollServer::tagRecvData* recv = NULL; - - map& adapters = _handleGroup->adapters; - - for (map::iterator it = adapters.begin(); it != adapters.end(); ++it) - { - TC_EpollServer::BindAdapterPtr& adapter = it->second; - - try - { - bool bFlag = true; - int iLoop = 100; - while(bFlag && iLoop > 0) - { - --iLoop; - - if(adapter->waitForRecvQueue(recv, 0)) - { - bYield = true; - - //上报心跳 - heartbeat(); - - //为了实现所有主逻辑的å•çº¿ç¨‹åŒ–,在æ¯æ¬¡å¾ªçŽ¯ä¸­ç»™ä¸šåŠ¡å¤„ç†è‡ªæœ‰æ¶ˆæ¯çš„机会 - handleAsyncResponse(); - - TC_EpollServer::tagRecvData& stRecvData = *recv; - - int64_t now = TNOWMS; - - stRecvData.adapter = adapter; - - //æ•°æ®å·²è¶…è½½ overload - if (stRecvData.isOverload) - { - handleOverload(stRecvData); - delete recv; - recv = NULL; - } - //å…³é—­è¿žæŽ¥çš„é€šçŸ¥æ¶ˆæ¯ - else if (stRecvData.isClosed) - { - handleClose(stRecvData); - delete recv; - recv = NULL; - } - //æ•°æ®åœ¨é˜Ÿåˆ—中已ç»è¶…时了 - else if ( (now - stRecvData.recvTimeStamp) > (int64_t)adapter->getQueueTimeout()) - { - handleTimeout(stRecvData); - delete recv; - recv = NULL; - } - else - { - uint32_t iRet = _coroSched->createCoroutine(std::bind(&ServantHandle::handleRecvData, this, recv)); - if(iRet == 0) - { - handleOverload(stRecvData); - delete recv; - recv = NULL; - } - } - handleCustomMessage(false); - } - else - { - bFlag = false; - bYield = false; - } - } - - if(iLoop == 0) - bYield = false; - } - catch (exception &ex) - { - if(recv) - { - close(recv->uid, recv->fd); - delete recv; - recv = NULL; - } - - getEpollServer()->error("[Handle::handleImp] error:" + string(ex.what())); - } - catch (...) - { - if(recv) - { - close(recv->uid, recv->fd); - delete recv; - recv = NULL; - } - - getEpollServer()->error("[Handle::handleImp] unknown error"); - } - } - - if(!bYield) - { - _coroSched->yield(); - } - } -} - -void ServantHandle::handleRecvData(TC_EpollServer::tagRecvData *stRecvData) -{ - try - { - TarsCurrentPtr current = createCurrent(*stRecvData); - - if (!current) - { - delete stRecvData; - stRecvData = NULL; - return; - } - - if (current->getBindAdapter()->isTarsProtocol()) - { - handleTarsProtocol(current); - } - else - { - handleNoTarsProtocol(current); - } - } - catch(exception &ex) - { - TLOGERROR("[TARS]ServantHandle::handleRecvData exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("[TARS]ServantHandle::handleRecvData unknown exception error" << endl); - } - - delete stRecvData; - stRecvData = NULL; -} - -void ServantHandle::handleAsyncResponse() -{ - ReqMessagePtr resp; - - map::iterator it = _servants.begin(); - - while (it != _servants.end()) - { - while (it->second->getResponseQueue().pop_front(resp, 0)) - { - try - { - if (resp->response.iRet == TARSSERVERSUCCESS) - { - it->second->doResponse(resp); - } - else if (resp->pObjectProxy == NULL) - { - it->second->doResponseNoRequest(resp); - } - else - { - it->second->doResponseException(resp); - } - } - catch (exception& e) - { - TLOGERROR("[TARS][ServantHandle::doResponse ex:" << e.what() << "]" << endl); - } - catch (...) - { - TLOGERROR("[TARS][ServantHandle::doResponse ex.]" << endl); - } - } - - //业务处ç†é™„åŠ çš„è‡ªæœ‰æ¶ˆæ¯ - try - { - it->second->doCustomMessage(false); - it->second->doCustomMessage(); - } - catch (exception& e) - { - TLOGERROR("[TARS][ServantHandle::doCustemMessage ex:" << e.what() << "]" << endl); - } - catch (...) - { - TLOGERROR("[TARS][ServantHandle::doCustemMessage ex.]" << endl); - } - - ++it; - } -} - -void ServantHandle::handleCustomMessage(bool bExpectIdle) -{ - for (map::iterator it = _servants.begin(); it != _servants.end(); it++) - { - //业务处ç†é™„åŠ çš„è‡ªæœ‰æ¶ˆæ¯ - try - { - it->second->doCustomMessage(bExpectIdle); - it->second->doCustomMessage(); - } - catch (exception& e) - { - TLOGERROR("[TARS][ServantHandle::doCustemMessage ex:" << e.what() << "]" << endl); - } - catch (...) - { - TLOGERROR("[TARS][ServantHandle::doCustemMessage ex.]" << endl); - } - } -} - -bool ServantHandle::allFilterIsEmpty() -{ - map::iterator it = _servants.begin(); - - while (it != _servants.end()) - { - if (it->second->getResponseQueue().size() > 0) - { - return false; - } - ++it; - } - return true; -} - -void ServantHandle::initialize() -{ - map::iterator adpit; - - map& adapters = _handleGroup->adapters; - - for (adpit = adapters.begin(); adpit != adapters.end(); ++adpit) - { - ServantPtr servant = ServantHelperManager::getInstance()->create(adpit->first); - - if (servant) - { - _servants[servant->getName()] = servant; - } - else - { - TLOGERROR("[TARS]ServantHandle initialize createServant ret null, for adapter `" + adpit->first + "`" << endl); - } - } - - map::iterator it = _servants.begin(); - - if(it == _servants.end()) - { - TLOGERROR("[TARS]initialize error: no servant exists." << endl); - - TarsRemoteNotify::getInstance()->report("initialize error: no servant exists."); - - exit(-1); - } - - while(it != _servants.end()) - { - try - { - it->second->setHandle(this); - - it->second->initialize(); - - TLOGINFO("[TARS][" << it->second->getName() << "] initialize" << endl); - } - catch(exception &ex) - { - TLOGERROR("[TARS]initialize error:" << ex.what() << endl); - - TarsRemoteNotify::getInstance()->report("initialize error:" + string(ex.what())); - - exit(-1); - } - catch(...) - { - TLOGERROR("[TARS]initialize unknown exception error" << endl); - - TarsRemoteNotify::getInstance()->report("initialize error"); - - exit(-1); - } - ++it; - } -} - -void ServantHandle::heartbeat() -{ - time_t fcur = TNOW; - - map::iterator it; - - map& adapters = _handleGroup->adapters; - - for (it = adapters.begin(); it != adapters.end(); ++it) - { - if (abs(fcur - it->second->getHeartBeatTime()) > HEART_BEAT_INTERVAL) - { - it->second->setHeartBeatTime(fcur); - - TARS_KEEPALIVE(it->second->getName()); - - //上报连接数 比率 - if (it->second->_pReportConRate) - { - it->second->_pReportConRate->report(it->second->getNowConnection()*1000/it->second->getMaxConns()); - } - - //有队列, 且队列长度>0æ‰ä¸ŠæŠ¥ - if (it->second->_pReportQueue) - { - it->second->_pReportQueue->report(it->second->getRecvBufferSize()); - } - } - } -} - -TarsCurrentPtr ServantHandle::createCurrent(const TC_EpollServer::tagRecvData &stRecvData) -{ - TarsCurrentPtr current = new TarsCurrent(this); - - try - { - current->initialize(stRecvData, stRecvData.recvTimeStamp); - } - catch (TarsDecodeException &ex) - { - TLOGERROR("[TARS]ServantHandle::handle request protocol decode error:" << ex.what() << endl); - close(stRecvData.uid, stRecvData.fd); - return NULL; - } - - //åªæœ‰TARSåè®®æ‰å¤„ç† - if(current->getBindAdapter()->isTarsProtocol()) - { - int64_t now = TNOWMS; - - //æ•°æ®åœ¨é˜Ÿåˆ—中的时间超过了客户端等待的时间(TARSåè®®) - if (current->_request.iTimeout > 0 && (now - stRecvData.recvTimeStamp) > current->_request.iTimeout) - { - //上报超时数目 - if(stRecvData.adapter->_pReportTimeoutNum) - stRecvData.adapter->_pReportTimeoutNum->report(1); - - TLOGERROR("[TARS]ServantHandle::handle queue timeout:" - << current->_request.sServantName << "|" - << current->_request.sFuncName << "|" - << stRecvData.recvTimeStamp << "|" - << stRecvData.adapter->getQueueTimeout() << "|" - << current->_request.iTimeout << "|" - << now << "|" << stRecvData.ip << ":" << stRecvData.port << endl); - - current->sendResponse(TARSSERVERQUEUETIMEOUT); - - return NULL; - } - } - - return current; -} - -TarsCurrentPtr ServantHandle::createCloseCurrent(const TC_EpollServer::tagRecvData &stRecvData) -{ - TarsCurrentPtr current = new TarsCurrent(this); - - current->initializeClose(stRecvData); - current->setReportStat(false); - current->setCloseType(stRecvData.closeType); - return current; -} - -void ServantHandle::handleClose(const TC_EpollServer::tagRecvData &stRecvData) -{ - TLOGINFO("[TARS]ServantHandle::handleClose,adapter:" << stRecvData.adapter->getName() - << ",peer:" << stRecvData.ip << ":" << stRecvData.port << endl); - - TarsCurrentPtr current = createCloseCurrent(stRecvData); - - map::iterator sit = _servants.find(current->getServantName()); - - if (sit == _servants.end()) - { - TLOGERROR("[TARS]ServantHandle::handleClose,adapter:" << stRecvData.adapter->getName() - << ",peer:" << stRecvData.ip << ":" << stRecvData.port <<", " << current->getServantName() << " not found" << endl); - - return; - } - - - try - { - //ä¸šåŠ¡é€»è¾‘å¤„ç† - sit->second->doClose(current); - } - catch(exception &ex) - { - TLOGERROR("[TARS]ServantHandle::handleClose " << ex.what() << endl); - - return; - } - catch(...) - { - TLOGERROR("[TARS]ServantHandle::handleClose unknown error" << endl); - - return; - } -} - -void ServantHandle::handleTimeout(const TC_EpollServer::tagRecvData &stRecvData) -{ - TarsCurrentPtr current = createCurrent(stRecvData); - - if (!current) return; - - //上报超时数目 - if(stRecvData.adapter->_pReportTimeoutNum) - stRecvData.adapter->_pReportTimeoutNum->report(1); - - TLOGERROR("[TARS]ServantHandle::handleTimeout adapter '" - << stRecvData.adapter->getName() - << "', recvtime:" << stRecvData.recvTimeStamp << "|" - << ", timeout:" << stRecvData.adapter->getQueueTimeout() - << ", id:" << current->getRequestId() << endl); - - if (current->getBindAdapter()->isTarsProtocol()) - { - current->sendResponse(TARSSERVERQUEUETIMEOUT); - } -} - -void ServantHandle::handleOverload(const TC_EpollServer::tagRecvData &stRecvData) -{ - TarsCurrentPtr current = createCurrent(stRecvData); - - if (!current) return; - - TLOGERROR("[TARS]ServantHandle::handleOverload adapter '" - << stRecvData.adapter->getName() - << "',overload:-1,queue capacity:" - << stRecvData.adapter->getQueueCapacity() - << ",id:" << current->getRequestId() << endl); - - if (current->getBindAdapter()->isTarsProtocol()) - { - current->sendResponse(TARSSERVEROVERLOAD); - } -} - -void ServantHandle::handle(const TC_EpollServer::tagRecvData &stRecvData) -{ - TarsCurrentPtr current = createCurrent(stRecvData); - - if (!current) return; - - if (current->getBindAdapter()->isTarsProtocol()) - { - handleTarsProtocol(current); - } - else - { - handleNoTarsProtocol(current); - } -} - -//处ç†é‡‡æ ·ä¿¡æ¯ -void ServantHandle::processSample(const TarsCurrentPtr ¤t) -{ - ServantProxyThreadData * sptd = ServantProxyThreadData::getData(); - - assert(sptd); - - if(!sptd) - { - return; - } - - sptd->_sampleKey.init(); - - //自æœåŠ¡æ¡†æž¶TarsCurrentåˆå§‹åŒ– 父节点广度设置为-1 - sptd->_sampleKey._parentWidth = -1; - - //å·²ç»æ˜¯tars调用了, 则éžæ ¹èŠ‚点 - sptd->_sampleKey._root = false; - - if(IS_MSG_TYPE(current->getMessageType(), tars::TARSMESSAGETYPESAMPLE)) - { - map::const_iterator iter; - const map & requestStatus = current->getRequestStatus(); - iter = requestStatus.find(ServantProxy::STATUS_SAMPLE_KEY); - if(iter != requestStatus.end()) - { - //statu 里é¢çš„内容为 "id|depth|width"å½¢å¼.åˆåœ¨ä¸€èµ·ä»¥ä¾¿èŠ‚çœç¼–è§£ç  - vector v = TC_Common::sepstr(iter->second,"|"); - - if(v.size() > 2) - { - sptd->_sampleKey._unid = v[0]; - - //深度+1 - sptd->_sampleKey._depth = TC_Common::strto(v[1])+1; - - sptd->_sampleKey._parentWidth = TC_Common::strto(v[2]); - - } - } - } -} - -bool ServantHandle::processDye(const TarsCurrentPtr ¤t, string& dyeingKey) -{ - //当å‰çº¿ç¨‹çš„çº¿ç¨‹æ•°æ® - ServantProxyThreadData* sptd = ServantProxyThreadData::getData(); - - if (sptd) - { - sptd->_dyeingKey = ""; - } - - //当å‰è¯·æ±‚å·²ç»è¢«æŸ“色, 需è¦æ‰“å°æŸ“色日志 - map::const_iterator dyeingIt = current->getRequestStatus().find(ServantProxy::STATUS_DYED_KEY); - - if (IS_MSG_TYPE(current->getMessageType(), tars::TARSMESSAGETYPEDYED)) - { - TLOGINFO("[TARS] servant got a dyeing request, message_type set" << current->getMessageType() << endl); - - if (dyeingIt != current->getRequestStatus().end()) - { - TLOGINFO("[TARS] servant got a dyeing request, dyeing key:" << dyeingIt->second << endl); - - dyeingKey = dyeingIt->second; - } - return true; - } - - //servantå·²ç»è¢«æŸ“色, å¼€å¯æŸ“色日志 - if (ServantHelperManager::getInstance()->isDyeing()) - { - map::const_iterator dyeingKeyIt = current->getRequestStatus().find(ServantProxy::STATUS_GRID_KEY); - - if (dyeingKeyIt != current->getRequestStatus().end() && - ServantHelperManager::getInstance()->isDyeingReq(dyeingKeyIt->second, current->getServantName(), current->getFuncName())) - { - TLOGINFO("[TARS] dyeing servant got a dyeing req, key:" << dyeingKeyIt->second << endl); - - dyeingKey = dyeingKeyIt->second; - - return true; - } - } - - return false; -} - -bool ServantHandle::checkValidSetInvoke(const TarsCurrentPtr ¤t) -{ - /*是å¦å…许检查åˆæ³•æ€§*/ - if (ServerConfig::IsCheckSet == 0) - { - //ä¸æ£€æŸ¥ - return true; - } - - bool isSetInvoke = IS_MSG_TYPE(current->getMessageType(), tars::TARSMESSAGETYPESETNAME); - //客户端按set规则调用且æœåŠ¡ç«¯å¯ç”¨set - if (isSetInvoke && ClientConfig::SetOpen) - { - /** - * åˆæ³•æ€§è§„则: - * 1 客户端setå称与æœåŠ¡ç«¯set在åŒä¸€åˆ†ç»„,eg, test.s.1 <-> test.s.1 - * 2 客户端setå称与æœåŠ¡ç«¯set在åŒä¸€åœ°åŒº,eg, test.s.* <-> test.s.1 | test.s.2 | test.s.* - * 3 客户端setå称与æœåŠ¡ç«¯set属于ä¸åŒå称,eg,test1.s.1 <->test2.n.2 - * 4 1,2,3æ¡ä»¶éƒ½ä¸æ»¡è¶³ï¼Œåˆ™è®¤ä¸ºè¯¥è°ƒç”¨ä¸åˆæ³• - */ - map::const_iterator setIt = current->getRequestStatus().find(ServantProxy::STATUS_SETNAME_VALUE); - string sSetName(""); - - if (setIt != current->getRequestStatus().end()) - { - TLOGINFO("[TARS] servant got a setname request, setname key:" << setIt->second << endl); - - sSetName = setIt->second; - - if (ClientConfig::SetDivision == sSetName) - { - return true; - } - else - { - //属于åŒä¸€åœ°åŒºæ˜¯ä¹Ÿå±žäºŽåˆæ³•è°ƒç”¨ - string setArea1 = ClientConfig::SetDivision.substr(0,ClientConfig::SetDivision.find_last_of(".")); - string setArea2 = sSetName.substr(0,sSetName.find_last_of(".")); - if (setArea1 == setArea2) - { - return true; - } - else if (ClientConfig::SetDivision.substr(0,ClientConfig::SetDivision.find_first_of(".")) != - sSetName.substr(0,sSetName.find_first_of("."))) - { - //属于ä¸åŒçš„set之间调用也属于åˆæ³• - return true; - } - else - { - TLOGERROR("[TARS]ServantHandle::checkValidSetInvoke|" - << current->getIp() << "|" - << current->getMessageType() << "|" - << current->getServantName() << "|" - << current->getFuncName() << "|client:" - << ClientConfig::SetDivision << "|server:" - << sSetName << endl); - current->sendResponse(TARSINVOKEBYINVALIDESET); - return false; - } - } - } - else - { - TLOGERROR("[TARS]ServantHandle::checkValidSetInvoke|" - << current->getIp() << "|" - << current->getMessageType() << "|" - << current->getServantName() << "|" - << current->getFuncName() << "|client:" - << ClientConfig::SetDivision << "|server:" - << sSetName << endl); - current->sendResponse(TARSINVOKEBYINVALIDESET); - return false; - } - } - - //没有按set规则调用 - return true; -} - -void ServantHandle::handleTarsProtocol(const TarsCurrentPtr ¤t) -{ - TLOGINFO("[TARS]ServantHandle::handleTarsProtocol current:" - << current->getIp() << "|" - << current->getPort() << "|" - << current->getMessageType() << "|" - << current->getServantName() << "|" - << current->getFuncName() << "|" - << current->getRequestId() << "|" - << TC_Common::tostr(current->getRequestStatus())<::iterator sit = _servants.find(current->getServantName()); - - if (sit == _servants.end()) - { - current->sendResponse(TARSSERVERNOSERVANTERR); - - return; - } - - int ret = TARSSERVERUNKNOWNERR; - - string sResultDesc = ""; - - vector buffer; - - try - { - //ä¸šåŠ¡é€»è¾‘å¤„ç† - ret = sit->second->dispatch(current, buffer); - } - catch(TarsDecodeException &ex) - { - TLOGERROR("[TARS]ServantHandle::handleTarsProtocol " << ex.what() << endl); - - ret = TARSSERVERDECODEERR; - - sResultDesc = ex.what(); - } - catch(TarsEncodeException &ex) - { - TLOGERROR("[TARS]ServantHandle::handleTarsProtocol " << ex.what() << endl); - - ret = TARSSERVERENCODEERR; - - sResultDesc = ex.what(); - } - catch(exception &ex) - { - TLOGERROR("[TARS]ServantHandle::handleTarsProtocol " << ex.what() << endl); - - ret = TARSSERVERUNKNOWNERR; - - sResultDesc = ex.what(); - } - catch(...) - { - TLOGERROR("[TARS]ServantHandle::handleTarsProtocol unknown error" << endl); - - ret = TARSSERVERUNKNOWNERR; - - sResultDesc = "handleTarsProtocol unknown exception error"; - } - - //å•å‘调用或者业务ä¸éœ€è¦åŒæ­¥è¿”回 - if (current->isResponse()) - { - current->sendResponse(ret, buffer, TarsCurrent::TARS_STATUS(), sResultDesc); - } -} - -void ServantHandle::handleNoTarsProtocol(const TarsCurrentPtr ¤t) -{ - TLOGINFO("[TARS]ServantHandle::handleNoTarsProtocol current:" - << current->getIp() << "|" - << current->getPort() << "|" - << current->getServantName() << endl); - - map::iterator sit = _servants.find(current->getServantName()); - - assert(sit != _servants.end()); - - vector buffer; - - try - { - //ä¸šåŠ¡é€»è¾‘å¤„ç† - sit->second->dispatch(current, buffer); - } - catch(exception &ex) - { - TLOGERROR("[TARS]ServantHandle::handleNoTarsProtocol " << ex.what() << endl); - } - catch(...) - { - TLOGERROR("[TARS]ServantHandle::handleNoTarsProtocol unknown error" << endl); - } - - if (current->isResponse()) - { - current->sendResponse((const char*)(&buffer[0]), buffer.size()); - } -} - -//////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/ServantHelper.cpp b/cpp/servant/libservant/ServantHelper.cpp deleted file mode 100644 index 98b955fc8..000000000 --- a/cpp/servant/libservant/ServantHelper.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/ServantHelper.h" - -namespace tars -{ - -ServantPtr ServantHelperManager::create(const string &sAdapter) -{ - if(_adapter_servant.find(sAdapter) == _adapter_servant.end()) - { - return NULL; - } - - ServantPtr servant = NULL; - - //æ ¹æ®adapter查找servantå称 - string s = _adapter_servant[sAdapter]; - - if(_servant_creator.find(s) != _servant_creator.end()) - { - servant = _servant_creator[s]->create(s); - } - return servant; -} - -void ServantHelperManager::setAdapterServant(const string &sAdapter, const string &sServant) -{ - _adapter_servant[sAdapter] = sServant; - - _servant_adapter[sServant] = sAdapter; -} - -bool ServantHelperManager::setDyeing(const string & sDyeingKey, const string & sDyeingServant, - const string & sDyeingInterface) -{ - TC_LockT lock(_mutex); - - _dyeingKey = sDyeingKey; - _dyeingServant = sDyeingServant; - _dyeingInterface = sDyeingInterface; - - _isDyeing = !sDyeingKey.empty(); - - return true; -} - -bool ServantHelperManager::isDyeingReq(const string & sKey, const string & sServant, const string & sInterface) -{ - TC_LockT lock(_mutex); - - return ((_dyeingKey == sKey) && (_dyeingServant == sServant) && - (_dyeingInterface == "" || _dyeingInterface == sInterface) ); -} - -} - - diff --git a/cpp/servant/libservant/ServantProxy.cpp b/cpp/servant/libservant/ServantProxy.cpp deleted file mode 100644 index ba370bef5..000000000 --- a/cpp/servant/libservant/ServantProxy.cpp +++ /dev/null @@ -1,994 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/ServantProxy.h" -#include "servant/ServantHandle.h" -#include "servant/StatReport.h" -#include "servant/Application.h" -#include "servant/BaseF.h" -#include "servant/TarsLogger.h" -#include "servant/Message.h" -#include "servant/EndpointManager.h" - -namespace tars -{ - -/////////////////////////////////////////////////////////////// - -TC_ThreadMutex ServantProxyThreadData::_mutex; - -pthread_key_t ServantProxyThreadData::_key = 0; - -SeqManager * ServantProxyThreadData::_pSeq = new SeqManager(MAX_CLIENT_NOTIFYEVENT_NUM); - -/////////////////////////////////////////////////////////////// -SeqManager::SeqManager(size_t iNum) -{ - assert(iNum < MAX_UNSIGN_SHORT); - assert(iNum > 0); - _p = NULL; - _p = new SeqInfo[iNum]; - assert(_p); - - //0xffè¡¨ç¤ºç»“æŸ - _free = 0; - _freeTail = iNum -1; - - for(uint16_t i=0;i<(uint16_t)iNum;i++) - { - _p[i].free = true; - _p[i].next = i+1; - } - _p[iNum-1].next = MAX_UNSIGN_SHORT; - _num = iNum; -} - -uint16_t SeqManager::get() -{ - assert(_free != MAX_UNSIGN_SHORT); - - assert(_p[_free].free); - - uint16_t buf = _free; - - if(_free == _freeTail) - { - assert(_p[buf].next == MAX_UNSIGN_SHORT); - _freeTail = MAX_UNSIGN_SHORT; - } - - _free = _p[buf].next; - - _p[buf].free = false; - - return buf; -} - -void SeqManager::del(uint16_t iSeq) -{ - assert(iSeq < _num); - assert(!_p[iSeq].free); - - _p[iSeq].next = MAX_UNSIGN_SHORT; - if(MAX_UNSIGN_SHORT == _freeTail) - { - _free = iSeq; - } - else - { - _p[_freeTail].next = iSeq; - } - _p[iSeq].free = true; - _freeTail = iSeq; -} - -/////////////////////////////////////////////////////////////// -ServantProxyThreadData::ServantProxyThreadData() -: _queueInit(false) -, _reqQNo(0) -, _netSeq(0) -, _netThreadSeq(-1) -, _hash(false) -, _conHash(false) -, _hashCode(-1) -, _dyeing(false) -, _hasTimeout(false) -, _timeout(0) -, _objectProxyNum(0) -, _objectProxy(NULL) -, _sched(NULL) -{ - _szHost[0] = '\0'; -} - -ServantProxyThreadData::~ServantProxyThreadData() -{ - try - { - TC_LockT lock(_mutex); - - if(_queueInit) - { - for(size_t i=0;i<_objectProxyNum;++i) - { - ReqMessage * msg = new ReqMessage(); - msg->eType = ReqMessage::THREAD_EXIT; - - bool bEmpty = false; - _reqQueue[i]->push_back(msg, bEmpty); - - _objectProxy[i]->getCommunicatorEpoll()->notifyDel(_reqQNo); - - _queueInit = false; - } - } - - _pSeq->del(_reqQNo); - } - catch (...) - { - } -} - -void ServantProxyThreadData::destructor(void* p) -{ - ServantProxyThreadData * pSptd = (ServantProxyThreadData*)p; - if(pSptd) - { - delete pSptd; - pSptd = NULL; - } -} - -ServantProxyThreadData * ServantProxyThreadData::getData() -{ - if(_key == 0) - { - TC_LockT lock(_mutex); - if(_key == 0) - { - int iRet = ::pthread_key_create(&_key, ServantProxyThreadData::destructor); - - if (iRet != 0) - { - TLOGERROR("[TARS][ServantProxyThreadData pthread_key_create fail:" << errno << ":" << strerror(errno) << "]" << endl); - return NULL; - } - } - } - - ServantProxyThreadData * pSptd = (ServantProxyThreadData*)pthread_getspecific(_key); - - if(!pSptd) - { - TC_LockT lock(_mutex); - - pSptd = new ServantProxyThreadData(); - pSptd->_reqQNo = _pSeq->get(); - - int iRet = pthread_setspecific(_key, (void *)pSptd); - - assert(iRet == 0); - } - - return pSptd; -} - -/////////////////////////////////////////////////////////////// -ServantProxyCallback::ServantProxyCallback() -: _bNetThreadProcess(false) -{ -} - -HttpServantProxyCallback::HttpServantProxyCallback(HttpCallback* cb) : - _httpCb(cb) -{ -} - -int HttpServantProxyCallback::onDispatch(ReqMessagePtr msg) -{ - if (!_httpCb) - return 0; - - if (msg->response.iRet != tars::TARSSERVERSUCCESS) - _httpCb->onHttpResponseException(msg->request.context, msg->response.iRet); - else - return _httpCb->onHttpResponse(msg->request.context, msg->response.status, msg->response.sBuffer); - - return 0; -} - -/////////////////////////////////////////////////////////////// -void coroWhenAll(const CoroParallelBasePtr &ptr) -{ - if(!ptr->checkAllReqSend()) - { - TLOGERROR("[TARS][coroWhenAll use coro invoke interface's num not equal ptr set value]"<_sched) - { - TLOGERROR("[TARS][coroWhenAll no open coroutine mode]"<_sched->yield(false); - - vector vMsg = ptr->getAllReqMessage(); - - for(size_t i = 0; i < vMsg.size(); ++i) - { - ReqMessagePtr msgPtr = vMsg[i]; - vMsg[i]->callback->onDispatch(msgPtr); - } -} - -/////////////////////////////////////////////////////////////// -string ServantProxy::STATUS_DYED_KEY = "STATUS_DYED_KEY"; - -string ServantProxy::STATUS_GRID_KEY = "STATUS_GRID_KEY"; - -string ServantProxy::STATUS_SAMPLE_KEY = "STATUS_SAMPLE_KEY"; - -string ServantProxy::STATUS_RESULT_CODE = "STATUS_RESULT_CODE"; - -string ServantProxy::STATUS_RESULT_DESC = "STATUS_RESULT_DESC"; - -string ServantProxy::STATUS_SETNAME_VALUE = "STATUS_SETNAME_VALUE"; - -string ServantProxy::TARS_MASTER_KEY = "TARS_MASTER_KEY"; - - -ServantProxy::ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObjectProxy, size_t iClientThreadNum) -: _communicator(pCommunicator) -, _objectProxy(ppObjectProxy) -, _objectProxyNum(iClientThreadNum) -, _syncTimeout(DEFAULT_SYNCTIMEOUT) -, _asyncTimeout(DEFAULT_ASYNCTIMEOUT) -, _id(0) -, _masterFlag(false) -, _queueSize(1000) -, _minTimeout(100) -{ - _endpointInfo.reset(new EndpointManagerThread(pCommunicator, (*_objectProxy)->name())); - - for(size_t i = 0;i < _objectProxyNum; ++i) - { - (*(_objectProxy + i))->setServantProxy(this); - } - - if(pCommunicator) - { - _queueSize = TC_Common::strto(pCommunicator->getProperty("reqqueuenum", "1000")); - if(_queueSize < 1000) - { - _queueSize = 1000; - } - } - - _minTimeout = pCommunicator->getMinTimeout(); - if(_minTimeout < 1) - { - _minTimeout = 1; - } - // get AK/SK - const TC_Config& conf = Application::getConfig(); - vector adapterNames; - - if (conf.getDomainVector("/tars/application/client", adapterNames)) - { - auto it = std::find(adapterNames.begin(), adapterNames.end(), tars_name()); - if (it != adapterNames.end()) - { - string accessKey = conf.get("/tars/application/client/" + *it + ""); - string secretKey = conf.get("/tars/application/client/" + *it + ""); - - for(size_t i = 0;i < _objectProxyNum; ++i) - { - _objectProxy[i]->setAccessKey(accessKey); - _objectProxy[i]->setSecretKey(secretKey); - } - } - } -} - -ServantProxy::~ServantProxy() -{ -} - -string ServantProxy::tars_name() const -{ - if(_objectProxyNum >= 1 && (*_objectProxy != NULL)) - { - return (*_objectProxy)->name(); - } - return "NULL"; -} - - -TC_Endpoint ServantProxy::tars_invoke_endpoint() -{ - ServantProxyThreadData* td = ServantProxyThreadData::getData(); - - if(td) - { - td->_szHost[sizeof(td->_szHost) - 1] = '\0';//防止被误æ“作,导致没有结æŸç¬¦ - - return TC_Endpoint(td->_szHost); - } - return TC_Endpoint(); -} - -void ServantProxy::tars_timeout(int msecond) -{ - { - TC_LockT lock(*this); - //ä¿æŠ¤ï¼Œè¶…时时间ä¸èƒ½å°äºŽ_minTimeout毫秒 - _syncTimeout = (msecond < _minTimeout)?_minTimeout:msecond; - } -} - -int ServantProxy::tars_timeout() const -{ - return _syncTimeout; -} - -void ServantProxy::tars_connect_timeout(int conTimeout) -{ - if(conTimeout < 100) - { - conTimeout = 100; - } - - if(conTimeout > 5000) - { - conTimeout = 5000; - } - - for(size_t i = 0;i < _objectProxyNum; ++i) - { - (*(_objectProxy + i))->setConTimeout(conTimeout); - } -} - -void ServantProxy::tars_async_timeout(int msecond) -{ - { - TC_LockT lock(*this); - //ä¿æŠ¤ï¼Œè¶…时时间ä¸èƒ½å°äºŽ_minTimeout毫秒 - _asyncTimeout = (msecond < _minTimeout)?_minTimeout:msecond; - } -} - -int ServantProxy::tars_async_timeout() const -{ - return _asyncTimeout; -} - - -void ServantProxy::tars_set_protocol(const ProxyProtocol& protocol, const std::string& protoName) -{ - TC_LockT lock(*this); - - for(size_t i = 0;i < _objectProxyNum; ++i) - { - (*(_objectProxy + i))->setProxyProtocol(protocol, protoName); - } -} - - -void ServantProxy::tars_set_sockopt(int level, int optname, const void * optval, socklen_t optlen) -{ - TC_LockT lock(*this); - - for(size_t i = 0;i < _objectProxyNum; ++i) - { - (*(_objectProxy + i))->setSocketOpt(level, optname, optval, optlen); - } -} - -void ServantProxy::tars_set_check_timeout(const CheckTimeoutInfo& checkTimeoutInfo) -{ - TC_LockT lock(*this); - - for(size_t i = 0;i < _objectProxyNum; ++i) - { - (*(_objectProxy + i))->checkTimeoutInfo().minTimeoutInvoke = checkTimeoutInfo.minTimeoutInvoke; - (*(_objectProxy + i))->checkTimeoutInfo().checkTimeoutInterval = checkTimeoutInfo.checkTimeoutInterval; - (*(_objectProxy + i))->checkTimeoutInfo().frequenceFailInvoke = checkTimeoutInfo.frequenceFailInvoke; - (*(_objectProxy + i))->checkTimeoutInfo().minFrequenceFailTime = checkTimeoutInfo.minFrequenceFailTime; - (*(_objectProxy + i))->checkTimeoutInfo().radio = checkTimeoutInfo.radio; - (*(_objectProxy + i))->checkTimeoutInfo().tryTimeInterval = checkTimeoutInfo.tryTimeInterval; - } -} - -CheckTimeoutInfo ServantProxy::tars_get_check_timeout() -{ - CheckTimeoutInfo checkTimeoutInfo; - - if(_objectProxyNum > 0) - { - checkTimeoutInfo.minTimeoutInvoke = (*_objectProxy)->checkTimeoutInfo().minTimeoutInvoke; - checkTimeoutInfo.checkTimeoutInterval = (*_objectProxy)->checkTimeoutInfo().checkTimeoutInterval; - checkTimeoutInfo.frequenceFailInvoke = (*_objectProxy)->checkTimeoutInfo().frequenceFailInvoke; - checkTimeoutInfo.minFrequenceFailTime = (*_objectProxy)->checkTimeoutInfo().minFrequenceFailTime; - checkTimeoutInfo.radio = (*_objectProxy)->checkTimeoutInfo().radio; - checkTimeoutInfo.tryTimeInterval = (*_objectProxy)->checkTimeoutInfo().tryTimeInterval; - } - - return checkTimeoutInfo; -} - -void ServantProxy::tars_ping() -{ - vector v; - - map m; - - map s; - - ResponsePacket rsp; - - tars_invoke(tars::TARSNORMAL, "tars_ping", v, m, s, rsp); -} - -ServantProxy* ServantProxy::tars_hash(int64_t key) -{ - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - - assert(pSptd != NULL); - - pSptd->_hash = true; - pSptd->_hashCode = key; - - return this; -} - -ServantProxy* ServantProxy::tars_consistent_hash(int64_t key) -{ - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - - assert(pSptd != NULL); - - pSptd->_hash = true; - pSptd->_conHash = true; - pSptd->_hashCode = key; - - return this; -} - -void ServantProxy::tars_clear_hash() -{ -} - -ServantProxy* ServantProxy::tars_set_timeout(int msecond) -{ - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - assert(pSptd != NULL); - - pSptd->_hasTimeout = true; - pSptd->_timeout = msecond; - - return this; -} - -uint32_t ServantProxy::tars_gen_requestid() -{ - TC_LockT lock(*this); - return (*_objectProxy)->generateId(); -} - -void ServantProxy::tars_set_push_callback(const ServantProxyCallbackPtr & cb) -{ - for(size_t i = 0;i < _objectProxyNum; ++i) - { - (*(_objectProxy + i))->setPushCallbacks(cb); - } -} - -void ServantProxy::invoke(ReqMessage * msg, bool bCoroAsync) -{ - msg->proxy = this; - msg->response.iRet = TARSSERVERUNKNOWNERR; - - //线程ç§æœ‰æ•°æ® - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - assert(pSptd != NULL); - - msg->bHash = pSptd->_hash; - msg->bConHash = pSptd->_conHash; - msg->iHashCode = pSptd->_hashCode; - - //hashæ¯æ¬¡è°ƒç”¨å®Œæˆéƒ½è¦æ¸…掉,ä¸ç”¨é€ä¼  - pSptd->_hash = false; - pSptd->_conHash = false; - - //染色需è¦é€ä¼  - msg->bDyeing = pSptd->_dyeing; - msg->sDyeingKey = pSptd->_dyeingKey; - - if(msg->bDyeing) - { - TLOGINFO("[TARS][ServantProxy::invoke, set dyeing, key=" << pSptd->_dyeingKey << endl); - } - - //采样信æ¯éœ€è¦é€ä¼  - msg->sampleKey = pSptd->_sampleKey; - //调用广度è¦+1 - pSptd->_sampleKey._width ++; - - //设置超时时间 - msg->request.iTimeout = (ReqMessage::SYNC_CALL == msg->eType)?_syncTimeout:_asyncTimeout; - - //判断是å¦é’ˆå¯¹æŽ¥å£çº§è®¾ç½®è¶…æ—¶ - if(pSptd->_hasTimeout) - { - msg->request.iTimeout = (pSptd->_timeout > 0)?pSptd->_timeout:msg->request.iTimeout; - pSptd->_hasTimeout = false; - } - - ObjectProxy * pObjProxy = NULL; - ReqInfoQueue * pReqQ = NULL; - - //选择网络线程 - selectNetThreadInfo(pSptd,pObjProxy,pReqQ); - - //调用å‘起时间 - msg->iBeginTime = TNOWMS; - msg->pObjectProxy = pObjProxy; - - //如果是按set规则调用 - if (pObjProxy && pObjProxy->IsInvokeBySet()) - { - SET_MSG_TYPE(msg->request.iMessageType, tars::TARSMESSAGETYPESETNAME); - msg->request.status[ServantProxy::STATUS_SETNAME_VALUE] = pObjProxy->getInvokeSetName(); - - TLOGINFO("[TARS][ServantProxy::invoke, " << msg->request.sServantName << ", invoke with set,"<getInvokeSetName()<<"]" << endl); - } - - //åŒæ­¥è°ƒç”¨ new 一个ReqMonitor - assert(msg->pMonitor == NULL); - if(msg->eType == ReqMessage::SYNC_CALL) - { - msg->bMonitorFin = false; - - if(pSptd->_sched) - { - msg->bCoroFlag = true; - msg->sched = pSptd->_sched; - msg->iCoroId = pSptd->_sched->getCoroutineId(); - } - else - { - msg->pMonitor = new ReqMonitor; - } - } - - if(ReqMessage::ASYNC_CALL == msg->eType) - { - //是å¦æ˜¯å程的并行请求 - if(bCoroAsync) - { - if(pSptd->_sched) - { - CoroParallelBasePtr coroPtr = msg->callback->getCoroParallelBasePtr(); - if(coroPtr) - { - coroPtr->incReqCount(); - - msg->bCoroFlag = true; - msg->sched = pSptd->_sched; - msg->iCoroId = pSptd->_sched->getCoroutineId(); - } - else - { - TLOGERROR("[TARS][ServantProxy::invoke use coroutine's callback not set CoroParallelBasePtr]"<eType == ReqMessage::SYNC_CALL); - - if(!pReqQ->push_back(msg,bEmpty)) - { - TLOGERROR("[TARS][ServantProxy::invoke msgQueue push_back error num:" << pSptd->_netSeq << "]" << endl); - - delete msg; - msg = NULL; - - pObjProxy->getCommunicatorEpoll()->notify(pSptd->_reqQNo, pReqQ); - - throw TarsClientQueueException("client queue full"); - } - - pObjProxy->getCommunicatorEpoll()->notify(pSptd->_reqQNo, pReqQ); - - //异步调用 å¦ä¸€ä¸ªçº¿ç¨‹delele msg 如果是异步åŽé¢ä¸èƒ½å†ç”¨msg了 - - if(bSync) - { - if(!msg->bCoroFlag) - { - if(!msg->bMonitorFin) - { - TC_ThreadLock::Lock lock(*(msg->pMonitor)); - - //ç­‰å¾…ç›´åˆ°ç½‘ç»œçº¿ç¨‹é€šçŸ¥è¿‡æ¥ - if(!msg->bMonitorFin) - { - msg->pMonitor->wait(); - } - } - } - else - { - msg->sched->yield(false); - } - - //判断eStatusæ¥åˆ¤æ–­çŠ¶æ€ - assert(msg->eStatus != ReqMessage::REQ_REQ); - - TLOGINFO("[TARS]ServantProxy::invoke line: " << __LINE__ << " status: " << msg->eStatus << " ret: " <response.iRet << endl); - - if(msg->eStatus == ReqMessage::REQ_RSP && msg->response.iRet == TARSSERVERSUCCESS) - { - snprintf(pSptd->_szHost, sizeof(pSptd->_szHost), "%s", msg->adapter->endpoint().desc().c_str()); - //æˆåŠŸ - return; - } - - ostringstream os; - if(msg->eStatus == ReqMessage::REQ_TIME) - { - //超时 - os << "[ServantProxy::invoke timeout:" << msg->request.iTimeout; - } - else - { - os << "[ServantProxy::invoke errno:" << msg->response.iRet << ",info:" << msg->response.sResultDesc; - } - - os << ",servant:" << msg->pObjectProxy->name() << ",func:" << msg->request.sFuncName; - - if(msg->adapter) - { - os << ",adapter" << msg->adapter->endpoint().desc(); - } - - os << ",reqid:" << msg->request.iRequestId << "]"; - - if(msg->eStatus == ReqMessage::REQ_TIME) - { - //超时 - delete msg; - msg = NULL; - - throw TarsSyncCallTimeoutException(os.str()); - } - - //异常调用 - int ret = msg->response.iRet; - - delete msg; - msg = NULL; - - TarsException::throwException(ret, os.str()); - } -} - -//åŒæ­¥è°ƒç”¨è¿”回,唤醒等待的业务线程 -void ServantProxy::finished(ReqMessage * msg) -{ - if(msg->pMonitor) - { - TC_ThreadLock::Lock sync(*(msg->pMonitor)); - msg->pMonitor->notify(); - msg->bMonitorFin = true; - } -} - -////////////////////////////////////////////////////////////////// -void ServantProxy::tars_invoke_async(char cPacketType, - const string &sFuncName, - const vector& buf, - const map& context, - const map& status, - const ServantProxyCallbackPtr& callback, - bool bCoro) -{ - ReqMessage * msg = new ReqMessage(); - - msg->init(callback?ReqMessage::ASYNC_CALL:ReqMessage::ONE_WAY,NULL,sFuncName); - msg->callback = callback; - - msg->request.iVersion = TARSVERSION; - msg->request.cPacketType = (callback ? cPacketType : TARSONEWAY); - - msg->request.sServantName = (*_objectProxy)->name(); - msg->request.sFuncName = sFuncName; - msg->request.sBuffer = buf; - msg->request.context = context; - msg->request.status = status; - msg->request.iTimeout = _asyncTimeout; - - // 在RequestPacket中的contextè®¾ç½®ä¸»è°ƒä¿¡æ¯ - if(_masterFlag) - { - msg->request.context.insert(std::make_pair(TARS_MASTER_KEY,ClientConfig::ModuleName)); //TARS_MASTER_KEY clientConfig.ModuleName - } - - checkDye(msg->request); - - invoke(msg, bCoro); -} - -void ServantProxy::tars_invoke(char cPacketType, - const string& sFuncName, - const vector& buf, - const map& context, - const map& status, - ResponsePacket& rsp) -{ - ReqMessage * msg = new ReqMessage(); - - msg->init(ReqMessage::SYNC_CALL,NULL,sFuncName); - - msg->request.iVersion = TARSVERSION; - msg->request.cPacketType = cPacketType; - - msg->request.sServantName = (*_objectProxy)->name(); - msg->request.sFuncName = sFuncName; - msg->request.sBuffer = buf; - msg->request.context = context; - msg->request.status = status; - msg->request.iTimeout = _syncTimeout; - - // 在RequestPacket中的contextè®¾ç½®ä¸»è°ƒä¿¡æ¯ - if(_masterFlag) - { - msg->request.context.insert(std::make_pair(TARS_MASTER_KEY,ClientConfig::ModuleName)); - } - - - checkDye(msg->request); - - invoke(msg); - - rsp = msg->response; - - delete msg; - msg = NULL; - -} -////////////////////////////////////////////////////////////////////////////// -//æœåŠ¡ç«¯æ˜¯éžtarså议,通过rpc_call调用 -void ServantProxy::rpc_call(uint32_t iRequestId, - const string& sFuncName, - const char* buff, - uint32_t len, - ResponsePacket& rsp) -{ - ReqMessage * msg = new ReqMessage(); - - msg->init(ReqMessage::SYNC_CALL,NULL,sFuncName); - msg->bFromRpc = true; - - msg->request.iRequestId = iRequestId; - msg->request.sFuncName = sFuncName; - msg->request.sBuffer.assign(buff, buff + len); - - invoke(msg); - - rsp = msg->response; - - delete msg; - msg = NULL; -} - -void ServantProxy::rpc_call_async(uint32_t iRequestId, - const string& sFuncName, - const char* buff, - uint32_t len, - const ServantProxyCallbackPtr& callback, - bool bCoro) -{ - ReqMessage * msg = new ReqMessage(); - - msg->init(callback?ReqMessage::ASYNC_CALL:ReqMessage::ONE_WAY,NULL,sFuncName); - - msg->bFromRpc = true; - msg->callback = callback; - - msg->request.iRequestId = iRequestId; - msg->request.sFuncName = sFuncName; - msg->request.sBuffer.assign(buff, buff + len); - - invoke(msg, bCoro); -} - -void ServantProxy::http_call(const std::string& method, - const std::string& uri, - const std::map& headers, - const std::string& body, - std::map& rheaders, - std::string& rbody) -{ - ReqMessage* msg = new ReqMessage(); - - msg->init(ReqMessage::SYNC_CALL, NULL, ""); - - msg->bFromRpc = true; - msg->request.sServantName = uri; - msg->request.sFuncName = method; - // 使用下é¢ä¸¤ä¸ªå­—段ä¿å­˜å¤´éƒ¨å’ŒåŒ…体 - msg->request.context = headers; - msg->request.sBuffer.assign(body.begin(), body.end()); - - invoke(msg); - - rheaders.swap(msg->response.status); - rbody.assign(msg->response.sBuffer.begin(), msg->response.sBuffer.end()); - - delete msg; - msg = NULL; -} - -void ServantProxy::http_call_async(const std::map& headers, - const std::string& body, - HttpCallback* cb) -{ - ReqMessage * msg = new ReqMessage(); - - msg->init(ReqMessage::ASYNC_CALL, NULL, ""); - - msg->bFromRpc = true; - // 使用下é¢ä¸¤ä¸ªå­—段ä¿å­˜å¤´éƒ¨å’ŒåŒ…体 - msg->request.context = headers; - msg->request.sBuffer.assign(body.begin(), body.end()); - - ServantProxyCallbackPtr callback = new HttpServantProxyCallback(cb); - msg->callback = callback; - - invoke(msg); -} - -//选å–ä¸€ä¸ªç½‘ç»œçº¿ç¨‹å¯¹åº”çš„ä¿¡æ¯ -void ServantProxy::selectNetThreadInfo(ServantProxyThreadData * pSptd, ObjectProxy * & pObjProxy, ReqInfoQueue * & pReqQ) -{ - //指针为空 å°±new一个 - if(!pSptd->_queueInit) - { - for(size_t i=0;i<_objectProxyNum;++i) - { - pSptd->_reqQueue[i] = new ReqInfoQueue(_queueSize); - } - pSptd->_objectProxyNum = _objectProxyNum; - pSptd->_objectProxy = _objectProxy; - pSptd->_queueInit = true; - } - - if(_objectProxyNum == 1) - { - pObjProxy = *_objectProxy; - pReqQ = pSptd->_reqQueue[0]; - } - else - { - if(pSptd->_netThreadSeq >= 0) - { - //网络线程å‘起的请求 - assert(pSptd->_netThreadSeq < static_cast(_objectProxyNum)); - - pObjProxy = *(_objectProxy + pSptd->_netThreadSeq); - pReqQ = pSptd->_reqQueue[pSptd->_netThreadSeq]; - } - else - { - //用线程的ç§æœ‰æ•°æ®æ¥ä¿å­˜é€‰åˆ°çš„seq - pObjProxy = *(_objectProxy + pSptd->_netSeq); - pReqQ = pSptd->_reqQueue[pSptd->_netSeq]; - pSptd->_netSeq++; - - if(pSptd->_netSeq == _objectProxyNum) - pSptd->_netSeq = 0; - } - } -} - -void ServantProxy::checkDye(RequestPacket& req) -{ - //线程ç§æœ‰æ•°æ® - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - assert(pSptd != NULL); - if(pSptd && pSptd->_dyeing) - { - SET_MSG_TYPE(req.iMessageType, tars::TARSMESSAGETYPEDYED); - - req.status[ServantProxy::STATUS_DYED_KEY] = pSptd->_dyeingKey; - } -} - - -void ServantProxy::tars_endpoints(vector &activeEndPoint, vector &inactiveEndPoint) -{ - _endpointInfo->getEndpoint(activeEndPoint,inactiveEndPoint); -} - -void ServantProxy::tars_endpointsAll(vector &activeEndPoint, vector &inactiveEndPoint) -{ - _endpointInfo->getEndpointByAll(activeEndPoint,inactiveEndPoint); -} - -void ServantProxy::tars_endpointsBySet(const string & sName,vector &activeEndPoint, vector &inactiveEndPoint) -{ - _endpointInfo->getEndpointBySet(sName,activeEndPoint,inactiveEndPoint); -} - -void ServantProxy::tars_endpointsByStation(const string & sName,vector &activeEndPoint, vector &inactiveEndPoint) -{ - _endpointInfo->getEndpointByStation(sName,activeEndPoint,inactiveEndPoint); -} - -vector ServantProxy::tars_endpoints(const std::string & sStation) -{ - vector activeEndPoint; - vector inactiveEndPoint; - _endpointInfo->getTCEndpoint(activeEndPoint,inactiveEndPoint); - return activeEndPoint; -} - -void ServantProxy::tars_endpoints(const std::string & sStation, vector & vecActive, vector & vecInactive) -{ - _endpointInfo->getTCEndpointByStation(sStation,vecActive,vecInactive); -} - -vector ServantProxy::getEndpoint() -{ - vector activeEndPoint; - vector inactiveEndPoint; - _endpointInfo->getTCEndpoint(activeEndPoint,inactiveEndPoint); - return activeEndPoint; -} - -vector ServantProxy::getEndpoint4All() -{ - vector activeEndPoint; - vector inactiveEndPoint; - _endpointInfo->getTCEndpointByAll(activeEndPoint,inactiveEndPoint); - return activeEndPoint; -} - - -////////////////////////////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/ServantProxyFactory.cpp b/cpp/servant/libservant/ServantProxyFactory.cpp deleted file mode 100644 index 91a0c1616..000000000 --- a/cpp/servant/libservant/ServantProxyFactory.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/ServantProxyFactory.h" -#include "servant/TarsLogger.h" - -namespace tars -{ - -ServantProxyFactory::ServantProxyFactory(Communicator* cm) -: _comm(cm) -{ -} - -ServantProxyFactory::~ServantProxyFactory() -{ -} - -ServantPrx::element_type* ServantProxyFactory::getServantProxy(const string& name,const string& setName) -{ - TC_LockT lock(*this); - - string tmpObjName = name + ":" + setName; - - map::iterator it = _servantProxy.find(tmpObjName); - if(it != _servantProxy.end()) - { - return it->second.get(); - } - - ObjectProxy ** ppObjectProxy = new ObjectProxy * [_comm->getClientThreadNum()]; - assert(ppObjectProxy != NULL); - - for(size_t i = 0; i < _comm->getClientThreadNum(); ++i) - { - ppObjectProxy[i] = _comm->getCommunicatorEpoll(i)->getObjectProxy(name, setName); - } - - ServantPrx sp = new ServantProxy(_comm, ppObjectProxy, _comm->getClientThreadNum()); - - int syncTimeout = TC_Common::strto(_comm->getProperty("sync-invoke-timeout", "3000")); - int asyncTimeout = TC_Common::strto(_comm->getProperty("async-invoke-timeout", "5000")); - int conTimeout = TC_Common::strto(_comm->getProperty("connect-timeout", "1500")); - - sp->tars_timeout(syncTimeout); - sp->tars_async_timeout(asyncTimeout); - sp->tars_connect_timeout(conTimeout); - - _servantProxy[tmpObjName] = sp; - - return sp.get(); -} -/////////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/StatReport.cpp b/cpp/servant/libservant/StatReport.cpp deleted file mode 100644 index f550bd0ca..000000000 --- a/cpp/servant/libservant/StatReport.cpp +++ /dev/null @@ -1,811 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/StatReport.h" -#include "util/tc_common.h" -#include "util/tc_timeprovider.h" -#include "servant/TarsLogger.h" -#include "servant/Communicator.h" -#include - -namespace tars -{ -////////////////////////////////////////////////////////////////// -// -StatReport::StatReport(size_t iEpollNum) -: _time(0) -, _reportInterval(60000) -, _reportTimeout(5000) -, _maxReportSize(MAX_REPORT_SIZE) -, _terminate(false) -, _sampleRate(1) -, _maxSampleCount(500) -, _epollNum(iEpollNum) -, _retValueNumLimit(10) -{ - for(size_t i = 0 ; i < _epollNum; i++) - { - _statMsg.push_back(new stat_queue(MAX_STAT_QUEUE_SIZE)); - } -} - -StatReport::~StatReport() -{ - if (isAlive()) - { - terminate(); - - getThreadControl().join(); - } -} - -void StatReport::terminate() -{ - Lock lock(*this); - - _terminate = true; - - notifyAll(); -} - -void StatReport::report(size_t iSeq,MapStatMicMsg * pmStatMicMsg) -{ - assert(iSeq < _epollNum); - bool bFlag = _statMsg[iSeq]->push_back(pmStatMicMsg); - if(!bFlag) - { - delete pmStatMicMsg; - pmStatMicMsg = NULL; - - TLOGERROR("[TARS][StatReport::report] queue full." << endl); - } -} - -void StatReport::setReportInfo(const StatFPrx& statPrx, - const PropertyFPrx& propertyPrx, - const string& strModuleName, - const string& strModuleIp, - const string& strSetDivision, - int iReportInterval, - int iSampleRate, - unsigned int iMaxSampleCount, - int iMaxReportSize, - int iReportTimeout) -{ - Lock lock(*this); - - _statPrx = statPrx; - - _propertyPrx = propertyPrx; - - //包头信æ¯,trim&substr 防止超长导致udp包å‘é€å¤±è´¥ - _moduleName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN); - - _ip = trimAndLimitStr(strModuleIp, MAX_MASTER_IP_LEN); - - _time = TNOW; - - _reportInterval = iReportInterval < 10000 ? 10000 : iReportInterval; - - _reportTimeout = iReportTimeout < 5000 ? 5000 : iReportTimeout; - - _sampleRate = (iSampleRate < 1)?1: iSampleRate; - - _maxSampleCount = iMaxSampleCount>500?500:iMaxSampleCount; - - if ( iMaxReportSize < MIN_REPORT_SIZE || iMaxReportSize > MAX_REPORT_SIZE ) - { - _maxReportSize = MAX_REPORT_SIZE; - } - else - { - _maxReportSize = iMaxReportSize; - } - vector vtSetInfo = TC_Common::sepstr(strSetDivision,"."); - if (vtSetInfo.size()!=3 ||(vtSetInfo.size()==3&&(vtSetInfo[0]=="*"||vtSetInfo[1]=="*"))) - { - _setArea= ""; - _setID = ""; - } - else - { - _setName = vtSetInfo[0]; - _setArea = vtSetInfo[1]; - _setID = vtSetInfo[2]; - } - //TLOGDEBUG("setReportInfo Division:" << strSetDivision << " " << _setName << " " << _setArea << " " << _setID << endl); - resetStatInterv(); - - if (!isAlive()) - { - start(); - } -} - -void StatReport::addStatInterv(int iInterv) -{ - Lock lock(*this); - - _timePoint.push_back(iInterv); - - sort(_timePoint.begin(),_timePoint.end()); - - unique(_timePoint.begin(),_timePoint.end()); -} - -void StatReport::getIntervCount(int time,StatMicMsgBody& body) -{ - int iTimePoint = 0; - bool bNeedInit = false; - bool bGetIntev = false; - if(body.intervalCount.size() == 0) //第一次需è¦å°†æ‰€æœ‰æ点值åˆå§‹åŒ–为0 - { - bNeedInit = true; - } - for(int i =0;i<(int)_timePoint.size();i++) - { - iTimePoint = _timePoint[i]; - if(bGetIntev == false && time < iTimePoint) - { - bGetIntev = true; - body.intervalCount[iTimePoint]++; - if(bNeedInit == false) - break; - else - continue; - } - if(bNeedInit == true) - { - body.intervalCount[iTimePoint] = 0; - } - } - return; -} - -void StatReport::resetStatInterv() -{ - _timePoint.clear(); - _timePoint.push_back(5); - _timePoint.push_back(10); - _timePoint.push_back(50); - _timePoint.push_back(100); - _timePoint.push_back(200); - _timePoint.push_back(500); - _timePoint.push_back(1000); - _timePoint.push_back(2000); - _timePoint.push_back(3000); - - sort(_timePoint.begin(),_timePoint.end()); - - unique(_timePoint.begin(),_timePoint.end()); -} - -string StatReport::trimAndLimitStr(const string& str, uint32_t limitlen) -{ - static const string strTime = "\r\t"; - - string ret = TC_Common::trim(str, strTime); - - if (ret.length() > limitlen) - { - ret.resize(limitlen); - } - return ret; -} - -bool StatReport::divison2SetInfo(const string& str, vector& vtSetInfo) -{ - vtSetInfo = TC_Common::sepstr(str,"."); - - if (vtSetInfo.size() != 3 ||(vtSetInfo.size()==3&&(vtSetInfo[0]=="*"||vtSetInfo[1]=="*"))) - { - TLOGERROR(__FUNCTION__ << ":" << __LINE__ << "|bad set name [" << str << endl); - return false; - } - return true; -} - -/* -tars.tarsstat to tarsstat -*/ -string StatReport::getServerName(string sModuleName) -{ - string::size_type pos = sModuleName.find("."); - if(pos != string::npos) - { - return sModuleName.substr(pos + 1); //+1:过滤. - } - return sModuleName; -} - -void StatReport::report(const string& strModuleName, - const string& setdivision, - const string& strInterfaceName, - const string& strModuleIp, - uint16_t iPort, - StatResult eResult, - int iSptime, - int iReturnValue, - bool bFromClient) -{ - //包头信æ¯,trim&substr 防止超长导致udp包å‘é€å¤±è´¥ - //masterIp为空æœåŠ¡ç«¯è‡ªå·±èŽ·å–。 - - StatMicMsgHead head; - StatMicMsgBody body; - string sMaterServerName = ""; - string sSlaveServerName = ""; - string appName = "";// ç”±setdivisionç”Ÿæˆ - - if(bFromClient) - { - if (!_setName.empty()) - { - head.masterName = _moduleName + "." + _setName + _setArea + _setID + "@" + ClientConfig::TarsVersion; - } - else - { - head.masterName = _moduleName + "@" + ClientConfig::TarsVersion; - } - - if (!setdivision.empty()) //被调没有å¯ç”¨set分组,slavenameä¿æŒåŽŸæ · - { - vector vtSetInfo; - if(divison2SetInfo(setdivision, vtSetInfo)) - { - head.slaveSetName = vtSetInfo[0]; - head.slaveSetArea = vtSetInfo[1]; - head.slaveSetID = vtSetInfo[2]; - } - head.slaveName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN) + "." + head.slaveSetName + head.slaveSetArea + head.slaveSetID; - } - else - { - head.slaveName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN); - } - - head.masterIp = ""; - head.slaveIp = trimAndLimitStr(strModuleIp, MAX_MASTER_IP_LEN); - - } - else - { - //被调上报,masterName没有setä¿¡æ¯ - head.masterName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN); - - head.masterIp = trimAndLimitStr(strModuleIp, MAX_MASTER_IP_LEN); - - - if(_setName.empty()) //被调上报,slaveçš„setä¿¡æ¯ä¸ºç©º - { - head.slaveName = _moduleName;//æœåŠ¡ç«¯versionä¸éœ€è¦ä¸ŠæŠ¥ - } - else - { - head.slaveName = _moduleName + "." + _setName + _setArea + _setID; - } - head.slaveIp = ""; - - head.slaveSetName = _setName; - - head.slaveSetArea = _setArea; - - head.slaveSetID = _setID; - } - - head.interfaceName = trimAndLimitStr(strInterfaceName, MAX_MASTER_NAME_LEN); - head.slavePort = iPort; - head.returnValue = iReturnValue; - - //包体信æ¯. - if(eResult == STAT_SUCC) - { - body.count = 1; - - body.totalRspTime = body.minRspTime = body.maxRspTime = iSptime; - } - else if(eResult == STAT_TIMEOUT) - { - body.timeoutCount = 1; - } - else - { - body.execCount = 1; - } - submit(head, body, bFromClient); -} - -void StatReport::report(const string& strMasterName, - const string& strMasterIp, - const string& strSlaveName, - const string& strSlaveIp, - uint16_t iSlavePort, - const string& strInterfaceName, - StatResult eResult, - int iSptime, - int iReturnValue) -{ - //包头信æ¯,trim&substr 防止超长导致udp包å‘é€å¤±è´¥ - //masterIp为空æœåŠ¡ç«¯è‡ªå·±èŽ·å–。 - - StatMicMsgHead head; - StatMicMsgBody body; - - head.masterName = trimAndLimitStr(strMasterName + "@" + ClientConfig::TarsVersion, MAX_MASTER_NAME_LEN); - head.masterIp = trimAndLimitStr(strMasterIp, MAX_MASTER_IP_LEN); - head.slaveName = trimAndLimitStr(strSlaveName, MAX_MASTER_NAME_LEN); - head.slaveIp = trimAndLimitStr(strSlaveIp, MAX_MASTER_IP_LEN); - head.interfaceName = trimAndLimitStr(strInterfaceName, MAX_MASTER_NAME_LEN); - head.slavePort = iSlavePort; - head.returnValue = iReturnValue; - - //包体信æ¯. - if(eResult == STAT_SUCC) - { - body.count = 1; - - body.totalRspTime = body.minRspTime = body.maxRspTime = iSptime; - } - else if(eResult == STAT_TIMEOUT) - { - body.timeoutCount = 1; - } - else - { - body.execCount = 1; - } - - submit(head, body, true); -} - -string StatReport::sampleUnid() -{ - char s[14] = {0}; - time_t t = TNOW; - int ip = inet_addr(_ip.c_str()); - int thread = syscall(SYS_gettid); - static unsigned short n = 0; - ++n; - memcpy( s, &ip, 4 ); - memcpy( s + 4, &t, 4); - memcpy( s + 8, &thread, 4); - memcpy( s + 12, &n, 2 ); - return TC_Common::bin2str(string(s,14)); -} - -void StatReport::submit( StatMicMsgHead& head, StatMicMsgBody& body,bool bFromClient ) -{ - Lock lock(*this); - - MapStatMicMsg& msg = (bFromClient == true)?_statMicMsgClient:_statMicMsgServer; - MapStatMicMsg::iterator it = msg.find( head ); - if ( it != msg.end() ) - { - StatMicMsgBody& stBody = it->second; - stBody.count += body.count; - stBody.timeoutCount += body.timeoutCount; - stBody.execCount += body.execCount; - stBody.totalRspTime += body.totalRspTime; - if ( stBody.maxRspTime < body.maxRspTime ) - { - stBody.maxRspTime = body.maxRspTime; - } - //éž0最å°å€¼ - if ( stBody.minRspTime == 0 ||(stBody.minRspTime > body.minRspTime && body.minRspTime != 0)) - { - stBody.minRspTime = body.minRspTime; - } - getIntervCount(body.maxRspTime, stBody); - } - else - { - getIntervCount(body.maxRspTime, body); - msg[head] = body; - } -} - -void StatReport::doSample(const string& strSlaveName, - const string& strInterfaceName, - const string& strSlaveIp, - map &status) -{ -} - -int StatReport::reportMicMsg(MapStatMicMsg& msg,bool bFromClient) -{ - if(msg.empty()) - return 0; - try - { - int iLen = 0; - MapStatMicMsg mTemp; - MapStatMicMsg mStatMsg; - mStatMsg.clear(); - mTemp.clear(); - { - Lock lock(*this); - msg.swap(mStatMsg); - } - - TLOGINFO("[TARS][StatReport::reportMicMsg get size:" << mStatMsg.size()<<"]"<< endl); - for(MapStatMicMsg::iterator it = mStatMsg.begin(); it != mStatMsg.end(); it++) - { - const StatMicMsgHead &head = it->first; - int iTemLen = STAT_PROTOCOL_LEN +head.masterName.length() + head.slaveName.length() + head.interfaceName.length() - + head.slaveSetName.length() + head.slaveSetArea.length() + head.slaveSetID.length(); - iLen = iLen + iTemLen; - if(iLen > _maxReportSize) //ä¸èƒ½è¶…过udp 1472 - { - if(_statPrx) - { - TLOGINFO("[TARS][StatReport::reportMicMsg send size:" << mTemp.size()<<"]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient); - } - iLen = iTemLen; - mTemp.clear(); - } - - mTemp[head] = it->second; - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - ostringstream os; - os.str(""); - head.displaySimple(os); - os << " "; - mTemp[head].displaySimple(os); - TLOGINFO("[TARS][StatReport::reportMicMsg display:" << os.str() << endl); - } - } - if(0 != (int)mTemp.size()) - { - if(_statPrx) - { - TLOGINFO("[TARS][StatReport::reportMicMsg send size:" << mTemp.size()<<"]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient); - } - } - return 0; - } - catch ( exception& e ) - { - TLOGERROR("StatReport::report catch exception:" << e.what() << endl); - } - catch ( ... ) - { - TLOGERROR("StatReport::report catch unkown exception" << endl); - } - return -1; -} - -int StatReport::reportPropMsg() -{ - try - { - MapStatPropMsg mStatMsg; - - { - Lock lock(*this); - for(map::iterator it = _statPropMsg.begin(); it != _statPropMsg.end(); ++it) - { - StatPropMsgHead head; - StatPropMsgBody body; - - if (!it->second->getMasterName().empty()) - { - if (!_setName.empty()) - { - head.moduleName = it->second->getMasterName() + "." + _setName + _setArea + _setID; - } - else - { - head.moduleName = it->second->getMasterName(); - } - } - else - { - if (!_setName.empty()) - { - head.moduleName = _moduleName + "." + _setName + _setArea + _setID; - } - else - { - head.moduleName = _moduleName; - } - } - - head.ip = ""; - head.propertyName = it->first; - head.setName = _setName; - head.setArea = _setArea; - head.setID = _setID; - head.iPropertyVer = 2; - - vector > v = it->second->get(); - for(size_t i = 0; i < v.size(); i++) - { - bool bFlag = false; - if(v[i].first == "Sum") - { - if(v[i].second != "0") - bFlag = true; - } - else if(v[i].first == "Avg") - { - if(v[i].second != "0") - bFlag = true; - } - else if(v[i].first == "Distr") - { - if(v[i].second != "") - bFlag = true; - } - else if(v[i].first == "Max") - { - if(v[i].second != "-9999999") - bFlag = true; - } - else if(v[i].first == "Min") - { - if(v[i].second != "0") - bFlag = true; - } - else if(v[i].first == "Count") - { - if(v[i].second != "0") - bFlag = true; - } - else - { - bFlag = true; - } - - if(bFlag) - { - StatPropInfo sp; - sp.policy = v[i].first; - sp.value = v[i].second; - body.vInfo.push_back(sp); - } - } - mStatMsg[head] = body; - if(LOG->IsNeedLog(TarsRollLogger::INFO_LOG)) - { - ostringstream os; - os.str(""); - head.displaySimple(os); - os << " "; - mStatMsg[head].displaySimple(os); - TLOGINFO("[TARS][StatReport::reportPropMsg display:" << os.str() << endl); - } - } - } - - TLOGINFO("[TARS][StatReport::reportPropMsg get size:" << mStatMsg.size()<<"]"<< endl); - int iLen = 0; - MapStatPropMsg mTemp; - for(MapStatPropMsg::iterator it = mStatMsg.begin(); it != mStatMsg.end(); it++) - { - const StatPropMsgHead &head = it->first; - const StatPropMsgBody &body = it->second; - int iTemLen = head.moduleName.length()+ head.ip.length() + head.propertyName.length() + head.setName.length() + head.setArea.length() + head.setID.length(); - for(size_t i = 0; i < body.vInfo.size(); i++) - { - iTemLen+=body.vInfo[i].policy.length(); - iTemLen+=body.vInfo[i].value.length(); - } - iTemLen = PROPERTY_PROTOCOL_LEN + body.vInfo.size(); // - iLen = iLen + iTemLen; - if(iLen > _maxReportSize) //ä¸èƒ½è¶…过udp 1472 - { - if(_propertyPrx) - { - TLOGINFO("[TARS][StatReport::reportPropMsg send size:" << mTemp.size()<<"]"<< endl); - _propertyPrx->tars_set_timeout(_reportTimeout)->async_reportPropMsg(NULL,mTemp); - } - iLen = iTemLen; - mTemp.clear(); - } - mTemp[it->first] = it->second; - } - if(0 != (int)mTemp.size()) - { - if(_propertyPrx) - { - TLOGINFO("[TARS][StatReport::reportPropMsg send size:" << mTemp.size()<< "]"<< endl); - _propertyPrx->tars_set_timeout(_reportTimeout)->async_reportPropMsg(NULL,mTemp); - } - } - return 0; - } - catch ( exception& e ) - { - TLOGERROR("StatReport::reportPropMsg catch exception:" << e.what() << endl); - } - catch ( ... ) - { - TLOGERROR("StatReport::reportPropMsg catch unkown exception" << endl); - } - return -1; -} - -int StatReport::reportSampleMsg() -{ - try - { - MMapStatSampleMsg mmStatSampleMsg; - { - Lock lock(*this); - _statSampleMsg.swap(mmStatSampleMsg); - } - - TLOGINFO("[TARS][StatReport::reportSampleMsg get size:" << mmStatSampleMsg.size()<<"]"<< endl); - - int iLen = 0; - vector vTemp; - for(MMapStatSampleMsg::const_iterator it = mmStatSampleMsg.begin() ;it != mmStatSampleMsg.end();++it) - { - StatSampleMsg sample = it->second; - int iTemLen = STAT_PROTOCOL_LEN +sample.masterName.length() + sample.slaveName.length() + sample.interfaceName.length(); - iLen = iLen + iTemLen; - if(iLen > _maxReportSize) //ä¸èƒ½è¶…过udp 1472 - { - if(_statPrx) - { - TLOGINFO("[TARS][StatReport::reportSampleMsg send size:" << vTemp.size()<< "]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportSampleMsg(NULL,vTemp); - } - iLen = iTemLen; - vTemp.clear(); - } - vTemp.push_back(sample); - } - if(0 != (int)vTemp.size()) - { - if(_statPrx) - { - TLOGINFO("[TARS][StatReport::reportSampleMsg send size:" << vTemp.size()<< "]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportSampleMsg(NULL,vTemp); - } - } - - return 0; - } - catch ( exception& e ) - { - TLOGERROR("StatReport::reportSampleMsg catch exception:" << e.what() << endl); - } - catch ( ... ) - { - TLOGERROR("StatReport::reportSampleMsg catch unkown exception" << endl); - } - return -1; -} - -void StatReport::addMicMsg(MapStatMicMsg & old,MapStatMicMsg & add) -{ - MapStatMicMsg::iterator iter; - MapStatMicMsg::iterator iterOld; - iter = add.begin(); - for(;iter != add.end();++iter) - { - iterOld = old.find(iter->first); - if(iterOld == old.end()) - { - //直接insert - old.insert(make_pair(iter->first,iter->second)); - } - else - { - //åˆå¹¶ - iterOld->second.count += iter->second.count; - iterOld->second.timeoutCount += iter->second.timeoutCount; - iterOld->second.execCount += iter->second.execCount; - - map::iterator iterOldInt,iterInt; - map & mCount = iter->second.intervalCount; - map & mCountOld = iterOld->second.intervalCount; - iterInt = mCount.begin(); - for(;iterInt != mCount.end();++iterInt) - { - iterOldInt = mCountOld.find(iterInt->first); - if(iterOldInt == mCountOld.end()) - { - mCountOld.insert(make_pair(iterInt->first,iterInt->second)); - } - else - { - iterOldInt->second += iterInt->second; - } - } - - iterOld->second.totalRspTime += iter->second.totalRspTime; - - if(iterOld->second.maxRspTime < iter->second.maxRspTime) - iterOld->second.maxRspTime = iter->second.maxRspTime; - - if(iterOld->second.minRspTime > iter->second.minRspTime) - iterOld->second.minRspTime = iter->second.minRspTime; - } - } -} - -void StatReport::run() -{ - while(!_terminate) - { - try - { - time_t tNow = TNOW; - - if(tNow - _time > _reportInterval/1000) - { - reportMicMsg(_statMicMsgClient, true); - - reportMicMsg(_statMicMsgServer, false); - - MapStatMicMsg mStatMsg; - - for(size_t i = 0; i < _epollNum; ++i) - { - MapStatMicMsg * pStatMsg; - while(_statMsg[i]->pop_front(pStatMsg)) - { - addMicMsg(mStatMsg,*pStatMsg); - delete pStatMsg; - } - } -#if 0 - - ostringstream os; - MapStatMicMsg::iterator iter; - iter = mStatMsg.begin(); - for(;iter != mStatMsg.end();++iter) - { - iter->first.display(os); - os<second.display(os); - os< - -namespace tars -{ - -int TarsRemoteConfig::setConfigInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string& basePath,const string& setdivision, int maxBakNum) -{ - _comm = comm; - if(!obj.empty()) - { - _configPrx = _comm->stringToProxy(obj); - } - _app = app; - _serverName = serverName; - _basePath = basePath; - _maxBakNum = maxBakNum; - _setdivision = setdivision; - return 0; -} - -bool TarsRemoteConfig::addConfig(const string & sFileName, string &buffer, bool bAppConfigOnly) -{ - TC_LockT lock(_mutex); - - try - { - string sFullFileName = _basePath + "/" + sFileName; - - string newFile = getRemoteFile(sFileName, bAppConfigOnly); - - if (newFile.empty() || access(newFile.c_str(), R_OK) != 0)//拉å–ä¸åˆ°é…置中心的é…置文件 - { - if(access(sFullFileName.c_str(), R_OK) == 0) //获å–本地é…ç½®æˆåŠŸï¼Œè¿”回æˆåŠŸï¼Œä½†éœ€è¦å‘Šè­¦ä¸€ä¸‹ã€‚ - { - buffer = "[fail] get remote config:" + sFileName + "fail,use the local config."; - - return true; - } - throw runtime_error("access file error:" + newFile); - } - - if (TC_File::load2str(newFile) != TC_File::load2str(sFullFileName)) - { - for (int i = _maxBakNum - 1; i >= 1; --i) - { - if (access(index2file(sFullFileName, i).c_str(), F_OK) == 0) - { - localRename(index2file(sFullFileName, i), index2file(sFullFileName, i+1)); - } - } - - if (access(sFullFileName.c_str(), F_OK) == 0) - { - localRename(sFullFileName, index2file(sFullFileName, 1)); - } - } - - localRename(newFile, sFullFileName); - - assert(!access(sFullFileName.c_str(), R_OK)); - - buffer = "[succ] get remote config:" + sFileName; - - return true; - } - catch (std::exception& e) - { - buffer = "[fail] get remote config '" + sFileName + "' error:" + string(e.what()); - } - catch (...) - { - buffer = "[fail] get remote config '" + sFileName + "' unknown error"; - } - - return false; -} - -string TarsRemoteConfig::getRemoteFile(const string &sFileName, bool bAppConfigOnly) -{ - if (_configPrx) - { - string stream; - int ret = -1; - for(int i = 0; i < 2;i++) - { - try - { - if(_setdivision.empty()) - { - ret = _configPrx->loadConfig(_app, (bAppConfigOnly ? "" : _serverName), sFileName, stream); - } - else - { - struct ConfigInfo confInfo; - confInfo.appname = _app; - confInfo.servername = (bAppConfigOnly ? "" : _serverName); - confInfo.filename = sFileName; - confInfo.bAppOnly = bAppConfigOnly; - confInfo.setdivision = _setdivision; - ret = _configPrx->loadConfigByInfo(confInfo,stream); - } - - break; - }catch(std::exception& e){ - // - }catch (...){ - // - } - } - - if (ret != 0 || stream.empty()) - { - throw runtime_error("remote config file is empty:" + sFileName); - } - - - string newFile = _basePath + "/" + sFileName + "." + TC_Common::tostr(time(NULL)); - - std::ofstream out(newFile.c_str()); - - string result; - if (out) - { - out << stream;//如果硬盘满了,是å¦èƒ½å†™å…¥æˆåŠŸéœ€è¦è¿›è¡Œåˆ¤æ–­ã€‚ - out.flush(); - if(out.bad()) - { - out.close(); - result = "[fail] copy stream to disk error." ; - TarsRemoteNotify::getInstance()->report(result); - return ""; - } - else - { - out.close(); - return newFile; - } - } - } - return ""; -} - -string TarsRemoteConfig::index2file(const string & sFullFileName, int index) -{ - return sFullFileName + "." + TC_Common::tostr(index) + ".bak"; -} - -void TarsRemoteConfig::localRename(const string& oldFile, const string& newFile) -{ - if (::rename(oldFile.c_str(), newFile.c_str()) != 0) - { - throw runtime_error("rename file error:" + oldFile + "->" + newFile); - } -} - -string TarsRemoteConfig::recoverSysConfig(const string & sFullFileName) -{ - try - { - for (int i = 1; i <= _maxBakNum; ++i) - { - if (access(index2file(sFullFileName,i).c_str(), R_OK) == 0) - { - localRename(index2file(sFullFileName, i), sFullFileName); - - return "[succ] recover file:" + index2file(sFullFileName, i); - } - } - } - catch (std::exception& e) - { - return "[fail] recover config error:" + string(e.what()); - } - catch (...) - { - return "[fail] recover config error"; - } - - return "[fail] no backup file."; -} - -} - - diff --git a/cpp/servant/libservant/TarsCurrent.cpp b/cpp/servant/libservant/TarsCurrent.cpp deleted file mode 100644 index ed9dd2e6c..000000000 --- a/cpp/servant/libservant/TarsCurrent.cpp +++ /dev/null @@ -1,341 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/TarsCurrent.h" -#include "servant/ServantHandle.h" -#include "servant/BaseF.h" -#include "servant/Application.h" -#include "tup/tup.h" -#include - -namespace tars -{ -////////////////////////////////////////////////////////////////// -TarsCurrent::TarsCurrent(ServantHandle *pServantHandle) -: _servantHandle(pServantHandle) -, _bindAdapter(NULL) -, _uid(0) -, _ip("NULL") -, _port(0) -, _fd(-1) -, _response(true) -, _begintime(0) -, _ret(0) -, _reportStat(true) -, _closeType(-1) -{ -} - -TarsCurrent::~TarsCurrent() -{ - //TUP调用或å•å‘调用,从æœåŠ¡ç«¯ä¸ŠæŠ¥è°ƒç”¨ä¿¡æ¯ - if(_reportStat) - { - if(_request.iVersion == TUPVERSION ) - { - reportToStat("tup_client"); - } - else if(_request.cPacketType == TARSONEWAY) - { - reportToStat("one_way_client"); - } - else if(!_bindAdapter->isTarsProtocol() && ServerConfig::ReportFlow) - { - //éžtars客户端 从æœåŠ¡ç«¯ä¸ŠæŠ¥è°ƒç”¨ä¿¡æ¯ - reportToStat("not_tars_client"); - } - } -} - -string TarsCurrent::getIp() const -{ - return _ip; -} - -int TarsCurrent::getPort() const -{ - return _port; -} - -uint32_t TarsCurrent::getUId() const -{ - return _uid; -} - -string TarsCurrent::getServantName() const -{ - return _request.sServantName; -} - -short TarsCurrent::getRequestVersion() const -{ - return _request.iVersion; -} - -map& TarsCurrent::getContext() -{ - return _request.context; -} - -const map& TarsCurrent::getRequestStatus() const -{ - return _request.status; -} - -string TarsCurrent::getFuncName() const -{ - return _request.sFuncName; -} - -uint32_t TarsCurrent::getRequestId() const -{ - return _request.iRequestId; -} - -char TarsCurrent::getPacketType() const -{ - return _request.cPacketType; -} - -tars::Int32 TarsCurrent::getMessageType() const -{ - return _request.iMessageType; -} - -struct timeval TarsCurrent::getRecvTime() const -{ - timeval tm; - tm.tv_sec = _begintime/1000; - tm.tv_usec = (_begintime%1000)*1000; - - return tm; -} - -void TarsCurrent::setReportStat(bool bReport) -{ - _reportStat = bReport; -} - -const vector& TarsCurrent::getRequestBuffer() const -{ - return _request.sBuffer; -} - -bool TarsCurrent::isResponse() const -{ - return _response; -} - -void TarsCurrent::setCloseType(int type) -{ - _closeType = type; -} - -int TarsCurrent::getCloseType() const -{ - return _closeType; -} - - -void TarsCurrent::initialize(const TC_EpollServer::tagRecvData &stRecvData) -{ - int64_t begintime = TNOWMS; - - initialize(stRecvData, begintime); -} - -void TarsCurrent::initialize(const TC_EpollServer::tagRecvData &stRecvData, int64_t beginTime) -{ - _ip = stRecvData.ip; - - _port = stRecvData.port; - - _uid = stRecvData.uid; - - _fd = stRecvData.fd; - - _bindAdapter = stRecvData.adapter.get(); - - _begintime = beginTime; - - _request.sServantName = ServantHelperManager::getInstance()->getAdapterServant(stRecvData.adapter->getName()); - - if (_bindAdapter->isTarsProtocol()) - { - initialize(stRecvData.buffer); - } - else - { - _request.sBuffer.reserve(stRecvData.buffer.length()); - - _request.sBuffer.resize(stRecvData.buffer.length()); - - ::memcpy(&_request.sBuffer[0], stRecvData.buffer.c_str(), stRecvData.buffer.length()); - } -} - -void TarsCurrent::initializeClose(const TC_EpollServer::tagRecvData &stRecvData) -{ - _ip = stRecvData.ip; - - _port = stRecvData.port; - - _uid = stRecvData.uid; - - _fd = stRecvData.fd; - - _bindAdapter = stRecvData.adapter.get(); - - _request.sServantName = ServantHelperManager::getInstance()->getAdapterServant(stRecvData.adapter->getName()); - - _begintime = TNOWMS; -} - -void TarsCurrent::initialize(const string &sRecvBuffer) -{ - TarsInputStream is; - - is.setBuffer(sRecvBuffer.c_str(), sRecvBuffer.length()); - - _request.readFrom(is); -} - -void TarsCurrent::sendResponse(const char* buff, uint32_t len) -{ - _servantHandle->sendResponse(_uid, string(buff, len), _ip, _port, _fd); -} - -void TarsCurrent::sendResponse(int iRet, const vector& buffer, const map& status, const string & sResultDesc) -{ - _ret = iRet; - - //å•å‘调用ä¸éœ€è¦è¿”回 - if (_request.cPacketType == TARSONEWAY) - { - return; - } - - TarsOutputStream os; - - if (_request.iVersion != TUPVERSION) - { - ResponsePacket response; - - response.iRequestId = _request.iRequestId; - response.iMessageType = _request.iMessageType; - response.cPacketType = TARSNORMAL; - response.iVersion = TARSVERSION; - response.status = status; - response.sBuffer = buffer; - response.sResultDesc = sResultDesc; - response.context = _responseContext; - response.iRet = iRet; - - TLOGINFO("[TARS]TarsCurrent::sendResponse :" - << response.iMessageType << "|" - << _request.sServantName << "|" - << _request.sFuncName << "|" - << response.iRequestId << endl); - - response.writeTo(os); - } - else - { - //tup回应包用请求包的结构 - RequestPacket response; - - response.iRequestId = _request.iRequestId; - response.iMessageType = _request.iMessageType; - response.cPacketType = TARSNORMAL; - response.iVersion = _request.iVersion; - response.status = status; - response.context = _responseContext; - response.sBuffer = buffer; - response.sServantName = _request.sServantName; - response.sFuncName = _request.sFuncName; - - //异常的情况下bufferå¯èƒ½ä¸ºç©ºï¼Œè¦ä¿è¯æœ‰ä¸€ä¸ªç©ºUniAttributeçš„ç¼–ç å†…容 - if(response.sBuffer.size() == 0) - { - tup::UniAttribute<> tarsAttr; - tarsAttr.setVersion(_request.iVersion); - tarsAttr.encode(response.sBuffer); - } - //iRet为0æ—¶,ä¸è®°å½•åœ¨status里é¢,节çœç©ºé—´ - if(iRet != 0) - { - response.status[ServantProxy::STATUS_RESULT_CODE] = TC_Common::tostr(iRet); - } - //sResultDesc为空时,ä¸è®°å½•åœ¨status里é¢,节çœç©ºé—´ - if(!sResultDesc.empty()) - { - response.status[ServantProxy::STATUS_RESULT_DESC] = sResultDesc; - } - - TLOGINFO("[TARS]TarsCurrent::sendResponse :" - << response.iMessageType << "|" - << response.sServantName << "|" - << response.sFuncName << "|" - << response.iRequestId << endl); - - response.writeTo(os); - } - - tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength()); - - string s = ""; - - s.append((const char*)&iHeaderLen, sizeof(tars::Int32)); - - s.append(os.getBuffer(), os.getLength()); - - _servantHandle->sendResponse(_uid, s, _ip, _port, _fd); -} - -void TarsCurrent::close() -{ - if (_servantHandle) - { - _servantHandle->close(_uid, _fd); - } -} - -ServantHandle* TarsCurrent::getServantHandle() -{ - return _servantHandle; -} - -TC_EpollServer::BindAdapter* TarsCurrent::getBindAdapter() -{ - return _bindAdapter; -} - -void TarsCurrent::reportToStat(const string& sObj) -{ - StatReport* stat = Application::getCommunicator()->getStatReport(); - - if(stat && stat->getStatPrx()) - { - int64_t endtime = TNOWMS; - int sptime = endtime - _begintime; - - //被调上报自己的setä¿¡æ¯ï¼Œsetä¿¡æ¯åœ¨setReportInfo设置 - stat->report(sObj, "" , _request.sFuncName, _ip, 0, (StatReport::StatResult)_ret, sptime, 0, false); - } -} - -//////////////////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/libservant/TarsLogger.cpp b/cpp/servant/libservant/TarsLogger.cpp deleted file mode 100644 index 7c7beb198..000000000 --- a/cpp/servant/libservant/TarsLogger.cpp +++ /dev/null @@ -1,723 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/TarsLogger.h" -#include "servant/Communicator.h" -#include "servant/Application.h" - -namespace tars -{ - -int RollWriteT::_dyeingThread = 0; -int TimeWriteT::_dyeing = 0; - -///////////////////////////////////////////////////////////////////////////////////// - -RollWriteT::RollWriteT():_dyeingRollLogger(NULL), _maxSize(10000), _maxNum(1), _logPrx(NULL) -{ -} - -RollWriteT::~RollWriteT() -{ - if(_dyeingRollLogger) - { - delete _dyeingRollLogger; - } -} - -void RollWriteT::operator()(ostream &of, const deque > &ds) -{ - vector vRemoteDyeing; - - deque >::const_iterator it = ds.begin(); - while(it != ds.end()) - { - of << it->second; - - //染色线程idä¸å­˜åœ¨ - if(it->first != 0) - { - if(!_dyeingRollLogger) - { - string sDyeingDir = _logPath; - sDyeingDir += "/"; - sDyeingDir += DYEING_DIR; - sDyeingDir += "/"; - - string sDyeingFile = sDyeingDir; - sDyeingFile += DYEING_FILE; - - TC_File::makeDirRecursive(sDyeingDir); - - //åˆå§‹åŒ–染色循环日志 - _dyeingRollLogger = new TC_RollLogger(); - - _dyeingRollLogger->init(sDyeingFile, _maxSize, _maxNum); - _dyeingRollLogger->modFlag(TC_DayLogger::HAS_TIME, false); - _dyeingRollLogger->modFlag(TC_DayLogger::HAS_TIME|TC_DayLogger::HAS_LEVEL|TC_DayLogger::HAS_PID, true); - _dyeingRollLogger->setLogLevel("DEBUG"); - } - - _dyeingRollLogger->roll(make_pair(it->first, _app + "." + _server + "|" + it->second )); - - vRemoteDyeing.push_back(_app + "." + _server + "|" + it->second); - } - - ++it; - } - of.flush(); - - if(_logPrx && vRemoteDyeing.size() > 0) - { - try - { - _logPrx->logger(DYEING_DIR, DYEING_FILE, "roll", "%Y%m%d", vRemoteDyeing); - } - catch(exception &ex) - { - TLOGERROR("[TARS] dyeing log write to remote log server error:" << ex.what() << endl); - } - } -} - -void RollWriteT::setDyeingLogInfo(const string &sApp, const string &sServer, const string & sLogPath, int iMaxSize, int iMaxNum, const CommunicatorPtr &comm, const string &sLogObj) -{ - _app = sApp; - _server = sServer; - _logPath = sLogPath; - _maxSize = iMaxSize; - _maxNum = iMaxNum; - - if(comm && !sLogObj.empty()) - { - _logPrx = comm->stringToProxy(sLogObj); - //å•ç‹¬è®¾ç½®è¶…时时间 - _logPrx->tars_timeout(3000); - } -} - - -///////////////////////////////////////////////////////////////////////////////////// - -void TarsRollLogger::setLogInfo(const string &sApp, const string &sServer, const string &sLogpath, int iMaxSize, int iMaxNum, const CommunicatorPtr &comm, const string &sLogObj) -{ - _app = sApp; - _server = sServer; - _logpath = sLogpath; - - //生æˆç›®å½• - TC_File::makeDirRecursive(_logpath + "/" + _app + "/" + _server); - - _local.start(1); - - //åˆå§‹åŒ–本地循环日志 - _logger.init(_logpath + "/" + _app + "/" + _server + "/" + _app + "." + _server, iMaxSize, iMaxNum); - _logger.modFlag(TC_DayLogger::HAS_TIME, false); - _logger.modFlag(TC_DayLogger::HAS_TIME|TC_DayLogger::HAS_LEVEL|TC_DayLogger::HAS_PID, true); - - //设置为异步 - sync(false); - - - //è®¾ç½®æŸ“è‰²æ—¥å¿—ä¿¡æ¯ - _logger.getWriteT().setDyeingLogInfo(sApp, sServer, sLogpath, iMaxSize, iMaxNum, comm, sLogObj); - -} - - -void TarsRollLogger::sync(bool bSync) -{ - if(bSync) - { - _logger.unSetupThread(); - } - else - { - _logger.setupThread(&_local); - } -} - -void TarsRollLogger::enableDyeing(bool bEnable, const string& sDyeingKey/* = ""*/) -{ - _logger.getRoll()->enableDyeing(bEnable, sDyeingKey); -} - -///////////////////////////////////////////////////////////////////////////////////// - -TarsLoggerThread::TarsLoggerThread() -{ - _local.start(1); - _remote.start(1); -} - -TarsLoggerThread::~TarsLoggerThread() -{ - //先刷新本地日志 - _local.flush(); - - //å†åˆ·æ–°è¿œç¨‹æ—¥å¿—, ä¿è¯ä¸ä¼šä¸¢æ—¥å¿— - _remote.flush(); -} - -TC_LoggerThreadGroup* TarsLoggerThread::local() -{ - return &_local; -} - -TC_LoggerThreadGroup* TarsLoggerThread::remote() -{ - return &_remote; -} - -///////////////////////////////////////////////////////////////////////////////////// -RemoteTimeWriteT::RemoteTimeWriteT():_timeWrite(NULL) -{ -} - -RemoteTimeWriteT::~RemoteTimeWriteT() -{ -} - -void RemoteTimeWriteT::setTimeWriteT(TimeWriteT *pTimeWrite) -{ - _timeWrite = pTimeWrite; -} - -void RemoteTimeWriteT::operator()(ostream &of, const deque > &buffer) -{ - const static uint32_t len = 2000; - - //写远程日志 - if(_timeWrite->_logPrx && !buffer.empty()) - { - //大于50wæ¡, 直接抛弃掉,å¦åˆ™å®¹æ˜“å¯¼è‡´å†…å­˜æ³„æ¼ - if(buffer.size() > 500000) - { - _timeWrite->writeError(buffer); - return; - } - - vector v; - v.reserve(len); - - deque >::const_iterator it = buffer.begin(); - while(it != buffer.end()) - { - v.push_back(it->second); - - ++it; - - //æ¯æ¬¡æœ€å¤šåŒæ­¥lenæ¡ - if(v.size() >= len) - { - sync2remote(v); - v.clear(); - v.reserve(len); - } - } - - if(v.size() > 0) - { - sync2remote(v); - } - } -} - -void RemoteTimeWriteT::sync2remote(const vector &v) -{ - try - { - //此处传递setä¿¡æ¯åˆ°è¿œç¨‹logserver - LogInfo stInfo; - stInfo.appname = _timeWrite->_app; - stInfo.servername = _timeWrite->_server; - stInfo.sFilename = _timeWrite->_file; - stInfo.sFormat = _timeWrite->_format; - stInfo.setdivision = _timeWrite->_setDivision; - stInfo.bHasSufix = _timeWrite->_hasSufix; - stInfo.bHasAppNamePrefix = _timeWrite->_hasAppNamePrefix; - stInfo.sConcatStr = _timeWrite->_concatStr; - stInfo.bHasSquareBracket = _timeWrite->_hasSquareBracket; - stInfo.sSepar = _timeWrite->_separ; - stInfo.sLogType = _timeWrite->_logType; - - _timeWrite->_logPrx->loggerbyInfo(stInfo,v); - - if (_timeWrite->_reportSuccPtr) - { - _timeWrite->_reportSuccPtr->report(v.size()); - } - } - catch(exception &ex) - { - TLOGERROR("[TARS] write to remote log server error:" << ex.what() << ": buffer size:" << v.size() << endl); - _timeWrite->writeError(v); - if (_timeWrite->_reportFailPtr) - { - _timeWrite->_reportFailPtr->report(v.size()); - } - } -} - -void RemoteTimeWriteT::sync2remoteDyeing(const vector &v) -{ - try - { - _timeWrite->_logPrx->logger(DYEING_DIR, DYEING_FILE, "", _timeWrite->_format, v); - } - catch(exception &ex) - { - TLOGERROR("[TARS] write dyeing log to remote log server error:" << ex.what() << ": buffer size:" << v.size() << endl); - _timeWrite->writeError(v); - } -} -///////////////////////////////////////////////////////////////////////////////////// -// -TimeWriteT::~TimeWriteT() -{ - if(_remoteTimeLogger) - { - delete _remoteTimeLogger; - } -} - -TimeWriteT::TimeWriteT() : _remoteTimeLogger(NULL), _local(true), _remote(true), _dyeingTimeLogger(NULL),_setDivision(""), - _hasSufix(true),_hasAppNamePrefix(true),_concatStr("_"),_separ("|"),_hasSquareBracket(false),_logType("") -{ -} - -void TimeWriteT::setLogInfo(const LogPrx &logPrx, const string &sApp, const string &sServer, const string &sFile, const string &sLogpath, const string &sFormat, const string& setdivision, const string& sLogType, const PropertyReportPtr &reportSuccPtr, const PropertyReportPtr &reportFailPtr) -{ - _logPrx = logPrx; - _app = sApp; - _server = sServer; - _format = sFormat; - _file = sFile; - _setDivision = setdivision; - _logType = sLogType; - _reportSuccPtr = reportSuccPtr; - _reportFailPtr = reportFailPtr; - - string sAppSrvName = _hasAppNamePrefix?(_app + "." + _server):""; - - _filePath = sLogpath + "/" + _app + "/" + _server + "/" + sAppSrvName; - if(!_file.empty()) - { - _filePath += (_hasAppNamePrefix?_concatStr:"") + sFile; - } - - string sDyeingDir = sLogpath; - sDyeingDir += "/"; - sDyeingDir += DYEING_DIR; - sDyeingDir += "/"; - - _dyeingFilePath = sDyeingDir; - - _remoteTimeLogger = new RemoteTimeLogger(); - _remoteTimeLogger->init(_filePath, _format,_hasSufix,_concatStr,NULL,true); - _remoteTimeLogger->modFlag(0xffff, false); - _remoteTimeLogger->setSeparator(_separ); - _remoteTimeLogger->enableSqareWrapper(_hasSquareBracket); - _remoteTimeLogger->setupThread(TarsLoggerThread::getInstance()->remote()); - _remoteTimeLogger->getWriteT().setTimeWriteT(this); - - if(!_local) - { - initError(); - } -} - -void TimeWriteT::initDyeingLog() -{ - TC_File::makeDirRecursive(_dyeingFilePath); - - string sDyeingFile = _dyeingFilePath; - sDyeingFile += "/"; - sDyeingFile += DYEING_FILE; - - _dyeingTimeLogger = new DyeingTimeLogger(); - _dyeingTimeLogger->init(sDyeingFile, _format); - _dyeingTimeLogger->modFlag(0xffff, false); -} - -void TimeWriteT::setLogPrx(const LogPrx &logPrx) -{ - _logPrx = logPrx; -} - -void TimeWriteT::initError() -{ - //远程错误日志 - _logger.init(_filePath + ".remote.error", _format); - _logger.modFlag(0xffff, false); -} - -void TimeWriteT::enableLocal(bool bEnable) -{ - _local = bEnable; - if(!_local) - { - initError(); - } -} - -void TimeWriteT::operator()(ostream &of, const deque > &buffer) -{ - - if(_local && of && !buffer.empty()) - { - try - { - _wt(of, buffer); - } - catch(...) - { - } - } - - if(_remote && _remoteTimeLogger && !buffer.empty()) - { - deque >::const_iterator it = buffer.begin(); - while(it != buffer.end()) - { - _remoteTimeLogger->any() << it->second; - ++it; - } - } - - vector vDyeingLog; - deque >::const_iterator it = buffer.begin(); - while(it != buffer.end()) - { - if(it->first != 0) - { - if(!_dyeingTimeLogger) - { - initDyeingLog(); - } - _dyeingTimeLogger->any() << _app << "." << _server << "|" << it->second; - - vDyeingLog.push_back(_app + "." + _server + "|" + it->second); - } - ++it; - } - if(_logPrx && !vDyeingLog.empty()) - { - try - { - _logPrx->logger(DYEING_DIR, DYEING_FILE, "day", "%Y%m%d", vDyeingLog); - } - catch(exception &ex) - { - TLOGERROR("[TARS] dyeing log write to remote log server error:" << ex.what() << endl); - } - } -} - -void TimeWriteT::writeError(const vector &buffer) -{ - if(!_local) - { - for(size_t i = 0; i < buffer.size(); i++) - { - _logger.any() << buffer[i]; - } - } - - //å‘Šè­¦ - string sInfo = _app + "." + _server + "|"; - sInfo += ServerConfig::LocalIp + "|sync log to remote tarslog error"; - FDLOG("tarserror") << sInfo < > &buffer) -{ - if(!_local) - { - deque >::const_iterator it = buffer.begin(); - while(it != buffer.end()) - { - _logger.any() << it->second; - ++it; - } - } - - //å‘Šè­¦ - string sInfo = _app + "." + _server + "|"; - sInfo += ServerConfig::LocalIp + "|sync log to remote tarslog error(buffer.size>500000)"; - FDLOG("tarserror") << sInfo <::iterator it = _loggers.begin(); - while(it != _loggers.end()) - { - delete it->second; - ++it; - } - _loggers.clear(); -} - -void TarsTimeLogger::initTimeLogger(TimeLogger *pTimeLogger, const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr) -{ - string sAppSrvName = _hasAppNamePrefix?(_app + "." + _server):""; - string sFilePath = _logpath + "/" + _app + "/" + _server + "/" + sAppSrvName; - - if(!sFile.empty()) - { - sFilePath += (_hasAppNamePrefix?_concatStr:"") + sFile; - } - - //æœ¬åœ°æ—¥å¿—æ ¼å¼ - pTimeLogger->init(sFilePath, sFormat,_hasSufix,_concatStr,logTypePtr,!_local); - pTimeLogger->modFlag(0xffff, false); - pTimeLogger->modFlag(TC_DayLogger::HAS_TIME, true); - pTimeLogger->setSeparator(_separ); - pTimeLogger->enableSqareWrapper(_hasSquareBracket); - pTimeLogger->setupThread(TarsLoggerThread::getInstance()->local()); - - //è¿œç¨‹æ—¥å¿—æ ¼å¼ - pTimeLogger->getWriteT().enableSufix(_hasSufix); - pTimeLogger->getWriteT().enablePrefix(_hasAppNamePrefix); - pTimeLogger->getWriteT().setFileNameConcatStr(_concatStr); - pTimeLogger->getWriteT().setSeparator(_separ); - pTimeLogger->getWriteT().enableSqareWrapper(_hasSquareBracket); - pTimeLogger->getWriteT().enableLocal(_local); - pTimeLogger->getWriteT().enableRemote(_remote); - - string sLogType = ""; - if(logTypePtr) - { - sLogType = logTypePtr->toString(); - } - - PropertyReportPtr reportSuccPtr = NULL; - PropertyReportPtr reportFailPtr = NULL; - if (_remote && _logStatReport) - { - string sKey = _app + "." + _server + "." + sFile; - reportSuccPtr = _comm->getStatReport()->createPropertyReport(sKey + "_log_send_succ", PropertyReport::sum()); - reportFailPtr = _comm->getStatReport()->createPropertyReport(sKey + "_log_send_fail", PropertyReport::sum()); - } - - pTimeLogger->getWriteT().setLogInfo(_logPrx, _app, _server, sFile, _logpath, sFormat, _setDivision, sLogType, reportSuccPtr, reportFailPtr); -} - -void TarsTimeLogger::initTimeLogger(TimeLogger *pTimeLogger,const string &sApp, const string &sServer, const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr) -{ - string sAppSrvName = _hasAppNamePrefix?(sApp + "." + sServer):""; - string sFilePath = _logpath + "/" + sApp + "/" + sServer + "/" + sAppSrvName; - - if(!sFile.empty()) - { - sFilePath += (_hasAppNamePrefix?_concatStr:"") + sFile; - - } - - //æœ¬åœ°æ—¥å¿—æ ¼å¼ - pTimeLogger->init(sFilePath,sFormat,_hasSufix,_concatStr,logTypePtr,!_local); - pTimeLogger->modFlag(0xffff, false); - pTimeLogger->modFlag(TC_DayLogger::HAS_TIME, true); - pTimeLogger->setSeparator(_separ); - pTimeLogger->enableSqareWrapper(_hasSquareBracket); - pTimeLogger->setupThread(TarsLoggerThread::getInstance()->local()); - - //è¿œç¨‹æ—¥å¿—æ ¼å¼ - pTimeLogger->getWriteT().enableSufix(_hasSufix); - pTimeLogger->getWriteT().enablePrefix(_hasAppNamePrefix); - pTimeLogger->getWriteT().setFileNameConcatStr(_concatStr); - pTimeLogger->getWriteT().setSeparator(_separ); - pTimeLogger->getWriteT().enableSqareWrapper(_hasSquareBracket); - pTimeLogger->getWriteT().enableLocal(_local); - pTimeLogger->getWriteT().enableRemote(_remote); - string sLogType = ""; - if(logTypePtr) - { - sLogType = logTypePtr->toString(); - } - - PropertyReportPtr reportSuccPtr = NULL; - PropertyReportPtr reportFailPtr = NULL; - if (_remote && _logStatReport) - { - string sKey = _app + "." + _server + "." + sFile; - reportSuccPtr = _comm->getStatReport()->createPropertyReport(sKey + "_log_send_succ", PropertyReport::sum()); - reportFailPtr = _comm->getStatReport()->createPropertyReport(sKey + "_log_send_fail", PropertyReport::sum()); - } - - pTimeLogger->getWriteT().setLogInfo(_logPrx, sApp, sServer, sFile, _logpath, sFormat, _setDivision, sLogType, reportSuccPtr, reportFailPtr); -} - -void TarsTimeLogger::setLogInfo(const CommunicatorPtr &comm, const string &obj, const string &sApp, const string &sServer, const string &sLogpath, const string& setdivision, const bool &bLogStatReport) -{ - _app = sApp; - _server = sServer; - _logpath = sLogpath; - _comm = comm; - _setDivision = setdivision; - _logStatReport = bLogStatReport; - if(!obj.empty()) - { - _logPrx = _comm->stringToProxy(obj); - //å•ç‹¬è®¾ç½®è¶…时时间 - _logPrx->tars_timeout(3000); - - if(_defaultLogger) - { - _defaultLogger->getWriteT().setLogPrx(_logPrx); - } - } - - //创建本地目录 - TC_File::makeDirRecursive(_logpath + "/" + _app + "/" + _server); -} - -void TarsTimeLogger::initFormat(const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr) -{ - if(sFile.empty()) - { - if(!_defaultLogger) - { - _defaultLogger = new TimeLogger(); - - } - initTimeLogger(_defaultLogger, "", sFormat,logTypePtr); - } - else - { - string s = _app + "/" + _server + "/"+ sFile; - Lock lock(*this); - map::iterator it = _loggers.find(s); - if( it == _loggers.end()) - { - TimeLogger *p = new TimeLogger(); - initTimeLogger(p, sFile, sFormat,logTypePtr); - _loggers[s] = p; - return; - } - - initTimeLogger(it->second, sFile, sFormat,logTypePtr); - } -} -void TarsTimeLogger::initFormat(const string &sApp, const string &sServer,const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr) -{ - string s = sApp + "/" + sServer + "/"+ sFile; - Lock lock(*this); - map::iterator it = _loggers.find(s); - if( it == _loggers.end()) - { - TimeLogger *p = new TimeLogger(); - initTimeLogger(p, sApp, sServer, sFile, sFormat,logTypePtr); - _loggers[s] = p; - return; - } - - initTimeLogger(it->second, sApp, sServer, sFile, sFormat,logTypePtr); -} - -TarsTimeLogger::TimeLogger* TarsTimeLogger::logger(const string &sFile) -{ - if(sFile.empty()) - { - if(!_defaultLogger) - { - _defaultLogger = new TimeLogger(); - initTimeLogger(_defaultLogger, "", "%Y%m%d"); - } - return _defaultLogger; - } - - string s = _app + "/" + _server + "/"+ sFile; - Lock lock(*this); - map::iterator it = _loggers.find(s); - if( it == _loggers.end()) - { - TimeLogger *p = new TimeLogger(); - initTimeLogger(p, sFile, "%Y%m%d"); - _loggers[s] = p; - return p; - } - - return it->second; -} - -TarsTimeLogger::TimeLogger* TarsTimeLogger::logger(const string &sApp, const string &sServer,const string &sFile) -{ - string s = sApp + "/" + sServer + "/"+ sFile; - - Lock lock(*this); - map::iterator it = _loggers.find(s); - if( it == _loggers.end()) - { - TimeLogger *p = new TimeLogger(); - initTimeLogger(p, sApp, sServer, sFile, "%Y%m%d"); - _loggers[s] = p; - return p; - } - - return it->second; -} - - -void TarsTimeLogger::sync(const string &sFile, bool bSync) -{ - if(bSync) - { - logger(sFile)->unSetupThread(); - } - else - { - logger(sFile)->setupThread(TarsLoggerThread::getInstance()->local()); - } -} - -void TarsTimeLogger::enableRemote(const string &sFile, bool bEnable) -{ - logger(sFile)->getWriteT().enableRemote(bEnable); -} - -void TarsTimeLogger::enableRemoteEx(const string &sApp, const string &sServer,const string &sFile, bool bEnable) -{ - logger(sApp,sServer,sFile)->getWriteT().enableRemote(bEnable); -} -void TarsTimeLogger::enableLocal(const string &sFile, bool bEnable) -{ - logger(sFile)->getWriteT().enableLocal(bEnable); - logger(sFile)->setRemote(!bEnable); -} - -void TarsTimeLogger::enableLocalEx(const string &sApp, const string &sServer,const string &sFile, bool bEnable) -{ - logger(sApp,sServer,sFile)->getWriteT().enableLocal(bEnable); - logger(sApp,sServer,sFile)->setRemote(!bEnable); -} - -} diff --git a/cpp/servant/libservant/TarsNodeF.cpp b/cpp/servant/libservant/TarsNodeF.cpp deleted file mode 100644 index bcd633b9d..000000000 --- a/cpp/servant/libservant/TarsNodeF.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/TarsNodeF.h" -#include "servant/TarsLogger.h" -#include "servant/Communicator.h" - -namespace tars -{ - -void TarsNodeFHelper::setNodeInfo(const CommunicatorPtr &comm, const string &obj, const string &app, const string &server) -{ - _comm = comm; - if(!obj.empty()) - { - _nodePrx = _comm->stringToProxy(obj); - } - - _si.application = app; - _si.serverName = server; - _si.pid = getpid(); -} - -void TarsNodeFHelper::keepAlive(const string &adapter) -{ - try - { - if(_nodePrx) - { - set s; - { - TC_LockT lock(*this); - - _adapterSet.insert(adapter); - - if(adapter != "AdminAdapter") - { - return; - } - s.swap(_adapterSet); - } - ServerInfo si = _si; - set::const_iterator it = s.begin(); - while(it != s.end()) - { - si.adapter = *it; - _nodePrx->async_keepAlive(NULL,si); - ++it; - } - - } - } - catch(exception &ex) - { - TLOGERROR("TarsNodeFHelper::keepAlive error:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("TarsNodeFHelper::keepAlive unknown error" << endl); - } -} - -void TarsNodeFHelper::reportVersion(const string &version) -{ - try - { - if(_nodePrx) - { - _nodePrx->async_reportVersion(NULL, _si.application, _si.serverName, version); - } - } - catch(exception &ex) - { - TLOGERROR("TarsNodeFHelper::reportVersion error:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("TarsNodeFHelper::reportVersion unknown error" << endl); - } -} - -} - diff --git a/cpp/servant/libservant/TarsNotify.cpp b/cpp/servant/libservant/TarsNotify.cpp deleted file mode 100644 index 60bfc7317..000000000 --- a/cpp/servant/libservant/TarsNotify.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/TarsNotify.h" -#include "servant/Communicator.h" -#include "servant/TarsLogger.h" - -namespace tars -{ - -int TarsRemoteNotify::setNotifyInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string &sSetName) -{ - _comm = comm; - if(!obj.empty()) - { - _notifyPrx = _comm->stringToProxy(obj); - _notifyPrx->tars_timeout(500); - } - - _setName = sSetName; - _app = app; - _serverName = serverName; - - return 0; -} - -void TarsRemoteNotify::report(const string &sResult, bool bSync) -{ - try - { - if(_notifyPrx) - { - ReportInfo info; - info.sApp = _app; - info.sServer = _serverName; - info.sSet = _setName; - info.sThreadId = TC_Common::tostr(pthread_self()); - info.sMessage = sResult; - if(!bSync) - { - //_notifyPrx->async_reportServer(NULL, _app + "." + _serverName, TC_Common::tostr(pthread_self()), sResult); - _notifyPrx->async_reportNotifyInfo(NULL, info); - } - else - { - //_notifyPrx->reportServer(_app + "." + _serverName, TC_Common::tostr(pthread_self()), sResult); - _notifyPrx->reportNotifyInfo(info); - } - } - } - catch(exception &ex) - { - TLOGERROR("TarsRemoteNotify::report error:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("TarsRemoteNotify::report unknown error" << endl); - } -} - -void TarsRemoteNotify::notify(NOTIFYLEVEL level, const string &sMessage) -{ - try - { - if(_notifyPrx) - { - ReportInfo info; - // info.eType = 0; - info.sApp = _app; - info.sServer = _serverName; - info.sSet = _setName; - info.sThreadId = TC_Common::tostr(pthread_self()); - info.sMessage = sMessage; - info.eLevel = level; - //_notifyPrx->async_notifyServer(NULL, _app + "." + _serverName, level, sMessage); - _notifyPrx->async_reportNotifyInfo(NULL, info); - } - } - catch(exception &ex) - { - TLOGERROR("TarsRemoteNotify::notify error:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("TarsRemoteNotify::notify unknown error" << endl); - } -} - -} - - diff --git a/cpp/servant/libservant/Transceiver.cpp b/cpp/servant/libservant/Transceiver.cpp deleted file mode 100644 index b56d5fc01..000000000 --- a/cpp/servant/libservant/Transceiver.cpp +++ /dev/null @@ -1,668 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "servant/Transceiver.h" -#include "servant/AdapterProxy.h" -#include "servant/Application.h" -#include "servant/TarsLogger.h" -//#include "servant/AuthLogic.h" -#include "servant/Auth.h" - -#if TARS_SSL -#include "util/tc_openssl.h" -#endif - -#if TARS_HTTP2 -#include "util/tc_nghttp2.h" -#include "util/tc_http2clientmgr.h" -#endif -namespace tars -{ -/////////////////////////////////////////////////////////////////////// -Transceiver::Transceiver(AdapterProxy * pAdapterProxy,const EndpointInfo &ep) -: _adapterProxy(pAdapterProxy) -, _ep(ep) -, _fd(-1) -, _connStatus(eUnconnected) -, _conTimeoutTime(0) -, _authState(AUTH_INIT) -{ - _fdInfo.iType = FDInfo::ET_C_NET; - _fdInfo.p = (void *)this; - _fdInfo.fd = -1; -} - -Transceiver::~Transceiver() -{ - close(); -} - -void Transceiver::checkTimeout() -{ - if(eConnecting == _connStatus && TNOWMS > _conTimeoutTime) - { - //链接超时 - TLOGERROR("[TARS][Transceiver::checkTimeout ep:"<<_adapterProxy->endpoint().desc()<<" , connect timeout]"<setConTimeout(true); - close(); - } -} - -bool Transceiver::isSSL() const -{ - return _adapterProxy->endpoint().type() == EndpointInfo::SSL; -} - -void Transceiver::reconnect() -{ - close(); - - connect(); -} - -void Transceiver::connect() -{ - if(isValid()) - { - return; - } - - if(_connStatus == eConnecting || _connStatus == eConnected) - { - return; - } - - int fd = -1; - - if (_ep.type() == EndpointInfo::UDP) - { - fd = NetworkUtil::createSocket(true); - NetworkUtil::setBlock(fd, false); - _connStatus = eConnected; - } - else - { - fd = NetworkUtil::createSocket(false); - NetworkUtil::setBlock(fd, false); - - bool bConnected = NetworkUtil::doConnect(fd, _ep.addr()); - if(bConnected) - { - setConnected(); - } - else - { - _connStatus = Transceiver::eConnecting; - _conTimeoutTime = TNOWMS + _adapterProxy->getConTimeout(); - } - } - - _fd = fd; - - TLOGINFO("[TARS][Transceiver::connect objname:" << _adapterProxy->getObjProxy()->name() - << ",connect:" << _ep.desc() << ",fd:" << _fd << "]" << endl); - - //设置网络qosçš„dscp标志 - if(0 != _ep.qos()) - { - int iQos=_ep.qos(); - ::setsockopt(fd,SOL_IP,IP_TOS,&iQos,sizeof(iQos)); - } - - //设置套接å£é€‰é¡¹ - vector &socketOpts = _adapterProxy->getObjProxy()->getSocketOpt(); - for(size_t i=0; igetObjProxy()->name() - << ",desc:" << _ep.desc() - << ",fd:" << _fd - << ",level:" << socketOpts[i].level - << ",optname:" << socketOpts[i].optname - << ",optval:" << socketOpts[i].optval - <<" ]"<< endl); - } - } - - _adapterProxy->getObjProxy()->getCommunicatorEpoll()->addFd(fd, &_fdInfo, EPOLLIN|EPOLLOUT); -} - -void Transceiver::setConnected() -{ - _connStatus = eConnected; - _adapterProxy->setConTimeout(false); - _adapterProxy->addConnExc(false); - - _onConnect(); -} - -void Transceiver::_onConnect() -{ -#if TARS_SSL - if (isSSL()) - { - // 分é…ssl对象 - SSL* ssl = NewSSL("client"); - if (!ssl) - { - ObjectProxy* obj = _adapterProxy->getObjProxy(); - TLOGERROR("[TARS][_onConnect:" << obj->name() << " can't find client SSL_CTX " << endl); - this->close(); - return; - } - - _openssl.reset(new TC_OpenSSL()); - _openssl->Init(ssl, false); - std::string out = _openssl->DoHandshake(); - if (_openssl->HasError()) - { - TLOGERROR("[TARS] SSL_connect failed " << endl); - this->close(); - return; - } - - // send the encrypt data from write buffer - if (!out.empty()) - { - this->sendRequest(out.data(), out.size(), true); - } - return; - } -#endif - - _doAuthReq(); -} - -void Transceiver::_doAuthReq() -{ - ObjectProxy* obj = _adapterProxy->getObjProxy(); - - TLOGINFO("[TARS][_onConnect:" << obj->name() << " auth Type is " << _adapterProxy->endpoint().authType() << endl); - - _adapterProxy->doInvoke(); -} - -void Transceiver::close() -{ - if(!isValid()) return; - -#if TARS_SSL - if (_openssl) - { - _openssl->Release(); - _openssl.reset(); - } -#endif - -#if TARS_HTTP2 - Http2ClientSessionManager::getInstance()->delSession(_adapterProxy->getId()); -#endif - - _adapterProxy->getObjProxy()->getCommunicatorEpoll()->delFd(_fd,&_fdInfo,EPOLLIN|EPOLLOUT); - - NetworkUtil::closeSocketNoThrow(_fd); - - _connStatus = eUnconnected; - - _fd = -1; - - _sendBuffer.Clear(); - - _recvBuffer.Clear(); - - _authState = AUTH_INIT; - - TLOGINFO("[TARS][trans close:"<< _adapterProxy->getObjProxy()->name()<< "," << _ep.desc() << "]" << endl); -} - -int Transceiver::doRequest() -{ - if(!isValid()) - { - return -1; - } - - int iRet = 0; - - //bufä¸ä¸ºç©º,å…ˆå‘生buffer的内容 - if(!_sendBuffer.IsEmpty()) - { - size_t length = 0; - void* data = NULL; - _sendBuffer.PeekData(data, length); - - iRet = this->send(data, length, 0); - - //失败,直接返回 - if(iRet < 0) - { - return iRet; - } - - if(iRet > 0) - { - _sendBuffer.Consume(iRet); - if (_sendBuffer.IsEmpty()) - _sendBuffer.Shrink(); - else - return 0; - } - } - - //å–adapter里é¢ç§¯æ”’çš„æ•°æ® - _adapterProxy->doInvoke(); - - //object里é¢åº”该是空的 - assert(_adapterProxy->getObjProxy()->timeoutQSize() == 0); - - return 0; -} - -int Transceiver::sendRequest(const char * pData, size_t iSize, bool forceSend) -{ - //ç©ºæ•°æ® ç›´æŽ¥è¿”å›žæˆåŠŸ - if(iSize == 0) - { - return eRetOk; - } - - if(_connStatus != eConnected) - { - return eRetError; - } - - //bufä¸ä¸ºç©º,直接返回失败 - //ç­‰bufferå¯å†™äº†,epoll会通知写时间 - if(!_sendBuffer.IsEmpty()) - { - return eRetError; - } - -#if TARS_SSL - // æ¡æ‰‹æ•°æ®å·²åŠ å¯†,直接å‘é€ï¼Œä¼šè¯æ•°æ®éœ€åŠ å¯† - std::string out; - if (isSSL() && _openssl->IsHandshaked()) - { - out = _openssl->Write(pData, iSize); - pData = out.data(); - iSize = out.size(); - } -#endif - - int iRet = this->send(pData,iSize,0); - - //失败,直接返回 - if(iRet < 0) - { - return eRetError; - } - - //没有全部å‘é€å®Œ,写buffer 返回æˆåŠŸ - if(iRet < (int)iSize) - { - _sendBuffer.PushData(pData+iRet,iSize-iRet); - return eRetFull; - } - - return eRetOk; -} - -////////////////////////////////////////////////////////// -TcpTransceiver::TcpTransceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep) -: Transceiver(pAdapterProxy, ep) -{ -} - -int TcpTransceiver::doResponse(list& done) -{ - if(!isValid()) - { - return -1; - } - - int iRet = 0; - - done.clear(); - - do - { - _recvBuffer.AssureSpace(8 * 1024); - char stackBuffer[64 * 1024]; - - struct iovec vecs[2]; - vecs[0].iov_base = _recvBuffer.WriteAddr(); - vecs[0].iov_len = _recvBuffer.WritableSize(); - vecs[1].iov_base = stackBuffer; - vecs[1].iov_len = sizeof stackBuffer; - - if ((iRet = this->readv(vecs, 2)) > 0) - { - if (static_cast(iRet) <= vecs[0].iov_len) - { - _recvBuffer.Produce(iRet); - } - else - { - _recvBuffer.Produce(vecs[0].iov_len); - size_t stackBytes = static_cast(iRet) - vecs[0].iov_len; - _recvBuffer.PushData(stackBuffer, stackBytes); - } - } - } - while (iRet>0); - - TLOGINFO("[TARS][tcp doResponse objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",recvbuf:" << _recvBuffer.ReadableSize() << "]" << endl); - - if(!_recvBuffer.IsEmpty()) - { - try - { - const char* data = _recvBuffer.ReadAddr(); - size_t len = _recvBuffer.ReadableSize(); -#if TARS_SSL - if (isSSL()) - { - const bool preNotHandshake = !_openssl->IsHandshaked(); - std::string out; - if (!_openssl->Read(_recvBuffer.ReadAddr(), _recvBuffer.ReadableSize(), out)) - { - TLOGERROR("[TARS][SSL_connect Failed: " << _adapterProxy->getObjProxy()->name() << endl); - this->close(); - return -1; - } - else - { - sendRequest(out.data(), out.size(), true); - } - - _recvBuffer.Clear(); - - if (!_openssl->IsHandshaked()) - return 0; - - if (preNotHandshake) - _doAuthReq(); - - std::string* plainBuf = _openssl->RecvBuffer(); - data = plainBuf->data(); - len = plainBuf->size(); - } -#endif - size_t pos = 0; - ProxyProtocol& proto = _adapterProxy->getObjProxy()->getProxyProtocol(); - - if (proto.responseExFunc) - { - long id = _adapterProxy->getId(); - pos = proto.responseExFunc(data, len, done, (void*)id); - } - else - { - pos = proto.responseFunc(data, len, done); - } - - if(pos > 0) - { -#if TARS_SSL - if (isSSL()) - { - std::string* plainBuf = _openssl->RecvBuffer(); - plainBuf->erase(0, pos); - } - else -#endif - { - _recvBuffer.Consume(pos); - if (_recvBuffer.Capacity() > 8 * 1024 * 1024) - _recvBuffer.Shrink(); - } - } - } - catch (exception &ex) - { - TLOGERROR("[TARS][tcp doResponse objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() << ",tcp recv decode error:" << ex.what() << endl); - - close(); - } - catch (...) - { - TLOGERROR("[TARS][tcp doResponse objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc" << _ep.desc() << ",tcp recv decode error." << endl); - - close(); - } - } - - return done.empty()?0:1; -} - -int TcpTransceiver::send(const void* buf, uint32_t len, uint32_t flag) -{ - //åªæœ‰æ˜¯è¿žæŽ¥çŠ¶æ€æ‰èƒ½æ”¶å‘æ•°æ® - if(_connStatus != eConnected) - { - return -1; - } - - int iRet = ::send(_fd, buf, len, flag); - - if (iRet < 0 && errno != EAGAIN) - { - TLOGINFO("[TARS][tcp send objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() - << ",fail! errno:" << errno << "," << strerror(errno) << ",close]" << endl); - - close(); - - return iRet; - } - - // EAGAINä¸èƒ½è®¤ä¸ºæ˜¯é”™è¯¯ - if (iRet < 0 && errno == EAGAIN) - iRet = 0; - - TLOGINFO("[TARS][tcp send," << _adapterProxy->getObjProxy()->name() << ",fd:" << _fd - << ",desc:" << _ep.desc() << ",len:" << iRet << "]" << endl); - - return iRet; -} - -int TcpTransceiver::readv(const struct iovec* vecs, int32_t vcnt) -{ - //åªæœ‰æ˜¯è¿žæŽ¥çŠ¶æ€æ‰èƒ½æ”¶å‘æ•°æ® - if(eConnected != _connStatus) - return -1; - - int iRet = ::readv(_fd, vecs, vcnt); - - if (iRet == 0 || (iRet < 0 && errno != EAGAIN)) - { - TLOGINFO("[TARS][tcp readv, " << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ", " << _ep.desc() <<",ret " << iRet - << ", fail! errno:" << errno << "," << strerror(errno) << ",close]" << endl); - - close(); - - return 0; - } - - TLOGINFO("[TARS][tcp readv," << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << "," << _ep.desc() << ",ret:" << iRet << "]" << endl); - - return iRet; -} - -int TcpTransceiver::recv(void* buf, uint32_t len, uint32_t flag) -{ - //åªæœ‰æ˜¯è¿žæŽ¥çŠ¶æ€æ‰èƒ½æ”¶å‘æ•°æ® - if(_connStatus != eConnected) - { - return -1; - } - - int iRet = ::recv(_fd, buf, len, flag); - - if (iRet == 0 || (iRet < 0 && errno != EAGAIN)) - { - TLOGINFO("[TARS][tcp recv objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ", " << _ep.desc() <<",ret " << iRet - << ", fail! errno:" << errno << "," << strerror(errno) << ",close]" << endl); - - close(); - - return 0; - } - - TLOGINFO("[TARS][tcp recv objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() << ",ret:" << iRet << "]" << endl); - - return iRet; -} - -///////////////////////////////////////////////////////////////// -UdpTransceiver::UdpTransceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep) -: Transceiver(pAdapterProxy, ep) -, _recvBuffer(NULL) -{ - // UDPä¸æ”¯æŒé‰´æƒ - _authState = AUTH_SUCC; - - if(!_recvBuffer) - { - _recvBuffer = new char[DEFAULT_RECV_BUFFERSIZE]; - if(!_recvBuffer) - { - throw TC_Exception("objproxy '" + _adapterProxy->getObjProxy()->name() + "' malloc udp receive buffer fail"); - } - } - -} - -UdpTransceiver::~UdpTransceiver() -{ - if(_recvBuffer) - { - delete _recvBuffer; - _recvBuffer = NULL; - } -} - -int UdpTransceiver::doResponse(list& done) -{ - if(!isValid()) - { - return -1; - } - - int recv = 0; - - done.clear(); - do - { - if ((recv = this->recv(_recvBuffer, DEFAULT_RECV_BUFFERSIZE, 0)) > 0) - { - TLOGINFO("[TARS][udp doResponse objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",recvbuf:" << recv << "]" << endl); - - try - { - _adapterProxy->getObjProxy()->getProxyProtocol().responseFunc(_recvBuffer, recv, done); - } - catch (exception &ex) - { - TLOGERROR("[TARS][udp doResponse, " << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() - << ", udp recv decode error:" << ex.what() << endl); - } - catch (...) - { - TLOGERROR("[TARS][udp doResponse, " << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() - << ", udp recv decode error." << endl); - } - } - } - while (recv > 0); - - return done.empty()?0:1; -} - -int UdpTransceiver::send(const void* buf, uint32_t len, uint32_t flag) -{ - if(!isValid()) - { - return -1; - } - - int iRet = ::sendto(_fd, buf, len, flag, (struct sockaddr*) &(_ep.addr()), sizeof(sockaddr)); - - if (iRet<0) - { - if(errno != EAGAIN) - { - TLOGERROR("[TARS][udp send objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() - << ", fail! errno:" << errno << "," << strerror(errno) << ",close]" << endl); - - close(); - - return iRet; - } - - iRet = 0; - } - else if(iRet > 0 && iRet != (int)len) - { - TLOGERROR("[TARS][udp send objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << "," << _ep.desc() << ", send error." - << ", len:" << len << ", sendLen:" << iRet << endl); - - //udpåªå‘一次 å‘é€ä¸€åŠä¹Ÿç®—全部å‘é€æˆåŠŸ - iRet = len; - } - - return iRet; -} - -int UdpTransceiver::recv(void* buf, uint32_t len, uint32_t flag) -{ - if(!isValid()) - { - return -1; - } - - int iRet = ::recvfrom(_fd, buf, len, flag, NULL, NULL); //need check from_ip & port - - if (iRet < 0 && errno != EAGAIN) - { - TLOGERROR("[TARS][udp recv objname:" << _adapterProxy->getObjProxy()->name() - << ",fd:" << _fd << ",desc:" << _ep.desc() - << ", fail! errno:" << errno << "," << strerror(errno) << ",close]" << endl); - - close(); - - return 0; - } - - return iRet; -} - -///////////////////////////////////////////////////////////////// -} diff --git a/cpp/servant/makefile/makefile.tars b/cpp/servant/makefile/makefile.tars deleted file mode 100644 index b99a82d18..000000000 --- a/cpp/servant/makefile/makefile.tars +++ /dev/null @@ -1,219 +0,0 @@ - -# ------------------------------------------------------------------------------- - -TARS_PATH :=/usr/local/tars/cpp -# !!如果业务è¦ä½¿ç”¨OPENSSL,那么请在业务makefile中添加定义 TARS_SSL := 1 -# # # !!必须放在include /usr/local/tars/makefile.tars之å‰ï¼Œå› ä¸ºæœ¬æ–‡ä»¶è¦å¼•ç”¨TARS_SSLï¼ - -#------------------------------------------------------------------------------- - -CC = gcc -CXX = g++ -CFLAGS += -std=c++11 -g -O2 -Wno-deprecated -Wall - -#------------------------------------------------------------------------------- - -TARS2CPP := ${TARS_PATH}/tools/tars2cpp ${TARS2CPP_FLAG} -PROTO2TARS := ${TARS_PATH}/tools/pb2tarscpp - -#------------------------------------------------------------------------------- - -TARS_SRC := $(wildcard *.tars) -TARS_H := $(patsubst %.tars,%.h, $(TARS_SRC)) - -PB_SRC := $(wildcard *.proto) -PB_H := $(patsubst %.proto,%.pb.h, $(PB_SRC)) -PB_TARS_H := $(patsubst %.proto,%.tars.h, $(PB_SRC)) -PB_CPP := $(patsubst %.proto,%.pb.cc, $(PB_SRC)) -PB_OBJ := $(patsubst %.proto,%.pb.o, $(PB_SRC)) - -ifneq ($(PB_SRC),) - LIB += -lprotobuf -endif - -LOCAL_SRC += $(sort $(wildcard *.cpp *.c) $(PB_CPP)) - -LOCAL_OBJ += $(patsubst %.cpp,%.o, $(patsubst %.cc,%.o, $(LOCAL_SRC))) - -DEP_FILE := $(foreach obj, $(LOCAL_OBJ), $(dir $(obj)).$(basename $(notdir $(obj))).d) - -#------------------------------------------------------------------------------- - -MYSQL_INC += -I/usr/local/mysql/include/mysql -I/usr/local/mysql/include -I/usr/include/mysql -MYSQL_LIB_DIR += -L/usr/local/mysql/lib/mysql -L/usr/local/mysql/lib -L/usr/lib/mysql -LIB_DIR += ${MYSQL_LIB_DIR} - -INCLUDE += -I${TARS_PATH}/include -I./ ${MYSQL_INC} -LIB += -L${TARS_PATH}/lib -ltarsservant -ltarsparse -ltarsutil -lpthread ${LIB_DIR} - -ifneq ($(TARS_SSL), 0) -ifneq ($(TARS_SSL), ) - #业务编译 - CFLAGS += -DTARS_SSL=1 - LIB += -lssl -lcrypto -endif -endif - -ifneq ($(TARS_HTTP2), 0) -ifneq ($(TARS_HTTP2), ) - #业务编译 - CFLAGS += -DTARS_HTTP2=1 - LIB += -lnghttp2 -endif -endif - -#----------------------------------------------------------------------------- - -TARGET := $(strip $(TARGET)) -IS_LIB := F - - -ifeq ($(SUFFIX),.a) - IS_LIB := T -endif - -ifeq ($(SUFFIX),.so) - IS_LIB := T -endif - -#------------------------------------------------------------------------------- - -all : $(PB_H) $(TARS_H) $(LOCAL_OBJ) $(TARGET) FORSTRIP - -#------------------------------------------------------------------------------- -ifeq ($(IS_LIB),T) - -$(filter %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ) - ar r $@ $(LOCAL_OBJ) - -$(filter %.so,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ) - $(CC) $(LFLAGS) -shared -o $@ $(LOCAL_OBJ) $(LIB) - -$(filter-out %.so %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ) - $(CXX) $(CFLAGS) -o $@ $^ $(INCLUDE) $(LIB) - -FORSTRIP:$(TARGET) -ifeq ($(STRIP_FLAG),Y) - @strip $(TARGET) -endif - -else - -$(filter %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ) - ar r $@ $(LOCAL_OBJ) - -$(filter %.so,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ) - $(CC) $(LFLAGS) -shared -o $@ $(LOCAL_OBJ) $(LIB) - -$(filter-out %.so %.a,$(TARGET)) : $(LOCAL_OBJ) $(REMOTE_OBJ) - $(CXX) $(CFLAGS) -o $@ $^ $(INCLUDE) $(LIB) - -FORSTRIP:$(TARGET) -ifeq ($(STRIP_FLAG),Y) - @strip $(TARGET) -endif - -endif - -#---------------------------------------------------------------------------------- - -copyfile = if test -z "$(APP)" || test -z "$(TARGET)"; then \ - echo "['APP' or 'TARGET' option is empty.]"; exit 1; \ - else \ - if test ! -d $(2); then \ - echo "[No such dir:$(2), now we create it.]";\ - mkdir -p $(2);\ - fi; \ - echo "[Copy file $(1) -> $(2)]"; \ - cp -v $(1) $(2); \ - fi; -#---------------------------------------------------------------------------------- - -$(PB_H) : $(PB_SRC) - rm -vf $(PB_H) $(PB_CPP) - protoc --plugin=protoc-gen-custom=${PROTO2TARS} --custom_out=./ ${PB_SRC} --cpp_out=./ - -#ifneq ($(TARS_SRC),) -$(TARS_H) : $(TARS_SRC) - @echo "tars2cpp ${TARS2CPP_FLAG} " - rm -vf $(TARS_H) - @echo "tars2cpp ${TARS2CPP_FLAG} " - $(TARS2CPP) ${TARS2CPP_FLAG} $(TARS_SRC) - -#endif -#---------------------------------------------------------------------------------- - -tar: $(TARGET) $(CONFIG) - @if [ -d $(TARGET)_tmp_dir ]; then \ - echo "dir has exist:$(TARGET)_tmp_dir, abort."; \ - exit 1; \ - else \ - mkdir $(TARGET)_tmp_dir $(TARGET)_tmp_dir/$(TARGET);\ - cp -rf $(TARGET) $(CONFIG) $(TARGET)_tmp_dir/$(TARGET)/; \ - cd $(TARGET)_tmp_dir; tar --exclude=".svn" --exclude="_svn" -czvf $(TARGET).tgz $(TARGET)/; cd ..; \ - if [ -f "$(TARGET).tgz" ]; then \ - mv -vf $(TARGET).tgz $(TARGET).`date +%Y%m%d%H%M%S`.tgz; \ - fi; \ - mv $(TARGET)_tmp_dir/$(TARGET).tgz ./; \ - rm -rf $(TARGET)_tmp_dir; \ - echo "tar cvfz $(TARGET).tgz ..."; \ - fi - -HELP += $(HELP_TAR) - -ifneq ($(TARS_SRC),) - -SERVER_NAME := $(TARGET) -SERVER_DIR := /home/tarsproto/$(APP)/$(SERVER_NAME) -RELEASE_TARS += $(TARS_SRC) $(TARS_H) - -release: $(TARS_H) - @$(call copyfile, $(RELEASE_TARS) [Mm]akefile , $(SERVER_DIR)) - echo "INCLUDE += -I$(SERVER_DIR)" > $(SERVER_DIR)/$(SERVER_NAME).mk; - -HELP += $(HELP_RELEASE) -endif -#---------------------------------------------------------------------------------- - -clean: - rm -vf $(LOCAL_OBJ) $(TARS_H) $(TARGET) $(TARGETS) ${CLEANFILE} .*.d.tmp gmon.out - -cleanall: - rm -vf $(LOCAL_OBJ) $(PB_TARS_H) $(TARS_H) $(PB_H) $(PB_CPP) $(TARGET) $(TARGETS) $(DEP_FILE) ${CLEANFILE} *.o .*.d.tmp .*.d gmon.out - rm -vf $(TARGET)*.tgz - -HELP += $(HELP_CLEAN) -HELP += $(HELP_CLEANALL) - -ifneq ($(DEP_FILE),) --include $(DEP_FILE) -endif - -HELP_RELEASE = "\n\e[1;33mrelease\e[0m:\t[copy '\e[1;32m$(TARS_SRC) $(TARS_H)\e[0m' -> '/home/tarsproto/$(APP)/$(TARGET)']" -HELP_CLEAN = "\n\e[1;33mclean\e[0m:\t\t[remove $(LOCAL_OBJ) $(TARGET)]" -HELP_CLEANALL = "\n\e[1;33mcleanall\e[0m:\t[clean & rm .*.d]" -HELP_TAR = "\n\e[1;33mtar\e[0m:\t\t[will do 'tar $(TARGET).tgz $(RELEASE_FILE)']" - -help: - @echo -e $(HELP)"\n" - -#---------------------------------------------------------------------------------- - -.%.d: %.cpp - @echo "update $@ ..."; \ - echo -n $< | sed s/\.cpp/\.o:/ > $@; \ - $(CXX) $(CFLAGS) $(INCLUDE) -MM $< | sed '1s/.*.://' >> $@; - -%.o: %.cpp - $(CXX) $(CFLAGS) $(INCLUDE) -o $@ -c $< - -.%.d: %.c - @echo "update $@ ..."; \ - echo -n $< | sed s/\.c/\.o:/ > $@; \ - $(CC) $(INCLUDE) -MM $< | sed '1s/.*.://' >> $@; - -%.o: %.c - $(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $< - -#---------------------------------------------------------------------------------- - diff --git a/cpp/servant/promise/exception_ptr.h b/cpp/servant/promise/exception_ptr.h deleted file mode 100644 index 0535e306b..000000000 --- a/cpp/servant/promise/exception_ptr.h +++ /dev/null @@ -1,235 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PROMISE_EXCEPTION_PTR_H_ -#define __TARS_PROMISE_EXCEPTION_PTR_H_ - -#include "util/tc_shared_ptr.h" -#include "promise_exception.h" -#include -#include -#include -#include - -using namespace std; - -namespace promise -{ -namespace detail -{ - class ExceptionBase : public std::exception - { - public: - ExceptionBase() {} - - virtual ~ExceptionBase() throw() {} - - virtual ExceptionBase* clone() const { return new ExceptionBase(*this); } - - virtual void rethrow() const { throw *this; } - - virtual const char* what() const throw() { return "ExceptionBase"; } - - virtual int getErrCode() const { return -1; } - }; - - class ExceptionBaseImpl : public ExceptionBase - { - public: - ExceptionBaseImpl() {} - - ExceptionBaseImpl(const std::string& s) - : _sInfo(s) - , _error(-1) - {} - - ExceptionBaseImpl(const std::string& s, int err) - : _sInfo(s) - , _error(err) - {} - - virtual ~ExceptionBaseImpl() throw() {} - - virtual ExceptionBase* clone() const - { - return new ExceptionBaseImpl(*this); - } - - virtual void rethrow() const - { - throw *this; - } - - virtual const char* what() const throw() - { - return _sInfo.c_str(); - } - - virtual int getErrCode() const - { - return _error; - } - private: - std::string _sInfo; - int _error; - }; - -} // namespace detail - -typedef TC_SharedPtr ExceptionPtr; - -class CurrentExceptionUnknownException : PromiseException -{ -private: - - const char *name() const - { - return "CurrentExceptionUnknownException"; - } -}; - -/** - * - */ -inline ExceptionPtr currentException() -{ - try - { - try - { - throw; - } - catch (detail::ExceptionBaseImpl& e) - { - return ExceptionPtr(e.clone()); - } - catch (detail::ExceptionBase& e) - { - return ExceptionPtr(e.clone()); - } - catch (std::domain_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::invalid_argument& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::length_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::out_of_range& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::range_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::overflow_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::underflow_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::logic_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_alloc& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_cast& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_typeid& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_exception& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (PromiseException& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::exception& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - } - catch (std::bad_alloc& e) - { - //return detail::ExceptionPtrStaticExceptionObject::e; - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (...) - { - //return detail::ExceptionPtrStaticExceptionObject::e; - return ExceptionPtr(new detail::ExceptionBaseImpl(string("PromiseUnknownException") )); - } -} - - -inline detail::ExceptionBaseImpl enableCurrentException(const string& t) -{ - return detail::ExceptionBaseImpl(t); -} - -inline detail::ExceptionBaseImpl enableCurrentException(const string& t, int err) -{ - return detail::ExceptionBaseImpl(t, err); -} - -template -inline void throwException(const E& e) -{ - throw enableCurrentException(string(e.what(), strlen(e.what()))); -} - -inline ExceptionPtr copyException(const string& e) -{ - try - { - throw enableCurrentException(e); - } - catch (...) - { - return currentException(); - } -} - -inline ExceptionPtr copyException(const string& e, int err) -{ - try - { - throw enableCurrentException(e, err); - } - catch (...) - { - return currentException(); - } -} - -} // namespace promise - -#endif // __EXCEPTION_PTR_H__ diff --git a/cpp/servant/promise/promise.h b/cpp/servant/promise/promise.h deleted file mode 100644 index c6d3b7f97..000000000 --- a/cpp/servant/promise/promise.h +++ /dev/null @@ -1,884 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PROMISE_H_ -#define __TARS_PROMISE_H_ - -#include "util/tc_callback.h" -#include "exception_ptr.h" -#include -#include -#include "promise_exception.h" -#include "util/tc_bind.h" -#include "util/tc_enable_shared_from_this.h" -#include "util/detail/tc_assert.h" -#include "util/detail/tc_template_util.h" -#include "util/tc_monitor.h" - -using namespace tars; - -namespace promise -{ - -class PromiseAlreadySatisfiedException : public PromiseException -{ -public: - - PromiseAlreadySatisfiedException(const char *filename, int line) - : PromiseException(filename, line) - {} - -protected: - - virtual const char *name() const - { - return "PromiseAlreadySatisfiedException"; - } -}; - -class FutureUninitializedException : public PromiseException -{ -public: - FutureUninitializedException(const char *filename, int line) - : PromiseException(filename, line) - {} - -protected: - virtual const char *name() const - { - return "FutureUninitializedException"; - } -}; - -template class Future; - -template struct is_future_type : tars::false_type {}; -template struct is_future_type > : tars::true_type {}; - -template class Promise; - -namespace detail -{ - template - struct resolved_type - { - typedef T type; - }; - - template - struct resolved_type > - { - typedef T type; - }; - - template - struct FutureTraits - { - typedef TC_ScopedPtr storage_type; - typedef const T& rvalue_source_type; - typedef const T& move_dest_type; - typedef T& dest_reference_type; - - static void init(storage_type& storage, rvalue_source_type t) - { - storage.reset(new T(t)); - } - - static void assign(dest_reference_type dest, const storage_type& storage) - { - dest = *storage; - } - }; - - template - struct FutureTraits - { - typedef T* storage_type; - typedef T& rvalue_source_type; - typedef T& move_dest_type; - typedef T*& dest_reference_type; - - static void init(storage_type& storage, T& t) - { - storage = &t; - } - - static void assign(dest_reference_type dest, const storage_type& storage) - { - dest = storage; - } - }; - - template <> - struct FutureTraits - { - typedef void rvalue_source_type; - typedef void move_dest_type; - }; - - template - class FutureObjectInterface - { - public: - typedef typename FutureTraits::rvalue_source_type rvalue_source_type; - typedef typename FutureTraits::move_dest_type move_dest_type; - typedef typename FutureTraits::dest_reference_type dest_reference_type; - typedef TC_Callback&)> CallbackType; - - virtual ~FutureObjectInterface() {} - - virtual bool isDone() const = 0; - virtual bool hasValue() const = 0; - virtual bool hasException() const = 0; - virtual void setValue(rvalue_source_type v) = 0; - virtual void setException(const ExceptionPtr& e) = 0; - virtual move_dest_type get() const = 0; - virtual bool tryGet(dest_reference_type v) const = 0; - virtual void registerCallback(const CallbackType& callback) = 0; - }; - - template <> - class FutureObjectInterface - { - public: - typedef FutureTraits::move_dest_type move_dest_type; - typedef TC_Callback&)> CallbackType; - - virtual ~FutureObjectInterface() {} - - virtual bool isDone() const = 0; - virtual bool hasValue() const = 0; - virtual bool hasException() const = 0; - virtual void set() = 0; - virtual void setException(const ExceptionPtr& e) = 0; - virtual move_dest_type get() const = 0; - virtual void registerCallback(const CallbackType& callback) = 0; - }; - - template - class PromptFutureObject : public FutureObjectInterface, - public TC_EnableSharedFromThis > - { - public: - typedef typename FutureObjectInterface::move_dest_type move_dest_type; - typedef typename FutureObjectInterface::rvalue_source_type rvalue_source_type; - typedef typename FutureObjectInterface::dest_reference_type dest_reference_type; - typedef typename FutureObjectInterface::CallbackType CallbackType; - - PromptFutureObject(rvalue_source_type v) - { - FutureTraits::init(m_value, v); - } - - PromptFutureObject(const ExceptionPtr& e) - : m_exception_ptr(e) - {} - - virtual ~PromptFutureObject() {} - - virtual bool isDone() const { return true; } - - virtual bool hasValue() const - { - if (m_exception_ptr) - return false; - return true; - } - - virtual bool hasException() const { return m_exception_ptr; } - - virtual void setValue(rvalue_source_type /*v*/) {} - virtual void setException(const ExceptionPtr& /*e*/) {} - - virtual move_dest_type get() const - { - TC_ASSERT(m_exception_ptr || m_value); - if (m_exception_ptr) - m_exception_ptr->rethrow(); - return *m_value; - } - - virtual bool tryGet(dest_reference_type v) const { FutureTraits::assign(v, m_value); return true; } - - virtual void registerCallback(const CallbackType& callback) - { - TC_ASSERT(callback); - try - { - callback(this->sharedFromThis()); - } - catch (...) - { - } - } - - private: - typename FutureTraits::storage_type m_value; - ExceptionPtr m_exception_ptr; - }; - - template <> - class PromptFutureObject : public FutureObjectInterface, - public TC_EnableSharedFromThis > - { - public: - typedef FutureObjectInterface::move_dest_type move_dest_type; - typedef FutureObjectInterface::CallbackType CallbackType; - - PromptFutureObject() {} - - PromptFutureObject(const ExceptionPtr& e) - : m_exception_ptr(e) - {} - - virtual ~PromptFutureObject() {} - - virtual bool isDone() const { return true; } - - virtual bool hasValue() const - { - if (m_exception_ptr) - return false; - return true; - } - - virtual bool hasException() const { return m_exception_ptr; } - - virtual void set() {} - virtual void setException(const ExceptionPtr& /*e*/) {} - - virtual move_dest_type get() const - { - if (m_exception_ptr) - m_exception_ptr->rethrow(); - } - - virtual void registerCallback(const CallbackType& callback) - { - TC_ASSERT(callback); - try - { - callback(this->sharedFromThis()); - } - catch (...) - { - } - } - - private: - ExceptionPtr m_exception_ptr; - }; - - struct FutureObjectBase - { - FutureObjectBase() - : m_is_done(false) - {} - - virtual ~FutureObjectBase() {} - - bool isDone() const - { - TC_ThreadLock::Lock lock(m_monitor); - return m_is_done; - } - - bool hasValue() const - { - TC_ThreadLock::Lock lock(m_monitor); - return m_is_done && !m_exception_ptr; - } - - bool hasException() const - { - TC_ThreadLock::Lock lock(m_monitor); - return m_is_done && m_exception_ptr; - } - - void markFinishedWithException(const ExceptionPtr& e) - { - TC_ASSERT(e); - - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - throw PromiseAlreadySatisfiedException(__FILE__, __LINE__); - } - m_exception_ptr = e; - markFinishedInternal(); - } - - void markFinishedInternal() - { - m_is_done = true; - - m_monitor.notifyAll(); - } - - void wait() const - { - { - TC_ThreadLock::Lock lock(m_monitor); - while(!m_is_done) - { - m_monitor.wait(); - } - } - - if (m_exception_ptr) - m_exception_ptr->rethrow(); - } - - TC_ThreadLock m_monitor; - bool m_is_done; - ExceptionPtr m_exception_ptr; - }; - - template - class FutureObject : public FutureObjectInterface, - public TC_EnableSharedFromThis >, - private FutureObjectBase - { - public: - - typedef typename FutureObjectInterface::move_dest_type move_dest_type; - typedef typename FutureObjectInterface::rvalue_source_type rvalue_source_type; - typedef typename FutureObjectInterface::CallbackType CallbackType; - typedef typename FutureObjectInterface::dest_reference_type dest_reference_type; - - FutureObject() {} - - virtual ~FutureObject() {} - - virtual bool isDone() const - { - return FutureObjectBase::isDone(); - } - - virtual bool hasValue() const - { - return FutureObjectBase::hasValue(); - } - - virtual bool hasException() const - { - return FutureObjectBase::hasException(); - } - - virtual move_dest_type get() const - { - wait(); - - TC_ASSERT(m_value); - return *m_value; - } - - virtual bool tryGet(dest_reference_type v) const - { - if (!isDone()) - return false; - if (m_exception_ptr) - m_exception_ptr->rethrow(); - FutureTraits::assign(v, m_value); - return true; - } - - virtual void setException(const ExceptionPtr& e) - { - markFinishedWithException(e); - doPendingCallbacks(); - } - - virtual void setValue(rvalue_source_type t) - { - { - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - throwException(PromiseAlreadySatisfiedException(__FILE__, __LINE__)); - } - m_value.reset(new T(t)); - markFinishedInternal(); - } - doPendingCallbacks(); - } - - virtual void registerCallback(const CallbackType& callback) - { - TC_ASSERT(callback); - - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - lock.release(); - try - { - callback(this->sharedFromThis()); - } - catch (...) - { - } - } - else - { - m_pending_callbacks.push_back(callback); - } - } - - private: - - void doPendingCallbacks() - { - std::list callbacks; - { - TC_ThreadLock::Lock lock(m_monitor); - callbacks.swap(m_pending_callbacks); - } - - for (typename std::list::const_iterator it = callbacks.begin(), - end = callbacks.end(); - it != end; ++it) - { - try - { - (*it)(this->sharedFromThis()); - } - catch (...) - { - - } - } - } - - typename FutureTraits::storage_type m_value; - std::list m_pending_callbacks; - }; - - template <> - class FutureObject : public FutureObjectInterface, - public TC_EnableSharedFromThis >, - private FutureObjectBase - { - public: - - using typename FutureObjectInterface::CallbackType; - - FutureObject() {} - - virtual ~FutureObject() {} - - virtual bool isDone() const - { - return FutureObjectBase::isDone(); - } - - virtual bool hasValue() const - { - return FutureObjectBase::hasValue(); - } - - virtual bool hasException() const - { - return FutureObjectBase::hasException(); - } - - virtual void get() const - { - wait(); - } - - virtual void set() - { - { - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - throwException(PromiseAlreadySatisfiedException(__FILE__, __LINE__)); - } - markFinishedInternal(); - } - - doPendingCallbacks(); - } - - virtual void setException(const ExceptionPtr& e) - { - markFinishedWithException(e); - doPendingCallbacks(); - } - - void registerCallback(const CallbackType& callback) - { - TC_ASSERT(callback); - - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - lock.release(); - try - { - callback(this->sharedFromThis()); - } - catch (...) - { - - } - return; - } - m_pending_callbacks.push_back(callback); - } - - private: - void doPendingCallbacks() - { - std::list callbacks; - { - TC_ThreadLock::Lock lock(m_monitor); - callbacks.swap(m_pending_callbacks); - } - - for (std::list::const_iterator it = callbacks.begin(), - end = callbacks.end(); - it != end; ++it) - { - (*it)(this->sharedFromThis()); - } - } - - std::list m_pending_callbacks; - }; - - template class ForwardValue; - - template - class SequentialCallback - { - private: - typedef typename detail::resolved_type::type value_type; - typedef TC_SharedPtr > FuturePtr; - public: - SequentialCallback(const TC_Callback&)>& callback, - const Promise& promise) - : m_callback(callback) - , m_promise(promise) - {} - - template - typename tc_enable_if >::type run(const FuturePtr& future) - { - try - { - m_callback(future); - m_promise.set(); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - template - typename tc_enable_if_c::value && !is_future_type::value>::type - run(const FuturePtr& future) - { - try - { - m_promise.setValue(m_callback(future)); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - template - typename tc_enable_if >::type run(const FuturePtr& future) - { - try - { - m_callback(future).then(TC_Bind(&ForwardValue::template run, - tc_owned(new ForwardValue(m_promise)))); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - private: - - TC_Callback&)> m_callback; - Promise m_promise; - }; - - template - class ForwardValue - { - public: - ForwardValue(const Promise& promise) - : m_promise(promise) - {} - - template - typename tc_enable_if >::type run(const Future& future) - { - try - { - future.get(); - m_promise.set(); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - template - typename tc_disable_if >::type run(const Future& future) - { - try - { - m_promise.setValue(future.get()); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - private: - Promise m_promise; - }; - - template - class FutureBase - { - private: - typedef typename detail::FutureTraits::move_dest_type move_dest_type; - typedef typename detail::FutureTraits::rvalue_source_type rvalue_source_type; - public: - typedef bool (FutureBase::*unspecified_bool_type)() const; - - FutureBase() {} - - FutureBase(const ExceptionPtr& e) - : m_future(TC_SharedPtr >(new detail::PromptFutureObject(e))) - {} - - virtual ~FutureBase() {} - - move_dest_type get() const - { - if (!m_future) - { - throwException(FutureUninitializedException(__FILE__, __LINE__)); - } - return m_future->get(); - } - - bool isDone() const - { - if (!m_future) - return false; - return m_future && m_future->isDone(); - } - - bool hasValue() const - { - if (!m_future) - return false; - return m_future && m_future->hasValue(); - } - - bool hasException() const - { - if (!m_future) - return false; - return m_future && m_future->hasException(); - } - - operator unspecified_bool_type() const - { - return m_future ? &FutureBase::isDone : NULL; - } - - protected: - - typedef TC_SharedPtr > FuturePtr; - - FutureBase(const FuturePtr& future) - : m_future(future) - {} - - FuturePtr m_future; - }; - -} // namespace detail - -template -class Future : public detail::FutureBase -{ -public: - Future() {} - - explicit Future(typename detail::FutureTraits::rvalue_source_type t) - : detail::FutureBase(TC_SharedPtr >(new detail::PromptFutureObject(t))) - {} - - Future(const ExceptionPtr& e) - : detail::FutureBase(e) - {} - - virtual ~Future() {} - - template - Future::type> then(const tars::TC_Callback& callback) const - { - typedef typename detail::resolved_type::type value_type; - - if (!this->m_future) - { - throwException(FutureUninitializedException(__FILE__, __LINE__)); - } - - Promise promise; - this->m_future->registerCallback(TC_Bind(&detail::SequentialCallback::template run, - tc_owned(new detail::SequentialCallback(callback, promise)))); - return promise.getFuture(); - } - -private: - - Future(const typename detail::FutureBase::FuturePtr& future) - : detail::FutureBase(future) - {} - - template friend class detail::SequentialCallback; - friend class Promise; -}; - -template <> -class Future : public detail::FutureBase -{ -public: - Future() {} - - Future(const ExceptionPtr& e) - : detail::FutureBase(e) - {} - - virtual ~Future() {} - - template - Future::type> then(const tars::TC_Callback& callback) const - { - typedef typename detail::resolved_type::type value_type; - - if (!this->m_future) - { - throwException(FutureUninitializedException(__FILE__, __LINE__)); - } - - Promise promise; - this->m_future->registerCallback(TC_Bind(&detail::SequentialCallback::template run, - tc_owned(new detail::SequentialCallback(callback, promise)))); - return promise.getFuture(); - } - -private: - - Future(const detail::FutureBase::FuturePtr& future) - : detail::FutureBase(future) - {} - - friend Future makeFuture(); - - template friend class detail::SequentialCallback; - friend class Promise; -}; - -template -typename tc_disable_if, Future >::type -makeFuture(typename detail::FutureTraits::rvalue_source_type t) -{ - return Future(t); -} - -inline Future makeFuture() -{ - return Future(TC_SharedPtr >(new detail::PromptFutureObject())); -} - -template -class Promise -{ -public: - - Promise() - : m_future(TC_SharedPtr >(new detail::FutureObject())) - {} - - void setValue(typename detail::FutureTraits::rvalue_source_type v) - { - TC_ASSERT(m_future); - m_future->setValue(v); - } - - void setException(const ExceptionPtr& e) - { - m_future->setException(e); - } - - Future getFuture() const - { - return Future(m_future); - } - -private: - TC_SharedPtr > m_future; -}; - -template <> -class Promise -{ -private: - TC_SharedPtr > m_future; -public: - - Promise() - : m_future(TC_SharedPtr >(new detail::FutureObject())) - {} - - void set() - { - TC_ASSERT(m_future); - m_future->set(); - } - - void setException(const ExceptionPtr& e) - { - m_future->setException(e); - } - - Future getFuture() const - { - return Future(m_future); - } -}; - -} // namespace promise - -#endif // __TARS_PROMISE_H_ diff --git a/cpp/servant/promise/promise_exception.h b/cpp/servant/promise/promise_exception.h deleted file mode 100644 index 727d3db21..000000000 --- a/cpp/servant/promise/promise_exception.h +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PROMISE_EXCEPTION_H_ -#define __TARS_PROMISE_EXCEPTION_H_ - -#include -#include -#include -#include -#include "util/tc_shared_ptr.h" -#include "util/detail/tc_template_util.h" - -namespace promise -{ - -class PromiseException : public std::exception -{ -public: - - PromiseException() - : _filename(NULL) - , _line(0) - , _sep(':') - {} - - PromiseException(const char *filename, int line) - : _filename(filename) - , _line(line) - , _sep(':') - {} - - virtual ~PromiseException() throw() {} - - virtual const char *what() const throw() - { - try - { - if (!_what.unique()) - _what.reset(new std::string(*_what)); - - if (!_what || _what->empty()) - { - std::stringstream ss; - ss << name() << " thrown"; - - if (_filename && _line > 0) - ss << " at " << _filename << ':' << _line; - - if (!_what) - _what.reset(new std::string()); - - const std::string& s = getErrorInfo(); - if (!s.empty()) - { - ss << _sep << ' ' << s; - _sep = ';'; - } - ss.str().swap(*_what); - } - - if (!_data.unique()) - _data.reset(new std::string(*_data)); - - if (_data && !_data->empty()) - { - *_what += _sep; - _sep = ';'; - *_what += ' '; - _what->append(*_data); - _data.reset(); - } - return _what->c_str(); - } - catch (...) - { - - } - return ""; - } - -private: - - virtual const char *name() const - { - return "PromiseException"; - } - - virtual std::string getErrorInfo() const - { - return ""; - } - - const char *_filename; - int _line; - - mutable TC_SharedPtr _what; - mutable TC_SharedPtr _data; - mutable char _sep; - - template - void injectErrorInfo(const T& t) const; - - template - friend typename tc_enable_if, const Exception&>::type - operator<<(const Exception& e, const T& t) - { - e.injectErrorInfo(t); - return e; - } -}; - -template -void PromiseException::injectErrorInfo(const T& t) const -{ - std::stringstream ss; - ss << t; - - if (!_data.unique()) - _data.reset(new std::string(*_data)); - - if (!_data) - { - _data.reset(new std::string(ss.str())); - } - else - { - (*_data) += ss.str(); - } -} - -namespace detail -{ - inline std::string errnoToString(int errorno) - { - return strerror(errorno); - } - -} // namespace detail - -class SyscallException : public PromiseException -{ -public: - - SyscallException(const char *filename, int line, int err) - : PromiseException(filename, line) - , _errno(err) - {} - - int error() const { return _errno; } - -protected: - - virtual const char *name() const - { - return "SyscallException"; - } - - virtual std::string getErrorInfo() const - { - return detail::errnoToString(_errno); - } - -private: - - int _errno; -}; - -class AssertionFailureException : public PromiseException -{ -public: - AssertionFailureException(const char *filename, int line) - : PromiseException(filename, line) - { - } - -private: - virtual const char *name() const - { - return "AssertionFailureException"; - } -}; - -} // namespace promise - -#endif // __TARS_PROMISE_EXCEPTION_H__ diff --git a/cpp/servant/promise/tuple.h b/cpp/servant/promise/tuple.h deleted file mode 100644 index 10f9bd07e..000000000 --- a/cpp/servant/promise/tuple.h +++ /dev/null @@ -1,591 +0,0 @@ -#ifndef __TARS_PROMISE_TUPLE_H_ -#define __TARS_PROMISE_TUPLE_H_ - -#include "util/detail/tc_assert.h" -#include - -namespace promise -{ - namespace detail - { - struct null_type {}; - - inline bool operator==(const null_type&, const null_type&) - { return true; } - - inline bool operator!=(const null_type&, const null_type&) - { return false; } - - inline bool operator<(const null_type&, const null_type&) - { return false; } - - // A helper function to make a const temporary. - inline const null_type make_null() { return null_type(); } - - template - struct Cons - { - typedef HT head_type; - typedef TT tail_type; - - Cons() - : head() - , tail() - {} - - Cons(const head_type& h, const tail_type& t) - : head(h) - , tail(t) - {} - - template - Cons(T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, - T5& t5, T6& t6, T7& t7, T8& t8, T9& t9) - : head(t0) - , tail(t1, t2, t3, t4, t5, t6, t7, t8, t9, make_null()) - {} - - template - Cons(const Cons& o) - : head(o.head) - , tail(o.tail) - {} - - template - Cons& operator=(const Cons& o) - { - head = o.head; - tail = o.tail; - return *this; - } - - void swap(const Cons& o) - { - std::swap(head, o.head); - std::swap(tail, o.tail); - } - - // Because the C++ standard prohibits the function template partial - // specialization, we have to define `operator==()' and `operator!=' as the - // member function. - - bool operator==(const Cons& o) const - { - return head == o.head && tail == o.tail; - } - - bool operator!=(const Cons& o) const - { - return !(operator==(o)); - } - - bool operator<(const Cons& o) const - { - return (head < o.head) || (!(o.head < head) && tail < o.tail); - } - - head_type head; - tail_type tail; - }; - - template - struct Cons - { - typedef HT head_type; - typedef null_type tail_type; - - Cons() - : head() - {} - - template - Cons(T0& t0, const null_type&, const null_type&, const null_type&, const null_type&, - const null_type&, const null_type&, const null_type&, const null_type&, const null_type&) - : head(t0) - {} - - template - Cons& operator=(const Cons& o) - { - head = o.head; - return *this; - } - - void swap(Cons& o) - { - std::swap(head, o.head); - } - - // Because the C++ standard prohibits the function template partial - // specialization, we have to define `operator==()' and `operator!=' as the - // member function. - - bool operator==(const Cons& o) const - { - return head == o.head; - } - - bool operator!=(const Cons& o) const - { - return !(operator==(o)); - } - - bool operator<(const Cons& o) const - { - return head < o.head; - } - - head_type head; - }; - - template - struct map_tuple_to_cons - { - typedef Cons::type> type; - }; - - template <> - struct map_tuple_to_cons - { - typedef null_type type; - }; - - // Gets the N-th tail of a tuple. - template - struct drop_front - { - typedef typename drop_front::type::tail_type type; - static const type& get_tail(const Tuple& tuple) - { - return drop_front::get_tail(tuple).tail; - } - }; - - template - struct drop_front<0, Tuple> - { - typedef Tuple type; - static const type& get_tail(const Tuple& tuple) - { - return tuple; - } - }; - - // Gets the type of the N-th element of a tuple. - template - struct element - { - typedef typename drop_front::type::head_type type; - }; - - // Gets the length of a tuple. - template - struct length - { - static const int value = length::value + 1; - }; - - template <> - struct length - { - static const int value = 0; - }; - - // Helper classes to print Cons. - template - struct print_cons_helper - { - static std::ostream& print(std::ostream& o, const Cons& cons) - { - o << cons.head; - o << ", "; - print_cons_helper::print(o, cons.tail); - return o; - } - }; - - template - struct print_cons_helper - { - static std::ostream& print(std::ostream& o, const Cons& cons) - { - o << cons.head; - return o; - } - }; - - template - inline std::ostream& print_cons(std::ostream& o, const Cons& cons) - { - return print_cons_helper::print(o, cons); - } - - inline std::ostream& print_cons(std::ostream& o, const null_type&) - { - return o; - } - - } // namespace detail - - template - class Tuple - { - private: - typedef typename detail::map_tuple_to_cons::type tuple_impl_type; - - public: - - // Returns the type of the N-th element of this tuple. - template - struct element - { - typedef typename detail::element::type type; - }; - - template - struct access_traits - { - typedef const T& parameter_type; - typedef const T& const_type; - typedef T& non_const_type; - }; - - template - struct access_traits - { - typedef T& parameter_type; - typedef T& const_type; - typedef T& non_const_type; - }; - - // Number of elements of this tuple. - enum - { - LENGTH = detail::length::value - }; - - Tuple() {} - - Tuple(typename access_traits::parameter_type t0) - : m_tuple_impl(t0, detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1) - : m_tuple_impl(t0, t1, detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2) - : m_tuple_impl(t0, t1, t2, detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3) - : m_tuple_impl(t0, t1, t2, t3, detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4) - : m_tuple_impl(t0, t1, t2, t3, t4, detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6, - typename access_traits::parameter_type t7) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6, - typename access_traits::parameter_type t7, - typename access_traits::parameter_type t8) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, t8, detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6, - typename access_traits::parameter_type t7, - typename access_traits::parameter_type t8, - typename access_traits::parameter_type t9) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) - {} - - template - Tuple(const Tuple& o) - : m_tuple_impl(o.m_tuple_impl) - {} - - template - Tuple& operator=(const Tuple& o) - { - m_tuple_impl = o.m_tuple_impl; - return *this; - } - - template - typename access_traits::type>::const_type - get() const - { - TC_STATIC_ASSERT(N < LENGTH, index_out_of_bounds); - return detail::drop_front::get_tail(m_tuple_impl).head; - } - - template - typename access_traits::type>::non_const_type - get() - { - TC_STATIC_ASSERT(N < LENGTH, index_out_of_bounds); - typedef detail::drop_front impl; - typedef typename impl::type cons_element; - return const_cast(impl::get_tail(m_tuple_impl)).head; - } - - void swap(Tuple& o) - { - m_tuple_impl.swap(o.m_tuple_impl); - } - - private: - tuple_impl_type m_tuple_impl; - - template - friend class Tuple; - - friend bool operator==(const Tuple& lhs, const Tuple& rhs) - { - return lhs.m_tuple_impl == rhs.m_tuple_impl; - } - - friend bool operator!=(const Tuple& lhs, const Tuple& rhs) - { - return lhs.m_tuple_impl != rhs.m_tuple_impl; - } - - friend bool operator<(const Tuple& lhs, const Tuple& rhs) - { - return lhs.m_tuple_impl < rhs.m_tuple_impl; - } - - friend std::ostream& operator<<(std::ostream& o, const Tuple& t) - { - o << '('; - detail::print_cons(o, t.m_tuple_impl); - o << ')'; - return o; - } - }; - - namespace detail - { - // Call traits for make_tuple - // - // Conversions: - // T -> T - // T& -> T - // const T& -> T - // T[] -> const T* - // const T[] -> const T* - - template - struct make_tuple_traits - { - typedef T type; - }; - - template - struct make_tuple_traits - { - typedef T type; - }; - - // To produce compilation error intentionally by referencing an non-exsitent - // member of this class. - template - class generate_error {}; - - template - struct make_tuple_traits - { - typedef const T* type; - }; - - template - struct make_tuple_traits - { - typedef const T* type; - }; - - template - struct make_tuple_mapper - { - typedef Tuple::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type> type; - }; - - } // namespace detail - - // make_tuple function templates for arities 0~10 - - inline Tuple<> make_tuple() - { return Tuple<>(); } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6, const T7& t7) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6, const T7& t7, const T8& t8) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7, t8); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9); - } - -} // namespace promise - -#endif // __TARS_PROMISE_TUPLE_H__ diff --git a/cpp/servant/promise/when_all.h b/cpp/servant/promise/when_all.h deleted file mode 100644 index a54b3e1ee..000000000 --- a/cpp/servant/promise/when_all.h +++ /dev/null @@ -1,333 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PROMISE_WHEN_ALL_H_ -#define __TARS_PROMISE_WHEN_ALL_H_ - -#include "promise.h" -#include -#include "tuple.h" -#include "util/tc_monitor.h" -#include "util/tc_shared_ptr.h" -#include "util/detail/tc_assert.h" -namespace promise -{ - namespace detail - { - // Callback implementation for parallel composition of futures. - template - struct ParallelAllCallbackBase - { - public: - typedef Promise PromiseAll; - - ParallelAllCallbackBase(const PromiseAll& promise_all, - size_t number_of_non_satisfied) - : m_number_of_non_satisfied(number_of_non_satisfied) - , m_promise_all(promise_all) - { - } - - virtual ~ParallelAllCallbackBase() - { - } - - size_t m_number_of_non_satisfied; - PromiseAll m_promise_all; - FutureAllValueType m_waiting_futures; - TC_ThreadLock m_monitor; - - private: - ParallelAllCallbackBase(const ParallelAllCallbackBase&); - void operator=(const ParallelAllCallbackBase&); - - }; - - // FutureAllValueType is supposed to be of type promise::Tuple or - // std::vector. - // - // This implementation is for promise::Tuple<>. - template - class ParallelAllCallback : private ParallelAllCallbackBase - { - public: - typedef typename ParallelAllCallbackBase::PromiseAll PromiseAll; - - ParallelAllCallback(const PromiseAll& promise_all) - : ParallelAllCallbackBase(promise_all, FutureAllValueType::LENGTH) - {} - - virtual ~ParallelAllCallback() {} - - template - void on_future(const typename FutureAllValueType::template element::type& future) - { - // Note: this future must be recorded no matter what exception is thrown. - TC_ThreadLock::Lock lock(this->m_monitor); - this->m_waiting_futures.template get() = future; - TC_ASSERT(this->m_number_of_non_satisfied > 0); - if (--this->m_number_of_non_satisfied == 0) - { - lock.release(); - try - { - this->m_promise_all.setValue(this->m_waiting_futures); - } - catch (...) - { - this->m_promise_all.setException(promise::currentException()); - } - return; - } - } - - private: - ParallelAllCallback(const ParallelAllCallback&); - void operator=(const ParallelAllCallback&); - - }; - - // This implementation is specialized for std::vector >. - template - class ParallelAllCallback > : private ParallelAllCallbackBase > - { - public: - typedef typename ParallelAllCallbackBase >::PromiseAll PromiseAll; - - ParallelAllCallback(const PromiseAll& promise_all, - size_t number_of_non_satisfied) - : ParallelAllCallbackBase >(promise_all, number_of_non_satisfied) - { - this->m_waiting_futures.resize(number_of_non_satisfied); - } - - virtual ~ParallelAllCallback() {} - - void on_future(const FutureType& future, size_t idx) - { - // Note: this future must be recorded no matter what exception is thrown. - TC_ASSERT(idx < this->m_waiting_futures.size()); - - TC_ThreadLock::Lock lock(this->m_monitor); - this->m_waiting_futures[idx] = future; - TC_ASSERT(this->m_number_of_non_satisfied > 0); - if (--this->m_number_of_non_satisfied == 0) - { - lock.release(); - try - { - this->m_promise_all.setValue(this->m_waiting_futures); - } - catch (...) - { - this->m_promise_all.setException(promise::currentException()); - } - return; - } - } - - private: - ParallelAllCallback(const ParallelAllCallback&); - void operator=(const ParallelAllCallback&); - - }; - - // A helper class to notify ParallelAllCallback > which future is done. - template - class VectorParallelCallback - { - public: - VectorParallelCallback(const tars::TC_SharedPtr > > >& cb, size_t idx) - : m_parallel_callback(cb), - m_idx(idx) - {} - - void on_future(const Future& future) - { - m_parallel_callback->on_future(future, m_idx); - } - - private: - tars::TC_SharedPtr > > > m_parallel_callback; - size_t m_idx; - }; - - } // namespace detail - - /** - * Parallelly composite two or more futues. Only when all futures are satisfied is - * the composited future satisfied. - */ - template - Future, Future > > - whenAll(const Future& future1, const Future& future2) - { - typedef Tuple, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3) - { - typedef Tuple, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4) - { - typedef Tuple, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5) - { - typedef Tuple, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future, Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5, const Future& future6) - { - typedef Tuple, Future, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future, Future, Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5, const Future& future6, const Future& future7) - { - typedef Tuple, Future, Future, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback))); - future7.then(tars::TC_Bind(&WhenAllCallback::template on_future<6>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future, Future, Future, Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5, const Future& future6, const Future& future7, const Future& future8) - { - typedef Tuple, Future, Future, Future, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - tars::TC_SharedPtr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback))); - future7.then(tars::TC_Bind(&WhenAllCallback::template on_future<6>, tars::tc_shared(future_callback))); - future8.then(tars::TC_Bind(&WhenAllCallback::template on_future<7>, tars::tc_shared(future_callback))); - return promise_all.getFuture(); - } - - template - Future > > - whenAll(InputIterator first, InputIterator last) - { - typedef std::vector > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - if (first == last) - { - promise_all.setValue(FutureAllValueType()); - } - else - { - tars::TC_SharedPtr when_all_callback(new WhenAllCallback(promise_all, std::distance(first, last))); - size_t i; - - for (i = 0; first != last; ++first, ++i) - { - first->then(tars::TC_Bind(&detail::VectorParallelCallback::on_future, - tars::tc_owned(new detail::VectorParallelCallback(when_all_callback, i)))); - } - } - return promise_all.getFuture(); - } - -} // namespace promise - -#endif // __TARS_PROMISE_WHEN_ALL_H__ diff --git a/cpp/servant/protocol/AdminF.tars b/cpp/servant/protocol/AdminF.tars deleted file mode 100644 index 56510bb5c..000000000 --- a/cpp/servant/protocol/AdminF.tars +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - interface AdminF - { - /** - * 关闭æœåŠ¡ - */ - void shutdown(); - - /** - * 通知æœåŠ¡ - */ - string notify(string command); - }; -}; - diff --git a/cpp/servant/protocol/Auth.tars b/cpp/servant/protocol/Auth.tars deleted file mode 100644 index d1c269bcf..000000000 --- a/cpp/servant/protocol/Auth.tars +++ /dev/null @@ -1,115 +0,0 @@ -module tars -{ - -// ÈÏ֤״̬£º -enum AUTH_STATE -{ - AUTH_INIT = -127, // ¼øȨµÄδ³õʼ»¯Öµ - AUTH_SUCC = 0, // ¼øȨ³É¹¦Íê³É - AUTH_WAIT_RSP = 127, // µÈ´ýAUTH·þÎñ¼øȨ·µ»Ø,ÕâÖÖ״̬ϲ»Ó¦¸ÃÊÕµ½¼øȨÇëÇó - - AUTH_PROTO_ERR = -100, // ЭÒé´íÎó - AUTH_WRONG_OBJ = -101, // obj²»´æÔÚ - AUTH_WRONG_CLIENT = -102, // client app.server²»´æÔÚ - AUTH_WRONG_TOKEN = -103, // token not match - AUTH_ERROR = -104, // ¼øȨÆäËü´íÎó - - // ÆäËü¸ºÊý [-1,-99] : ÈÏÖ¤µÄʧ°Ü״̬£¬Óû§×Ô¶¨Òå -}; - -// ÈÏÖ¤ÀàÐÍ£º -enum AUTH_TYPE -{ - AUTH_TYPENONE = 0, // ÎÞ - AUTH_TYPEREMOTE = 1, // ×ßauthserver - - //AUTH_TYPEANONY = 2, // ¼øȨÔÊÐíÄäÃû - //AUTH_TYPEREMOTE = 2, // Òì²½¼øȨ²»ÔÙÖ§³Ö£¬201804 -}; - - -/* -* tokenΨһ±êʶ -*/ -struct TokenKey -{ - 1 require string sApplication; // client's app - 2 require string sServer; // client's server - 3 require string sObjName; // call this obj, nameÊÇÈ«¾ÖΨһµÄ -}; - - -/* -* ¼øȨÇëÇó,´Óclientµ½ÒµÎñserverµ½AUTH -*/ -struct AuthRequest -{ - 1 require TokenKey sKey; - 2 require string sToken; -}; - -/*¼øȨ½á¹ûÊÇint£¬ stateµÄÖµ*/ - -/* -* Õ˺ÅÇëÇó,´Ówebµ½AUTH -*/ -struct TokenRequest -{ - 1 require vector vObjName; // objnameÊÇÈ«¾ÖΨһµÄ -}; - -/* -* Õ˺ſâ,´ÓAUTHµ½web -*/ -struct TokenResponse -{ - 1 require string sObjName; // objnameÊÇÈ«¾ÖΨһµÄ - 2 require map mTokens; // key = Ö÷µ÷·½app.server, value = token -}; -/* -* req ÉêÇëtoken,´Ówebµ½AUTH, web ipÐèÒªÈÏÖ¤£¬×îºÃÊÇ°×Ãûµ¥ -*/ -struct ApplyTokenRequest -{ - 1 require TokenKey sKey; -}; - -/* -* rsp -*/ -struct ApplyTokenResponse -{ - 1 require TokenKey sKey; - 2 require string sToken; -}; - -/* -* req ɾ³ýtoken,´Ówebµ½AUTH, web ipÐèÒªÈÏÖ¤£¬×îºÃÊÇ°×Ãûµ¥ -*/ -struct DeleteTokenRequest -{ - 1 require TokenKey sKey; -}; - - -interface Auth -{ - /** - * ÇëÇóÔ¶³Ì¼øȨ,±»µ÷·þÎñÏòauthserver·¢ËÍtoken£¬authserver·µ»Ø״̬ - **/ - int authProcess(AuthRequest request); - /** - * ÇëÇótokenÕ˺ſâ - **/ - vector getTokens(TokenRequest request); - /** - * ´´½¨token - **/ - ApplyTokenResponse applyToken(ApplyTokenRequest request); - /** - * del token from web - **/ - int deleteToken(DeleteTokenRequest request); -}; - -}; diff --git a/cpp/servant/protocol/BaseF.tars b/cpp/servant/protocol/BaseF.tars deleted file mode 100644 index 1f4f7441c..000000000 --- a/cpp/servant/protocol/BaseF.tars +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - //////////////////////////////////////////////////////////////// - // 定义åè®®çš„ç‰ˆæœ¬å· - - const short TARSVERSION = 0x01; - const short TUPVERSION = 0x03; - - //////////////////////////////////////////////////////////////// - // 定义消æ¯çš„类型 - - // 普通调用 - const byte TARSNORMAL = 0x00; - - // å•å‘调用 - const byte TARSONEWAY = 0x01; - - //////////////////////////////////////////////////////////////// - // TARSå®šä¹‰çš„è¿”å›žç  - - const int TARSSERVERSUCCESS = 0; //æœåŠ¡å™¨ç«¯å¤„ç†æˆåŠŸ - const int TARSSERVERDECODEERR = -1; //æœåŠ¡å™¨ç«¯è§£ç å¼‚常 - const int TARSSERVERENCODEERR = -2; //æœåŠ¡å™¨ç«¯ç¼–ç å¼‚常 - const int TARSSERVERNOFUNCERR = -3; //æœåŠ¡å™¨ç«¯æ²¡æœ‰è¯¥å‡½æ•° - const int TARSSERVERNOSERVANTERR = -4; //æœåŠ¡å™¨ç«¯æ²¡æœ‰è¯¥Servant对象 - const int TARSSERVERRESETGRID = -5; //æœåŠ¡å™¨ç«¯ç°åº¦çŠ¶æ€ä¸ä¸€è‡´ - const int TARSSERVERQUEUETIMEOUT = -6; //æœåŠ¡å™¨é˜Ÿåˆ—超过é™åˆ¶ - const int TARSASYNCCALLTIMEOUT = -7; //异步调用超时 - const int TARSINVOKETIMEOUT = -7; //调用超时 - const int TARSPROXYCONNECTERR = -8; //proxy链接异常 - const int TARSSERVEROVERLOAD = -9; //æœåŠ¡å™¨ç«¯è¶…è´Ÿè½½,超过队列长度 - const int TARSADAPTERNULL = -10; //客户端选路为空,æœåŠ¡ä¸å­˜åœ¨æˆ–者所有æœåŠ¡down掉了 - const int TARSINVOKEBYINVALIDESET = -11; //客户端按set规则调用éžæ³• - const int TARSCLIENTDECODEERR = -12; //客户端解ç å¼‚常 - const int TARSSERVERUNKNOWNERR = -99; //æœåŠ¡å™¨ç«¯ä½ç½®å¼‚常 - - ///////////////////////////////////////////////////////////////// - // 定义按ä½çš„消æ¯çŠ¶æ€ç±»åž‹,å¯å¤åˆ - - const int TARSMESSAGETYPENULL = 0x00; //æ— çŠ¶æ€ - const int TARSMESSAGETYPEHASH = 0x01; //HASHæ¶ˆæ¯ - const int TARSMESSAGETYPEGRID = 0x02; //ç°åº¦æ¶ˆæ¯ - const int TARSMESSAGETYPEDYED = 0x04; //æŸ“è‰²æ¶ˆæ¯ - const int TARSMESSAGETYPESAMPLE = 0x08; //staté‡‡æ ·æ¶ˆæ¯ - const int TARSMESSAGETYPEASYNC = 0x10; //å¼‚æ­¥è°ƒç”¨ç¨‹åº - //const int TARSMESSAGETYPELOADED = 0x20; //æŒ‰è´Ÿè½½å€¼è°ƒç”¨ç¨‹åº - //const int TARSMESSAGETYPESETED = 0x40; //按set规则调用类型,此字段åŽé¢å°†ä¸ä½¿ç”¨ - const int TARSMESSAGETYPESETNAME = 0x80; //按setname规则调用类型 - ///////////////////////////////////////////////////////////////// -}; diff --git a/cpp/servant/protocol/CMakeLists.txt b/cpp/servant/protocol/CMakeLists.txt deleted file mode 100644 index 0b73ec0e3..000000000 --- a/cpp/servant/protocol/CMakeLists.txt +++ /dev/null @@ -1,65 +0,0 @@ - -########################################################################################## -#生æˆtars文件, 并将.h文件copy到servant目录下 - -set(OUT_DEPENDS_LIST) - -#在当å‰ç›®å½•ç”Ÿæˆtars对应的文件 -complice_tars(OUT_DEPENDS_LIST "servant") - -FILE(GLOB SRC_LIST "*.tars") - -set(CLEAN_LIST) - -#将生æˆtars文件copy到对应的æºç ç›®å½• -foreach (FILE ${SRC_LIST}) - - #copy .h文件 - get_filename_component(NAME_WE ${FILE} NAME_WE) - set(TARS_H ${NAME_WE}.h) - set(CP_SOURCE_H_DIR "${PROJECT_SOURCE_DIR}/servant") - - add_custom_command( - OUTPUT ${CP_SOURCE_H_DIR}/${TARS_H} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - COMMAND cp -rf ${TARS_H} ${CP_SOURCE_H_DIR} - COMMENT "cp -rf ${TARS_H} ${CP_SOURCE_H_DIR}") - - #设置ä¾èµ– - list(APPEND OUT_DEPENDS_LIST ${CP_SOURCE_H_DIR}/${TARS_H}) - - #设置需è¦æ¸…除的.h文件 - list(APPEND CLEAN_LIST ${CP_SOURCE_CPP_DIR}/${NAME_WE}.h) - -endforeach () - -#设置需è¦æ¸…除的文件 -set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") - -add_custom_target(COPY-SERVENT-TARS ALL DEPENDS TUP-TARS ${OUT_DEPENDS_LIST}) - -set(TARSPROTOCOLS -AdminF.tars -AdminF.h -Auth.tars -Auth.h -BaseF.tars -BaseF.h -ConfigF.tars -ConfigF.h -EndpointF.tars -EndpointF.h -LogF.tars -LogF.h -NodeF.tars -NodeF.h -NotifyF.tars -NotifyF.h -PropertyF.tars -PropertyF.h -QueryF.tars -QueryF.h -StatF.tars -StatF.h) - -INSTALL(FILES ${TARSPROTOCOLS} DESTINATION /home/tarsproto/protocol/) diff --git a/cpp/servant/protocol/ConfigF.tars b/cpp/servant/protocol/ConfigF.tars deleted file mode 100644 index c824ec57d..000000000 --- a/cpp/servant/protocol/ConfigF.tars +++ /dev/null @@ -1,127 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - struct ConfigInfo - { - //业务å称 - 0 require string appname; - //æœåŠ¡å称 - 1 require string servername; - //é…置文件å称 - 2 require string filename; - //是å¦åªèŽ·å–应用é…ç½®,默认为false,如果为true则servernameå¯ä»¥ä¸ºç©º - 3 require bool bAppOnly=false; - //æœåŠ¡æ‰€åœ¨èŠ‚点(ip) - 4 optional string host; - //set分组å称 - 5 optional string setdivision; - }; - - struct GetConfigListInfo - { - //业务å称 - 0 require string appname; - //æœåŠ¡å称 - 1 optional string servername; - //是å¦åªèŽ·å–应用é…ç½®,默认为false,如果为trueåªæ‹‰å–应用级别的列表,其它级别无效 - 2 optional bool bAppOnly=false; - //æœåŠ¡æ‰€åœ¨èŠ‚点(ip),è¦æœºå™¨çº§åˆ«çš„,请赋值 - 3 optional string host=""; - //set分组å称,需è¦æ‹‰å–set级别的,请赋值 - 4 optional string setdivision=""; - //容器级别,需è¦æ‹‰å–容器级别的,请赋值 - 5 optional string containername=""; - }; - - /** - * config obj to load server's system config. - **/ - interface Config - { - /** - * 读å–é…置文件列表 - * @param app, app name - * @param server, server name - * @param vf, config file name - **/ - int ListConfig(string app, string server, out vector vf); - - /** - * 读å–é…置文件 - * @param app, app name - * @param server, server name - * @param filename, config file name - * @param config, é…置文件内容 - **/ - int loadConfig(string app, string server, string filename, out string config); - - /** - * æ ¹æ®host读å–文件 - * @param appSerever, app.server - * @param filename, config file name - * @param host, ip - * @param config, é…置文件内容 - **/ - int loadConfigByHost(string appServerName, string filename, string host, out string config); - - /** - * 检查é…置文件是å¦æœåŠ¡TC_Configæ ¼å¼ - * @param appSerever, app.server - * @param filename, config file name - * @param host, ip - * @param result, æ示 - * @return int 0: åˆæ³•, -1:éžæ³• - **/ - int checkConfig(string appServerName, string filename, string host, out string result); - - /** - * 读å–æœåŠ¡æ‰€åœ¨èŠ‚点的é…置文件列表 - * - * @param configInfo, ConfigInfo é…置文件信æ¯ï¼Œå¦‚果没有set分组信æ¯ï¼Œè¯·è®¾ç½®setdivision为空"" - * @param vf, config file name - * @return int 0: æˆåŠŸ, -1:失败 - **/ - int ListConfigByInfo(ConfigInfo configInfo, out vector vf); - - /** - * 读å–é…置文件 - * @param configInfo, ConfigInfo é…ç½®æ–‡ä»¶ä¿¡æ¯ - * @param config, é…置文件内容 - * @return int 0: æˆåŠŸ, -1:失败 - **/ - int loadConfigByInfo(ConfigInfo configInfo, out string config); - - /** - * 检查é…置文件是å¦ç¬¦åˆTC_Configæ ¼å¼ - * @param configInfo, ConfigInfo é…ç½®æ–‡ä»¶ä¿¡æ¯ - * @param result, æ示 - * @return int 0: åˆæ³•, -1:éžæ³• - **/ - int checkConfigByInfo(ConfigInfo configInfo, out string result); - - /** - * 获å–æœåŠ¡çš„所有é…置文件列表, - * @param configInfo 支æŒæ‹‰å–应用é…置列表,æœåŠ¡é…置列表,机器é…置列表 - * @param[out] vf 获å–到的文件å称列表 - * @return int 0: æˆåŠŸ, -1:失败 - **/ - int ListAllConfigByInfo(GetConfigListInfo configInfo, out vector vf); - - }; -}; - diff --git a/cpp/servant/protocol/EndpointF.tars b/cpp/servant/protocol/EndpointF.tars deleted file mode 100644 index 25fbb7ce2..000000000 --- a/cpp/servant/protocol/EndpointF.tars +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - /** - * 端å£ä¿¡æ¯ - */ - struct EndpointF - { - 0 require string host; - 1 require int port; - 2 require int timeout; - 3 require int istcp; - 4 require int grid; - 5 optional int groupworkid; - 6 optional int grouprealid; - 7 optional string setId; - 8 optional int qos; - 9 optional int bakFlag; - 11 optional int weight; - 12 optional int weightType; - 13 optional int authType; - }; - key[EndpointF, host, port, timeout, istcp, grid, qos, weight, weightType, authType]; -}; - - diff --git a/cpp/servant/protocol/LogF.tars b/cpp/servant/protocol/LogF.tars deleted file mode 100644 index ba2e623f0..000000000 --- a/cpp/servant/protocol/LogF.tars +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - struct LogInfo - { - //业务å称 - 0 require string appname; - //æœåŠ¡å称 - 1 require string servername; - //日志文件å称 - 2 require string sFilename; - //æ—¶é—´æ ¼å¼ - 3 require string sFormat; - //set分组å称 - 4 optional string setdivision; - // 日志文件å是å¦å¸¦.logåŽç¼€ - 5 optional bool bHasSufix=true; - // 是å¦å…许框架在日志文件å上增加业务相关的标识 - 6 optional bool bHasAppNamePrefix=true; - // 框架内部的日期和时间部分是å¦åŠ ä¸Š[]ç¬¦å· - 7 optional bool bHasSquareBracket=false; - // 日志文件å中用户自定义字符与日期字符间的连接符,默认是"_" - 8 optional string sConcatStr="_"; - // 日志内容项之间的分隔符,默认是"|" - 9 optional string sSepar="|"; - //按天/å°æ—¶/分钟输出日志时的记录类型,例如,按一天:day或者1day;按两å°æ—¶:2hour;按10分钟:10minute - 10 optional string sLogType = ""; - }; - interface Log - { - /** - * 记录远程日志 - * @param app, 应用å称 - * @param server, æœåŠ¡å称 - * @param file, 文件å称 - * @param format, 日志时间格å¼(%Y%m%d) - * @param buffer, 日志内容 - */ - void logger(string app, string server, string file, string format, vector buffer); - - /** - * 记录远程日志 - * @param info, LogInfo - * @param buffer, 日志内容 - */ - void loggerbyInfo(LogInfo info, vector buffer); - }; -}; diff --git a/cpp/servant/protocol/NodeF.tars b/cpp/servant/protocol/NodeF.tars deleted file mode 100644 index 45cf8ac4b..000000000 --- a/cpp/servant/protocol/NodeF.tars +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - struct ServerInfo - { - 0 require string application; - 1 require string serverName; - 2 require int pid; - 3 optional string adapter; - }; - - interface ServerF - { - /** - * å‘node定时上报serverInfo - * @param serverInfo æœåŠ¡çŠ¶æ€ - * @return int - */ - int keepAlive(ServerInfo serverInfo); - - /** - * å‘node上报TARSç‰ˆæœ¬ä¿¡æ¯ - * @param string ç‰ˆæœ¬ä¿¡æ¯ - * @return int - */ - int reportVersion(string app,string serverName,string version); - }; -}; \ No newline at end of file diff --git a/cpp/servant/protocol/NotifyF.tars b/cpp/servant/protocol/NotifyF.tars deleted file mode 100644 index 7d2ca3d3d..000000000 --- a/cpp/servant/protocol/NotifyF.tars +++ /dev/null @@ -1,118 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - -/** -* notifyä¿¡æ¯çš„键值 -*/ -struct NotifyKey -{ - 1 require string name; //为app+servername 例如:Comm.BindServer - 2 require string ip; //ip - 3 require int page; -}; - -/** -* 上报信æ¯çš„å•æ¡å†…容 -*/ -struct NotifyItem -{ - 1 require string sTimeStamp; - 2 require string sServerId; - 3 require int iLevel; - 4 require string sMessage; -}; - -/** -* 上报的信æ¯åˆ†é¡µè®°å½• -*/ -struct NotifyInfo -{ - 1 require int nextpage; - 2 require vector notifyItems; -}; - -/** -* 定义上报信æ¯çš„等级 -*/ -enum NOTIFYLEVEL -{ - NOTIFYNORMAL, - NOTIFYWARN, - NOTIFYERROR, -}; - -/* -*上报类型 -*/ -enum ReportType -{ - REPORT, - NOTIFY -}; - -/* -*上报notifyä¿¡æ¯çš„æ•°æ®ç»“æž„ -*/ -struct ReportInfo -{ - 1 require ReportType eType; - 2 require string sApp; - 3 require string sSet; - 4 require string sContainer; - 5 require string sServer; - 6 require string sMessage; - 7 optional string sThreadId; - 8 optional NOTIFYLEVEL eLevel; - -}; - -interface Notify -{ - /** - * 框架上报的信æ¯, ä¿å­˜äºŽæ•°æ®åº“中 - * @param sServerName, server name - * @param sThreadId, server current thread id - * @param sMessage, message - **/ - void reportServer(string sServerName, string sThreadId, string sMessage); - - - /** - * 业务上报的信æ¯, 用于报警 - * @param sServerName, server name - * @param level, notify level - * @param sMessage, message - **/ - void notifyServer(string sServerName, NOTIFYLEVEL level, string sMessage); - - /** - * 获å–ä¸ŠæŠ¥ä¿¡æ¯ - * @param sServerName, server name - * @param out , notify info detail - * @return int 0=success, others=failed - **/ - int getNotifyInfo(NotifyKey stKey, out NotifyInfo stInfo); - - /* - *上报框架信æ¯ä»¥åŠä¸šåŠ¡å‘Šè­¦ä¿¡æ¯ - */ - void reportNotifyInfo(ReportInfo info); -}; - -}; diff --git a/cpp/servant/protocol/PropertyF.tars b/cpp/servant/protocol/PropertyF.tars deleted file mode 100644 index c983d4a93..000000000 --- a/cpp/servant/protocol/PropertyF.tars +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - -//属性上报消æ¯åŒ… Prop = property -struct StatPropMsgHead -{ - 0 require string moduleName; //模å—name - 1 require string ip; //ip - 2 require string propertyName; //属性name - 3 optional string setName; //setå - 4 optional string setArea; //set地区å - 5 optional string setID; //set组å - 6 optional string sContainer; //容器å - 7 optional int iPropertyVer=1; //å议版本 -}; - -key[StatPropMsgHead, moduleName, ip, propertyName, setName, setArea, setID ]; - -//å±žæ€§å€¼ä¿¡æ¯ -struct StatPropInfo -{ - 0 require string policy; //Sum\Avg\Distr\Count\Max\Min - 1 require string value; //值 -}; - -//属性上报包体 -struct StatPropMsgBody -{ - 0 require vector vInfo; //ä¸ŠæŠ¥ä¿¡æ¯ -}; - -/** - * - * 上报æœåŠ¡çš„æŽ¥å£ - * - **/ -interface PropertyF -{ - /** - * ä¸ŠæŠ¥å±žæ€§ç»Ÿè®¡ä¿¡æ¯ Prop = property - * @param statmsg, ä¸ŠæŠ¥ä¿¡æ¯ - * @return int, 返回0表示æˆåŠŸ - */ - int reportPropMsg( map statmsg ); -}; - - -}; diff --git a/cpp/servant/protocol/QueryF.tars b/cpp/servant/protocol/QueryF.tars deleted file mode 100644 index 64175ea8f..000000000 --- a/cpp/servant/protocol/QueryF.tars +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "EndpointF.tars" - -module tars -{ - /** - * 获å–对象endpointçš„queryæŽ¥å£ - */ - - interface QueryF - { - /** æ ¹æ®id获å–对象 - * - * @param id 对象å称 - * - * @return 返回所有该对象的活动endpoint列表 - */ - vector findObjectById(string id); - - /**æ ¹æ®id获å–所有对象,包括活动和éžæ´»åŠ¨å¯¹è±¡ - * - * @param id 对象å称 - * @param activeEp 存活endpoint列表 - * @param inactiveEp éžå­˜æ´»endpoint列表 - * @return: 0-æˆåŠŸ others-失败 - */ - int findObjectById4Any(string id, out vector activeEp, out vector inactiveEp); - - /** æ ¹æ®id获å–对象所有endpoint列表,功能åŒfindObjectByIdInSameGroup - * - * @param id 对象å称 - * @param activeEp 存活endpoint列表 - * @param inactiveEp éžå­˜æ´»endpoint列表 - * @return: 0-æˆåŠŸ others-失败 - */ - int findObjectById4All(string id, out vector activeEp, out vector inactiveEp); - - /** æ ¹æ®id获å–对象åŒç»„endpoint列表 - * - * @param id 对象å称 - * @param activeEp 存活endpoint列表 - * @param inactiveEp éžå­˜æ´»endpoint列表 - * @return: 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameGroup(string id, out vector activeEp, out vector inactiveEp); - - - /** æ ¹æ®id获å–对象指定归属地的endpoint列表 - * - * @param id 对象å称 - * @param activeEp 存活endpoint列表 - * @param inactiveEp éžå­˜æ´»endpoint列表 - * @return: 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameStation(string id, string sStation, out vector activeEp, out vector inactiveEp); - - /** æ ¹æ®id获å–对象åŒç»„endpoint列表 - * - * @param id 对象å称 - * @param setId set全称,æ ¼å¼ä¸ºsetname.setarea.setgroup - * @param activeEp 存活endpoint列表 - * @param inactiveEp éžå­˜æ´»endpoint列表 - * @return: 0-æˆåŠŸ others-失败 - */ - int findObjectByIdInSameSet(string id, string setId, out vector activeEp, out vector inactiveEp); - - }; - -}; - - diff --git a/cpp/servant/protocol/StatF.tars b/cpp/servant/protocol/StatF.tars deleted file mode 100644 index d793de9c7..000000000 --- a/cpp/servant/protocol/StatF.tars +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - -//模å—间调用包头 Mic = module interval call -struct StatMicMsgHead -{ - 0 require string masterName; //主调模å—name - 1 require string slaveName; //被调模å—name - 2 require string interfaceName; //被调模å—接å£name - 3 require string masterIp; //主调ip - 4 require string slaveIp; //被调ip - 5 require int slavePort; //è¢«è°ƒç«¯å£ - 6 require int returnValue; //返回值 - 7 optional string slaveSetName; //被调setå - 8 optional string slaveSetArea; //被调set地区å - 9 optional string slaveSetID; //被调set组å - 10 optional string tarsVersion; //主调tars版本 -}; - -key[StatMicMsgHead, masterName, slaveName,interfaceName,masterIp,slaveIp,slavePort,returnValue, slaveSetName, slaveSetArea, slaveSetID, tarsVersion]; - -//模å—间调用包体åè®® -struct StatMicMsgBody -{ - 0 require int count; //æˆåŠŸè°ƒç”¨é‡ - 1 require int timeoutCount; //è¶…æ—¶è°ƒç”¨é‡ - 2 require int execCount; //å¼‚å¸¸è°ƒç”¨é‡ - //以下字段针对æˆåŠŸè°ƒç”¨é‡æ‰€ä½œç»Ÿè®¡ - 3 require map intervalCount; //æ—¶é—´é—´éš”å†…è°ƒç”¨é‡ - 4 require long totalRspTime; //调用总时间用æ¥è®¡ç®—å¹³å‡æ—¶é—´ - 5 require int maxRspTime; //最大å“应时间 - 6 require int minRspTime; //最å°å“应时间 -}; - -//模å—é—´è°ƒç”¨é‡‡æ ·ä¿¡æ¯ -struct StatSampleMsg -{ - 0 require string unid; //唯一id值 - 1 require string masterName; //主调模å—name - 2 require string slaveName; //被调模å—name - 3 require string interfaceName; //被调模å—接å£name - 4 require string masterIp; //主调ip - 5 require string slaveIp; //被调ip - 6 require int depth; //深度值 - 7 require int width; //广度值 - 8 require int parentWidth; //父节点广度值 -}; - -struct ProxyInfo -{ - 0 require bool bFromClient;//是å¦æ¥è‡ªå®¢æˆ·ç«¯ -}; -/** - * - * 上报æœåŠ¡çš„æŽ¥å£ - * - **/ -interface StatF -{ - /** - * 上报模å—é—´è°ƒç”¨ä¿¡æ¯ Mic = module interval call - * @param statmsg, ä¸ŠæŠ¥ä¿¡æ¯ - * @param bFromServer, ä¸ŠæŠ¥æº bFromClient: true 客户端å‘起的上报 false æœåŠ¡ç«¯å‘起上报 - * @return int, 返回0表示æˆåŠŸ - */ - int reportMicMsg( map msg, bool bFromClient); - - /** - * 上报模å—é—´è°ƒç”¨é‡‡æ ·ä¿¡æ¯ Mic = module interval call - * @param msg, ä¸ŠæŠ¥ä¿¡æ¯ - * @return int, 返回0表示æˆåŠŸ - */ - int reportSampleMsg(vector msg); -}; - -}; diff --git a/cpp/servant/script/create_http_server.sh b/cpp/servant/script/create_http_server.sh deleted file mode 100755 index 230b0ca19..000000000 --- a/cpp/servant/script/create_http_server.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/sh - -if [ $# -lt 3 ] -then - echo "" - exit 0 -fi - -APP=$1 -SERVER=$2 -SERVANT=$3 - -if [ "$SERVER" == "$SERVANT" ] -then - echo "Error!(ServerName == ServantName)" - exit -1 -fi - -if [ ! -d $APP/$SERVER ] -then - echo "[mkdir: $APP/$SERVER]" - mkdir -p $APP/$SERVER -fi - -echo "[create server: $APP.$SERVER ...]" - -DEMO_PATH=/usr/local/tars/cpp/script/http_demo - -cp $DEMO_PATH/* $APP/$SERVER/ - -cd $APP/$SERVER/ - -SRC_FILE="DemoServer.h DemoServer.cpp DemoServantImp.h DemoServantImp.cpp makefile" - -for FILE in $SRC_FILE -do - cat $FILE | sed "s/DemoServer/$SERVER/g" > $FILE.tmp - mv $FILE.tmp $FILE - - cat $FILE | sed "s/DemoApp/$APP/g" > $FILE.tmp - mv $FILE.tmp $FILE - - cat $FILE | sed "s/DemoServant/$SERVANT/g" > $FILE.tmp - mv $FILE.tmp $FILE -done - -rename "DemoServer" "$SERVER" $SRC_FILE -rename "DemoServant" "$SERVANT" $SRC_FILE - -cd ../../ - -echo "[done.]" diff --git a/cpp/servant/script/create_tars_server.sh b/cpp/servant/script/create_tars_server.sh deleted file mode 100644 index 0acee5d74..000000000 --- a/cpp/servant/script/create_tars_server.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/sh - -if [ $# -lt 3 ] -then - echo "" - exit 0 -fi - -APP=$1 -SERVER=$2 -SERVANT=$3 - -if [ "$SERVER" == "$SERVANT" ] -then - echo "Error!(ServerName == ServantName)" - exit -1 -fi - -if [ ! -d $APP/$SERVER ] -then - echo "[mkdir: $APP/$SERVER]" - mkdir -p $APP/$SERVER -fi - -echo "[create server: $APP.$SERVER ...]" - -DEMO_PATH=/usr/local/tars/cpp/script/demo - -cp $DEMO_PATH/* $APP/$SERVER/ - -cd $APP/$SERVER/ - -SRC_FILE="DemoServer.h DemoServer.cpp DemoServantImp.h DemoServantImp.cpp DemoServant.tars makefile" - -for FILE in $SRC_FILE -do - cat $FILE | sed "s/DemoServer/$SERVER/g" > $FILE.tmp - mv $FILE.tmp $FILE - - cat $FILE | sed "s/DemoApp/$APP/g" > $FILE.tmp - mv $FILE.tmp $FILE - - cat $FILE | sed "s/DemoServant/$SERVANT/g" > $FILE.tmp - mv $FILE.tmp $FILE -done - -rename "DemoServer" "$SERVER" $SRC_FILE -rename "DemoServant" "$SERVANT" $SRC_FILE - -cd ../../ - -echo "[done.]" diff --git a/cpp/servant/script/demo/DemoServant.tars b/cpp/servant/script/demo/DemoServant.tars deleted file mode 100644 index 87f191010..000000000 --- a/cpp/servant/script/demo/DemoServant.tars +++ /dev/null @@ -1,10 +0,0 @@ - -module DemoApp -{ - -interface DemoServant -{ - int test(); -}; - -}; diff --git a/cpp/servant/script/demo/DemoServantImp.cpp b/cpp/servant/script/demo/DemoServantImp.cpp deleted file mode 100644 index eca4ec578..000000000 --- a/cpp/servant/script/demo/DemoServantImp.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "DemoServantImp.h" -#include "servant/Application.h" - -using namespace std; - -////////////////////////////////////////////////////// -void DemoServantImp::initialize() -{ - //initialize servant here: - //... -} - -////////////////////////////////////////////////////// -void DemoServantImp::destroy() -{ - //destroy servant here: - //... -} - diff --git a/cpp/servant/script/demo/DemoServantImp.h b/cpp/servant/script/demo/DemoServantImp.h deleted file mode 100644 index 809aca77e..000000000 --- a/cpp/servant/script/demo/DemoServantImp.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _DemoServantImp_H_ -#define _DemoServantImp_H_ - -#include "servant/Application.h" -#include "DemoServant.h" - -/** - * - * - */ -class DemoServantImp : public DemoApp::DemoServant -{ -public: - /** - * - */ - virtual ~DemoServantImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual int test(tars::TarsCurrentPtr current) { return 0;}; -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/servant/script/demo/DemoServer.cpp b/cpp/servant/script/demo/DemoServer.cpp deleted file mode 100644 index 6dc57de58..000000000 --- a/cpp/servant/script/demo/DemoServer.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "DemoServer.h" -#include "DemoServantImp.h" - -using namespace std; - -DemoServer g_app; - -///////////////////////////////////////////////////////////////// -void -DemoServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".DemoServantObj"); -} -///////////////////////////////////////////////////////////////// -void -DemoServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/servant/script/demo/DemoServer.h b/cpp/servant/script/demo/DemoServer.h deleted file mode 100644 index e3d102d0f..000000000 --- a/cpp/servant/script/demo/DemoServer.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef _DemoServer_H_ -#define _DemoServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class DemoServer : public Application -{ -public: - /** - * - **/ - virtual ~DemoServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -}; - -extern DemoServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/servant/script/demo/makefile b/cpp/servant/script/demo/makefile deleted file mode 100644 index 50d831d2e..000000000 --- a/cpp/servant/script/demo/makefile +++ /dev/null @@ -1,17 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := DemoApp -TARGET := DemoServer -CONFIG := -STRIP_FLAG:= N -TARS2CPP_FLAG:= - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- - -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/cpp/servant/script/http_demo/DemoServantImp.cpp b/cpp/servant/script/http_demo/DemoServantImp.cpp deleted file mode 100644 index edd29dc53..000000000 --- a/cpp/servant/script/http_demo/DemoServantImp.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "DemoServantImp.h" -#include "servant/Application.h" - -using namespace std; - -////////////////////////////////////////////////////// -void DemoServantImp::initialize() -{ - //initialize servant here: - //... -} - -////////////////////////////////////////////////////// -void DemoServantImp::destroy() -{ - //destroy servant here: - //... -} - -int DemoServantImp::doRequest(TarsCurrentPtr current, vector &buffer) -{ - TC_HttpRequest req; - TC_HttpResponse rsp; - - // parse request header - vector v = current->getRequestBuffer(); - string sBuf; - sBuf.assign(&v[0], v.size()); - req.decode(sBuf); - - int ret = doRequest(req, rsp); - - rsp.encode(buffer); - - return ret; -} - -int DemoServantImp::doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp) -{ - string msg = "Hello Tars!"; - rsp.setContentType("html/text"); - rsp.setResponse(msg.c_str(), msg.size()); - return 0; -} diff --git a/cpp/servant/script/http_demo/DemoServantImp.h b/cpp/servant/script/http_demo/DemoServantImp.h deleted file mode 100644 index e174905b7..000000000 --- a/cpp/servant/script/http_demo/DemoServantImp.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _DemoServantImp_H_ -#define _DemoServantImp_H_ - -#include "servant/Application.h" - -/** - * - * - */ -class DemoServantImp : public Servant -{ -public: - /** - * - */ - virtual ~DemoServantImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - int doRequest(TarsCurrentPtr current, vector &buffer); - -private: - int doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp); -}; -///////////////////////////////////////////////////// -#endif diff --git a/cpp/servant/script/http_demo/DemoServer.cpp b/cpp/servant/script/http_demo/DemoServer.cpp deleted file mode 100644 index 9d7d83c92..000000000 --- a/cpp/servant/script/http_demo/DemoServer.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "DemoServer.h" -#include "DemoServantImp.h" - -using namespace std; - -DemoServer g_app; - -struct HttpProtocol -{ - /** - * 解æžhttp请求 - * @param in - * @param out - * - * @return int - */ - static int parseHttp(string &in, string &out) - { - try - { - //判断请求是å¦æ˜¯HTTP请求 - bool b = TC_HttpRequest ::checkRequest(in.c_str(), in.length()); - //完整的HTTP请求 - if(b) - { - out = in; - in = ""; - //TLOGDEBUG("out size: " << out.size() << endl); - return TC_EpollServer::PACKET_FULL; - } - else - { - return TC_EpollServer::PACKET_LESS; - } - } - catch(exception &ex) - { - return TC_EpollServer::PACKET_ERR; - } - - return TC_EpollServer::PACKET_LESS; //表示收到的包ä¸å®Œå…¨ - } - -}; - -///////////////////////////////////////////////////////////////// -void -DemoServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".DemoServantObj"); - addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".DemoServantObj", &HttpProtocol::parseHttp); -} -///////////////////////////////////////////////////////////////// -void -DemoServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/cpp/servant/script/http_demo/DemoServer.h b/cpp/servant/script/http_demo/DemoServer.h deleted file mode 100644 index e3d102d0f..000000000 --- a/cpp/servant/script/http_demo/DemoServer.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef _DemoServer_H_ -#define _DemoServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class DemoServer : public Application -{ -public: - /** - * - **/ - virtual ~DemoServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -}; - -extern DemoServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/cpp/servant/script/http_demo/makefile b/cpp/servant/script/http_demo/makefile deleted file mode 100644 index 50d831d2e..000000000 --- a/cpp/servant/script/http_demo/makefile +++ /dev/null @@ -1,17 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := DemoApp -TARGET := DemoServer -CONFIG := -STRIP_FLAG:= N -TARS2CPP_FLAG:= - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- - -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/cpp/servant/servant/AdapterProxy.h b/cpp/servant/servant/AdapterProxy.h deleted file mode 100644 index 0336a7390..000000000 --- a/cpp/servant/servant/AdapterProxy.h +++ /dev/null @@ -1,327 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_ADAPTER_PROXY_H_ -#define __TARS_ADAPTER_PROXY_H_ - -#include "util/tc_timeout_queue_new.h" -#include "util/tc_timeout_queue_map.h" -#include "servant/Global.h" -#include "servant/EndpointInfo.h" -#include "servant/ObjectProxy.h" -#include "servant/Transceiver.h" -#include "servant/Message.h" -#include "servant/StatReport.h" -#include - -namespace tars -{ -//////////////////////////////////////////////////////////////////////// -/** - * æ¯ä¸ªAdapter对应一个Endpoint,也就是一个æœåŠ¡ç«¯å£ - */ -class AdapterProxy -{ -public: - /** - * 构造函数 - * @param ep - * @param op - */ - AdapterProxy(ObjectProxy * pObjectProxy, const EndpointInfo &ep, Communicator* pCom); - - /** - * æžæž„函数 - */ - ~AdapterProxy(); - - /** - * 调用server端对象方法 - */ - int invoke(ReqMessage * msg); - - /** - * å‘é€è¯·æ±‚ - * å‘é€æŒ¤åŽ‹çš„æ•°æ® - */ - void doInvoke(); - - /** - * server端的å“应包返回 - */ - void finishInvoke(ResponsePacket &rsp); - - /** - * 端å£æ˜¯å¦æœ‰æ•ˆ,当连接全部失效时返回false - * @param bForceConnect : 是å¦å¼ºåˆ¶å‘起连接,为trueæ—¶ä¸å¯¹çŠ¶æ€è¿›è¡Œåˆ¤æ–­å°±å‘起连接 - * @return bool - */ - bool checkActive(bool bForceConnect = false); - - /** - * 记录连接是å¦å¼‚常 - */ - void addConnExc(bool bExc); - - /** - * 处ç†è¶…æ—¶ - */ - void doTimeout(); - - /** - * 处ç†stat - */ - void doStat(map & mStatMicMsg); - - /** - * 处ç†é‡‡æ · - */ - void sample(ReqMessage * msg); - - /** - * 获å–ObjectProxy - */ - inline ObjectProxy * getObjProxy() { return _objectProxy; } - - /** - * 获å–端å£ä¿¡æ¯ - * @return const EndpointInfo& - */ - inline const EndpointInfo & endpoint() const { return _endpoint; } - - /** - * 连接超时的时间 - */ - int getConTimeout(); - - /** - * 连接是å¦è¶…æ—¶ - */ - inline bool isConnTimeout() { return _connTimeout; } - - /** - * 设置连接是å¦è¶…æ—¶ - */ - void setConTimeout(bool bConTimeout); - - /** - * 连接是å¦å¼‚常 - */ - inline bool isConnExc() { return _connExc; } - - /** - * 连接异常的次数 - */ - inline int ConnExcCnt() const { return _connExcCnt; } - - /** - * æœåŠ¡åœ¨ä¸»æŽ§çš„状æ€æ˜¯å¦ä¸ºactive - */ - inline bool isActiveInReg() { return _activeStateInReg; } - - /** - * æœåŠ¡åœ¨ä¸»æŽ§çš„状æ€æ˜¯å¦ä¸ºactive - */ - inline void setActiveInReg(bool bActive) { _activeStateInReg = bActive; } - - /** - * 获å–连接 - * - * @return Transceiver* - */ - inline Transceiver* trans() { return _trans.get(); } - - /** - * 设置节点的é™æ€æƒé‡å€¼ - */ - inline void setWeight(int iWeight) { _staticWeight = iWeight; } - - /** - * 获å–节点的é™æ€æƒé‡å€¼ - */ - inline int getWeight() { return _staticWeight; } - /** - * 获å–id - */ - inline int getId() const { return _id; } - -private: - - /** - * å±è”½ç»“点 - */ - void setInactive(); - -private: - - /** - * 请求的å“åº”å¤„ç† - */ - void finishInvoke(ReqMessage * msg); - - /** - * è¶…æ—¶ç»Ÿè®¡å¤„ç† - */ - void finishInvoke(bool bTimeout); - - /** - * åˆå§‹åŒ–模å—间调用信æ¯çš„头部 - */ - void initStatHead(); - - /** - * 模å—间调用信æ¯çš„统计 - */ - void stat(ReqMessage * msg); - - /** - * 模å—间调用信æ¯çš„åˆå¹¶ - */ - void merge(const StatMicMsgBody& inBody, StatMicMsgBody& outBody); - - /** - * 获å–被调å - */ - string getSlaveName(const string& sSlaveName); - -private: - - /* - * 通信器 - */ - Communicator* _communicator; - - /* - * ObjectProxy - */ - ObjectProxy* _objectProxy; - - /* - * èŠ‚ç‚¹ä¿¡æ¯ - */ - EndpointInfo _endpoint; - - /* - * 收å‘åŒ…å¤„ç† - */ - std::unique_ptr _trans; - - /* - * 超时队列 - */ - std::unique_ptr> _timeoutQueue; - - /* - * èŠ‚ç‚¹åœ¨ä¸»æŽ§çš„å­˜æ´»çŠ¶æ€ - */ - bool _activeStateInReg; - - /* - * 节点当å‰çš„å­˜æ´»çŠ¶æ€ - */ - bool _activeStatus; - - /* - * å•ä½æ—¶é—´å†…的总调用次数 - */ - uint32_t _totalInvoke; - - /* - * å•ä½æ—¶é—´å†…的调用超时次数 - */ - uint32_t _timeoutInvoke; - - /* - * 下一次进行超时比率检测的时间 - */ - time_t _nextFinishInvokeTime; - - /* - * 连续超时的次数 - */ - uint32_t _frequenceFailInvoke; - - /* - * 连续失败的检查时间点 - */ - time_t _frequenceFailTime; - - /* - * 下一次é‡è¯•çš„时间 - */ - time_t _nextRetryTime; - - /* - * 是å¦è¿žæŽ¥è¶…æ—¶ - */ - bool _connTimeout; - - /* - * 是å¦è¿žæŽ¥å¼‚常 - */ - bool _connExc; - - /* - * 连接异常的次数 - */ - uint32_t _connExcCnt; - - /* - * é™æ€æƒé‡å€¼ - */ - int _staticWeight; - - /* - * 超时请求的回包回æ¥åŽï¼Œæ˜¯å¦æ‰“å°è¶…æ—¶çš„æ—¥å¿—ä¿¡æ¯ - */ - bool _timeoutLogFlag; - - /* - * éžå‘é€é˜Ÿåˆ—的大å°é™åˆ¶ï¼Œç”¨äºŽå‘é€è¿‡è½½åˆ¤æ–­ - */ - size_t _noSendQueueLimit; - - /* - * 模å—间调用统计信æ¯çš„headä¿¡æ¯ - */ - StatMicMsgHead _statHead; - - /* - * 模å—间调用统计信æ¯çš„bodyä¿¡æ¯ - */ - map _statBody; - - /* - * 最大采样次数 - */ - uint32_t _maxSampleCount; - - /* - * 采样比率 - */ - int _sampleRate; - - /* - * é‡‡æ ·ä¿¡æ¯ - */ - map > _sample; - - int _id; - static TC_Atomic _idGen; -}; -//////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/AdminServant.h b/cpp/servant/servant/AdminServant.h deleted file mode 100644 index 59887af26..000000000 --- a/cpp/servant/servant/AdminServant.h +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_ADMIN_SERVANT_H -#define __TARS_ADMIN_SERVANT_H - -#include "servant/AdminF.h" - -namespace tars -{ - -class TarsCurrent; -class Application; - -//////////////////////////////////////////////////////////////////////// -/** - * 管ç†Servant - */ -class AdminServant : public AdminF -{ -public: - /** - * 构造函数 - * @param pEpollServer - */ - AdminServant(); - - /** - * æžæž„函数 - */ - virtual ~AdminServant(); - - /** - * åˆå§‹åŒ– - * @return int - */ - virtual void initialize(); - - /** - * 退出 - */ - virtual void destroy(); - - /** - * 关闭æœåŠ¡ - * @param current - */ - void shutdown(TarsCurrentPtr current); - - /** - * 管ç†å‘½ä»¤é€šçŸ¥ - * @param command - */ - string notify(const string &command, TarsCurrentPtr current); - -}; -//////////////////////////////////////////////////////////////// -} - -#endif - - diff --git a/cpp/servant/servant/AppCache.h b/cpp/servant/servant/AppCache.h deleted file mode 100644 index d99dfad93..000000000 --- a/cpp/servant/servant/AppCache.h +++ /dev/null @@ -1,111 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __APP_CACHE_H_ -#define __APP_CACHE_H_ - -#include -#include -#include -#include -#include "util/tc_singleton.h" -#include "util/tc_config.h" -#include "util/tc_timeprovider.h" -#include "util/tc_file.h" -#include "util/tc_thread_mutex.h" -#include "servant/TarsLogger.h" - -#define APPCACHE_ROOT_PATH "/cache" - -using namespace std; - -namespace tars -{ -////////////////////////////////////////////////////////////////////// -/** - * 缓存 - */ -class AppCache : public TC_Singleton, public TC_ThreadMutex -{ -public: - AppCache() - : _lastSynTime(0) - , _synInterval(1000) - { - - } - - ~AppCache() - { - - } - /** - * è®¾ç½®æœ¬åœ°ä¿¡æ¯ - * @param sFilePath, cache文件路径 - * @param iSynInterval(ms), åŒæ­¥é—´éš”时间 - */ - void setCacheInfo(const string &sFile,int32_t iSynInterval = 1000); - - /** - * 从cache文件获å–æœåŠ¡åˆ—表 - * @param sObjName - * @param sDomain - * @return string - */ - string get(const string & sName,const string sDomain = ""/*=APPCACHE_ROOT_PATH*/); - - /** - * 获å–域下é¢çš„å‚数值对 - * ä¸å­˜åˆ™è¿”回空map - * @param path: 域å称, 域标识, 例如: /Main/Domain - * - * @return map - */ - map getDomainMap(const string &path); - - /** - * 设置数æ®åˆ°cache文件 - * @param sName - * @param sValue - * @param sDomain - * @return int - */ - int set(const string &sName,const string &sValue,const string sDomain = ""/*=APPCACHE_ROOT_PATH*/); - -private: - /* - * 缓存文件 - */ - string _file; - - /* - * 缓存文件的内存cache - */ - TC_Config _fileCache; - - /* - * 上次åŒæ­¥æ–‡ä»¶çš„时间 - */ - time_t _lastSynTime; - - /* - * åŒæ­¥çš„时间间隔 - */ - int32_t _synInterval; -}; -////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/AppProtocol.h b/cpp/servant/servant/AppProtocol.h deleted file mode 100644 index a4df695d0..000000000 --- a/cpp/servant/servant/AppProtocol.h +++ /dev/null @@ -1,514 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PROTOCOL_H_ -#define __TARS_PROTOCOL_H_ - -#include -#include -#include -#include -#include "tup/RequestF.h" -#include "tup/tup.h" -#include "servant/BaseF.h" -#include "util/tc_epoll_server.h" - -#if TARS_HTTP2 -#include "nghttp2/nghttp2.h" -#endif - -using namespace std; -using namespace tup; - -namespace tars -{ - -template -T net2host(T len) -{ - switch(sizeof(T)) - { - case sizeof(uint8_t): return len; - case sizeof(uint16_t): return ntohs(len); - case sizeof(uint32_t): return ntohl(len); - } - assert(true); - return 0; -} -////////////////////////////////////////////////////////////////////// -/** - * åè®®è§£æž - */ -class AppProtocol -{ -public: - /** - * 解æžåè®® - * @param in, ç›®å‰çš„buffer - * @param out, 一个完整的包 - * - * @return int, 0表示没有接收完全, 1表示收到一个完整包 - */ - static int parse(string &in, string &out) - { - return parseLen<10000000>(in,out); - } - - template - static int parseLen(string &in, string &out) - { - if(in.length() < sizeof(tars::Int32)) - { - return TC_EpollServer::PACKET_LESS; - } - - tars::Int32 iHeaderLen; - - memcpy(&iHeaderLen, in.c_str(), sizeof(tars::Int32)); - - iHeaderLen = ntohl(iHeaderLen); - - if(iHeaderLen < tars::Int32(sizeof(tars::Int32))|| iHeaderLen > iMaxLength) - { - return TC_EpollServer::PACKET_ERR; - } - - if((int)in.length() < iHeaderLen) - { - return TC_EpollServer::PACKET_LESS; - } - - out = in.substr(sizeof(tars::Int32), iHeaderLen - sizeof(tars::Int32)); - - in = in.substr(iHeaderLen); - - return TC_EpollServer::PACKET_FULL; - } - - /** - * 解æžåè®® - * @param in, ç›®å‰çš„buffer - * @param out, 一个完整的包 - * - * @return int, 0表示没有接收完全, 1表示收到一个完整包 - */ - static int parseAdmin(string &in, string &out); - - /** - * - * @param T - * @param offset - * @param netorder - * @param in - * @param out - * @return int - */ - template - static int parseStream(string& in, string& out) - { - if(in.length() < offset + sizeof(T)) - { - return TC_EpollServer::PACKET_LESS; - } - - T iHeaderLen = 0; - - ::memcpy(&iHeaderLen, in.c_str() + offset, sizeof(T)); - - if (netorder) - { - iHeaderLen = net2host(iHeaderLen); - } - - if (iHeaderLen < (T)(offset + sizeof(T)) || (uint32_t)iHeaderLen > 100000000) - { - return TC_EpollServer::PACKET_ERR; - } - - if (in.length() < (uint32_t)iHeaderLen) - { - return TC_EpollServer::PACKET_LESS; - } - - out = in.substr(0, iHeaderLen); - - in = in.substr(iHeaderLen); - - return TC_EpollServer::PACKET_FULL; - } -}; - -using request_protocol = std::function; -/** - * 接收å议处ç†, 返回值表示解æžäº†å¤šå°‘字节 - * 框架层会自动对处ç†äº†åŒ…åšå¤„ç† - */ -using response_protocol = std::function&)>; -using response_ex_protocol = std::function&, void*)>; - -////////////////////////////////////////////////////////////////////// -/** - * 客户端自定义å议设置 - */ -class ProxyProtocol -{ -public: - /** - * 构造 - */ - ProxyProtocol() : requestFunc(streamRequest) {} - - /** - * 普通二进制请求包 - * @param request - * @param buff - */ - static void streamRequest(const RequestPacket& request, string& buff) - { - buff.assign((const char*)(&request.sBuffer[0]), request.sBuffer.size()); - } - - /** - * 普通二ç¦æ­¢åŒ…æ™®tars请求包 - * @param request - * @param buff - */ - template - < - size_t offset, - typename T, - bool netorder, - size_t idOffset, - typename K, - bool idNetorder, - size_t packetMaxSize - > - static size_t streamResponse(const char* recvBuffer, size_t length, list& done) - { - size_t pos = 0; - - while (pos < length) - { - uint32_t len = length - pos; - - if (len < offset + sizeof(T)) - { - break; - } - - T iHeaderLen = 0; - - ::memcpy(&iHeaderLen, recvBuffer + pos + offset, sizeof(T)); - - if (netorder) - { - iHeaderLen = net2host(iHeaderLen); - } - - //åšä¸€ä¸‹ä¿æŠ¤,长度大于10M - size_t sizeHeaderLen = static_cast(iHeaderLen); - if (sizeHeaderLen > packetMaxSize || iHeaderLen == 0) - { - throw TarsDecodeException("packet length too long or zero,len:(" + TC_Common::tostr(packetMaxSize) + ")" + TC_Common::tostr(iHeaderLen)); - } - - //包没有接收全 - if (len < iHeaderLen) - { - break; - } - - ResponsePacket rsp; - - rsp.sBuffer.reserve(iHeaderLen); - rsp.sBuffer.resize(iHeaderLen); - - ::memcpy(&rsp.sBuffer[0], recvBuffer + pos, iHeaderLen); - - K requestId; - - ::memcpy(&requestId, recvBuffer + pos + idOffset, sizeof(K)); - - if (idNetorder) - { - requestId = net2host(requestId); - } - - rsp.iRequestId = static_cast(requestId); - - done.push_back(rsp); - - pos += iHeaderLen; - } - - return pos; - } - - template - < - size_t offset, - typename T, - bool netorder, - size_t idOffset, - typename K, - bool idNetorder - > - static size_t streamResponse(const char* recvBuffer, size_t length, list& done) - { - return streamResponse(recvBuffer, length, done); - } - - /** - * tupå“应包(tupçš„å“应会放在ResponsePacketçš„buffer中) - * @param request - * @param buff - */ - static size_t tupResponse(const char* recvBuffer, size_t length, list& done) - { - return tupResponseLen<10000000>(recvBuffer,length,done); - } - - template - static size_t tupResponseLen(const char* recvBuffer, size_t length, list& done) - { - size_t pos = 0; - while (pos < length) - { - uint32_t len = length - pos; - - if(len < sizeof(tars::Int32)) - { - break; - } - - uint32_t iHeaderLen = ntohl(*(uint32_t*)(recvBuffer + pos)); - - //åšä¸€ä¸‹ä¿æŠ¤,长度大于10M - if (iHeaderLen > iMaxLength || iHeaderLen < sizeof(tars::Int32)) - { - throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen)); - } - - //包没有接收全 - if (len < iHeaderLen) - { - //看看包头是å¦æ­£ç¡® - static const uint32_t head = 20; - - if(len >= head) - { - TarsInputStream is; - - is.setBuffer(recvBuffer + pos + sizeof(tars::Int32), head); - - //tup回æ¥æ˜¯requestpackage - RequestPacket rsp; - - is.read(rsp.iVersion, 1, true); - - if (rsp.iVersion != TUPVERSION) - { - throw TarsDecodeException("version not correct, version:" + TC_Common::tostr(rsp.iVersion)); - } - - is.read(rsp.cPacketType, 2, true); - - if (rsp.cPacketType != TARSNORMAL) - { - throw TarsDecodeException("packettype not correct, packettype:" + TC_Common::tostr((int)rsp.cPacketType)); - } - - is.read(rsp.iMessageType, 3, true); - is.read(rsp.iRequestId, 4, true); - } - break; - } - else - { - TarsInputStream is; - - //buffer包括4个字节长度 - vector buffer; - - buffer.reserve(iHeaderLen); - buffer.resize(iHeaderLen); - memcpy(&(buffer[0]), recvBuffer + pos, iHeaderLen); - - is.setBuffer(&(buffer[0]) + sizeof(tars::Int32), buffer.size() - sizeof(tars::Int32)); - - pos += iHeaderLen; - - //TUPçš„å“应包其实也是返回包 - RequestPacket req; - req.readFrom(is); - - if (req.iVersion != TUPVERSION ) - { - throw TarsDecodeException("version not correct, version:" + TC_Common::tostr(req.iVersion)); - } - - if (req.cPacketType != TARSNORMAL) - { - throw TarsDecodeException("packettype not correct, packettype:" + TC_Common::tostr((int)req.cPacketType)); - } - - ResponsePacket rsp; - rsp.cPacketType = req.cPacketType; - rsp.iMessageType = req.iMessageType; - rsp.iRequestId = req.iRequestId; - rsp.iVersion = req.iVersion; - rsp.context = req.context; - //tupçš„å“应包直接放入到sBufferé‡Œé¢ - rsp.sBuffer = buffer; - - done.push_back(rsp); - } - } - - return pos; - } - - -public: - /** - * tars请求包 - * @param request - * @param buff - */ - static void tarsRequest(const RequestPacket& request, string& buff); - - /** - * tarså“åº”åŒ…è§£æž - * @param recvBuffer - * @param done - */ - static size_t tarsResponse(const char* recvBuffer, size_t length, list& done) - { - return tarsResponseLen<10000000>(recvBuffer,length,done); - } - - template - static size_t tarsResponseLen(const char* recvBuffer, size_t length, list& done) - { - size_t pos = 0; - while (pos < length) - { - uint32_t len = length - pos; - if(len < sizeof(tars::Int32)) - { - break; - } - - uint32_t iHeaderLen = ntohl(*(uint32_t*)(recvBuffer + pos)); - - //åšä¸€ä¸‹ä¿æŠ¤,长度大于10M - if (iHeaderLen > iMaxLength || iHeaderLen < sizeof(tars::Int32)) - { - throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen)); - } - - //包没有接收全 - if (len < iHeaderLen) - { - //看看包头是å¦æ­£ç¡® - static const uint32_t head = 20; - - if(len >= head) - { - TarsInputStream is; - is.setBuffer(recvBuffer + pos + sizeof(tars::Int32), head); - - ResponsePacket rsp; - is.read(rsp.iVersion, 1, false); - - if (rsp.iVersion != TARSVERSION) - { - throw TarsDecodeException("version not correct, version:" + TC_Common::tostr(rsp.iVersion)); - } - - is.read(rsp.cPacketType, 2, false); - - if (rsp.cPacketType != TARSNORMAL) - { - throw TarsDecodeException("packettype not correct, packettype:" + TC_Common::tostr((int)rsp.cPacketType)); - } - - is.read(rsp.iRequestId, 3, false); - is.read(rsp.iMessageType, 4, false); - is.read(rsp.iRet, 5, false); - - if (rsp.iRet < TARSSERVERUNKNOWNERR) - { - throw TarsDecodeException("response value not correct, value:" + TC_Common::tostr(rsp.iRet)); - } - } - break; - } - else - { - TarsInputStream is; - is.setBuffer(recvBuffer + pos + sizeof(tars::Int32), iHeaderLen - sizeof(tars::Int32)); - pos += iHeaderLen; - - ResponsePacket rsp; - rsp.readFrom(is); - - if (rsp.iVersion != TARSVERSION) - { - throw TarsDecodeException("version not correct, version:" + TC_Common::tostr(rsp.iVersion)); - } - - if (rsp.cPacketType != TARSNORMAL) - { - throw TarsDecodeException("packettype not correct, packettype:" + TC_Common::tostr((int)rsp.cPacketType)); - } - - if (rsp.iRet < TARSSERVERUNKNOWNERR) - { - throw TarsDecodeException("response value not correct, value:" + TC_Common::tostr(rsp.iRet)); - } - - done.push_back(rsp); - } - } - - return pos; - } - -public: - request_protocol requestFunc; - - response_protocol responseFunc; - - response_ex_protocol responseExFunc; -}; - -class TC_NgHttp2; - -void http1Request(const tars::RequestPacket& request, std::string& out); -size_t http1Response(const char* recvBuffer, size_t length, std::list& done); -std::string encodeHttp2(RequestPacket& request, TC_NgHttp2* session); - -// ENCODE function, called by network thread -void http2Request(const tars::RequestPacket& request, std::string& out); - -// DECODE function, called by network thread -size_t http2Response(const char* recvBuffer, size_t length, std::list& done, void* userData); - - -////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/Application.h b/cpp/servant/servant/Application.h deleted file mode 100644 index af0cf4431..000000000 --- a/cpp/servant/servant/Application.h +++ /dev/null @@ -1,452 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_APPLICATION_H -#define __TARS_APPLICATION_H - -#include -#include -#include - -#include "util/tc_autoptr.h" -#include "util/tc_config.h" -#include "util/tc_epoll_server.h" -#include "servant/BaseNotify.h" -#include "servant/ServantHelper.h" -#include "servant/ServantHandle.h" -#include "servant/StatReport.h" -#include "servant/CommunicatorFactory.h" -#include "servant/TarsLogger.h" -#include "servant/TarsConfig.h" -#include "servant/TarsNotify.h" - -namespace tars -{ -////////////////////////////////////////////////////////////////////// -/** - * 以下定义é…置框架支æŒçš„命令 - */ -#define TARS_CMD_LOAD_CONFIG "tars.loadconfig" //从é…置中心, 拉å–é…置下æ¥: tars.loadconfig filename -#define TARS_CMD_SET_LOG_LEVEL "tars.setloglevel" //设置滚动日志的等级: tars.setloglevel [NONE, ERROR, WARN, DEBUG] -#define TARS_CMD_VIEW_STATUS "tars.viewstatus" //查看æœåŠ¡çŠ¶æ€ -#define TARS_CMD_VIEW_VERSION "tars.viewversion" //查看æœåŠ¡é‡‡ç”¨TARS的版本 -#define TARS_CMD_CONNECTIONS "tars.connection" //查看当å‰é“¾æŽ¥æƒ…况 -#define TARS_CMD_LOAD_PROPERTY "tars.loadproperty" //使é…置文件的propertyä¿¡æ¯ç”Ÿæ•ˆ -#define TARS_CMD_VIEW_ADMIN_COMMANDS "tars.help" //帮助查看æœåŠ¡æ”¯æŒçš„管ç†å‘½ä»¤ -#define TARS_CMD_SET_DYEING "tars.setdyeing" //设置染色信æ¯: tars.setdyeing key servant [interface] -#define TARS_CMD_CLOSE_COUT "tars.closecout" //设置是å¦å¯å…³é—­cout\cin\cerr: tars.openthreadcontext yes/NO æœåŠ¡é‡å¯æ‰ç”Ÿæ•ˆ -#define TARS_CMD_SET_DAYLOG_LEVEL "tars.enabledaylog" //设置按天日志是å¦è¾“出: tars.enabledaylog [remote|local]|[logname]|[true|false] -#define TARS_CMD_CLOSE_CORE "tars.closecore" //设置æœåŠ¡çš„core limit: tars.setlimit [yes|no] -#define TARS_CMD_RELOAD_LOCATOR "tars.reloadlocator" //é‡æ–°åŠ è½½locatorçš„é…ç½®ä¿¡æ¯ - -////////////////////////////////////////////////////////////////////// -/** - * 通知信æ¯ç»™notifyæœåŠ¡, 展示在页é¢ä¸Š - */ -//ä¸ŠæŠ¥æ™®é€šä¿¡æ¯ -#define TARS_NOTIFY_NORMAL(info) {TarsRemoteNotify::getInstance()->notify(NOTIFYNORMAL, info);} - -//ä¸ŠæŠ¥è­¦å‘Šä¿¡æ¯ -#define TARS_NOTIFY_WARN(info) {TarsRemoteNotify::getInstance()->notify(NOTIFYWARN, info);} - -//ä¸ŠæŠ¥é”™è¯¯ä¿¡æ¯ -#define TARS_NOTIFY_ERROR(info) {TarsRemoteNotify::getInstance()->notify(NOTIFYERROR, info);} - -//å‘é€å¿ƒè·³ç»™node 多个adapter分别上报 -#define TARS_KEEPALIVE(adapter) {TarsNodeFHelper::getInstance()->keepAlive(adapter);} - -//å‘é€TARS版本给node -#define TARS_REPORTVERSION(x) {TarsNodeFHelper::getInstance()->reportVersion(TARS_VERSION);} - -////////////////////////////////////////////////////////////////////// -/** - * 添加å‰ç½®çš„命令处ç†æ–¹æ³• - * 在所有Normal方法之å‰æ‰§è¡Œ - * 多个å‰ç½®æ–¹æ³•ä¹‹é—´é¡ºåºä¸ç¡®å®š - */ -#define TARS_ADD_ADMIN_CMD_PREFIX(c,f) \ - do { addAdminCommandPrefix(string(c), std::bind(&f, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); } while (0); - -/** - * 添加Normal命令处ç†æ–¹æ³• - * 在所有å‰ç½®æ–¹æ³•æœ€åŽæ‰§è¡Œ - * 多个Normal方法之间顺åºä¸ç¡®å®š - */ -#define TARS_ADD_ADMIN_CMD_NORMAL(c,f) \ - do { addAdminCommandNormal(string(c), std::bind(&f, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); } while (0); - -////////////////////////////////////////////////////////////////////// -/** - * æœåŠ¡åŸºæœ¬ä¿¡æ¯ - */ -struct ServerConfig -{ - static std::string Application; //应用å称 - static std::string ServerName; //æœåŠ¡å称,一个æœåŠ¡å称å«ä¸€ä¸ªæˆ–多个æœåŠ¡æ ‡è¯† - static std::string BasePath; //应用程åºè·¯å¾„,用于ä¿å­˜è¿œç¨‹ç³»ç»Ÿé…置的本地目录 - static std::string DataPath; //应用程åºæ•°æ®è·¯å¾„用于ä¿å­˜æ™®é€šæ•°æ®æ–‡ä»¶ - static std::string LocalIp; //本机IP - static std::string LogPath; //log路径 - static int LogSize; //log大å°(字节) - static int LogNum; //log个数() - static std::string LogLevel; //log日志级别 - static std::string Local; //本地套接字 - static std::string Node; //本机nodeåœ°å€ - static std::string Log; //æ—¥å¿—ä¸­å¿ƒåœ°å€ - static std::string Config; //é…ç½®ä¸­å¿ƒåœ°å€ - static std::string Notify; //ä¿¡æ¯é€šçŸ¥ä¸­å¿ƒ - static std::string ConfigFile; //框架é…置文件路径 - static int ReportFlow; //是å¦æœåŠ¡ç«¯ä¸ŠæŠ¥æ‰€æœ‰æŽ¥å£statæµé‡ 0ä¸ä¸ŠæŠ¥ 1上报(用于éžtarsåè®®æœåŠ¡æµé‡ç»Ÿè®¡) - static int IsCheckSet; //是å¦å¯¹æŒ‰ç…§set规则调用进行åˆæ³•æ€§æ£€æŸ¥ 0,ä¸æ£€æŸ¥ï¼Œ1检查 - static bool OpenCoroutine; //是å¦å¯ç”¨å程处ç†æ–¹å¼ - static size_t CoroutineMemSize; //å程å ç”¨å†…å­˜ç©ºé—´çš„æœ€å¤§å¤§å° - static uint32_t CoroutineStackSize; //æ¯ä¸ªå程的栈大å°(默认128k) -}; - -////////////////////////////////////////////////////////////////////// -/** - * æœåŠ¡çš„基类 - */ -class Application : public BaseNotify -{ -public: - - enum - { - REPORT_SEND_QUEUE_INTERVAL = 10, /**上报æœåŠ¡ç«¯å‘é€é˜Ÿåˆ—大å°çš„间隔时间**/ - }; - -public: - /** - * 应用构造 - */ - Application(); - - /** - * 应用æžæž„ - */ - virtual ~Application(); - - /** - * åˆå§‹åŒ– - * @param argv - */ - void main(int argc, char *argv[]); - - /** - * è¿è¡Œ - */ - void waitForShutdown(); - -public: - /** - * 获å–é…置文件 - * - * @return TC_Config& - */ - static TC_Config& getConfig(); - - /** - * 获å–通信器 - * - * @return CommunicatorPtr& - */ - static CommunicatorPtr& getCommunicator(); - - /** - * 获å–æœåŠ¡Server对象 - * - * @return TC_EpollServerPtr& - */ - static TC_EpollServerPtr& getEpollServer(); - - /** - * 中止应用 - */ - static void terminate(); - - /** - * 添加Config - * @param filename - */ - bool addConfig(const string &filename); - - /** - * 添加应用级的Config - * @param filename - */ - bool addAppConfig(const string &filename); - -protected: - /** - * åˆå§‹åŒ–, åªä¼šè¿›ç¨‹è°ƒç”¨ä¸€æ¬¡ - */ - virtual void initialize() = 0; - - /** - * æžå¤Ÿ, 进程åªä¼šè°ƒç”¨ä¸€æ¬¡ - */ - virtual void destroyApp() = 0; - - /** - * 处ç†åŠ è½½é…置的命令 - * 处ç†å®ŒæˆåŽç»§ç»­é€šçŸ¥Servant - * @param filename - */ - bool cmdLoadConfig(const string& command, const string& params, string& result); - - /** - * 设置滚动日志等级 - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdSetLogLevel(const string& command, const string& params, string& result); - - /** - * 设置æœåŠ¡çš„core limit - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdCloseCoreDump(const string& command, const string& params, string& result); - - /** - * 设置按天日志是å¦ç”Ÿæ•ˆ - * @param command - * @param params [remote|local]|[logname]|[true|false] - * @param result - * - * @return bool - */ - bool cmdEnableDayLog(const string& command, const string& params, string& result); - - /** - * 查看æœåŠ¡çŠ¶æ€ - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdViewStatus(const string& command, const string& params, string& result); - - /** - * æŸ¥çœ‹é“¾æŽ¥çŠ¶æ€ - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdConnections(const string& command, const string& params, string& result); - - /** - * 查看编译的版本 - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdViewVersion(const string& command, const string& params, string& result); - - /** - * 使é…置文件的propertyä¿¡æ¯ç”Ÿæ•ˆ - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdLoadProperty(const string& command, const string& params, string& result); - - /** - *查看æœåŠ¡æ”¯æŒçš„管ç†å‘½ä»¤ - * @param params - * @param result - * - * @return bool - */ - bool cmdViewAdminCommands(const string& command, const string& params, string& result); - - /** - * è®¾ç½®æŸ“è‰²æ¶ˆæ¯ - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdSetDyeing(const string& command, const string& params, string& result); - - - /** - * 设置是å¦å…³é—­stdcout/stderr/stdin æœåŠ¡é‡å¯èƒ½æ‰ç”Ÿæ•ˆ - * @param command - * @param params - * @param result - * - * @return bool - */ - bool cmdCloseCout(const string& command, const string& params, string& result); - - /* - *通过命令动æ€åŠ è½½é…置文件,获å–最新的locator,以方便应对主控便更 - * @param command - * @param params - * @param result - */ - bool cmdReloadLocator(const string& command, const string& params, string& result); - -protected: - - /** - * 为Adapter绑定对应的handle类型 - * 缺çœå®žçŽ°æ˜¯ServantHandle类型 - * @param adapter - */ - virtual void setHandle(TC_EpollServer::BindAdapterPtr& adapter); - - /** - * 添加Servant - * @param T - * @param id - */ - template void addServant(const string &id) - { - ServantHelperManager::getInstance()->addServant(id,true); - } - - /** - * éžtarsåè®®server,设置Servantçš„å议解æžå™¨ - * @param protocol - * @param servant - */ - void addServantProtocol(const string& servant, const TC_EpollServer::protocol_functor& protocol); - - /** - * éžtafåè®®server,设置Servantçš„å议解æžå™¨,å¸¦æœ‰è¿žæŽ¥ä¿¡æ¯ - * @param protocol - * @param servant - */ - void addServantConnProtocol(const string& servant, const TC_EpollServer::conn_protocol_functor& protocol); - /** - *设置Servant的连接断开回调 - */ - void addServantOnClose(const string& servant, const TC_EpollServer::close_functor& f); - -protected: - /** - * 读å–åŸºæœ¬ä¿¡æ¯ - */ - void initializeClient(); - - /** - * 输出 - * @param os - */ - void outClient(ostream &os); - - /** - * åˆå§‹åŒ–servant - */ - void initializeServer(); - - /** - * 输出 - * @param os - */ - void outServer(ostream &os); - - /** - * 输出所有的adapter - * @param os - */ - void outAllAdapter(ostream &os); - - /** - * 输出 - * @param os - */ - void outAdapter(ostream &os, const string &v, TC_EpollServer::BindAdapterPtr lsPtr); - - /** - * 解æžé…置文件 - */ - void parseConfig(int argc, char *argv[]); - - /** - * 解æžipæƒé™allow deny æ¬¡åº - */ - TC_EpollServer::BindAdapter::EOrder parseOrder(const string &s); - - /** - * 绑定serveré…置的Adapter和对象 - */ - void bindAdapter(vector& adapters); - - /** - * @param servant - * @param sPrefix - */ - void checkServantNameValid(const string& servant, const string& sPrefix); - - /** - * æ¢æˆç¼ºçœå€¼ - * @param s - * - * @return string - */ - string toDefault(const string &s, const string &sDefault); - - /** - * 获å–æœåŠ¡çš„set分组信æ¯,setname.setarea.setgroup - * - * @return string 没有按set分组则返回空"" - */ - string setDivision(void); - - /* - * 等待æœåŠ¡é€€å‡º - */ - void waitForQuit(); - -protected: - /** - * é…置文件 - */ - static TC_Config _conf; - - /** - * æœåŠ¡ - */ - static TC_EpollServerPtr _epollServer; - - /** - * 通信器 - */ - static CommunicatorPtr _communicator; -}; -//////////////////////////////////////////////////////////////////// -} - -#endif - diff --git a/cpp/servant/servant/AsyncProcThread.h b/cpp/servant/servant/AsyncProcThread.h deleted file mode 100644 index fe0499898..000000000 --- a/cpp/servant/servant/AsyncProcThread.h +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_ASYNC_PROC_THREAD_H_ -#define __TARS_ASYNC_PROC_THREAD_H_ - -#include "servant/Message.h" -#include "servant/Global.h" - -namespace tars -{ - -////////////////////////////////////////////////////////// -/** - * 异步回调åŽçš„处ç†çº¿ç¨‹ - */ -class AsyncProcThread : public TC_Thread, public TC_HandleBase, public TC_ThreadLock -{ -public: - /** - * 构造函数 - */ - AsyncProcThread(size_t iQueueCap = 10000); - - /** - * æžæž„函数 - */ - virtual ~AsyncProcThread(); - - /** - * 结æŸå¤„ç†çº¿ç¨‹ - */ - void terminate(); - - /** - * æ’å…¥ - */ - void push_back(ReqMessage * msg); - - /** - * 从队列中å–消æ¯åŽæ‰§è¡Œå›žè°ƒé€»è¾‘ - */ - void run(); - - /** - * 获å–å¼‚æ­¥é˜Ÿåˆ—çš„å¤§å° - */ - size_t getSize() - { - return _msgQueue->size(); - } - -private: - /** - * 是å¦éœ€è¦é€€å‡º - */ - bool _terminate; - - /** - * 异步队列 - */ - ReqInfoQueue * _msgQueue; -}; -/////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/BaseNotify.h b/cpp/servant/servant/BaseNotify.h deleted file mode 100644 index 3a81abcbb..000000000 --- a/cpp/servant/servant/BaseNotify.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_BASE_NOTIFY_H_ -#define __TARS_BASE_NOTIFY_H_ - -#include "servant/Global.h" -#include "util/tc_thread_queue.h" -#include "util/tc_thread_mutex.h" -#include - -namespace tars -{ -////////////////////////////////////////////////////////////////////// -/** - * 需è¦æŽ¥æ”¶åˆ°ç®¡ç†å‘½ä»¤çš„对象从该类派生 - */ -class BaseNotify : public TC_ThreadRecMutex, public TC_HandleBase -{ -public: - /** - * 构造函数 - */ - BaseNotify(); - - /** - * æžæž„函数 - */ - virtual ~BaseNotify(); - - /** - * 接收管ç†å‘½ä»¤ - * @param command - * @param params - * @param current - * @param result - * @return bool - */ - bool notify(const string& command, const string& params, TarsCurrentPtr current, string& result); - - /** - * 处ç†å‘½ä»¤çš„函数类型 - * bool: true(继续往åŽé€šçŸ¥å…¶ä»–object),false(通知中止) - * result: 处ç†ç»“æžœæè¿° - */ - using TAdminFunc = std::function; - - /** - * 添加Servant管ç†å‘½ä»¤å’Œå¯¹åº”的处ç†æ–¹æ³• - * @param command - * @param func - */ - void addAdminCommandNormal(const string& command, TAdminFunc func); - - /** - * 添加管ç†å‘½ä»¤å’Œå¯¹åº”的处ç†æ–¹æ³• - * æå‰äºŽServant对象执行收到的命令 - * @param command - * @param func - */ - void addAdminCommandPrefix(const string& command, TAdminFunc func); - -protected: - /** - * 命令处ç†æ–¹æ³• - */ - map _procFunctors; -}; -///////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/Communicator.h b/cpp/servant/servant/Communicator.h deleted file mode 100644 index 40f84c469..000000000 --- a/cpp/servant/servant/Communicator.h +++ /dev/null @@ -1,309 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_COMMUNICATOR_H_ -#define __TARS_COMMUNICATOR_H_ - -#include "util/tc_thread.h" -#include "util/tc_config.h" -#include "util/tc_singleton.h" -#include "servant/Global.h" -#include "servant/ServantProxy.h" -#include "servant/ServantProxyFactory.h" -#include "servant/ObjectProxyFactory.h" -#include "servant/AsyncProcThread.h" -#include "servant/CommunicatorEpoll.h" - -#define CONFIG_ROOT_PATH "/tars/application/client" - -namespace tars -{ -//////////////////////////////////////////////////////////////////////// -/** - * 客户端é…ç½® - */ -struct ClientConfig -{ - /** - * 客户端IPåœ°å€ - */ - static string LocalIp; - /** - * 客户端模å—å称 - */ - static string ModuleName; - /** - * 客户端所有的IPåœ°å€ - */ - static set SetLocalIp; - /** - *客户端是å¦æ‰“å¼€set分组 - */ - static bool SetOpen; - /** - *客户端set分组 - */ - static string SetDivision; - - /** - * 客户端的tarsç‰ˆæœ¬å· - */ - static string TarsVersion; -}; - -//////////////////////////////////////////////////////////////////////// -/** - * 通信器,用于创建和维护客户端proxy - */ -class Communicator : public TC_HandleBase, public TC_ThreadRecMutex -{ -public: - - /** - * 构造函数 - */ - Communicator(); - - /** - * 采用é…置构造 - * @param conf - * @param path - */ - Communicator(TC_Config& conf, const string& domain = CONFIG_ROOT_PATH); - - /** - * æžå¤Ÿ - * æžå¤Ÿæ—¶è‡ªåŠ¨æŽ¥æ”¶ç›¸å…³çº¿ç¨‹ - */ - ~Communicator(); - - /** - * 生æˆä»£ç† - * @param T - * @param objectName - * @param setName 指定set调用的setid - * @return T - */ - template T stringToProxy(const string& objectName,const string& setName="") - { - T prx = NULL; - - stringToProxy(objectName, prx,setName); - - return prx; - } - - /** - * 生æˆä»£ç† - * @param T - * @param objectName - * @param setName 指定set调用的setid - * @param proxy - */ - template void stringToProxy(const string& objectName, T& proxy,const string& setName="") - { - ServantProxy * pServantProxy = getServantProxy(objectName,setName); - proxy = (typename T::element_type*)(pServantProxy); - } - - /* - *获å–客户端网络线程的个数 - */ - inline size_t getClientThreadNum() - { - return _clientThreadNum; - } - - /* - *获å–客户端网络线程的对象 - */ - inline CommunicatorEpoll * getCommunicatorEpoll(size_t iNum) - { - assert(iNum<_clientThreadNum); - return _communicatorEpoll[iNum]; - } - - /** - * 获å–属性 - * @param name - * @param dft, 缺çœå€¼ - * @return string - */ - string getProperty(const string& name, const string& dft = ""); - - /** - * 设置属性 - * @param properties - */ - void setProperty(const map& properties); - - /** - * 设置æŸä¸€ä¸ªå±žæ€§ - * @param name - * @param value - */ - void setProperty(const string& name, const string& value); - - /** - * 设置属性 - * @param conf - * @param path - */ - void setProperty(TC_Config& conf, const string& domain = CONFIG_ROOT_PATH); - - /** - * 上报统计 - * @return StatReport* - */ - StatReport * getStatReport(); - - /** - * é‡æ–°åŠ è½½å±žæ€§ - */ - int reloadProperty(string & sResult); - - /* - * é‡æ–°åŠ è½½locator - */ - void reloadLocator(); - - /** - * 获å–obj对应å¯ç”¨ip port列表 如果å¯ç”¨åˆ†ç»„,åªè¿”回åŒåˆ†ç»„çš„æœåŠ¡ç«¯ip - * @param sObjName - * @return vector - */ - vector getEndpoint(const string & objName); - - /** - * 获å–obj对应å¯ç”¨ip port列表 包括所有IDCçš„ - * @param sObjName - * @return vector - */ - vector getEndpoint4All(const string & objName); - - /** - * ç»“æŸ - */ - void terminate(); - - /** - * 超时请求的回包回æ¥åŽï¼Œæ˜¯å¦æ‰“å°è¶…时的日志信æ¯ï¼ŒAdapterProxy里用到 - */ - bool getTimeoutLogFlag() { return _timeoutLogFlag; } - - /** - * 获å–最å°çš„超时时间 - */ - int64_t getMinTimeout() { return _minTimeout; } - -protected: - /** - * åˆå§‹åŒ– - */ - void initialize(); - - /** - * 是å¦æžæž„中 - * @return bool - */ - bool isTerminating(); - - /** - * ç”±Propertyåˆå§‹åŒ–客户端é…ç½® - */ - void initClientConfig(); - - /** - * 获å–对象代ç†ç”Ÿæˆå™¨ - * @return ServantProxyFactoryPtr - */ - ServantProxyFactory * servantProxyFactory(); - - /** - * 获å–通用对象 - * @param objectName - * @param setName 指定set调用的setid - * @return ServantPrx - */ - ServantProxy * getServantProxy(const string& objectName,const string& setName=""); - - /** - * 框架内部需è¦ç›´æŽ¥è®¿é—®é€šä¿¡å™¨çš„ç±» - */ - friend class AdapterProxy; - - friend class ServantProxy; - - friend class ObjectProxy; - - friend class ServantProxyFactory; - - friend class ObjectProxyFactory; - - friend class AsyncProcThread; - - friend class CommunicatorEpoll; - -protected: - /** - * 是å¦åˆå§‹åŒ– - */ - bool _initialized; - - /** - * åœæ­¢æ ‡å¿— - */ - bool _terminating; - - /** - * 客户端的属性é…ç½® - */ - map _properties; - - /** - * ServantProxy代ç çš„工厂类 - */ - ServantProxyFactoryPtr _servantProxyFactory; - - /* - * 网络线程数组 - */ - CommunicatorEpoll * _communicatorEpoll[MAX_CLIENT_THREAD_NUM]; - - /* - * 网络线程数目 - */ - size_t _clientThreadNum; - - /* - * 上报类 - */ - StatReport * _statReport; - - /* - * 超时请求的回包回æ¥åŽï¼Œæ˜¯å¦æ‰“å°è¶…时的日志信æ¯ï¼ŒAdapterProxy里用到 - */ - bool _timeoutLogFlag; - - /* - * 最å°çš„超时时间 - */ - int64_t _minTimeout; - -}; -//////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/CommunicatorEpoll.h b/cpp/servant/servant/CommunicatorEpoll.h deleted file mode 100644 index d9563e8f9..000000000 --- a/cpp/servant/servant/CommunicatorEpoll.h +++ /dev/null @@ -1,329 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS__COMMUNICATOR_EPOLL_H_ -#define __TARS__COMMUNICATOR_EPOLL_H_ - -#include "util/tc_thread.h" -#include "util/tc_thread_mutex.h" -#include "util/tc_epoller.h" -#include "util/tc_loop_queue.h" -#include "servant/Message.h" -#include - -namespace tars -{ - -class Communicator; -class ObjectProxy; -class ObjectProxyFactory; -class StatReport; -class PropertyReport; - -//////////////////////////////////////////////////////////////////////// -/** - * 监å¬FD事件并触å‘注册的handle - */ -struct FDInfo -{ - enum - { - ET_C_NOTIFY = 1, - ET_C_NET = 2, - }; - - /** - * 构造函数 - */ - FDInfo() - : iSeq(0) - , fd(-1) - , iType(ET_C_NOTIFY) - , p(NULL) - { - } - - /** - * æžæž„函数 - */ - ~FDInfo() - { - } - - size_t iSeq; - - int fd; - - int iType; - - void * p; -}; - -//////////////////////////////////////////////////////////////////////// -/** - * 客户端网络处ç†çš„线程类 - */ -class CommunicatorEpoll : public TC_Thread ,public TC_ThreadRecMutex -{ -public: - struct NotifyInfo - { - /** - * 构造函数 - */ - NotifyInfo() - : eventFd(-1) - , bValid(false) - { - } - - /** - * æžæž„函数 - */ - ~NotifyInfo() - { - } - - - FDInfo stFDInfo; //通知FDä¿¡æ¯ - - TC_Socket notify; //通知fd - - int eventFd; //eventfd,ç›®å‰æœªä½¿ç”¨ - - bool bValid; //是å¦æœ‰æ•ˆ - }; - - /** - * 构造函数 - */ - CommunicatorEpoll(Communicator * pCommunicator, size_t _netThreadSeq); - - /** - * æžæž„函数 - */ - virtual ~CommunicatorEpoll(); - - /** - * èŽ·å– Communicator - */ - inline Communicator * getCommunicator() - { - return _communicator; - } - - /** - * èŽ·å– ObjectProxyFactory - */ - inline ObjectProxyFactory * getObjectProxyFactory() - { - return _objectProxyFactory; - } - - /** - * èŽ·å– ç½‘ç»œçº¿ç¨‹id - */ - inline size_t getCommunicatorEpollId() - { - return _netThreadSeq; - } - - /** - * èŽ·å– èŠ‚ç‚¹é˜Ÿåˆ—æœªå‘é€è¯·æ±‚的大å°é™åˆ¶ - */ - inline size_t getNoSendQueueLimit() - { - return _noSendQueueLimit; - } - - /* - * 判断是å¦æ˜¯ç¬¬ä¸€ä¸ªç½‘络线程 主控写缓存的时候用到 - */ - inline bool isFirstNetThread() - { - return (_netThreadSeq == 0); - } - - /* - * 获å–本epoll的代ç†å¯¹è±¡ - */ - ObjectProxy * getObjectProxy(const string & sObjectProxyName,const string& setName=""); - - /** - * 循环监å¬ç½‘络事件 - */ - virtual void run(); - - /** - * ä¸­æ­¢ç›‘å¬ - */ - void terminate(); - - /** - * 注册fd对应的处ç†handle - * @param fd - * @param info 事件指针 - * @param event - * @param handle - */ - void addFd(int fd,FDInfo * info, uint32_t events); - - /** - * å–消已注册的handle - * @param fd - * @param info 事件指针 - * @param event - * @param handle - */ - void delFd(int fd,FDInfo * info, uint32_t events); - - /** - * é€šçŸ¥äº‹ä»¶è¿‡æ¥ - * @param fd - */ - void notify(size_t iSeq,ReqInfoQueue * pReqQueue); - - /** - * é€šçŸ¥åˆ é™¤äº‹ä»¶è¿‡æ¥ - * @param iSeq - */ - void notifyDel(size_t iSeq); - - /** - * æ•°æ®åŠ å…¥åˆ°å¼‚æ­¥çº¿ç¨‹é˜Ÿåˆ—é‡Œé¢ - * @return - */ - void pushAsyncThreadQueue(ReqMessage * msg); - -protected: - /** - * 处ç†å‡½æ•° - * - * @param pFDInfo - * @param events - */ - void handle(FDInfo * pFDInfo, uint32_t events); - - /** - * 输入事件 - * @param pi - */ - void handleInputImp(Transceiver * pTransceiver); - - /** - * 输出事件 - * @param pi - */ - void handleOutputImp(Transceiver * pTransceiver); - - /** - * 处ç†è¶…æ—¶ - * @param pi - */ - void doTimeout(); - - /** - * 处ç†stat - * @param pi - */ - void doStat(); - -protected: - /* - * 通信器 - */ - Communicator * _communicator; - - /* - * 请求事件通知数组 - */ - NotifyInfo _notify[MAX_CLIENT_NOTIFYEVENT_NUM]; - - /* - * 关闭线程请求的事件通知 - */ - TC_Socket _shutdown; - - /* - * 线程是å¦ç»ˆæ­¢ - */ - bool _terminate; - - /* - * epoll - */ - TC_Epoller _ep; - - /* - * 下次检查超时的时间 - */ - int64_t _nextTime; - - /* - * 下次上报数æ®çš„时间 - */ - int64_t _nextStatTime; - - /* - * ObjectProxy的工厂类 - */ - ObjectProxyFactory * _objectProxyFactory; - - /* - * 异步线程数组 - */ - AsyncProcThread * _asyncThread[MAX_CLIENT_ASYNCTHREAD_NUM]; - - /* - * 异步线程数目 - */ - size_t _asyncThreadNum; - - /* - * 分å‘给异步线程的索引seq - */ - size_t _asyncSeq; - - /* - * 网络线程的idå· - */ - size_t _netThreadSeq; - - /* - * 异步队列的统计上报的对象 - */ - PropertyReport * _reportAsyncQueue; - - /* - * 节点ip队列未å‘é€è¯·æ±‚的大å°é™åˆ¶ - */ - size_t _noSendQueueLimit; - - /* - * epoll wait的超时时间 - */ - int64_t _waitTimeout; - - /* - * 超时的检查时间间隔 - */ - int64_t _timeoutCheckInterval; -}; - -///////////////////////////////////////////////////////////////////////////////////// -} - -#endif diff --git a/cpp/servant/servant/CommunicatorFactory.h b/cpp/servant/servant/CommunicatorFactory.h deleted file mode 100644 index 746bf7250..000000000 --- a/cpp/servant/servant/CommunicatorFactory.h +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_COMMUNICATOR_FACTORY_H_ -#define __TARS_COMMUNICATOR_FACTORY_H_ - -#include "servant/Global.h" -#include "servant/Communicator.h" - -namespace tars -{ -////////////////////////////////////////////////////////////////////////////// -/** - * 创建CommunicatorPtr对象 - */ -class CommunicatorFactory : public TC_Singleton, public TC_HandleBase, public TC_ThreadRecMutex -{ -public: - /** - * 构造函数 - * @param comm - */ - CommunicatorFactory(){}; - - /** - * æžæž„ - */ - ~CommunicatorFactory(){}; - - /** - * 获å–CommunicatorPtr对象 - * @param name - * @return ServantPrx - */ - CommunicatorPtr getCommunicator(const string& name = "default") - { - TC_LockT lock(*this); - - map::iterator it = _comms.find(name); - - if (it == _comms.end()) - { - _comms[name] = new Communicator(); - - it = _comms.find(name); - } - return it->second; - } - - /** - * 获å–CommunicatorPtr对象 - * @param conf - * @param name - * @return ServantPrx - */ - CommunicatorPtr getCommunicator(TC_Config& conf, const string& name = "default") - { - TC_LockT lock(*this); - - map::iterator it = _comms.find(name); - - if (it == _comms.end()) - { - _comms[name] = new Communicator(conf); - - it = _comms.find(name); - - return it->second; - } - - string s = ""; - - it->second->setProperty(conf); - - it->second->reloadProperty(s); - - return it->second; - } - -private: - - /** - * 已创建的对象 - */ - map _comms; -}; -////////////////////////////////////////////////////// -} - -#endif diff --git a/cpp/servant/servant/CoroutineScheduler.h b/cpp/servant/servant/CoroutineScheduler.h deleted file mode 100644 index ade6e4336..000000000 --- a/cpp/servant/servant/CoroutineScheduler.h +++ /dev/null @@ -1,715 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _COROUTINES_SCHEDULLER_H_ -#define _COROUTINES_SCHEDULLER_H_ - -#include -#include -#include -#include -#include -#include "util/tc_fcontext.h" -#include "util/tc_thread_queue.h" -#include "util/tc_monitor.h" -#include -#include "util/tc_thread.h" - -using namespace std; - -namespace tars -{ -class ServantHandle; - -///////////////////////////////////////////// -/** - * åç¨‹ä½¿ç”¨çš„æ ˆå†…å®¹ä¿¡æ¯ - */ -struct stack_context -{ - std::size_t size; - void* sp; - - stack_context() - : size(0) - , sp(0) - {} -}; - -///////////////////////////////////////////// -/** - * å程使用的栈内存分é…器 - */ -class standard_stack_allocator -{ -public: - static bool is_stack_unbound(); - - static std::size_t default_stacksize(); - - static std::size_t minimum_stacksize(); - - static std::size_t maximum_stacksize(); - - int allocate( stack_context &, std::size_t); - - void deallocate( stack_context &); -}; - -///////////////////////////////////////////// -/** - * å程的状æ€ä¿¡æ¯ - */ -enum CORO_STATUS -{ - CORO_FREE = 0, - CORO_ACTIVE = 1, - CORO_AVAIL = 2, - CORO_INACTIVE = 3, - CORO_TIMEOUT = 4 -}; - -///////////////////////////////////////////// -/* - * å程内部使用的函数 - */ -typedef void( * Func)(void *); - -struct CoroutineFunc -{ - Func coroFunc; - void* args; -}; - -class CoroutineScheduler; - -/////////////////////////////////////////// -/** - * å程信æ¯ç±» - */ -class CoroutineInfo -{ -public: - /** - * 链表åˆå§‹åŒ– - */ - static inline void CoroutineHeadInit(CoroutineInfo *coro) - { - coro->_next = coro; - coro->_prev = coro; - } - - /** - * 链表是å¦ä¸ºç©º - */ - static inline bool CoroutineHeadEmpty(CoroutineInfo *coro_head) - { - return coro_head->_next == coro_head; - } - - /** - * æ’å…¥ - */ - static inline void __CoroutineAdd(CoroutineInfo *coro, CoroutineInfo *prev, CoroutineInfo *next) - { - next->_prev = coro; - coro->_next = next; - coro->_prev = prev; - prev->_next = coro; - } - - /** - * æ’入头部 - */ - static inline void CoroutineAdd(CoroutineInfo *new_coro, CoroutineInfo *coro_head) - { - __CoroutineAdd(new_coro, coro_head, coro_head->_next); - } - - /** - * æ’入尾部 - */ - static inline void CoroutineAddTail(CoroutineInfo *new_coro, CoroutineInfo *coro_head) - { - __CoroutineAdd(new_coro, coro_head->_prev, coro_head); - } - - /** - * 删除 - */ - static inline void __CoroutineDel(CoroutineInfo * prev, CoroutineInfo * next) - { - next->_prev = prev; - prev->_next = next; - } - - /** - * 删除 - */ - static inline void CoroutineDel(CoroutineInfo *coro) - { - __CoroutineDel(coro->_prev, coro->_next); - coro->_next = NULL; - coro->_prev = NULL; - } - - /** - * 从一个链表移动到å¦å¤–一个链表头部 - */ - static inline void CoroutineMove(CoroutineInfo *coro, CoroutineInfo *coro_head) - { - CoroutineDel(coro); - CoroutineAdd(coro, coro_head); - } - - /** - * 从一个链表移动到å¦å¤–一个链表尾部 - */ - static inline void CoroutineMoveTail(CoroutineInfo *coro, CoroutineInfo *coro_head) - { - CoroutineDel(coro); - CoroutineAddTail(coro, coro_head); - } - -protected: - - /** - * å程的入å£å‡½æ•° - */ - static void corotineEntry(intptr_t q); - - /** - * 在å程里执行实际逻辑的入å£å‡½æ•° - */ - static void corotineProc(void * args); - -public: - /** - * 构造函数 - */ - CoroutineInfo(); - - /** - * 构造函数 - */ - CoroutineInfo(CoroutineScheduler* scheduler); - - /** - * 构造函数 - */ - CoroutineInfo(CoroutineScheduler* scheduler, uint32_t iUid, stack_context stack_ctx); - - /** - * æžæž„函数 - */ - ~CoroutineInfo(); - - /** - * 注册å程实际的处ç†å‡½æ•° - */ - void registerFunc(const std::function &callback); - - /** - * 设置å程的内存空间 - */ - void setStackContext(stack_context stack_ctx); - - /** - * 获å–å程的内存空间 - */ - inline stack_context& getStackContext() { return _stack_ctx; } - - /** - * 获å–å程所处的调度器 - */ - inline CoroutineScheduler* getScheduler() { return _scheduler; } - - /** - * 获å–å程的标志 - */ - inline uint32_t getUid() { return _uid; } - - /** - * 设置å程的标志 - */ - inline void setUid(uint32_t iUid) { _uid = iUid; } - - /** - * 获å–åç¨‹çš„çŠ¶æ€ - */ - inline CORO_STATUS getStatus() { return _eStatus; } - - /** - * 设置åç¨‹çš„çŠ¶æ€ - */ - inline void setStatus(CORO_STATUS status) { _eStatus = status; } - - /** - * 获å–å程所处的上下文 - */ - inline fcontext_t* getCtx() { return (!_main ? _ctx_to : &_ctx_from); } - -public: - /* - * åŒå‘链表指针 - */ - CoroutineInfo* _prev; - CoroutineInfo* _next; - -private: - /* - * 是å¦æ˜¯ä¸»å程 - */ - bool _main; - - /* - * å程所属的调度器 - */ - CoroutineScheduler* _scheduler; - - /*I - * å程的标识 - */ - uint32_t _uid; - - /* - * åç¨‹çš„çŠ¶æ€ - */ - CORO_STATUS _eStatus; - - /* - * å程的内存空间 - */ - stack_context _stack_ctx; - - /* - * 创建å程åŽï¼Œå程所在的上下文 - */ - fcontext_t* _ctx_to; - - /* - * 创建å程å‰çš„上下文 - */ - fcontext_t _ctx_from; - - /* - * å程åˆå§‹åŒ–函数入å£å‡½æ•° - */ - CoroutineFunc _init_func; - - /* - * å程具体执行函数 - */ - std::function _callback; -}; - -/////////////////////////////////////////// -/** - * å程调度类 - */ -class CoroutineScheduler -{ -public: - - /** - * 构造函数 - */ - CoroutineScheduler(); - - /** - * æžæž„函数 - */ - ~CoroutineScheduler(); - - /** - * åˆå§‹åŒ–å程池的大å°ã€ä»¥åŠåç¨‹çš„å †æ ˆå¤§å° - */ - void init(uint32_t iPoolSize, size_t iStackSize); - - /** - * 创建å程 - */ - uint32_t createCoroutine(const std::function &callback); - - /** - * 在用户自己起的线程中使用å程时,用到的å程调度 - */ - void run(); - - /** - * tars框架业务线程使用å程时,用的å程调度 - */ - void tars_run(); - - /** - * 当å‰å程放弃继续执行 - */ - void yield(bool bFlag = true); - - /** - * 当å‰å程休眠iSleepTime时间(å•ä½:毫秒),然åŽä¼šè¢«å”¤é†’继续执行 - */ - void sleep(int iSleepTime); - - /** - * 放入需è¦å”¤é†’çš„å程 - */ - void put(uint32_t iCoroId); - - /** - * åç¨‹åˆ‡æ¢ - */ - void switchCoro(CoroutineInfo *from, CoroutineInfo *to); - - /** - * åœæ­¢ - */ - void terminate(); - - /** - * 资æºé”€æ¯ - */ - void destroy(); - - /** - * 获å–最大的å程数目 - */ - inline uint32_t getPoolSize() { return _poolSize; } - - /** - * 获å–当å‰å·²ç»åˆ›å»ºçš„å程数目 - */ - inline uint32_t getCurrentSize() { return _currentSize; } - - /** - * 获å–请求å“应回æ¥çš„å程数目 - */ - inline size_t getResponseCoroSize() { return _activeCoroQueue.size(); } - - /** - * 获å–框架业务线程的Handle - */ - inline ServantHandle* getHandle() { return _handle; } - - /** - * 设置框架业务线程的Handle - */ - inline void setHandle(ServantHandle* handle) { _handle = handle; } - - /** - * 获å–ç†è®ºä¸Šç©ºé—²çš„å程数目 - */ - inline uint32_t getFreeSize() { return _poolSize - _usedSize; } - - /** - * å‡å°‘正在使用的å程数目 - */ - inline void decUsedSize() { --_usedSize; } - - /** - * 增加正在使用的å程数目 - */ - inline void incUsedSize() { ++_usedSize; } - - /** - * 调度器中的主å程 - */ - inline CoroutineInfo& getMainCoroutine() { return _mainCoro; } - - /** - * 当å‰å程的标识Id - */ - inline uint32_t getCoroutineId() { return _currentCoro->getUid(); } - - friend class CoroutineInfo; - -private: - /** - * 产生å程id - */ - uint32_t generateId(); - - /** - * 增加åç¨‹æ± çš„å¤§å° - */ - int increaseCoroPoolSize(); - - /** - * 唤醒需è¦è¿è¡Œçš„å程 - */ - void wakeup(); - - /** - * 唤醒自己放弃è¿è¡Œçš„å程 - */ - void wakeupbyself(); - - /** - * 自己放弃è¿è¡Œæ—¶ï¼Œç”¨åˆ° - */ - void putbyself(uint32_t iCoroId); - - /** - * 唤醒休眠的å程 - */ - void wakeupbytimeout(); - - /** - * 放到activeçš„å程链表中 - */ - void moveToActive(CoroutineInfo *coro, bool bFlag = false); - - /** - * 放到availçš„å程链表中 - */ - void moveToAvail(CoroutineInfo *coro); - - /** - * 放到inactiveçš„å程链表中 - */ - void moveToInactive(CoroutineInfo *coro); - - /** - * 放到超时等待的å程链表中 - */ - void moveToTimeout(CoroutineInfo *coro); - - /** - * 放到空闲的å程链表中 - */ - void moveToFreeList(CoroutineInfo *coro); - -private: - /* - * 是å¦åœæ­¢è¿è¡Œ - */ - bool _terminal; - - /* - * åç¨‹æ± çš„å¤§å° - */ - uint32_t _poolSize; - - /* - * åç¨‹çš„æ ˆç©ºé—´å¤§å° - */ - size_t _stackSize; - - /* - * 当å‰å·²ç»åˆ›å»ºçš„å程数 - */ - uint32_t _currentSize; - - /* - * 正在使用的å程数 - */ - uint32_t _usedSize; - - /* - * 产生å程Idçš„å˜é‡ - */ - uint32_t _uniqId; - - /* - * 框架的ServantHandleç±» - */ - ServantHandle* _handle; - - /* - * 主å程 - */ - CoroutineInfo _mainCoro; - - /* - * 当å‰è¿è¡Œçš„å程 - */ - CoroutineInfo* _currentCoro; - - /* - * 存放所有å程的数组指针 - */ - CoroutineInfo** _all_coro; - - /* - * 活跃的å程链表 - */ - CoroutineInfo _active; - - /* - * å¯ç”¨çš„å程链表 - */ - CoroutineInfo _avail; - - /* - * ä¸æ´»è·ƒçš„å程链表 - */ - CoroutineInfo _inactive; - - /* - * 超时的å程链表 - */ - CoroutineInfo _timeout; - - /* - * 空闲的å程链表 - */ - CoroutineInfo _free; - - /* - * å程栈空间的内存分é…器 - */ - standard_stack_allocator _alloc; - - /* - * 需è¦æ¿€æ´»çš„å程队列,其他线程使用,用æ¥æ¿€æ´»ç­‰å¾…结果的å程 - */ - TC_ThreadQueue > _activeCoroQueue; - - /* - * é”通知 - */ - TC_ThreadLock _monitor; - - /* - * 需è¦æ¿€æ´»çš„å程队列,本线程使用 - */ - list _needActiveCoroId; - - /* - * 存放超时的å程 - */ - multimap _timeoutCoroId; -}; - -///////////////////////////////////////////////////////////////////// -/** - * @brief å程异常类 - */ -struct CoroutineException : public TC_Exception -{ - CoroutineException(const string &buffer) : TC_Exception(buffer){}; - CoroutineException(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~CoroutineException() throw() {}; -}; - -//对线程进行包装的å程类,主è¦ç”¨äºŽåœ¨è‡ªå·±èµ·çš„线程中使用å程,业务å¯ä»¥ç»§æ‰¿è¿™ä¸ªç±» -class Coroutine : public TC_Thread -{ -public: - /** - * 构造函数 - */ - Coroutine(); - - /** - * æžæž„函数 - */ - virtual ~Coroutine(); - - /** - * åˆå§‹åŒ– - * @iNum, 表示è¦è¿è¡Œå¤šä¸ªå程,å³ä¼šæœ‰è¿™ä¸ªç±»å¤šå°‘个coroRunè¿è¡Œ - * @iTotalNum,表示这个线程最多包å«çš„å程个数 - * @iStackSize,åç¨‹çš„æ ˆå¤§å° - */ - void setCoroInfo(uint32_t iNum, uint32_t iMaxNum, size_t iStackSize); - - /** - * 创建å程,在已ç»åˆ›å»ºçš„å程中使用 - * 返回值为å程的id,大于0,表示æˆåŠŸï¼Œï¼Œå°äºŽç­‰äºŽ0,表示失败 - */ - uint32_t createCoroutine(const std::function &coroFunc); - - /** - * 当å‰å程自己放弃执行,会自动被调度器唤醒 - * 在已ç»åˆ›å»ºçš„å程中使用 - */ - void yield(); - - /** - * 当å‰å程休眠iSleepTime时间(å•ä½:毫秒),时间到了,会自动被调度器唤醒 - * 在已ç»åˆ›å»ºçš„å程中使用 - */ - void Sleep(int iSleepTime); - - /** - * 返回å程调度类的指针 - */ - CoroutineScheduler * getCoroSched() { return _coroSched; } - - /** - * 获å–设置的最大å程的数目 - */ - uint32_t getMaxCoroNum() { return _maxNum; } - - /** - * 获å–å¯åŠ¨æ—¶ï¼Œè®¾ç½®çš„å程的数目 - */ - uint32_t getCoroNum() { return _num; } - - /** - * 设置åç¨‹çš„æ ˆå¤§å° - */ - size_t getCoroStackSize() { return _stackSize; } - - /** - * åœæ­¢ - */ - void terminate(); - -protected: - - /** - * 线程处ç†æ–¹æ³• - */ - virtual void run(); - - /** - * é™æ€å‡½æ•°, å程入å£. - */ - static void coroEntry(Coroutine *pCoro); - - /** - * å程è¿è¡Œçš„函数,根æ®_num的数目,会å¯åŠ¨_num个这个函数 - */ - virtual void handle() = 0; - -protected: - /** - * 线程已ç»å¯åŠ¨, 进入å程处ç†å‰è°ƒç”¨ - */ - virtual void initialize() {} - - /** - * 所有å程åœæ­¢è¿è¡Œä¹‹åŽï¼Œçº¿ç¨‹é€€å‡ºä¹‹å‰æ—¶è°ƒç”¨ - */ - virtual void destroy() {} - - /** - * 具体的处ç†é€»è¾‘ - */ - virtual void handleCoro(); - -private: - CoroutineScheduler *_coroSched; - uint32_t _num; - uint32_t _maxNum; - size_t _stackSize; -}; - -} - -#endif diff --git a/cpp/servant/servant/EndpointInfo.h b/cpp/servant/servant/EndpointInfo.h deleted file mode 100644 index 2ef0f80b0..000000000 --- a/cpp/servant/servant/EndpointInfo.h +++ /dev/null @@ -1,255 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_ENDPOINT_INFO_H_ -#define __TARS_ENDPOINT_INFO_H_ - -#include "servant/Global.h" -#include "servant/NetworkUtil.h" - -using namespace std; - -namespace tars -{ -////////////////////////////////////////////////////////////////////////////// -/** - * 地å€ä¿¡æ¯IP:Port - */ -class EndpointInfo -{ -public: - enum EType { UDP = 0, TCP = 1, SSL = 2}; - - /** - * 构造函数 - */ - EndpointInfo(); - - /** - * 构造函数 - * @param host - * @param port - * @param type - */ - EndpointInfo(const string& host, uint16_t port, EndpointInfo::EType type, int32_t grid, const string & setDivision, int qos, int weight = -1, unsigned int weighttype = 0, int authType = 0); - - /** - * 地å€çš„字符串æè¿°,ä¸å¸¦setä¿¡æ¯ - * - * @return string - */ - const string& descNoSetInfo() const; - - /** - * 地å€çš„字符串æè¿° - * - * @return string - */ - const string & desc() const - { - return _desc; - } - - /** - * 比较的地å€çš„字符串æè¿° - * - * @return string - */ - const string & compareDesc() const - { - return _cmpDesc; - } - - /** - * 详细地å€å­—符串æè¿° - * - * @return string - */ - const string& fulldesc() const; - - /** - * 获å–主机å - * - * @return const string& - */ - string host() const; - - /** - * 获å–端å£å· - * - * @return uint16_t - */ - uint16_t port() const; - - /** - * 获å–è·¯ç”±çŠ¶æ€ - * @return int32_t - */ - int32_t grid() const; - - /* - * 获å–qosçš„descp值 - */ - int32_t qos() const {return _qos;} - - /* - * 获å–节点的é™æ€æƒé‡å€¼ - */ - int weight() const {return _weight;} - - /** - * @brief 获å–节点的æƒé‡ä½¿ç”¨æ–¹å¼ - */ - unsigned int getWeightType() const { return _weighttype; } - - /** - * 获å–ä¸»æœºåœ°å€ - * - * @return const struct sockaddr_in& - */ - const struct sockaddr_in& addr() const; - - /** - * 返回端å£ç±»åž‹ - * - * @return EndpointInfo::EType - */ - EndpointInfo::EType type() const; - - /** - *设置setåˆ†ç»„ä¿¡æ¯ - * - *@return void - */ - void setDivision(const string& sSetDivision); - - /** - *返回setåˆ†ç»„ä¿¡æ¯ - * - *@return string - */ - const string& setDivision() const; - - /* - * 获å–认è¯ç±»åž‹ - */ - int authType() const { return _authType; } - - /** - * 等于 - * @param r - * - * @return bool - */ - bool operator == (const EndpointInfo& r) const; - - /** - *等于,setä¿¡æ¯ä¸å‚与比较 - *@param r - * - *@return bool - */ - bool equalNoSetInfo(const EndpointInfo& r) const; - - /** - * å°äºŽ - * @param r - * - * @return bool - */ - bool operator < (const EndpointInfo& r) const; - -protected: - - /** - * 地å€çš„字符串æè¿° - * @param bWithSetInfo,标识 - * @return string - */ - string createDesc() const; - - /** - * 详细地å€å­—符串æè¿° - * - * @return string - */ - string createCompareDesc(); - -private: - /** - * 地å€IP - */ - string _host; - - /** - * 端å£å· - */ - uint16_t _port; - - /** - * è·¯ç”±çŠ¶æ€ - */ - int32_t _grid; - - /* - * qosçš„dscp值 - */ - - int32_t _qos; - - /** - * 类型 - */ - EndpointInfo::EType _type; - - /** - *setåˆ†ç»„ä¿¡æ¯ - */ - string _setDivision; - - /** - * 节点的é™æ€æƒé‡å€¼ - */ - int _weight; - - /** - * 节点的æƒé‡ä½¿ç”¨æ–¹å¼ - */ - unsigned int _weighttype; - - /** - * åœ°å€ - */ - struct sockaddr_in _addr; - - /** - * 比较的地å€å­—符串æè¿° - */ - string _cmpDesc; - - /** - * 地å€çš„字符串æè¿° - */ - string _desc; - - /** - * 认è¯ç±»åž‹ - */ - int _authType; -}; -///////////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/EndpointManager.h b/cpp/servant/servant/EndpointManager.h deleted file mode 100644 index 1a6f67a46..000000000 --- a/cpp/servant/servant/EndpointManager.h +++ /dev/null @@ -1,688 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_ENDPOINT_MANAGER_H_ -#define __TARS_ENDPOINT_MANAGER_H_ - -#include "util/tc_consistent_hash_new.h" -#include "servant/EndpointInfo.h" -#include "servant/EndpointF.h" -#include "servant/QueryF.h" -#include "servant/AppProtocol.h" - -namespace tars -{ -//////////////////////////////////////////////////////////////////////// -/* - * 获å–è·¯ç”±çš„æ–¹å¼ - */ -enum GetEndpointType -{ - E_DEFAULT = 0, - E_ALL = 1, - E_SET = 2, - E_STATION = 3 -}; - -/* - * æƒé‡ç±»åž‹ - */ -enum EndpointWeightType -{ - E_LOOP = 0, - E_STATIC_WEIGHT = 1, -}; - -//////////////////////////////////////////////////////////////////////// -/* - * 路由请求与回调的实现类 - */ -class QueryEpBase : public QueryFPrxCallback -{ -public: - /* - * 构造函数 - */ - QueryEpBase(Communicator * pComm, bool bFirstNetThread, bool bInterfaceReq); - - /* - * æžæž„函数 - */ - virtual ~QueryEpBase(){} - - /* - * åˆå§‹åŒ– - */ - bool init(const string & sObjName, const string & sLocator, const string& setName = ""); - - /* - * 获å–所有节点信æ¯çš„å›žè°ƒå¤„ç† - */ - void callback_findObjectById4All(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp); - - /* - * 获å–所有节点信æ¯çš„å¼‚å¸¸å›žè°ƒå¤„ç† - */ - void callback_findObjectById4All_exception(tars::Int32 ret); - - /* - * 获å–所有节点信æ¯çš„å›žè°ƒå¤„ç† - */ - void callback_findObjectById4Any(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp); - - /* - * 获å–所有节点信æ¯çš„å¼‚å¸¸å›žè°ƒå¤„ç† - */ - void callback_findObjectById4Any_exception(tars::Int32 ret); - - /* - * 按idc获å–的节点信æ¯çš„å›žè°ƒå¤„ç† - */ - void callback_findObjectByIdInSameGroup(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp); - - /* - * 按idc分组获å–的节点信æ¯çš„å¼‚å¸¸å›žè°ƒå¤„ç† - */ - void callback_findObjectByIdInSameGroup_exception(tars::Int32 ret); - - /* - * 按set获å–的节点信æ¯çš„å›žè°ƒå¤„ç† - */ - void callback_findObjectByIdInSameSet(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp); - - /* - * 按set获å–的节点信æ¯çš„å¼‚å¸¸å›žè°ƒå¤„ç† - */ - void callback_findObjectByIdInSameSet_exception(tars::Int32 ret); - - /* - * 按地区获å–的节点信æ¯çš„å›žè°ƒå¤„ç† - */ - void callback_findObjectByIdInSameStation(tars::Int32 ret, const vector& activeEp, const vector& inactiveEp); - - /* - * 按地区获å–的节点信æ¯çš„å¼‚å¸¸å›žè°ƒå¤„ç† - */ - void callback_findObjectByIdInSameStation_exception(tars::Int32 ret); - - /* - * 从主控请求到数æ®äº† 通知更新ipåˆ—è¡¨ä¿¡æ¯ - */ - virtual void notifyEndpoints(const set & active,const set & inactive,bool bSync) = 0; - - /* - * 从主控请求到数æ®äº† 最开始调用主控 è¦é€šçŸ¥ - */ - virtual void doNotify() = 0; - - /* - * è®¾ç½®ä¸»æŽ§çš„ä»£ç† - */ - int setLocatorPrx(QueryFPrx prx); - - /* - * 是å¦ç›´è¿žåŽç«¯ - */ - inline bool getDirectProxy() { return _direct; } - -protected: - - /* - * 刷新主控 - */ - void refreshReg(GetEndpointType type,const string & sName); - -private: - - /* - * 设置objåå­— - * 如果是直接连接,则从objå字中æå–ipåˆ—è¡¨ä¿¡æ¯ - * 如果是间接连接,则设置主控代ç†ï¼Œå¹¶ä»Žç¼“存中加载相应的列表 - */ - void setObjName(const string & sObjName); - - /* - * 从sEndpointsæå–ipåˆ—è¡¨ä¿¡æ¯ - */ - void setEndpoints(const string & sEndpoints, set & setEndpoints); - - /* - * 主控的请求的å“应到了,åšç›¸åº”çš„å¤„ç† - */ - void doEndpoints(const vector& activeEp, const vector& inactiveEp, int iRet, bool bSync = false); - - /* - * 请求主控异常,åšç›¸åº”çš„å¤„ç† - */ - void doEndpointsExp(int iRet); - - /* - * 刷新ip列表信æ¯åˆ°ç¼“存文件 - */ - void setEndPointToCache(bool bInactive); - -protected: - - /* - * 通信器 - */ - Communicator * _communicator; - - /* - * 是å¦ç¬¬ä¸€ä¸ªå®¢æˆ·ç«¯ç½‘络线程 - * 若是,会对ip列表信æ¯çš„写缓存 - */ - bool _firstNetThread; - - /* - * 是å¦ä¸»åŠ¨è¯·æ±‚ip列表信æ¯çš„接å£çš„请求 - * 比如按idc获å–æŸä¸ªobjçš„ipåˆ—è¡¨ä¿¡æ¯ - */ - bool _interfaceReq; - - /* - * 是å¦ç›´è¿žåŽç«¯æœåŠ¡ - */ - bool _direct; - - /* - * 请求的åŽç«¯æœåŠ¡çš„Obj对象å称 - */ - string _objName; - - /* - * 指定set调用的setid,默认为空 - * 如果有值,则说明是指定set调用 - */ - string _invokeSetId; - - /* - * æ¡†æž¶çš„ä¸»æŽ§åœ°å€ - */ - string _locator; - - /* - * ä¸»æŽ§çš„è·¯ç”±ä»£ç† - */ - QueryFPrx _queryFPrx; - - /* - * æ•°æ®æ˜¯å¦æœ‰æ•ˆ,åˆå§‹åŒ–çš„æ—¶å€™æ˜¯æ— æ•ˆçš„æ•°æ® - * åªæœ‰è¯·æ±‚过主控或者从文件缓存加载的数æ®æ‰æ˜¯æœ‰æ•ˆæ•°æ® - */ - bool _valid; - - /* - * æƒé‡ç±»åž‹ - */ - EndpointWeightType _weightType; - - /* - * 活跃节点列表 - */ - set _activeEndpoints; - - /* - * ä¸æ´»è·ƒèŠ‚点列表 - */ - set _inactiveEndpoints; - - -private: - - /////////以下是请求主控的策略信æ¯///////////////// - - /* - * 是å¦æ­£åœ¨å‘请求主控æœåŠ¡çš„ipåˆ—è¡¨ä¿¡æ¯ - */ - bool _requestRegistry; - - /* - * 请求主控的超时时间(ç»å¯¹æ—¶é—´),å•ä½æ¯«ç§’ - * 防止请求超时或者失败,一直处ç†è¯·æ±‚çŠ¶æ€ - */ - int64_t _requestTimeout; - - /* - * 请求主控的超时间隔,默认5s - */ - int _timeoutInterval; - - /* - * 下次请求主控的时间(ç»å¯¹æ—¶é—´),å•ä½æ¯«ç§’ - */ - int64_t _refreshTime; - - /* - * 正常请求主控的频率(有ip列表信æ¯),å•ä½æ¯«ç§’ - * 默认60s一次 - */ - int _refreshInterval; - - /* - * 主控返回的活跃ip列表为空情况下 - * 请求主控的频率,默认10s一次 - */ - int _activeEmptyInterval; - - /* - * 请求主控失败的时间频率 - * 默认2s一次 - */ - int _failInterval; - - /* - * è¯·æ±‚ä¸»æŽ§è¿žç»­å¤±è´¥è¾¾åˆ°ä¸€å®šçš„æ¬¡æ•°åŽ - * 请求主控的时间频率,默认30s一次 - */ - int _manyFailInterval; - - /* - * 连续请求失败的次数é™åˆ¶ï¼Œé»˜è®¤3次 - */ - int _failTimesLimit; - - /* - * 连续失败的次数 - */ - int _failTimes; - - -}; - -//////////////////////////////////////////////////////////////////////// -/* - * 框架内部的路由管ç†çš„实现类 - */ -class EndpointManager : public QueryEpBase -{ -public: - static const size_t iMinWeightLimit = 10; - static const size_t iMaxWeightLimit = 100; - -public: - /* - * 构造函数 - */ - EndpointManager(ObjectProxy* pObjectProxy, Communicator* pComm, const string & sObjName, bool bFirstNetThread, const string& setName = ""); - - /* - * æžæž„函数 - */ - virtual ~EndpointManager(); - - /* - * é‡å†™åŸºç±»çš„实现 - */ - void notifyEndpoints(const set & active, const set & inactive, bool bSync = false); - - /* - * é‡å†™åŸºç±»çš„实现 - */ - void doNotify(); - - /** - * æ ¹æ®è¯·æ±‚策略从å¯ç”¨çš„æœåŠ¡åˆ—表选择一个æœåŠ¡èŠ‚点 - */ - bool selectAdapterProxy(ReqMessage * msg, AdapterProxy * & pAdapterProxy); - - /** - * 获å–所有的æœåŠ¡èŠ‚点 - */ - const vector & getAdapters() - { - return _vAllProxys; - } - -private: - - /* - * 轮询选å–一个结点 - */ - AdapterProxy * getNextValidProxy(); - - /* - * æ ¹æ®hash值选å–一个结点 - */ - AdapterProxy* getHashProxy(int64_t hashCode, bool bConsistentHash = false); - - /* - * æ ¹æ®hash值按å–模方å¼ï¼Œä»Žæ­£å¸¸èŠ‚点中选å–一个结点 - */ - AdapterProxy* getHashProxyForNormal(int64_t hashCode); - - /* - * æ ¹æ®hash值按一致性hashæ–¹å¼ï¼Œä»Žæ­£å¸¸èŠ‚点中选å–一个结点 - */ - AdapterProxy* getConHashProxyForNormal(int64_t hashCode); - - /* - * æ ¹æ®hash值按å–模方å¼ï¼Œä»Žé™æ€æƒé‡èŠ‚点中选å–一个结点 - */ - AdapterProxy* getHashProxyForWeight(int64_t hashCode, bool bStatic, vector &vRouterCache); - - /* - * æ ¹æ®hash值按一致性hashæ–¹å¼ï¼Œä»Žé™æ€æƒé‡èŠ‚点中选å–一个结点 - */ - AdapterProxy* getConHashProxyForWeight(int64_t hashCode, bool bStatic); - - /* - * 判断é™æ€æƒé‡èŠ‚点是å¦æœ‰å˜åŒ– - */ - bool checkHashStaticWeightChange(bool bStatic); - - /* - * 判断é™æ€æƒé‡èŠ‚点是å¦æœ‰å˜åŒ– - */ - bool checkConHashChange(bool bStatic, const vector &vLastConHashProxys); - - /* - * æ›´æ–°å–模hash方法的é™æ€æƒé‡èŠ‚ç‚¹ä¿¡æ¯ - */ - void updateHashProxyWeighted(bool bStatic); - - /* - * 更新一致性hash方法的é™æ€æƒé‡èŠ‚ç‚¹ä¿¡æ¯ - */ - void updateConHashProxyWeighted(bool bStatic, vector &vLastConHashProxys, TC_ConsistentHashNew &conHash); - - /* - * æ ¹æ®åŽç«¯æœåŠ¡çš„æƒé‡å€¼é€‰å–一个结点 - */ - AdapterProxy* getWeightedProxy(bool bStaticWeighted); - - /* - * æ ¹æ®åŽç«¯æœåŠ¡çš„æƒé‡å€¼é€‰å–一个结点 - */ - AdapterProxy* getWeightedForNormal(bool bStaticWeighted); - - /* - * æ ¹æ®å„个节点的æƒé‡å€¼ï¼Œå»ºç«‹å„个节点的调用数 - */ - void updateProxyWeighted(); - - /* - * æ ¹æ®å„个节点的é™æ€æƒé‡å€¼ï¼Œå»ºç«‹å„个节点的é™æ€æƒé‡ - */ - void updateStaticWeighted(); - - /* - * 建立é™æ€æƒé‡èŠ‚点信æ¯çš„缓存 - */ - void dispatchEndpointCache(const vector &vWeight); - -private: - - /* - * ObjectProxy - */ - ObjectProxy * _objectProxy; - - /* - * 活跃的结点 - */ - vector _activeProxys; - - /* - * 部署的结点 包括活跃的和ä¸æ´»è·ƒçš„ - */ - map _regProxys; - vector _vRegProxys; - - /* - * 所有曾ç»create的结点 - */ - map _allProxys; - vector _vAllProxys; - - /* - * 轮训访问_activeProxysçš„å移 - */ - size_t _lastRoundPosition; - - /* - * 节点信æ¯æ˜¯å¦æœ‰æ›´æ–° - */ - bool _update; - - /* - * 是å¦æ˜¯ç¬¬ä¸€æ¬¡å»ºç«‹æƒé‡ä¿¡æ¯ - */ - bool _first; - - /** - * 上次é‡æ–°å»ºç«‹æƒé‡ä¿¡æ¯è¡¨çš„时间 - */ - int64_t _lastBuildWeightTime; - - /** - * 负载值更新频率,å•ä½æ¯«ç§’ - */ - int32_t _updateWeightInterval; - - /** - * é™æ€æ—¶ï¼Œå¯¹åº”的节点路由选择 - */ - size_t _lastSWeightPosition; - - /** - * é™æ€æƒé‡å¯¹åº”的节点路由缓存 - */ - vector _staticRouterCache; - - /* - * é™æ€æƒé‡çš„活跃节点 - */ - vector _activeWeightProxy; - - /* - * hashé™æ€æƒé‡çš„路由缓存 - */ - vector _hashStaticRouterCache; - - /* - * hashé™æ€æƒé‡çš„缓存节点 - */ - vector _lastHashStaticProxys; - - /* - * 一致性hashé™æ€æƒé‡æ—¶ä½¿ç”¨ - */ - vector _lastConHashWeightProxys; - - /* - * 一致性hashé™æ€æƒé‡æ—¶ä½¿ç”¨ - */ - TC_ConsistentHashNew _consistentHashWeight; - - /* - * 一致性hash普通使用 - */ - vector _lastConHashProxys; - - /* - * 一致性hash普通使用 - */ - TC_ConsistentHashNew _consistentHash; -}; - -//////////////////////////////////////////////////////////////////////// -/* - * 对外按类型获å–路由的实现类 - */ -class EndpointThread : public QueryEpBase -{ -public: - /* - * 构造函数 - */ - EndpointThread(Communicator* pComm, const string & sObjName, GetEndpointType type, const string & sSetName, bool bFirstNetThread = false); - - /* - * æžæž„函数 - */ - ~EndpointThread(){}; - - /* - * 用EndpointInfo存在å¯ç”¨ä¸Žä¸å¯ç”¨çš„èŠ‚ç‚¹ä¿¡æ¯ - */ - void getEndpoints(vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * 用TC_Endpoint存在å¯ç”¨ä¸Žä¸å¯ç”¨çš„èŠ‚ç‚¹ä¿¡æ¯ - */ - void getTCEndpoints(vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * é‡å†™åŸºç±»çš„实现 - */ - void notifyEndpoints(const set & active, const set & inactive, bool bSync); - - /* - * é‡å†™åŸºç±»çš„实现 - */ - void doNotify() - { - } - -private: - - /* - * 更新缓存的ipåˆ—è¡¨ä¿¡æ¯ - */ - void update(const set & active, const set & inactive); - -private: - - /* - * 类型 - */ - GetEndpointType _type; - - /* - * Objå称 - */ - string _name; - - /* - * é” - */ - TC_ThreadLock _lock; - - - /* - * 活跃的结点 - */ - vector _activeEndPoint; - vector _activeTCEndPoint; - - /* - * ä¸æ´»è·ƒçš„结点 - */ - vector _inactiveEndPoint; - vector _inactiveTCEndPoint; - -}; - -//////////////////////////////////////////////////////////////////////// -/* - * 对外获å–路由请求的å°è£…ç±» - */ -class EndpointManagerThread -{ -public: - /* - * 构造函数 - */ - EndpointManagerThread(Communicator *pComm, const string &sObjName); - - /* - * æžæž„函数 - */ - ~EndpointManagerThread(); - - /* - * 按idc获å–å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getEndpoint(vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * 获å–所有å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getEndpointByAll(vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * æ ¹æ®set获å–å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getEndpointBySet(const string sName, vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * æ ¹æ®åœ°åŒºèŽ·å–å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getEndpointByStation(const string sName, vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * 按idc获å–å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getTCEndpoint( vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * 获å–所有å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getTCEndpointByAll(vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * æ ¹æ®set获å–å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getTCEndpointBySet(const string sName, vector &activeEndPoint, vector &inactiveEndPoint); - - /* - * æ ¹æ®åœ°åŒºèŽ·å–å¯ç”¨ä¸Žä¸å¯ç”¨çš„结点 - */ - void getTCEndpointByStation(const string sName, vector &activeEndPoint, vector &inactiveEndPoint); - -protected: - - /* - * æ ¹æ®type创建相应的EndpointThread - */ - EndpointThread * getEndpointThread(GetEndpointType type, const string & sName); - -private: - /* - * 通信器 - */ - Communicator * _communicator; - - /* - * Objå称 - */ - string _objName; - - /* - * é” - */ - TC_ThreadLock _lock; - - /* - * ä¿å­˜å¯¹è±¡çš„map - */ - map _info; -}; - -//////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/Global.h b/cpp/servant/servant/Global.h deleted file mode 100644 index 8a2a2ba24..000000000 --- a/cpp/servant/servant/Global.h +++ /dev/null @@ -1,239 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_GLOBAL_H_ -#define __TARS_GLOBAL_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "util/tc_clientsocket.h" -#include "util/tc_autoptr.h" -#include "util/tc_common.h" -#include "util/tc_logger.h" -#include "util/tc_thread_mutex.h" -#include "tup/RequestF.h" -#include "servant/BaseF.h" - -using namespace std; - -namespace tars -{ -////////////////////////////////////////////////////////////// - -const size_t MAX_CLIENT_THREAD_NUM = 64; //客户端最大网络线程数 -const size_t MAX_CLIENT_ASYNCTHREAD_NUM = 1024; //客户端æ¯ä¸ªç½‘络线程拥有的最大异步线程数 -const size_t MAX_CLIENT_NOTIFYEVENT_NUM = 2048; //客户端æ¯ä¸ªç½‘络线程拥有的最大通知事件的数目 - -////////////////////////////////////////////////////////////// -class Communicator; -class AdapterProxy; -class ServantProxy; -class ServantProxyCallback; -class ObjectProxy; -class TarsCurrent; -class FDReactor; -class Transceiver; -class StatFProxy; -class StatReport; -class ServantProxyFactory; -class ObjectProxyFactory; -class AsyncProcThread; - -typedef TC_AutoPtr CommunicatorPtr; -typedef TC_AutoPtr ServantPrx; -typedef TC_AutoPtr ServantProxyCallbackPtr; -typedef TC_AutoPtr ObjectPrx; -typedef TC_AutoPtr TarsCurrentPtr; -typedef TC_AutoPtr StatFPrx; -typedef TC_AutoPtr StatReportPtr; -typedef TC_AutoPtr FDReactorPtr; -typedef TC_AutoPtr ServantProxyFactoryPtr; -typedef TC_AutoPtr ObjectProxyFactoryPtr; -typedef TC_AutoPtr AsyncProcThreadPtr; - -////////////////////////////////////////////////////////////// -/** - * 定义TARS网络调用的异常基类 - */ -struct TarsException : public TC_Exception -{ - TarsException(const string &buffer) : TC_Exception(buffer){}; - TarsException(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TarsException() throw(){}; - /** - * æ ¹æ®è¿”回值抛出异常 - * @param ret - * @param desc - */ - static void throwException(int ret, const string& desc = ""); -}; - -//////////////////////////////////////////////////////////////// -// 定义网络异常 -/** - * 建立连接异常 - */ -struct TarsNetConnectException : public TarsException -{ - TarsNetConnectException(const string &buffer) : TarsException(buffer){}; - TarsNetConnectException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsNetConnectException() throw(){}; -}; -/** - * 链接丢失 - */ -struct TarsNetConnectLostException : public TarsException -{ - TarsNetConnectLostException(const string &buffer) : TarsException(buffer){}; - TarsNetConnectLostException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsNetConnectLostException() throw(){}; -}; -/** - * Socket异常 - */ -struct TarsNetSocketException : public TarsException -{ - TarsNetSocketException(const string &buffer) : TarsException(buffer){}; - TarsNetSocketException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsNetSocketException() throw(){}; -}; -/** - * Proxy解ç å¼‚常 - */ -struct TarsProxyDecodeException : public TarsException -{ - TarsProxyDecodeException(const string &buffer) : TarsException(buffer){}; - TarsProxyDecodeException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsProxyDecodeException() throw(){}; -}; -/** - * Proxyç¼–ç å¼‚常 - */ -struct TarsProxyEncodeException : public TarsException -{ - TarsProxyEncodeException(const string &buffer) : TarsException(buffer){}; - TarsProxyEncodeException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsProxyEncodeException() throw(){}; -}; -/** - * Serverç¼–ç å¼‚常 - */ -struct TarsServerEncodeException : public TarsException -{ - TarsServerEncodeException(const string &buffer) : TarsException(buffer){}; - TarsServerEncodeException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsServerEncodeException() throw(){}; -}; -/** - * Server解ç å¼‚常 - */ -struct TarsServerDecodeException : public TarsException -{ - TarsServerDecodeException(const string &buffer) : TarsException(buffer){}; - TarsServerDecodeException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsServerDecodeException() throw(){}; -}; -/** - * Server无函数异常 - */ -struct TarsServerNoFuncException : public TarsException -{ - TarsServerNoFuncException(const string &buffer) : TarsException(buffer){}; - TarsServerNoFuncException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsServerNoFuncException() throw(){}; -}; -/** - * Server无对象异常 - */ -struct TarsServerNoServantException : public TarsException -{ - TarsServerNoServantException(const string &buffer) : TarsException(buffer){}; - TarsServerNoServantException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsServerNoServantException() throw(){}; -}; -/** - * 消æ¯åœ¨æœåŠ¡ç«¯é˜Ÿåˆ—超时 - */ -struct TarsServerQueueTimeoutException : public TarsException -{ - TarsServerQueueTimeoutException(const string &buffer) : TarsException(buffer){}; - TarsServerQueueTimeoutException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsServerQueueTimeoutException() throw(){}; -}; -/** - * æœåŠ¡ç«¯è¿”回的未知值 - */ -struct TarsServerUnknownException : public TarsException -{ - TarsServerUnknownException(const string &buffer) : TarsException(buffer){}; - TarsServerUnknownException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsServerUnknownException() throw(){}; -}; -/** - * åŒæ­¥è°ƒç”¨è¶…时异常 - */ -struct TarsSyncCallTimeoutException : public TarsException -{ - TarsSyncCallTimeoutException (const string &buffer) : TarsException(buffer){}; - TarsSyncCallTimeoutException (const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsSyncCallTimeoutException () throw(){}; -}; -/** - * 访问 Registry 错误 - */ -struct TarsRegistryException : public TarsException -{ - TarsRegistryException(const string &buffer) : TarsException(buffer){}; - TarsRegistryException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsRegistryException() throw(){}; -}; -/** - * 客户端队列满了 - */ -struct TarsClientQueueException : public TarsException -{ - TarsClientQueueException(const string &buffer) : TarsException(buffer){}; - TarsClientQueueException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsClientQueueException() throw(){}; -}; - -/** - * 业务线程调用å程并行请求接å£æ—¶ï¼ŒæŠ›å‡ºçš„异常 - */ -struct TarsUseCoroException : public TarsException -{ - TarsUseCoroException(const string &buffer) : TarsException(buffer){}; - TarsUseCoroException(const string &buffer, int err) : TarsException(buffer, err){}; - ~TarsUseCoroException() throw(){}; -}; -/////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/Message.h b/cpp/servant/servant/Message.h deleted file mode 100644 index 20fc5c34f..000000000 --- a/cpp/servant/servant/Message.h +++ /dev/null @@ -1,300 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_MESSAGE_H_ -#define __TARS_MESSAGE_H_ - -#include "servant/Global.h" -#include "util/tc_autoptr.h" -#include "util/tc_monitor.h" -#include "util/tc_loop_queue.h" -#include "servant/CoroutineScheduler.h" - -namespace tars -{ - -///////////////////////////////////////////////////////////////////////// -/** - * 超时一定比率åŽè¿›è¡ŒèŠ‚点å±è”½ - * 设置超时检查å‚æ•° - * 计算到æŸå°æœåŠ¡å™¨çš„超时情况, 如果连续超时次数或者超时比例超过阀值 - * 默认60s内, 超时调用次数>=2, 超时比率0.5,或者5s内连续超时次数>5 - * 或者连接异常连续超过5次 - * 则失效,进行å±è”½ - * æœåŠ¡å±è”½åŽ, 请求将尽å¯èƒ½çš„切æ¢åˆ°å…¶ä»–å¯èƒ½çš„æœåŠ¡å™¨, 并æ¯éš”tryTimeIntervalå°è¯•ä¸€æ¬¡, 如果æˆåŠŸåˆ™è®¤ä¸ºæ¢å¤ - * 如果其他æœåŠ¡å™¨éƒ½å±è”½, 则éšæœºé€‰æ‹©ä¸€å°å°è¯• - */ -struct CheckTimeoutInfo -{ - /* - * 构造函数 - */ - CheckTimeoutInfo() - : minTimeoutInvoke(2) - , checkTimeoutInterval(60) - , frequenceFailInvoke(5) - , minFrequenceFailTime(5) - , radio(0.5) - , tryTimeInterval(30) - , maxConnectExc(5) - { - } - - /* - * 判断超时å±è”½æ—¶ï¼Œè®¡ç®—的最å°çš„超时次数 - */ - uint16_t minTimeoutInvoke; - - /* - * 判断超时å±è”½æ—¶ï¼Œæ—¶é—´é—´éš” - */ - uint32_t checkTimeoutInterval; - - /* - * 判断因连续失败而进行å±è”½æ—¶ï¼Œè¿žç»­å¤±è´¥çš„次数 - */ - uint32_t frequenceFailInvoke; - - /* - * 判断因连续失败而进行å±è”½æ—¶ï¼Œæœ€å°çš„时间间隔 - */ - uint32_t minFrequenceFailTime; - - /* - * 超时的比率 - */ - float radio; - - /* - * é‡è¯•é—´éš” - */ - uint32_t tryTimeInterval; - - /* - * 最大连接异常次数 - */ - uint32_t maxConnectExc; -}; - -///////////////////////////////////////////////////////////////////////// -/* - * stat采样信æ¯(用于调用链时åºåˆ†æž) - */ -struct SampleKey -{ - /* - * 构造函数 - */ - SampleKey() - : _root(true) - , _unid("") - , _depth(0) - , _width(0) - , _parentWidth(0) - {} - - /* - * åˆå§‹åŒ– - */ - void init() - { - _root = true; - _unid.clear(); - _depth = 0; - _width = 0; - _parentWidth = 0; - } - - /* - * 是å¦æ ¹èŠ‚点 在根节点产生唯一id - */ - bool _root; - - /* - * 唯一id - */ - string _unid; - - /* - * 深度 - */ - int _depth; - - /* - * 广度 - */ - int _width; - - /* - * 父节点广度值 - */ - int _parentWidth; -}; - -///////////////////////////////////////////////////////////////////////// -/** - * 用于åŒæ­¥è°ƒç”¨æ—¶çš„æ¡ä»¶å˜é‡ - */ -struct ReqMonitor : public TC_ThreadLock -{ -}; - -///////////////////////////////////////////////////////////////////////// - -#define IS_MSG_TYPE(m, t) ((m & t) != 0) -#define SET_MSG_TYPE(m, t) do { (m |= t); } while (0); -#define CLR_MSG_TYPE(m, t) do { (m &=~t); } while (0); - -struct ReqMessage : public TC_HandleBase -{ - //调用类型 - enum CallType - { - SYNC_CALL = 1, //åŒæ­¥ - ASYNC_CALL, //异步 - ONE_WAY, //å•å‘ - THREAD_EXIT //线程退出的标识 - }; - - //è¯·æ±‚çŠ¶æ€ - enum ReqStatus - { - REQ_REQ = 0, //状æ€æ­£å¸¸,正在请求中 - REQ_RSP, //请求已ç»å‘出去 - REQ_TIME, //请求超时 - REQ_BUSY, //请求队列满了 - REQ_EXC //客户端请求异常 - }; - - /* - * 构造函数 - */ - ReqMessage() - : eStatus(ReqMessage::REQ_REQ) - , eType(SYNC_CALL) - , bFromRpc(false) - , callback(NULL) - , proxy(NULL) - , pObjectProxy(NULL) - , pMonitor(NULL) - , bMonitorFin(false) - , iBeginTime(0) - , iEndTime(0) - , bHash(false) - , bConHash(false) - , iHashCode(0) - , adapter(NULL) - , bDyeing(false) - , bPush(false) - , bCoroFlag(false) - , sched(NULL) - , iCoroId(0) - { - } - - /* - * æžæž„函数 - */ - ~ReqMessage() - { - if(pMonitor != NULL) - { - delete pMonitor; - pMonitor = NULL; - } - } - - /* - * åˆå§‹åŒ– - */ - void init(CallType eCallType, ObjectProxy * pObj, const string & sFuncName) - { - eStatus = ReqMessage::REQ_REQ; - eType = eCallType; - bFromRpc = false; - - callback = NULL; - proxy = NULL; - pObjectProxy = pObj; - - sReqData.clear(); - pMonitor = NULL; - bMonitorFin = false; - - iBeginTime = 0; - iEndTime = 0; - bHash = false; - bConHash = false; - iHashCode = 0; - adapter = NULL; - - bDyeing = false; - - bPush = false; - - bCoroFlag = false; - sched = NULL; - iCoroId = 0; - } - - - ReqStatus eStatus; //è°ƒç”¨çš„çŠ¶æ€ - CallType eType; //调用类型 - bool bFromRpc; //是å¦æ˜¯ç¬¬ä¸‰æ–¹å议的rcp_call,缺çœä¸ºfalse - - ServantProxyCallbackPtr callback; //异步调用时的回调对象 - - ServantProxy * proxy; //调用的ServantProxy对象 - ObjectProxy * pObjectProxy; //调用端的proxy对象 - - RequestPacket request; //请求消æ¯ä½“ - ResponsePacket response; //å“应消æ¯ä½“ - - string sReqData; //请求消æ¯ä½“ - - ReqMonitor * pMonitor; //用于åŒæ­¥çš„monitor - bool bMonitorFin; //åŒæ­¥è¯·æ±‚timewait是å¦ç»“æŸ - - int64_t iBeginTime; //请求时间 - int64_t iEndTime; //完æˆæ—¶é—´ - - bool bHash; //是å¦hash调用 - bool bConHash; //是å¦ä¸€è‡´æ€§hash调用 - int64_t iHashCode; //hash值,用户ä¿è¯ä¸€ä¸ªç”¨æˆ·çš„请求å‘é€åˆ°åŒä¸€ä¸ªserver(ä¸ä¸¥æ ¼ä¿è¯) - - AdapterProxy * adapter; //调用的adapter - - bool bDyeing; //是å¦éœ€è¦æŸ“色 - string sDyeingKey; //染色key - - bool bPush; //push back æ¶ˆæ¯ - - bool bCoroFlag; //是å¦æ˜¯å程里å‘出的请求 - CoroutineScheduler* sched; //å程调度器 - uint32_t iCoroId; //å程的id - - SampleKey sampleKey; //é‡‡æ ·ä¿¡æ¯ - -}; - -typedef TC_AutoPtr ReqMessagePtr; -typedef TC_LoopQueue ReqInfoQueue; - -#define HTTP2 "http2" - -} - -#endif diff --git a/cpp/servant/servant/NetworkUtil.h b/cpp/servant/servant/NetworkUtil.h deleted file mode 100644 index 7f29cd717..000000000 --- a/cpp/servant/servant/NetworkUtil.h +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_NETWORK_UTIL_H_ -#define __TARS_NETWORK_UTIL_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace tars -{ - -struct NetworkUtil -{ - static const int INVALID_SOCKET = -1; - static const int SOCKET_ERROR = -1; - - static int createSocket(bool, bool isLocal = false); - - static void closeSocketNoThrow(int); - - static void setBlock(int, bool); - - static void setTcpNoDelay(int); - - static void setKeepAlive(int); - - static void doBind(int, struct sockaddr_in&); - - static bool doConnect(int, const struct sockaddr_in&); - - static void getAddress(const std::string&, int, struct sockaddr_in&); - - static std::string errorToString(int); -}; - -} -#endif diff --git a/cpp/servant/servant/NotifyObserver.h b/cpp/servant/servant/NotifyObserver.h deleted file mode 100644 index 00800d5d9..000000000 --- a/cpp/servant/servant/NotifyObserver.h +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_NOTIFY_OBSERVER_H_ -#define __TARS_NOTIFY_OBSERVER_H_ - -#include "servant/Global.h" -#include "servant/TarsCurrent.h" -#include "util/tc_singleton.h" - -namespace tars -{ - -class BaseNotify; - -////////////////////////////////////////////////////////////// -/** - * 全局接收管ç†å‘½ä»¤ï¼Œå¹¶é€šçŸ¥åˆ°å·²æ³¨å†Œçš„对象 - */ -class NotifyObserver : public TC_Singleton, public TC_ThreadRecMutex -{ -public: - /** - * 构造函数 - */ - NotifyObserver(); - - /** - * æžæž„函数 - */ - ~NotifyObserver(); - -public: - - /** - * 接收管ç†å‘½ä»¤ - * @param command - * @param current - * @return string - */ - string notify(const string& command, TarsCurrentPtr current); - -public: - /** - * 注册å‰ç½®å‘½ä»¤å¤„ç†å™¨ï¼Œåœ¨Normal之å‰å¤„ç† - * @param command - * @param obj - */ - void registerPrefix(const string& command, BaseNotify* obj); - - /** - * 注销å‰ç½®å¤„ç†å™¨ - * @param command - * @param obj - */ - void unregisterPrefix(const string& command, BaseNotify* obj); - - /** - * 注册普通的命令处ç†å™¨ï¼Œå¤šä¸ªç›´æŽ¥é¡ºåºä¸ä¿è¯ - * @param command - * @param obj - */ - void registerNotify(const string& command, BaseNotify* obj); - - /** - * 注销普通的消æ¯å¤„ç†å™¨ - * @param command - * @param obj - */ - void unregisterNotify(const string& command, BaseNotify* obj); - - /** - * 展示已注册的命令处ç†å™¨ï¼Œå¤šä¸ªé¡ºåºä¸ä¿è¯ - */ - string viewRegisterCommand(); - -protected: - /** - * 注册对象 - * @param command - * @param obj - * @param target - */ - void registerObject(const string& command, BaseNotify* obj, map >& target); - - /** - * å–消对象注册 - * @param command - * @param obj - * @param target - */ - void unregisterObject(const string& command, BaseNotify* obj, map >& target); - -protected: - - /** - * å‰ç½®å¤„ç†å™¨ - */ - map > _prefix; - - /** - * 普通处ç†å™¨ - */ - map > _notifys; -}; - -///////////////////////////////////////////////////////////////// -} - -#endif diff --git a/cpp/servant/servant/ObjectProxy.h b/cpp/servant/servant/ObjectProxy.h deleted file mode 100644 index b0ac2970b..000000000 --- a/cpp/servant/servant/ObjectProxy.h +++ /dev/null @@ -1,344 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_OBJECT_PROXY_H_ -#define __TARS_OBJECT_PROXY_H_ - -#include "servant/Communicator.h" -#include "servant/Message.h" -#include "servant/AdapterProxy.h" -#include "servant/EndpointInfo.h" -#include "servant/EndpointF.h" -#include "servant/AppProtocol.h" -#include "servant/Global.h" -#include "util/tc_timeout_queue_noid.h" - -namespace tars -{ -class EndpointManager; - -/////////////////////////////////////////////////////////////////// -/** - * socket选项 - */ -struct SocketOpt -{ - int level; - - int optname; - - const void *optval; - - socklen_t optlen; -}; - -/////////////////////////////////////////////////////////////////// -/** - * æ¯ä¸ªobjectname在æ¯ä¸ªå®¢æˆ·ç«¯ç½‘络线程中有唯一一个objectproxy - * - */ -class ObjectProxy : public TC_HandleBase, public TC_ThreadMutex -{ -public: - /** - * 构造函数 - * @param pCommunicatorEpoll - * @param sObjectProxyName - * @param setName 指定set调用的setid - */ - ObjectProxy(CommunicatorEpoll * pCommunicatorEpoll, const string & sObjectProxyName, const string& setName=""); - - /** - * æžæž„函数 - */ - ~ObjectProxy(); - - /** - * åˆå§‹åŒ– - */ - void initialize(); - - /** - * 加载locator - */ - int loadLocator(); - - /** - * 方法调用 - */ - void invoke(ReqMessage* msg); - - /** - * å‘é€ç§¯æ”’在objçš„æ•°æ® - */ - void doInvoke(); - - /** - * 设置å议解æžå™¨ - * @return UserProtocol& - */ - void setProxyProtocol(const ProxyProtocol& protocol, const std::string& name = "tars"); - /** - * 获å–å议解æžå™¨ - * @return ProxyProtocol& - */ - ProxyProtocol& getProxyProtocol(); - - /** - * 获å–åè®®åå­— - * @return ProxyProtocol& - */ - const std::string& getProtoName() const; - - /** - * 设置套接å£é€‰é¡¹ - */ - void setSocketOpt(int level, int optname, const void *optval, socklen_t optlen); - - /** - * 获å–套接字选项 - */ - vector& getSocketOpt(); - - /** - * 设置PUSH类消æ¯çš„callback对象 - * @param cb - */ - void setPushCallbacks(const ServantProxyCallbackPtr& cb); - - /** - * 获å–PUSH类消æ¯çš„callback对象 - */ - ServantProxyCallbackPtr getPushCallback(); - - /** - * 获å–所有的adapter - */ - const vector & getAdapters() const; - - /** - * 检查Obj的队列超时 - */ - void doTimeout(); - - /** - * Obj的超时队列的长度 - */ - size_t timeoutQSize() - { - return _reqTimeoutQueue.size(); - } - - /** - * 获å–CommunicatorEpoll* - */ - inline CommunicatorEpoll * getCommunicatorEpoll() - { - return _communicatorEpoll; - } - - /** - * 获å–objectå称 - * @return const string& - */ - inline const string & name() const - { - return _name; - } - - /** - * 判断此obj是å¦èµ°æŒ‰set规则调用æµç¨‹ï¼Œå¦‚果是直连方å¼ï¼Œå³ä½¿æœåŠ¡ç«¯æ˜¯å¯ç”¨set的,也ä¸è®¤ä¸ºæ˜¯æŒ‰set规则调用的 - */ - bool IsInvokeBySet() const - { - return _isInvokeBySet; - } - - /** - * 获å–按set规则调用的setå称 - */ - const string& getInvokeSetName() const - { - return _invokeSetId; - } - - /** - * 生æˆå”¯ä¸€Id - */ - inline uint32_t generateId() - { - _id++; - - if(_id == 0) - { - ++_id; - } - - return _id; - } - - /** - * 获å–连接超时时间 - * @return int - */ - inline int getConTimeout() - { - return _conTimeout; - } - - /** - * 设置连接超时时间 - */ - inline void setConTimeout(int conTimeout) - { - _conTimeout = conTimeout; - } - - /** - * 超时策略获å–和设置 - * @return CheckTimeoutInfo& - */ - inline CheckTimeoutInfo& checkTimeoutInfo() - { - return _checkTimeoutInfo; - } - - /** - * 获å–servantproxy - */ - inline ServantProxy * getServantProxy() - { - return _servantProxy; - } - - /** - * 设置servantproxy - */ - inline void setServantProxy(ServantProxy * pServantProxy) - { - _servantProxy = pServantProxy; - } - - /** - * 设置AK - */ - void setAccessKey(const string& k) { _accessKey = k; } - /** - * 设置SK - */ - void setSecretKey(const string& k) { _secretKey = k; } - /** - * 获å–AK - */ - const string& getAccessKey() const { return _accessKey; } - /** - * 获å–SK - */ - const string& getSecretKey() const { return _secretKey; } - -protected: - - /** - * 处ç†è¯·æ±‚异常 - */ - void doInvokeException(ReqMessage * msg); - -private: - /* - * 客户端网络线程的指针 - */ - CommunicatorEpoll * _communicatorEpoll; - - /* - * objectçš„å称 - */ - string _name; - - /* - * 按set规则调用的setå称 - */ - string _invokeSetId; - - /* - * 是å¦æ˜¯æŒ‰set规则调用 - */ - bool _isInvokeBySet; - - /* - * 生æˆè¯·æ±‚的唯一id - */ - uint32_t _id; - - /* - * 是å¦è°ƒç”¨äº†tars_set_protocol设置过proxyçš„å议函数, - * 设置过了就ä¸åœ¨è®¾ç½® - */ - bool _hasSetProtocol; - - /* - * 请求和å“应的å议解æžå™¨ - */ - ProxyProtocol _proxyProtocol; - /* - * åè®®å称 - */ - std::string _protoName; - - /* - * 连接超时的时间 - */ - int _conTimeout; - - /* - * è¶…æ—¶æŽ§åˆ¶ç­–ç•¥ä¿¡æ¯ - */ - CheckTimeoutInfo _checkTimeoutInfo; - - /* - * socket选项 - */ - vector _socketOpts; - - /* - * pushæ¶ˆæ¯ callback - */ - ServantProxyCallbackPtr _pushCallback; - - /* - * 结点路由管ç†ç±» - */ - std::unique_ptr _endpointManger; - - /* - * 超时队列 - */ - TC_TimeoutQueueNoID _reqTimeoutQueue; - - /* - * ServantProxy - */ - ServantProxy * _servantProxy; - - /** - * AK/SK - */ - std::string _accessKey; - std::string _secretKey; - -}; -/////////////////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/ObjectProxyFactory.h b/cpp/servant/servant/ObjectProxyFactory.h deleted file mode 100644 index a16ac0a75..000000000 --- a/cpp/servant/servant/ObjectProxyFactory.h +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_OBJECT_PROXY_FACTORY_H_ -#define __TARS_OBJECT_PROXY_FACTORY_H_ - -#include "servant/Communicator.h" -#include "servant/ObjectProxy.h" -#include - -namespace tars -{ -///////////////////////////////////////////////////////////////////////////////////////// -/** - * 获å–ObjectProxy对象 - * æ¯ä¸ªobjectname在æ¯ä¸ªå®¢æˆ·ç«¯ç½‘络线程中有唯一一个objectproxy - * - */ -class ObjectProxyFactory : public TC_HandleBase, public TC_ThreadRecMutex -{ -public: - /** - * 构造函数 - * @param pCommunicatorEpoll - */ - ObjectProxyFactory(CommunicatorEpoll * pCommunicatorEpoll); - - /** - * 获å–å¯¹è±¡ä»£ç† - * @param sObjectProxyName - * @param setName 指定set调用的setid - * - * @return ObjectPrx - */ - ObjectProxy * getObjectProxy(const string& sObjectProxyName,const string& setName = ""); - - /** - * æžæž„函数 - */ - ~ObjectProxyFactory(); - - /** - * 所有对象代ç†åŠ è½½locatorä¿¡æ¯ - */ - int loadObjectLocator(); - - /** - * 获å–所有对象的个数,为了ä¸åŠ é”ä¸ç”¨map - */ - inline size_t getObjNum() - { - return _objNum; - } - - /** - * æ ¹æ®åºå· 获å–所有obj对象,为了ä¸åŠ é”ä¸ç”¨map - */ - inline ObjectProxy * getObjectProxy(size_t iNum) - { - assert(iNum < _objNum); - return _vObjectProxys[iNum]; - } - -private: - /** - * 客户端网络线程 - */ - CommunicatorEpoll * _communicatorEpoll; - - /** - * ä¿å­˜å·²åˆ›å»ºçš„objectproxy - */ - map _objectProxys; - - /** - * ä¿å­˜å·²ç»åˆ›å»ºçš„obj å–的时候å¯ä»¥ä¸ç”¨åŠ é” - */ - vector _vObjectProxys; - - /* - *ä¿å­˜å·²ç»åˆ›å»ºobjçš„æ•°é‡ - */ - size_t _objNum; -}; - -///////////////////////////////////////////////////////////////////////////////////////// - -} -#endif diff --git a/cpp/servant/servant/PropertyReport.h b/cpp/servant/servant/PropertyReport.h deleted file mode 100644 index 018071000..000000000 --- a/cpp/servant/servant/PropertyReport.h +++ /dev/null @@ -1,261 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PROPERTY_REPORT_H_ -#define __TARS_PROPERTY_REPORT_H_ - -#include "util/tc_autoptr.h" -#include "util/tc_thread_mutex.h" - -#include -#include -#include - -using namespace std; - -namespace tars -{ - -/////////////////////////////////////////////////////////////////////////////////// -// -/** - * 用户自定义属性统计基类 - */ -class PropertyReport : public TC_HandleBase -{ -public: - /** - * 设置该属性的æœåŠ¡å称,如果ä¸è®¾ç½®åˆ™ä¸ºå½“å‰æœåŠ¡ - */ - void setMasterName(const std::string & sMasterName) { _sMasterName = sMasterName; } - - /** - * 获å–该属性的æœåŠ¡å称 - */ - std::string getMasterName() { return _sMasterName; } - -public: - - /** - * 求和 - */ - class sum - { - public: - sum() :_d(0) { } - string get(); - string desc() { return "Sum"; } - void set(int o) { _d += o; } - protected: - void clear() { _d = 0; } - private: - int _d; - }; - - /** - * æ±‚å¹³å‡ - */ - class avg - { - public: - avg():_sum(0), _count(0) { } - string desc() { return "Avg"; } - string get(); - void set(int o) { _sum += o;++_count; } - protected: - void clear() { _sum = 0; _count = 0; } - private: - int _sum; - int _count; - }; - - /** - * 分布 - */ - class distr - { - public: - distr(){}; - distr(const vector& range); - string desc() { return "Distr"; } - void set(int o); - string get(); - protected: - void clear() { _result.clear();} - private: - vector _range; - vector _result; - }; - - /** - * 求最大值 - */ - class max - { - public: - max() : _d(-9999999) { } - string desc() { return "Max"; } - string get(); - void set(int o) { _d < o?_d = o:1; } - protected: - void clear() { _d = 0; } - private: - int _d; - }; - - /** - * 求最å°å€¼ - */ - class min - { - public: - min():_d(0) { } - string desc() { return "Min"; } - string get(); - void set(int o); - protected: - void clear() { _d = 0; } - private: - int _d; - }; - - /** - * 计数 - */ - class count - { - public: - count():_d(0) { } - string desc() { return "Count"; } - string get(); - void set(int o) { _d++; } - protected: - void clear() { _d = 0; } - private: - int _d; - }; - -public: - - virtual void report(int iValue) = 0; - virtual vector > get() = 0; - -protected: - std::string _sMasterName; //属性所属æœåŠ¡å称 -}; - -typedef TC_AutoPtr PropertyReportPtr; - -/////////////////////////////////////////////////////////////////////////////////// -// -/** - * å…·ä½“çš„å±žæ€§ç­–ç•¥ç®¡ç† - */ - -template -class PropertyReportImp : public PropertyReport, public TC_ThreadMutex -{ -public: - using PropertyReportData = std::tuple; - - PropertyReportImp(Params&&... args) : - _propertyReportData(std::forward(args)...) - { - } - - // do NOT copy - PropertyReportImp(const PropertyReportImp& ) = delete; - void operator = (const PropertyReportImp& ) = delete; - - // but CAN move - PropertyReportImp(PropertyReportImp&& ) = default; - PropertyReportImp& operator= (PropertyReportImp&& ) = default; - - - /** - * è®¾ç½®è°ƒç”¨æ•°æ® - * @param iValue,值 - */ - void report(int iValue) override - { - TC_LockT lock(*this); - Helper::value>::Report(*this, iValue); - } - - - /** - * 获å–å±žæ€§ä¿¡æ¯ - * - * @return vector> - */ - vector > get() override - { - TC_LockT lock(*this); - return Helper::value>::Get(*this); - } - -private: - // report helper - template - struct Helper - { - static void Report(PropertyReportImp& pp, int iValue) - { - static_assert(N >= 1, "Obviously success"); - Helper::Report(pp, iValue); - pp.template SetResult(iValue); - } - - static std::vector> Get(PropertyReportImp& pp) - { - static_assert(N >= 1, "Obviously success"); - - std::vector > vs = Helper::Get(pp); - - vs.push_back({std::get(pp._propertyReportData).desc(), std::get(pp._propertyReportData).get()}); - return vs; - } - }; - - template - struct Helper<0, DUMMY> - { - // base template - static void Report(PropertyReportImp&, int ) - { - } - - static - std::vector> Get(PropertyReportImp& ) - { - return std::vector >(); - } - }; - - template - void SetResult(int iValue) - { - std::get(_propertyReportData).set(iValue); - } - /** - * 状æ€æŠ¥å‘Šæ•°æ® - */ - PropertyReportData _propertyReportData; -}; - -} - -#endif diff --git a/cpp/servant/servant/Servant.h b/cpp/servant/servant/Servant.h deleted file mode 100644 index 0a1b91d67..000000000 --- a/cpp/servant/servant/Servant.h +++ /dev/null @@ -1,299 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_SERVANT_H_ -#define __TARS_SERVANT_H_ - -#include "util/tc_autoptr.h" -#include "util/tc_epoll_server.h" -#include "servant/ServantProxy.h" -#include "servant/TarsCurrent.h" -#include "servant/BaseNotify.h" - -namespace tars -{ - -class BaseNotify; -//////////////////////////////////////////////////////////////////// -/** - * æ¯ä¸ªå¯¹è±¡çš„基类 - */ -class Servant : public BaseNotify -{ -public: - /** - * 构造函数 - */ - Servant(); - - /** - * æžæž„函数 - */ - ~Servant(); - - /** - * 设置å称 - * @param name - */ - void setName(const string &name); - - /** - * å称 - * @return string - */ - string getName() const; - - /** - * 设置所属的Handle - * @param handle - */ - void setHandle(TC_EpollServer::Handle* handle); - - /** - * 获å–所属的Handle - * @return HandlePtr& - */ - TC_EpollServer::Handle* getHandle(); - - /** - * åˆå§‹åŒ– - * @return int - */ - virtual void initialize() = 0; - - /** - * 退出 - */ - virtual void destroy() = 0; - -public: - /** - * 分å‘收到的请求 - * @param sRecv - * @param sFuncName - * @param sSend - * @return int - */ - virtual int dispatch(TarsCurrentPtr current, vector &buffer); - - /** - * tarså议,分å‘并处ç†è¯·æ±‚ - * @param sRecv - * @param sFuncName - * @param sSend - * @return int - */ - virtual int onDispatch(TarsCurrentPtr current, vector &buffer) { return -1; } - -public: - /** - * 普通å议的请求,没有方法å,ä¸éœ€è¦Dispatch - * @param current - * @param buffer - * @return int - */ - virtual int doRequest(TarsCurrentPtr current, vector &buffer) { return -1; } - - /** - * 作为客户端访问其他server时,æˆåŠŸè¿”回的å“åº”æŽ¥å£ - * @param resp - * @return int - */ - virtual int doResponse(ReqMessagePtr resp) { return -1; } - - /** - * 作为客户端访问其他server时,返回其他异常的å“åº”æŽ¥å£ - * @param resp - * @return int - */ - virtual int doResponseException(ReqMessagePtr resp) { return -1; } - - /** - * 作为客户端访问其他server时,如果resp没有找到request,则å“åº”è¯¥æŽ¥å£ - * 例如:push消æ¯æˆ–者超时åŽæœåŠ¡ç«¯è¿Ÿåˆ°çš„å“应 - * @param resp - * @return int - */ - virtual int doResponseNoRequest(ReqMessagePtr resp) { return -1; } - - /** - * æ¯æ¬¡handle被唤醒åŽéƒ½ä¼šè°ƒç”¨ï¼Œä¸šåŠ¡å¯ä»¥é€šè¿‡åœ¨å…¶ä»–线程中调用handleçš„notify - * 实现在主线程中处ç†è‡ªæœ‰æ•°æ®çš„功能,比如定时器任务或自有网络的异步å“应; - * [一般都需è¦é…åˆä¸šåŠ¡è‡ªæœ‰çš„队列使用,队列å¯ä»¥å°è£…在ServantImp对象中] - * - * 关于å‚æ•°bExpectIdle: - * 一般业务ä¸ç”¨å…³æ³¨è¯¥å€¼ï¼Œå¯å¿½ç•¥ã€‚ - * - * bExpectIdle为true时,在循环Adapter时调用的 - * bExpectIdle为false时,在处ç†Adapter上的请求时调用的 - * - * doCustomMessage() ä¸å¸¦å‚数的是为了兼容è€ç‰ˆæœ¬ã€‚å°½é‡ç”¨å¸¦å‚数的函数 - * 现在ä¸å¸¦å‚数和带å‚数的都会调用 - */ - virtual int doCustomMessage(bool bExpectIdle) { return -1; } - virtual int doCustomMessage() { return -1; } - - - /** - * å®¢æˆ·ç«¯å…³é—­è¿žæŽ¥æ—¶çš„å¤„ç† - * @param current - * @return int - */ - virtual int doClose(TarsCurrentPtr current){ return -1; } - - /** - * 获得å“应的数æ®é˜Ÿåˆ— - * @return TC_ThreadQueue& - */ - TC_ThreadQueue& getResponseQueue(); - -protected: - /** - * åå­— - */ - string _name; - - /** - * 所属的Handle - */ - TC_EpollServer::Handle* _handle; - - /** - * 异步å“应队列 - */ - TC_ThreadQueue _asyncResponseQueue; -}; - -typedef TC_AutoPtr ServantPtr; - -////////////////////////////////////////////////////////////////////// -/** - * å•çº¿ç¨‹å…¨å¼‚步中的callback对象,业务自有的callback需è¦ä»Žè¿™é‡Œç»§æ‰¿ - */ -class ServantCallback : public ServantProxyCallback -{ -public: - /** - * 构造函数,type用æ¥åŒºåˆ†åŒä¸€é“¾è·¯ä¸Šçš„多ç§cb对象类型 - * @param type - * @param servant - * @param current - */ - ServantCallback(const string& type, const ServantPtr& servant, const TarsCurrentPtr& current); - - /** - * callbackçš„å“åº”æŽ¥å£ - * @param msg - * @return int - */ - virtual int onDispatch(ReqMessagePtr msg); - - /** - * 获得生æˆæ—¶æ‰€å±žçš„servant - * @return const ServantPtr& - */ - const ServantPtr& getServant(); - - /** - * 获得网络上下文 - * @return const TarsCurrentPtr& - */ - const TarsCurrentPtr& getCurrent(); - -protected: - /* - * Servant - */ - ServantPtr _servant; - - /* - * TarsCurrent - */ - TarsCurrentPtr _current; -}; - -////////////////////////////////////////////////////////////////////// - -//线程ç§æœ‰æ•°æ® -class CallbackThreadData : public TC_ThreadPool::ThreadData -{ -public: - static TC_ThreadMutex _mutex; //å…¨å±€çš„äº’æ–¥é” - static pthread_key_t _key; //ç§æœ‰çº¿ç¨‹æ•°æ®key - - /** - * 构造函数 - */ - CallbackThreadData(); - - /** - * æžæž„函数 - */ - ~CallbackThreadData() {} - - /** - * æ•°æ®èµ„æºé‡Šæ”¾ - * @param p - */ - static void destructor(void* p); - - /** - * 获å–线程数æ®ï¼Œæ²¡æœ‰çš„è¯ä¼šè‡ªåŠ¨åˆ›å»º - * @return CallbackThreadData* - */ - static CallbackThreadData * getData(); - -public: - /** - * 设置返回的é¢å¤–内容 - * @param context - */ - void setResponseContext(const map & context); - - /** - * 获å–返回的é¢å¤–内容是å¦æœ‰æ•ˆ - * @param context - */ - bool getContextValid(){return _contextValid;} - - /** - * 获å–返回的é¢å¤–内容 - * @param context - */ - map & getResponseContext(); - - /** - * 清除返回的é¢å¤–内容是å¦æœ‰æ•ˆ - * @param context - */ - void delResponseContext(); - -private: - /* - * 返回的context是å¦åˆæ³• - */ - bool _contextValid; - - /* - * 返回的context - */ - map _responseContext; -}; - -////////////////////////////////////////////////////////////////////// -} -////////////////////////////////////////////////////////////////////// -#endif diff --git a/cpp/servant/servant/ServantHandle.h b/cpp/servant/servant/ServantHandle.h deleted file mode 100644 index fba9a133f..000000000 --- a/cpp/servant/servant/ServantHandle.h +++ /dev/null @@ -1,203 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_SERVANT_HANDLE_H -#define __TARS_SERVANT_HANDLE_H - -#include -#include -#include -#include -#include "util/tc_monitor.h" -#include "util/tc_epoll_server.h" -#include "servant/Servant.h" -#include "servant/StatReport.h" -#include -#include "servant/CoroutineScheduler.h" - -namespace tars -{ -////////////////////////////////////////////////////////////////////////////// -/** - * 处ç†ç½‘络请求线程 - */ -class ServantHandle : public TC_EpollServer::Handle -{ -public: - /** - * å®šä¹‰å¸¸é‡ - */ - enum - { - HEART_BEAT_INTERVAL = 10, /**心跳间隔时间**/ - }; - - /** - * 构造 - */ - ServantHandle(); - - /** - * æžå¤Ÿ - */ - ~ServantHandle(); - - /** - * 线程处ç†æ–¹æ³• - */ - virtual void run(); - - /** - * 获å–å程调度器 - */ - CoroutineScheduler* getCoroSched() { return _coroSched; } - -protected: - - /** - * 处ç†æŽ¥æ”¶è¯·æ±‚çš„å程函数 - */ - virtual void handleRequest(); - - /** - * 处ç†è¯·æ±‚çš„å程函数 - */ - virtual void handleRecvData(TC_EpollServer::tagRecvData *stRecvData); - -protected: - /** - * 线程åˆå§‹åŒ– - */ - void initialize(); - - /** - * é€»è¾‘å¤„ç† - * @param stRecvData - */ - virtual void handle(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * è¶…æ—¶å¤„ç† - * @param stRecvData - */ - virtual void handleTimeout(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * overload å¤„ç† - * @param stRecvData - */ - virtual void handleOverload(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * close äº‹ä»¶å¤„ç† - * @param stRecvData - */ - virtual void handleClose(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * handleFilter拆分的第一部分,处ç†å¼‚步调用队列 - */ - virtual void handleAsyncResponse(); - - /** - * handleFilter拆分的第二部分,处ç†ç”¨æˆ·è‡ªæœ‰æ•°æ® - * éžæ¸¸æˆé€»è¾‘å¯å¿½ç•¥bExpectIdleå‚æ•° - */ - virtual void handleCustomMessage(bool bExpectIdle = false); - - /** - * 心跳 - */ - virtual void heartbeat(); - - /** - * 检查servant有没有resp消æ¯å¾…å¤„ç† - * @return bool - */ - virtual bool allFilterIsEmpty(); - - /** - * 创建上下文 - * @param stRecvData - * @return TarsCurrent* - */ - TarsCurrentPtr createCurrent(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * 创建闭连接时的关上下文 - * @param stRecvData - * @return TarsCurrent* - */ - TarsCurrentPtr createCloseCurrent(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * 处ç†Tarsåè®® - * - * @param current - */ - void handleTarsProtocol(const TarsCurrentPtr ¤t); - - /** - * 处ç†éžTarsåè®® - * - * @param current - */ - void handleNoTarsProtocol(const TarsCurrentPtr ¤t); - - /** - * 处ç†TARS下的采样统计逻辑 - * - * @param current - */ - void processSample(const TarsCurrentPtr ¤t); - - /** - * 处ç†TARS下的染色逻辑 - * - * @param current - */ - bool processDye(const TarsCurrentPtr ¤t, string& dyeingKey); - - /** - * 检查set调用åˆæ³•æ€§ - * - * @param current - * @return bool 如果调用åˆæ³•è¿”回true,如果调用éžæ³•åˆ™è¿”回false - */ - bool checkValidSetInvoke(const TarsCurrentPtr ¤t); -protected: - - /** - * 处ç†å¯¹è±¡ - */ - map _servants; - - /** - * 消æ¯åˆ°è¾¾é€šçŸ¥ - */ - TC_ThreadLock _monitor; - - /** - * å程调度器 - */ - CoroutineScheduler *_coroSched; -}; - -typedef TC_AutoPtr ServantHandlePtr; -/////////////////////////////////////////////////////////// -} -#endif - diff --git a/cpp/servant/servant/ServantHelper.h b/cpp/servant/servant/ServantHelper.h deleted file mode 100644 index 366a299af..000000000 --- a/cpp/servant/servant/ServantHelper.h +++ /dev/null @@ -1,207 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_SERVANT_HELPER_H -#define __TARS_SERVANT_HELPER_H - -#include -#include -#include -#include "util/tc_autoptr.h" -#include "util/tc_singleton.h" -#include "servant/Servant.h" - -namespace tars -{ -////////////////////////////////////////////////////////////////////////////// -/** - * Servant - */ -class ServantHelperCreation : public TC_HandleBase -{ -public: - virtual ServantPtr create(const string &s) = 0; -}; -typedef TC_AutoPtr ServantHelperCreationPtr; - -////////////////////////////////////////////////////////////////////////////// -/** - * Servant - */ -template -struct ServantCreation : public ServantHelperCreation -{ - ServantPtr create(const string &s) { T *p = new T; p->setName(s); return p; } -}; - -////////////////////////////////////////////////////////////////////////////// -// -/** - * Servantç®¡ç† - */ -class ServantHelperManager : public TC_Singleton -{ -public: - /** - * 构造函数 - */ - ServantHelperManager() - : _isDyeing(false) - { - } - - /** - * 添加Servant - * @param T - * @param id - */ - template - void addServant(const string &id,bool check = false) - { - if(check && _servant_adapter.end() == _servant_adapter.find(id)) - { - cerr<<"[TARS]ServantHelperManager::addServant "<< id <<" not find adapter.(maybe not conf in the web)"<(); - } - - /** - * 生æˆServant - * @param id - * - * @return ServantPtr - */ - ServantPtr create(const string &sAdapter); - - /** - * 添加Adapterçš„Servant - * @param sAdapter - * @param sServant - */ - void setAdapterServant(const string &sAdapter, const string &sServant); - - /** - * æ ¹æ®adapterå称获å–Servantå称 - * @param sAdapter - * @return string - */ - string getAdapterServant(const string &sAdapter) - { - map::iterator it = _adapter_servant.find(sAdapter); - if(it != _adapter_servant.end()) - { - return it->second; - } - return "(NO TARS PROTOCOL)"; - } - - /** - * æ ¹æ®servantå称获å–adapterå称 - * @param sServant - * @return string - */ - string getServantAdapter(const string& sServant) - { - map::iterator it = _servant_adapter.find(sServant); - if(it != _servant_adapter.end()) - { - return it->second; - } - return ""; - } - - /** - * 获å–Adapter/Servant对应表 - * @return map - */ - map getAdapterServant() {return _adapter_servant;} - - /** - * è®¾ç½®æŸ“è‰²ä¿¡æ¯ - * @param params: notify的输入å‚æ•° - * @param sDyeingKey: 用户å·ç  - * @param sDyeingServant: 对象å称 - * @param sDyeingInterface:接å£å称 - * @return string: 设置结果 - */ - bool setDyeing(const string & sDyeingKey, const string & sDyeingServant, const string & sDyeingInterface); - - /** - * 是å¦æ˜¯æŸ“色的请求 - * @param sKey: 用户å·ç  - * @param sServant: 对象å称 - * @param sInterface:接å£å称 - * @return string: 设置结果 - */ - bool isDyeingReq(const string & sKey, const string & sServant, const string & sInterface); - - /** - * 是å¦æ˜¯å·²ç»è¢«æŸ“色 - */ - bool isDyeing() {return _isDyeing;} - - -protected: - - /** - * Servant生æˆç±» - */ - map _servant_creator; - - /** - * Adapter包å«çš„Servant(Adapterå称:servantå称) - */ - map _adapter_servant; - - /** - * Adapter包å«çš„Servant(Servantå称:Adapterå称) - */ - map _servant_adapter; - -protected: - - /** - * é” - */ - TC_ThreadMutex _mutex; - - /** - * 是å¦æŸ“色 - */ - bool _isDyeing; - - /** - * 染色用户å·ç  - */ - string _dyeingKey; - - /** - * 染色的servant对象 - */ - string _dyeingServant; - - /** - * æŸ“è‰²çš„æŽ¥å£ - */ - string _dyeingInterface; - -}; - -} - -#endif - diff --git a/cpp/servant/servant/ServantProxy.h b/cpp/servant/servant/ServantProxy.h deleted file mode 100644 index 0ffca856f..000000000 --- a/cpp/servant/servant/ServantProxy.h +++ /dev/null @@ -1,704 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _TARS_SERVANT_PROXY_H_ -#define _TARS_SERVANT_PROXY_H_ - -#include "util/tc_monitor.h" -#include "util/tc_autoptr.h" -#include "servant/Message.h" -#include "servant/AppProtocol.h" -#include "servant/TarsCurrent.h" -#include "servant/EndpointInfo.h" -#include "servant/CommunicatorEpoll.h" - -namespace tars -{ - -///////////////////////////////////////////////////////////////////////// -/* - * seq 管ç†çš„ç±» - */ -class SeqManager -{ -public: - const static uint16_t MAX_UNSIGN_SHORT = 0xffff; - - struct SeqInfo - { - uint16_t next; - bool free; - }; - - /** - * 构造函数 - */ - SeqManager(size_t iNum); - - /** - * 获å–seq - */ - uint16_t get(); - - /** - * 删除seq - */ - void del(uint16_t iSeq); - -private: - uint16_t _num; - - uint16_t _free; - - uint16_t _freeTail; - - SeqInfo * _p; -}; - -///////////////////////////////////////////////////////////////////////// -/* - * 线程ç§æœ‰æ•°æ® - */ -class ServantProxyThreadData : public TC_ThreadPool::ThreadData -{ -public: - static TC_ThreadMutex _mutex; //å…¨å±€çš„äº’æ–¥é” - static pthread_key_t _key; //ç§æœ‰çº¿ç¨‹æ•°æ®key - static SeqManager * _pSeq; //生æˆseq的管ç†ç±» - - /** - * 构造函数 - */ - ServantProxyThreadData(); - - /** - * æžæž„函数 - */ - ~ServantProxyThreadData(); - - /** - * æ•°æ®èµ„æºé‡Šæ”¾ - * @param p - */ - static void destructor(void* p); - - /** - * 获å–线程数æ®ï¼Œæ²¡æœ‰çš„è¯ä¼šè‡ªåŠ¨åˆ›å»º - * @return ServantProxyThreadData* - */ - static ServantProxyThreadData * getData(); - -public: - /* - * æ¯ä¸ªçº¿ç¨‹è·Ÿå®¢æˆ·ç«¯ç½‘络线程通信的队列 - */ - ReqInfoQueue * _reqQueue[MAX_CLIENT_THREAD_NUM]; //队列数组 - bool _queueInit; //是å¦åˆå§‹åŒ– - size_t _reqQNo; //请求事件通知的seq - size_t _netSeq; //轮训选择网络线程的åç§»é‡ - int _netThreadSeq; //网络线程å‘起的请求回到自己的网络线程æ¥å¤„ç†,其值为网络线程的id - - /** - * hash属性,客户端æ¯æ¬¡è°ƒç”¨éƒ½è¿›è¡Œè®¾ç½® - */ - bool _hash; //是å¦æ™®é€šå–模hash - bool _conHash; //是å¦ä¸€è‡´æ€§hash - int64_t _hashCode; //hash值 - - /** - * æŸ“è‰²ä¿¡æ¯ - */ - bool _dyeing; //标识当å‰çº¿ç¨‹æ˜¯å¦éœ€è¦æŸ“色 - string _dyeingKey; //染色的key值 - - /** - * å…许客户端设置接å£çº§åˆ«çš„超时时间,包括åŒæ­¥å’Œå¼‚æ­¥ã€å•å‘ - */ - bool _hasTimeout; //是å¦è®¾ç½®è¶…时间 - int _timeout; //超时时间 - - /** - * ä¿å­˜è°ƒç”¨åŽç«¯æœåŠ¡çš„地å€ä¿¡æ¯ - */ - char _szHost[64]; //è°ƒç”¨å¯¹ç«¯åœ°å€ - - /** - * ObjectProxy - */ - size_t _objectProxyNum; //ObjectProxy对象的个数,其个数由客户端的网络线程数决定,æ¯ä¸ªç½‘络线程有一个ObjectProxy - ObjectProxy ** _objectProxy; //ä¿å­˜ObjectProxy对象的指针数组 - - /** - * å程调度 - */ - CoroutineScheduler* _sched; //å程调度器 - - /** - * 采样 - */ - SampleKey _sampleKey; //é‡‡æ ·ä¿¡æ¯ -}; - - -////////////////////////////////////////////////////////////////////////// -// å程并行请求的基类 -class CoroParallelBase : virtual public TC_HandleBase -{ -public: - /** - * 构造 - */ - CoroParallelBase(int iNum) - : _num(iNum) - , _count(iNum) - , _req_count(0) - {} - - /** - * æžæž„函数 - */ - virtual ~CoroParallelBase() {} - - /** - * 增加调用å程接å£è¯·æ±‚çš„æ•°ç›® - */ - int incReqCount() { return _req_count.inc(); } - - /** - * 判断å程并行请求数目是å¦éƒ½å‘é€äº† - */ - bool checkAllReqSend() { return _num == _req_count.get(); } - - /** - * 判断å程并行请求是å¦éƒ½å›žæ¥äº† - */ - bool checkAllReqReturn() { return _count.dec_and_test(); } - - /** - * 获å–所有请求回æ¥çš„å“应 - */ - vector getAllReqMessage() - { - vector vRet; - - { - TC_LockT lock(_mutex); - vRet = _vReqMessage; - _vReqMessage.clear(); - } - - return vRet; - } - - /** - * æ’入请求回æ¥çš„å“应 - */ - void insert(ReqMessage* msg) - { - TC_LockT lock(_mutex); - _vReqMessage.push_back(msg); - } - -protected: - /** - * 并行请求的数目 - */ - int _num; - - /** - * 并行请求的å“应还未回æ¥çš„æ•°ç›® - */ - TC_Atomic _count; - - /** - * 并行请求的已å‘é€çš„æ•°ç›® - */ - TC_Atomic _req_count; - - /** - * äº’æ–¥é” - */ - TC_ThreadMutex _mutex; - - /** - * 请求的å“应的容器 - */ - vector _vReqMessage; -}; -typedef TC_AutoPtr CoroParallelBasePtr; - -//ç­‰å¾…æ‰€æœ‰çš„è¯·æ±‚å›žæ¥ -void coroWhenAll(const CoroParallelBasePtr &ptr); - -////////////////////////////////////////////////////////////////////////// -// 异步回调对象的基类 -class ServantProxyCallback : virtual public TC_HandleBase -{ -public: - /** - * 构造 - */ - ServantProxyCallback(); - - /** - * æžæž„函数 - */ - virtual ~ServantProxyCallback() {} - - /** - * 获å–类型 - * @return const string& - */ - virtual const string& getType() { return _type; } - - /** - * 设置类型 - * @return const string& - */ - virtual void setType(const string& type) { _type = type; } - - /** - * 设置coro并行请求的共享智能指针 - */ - virtual void setCoroParallelBasePtr(tars::CoroParallelBasePtr pPtr) { _pPtr = pPtr; } - - /** - * 获å–coro并行请求的共享智能指针 - */ - virtual const tars::CoroParallelBasePtr& getCoroParallelBasePtr() { return _pPtr; } - - /** - * 异步请求是å¦åœ¨ç½‘ç»œçº¿ç¨‹å¤„ç† - * tars内部用的到 业务ä¸èƒ½è®¾ç½®è¿™ä¸ªå€¼ - * */ - inline void setNetThreadProcess(bool bNetThreadProcess) - { - _bNetThreadProcess = bNetThreadProcess; - } - - inline bool getNetThreadProcess() - { - return _bNetThreadProcess; - } - -public: - /** - * å¼‚æ­¥å›žè°ƒå¯¹è±¡å®žçŽ°è¯¥æ–¹æ³•ï¼Œè¿›è¡Œä¸šåŠ¡é€»è¾‘å¤„ç† - * @param msg - * @return int - */ - virtual int onDispatch(ReqMessagePtr msg) = 0; - -protected: - - /** - * åŒä¸€é“¾è·¯å¤šä¸ªcb的时候å¯ä»¥ç”¨æ¥åŒºåˆ†class类型 - */ - string _type; - - /** - * 异步请求是å¦åœ¨ç½‘ç»œçº¿ç¨‹å¤„ç† - * tars内部用的到 业务ä¸èƒ½è®¾ç½®è¿™ä¸ªå€¼ - * */ - bool _bNetThreadProcess; - - /** - * å程并行请求的共享智能指针 - */ - tars::CoroParallelBasePtr _pPtr; -}; - -// for http -class HttpCallback : public TC_HandleBase -{ -public: - virtual ~HttpCallback() {} - virtual int onHttpResponse(const std::map& requestHeaders , - const std::map& responseHeaders , - const std::vector& rspBody) = 0; - virtual int onHttpResponseException(const std::map& requestHeaders, - int expCode) = 0; -}; - -class HttpServantProxyCallback : virtual public ServantProxyCallback -{ -public: - explicit - HttpServantProxyCallback(HttpCallback* cb); - - virtual int onDispatch(ReqMessagePtr msg); - -private: - TC_AutoPtr _httpCb; -}; - - -////////////////////////////////////////////////////////////////////////// -/** - * 1:è¿œç¨‹å¯¹è±¡çš„æœ¬åœ°ä»£ç† - * 2:åŒåservant在一个通信器中最多åªæœ‰ä¸€ä¸ªå®žä¾‹ - * 3:防止和用户在Tars中定义的函数å冲çªï¼ŒæŽ¥å£ä»¥tars_开头 - */ -class EndpointManagerThread; - -class ServantProxy : public TC_HandleBase, public TC_ThreadMutex -{ -public: - /** - * 通过status传递数æ®æ—¶ç”¨åˆ°çš„缺çœå­—符串 - */ - static string STATUS_DYED_KEY; //需è¦æŸ“色的用户ID - - static string STATUS_GRID_KEY; //需è¦ç°åº¦çš„用户ID - - static string STATUS_SAMPLE_KEY; //stat é‡‡æ ·çš„ä¿¡æ¯ - - static string STATUS_RESULT_CODE; //处ç†ç»“æžœç ,tup使用 - - static string STATUS_RESULT_DESC; //处ç†ç»“æžœæè¿°,tup使用 - - static string STATUS_SETNAME_VALUE; //set调用 - - static string TARS_MASTER_KEY; //é€ä¼ ä¸»è°ƒåç§°ä¿¡æ¯ - - /** - * 缺çœçš„åŒæ­¥è°ƒç”¨è¶…时时间 - * 超时åŽä¸ä¿è¯æ¶ˆæ¯ä¸ä¼šè¢«æœåŠ¡ç«¯å¤„ç† - */ - enum { DEFAULT_SYNCTIMEOUT = 3000, DEFAULT_ASYNCTIMEOUT=5000}; - - /** - * 构造函数 - * @param op - */ - ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObjectProxy, size_t iClientThreadNum); - - /** - * æžæž„函数 - */ - virtual ~ServantProxy(); -public: - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 如果å¯ç”¨set则获å–本setçš„,如果å¯ç”¨åˆ†ç»„,åªè¿”回åŒåˆ†ç»„çš„æœåŠ¡ç«¯ip - * @return void - */ - void tars_endpoints(vector &activeEndPoint, vector &inactiveEndPoint); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 所有的列表 - * @return void - */ - void tars_endpointsAll(vector &activeEndPoint, vector &inactiveEndPoint); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 æ ¹æ®setåå­—èŽ·å– - * @return void - */ - void tars_endpointsBySet(const string & sName,vector &activeEndPoint, vector &inactiveEndPoint); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 æ ¹æ®åœ°åŒºåå­—èŽ·å– - * @return void - */ - void tars_endpointsByStation(const string & sName,vector &activeEndPoint, vector &inactiveEndPoint); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 包括指定归属地 - * @return vector - **/ - vector tars_endpoints(const std::string & sStation); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 包括指定归属地 - * @return void - */ - void tars_endpoints(const std::string & sStation, vector & vecActive, vector & vecInactive); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 如果å¯ç”¨åˆ†ç»„,åªè¿”回åŒåˆ†ç»„çš„æœåŠ¡ç«¯ip - * @return vector - */ - vector getEndpoint(); - - /** - * 获å–Objectå¯ç”¨æœåŠ¡åˆ—表 包括所有IDC - * @return vector - */ - vector getEndpoint4All(); - -public: - - /** - * 获å–通信器 - * - * @return Communicator* - */ - Communicator* tars_communicator() { return _communicator; } - - /** - * å‘é€æµ‹è¯•æ¶ˆæ¯åˆ°æœåŠ¡å™¨ - */ - void tars_ping(); - - /** - * 设置åŒæ­¥è°ƒç”¨è¶…时时间,对该proxy上所有方法都有效 - * @param msecond - */ - void tars_timeout(int msecond); - - /** - * 获å–åŒæ­¥è°ƒç”¨è¶…时时间,对该proxy上所有方法都有效 - * @return int - */ - int tars_timeout() const; - - /** - * 设置连接超时时间 - * @return int - */ - void tars_connect_timeout(int conTimeout); - - /** - * 获å–所属的Objectå称 - * @return string - */ - string tars_name() const; - - /** - * 获å–最近一次调用的IP地å€å’Œç«¯å£ - * @return string - */ - static TC_Endpoint tars_invoke_endpoint(); - - /** - * 设置用户自定义åè®® - * @param protocol - */ - void tars_set_protocol(const ProxyProtocol& protocol, const std::string& protoName = "tars"); - - /** - *设置套接字选项 - */ - void tars_set_sockopt(int level, int optname, const void *optval, socklen_t optlen); - - /** - * 设置超时检查å‚æ•° - */ - void tars_set_check_timeout(const CheckTimeoutInfo& checkTimeoutInfo); - - /** - * 获å–超时检查å‚æ•° - */ - CheckTimeoutInfo tars_get_check_timeout(); - - /** - * hash方法,为ä¿è¯ä¸€æ®µæ—¶é—´å†…åŒä¸€ä¸ªkey的消æ¯å‘é€ - * 到相åŒçš„æœåŠ¡ç«¯ï¼Œç”±äºŽæœåŠ¡åˆ—表动æ€å˜åŒ–,所以 - * ä¸ä¸¥æ ¼ä¿è¯ - * @param key - * @return ServantProxy* - */ - virtual ServantProxy* tars_hash(int64_t key); - - /** - * 一致性hash方法 - */ - virtual ServantProxy* tars_consistent_hash(int64_t key); - - /** - * 清除当å‰çš„Hashæ•°æ® - * 空函数 为了兼容以å‰çš„ - * @param key - * @return ServantProxy* - */ - void tars_clear_hash(); - - /** - * 针对客户端调用接å£çº§åˆ«çš„超时时间设置,包括åŒæ­¥å’Œå¼‚步调用 - * æ¯æ¬¡æŽ¥å£è°ƒç”¨éƒ½å¿…须设置,å¦åˆ™å–系统默认超时时间 - * - * @param msecond å•ä½æ¯«ç§’ - * @return ServantProxy* - * 示例: prxy->tars_set_timeout(3000)->sayHello(); - */ - virtual ServantProxy* tars_set_timeout(int msecond); - - /** - * 设置异步调用超时时间,对该proxy上所有方法都有效 - * @param msecond - */ - void tars_async_timeout(int msecond); - - /** - * 获å–异步调用超时时间,对该proxy上所有方法都有效 - * @return int - */ - int tars_async_timeout() const; - - /** - * 用proxy产生一个该object上的åºåˆ—å· - * @return uint32_t - */ - virtual uint32_t tars_gen_requestid(); - - /** - * 设置PUSH类消æ¯çš„å“应callback - * @param cb - */ - virtual void tars_set_push_callback(const ServantProxyCallbackPtr& cb); - - /** - * TARSåè®®åŒæ­¥æ–¹æ³•è°ƒç”¨ - */ - virtual void tars_invoke(char cPacketType, - const string& sFuncName, - const vector &buf, - const map& context, - const map& status, - ResponsePacket& rep); - - /** - * TARSå议异步方法调用 - */ - virtual void tars_invoke_async(char cPacketType, - const string& sFuncName, - const vector &buf, - const map& context, - const map& status, - const ServantProxyCallbackPtr& callback, - bool bCoro = false); - - /** - * 普通åè®®åŒæ­¥è¿œç¨‹è°ƒç”¨ - */ - virtual void rpc_call(uint32_t requestId, const string& sFuncName, - const char* buff, uint32_t len, ResponsePacket& rsp); - - /** - * 普通å议异步调用 - */ - virtual void rpc_call_async(uint32_t requestId, const string& sFuncName, - const char* buff, uint32_t len, - const ServantProxyCallbackPtr& callback, - bool bCoro = false); - - /** - * http1 or 2åè®®åŒæ­¥è¿œç¨‹è°ƒç”¨ - */ - void http_call(const std::string& method, - const std::string& uri, - const std::map& headers, - const std::string& body, - std::map& rheaders, - std::string& rbody); - /** - * http2å议异步远程调用 - */ - void http_call_async(const std::map& headers, - const std::string& body, - HttpCallback* cb); - - /** - * 在RequestPacket中的context设置主调信æ¯æ ‡è¯† - */ - virtual void tars_setMasterFlag(bool bMasterFlag) {_masterFlag = bMasterFlag;} - - -private: - /** - * 远程方法调用 - * @param req - * @return int - */ - void invoke(ReqMessage * msg, bool bCoroAsync = false); - - /** - * 远程方法调用返回 - * @param req - * @return int - */ - void finished(ReqMessage * msg); - - /** - * 选å–ä¸€ä¸ªç½‘ç»œçº¿ç¨‹å¯¹åº”çš„ä¿¡æ¯ - * @param pSptd - * @return void - */ - void selectNetThreadInfo(ServantProxyThreadData * pSptd, ObjectProxy * & pObjProxy, ReqInfoQueue * & pReqQ); - - /** - * 检查是å¦éœ€è¦è®¾ç½®æŸ“è‰²æ¶ˆæ¯ - * @param req - */ - void checkDye(RequestPacket& req); -private: - friend class ObjectProxy; - friend class AdapterProxy; - - /** - * 通信器 - */ - Communicator * _communicator; - - /** - * ä¿å­˜ObjectProxy对象的指针数组 - */ - ObjectProxy ** _objectProxy; - - /** - * ObjectProxy对象的个数,其个数由客户端的网络线程数决定, - * æ¯ä¸ªç½‘络线程有一个ObjectProxy - */ - size_t _objectProxyNum; - - /** - * åŒæ­¥è°ƒç”¨è¶…æ—¶(毫秒) - */ - int _syncTimeout; - - /** - * åŒæ­¥è°ƒç”¨è¶…æ—¶(毫秒) - */ - int _asyncTimeout; - - /** - * 唯一id - */ - uint32_t _id; - - /** - * 获å–endpoint对象 - */ - std::unique_ptr _endpointInfo; - - /** - * 是å¦åœ¨RequestPacket中的contextè®¾ç½®ä¸»è°ƒä¿¡æ¯ - */ - bool _masterFlag; - - /** - *æ¯ä¸ªä¸šåŠ¡çº¿ç¨‹ä¸Žæ¯ä¸ªå®¢æˆ·ç«¯ç½‘ç»œçº¿ç¨‹ä¹‹é—´é€šä¿¡çš„è¯·æ±‚é˜Ÿåˆ—å¤§å° - */ - int _queueSize; - - /* - *最å°çš„超时时间 - */ - int64_t _minTimeout; -}; -} -#endif diff --git a/cpp/servant/servant/ServantProxyFactory.h b/cpp/servant/servant/ServantProxyFactory.h deleted file mode 100644 index 57d783217..000000000 --- a/cpp/servant/servant/ServantProxyFactory.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_SERVANT_PROXY_FACTORY_H_ -#define __TARS_SERVANT_PROXY_FACTORY_H_ - -#include "servant/Global.h" -#include "servant/Communicator.h" -#include "servant/ServantProxy.h" - -namespace tars -{ -///////////////////////////////////////////////////////////////////////////////////////// -/** - * 创建ServantProxy对象,æ¯ä¸ªobject在进程空间åªæœ‰ä¸€ä¸ªServantProxy实例 - */ -class ServantProxyFactory : public TC_HandleBase, public TC_ThreadRecMutex -{ -public: - /** - * 构造函数 - * @param comm - */ - ServantProxyFactory(Communicator* comm); - - /** - * æžæž„ - */ - ~ServantProxyFactory(); - - /** - * 获å–ServantProxy对象 - * @param name - * @param setName 指定set调用的setid - * @return ServantPrx - */ - ServantPrx::element_type* getServantProxy(const string& name,const string& setName=""); - -private: - /** - * 通信器 - */ - Communicator* _comm; - - /** - * 已创建的对象 - */ - map _servantProxy; -}; -////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/StatReport.h b/cpp/servant/servant/StatReport.h deleted file mode 100644 index d3b3a27cd..000000000 --- a/cpp/servant/servant/StatReport.h +++ /dev/null @@ -1,374 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_STAT_REPORT_H_ -#define __TARS_STAT_REPORT_H_ - -#include "util/tc_thread.h" -#include "util/tc_readers_writer_data.h" -#include "servant/PropertyReport.h" -#include "servant/StatF.h" -#include "servant/PropertyF.h" - -///////////////////////////////////////////////////////////////////////// -/* - 属性上报使用方å¼: - StatReport report; - report->setReportInfo( pPtr,strMasterName,strMasterIp, 5 ); - vector v; - v.push_back(1000); - v.push_back(1001); - v.push_back(1002); - v.push_back(1003); - v.push_back(1004); - - PropertyReportPtr srPtr = report->createPropertyReport("test", PropertyReport::sum(), PropertyReport::dist(v)); - srPtr->report(i); -*/ - -namespace tars -{ - -struct StatSampleMsgHead -{ - string slaveName; - string interfaceName; - string ip; - bool operator <(const StatSampleMsgHead& m)const - { - if(slaveName != m.slaveName) - { - return slaveName < m.slaveName; - } - if(interfaceName != m.interfaceName) - { - return interfaceName < m.interfaceName; - } - return ip < m.ip; - } -}; - -///////////////////////////////////////////////////////////////////////// -/** - * 状æ€ä¸ŠæŠ¥ç±», 上报的信æ¯åŒ…括: - * 1 模å—é—´è°ƒç”¨çš„ä¿¡æ¯ - * 2 业务自定义的属性统计 - */ -class StatReport : public TC_HandleBase, public TC_Thread, public TC_ThreadLock -{ -public: - typedef map MapStatMicMsg; - typedef map MapStatPropMsg; - typedef multimap MMapStatSampleMsg; - typedef TC_LoopQueue stat_queue; - - const static int MAX_MASTER_NAME_LEN = 127; - const static int MAX_MASTER_IP_LEN = 20; - const static int MAX_REPORT_SIZE = 1400; //上报的最大大å°é™åˆ¶ - const static int MIN_REPORT_SIZE = 500; //上报的最å°å¤§å°é™åˆ¶ - const static int STAT_PROTOCOL_LEN = 100; //一次stat mic上报纯å议部分å ç”¨å¤§å°ï¼Œç”¨æ¥æŽ§åˆ¶udp大å°é˜²æ­¢è¶…MTU - const static int PROPERTY_PROTOCOL_LEN = 50; //一次property上纯报å议部分å ç”¨å¤§å°ï¼Œç”¨æ¥æŽ§åˆ¶udp大å°é˜²æ­¢è¶…MTU - const static int MAX_STAT_QUEUE_SIZE = 10000; //上报队列缓存大å°é™åˆ¶ - - enum StatResult - { - STAT_SUCC = 0, - STAT_TIMEOUT = 1, - STAT_EXCE = 2, - }; -public: - /** - * 构造函数 - */ - StatReport(size_t iEpollNum=0); - - /** - * æžå¤Ÿå‡½æ•° - */ - ~StatReport(); - - /** - * åˆå§‹åŒ– - * @param statPrx, 模å—间调用æœåŠ¡å™¨åœ°å€ - * @param propertyPrx, 用户自定义属性æœåŠ¡å™¨åœ°å€ - * @param strModuleName, 模å—å - * @param strModuleIp, 模å—ip - * @param iReportInterval, 上报间隔å•ä½ç§’ - * @param iMaxReporSize一次最大上报包长度。 è·Ÿudp最大å…许包8kã€MTU长度1472有关,暂定å–值范围[500-1400] - * @param iSampleRate, 采样比率1/1000 - * @param iMaxSampleCount, 最大采样数 - * @param iReportTimeout, 上报接å£è°ƒç”¨çš„超时时间 - * @param sContainer, 设置上报的容器å - */ - void setReportInfo(const StatFPrx& statPrx, - const PropertyFPrx& propertyPrx, - const string& strModuleName, - const string& strModuleIp, - const string& strSetDivision, - int iReportInterval = 60, - int iSampleRate = 1000, - unsigned int iMaxSampleCount = 100, - int iMaxReportSize = 1400, - int iReportTimeout = 5000); - - /** - * 设置模å—é—´è°ƒç”¨æ•°æ® - * @param strModuleName, 被调模å—. 一般采用app.servername 例如:Comm.BindServer - * @param setdivision,被调setä¿¡æ¯,=>>MTT.s.s - * @param strInterfaceName,被调接å£.一般为函数å - * @param strModuleIp,被调ip - * @param shSlavePort,被调port - * @param eResult,æˆåŠŸSTAT_SUCC,超时 STAT_TIMEOUT,异常STAT_EXC. - * @param iSptime,耗时 - * @param iReturnValue,返回值 - * @param bFromClient,从客户端采集 false从æœåŠ¡ç«¯é‡‡é›† - * @param sContainer, 设置上报的容器å - * 。 - */ - void report(const string& strModuleName, - const string& setdivision, - const string& strInterfaceName, - const string& strModuleIp, - uint16_t iPort, - StatResult eResult, - int iSptime, - int iReturnValue = 0, - bool bFromClient = true); - - /** - * 设置模å—é—´è°ƒç”¨æ•°æ® - * @param strMasterName 主调用方的åå­— - * @param strMasterIp ä¸»è°ƒç”¨æ–¹çš„åœ°å€ - * @param strSlaveName 被调用方的åå­— - * @param strSlaveIp è¢«è°ƒç”¨æ–¹çš„åœ°å€ - * @param iSlavePort è¢«è°ƒç”¨æ–¹çš„ç«¯å£ - * @param strInterfaceName 被调接å£.一般为函数å - * @param eResult æˆåŠŸSTAT_SUCC,超时 STAT_TIMEOUT,异常STAT_EXC. - * @param iSptime 耗时(å•ä½æ¯«ç§’) - * @param iReturnValue 返回值 - */ - void report(const string& strMasterName, - const string& strMasterIp, - const string& strSlaveName, - const string& strSlaveIp, - uint16_t iSlavePort, - const string& strInterfaceName, - StatResult eResult, - int iSptime, - int iReturnValue = 0); - - /** - * æ ¹æ®å字获å–属性上报对象 - * @param sProperty - * - * @return PropertyReportPtr - */ - PropertyReportPtr getPropertyReport(const string& strProperty) - { - Lock lock(*this); - if(_statPropMsg.find(strProperty) != _statPropMsg.end()) - { - return _statPropMsg[strProperty]; - } - - return NULL; - } - - /** - * 生æˆå±žæ€§ä¸ŠæŠ¥å¯¹è±¡ - * @param strProperty - * @param t1 - * - * @return PropertyReportPtr - */ - template - PropertyReportPtr createPropertyReport(const string& strProperty, Args... args) - { - Lock lock(*this); - - if(_statPropMsg.find(strProperty) != _statPropMsg.end()) - { - return _statPropMsg[strProperty]; - } - - PropertyReportPtr srPtr = new PropertyReportImp(std::forward(args)...); - - _statPropMsg[strProperty] = srPtr; - - return srPtr; - } - -public: - void report(size_t iSeq,MapStatMicMsg * pmStatMicMsg); - - /* - * 获å–statä»£ç† - */ - StatFPrx getStatPrx() {return _statPrx; } - - /* - * 采样 - */ - void doSample(const string& strSlaveName, - const string& strInterfaceName, - const string& strSlaveIp, - map &status); - /* - * 采样id - */ - string sampleUnid(); - - /** - * 增加关注时间点. 调用方å¼addStatInterv(5) - * @param strTimePoint 时间点åºåˆ— - * @return - */ - void addStatInterv(int iInterv); - - /** - * é‡ç½®å…³æ³¨æ—¶é—´ç‚¹. - * @return - */ - void resetStatInterv(); - - /** - * - * @param body - * @param time - * @return void - */ - void getIntervCount(int time,StatMicMsgBody& body); - - /** - * 结æŸçº¿ç¨‹ - */ - void terminate(); - - /** - * 轮询函数 - */ - virtual void run(); -public: - /** - * é™åˆ¶é•¿åº¦ - * @param str - * @param limitlen - * - * @return string - */ - static string trimAndLimitStr(const string& str, uint32_t limitlen); - - /* - * func: tars.s.1 =>> MTT s 1 - */ - static bool divison2SetInfo(const string& str, vector& vtSetInfo); - - /* - * func: tars.xxxServer =>> xxxServer - */ - static string getServerName(string sModuleName); - -private: - - /** - * - * @param head - * @param body - * @param msg - * @param bFromClient - */ - void submit(StatMicMsgHead& head, StatMicMsgBody& body, bool bFromClient); - - /** - * 上报模å—é—´è°ƒç”¨ä¿¡æ¯ Mic = module interval call - * @param msg - * @param bFromClient 是å¦ä¸ºå®¢æˆ·ç«¯ä¸ŠæŠ¥ - * @return int - */ - int reportMicMsg(MapStatMicMsg &msg, bool bFromClient); - - /** - * ä¸ŠæŠ¥å±žæ€§ä¿¡æ¯ Prop = property - * @return int - */ - int reportPropMsg(); - - /** - * ä¸ŠæŠ¥å¤šç»´åº¦å±žæ€§ä¿¡æ¯ Prop = property - * @return int - */ - int reportPropPlusMsg(); - - /** - * stat 采样 - */ - int reportSampleMsg(); - - - //åˆå¹¶ä¸¤ä¸ªMicMsg - void addMicMsg(MapStatMicMsg & old,MapStatMicMsg & add); - -private: - time_t _time; - - int _reportInterval; - - int _reportTimeout; - - int _maxReportSize; - - bool _terminate; - - string _moduleName; - - string _setName; // 主调上报,表示主调setå,被调上报,表示被调setå - - string _setArea; // - - string _setID; // - - string _ip; - - int _sampleRate; //生æˆæ¨¡å—间调用时åºå›¾çš„采样比率 - - unsigned int _maxSampleCount; //1分钟内最大采样æ¡æ•° - - StatFPrx _statPrx; - - MapStatMicMsg _statMicMsgClient; - - MapStatMicMsg _statMicMsgServer; - - MMapStatSampleMsg _statSampleMsg; - - vector _timePoint; - - PropertyFPrx _propertyPrx; - - map _statPropMsg; - -private: - - size_t _epollNum; - vector _statMsg; - - size_t _retValueNumLimit; - -}; -/////////////////////////////////////////////////////////// -} - -#endif diff --git a/cpp/servant/servant/TarsConfig.h b/cpp/servant/servant/TarsConfig.h deleted file mode 100644 index 3bea0dfb4..000000000 --- a/cpp/servant/servant/TarsConfig.h +++ /dev/null @@ -1,154 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_CONFIG_H_ -#define __TARS_CONFIG_H_ - -#include "util/tc_autoptr.h" -#include "util/tc_singleton.h" -#include "servant/Global.h" -#include "servant/ConfigF.h" - -using namespace std; - -namespace tars -{ - -/** - * - * 功能:获å–远程系统é…置,生æˆæœ¬åœ°æ–‡ä»¶ï¼Œæ”¯æŒå¤‡ä»½å’Œå›žæ»š - * - * 说明:该对象åªå®žçŽ°å°†ConfigServer上指定的é…置文件 - * 读å–到本地(ä¿å­˜åˆ°åº”用程åºæ‰§è¡Œç›®å½•),具体é…ç½®è§£æž - * 由客户端实现 - * - * 客户端通过调用addConfig接å£ä¸º - * æ¯ä¸ªé…置文件创建一个TarsRemoteConfig实例 - * - * 备份文件数目在对象创建时指定,缺çœä¸º5个, - * 能回滚的次数等于备份文件数目 - * - */ - -class TarsRemoteConfig : public TC_Singleton -{ -public: - /** - * åˆå§‹åŒ– - * @param comm, 通信器 - * @param obj, 对象å称 - * @param app, 应用å称 - * @param serverName, æœåŠ¡å称 - * @param basePath, 基本路径 - * @param maxBakNum, 最大备份文件个数 - * - * @return int - */ - int setConfigInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string& basePath,const string& setdivision="",int maxBakNum=5); - - /** - * 读å–ConfigServer上é…置文件到本地,并备份原文件 - * @param sFullFileName 文件å称 - * @param result 结果 - * @param bAppOnly 是å¦åªèŽ·å–应用级别的é…ç½® - * - * @return bool - */ - bool addConfig(const string & filename, string &result, bool bAppConfigOnly = false); - -private: - /** - * 实现请求ConfigServer并将结果以文件形å¼ä¿å­˜åˆ°æœ¬åœ°ç›®å½• - * @param sFullFileName 文件å称 - * @param bAppOnly 是å¦åªèŽ·å–应用级别的é…ç½® - * - * @return string 生æˆçš„文件å称 - */ - string getRemoteFile(const string & sFullFileName, bool bAppConfigOnly = false); - - /** - * 实现本地文件的回滚,å¯å›žæ»šæ¬¡æ•°ç­‰äºŽæœ€å¤§å¤‡ä»½æ–‡ä»¶æ•°ï¼Œæ¯æ¬¡ - * 都使用最近的备份文件覆盖当å‰é…置文件 - * - * @return string - */ - string recoverSysConfig(const string & sFullFileName); - - /** - * 备份文件å称 Config.conf.1.bak,Config.conf.2.bak ... - * 该方法æ供下标到文件å的转化 - * - * @param index 第几个备份文件 - * - * @return string é…置文件全路径 - */ - inline string index2file(const string & sFullFileName, int index); - - /** - * rename系统æ“作的å°è£…,当oldFileä¸å­˜åœ¨æ—¶æŠ›å‡ºå¼‚常 - * - * @param oldFile 原文件路径和å称 - * @param newFile 新文件逻辑和å称 - */ - inline void localRename(const string& oldFile, const string& newFile); - -protected: - - /** - * 通信器 - */ - CommunicatorPtr _comm; - - /** - * é…ç½®ä»£ç† - */ - ConfigPrx _configPrx; - - /** - * 应用 - */ - string _app; - - /** - * æœåŠ¡å称 - */ - string _serverName; - - /** - * 路径 - */ - string _basePath; - - /** - * setä¿¡æ¯ - */ - - string _setdivision; - - /** - * 最大备份数 - */ - int _maxBakNum; - - /** - * çº¿ç¨‹é” - */ - TC_ThreadMutex _mutex; -}; - -} - -#endif diff --git a/cpp/servant/servant/TarsCurrent.h b/cpp/servant/servant/TarsCurrent.h deleted file mode 100644 index 5475314b4..000000000 --- a/cpp/servant/servant/TarsCurrent.h +++ /dev/null @@ -1,308 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_CURRENT_H_ -#define __TARS_CURRENT_H_ - -#include "util/tc_epoll_server.h" -#include "tup/RequestF.h" -#include "servant/BaseF.h" - -namespace tars -{ - -class ServantHandle; - -////////////////////////////////////////////////////////////// -/** - * 当å‰è¯·æ±‚的上下文 - */ -class TarsCurrent : public TC_HandleBase -{ -public: - typedef std::map TARS_STATUS; - - typedef std::vector TARS_BUFFER; - - /** - * 构造函数 - * @param pServantHandle - */ - TarsCurrent(ServantHandle *pServantHandle); - - /** - * æžæž„ - */ - ~TarsCurrent(); - - /** - * 获å–IP - * @return string - */ - string getIp() const; - - /** - * 获å–ç«¯å£ - * @return int - */ - int getPort() const; - - /** - * 获å–uid - * @return uint32 - */ - uint32_t getUId() const; - - /** - * 获å–fd - * @return int - */ - int getFd() const { return _fd; } - - /** - * 是å¦å‡½æ•°è¿”回时å‘é€å“应包给客户端 - * @return bool - */ - bool isResponse() const; - - /** - * 设置连接的关闭类型,详情å‚看TC_EpollServer::EM_CLOSE_T - */ - void setCloseType(int type); - - /** - * 获å–连接关闭类型,详情请å‚考TC_EpollServer::EM_CLOSE_T类型 - */ - int getCloseType() const; - - /** - * 设置是å¦è‡ªåŠ¨å›žå“应包 - */ - void setResponse(bool value) { _response = value; } - - /** - * 设置返回的context(ä»…TARSå议有效) - */ - void setResponseContext(const map & context){_responseContext = context;} - - /** - * 获å–返回的context(ä»…TARSå议有效) - */ - const map & getResponseContext() const {return _responseContext;} - - /** - * 关闭当å‰è¿žæŽ¥ - */ - void close(); - - /** - * 获å–所属的ServantHandle - */ - ServantHandle* getServantHandle(); - - /** - * 获å–æ¥æºçš„Adapter - * @return TC_EpollServer::BindAdapter* - */ - TC_EpollServer::BindAdapter* getBindAdapter(); - - /** - * 获å–请求buffer - * @return string - */ - const vector &getRequestBuffer() const; - - /** - * 获å–æœåŠ¡Servantå称 - * @return string - */ - string getServantName() const; - - /** - * 请求的å议的版本å·(ä»…TARSå议有效) - * - * @return short - */ - short getRequestVersion() const; - - /** - * 扩展map(ä»…TARSå议有效) - * @return map& - */ - map& getContext(); - - /** - * 获å–ä¿å­˜çŠ¶æ€ä¿¡æ¯ï¼Œæ¯”如染色等(ä»…TARSå议有效) - * @return map& - */ - const map& getRequestStatus() const; - - /** - * 函数å称(ä»…TARSå议有效) - * @return string - */ - string getFuncName() const; - - /** - * 请求ID(ä»…TARSå议有效) - * @return int - */ - uint32_t getRequestId() const; - - /** - * 获å–包类型(ä»…TARSå议有效) - * @return char - */ - char getPacketType() const; - - /** - * 获å–消æ¯ç±»åž‹(ä»…TARSå议有效) - * @return tars::Int32 - */ - tars::Int32 getMessageType() const; - - /** - * 获å–接收到请求的时间 - */ - struct timeval getRecvTime() const; - - /** - * 设置是å¦ä¸ŠæŠ¥çŠ¶æ€æŠ¥å‘Š - */ - void setReportStat(bool bReport); - - /** - * tarså议的å‘é€å“应数æ®(ä»…TARSå议有效) - * @param iRet - * @param status - * @param buffer - */ - void sendResponse(int iRet, const vector& buffer = TARS_BUFFER(), - const map& status = TARS_STATUS(), - const string & sResultDesc = ""); - - /** - * 普通å议的å‘é€å“应数æ®(éžTARSå议有效) - * @param buff - * @param len - */ - void sendResponse(const char* buff, uint32_t len); - -protected: - - friend class ServantHandle; - - friend class Application; - - /** - * åˆå§‹åŒ– - * @param stRecvData - */ - void initialize(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * åˆå§‹åŒ– - * @param stRecvData - * @param beginTime - */ - void initialize(const TC_EpollServer::tagRecvData &stRecvData, int64_t beginTime); - - /** - * åˆå§‹åŒ– - * @param stRecvData - */ - void initializeClose(const TC_EpollServer::tagRecvData &stRecvData); - - /** - * åˆå§‹åŒ– - * @param sRecvBuffer - */ - void initialize(const string &sRecvBuffer); - - /** - * æœåŠ¡ç«¯ä¸ŠæŠ¥çŠ¶æ€ï¼Œé’ˆå¯¹å•å‘调用åŠTUP调用(仅对TARSå议有效) - */ - void reportToStat(const string & sObj); - -protected: - /** - * æ“作类指针 - */ - ServantHandle* _servantHandle; - - /** - * 消æ¯_bindAdapter - */ - TC_EpollServer::BindAdapter* _bindAdapter; - - /** - * 唯一标识 - */ - uint32_t _uid; - - /** - * ipåœ°å€ - */ - string _ip; - - /** - * ç«¯å£ - */ - int _port; - - /** - * 用于回包时选择网络线程 - */ - int _fd; - - /** - * 客户端请求包 - */ - RequestPacket _request; - - /** - * å“应 - */ - bool _response; - - /** - * 收到请求时间 - */ - int64_t _begintime; - - /** - * 接å£å¤„ç†çš„返回值 - */ - int _ret; - - /** - * 是å¦ä¸ŠæŠ¥stat - */ - bool _reportStat; - - /** - * 连接关闭的类型,åˆå§‹å€¼æ˜¯-1,éžæ³•å€¼ - */ - int _closeType; - - /** - * 设置é¢å¤–返回的内容 - */ - map _responseContext; -}; -////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/servant/TarsLogger.h b/cpp/servant/servant/TarsLogger.h deleted file mode 100644 index 600fcfb15..000000000 --- a/cpp/servant/servant/TarsLogger.h +++ /dev/null @@ -1,879 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_LOGGER_H__ -#define __TARS_LOGGER_H__ - -#include "util/tc_logger.h" -#include "util/tc_file.h" -#include "util/tc_singleton.h" -#include "servant/Global.h" -#include "servant/LogF.h" -#include "servant/PropertyReport.h" - -#define DYEING_DIR "tars_dyeing" -#define DYEING_FILE "dyeing" - -namespace tars -{ - -/** - * LOG的库说明: - * 1 循环日志采用TLOGERROR(...),TLOGDEBUG(...) - * 2 循环日志ä¸ä¸Šä¼ åˆ°æœåŠ¡å™¨ - * 3 按天日志采用DLOG, FDLOGæ¥è®°å½• - * 4 按天日志也å¯ä»¥ä¸ä¸Šä¼ åˆ°è¿œç¨‹æœåŠ¡å™¨:DLOG("")->disableRemote(); - * 5 按天日志å¯ä»¥æ”¹å˜æ¯å¤©ä¸€ä¸ªæ–‡ä»¶çš„æ–¹å¼: - * DLOG("abc3")->setFormat("%Y%m%d%H"); - * æ¯ä¸ªå°æ—¶ä¸€ä¸ªæ–‡ä»¶ - */ - -/***************************************************************************** -实现方å¼è¯´æ˜Ž(åªä»‹ç»æŒ‰æ—¶é—´çš„日志, 会写到tarslog): - 1 自定义时间日志的WriteTç±»:RemoteTimeWriteT - 2 在RemoteTimeWriteT类中, 写入到远程 - 3 定义远程日志类:typedef TC_Logger RemoteTimeLogger; - 4 为了ä¿è¯è¿œç¨‹çš„写日志也是在å•ç‹¬çº¿ç¨‹å¤„ç†,é‡æ–°å®šä¹‰æœ¬åœ°æŒ‰å¤©æ—¥å¿—ç±» - 5 自定义时间日志的WriteTç±»:TimeWriteT - 6 在TimeWriteT类中包å«RemoteTimeLogger对象 - 7 在TimeWriteT类的写入æ“作中, 写入本地文件åŽ, åŒæ—¶å†™å…¥åˆ°RemoteTimeLogger对象中 - 8 RemoteTimeLogger会在RemoteTimeWriteT对象中, 异步写入到远程 - 9 从而本地文件写和远程写ä¸åœ¨ä¸€ä¸ªçº¿ç¨‹ä¸­. -*****************************************************************************/ - -/////////////////////////////////////////////////////////////////////////////// - -class RollWriteT -{ -public: - RollWriteT(); - ~RollWriteT(); - - void operator()(ostream &of, const deque > &ds); - - void setDyeingLogInfo(const string &sApp, const string &sServer, const string & sLogPath, - int iMaxSize, int iMaxNum, const CommunicatorPtr &comm, const string & sLogObj); - -protected: - - TC_RollLogger *_dyeingRollLogger; - - static int _dyeingThread; - - string _app; - string _server; - string _logPath; - int _maxSize; - int _maxNum; - - /** - * æŸ“è‰²è¿œç¨‹æ»šåŠ¨æ—¥å¿—ä»£ç† - */ - LogPrx _logPrx; - - -}; - - -/** - * 本地日志帮助类, å•ä»¶ - * 循环日志å•ä»¶æ˜¯æ°¸ç”Ÿä¸æ­»çš„, ä¿è¯ä»»ä½•åœ°æ–¹éƒ½å¯ä»¥ä½¿ç”¨ - * 当该对象æžå¤Ÿä»¥åŽ, 则直接coutå‡ºæ¥ - */ -class TarsRollLogger : public TC_Singleton -{ -public: - enum - { - NONE_LOG = 1, /**所有的log都ä¸å†™*/ - ERROR_LOG = 2, /**写错误log*/ - WARN_LOG = 3, /**写错误,警告log*/ - DEBUG_LOG = 4, /**写错误,警告,调试log*/ - INFO_LOG = 5 /**写错误,警告,调试,Info log*/ - }; -public: - typedef TC_Logger RollLogger; - - /** - * è®¾ç½®æœ¬åœ°ä¿¡æ¯ - * @param app, 业务å称 - * @param server, æœåŠ¡å称 - * @param logpath, 日志路径 - * @param iMaxSize, 文件最大大å°,字节 - * @param iMaxNum, 文件最大数 - */ - void setLogInfo(const string &sApp, const string &sServer, const string &sLogpath, int iMaxSize = 1024*1024*50, int iMaxNum = 10, const CommunicatorPtr &comm=NULL, const string &sLogObj=""); - - /** - * 设置åŒæ­¥å†™æ—¥å¿— - * - * @param bSync - */ - void sync(bool bSync = true); - - /** - * 获å–循环日志 - * - * @return RollLogger - */ - RollLogger *logger() { return &_logger; } - - /** - * 染色日志是å¦å¯ç”¨ - * @param bEnable - */ - void enableDyeing(bool bEnable, const string& sDyeingKey = ""); - -protected: - - /** - * 应用 - */ - string _app; - - /** - * æœåŠ¡å称 - */ - string _server; - - /** - * 日志路径 - */ - string _logpath; - - /** - * 循环日志 - */ - RollLogger _logger; - - /** - * 本地线程组 - */ - TC_LoggerThreadGroup _local; - -}; - -/////////////////////////////////////////////////////////////////////////////////////// -// -/** - * 写日志线程 - * 将写本地日志和远程分开到ä¸åŒçš„线程 - * 作为å•ä»¶å­˜åœ¨, 且是永生ä¸æ­»çš„å•ä»¶ - */ -class TarsLoggerThread : public TC_Singleton -{ -public: - /** - * 构造函数 - */ - TarsLoggerThread(); - - /** - * æžå¤Ÿå‡½æ•° - */ - ~TarsLoggerThread(); - - /** - * 本地写日志线程 - */ - TC_LoggerThreadGroup* local(); - - /** - * 远程写日志线程 - * - * @return TC_LoggerThreadGroup* - */ - TC_LoggerThreadGroup* remote(); - -protected: - - /** - * 本地线程组 - */ - TC_LoggerThreadGroup _local; - - /** - * 远程写线程组 - */ - TC_LoggerThreadGroup _remote; -}; - -/////////////////////////////////////////////////////////////////////////////////////// -class TimeWriteT; - -/** - * 远程的Log写æ“作类 - */ -class RemoteTimeWriteT -{ -public: - RemoteTimeWriteT(); - ~RemoteTimeWriteT(); - - /** - * 构造函数 - */ - void setTimeWriteT(TimeWriteT *pTimeWrite); - - /** - * 具体调用 - * @param of - * @param buffer - */ - void operator()(ostream &of, const deque > &buffer); - -protected: - /** - * åŒæ­¥åˆ°è¿œç¨‹ - */ - void sync2remote(const vector &buffer); - - /** - * 染色日志åŒæ­¥åˆ°è¿œç¨‹ - */ - void sync2remoteDyeing(const vector &buffer); - -protected: - /** - * 指针 - */ - TimeWriteT *_timeWrite; - -}; - -//////////////////////////////////////////////////////////////////////////// -/** - * 写Logger - */ -class TimeWriteT -{ -public: - typedef TC_Logger RemoteTimeLogger; - - typedef TC_Logger DyeingTimeLogger; - - /** - * 构造 - */ - TimeWriteT(); - - /** - * æžå¤Ÿ - */ - ~TimeWriteT(); - - /** - * è®¾ç½®åŸºæœ¬ä¿¡æ¯ - * @param app, 应用å称 - * @param server, æœåŠ¡å称 - * @param file, 日志文件å - * @param sFormat, æ ¼å¼ - * @param setdivision,setå称 - * @param sLogType,日志记录类型 - */ - void setLogInfo(const LogPrx &logPrx, const string &sApp, const string &sServer, const string &sFile, const string &sLogpath, const string &sFormat, const string& setdivision = "", const string& sLogType = "", const PropertyReportPtr &reportSuccPtr = NULL, const PropertyReportPtr &reportFailPtr = NULL); - - /** - * è®¾ç½®ä»£ç† - * @param logPrx 代ç†ä¿¡æ¯ - */ - void setLogPrx(const LogPrx &logPrx); - - /** - * 远程日志功能打开或关闭 - * @param bEnable - */ - void enableRemote(bool bEnable) { _remote = bEnable; } - - /** - * 本地日志功能功能打开或关闭 - * @param bEnable - */ - void enableLocal(bool bEnable); - - /** - * 染色日志功能打开或关闭 - * @param bEnable - */ - void enableDyeing (bool bEnable, const string& sDyeingKey = ""); - - - /** - * @brief 日志文件å是å¦å¸¦.logåŽç¼€ - * @param bEnable - */ - void enableSufix(bool bEnable=true){_hasSufix = bEnable;} - /** - * @brief 是å¦å…许框架在日志文件å上增加业务相关的标识 - * @param bEnable - */ - void enablePrefix(bool bEnable=true){_hasAppNamePrefix = bEnable;} - /** - * @brief 日志文件å中用户自定义字符与日期字符间的连接符,默认是"_" - * @param str - */ - void setFileNameConcatStr(const string& str) {_concatStr = str;} - - /** - * @brief 框架中增加的日志内容之间的分割符,默认是"|" - * @param str - */ - void setSeparator(const string& str) {_separ = str;} - - /** - * @brief 框架中日期和时间之间是å¦éœ€è¦åŠ ä¸­æ‹¬å·[],有些统计由特殊需求;默认ä¸åŠ  - * @param bEnable - */ - void enableSqareWrapper(bool bEnable) {_hasSquareBracket = bEnable;} - - - /** - * 设置时间格å¼("%Y%m%d") - * @param sFormat - */ - void setFormat(const string &sFormat) { _format = sFormat;} - - /** - * 具体调用 - * @param of - * @param buffer - */ - void operator()(ostream &of, const deque > &buffer); - -protected: - - /** - * å‹å…ƒ - */ - friend class RemoteTimeWriteT; - - /** - * 记录错误文件 - * @param buffer - */ - void writeError(const vector &buffer); - - /** - * 记录错误文件 - * @param buffer - */ - void writeError(const deque > &buffer); - - /** - * åˆå§‹åŒ–logger - */ - void initError(); - - /** - * åˆå§‹åŒ–染色日志 - */ - void initDyeingLog(); - -protected: - - /** - * 远程时间日志 - */ - RemoteTimeLogger *_remoteTimeLogger; - - /** - * 本地功能 - */ - bool _local; - - /** - * 远程功能 - */ - bool _remote; - - /** - * 远程æœåŠ¡å¥æŸ„ - */ - LogPrx _logPrx; - - /** - * appå称 - */ - string _app; - - /** - * æœåŠ¡å称 - */ - string _server; - - /** - * 日志文件å称 - */ - string _file; - - /** - * æ—¶é—´æ ¼å¼ - */ - string _format; - - /** - * 具体文件 - */ - string _filePath; - - /** - * 错误文件 - */ - TC_DayLogger _logger; - - /** - * 缺çœå†™æ¨¡å¼ - */ - TC_DefaultWriteT _wt; - - /** - * 染色日志 - */ - static int _dyeing; - - /** - * 染色日志目录路径 - */ - string _dyeingFilePath; - - /** - * 远程时间日志 - */ - DyeingTimeLogger *_dyeingTimeLogger; - - /** - * setåˆ†ç»„ä¿¡æ¯ - */ - string _setDivision; - - /** - * 日志文件å是å¦å¸¦.logåŽç¼€ - */ - bool _hasSufix; - /** - * 是å¦å…许框架在日志文件å上增加业务相关的标识 - */ - bool _hasAppNamePrefix; - - /** - * 日志文件å中用户自定义字符与日期字符间的连接符,默认是"_" - */ - string _concatStr; - /** - * 分隔符 - */ - string _separ; - /** - * 日期部分是å¦åŠ ä¸Š[] - */ - bool _hasSquareBracket; - - /* - * 本地日志的记录类型,æ ¼å¼ä¸ºTarsLogType.toString()返回值,如果ä¸é‡‡ç”¨TarsLogType,则该值为"" - */ - string _logType; - - /* - * 对于远程日志,上报åŒæ­¥åˆ°logserçš„æˆåŠŸé‡ï¼Œé»˜è®¤ä¸ä¸ŠæŠ¥ - */ - PropertyReportPtr _reportSuccPtr; - - /* - * 对于远程日志,上报åŒæ­¥åˆ°logser的失败é‡ï¼Œé»˜è®¤ä¸ä¸ŠæŠ¥ - */ - PropertyReportPtr _reportFailPtr; -}; - -//////////////////////////////////////////////////////////////////////////// -/** - * 远程日志帮助类, å•ä»¶ - */ -class TarsTimeLogger : public TC_HandleBase - , public TC_ThreadLock - , public TC_Singleton -{ -public: - - //定义按时间滚动的日志 - typedef TC_Logger TimeLogger; - - /** - * 构造 - */ - TarsTimeLogger(); - - /** - * æžå¤Ÿ - */ - ~TarsTimeLogger(); - - /** - * è®¾ç½®æœ¬åœ°ä¿¡æ¯ - * @param comm, 通信器 - * @param obj, 日志对象å称 - * @param app, 业务å称 - * @param server, æœåŠ¡å称 - * @param logpath, 日志路径 - */ - void setLogInfo(const CommunicatorPtr &comm, const string &obj, const string &sApp, const string &sServer, const string &sLogpath,const string& setdivision="", const bool &bLogStatReport = false); - - /** - * åˆå§‹åŒ–设置时间格å¼("%Y%m%d") - * ä¸è¦åŠ¨æ€ä¿®æ”¹, 线程ä¸å®‰å…¨ - * 如果有需è¦, åˆå§‹åŒ–åŽç›´æŽ¥ä¿®æ”¹ - * @param sFormat, 文件å称, 为空表示缺çœçš„时间日志 - */ - void initFormat(const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr=NULL); - void initFormat(const string &sApp, const string &sServer,const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr=NULL); - /** - * åˆå§‹åŒ–设置时间格å¼("%Y%m%d") - * ä¸è¦åŠ¨æ€ä¿®æ”¹, 线程ä¸å®‰å…¨ - * 如果有需è¦, åˆå§‹åŒ–åŽç›´æŽ¥ä¿®æ”¹ - * @param sFormat, 文件å称, 为空表示缺çœçš„时间日志 - * @param frequency 支æŒæ¯å¤šå°‘天/å°æ—¶/分钟,详è§TC_logger.h中关于TarsLogByDay,TarsLogByHour,TarsLogByMinuteçš„æè¿° - * - * 用法: 按两个å°æ—¶è®°å½•æ—¥å¿— - * initFormat("logfile",TarsLogByHour::FORMAT,2); - */ - template - void initFormatWithType(const string &sFile, const string &sFormat,size_t frequency) - { - TarsLogTypePtr logTypePtr = new TarsLogType(sFormat,frequency); - initFormat(sFile,sFormat,logTypePtr); - } - - template - void initFormatWithType(const string &sApp, const string &sServer,const string &sFile, const string &sFormat,size_t frequency) - { - TarsLogTypePtr logTypePtr = new TarsLogType(sFormat,frequency); - initFormat(sApp,sServer,sFile,sFormat,logTypePtr); - } - /** - * 获å–时间日志 - * @param file - */ - TimeLogger *logger(const string &sFile = ""); - - /** - * 获å–时间日志 - * @param app, 业务å称 - * @param server, æœåŠ¡å称 - * @param file - */ - TimeLogger *logger(const string &sApp, const string &sServer,const string &sFile = ""); - - /** - * åŒæ­¥å†™æœ¬åœ°æ—¶é—´æ—¥å¿—(远程日志一定是异步写的, 无法调整) - * @param bSync - */ - void sync(const string &sFile, bool bSync); - - /** - * 远程时间日志 - * @param sFile, 文件å称, 为空表示缺çœçš„时间日志 - * @param bEnable - */ - void enableRemote(const string &sFile, bool bEnable); - - /** - * 远程时间日志 - * @param sApp,应用å称 - * @param sServer,æœåŠ¡å称 - * @param sFile, 文件å称, 为空表示缺çœçš„时间日志 - * @param bEnable - */ - void enableRemoteEx(const string &sApp, const string &sServer,const string &sFile, bool bEnable); - /** - * 本地时间日志 - * @param sFile,文件å称, 为空表示缺çœçš„时间日志 - * @param bEnable - */ - void enableLocal(const string &sFile, bool bEnable); - /** - * 本地时间日志 - * @param sApp,应用å称 - * @param sServer,æœåŠ¡å称 - * @param sFile, 文件å称, 为空表示缺çœçš„时间日志 - * @param bEnable - */ - void enableLocalEx(const string &sApp, const string &sServer,const string &sFile, bool bEnable); - - /** - * @brief 日志文件å是å¦å¸¦.logåŽç¼€,å½±å“全部日志文件 - * @param bEnable - */ - void enableSufix(bool bEnable=true){_hasSufix = bEnable;} - /** - * @brief 是å¦å…许框架在日志文件å上增加业务相关的标识,å½±å“全部日志文件 - * @param bEnable - */ - void enablePrefix(bool bEnable=true){_hasAppNamePrefix = bEnable;} - /** - * @brief 日志文件å中用户自定义字符与日期字符间的连接符,默认是"_",å½±å“全部日志文件 - * @param str - */ - void setFileNameConcatStr(const string& str) {_concatStr = str;} - - /** - * @brief 框架中增加的日志内容之间的分割符,默认是"|",å½±å“全部日志文件 - * @param str - */ - void setSeparator(const string& str) {_separ = str;} - - /** - * @brief 框架中日期和时间之间是å¦éœ€è¦åŠ ä¸­æ‹¬å·[],有些统计由特殊需求;默认ä¸åŠ ,å½±å“全部日志文件 - * @param bEnable - */ - void enableSqareWrapper(bool bEnable) {_hasSquareBracket = bEnable;} - /** - * @brief 是å¦è¾“出本地日志文件,影å“全部日志文件 - * @param bEnable - */ - void enableLocalLog(bool bEnable) {_local = bEnable;} - /** - * @brief 是å¦è¾“出远程日志文件,å½±å“全部日志文件 - * @param bEnable - */ - void enableRemoteLog(bool bEnable) {_remote = bEnable;} -protected: - - /** - * åˆå§‹åŒ–时间日志 - * @param pTimeLogger - * @param sFile - * @param sFormat - * @param frequence, æ¯å¤šå°‘天/å°æ—¶/分钟,å•ä½æ˜¯ç§’ - */ - void initTimeLogger(TimeLogger *pTimeLogger, const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr=NULL); - - /** - * åˆå§‹åŒ–时间日志 - * @param pTimeLogger - * @param sApp - * @param sServer - * @param sFile - * @param sFormat - * @param frequence, æ¯å¤šå°‘天/å°æ—¶/分钟,å•ä½æ˜¯ç§’ - */ - - void initTimeLogger(TimeLogger *pTimeLogger,const string &sApp, const string &sServer, const string &sFile, const string &sFormat,const TarsLogTypePtr& logTypePtr=NULL); - -protected: - - /** - * 通信器 - */ - CommunicatorPtr _comm; - - /** - * 远程æœåŠ¡å¥æŸ„ - */ - LogPrx _logPrx; - - /** - * 应用 - */ - string _app; - - /** - * æœåŠ¡å称 - */ - string _server; - - /** - * 日志路径 - */ - string _logpath; - - /** - * 缺çœæŒ‰å¤©æ—¥å¿— - */ - TimeLogger *_defaultLogger; - - /** - * 远程日志 - */ - map _loggers; - /** - * setåˆ†ç»„ä¿¡æ¯ - */ - string _setDivision; - - /** - * 是å¦å¸¦.logåŽç¼€ - */ - bool _hasSufix; - /** - * 是å¦å…许框架在日志文件å上增加业务相关的标识 - */ - bool _hasAppNamePrefix; - - /** - * 日志文件å中用户自定义字符与日期字符间的连接符,默认是"_" - */ - string _concatStr; - /** - * 分隔符 - */ - string _separ; - /** - * 日期部分是å¦åŠ ä¸Š[] - */ - bool _hasSquareBracket; - /** - * 是å¦è¾“出本地日志 - */ - bool _local; - /** - * 是å¦è¾“出远程日志 - */ - bool _remote; - - /* - * æœåŠ¡æ—¥å¿—上报logser是å¦ä¸ŠæŠ¥æˆåŠŸæ•°é‡ - */ - bool _logStatReport; -}; - -/** - * 染色开关类,æžæž„时关闭 - */ -class TarsDyeingSwitch -{ -public: - /** - * 构造函数,默认ä¸æ‰“开染色日志 - */ - TarsDyeingSwitch() - :_needDyeing(false) - { - } - - /** - * æžæž„函数,关闭已打开的染色日志 - */ - ~TarsDyeingSwitch() - { - if(_needDyeing) - { - TarsRollLogger::getInstance()->enableDyeing(false); - - ServantProxyThreadData * td = ServantProxyThreadData::getData(); - assert(NULL != td); - if (td) - { - td->_dyeing = false; - td->_dyeingKey = ""; - } - } - } - - /** - * 获å–染色的key - * - * @param key - * @return bool - */ - static bool getDyeingKey(string & sDyeingkey) - { - ServantProxyThreadData * td = ServantProxyThreadData::getData(); - assert(NULL != td); - - if (td && td->_dyeing == true) - { - sDyeingkey = td->_dyeingKey; - return true; - } - return false; - } - - /** - * å¯ç”¨æŸ“色日志 - */ - void enableDyeing(const string & sDyeingKey = "") - { - TarsRollLogger::getInstance()->enableDyeing(true); - - ServantProxyThreadData * td = ServantProxyThreadData::getData(); - assert(NULL != td); - if(td) - - { - td->_dyeing = true; - td->_dyeingKey = sDyeingKey; - } - _needDyeing = true; - _dyeingKey = sDyeingKey; - } - -protected: - bool _needDyeing; - string _dyeingKey; -}; - -/** - * 循环日志 - */ -#define LOG (TarsRollLogger::getInstance()->logger()) - -/** - * @brief æŒ‰çº§åˆ«å¾ªçŽ¯æ—¥å¿—å® - * - * @param level 日志等级,TarsRollLogger::INFO_LOG,TarsRollLogger::DEBUG_LOG,TarsRollLogger::WARN_LOG,TarsRollLogger::ERROR_LOG - * @msg 日志内容语å¥,包括<<é‡å®šå‘符连接的语å¥,如 "Demo begin" << " testing !" <IsNeedLog(level)) LOG->log(level)<logger()->any()) -#define FDLOG(x) (TarsTimeLogger::getInstance()->logger(x)->any()) -#define FFDLOG(x,y,z) (TarsTimeLogger::getInstance()->logger(x,y,z)->any()) - -/** - * 按天日志局部使能开关,针对å•ä¸ªæ—¥å¿—文件进行使能,请在所有按天日志输出å‰è°ƒç”¨ - */ -#define TENREMOTE_FDLOG(swith,sApp,sServer,sFile) (TarsTimeLogger::getInstance()->enableRemoteEx(sApp,sServer,sFile,swith)) -#define TENLOCAL_FDLOG(swith,sApp,sServer,sFile) (TarsTimeLogger::getInstance()->enableLocalEx(sApp,sServer,sFile,swith)) - -/** - * 按天日志全局使能开关,请在所有按天日志输出å‰è°ƒç”¨ - */ -#define TENREMOTE(swith) (TarsTimeLogger::getInstance()->enableRemoteLog(swith)) -#define TENLOCAL(swith) (TarsTimeLogger::getInstance()->enableLocalLog(swith)) -} - -#endif - diff --git a/cpp/servant/servant/TarsNodeF.h b/cpp/servant/servant/TarsNodeF.h deleted file mode 100644 index 3d552c715..000000000 --- a/cpp/servant/servant/TarsNodeF.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_NODEF_H__ -#define __TARS_NODEF_H__ - -#include "servant/NodeF.h" -#include "servant/Global.h" -#include "util/tc_singleton.h" - -namespace tars -{ - -/** - * ç»™nodeå‘é€å¿ƒè·³ - * 调用keepAlive异步å‘é€å¿ƒè·³ç»™node - */ -class TarsNodeFHelper : public TC_Singleton,public TC_ThreadMutex -{ -public: - - /** - * 设置nodeä¿¡æ¯ - * @param comm, 通信器 - * @param obj, - * @param app - * @param server - */ - void setNodeInfo(const CommunicatorPtr &comm, const string &obj, const string &app, const string &server); - - /** - * keepAlive - */ - void keepAlive(const string &adapter = ""); - - /** - * 上报TARS的编译版本 - * @param version - */ - void reportVersion(const string &version); - -protected: - /** - * 通信器 - */ - CommunicatorPtr _comm; - - /** - * Node - */ - ServerFPrx _nodePrx; - - /** - * ä¿¡æ¯ - */ - ServerInfo _si; - - set _adapterSet; - -}; - -} - -#endif - diff --git a/cpp/servant/servant/TarsNotify.h b/cpp/servant/servant/TarsNotify.h deleted file mode 100644 index 4e01611e8..000000000 --- a/cpp/servant/servant/TarsNotify.h +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_NOTIFY_H_ -#define __TARS_NOTIFY_H_ - -#include "servant/NotifyF.h" -#include "servant/Global.h" -#include "util/tc_singleton.h" - -using namespace std; - -namespace tars -{ - -/** - * 上报信æ¯ç»™NotifyæœåŠ¡ - * 异步上报给notifyæœåŠ¡ - */ -class TarsRemoteNotify : public TC_Singleton -{ -public: - /** - * åˆå§‹åŒ– - * @param comm, 通信器 - * @param obj, 对象å称 - * @param notifyPrx - * @param app - * @param serverName - * - * @return int - */ - int setNotifyInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string &sSetName=""); - - /** - * 通知, 一定是异步上报的 - * @param message - */ - void notify(NOTIFYLEVEL level, const string &sMesage); - - /** - * 上报 - * @param sResult - * @param bSync - */ - void report(const string &sResult, bool bSync = false); - -protected: - /** - * 通信器 - */ - CommunicatorPtr _comm; - - /** - * é€šçŸ¥ä»£ç† - */ - NotifyPrx _notifyPrx; - - /** - * 应用 - */ - string _app; - - /** - * æœåŠ¡å称 - */ - string _serverName; - - /* - *set åå­— - */ - string _setName; -}; - -} - -#endif diff --git a/cpp/servant/servant/Transceiver.h b/cpp/servant/servant/Transceiver.h deleted file mode 100644 index 767a78b41..000000000 --- a/cpp/servant/servant/Transceiver.h +++ /dev/null @@ -1,376 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_TRANSCEIVER_H_ -#define __TARS_TRANSCEIVER_H_ - -#include "servant/EndpointInfo.h" -#include "servant/NetworkUtil.h" -#include "servant/CommunicatorEpoll.h" -#include "util/tc_buffer.h" -#include - -using namespace std; - -namespace tars -{ -#if TARS_SSL - class TC_OpenSSL; -#endif - -class AdapterProxy; - -////////////////////////////////////////////////////////// -/** - * 网络传输基类,主è¦æä¾›send/recvæŽ¥å£ - */ -class Transceiver -{ -public: - /* - * è¿žæŽ¥çŠ¶æ€ - */ - enum ConnectStatus - { - eUnconnected, - eConnecting, - eConnected, - }; - - /* - * å‘æ•°æ®çš„è¿”å›žçŠ¶æ€ - */ - enum ReturnStatus - { - eRetError = -1, - eRetOk = 0, - eRetFull = 1, - }; - - /* - * 构造函数 - * @param ep - * @param fd - */ - Transceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep); - - /* - * - *æžæž„函数 - */ - virtual ~Transceiver(); - - /** - * åˆå§‹åŒ– - */ - virtual void init(){} - - /** - * 是å¦ssl - */ - bool isSSL() const ; - - /* - * 检查连接是å¦è¶…æ—¶ - */ - void checkTimeout(); - - /* - * é‡æ–°åˆ›å»ºè¿žæŽ¥ - */ - void reconnect(); - - /* - * 创建连接,åˆå§‹åŒ– - */ - void connect(); - - /* - * 关闭连接 - */ - virtual void close(); - - /* - * 设置当å‰è¿žæŽ¥æ€ - */ - void setConnected(); - - /* - * å¾€fd里é¢å‘é€æ•°æ® - * 如果fd缓冲区已满,返回错误 - * 如果数æ®å‘é€ä¸€åŠï¼Œç¼“冲区满了,返回æˆåŠŸ - */ - int sendRequest(const char * pData,size_t iSize, bool forceSend = false); - - /* - * 处ç†è¯·æ±‚,判断Send BufferCache是å¦æœ‰å®Œæ•´çš„包 - * @return int - */ - virtual int doRequest(); - - /* - * 处ç†è¿”回,判断Recv BufferCache是å¦æœ‰å®Œæ•´çš„包 - * @param done - * @return int - */ - virtual int doResponse(list& done) = 0; - - /* - * 网络å‘é€æŽ¥å£ - * @param buf - * @param len - * @param flag - * @return int - */ - virtual int send(const void* buf, uint32_t len, uint32_t flag) = 0; - - /* - * ç½‘ç»œæŽ¥æ”¶æŽ¥å£ - * @param buf - * @param len - * @param flag - * - * @return int - */ - virtual int recv(void* buf, uint32_t len, uint32_t flag) = 0; - - /* - * 获å–文件æ述符 - * @return int - */ - virtual int fd() const - { - return _fd; - } - - /* - * 是å¦æœ‰æ•ˆ - */ - bool isValid() const - { - return (_fd != -1); - } - - /* - * 获å–端å£ä¿¡æ¯ - */ - EndpointInfo& getEndpointInfo() - { - return _ep; - } - - /* - * 获å–connect所属的adapter - */ - AdapterProxy * getAdapterProxy() - { - return _adapterProxy; - } - - /* - * 判断是å¦å·²ç»è¿žæŽ¥åˆ°æœåŠ¡ç«¯ - */ - bool hasConnected() - { - return isValid() && (_connStatus == eConnected); - } - - /* - * 判断是å¦æ­£åœ¨è¿žæŽ¥ - */ - bool isConnecting() - { - return isValid() && (_connStatus == eConnecting); - } - - /* - * 设置连接失败 - */ - void setConnectFailed() - { - _connStatus = eUnconnected; - } - -protected: - /** - ** 物ç†è¿žæŽ¥æˆåŠŸå›žè°ƒ - **/ - void _onConnect(); - - /** - ** 鉴æƒåˆå§‹åŒ–请求 - **/ - void _doAuthReq(); - - /* - * AdapterProxy - */ - AdapterProxy * _adapterProxy; - - /* - * è¿žæŽ¥çš„èŠ‚ç‚¹ä¿¡æ¯ - */ - EndpointInfo _ep; - - /* - * 套接字 - */ - int _fd; - - /* - * äº‹ä»¶æ³¨å†Œä¿¡æ¯ - */ - FDInfo _fdInfo; - - /* - * è¿žæŽ¥çŠ¶æ€ - */ - ConnectStatus _connStatus; - - /* - * 连接的超时时间 - */ - int64_t _conTimeoutTime; - - /* - * å‘é€ç¼“å­˜buff - */ - TC_Buffer _sendBuffer; - - /* - * 接收缓存buff - */ - TC_Buffer _recvBuffer; - - /* - * 鉴æƒçŠ¶æ€ - */ - int _authState; - -protected: -#if TARS_SSL - std::unique_ptr _openssl; -#endif - -}; - -////////////////////////////////////////////////////////// -/** - * TCP 传输实现 - */ -class TcpTransceiver : public Transceiver -{ -public: - /** - * 构造函数 - * @param ep - * @param fd - */ - TcpTransceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep); - - /** - * TCP å‘é€å®žçŽ° - * @param buf - * @param len - * @param flag - * @return int - */ - virtual int send(const void* buf, uint32_t len, uint32_t flag); - - /** - * TCP 接收实现 - * @param buf - * @param len - * @param flag - * - * @return int - */ - virtual int recv(void* buf, uint32_t len, uint32_t flag); - - /** - * TCP 接收实现 - * @param iovec - * @param count - * - * @return int - */ - int readv(const struct iovec*, int32_t count); - /** - * 处ç†è¿”回,判断接收是å¦æœ‰å®Œæ•´çš„包 - * @param done - * @return int, =1,表示有数æ®å°±åŒ… - */ - virtual int doResponse(list& done); - -}; -////////////////////////////////////////////////////////// -/** - * UDP 传输实现 - */ -class UdpTransceiver : public Transceiver -{ -public: - enum - { - DEFAULT_RECV_BUFFERSIZE = 64*1024 /*缺çœæ•°æ®æŽ¥æ”¶buffer的大å°*/ - }; - - /** - * 构造函数 - */ - UdpTransceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep); - - /** - * æžæž„函数 - */ - ~UdpTransceiver(); - - /** - * UDP å‘é€å®žçŽ° - * @param buf - * @param len - * @param flag - * @return int - */ - - virtual int send(const void* buf, uint32_t len, uint32_t flag); - - /** - * UDP 接收实现 - * @param buf - * @param len - * @param flag - * @return int - */ - virtual int recv(void* buf, uint32_t len, uint32_t flag); - - /** - * 处ç†è¿”回,判断接收缓存是å¦æœ‰å®Œæ•´çš„包 - * @param done - * @return int - */ - virtual int doResponse(list& done); - -private: - /* - * 接收缓存 - */ - char* _recvBuffer; -}; -////////////////////////////////////////////////////////// - -} -#endif diff --git a/cpp/servant/tup/CMakeLists.txt b/cpp/servant/tup/CMakeLists.txt deleted file mode 100644 index e82a1b633..000000000 --- a/cpp/servant/tup/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set(OUT_DEPENDS_LIST) - -complice_tars(OUT_DEPENDS_LIST "") - -add_custom_target(TUP-TARS ALL DEPENDS ${OUT_DEPENDS_LIST}) diff --git a/cpp/servant/tup/RequestF.tars b/cpp/servant/tup/RequestF.tars deleted file mode 100644 index e74b67306..000000000 --- a/cpp/servant/tup/RequestF.tars +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module tars -{ - //请求包体 - struct RequestPacket - { - 1 require short iVersion; - 2 require byte cPacketType = 0; - 3 require int iMessageType = 0; - 4 require int iRequestId; - 5 require string sServantName = ""; - 6 require string sFuncName = ""; - 7 require vector sBuffer; - 8 require int iTimeout = 0; - 9 require map context; - 10 require map status; - }; - - //å“应包体 - struct ResponsePacket - { - 1 require short iVersion; - 2 require byte cPacketType = 0; - 3 require int iRequestId; - 4 require int iMessageType = 0; - 5 require int iRet = 0; - 6 require vector sBuffer; - 7 require map status; - 8 optional string sResultDesc; - 9 optional map context; - }; -}; diff --git a/cpp/servant/tup/Tars.h b/cpp/servant/tup/Tars.h deleted file mode 100644 index fea5646c5..000000000 --- a/cpp/servant/tup/Tars.h +++ /dev/null @@ -1,1922 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_H__ -#define __TARS_H__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - - -//支æŒiphone -#ifdef __APPLE__ - #include "TarsType.h" -#elif defined ANDROID // android - #include "TarsType.h" -#else - #include "tup/TarsType.h" -#endif - - -#ifndef tars_likely -#if defined(__GNUC__) && __GNUC__ >= 4 -#define tars_likely(x) (__builtin_expect(!!(x),1)) -#else -#define tars_likely(x) (x) -#endif -#endif - -#ifndef tars_unlikely -#if defined(__GNUC__) && __GNUC__ >= 4 -#define tars_unlikely(x) (__builtin_expect(!!(x),0)) -#else -#define tars_unlikely(x) (x) -#endif -#endif - -//æ•°æ®å¤´ç±»åž‹ -#define TarsHeadeChar 0 -#define TarsHeadeShort 1 -#define TarsHeadeInt32 2 -#define TarsHeadeInt64 3 -#define TarsHeadeFloat 4 -#define TarsHeadeDouble 5 -#define TarsHeadeString1 6 -#define TarsHeadeString4 7 -#define TarsHeadeMap 8 -#define TarsHeadeList 9 -#define TarsHeadeStructBegin 10 -#define TarsHeadeStructEnd 11 -#define TarsHeadeZeroTag 12 -#define TarsHeadeSimpleList 13 - - -////////////////////////////////////////////////////////////////// -//// ä¿ç•™æŽ¥å£ç‰ˆæœ¬Tarså®å®šä¹‰ -//ç¼–ç ç›¸åº”çš„å® -#define TarsReserveBuf(os, len) \ -do{ \ - if((os)._reverse) \ - { \ - if(tars_unlikely((os)._buf_len < (len))) \ - { \ - size_t len1 = (len)<<1; \ - char * p = new char[(len1)]; \ - memcpy(p, (os)._buf, (os)._len); \ - delete[] (os)._buf; \ - (os)._buf = p; \ - (os)._buf_len = (len1); \ - } \ - } \ -}while(0) - -#define TarsWriteToHead(os, type, tag) \ -do { \ - if (tars_likely((tag) < 15)) \ - { \ - TarsWriteUInt8TTypeBuf( os, (type) + ((tag)<<4) , (os)._len); \ - } \ - else \ - { \ - TarsWriteUInt8TTypeBuf( os, (type) + (240) , (os)._len); \ - TarsWriteUInt8TTypeBuf( os, (tag), (os)._len);\ - } \ -} while(0) - -#define TarsWriteCharTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(Char)); \ - (*(Char *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(Char); \ -} while(0) - -#define TarsWriteInt32TypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(Int32)); \ - (*(Int32 *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(Int32); \ -} while(0) - -#define TarsWriteInt64TypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(Int64)); \ - (*(Int64 *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(Int64); \ -} while(0) - -#define TarsWriteFloatTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(Float)); \ - (*(Float *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(Float); \ -} while(0) - -#define TarsWriteDoubleTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(Double)); \ - (*(Double *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(Double); \ -} while(0) - -#define TarsWriteUInt32TTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(uint32_t)); \ - (*(uint32_t *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(uint32_t); \ -} while(0) - -#define TarsWriteUInt8TTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(uint8_t)); \ - (*(uint8_t *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(uint8_t); \ -} while(0) - -#define TarsWriteUIntTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(unsigned int)); \ - (*(unsigned int *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(unsigned int); \ -} while(0) - -#define TarsWriteShortTypeBuf(os, val, osLen) \ -do { \ - TarsReserveBuf(os, (osLen)+sizeof(Short)); \ - (*(Short *)((os)._buf + (osLen))) = (val); \ - (osLen) += sizeof(Short); \ -} while(0) - -#define TarsWriteTypeBuf(os, buf, len) \ -do { \ - TarsReserveBuf(os, (os)._len + (len)); \ - memcpy((os)._buf + (os)._len, (const void *)(buf), (len)); \ - (os)._len += (len); \ -} while(0) - - -//解ç ç›¸åº”çš„å® -#define TarsPeekTypeBuf(is, buf, offset, type) \ -do { \ - if(tars_unlikely((is)._cur+(offset)+sizeof(type)>(is)._buf_len) )\ - { \ - char s[64]; \ - snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \ - throw TarsDecodeException(s); \ - } \ - (buf) = (*((type *)((is)._buf+(is)._cur+(offset)))); \ -} while(0) - -#define TarsPeekTypeBufNoTag(is, offset, type) \ -do { \ - if(tars_unlikely((is)._cur+(offset)+sizeof(type)>(is)._buf_len) )\ - { \ - char s[64]; \ - snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \ - throw TarsDecodeException(s); \ - } \ -} while(0) - -#define TarsReadCharTypeBuf(is, buf) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, Char); \ - (is)._cur += sizeof(Char); \ -} while(0) - -#define TarsReadShortTypeBuf(is, buf) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, Short); \ - (is)._cur += sizeof(Short); \ -} while(0) - -#define TarsReadInt32TypeBuf(is, buf) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, Int32); \ - (is)._cur += sizeof(Int32); \ -} while(0) - -#define TarsReadInt64TypeBuf(is, buf) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, Int64); \ - (is)._cur += sizeof(Int64); \ -} while(0) - -#define TarsReadFloatTypeBuf(is, buf) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, Float); \ - (is)._cur += sizeof(Float); \ -} while(0) - -#define TarsReadDoubleTypeBuf(is, buf) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, Double); \ - (is)._cur += sizeof(Double); \ -} while(0) - -#define TarsReadTypeBuf(is, buf, type) \ -do { \ - TarsPeekTypeBuf(is, buf, 0, type); \ - (is)._cur += sizeof(type); \ -} while(0) - -#define TarsReadHeadSkip(is, len) \ -do {\ - (is)._cur += (len); \ -} while(0) - -#define TarsPeekFromHead(is, type, tag, n) \ -do { \ - (n) = 1; \ - uint8_t typeTag, tmpTag; \ - TarsPeekTypeBuf(is, typeTag, 0, uint8_t); \ - tmpTag = typeTag >> 4; \ - (type) = (typeTag & 0x0F); \ - if(tars_unlikely(tmpTag == 15)) \ - { \ - TarsPeekTypeBuf(is, tag, 1, uint8_t); \ - (n) += 1; \ - } \ - else \ - { \ - (tag) = tmpTag; \ - } \ -} while(0) - -#define readFromHead(is, type ,tag) \ -do { \ - size_t n = 0; \ - TarsPeekFromHead(is, type, tag, n); \ - TarsReadHeadSkip(is, n); \ -} while(0) - -#define TarsPeekFromHeadNoTag(is, type, n) \ -do { \ - (n) = 1; \ - uint8_t typeTag, tmpTag; \ - TarsPeekTypeBuf(is, typeTag, 0, uint8_t); \ - tmpTag = typeTag >> 4; \ - (type) = (typeTag & 0x0F); \ - if(tars_unlikely(tmpTag == 15)) \ - { \ - TarsPeekTypeBufNoTag(is, 1, uint8_t); \ - (n) += 1; \ - } \ -} while(0) - -#define readFromHeadNoTag(is, type) \ -do { \ - size_t n = 0; \ - TarsPeekFromHeadNoTag(is, type, n); \ - TarsReadHeadSkip(is, n); \ -} while(0) - -#define TarsPeekBuf(is ,buf, len, offset) \ -do {\ - if (tars_unlikely((is)._cur + (offset) + (len) > (is)._buf_len)) \ - { \ - char s[64]; \ - snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \ - throw TarsDecodeException(s); \ - } \ - ::memcpy(buf, (is)._buf + (is)._cur + (offset), (len)); \ -} while(0) - -#define TarsReadBuf(is, buf, len) \ -do { \ - TarsPeekBuf(is, buf, len, 0); \ - (is)._cur += (len); \ -} while(0) - -#define TarsReadStringBuf(is, str, len) \ -do{\ - if (tars_unlikely((is)._cur + (len) > (is)._buf_len)) \ - { \ - char s[64]; \ - snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \ - throw TarsDecodeException(s); \ - } \ - str.assign((is)._buf + (is)._cur, (is)._buf + (is)._cur + (len)); \ - (is)._cur += len; \ -} while (0) - -#define TarsSkipToTag(flag, tag, retHeadType, retHeadTag) \ -do { \ - try \ - { \ - uint8_t nextHeadType, nextHeadTag; \ - while (!ReaderT::hasEnd()) \ - { \ - size_t len = 0; \ - TarsPeekFromHead(*this, nextHeadType, nextHeadTag, len); \ - if (tars_unlikely(nextHeadType == TarsHeadeStructEnd || tag < nextHeadTag)) \ - { \ - break; \ - } \ - if (tag == nextHeadTag) \ - { \ - (retHeadType) = nextHeadType; \ - (retHeadTag) = nextHeadTag; \ - TarsReadHeadSkip(*this, len); \ - (flag) = true; \ - break; \ - } \ - TarsReadHeadSkip(*this, len); \ - skipField(nextHeadType); \ - } \ - } \ - catch (TarsDecodeException& e) \ - { \ - } \ -} while(0) - -namespace tars -{ -////////////////////////////////////////////////////////////////// - struct TarsStructBase - { - protected: - TarsStructBase() {} - - ~TarsStructBase() {} - }; - - struct TarsProtoException : public std::runtime_error - { - TarsProtoException(const std::string& s) : std::runtime_error(s) {} - }; - - struct TarsEncodeException : public TarsProtoException - { - TarsEncodeException(const std::string& s) : TarsProtoException(s) {} - }; - - struct TarsDecodeException : public TarsProtoException - { - TarsDecodeException(const std::string& s) : TarsProtoException(s) {} - }; - - struct TarsDecodeMismatch : public TarsDecodeException - { - TarsDecodeMismatch(const std::string & s) : TarsDecodeException(s) {} - }; - - struct TarsDecodeRequireNotExist : public TarsDecodeException - { - TarsDecodeRequireNotExist(const std::string & s) : TarsDecodeException(s) {} - }; - - struct TarsDecodeInvalidValue : public TarsDecodeException - { - TarsDecodeInvalidValue(const std::string & s) : TarsDecodeException(s) {} - }; - - struct TarsNotEnoughBuff : public TarsProtoException - { - TarsNotEnoughBuff(const std::string & s) : TarsProtoException(s) {} - }; - -////////////////////////////////////////////////////////////////// - namespace - { - /// æ•°æ®å¤´ä¿¡æ¯çš„å°è£…,包括类型和tag - class DataHead - { - uint8_t _type; - uint8_t _tag; - public: - enum - { - eChar = 0, - eShort = 1, - eInt32 = 2, - eInt64 = 3, - eFloat = 4, - eDouble = 5, - eString1 = 6, - eString4 = 7, - eMap = 8, - eList = 9, - eStructBegin = 10, - eStructEnd = 11, - eZeroTag = 12, - eSimpleList = 13, - }; - - struct helper - { - uint8_t type : 4; - uint8_t tag : 4; - }__attribute__((packed)); - - public: - DataHead() : _type(0), _tag(0) {} - DataHead(uint8_t type, uint8_t tag) : _type(type), _tag(tag) {} - - uint8_t getTag() const { return _tag;} - void setTag(uint8_t t) { _tag = t;} - uint8_t getType() const { return _type;} - void setType(uint8_t t) { _type = t;} - - /// 读å–æ•°æ®å¤´ä¿¡æ¯ - template - void readFrom(InputStreamT& is) - { - size_t n = peekFrom(is); - is.skip(n); - } - - /// 读å–头信æ¯ï¼Œä½†ä¸å‰ç§»æµçš„åç§»é‡ - template - size_t peekFrom(InputStreamT& is) - { - helper h; - size_t n = sizeof(h); - is.peekBuf(&h, sizeof(h)); - _type = h.type; - if (h.tag == 15) - { - is.peekBuf(&_tag, sizeof(_tag), sizeof(h)); - n += sizeof(_tag); - } - else - { - _tag = h.tag; - } - return n; - } - - /// 写入数æ®å¤´ä¿¡æ¯ - template - void writeTo(OutputStreamT& os) - { - /* - helper h; - h.type = _type; - if(_tag < 15){ - h.tag = _tag; - os.writeBuf(&h, sizeof(h)); - }else{ - h.tag = 15; - os.writeBuf(&h, sizeof(h)); - os.writeBuf(&_tag, sizeof(_tag)); - } - */ - writeTo(os, _type, _tag); - } - - /// 写入数æ®å¤´ä¿¡æ¯ - template - static void writeTo(OutputStreamT& os, uint8_t type, uint8_t tag) - { - helper h; - h.type = type; - if (tag < 15) - { - h.tag = tag; - os.writeBuf(&h, sizeof(h)); - } - else - { - h.tag = 15; - os.writeBuf(&h, sizeof(h)); - os.writeBuf(&tag, sizeof(tag)); - } - } - }; - } - - -////////////////////////////////////////////////////////////////// -/// 缓冲区读å–器å°è£… - class BufferReader - { - public: - const char * _buf; ///< 缓冲区 - size_t _buf_len; ///< 缓冲区长度 - size_t _cur; ///< 当å‰ä½ç½® - - public: - - BufferReader() : _buf(NULL),_buf_len(0),_cur(0) {} - - void reset() { _cur = 0;} - - /// 读å–缓存 - void readBuf(void * buf, size_t len) - { - if(len <= _buf_len && (_cur + len) <= _buf_len) - { - peekBuf(buf, len); - _cur += len; - } - else - { - char s[64]; - snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len); - throw TarsDecodeException(s); - } - } - - /// 读å–缓存,但ä¸æ”¹å˜åç§»é‡ - void peekBuf(void * buf, size_t len, size_t offset = 0) - { - if (_cur + offset + len > _buf_len) - { - char s[64]; - snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len); - throw TarsDecodeException(s); - } - ::memcpy(buf, _buf + _cur + offset, len); - } - - /// 读å–缓存 for vector - template - void readBuf(std::vector& v, size_t len) - { - if(len <= _buf_len && (_cur + len) <= _buf_len) - { - peekBuf(v, len); - _cur += len; - } - else - { - char s[64]; - snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len); - throw TarsDecodeException(s); - } - } - - /// 读å–缓存,但ä¸æ”¹å˜åç§»é‡ for vector - template - void peekBuf(std::vector& v, size_t len, size_t offset = 0) - { - if (_cur + offset + len > _buf_len) - { - char s[64]; - snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len); - throw TarsDecodeException(s); - } - - const char* begin = _buf + _cur + offset; - v.assign(begin, begin + len); - } - - /// 跳过len个字节 - void skip(size_t len) - { - if(len <= _buf_len && (_cur + len) <= _buf_len) - { - _cur += len; - } - else - { - char s[64]; - snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len); - throw TarsDecodeException(s); - } - } - - /// 设置缓存 - void setBuffer(const char * buf, size_t len) - { - _buf = buf; - _buf_len = len; - _cur = 0; - } - - /// 设置缓存 - template - void setBuffer(const std::vector &buf) - { - _buf = &buf[0]; - _buf_len = buf.size(); - _cur = 0; - } - - /** - * 判断是å¦å·²ç»åˆ°BUF的末尾 - */ - bool hasEnd() - { - return _cur >= _buf_len; - } - size_t tellp() const - { - return _cur; - } - const char* base() const - { - return _buf; - } - size_t size() const - { - return _buf_len; - } - }; - -//当tars文件中å«æœ‰æŒ‡é’ˆåž‹ç±»åž‹çš„æ•°æ®ç”¨MapBufferReaderè¯»å– -//在读数æ®æ—¶åˆ©ç”¨MapBufferReaderæå‰åˆ†é…的内存 å‡å°‘è¿è¡Œè¿‡ç¨‹ä¸­é¢‘ç¹å†…å­˜åˆ†é… -//结构中定义byte指针类型,指针用*æ¥å®šä¹‰ï¼Œå¦‚下: -//byte *m; -//指针类型使用时需è¦MapBufferReaderæå‰è®¾å®šé¢„分é…内存å—setMapBuffer(), -//指针需è¦å†…存时通过å移指å‘预分é…内存å—,å‡å°‘解ç è¿‡ç¨‹ä¸­çš„内存申请 - class MapBufferReader : public BufferReader - { - - public: - MapBufferReader() : _buf_m(NULL),_buf_len_m(0),_cur_m(0) {} - - void reset() { _cur_m = 0; BufferReader::reset();} - - char* cur() - { - if (tars_unlikely(_buf_m == NULL)) - { - char s[64]; - snprintf(s, sizeof(s), "MapBufferReader's buff not set,_buf = null"); - throw TarsDecodeException(s); - } - return _buf_m+_cur_m; - } - - size_t left(){return _buf_len_m-_cur_m;} - - /// 跳过len个字节 - void mapBufferSkip(size_t len) - { - if (tars_unlikely(_cur_m + len > _buf_len_m)) - { - char s[64]; - snprintf(s, sizeof(s), "MapBufferReader's buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len_m); - throw TarsDecodeException(s); - } - _cur_m += len; - } - - /// 设置缓存 - void setMapBuffer(char * buf, size_t len) - { - _buf_m = buf; - _buf_len_m = len; - _cur_m = 0; - } - - /// 设置缓存 - template - void setMapBuffer(std::vector &buf) - { - _buf_m = &buf[0]; - _buf_len_m = buf.size(); - _cur_m = 0; - } - public: - char * _buf_m; ///< 缓冲区 - size_t _buf_len_m; ///< 缓冲区长度 - size_t _cur_m; ///< 当å‰ä½ç½® - }; - -////////////////////////////////////////////////////////////////// -/// 缓冲区写入器å°è£… - class BufferWriter - { - public: - char * _buf; - size_t _len; - size_t _buf_len; - bool _reverse; - - public: - BufferWriter(const BufferWriter & bw) - { - _buf = NULL; - _len = 0; - _buf_len = 0; - _reverse = true; - - writeBuf(bw._buf, bw._len); - _len = bw._len; - //_buf_len = bw._buf_len; - } - - BufferWriter& operator=(const BufferWriter& buf) - { - _reverse = true; - writeBuf(buf._buf,buf._len); - _len = buf._len; - //_buf_len = buf._buf_len; - return *this; - } - - BufferWriter() - : _buf(NULL) - , _len(0) - , _buf_len(0) - , _reverse(true) - {} - ~BufferWriter() - { - delete[] _buf; - } - - void reserve(size_t len) - { - if (tars_unlikely(_buf_len < len)) - { - len <<= 1; - if(len<128) - len=128; - char * p = new char[len]; - memcpy(p, _buf, _len); - delete[] _buf; - _buf = p; - _buf_len = len; - } - } - void reset() { _len = 0;} - void writeBuf(const void * buf, size_t len) - { - TarsReserveBuf(*this, _len + len); - memcpy(_buf + _len, buf, len); - _len += len; - } - //const std::vector &getByteBuffer() const { return _buf; } - std::vector getByteBuffer() const { return std::vector(_buf, _buf + _len);} - const char * getBuffer() const { return _buf;}//{ return &_buf[0]; } - size_t getLength() const { return _len;} //{ return _buf.size(); } - //void swap(std::vector& v) { _buf.swap(v); } - void swap(std::vector& v) - { - v.assign(_buf, _buf + _len); - } - void swap(BufferWriter& buf) - { - std::swap(_buf, buf._buf); - std::swap(_buf_len, buf._buf_len); - std::swap(_len, buf._len); - std::swap(_reverse, buf._reverse); - } - }; - -/////////////////////////////////////////////////////////////////////////////////////////////////// -/// 预先设定缓存的å°è£…器 - class BufferWriterBuff - { - public: - char * _buf; - size_t _len; - size_t _buf_len; - bool _reverse; - private: - BufferWriterBuff(const BufferWriterBuff&); - public: - - BufferWriterBuff& operator=(const BufferWriterBuff& buf) - { - _reverse = false; - writeBuf(buf._buf, buf._len); - _len = buf._len; - _buf_len = buf._buf_len; - return *this; - } - - BufferWriterBuff() - : _buf(NULL) - , _len(0) - , _buf_len(0) - , _reverse(false) - {} - ~BufferWriterBuff() - { - - } - - void setBuffer(char * buffer, size_t size_buff) - { - _buf = buffer; - _len = 0; - _buf_len = size_buff; - _reverse = false; - } - - /* - void reserve(size_t len) - { - if(_buf_len < len) - { - - } - } - */ - void reset() { _len = 0;} - - void writeBuf(const void * buf, size_t len) - { - if (tars_unlikely(_buf_len < _len + len)) - { - throw TarsNotEnoughBuff("not enough buffer"); - } - - memcpy(_buf + _len, buf, len); - _len += len; - } - - std::vector getByteBuffer() const { return std::vector(_buf, _buf + _len);} - const char * getBuffer() const { return _buf;} - size_t getLength() const { return _len;} - void swap(std::vector& v) - { - v.assign(_buf, _buf + _len); - } - void swap(BufferWriterBuff& buf) - { - std::swap(_buf, buf._buf); - std::swap(_buf_len, buf._buf_len); - std::swap(_len, buf._len); - std::swap(_reverse, buf._reverse); - } - }; - -////////////////////////////////////////////////////////////////// - template - class TarsInputStream : public ReaderT - { - public: - - /// è·³åˆ°æŒ‡å®šæ ‡ç­¾çš„å…ƒç´ å‰ - bool skipToTag(uint8_t tag) - { - try - { - uint8_t headType = 0, headTag = 0; - while (!ReaderT::hasEnd()) - { - size_t len = 0; - TarsPeekFromHead(*this, headType, headTag, len); - if (tag <= headTag || headType == TarsHeadeStructEnd) - return headType == TarsHeadeStructEnd?false:(tag == headTag); - TarsReadHeadSkip(*this, len); - skipField(headType); - } - } - catch (TarsDecodeException& e) - { - } - return false; - } - - /// 跳到当å‰ç»“æž„çš„ç»“æŸ - void skipToStructEnd() - { - uint8_t headType = 0; - do - { - readFromHeadNoTag(*this, headType); - skipField(headType); - }while (headType != TarsHeadeStructEnd); - } - - /// 跳过一个字段 - void skipField() - { - uint8_t headType = 0; - readFromHeadNoTag(*this, headType); - skipField(headType); - } - - /// 跳过一个字段,ä¸åŒ…å«å¤´ä¿¡æ¯ - void skipField(uint8_t type) - { - switch (type) - { - case TarsHeadeChar: - TarsReadHeadSkip(*this, sizeof(Char)); - break; - case TarsHeadeShort: - TarsReadHeadSkip(*this, sizeof(Short)); - break; - case TarsHeadeInt32: - TarsReadHeadSkip(*this, sizeof(Int32)); - break; - case TarsHeadeInt64: - TarsReadHeadSkip(*this, sizeof(Int64)); - break; - case TarsHeadeFloat: - TarsReadHeadSkip(*this, sizeof(Float)); - break; - case TarsHeadeDouble: - TarsReadHeadSkip(*this, sizeof(Double)); - break; - case TarsHeadeString1: - { - size_t len = 0; - TarsReadTypeBuf(*this, len, uint8_t); - TarsReadHeadSkip(*this, len); - } - break; - case TarsHeadeString4: - { - size_t len = 0; - TarsReadTypeBuf(*this, len, uint32_t); - len = ntohl(len); - TarsReadHeadSkip(*this, len); - } - break; - case TarsHeadeMap: - { - Int32 size = 0; - read(size, 0); - for (Int32 i = 0; i < size * 2; ++i) - skipField(); - } - break; - case TarsHeadeList: - { - Int32 size = 0; - read(size, 0); - for (Int32 i = 0; i < size; ++i) - skipField(); - } - break; - case TarsHeadeSimpleList: - { - uint8_t headType = 0, headTag = 0; - readFromHead(*this, headType, headTag); - if (tars_unlikely(headType != TarsHeadeChar)) - { - char s[64]; - snprintf(s, sizeof(s), "skipField with invalid type, type value: %d, %d, %d.", type, headType, headTag); - throw TarsDecodeMismatch(s); - } - Int32 size = 0; - read(size, 0); - TarsReadHeadSkip(*this, size); - } - break; - case TarsHeadeStructBegin: - skipToStructEnd(); - break; - case TarsHeadeStructEnd: - case TarsHeadeZeroTag: - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "skipField with invalid type, type value:%d.", type); - throw TarsDecodeMismatch(s); - } - } - } - - /// 读å–一个指定类型的数æ®ï¼ˆåŸºæœ¬ç±»åž‹ï¼‰ - template - inline T readByType() - { - T n; - this->readBuf(&n, sizeof(n)); - return n; - } - void readUnknown(std::string & sUnkown, uint8_t tag) - { - - size_t start = ReaderT::tellp(); - size_t last = start; - try - { - uint8_t lasttag = tag; - DataHead h; - while (!ReaderT::hasEnd()) - { - size_t len = h.peekFrom(*this); - if ( h.getTag() <=lasttag) - { - break; - } - lasttag = h.getTag(); - this->skip(len); - skipField(h.getType()); - last = ReaderT::tellp(); //记录下最åŽä¸€æ¬¡æ­£å¸¸åˆ°è¾¾çš„ä½ç½® - } - } - catch (...) // - { - throw; - } - std::string s(ReaderT::base() +start, last - start); - sUnkown = s; - return ; - - } - friend class XmlProxyCallback; - - void read(Bool& b, uint8_t tag, bool isRequire = true) - { - Char c = b; - read(c, tag, isRequire); - b = c ? true : false; - } - - void read(Char& c, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch (headType) - { - case TarsHeadeZeroTag: - c = 0; - break; - case TarsHeadeChar: - TarsReadTypeBuf(*this, c, Char); - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'Char' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d.", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - void read(UInt8& n, uint8_t tag, bool isRequire = true) - { - Short i = (Short)n; - read(i,tag,isRequire); - n = (UInt8)i; - } - - void read(Short& n, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch (headType) - { - case TarsHeadeZeroTag: - n = 0; - break; - case TarsHeadeChar: - TarsReadTypeBuf(*this, n, Char); - break; - case TarsHeadeShort: - TarsReadTypeBuf(*this, n, Short); - n = ntohs(n); - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'Short' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - void read(UInt16& n, uint8_t tag, bool isRequire = true) - { - Int32 i = (Int32)n; - read(i,tag,isRequire); - n = (UInt16)i; - } - - void read(Int32& n, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 1, headTag = 1; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch (headType) - { - case TarsHeadeZeroTag: - n = 0; - break; - case TarsHeadeChar: - TarsReadTypeBuf(*this, n, Char); - break; - case TarsHeadeShort: - TarsReadTypeBuf(*this, n, Short); - n = (Short)ntohs(n); - break; - case TarsHeadeInt32: - TarsReadTypeBuf(*this, n, Int32); - n = ntohl(n); - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'Int32' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d headType: %d, headTag: %d", tag, headType, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - void read(UInt32& n, uint8_t tag, bool isRequire = true) - { - Int64 i = (Int64)n; - read(i,tag,isRequire); - n = (UInt32)i; - } - - void read(Int64& n, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeZeroTag: - n = 0; - break; - case TarsHeadeChar: - TarsReadTypeBuf(*this, n, Char); - break; - case TarsHeadeShort: - TarsReadTypeBuf(*this, n, Short); - n = (Short) ntohs(n); - break; - case TarsHeadeInt32: - TarsReadTypeBuf(*this, n, Int32); - n = (Int32) ntohl(n); - break; - case TarsHeadeInt64: - TarsReadTypeBuf(*this, n, Int64); - n = tars_ntohll(n); - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'Int64' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - void read(Float& n, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeZeroTag: - n = 0; - break; - case TarsHeadeFloat: - TarsReadTypeBuf(*this, n, float); - n = tars_ntohf(n); - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'Float' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - void read(Double& n, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeZeroTag: - n = 0; - break; - case TarsHeadeFloat: - TarsReadTypeBuf(*this, n, float); - n = tars_ntohf(n); - break; - case TarsHeadeDouble: - TarsReadTypeBuf(*this, n, double); - n = tars_ntohd(n); - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'Double' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - /*void read(std::string& s, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeString1: - { - size_t len = 0; - TarsReadTypeBuf(*this, len, uint8_t); - char ss[256]; - //s.resize(len); - //this->readBuf((void *)s.c_str(), len); - TarsReadStringBuf(*this, s, len); - //TarsReadBuf(*this, s, len); - //s.assign(ss, ss + len); - } - break; - case TarsHeadeString4: - { - uint32_t len = 0; - TarsReadTypeBuf(*this, len, uint32_t); - len = ntohl(len); - if (tars_unlikely(len > TARS_MAX_STRING_LENGTH)) - { - char s[128]; - snprintf(s, sizeof(s), "invalid string size, tag: %d, size: %d", tag, len); - throw TarsDecodeInvalidValue(s); - } - //char *ss = new char[len]; - //s.resize(len); - //this->readBuf((void *)s.c_str(), len); - - char *ss = new char[len]; - try - { - TarsReadBuf(*this, ss, len); - s.assign(ss, ss + len); - } - catch (...) - { - delete[] ss; - throw; - } - delete[] ss; - TarsReadStringBuf(*this, s, len); - } - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'string' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d", tag); - throw TarsDecodeRequireNotExist(s); - } - }*/ - - void read(std::string& s, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - uint32_t strLength = 0; - switch (headType) - { - case TarsHeadeString1: - { - TarsReadTypeBuf(*this, strLength, uint8_t); - } - break; - case TarsHeadeString4: - { - TarsReadTypeBuf(*this, strLength, uint32_t); - strLength = ntohl(strLength); - if (tars_unlikely(strLength > TARS_MAX_STRING_LENGTH)) - { - char s[128]; - snprintf(s, sizeof(s), "invalid string size, tag: %d, size: %d", tag, strLength); - throw TarsDecodeInvalidValue(s); - } - } - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'string' type mismatch, tag: %d, get type: %d, tag: %d.", tag, headType, headTag); - throw TarsDecodeMismatch(s); - } - } - TarsReadStringBuf(*this, s, strLength); - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d", tag); - throw TarsDecodeRequireNotExist(s); - } - } - - void read(char *buf, const UInt32 bufLen, UInt32 & readLen, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeSimpleList: - { - uint8_t hheadType, hheadTag; - readFromHead(*this, hheadType, hheadTag); - if (tars_unlikely(hheadType != TarsHeadeChar)) - { - char s[128]; - snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d, %d, %d", tag, headType, hheadType, hheadTag); - throw TarsDecodeMismatch(s); - } - UInt32 size = 0; - read(size, 0); - if (tars_unlikely(size > bufLen)) - { - char s[128]; - snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, %d, size: %d", tag, headType, hheadType, size); - throw TarsDecodeInvalidValue(s); - } - //TarsReadTypeBuf(*this, size, UInt32); - this->readBuf(buf, size); - readLen = size; - } - break; - - default: - { - char s[128]; - snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[128]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - - template - void read(std::map& m, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeMap: - { - UInt32 size = 0; - read(size, 0); - if (tars_unlikely(size > this->size())) - { - char s[128]; - snprintf(s, sizeof(s), "invalid map, tag: %d, size: %d", tag, size); - throw TarsDecodeInvalidValue(s); - } - m.clear(); - - for (UInt32 i = 0; i < size; ++i) - { - std::pair pr; - read(pr.first, 0); - read(pr.second, 1); - m.insert(pr); - } - } - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'map' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - template - void read(std::vector& v, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeSimpleList: - { - uint8_t hheadType, hheadTag; - readFromHead(*this, hheadType, hheadTag); - if (tars_unlikely(hheadType != TarsHeadeChar)) - { - char s[128]; - snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d, %d, %d", tag, headType, hheadType, hheadTag); - throw TarsDecodeMismatch(s); - } - UInt32 size = 0; - read(size, 0); - if (tars_unlikely(size > this->size())) - { - char s[128]; - snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, %d, size: %d", tag, headType, hheadType, size); - throw TarsDecodeInvalidValue(s); - } - - this->readBuf(v, size); - } - break; - case TarsHeadeList: - { - UInt32 size = 0; - read(size, 0); - if (tars_unlikely(size > this->size())) - { - char s[128]; - snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d", tag, headType, size); - throw TarsDecodeInvalidValue(s); - } - v.reserve(size); - v.resize(size); - for (UInt32 i = 0; i < size; ++i) - read(v[i], 0); - } - break; - default: - { - char s[128]; - snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[128]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - template - void read(std::vector& v, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeList: - { - UInt32 size = 0; - read(size, 0); - if (tars_unlikely(size > this->size())) - { - char s[128]; - snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d", tag, headType, size); - throw TarsDecodeInvalidValue(s); - } - v.reserve(size); - v.resize(size); - for (UInt32 i = 0; i < size; ++i) - read(v[i], 0); - } - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'vector' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - /// 读å–结构数组 - template - void read(T* v, const UInt32 len, UInt32 & readLen, uint8_t tag, bool isRequire = true) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - switch(headType) - { - case TarsHeadeList: - { - UInt32 size = 0; - read(size, 0); - if (tars_unlikely(size > this->size())) - { - char s[128]; - snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d", tag, headType, size); - throw TarsDecodeInvalidValue(s); - } - for (UInt32 i = 0; i < size; ++i) - read(v[i], 0); - readLen = size; - } - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'vector struct' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - } - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - - template - void read(T& v, uint8_t tag, bool isRequire = true, typename detail::disable_if, void ***>::type dummy = 0) - { - Int32 n = 0; - read(n, tag, isRequire); - v = (T) n; - } - - /// 读å–结构 - template - void read(T& v, uint8_t tag, bool isRequire = true, typename detail::enable_if, void ***>::type dummy = 0) - { - uint8_t headType = 0, headTag = 0; - bool skipFlag = false; - TarsSkipToTag(skipFlag, tag, headType, headTag); - if (tars_likely(skipFlag)) - { - if (tars_unlikely(headType != TarsHeadeStructBegin)) - { - char s[64]; - snprintf(s, sizeof(s), "read 'struct' type mismatch, tag: %d, get type: %d.", tag, headType); - throw TarsDecodeMismatch(s); - } - v.readFrom(*this); - skipToStructEnd(); - } - else if (tars_unlikely(isRequire)) - { - char s[64]; - snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag); - throw TarsDecodeRequireNotExist(s); - } - } - }; - -////////////////////////////////////////////////////////////////// - template - class TarsOutputStream : public WriterT - { - public: - void writeUnknown(const std::string& s) - { - this->writeBuf(s.data(), s.size()); - } - void writeUnknownV2(const std::string& s) - { - DataHead::writeTo(*this, DataHead::eStructBegin, 0); - this->writeBuf(s.data(), s.size()); - DataHead::writeTo(*this, DataHead::eStructEnd, 0); - } - void write(Bool b, uint8_t tag) - { - write((Char) b, tag); - } - - void write(Char n, uint8_t tag) - { - /* - DataHead h(DataHead::eChar, tag); - if(n == 0){ - h.setType(DataHead::eZeroTag); - h.writeTo(*this); - }else{ - h.writeTo(*this); - this->writeBuf(&n, sizeof(n)); - } - */ - if (tars_unlikely(n == 0)) - { - TarsWriteToHead(*this, TarsHeadeZeroTag, tag); - } - else - { - TarsWriteToHead(*this, TarsHeadeChar, tag); - TarsWriteCharTypeBuf(*this, n, (*this)._len); - } - } - - void write(UInt8 n, uint8_t tag) - { - write((Short) n, tag); - } - - void write(Short n, uint8_t tag) - { - //if(n >= CHAR_MIN && n <= CHAR_MAX){ - if (n >= (-128) && n <= 127) - { - write((Char) n, tag); - } - else - { - /* - DataHead h(DataHead::eShort, tag); - h.writeTo(*this); - n = htons(n); - this->writeBuf(&n, sizeof(n)); - */ - TarsWriteToHead(*this, TarsHeadeShort, tag); - n = htons(n); - TarsWriteShortTypeBuf(*this, n, (*this)._len); - } - } - - void write(UInt16 n, uint8_t tag) - { - write((Int32) n, tag); - } - - void write(Int32 n, uint8_t tag) - { - //if(n >= SHRT_MIN && n <= SHRT_MAX){ - if (n >= (-32768) && n <= 32767) - { - write((Short) n, tag); - } - else - { - //DataHead h(DataHead::eInt32, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeInt32, tag); - n = htonl(n); - TarsWriteInt32TypeBuf(*this, n, (*this)._len); - } - } - - void write(UInt32 n, uint8_t tag) - { - write((Int64) n, tag); - } - - void write(Int64 n, uint8_t tag) - { - //if(n >= INT_MIN && n <= INT_MAX){ - if (n >= (-2147483647-1) && n <= 2147483647) - { - write((Int32) n, tag); - } - else - { - //DataHead h(DataHead::eInt64, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeInt64, tag); - n = tars_htonll(n); - TarsWriteInt64TypeBuf(*this, n, (*this)._len); - } - } - - void write(Float n, uint8_t tag) - { - //DataHead h(DataHead::eFloat, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeFloat, tag); - n = tars_htonf(n); - TarsWriteFloatTypeBuf(*this, n, (*this)._len); - } - - void write(Double n, uint8_t tag) - { - //DataHead h(DataHead::eDouble, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeDouble, tag); - n = tars_htond(n); - TarsWriteDoubleTypeBuf(*this, n, (*this)._len); - } - - void write(const std::string& s, uint8_t tag) - { - if (tars_unlikely(s.size() > 255)) - { - if (tars_unlikely(s.size() > TARS_MAX_STRING_LENGTH)) - { - char ss[128]; - snprintf(ss, sizeof(ss), "invalid string size, tag: %d, size: %u", tag, (uint32_t)s.size()); - throw TarsDecodeInvalidValue(ss); - } - TarsWriteToHead(*this, TarsHeadeString4, tag); - uint32_t n = htonl(s.size()); - TarsWriteUInt32TTypeBuf(*this, n, (*this)._len); - //this->writeBuf(s.data(), s.size()); - TarsWriteTypeBuf(*this, s.data(), s.size()); - } - else - { - TarsWriteToHead(*this, TarsHeadeString1, tag); - uint8_t n = s.size(); - TarsWriteUInt8TTypeBuf(*this, n, (*this)._len); - //this->writeBuf(s.data(), s.size()); - TarsWriteTypeBuf(*this, s.data(), s.size()); - } - } - - void write(const char *buf, const UInt32 len, uint8_t tag) - { - TarsWriteToHead(*this, TarsHeadeSimpleList, tag); - TarsWriteToHead(*this, TarsHeadeChar, 0); - write(len, 0); - //this->writeBuf(buf, len); - TarsWriteTypeBuf(*this, buf, len); - } - - template - void write(const std::map& m, uint8_t tag) - { - //DataHead h(DataHead::eMap, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeMap, tag); - Int32 n = m.size(); - write(n, 0); - typedef typename std::map::const_iterator IT; - for (IT i = m.begin(); i != m.end(); ++i) - { - write(i->first, 0); - write(i->second, 1); - } - } - - template - void write(const std::vector& v, uint8_t tag) - { - //DataHead h(DataHead::eList, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeList, tag); - Int32 n = v.size(); - write(n, 0); - typedef typename std::vector::const_iterator IT; - for (IT i = v.begin(); i != v.end(); ++i) - write(*i, 0); - } - - template - void write(const T *v, const UInt32 len, uint8_t tag) - { - TarsWriteToHead(*this, TarsHeadeList, tag); - write(len, 0); - for (Int32 i = 0; i < (Int32)len; ++i) - { - write(v[i], 0); - } - } - - template - void write(const std::vector& v, uint8_t tag) - { - //DataHead h(DataHead::eSimpleList, tag); - //h.writeTo(*this); - //DataHead hh(DataHead::eChar, 0); - //hh.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeSimpleList, tag); - TarsWriteToHead(*this, TarsHeadeChar, 0); - Int32 n = v.size(); - write(n, 0); - //writeBuf(&v[0], v.size()); - TarsWriteTypeBuf(*this, &v[0], v.size()); - } - - template - void write(const T& v, uint8_t tag, typename detail::disable_if, void ***>::type dummy = 0) - { - write((Int32) v, tag); - } - - template - void write(const T& v, uint8_t tag, typename detail::enable_if, void ***>::type dummy = 0) - { - //DataHead h(DataHead::eStructBegin, tag); - //h.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeStructBegin, tag); - v.writeTo(*this); - TarsWriteToHead(*this, TarsHeadeStructEnd, 0); - /* - h.setType(DataHead::eStructEnd); - h.setTag(0); - h.writeTo(*this); - */ - } - }; -//////////////////////////////////////////////////////////////////////////////////////////////////// -} - -//支æŒiphone -#ifdef __APPLE__ - #include "TarsDisplayer.h" -#else - #include "tup/TarsDisplayer.h" -#endif - -#endif diff --git a/cpp/servant/tup/TarsDisplayer.h b/cpp/servant/tup/TarsDisplayer.h deleted file mode 100644 index b5f7ef93b..000000000 --- a/cpp/servant/tup/TarsDisplayer.h +++ /dev/null @@ -1,371 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_DISPLAYER_H__ -#define __TARS_DISPLAYER_H__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -//支æŒiphone -#ifdef __APPLE__ -#include "TarsType.h" -#else -#include "tup/TarsType.h" -#endif - -namespace tars -{ -////////////////////////////////////////////////////////////////////// -/// 用于输出 -class TarsDisplayer -{ - std::ostream& _os; - int _level; - - void ps(const char * fieldName) - { - for(int i = 0; i < _level; ++i) - _os << '\t'; - if(fieldName != NULL) - _os << fieldName << ": "; - } -public: - explicit TarsDisplayer(std::ostream& os, int level = 0) - : _os(os) - , _level(level) - {} - - TarsDisplayer& display(Bool b, const char * fieldName) - { - ps(fieldName); - _os << (b ? 'T' : 'F') << std::endl; - return *this; - } - - TarsDisplayer& display(Char n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(UInt8 n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(Short n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(UInt16 n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - - TarsDisplayer& display(Int32 n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(UInt32 n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(Int64 n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(Float n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(Double n, const char * fieldName) - { - ps(fieldName); - _os << n << std::endl; - return *this; - } - - TarsDisplayer& display(const std::string& s, const char * fieldName) - { - ps(fieldName); - _os << s << std::endl; - return *this; - } - - TarsDisplayer& display(const char *s, const size_t len, const char * fieldName) - { - ps(fieldName); - for(unsigned i=0;i< len; i++) { - _os << s[i]; - } - _os< - TarsDisplayer& display(const std::map& m, const char * fieldName) - { - ps(fieldName); - if(m.empty()){ - _os << m.size() << ", {}" << std::endl; - return *this; - } - _os << m.size() << ", {" << std::endl; - TarsDisplayer jd1(_os, _level + 1); - TarsDisplayer jd(_os, _level + 2); - typedef typename std::map::const_iterator IT; - IT f = m.begin(), l = m.end(); - for(; f != l; ++f){ - jd1.display('(', NULL); - jd.display(f->first, NULL); - jd.display(f->second, NULL); - jd1.display(')', NULL); - } - display('}', NULL); - return *this; - } - - template < typename T, typename Alloc > - TarsDisplayer& display(const std::vector& v, const char * fieldName) - { - ps(fieldName); - if(v.empty()){ - _os << v.size() << ", []" << std::endl; - return *this; - } - _os << v.size() << ", [" << std::endl; - TarsDisplayer jd(_os, _level + 1); - typedef typename std::vector::const_iterator IT; - IT f = v.begin(), l = v.end(); - for(; f != l; ++f) - jd.display(*f, NULL); - display(']', NULL); - return *this; - } - - template < typename T > - TarsDisplayer& display(const T * v, const size_t len ,const char * fieldName) - { - ps(fieldName); - if(len == 0){ - _os << len << ", []" << std::endl; - return *this; - } - _os << len << ", [" << std::endl; - TarsDisplayer jd(_os, _level + 1); - for(size_t i=0; i< len; ++i) - jd.display(v[i], NULL); - display(']', NULL); - return *this; - } - - template < typename T > - TarsDisplayer& display(const T& v, const char * fieldName, typename detail::disable_if, void ***>::type dummy = 0) - { - return display((Int32) v, fieldName); - } - - template < typename T > - TarsDisplayer& display(const T& v, const char * fieldName, typename detail::enable_if, void ***>::type dummy = 0) - { - display('{', fieldName); - v.display(_os, _level + 1); - display('}', NULL); - return *this; - } - - TarsDisplayer& displaySimple(Bool b, bool bSep) - { - _os << (b ? 'T' : 'F') << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(Char n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(UInt8 n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(Short n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(UInt16 n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(Int32 n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(UInt32 n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(Int64 n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(Float n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(Double n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(const std::string& n, bool bSep) - { - _os << n << (bSep ? "|" : ""); - return *this; - } - - TarsDisplayer& displaySimple(const char * n, const size_t len, bool bSep) - { - for(unsigned i=0;i< len; i++) { - _os << n[i] ; - } - _os<<(bSep ? "|" : ""); - return *this; - } - - template - TarsDisplayer& displaySimple(const std::map& m, bool bSep) - { - if(m.empty()){ - _os << m.size() << "{}"; - return *this; - } - _os << m.size() << "{"; - TarsDisplayer jd1(_os, _level + 1); - TarsDisplayer jd(_os, _level + 2); - typedef typename std::map::const_iterator IT; - IT f = m.begin(), l = m.end(); - for(; f != l; ++f){ - if(f != m.begin()) _os << ','; - jd.displaySimple(f->first, true); - jd.displaySimple(f->second, false); - } - _os << '}' << (bSep ? "|" : ""); - return *this; - } - - template < typename T, typename Alloc > - TarsDisplayer& displaySimple(const std::vector& v, bool bSep) - { - if(v.empty()){ - _os << v.size() << "{}"; - return *this; - } - _os << v.size() << '{'; - TarsDisplayer jd(_os, _level + 1); - typedef typename std::vector::const_iterator IT; - IT f = v.begin(), l = v.end(); - for(; f != l; ++f) - { - if(f != v.begin()) _os << "|"; - jd.displaySimple(*f, false); - } - _os << '}' << (bSep ? "|" : ""); - return *this; - } - - template < typename T> - TarsDisplayer& displaySimple(const T* v, const size_t len, bool bSep) - { - if(len == 0){ - _os << len << "{}"; - return *this; - } - _os << len << '{'; - TarsDisplayer jd(_os, _level + 1); - for(size_t i=0; i - TarsDisplayer& displaySimple(const T& v, bool bSep, typename detail::disable_if, void ***>::type dummy = 0) - { - return displaySimple((Int32) v, bSep); - } - - template < typename T > - TarsDisplayer& displaySimple(const T& v, bool bSep, typename detail::enable_if, void ***>::type dummy = 0) - { - _os << "{"; - v.displaySimple(_os, _level + 1); - _os << "}" << (bSep ? "|" : ""); - return *this; - } -}; -//////////////////////////////////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/tup/TarsType.h b/cpp/servant/tup/TarsType.h deleted file mode 100644 index 473dc15e7..000000000 --- a/cpp/servant/tup/TarsType.h +++ /dev/null @@ -1,248 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_TYPE_H__ -#define __TARS_TYPE_H__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace tars -{ -///////////////////////////////////////////////////////////////////////////////////// -typedef bool Bool; -typedef char Char; -typedef short Short; -typedef float Float; -typedef double Double; -typedef int Int32; - -typedef unsigned char UInt8; -typedef unsigned short UInt16; -typedef unsigned int UInt32; - -#if __WORDSIZE == 64 -typedef long Int64; -#else -typedef long long Int64; -#endif - -#ifndef TARS_MAX_STRING_LENGTH -#define TARS_MAX_STRING_LENGTH (100 * 1024 * 1024) -#endif -/* -#define tars__bswap_constant_32(x) \ - ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ - (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) - -#define tars__bswap_constant_64(x) \ - ((((x) & 0xff00000000000000ull) >> 56) \ - | (((x) & 0x00ff000000000000ull) >> 40) \ - | (((x) & 0x0000ff0000000000ull) >> 24) \ - | (((x) & 0x000000ff00000000ull) >> 8) \ - | (((x) & 0x00000000ff000000ull) << 8) \ - | (((x) & 0x0000000000ff0000ull) << 24) \ - | (((x) & 0x000000000000ff00ull) << 40) \ - | (((x) & 0x00000000000000ffull) << 56)) -*/ -#ifdef __APPLE__ -# ifndef __LITTLE_ENDIAN -# define __LITTLE_ENDIAN 1234 -# endif -# ifndef __BIG_ENDIAN -# define __BIG_ENDIAN 4321 -# endif -# ifndef __BYTE_ORDER -# define __BYTE_ORDER __LITTLE_ENDIAN -# endif -#endif - -#if __BYTE_ORDER == __BIG_ENDIAN -# define tars_ntohll(x) (x) -# define tars_htonll(x) (x) -# define tars_ntohf(x) (x) -# define tars_htonf(x) (x) -# define tars_ntohd(x) (x) -# define tars_htond(x) (x) -#else -# if __BYTE_ORDER == __LITTLE_ENDIAN -# define tars_ntohll(x) tars_htonll(x) -//# define tars_htonll(x) tars__bswap_constant_64(x) -namespace detail -{ - union bswap_helper - { - Int64 i64; - Int32 i32[2]; - }; -} -inline Int64 tars_htonll(Int64 x) -{ - detail::bswap_helper h; - h.i64 = x; - Int32 tmp = htonl(h.i32[1]); - h.i32[1] = htonl(h.i32[0]); - h.i32[0] = tmp; - return h.i64; -} -inline Float tars_ntohf(Float x) -{ - union { - Float f; - Int32 i32; - } helper; - - helper.f = x; - helper.i32 = htonl( helper.i32 ); - - return helper.f; -} -# define tars_htonf(x) tars_ntohf(x) -inline Double tars_ntohd(Double x) -{ - union { - Double d; - Int64 i64; - } helper; - - helper.d = x; - helper.i64 = tars_htonll( helper.i64 ); - - return helper.d; -} -# define tars_htond(x) tars_ntohd(x) -# endif -#endif - -//type2name -template struct TarsClass { static std::string name() { return T::className(); } }; -template<> struct TarsClass { static std::string name() { return "bool"; } }; -template<> struct TarsClass { static std::string name() { return "char"; } }; -template<> struct TarsClass { static std::string name() { return "short"; } }; -template<> struct TarsClass { static std::string name() { return "float"; } }; -template<> struct TarsClass { static std::string name() { return "double"; } }; -template<> struct TarsClass { static std::string name() { return "int32"; } }; -template<> struct TarsClass { static std::string name() { return "int64"; } }; -template<> struct TarsClass { static std::string name() { return "short"; } }; -template<> struct TarsClass { static std::string name() { return "int32"; } }; -template<> struct TarsClass { static std::string name() { return "int64"; } }; -template<> struct TarsClass { static std::string name() { return "string"; } }; -template struct TarsClass > { static std::string name() { return std::string("list<") + TarsClass::name() + ">"; } }; -template struct TarsClass > { static std::string name() { return std::string("map<") + TarsClass::name() + "," + TarsClass::name() + ">"; } }; - -namespace detail -{ - // is_convertible - template struct type_of_size { char elements[N]; }; - - typedef type_of_size<1> yes_type; - typedef type_of_size<2> no_type; - - namespace meta_detail - { - struct any_conversion - { - template any_conversion(const volatile T&); - template any_conversion(T&); - }; - - template struct conversion_checker - { - static no_type _m_check(any_conversion ...); - static yes_type _m_check(T, int); - }; - } - - template - class is_convertible - { - static From _m_from; - public: - enum { value = sizeof( meta_detail::conversion_checker::_m_check(_m_from, 0) ) == sizeof(yes_type) }; - }; - - template - struct type2type { typedef T type; }; - - template - struct is_same_type - { - enum { value = is_convertible< type2type, type2type >::value }; - }; - - // enable_if - template struct enable_if_c { typedef T type; }; - - template struct enable_if_c {}; - - template - struct enable_if : public enable_if_c {}; - - template struct disable_if_c { typedef T type; }; - - template struct disable_if_c {}; - - template - struct disable_if : public disable_if_c {}; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -#define DEFINE_TARS_COPY_STRUCT_1(x, y, s) \ - inline void tars_copy_struct_impl(x& a, const y& b) { s; } \ - inline void tars_copy_struct_impl(y& a, const x& b) { s; } \ - inline void tars_copy_struct(x& a, const y& b) { tars_copy_struct_impl(a, b); } \ - inline void tars_copy_struct(y& a, const x& b) { tars_copy_struct_impl(a, b); } - -#define DEFINE_TARS_COPY_STRUCT(n, x, y) \ - DEFINE_TARS_COPY_STRUCT_1(n::x, y, n##_##x##_TARS_COPY_STRUCT_HELPER) - -inline void tars_copy_struct(char& a, const unsigned char& b) { a = b; } -inline void tars_copy_struct(unsigned char& a, const char& b) { a = b; } -template inline void tars_copy_struct(T& a, const T& b) { a = b; } - -template -inline void tars_copy_struct(std::vector& a, const std::vector& b, typename detail::disable_if, void ***>::type dummy = 0) -{ - a.resize(b.size()); - for(size_t i = 0; i < a.size(); ++i) - tars_copy_struct_impl(a[i], b[i]); -} - -template -inline void tars_copy_struct(std::map& a, const std::map& b, typename detail::disable_if, std::map >, void ***>::type dummy = 0) -{ - a.clear(); - std::pair pr; - typedef typename std::map::const_iterator IT; - IT ib = b.begin(), ie = b.end(); - for(; ib != ie; ++ib){ - tars_copy_struct_impl(pr.first, ib->first); - tars_copy_struct_impl(pr.second, ib->second); - a.insert(pr); - } -} -//////////////////////////////////////////////////////////////////////////////////////////////////// -} -#endif diff --git a/cpp/servant/tup/tup.h b/cpp/servant/tup/tup.h deleted file mode 100644 index 405582c06..000000000 --- a/cpp/servant/tup/tup.h +++ /dev/null @@ -1,686 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _WUP_H_ -#define _WUP_H_ -#include -#include -#include -#include - -//支æŒiphone -#ifdef __APPLE__ - #include "RequestF.h" -#elif defined ANDROID // android - #include "RequestF.h" -#else - #include "tup/RequestF.h" -#endif - -#ifdef __GNUC__ -# if __GNUC__ >3 || __GNUC_MINOR__ > 3 -# include -# endif -#endif - -using namespace std; -using namespace tars; - -namespace tup -{ - -//存放tars返回值的key -const string STATUS_RESULT_CODE = "STATUS_RESULT_CODE"; -const string STATUS_RESULT_DESC = "STATUS_RESULT_DESC"; - -///////////////////////////////////////////////////////////////////////////////// -// 属性å°è£…ç±» - -template class Alloc = std::allocator > - //template class Alloc = __gnu_cxx::__pool_alloc > -class UniAttribute -{ - typedef vector > VECTOR_CHAR_TYPE; - typedef map,Alloc< pair > > VECTOR_CHAR_IN_MAP_TYPE; - typedef map,Alloc< pair > > WUP_DATA_TYPE; - -public: - /** - * 构造函数 - */ - UniAttribute() - { - _iVer = 3; - } - - void setVersion(short iVer) - { - _iVer = iVer; - } - /** - * 添加属性值 - * - * @param T: 属性类型 - * @param name:属性å称 - * @param t: 属性值 - */ - template void put(const string& name, const T& t) - { - os.reset(); - - os.write(t, 0); - - VECTOR_CHAR_TYPE & v = _data[name]; - v.assign(os.getBuffer(), os.getBuffer() + os.getLength()); - - } - - void putUnknown(const string& name, const string& value) - { - os.reset(); - os.writeUnknownV2(value); - - VECTOR_CHAR_TYPE & v = _data[name]; - v.assign(os.getBuffer(), os.getBuffer() + os.getLength()); - - } - - void getUnknown(const string& name, string& value) - { - typename VECTOR_CHAR_IN_MAP_TYPE::iterator mit; - - mit = _data.find(name); - - if (mit != _data.end() && mit->second.size()>2) - { - //去掉DataHead::eStructBegin,DataHead::eStructEnd - value = string(&mit->second[0]+1, mit->second.size()-2); - return; - - } - throw runtime_error(string("UniAttribute not found key:") + name); - } - - /** - * 获å–属性值,属性ä¸å­˜åœ¨åˆ™æŠ›å‡ºå¼‚常 - * - * @throw runtime_error - * @param T: 属性类型 - * @param name:属性å称 - * @param t: 属性值输出å‚æ•° - */ - template void get(const string& name, T& t) - { - typename VECTOR_CHAR_IN_MAP_TYPE::iterator mit; - - mit = _data.find(name); - - if (mit != _data.end()) - { - is.reset(); - - is.setBuffer(mit->second); - - is.read(t, 0, true); - - return; - - } - throw runtime_error(string("UniAttribute not found key:") + name); - } - /** - * 获å–属性值,属性ä¸å­˜åœ¨åˆ™æŠ›å‡ºå¼‚常 - * - * @throw runtime_error - * @param T: 属性类型 - * @param name:属性å称 - * @return T: 属性值 - */ - template T get(const string& name) - { - T t; - - get(name, t); - - return t; - } - /** - * 获å–属性值,忽略异常,ä¸å­˜åœ¨çš„属性返回缺çœå€¼ - * - * @param T: 属性类型 - * @param name:属性å称 - * @param t: 属性值输出å‚æ•° - * @param def: 默认值 - */ - template void getByDefault(const string& name, T& t, const T& def) - { - try - { - get(name, t); - } - catch (runtime_error& e) - { - t = def; - } - } - /** - * 获å–属性值(忽略异常,def为缺çœå€¼) - * - * @param T: 属性类型 - * @param name:属性å称 - * @param: 默认值 - * @return T: 属性值 - */ - template T getByDefault(const string& name, const T& def) - { - T t; - - getByDefault(name, t, def); - - return t; - } - - /** - *清除全部属性值 - */ - void clear() - { - _data.clear(); - } - - /** ç¼–ç  - * - * @param buff: ç¼–ç ç»“果输出å‚æ•° - */ - void encode(string& buff) - { - os.reset(); - - os.write(_data, 0); - - buff.assign(os.getBuffer(), os.getLength()); - } - - /** ç¼–ç  - * - * @param buff: ç¼–ç ç»“果输出å‚æ•° - */ - void encode(vector& buff) - { - os.reset(); - - os.write(_data, 0); - - buff.assign(os.getBuffer(), os.getBuffer() + os.getLength()); - } - - /** ç¼–ç  - * - * @throw runtime_error - * @param buff:输出存放编ç ç»“果的buffer指针 - * @param len: 输入buff长度,输出编ç ç»“果长度 - */ - void encode(char* buff, size_t & len) - { - os.reset(); - - os.write(_data, 0); - - if(len < os.getLength()) throw runtime_error("encode error, buffer length too short"); - memcpy(buff, os.getBuffer(), os.getLength()); - len = os.getLength(); - } - - /** è§£ç  - * - * @throw runtime_error - * @param buff:待解ç å­—节æµçš„buffer指针 - * @param len: 待解ç å­—节æµçš„长度 - */ - void decode(const char* buff, size_t len) - { - is.reset(); - - is.setBuffer(buff, len); - - _data.clear(); - - is.read(_data, 0, true); - - } - /** - * è§£ç  - * - * @throw runtime_error - * @param buff: 待解ç çš„å­—èŠ‚æµ - */ - void decode(const vector& buff) - { - is.reset(); - - is.setBuffer(buff); - - _data.clear(); - - is.read(_data, 0, true); - - } - - /** - * 获å–已有的属性 - * - * @return const map>>& : 属性map - */ - const map >& getData() const - { - return _data; - } - - /** - * 判断属性集åˆæ˜¯å¦ä¸ºç©º - * - * @return bool:属性是å¦ä¸ºç©º - */ - bool isEmpty() - { - return _data.empty(); - } - - /** - * 获å–属性集åˆå¤§å° - * - * @return size_t: 集åˆå¤§å° - */ - size_t size() - { - return _data.size(); - } - - /** - * 判断属性是å¦å­˜åœ¨ - * - * @param key:属性å称 - * @return bool:是å¦å­˜åœ¨ - */ - bool containsKey(const string & key) - { - return _data.find(key) != _data.end(); - } - -protected: - VECTOR_CHAR_IN_MAP_TYPE _data; - short _iVer; - -public: - TarsInputStream is; - TarsOutputStream os; -}; - -///////////////////////////////////////////////////////////////////////////////// -// 请求ã€å›žåº”包å°è£…ç±» - -template class Alloc = std::allocator > -struct UniPacket : protected RequestPacket, public UniAttribute -{ -public: - /** - * 构造函数 - */ - UniPacket() - { - iVersion = 3; cPacketType = 0; - - iMessageType = 0; iRequestId = 0; - - sServantName = ""; sFuncName = ""; - - iTimeout = 0; sBuffer.clear(); - - context.clear(); status.clear(); - - UniAttribute::_iVer = iVersion; - - UniAttribute::_data.clear(); - - UniAttribute::_data.clear(); - } - - /** - * æ‹·è´æž„造 - * @param tup - */ - UniPacket(const UniPacket &tup) { *this = tup;} - - void setVersion(short iVer) - { - UniAttribute::_iVer = iVer; - iVersion = iVer; - } - - /** - * 由请求包生æˆå›žåº”åŒ…åŸºæœ¬ç»“æž„ï¼Œå›žå¡«å…³é”®çš„è¯·æ±‚ä¿¡æ¯ - * - * @return UniPacket: 回应包 - */ - UniPacket createResponse() - { - UniPacket respPacket; - - respPacket.sServantName = sServantName; - respPacket.sFuncName = sFuncName; - respPacket.iRequestId = iRequestId; - - return respPacket; - } - - /** - * ç¼–ç ï¼Œç»“果的包头4ä¸ªå­—èŠ‚ä¸ºæ•´ä¸ªåŒ…çš„é•¿åº¦ï¼Œç½‘ç»œå­—èŠ‚åº - * - * @throw runtime_error - * @param buff: ç¼–ç ç»“果输出å‚æ•° - */ - void encode(string& buff) - { - TarsOutputStream &os = UniAttribute::os; - - os.reset(); - - doEncode(os); - - tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength()); - buff.assign((const char*)&iHeaderLen, sizeof(tars::Int32)); - - buff.append(os.getBuffer(), os.getLength()); - } - - /** - * ç¼–ç ï¼Œç»“果的包头4ä¸ªå­—èŠ‚ä¸ºæ•´ä¸ªåŒ…çš„é•¿åº¦ï¼Œç½‘ç»œå­—èŠ‚åº - * - * @throw runtime_error - * @param buff: ç¼–ç ç»“果输出å‚æ•° - */ - void encode(vector& buff) - { - TarsOutputStream & os = UniAttribute::os; - - os.reset(); - - doEncode(os); - - tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength()); - - buff.resize(sizeof(tars::Int32) + os.getLength()); - memcpy(&buff[0], &iHeaderLen, sizeof(tars::Int32)); - memcpy(&buff[sizeof(tars::Int32)], os.getBuffer(), os.getLength()); - - } - - /** - * ç¼–ç ï¼Œç»“果的包头4ä¸ªå­—èŠ‚ä¸ºæ•´ä¸ªåŒ…çš„é•¿åº¦ï¼Œç½‘ç»œå­—èŠ‚åº - * @throw runtime_error - * @param buff:存放编ç ç»“果的buffer指针 - * @param len: 输入buff长度,输出编ç ç»“果长度 - */ - void encode(char* buff, size_t & len) - { - TarsOutputStream &os = UniAttribute::os; - - os.reset(); - - doEncode(os); - - tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength()); - if(len < sizeof(tars::Int32) + os.getLength()) throw runtime_error("encode error, buffer length too short"); - - memcpy(buff, &iHeaderLen, sizeof(tars::Int32)); - memcpy(buff + sizeof(tars::Int32), os.getBuffer(), os.getLength()); - - len = sizeof(tars::Int32) + os.getLength(); - } - - /** è§£ç  - * - * @throw runtime_error - * @param buff:待解ç å­—节æµçš„buffer指针 - * @param len: 待解ç å­—节æµçš„长度 - */ - - void decode(const char* buff, size_t len) - { - if(len < sizeof(tars::Int32)) throw runtime_error("packet length too short"); - - TarsInputStream &is = UniAttribute::is; - - is.reset(); - - is.setBuffer(buff + sizeof(tars::Int32), len - sizeof(tars::Int32)); - - readFrom(is); - - UniAttribute::_iVer = iVersion; - - is.reset(); - - is.setBuffer(sBuffer); - - UniAttribute::_data.clear(); - - is.read(UniAttribute::_data, 0, true); - - } -public: - /** - * 获å–消æ¯version - * @return tars::Short - */ - tars::Short getVersion() const { return iVersion; } - /** - * 获å–消æ¯ID - * @return tars::Int32 - */ - tars::Int32 getRequestId() const { return iRequestId; } - /** - * 设置请求ID - * @param value - */ - void setRequestId(tars::Int32 value) { iRequestId = value; } - /** - * 获å–对象å称 - * @return const std::string& - */ - const std::string& getServantName() const { return sServantName; } - /** - * 设置对象å称 - * @param value - */ - void setServantName(const std::string& value) { sServantName = value; } - /** - * 获å–方法å - * @return const std::string& - */ - const std::string& getFuncName() const { return sFuncName; } - /** - * 设置方法å - * @param value - */ - void setFuncName(const std::string& value) { sFuncName = value; } - -protected: - /** - * å†…éƒ¨ç¼–ç  - */ - void doEncode(TarsOutputStream& os) - { - //ServantNameã€FuncNameä¸èƒ½ä¸ºç©º - if(sServantName.empty()) throw runtime_error("ServantName must not be empty"); - if(sFuncName.empty()) throw runtime_error("FuncName must not be empty"); - - os.reset(); - - os.write(UniAttribute::_data, 0); - - sBuffer.assign(os.getBuffer(), os.getBuffer() + os.getLength()); - - os.reset(); - - writeTo(os); - } -}; - -///////////////////////////////////////////////////////////////////////////////// -// 调用TARSçš„æœåŠ¡æ—¶ä½¿ç”¨çš„ç±» - -template class Alloc = std::allocator> -struct TarsUniPacket: public UniPacket -{ -public: - TarsUniPacket(){}; - TarsUniPacket(const UniPacket &tup) - : UniPacket(tup) {}; - - /** - * 设置å议版本 - * @param value - */ - void setTarsVersion(tars::Short value) { UniPacket::setVersion(value); } - - /** - * 设置调用类型 - * @param value - */ - void setTarsPacketType(tars::Char value) { this->cPacketType = value; } - - /** - * 设置消æ¯ç±»åž‹ - * @param value - */ - void setTarsMessageType(tars::Int32 value) { this->iMessageType = value; } - - /** - * 设置超时时间 - * @param value - */ - void setTarsTimeout(tars::Int32 value) { this->iTimeout = value; } - - /** - * 设置å‚æ•°ç¼–ç å†…容 - * @param value - */ - void setTarsBuffer(const vector& value) { this->sBuffer = value; } - - /** - * 设置上下文 - * @param value - */ - void setTarsContext(const map& value) { this->context = value; } - - /** - * 设置特殊消æ¯çš„状æ€å€¼ - * @param value - */ - void setTarsStatus(const map& value) { this->status = value; } - - /** - * 获å–å议版本 - * @return tars::Short - */ - tars::Short getTarsVersion() const { return this->iVersion; } - - /** - * 获å–调用类型 - * @return tars::Char - */ - tars::Char getTarsPacketType() const { return this->cPacketType; } - - /** - * 获å–消æ¯ç±»åž‹ - * @return tars::Int32 - */ - tars::Int32 getTarsMessageType() const { return this->iMessageType; } - - /** - * 获å–超时时间 - * @return tars::Int32 - */ - tars::Int32 getTarsTimeout() const { return this->iTimeout; } - - /** - * 获å–å‚æ•°ç¼–ç åŽå†…容 - * @return const vector& - */ - const vector& getTarsBuffer() const { return this->sBuffer; } - - /** - * 获å–ä¸Šä¸‹æ–‡ä¿¡æ¯ - * @return const map& - */ - const map& getTarsContext() const { return this->context; } - - /** - * 获å–特殊消æ¯çš„状æ€å€¼ - * @return const map& - */ - const map& getTarsStatus() const { return this->status; } - - /** - * 获å–调用tars的返回值 - * - * @retrun tars::Int32 - */ - tars::Int32 getTarsResultCode() const - { - map::const_iterator it; - if((it = this->status.find(STATUS_RESULT_CODE)) == this->status.end()) - { - return 0; - } - else - { - return atoi(it->second.c_str()); - } - } - - /** - * 获å–调用tars的返回æè¿° - * - * @retrun string - */ - string getTarsResultDesc() const - { - map::const_iterator it; - if((it = this->status.find(STATUS_RESULT_DESC)) == this->status.end()) - { - return ""; - } - else - { - return it->second; - } - } - -}; - -#ifdef __GNUC__ -# if __GNUC__ >3 || __GNUC_MINOR__ > 3 - typedef UniAttribute UniAttrPoolAlloc; - typedef UniPacket UniPacketPoolAlloc; - typedef TarsUniPacket TarsUniPacketPoolAlloc; -# endif -#endif - -} -//////////////////////////////////////////////////////////////////////////////////////////////// -#endif diff --git a/cpp/test/CMakeLists.txt b/cpp/test/CMakeLists.txt deleted file mode 100644 index c424be503..000000000 --- a/cpp/test/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 2.8) -project(test) - -add_subdirectory(testUtil) -add_subdirectory(testServant) -add_subdirectory(testFramework) - diff --git a/cpp/test/README.md b/cpp/test/README.md deleted file mode 100644 index 0225ce35c..000000000 --- a/cpp/test/README.md +++ /dev/null @@ -1,8 +0,0 @@ -该工程是Tars测试程åºã€‚ - - -目录å称 |目录功能 -------------------|---------------- -testUtil |æµ‹è¯•åŸºç¡€å…¬å…±åº“çš„ä»£ç  -testServant |测试rpcç›¸å…³çš„ä»£ç  -testFramework |测试框架基础æœåŠ¡çš„ä»£ç  \ No newline at end of file diff --git a/cpp/test/testFramework/CMakeLists.txt b/cpp/test/testFramework/CMakeLists.txt deleted file mode 100644 index daf58152c..000000000 --- a/cpp/test/testFramework/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 2.8) -project(testFramework) - - -add_subdirectory(testAdminRegistry) - - - diff --git a/cpp/test/testFramework/testAdminRegistry/CMakeLists.txt b/cpp/test/testFramework/testAdminRegistry/CMakeLists.txt deleted file mode 100644 index 257081676..000000000 --- a/cpp/test/testFramework/testAdminRegistry/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) -set(TARGETNAME "testAdminRegistry") - -include_directories(${util_SOURCE_DIR}/include) -include_directories(${tools_SOURCE_DIR}) -include_directories(${servant_SOURCE_DIR}) -include_directories(${framework_SOURCE_DIR}/protocol) - -link_libraries(tarsservant tarsparse tarsutil pthread z rt) - -aux_source_directory(. DIR_SRCS) -add_executable(${TARGETNAME} ${DIR_SRCS}) - diff --git a/cpp/test/testFramework/testAdminRegistry/main.cpp b/cpp/test/testFramework/testAdminRegistry/main.cpp deleted file mode 100644 index 8ddf5f32a..000000000 --- a/cpp/test/testFramework/testAdminRegistry/main.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "AdminReg.h" -#include "servant/Communicator.h" -#include - -using namespace std; -using namespace tars; - -class Test1 -{ -public: - Test1(const string &sStr); - ~Test1(); - void th_dohandle(int excut_num); -private: - Communicator _comm; - AdminRegPrx _prx; -}; - -Test1::Test1(const string &sStr) -{ - _comm.stringToProxy("tars.tarsAdminRegistry.AdminRegObj@tcp -h 10.208.139.242 -p 12000 -t 60000", _prx); -} - -Test1::~Test1() -{ - -} - -void Test1::th_dohandle(int excut_num) -{ - for(int i=0; itars_set_timeout(15000)->getServerState(application, serverName, nodeName, state, result); - if(iRet == 0) - { - cout << "succ." << state.presentStateInReg << endl; - } - else - { - cout << "fail." << endl; - } - } - catch(TC_Exception &e) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl; - } - } -} - -int main(int argc,char ** argv) -{ - if(argc != 4) - { - cout << "usage: " << argv[0] << " ThreadNum CallTimes sObj" << endl; - return -1; - } - - string s = string(argv[3]); - - Test1 test1(s); - try - { - - tars::Int32 times = TC_Common::strto(string(argv[3])); - - test1.th_dohandle(times); - - } - catch(exception &e) - { - cout< &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - usage(); - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTarsFiles = option.getSingle(); - - check(vTarsFiles); - - if(option.hasParam("help")) - { - usage(); - } - - string streamFile; - - TarsAnalyzer jAnalyzer; - - if(option.hasParam("FileBuff")) - { - streamFile = option.getValue("FileBuff"); - jAnalyzer.setStreamFileName(streamFile); - } - else - { - usage(); - } - - - if(option.hasParam("structName")) - { - string sStructName = option.getValue("structName"); - if(!sStructName.empty()) - { - jAnalyzer.setStructName(sStructName); - } - else - { - cout<<"structName shouldn't be empty"<(pos) >= 0) - { - jAnalyzer.setStartPos(tars::TC_Common::strto(pos)); - } - else - { - cout<<"startPos should be >=0 "<parse("./tmpTupRequestF.tars"); - - tars::TC_File::removeFile("./tmpTupRequestF.tars", true); - - //analyze tup tars - jAnalyzer.analyzeFile("./tmpTupRequestF.tars", streamFile); - jAnalyzer.setTup(false); - - - if (vTarsFiles.size() > 0) - { - //需è¦è€ƒè™‘tup包中的内容在多个tars文件的情况 - g_parse->parse(vTarsFiles[0]); - //分æžtupæ•°æ®åŒ…中的sBufferæ•°æ®,å³structNameå¯¹åº”çš„æ•°æ® - jAnalyzer.analyzeWithTupBuffer(vTarsFiles[0], ""); - } - return 0; - - } - try - { - for (size_t i = 0; i < vTarsFiles.size(); i++) - { - g_parse->parse(vTarsFiles[i]); - jAnalyzer.analyzeFile(vTarsFiles[i], streamFile); - } - }catch(exception& e) - { - cerr< - -using namespace std; - - -/// 读å–一个指定类型的数æ®ï¼ˆåŸºæœ¬ç±»åž‹ï¼‰ -template -T TarsAnalyzer::readByType() -{ - T n; - readBuf(&n, sizeof(n)); - return n; -} - -void TarsAnalyzer::analyzeWithTupBuffer(const string &file, const string& putName) -{ - string stName; - if(_tupVersion == 2) - { - map > >::iterator it1 = _tup2_helper.find(_putName); - if (it1 != _tup2_helper.end()) - { - cout << "put name:" << it1->first << endl; - map >::iterator it2 = it1->second.begin(); - while(it2 != it1->second.end()) - { - //去掉å字空间 - string::size_type pos = 0; - pos = it2->first.find("."); - if (pos != string::npos) - { - stName = it2->first.substr(pos + 1); - } - else - { - stName = it2->first; - } - _StructName = stName; //设定需è¦åˆ†æžçš„结构å - cout << "struct name:" << stName << endl; - - - vector v = it2->second; - - //分æžtup包中的sBuffer - setBuffer(&v[0], v.size()) ; - std::vector contexts = g_parse->getContexts(); - for(size_t i = 0; i < contexts.size(); i++) - { - if(file == contexts[i]->getFileName()) - { - analyzeContext(contexts[i]); - } - } - it2++; - } - } - else - { - cout << "not find tup put name:" << _putName< >::const_iterator it = _tup3_helper.find(_putName); - if( it != _tup3_helper.end()) - { - cout << "put name:" << it->first << endl; - cout << "struct name:" << _StructName << endl; - vector v = it->second; - - //分æžtup包中的sBuffer - setBuffer(&v[0], v.size()) ; - std::vector contexts = g_parse->getContexts(); - for(size_t i = 0; i < contexts.size(); i++) - { - if(file == contexts[i]->getFileName()) - { - analyzeContext(contexts[i]); - } - } - - } - else - { - cout << "not find tup put name:" << _putName< 0) - { - _cur = _startPos; - _last_pos = _startPos; - - } - if (_bTup) - { - checkTupBuffer(); - } - std::vector contexts = g_parse->getContexts(); - for(size_t i = 0; i < contexts.size(); i++) - { - if(file == contexts[i]->getFileName()) - { - analyzeContext(contexts[i]); - } - } -} - -void TarsAnalyzer::checkTupBuffer(void) -{ - if(_buf_len < sizeof(int)) - { - cout << "ERROR, tup buffer too short!" << endl; - exit(-1); - } - uint32_t iHeaderLen; - - if (_buf) - { - iHeaderLen = ntohl(*(uint32_t*)(_buf)); - } - else - { - cout << "ERROR, tup buffer not set!" << endl; - exit(-1); - } - - //长度大于10M - if (iHeaderLen > 10000000) - { - cout << "packet length too long,len:" + tars::TC_Common::tostr(iHeaderLen) << endl; - exit(-1); - } - - if (iHeaderLen != (_buf_len - chars_buf_reserve)) - { - cout << "ERROR, buffer size: " << _buf_len - chars_buf_reserve << " not eq header len:" << iHeaderLen << endl; - exit(-1); - } - - cout << "check tup buffer ok!" << endl; - - -} - -void TarsAnalyzer::peekBuf(void * buf, size_t len, size_t offset) -{ - if (_cur + offset + len > _buf_len) - { - - cout<< "ERROR, buffer overflow when peekBuf, buf_len:" << _buf_len << " cur:" << _cur << " len:"<< len <skip(sizeof(char)); - break; - case AnalyzerDataHead::eShort: - this->skip(sizeof(short)); - break; - case AnalyzerDataHead::eInt32: - this->skip(sizeof(int)); - break; - case AnalyzerDataHead::eInt64: - this->skip(sizeof(long long)); - break; - case AnalyzerDataHead::eFloat: - this->skip(sizeof(float)); - break; - case AnalyzerDataHead::eDouble: - this->skip(sizeof(double)); - break; - case AnalyzerDataHead::eString1: - { - size_t len = readByType(); - this->skip(len); - } - break; - case AnalyzerDataHead::eString4: - { - size_t len = ntohl(readByType()); - this->skip(len); - } - break; - case AnalyzerDataHead::eMap: - { - Int32 size; - read(size, tag, realType); - for (int i = 0; i < size * 2; ++i) - skipField(); - } - break; - case AnalyzerDataHead::eList: - { - Int32 size; - read(size, tag, realType); - for (int i = 0; i < size; ++i) - skipField(); - } - break; - case AnalyzerDataHead::eSimpleList: - { - AnalyzerDataHead h; - h.readFrom(*this); - if (h.getType() != AnalyzerDataHead::eChar) - { - - cout << "ERROR, skipField with invalid type, type value: " << (int)type <<", " << h.tostrType(h.getType()) << endl; - - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - } - Int32 size; - read(size, tag, realType); - this->skip(size); - } - break; - case AnalyzerDataHead::eStructBegin: - skipToStructEnd(); - break; - case AnalyzerDataHead::eStructEnd: - case AnalyzerDataHead::eZeroTag: - break; - default: - { - cout << "ERROR, skipField with invalid type, type value:" << (int)type << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - } - } -} - - -void TarsAnalyzer::backToLast() -{ - _cur = _last_pos; -} - - -int TarsAnalyzer::readType(const TypePtr &pPtr, int expectTag, bool isRequire) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) return readBuiltin(bPtr, expectTag, isRequire, NULL); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return readVector(vPtr, expectTag,isRequire, NULL); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if(mPtr) return readMap(mPtr, expectTag, isRequire); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return readStruct(sPtr, NULL); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return 0; - - if (!pPtr) return 0; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - return 0; -} - -/*******************************BuiltinPtr********************************/ -string TarsAnalyzer::tostrBuiltin(const BuiltinPtr &pPtr) -{ - string s; - - switch(pPtr->kind()) - { - case Builtin::KindBool: - s = " bool "; - break; - case Builtin::KindByte: - s = " char "; - break; - case Builtin::KindShort: - //为了兼容javaæ— unsigned, 编结ç æ—¶æŠŠtars问件中 unsigned char 对应到short - //c++中需è¦è¿˜åŽŸå›žæ¥ - s = (pPtr->isUnsigned()?"uchar":" short "); - break; - case Builtin::KindInt: - s = (pPtr->isUnsigned()?"ushort":" int "); - break; - case Builtin::KindLong: - s = (pPtr->isUnsigned()?"uint":" int64 "); - break; - case Builtin::KindFloat: - s = " float "; - break; - case Builtin::KindDouble: - s = " double "; - break; - case Builtin::KindString: - if(pPtr->isArray()) - s = "char[]"; //char a [8]; - else - s = " string ";//string a; - break; - case Builtin::KindVector: - s = " vector "; - break; - case Builtin::KindMap: - s = " map "; - break; - default: - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl; - exit(-1); - break; - } - - return s; -} -string TarsAnalyzer::tostr(const TypePtr &pPtr) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if(bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if(vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if(mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if(sPtr) return tostrStruct(sPtr); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - //if(ePtr) return tostrEnum(ePtr); - - if(!pPtr) return "void"; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - return ""; -} - -string TarsAnalyzer::tostrMap(const MapPtr &pPtr, bool bNew) -{ - string s = Builtin::builtinTable[Builtin::KindMap] + string("<") + tostr(pPtr->getLeftTypePtr()) + ", " + tostr(pPtr->getRightTypePtr()); - if(MapPtr::dynamicCast(pPtr->getRightTypePtr()) || VectorPtr::dynamicCast(pPtr->getRightTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} - - -string TarsAnalyzer::getTab() -{ - ostringstream s; - for(int i = 0; i < _itab; i++) - { - s << " "; - } - - return s.str(); -} -string TarsAnalyzer::getSep() -{ - ostringstream s; - for(int i = 0; i < CHAR_WIDTH_ID; i++) - { - s << "-"; - } - - return s.str(); -} - -/** - * 增加tabæ•° - */ -void TarsAnalyzer::incTab() -{ - _itab++; -} - -void TarsAnalyzer::addTab(int count) -{ - _itab += count; -} - -void TarsAnalyzer::eraseTab(int count) -{ - _itab -= count; -} - -/** - * å‡å°‘tabæ•° - */ -void TarsAnalyzer::decTab() -{ - _itab--; -} - -void TarsAnalyzer::read(double& n, uint8_t expecttag, int & realType, bool isRequire) -{ - AnalyzerDataHead h; - h.readFrom(*this); - - int ret = compareTag(h.getTag(), expecttag, isRequire); - expecttag = h.getTag(); - realType = h.getType(); - n = 0.0f; - if (ret != eOK) - { - return; - } - - switch (h.getType()) - { - case AnalyzerDataHead::eZeroTag: - n = 0; - break; - case AnalyzerDataHead::eFloat: - n = readByType(); - n = tars_ntohf(n); - break; - case AnalyzerDataHead::eDouble: - this->readBuf(&n, sizeof(n)); - n = tars_ntohd(n); - break; - default: - { - cout << "ERROR, read string type mismatch, tag: " << (int) expecttag << ", get type: " << (int)h.getType() << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl;; - exit(-1); - } - - } -} - -void TarsAnalyzer::read(string& s, uint8_t& expecttag, int& realType, bool isRequire ) -{ - AnalyzerDataHead h; - h.readFrom(*this); - - int ret = compareTag(h.getTag(), expecttag, isRequire); - - expecttag = h.getTag(); - realType = h.getType(); - s = ""; - - if (ret != eOK) - { - return; - } - switch (h.getType()) - { - case AnalyzerDataHead::eString1: - { - size_t len = readByType(); - char ss[256]; - this->readBuf(ss, len); - s.assign(ss, ss + len); - } - break; - case AnalyzerDataHead::eString4: - { - //读å–string4的长度 - uint32_t len = ntohl(readByType()); - if (len > TARS_MAX_STRING_LENGTH) - { - - } - char *ss = new char[len]; - try - { - this->readBuf(ss, len); - s.assign(ss, ss + len); - } - catch (...) - { - delete[] ss; - throw; - } - delete[] ss; - } - break; - default: - { - cout << "ERROR, read string type mismatch, tag: " << (int) expecttag << ", get type: " << (int)h.getType() << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl;; - exit(-1); - } - } -} -void TarsAnalyzer::read(Int32& n, uint8_t& tag, int & realType, bool isRequire) -{ - AnalyzerDataHead h; - h.readFrom(*this); - - int ret = compareTag(h.getTag(), tag, isRequire); - tag = h.getTag(); - realType = h.getType(); - n = 0; - if (ret != eOK) - { - return; - } - switch (h.getType()) - { - case AnalyzerDataHead::eZeroTag: - n = 0; - break; - case AnalyzerDataHead::eChar: - n = readByType(); - break; - case AnalyzerDataHead::eShort: - n = (short) ntohs(readByType()); - break; - case AnalyzerDataHead::eInt32: - this->readBuf(&n, sizeof(n) ); - n = ntohl((Int32)n); - break; - default: - { - cout<< "ERROR, read numeric type mismatch, buf tag: " << (int)tag << ", get type: " << h.tostrType(h.getType()) << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl;; - exit(-1); - } - } - -} - - -void TarsAnalyzer::read(Int64& n, uint8_t& tag, int & realType, bool isRequire) -{ - AnalyzerDataHead h; - h.readFrom(*this); - - int ret = compareTag(h.getTag(), tag, isRequire); - tag = h.getTag(); - realType = h.getType(); - n = 0; - if (ret != eOK) - { - return; - } - switch (h.getType()) - { - case AnalyzerDataHead::eZeroTag: - n = 0; - break; - case AnalyzerDataHead::eChar: - n = readByType(); - break; - case AnalyzerDataHead::eShort: - n = (short) ntohs(readByType()); - break; - case AnalyzerDataHead::eInt32: - n = (Int32) ntohl(readByType()); - break; - case AnalyzerDataHead::eInt64: - this->readBuf(&n, sizeof(n) ); - n = tars_ntohll((Int64)n); - break; - default: - { - cout<< "ERROR, read numeric type mismatch, buf tag: " << (int)tag << ", get type: " << h.tostrType(h.getType()) << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl;; - exit(-1); - } - } - -} - - -template -void TarsAnalyzer::read(std::map& m, uint8_t tag, bool isRequire) -{ - uint8_t utag = 0; - int realtype; - - AnalyzerDataHead h; - h.readFrom(*this); - switch (h.getType()) - { - case AnalyzerDataHead::eMap: - { - int size; - read(size, utag, realtype); - if (size < 0) - { - char s[128]; - snprintf(s, sizeof(s), "invalid map, tag: %d, size: %d", tag, size); - cout << s << endl; - } - m.clear(); - - for (int i = 0; i < size; ++i) - { - std::pair pr; - utag = 0; - read(pr.first, utag, realtype); - utag = 1; - read(pr.second, utag, realtype); - m.insert(pr); - } - } - break; - default: - { - char s[64]; - snprintf(s, sizeof(s), "read 'map' type mismatch, tag: %d, get type: %d.", tag, h.getType()); - cout << s << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - } - } - -} - -template -void TarsAnalyzer::read(std::vector& v, uint8_t& tag, int & realType, bool isRequire ) -{ - //if (skipToTag(tag)) - { - uint8_t t=0; - int realType; - AnalyzerDataHead h; - h.readFrom(*this); - switch (h.getType()) - { - case AnalyzerDataHead::eSimpleList: - { - AnalyzerDataHead hh; - hh.readFrom(*this); - if (hh.getType() != AnalyzerDataHead::eChar) - { - char s[128]; - snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d, %d", tag, h.getType(), hh.getType()); - //throw TarsDecodeMismatch(s); - } - int size; - read(size, t, realType); - if (size < 0) - { - char s[128]; - snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, %d, size: %d", tag, h.getType(), hh.getType(), size); - //throw TarsDecodeInvalidValue(s); - } - v.resize(size); - readBuf(&v[0], size); - } - break; - - - default: - { - char s[128]; - snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d", tag, h.getType()); - //throw TarsDecodeMismatch(s); - } - } - } - - -} - -int TarsAnalyzer::readVector(const VectorPtr &pPtr, int expectTag, bool isRequire, TypeIdPtr pType) -{ - { - int type; - AnalyzerDataHead h; - h.readFrom(*this); - - uint8_t t=0; - uint8_t tag =h.getTag(); - int ret = compareTag(tag, expectTag,isRequire); - if (ret != eOK) - { - return 0; - } - switch (h.getType()) - { - case AnalyzerDataHead::eList: - { - int size; - read(size, t, type); - cout << getTab()<< "read buf tag " << (int)tag << " vector size:" << size << endl; - if (size < 0|| size > 10 * 1024 * 1024) - { - cout<< getTab() << "ERROR, invalid size, tag: " << (int)tag << endl; - exit(-1); - } - //addTab(7); - - cout << getTab()<<"{ //tag:" << (int)tag << endl; - for (int i = 0; i < size; ++i) - { - - cout << getTab() << " index:" << i << " "; - readType(pPtr->getTypePtr(), 0, isRequire); - } - - cout << getTab()<<"} //tag:" << (int)tag << endl; - //eraseTab(7); - } - break; - - case AnalyzerDataHead::eSimpleList: - { - - AnalyzerDataHead hh; - hh.readFrom(*this); - if (hh.getType() != AnalyzerDataHead::eChar) - { - cout<< getTab() << "error, type mismatch, tag: " << (int)tag; - - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - } - int size; - read(size, t, type); - - cout<< getTab() << "read buf tag " << (int)tag << " vector byte size:" << size << endl; - if (size < 0 || size > 10 * 1024 * 1024) - { - cout<< getTab() << "ERROR, invalid size " << size << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - - } - - char *ss = new char[size]; - try - { - memset(ss, 0, size); - readBuf(ss, size); - // debug å†æ‰“å¼€ - //cout << getTab()<< "tag " << (int)tag << " vector byte :\n" << tars::TC_Common::bin2str(ss, size, " ", chars_one_line) << endl; - } - catch (...) - { - delete[] ss; - throw; - } - delete[] ss; - - - } - break; - default: - { - cout << getTab() << "ERROR, read 'vector' type mismatch, tag: " << (int)tag << " get type: " << h.tostrType(h.getType()) << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl;; - exit(-1); - } - } - } - - return 0; -} - - - -int TarsAnalyzer::readMap(MapPtr pPtr, int expectTag, bool isRequire) -{ - - int type; - AnalyzerDataHead h; - h.readFrom(*this); - - uint8_t tag = h.getTag(); - - int ret = compareTag(tag, expectTag,isRequire); - if (ret != eOK) - { - return 0; - } - switch (h.getType()) - { - case AnalyzerDataHead::eMap: - { - int size; - uint8_t t = 0; - read(size, t, type); - - cout << "read " << " map size:" << size << endl; - if (size < 0|| size > 10 * 1024 * 1024) - { - cout<< getTab() << "ERROR, invalid size " << size << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - } - //addTab(7); - cout << getTab()<< "{ //tag:" << (int)tag << endl; - for (int i = 0; i < size; ++i) - { - - cout << getTab() << " index:" << i << " first:"; - readType(pPtr->getLeftTypePtr(), 0, isRequire); - cout<< getTab()<< " index:" << i << " second:" ; - readType(pPtr->getRightTypePtr(), 1, isRequire); - - } - - cout << getTab()<< "} //tag:" << (int)tag << endl; - //eraseTab(7); - } - break; - default: - { - cout << "ERROR, read map type mismatch, tag:" << (int)tag <<"get type " << h.tostrType(h.getType() )<< endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << endl;; - exit(-1); - } - } - - return 0; - -} - -int TarsAnalyzer::readEnum(EnumPtr pPtr, int expectTag, bool isRequire) -{ - - int n = 0; - AnalyzerDataHead h; - h.readFrom(*this); - uint8_t tag = h.getTag(); - int ret = compareTag(tag, expectTag,isRequire); - if (ret != eOK) - { - return 0; - } - switch (h.getType()) - { - case AnalyzerDataHead::eZeroTag: - n = 0; - break; - case AnalyzerDataHead::eChar: - n = readByType(); - break; - case AnalyzerDataHead::eShort: - n = (short) ntohs(readByType()); - break; - case AnalyzerDataHead::eInt32: - this->readBuf(&n, sizeof(n)); - n = ntohl(n); - break; - - default: - { - cout<< "ERROR, read enum type mismatch, buf tag: " << (int)tag << ", get type: " << h.tostrType(h.getType()) << endl; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos ; - exit(-1); - } - } - cout << "read enum:" << n << endl; - return 0; - - -} - -int TarsAnalyzer::readBuiltin(const BuiltinPtr &pPtr, int expectTag, bool bIsRequire, TypeIdPtr pType) -{ - uint8_t tag = expectTag; - int realType = 0 ; - - switch (pPtr->kind()) - { - case Builtin::KindBool: - - case Builtin::KindByte: - - case Builtin::KindShort: - - case Builtin::KindInt: - - case Builtin::KindLong: - { - - Int64 n; - read(n, tag, realType, bIsRequire); - cout << "read buf tag [" <<(int)tag <<"|"<< AnalyzerDataHead::tostrType(realType)<<"|"< tag) - { - // å¯èƒ½æ˜¯TARS文件中增加了å¯é€‰å­—段 - //å¯èƒ½TARS文件把tagå·å†™é”™äº† - //å¯èƒ½æ˜¯TARS文件中把原æ¥çš„å¯é€‰å­—段去掉了 - cout << getTab() << "warning ,optional tag is " << expectTag << " < tag:" << tag<< " from buf !" ; - cout << getTab() << " _cur:" << _cur << " _last_pos:" << _last_pos << " "; - backToLast(); - skipField(); - return eNeedSkipTag; - } - return eOK; -} -string TarsAnalyzer::tostrVector(const VectorPtr &pPtr) -{ - //数组类型 - if(pPtr->isArray()) - { - return tostr(pPtr->getTypePtr()); - } - - //指针类型 - if(pPtr->isPointer()) - { - return tostr(pPtr->getTypePtr())+" *"; - } - - string s = Builtin::builtinTable[Builtin::KindVector] + string("<") + tostr(pPtr->getTypePtr()); - - if(MapPtr::dynamicCast(pPtr->getTypePtr()) || VectorPtr::dynamicCast(pPtr->getTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} - - -string TarsAnalyzer::tostrStruct(const StructPtr &pPtr) const -{ - return pPtr->getSid(); -} - -string TarsAnalyzer::tostrEnum(const EnumPtr &pPtr) -{ - return pPtr->getSid(); -} - - -int TarsAnalyzer::readStruct(const StructPtr & pPtr, const NamespacePtr & nPtr) -{ - vector& member = pPtr->getAllMemberPtr(); - - AnalyzerDataHead h; - size_t hLen = h.peekFrom(*this); - if (AnalyzerDataHead::eStructBegin == h.getType()) // 检查结构开始 - { - cout << endl; - incTab(); - cout << getTab()<< "struct " << pPtr->getId() << " {" << endl; - skip(hLen); - } - - //解æžstructæˆå‘˜å˜é‡ - for (size_t i = 0; i < member.size(); i++) - { - cout<< getTab() <<"expect tag "; - cout.setf(ios::right); - cout.width(CHAR_WIDTH_TAG); - cout<< member[i]->getTag(); - cout.width(CHAR_WIDTH_ID); // æˆå‘˜å12字符宽 - cout<< member[i]->getId(); - - //获å–tupç‰ˆæœ¬å· - if(isTup() && (pPtr->getId() == "RequestPacket")) - { - if(member[i]->getTag() == 1) - { - char iVer = 2; - this->peekBuf(&iVer, sizeof(iVer),1); - _tupVersion = iVer; - } - else if(member[i]->getTag() == 7)///解æžsbuffer - { - _tup_sbuffer_pos = _cur; - _cur +=2; //7D 00 - - int n; - uint8_t tag = 0; - int realType; - read(n, tag, realType, true ); //sbuffer的长度 - - if(_tupVersion == 2) - { - read(_tup2_helper, 7); - _bIsDumpTup?dumpTup(_tup2_helper):(void(0)); - } - else///3 - { - read(_tup3_helper, 7); - _bIsDumpTup?dumpTup(_tup3_helper):(void(0)); - } - - _cur = _tup_sbuffer_pos; - } - } - - //读å–内置基本类型 - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - if (bPtr) - { - cout.width(CHAR_WIDTH_TYPE); - cout<getTag(), member[i]->isRequire(), NULL); - } - - VectorPtr vPtr = VectorPtr::dynamicCast(member[i]->getTypePtr()); - if (vPtr) - { - cout.width(CHAR_WIDTH_TYPE); - cout<getTag(),member[i]->isRequire(), NULL); - } - - MapPtr mPtr = MapPtr::dynamicCast(member[i]->getTypePtr()); - if (mPtr) - { - cout.width(CHAR_WIDTH_TYPE); - cout<getTag(), member[i]->isRequire()); - } - - StructPtr sPtr = StructPtr::dynamicCast(member[i]->getTypePtr()); - if (sPtr) - { - cout.width(CHAR_WIDTH_TYPE); - cout<< tostrStruct(sPtr); - cout<getTypePtr()); - if (ePtr) - { - - cout.width(CHAR_WIDTH_TYPE); - cout<< tostrEnum(ePtr); - cout<getTag(), member[i]->isRequire()); - } - - } - - if (!_bTup) - { - hLen = h.peekFrom(*this); //buf中没有结构结æŸæ ‡å¿—,也ä¸ä¼šæŠ›å¼‚常,因为setBufferçš„buf_len有1个ä¿æŠ¤å­—节 - if (AnalyzerDataHead::eStructEnd == h.getType() || _cur >= _buf_len-chars_buf_reserve) // æ£€æŸ¥ç»“æž„ç»“æŸ - { - cout << getTab() << "} //struct " << pPtr->getId() << endl; - decTab(); - skip(hLen); - if (pPtr->getId() == _StructName) - { - cout << "analyze " << _streamFileName << " with struct " << _StructName << " ok!" << endl; - } - } - } - return 0; - -} - -void TarsAnalyzer::dumpTup(const map >& tupData) -{ - map >::const_iterator it = tupData.begin(); - cout << "\ntup attr begin:" << endl; - while(it != tupData.end()) - { - cout << "tup Attribute name: " << it->first << endl; - vector vContent = it->second; - cout << "struct buff: "; - cout << tars::TC_Common::bin2str(&vContent[0], vContent.size(), " ", chars_one_line ) << endl; - it++; - } - cout << "tup attr end:\n" << endl; -} - -void TarsAnalyzer::dumpTup(const map > >& tupData) -{ - map > >::const_iterator it1 = tupData.begin(); - cout << "\ntup attr begin:" << endl; - while (it1 != tupData.end()) - { - cout << "tup Attribute name: " << it1->first << endl; - map >::const_iterator it2 = it1->second.begin(); - while(it2 != it1->second.end()) - { - - cout << "struct name: " << it2->first << endl; - vector vContent = it2->second; - - //int lines = vContent.size()/chars_one_line + 1; - //cout << "struct buff lines:"<< lines << endl; - cout << "struct buff: "; - cout << tars::TC_Common::bin2str(&vContent[0], vContent.size(), " ", chars_one_line ) << endl; - it2++; - } - - it1++; - } - cout << "tup attr end:\n" << endl; -} - diff --git a/cpp/test/testServant/testTarsAnalyzer/tarsAnalyzer.h b/cpp/test/testServant/testTarsAnalyzer/tarsAnalyzer.h deleted file mode 100644 index 5d0cb11a4..000000000 --- a/cpp/test/testServant/testTarsAnalyzer/tarsAnalyzer.h +++ /dev/null @@ -1,442 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef __TARS_ANALYZER_H_ -#define __TARS_ANALYZER_H_ - -#include "tarsparse/parse.h" -#include "util/tc_md5.h" -#include "util/tc_file.h" -#include "util/tc_common.h" -#include -#include "tup/Tars.h" - -using namespace tars; - -#define chars_one_line 16 -#define chars_buf_reserve 1 - - -#define CHAR_WIDTH_TAG 2 -#define CHAR_WIDTH_ID 12 -#define CHAR_WIDTH_TYPE 23 - -#ifndef TARS_MAX_STRING_LENGTH -#define TARS_MAX_STRING_LENGTH (100 * 1024 * 1024) -#endif - -class TarsAnalyzer -{ -public: - TarsAnalyzer() - { - _startPos = 0; - _itab = 0; - _buf_len = 0; - _buf = NULL; - _bTup = false; - _bIsDumpTup = false; - _tupVersion = 2; - _tup2_helper.clear(); - _tup3_helper.clear(); - } - /** - * 设置待解æžçš„结构体å - * @param name - */ - void setStructName(const string &name) - { - _StructName = name; - } - /** - *tup打包时put的属性å,eg.UniPacket<> pack; pack.put(name,xxx); - *@param name - */ - void setPutName(const string &name) - { - _putName = name; - } - /** - * 设置待解æžçš„æ•°æ®æµçš„文件å - *@param sFile - */ - void setStreamFileName(const string &sFile) - { - _streamFileName = sFile; - } - - void setStartPos(int p) - { - _startPos = p; - } - - bool isTup() - { - return _bTup; - } - void setTup(bool b) - { - _bTup = b; - } - - enum - { - eOK = 0, - eNeedBackDataHead = 1, - eNeedSkipTag = 2 - }; - -protected: - /** - * æ ¹æ®å‘½å空间获å–文件路径 - * @param ns 命å空间 - * - * @return string - */ - string getFilePath(const string &ns) const - { - return "/" + tars::TC_Common::replace(_packagePrefix, ".", "/") + "/" + ns + "/"; - } - - /** - * 生æˆæŸç±»åž‹çš„字符串æè¿°æºç  - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr &pPtr) ; - - /** - * 生æˆå†…建类型的字符串æè¿°æºç ,eg,int,short,float,double - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr &pPtr) ; - /** - * 生æˆvector的字符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr &pPtr) ; - - /** - * 生æˆmap的字符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr &pPtr, bool bNew = false) ; - - /** - * 生æˆæŸç§ç»“构的符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr &pPtr) const; - string tostrEnum(const EnumPtr &pPtr) ; - -protected: - /** - * 从编ç buffer中读å–struct类型 - * @param pPtr - * - * @return string - */ - - int readStruct(const StructPtr & pPtr, const NamespacePtr & nPtr) ; - - /** - * 解æžå字空间中相对应的struct - * @param pPtr - * - * @return string - */ - void analyzeNS(const NamespacePtr &pPtr) - { - vector& vStruct = pPtr->getAllStructPtr(); - size_t i; - for ( i= 0; i < vStruct.size(); i++) - { - if (vStruct[i]->getId() == _StructName || _bTup) - { - readStruct(vStruct[i], pPtr); - break; - } - } - if (i>= vStruct.size()) - { - cout<< "not find structName: " << _StructName << endl; - } - } - - /** - * 生æˆæ¯ä¸ªtars文件的java文件æºç  - * @param pPtr - * - * @return string - */ - void analyzeContext(const ContextPtr &pPtr) - { - vector namespaces = pPtr->getNamespaces(); - - for(size_t i = 0; i < namespaces.size(); i++) - { - analyzeNS(namespaces[i]); - } - } - -public: - - void skip(size_t len); - void skipToStructEnd(); - -void skipField(); - - -/// 跳过一个字段,ä¸åŒ…å«å¤´ä¿¡æ¯ -void skipField(uint8_t type); - void backToLast(); - void peekBuf(void * buf, size_t len, size_t offset = 0); - void readBuf(void * buf, size_t len); - template - T readByType(); - - void read(double& n, uint8_t expecttag, int& realType, bool isRequire); - - void read(Int32& n, uint8_t& tag, int& type, bool isRequire = true); - - void read(Int64& n, uint8_t& tag, int& type, bool isRequire = true); - - void read(string& s, uint8_t& tag, int& type, bool isRequire = true ); - - template - void read(std::map& m, uint8_t tag, bool isRequire = true); - - - - template - void read(std::vector& v, uint8_t& tag, int & realType, bool isRequire = true); - - int readBuiltin(const BuiltinPtr &pPtr, int tag, bool isRequire, TypeIdPtr pType) ; - - int readType(const TypePtr &pPtr, int tag, bool isRequire); - - int readVector(const VectorPtr &pPtr, int tag, bool isRequire, TypeIdPtr pType); - - int readMap(MapPtr pType, int tag, bool isRequire); - int readEnum(EnumPtr pPtr, int expectTag, bool isRequire); - void analyzeFile(const string &file, string streamFile); - - /** - *解æžtupæ•°æ®åŒ…中的sBufferæ•°æ® - */ - void analyzeWithTupBuffer(const string &file, const string& putName); - void checkTupBuffer(void); - int compareTag(int tag, int expectTag, bool bIsRequire = true); - - string getTab(); - string getSep(); - /** - * 增加tabæ•° - */ - void incTab() ; - - /** - * å‡å°‘tabæ•° - */ - void decTab() ; - - void addTab(int count) ; - void eraseTab(int count) ; - void setBuffer(const char * buf, size_t len) - { - _buf = buf; - _buf_len = len +chars_buf_reserve; //+1的目的是兼容没有结构结æŸæ ‡å¿—字节 - _cur = 0; - _last_pos =0; - } - /** - * 输出tup包的内容 - */ - void setDumpTup(bool isDump){_bIsDumpTup = isDump;} - void dumpTup(const map > >& tupData); - void dumpTup(const map >& tupData); - -private: - string _packagePrefix; - string _StructName; - string _putName; // tup put 接å£ä¼ äººçš„name - string _streamFileName; - - //起始分æžä½ç½® - int _startPos; - int _itab; - - const char * _buf; ///< 缓冲区 - size_t _buf_len; ///< 缓冲区长度 - - size_t _cur; ///< 当å‰ä½ç½® - - size_t _last_pos; ///< 当å‰ä½ç½® - size_t _tup_sbuffer_pos; - - //是å¦åœ¨è§£æžtupæ•°æ®åŒ… - bool _bTup; - bool _bIsDumpTup; - - //tup的版本å·ï¼Œ2或者3 - short _tupVersion; - - //tup 版本2对应的数æ®ç±»åž‹ - map > > _tup2_helper; - - //tup 版本3对应的数æ®ç±»åž‹ - map > _tup3_helper; - -}; - -class AnalyzerDataHead -{ - -public: - uint8_t _type; - uint8_t _tag; - enum - { - eChar = 0, - eShort = 1, - eInt32 = 2, - eInt64 = 3, - eFloat = 4, - eDouble = 5, - eString1 = 6, - eString4 = 7, - eMap = 8, - eList = 9, - eStructBegin = 10, - eStructEnd = 11, - eZeroTag = 12, - eSimpleList = 13, - }; - - static string tostrType(int type) - { - string s; - - switch(type) - { - case eChar: - s = " char "; - break; - case eShort: - s = " short "; - break; - case eInt32: - s = " int "; - break; - case eInt64: - s = " int64 "; - break; - case eFloat: - s = " float "; - break; - case eDouble: - s = " double "; - break; - case eString1: - s = " string1 "; - break; - case eString4: - s = " string4 "; - break; - case eMap: - s = " map "; - break; - case eList: - s = " vector "; - break; - case eStructBegin: - s = " struct beg "; - break; - case eStructEnd: - s = " struct end "; - break; - case eZeroTag: - s = " zero "; - break; - case eSimpleList: - s = " vector byte "; - break; - - default: - cout << "ERROR, tostrType failed!" << type << endl; - exit(-1); - break; - - } - return s; - - } - - struct helper - { - unsigned int type : 4; - unsigned int tag : 4; - }__attribute__((packed)); - -public: - AnalyzerDataHead() : _type(0), _tag(0) {} - AnalyzerDataHead(uint8_t type, uint8_t tag) : _type(type), _tag(tag) {} - - uint8_t getTag() const { return _tag;} - void setTag(uint8_t t) { _tag = t;} - uint8_t getType() const { return _type;} - void setType(uint8_t t) { _type = t;} - - /// 读å–æ•°æ®å¤´ä¿¡æ¯ - void readFrom(TarsAnalyzer& is) - { - size_t n = peekFrom(is); - is.skip(n); - } - - /// 读å–头信æ¯ï¼Œä½†ä¸å‰ç§»æµçš„åç§»é‡ - size_t peekFrom(TarsAnalyzer& is) - { - helper h; - size_t n = sizeof(h); - is.peekBuf(&h, sizeof(h)); - _type = h.type; - if (h.tag == 15) - { - is.peekBuf(&_tag, sizeof(_tag), sizeof(h)); - n += sizeof(_tag); - } - else - { - _tag = h.tag; - } - return n; - } - - - -}; - -#endif - diff --git a/cpp/test/testServant/testTarsAnalyzer/tupRequestF.h b/cpp/test/testServant/testTarsAnalyzer/tupRequestF.h deleted file mode 100644 index b1ac771af..000000000 --- a/cpp/test/testServant/testTarsAnalyzer/tupRequestF.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef _TUPREQUEST_H -#define _TUPREQUEST_H - -string g_sTupPacket = "module Ttars\n" -"{\n" -" struct RequestPacket\n" -" {\n" -" 1 require short iVersion;\n" -" 2 require byte cPacketType = 0;\n" -" 3 require int iMessageType = 0;\n" -" 4 require int iRequestId;\n" -" 5 require string sServantName = \"\";\n" -" 6 require string sFuncName = \"\";\n" -" 7 require vector sBuffer;\n" -" 8 require int iTimeout = 0;\n" -" 9 require map context;\n" -" 10 require map status; \n" -" };\n" - -" struct ResponsePacket\n" -" {\n" -" 1 require short iVersion;\n" -" 2 require byte cPacketType = 0;\n" -" 3 require int iRequestId;\n" -" 4 require int iMessageType = 0;\n" -" 5 require int iRet = 0;\n" -" 6 require vector sBuffer;\n" -" 7 require map status; \n" -" 8 optional string sResultDesc; \n" -" };\n" -"};\n" ; - - -#endif - diff --git a/cpp/test/testServant/testTup/CMakeLists.txt b/cpp/test/testServant/testTup/CMakeLists.txt deleted file mode 100644 index 7c7be941d..000000000 --- a/cpp/test/testServant/testTup/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) -set(TARGETNAME "test-tup") - -include_directories(${util_SOURCE_DIR}/include) -include_directories(${tools_SOURCE_DIR}) -include_directories(${servant_SOURCE_DIR}) - -link_libraries(tarsutil pthread) - -aux_source_directory(. DIR_SRCS) -add_executable(${TARGETNAME} ${DIR_SRCS}) - diff --git a/cpp/test/testServant/testTup/UserInfo.h b/cpp/test/testServant/testTup/UserInfo.h deleted file mode 100644 index 74b786e3f..000000000 --- a/cpp/test/testServant/testTup/UserInfo.h +++ /dev/null @@ -1,170 +0,0 @@ -// ********************************************************************** -// This file was generated by a TARS parser! -// TARS version 1.0.1. -// ********************************************************************** - -#ifndef __USERINFO_H_ -#define __USERINFO_H_ - -#include -#include -#include -#include "tup/Tars.h" -using namespace std; - - -namespace Test -{ - struct FriendInfo : public tars::TarsStructBase - { - public: - static string className() - { - return "Test.FriendInfo"; - } - static string MD5() - { - return "0ff2fc9fe39c884ba8445aa27b369e0c"; - } - FriendInfo() - :uin(0),nick("") - { - } - void resetDefautlt() - { - uin = 0; - nick = ""; - } - template - void writeTo(tars::TarsOutputStream& _os) const - { - _os.write(uin, 1); - _os.write(nick, 2); - if (birthday.size() > 0) - { - _os.write(birthday, 3); - } - } - template - void readFrom(tars::TarsInputStream& _is) - { - resetDefautlt(); - _is.read(uin, 1, true); - _is.read(nick, 2, true); - _is.read(birthday, 3, false); - } - ostream& display(ostream& _os, int _level=0) const - { - tars::TarsDisplayer _ds(_os, _level); - _ds.display(uin,"uin"); - _ds.display(nick,"nick"); - _ds.display(birthday,"birthday"); - return _os; - } - ostream& displaySimple(ostream& _os, int _level=0) const - { - tars::TarsDisplayer _ds(_os, _level); - _ds.displaySimple(uin, true); - _ds.displaySimple(nick, true); - _ds.displaySimple(birthday, false); - return _os; - } - public: - tars::Int32 uin; - std::string nick; - vector birthday; - }; - inline bool operator==(const FriendInfo&l, const FriendInfo&r) - { - return l.uin == r.uin && l.nick == r.nick && l.birthday == r.birthday; - } - inline bool operator!=(const FriendInfo&l, const FriendInfo&r) - { - return !(l == r); - } - - struct UserInfo : public tars::TarsStructBase - { - public: - static string className() - { - return "Test.UserInfo"; - } - static string MD5() - { - return "12ec3745dc4fc00ebfd8907b23628a11"; - } - UserInfo() - :qq(0),nick(""),city("gd") - { - } - void resetDefautlt() - { - qq = 0; - nick = ""; - city = "gd"; - } - template - void writeTo(tars::TarsOutputStream& _os) const - { - _os.write(qq, 1); - _os.write(nick, 2); - _os.write(birthday, 3); - if (city != "gd") - { - _os.write(city, 4); - } - _os.write(friends, 5); - } - template - void readFrom(tars::TarsInputStream& _is) - { - resetDefautlt(); - _is.read(qq, 1, true); - _is.read(nick, 2, true); - _is.read(birthday, 3, true); - _is.read(city, 4, false); - _is.read(friends, 5, true); - } - ostream& display(ostream& _os, int _level=0) const - { - tars::TarsDisplayer _ds(_os, _level); - _ds.display(qq,"qq"); - _ds.display(nick,"nick"); - _ds.display(birthday,"birthday"); - _ds.display(city,"city"); - _ds.display(friends,"friends"); - return _os; - } - ostream& displaySimple(ostream& _os, int _level=0) const - { - tars::TarsDisplayer _ds(_os, _level); - _ds.displaySimple(qq, true); - _ds.displaySimple(nick, true); - _ds.displaySimple(birthday, true); - _ds.displaySimple(city, true); - _ds.displaySimple(friends, false); - return _os; - } - public: - tars::Int32 qq; - std::string nick; - vector birthday; - std::string city; - map friends; - }; - inline bool operator==(const UserInfo&l, const UserInfo&r) - { - return l.qq == r.qq && l.nick == r.nick && l.birthday == r.birthday && l.city == r.city && l.friends == r.friends; - } - inline bool operator!=(const UserInfo&l, const UserInfo&r) - { - return !(l == r); - } - - -} - - - -#endif diff --git a/cpp/test/testServant/testTup/UserInfo.tars b/cpp/test/testServant/testTup/UserInfo.tars deleted file mode 100644 index 1bedb29b8..000000000 --- a/cpp/test/testServant/testTup/UserInfo.tars +++ /dev/null @@ -1,17 +0,0 @@ -module Test -{ - struct FriendInfo - { - 1 require int uin; - 2 require string nick; - 3 optional vector birthday; - }; - struct UserInfo - { - 1 require int qq; - 2 require string nick; - 3 require vector birthday; - 5 require map friends; - 4 optional string city = "gd"; - }; -}; diff --git a/cpp/test/testServant/testTup/main.cpp b/cpp/test/testServant/testTup/main.cpp deleted file mode 100644 index 546226b2c..000000000 --- a/cpp/test/testServant/testTup/main.cpp +++ /dev/null @@ -1,213 +0,0 @@ -#include -#include -#include -#include -#include "UserInfo.h" -#include "tup/tup.h" - -using namespace std; -using namespace tars; -using namespace tup; -using namespace Test; - -string str2hex(const string& sBuffer) -{ - string str = ""; - for (size_t i = 0; i < sBuffer.length(); ++i) { - char sb[4] = {0}; - snprintf(sb, 4, "%02x", (unsigned char)sBuffer[i]); - if (i%10==0)str.append("\n"); - str.append("(byte)0x"); - str.append(sb); - str.append(","); - } - return str; -} - -/*static char __data[] = { - 0x1c,0x2c,0x3c,0x40,0x03,0x56,0x00,0x66,0x09,0x6e,0x4c,0x6f,0x67,0x69,0x6e,0x6e,0x65,0x77,0x7d,0x00,0x00,0x39,0x08,0x00,0x02,0x06,0x03,0x70,0x77,0x64,0x18,0x00,0x01,0x06,0x06,0x73,0x74,0x72,0x69,0x6e,0x67,0x1d,0x00,0x00,0x0a,0x06,0x08,0x33,0x67,0x71,0x71,0x74,0x65,0x73,0x74,0x06,0x03,0x75,0x69,0x6e,0x18,0x00,0x01,0x06,0x05,0x69,0x6e,0x74,0x33,0x32,0x1d,0x00,0x00,0x05,0x02,0x3f,0xe2,0x78,0xac,0x8c,0x98,0x0c,0xa8,0x0c,}; -*/ - -int main() -{ - try - { - //////////////////////////////////////// - /* - string data(__data, sizeof(__data)); - UniRequest<> request; - request.decode(data.c_str(), data.length()); - return 0; - cout << "type:" << Class::name() << endl; - cout << "type:" << Class >::name() << endl; - cout << "type:" << Class > >::name() << endl; - cout << "type:" << Class > > >::name() << endl; - cout << "type:" << Class > >::name() << endl; - cout << "type:" << Class >::name() << endl; - */ - - string buff = ""; - //////////////////////////////////////// - UniPacket<> client; - - client.setRequestId(10000); - client.setServantName("do"); - client.setFuncName("test"); - - client.put("qq", 11688188); - client.put("nick", "zhangsan"); - - //struct - FriendInfo fi; - fi.nick = "zhangsan"; - fi.uin = 172917; - fi.birthday.push_back('5'); - fi.birthday.push_back('3'); - client.put("friendInfo", fi); - - //vector - vector vui; - UserInfo ui; - ui.qq = 123456; - ui.nick = "wawa"; - ui.birthday.push_back('2'); - ui.birthday.push_back('0'); - vui.push_back(ui); - ui.qq = 122400997; - ui.nick = "wawahaha"; - ui.birthday.push_back('1'); - ui.birthday.push_back('9'); - ui.city = "bj"; - vui.push_back(ui); - client.put >("userInfo", vui); - - //map - map mfi; - FriendInfo ffi; - ffi.uin = 10001; - ffi.nick = "friend1"; - ffi.birthday.push_back('9'); - ffi.birthday.push_back('8'); - mfi[10001] = ffi; - ffi.uin = 10002; - ffi.nick = "friend2"; - ffi.birthday.clear(); - ffi.birthday.push_back('9'); - ffi.birthday.push_back('7'); - mfi[10002] = ffi; - client.put >("friendInfo", mfi); - - //crypt-attr - UniAttribute<> attr; - attr.put("attr-qq", 172917); - attr.put("attr-nick", "sanzhang"); - vector vfi; - - attr.put >("attr-friends", vfi); - - vector crpyBuffer; - attr.encode(crpyBuffer); - //do encrypt... - client.put("crypt-attr", crpyBuffer); - - vector vc; - vc.push_back('a'); - vc.push_back('b'); - vc.push_back('c'); - vc.push_back('d'); - vc.push_back('e'); - client.put >("vc", vc); - - //å®¢æˆ·ç«¯ç¼–ç  - client.encode(buff); - - //////////////////////////////////////// - //网络传输... - /* - cout << str2hex(buff) << endl; - output.close(); - return 0; - */ - //////////////////////////////////////// - - cout << "---------------------------------" << endl; - - //æœåŠ¡ç«¯è§£ç  - UniPacket<> server; - server.decode(buff.c_str(), buff.length()); - - cout << "[requestId]:" << server.getRequestId() << endl; - cout << "[servantName]:" << server.getServantName() << endl; - cout << "[funcName]:" << server.getFuncName() << endl; - cout << "[qq]:" << server.get("qq") << endl; - cout << "[nick]:" << server.get("nick") << endl; - - //vector - vector vinfo; - server.get("userInfo", vinfo); - - for (size_t j = 0; j < vinfo.size(); ++j) - { - UserInfo info = vinfo[j]; - cout << "[userInfo]:" << j << endl; - cout << ":" << info.qq << endl; - cout << ":" << info.nick << endl; - cout << ":"; - for (size_t i = 0; i < info.birthday.size(); ++i) - { - cout << info.birthday[i] << ","; - } - cout << endl; - cout << ":" << info.city << endl; - } - - cout << "---------------------------------" << endl; - //map - map minfo; - server.get("friendInfo", minfo); - - for (map::iterator it = minfo.begin(); it != minfo.end(); ++it) - { - cout << "[friendInfo]:" << it->first << endl; - cout << ":" << it->second.uin << endl; - cout << ":" << it->second.nick << endl; - cout << ":"; - for (size_t i = 0; i < it->second.birthday.size(); ++i) - { - cout << it->second.birthday[i] << ","; - } - cout << endl; - } - - cout << "---------------------------------" << endl; - //crypt-attr - UniAttribute<> cryptAttr; - vector cryptAttrBuffer; - server.get("crypt-attr", cryptAttrBuffer); - - //do decrypt... - cryptAttr.decode(cryptAttrBuffer); - cout << "[attr-qq]:" << cryptAttr.get("attr-qq") << endl; - cout << "[attr-nick]:" << cryptAttr.get("attr-nick") << endl; - vector sfi; - cryptAttr.get("attr-friends", sfi); - cout << "[attr-friends]:" << sfi.size() << endl; - for (size_t i = 0; i < sfi.size(); ++i) - { - cout << ":" << sfi[i].uin << endl; - cout << ":" << sfi[i].nick << endl; - } - cout << "---------------------------------" << endl; - //////////////////////////////////////// - cout << "{default}:" << server.getByDefault("default", "kaka") << endl; - } - catch (exception& e) - { - cout << "excep:" << e.what() << endl; - } - catch (...) - { - cout << "excep." << endl; - } - return 0; -} diff --git a/cpp/test/testUtil/CMakeLists.txt b/cpp/test/testUtil/CMakeLists.txt deleted file mode 100644 index b63562b0c..000000000 --- a/cpp/test/testUtil/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -include_directories(${util_SOURCE_DIR}/include) -include_directories(${MYSQL_DIR_INC}) - -link_libraries(tarsutil) -link_libraries(${MYSQL_DIR_LIB}/libmysqlclient.a) -link_libraries(pthread z dl rt) - -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) - -FILE(GLOB_RECURSE SRC_LIST "*.cpp") - -foreach(FILE ${SRC_LIST}) - get_filename_component(TARGETNAME ${FILE} NAME_WE) - add_executable(${TARGETNAME} ${FILE}) - -endforeach(FILE) - - diff --git a/cpp/test/testUtil/example_tc_autoptr.cpp b/cpp/test/testUtil/example_tc_autoptr.cpp deleted file mode 100644 index cb54e257d..000000000 --- a/cpp/test/testUtil/example_tc_autoptr.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_autoptr.h" -#include "util/tc_thread_pool.h" -#include -#include -#include - -using namespace std; -using namespace tars; - -void func(int &i) -{ -} - -class TestPointer : public TC_HandleBase -{ -public: - void func(int &i) - { - int n = 10000; - while(n) - { - i++; - n--; - } - } -}; - -void test() -{ - int i = 0; - - typedef TC_AutoPtr TestPointerPtr; - vector vtp; - for(size_t j = 0; j < 10; j++) - { - vtp.push_back(new TestPointer()); - } - - cout << i << endl; - for(size_t j = 0; j < 10; j++) - { - vtp[j]->func(i); - } - - cout << i << endl; - vector vtp1 = vtp; - for(size_t j = 0; j < 10; j++) - { - vtp[j]->func(i); - } - - cout << i << endl; -} - -TC_Atomic a; - -void testAdd() -{ - int i = 10000000; - while(i--) - { - a.inc(); - } -} - -void testDel() -{ - int i = 10000000; - while(i--) - { - a.dec(); - } -} - -int main(int argc, char *argv[]) -{ - try - { - cout << a.get() << endl; - - TC_ThreadPool tpoolA; - tpoolA.init(10); - tpoolA.start(); - - TC_ThreadPool tpoolB; - tpoolB.init(10); - tpoolB.start(); - - { - for(size_t i = 0; i < tpoolA.getThreadNum(); i++) - { - tpoolA.exec(testAdd); - } - } - - { - for(size_t i = 0; i < tpoolB.getThreadNum(); i++) - { - tpoolB.exec(testDel); - } - } - - tpoolA.waitForAllDone(); - tpoolB.waitForAllDone(); - - cout << a.get() << endl; - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_bitmap.cpp b/cpp/test/testUtil/example_tc_bitmap.cpp deleted file mode 100644 index 5c8bd6d20..000000000 --- a/cpp/test/testUtil/example_tc_bitmap.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_shm.h" -#include "util/tc_bitmap.h" -#include "util/tc_option.h" -#include "util/tc_common.h" - -#include -#include - -using namespace tars; - -TC_Shm g_shm; -TC_BitMap g_bmap; - -void initMap() -{ - unsigned iBit = 3; - size_t iMemSize = TC_BitMap::calcMemSize(100, iBit); - g_shm.init(iMemSize, 9999); - - if (g_shm.iscreate()) - { - g_bmap.create(g_shm.getPointer(), g_shm.size(), iBit); - } - else - { - g_bmap.connect(g_shm.getPointer(), g_shm.size(), iBit); - } - - cout << "init mem:" << iMemSize << endl; -} - -void usage(char *argv[]) -{ - cout << argv[0] << " --get=uin --bit" << endl; - cout << argv[0] << " --set=uin --bit" << endl; - cout << argv[0] << " --clear=uin --bit" << endl; - cout << argv[0] << " --clear4all --bit" << endl; - cout << argv[0] << " --dump=file" << endl; - cout << argv[0] << " --load=file" << endl; - cout << argv[0] << " --help" << endl; -} - -int main(int argc, char *argv[]) -{ - try - { - TC_Option op; - op.decode(argc, argv); - - initMap(); - - if(op.hasParam("get")) - { - size_t uin = TC_Common::strto(op.getValue("get")); - unsigned bit = TC_Common::strto(op.getValue("bit")); - cout << "get:" << uin << "=" << g_bmap.get(uin, bit) << endl; - } - else if(op.hasParam("set")) - { - size_t uin = TC_Common::strto(op.getValue("set")); - unsigned bit = TC_Common::strto(op.getValue("bit")); - cout << "set:" << uin << "=" << g_bmap.set(uin, bit) << endl; - } - else if(op.hasParam("clear")) - { - size_t uin = TC_Common::strto(op.getValue("clear")); - unsigned bit = TC_Common::strto(op.getValue("bit")); - cout << "clear:" << uin << "=" << g_bmap.clear(uin, bit) << endl; - } - else if(op.hasParam("clear4all")) - { - int bit = TC_Common::strto(op.getValue("bit")); - cout << "clear4all:" << bit << "=" << g_bmap.clear4all((unsigned)bit) << endl; - } - else if(op.hasParam("dump")) - { - string file = op.getValue("dump"); - int ret = g_bmap.dump2file(file); - if(ret == 0) - cout << "dump2file:" << file << " ok" << endl; - else - cout << "dump2file:" << file << " err:" << ret << endl; - } - else if(op.hasParam("load")) - { - string file = op.getValue("load"); - int ret = g_bmap.load5file(file); - if(ret == 0) - cout << "load5file:" << file << " ok" << endl; - else - cout << "load5file:" << file << " err:" << ret << endl; - } - else - { - usage(argv); - } - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_cgi.cpp b/cpp/test/testUtil/example_tc_cgi.cpp deleted file mode 100644 index e1233534c..000000000 --- a/cpp/test/testUtil/example_tc_cgi.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_cgi.h" -#include "util/tc_common.h" - -using namespace tars; - -void testStdin() -{ - string sFileName = "/data/tars/test/util/" + TC_Common::now2str() + ".tmp"; - - TC_Cgi cgi; - cout << TC_Cgi::htmlHeader("text/html"); - - cgi.setUpload(sFileName, 2, 1024*1024*10); - cgi.parseCgi(); - - cout << cgi.setCookie(cgi.getValue("cookie1"), cgi.getValue("cookie2"), TC_Common::now2str()); - cout << "value:" << cgi.getValue("value") << endl; - - string s = "abc 中文"; - cout << TC_Cgi::encodeURL(s) << "
"; - cout << TC_Cgi::decodeURL(s) << "
";; - - cout << TC_Cgi::encodeHTML(cgi.getValue("D1"), true) ; - cout << TC_Common::tostr(cgi.getParamMap()) << "
"; - cout << "
"; - cout << TC_Common::tostr(cgi.getEnvMap()) << "
"; -} - -void parseNormal(multimap &mmpParams, const string& sBuffer) -{ - int iFlag = 0; - string sName; - string sValue; - string sTmp; - string::size_type len = sBuffer.length(); - string::size_type pos = 0; - - while (pos < len) - { - sTmp = ""; - - if(iFlag == 0) - { - while ( (sBuffer[pos] != '=') && (pos < len) ) - { - sTmp += (sBuffer[pos] == '+') ? ' ' : sBuffer[pos]; - - ++pos; - } - } - else - { - while ( (sBuffer[pos] != '&') && (pos < len) ) - { - sTmp += (sBuffer[pos] == '+') ? ' ' : sBuffer[pos]; - - ++pos; - } - } - - if (iFlag == 0) //param name - { - sName = TC_Cgi::decodeURL(sTmp); - - if ( (sBuffer[pos] != '=') || (pos == len - 1) ) - { - sValue = ""; - - mmpParams.insert(multimap::value_type(sName, sValue)); - } - else - { - iFlag = 1; - } - } - else - { - sValue = TC_Cgi::decodeURL(sTmp); - - mmpParams.insert(multimap::value_type(sName, sValue)); - - iFlag = 0; - } - - ++pos; - } -} - -void testCgiParam(const string &buffer) -{ - - - multimap m; - - parseNormal(m, buffer); - - cout << TC_Common::tostr(m) << endl; -} - -int main(int argc, char *argv[]) -{ - try - { -// testStdin(); -// testCgiParam("a=b&c=d&e=f="); -// testCgiParam("abc&=b&c=d&e=f="); -// testCgiParam("a++&=b&c=d&e=f="); -// testCgiParam("=b&c=d===&e=f="); -// testCgiParam("=b&c=d++=&e=f="); - - testCgiParam("db=sina&bid=161423,230233,235253&cid=0,0,0&sid=224587&advid=1293&camid=25420&show=ignore&url=http://bbs.fangyou.com/viewthread.php?tid=1021196&pi=sina-langshou-wzl1"); - - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} diff --git a/cpp/test/testUtil/example_tc_common.cpp b/cpp/test/testUtil/example_tc_common.cpp deleted file mode 100644 index 133f72462..000000000 --- a/cpp/test/testUtil/example_tc_common.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "util/tc_common.h" - -#include -#include -using namespace tars; - - - -string tm2GMTstr(const time_t &t) -{ - struct tm tt; - gmtime_r(&t, &tt); - return TC_Common::tm2str(tt, "%a, %d %b %Y %H:%M:%S GMT"); -} - -extern char *tzname[2]; -extern long timezone; - -struct CmpCase -{ - bool operator()(const string &s1, const string &s2) - { - return TC_Common::upper(s1) < TC_Common::upper(s2); - } -}; - -int main(int argc, char *argv[]) -{ - try - { - map m; - m["abc"] = "def"; - m["Abc"] = "def"; - m["AbC"] = "def"; - m["AbCd"] = "def"; - m["AbCD"] = "def"; - - cout << TC_Common::tostr(m) << endl; - cout << m["ABC"] << endl; - cout << m["ABCD"] << endl; - - - /* - { - string n = "abc"; - vector s; - s.assign(n.c_str(), n.c_str() + n.length()); - cout << s.size() << ":" << s.capacity() << endl; - s.insert(s.end(), n.c_str(), n.c_str() + n.length()); - cout << s.size() << ":" << s.capacity() << endl; - s.insert(s.end(), n.c_str(), n.c_str() + n.length()); - cout << s.size() << ":" << s.capacity() << endl; - s.clear(); - cout << s.size() << ":" << s.capacity() << endl; - s.reserve(100); - memcpy((&s[0] + s.size()), "dfetdg", 6); - cout << TC_Common::tostr(s) << endl; - - s.resize(s.size() + 10); - cout << s.size() << ":" << s.capacity() << endl; - cout << TC_Common::tostr(s) << endl; - } - return 0; - - { - string s; - s = "abc"; - cout << s.length() << ":" << s.capacity() << endl; - s += "abcd"; - cout << s.length() << ":" << s.capacity() << endl; - s += "abcd"; - cout << s.length() << ":" << s.capacity() << endl; -// s.clear(); -// s = s.substr(5); - s.erase(s.begin(), s.begin() + min((size_t)100, s.size())); - cout << s.size() << ":" << s.capacity() << endl; - s.reserve(100); - cout << s.length() << ":" << s.capacity() << endl; - } - return 0; - - time_t tt = time(NULL); - string ss = tm2GMTstr(tt); - cout << ss << endl; - cout << tt << endl; - -// Sat, 02 Feb 2010 08:57:34 GMT -// Fri, 30 Oct 1998 13:19:41 GMT - -// tzset(); - struct tm stTm; - int ret = TC_Common::strgmt2tm(ss, stTm); - - cout << stTm.tm_sec << endl; - cout << stTm.tm_min << endl; - cout << stTm.tm_hour << endl; - cout << stTm.tm_mday << endl; - cout << stTm.tm_mon << endl; - cout << stTm.tm_year << endl; - cout << timezone << endl; - cout << mktime(&stTm) - timezone << endl; - - cout << "***" << endl; - cout << stTm.tm_zone << endl; - cout << "***" << endl; - cout << tzname[0] << ":" << tzname[1] << endl; - cout << endl; - cout << mktime(&stTm) - tt << ":" << stTm.tm_year << endl; - cout << ret << endl; - return 0; - - vector v = TC_Common::sepstr("|a|b||c|", "|", true); - cout << TC_Common::tostr(v.begin(), v.end(), "|") << endl; - - vector v2 = TC_Common::sepstr("|a|b||c|", "|", false); - cout << TC_Common::tostr(v2.begin(), v2.end(), "|") << endl; - - vector v1 = TC_Common::sepstr("|3|4||2|", "|", true); - cout << TC_Common::tostr(v1) << endl; - - map m; - m["abc"] = ""; - m["abc1"] = "ef1"; - m["abc2"] = "ef2"; - cout << TC_Common::tostr(m) << endl; - - v = TC_Common::sepstr("|a|b||c|", "|", true); - cout << TC_Common::tostr(v) << endl; - - return 0; - - struct timeval t; - gettimeofday(&t, NULL); - - cout << TC_Common::tm2str(t.tv_sec, "%Y-%m-%d %H:%M:%S.") << outfill(TC_Common::tostr(t.tv_usec/1000), '0', 3) << "|"; - - string s = "asdfoasdfijfa"; - cout << s << endl; - string s1 = TC_Common::bin2str(s); - cout << s1 << endl; - string s2 = TC_Common::bin2str(s, " ", 4); - cout << s2 << endl; - string s3 = TC_Common::str2bin(s1); - cout << s3 << endl; - string s4 = TC_Common::str2bin(s2, " ", 4); - cout << s4 << endl; - - cout << "matchPeriod :" << TC_Common::matchPeriod("127.0.0.1", "127.*.*.*") << endl; - - cout << "[" << TC_Common::trim("abc\n\t") << "]" << endl; - cout << "[" << TC_Common::trimleft("abc\n\t") << "]" << endl; - cout << "[" << TC_Common::trim("\r\nabc") << "]" << endl; - cout << "[" << TC_Common::trim("\r\nabc\r\n") << "]" << endl; - cout << "[" << TC_Common::trim(" abc ", " \r\t") << "]" << endl; - cout << "[" << TC_Common::trim("abcdefab", "ab", false) << "]" << endl; - - cout << TC_Common::toSize("3K", 3000) << endl; - cout << TC_Common::toSize("23M", 3000) << endl; - cout << TC_Common::toSize("15G", 3000) << endl; - cout << TC_Common::toSize("15", 3000) << endl; - cout << TC_Common::toSize("M", 3000) << endl; - cout << TC_Common::toSize("34.32M", 3000) << endl; - cout << TC_Common::toSize("3.2G", 3000) << endl; - cout << TC_Common::toSize("3r,.2G", 3000) << endl; - */ - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_config.cpp b/cpp/test/testUtil/example_tc_config.cpp deleted file mode 100644 index e3bd174e4..000000000 --- a/cpp/test/testUtil/example_tc_config.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_config.h" -#include "util/tc_file.h" -#include -#include -using namespace tars; - - -int main(int argc, char *argv[]) -{ - try - { - TC_Config conf; - conf.parseFile("./template.config.conf"); - - cout << "parseFile************************************" << endl; - cout << conf.tostr() << endl; - - vector n = conf.getDomainVector("/"); - cout << TC_Common::tostr(n) << endl; - - return 0; - cout << "parseFile************************************" << endl; - TC_Config conft = conf; - cout << conft.tostr() << endl; - - cout << conf["/tars/application/client"] << endl; - map m1 = conf.getDomainMap("/tars/allow"); - cout << TC_Common::tostr(m1) << endl; - cout << conf["/tars/allow"] << endl; - - cout << "parseString************************************" << endl; - TC_File tf; - conf.parseString(tf.load2str("./template.config.conf")); - cout << conf.tostr() << endl; - cout << conf["/tars/application/client"] << endl; - - cout << "insertDomain create false************************************" << endl; - - conf.insertDomain("/tars/insert", "insert", false); - cout << conf.tostr() << endl; - conf.parseString(conf.tostr()); - cout << conf.tostr() << endl; - - cout << "insertDomain create true************************************" << endl; - - conf.insertDomain("/tars/insert", "insert", true); - cout << conf.tostr() << endl; - conf.parseString(conf.tostr()); - cout << conf.tostr() << endl; - - cout << "insertDomainParam************************************" << endl; - - map m; - m["abc"] = "def"; - m["ttt"] = "yyy"; - conf.insertDomainParam("/tars/insert/insert1", m, false); - cout << conf.tostr() << endl; - conf.insertDomainParam("/tars/insert/insert1", m, true); - cout << conf.tostr() << endl; - - cout << "joinConfig true************************************" << endl; - TC_Config conf1; - conf1.parseFile("./log.config.conf"); - - conf1.joinConfig(conf, true); - cout << conf1.tostr() << endl; - - cout << "joinConfig false************************************" << endl; - TC_Config conf2; - conf2.parseFile("./log.config.conf"); - conf2.joinConfig(conf, false); - cout << conf2.tostr() << endl; - - TC_Config conf3 = conf2; - cout << conf3.tostr() << endl; - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_encoder.cpp b/cpp/test/testUtil/example_tc_encoder.cpp deleted file mode 100644 index 739a467a2..000000000 --- a/cpp/test/testUtil/example_tc_encoder.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_encoder.h" -#include "util/tc_file.h" -#include "util/tc_http.h" -#include -#include - -using namespace tars; - -bool isUTF8(const string &buffer) -{ - if(buffer.length() <= strlen("charset=utf")) - { - return false; - } - - size_t len = strlen("charset"); - - for(size_t i = 0; i < buffer.length() - len; i++) - { - if(strncasecmp(buffer.c_str() + i, "charset", len) != 0) - continue; - - if(i > 0 && buffer[i-1] != ' ' - && buffer[i-1] != '\t' - && buffer[i-1] != ';' - && buffer[i-1] != '\"' - && buffer[i-1] != '\'') - continue; - - int flag = 0; - for(size_t j = i + len; j < buffer.length(); j++) - { - if(flag == 0) - { - if(buffer[j] == ' ' || buffer[j] == '\t') - continue; - - if(buffer[j] == '=') - { - flag = 1; - continue; - } - - break; - } - else if(flag == 1) - { - if(buffer[j] == ' ' || buffer[j] == '\t') - continue; - - string tmp = TC_Common::trimleft(buffer.c_str() + j, "\" \t'"); - - if(strncmp(tmp.c_str(), "utf", 3) == 0) - return true; - - return false; - } - else - { - break; - } - } - } - - return false; -} - - -int main(int argc, char *argv[]) -{ - try - { - /* - string s = "我们"; -// string v = gbk2utf8(s); -// cout << s << ":" << v << TC_Encoder::utf82gbk(v) << endl; - - string n = TC_File::load2str("./content.html"); - cout << n << endl; -*/ - string s = ""; - cout << isUTF8(s) << endl; - -/* - TC_HttpRequest stHttpReq; - stHttpReq.setCacheControl("no-cache"); - stHttpReq.setUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1; .NET CLR 1.1.4322)"); - stHttpReq.setGetRequest("http://cn.engadget.com/"); - string sSendBuffer = stHttpReq.encode(); - - TC_HttpResponse stHttpRsp; - int iRet = stHttpReq.doRequest(stHttpRsp, 60000); - if(iRet != 0) - { - return 0; - } - string sRspHeader = stHttpRsp.encode(); - sRspHeader = sRspHeader.substr(0, sRspHeader.find("\r\n\r\n")); - cout << "[preview] http response:" << sRspHeader << endl; - - string content = stHttpRsp.getContent(); - - string contentType = TC_Common::lower(stHttpRsp.getContentType()); - - cout << "[preview] contentType:" << contentType << endl; - - string tmp = TC_Common::lower(content).substr(0, 512); - cout << "[preview] tmp:" << tmp << endl; - - if(contentType.find("utf8") == string::npos - && contentType.find("utf-8") == string::npos - && !isUTF8(tmp)) - { - cout << "[preview] gbk to utf8" << endl; -// content = gbk2utf8(content); - } - - cout << isUTF8(tmp) << endl; -*/ - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_file.cpp b/cpp/test/testUtil/example_tc_file.cpp deleted file mode 100644 index 867e6d81c..000000000 --- a/cpp/test/testUtil/example_tc_file.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_file.h" -#include -#include -#include - -using namespace tars; - -/* -int removeFile(const string &sFullFileName, bool bRecursive) -{ - cout << sFullFileName << endl; - string path = TC_File::simplifyDirectory(sFullFileName); - - struct stat f_stat; - - if (lstat(path.c_str(), &f_stat) == -1) - { - return -1; - } - - if(S_ISDIR(f_stat.st_mode)) - { - if(bRecursive) - { - vector files; - TC_File::listDirectory(path, files, true, false); - for(size_t i = 0; i < files.size(); i++) - { - removeFile(files[i], bRecursive); - } - - if(path != "/") - { - if(::rmdir(path.c_str()) == -1) - { - return -1; - } - return 0; - } - } - else - { - if(::rmdir(path.c_str()) == -1) - { - return -1; - } - } - } - else - { - if(::remove(path.c_str()) == -1) - { - return -1; - } - } - - return 0; -} -*/ - -struct Out -{ - void operator()(const string &n) - { - cout << n << endl; - } -}; - -int main(int argc, char *argv[]) -{ - try - { -// cout << TC_File::getFileSize("./test_tc_file.cpp") << endl; -// cout << TC_File::isFileExist("./test_tc_file.cpp", S_IFDIR) << endl; -// cout << TC_File::makeDir("test") << endl; - cout << TC_File::simplifyDirectory("/.") << endl; - cout << TC_File::simplifyDirectory("/./ab/tt//t///t//../tt/") << endl; - TC_File::removeFile("./", true); - vector v; - TC_File::listDirectory("/home/base.l", v, true); - for_each(v.begin(), v.end(), Out()); - -// TC_File::removeFile("/home/base.l", false); - - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_file_lock.cpp b/cpp/test/testUtil/example_tc_file_lock.cpp deleted file mode 100644 index 8ee0160b9..000000000 --- a/cpp/test/testUtil/example_tc_file_lock.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_file_mutex.h" -#include -#include - -using namespace tars; - -void ThreadEntry1(TC_FileMutex *fl) -{ - int i = 1; - while(i == 0) - { - fl->rlock(); - cout << i << ":" << pthread_self() << " : 1234567890abcdefghijklmnopqrstuvwxyz" << endl; - sleep(1); - cout << i << ":" << pthread_self() << " : 1234567890abcdefghijklmnopqrstuvwxyz" << endl; -// fl->unlock(); - sleep(1); - i++; - } -} - -void ThreadEntry2(TC_FileMutex *fl) -{ - int i = 1; - while(i == 0) - { - fl->wlock(); - cout << i << ":" << pthread_self() << " : 1234567890abcdefghijklmnopqrstuvwxyz" << endl; - sleep(1); - cout << i << ":" << pthread_self() << " : 1234567890abcdefghijklmnopqrstuvwxyz" << endl; -// fl->unlock(); - sleep(1); - i++; - } -} - - -int main(int argc, char *argv[]) -{ - try - { - TC_FileMutex sl; - sl.init("./test.lock"); - - pthread_t itid1; - pthread_t itid2; - - pthread_create(&itid1, NULL, (void *(*)(void *))&ThreadEntry1, (void*)&sl); - pthread_create(&itid2, NULL, (void *(*)(void *))&ThreadEntry2, (void*)&sl); - - pthread_join(itid1, NULL); - pthread_join(itid2, NULL); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_gzip.cpp b/cpp/test/testUtil/example_tc_gzip.cpp deleted file mode 100644 index 64a88ee2e..000000000 --- a/cpp/test/testUtil/example_tc_gzip.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_file.h" -#include "util/tc_gzip.h" -#include "util/tc_common.h" - -using namespace tars; - -int main(int argc, char *argv[]) -{ - try - { - vector file; - TC_File::load2str("./example_tc_gzip.cpp", file); - - { - vector v; - tars::TC_GZip::compress(&file[0], file.size(), v); - - cout << "compress ok, size:" << v.size() << endl; - - vector s1; - tars::TC_GZip::uncompress(&v[0], v.size(), s1); - - assert(file == s1); - } - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_hashmap.cpp b/cpp/test/testUtil/example_tc_hashmap.cpp deleted file mode 100644 index 1593e1f85..000000000 --- a/cpp/test/testUtil/example_tc_hashmap.cpp +++ /dev/null @@ -1,402 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_hashmap.h" -#include "util/tc_common.h" -#include "util/tc_shm.h" -#include "util/tc_option.h" -#include -#include - -using namespace tars; - -//////////////////////////////////////////////////////////////////// -// - -TC_Shm g_shm; -TC_HashMap g_hmap; - -vector > prepare() -{ - cout << "start pareparing data" << endl; - - size_t count = 10000; - vector > v; - - vector vs; - vs.push_back("a"); - vs.push_back("ab"); - vs.push_back("abc"); - vs.push_back("abcd"); - vs.push_back("abcde"); - vs.push_back("abcdef"); - vs.push_back("abcdefg"); - vs.push_back("abcdefgh"); - vs.push_back("abcdefghi"); - vs.push_back("abcdefghij"); - vs.push_back("abcdefghijk"); - vs.push_back("abcdefghijkl"); - vs.push_back("abcdefghijklm"); - - srand(time(NULL)); - - for(size_t i = 0; i < count; i++) - { - pair data; - - ostringstream s; - s << i << "_" << rand(); - data.first = s.str() + "_" + vs[rand() % vs.size()]; - data.second = s.str() + "_" + vs[rand() % vs.size()]; - - v.push_back(data); - } - - cout << "pareparing data OK:" << v.size() << endl; - return v; -} - -void initMem() -{ - size_t iMemSize = 1024*10; - g_shm.init(iMemSize, 8888); -} - -void initMap() -{ - if (g_shm.iscreate()) - { - size_t iAvgSize = 10; - g_hmap.initDataBlockSize(iAvgSize, iAvgSize, 1.0); - g_hmap.create(g_shm.getPointer(), g_shm.size()); - g_hmap.setEraseCount(3); - } - else - { - g_hmap.connect(g_shm.getPointer(), g_shm.size()); - } - - cout << "init mem" << endl; -} - -void testSendRand() -{ - vector > v = prepare(); - vector vRecords; - - for(int i = 0; i < 10000; i++) - { - size_t index = rand() % v.size(); - - ostringstream s; - s << i ; - - v[index].first = s.str(); - v[index].second = s.str(); - - cout << "set:" << v[index].first << ":" << v[index].second << ":" << g_hmap.size() << ":" << g_hmap.dirtyCount() << endl; - - int ret = g_hmap.set(v[index].first, v[index].second, true, vRecords); - assert(ret == 0); - - } - - cout << g_hmap.size() << "|" << g_hmap.dirtyCount() << endl; -} - -void testGetRand() -{ - vector > v = prepare(); - - for(int i = 0; i < 200000; i++) - { - size_t index = rand() % v.size(); - time_t iSyncTime; - int ret = g_hmap.get(v[index].first, v[index].second, iSyncTime); - assert(ret == 0); - cout << "get:" << v[index].first << ":" << v[index].second << ":" << g_hmap.size() << ":" << g_hmap.dirtyCount() << endl; - } - - cout << g_hmap.size() << "|" << g_hmap.dirtyCount() << endl; -} - -void usage(char *argv) -{ - cout << argv << " --help" << endl; - cout << "\t --release" << endl; - cout << "\t --view" << endl; - cout << "\t --sync [--count=]" << endl; - cout << "\t --set-rand" << endl; - cout << "\t --set-tars [--key=] [--value=]" << endl; - cout << "\t --del [--key=]" << endl; - cout << "\t --get [--key=]" << endl; - cout << "\t --get-rand" << endl; - cout << "\t --erase [--key=]" << endl; - cout << "\t --isdirty [--key=]" << endl; - cout << "\t --find [--key=]" << endl; - cout << "\t --listh [--asc]" << endl; - cout << "\t --listt [--asc]" << endl; - cout << "\t --listg [--asc]" << endl; - cout << "\t --listd" << endl; -} - -int main(int argc, char *argv[]) -{ - { - TC_Option option; - option.decode(argc, argv); - - map m = option.getMulti(); - - if(option.hasParam("help") || option.getMulti().empty()) - { - usage(argv[0]); - return 0; - } - - initMem(); - - if(option.hasParam("release")) - { - g_shm.del(); - return 0; - } - - initMap(); - - /* - fork(); - fork(); - fork(); - */ - - if(option.hasParam("set-rand")) - { - testSendRand(); - } - else if(option.hasParam("set-tars")) - { - vector vRecords; - string k; - string v; - k = option.getValue("key"); - v = option.getValue("value"); - int ret = g_hmap.set(k, v, true, vRecords); - assert(ret == 0); - cout << "set-tars:" << k << ":" << v << " ret=" << ret << endl; - for(size_t i = 0; i < vRecords.size(); i++) - { - cout << "set-tars erase:" << vRecords[i]._key << ":" << vRecords[i]._value << endl; - } - } - else if(option.hasParam("get-rand")) - { - testGetRand(); - } - else if(option.hasParam("del")) - { - TC_HashMap::BlockData stRecord; - string k; - k = option.getValue("key"); - int ret = g_hmap.del(k, stRecord); - cout << "del:" << k << " ret=" << ret << endl; - cout << "del:" << stRecord._key << endl; - } - else if(option.hasParam("get")) - { - string k; - string v; - k = option.getValue("key"); - time_t iSyncTime; - int ret = g_hmap.get(k, v, iSyncTime); - cout << "get:" << k << ":" << v << " ret=" << ret << endl; - } - else if(option.hasParam("erase")) - { - int ratio = TC_Common::strto(option.getValue("ratio")); - TC_HashMap::BlockData data; - int ret = TC_HashMap::RT_OK; - do - { - ret = g_hmap.erase(ratio, data, true); - cout << "erase: " << data._key << "|" << data._value << "|ret:" << ret << endl; - }while(/*ret == TC_HashMap::RT_ERASE_OK*/false); - } - else if(option.hasParam("set-dirty")) - { - string k = option.getValue("key"); - int ret = g_hmap.setDirty(k); - cout << "set-dirty: " << k << "|" << ret << endl; - } - else if(option.hasParam("set-clean")) - { - string k = option.getValue("key"); - int ret = g_hmap.setClean(k); - cout << "set-clean: " << k << "|" << ret << endl; - } - else if(option.hasParam("isdirty")) - { - string k; - k = option.getValue("key"); - int ret = g_hmap.checkDirty(k); - cout << "isdirty:" << k << " ret=" << ret << endl; - } - else if(option.hasParam("find")) - { - string k; - k = option.getValue("key"); - TC_HashMap::lock_iterator it = g_hmap.find(k); - if(it != g_hmap.end()) - { - string v; - int ret = it->get(k, v); - if(ret != TC_HashMap::RT_OK) - { - cout << "get error: ret=" << ret << endl; - } - else - { - cout << "find ok:" << k << " value=" << v << endl; - } - } - else - { - cout << "find none:" << k << endl; - } - } - else if(option.hasParam("sync")) - { - TC_HashMap::BlockData data; -// size_t n = atoi(option.getValue("count").c_str()); - size_t n = 0; - while(true) - { - int ret = g_hmap.sync(time(NULL), data); - if (ret == TC_HashMap::RT_OK) - { - break; - } - else - { - ++n; - cout << "sync:" << data._key << endl; - } - } - cout << g_hmap.desc() << endl; - } - else if(option.hasParam("view")) - { - cout << g_hmap.desc() << endl; - } - else if(option.hasParam("listh")) - { - TC_HashMap::lock_iterator it; - if(option.hasParam("asc")) - { - it = g_hmap.begin(); - } - else - { - it = g_hmap.rbegin(); - } - - while(it != g_hmap.end()) - { - string k; - string v; - - it->get(k, v); - ++it; - -// usleep(100000); - cout << k << ":" << v << endl; - } - } - else if(option.hasParam("listd")) - { - TC_HashMap::lock_iterator it = g_hmap.beginDirty(); - - while(it != g_hmap.end()) - { - string k; - string v; - - it->get(k, v); - it++; - -// usleep(100000); - cout << k << ":" << v << endl; - } - } - else if(option.hasParam("listt")) - { - TC_HashMap::lock_iterator it; - - if(option.hasParam("asc")) - { - it = g_hmap.beginSetTime(); - } - else - { - it = g_hmap.rbeginSetTime(); - } - - while(it != g_hmap.end()) - { - string k; - string v; - - it->get(k, v); - ++it; - -// usleep(100000); - cout << k << ":" << v << endl; - } - } - else if(option.hasParam("listg")) - { - TC_HashMap::lock_iterator it; - - if(option.hasParam("asc")) - { - it = g_hmap.beginGetTime(); - } - else - { - it = g_hmap.rbeginGetTime(); - } - - while(it != g_hmap.end()) - { - string k; - string v; - - it->get(k, v); - ++it; - - cout << k << ":" << v << endl; - } - } - - else - { - usage(argv[0]); - } - } - - return 0; -} - diff --git a/cpp/test/testUtil/example_tc_http.cpp b/cpp/test/testUtil/example_tc_http.cpp deleted file mode 100644 index c417484c4..000000000 --- a/cpp/test/testUtil/example_tc_http.cpp +++ /dev/null @@ -1,636 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_socket.h" -#include "util/tc_clientsocket.h" -#include "util/tc_http.h" -#include "util/tc_file.h" -#include "util/tc_epoller.h" -#include "util/tc_common.h" -#include -#include -#include -#include -#include - -using namespace tars; - - -void testTC_HTTPClient() -{ - TC_HttpRequest stHttpReq; - stHttpReq.setCacheControl("no-cache"); - stHttpReq.setUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1; .NET CLR 1.1.4322)"); -// stHttpReq.setGetRequest("http://www.baidu.com"); -// stHttpReq.setGetRequest("http://www.google.com"); - stHttpReq.setGetRequest("http://s39.cnzz.com/stat.php?id=533357&web_id=533357&show=pic2"); -// stHttpReq.setGetRequest("http://172.23.154.13:8080/"); -// stHttpReq.setGetRequest("http://172.25.38.67:8080"); -// stHttpReq.setGetRequest("http://172.27.31.50:8090/mqqservice/jiaxin.jsp?qq=39067484&type=4"); - - string sSendBuffer = stHttpReq.encode(); - cout << sSendBuffer << endl; - cout << "***********************************" << endl; - - TC_HttpResponse stHttpRep; - int iRet = stHttpReq.doRequest(stHttpRep, 3000); - if(iRet != 0) - { - cout << iRet << endl; - } - - cout << TC_Common::tostr(stHttpRep.getHeaders()) << endl; - - string r = stHttpRep.encode(); -// cout << r << endl; - cout << "***********************************" << endl; - TC_File::save2file("tmp.html", stHttpRep.getContent()); - cout << "***********************************" << endl; - cout << stHttpRep.getContent().length() << endl; -} - -void testHttpHeader1() -{ - string s = "HTTP/1.1 200 OK\r\n"; - s += "Server:nginx/0.6.39\r\n"; - s += "Accept-Ranges: bytes\r\n"; - s += "Accept-Ranges: bytes\r\n"; - - TC_Http::http_header_type m; - TC_Http::parseHeader(s.c_str(), m); - cout << TC_Common::tostr(m) << endl; - - TC_Http t; - t.setHeader("abc", "def"); - t.setHeader("abC", "def"); - - m = t.getHeaders(); - - cout << TC_Common::tostr(m) << endl; -} - -void testHttp1() -{ - TC_HttpRequest stHttpReq; -// stHttpReq.setGetRequest("http://www.baidu.com"); -// stHttpReq.setGetRequest("http://www.google.com"); -// stHttpReq.setGetRequest("https://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2", true); - string a; - stHttpReq.setPostRequest("https://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2", a, true ); - - cout << stHttpReq.encode() << endl; - - cout << "----------------------------------------" << endl; - - cout << stHttpReq.getHost() << endl; - cout << stHttpReq.getOriginRequest() << endl; - cout << stHttpReq.getRequest() << endl; - cout << stHttpReq.getRequestParam() << endl; - cout << stHttpReq.getRequestUrl() << endl; - - cout << "----------------------------------------" << endl; - - string sSendBuffer = stHttpReq.encode(); - cout << sSendBuffer << endl; - cout << "***********************************" << endl; -} - -void testHttp2() -{ - TC_HttpRequest stHttpReq; - - stHttpReq.setGetRequest("http://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2", true); - - string sSendBuffer = stHttpReq.encode(); - - cout << sSendBuffer << endl; - - string sHost; - uint32_t iPort; - stHttpReq.getHostPort(sHost, iPort); - - cout << "-----" << sHost << ", " << iPort <= 2) && (sNewPath.substr(sNewPath.length()-2) == "/.")) || (sNewPath == ".")) - { - sNewPath.erase(sNewPath.length() - 1); - } - - //处ç†/../çš„å½¢å¼ - pos = 0; - size_t startPos = 0; - - while (1) - { - size_t slashDot = sNewPath.find("/../", pos); - - if (slashDot != string::npos) - { - if (0 == slashDot) - { - sNewPath.erase(0, 3); - continue; - } - - if ( (slashDot > 1) && (sNewPath.substr(slashDot - 2, 2) == "..") ) - { - pos = slashDot + 4; - continue; - } - - startPos = sNewPath.rfind('/', slashDot - 1); - - if (startPos == string::npos) startPos = 0; - - sNewPath.erase(startPos, slashDot + 4 - startPos - 1); - } - else - { - break; - } - } - - //处ç†/..结尾的情况 - if ((sNewPath.size() >= 3) && (sNewPath.substr(sNewPath.size() - 3, 3) == "/..")) - { - size_t slashDot = sNewPath.size() - 3; - if (!((slashDot > 1) && (sNewPath.substr(slashDot - 2, 2) == ".."))) - { - startPos = sNewPath.rfind ('/', slashDot - 1); - if (startPos == string::npos) startPos = 0; - sNewPath.erase (startPos + 1); - } - } - - return sNewPath; - /* - string result = path; - - string::size_type pos; - - pos = 0; - while((pos = result.find("//", pos)) != string::npos) - { - result.erase(pos, 1); - } - - pos = 0; - while((pos = result.find("/./", pos)) != string::npos) - { - result.erase(pos, 2); - } - - pos = 0; - while((pos = result.find("/../", pos)) != string::npos) - { - result.erase(pos, 3); - } - - while(result.substr(0, 4) == "/../") - { - result.erase(0, 3); - } - - if(result == "/.") - { - return result.substr(0, result.size() - 1); - } - - if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.") - { - result.erase(result.size() - 2, 2); - } - - if(result == "/") - { - return result; - } - - if(result.size() >= 1 && result[result.size() - 1] == '/') - { - result.erase(result.size() - 1); - } - - if(result == "/..") - { - result = "/"; - } - - return result; - */ -} - -void testURL() -{ - TC_URL url; - /* - testURL(url, ":pass@www.google.com", "http://www.google.com/"); - - - testURL(url, "http://:pass@www.google.com", "http://www.google.com/"); - testURL(url, "http://www.google.com/abc/def", "http://www.google.com/abc/def"); - cout << url.getPath() << endl; - cout << url.getQuery() << endl; - - testURL(url, "http://user:pass@google.com:99/foo;bar?q=a#ref", "http://user:pass@google.com:99/foo;bar?q=a#ref"); - testURL(url, "http://user@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "http://user@www.qq.com:8080/abc/dev/query?a=b&c=3#ref"); - testURL(url, "http://@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "http://www.qq.com:8080/abc/dev/query?a=b&c=3#ref"); - testURL(url, "http://www.qq.com/abc/dev/query?a=b&c=3#ref", "http://www.qq.com/abc/dev/query?a=b&c=3#ref"); - testURL(url, "http://www.qq.com?a=b&c=3#ref", "http://www.qq.com/?a=b&c=3#ref"); - testURL(url, "http://www.qq.com#ref", "http://www.qq.com/#ref"); - testURL(url, "http://www.qq.com?#ref", "http://www.qq.com/#ref"); - testURL(url, "http://:pass@www.google.com", "http://www.google.com/"), - testURL(url, "http://:@www.google.com", "http://www.google.com/"), - - testURL(url, "ftp://user:password@www.qq.com:21/abc/dev/query?a=b&c=3#ref", "ftp://user:password@www.qq.com/abc/dev/query?a=b&c=3#ref"); - testURL(url, "https://user:password@www.qq.com:433/abc/dev/query?a=b&c=3#ref", "https://user:password@www.qq.com/abc/dev/query?a=b&c=3#ref"); - testURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref"); -*/ - testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "abc/def", "https://user:password@www.qq.com:8080/abc/dev/abc/def"); - testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "/abc/def", "https://user:password@www.qq.com:8080/abc/def"); - testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "../../abc/def", "https://user:password@www.qq.com:8080/abc/def"); - testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "#abc", "https://user:password@www.qq.com:8080/abc/def"); - testBuildURL(url, "http://www.qq.com", "#abc", "http://www.qq.com/#abc"); - testBuildURL(url, "http://www.qq.com/a/b", "#abc", "http://www.qq.com/#abc"); - testBuildURL(url, "http://www.qq.com/a/b", "../../#abc", "http://www.qq.com/#abc"); - testBuildURL(url, "http://www.qq.com/a/b", "./../../#abc", "http://www.qq.com/#abc"); - testBuildURL(url, "http://www.qq.com/a/b/", "../#abc", "http://www.qq.com/#abc"); - testBuildURL(url, "http://www.qq.com/a/b/", "../abc/def/..", "http://www.qq.com/#abc"); - testBuildURL(url, "http://www.qq.com/a/b", "../abc/def/../../.", "http://www.qq.com/#abc"); -} - -void testCookie(const string &sRspURL, const string &sReqURL, const vector &vsCookie) -{ - cout << sRspURL << "=>" << sReqURL << "-----------------------------------" << endl; - - TC_HttpCookie cookie; - - cookie.addCookie(sRspURL, vsCookie); - - list vCookie = cookie.getAllCookie(); - - cout << "All Cookie:" << sRspURL << "-----------------------------------" << endl; - - list::iterator it = vCookie.begin(); - - while(it != vCookie.end()) - { - cout << TC_Common::tostr(it->_data.begin(), it->_data.end(), "; ") << ", " << it->_expires << ", " << it->_path << endl; - - ++it; - } - - cout << "-----------------------------------" << endl << endl; - - string sCookie; - - cookie.getCookieForURL(sReqURL, sCookie); - - cout << TC_Common::tostr(sCookie) << endl; - - cout << "-----------------------------------" << endl << endl; -} - -void testCookie() -{ - assert(TC_HttpCookie::matchDomain("qq.com", "www.qq.com") == true); - assert(TC_HttpCookie::matchDomain(".qq.com", "www.qq.com") == true); - assert(TC_HttpCookie::matchDomain(".qq.com", "qq.com") == true); - assert(TC_HttpCookie::matchDomain("t.qq.com", "www.qq.com") == false); - assert(TC_HttpCookie::matchDomain(".t.qq.com", "www.qq.com") == false); - assert(TC_HttpCookie::matchDomain(".t.qq.com", "t.qq.com") == true); - assert(TC_HttpCookie::matchDomain(".com", "www.qq.com") == false); - assert(TC_HttpCookie::matchDomain(".com", "qq.com") == false); - assert(TC_HttpCookie::matchDomain(".y.qq.com", "x.y.qq.com") == true); - assert(TC_HttpCookie::matchDomain(".x.y.qq.com", "x.y.qq.com") == true); - assert(TC_HttpCookie::matchDomain(".qq.com", "x.y.qq.com") == true); - assert(TC_HttpCookie::matchDomain(".qq.com", "y.qq.com") == true); - assert(TC_HttpCookie::matchDomain("qq.com", "y.qq.com") == true); - - cout << TC_Common::now2GMTstr() << endl; - - string gmt = TC_Common::tm2GMTstr(time(NULL) + 10); - - string s = "HTTP/1.1 200 OK\r\n";// 200 Aouut Error\r\n"; - s += "Set-Cookie: n1=1; a=1; c=d; Path=/; Domain=qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n2=2; a=0; c=d; Path=/abc/def; Domain=.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n3=3; a=5; c=d; Path=/abc/def/aaa; Domain=.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n4=4; a=6; c=d; Path=/abc; Domain=.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n5=5; a=2; c=d; Path=/; Domain=.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n6=6; c=3; Path=/; Domain=y.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n7=7; c=3; Path=/abc; Domain=.y.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n8=6; c=3; Path=/; Domain=x.y.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n9=7; c=4; Path=/; Domain=.x.y.qq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n10=7; c=4; Path=/; Domain=qqq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n11=7; c=4; Path=/; Domain=.qqq.com; Expires=" + gmt + "\r\n"; - s += "Set-Cookie: n12=8; c=4; Expires=" + gmt + "\r\n"; - s += "Accept-Ranges: bytes\r\n\r\n"; - - TC_HttpResponse rsp; - rsp.decode(s); - - cout << "-----------------------------------" << endl; - - vector vsCookie = rsp.getSetCookie(); - - cout << TC_Common::tostr(vsCookie.begin(), vsCookie.end(), "\r\n") << endl << endl; - - testCookie("http://www.qq.com", "http://www.qq.com", vsCookie); - testCookie("http://www.qq.com/abc/def", "http://www.qq.com", vsCookie); - testCookie("http://www.qq.com/abc/def", "http://www.qq.com/abc", vsCookie); - - cout << endl; -// sleep(11); - - testCookie("http://www.qq.com", "http://qq.com", vsCookie); - testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com/abc/def/aaa", vsCookie); - testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com", vsCookie); - testCookie("http://www.qq.com", "http://www.qq.com/abc/def", vsCookie); - testCookie("http://qq.com", "http://qq.com/abc/def", vsCookie); - testCookie("http://qq.com", "http://t.qq.com/abc/def", vsCookie); - testCookie("http://qq.com", "http://y.qq.com/", vsCookie); - testCookie("http://qq.com", "http://y.qq.com/abc", vsCookie); - testCookie("http://x.y.qq.com", "http://x.y.qq.com", vsCookie); -} - -int main(int argc, char *argv[]) -{ - try - { -// testHttp1(); -// testHttp2(); - -// return 0; - - cout << "testURL-----------------------------------" << endl; - testURL(); - - return 0; - cout << "testHttpHeader2-----------------------------------" << endl; - testHttpHeader2(); - cout << "-----------------------------------" << endl; - testHttpHeader3(); - cout << "testHttpHeader3-----------------------------------" << endl; - testHttpHeader4(); - cout << "testHttpHeader4-----------------------------------" << endl; - testHttpsHeader2(); - cout << "testCookie-----------------------------------" << endl; -// testCookie(); - -// testHttpResponse(); -// testEastMoney(); - -// testHttp2(); -// testTC_HTTPClient(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_http_async.cpp b/cpp/test/testUtil/example_tc_http_async.cpp deleted file mode 100644 index 518ce002e..000000000 --- a/cpp/test/testUtil/example_tc_http_async.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include -#include "util/tc_file.h" -#include "util/tc_http_async.h" - -using namespace std; -using namespace tars; - -class AsyncHttpCallback : public TC_HttpAsync::RequestCallback -{ -public: - AsyncHttpCallback(const string &sUrl) : _sUrl(sUrl) - { - } - virtual void onException(const string &ex) - { - cout << "onException:" << _sUrl << ":" << ex << endl; - } - virtual void onResponse(bool bClose, TC_HttpResponse &stHttpResponse) - { - cout << "onResponse:" << _sUrl << endl;//<< ":" << TC_Common::tostr(stHttpResponse.getHeaders()) << endl; - TC_File::save2file("tmp.html", stHttpResponse.getContent()); - } - virtual void onTimeout() - { - cout << "onTimeout:" << _sUrl << endl; - } - virtual void onClose() - { - cout << "onClose:" << _sUrl << endl; - } -protected: - string _sUrl; -}; - -int addAsyncRequest(TC_HttpAsync &ast, const string &sUrl) -{ - TC_HttpRequest stHttpReq; - stHttpReq.setGetRequest(sUrl); - - TC_HttpAsync::RequestCallbackPtr p = new AsyncHttpCallback(sUrl); - - return ast.doAsyncRequest(stHttpReq, p); -} - -int main(int argc, char *argv[]) -{ - try - { - uint64_t i = TC_Common::now2ms(); - TC_HttpAsync ast; - ast.setTimeout(10000); - ast.start(1); - -// int ret = addAsyncRequest(ast, "www.baidu.com"); - int ret = addAsyncRequest(ast, "http://www.qq.com"); - cout << ret << endl; - /* - addAsyncRequest(ast, "www.google.com"); - addAsyncRequest(ast, "http://news.qq.com/a/20100108/002269.htm"); - addAsyncRequest(ast, "http://news.qq.com/zt/2010/mtjunshou/"); - addAsyncRequest(ast, "http://news.qq.com/a/20100108/000884.htm"); - addAsyncRequest(ast, "http://tech.qq.com/zt/2009/renovate/index.htm"); - */ - ast.waitForAllDone(); - cout << (TC_Common::now2ms() - i) << endl; - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_lock.cpp b/cpp/test/testUtil/example_tc_lock.cpp deleted file mode 100644 index dd5728241..000000000 --- a/cpp/test/testUtil/example_tc_lock.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_thread.h" -#include -#include -#include - -using namespace std; -using namespace tars; - -class MyThread : public TC_Thread, public TC_ThreadLock -{ -public: - MyThread() - { - bTerminate = false; - } - /** - * 结æŸçº¿ç¨‹ - */ - void terminate() - { - //先加é”, 然åŽè®¾ç½®ç»“æŸçŠ¶æ€, 然åŽé€šçŸ¥å…¶ä»–çº¿ç¨‹é†’è¿‡æ¥ - Lock sync(*this); - bTerminate = true; - notifyAll(); - } - - void doSomething() - { - cout << "doSomething" << endl; - } - /** - * è¿è¡Œ - */ -protected: - virtual void run() - { - while (!bTerminate) - { - { - Lock sync(*this); - timedWait(10000); - } - - if (bTerminate) - { - return; - } - doSomething(); - } - } - -protected: - bool bTerminate; -}; - -int main(int argc, char *argv[]) -{ - try - { - MyThread mt; - mt.start(); - - mt.terminate(); - mt.getThreadControl().join(); - } - catch (exception& ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_log.cpp b/cpp/test/testUtil/example_tc_log.cpp deleted file mode 100644 index 8d4d55b5f..000000000 --- a/cpp/test/testUtil/example_tc_log.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - - -#include "util/tc_logger.h" - -using namespace tars; - -ostream& display(ostream& os) -{ -// cout << &os << endl; - os << "display" << endl; - return os; -} - -TC_LoggerThreadGroup group; -TC_RollLogger logger; -TC_DayLogger dlogger; - -#define DT \ - n1 = TC_Common::now2ms(); \ - t = n1 - n; \ - n = n1; - -#define OT if(t > 2) cout << i << ":" << t << endl; - -void test(){ofstream os; os << "abc";}; - -void RollTest( ) -{ - int i = 1000000; - - while(i) - { -/* - int64_t n = TC_Common::now2ms(); - int64_t n1=TC_Common::now2ms(); - int64_t t =TC_Common::now2ms(); -*/ -// t = TC_Common::now2ms() - t; -// logger.info() << "|" << i << "|" << t << endl; - -// DT; -// ostringstream os; -// test(); - -// display(logger.debug()); -// logger.info(); - logger.debug() << "|" << i << "|" << endl; -// OT; -/* - DT; - logger.warn() << "|" << i << "|" << t << endl; - OT; - - DT; - logger.error() << "|" << i << "|" << t << endl; - OT; -*/ - --i; - } - - -} - -void RollTestThread() -{ - logger.init("./test", 1024000, 10); - logger.modFlag(TC_RollLogger::HAS_LEVEL | TC_RollLogger::HAS_PID, true); - logger.setupThread(&group); - - cout << TC_Common::now2str() << endl; - TC_ThreadPool tpoolA; - tpoolA.init(10); - tpoolA.start(); - - { - - for(size_t i = 0; i < tpoolA.getThreadNum(); i++) - { - tpoolA.exec(RollTest); - } - } - - tpoolA.waitForAllDone(); - cout << TC_Common::now2str() << endl; -} - -void DayTest( ) -{ - int i = 10000000; - while(i) - { - dlogger.any() << i << endl; - --i; - } -} - -void DayTestThread() -{ - dlogger.init("./test", "%Y%m%d%H%M"); - - dlogger.setupThread(&group); - - cout << TC_Common::now2str() << endl; - TC_ThreadPool tpoolA; - tpoolA.init(5); - tpoolA.start(); - - { - for(size_t i = 0; i < tpoolA.getThreadNum(); i++) - { - tpoolA.exec(DayTest); - } - } - - tpoolA.waitForAllDone(); - cout << TC_Common::now2str() << endl; -} - -int main(int argc, char *argv[]) -{ - try - { - group.start(3); - - RollTestThread(); -// DayTestThread(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - diff --git a/cpp/test/testUtil/example_tc_mem_chunk.cpp b/cpp/test/testUtil/example_tc_mem_chunk.cpp deleted file mode 100644 index 8c90410a5..000000000 --- a/cpp/test/testUtil/example_tc_mem_chunk.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_mem_chunk.h" -#include "util/tc_sem_mutex.h" -#include "util/tc_shm.h" -#include "util/tc_mmap.h" -#include "util/tc_file.h" -#include "util/tc_option.h" -#include "util/tc_common.h" -#include -#include - -using namespace tars; - -TC_Mmap m; -TC_MemMultiChunkAllocator alloc; - - -void testAllocate() -{ - vector b = alloc.getBlockSize(); - cout << TC_Common::tostr(b) << endl; - cout << alloc.allBlockChunkCount() << endl; - - vector v; - size_t n = 77; - while(true) - { - size_t iAllocSize; - void *p = alloc.allocate(n, iAllocSize); - cout << p << endl; - if(!p) - { - break; - } - v.push_back(p); - n += 2; - } - - cout << v.size() << endl; - - for(size_t i = 0; i < v.size(); i++) - { - alloc.deallocate(v[i]); - } -} -int main(int argc, char *argv[]) -{ - try - { - TC_Option option; - option.decode(argc, argv); - - if(TC_File::isFileExist("mc.hmap")) - { - m.mmap("mc.hmap", TC_File::getFileSize("mc.hmap")); - } - else - { - m.mmap("mc.hmap", 1204); - } - - if(m.iscreate()) - { - alloc.create(m.getPointer(), m.getSize(), 30, 100, 1.2); - } - else - { - alloc.connect(m.getPointer()); - } - - if(option.hasParam("append")) - { - size_t i = m.getSize() * 3; - - m.munmap(); - m.mmap("mc.hmap", i); - alloc.append(m.getPointer(), i); - - m.munmap(); - m.mmap("mc.hmap", i*3); - alloc.append(m.getPointer(), i*3); - - m.munmap(); - m.mmap("mc.hmap", i*5); - alloc.append(m.getPointer(), i*5); - } - else if(option.hasParam("alloc")) - { - testAllocate(); - } - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_mem_queue.cpp b/cpp/test/testUtil/example_tc_mem_queue.cpp deleted file mode 100644 index ff9b85b46..000000000 --- a/cpp/test/testUtil/example_tc_mem_queue.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_mem_queue.h" -#include "util/tc_sem_mutex.h" -#include "util/tc_shm.h" -#include "util/tc_thread_pool.h" -#include "util/tc_common.h" -#include -#include - -using namespace tars; - -TC_Shm shm; -TC_SemMutex semLock; -TC_MemQueue memQueue; - -/** - * 线程调用过程 - * @param s - * @param i - */ -void writeQueue() -{ - int i = 100000000; - while(i) - { - TC_LockT l(semLock); - if(memQueue.push_back(TC_Common::tostr(i))) - { - cout << pthread_self() << " | writeQueue OK " << i << ":" << memQueue.elementCount() << endl; - i--; - } - else - { - cout << pthread_self() << " | writeQueue FULL " << i << endl; - } - } -} - -void readQueue() -{ - while(true) - { - string s; - TC_LockT l(semLock); - if(memQueue.pop_front(s)) - { - cout << pthread_self() << " | readQueue OK " << s << endl; - } - else - { - cout << pthread_self() << " | readQueue EMPTY" << endl; - sleep(1); - } - } -} - -int main(int argc, char *argv[]) -{ - try - { - size_t l = 1024000; - shm.init(l, 8000); - semLock.init(8000); - - if(shm.iscreate()) - { - memQueue.create(shm.getPointer(), l); - } - else - { - memQueue.connect(shm.getPointer(), l); - } - - if(argc > 1) - { - TC_ThreadPool twpool; - twpool.init(4); - twpool.start(); - - for(size_t i = 0; i < twpool.getThreadNum(); i++) - { - twpool.exec(writeQueue); - } - - twpool.waitForAllDone(); - } - else - { - TC_ThreadPool trpool; - trpool.init(4); - trpool.start(); - - for(size_t i = 0; i < trpool.getThreadNum(); i++) - { - trpool.exec(readQueue); - } - - trpool.waitForAllDone(); - } - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_mmap.cpp b/cpp/test/testUtil/example_tc_mmap.cpp deleted file mode 100644 index 32d810c07..000000000 --- a/cpp/test/testUtil/example_tc_mmap.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_mmap.h" -#include "util/tc_option.h" -#include -#include -#include -#include -#include -#include - -using namespace tars; - -void testCreate(size_t n) -{ - TC_Mmap mmap; - cout << "create mmap" << endl; - mmap.mmap("mmap.dat", n); - mmap.munmap(); - cout << "create mmap OK" << endl; -} - -void testWrite(const string &s) -{ - TC_Mmap mmap; - cout << "write mmap" << endl; - mmap.mmap("mmap.dat", 1000); - memcpy(mmap.getPointer(), s.c_str(), s.length()); - - sleep(10); - - mmap.munmap(); -} - -void testRead() -{ - TC_Mmap mmap; - cout << "read mmap" << endl; - mmap.mmap("mmap.dat", 1000); - - string s; - s.assign((char*)mmap.getPointer(), mmap.getSize()); - mmap.munmap(); - - cout << s << endl; -} - -int main(int argc, char *argv[]) -{ - try - { - TC_Option option; - option.decode(argc, argv); - - int pagesize = sysconf(_SC_PAGESIZE); - - cout << "pagesize:" << pagesize << endl; - - if(option.getValue("test") == "create") - { - size_t n = 50; - testCreate(n); - } - else if(option.getValue("test") == "write") - { - testWrite(option.getValue("c")); - } - else if(option.getValue("test") == "read") - { - testRead(); - } - - - return 0; - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_multi_hashmap.cpp b/cpp/test/testUtil/example_tc_multi_hashmap.cpp deleted file mode 100644 index d8e6d0dd9..000000000 --- a/cpp/test/testUtil/example_tc_multi_hashmap.cpp +++ /dev/null @@ -1,812 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_multi_hashmap.h" -#include "util/tc_shm.h" -#include "util/tc_option.h" -#include -#include -#include "util/tc_common.h" -#include - -using namespace tars; - -//////////////////////////////////////////////////////////////////// -// - -TC_Shm g_shm; -TC_Multi_HashMap g_hmap; -bool g_bSilent = false; // 是å¦ä¸ºå®‰é™æ¨¡å¼ï¼Œä¸è¾“出日志,å¯ä»¥å–得更高的速度 - -// 普通的功能测试 -struct FunctionTest -{ - static void initMem() - { -#if __WORDSIZE == 64 - size_t iMemSize = 1024*20; -#else - size_t iMemSize = 1024*10; -#endif - g_shm.init(iMemSize, 0x12345); - cout << "init mem, shm key=0x" << hex << 0x12345 << dec << endl; - } - - static void initMap() - { - if (g_shm.iscreate()) - { - size_t iMinSize = 2, iMaxSize = 8; - g_hmap.initDataBlockSize(iMinSize, iMaxSize, 2.0); - g_hmap.create(g_shm.getPointer(), g_shm.size()); - g_hmap.setEraseCount(3); - } - else - { - g_hmap.connect(g_shm.getPointer(), g_shm.size()); - } - } -}; - -// 大数æ®é‡æ€§èƒ½æµ‹è¯• -struct PerformanceTest -{ - static void initMem() - { - size_t iMemSize = 1073741824; // 1G - g_shm.init(iMemSize, 0x23456); - cout << "init mem, shm key=0x" << hex << 0x23456 << dec << endl; - } - - static void initMap() - { - if (g_shm.iscreate()) - { - size_t iMinSize = 5, iMaxSize = 20; - g_hmap.initDataBlockSize(iMinSize, iMaxSize, 2.0); - g_hmap.create(g_shm.getPointer(), g_shm.size()); - g_hmap.setEraseCount(10); - } - else - { - g_hmap.connect(g_shm.getPointer(), g_shm.size()); - } - } -}; - -void set(const string &mk, const string &uk, const string &v, uint8_t version, bool dirty, bool full, bool head) -{ - vector vRecords; - - TC_Multi_HashMap::DATATYPE eType = (full ? TC_Multi_HashMap::FULL_DATA : TC_Multi_HashMap::PART_DATA); - - timeval tv_1, tv_2; - gettimeofday(&tv_1, NULL); - - int ret = TC_Multi_HashMap::RT_OK; - if(uk.empty()) - { - // 设置主keyçš„only key - ret = g_hmap.set(mk, vRecords); - } - else - { - if(v.empty()) - { - // 设置主键的only key - ret = g_hmap.set(mk, uk, eType, head, vRecords); - } - else - { - ret = g_hmap.set(mk, uk, v, version, dirty, eType, head, vRecords); - } - } - - gettimeofday(&tv_2, NULL); - - long lTime = 0; - if(tv_2.tv_usec < tv_1.tv_usec) - { - lTime = (tv_2.tv_sec -1 - tv_1.tv_sec) * 1000000; - } - else - { - lTime = (tv_2.tv_sec - tv_1.tv_sec) * 1000000 + (tv_2.tv_usec - tv_1.tv_usec); - } - - //assert(ret == 0); - cout << "set:" << mk << "|" << uk << "|" << v << "|" << ret << "|" << lTime << endl; - for(size_t i = 0; i < vRecords.size(); i++) - { - cout << "set erase:" << vRecords[i]._mkey << "|" << vRecords[i]._data._key - << "|" << vRecords[i]._data._value << endl; - } -} - -void get(const string &mk, const string &uk) -{ - TC_Multi_HashMap::Value v; - if(!uk.empty()) - { - // æ ¹æ®ä¸»é”®æŸ¥è¯¢æ•°æ® - int ret = g_hmap.get(mk, uk, v); - cout << "get:" << mk << "|" << uk << "|" << v._data._value << "|" - << (int)v._data._iVersion << "|" << v._data._dirty << "|" << ret << endl; - } - else - { - // 查主keyä¸‹çš„æ‰€æœ‰æ•°æ® - size_t count = g_hmap.count(mk); - cout << "count of " << mk << ": " << count << endl; - TC_Multi_HashMap::lock_iterator it = g_hmap.find(mk); - while(it != g_hmap.end()) - { - TC_Multi_HashMap::Value v; - int ret = it->get(v); - cout << v._mkey << "|" << v._data._key << "|" << v._data._value - << "|" << (int)v._data._iVersion << "|" << v._data._dirty << "|" << ret << endl; - it ++; - } - } -} - -void del(const string &mk, const string &uk) -{ - vector vtDel; - int ret = TC_Multi_HashMap::RT_OK; - if(uk.empty()) - { - ret = g_hmap.del(mk, vtDel); - } - else - { - TC_Multi_HashMap::Value v; - ret = g_hmap.del(mk, uk, v); - vtDel.push_back(v); - } - cout << "del:" << mk << "|" << uk << "|" << ret << endl; - for(size_t i = 0; i < vtDel.size(); i ++) - { - cout << "del:" << vtDel[i]._mkey << "|" << vtDel[i]._data._key << "|" << vtDel[i]._data._value << endl; - } -} - -void sync() -{ - TC_Multi_HashMap::Value data; - size_t n = 0; - g_hmap.setSyncTime(30); - g_hmap.sync(); - while(true) - { - int ret = g_hmap.sync(time(NULL), data); - if (ret == TC_Multi_HashMap::RT_OK) - { - break; - } - else if(ret == TC_Multi_HashMap::RT_NEED_SYNC) - { - ++n; - cout << "sync:" << data._mkey << "|" << data._data._key << "|" - << data._data._value << "|" << data._data._synct << endl; - } - } - cout << n << " records synced" << endl; -} - -void erase(int ratio) -{ - vector vtErased; - int ret = g_hmap.erase(ratio, vtErased, true); - cout << "erase:" << ratio << "|" << vtErased.size() << "|" << ret << endl; - for(size_t i = 0; i < vtErased.size(); i++) - { - cout << "erase:" << vtErased[i]._mkey << "|" << vtErased[i]._data._key - << "|" << vtErased[i]._data._value << endl; - } -} - -void listh() -{ - const TC_Multi_HashMap::tagMapHead &head = g_hmap.getMapHead(); - cout << "list hash count: " << head._iElementCount << endl; - TC_Multi_HashMap::hash_iterator it = g_hmap.hashBegin(); - while(it != g_hmap.hashEnd()) - { - vector vv; - it->get(vv); - ++it; - - for(size_t i = 0; i < vv.size(); i ++) - { - cout << vv[i]._mkey << "|" << vv[i]._data._key << "|" << vv[i]._data._value - << "|" << (int)vv[i]._data._iVersion << "|" << vv[i]._data._dirty << "|" << vv[i]._data._synct << endl; - } - } -} - -void list(int type, bool asc = true) -{ - TC_Multi_HashMap::lock_iterator it; - switch(type) - { - case 0: - // 按block链é历 - if(asc) - { - // å‡åº - it = g_hmap.begin(); - } - else - { - // é™åº - it = g_hmap.rbegin(); - } - break; - case 1: - // 按get时间链é历 - if(asc) - { - // å‡åº - it = g_hmap.beginGetTime(); - } - else - { - // é™åº - it = g_hmap.rbeginGetTime(); - } - break; - case 2: - // 按set时间链é历 - if(asc) - { - // å‡åº - it = g_hmap.beginSetTime(); - } - else - { - // é™åº - it = g_hmap.rbeginSetTime(); - } - break; - case 3: - // 按è„æ•°æ®é“¾é历 - it = g_hmap.beginDirty(); - break; - default: - return; - } - - while(it != g_hmap.end()) - { - TC_Multi_HashMap::Value v; - it->get(v); - ++it; - - cout << v._mkey << "|" << v._data._key << "|" << v._data._value - << "|" << (int)v._data._iVersion << "|" << v._data._dirty << "|" << v._data._synct << endl; - } -} - -void batchset(size_t iCount, size_t iAvgRec) -{ - srand(time(NULL)); - string mk, uk, v; - vector vRecords; - - timeval tv_1, tv_2; - gettimeofday(&tv_1, NULL); - - for(size_t i = 1; i <= iCount / iAvgRec; i ++) - { - mk = TC_Common::tostr(i); - size_t iRec = rand() % iAvgRec + 1; - for(size_t j = 1; j <= iRec; j ++) - { - uk = TC_Common::tostr(j); - v = mk + uk; - - timeval tv_1, tv_2; - if(!g_bSilent) - { - gettimeofday(&tv_1, NULL); - } - - vRecords.clear(); - int ret = g_hmap.set(mk, uk, v, 0, true, TC_Multi_HashMap::FULL_DATA, true, vRecords); - - if(!g_bSilent) - { - gettimeofday(&tv_2, NULL); - long lTime = 0; - if(tv_2.tv_usec < tv_1.tv_usec) - { - lTime = (tv_2.tv_sec -1 - tv_1.tv_sec) * 1000000; - } - else - { - lTime = (tv_2.tv_sec - tv_1.tv_sec) * 1000000 + (tv_2.tv_usec - tv_1.tv_usec); - } - - //assert(ret == 0); - cout << "set:" << mk << "|" << uk << "|" << v << "|" << ret << "|" << lTime << endl; - for(size_t i = 0; i < vRecords.size(); i++) - { - cout << "set erase:" << vRecords[i]._mkey << "|" << vRecords[i]._data._key - << "|" << vRecords[i]._data._value << endl; - } - } - } - } - - gettimeofday(&tv_2, NULL); - long lTime = 0; - if(tv_2.tv_usec < tv_1.tv_usec) - { - lTime = (tv_2.tv_sec -1 - tv_1.tv_sec) * 1000000; - } - else - { - lTime = (tv_2.tv_sec - tv_1.tv_sec) * 1000000 + (tv_2.tv_usec - tv_1.tv_usec); - } - cout << "batchset:" << iCount << "|" << iAvgRec << "|" << lTime << endl; -} - -void batchset() -{ - vector vtSet, vtErased; - - TC_Multi_HashMap::Value v; - - v._mkey = "1", v._data._key = "1", v._data._value = "11"; - vtSet.push_back(v); - - v._mkey = "1", v._data._key = "2", v._data._value = "12"; - vtSet.push_back(v); - - v._mkey = "2", v._data._key = "1", v._data._value = "21"; - vtSet.push_back(v); - - v._mkey = "3", v._data._key = "1", v._data._value = "31"; - vtSet.push_back(v); - - v._mkey = "4", v._data._key = "1", v._data._value = "41"; - vtSet.push_back(v); - - int ret = g_hmap.set(vtSet, TC_Multi_HashMap::FULL_DATA, true, true, vtErased); - - for(size_t i = 0; i < vtSet.size(); i ++) - { - cout << "set:" << vtSet[i]._mkey << "|" << vtSet[i]._data._key << "|" << vtSet[i]._data._value << "|" << ret << endl; - } - for(size_t i = 0; i < vtErased.size(); i++) - { - cout << "set erase:" << vtErased[i]._mkey << "|" << vtErased[i]._data._key - << "|" << vtErased[i]._data._value << endl; - } -} - -void batchget(size_t iCount, size_t iAvgRec) -{ - srand(time(NULL)); - string mk, uk; - TC_Multi_HashMap::Value v; - - timeval tv_1, tv_2; - gettimeofday(&tv_1, NULL); - - for(size_t i = 1; i <= iCount / iAvgRec; i ++) - { - mk = TC_Common::tostr(i); - size_t iRec = rand() % iAvgRec + 1; - for(size_t j = 1; j <= iRec; j ++) - { - uk = TC_Common::tostr(j); - - timeval tv_1, tv_2; - if(!g_bSilent) - { - gettimeofday(&tv_1, NULL); - } - - int ret = g_hmap.get(mk, uk, v); - - if(!g_bSilent) - { - gettimeofday(&tv_2, NULL); - long lTime = 0; - if(tv_2.tv_usec < tv_1.tv_usec) - { - lTime = (tv_2.tv_sec -1 - tv_1.tv_sec) * 1000000; - } - else - { - lTime = (tv_2.tv_sec - tv_1.tv_sec) * 1000000 + (tv_2.tv_usec - tv_1.tv_usec); - } - - //assert(ret == 0); - cout << "get:" << mk << "|" << uk << "|" << v._data._value << "|" - << (int)v._data._iVersion << "|" << v._data._dirty << "|" << ret << "|" << lTime << endl; - } - } - } - - gettimeofday(&tv_2, NULL); - long lTime = 0; - if(tv_2.tv_usec < tv_1.tv_usec) - { - lTime = (tv_2.tv_sec -1 - tv_1.tv_sec) * 1000000; - } - else - { - lTime = (tv_2.tv_sec - tv_1.tv_sec) * 1000000 + (tv_2.tv_usec - tv_1.tv_usec); - } - cout << "batchset:" << iCount << "|" << iAvgRec << "|" << lTime << endl; -} - - -// 内存大å°æµ‹è¯•ï¼Œæµ‹è¯•ä¸€äº›ç»“构在ä¸åŒçš„机器上所å ç”¨å­—节数 -void testMemSize() -{ - cout << "sizeof(int)=" << sizeof(int) << endl; - cout << "sizeof(long)=" << sizeof(long) << endl; - cout << "sizeof(size_t)=" << sizeof(size_t) << endl; - cout << "sizeof(time_t)=" << sizeof(time_t) << endl; - cout << "sizeof(tagBlockHead)=" << sizeof(TC_Multi_HashMap::Block::tagBlockHead) << endl; - cout << "sizeof(tagMainKeyHead)=" << sizeof(TC_Multi_HashMap::MainKey::tagMainKeyHead) << endl; - cout << "sizeof(tagChunkHead)=" << sizeof(TC_Multi_HashMap::Block::tagChunkHead) << endl; - cout << "sizeof(tagHashItem)=" << sizeof(TC_Multi_HashMap::tagHashItem) << endl; - cout << "sizeof(tagMainKeyHashItem)=" << sizeof(TC_Multi_HashMap::tagMainKeyHashItem) << endl; -} - -void usage(char *argv) -{ - cout << argv << " --help\tshow this help message" << endl; - cout << "\t --mem\tview memory size of some structs" << endl; - cout << "\t --f\tdo the functionality test(default option)" << endl; - cout << "\t --p\tdo the performance test" << endl; - cout << "\t --s\tsilent mode in performance test, no output" << endl; - cout << "\t --release\trelease the allocated share memory" << endl; - cout << "\t --view\tview statistic information of this map" << endl; - cout << "\t --set [--mkey=] [--ukey=] [--value=] [--version] [--clean] [--full] [--tail]" << endl; - cout << "\t --get [--mkey=] [--ukey=]" << endl; - cout << "\t --del [--mkey=] [--ukey=]" << endl; - cout << "\t --sync" << endl; - cout << "\t --erase [--ratio]" << endl; - cout << "\t --isdirty [--mkey=] [--ukey=]" << endl; - cout << "\t --listh\tlist data by hash index" << endl; - cout << "\t --listb [--desc]\tlist data by block chain" << endl; - cout << "\t --listg [--desc]\tlist data by get time chain" << endl; - cout << "\t --lists [--desc]\tlist data by set time chain" << endl; - cout << "\t --listd\tlist data by dirty data chain" << endl; - cout << "\t --set-batch [--count] [--avgrec]\tdo batch set operation, will set total count records with avgrec records per main key" << endl; - cout << "\t --get-batch [--count] [--avgrec]\tdo batch get operation" << endl; - cout << "\t --chkmk [--mkey]\tcheck if main key exists" << endl; - cout << "\t --setstate [--mkey] [--ukey] [--dirty] [--clean] [--part] [--full]\tcheck if main key exists" << endl; - cout << "\t --chkbad [--hash] [--repair]" << endl; -} - -int main(int argc, char *argv[]) -{ - try - { - TC_Option option; - option.decode(argc, argv); - - map m = option.getMulti(); - - if(option.hasParam("help") || option.getMulti().empty()) - { - usage(argv[0]); - return 0; - } - - if(option.hasParam("mem")) - { - testMemSize(); - return 0; - } - - g_bSilent = option.hasParam("s"); - - // éšæœºè®¾ç½®å’ŒèŽ·å–时也当作性能测试 - bool bPerfTest = option.hasParam("p"); - if(bPerfTest) - { - PerformanceTest::initMem(); - } - else - { - FunctionTest::initMem(); - } - - if(option.hasParam("release")) - { - g_shm.del(); - return 0; - } - - if(bPerfTest) - { - PerformanceTest::initMap(); - } - else - { - FunctionTest::initMap(); - } - - if(option.hasParam("view")) - { - cout << g_hmap.desc() << endl; - } - else if(option.hasParam("set")) - { - string mk = option.getValue("mkey"); - string uk = option.getValue("ukey"); - string v = option.getValue("value"); - uint8_t version = option.getValue("version").empty() ? 0 : atoi(option.getValue("version").c_str()); - bool dirty = option.getValue("clean").empty() ? true : false; - bool full = option.getValue("full").empty() ? false : true; - bool head = option.getValue("tail").empty() ? true : false; - - if(mk.empty()) - { - usage(argv[0]); - return 0; - } - - set(mk, uk, v, version, dirty, full, head); - } - else if(option.hasParam("get")) - { - string mk; - string uk; - mk = option.getValue("mkey"); - uk = option.getValue("ukey"); - - if(mk.empty()) - { - usage(argv[0]); - return 0; - } - - get(mk, uk); - } - else if(option.hasParam("del")) - { - string mk; - string uk; - mk = option.getValue("mkey"); - uk = option.getValue("ukey"); - - if(mk.empty()) - { - usage(argv[0]); - return 0; - } - - del(mk, uk); - } - else if(option.hasParam("sync")) - { - sync(); - } - else if(option.hasParam("erase")) - { - string ratio = option.getValue("ratio"); - if(ratio.empty()) - { - usage(argv[0]); - return 0; - } - - erase(TC_Common::strto(ratio)); - } - else if(option.hasParam("isdirty")) - { - string mk; - string uk; - mk = option.getValue("mkey"); - uk = option.getValue("ukey"); - if(mk.empty()) - { - usage(argv[0]); - return 0; - } - int ret = TC_Multi_HashMap::RT_OK; - if(uk.empty()) - { - ret = g_hmap.checkDirty(mk); - } - else - { - ret = g_hmap.checkDirty(mk, uk); - } - cout << "isdirty:" << mk << "|" << uk << "|" << (ret == TC_Multi_HashMap::RT_DIRTY_DATA) << endl; - } - else if(option.hasParam("listh")) - { - listh(); - } - else if(option.hasParam("listb")) - { - if(option.hasParam("desc")) - { - list(0, false); - } - else - { - list(0, true); - } - } - else if(option.hasParam("listg")) - { - if(option.hasParam("desc")) - { - list(1, false); - } - else - { - list(1, true); - } - } - else if(option.hasParam("lists")) - { - if(option.hasParam("desc")) - { - list(2, false); - } - else - { - list(2, true); - } - } - else if(option.hasParam("listd")) - { - list(3); - } - else if(option.hasParam("set-batch")) - { - string sCount = option.getValue("count"); - string sAvgRec = option.getValue("avgrec"); - if(!sCount.empty()) - { - batchset(TC_Common::strto(sCount), sAvgRec.empty() ? 2 : TC_Common::strto(sAvgRec)); - } - else - { - // ä¸å¸¦å‚数的批é‡è®¾ç½® - batchset(); - } - } - else if(option.hasParam("get-batch")) - { - string sCount = option.getValue("count"); - string sAvgRec = option.getValue("avgrec"); - if(sCount.empty()) - { - usage(argv[0]); - return 0; - } - - batchget(TC_Common::strto(sCount), sAvgRec.empty() ? 2 : TC_Common::strto(sAvgRec)); - } - else if(option.hasParam("chkmk")) - { - string mk = option.getValue("mkey"); - if(mk.empty()) - { - usage(argv[0]); - return 0; - } - - int ret = g_hmap.checkMainKey(mk); - if(ret == TC_Multi_HashMap::RT_OK) - { - cout << "main key " << mk << " exists" << endl; - } - else if(ret == TC_Multi_HashMap::RT_ONLY_KEY) - { - cout << "main key " << mk << " exists, but only key" << endl; - } - else if(ret == TC_Multi_HashMap::RT_PART_DATA) - { - cout << "main key " << mk << " exists, and possible part data" << endl; - } - else if(ret == TC_Multi_HashMap::RT_NO_DATA) - { - cout << "main key " << mk << " not exists" << endl; - } - else - { - cout << "chkmk " << mk << " failed: " << ret << endl; - } - } - else if(option.hasParam("setstate")) - { - string mk = option.getValue("mkey"); - string uk = option.getValue("ukey"); - bool bDirty = option.hasParam("dirty"); - bool bClean = option.hasParam("clean"); - bool bFull = option.hasParam("full"); - bool bPart = option.hasParam("part"); - if(mk.empty()) - { - usage(argv[0]); - return 0; - } - if(!uk.empty()) - { - if(bDirty) - { - int ret = g_hmap.setDirty(mk, uk); - cout << "setdirty:" << mk << "|" << uk << "|" << ret << endl; - } - else if(bClean) - { - int ret = g_hmap.setClean(mk, uk); - cout << "setclean:" << mk << "|" << uk << "|" << ret << endl; - } - else - { - usage(argv[0]); - return 0; - } - } - else - { - if(bFull) - { - int ret = g_hmap.setFullData(mk, true); - cout << "set full data:" << mk << "|" << ret << endl; - } - else if(bPart) - { - int ret = g_hmap.setFullData(mk, false); - cout << "set part data:" << mk << "|" << ret << endl; - } - else - { - usage(argv[0]); - return 0; - } - } - } - else if(option.hasParam("chkbad")) - { - string sHash = option.getValue("hash"); - if(sHash.empty()) - { - usage(argv[0]); - return 0; - } - uint32_t iHash = TC_Common::strto(sHash); - size_t iCount = g_hmap.checkBadBlock(iHash, option.hasParam("repair")); - cout << iCount << " bad block under hash " << iHash << endl; - } - else - { - usage(argv[0]); - } - } - catch(exception &e) - { - cout << "error! " << e.what() << endl; - } - - return 0; -} - diff --git a/cpp/test/testUtil/example_tc_mysql.cpp b/cpp/test/testUtil/example_tc_mysql.cpp deleted file mode 100644 index a1512df47..000000000 --- a/cpp/test/testUtil/example_tc_mysql.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_mysql.h" -#include - -using namespace tars; - -TC_Mysql mysql; - -void test() -{ -// cout << mysql.getVariables("character_set_client") << endl; - - TC_Mysql::MysqlData data; - data = mysql.queryRecord("select * from t_app_users"); - for(size_t i = 0; i < data.size(); i++) - { - cout << data[i]["ID"] << endl; - } -} - -void testInsert() -{ - map > m; - m["ID"] = make_pair(TC_Mysql::DB_INT, "2334"); - m["USERID"] = make_pair(TC_Mysql::DB_STR, "abcttt"); - m["APP"] = make_pair(TC_Mysql::DB_STR, "abcapbbp"); - mysql.insertRecord("t_user_logs", m); - mysql.replaceRecord("t_user_logs", m); - - mysql.updateRecord("t_user_logs", m, "where ID=2234"); -} - -int main(int argc, char *argv[]) -{ - try - { - try{ - mysql.init("172.25.38.21", "pc", "pc@sn", "db_dmqq_system"); - mysql.connect(); - - }catch(exception &ex) - { - cout << ex.what() << endl; - } - mysql.execute("select * from t_app_users"); - test(); - -// sleep(10); -// test(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_option.cpp b/cpp/test/testUtil/example_tc_option.cpp deleted file mode 100644 index 4e44b03d2..000000000 --- a/cpp/test/testUtil/example_tc_option.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_option.h" -#include "util/tc_common.h" -#include -#include -#include - -using namespace tars; - - -int main(int argc, char *argv[]) -{ - try - { - TC_Option op; - op.decode(argc, argv); - - map mp = op.getMulti(); - - cout << "map:" << endl; - cout << TC_Common::tostr(mp) << endl; - - vector d = op.getSingle(); - cout << "vector:" << endl; - cout << TC_Common::tostr(d) << endl; - - cout << op.getValue("abc") << endl; - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_pack.cpp b/cpp/test/testUtil/example_tc_pack.cpp deleted file mode 100644 index 8ff6f9fbe..000000000 --- a/cpp/test/testUtil/example_tc_pack.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_pack.h" -#include -#include - -using namespace tars; - -int main(int argc, char *argv[]) -{ - try - { - bool b = true; - char c = 'a'; - short si = 3; - int ii = 4; - char cn[] = "abc"; - string sn = "def"; - - TC_PackIn pi; - pi << b << c << si << ii << cn << sn; - - string s = pi.topacket(); - - TC_PackOut po(s.c_str(), s.length()); - po >> b; - assert(b == true); - cout << "bool OK" << endl; - - po >> c; - assert(c == 'a'); - cout << "char OK" << endl; - - po >> si; - assert(si == 3); - cout << "short OK" << endl; - - po >> ii; - assert(ii == 4); - cout << "int OK" << endl; - - po >> cn; - assert(cn == string("abc")); - cout << "char[] OK" << endl; - - po >> sn; - assert(sn == "def"); - cout << "string OK" << endl; - - { - pi.clear(); - - pi << b << c; - pi.insert(1) << cn; - - s = pi.topacket(); - po.init(s.c_str(), s.length()); - po >> b; - assert(b == true); - cout << "bool OK" << endl; - - po >> cn; - assert(cn == string("abc")); - cout << "char[] OK" << endl; - - po >> c; - assert(c == 'a'); - cout << "char OK" << endl; - } - - { - pi.clear(); - pi << b << c; - pi.replace(1) << 'b'; - - s = pi.topacket(); - po.init(s.c_str(), s.length()); - po >> b; - assert(b == true); - cout << "bool OK" << endl; - - po >> c; - assert(c == 'b'); - cout << "char OK" << endl; - } - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_singleton.cpp b/cpp/test/testUtil/example_tc_singleton.cpp deleted file mode 100644 index 4de334e74..000000000 --- a/cpp/test/testUtil/example_tc_singleton.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_singleton.h" -#include "util/tc_timeprovider.h" -#include -#include - -using namespace std; -using namespace tars; - -class B : public TC_Singleton -{ -public: - B(){cout << "B" << endl;} - ~B(){cout << "~B" << endl;} - - void test(){cout << "test B" << endl;} -}; - -class A : public TC_Singleton -{ -public: - A(){cout << "A" << endl;} - ~A() - { - cout << "~A" << endl; - B::getInstance()->test(); - } - - void test(){cout << "test A" << endl;} -}; - - - -int main(int argc, char *argv[]) -{ - try - { -// A::getInstance()->test(); -// B::getInstance()->test(); - - cout << TC_TimeProvider::getInstance()->getNow() << endl; - sleep(1); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_socket.cpp b/cpp/test/testUtil/example_tc_socket.cpp deleted file mode 100644 index c3dd9eec4..000000000 --- a/cpp/test/testUtil/example_tc_socket.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_socket.h" -#include "util/tc_clientsocket.h" -#include "util/tc_http.h" -#include "util/tc_epoller.h" -#include "util/tc_common.h" -#include -#include -#include -#include - -using namespace tars; - -string now2str(const string &sFormat = "%Y%m%d%H%M%S") -{ - time_t t = time(NULL); - struct tm *pTm = localtime(&t); - if(pTm == NULL) - { - return ""; - } - - char sTimeString[255] = "\0"; - - strftime(sTimeString, sizeof(sTimeString), sFormat.c_str(), pTm); - - return string(sTimeString); -} - -void testTC_Socket() -{ - TC_Socket tcSocket; - tcSocket.createSocket(); - tcSocket.bind("192.168.128.66", 8765); - tcSocket.listen(5); -} - -void testTC_TCPClient() -{ - TC_TCPClient tc; - tc.init("172.16.28.79", 8382, 3); - - cout << now2str() << endl; - int i = 10000; - while(i>0) - { - string s = "test"; - char c[1024] = "\0"; - size_t length = 4; - int iRet = tc.sendRecv(s.c_str(), s.length(), c, length); - if(iRet < 0) - { - cout << "send recv error:" << iRet << ":" << c << endl; - } - i--; - // cout << c << endl; - assert(c == s); - } - cout << now2str() << endl; - -} - -void testShortSock() -{ - int i = 1000; - while(i>0) - { - TC_TCPClient tc; - tc.init("127.0.0.1", 8382, 10); - - string s = "test"; - char c[1024] = "\0"; - size_t length = 4; - int iRet = tc.sendRecv(s.c_str(), s.length(), c, length); - if(iRet < 0) - { - cout << "send recv error" << endl; - } - if(i % 1000 == 0) - { - cout << i << endl; - } - usleep(10); - i--; - assert(c == s); - } -} - -void testUdp() -{ - fork();fork();fork();fork();fork(); - int i = 1000; - string s; - for(int j = 0; j < 7192; j++) - { - s += "0"; - } - s += "\n"; - - while(i>0) - { - TC_UDPClient tc; - tc.init("127.0.0.1", 8082, 3000); - - char c[10240] = "\0"; - size_t length = sizeof(c); - - int iRet = tc.sendRecv(s.c_str(), s.length(), c, length); - if(iRet < 0) - { - cout << "send recv error:" << iRet << endl; - } - if(i % 1000 == 0) - { - cout << i << endl; - } - - i--; - if(c != s) - { - cout << c << endl; -// break; - } - } -} -void testTimeoutSock() -{ - int i = 10; - while(i>0) - { - TC_TCPClient tc; - tc.init("127.0.0.1", 8382, 3); - - string s = "test"; - char c[1024] = "\0"; - size_t length = 4; - int iRet = tc.sendRecv(s.c_str(), s.length(), c, length); - if(iRet < 0) - { - cout << "send recv error" << endl; - } - if(i % 1000 == 0) - { - cout << i << endl; - } - i--; - sleep(3); - - assert(c == s); - } -} - -void testLocalHost() -{ - vector v = TC_Socket::getLocalHosts(); - cout << TC_Common::tostr(v.begin(), v.end()) << endl; -} - -int main(int argc, char *argv[]) -{ - try - { - testUdp(); - return 0; - - TC_Socket t; - t.createSocket(); - t.bind("127.0.0.1", 0); - string d; - uint16_t p; - t.getSockName(d, p); - t.close(); - - cout << d << ":" << p << endl; - return 0; - - testLocalHost(); - - string st; - TC_Socket s; - s.createSocket(SOCK_STREAM, AF_LOCAL); - if(argc > 1) - { - s.bind("/tmp/tmp.udp.sock"); - s.listen(5); - - s.getSockName(st); - cout << st << endl; - - struct sockaddr_un stSockAddr; - socklen_t iSockAddrSize = sizeof(sockaddr_un); - TC_Socket c; - s.accept(c, (struct sockaddr *) &stSockAddr, iSockAddrSize); - } - else - { - s.connect("/tmp/tmp.udp.sock"); - s.getPeerName(st); - cout << st << endl; - } - - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_thread.cpp b/cpp/test/testUtil/example_tc_thread.cpp deleted file mode 100644 index 66b6236e2..000000000 --- a/cpp/test/testUtil/example_tc_thread.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_thread.h" -#include -#include -#include - -using namespace std; -using namespace tars; - -class MyThread : public TC_Thread, public TC_ThreadLock -{ -public: - MyThread() - { - bTerminate = false; - } - /** - * 结æŸçº¿ç¨‹ - */ - void terminate() - { - bTerminate = true; - - { - TC_ThreadLock::Lock sync(*this); - notifyAll(); - } - } - - void doSomething() - { - cout << "doSomething" << endl; - } - /** - * è¿è¡Œ - */ -protected: - virtual void run() - { - while(!bTerminate) - { - //TODO: your business - doSomething(); - - { - TC_ThreadLock::Lock sync(*this); - timedWait(1000); - } - } - } - -protected: - bool bTerminate; -}; - -int main(int argc, char *argv[]) -{ - try - { - MyThread mt; - mt.start(); - - sleep(5); - - mt.terminate(); - mt.getThreadControl().join(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_thread_lock.cpp b/cpp/test/testUtil/example_tc_thread_lock.cpp deleted file mode 100644 index 393f8a939..000000000 --- a/cpp/test/testUtil/example_tc_thread_lock.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_monitor.h" -#include "util/tc_common.h" -#include - -using namespace tars; - -TC_ThreadLock mutex; - -template -class TestLock : public T -{ -public: - TestLock() - { - i = 0; - } - - void out() - { - typename T::Lock lock(*this); - ++i; - cout << pthread_self() << ":" << i << endl; - sleep(3); - cout << pthread_self() << ":" << i << endl; - } - - void testLock(int i) - { - long long n = TC_Common::now2us(); - while(i) - { - typename T::Lock lock(*this); - --i; - } - cout << (TC_Common::now2us() - n)/1000. << endl; - } -protected: - int i; -}; - -//TestLock tl; -TestLock tl; - -void ThreadEntry1(void *) -{ - while(1) - tl.out(); -} - -void ThreadEntry2(void *) -{ - while(1) - tl.out(); -} - -int main(int argc, char *argv[]) -{ - try - { - int i = 1000000; - tl.testLock(i); - - pthread_t itid1; - pthread_t itid2; - - pthread_create(&itid1, NULL, (void *(*)(void *))&ThreadEntry1, NULL); - pthread_create(&itid2, NULL, (void *(*)(void *))&ThreadEntry2, NULL); - - pthread_join(itid1, NULL); - pthread_join(itid2, NULL); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_thread_pool.cpp b/cpp/test/testUtil/example_tc_thread_pool.cpp deleted file mode 100644 index a62baea75..000000000 --- a/cpp/test/testUtil/example_tc_thread_pool.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_thread_pool.h" -#include "util/tc_common.h" - -#include -#include - -using namespace std; -using namespace tars; - -TC_ThreadPool tpool; -TC_ThreadLock l; - -/** - * 线程ç§æœ‰æ•°æ® - */ -class MyThreadData : public TC_ThreadPool::ThreadData -{ -public: - virtual ~MyThreadData() - { - cout << pthread_self() << endl; - } - -public: - pthread_t _idata; -}; - -/** - * 线程åˆå§‹åŒ– - */ -void threadInitialize() -{ - MyThreadData *p = TC_ThreadPool::ThreadData::makeThreadData(); - p->_idata = pthread_self(); - TC_ThreadPool::setThreadData(p); - - cout << p->_idata << endl; -} - -/** - * 线程调用过程 - * @param s - * @param i - */ -void TestFunction3(const string &s, int i) -{ - MyThreadData *p = (MyThreadData*)TC_ThreadPool::getThreadData(); - assert(pthread_self() == p->_idata); - -// cout << pthread_self() << " | TestFunction3('" << s << "', " << i << ")" << endl; -} - -/** - * è¿è¡Œçº¿ç¨‹æ±  - */ -void testThreadPool() -{ - //4个线程 - tpool.init(4); - - //å¯åŠ¨çº¿ç¨‹, 指定åˆå§‹åŒ–对象,也å¯ä»¥æ²¡æœ‰åˆå§‹åŒ–对象:tpool.start(); - tpool.start(threadInitialize); - - string s("a"); - int i = 1000000; - - //调用i次 - while(i) - { - tpool.exec(std::bind(&TestFunction3, std::cref(s), i)); - --i; - } - - //ç­‰å¾…çº¿ç¨‹ç»“æŸ - cout << "waitForAllDone..." << endl; - bool b = tpool.waitForAllDone(1000); - cout << "waitForAllDone..." << b << ":" << tpool.getJobNum() << endl; - - //åœæ­¢çº¿ç¨‹,æžå¤Ÿçš„时候也会自动åœæ­¢çº¿ç¨‹ - //线程结æŸæ—¶,会自动释放ç§æœ‰æ•°æ® - tpool.stop(); -} - -void test(int i, string &s) -{ - cout << i << ":" << s << endl; - s = TC_Common::tostr(i + 10); -} - -/** - * è¿è¡Œçº¿ç¨‹æ±  - */ -void testThreadPool1() -{ - //4个线程 - tpool.init(1); - - //å¯åŠ¨çº¿ç¨‹, 指定åˆå§‹åŒ–对象,也å¯ä»¥æ²¡æœ‰åˆå§‹åŒ–对象:tpool.start(); - tpool.start(); - - string bid; - for(int i=0; i<10; i++) - { - bid = TC_Common::tostr(i); - cout << bid << endl; - - cout << "index = " << i << ",bid = " << bid << endl; - - tpool.exec(std::bind(&test, i, std::ref(bid))); - } - - //ç­‰å¾…çº¿ç¨‹ç»“æŸ - cout << "waitForAllDone..." << endl; - bool b = tpool.waitForAllDone(1000); - cout << "waitForAllDone..." << b << ":" << tpool.getJobNum() << endl; - - //åœæ­¢çº¿ç¨‹,æžå¤Ÿçš„时候也会自动åœæ­¢çº¿ç¨‹ - //线程结æŸæ—¶,会自动释放ç§æœ‰æ•°æ® - tpool.stop(); -} - -int main(int argc, char *argv[]) -{ - try - { - testThreadPool1(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/example_tc_thread_queue.cpp b/cpp/test/testUtil/example_tc_thread_queue.cpp deleted file mode 100644 index 63f9056e8..000000000 --- a/cpp/test/testUtil/example_tc_thread_queue.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_monitor.h" -#include "util/tc_common.h" -#include "util/tc_thread_queue.h" -#include "util/tc_autoptr.h" -#include "util/tc_thread.h" -#include "util/tc_logger.h" -#include - -using namespace tars; - -TC_ThreadQueue _queue; -TC_RollLogger _logger; - -struct A : public TC_HandleBase -{ -}; - -typedef TC_AutoPtr APtr; - -class WriteThread : public TC_Thread, public TC_HandleBase -{ - /** - * è¿è¡Œ - */ -protected: - virtual void run() - { - while(true) - { - timeval t1; - gettimeofday(&t1, NULL); - - _queue.push_back("abc"); - usleep(1000); - - timeval t2; - gettimeofday(&t2, NULL); - - _logger.debug() << "push_back:" << t2.tv_usec - t1.tv_usec << endl; - usleep(1000); - cout << "write" << endl; - } - } -}; - -typedef TC_AutoPtr WriteThreadPtr; - -class ReadThread : public TC_Thread, public TC_HandleBase -{ - /** - * è¿è¡Œ - */ -protected: - virtual void run() - { - while(true) - { - string t; - timeval t1; - gettimeofday(&t1, NULL); - if(_queue.pop_front(t, 60000)) - { - timeval t2; - gettimeofday(&t2, NULL); - _logger.debug() << "pop_front:" << t2.tv_usec - t1.tv_usec << endl; -// cout << pthread_self() << ":" << t << endl; - usleep(20 * 1000); - } - - } - } -}; - -typedef TC_AutoPtr ReadThreadPtr; - -int main(int argc, char *argv[]) -{ - try - { - WriteThreadPtr wt = new WriteThread(); - wt->start(); - - vector rts; - for(size_t i = 0; i < 3;i++) - { - rts.push_back(new ReadThread()); - rts.back()->start(); - } - - wt->getThreadControl().join(); - } - catch(exception &ex) - { - cout << ex.what() << endl; - } - - return 0; -} - - diff --git a/cpp/test/testUtil/log.config.conf b/cpp/test/testUtil/log.config.conf deleted file mode 100644 index 0f2fd6e66..000000000 --- a/cpp/test/testUtil/log.config.conf +++ /dev/null @@ -1,64 +0,0 @@ - - - - #proxy需è¦çš„é…ç½® - - #åœ°å€ - locator = - #最大超时时间 - maxinvoketimeout = - #模å—间调用[å¯é€‰] - stat = one.onestat.StatObj - #网络å‘é€çº¿ç¨‹ä¸ªæ•° - sendthread = 1 - #网络接收线程个数 - recvthread = 1 - #网络异步回调线程个数 - asyncthread = 3 - #模å—å称 - modulename = one.onelog - - - #定义所有绑定的IP - - #应用å称 - app = one - #æœåŠ¡å称 - server = onelog - #æœåŠ¡çš„æ•°æ®ç›®å½•,å¯æ‰§è¡Œæ–‡ä»¶,é…置文件等 - basepath = /usr/local/app/one/onelog/data - #日志路径 - logpath = /usr/local/app/one/app_log/ - #网络线程个数 - netthread = 2 - #本地管ç†å¥—接字[å¯é€‰] - local = tcp -h /tmp/one.onelog.sock -p 0 -t 10000 - #本地nodeçš„ip:port:timeout[å¯é€‰] - node = ServerObj@tcp -h 127.0.0.1 -p 2345 -t 10000 - #é…置中心的地å€[å¯é€‰] - config = one.oneconfig.ConfigObj - - #é…ç½®ç»‘å®šç«¯å£ - - #ip:port:timeout - endpoint = tcp -h 10.1.36.39 -p 20500 -t 10000 - #å…许的IPåœ°å€ - allow = - #最大连接数 - maxconns = 4096 - #当å‰çº¿ç¨‹ä¸ªæ•° - threads = 10 - #处ç†å¯¹è±¡, 支æŒå¤šä¸ª, 以,分隔 - servants = LogObj - - - - - - #logçš„æ•°æ®ç›®å½• - logpath = /usr/local/app/one/app_log - #写线程个数 - logthread = 10 - - - diff --git a/cpp/test/testUtil/template.config.conf b/cpp/test/testUtil/template.config.conf deleted file mode 100644 index 58e950cf6..000000000 --- a/cpp/test/testUtil/template.config.conf +++ /dev/null @@ -1,55 +0,0 @@ - - - - #proxy需è¦çš„é…ç½® - - #åœ°å€ - locator = QueryObj@tcp -h 127.0.0.1 -p 20000:tcp -h 127.0.0.1 -p 30000 - #最大超时时间 - maxinvoketimeout = 3 - #模å—间调用[å¯é€‰] - stat = tars.tarsstat.StatObj - #网络å‘é€çº¿ç¨‹ä¸ªæ•° - sendthread = 3 - #网络接收线程个数 - recvthread = 3 - #网络异步回调线程个数 - asyncthread = 3 - #模å—å称 - modulename = ${modulename} - - - #定义所有绑定的IP - - #应用å称 - app = ${app} - #æœåŠ¡å称 - server = ${server} - #æœåŠ¡çš„æ•°æ®ç›®å½•,å¯æ‰§è¡Œæ–‡ä»¶,é…置文件等 - basepath = ${basepath} - #日志路径 - logpath = ${logpath} - #网络线程个数 - netthread = 2 - #本地管ç†å¥—接字[å¯é€‰] - local = ${local} - #本地nodeçš„ip:port:timeout[å¯é€‰] - node = ${node} - #é…置中心的地å€[å¯é€‰] - config = tars.tarsconfig.ConfigObj - #远程LogServer[å¯é€‰] - log = tars.tarslog.LogObj - - - - - A = B - 127.0.0.1 - 192.168.1.1 - 4.5.6.6 - - - - - - diff --git a/cpp/test/testUtil/test.html b/cpp/test/testUtil/test.html deleted file mode 100644 index 33603119f..000000000 --- a/cpp/test/testUtil/test.html +++ /dev/null @@ -1,33 +0,0 @@ - - - - New Document - - - - - - - - -
- - - - - - - - - -
- -
- - - - -
- - - \ No newline at end of file diff --git a/cpp/thirdparty/thirdparty.sh b/cpp/thirdparty/thirdparty.sh deleted file mode 100644 index 362b95c64..000000000 --- a/cpp/thirdparty/thirdparty.sh +++ /dev/null @@ -1 +0,0 @@ -git clone https://github.com/Tencent/rapidjson.git \ No newline at end of file diff --git a/cpp/tools/CMakeLists.txt b/cpp/tools/CMakeLists.txt deleted file mode 100644 index 7f93f59b1..000000000 --- a/cpp/tools/CMakeLists.txt +++ /dev/null @@ -1,112 +0,0 @@ -cmake_minimum_required(VERSION 2.8) -project(tools) - -include_directories(${util_SOURCE_DIR}/include) - -link_libraries(tarsutil) - -function(tars2language TARGET) - set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin") - - include_directories(${PROJECT_SOURCE_DIR}/tarsparse) - - aux_source_directory(. DIR_SRCS) - - link_libraries(tarsparse) - - add_executable(${TARGET} ${DIR_SRCS}) - - install(TARGETS ${TARGET} - RUNTIME DESTINATION tools) -endfunction() - -macro(complice_lex OUT_DEPENDS_LEX_LIST) - set(DEPENDS_LIST) - - FILE(GLOB SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.l") - - set(CLEAN_LIST) - - foreach (FILE ${SRC_LIST}) - - get_filename_component(NAME_WE ${FILE} NAME_WE) - - # Éú³ÉtarsÎļþ - set(LEX_IN ${FILE}) - set(LEX_CPP ${NAME_WE}.lex.cpp) - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${LEX_CPP} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - COMMAND flex ${LEX_IN} - COMMENT "flex ${LEX_IN}") - - list(APPEND DEPENDS_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${LEX_CPP}) - - #ÉèÖÃÐèÒªÇå³ýµÄÎļþ - get_filename_component(PATH ${FILE} PATH) - list(APPEND CLEAN_LIST ${PATH}/${LEX_CPP}) - - endforeach (FILE) - - set(OUT_DEPENDS_LEX_LIST ${DEPENDS_LIST}) - - set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") - -endmacro() - -macro(complice_yacc OUT_DEPENDS_YACC_LIST) - set(DEPENDS_LIST) - - FILE(GLOB SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.y") - - set(CLEAN_LIST) - - foreach (FILE ${SRC_LIST}) - - get_filename_component(NAME_WE ${FILE} NAME_WE) - - set(YACC_IN ${FILE}) - set(YACC_H ${NAME_WE}.tab.hpp) - set(YACC_CPP ${NAME_WE}.tab.cpp) - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${YACC_H} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - COMMAND bison ${YACC_IN} -o ${YACC_CPP} - COMMENT "bison ${YACC_IN} -o ${YACC_CPP}") - - list(APPEND DEPENDS_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${YACC_H}) - - #ÉèÖÃÐèÒªÇå³ýµÄÎļþ - get_filename_component(PATH ${FILE} PATH) - - list(APPEND CLEAN_LIST ${PATH}/${YACC_H}) - list(APPEND CLEAN_LIST ${PATH}/${YACC_CPP}) - #if (EXISTS ${PATH}/${NAME_WE}.tab.cpp) - # list(APPEND CLEAN_LIST ${PATH}/${NAME_WE}.tab.cpp) - #endif (EXISTS ${PATH}/${NAME_WE}.tab.cpp) - - endforeach (FILE) - - set(OUT_DEPENDS_YACC_LIST ${DEPENDS_LIST}) - - set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") - -endmacro() - -add_subdirectory(tarsgrammar) -add_subdirectory(tarsparse) -add_subdirectory(tars2cpp) -add_subdirectory(tars2python) -add_subdirectory(tars2cs) -add_subdirectory(tars2c) -add_subdirectory(tars2oc) -add_subdirectory(tars2php) -add_subdirectory(tars2android) -add_subdirectory(tars2node) - -FIND_PACKAGE(Protobuf) -IF(PROTOBUF_FOUND) - add_subdirectory(pb2tarscpp) -ENDIF() diff --git a/cpp/tools/README.md b/cpp/tools/README.md deleted file mode 100644 index dcf12d1f2..000000000 --- a/cpp/tools/README.md +++ /dev/null @@ -1,19 +0,0 @@ -该工程是Tars框架C++语言IDLçš„æºä»£ç  - -代ç ç›®å½•ä»‹ç» - - -目录å称 |功能 -------------------|---------------- -tarsgrammer | 定义tarsè¯æ³•å’Œè¯­æ³•çš„分æžè§„则 -tarsparse | 解æžtarsè¯è¯­å’Œè¯­æ³•åˆ†æžçš„æºç å®žçŽ° -tars2cpp | 通过tarsæ–‡ä»¶ç”Ÿæˆ C++ 代ç çš„IDL工具的æºç å®žçŽ° -tars2c | 通过tarsæ–‡ä»¶ç”Ÿæˆ C 代ç çš„IDL工具的æºç å®žçŽ° -tars2cs | 通过tarsæ–‡ä»¶ç”Ÿæˆ C# 代ç çš„IDL工具的æºç å®žçŽ° -tars2oc | 通过tarsæ–‡ä»¶ç”Ÿæˆ ObjectC 代ç çš„IDL工具的æºç å®žçŽ° -tars2php | 通过tarsæ–‡ä»¶ç”Ÿæˆ PHP 代ç çš„IDL工具的æºç å®žçŽ° -tars2python | 通过tarsæ–‡ä»¶ç”Ÿæˆ Python 代ç çš„IDL工具的æºç å®žçŽ° -tars2node | 通过tarsæ–‡ä»¶ç”Ÿæˆ Node.js 代ç çš„IDL工具的æºç å®žçŽ° -tars2android | 通过tarsæ–‡ä»¶ç”Ÿæˆ Android 代ç çš„IDL工具的æºç å®žçŽ° -tars2android | 通过tarsæ–‡ä»¶ç”Ÿæˆ Android 代ç çš„IDL工具的æºç å®žçŽ° -pb2tarscpp |通过proto文件生æˆtars C++ 代ç çš„protocæ’件æºç å®žçŽ° diff --git a/cpp/tools/pb2tarscpp/CMakeLists.txt b/cpp/tools/pb2tarscpp/CMakeLists.txt deleted file mode 100644 index b3cfc5b9d..000000000 --- a/cpp/tools/pb2tarscpp/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -set(MODULE "pb2tarscpp") - -set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin") - -aux_source_directory(. DIR_SRCS) - -link_libraries(protobuf;protoc) - -add_executable("pb2tarscpp" ${DIR_SRCS}) - -install(TARGETS "pb2tarscpp" - RUNTIME DESTINATION tools) - diff --git a/cpp/tools/pb2tarscpp/CppGenCallback.cpp b/cpp/tools/pb2tarscpp/CppGenCallback.cpp deleted file mode 100644 index 75d50050e..000000000 --- a/cpp/tools/pb2tarscpp/CppGenCallback.cpp +++ /dev/null @@ -1,146 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#include -#include -#include -#include - -#include "CppGenCallback.h" - -static std::string GenCallbackMethod(const ::google::protobuf::MethodDescriptor* method, const std::string& pkg, int indent) { - std::string out; - out.reserve(8 * 1024); - - out = "virtual void callback_" + method->name() + "(const " + pkg + "::" + method->output_type()->name() + "& ret)" + LineFeed(indent); - out += "{ throw std::runtime_error(\"callback_" + method->name() + " override incorrect.\"); }" + LineFeed(indent); - - out += "virtual void callback_" + method->name() + "_exception(tars::Int32 ret)" + LineFeed(indent); - out += "{ throw std::runtime_error(\"callback_" + method->name() + "_exception() override incorrect.\"); }" + LineFeed(indent); - - return out; -} - -static std::string GenResponseContext(int indent) { - std::string out; - out.reserve(8 * 1024); - - out += "virtual const map & getResponseContext() const"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(++indent); - out += "CallbackThreadData * pCbtd = CallbackThreadData::getData();"; - out += LineFeed(indent); - out += LineFeed(indent); - out += "assert(pCbtd != NULL);"; - out += LineFeed(indent); - out += "if (!pCbtd->getContextValid())"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(++indent); - out += "throw TC_Exception(\"can't get response context\");"; - out += LineFeed(--indent); - out += "}"; - out += LineFeed(indent); - out += "return pCbtd->getResponseContext();"; - out += LineFeed(--indent); - out += "}"; - - return out; -} - -std::string GenPrxCallback(const ::google::protobuf::ServiceDescriptor* desc, int indent) { - std::string out; - out.reserve(8 * 1024); - - const auto& name = desc->name(); - const auto& pkg = desc->file()->package(); - out += "/* callback of async proxy for client */"; - out += LineFeed(indent); - out += "class " + name + "PrxCallback : public tars::ServantProxyCallback" + LineFeed(indent); - out += "{"; - out += LineFeed(indent); - out += "public:" + LineFeed(++indent) + "virtual ~" + name + "PrxCallback() {}"; - out += LineFeed(indent); - out += LineFeed(indent); - - for (int i = 0; i < desc->method_count(); ++i) { - auto method = desc->method(i); - out += GenCallbackMethod(method, pkg, indent); - } - - out += LineFeed(indent); - out += LineFeed(indent); - - out += GenResponseContext(indent); - - // gen onDispatch - out += LineFeed(indent); - out += LineFeed(indent); - out += "virtual int onDispatch(tars::ReqMessagePtr msg)"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(++indent); - out += "static ::std::string __all[] = "; - out += "{"; - out += LineFeed(++indent); - for (int i = 0; i < desc->method_count(); ++i) { - auto method = desc->method(i); - out += "\"" + method->name() + "\","; - out += LineFeed(indent); - } - out += LineFeed(--indent); - out += "};" + LineFeed(indent); - out += "pair r = equal_range(__all, __all + " + std::to_string((long long)desc->method_count()) + ", " + "std::string(msg->request.sFuncName));"; - out += LineFeed(indent); - out += "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;" + LineFeed(indent); - out += "switch(r.first - __all)" + LineFeed(indent); - out += "{"; - out += LineFeed(++indent); - for (int i = 0; i < desc->method_count(); ++i) { - auto method = desc->method(i); - out += LineFeed(indent); - out += "case " + std::to_string((long long)i) + ":" + LineFeed(indent); - out += "{" + LineFeed(++indent); - out += "if (msg->response.iRet != tars::TARSSERVERSUCCESS)" + LineFeed(indent); - out += "{" + LineFeed(++indent); - out += "callback_" + method->name() + "_exception(msg->response.iRet);" + LineFeed(indent); - out += "return msg->response.iRet;" + LineFeed(--indent) + "}"; - - out += LineFeed(indent); - out += pkg + "::" + method->output_type()->name() + " _ret;" + LineFeed(indent); - out += "_ret.ParseFromArray(msg->response.sBuffer.data(), msg->response.sBuffer.size());" + LineFeed(indent); - out += "CallbackThreadData * pCbtd = CallbackThreadData::getData();" + LineFeed(indent); - out += "assert(pCbtd != NULL);" + LineFeed(indent); - out += LineFeed(indent); - out += "pCbtd->setResponseContext(msg->response.context);" + LineFeed(indent); - out += "callback_" + method->name() + "(_ret);" + LineFeed(indent); - out += "pCbtd->delResponseContext();" + LineFeed(indent); - out += LineFeed(indent); - out += "return tars::TARSSERVERSUCCESS;"; - - out += LineFeed(--indent); - out += "}"; - } - - // end switch - out += LineFeed(--indent); - out += "}"; - - out += LineFeed(indent); - out += LineFeed(indent); - out += "return tars::TARSSERVERNOFUNCERR;"; - out += LineFeed(--indent); - out += "}"; // end of onDispatch - out += LineFeed(--indent); - out += "};"; // end of class CallbackPrx - - out += LineFeed(indent); - out += "typedef tars::TC_AutoPtr<" + name + "PrxCallback> " + name + "PrxCallbackPtr;"; - out += LineFeed(indent); - - return out; -} - diff --git a/cpp/tools/pb2tarscpp/CppGenCallback.h b/cpp/tools/pb2tarscpp/CppGenCallback.h deleted file mode 100644 index 9bc843f8a..000000000 --- a/cpp/tools/pb2tarscpp/CppGenCallback.h +++ /dev/null @@ -1,24 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#pragma once - -#include -#include "CppPbUtils.h" - -namespace google -{ - -namespace protobuf -{ -class ServiceDescriptor; -} - -} - - -// gen prx callback -std::string GenPrxCallback(const ::google::protobuf::ServiceDescriptor* desc, int indent); - diff --git a/cpp/tools/pb2tarscpp/CppGenProxy.cpp b/cpp/tools/pb2tarscpp/CppGenProxy.cpp deleted file mode 100644 index fffc21ea5..000000000 --- a/cpp/tools/pb2tarscpp/CppGenProxy.cpp +++ /dev/null @@ -1,126 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#include - -#include -#include -#include -#include - -#include "CppGenCallback.h" - -static std::string GenSyncCall(const ::google::protobuf::MethodDescriptor* method, const std::string& pkg, int indent) { - std::string out; - out.reserve(8 * 1024); - - out += pkg + "::" + method->output_type()->name() + " " + method->name() + "(const " + - pkg + "::" + method->input_type()->name() + "& req, const std::map& context = TARS_CONTEXT(), " + - "std::map* pResponseContext = NULL)"; - out += LineFeed(indent); - out += "{" + LineFeed(++indent); - out += "std::string _os;" + LineFeed(indent) + - "req.SerializeToString(&_os);" + LineFeed(indent) + - "std::vector _vc(_os.begin(), _os.end());" + LineFeed(indent); - out += LineFeed(indent); - out += "tars::ResponsePacket rep;" + LineFeed(indent) + - "std::map _mStatus;" + LineFeed(indent); - out += "tars_invoke(tars::TARSNORMAL, \"" + method->name() + "\", _vc, context, _mStatus, rep);" + LineFeed(indent); - out += "if (pResponseContext)" + LineFeed(++indent); - out += "*pResponseContext = rep.context;" + LineFeed(--indent); - - out += LineFeed(indent); - out += pkg + "::" + method->output_type()->name() + " _ret;" + LineFeed(indent); - out += "_ret.ParseFromArray(rep.sBuffer.data(), rep.sBuffer.size());" + LineFeed(indent) + - "return _ret;"; - out += LineFeed(--indent) + "}"; - out += LineFeed(indent); - out += LineFeed(indent); - - return out; -} - -static std::string GenAsyncCall(const ::google::protobuf::MethodDescriptor* method, - const std::string& name, - const std::string& pkg, - int indent) { - std::string out; - out.reserve(8 * 1024); - - out += "void async_" + method->name() + "(" + name + "PrxCallbackPtr callback, const " + pkg + "::" + - method->input_type()->name() + "& req, const std::map& context = TARS_CONTEXT())" + LineFeed(indent); - out += "{" + LineFeed(++indent); - out += "std::string _os;" + LineFeed(indent) + - "req.SerializeToString(&_os);" + LineFeed(indent) + - "std::vector _vc(_os.begin(), _os.end());" + LineFeed(indent); - out += "std::map _mStatus;" + LineFeed(indent); - out += "tars_invoke_async(tars::TARSNORMAL, \"" + method->name() + "\", _vc, context, _mStatus, callback);"; - out += LineFeed(--indent) + "}"; - out += LineFeed(indent); - - return out; -} - -std::string GenPrx(const ::google::protobuf::ServiceDescriptor* desc, int indent) { - std::string out; - out.reserve(8 * 1024); - - const auto& name = desc->name(); - const auto& pkg = desc->file()->package(); - const auto& proxy = name + "Proxy"; - - out += LineFeed(indent); - out += "/* proxy for client */"; - out += LineFeed(indent); - out += "class " + proxy + " : public tars::ServantProxy"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(indent); - out += "public:"; - out += LineFeed(++indent); - out += "typedef std::map TARS_CONTEXT;" + LineFeed(indent); - // gen methods - for (int i = 0; i < desc->method_count(); ++i) { - auto method = desc->method(i); - out += LineFeed(indent); - // sync method call - out += GenSyncCall(method, pkg, indent); - // async method call - out += GenAsyncCall(method, name, pkg, indent); - } - - // hash call - out += LineFeed(indent); - out += proxy + "* tars_hash(int64_t key)"; - out += "{" + LineFeed(++indent); - out += "return (" + proxy + "*)ServantProxy::tars_hash(key);"; - out += LineFeed(--indent) + "}"; - out += LineFeed(indent); - - // consist hash - out += LineFeed(indent); - out += proxy + "* tars_consistent_hash(int64_t key)"; - out += "{" + LineFeed(++indent); - out += "return (" + proxy + "*)ServantProxy::tars_consistent_hash(key);"; - out += LineFeed(--indent) + "}"; - out += LineFeed(indent); - - // set_timeout - out += LineFeed(indent); - out += proxy + "* tars_set_timeout(int msecond)"; - out += "{" + LineFeed(++indent); - out += "return (" + proxy + "*)ServantProxy::tars_set_timeout(msecond);"; - out += LineFeed(--indent) + "}"; - - out += LineFeed(--indent) + "}; // end class " + proxy; - out += LineFeed(indent); // end class - - out += LineFeed(indent); - out += "typedef tars::TC_AutoPtr<" + proxy + "> " + name + "Prx;"; - out += LineFeed(indent); - - return out; -} - diff --git a/cpp/tools/pb2tarscpp/CppGenProxy.h b/cpp/tools/pb2tarscpp/CppGenProxy.h deleted file mode 100644 index 9febd5a51..000000000 --- a/cpp/tools/pb2tarscpp/CppGenProxy.h +++ /dev/null @@ -1,23 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#pragma once - -#include -#include "CppPbUtils.h" - -namespace google -{ - -namespace protobuf -{ -class ServiceDescriptor; -} - -} - -// gen proxy -std::string GenPrx(const ::google::protobuf::ServiceDescriptor* desc, int indent); - diff --git a/cpp/tools/pb2tarscpp/CppGenServant.cpp b/cpp/tools/pb2tarscpp/CppGenServant.cpp deleted file mode 100644 index 80b556220..000000000 --- a/cpp/tools/pb2tarscpp/CppGenServant.cpp +++ /dev/null @@ -1,142 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#include -#include -#include -#include - -#include "CppGenServant.h" - -static std::string GenMethods(const ::google::protobuf::MethodDescriptor* method, - const std::string& pkg, - int indent) { - std::string out; - out.reserve(8 * 1024); - - out += "virtual " + pkg + "::" + method->output_type()->name() + " " + method->name() + - "(const " + pkg + "::" + method->input_type()->name() + "& , tars::TarsCurrentPtr current) = 0;" + LineFeed(indent); - out += "static void async_response_" + method->name() + "(tars::TarsCurrentPtr current, const " + pkg + "::" + method->output_type()->name() + "&_ret)" + LineFeed(indent); - out += "{" + LineFeed(++indent); - out += "std::string _os;" + LineFeed(indent) + - " _ret.SerializeToString(&_os);" + LineFeed(indent) + - "vector _vc(_os.begin(), _os.end());" + LineFeed(indent) + - "current->sendResponse(tars::TARSSERVERSUCCESS, _vc);"; - out += LineFeed(--indent) + "}"; - out += LineFeed(indent); - out += LineFeed(indent); - - return out; -} - -static std::string GenDispatchCase(const ::google::protobuf::MethodDescriptor* method, - const std::string& pkg, - int indent) { - std::string out; - out.reserve(8 * 1024); - - out += "{"; - out += LineFeed(++indent); - out += "tars::TarsInputStream _is;" + LineFeed(indent) + - "_is.setBuffer(_current->getRequestBuffer());" + LineFeed(indent); - out += LineFeed(indent); - - out += pkg + "::" + method->input_type()->name() + " req;" + LineFeed(indent); - out += "req.ParseFromArray(&_current->getRequestBuffer()[0], _current->getRequestBuffer().size());" + LineFeed(indent); - out += LineFeed(indent); - - out += pkg + "::" + method->output_type()->name() + " _ret = " + method->name() + "(req, _current);" + LineFeed(indent); - out += "if (_current->isResponse())" + LineFeed(indent); - out += "{" + LineFeed(++indent); - out += "std::string _os;" + LineFeed(indent); - out += "_ret.SerializeToString(&_os);" + LineFeed(indent); - out += "std::vector _vc(_os.begin(), _os.end());" + LineFeed(indent); - out += "_sResponseBuffer.assign(_os.begin(), _os.end());"; - out += LineFeed(--indent) + "}"; - out += LineFeed(indent); - out += "return tars::TARSSERVERSUCCESS;"; - - out += LineFeed(--indent); - out += "}"; - - return out; -} - -std::string GenServant(const ::google::protobuf::ServiceDescriptor* desc, int indent) { - std::string out; - out.reserve(8 * 1024); - - const auto& name = desc->name(); - const auto& pkg = desc->file()->package(); - const auto& servant = name; - - out += LineFeed(indent); - out += "/* servant for server */"; - out += LineFeed(indent); - out += "class " + servant + " : public tars::Servant"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(indent); - out += "public:"; - out += LineFeed(++indent); - out += "virtual ~" + servant + "() {}"; - out += LineFeed(indent); - - for (int i = 0; i < desc->method_count(); ++i) { - out += GenMethods(desc->method(i), pkg, indent); - } - - // gen onDispatch - out += LineFeed(indent); - out += "int onDispatch(tars::TarsCurrentPtr _current, std::vector& _sResponseBuffer)"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(++indent); - out += "static ::std::string __all[] = "; - out += "{"; - out += LineFeed(++indent); - for (int i = 0; i < desc->method_count(); ++i) { - auto method = desc->method(i); - out += "\"" + method->name() + "\","; - out += LineFeed(indent); - } - out += LineFeed(--indent); - out += "};"; - out += LineFeed(indent); - out += "pair r = equal_range(__all, __all + " + std::to_string((long long)desc->method_count()) + ", " + "_current->getFuncName());"; - out += LineFeed(indent); - out += "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;"; - out += LineFeed(indent); - out += "switch(r.first - __all)"; - out += LineFeed(indent); - out += "{"; - out += LineFeed(++indent); - for (int i = 0; i < desc->method_count(); ++i) { - auto method = desc->method(i); - out += LineFeed(indent); - out += "case " + std::to_string((long long)i) + ":"; - out += LineFeed(indent); - - out += GenDispatchCase(method, pkg, indent); - } - - // end switch - out += LineFeed(--indent); - out += "} // end switch"; - - out += LineFeed(indent); - out += "return tars::TARSSERVERNOFUNCERR;"; - out += LineFeed(--indent); - out += "}"; // end of onDispatch - out += LineFeed(indent); - - out += LineFeed(--indent) + "}; // end class " + servant; - out += LineFeed(indent); // end class - - out += LineFeed(indent); - - return out; -} - diff --git a/cpp/tools/pb2tarscpp/CppGenServant.h b/cpp/tools/pb2tarscpp/CppGenServant.h deleted file mode 100644 index ea4c00cbb..000000000 --- a/cpp/tools/pb2tarscpp/CppGenServant.h +++ /dev/null @@ -1,22 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#pragma once - -#include -#include "CppPbUtils.h" - -namespace google -{ - -namespace protobuf -{ -class ServiceDescriptor; -} - -} - -std::string GenServant(const ::google::protobuf::ServiceDescriptor* desc, int indent); - diff --git a/cpp/tools/pb2tarscpp/CppPbUtils.cpp b/cpp/tools/pb2tarscpp/CppPbUtils.cpp deleted file mode 100644 index 560de2578..000000000 --- a/cpp/tools/pb2tarscpp/CppPbUtils.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#include -#include "CppPbUtils.h" - - -std::string ProtoFileBaseName(const std::string& fullName) { - std::size_t p = fullName.rfind("."); - assert (p != std::string::npos); - - return fullName.substr(0, p); -} - -const std::string kDeclaration = "/**\n" - " * Tencent is pleased to support the open source community by making Tars available.\n" - " * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.\n" - " * Licensed under the BSD 3-Clause License (the \"License\"); you may not use this file\n" - " * except in compliance with the License. You may obtain a copy of the License at\n" - " * https://opensource.org/licenses/BSD-3-Clause\n" - " * Unless required by applicable law or agreed to in writing, software distributed\n" - " * under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR\n" - " * CONDITIONS OF ANY KIND, either express or implied. See the License for the\n" - " * specific language governing permissions and limitations under the License.\n" - " */\n\n"; - -const std::string kIndent = " "; - -std::string LineFeed(int indent) { - std::string data("\n"); - - for (int i = 0; i < indent; ++i) - data += kIndent; - - return data; -} - diff --git a/cpp/tools/pb2tarscpp/CppPbUtils.h b/cpp/tools/pb2tarscpp/CppPbUtils.h deleted file mode 100644 index bb77ce0e2..000000000 --- a/cpp/tools/pb2tarscpp/CppPbUtils.h +++ /dev/null @@ -1,16 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#pragma once - -#include - -std::string ProtoFileBaseName(const std::string& fullName); - -extern const std::string kDeclaration; -extern const std::string kIndent; - -std::string LineFeed(int indent = 0) ; - diff --git a/cpp/tools/pb2tarscpp/CppPlugin.cpp b/cpp/tools/pb2tarscpp/CppPlugin.cpp deleted file mode 100644 index 69a5aec04..000000000 --- a/cpp/tools/pb2tarscpp/CppPlugin.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#include - -#include -#include -#include - -#include "CppPlugin.h" -#include "CppGenCallback.h" -#include "CppGenProxy.h" -#include "CppGenServant.h" - - -bool CppTarsGenerator::Generate(const google::protobuf::FileDescriptor *file, - const std::string ¶meter, - google::protobuf::compiler::GeneratorContext *context, - std::string *error) const { - - if (!_CheckFile(file, error)) { - return false; - } - - std::string content = _GenHeader(ProtoFileBaseName(file->name())); - - // namespace - content += _GenNamespaceBegin(file->package()); - - const int indent = 1; - content += LineFeed(indent); - for (int i = 0; i < file->service_count(); ++i) { - content += GenPrxCallback(file->service(i), indent); - content += GenPrx(file->service(i), indent); - content += GenServant(file->service(i), indent); - } - - content += _GenNamespaceEnd(file->package()); - - // gen response to parent - const std::string& outputFile = ProtoFileBaseName(file->name()) + ".tars.h"; - std::string output = _GenResponse(outputFile, content); - std::cout << output; - - return true; -} - -std::string -CppTarsGenerator::_GenResponse(const std::string& filename, - const std::string& content) { - google::protobuf::compiler::CodeGeneratorResponse response; - auto f = response.add_file(); - f->set_name(filename); - f->set_content(content); - - // output to parent - std::string outbytes; - response.SerializeToString(&outbytes); - return outbytes; -} - -bool CppTarsGenerator::_CheckFile(const google::protobuf::FileDescriptor* file, - std::string* error) const { - if (file->package().empty()) { - error->append("package name is missed."); - return false; - } - - return true; -} - -std::string CppTarsGenerator::_GenHeader(const std::string& name) { - std::string content; - content.reserve(8 * 1024); - - content += kDeclaration; - content += "#pragma once\n\n"; - content += "#include \n"; - content += "#include \n"; - content += "#include \n"; - content += "#include \"servant/ServantProxy.h\"\n"; - content += "#include \"servant/Servant.h\"\n"; - - // include pb file - content += "#include \"" + name + ".pb.h\"\n"; - - return content; -} - -std::string CppTarsGenerator::_GenNamespaceBegin(const std::string& ns) { - std::string content; - content += "\n\nnamespace "; - content += ns + "\n{"; - - return content; -} - -std::string CppTarsGenerator::_GenNamespaceEnd(const std::string& ns) { - return std::string("\n} // end namespace " + ns + "\n\n"); -} - -int main(int argc, char *argv[]) { - CppTarsGenerator generator; - return google::protobuf::compiler::PluginMain(argc, argv, &generator); -} - diff --git a/cpp/tools/pb2tarscpp/CppPlugin.h b/cpp/tools/pb2tarscpp/CppPlugin.h deleted file mode 100644 index eae575732..000000000 --- a/cpp/tools/pb2tarscpp/CppPlugin.h +++ /dev/null @@ -1,37 +0,0 @@ -// Generates C++ tars service interface out of Protobuf IDL. -// -// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto -// and net/proto2/compiler/public/plugin.h for more information on plugins. - -#include -#include - -namespace google -{ - -namespace protobuf -{ -class FileDescriptor; -} - -} - -class CppTarsGenerator : public google::protobuf::compiler::CodeGenerator { -public: - CppTarsGenerator() {} - - virtual ~CppTarsGenerator() {} - - bool Generate(const google::protobuf::FileDescriptor *file, - const std::string ¶meter, - google::protobuf::compiler::GeneratorContext *context, - std::string *error) const; //override final; -private: - bool _CheckFile(const google::protobuf::FileDescriptor* file, std::string* error) const; - static std::string _GenHeader(const std::string& file); - static std::string _GenNamespaceBegin(const std::string& ns); - static std::string _GenNamespaceEnd(const std::string& ns); - - static std::string _GenResponse(const std::string& filename, const std::string& content); -}; - diff --git a/cpp/tools/tars2android/CMakeLists.txt b/cpp/tools/tars2android/CMakeLists.txt deleted file mode 100644 index 38971c293..000000000 --- a/cpp/tools/tars2android/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -tars2language("tars2android") diff --git a/cpp/tools/tars2android/interface_analysis.cpp b/cpp/tools/tars2android/interface_analysis.cpp deleted file mode 100644 index a22103435..000000000 --- a/cpp/tools/tars2android/interface_analysis.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "interface_analysis.h" - -#include - -InterfaceAnalysis::InterfaceAnalysis() { -} - -void InterfaceAnalysis::analysis(const vector& interfacePtrs) { - for (size_t index = 0; index < interfacePtrs.size(); ++index) { - analysis(interfacePtrs[index]); - } -} - -void InterfaceAnalysis::analysis(const InterfacePtr& interfacePtr) { - const vector& operations = interfacePtr->getAllOperationPtr(); - - for (size_t operationIndex = 0; operationIndex < operations.size(); ++operationIndex) { - const OperationPtr& operation = operations[operationIndex]; - - analysis(operation->getReturnPtr()->getTypePtr()); - - const vector& paramDecls = operation->getAllParamDeclPtr(); - - for (size_t paramIndex = 0; paramIndex < paramDecls.size(); paramIndex++) { - const ParamDeclPtr& paramDeclPtr = paramDecls[paramIndex]; - analysis(paramDeclPtr->getTypeIdPtr()->getTypePtr()); - } - } -} - -void InterfaceAnalysis::analysis(const StructPtr& structPtr) { - const vector& allMembersPtr = structPtr->getAllMemberPtr(); - for (size_t index = 0; index < allMembersPtr.size(); ++index) { - analysis(allMembersPtr[index]->getTypePtr()); - } -} - -void InterfaceAnalysis::analysis(const TypePtr& typePtr) { - VectorPtr vPtr = VectorPtr::dynamicCast(typePtr); - if (vPtr) { - analysis(vPtr->getTypePtr()); - return ; - } - - MapPtr mPtr = MapPtr::dynamicCast(typePtr); - if (mPtr) { - analysis(mPtr->getLeftTypePtr()); - analysis(mPtr->getRightTypePtr()); - return ; - } - - StructPtr sPtr = StructPtr::dynamicCast(typePtr); - if (sPtr) { - // 说明已ç»åˆ†æžè¿‡ï¼Œå¹¶ä¸”找到了 - if (mAllStructs.find(sPtr->getSid()) != mAllStructs.end()) { - return ; - } - - mAllStructs.insert(std::pair(sPtr->getSid(), sPtr)); - analysis(sPtr); - } - - EnumPtr ePtr = EnumPtr::dynamicCast(typePtr); - if (ePtr) { - if (mAllEnums.find(ePtr->getSid()) != mAllEnums.end()) { - return ; - } - - mAllEnums.insert(std::pair(ePtr->getSid(), ePtr)); - } -} - -const std::map& InterfaceAnalysis::getAllStructs() const { - return mAllStructs; -} - -const std::map& InterfaceAnalysis::getAllEnums() const { - return mAllEnums; -} - -const std::map& InterfaceAnalysis::getAllConsts() const { - return mAllConsts; -} - - diff --git a/cpp/tools/tars2android/interface_analysis.h b/cpp/tools/tars2android/interface_analysis.h deleted file mode 100644 index 32497979f..000000000 --- a/cpp/tools/tars2android/interface_analysis.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef INTERFACE_ANALYSIS_H_ -#define INTERFACE_ANALYSIS_H_ - -#include "parse.h" -#include -#include - -class InterfaceAnalysis { -public: - InterfaceAnalysis(); - - const std::map& getAllStructs() const; - const std::map& getAllEnums() const; - const std::map& getAllConsts() const; - - void analysis(const InterfacePtr& interfacePtr); - void analysis(const vector& interfacePtrs); - -private: - void analysis(const StructPtr& structPtr); - void analysis(const TypePtr& typePtr); - -private: - std::map mAllStructs; - std::map mAllEnums; - std::map mAllConsts; -}; - - - -#endif /* INTERFACE_ANALYSIS_H_ */ diff --git a/cpp/tools/tars2android/main.cpp b/cpp/tools/tars2android/main.cpp deleted file mode 100644 index 89bd86353..000000000 --- a/cpp/tools/tars2android/main.cpp +++ /dev/null @@ -1,159 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "tars2android.h" - -void usage() -{ - cout << "Usage : tars2android [OPTION] tarsfile" << endl; - cout << " tars2android support type: bool byte short int long float double vector map" << endl; - cout << "supported [OPTION]:" << endl; - cout << " --help help,print this" << endl; - cout << " --dir=DIRECTORY generate java file to DIRECTORY(default to current)" << endl; - cout << " --base-package=NAME package prefix, default(com.qq.)" << endl; - cout << " --not-force-array default changed byte vector to byte[], use this for list" << endl; - cout << " --check-default= optional field not package(default package)" << endl; - cout << " --extends-package=NAME set the extends package name"<< endl; - cout << " --with-charset set charset, default UTF8" << endl; - cout << " --with-JavaBeanRule support javabeab, default not support" << endl; - cout << " --include=dir1;dir2;dir3 设置tars文件æœç´¢è·¯å¾„" << endl; - - cout << endl; - exit(0); -} - -void check(vector &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - Tars2Java t2a; - - g_parse->setTars(option.hasParam("with-tars")); - - if(option.getValue("dir") != "") - { - t2a.setBaseDir(option.getValue("dir")); - } - else - { - t2a.setBaseDir("."); - } - - if(option.hasParam("base-package")) - { - t2a.setBasePackage(option.getValue("base-package")); - } - else - { - t2a.setBasePackage("prx."); - } - - if (option.hasParam("not-force-array")) - { - t2a.setForceArray(false); - } - else - { - t2a.setForceArray(true); - } - - if (option.hasParam("extends-package")) - { - t2a.setTafPacket(option.getValue("extends-package")); - } - - t2a.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "true"?true:false); - - - if (option.hasParam("with-charset")) - { - t2a.setCharset(option.getValue("with-charset")); - } - else - { - t2a.setCharset("UTF8"); - } - - if(option.hasParam("with-JavaBeanRule")) - { - t2a.setWithJbr(true); - } - else - { - t2a.setWithJbr(false); - } - - t2a.setWithCompact(false); - t2a.setEnumCompact(true); - - t2a.setWithGenerateInterfaceDependencies(true); - t2a.setWithFilterRomTars(true); - - try - { - //增加includeæœç´¢è·¯å¾„ - g_parse->addIncludePath(option.getValue("include")); - - for(size_t i = 0; i < vTars.size(); i++) - { - g_parse->parse(vTars[i]); - t2a.createFile(vTars[i]); - } - }catch(exception& e) - { - cerr< - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -static string g_default_package = "com.tars"; -////////////////////////////////////////////////////////////////////////////////// -// -Tars2Java::Tars2Java() -{ - _bForceArray = false; - _bCheckDefault = false; - _bWithJbr = false; - _bWithCompact = false; - s_TARS_PACKAGE = g_default_package + TARS_PACKAGE; - s_PROXY_PACKAGE = g_default_package + PROXY_PACKAGE; - s_WUP_PACKAGE = g_default_package + WUP_PACKAGE; -} - -string Tars2Java::writeTo(const TypeIdPtr& pPtr) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "_os.write(" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else - { - s << TAB << "_os.write(" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - - return s.str(); -} - -string Tars2Java::readFrom(const TypeIdPtr& pPtr) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "_is.read((tars::Int32&)" << pPtr->getId(); - } - else - { - s << TAB << "_is.read(" << pPtr->getId(); - } - - s << ", " << pPtr->getTag() << ", " << (pPtr->isRequire() ? "true" : "false") << ");" << endl; - - return s.str(); -} - -string Tars2Java::display(const TypeIdPtr& pPtr) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "_ds.display((tars::Int32)" << pPtr->getId() << ",\"" << pPtr->getId() << "\");" << endl;; - } - else - { - s << TAB << "_ds.display(" << pPtr->getId() << ",\"" << pPtr->getId() << "\");" << endl;; - } - - return s.str(); -} - -string Tars2Java::toTypeInit(const TypePtr& pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool: - return "false;"; - case Builtin::KindByte: - return "0;"; - case Builtin::KindShort: - return "0;"; - case Builtin::KindInt: - return "0;"; - case Builtin::KindLong: - return "0L;"; - case Builtin::KindFloat: - return "0.0f;"; - case Builtin::KindDouble: - return "0.0;"; - case Builtin::KindString: - return "\"\";"; - default: - return ""; - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (bPtr && bPtr->kind() == Builtin::KindByte) - { - return "(" + tostr(vPtr->getTypePtr()) + "[]) new " + tostr(vPtr->getTypePtr()) + "[1];"; - } - - if (_bForceArray) - { - string sType; - size_t iPosBegin, iPosEnd; - sType = tostr(vPtr->getTypePtr()); - //map<>的数组去掉 '<...>' - if ((iPosBegin = sType.find("<")) != string::npos && (iPosEnd = sType.rfind(">")) != string::npos) - { - sType = sType.substr(0, iPosBegin) + sType.substr(iPosEnd + 1); - } - //[] (数组)的数组å˜ä¸º[1] - sType = tars::TC_Common::replace(sType, "[]", "[1]"); - return "(" + tostr(vPtr->getTypePtr()) + "[]) new " + sType + "[1];";; - } - - return "new " + tostrVector(vPtr) + "();"; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return "new " + tostrMap(mPtr, true) + "();"; - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return "new " + tostrStruct(sPtr) + "();"; - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return "0;"; - - return ""; -} - - -string Tars2Java::toObjStr(const TypePtr& pPtr) const -{ - string sType = tostr(pPtr); - - if (sType == "boolean") return "Boolean"; - if (sType == "byte") return "Byte"; - if (sType == "short") return "Short"; - if (sType == "int") return "Integer"; - if (sType == "long") return "Long"; - if (sType == "float") return "Float"; - if (sType == "double") return "Double"; - - return sType; -} - -bool Tars2Java::isObjType(const TypePtr& pPtr) const -{ - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - return false; - } - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool: - return false; - case Builtin::KindByte: - return false; - case Builtin::KindShort: - return false; - case Builtin::KindInt: - return false; - case Builtin::KindLong: - return false; - case Builtin::KindFloat: - return false; - case Builtin::KindDouble: - return false; - default: - return true; - } - } - - return true; -} - -string Tars2Java::tostr(const TypePtr& pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return tostrStruct(sPtr); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - -/*******************************BuiltinPtr********************************/ -string Tars2Java::tostrBuiltin(const BuiltinPtr& pPtr) const -{ - string s; - - switch (pPtr->kind()) - { - case Builtin::KindBool: - s = "boolean"; - break; - case Builtin::KindByte: - s = "byte"; - break; - case Builtin::KindShort: - s = "short"; - break; - case Builtin::KindInt: - s = "int"; - break; - case Builtin::KindLong: - s = "long"; - break; - case Builtin::KindFloat: - s = "float"; - break; - case Builtin::KindDouble: - s = "double"; - break; - case Builtin::KindString: - s = "String"; - break; - case Builtin::KindVector: - s = "java.util.ArrayList"; - break; - case Builtin::KindMap: - s = "java.util.HashMap"; - break; - default: - assert(false); - break; - } - - return s; -} -/*******************************VectorPtr********************************/ -string Tars2Java::tostrVector(const VectorPtr& pPtr) const -{ - string s; - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr && bPtr->kind() == Builtin::KindByte) - { - s = "byte []"; - } - else if (_bForceArray) - { - s = tostr(pPtr->getTypePtr()) + "[]"; - } - else - { - s = "java.util.ArrayList" + string("<") + toObjStr(pPtr->getTypePtr()) + ">"; - } - return s; -} - -/*******************************MapPtr********************************/ -string Tars2Java::tostrMap(const MapPtr& pPtr, bool bNew) const -{ - string s; - if (!bNew) - { - s = "java.util.Map"; - } - else - { - s = "java.util.HashMap"; - } - s += string("<") + toObjStr(pPtr->getLeftTypePtr()) + ", " + toObjStr(pPtr->getRightTypePtr()) + ">"; - - return s; -} - -string Tars2Java::tostrStruct(const StructPtr& pPtr) const -{ - return _packagePrefix + tars::TC_Common::replace(pPtr->getSid(), "::", "."); -} - -string Tars2Java::tostrEnum(const EnumPtr& pPtr) const -{ - return "int"; -} - -///////////////////////////////////////////////////////////////////// - -string Tars2Java::MD5(const StructPtr& pPtr) const -{ - string s; - vector& member = pPtr->getAllMemberPtr(); - for (size_t j = 0; j < member.size(); j++) - { - s += "_" + tostr(member[j]->getTypePtr()); - } - - return "\"" + tars::TC_MD5::md5str(s) + "\""; -} - -string Tars2Java::generateJava(const ParamDeclPtr& pPtr) const -{ - ostringstream s; - - TypePtr typePtr = pPtr->getTypeIdPtr()->getTypePtr(); - if (!pPtr->isOut()) - { - s << tostr(typePtr); - } - - s << " " << pPtr->getTypeIdPtr()->getId(); - - return s.str(); -} - -struct SortOperation -{ - bool operator ()(const OperationPtr& o1, const OperationPtr& o2) - { - return o1->getId() < o2->getId(); - } -}; - -string Tars2Java::generateJava(const InterfacePtr& pPtr, const NamespacePtr& nPtr) const -{ - ostringstream s; - generateAndroidStub(pPtr, nPtr); - return s.str(); -} - -string Tars2Java::generateDefautElem(const TypePtr& pPtr, const string& sElemName) const -{ - static int iCount = 0; - ostringstream s; - - if (sElemName.empty()) - { - iCount++; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - int iId = iCount; - string sVar = sElemName.empty() ? "__var_" + tars::TC_Common::tostr(iId) : sElemName; - if (sElemName.empty()) - { - s << TAB << toObjStr(vPtr) << " " << sVar - << " = " << toTypeInit(vPtr) << endl; - } - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (bPtr && bPtr->kind() == Builtin::KindByte) - { - iCount++; - s << TAB << "byte __var_" << tars::TC_Common::tostr(iCount) - << " = 0;" << endl; - - s << TAB << "((" << tostr(vPtr->getTypePtr()) << "[])" << sVar - << ")[0] = __var_" << tars::TC_Common::tostr(iId + 1) << ";" << endl; - } - else if (_bForceArray) - { - s << generateDefautElem(vPtr->getTypePtr(), ""); - s << TAB << "((" << tostr(vPtr->getTypePtr()) << "[])" << sVar - << ")[0] = __var_" << tars::TC_Common::tostr(iId + 1) << ";" << endl; - } - else - { - s << generateDefautElem(vPtr->getTypePtr(), ""); - s << TAB << "((" << tostr(pPtr) << ")" << sVar << ").add(__var_" << tars::TC_Common::tostr(iId + 1) << ");" << endl; - } - - return s.str(); - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) - { - int iId = iCount; - string sVar = sElemName.empty() ? "__var_" + tars::TC_Common::tostr(iId) : sElemName; - - if (sElemName.empty()) - { - s << TAB << toObjStr(mPtr) << " " << sVar - << " = " << toTypeInit(mPtr) << endl; - } - - s << generateDefautElem(mPtr->getLeftTypePtr(), ""); - int iIdRight = iCount + 1; - s << generateDefautElem(mPtr->getRightTypePtr(), ""); - - s << TAB << sVar << ".put(__var_" << tars::TC_Common::tostr(iId + 1) << ", __var_" - << tars::TC_Common::tostr(iIdRight) << ");" << endl; - - return s.str(); - } - - //首个éžmapã€vector调用,ä¸ç”¨ç”Ÿæˆé»˜è®¤å…ƒç´  - if (!sElemName.empty()) - { - return s.str(); - } - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - s << TAB << toObjStr(bPtr) << " __var_" << tars::TC_Common::tostr(iCount) - << " = " << toTypeInit(bPtr) << endl; - return s.str(); - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) - { - s << TAB << toObjStr(sPtr) << " __var_" << tars::TC_Common::tostr(iCount) - << " = " << toTypeInit(sPtr) << endl; - return s.str(); - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - s << TAB << toObjStr(ePtr) << " __var_" << tars::TC_Common::tostr(iCount) - << " = " << toTypeInit(ePtr) << endl; - return s.str(); - } - - return s.str(); -} - - -/******************************StructPtr***************************************/ -string Tars2Java::generateJava(const StructPtr& pPtr, const NamespacePtr& nPtr) const -{ - ostringstream s; - s << g_parse->printHeaderRemark(); - - vector key = pPtr->getKey(); - vector& member = pPtr->getAllMemberPtr(); - - s << TAB << "package " << _packagePrefix << nPtr->getId() << ";" << endl; - s << endl; - - bool bHasImpPrefix = false; - -// if (_bWithWsp) -// { -// s << TAB << "import java.util.HashMap" << ";" << endl; -// s << endl; -// -// s << TAB << "import "<< s_WSP_PACKAGE << ";" << endl; -// s << endl; -// } - -// if (_bJson) -// { -// s << TAB << "import com.qq.component.json.JSON;" << endl; -// s << TAB << "import com.qq.component.json.JSONException;" << endl; -// s << endl; -// } - - s << TAB << "public final class " << pPtr->getId() << " extends " << s_TARS_PACKAGE << ".TarsStruct"; -// if (_bWithWsp) -// { -// s << " implements WspStruct"; -// bHasImpPrefix = true; -// } - - if (key.size() > 0) - { - if (bHasImpPrefix) - { - s << ", Comparable<" << pPtr->getId() << ">"; - } - else - { - s << " implements Comparable<" << pPtr->getId() << ">"; - bHasImpPrefix = true; - } - } - - if (!_bWithCompact) - { - if (bHasImpPrefix) - { - s << ", java.lang.Cloneable"; - } - else - { - s << " implements java.lang.Cloneable"; - } - } - s << endl; - s << TAB << "{" << endl; - INC_TAB; - - if (!_bWithCompact) - { - //生æˆç»“æž„å称 - s << TAB << "public String className()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return " << "\"" << nPtr->getId() << "." << pPtr->getId() << "\"" << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - - // 生æˆå¸¦åŒ…å‰ç¼€çš„结构å - s << TAB << "public String fullClassName()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return " << "\"" << _packagePrefix << nPtr->getId() << "." << pPtr->getId() << "\"" << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - } - -// if (_bWithWsp) -// { -// s << TAB <<"private HashMap arr = new HashMap();"<hasDefault()) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - EnumPtr ePtr = EnumPtr::dynamicCast(member[i]->getTypePtr()); - //string转义 - if (bPtr && bPtr->kind() == Builtin::KindString) - { - sDefalut = tars::TC_Common::replace(member[i]->def(), "\"", "\\\""); - //sDefalut = " = (" + tostr(member[i]->getTypePtr()) + ")\"" + sDefalut + "\""; - sDefalut = " = \"" + sDefalut + "\""; - } - else if (ePtr) - { - - std::string sdef = member[i]->def(); - std::string::size_type pos = sdef.rfind("::"); - if (pos != std::string::npos && pos + 2 < sdef.size()) - { - sdef = sdef.substr(pos + 2); - } - - if (_bEnumCompact) - { - sDefalut = " = " + _packagePrefix + tars::TC_Common::replace(ePtr->getSid(), "::", ".") + "._" + sdef; - } - else - { - sDefalut = " = " + _packagePrefix + tars::TC_Common::replace(ePtr->getSid(), "::", ".") + "." + sdef + ".value()"; - } - } - else - { - //sDefalut = " = (" + tostr(member[i]->getTypePtr()) + ")" + member[i]->def(); - sDefalut = " = " + member[i]->def(); - } - s << TAB << "public " << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() << sDefalut << ";" << endl; - } - else - { - sDefalut = " = " + toTypeInit(member[i]->getTypePtr()); - //s << TAB << "public " << tostr(member[i]->getTypePtr()) << " "<< member[i]->getId() << sDefalut << endl; - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - EnumPtr ePtr = EnumPtr::dynamicCast(member[i]->getTypePtr()); - if ((!bPtr && !ePtr) || (bPtr && bPtr->kind() == Builtin::KindString)) - { - s << TAB << "public " << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() << " = null;" << endl; - } - else - { - s << TAB << "public " << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() << sDefalut << endl; - } - } - s << endl; - } - - if (!_bWithCompact) - { - //æˆå‘˜å˜é‡get/set for java bean - for (size_t i = 0; i < member.size(); i++) - { - string sName = ""; - //支æŒjavabean规范,并且第二个字符是大写 - if (_bWithJbr && member[i]->getId()[1] >= 'A' && member[i]->getId()[1] <= 'Z') - { - sName = member[i]->getId(); - } - else - { - sName = tars::TC_Common::upper(member[i]->getId().substr(0, 1)) + member[i]->getId().substr(1); - } - s << TAB << "public " << tostr(member[i]->getTypePtr()) << " get" << sName - << "()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return " << member[i]->getId() << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - s << TAB << "public void " << " set" << sName << "(" << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "this." << member[i]->getId() << " = " << member[i]->getId() << ";" << endl; -// if (_bWithWsp) -// { -// s << TAB <<"arr.put(\""<getId()<<"\","<< member[i]->getId()<<");"<getId() << "()" << endl; - s << TAB << "{" << endl; - INC_TAB; -// if (_bWithWsp) -// { -// s << TAB << "buildIndex();" << endl; -// } -// else - { -/* - * æ— å‚的够造函数,ä¸ç”¨è°ƒç”¨æˆå‘˜è®¾ç½®æ–¹æ³• - for (size_t i = 0; i < member.size(); i++) - { - string sName = ""; - //支æŒjavabean规范,并且第二个字符是大写 - if (_bWithJbr && member[i]->getId()[1] >= 'A' && member[i]->getId()[1] <= 'Z' ) - { - sName = member[i]->getId(); - } - else - { - sName = tars::TC_Common::upper(member[i]->getId().substr(0, 1)) + member[i]->getId().substr(1); - } - s << TAB <<"set" << sName << "(" << member[i]->getId() << ");" << endl; - } -*/ - } - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //(constructor)(...) - s << TAB << "public " << pPtr->getId() << "("; - for (size_t i = 0; i < member.size(); i++) - { - s << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() - << ((i < member.size() - 1) ? ", " : ""); - } - s << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "this." << member[i]->getId() << " = " << member[i]->getId() << ";" << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //compareTo() - if (key.size() > 0) - { - s << TAB << "public int compareTo(" << pPtr->getId() << " o)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "int[] r = " << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < key.size(); i++) - { - s << TAB << s_TARS_PACKAGE << ".TarsUtil.compareTo(" << key[i] << ", o." - << key[i] << ")" << ((i < key.size() - 1) ? ", " : "") << endl; - } - DEL_TAB; - s << TAB << "};" << endl; - - s << TAB << "for(int i = 0; i < r.length; ++i)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "if(r[i] != 0) return r[i];" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "return 0;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - } - - if (!_bWithCompact) - { - //equals() - s << TAB << "public boolean equals(Object o)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "if(o == null)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return false;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - s << TAB << pPtr->getId() << " t = (" << pPtr->getId() << ") o;" << endl; - s << TAB << "return (" << endl; - - INC_TAB; - - //定义了key - if (key.size() > 0) - { - for (size_t i = 0; i < key.size(); i++) - { - s << TAB << s_TARS_PACKAGE << ".TarsUtil.equals(" << key[i] << ", t." << key[i] << ")" - << ((i < key.size() - 1) ? " && " : " );") << endl; - } - } - else - { - //使用所有元素比较 - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << s_TARS_PACKAGE << ".TarsUtil.equals(" << member[i]->getId() << ", t." << member[i]->getId() << ")" - << ((i < member.size() - 1) ? " && " : " );") << endl; - } - } - DEL_TAB; - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - } - - if (!_bWithCompact) - { - //hashCode() - if (key.size() > 0) - { - s << TAB << "public int hashCode()" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "int [] hc = { " << endl; - INC_TAB; - for (size_t i = 0; i < key.size(); i++) - { - s << TAB << s_TARS_PACKAGE << ".TarsUtil.hashCode(" << key[i] - << ")" << ((i < key.size() - 1) ? ", " : "") << endl; - } - DEL_TAB; - s << TAB << "};" << endl; - s << TAB << "return java.util.Arrays.hashCode(hc);" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - } - else //生æˆå¼‚å¸¸ä»£ç  - { - s << TAB << "public int hashCode()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "try" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "throw new Exception(\"Need define key first!\");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "catch(Exception ex)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "ex.printStackTrace();" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "return 0;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - } - - if (!_bWithCompact) - { - //clone() - s << TAB << "public java.lang.Object clone()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "java.lang.Object o = null;" << endl; - s << TAB << "try" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "o = super.clone();" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "catch(CloneNotSupportedException ex)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "assert false; // impossible" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "return o;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - } - - //writeTo() - s << TAB << "public void writeTo(" << s_TARS_PACKAGE << ".TarsOutputStream _os)" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - EnumPtr ePtr = EnumPtr::dynamicCast(member[i]->getTypePtr()); - if (!member[i]->isRequire() && ((!bPtr && !ePtr) || (bPtr && bPtr->kind() == Builtin::KindString))) - { - s << TAB << "if (null != " << member[i]->getId() << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "_os.write(" << member[i]->getId() << ", " << member[i]->getTag() << ");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - else if (EnumPtr::dynamicCast(member[i]->getTypePtr())) - { - s << TAB << "_os.write(" << member[i]->getId() << ", " << member[i]->getTag() << ");" << endl; - } - else - { - //s << TAB << "_os.write(" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - MapPtr mPtr = MapPtr::dynamicCast(member[i]->getTypePtr()); - VectorPtr vPtr = VectorPtr::dynamicCast(member[i]->getTypePtr()); - - if (!_bCheckDefault || member[i]->isRequire() || (!member[i]->hasDefault() && !mPtr && !vPtr)) - { - s << TAB << "_os.write(" << member[i]->getId() << ", " << member[i]->getTag() << ");" << endl; - } - else - { - string sDefault = member[i]->def(); - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - - if (bPtr && bPtr->kind() == Builtin::KindString) - { - sDefault = "\"" + tars::TC_Common::replace(member[i]->def(), "\"", "\\\"") + "\""; - } - - if (mPtr) - { - s << TAB << "if (" << member[i]->getId() << ".size() > 0)" << endl; - } - else if (vPtr) - { - BuiltinPtr newPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (newPtr && newPtr->kind() == Builtin::KindByte) - { - s << TAB << "if (" << member[i]->getId() << ".length > 0)" << endl; - } - else if (_bForceArray) - { - s << TAB << "if (" << member[i]->getId() << ".length > 0)" << endl; - } - else - { - s << TAB << "if (" << member[i]->getId() << ".size() > 0)" << endl; - } - } - else if (bPtr && bPtr->kind() == Builtin::KindString) - { - s << TAB << "if (!(" << sDefault << ".equals(" << member[i]->getId() << ")))" << endl; - } - else - { - s << TAB << "if (" << member[i]->getId() << " != " << sDefault << ")" << endl; - } - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "_os.write(" << member[i]->getId() << ", " << member[i]->getTag() << ");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - } - } - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - for (size_t i = 0; i < member.size(); i++) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - if (!bPtr) - { - string prefix = "cache_"; - s << TAB << "static " << tostr(member[i]->getTypePtr()) << " cache_" << member[i]->getId() << ";" << endl; - s << TAB << "static {" << endl; - INC_TAB; - s << TAB << prefix << member[i]->getId() << " = " << toTypeInit(member[i]->getTypePtr()) << endl; - s << generateDefautElem(member[i]->getTypePtr(), prefix + member[i]->getId()); - DEL_TAB; - s << TAB << "}" << endl; - } - } - s << endl; - //readFrom() - s << TAB << "public void readFrom(" << s_TARS_PACKAGE << ".TarsInputStream _is)" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - string prefix = ""; - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - EnumPtr ePtr = EnumPtr::dynamicCast(member[i]->getTypePtr()); - if (!bPtr && !ePtr) - { - prefix = "cache_"; - } - //if (!bPtr && !ePtr) - //{ - // prefix = "cache_"; - // //放置默认元素用于识别类型 - // s << TAB <<"if(null == "<getId()<<")"<getId()<<" = " << toTypeInit(member[i]->getTypePtr()) << endl; - // s << generateDefautElem(member[i]->getTypePtr(), prefix+member[i]->getId()); - // DEL_TAB; - // s << TAB <<"}"<getId() << " = " << toTypeInit(member[i]->getTypePtr()) << endl; - // //s << generateDefautElem(member[i]->getTypePtr(), member[i]->getId()); - - //} - - //stringç‰¹æ®Šå¤„ç† - if (bPtr && bPtr->kind() == Builtin::KindString) - { - s << TAB << "this." << member[i]->getId() << " = " - << " _is.readString(" << member[i]->getTag() << ", " << (member[i]->isRequire() ? "true" : "false") << ");" << endl; - } - else - { - s << TAB << "this." << member[i]->getId() << " = (" + tostr(member[i]->getTypePtr()) + ")" - << " _is.read(" << prefix + member[i]->getId() - << ", " << member[i]->getTag() << ", " << (member[i]->isRequire() ? "true" : "false") << ");" << endl; - } - } - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - -// if (_bJson) -// { -// //writeToJsonString -// s << TAB << "public String writeToJsonString() throws JSONException" << endl; -// s << TAB << "{" << endl; -// INC_TAB; -// s << TAB << "return JSON.toJSONString(this);" << endl; -// DEL_TAB; -// s << TAB << "}" << endl; -// s << endl; -// -// //readFromJsonString -// s << TAB << "public void readFromJsonString(String text) throws JSONException" << endl; -// s << TAB << "{" << endl; -// INC_TAB; -// s << TAB << pPtr->getId() << " temp = " << "JSON.parseObject(text, " << pPtr->getId() << ".class);" << endl; -// for (size_t i = 0; i < member.size(); i++) -// { -// s << TAB << "this." << member[i]->getId() << " = temp." << member[i]->getId() << ";" << endl; -// } -// DEL_TAB; -// s << TAB << "}" << endl; -// s << endl; -// } - - if (!_bWithCompact) - { - //display() - s << TAB << "public void display(java.lang.StringBuilder _os, int _level)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << s_TARS_PACKAGE << ".TarsDisplayer _ds = new " << s_TARS_PACKAGE << ".TarsDisplayer(_os, _level);" << endl; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "_ds.display(" << member[i]->getId() - << ", \"" << member[i]->getId() << "\");" << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //displaySimple() -// s << TAB << "public void displaySimple(java.lang.StringBuilder _os, int _level)" << endl; -// s << TAB << "{" << endl; -// INC_TAB; -// s << TAB << s_TARS_PACKAGE << ".TarsDisplayer _ds = new " << s_TARS_PACKAGE << ".TarsDisplayer(_os, _level);" << endl; -// for (size_t i = 0; i < member.size(); i++) -// { -// if(i != member.size() - 1) -// { -// s << TAB << "_ds.displaySimple(" << member[i]->getId() << ", true);" << endl; -// } -// else -// { -// s << TAB << "_ds.displaySimple(" << member[i]->getId() << ", false);" << endl; -// } -// } -// DEL_TAB; -// s << TAB << "}" << endl; -// s << endl; - } - - -// if (_bWithWsp) -// { -// s << TAB << "public Object getFieldByName(String name)" << endl; -// s << TAB << "{" << endl; -// INC_TAB; -// s << TAB <<"return arr.get(name);"<getId()[1] >= 'A' && member[i]->getId()[1] <= 'Z' ) -// { -// sName = member[i]->getId(); -// } -// else -// { -// sName = tars::TC_Common::upper(member[i]->getId().substr(0, 1)) + member[i]->getId().substr(1); -// } -// s << TAB <<"set" << sName << "(" << member[i]->getId() << ");" << endl; -// } -// DEL_TAB; -// s << TAB << "}" << endl; -// } - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - - - string fileJava = getFilePath(nPtr->getId()) + pPtr->getId() + ".java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); - tars::TC_File::save2file(fileJava, s.str()); - - return s.str(); -} - -/******************************EnumPtr***************************************/ -string Tars2Java::generateJava(const EnumPtr& pPtr, const NamespacePtr& nPtr) const -{ - ostringstream s; - s << g_parse->printHeaderRemark(); - - s << TAB << "package " << _packagePrefix << nPtr->getId() << ";" << endl; - s << endl; - - s << TAB << "public final class " << pPtr->getId() << " implements java.io.Serializable" << endl; - s << TAB << "{" << endl; - INC_TAB; - - if (_bEnumCompact) - { - //æˆå‘˜å˜é‡ - vector& member = pPtr->getAllMemberPtr(); - bool bFlag = false; - string sValue; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "public static final int _" << member[i]->getId() << " = "; - if (member[i]->hasDefault()) - { - bFlag = true; - sValue = member[i]->def(); - s << sValue; - } - else - { - if (bFlag == true) - { - assert(i > 0); - string stem = tars::TC_Common::tostr(tars::TC_Common::strto(sValue) + 1); - sValue = stem; - s << stem; - } - else - { - s << tars::TC_Common::tostr(i); - } - } - s << ";" << endl; - } - } - else - { - //æˆå‘˜å˜é‡ - vector& member = pPtr->getAllMemberPtr(); - s << TAB << "private static " << pPtr->getId() - << "[] __values = new " << pPtr->getId() - << "[" << tars::TC_Common::tostr(member.size()) << "];" << endl; - s << TAB << "private int __value;" << endl; - - s << TAB << "private String __T = new String();" << endl; - s << endl; - - bool bFlag = false; - string sValue; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "public static final int _" << member[i]->getId() << " = "; - if (member[i]->hasDefault()) - { - bFlag = true; - sValue = member[i]->def(); - s << sValue; - } - else - { - if (bFlag == true) - { - assert(i > 0); - string stem = tars::TC_Common::tostr(tars::TC_Common::strto(sValue) + 1); - sValue = stem; - s << stem; - } - else - { - s << tars::TC_Common::tostr(i); - } - } - s << ";" << endl; - s << TAB << "public static final " << pPtr->getId() << " " - << member[i]->getId() << " = new " << pPtr->getId() << "(" << i << ",_" - << member[i]->getId() << ",\"" << member[i]->getId() << "\");" << endl; - } - s << endl; - - - //convert(int) - s << TAB << "public static " << pPtr->getId() << " convert(int val)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "for(int __i = 0; __i < __values.length; ++__i)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "if(__values[__i].value() == val)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return __values[__i];" << endl; - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "assert false;" << endl; - s << TAB << "return null;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //convert(String) - s << TAB << "public static " << pPtr->getId() << " convert(String val)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "for(int __i = 0; __i < __values.length; ++__i)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "if(__values[__i].toString().equals(val))" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return __values[__i];" << endl; - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "assert false;" << endl; - s << TAB << "return null;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //value() - s << TAB << "public int value()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return __value;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //toString() - s << TAB << "public String toString()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return __T;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //(constructor)(int) - s << TAB << "private " << pPtr->getId() << "(int index, int val, String s)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "__T = s;" << endl; - s << TAB << "__value = val;" << endl; - s << TAB << "__values[index] = this;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - - string fileJava = getFilePath(nPtr->getId()) + pPtr->getId() + ".java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); - tars::TC_File::save2file(fileJava, s.str()); - - - return s.str(); -} - -/******************************ConstPtr***************************************/ -void Tars2Java::generateJava(const ConstPtr& pPtr, const NamespacePtr& nPtr) const -{ - ostringstream s; - s << g_parse->printHeaderRemark(); - - //const类型的包å•ç‹¬æ”¾åˆ°ä¸€ä¸ªconstç›®å½•ä¸‹é¢ - s << TAB << "package " << _packagePrefix << nPtr->getId() << ".cnst" << ";" << endl; - s << endl; - - s << TAB << "public interface " << pPtr->getTypeIdPtr()->getId() << endl; - s << TAB << "{" << endl; - INC_TAB; - - //TODO: String转义 / long 识别 / byte 范围 - if (pPtr->getConstGrammarPtr()->t == ConstGrammar::STRING) - { - string tmp = tars::TC_Common::replace(pPtr->getConstGrammarPtr()->v, "\"", "\\\""); - s << TAB << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " value = \"" - << tmp << "\";" << endl; - } - else if (tostr(pPtr->getTypeIdPtr()->getTypePtr()) == "long") - { - s << TAB << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " value = " - << pPtr->getConstGrammarPtr()->v << "L;" << endl; - } - else - { - s << TAB << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " value = " - << pPtr->getConstGrammarPtr()->v << ";" << endl; - } - - DEL_TAB; - s << TAB << "}" << endl; - - string fileJava = getFilePath(nPtr->getId()) + "/cnst/" + pPtr->getTypeIdPtr()->getId() + ".java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId() + "/cnst/"), 0755); - tars::TC_File::save2file(fileJava, s.str()); - - return; -} - -/******************************Tars2Java***************************************/ - -void Tars2Java::generateJava(const ContextPtr& pPtr) const -{ - ostringstream s; - - vector namespaces = pPtr->getNamespaces(); - - for (size_t i = 0; i < namespaces.size(); i++) - { - generateJava(namespaces[i]); - } - -} - -void Tars2Java::createFile(const string& file) -{ - std::vector contexts = g_parse->getContexts(); - for (size_t i = 0; i < contexts.size(); i++) - { - if (file == contexts[i]->getFileName()) - { - generateJava(contexts[i]); - } - } -} - -void Tars2Java::setBaseDir(const string& dir) -{ - _baseDir = dir; -} - - -void Tars2Java::setBasePackage(const string& prefix) -{ - _packagePrefix = prefix; - if (_packagePrefix.length() != 0 - && _packagePrefix.substr(_packagePrefix.length() - 1, 1) != ".") - { - _packagePrefix += "."; - } -} - - -string Tars2Java::getFilePath(const string& ns) const -{ - return _baseDir + "/" + tars::TC_Common::replace(_packagePrefix, ".", "/") + "/" + ns + "/"; -} - -// 增加的函数 -static string firstCharUpper(const string& castStr) { - string resultStr = castStr; - if (castStr.size() > 0) { - resultStr[0] = toupper(castStr[0]); - } - return resultStr; -} - -void Tars2Java::generateJava(const NamespacePtr &pPtr) const -{ - vector &is = pPtr->getAllInterfacePtr(); - vector &ss = pPtr->getAllStructPtr(); - vector &es = pPtr->getAllEnumPtr(); - vector &cs = pPtr->getAllConstPtr(); - - InterfaceAnalysis interfaceAnalysis; - - if (_bWithGenerateInterfaceDependencies) { - interfaceAnalysis.analysis(is); - } - - for (size_t i = 0; i < is.size(); i++) - { - generateJava(is[i], pPtr); - } - - TarsFilter romTarsFilter; - std::map needGenerateStructMap; - if (_bWithGenerateInterfaceDependencies) { - if (_bWithFilterRomTars) { - romTarsFilter.filterStructs( - interfaceAnalysis.getAllStructs(), needGenerateStructMap); - } else { - needGenerateStructMap = interfaceAnalysis.getAllStructs(); - } - } - - for (size_t i = 0; i < ss.size(); i++) - { - generateJava(ss[i], pPtr); - needGenerateStructMap.erase(ss[i]->getSid()); - } - for (std::map::const_iterator - it = needGenerateStructMap.begin(); it != needGenerateStructMap.end(); ++it) { - generateJava(it->second, pPtr); - } - - std::map needGenerateEnumMap; - if (_bWithGenerateInterfaceDependencies) { - if (_bWithFilterRomTars) { - romTarsFilter.filterEnums( - interfaceAnalysis.getAllEnums(), needGenerateEnumMap); - } else { - needGenerateEnumMap = interfaceAnalysis.getAllEnums(); - } - } - - for (size_t i = 0; i < es.size(); i++) - { - generateJava(es[i], pPtr); - needGenerateEnumMap.erase(es[i]->getSid()); - } - for (std::map::const_iterator - it = needGenerateEnumMap.begin(); it != needGenerateEnumMap.end(); ++it) { - generateJava(it->second, pPtr); - } - - for (size_t i = 0; i < cs.size(); i++) - { - generateJava(cs[i], pPtr); - } - - return; -} - -// 增加的实现 -string Tars2Java::generateConstructor(const TypePtr& vType) const { - stringstream s; - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vType); - if (bPtr) { - switch (bPtr->kind()) { - case Builtin::KindBool: { - s << "Boolean.valueOf(false)"; - } break; - case Builtin::KindByte: { - s << "Byte.valueOf((byte)0)"; - } break; - case Builtin::KindShort: { - s << "Short.valueOf((short)0)"; - } break; - case Builtin::KindInt: { - s << "Integer.valueOf(0)"; - } break; - case Builtin::KindLong: { - s << "Long.valueOf(0)"; - } break; - case Builtin::KindFloat : { - s << "Float.valueOf((float)0)"; - } break; - case Builtin::KindDouble : { - s << "Double.valueOf((double)0)"; - } break; - default:{ - s << "new " << toObjStr(vType) << "()"; - } break; - } - } else { - s << "new " << toObjStr(vType) << "()"; - } - - return s.str(); -} - -string Tars2Java::generateAndroidJavaParams( - const vector& vParamDecl - , bool needParamType - , bool needOutParam) const { - ostringstream s; - - bool needDot = false; - for (size_t index = 0; index < vParamDecl.size(); ++index) { - ParamDeclPtr paramDeclPtr = vParamDecl[index]; - - if (!paramDeclPtr->isOut()) { - if (!needDot) { - needDot = true; - } else { - s << ", "; - } - if (needParamType) { - s << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) << " "; - } - s << "in" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()); - } else if (needOutParam){ - if (!needDot) { - needDot = true; - } else { - s << ", "; - } - if (needParamType) { - s << "WupOutWrapper<" << toObjStr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) << "> "; - } - s<< "out" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()); - } - } - return s.str(); -} - -string Tars2Java::generateAndroidStub(const InterfacePtr &pPtr, const NamespacePtr &nPtr) const { - ostringstream s; - - s << g_parse->printHeaderRemark(); - s << TAB << "package " << _packagePrefix << nPtr->getId() << ";" << endl; - - s << endl; - s << TAB << "import java.util.concurrent.Semaphore;" << endl; - s << TAB << "import com.tars.wupagent.api.WupConstants.WUP_ERROR_CODE;" << endl; - s << TAB << "import com.tars.wupagent.api.*;" << endl; - - s << TAB << "import com.tars.wup.UniPacket;" << endl; - s << endl; - - s << TAB << "public class " << firstCharUpper(pPtr->getId()) << "Agent extends WupAgent {" << endl << endl; - INC_TAB; - - s << TAB << "public " << firstCharUpper(pPtr->getId()) << "Agent(String servantName) {" << endl; - INC_TAB; - s << TAB << "super(servantName);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - string sParams; - vector& vOperation = pPtr->getAllOperationPtr(); - std::sort(vOperation.begin(), vOperation.end(), SortOperation()); - - for (size_t i = 0; i < vOperation.size(); i++) - { - vector& vParamDecl = vOperation[i]->getAllParamDeclPtr(); - - TypeIdPtr& returnType = vOperation[i]->getReturnPtr(); - bool isReturnTypeVoid = false; - if (!returnType->getTypePtr()) { - isReturnTypeVoid = true; - } - - - // generate Result - s << TAB << "public static class " << firstCharUpper(vOperation[i]->getId()) << "Result extends WupBaseResult " << "{" << endl; - - INC_TAB; - - s << TAB << "public " << firstCharUpper(vOperation[i]->getId()) << "Result() {" << endl; - s << TAB << "}" << endl << endl; - - s << TAB << "public " << firstCharUpper(vOperation[i]->getId()) << "Result(int errorCode, String errorMsg) {" << endl; - INC_TAB; - s << TAB << "super(errorCode, errorMsg);" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - for (size_t paramIndex = 0; paramIndex < vParamDecl.size(); paramIndex++) { - - ParamDeclPtr paramDeclPtr = vParamDecl[paramIndex]; - - string inOrOutPrefix = "in"; - if (paramDeclPtr->isOut()) { - inOrOutPrefix = "out"; - } - - s << TAB << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) << " "; - s << inOrOutPrefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << ";" << endl; - - s << TAB << "public " << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) - << " get" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "() {" << endl; - INC_TAB; - - s << TAB << "return "; - s << inOrOutPrefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << ";" << endl; - - DEL_TAB; - - s << TAB << "}" << endl; - - - s << TAB << "public " << firstCharUpper(vOperation[i]->getId()) << "Result" - << " set" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "(" << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) - << " " << paramDeclPtr->getTypeIdPtr()->getId() - << ") {" << endl; - INC_TAB; - s << TAB << "this." << inOrOutPrefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << " = " << paramDeclPtr->getTypeIdPtr()->getId() << ";" << endl; - s << TAB << "return this;" << endl; - DEL_TAB; - - s << TAB << "}" << endl << endl; - - } - - if (!isReturnTypeVoid) { - s << TAB << tostr(returnType->getTypePtr()) << " ret;" << endl; - s << TAB << "public " << tostr(returnType->getTypePtr()) - << " getRet() {" << endl; - INC_TAB; - s << TAB << " return ret;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "public " << firstCharUpper(vOperation[i]->getId()) << "Result" - << " setRet(" << tostr(returnType->getTypePtr()) << " ret) {" << endl; - INC_TAB; - s << TAB << "this.ret = ret;" << endl; - s << TAB << "return this;" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - } - - DEL_TAB; - - s << TAB << "}" << endl << endl; - - s << TAB << "public static interface I" << firstCharUpper(vOperation[i]->getId()) << "Callback {" << endl; - INC_TAB; - s << TAB << "public void on" << firstCharUpper(vOperation[i]->getId()) << "Callback(" - << firstCharUpper(vOperation[i]->getId()) << "Result result);" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - // generate BaseMethod - - s << TAB << "private static abstract class " - << firstCharUpper(vOperation[i]->getId()) << "BaseMethod extends AsyncWupMethod {" << endl; - INC_TAB; - - s << TAB << "public " << firstCharUpper(vOperation[i]->getId()) - << "BaseMethod(String servantName, WupOption wupOption) {" << endl; - INC_TAB; - s << TAB << "super(servantName, \"" << vOperation[i]->getId() << "\", wupOption);" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - for (size_t paramIndex = 0; paramIndex < vParamDecl.size(); paramIndex++) { - ParamDeclPtr paramDeclPtr = vParamDecl[paramIndex]; - - string inOrOutPrefix = "in"; - bool generateSet = true; - if (paramDeclPtr->isOut()) { - inOrOutPrefix = "out"; - generateSet = false; - } - - s << TAB << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) << " "; - s << inOrOutPrefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << ";" << endl; - - s << TAB << "public " << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) - << " get" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "() {" << endl; - INC_TAB; - - s << TAB << "return "; - s << inOrOutPrefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << ";" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - if (generateSet) { - - s << TAB << "public void set" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "(" << tostr(paramDeclPtr->getTypeIdPtr()->getTypePtr()) - << " " << paramDeclPtr->getTypeIdPtr()->getId() - << ") {" << endl; - INC_TAB; - s << TAB << "this." << inOrOutPrefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << " = " << paramDeclPtr->getTypeIdPtr()->getId() << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - - s << endl; - } - - if (!isReturnTypeVoid) { - s << TAB << tostr(returnType->getTypePtr()) << " ret;" << endl; - s << TAB << "public " << tostr(returnType->getTypePtr()) - << " getRet() {" << endl; - INC_TAB; - s << TAB << " return ret;" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - } - - s << TAB << "@Override" << endl; - s << TAB << "protected void fillReqUniPacket(UniPacket reqPacket) {" << endl; - INC_TAB; - for (size_t paramIndex = 0; paramIndex < vParamDecl.size(); paramIndex++) { - ParamDeclPtr paramDeclPtr = vParamDecl[paramIndex]; - - if (!paramDeclPtr->isOut()) { - s << TAB << "reqPacket.put(\"" << paramDeclPtr->getTypeIdPtr()->getId() - << "\", in" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << ");" << endl; - } - } - DEL_TAB; - s << TAB << "}" << endl <getTypePtr()) << ");" << endl; - } - - for (size_t paramIndex = 0; paramIndex < vParamDecl.size(); paramIndex++) { - ParamDeclPtr paramDeclPtr = vParamDecl[paramIndex]; - if (paramDeclPtr->isOut()) { - s << TAB << "out" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << " = respPacket.get(\"" << paramDeclPtr->getTypeIdPtr()->getId() - << "\", " << generateConstructor(paramDeclPtr->getTypeIdPtr()->getTypePtr()) << ");" << endl; - } - } - - DEL_TAB; - s << TAB << "}" << endl <getId()) - << "Method extends " << firstCharUpper(vOperation[i]->getId()) << "BaseMethod {" << endl; - INC_TAB; - s << TAB << "private I" << firstCharUpper(vOperation[i]->getId()) - << "Callback m" << firstCharUpper(vOperation[i]->getId()) << "Callback;" << endl; - - s << TAB << "public Async" << firstCharUpper(vOperation[i]->getId()) << "Method(String servantName" - << ", WupOption wupOption" - << ", I" << firstCharUpper(vOperation[i]->getId()) - << "Callback " << vOperation[i]->getId() << "Callback) {" << endl; - INC_TAB; - s << TAB << "super(servantName, wupOption);" << endl; - s << TAB << "this.m" << firstCharUpper(vOperation[i]->getId()) << "Callback = " - << vOperation[i]->getId() << "Callback;" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - s << TAB << "@Override" << endl; - s << TAB << "protected void onError(int errorCode, String errorMsg) {" << endl; - INC_TAB; - s << TAB << firstCharUpper(vOperation[i]->getId()) - << "Result result = new " << firstCharUpper(vOperation[i]->getId()) << "Result(errorCode, errorMsg);" << endl; - s << TAB << "result.setRequestId(getRequestId());" << endl; - s << TAB << "m" << firstCharUpper(vOperation[i]->getId()) - << "Callback.on" << firstCharUpper(vOperation[i]->getId()) << "Callback(result);" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - s << TAB << "@Override" << endl; - s << TAB << "protected void onFinished() {" << endl; - INC_TAB; - s << TAB << firstCharUpper(vOperation[i]->getId()) - << "Result result = new " << firstCharUpper(vOperation[i]->getId()) << "Result();" << endl; - s << TAB << "result.setRequestId(getRequestId());" << endl; - for (size_t paramIndex = 0; paramIndex < vParamDecl.size(); paramIndex++) { - ParamDeclPtr paramDeclPtr = vParamDecl[paramIndex]; - - s << TAB << "result.set" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "(get" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << "());" << endl; - } - if (!isReturnTypeVoid) { - s << TAB << "result.setRet(getRet());" << endl; - } - s << TAB << "m" << firstCharUpper(vOperation[i]->getId()) - << "Callback.on" << firstCharUpper(vOperation[i]->getId()) << "Callback(result);" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - DEL_TAB; - s << TAB << "}" << endl << endl; - - // generate AsyncMethod end - - // generate SyncMethod - s << TAB << "private static class Sync" << firstCharUpper(vOperation[i]->getId()) - << "Method extends " << firstCharUpper(vOperation[i]->getId()) << "BaseMethod {" << endl; - INC_TAB; - s << TAB << "private Semaphore mSemaphore;" << endl; - s << TAB << "private WupException mWupException;" << endl; - - s << TAB << "public Sync" << firstCharUpper(vOperation[i]->getId()) - << "Method(String servantName, WupOption wupOption) {" << endl; - INC_TAB; - s << TAB << "super(servantName, wupOption);" << endl; - s << TAB << "mSemaphore = new Semaphore(0);" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - s << TAB << "public void waitResponse() throws InterruptedException {" << endl; - INC_TAB; - s << TAB << "mSemaphore.acquire();" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - s << TAB << "@Override" << endl; - s << TAB << "protected void handleFinished() {" << endl; - INC_TAB; - s << TAB << "mSemaphore.release();" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - s << TAB << "@Override" << endl; - s << TAB << "protected void handleError(final int errorCode, final String errorMsg) {" << endl; - INC_TAB; - s << TAB << "mWupException = new WupException(errorCode, errorMsg);" << endl; - s << TAB << "mSemaphore.release();" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - s << TAB << "public WupException getWupException() {" << endl; - INC_TAB; - s << TAB << "return mWupException;" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - DEL_TAB; - s << TAB << "}" << endl << endl; - - // generate SyncMethod end - - // async api - s << TAB << "public WupHandle async_" << vOperation[i]->getId() << "("; - if (vParamDecl.size()) - { - s << generateAndroidJavaParams(vParamDecl, true, false) << ", "; - } - s << "I" << firstCharUpper(vOperation[i]->getId()) << "Callback " - << vOperation[i]->getId() << "Callback) throws WupException { " << endl; - INC_TAB; - s << TAB << "if (" << vOperation[i]->getId() << "Callback == null) {" << endl; - INC_TAB; - s << TAB << "throw new IllegalArgumentException(\"" << vOperation[i]->getId() - << "Callback should not be null\");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "WupOption asyncWupOption = new WupOption(wupOption);" << endl; - - for (size_t index = 0; index < vParamDecl.size(); ++index) { - ParamDeclPtr paramDeclPtr = vParamDecl[index]; - - if (!paramDeclPtr->isOut() && isObjType(paramDeclPtr->getTypeIdPtr()->getTypePtr())) { - s << TAB << "if (in" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << " == null) {" << endl; - INC_TAB; - s << TAB << "throw new IllegalArgumentException(\"in" - << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << " should not be null\");" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - } - - } - - s << TAB << "Async" << firstCharUpper(vOperation[i]->getId()) << "Method " - << vOperation[i]->getId() << "Method = new Async" - << firstCharUpper(vOperation[i]->getId()) - << "Method(getServantName(), asyncWupOption, " << vOperation[i]->getId() << "Callback);" << endl; - - for (size_t index = 0; index < vParamDecl.size(); ++index) { - ParamDeclPtr paramDeclPtr = vParamDecl[index]; - - if (!paramDeclPtr->isOut()) { - s << TAB << vOperation[i]->getId() << "Method.set" - << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "(in" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << ");" << endl; - } - - } - - s << TAB << vOperation[i]->getId() << "Method.start();" << endl; - s << TAB << "return new WupHandle(" << vOperation[i]->getId() << "Method);" << endl; - - DEL_TAB; - s << TAB << "}" << endl << endl; - - // async api end - - // sync api - s << TAB << "public " << tostr(returnType->getTypePtr()) - << " " << vOperation[i]->getId() << "(" - << generateAndroidJavaParams(vParamDecl, true, true) - << ") throws WupException {" << endl; - INC_TAB; - - for (size_t index = 0; index < vParamDecl.size(); ++index) { - ParamDeclPtr paramDeclPtr = vParamDecl[index]; - - string prefix = "in"; - if (paramDeclPtr->isOut()) { - prefix = "out"; - } - if (isObjType(paramDeclPtr->getTypeIdPtr()->getTypePtr())) { - s << TAB << "if (" << prefix << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << " == null) {" << endl; - INC_TAB; - s << TAB << "throw new IllegalArgumentException(\"" << prefix - << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) << " should not be null\");" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - } - - } - - s << TAB << "Sync" << firstCharUpper(vOperation[i]->getId()) - << "Method " << "sync" << firstCharUpper(vOperation[i]->getId()) - << "Method = " << "new Sync" - << firstCharUpper(vOperation[i]->getId()) - << "Method(getServantName(), wupOption);" - << endl; - - for (size_t index = 0; index < vParamDecl.size(); ++index) { - ParamDeclPtr paramDeclPtr = vParamDecl[index]; - - if (!paramDeclPtr->isOut()) { - s << TAB << "sync" << firstCharUpper(vOperation[i]->getId()) - << "Method.set" - << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "(in" - << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << ");" << endl; - } - - } - - s << TAB << "sync" << firstCharUpper(vOperation[i]->getId()) - << "Method.start();" << endl << endl; - s << TAB << "try {" << endl; - INC_TAB; - s << TAB << "sync" << firstCharUpper(vOperation[i]->getId()) - << "Method.waitResponse();" << endl; - DEL_TAB; - s << TAB << "} catch (InterruptedException e) {" << endl; - INC_TAB; - s << TAB << "throw new WupException(WUP_ERROR_CODE.WUP_INNER_ERROR, e.getMessage());" - << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - s << TAB << "if (sync" << firstCharUpper(vOperation[i]->getId()) - << "Method.getWupException() != null) {" << endl; - INC_TAB; - s << TAB << "throw sync" << firstCharUpper(vOperation[i]->getId()) - << "Method.getWupException();" << endl; - DEL_TAB; - s << TAB << "}" << endl << endl; - - for (size_t index = 0; index < vParamDecl.size(); ++index) { - ParamDeclPtr paramDeclPtr = vParamDecl[index]; - - if (paramDeclPtr->isOut()) { - s << TAB << "out" << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << ".setOut" - << "(sync" << firstCharUpper(vOperation[i]->getId()) << "Method.get" - << firstCharUpper(paramDeclPtr->getTypeIdPtr()->getId()) - << "());" << endl; - } - } - - if (!isReturnTypeVoid) { - s << TAB << "return sync" << firstCharUpper(vOperation[i]->getId()) - << "Method.getRet();" << endl; - } - - DEL_TAB; - s << TAB << "}" << endl << endl; - // sync api end - } - - DEL_TAB; - s << TAB << "}" << endl << endl; - - - string fileJava = getFilePath(nPtr->getId()) + pPtr->getId() + "Agent.java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); - tars::TC_File::save2file(fileJava, s.str()); - - return s.str(); -} - - diff --git a/cpp/tools/tars2android/tars2android.h b/cpp/tools/tars2android/tars2android.h deleted file mode 100644 index 42258d11d..000000000 --- a/cpp/tools/tars2android/tars2android.h +++ /dev/null @@ -1,384 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef _TARS2ANDROID_H -#define _TARS2ANDROID_H - -#include "parse.h" - -#include -#include - -#define TARS_PACKAGE ".tup.tars" -#define PROXY_PACKAGE ".proxy" -#define WUP_PACKAGE ".tup" - -/** - * æ ¹æ®tars生æˆjava文件 - * 包括结构的编解ç ä»¥åŠç”ŸæˆProxyå’ŒServant - */ -class Tars2Java -{ -public: - Tars2Java(); - - /** - * 设置代ç ç”Ÿæˆçš„根目录 - * @param dir - */ - void setBaseDir(const string& dir); - - /** - * 设置包å‰ç¼€ - * @param prefix - */ - void setBasePackage(const string& prefix); - - /** - * 默认将éžbyteçš„vector转list 加上该å‚æ•°åŽè½¬æ•°ç»„ - */ - void setForceArray(bool bArray) { _bForceArray = bArray; } - - void setCheckDefault(bool bCheck) { _bCheckDefault = bCheck; } - - /*支æŒè‡ªå®šä¹‰å­—符集*/ - void setCharset(string charset) { _sCharset = charset; } - /*支æŒjavabean规范*/ - void setWithJbr(bool bJbr) { _bWithJbr = bJbr;} - /*去掉属性的read write方法〠cloneableã€equalsã€hashCodeã€cloneã€display*/ - void setWithCompact(bool bCompact) { _bWithCompact = bCompact;} - - /*生æˆæžšä¸¾ç±»æ—¶ï¼Œé‡‡ç”¨intæ–¹å¼å®žçŽ°*/ - void setEnumCompact(bool bCompact) { _bEnumCompact = bCompact;} - - /*tars与json互相转æ¢*/ - void setJsonSupport(bool bJson) { _bJson = bJson;} - - void setWithGenerateInterfaceDependencies(bool bWithGenerateInterfaceDependencies) - { - _bWithGenerateInterfaceDependencies = bWithGenerateInterfaceDependencies; - } - void setWithFilterRomTars(bool bWithFilterRomTars) - { - _bWithFilterRomTars = bWithFilterRomTars; - } - -// 增加的接å£å®šä¹‰ - string generateConstructor(const TypePtr& vType) const; - string generateAndroidJavaParams(const vector& vParamDecl, bool needParamType, bool needOutParam) const; - string generateAndroidStub(const InterfacePtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * ç”Ÿæˆ - * @param file - * @param isFramework 是å¦æ˜¯æ¡†æž¶ - */ - void createFile(const string& file); - - /** - * 设置TAF库的报å - */ - void setTafPacket(const std::string& sPacket) - { - s_TARS_PACKAGE = sPacket + TARS_PACKAGE; - s_PROXY_PACKAGE = sPacket + PROXY_PACKAGE; - s_WUP_PACKAGE = sPacket + WUP_PACKAGE; - } - -protected: - /** - * æ ¹æ®å‘½å空间获å–文件路径 - * @param ns 命å空间 - * - * @return string - */ - string getFilePath(const string& ns) const; - - string _packagePrefix; - string _baseDir; - bool _bForceArray; - bool _bCheckDefault; - string _sCharset; - bool _bWithJbr; - bool _bWithCompact; - bool _bEnumCompact; - bool _bJson; - - bool _bWithGenerateInterfaceDependencies; - bool _bWithFilterRomTars; - -protected: - - /** - * 生æˆæŸç±»åž‹çš„解ç æºç  - * @param pPtr - * - * @return string - */ - string writeTo(const TypeIdPtr& pPtr) const; - - /** - * 生æˆæŸç±»åž‹çš„ç¼–ç æºç  - * @param pPtr - * - * @return string - */ - string readFrom(const TypeIdPtr& pPtr) const; - - /** - * - * 这个函数目å‰æ˜¯ç”¨ä¸åˆ°çš„ - * @param pPtr - * - * @return string - */ - string display(const TypeIdPtr& pPtr) const; - - //下é¢æ˜¯ç±»åž‹æè¿°çš„æºç ç”Ÿæˆ -protected: - - /* - * 生æˆæŸç±»åž‹çš„åˆå§‹åŒ–字符串 - * @param pPtr - * - * @return string - */ - string toTypeInit(const TypePtr& pPtr) const; - - /** - * 生æˆæŸç±»åž‹çš„对应对象的字符串æè¿°æºç  - * @param pPtr - * - * @return string - */ - string toObjStr(const TypePtr& pPtr) const; - - /** - * 判断是å¦æ˜¯å¯¹è±¡ç±»åž‹ - */ - bool isObjType(const TypePtr& pPtr) const; - - /** - * 生æˆæŸç±»åž‹çš„字符串æè¿°æºç  - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr& pPtr) const; - - /** - * 生æˆå†…建类型的字符串æºç  - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr& pPtr) const; - /** - * 生æˆvector的字符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr& pPtr) const; - - /** - * 生æˆmap的字符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr& pPtr, bool bNew = false) const; - - /** - * 生æˆæŸç§ç»“构的符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr& pPtr) const; - - /** - * 生æˆæŸç§æžšä¸¾çš„符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrEnum(const EnumPtr& pPtr) const; - - /** - * 生æˆç±»åž‹å˜é‡çš„解ç æºç  - * @param pPtr - * - * @return string - */ - string decode(const TypeIdPtr& pPtr) const; - - /** - * 生æˆç±»åž‹å˜é‡çš„ç¼–ç æºç  - * @param pPtr - * - * @return string - */ - string encode(const TypeIdPtr& pPtr) const; - - //以下是hå’Œjavaæ–‡ä»¶çš„å…·ä½“ç”Ÿæˆ -protected: - /** - * 结构的md5 - * @param pPtr - * - * @return string - */ - string MD5(const StructPtr& pPtr) const; - - /** - * 生æˆç»“æž„çš„Holder类,用于引用传递 - * @param pPtr - * @param nPtr - * - * @return string - */ - string generateHolder(const StructPtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 放置默认元素用于识别map/list类型 - * @param pPtr - * @param sElemName 元素å称 - * - * @return string - */ - string generateDefautElem(const TypePtr& pPtr, const string& sElemName) const; - - /** - * 生æˆç»“æž„çš„java文件内容 - * @param pPtr - * - * @return string - */ - string generateJava(const StructPtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆå®¹å™¨çš„javaæºç  - * @param pPtr - * - * @return string - */ - string generateJava(const ContainerPtr& pPtr) const; - - /** - * 生æˆå‚数声明的java文件内容 - * @param pPtr - * - * @return string - */ - string generateJava(const ParamDeclPtr& pPtr) const; - - /** - * 生æˆä½œæ“作的proxyçš„java文件内容 - * @param pPtr - * @param cn - * - * @return string - */ - string generateJava(const OperationPtr& pPtr, const string& cn) const; - - /** - * 生æˆæ“作java文件函数调用分å‘çš„æºç  - * @param pPtr - * @param cn - * - * @return string - */ - string generateDispatchJava(const OperationPtr& pPtr, const string& cn) const; - - /** - * 生æˆæŽ¥å£çš„java文件的æºç  - * @param pPtr - * @param nPtr - * - * @return string - */ - string generateJava(const InterfacePtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆProxy接å£çš„java文件的æºç  - * @param pPtr - * @param nPtr - * - * @return string - */ - string generatePrx(const InterfacePtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆProxy帮助类的java文件的æºç  - * @param pPtr - * @param nPtr - * - * @return string - */ - string generatePrxHelper(const InterfacePtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆProxy回调类的java文件的æºç  - * @param pPtr - * @param nPtr - * - * @return string - */ - string generatePrxCallback(const InterfacePtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆæžšä¸¾çš„头文件æºç  - * @param pPtr - * - * @return string - */ - string generateJava(const EnumPtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆå¸¸é‡javaæºç  - * @param pPtr - * - * @return string - */ - void generateJava(const ConstPtr& pPtr, const NamespacePtr& nPtr) const; - - /** - * 生æˆå字空间java文件æºç  - * @param pPtr - * - * @return string - */ - void generateJava(const NamespacePtr& pPtr) const; - - /** - * 生æˆæ¯ä¸ªtars文件的java文件æºç  - * @param pPtr - * - * @return string - */ - void generateJava(const ContextPtr& pPtr) const; - -private: - std::string s_TARS_PACKAGE; - std::string s_PROXY_PACKAGE; - std::string s_HOLDER_PACKAGE; - std::string s_SERVER_PACKAGE; - std::string s_WUP_PACKAGE; -}; - -#endif - - diff --git a/cpp/tools/tars2android/tars_filter.cpp b/cpp/tools/tars2android/tars_filter.cpp deleted file mode 100644 index 6e1d215eb..000000000 --- a/cpp/tools/tars2android/tars_filter.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "tars_filter.h" - -TarsFilter::TarsFilter() { - addSid("TRom::E_ROM_DEVICE_TYPE"); - addSid("TRom::EAPNTYPE"); - addSid("TRom::EIPType"); - addSid("TRom::ELOGINRET"); - addSid("TRom::ENETTYPE"); - addSid("TRom::IPListReq"); - addSid("TRom::IPListRsp"); - addSid("TRom::JoinIPInfo"); - addSid("TRom::LoginReq"); - addSid("TRom::LoginRsp"); - addSid("TRom::RomBaseInfo"); - addSid("TRom::SECPROXY_RETCODE"); - addSid("TRom::SecureReq"); - addSid("TRom::SecureRsp"); -} - -void TarsFilter::addSid(const std::string& sid) { - mFilterTarsSidMap.insert(std::pair(sid, true)); -} - -void TarsFilter::filterStructs( - const std::map& orignalMap - , std::map& resultMap) const { - for (std::map::const_iterator it = orignalMap.begin() - ; it != orignalMap.end(); ++it) { - if (mFilterTarsSidMap.find(it->first) != mFilterTarsSidMap.end()) { - continue; - } - resultMap.insert(std::pair(it->first, it->second)); - } -} - -void TarsFilter::filterEnums( - const std::map& orignalMap - , std::map& resultMap) const { - for (std::map::const_iterator it = orignalMap.begin() - ; it != orignalMap.end(); ++it) { - if (mFilterTarsSidMap.find(it->first) != mFilterTarsSidMap.end()) { - continue; - } - resultMap.insert(std::pair(it->first, it->second)); - } -} diff --git a/cpp/tools/tars2android/tars_filter.h b/cpp/tools/tars2android/tars_filter.h deleted file mode 100644 index 969328811..000000000 --- a/cpp/tools/tars2android/tars_filter.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef TARS_FILTER_H_WILEY -#define TARS_FILTER_H_WILEY - -#include "parse.h" -#include - -class TarsFilter { -public: - TarsFilter(); - - void filterStructs(const std::map& orignalMap - , std::map& resultMap) const; - void filterEnums(const std::map& orignalMap - , std::map& resultMap) const; - -private: - void addSid(const std::string& sid); - - std::map mFilterTarsSidMap; -}; - - - -#endif /* TARS_FILTER_H_ */ diff --git a/cpp/tools/tars2c/CMakeLists.txt b/cpp/tools/tars2c/CMakeLists.txt deleted file mode 100644 index 57d433214..000000000 --- a/cpp/tools/tars2c/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -tars2language("tars2c") diff --git a/cpp/tools/tars2c/main.cpp b/cpp/tools/tars2c/main.cpp deleted file mode 100644 index cd8faa0e8..000000000 --- a/cpp/tools/tars2c/main.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_common.h" -#include "tars2c.h" - - -void usage() -{ - cout << "Usage : tars2c [OPTION] tarsfile" << endl; - cout << " --dir=DIRECTORY generate source file to DIRECTORY(Éú³ÉÎļþµ½Ä¿Â¼DIRECTORY,ĬÈÏΪµ±Ç°Ä¿Â¼)" << endl; - cout << " --check-default<> Èç¹ûoptional×Ö¶ÎֵΪĬÈÏÖµ£¬²»´ò°ü(ĬÈϲ»´ò°ü)(value:true or false)" << endl; - cout << " tars2c support type: bool byte short int long float double vector map" << endl; - exit(0); -} - -void check(vector &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - - - Tars2C j2c; - if (option.hasParam("dir")) - { - j2c.setBaseDir(option.getValue("dir")); - } - else - { - j2c.setBaseDir("."); - } - - j2c.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true); - try - { - //ÊÇ·ñ¿ÉÒÔÒÔtars¿ªÍ· - g_parse->setTars(option.hasParam("with-tars")); - g_parse->setHeader(option.getValue("header")); - - for(size_t i = 0; i < vTars.size(); i++) - { - - g_parse->parse(vTars[i]); - j2c.createFile(vTars[i]); - } - }catch(exception& e) - { - cerr< - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -////////////////////////////////////////////////////////////////////////////////// -// -string Tars2C::writeTo(const TypeIdPtr &pPtr,const string& namespaceId) const -{ - ostringstream s; - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); - - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "ret = TarsOutputStream_writeInt32(os, (Int32)tars_st->" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else if (toClassName(VectorPtr::dynamicCast(pPtr->getTypePtr())) == "list" ) - { - s << TAB << "ret = TarsOutputStream_writeVectorChar(os, tars_st->" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else if (sPtr) - { - s << TAB << "ret = TarsOutputStream_writeStruct(os, tars_st->" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else if (m_bCheckDefault == false || pPtr->isRequire() || (!pPtr->hasDefault() && !mPtr && !vPtr)) - { - s << TAB << "ret = TarsOutputStream_write" << toFuncName(pPtr->getTypePtr()) << "(os, tars_st->" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else - { - std::string sCheckCond = "true"; - - if (mPtr) - { - sCheckCond = "JMapWrapper_size(tars_st->" + pPtr->getId() + ") > 0"; - } - - if (vPtr) - { - sCheckCond = "JArray_size(tars_st->" + pPtr->getId() + ") > 0"; - } - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr && bPtr->kind() == Builtin::KindString && pPtr->hasDefault()) - { - string tmp = tars::TC_Common::replace(pPtr->def(), "\"", "\\\""); - sCheckCond = "JString_size(tars_st->" + pPtr->getId() + ") != strlen(\"" + tmp + "\") || strncmp(JString_data(tars_st->" + pPtr->getId() + "), \"" + tmp + "\", JString_size(tars_st->" + pPtr->getId() + ")) != 0"; - } - else if (pPtr->hasDefault()) - { - sCheckCond = "tars_st->" + pPtr->getId() + " != " + pPtr->def(); - } - - s << TAB << "if (" << sCheckCond << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "ret = TarsOutputStream_write" << toFuncName(pPtr->getTypePtr()) << "(os, tars_st->" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - - - s << TAB <<"if (TARS_SUCCESS != ret) return ret;" << endl; - s <getTypePtr()); - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - if (type == "String" || type == "Vector" || type == "Map" || type == "Struct") - { - isPtr = true; - } - - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "ret = TarsInputStream_readInt32(is, & tars_st->" << pPtr->getId(); - } - else if (toClassName(VectorPtr::dynamicCast(pPtr->getTypePtr())) == "list" ) - { - s << TAB << "ret = TarsInputStream_readVectorChar(is, tars_st->" << pPtr->getId(); - } - else if (sPtr) - { - s << TAB << "ret = TarsInputStream_readStruct(is, tars_st->" << pPtr->getId(); - } - else - { - s << TAB << "ret = TarsInputStream_read" << toFuncName(pPtr->getTypePtr()) << "(is, "<< (isPtr ? "" : "&") <<"tars_st->" << pPtr->getId(); - } - - s << ", " << pPtr->getTag() << ", " << ((pPtr->isRequire() && bIsRequire)?"true":"false") << ");"<kind()) - { - case Builtin::KindBool: s = "bool"; break; - case Builtin::KindByte: s = "char"; break; - case Builtin::KindShort: s = "short"; break; - case Builtin::KindInt: s = "int32"; break; - case Builtin::KindLong: s = "int64"; break; - case Builtin::KindFloat: s = "float"; break; - case Builtin::KindDouble: s = "double"; break; - case Builtin::KindString: s = "string"; break; - default: assert(false); break; - } - - return s; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return TC_Common::replace(sPtr->getSid(), "::", "."); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - - -string Tars2C::toFuncName(const TypePtr &pPtr) const -{ - string s; - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool: s = "Bool"; break; - case Builtin::KindByte: s = "Char"; break; - case Builtin::KindShort: s = bPtr->isUnsigned()?"UInt8" :"Short"; break; - case Builtin::KindInt: s = bPtr->isUnsigned()?"UInt16":"Int32"; break; - case Builtin::KindLong: s = bPtr->isUnsigned()?"UInt32":"Int64"; break; - case Builtin::KindFloat: s = "Float"; break; - case Builtin::KindDouble: s = "Double"; break; - case Builtin::KindString: s = "String"; break; - default: assert(false); break; - } - - return s; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return "Vector"; - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return "Map"; - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return "Struct"; - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - - -/*******************************BuiltinPtr********************************/ -string Tars2C::tostr(const TypePtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - if (toClassName(vPtr) == "list") - return "JString * "; - else - return "JArray * "; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return "JMapWrapper * "; - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - - if (sPtr) return tostrStruct(sPtr)+ " *"; - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return "Int32"; - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - -string Tars2C::tostrBuiltin(const BuiltinPtr &pPtr) const -{ - string s; - - switch (pPtr->kind()) - { - case Builtin::KindBool: - s = "Bool"; - break; - case Builtin::KindByte: - s = "Char"; - break; - case Builtin::KindShort: - s = (pPtr->isUnsigned()?"UInt8":"Short"); - break; - case Builtin::KindInt: - s = (pPtr->isUnsigned()?"UInt16":"Int32"); - break; - case Builtin::KindLong: - s = (pPtr->isUnsigned()?"UInt32":"Int64"); - break; - case Builtin::KindFloat: - s = "Float"; - break; - case Builtin::KindDouble: - s = "Double"; - break; - case Builtin::KindString: - s = "JString * "; - break; - default: - assert(false); - break; - } - - return s; -} -/*******************************VectorPtr********************************/ -string Tars2C::tostrVector(const VectorPtr &pPtr) const -{ - string s = string("list<") + toClassName(pPtr->getTypePtr()); - - if (MapPtr::dynamicCast(pPtr->getTypePtr()) || VectorPtr::dynamicCast(pPtr->getTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} -/*******************************MapPtr********************************/ -string Tars2C::tostrMap(const MapPtr &pPtr) const -{ - string s = string("map<") + toClassName(pPtr->getLeftTypePtr()) + "," + toClassName(pPtr->getRightTypePtr()); - if (MapPtr::dynamicCast(pPtr->getRightTypePtr()) || VectorPtr::dynamicCast(pPtr->getRightTypePtr())) - { - s += ">"; - } - else - { - s += ">"; - } - return s; -} - -/*******************************StructPtr********************************/ -string Tars2C::tostrStruct(const StructPtr &pPtr) const -{ - return TC_Common::replace(pPtr->getSid(), "::", "_"); -} - -string Tars2C::MD5(const StructPtr &pPtr) const -{ - string s; - vector& member = pPtr->getAllMemberPtr(); - for (size_t j = 0; j < member.size(); j++) - { - s += "_" + tostr(member[j]->getTypePtr()); - } - - return "\"" + tars::TC_MD5::md5str(s) + "\""; -} - -///////////////////////////////////////////////////////////////////// -string Tars2C::tostrEnum(const EnumPtr &pPtr) const -{ - return TC_Common::replace(pPtr->getSid(), "::", "_"); -} -/////////////////////////////////////////////////////////////////////// -string Tars2C::generateH(const StructPtr &pPtr, const string& namespaceId) const -{ - ostringstream s; - string sStructName = namespaceId + "_" + pPtr->getId(); - - s << TAB << "typedef struct " << sStructName << " " << sStructName << ";" << endl; - s << TAB << "struct " << sStructName << endl; - s << TAB << "{" << endl; - //s << TAB << "public:" << endl; - - INC_TAB; - - s << TAB << "char * className;" << endl; - s << TAB << "Int32 (*writeTo)( const "<& member = pPtr->getAllMemberPtr(); - for (size_t j = 0; j < member.size(); j++) - { - s << TAB << tostr(member[j]->getTypePtr()) << " " << member[j]->getId() << ";" << endl; - } - - - s << endl; - - DEL_TAB; - s << TAB << "};" << endl; - s << endl; - - //////////////////////////////////////////////////////////// - s << TAB << "Int32 " << sStructName << "_writeTo(const " << sStructName << "* tars_st, TarsOutputStream * os);" << endl; - s << TAB << "Int32 " << sStructName << "_readFrom(" << sStructName << "* tars_st, TarsInputStream *is);" << endl; - s << TAB << "void " << sStructName << "_del(" << sStructName << " ** handle);" << endl; - s << TAB << "Int32 " << sStructName << "_init(" << sStructName << " * handle);" << endl; - s << TAB << sStructName << " * " << sStructName << "_new(void);" << endl; - s << TAB <getId(); - - //¶¨Òå³ÉÔ±±äÁ¿ - vector& member = pPtr->getAllMemberPtr(); - - //////////////////////////////////////////////////////////// - s << TAB << "Int32 " << sStructName << "_writeTo(const " << sStructName << "* tars_st, TarsOutputStream * os)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "Int32 ret=0;" << endl; - - for (size_t j = 0; j < member.size(); j++) - { - s << writeTo(member[j],namespaceId); - } - - s << endl; - s << TAB << "return TARS_SUCCESS;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - /////////////////////////////////////////////////////////// - s << endl; - s << TAB << "Int32 " << sStructName << "_readFrom(" << sStructName << "* tars_st, TarsInputStream *is)" << endl;; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "Int32 ret=0;" << endl; - - for (size_t j = 0; j < member.size(); j++) - { - s << readFrom(member[j],true,namespaceId); - } - - s << endl; - s << TAB << "return TARS_SUCCESS;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - s << TAB << "void " << sStructName << "_del(" << sStructName << " ** handle)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << sStructName << " ** this = (" << sStructName << "**)handle;" << endl; - - for (size_t j = 0; j < member.size(); j++) - { - string type = toFuncName(member[j]->getTypePtr()); - if (type == "String") - s << TAB << "if((*this)->" << member[j]->getId() << ") JString_del(&(*this)->" << member[j]->getId() << ");" << endl; - else if (type == "Vector") - if (toClassName(member[j]->getTypePtr()) == "list") - s << TAB << "if((*this)->" << member[j]->getId() << ") JString_del(&(*this)->" << member[j]->getId() << ");" << endl; - else - s << TAB << "if((*this)->" << member[j]->getId() << ") JArray_del(&(*this)->" << member[j]->getId() << ");" << endl; - else if (type == "Map") - s << TAB << "if((*this)->" << member[j]->getId() << ") JMapWrapper_del(&(*this)->" << member[j]->getId() << ");" << endl; - else if (type == "Struct") - { - StructPtr sPtr = StructPtr::dynamicCast(member[j]->getTypePtr()); - if (sPtr) - s << TAB << "if((*this)->" << member[j]->getId() << ") "<< namespaceId + "_" + sPtr->getId()<<"_del(&(*this)->" << member[j]->getId() << ");" << endl; - } - - } - s << TAB << "if((*this)->className) TarsFree((*this)->className);" << endl; - s << TAB << "TarsFree(*this);" << endl; - s << TAB << "*this = NULL;" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - s << TAB << "Int32 " << sStructName << "_init(" << sStructName << " * handle)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << sStructName << " * this = (" << sStructName << "*) handle;" << endl; - s << endl; - - s << TAB << "char * tarsClassName = \"" << namespaceId << "." << pPtr->getId() << "\";" << endl; - s << endl; - - s << TAB << "this->className = TarsMalloc(strlen(tarsClassName)+1);" << endl; - s << TAB << "this->writeTo = "<< sStructName << "_writeTo;" <readFrom = "<< sStructName << "_readFrom;" << endl; - s << endl; - - string sCleanCondition = "!this->className "; - for (size_t j = 0; j < member.size(); j++) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[j]->getTypePtr()); - //stringֵҪתÒå - if (bPtr && bPtr->kind() == Builtin::KindString) - { - s << TAB << "this->" << member[j]->getId() << " = JString_new();" << endl; - sCleanCondition += " || !this->" + member[j]->getId(); - continue; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(member[j]->getTypePtr()); - if (vPtr) - { - if (toClassName(vPtr) == "list") - { - s << TAB << "this->" << member[j]->getId() << " = JString_new();" << endl; - sCleanCondition += " || !this->" + member[j]->getId(); - } - else - { - s << TAB << "this->" << member[j]->getId() << " = JArray_new(\"" - << toClassName(vPtr->getTypePtr()) << "\");" << endl; - sCleanCondition += " || !this->" + member[j]->getId(); - } - continue; - } - - MapPtr mPtr = MapPtr::dynamicCast(member[j]->getTypePtr()); - if (mPtr) - { - s << TAB << "this->" << member[j]->getId() << " = JMapWrapper_new(\"" - << toClassName(mPtr->getLeftTypePtr()) << "\", \"" << toClassName(mPtr->getRightTypePtr()) << "\");" << endl; - sCleanCondition += " || !this->" + member[j]->getId(); - continue; - } - - StructPtr sPtr = StructPtr::dynamicCast(member[j]->getTypePtr()); - if (sPtr) - { - s << TAB << "this->" << member[j]->getId() << " = "<< namespaceId + "_" + sPtr->getId()+"_new();" << endl; - sCleanCondition += " || !this->" + member[j]->getId(); - continue; - } - - if (tostr(member[j]->getTypePtr()) == "Int64") - { - s << "#ifdef __MTK_64"<" << member[j]->getId() << ".high = " << member[j]->def() << ";" << endl; - s << TAB << "this->" << member[j]->getId() << ".low = " << member[j]->def() << ";" << endl; - s << "#else"<" << member[j]->getId() << " = " << member[j]->def() << ";" << endl; - s << "#endif"<hasDefault()) - { - s << TAB << "this->" << member[j]->getId() << " = " << member[j]->def() << ";" << endl; - } - } - s << endl; - - s << TAB << "if(" << sCleanCondition << ")" << endl; - s << TAB << "{"<className, tarsClassName, strlen(tarsClassName)+1);" << endl; - s << endl; - - //string default - for (size_t j = 0; j < member.size(); j++) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[j]->getTypePtr()); - //stringֵҪתÒå - if (bPtr && bPtr->kind() == Builtin::KindString) - { - if (member[j]->hasDefault()) - { - string tmp = tars::TC_Common::replace(member[j]->def(), "\"", "\\\""); - s << TAB << "JString_assign(this->" << member[j]->getId() << ", \"" - << tmp << "\", strlen(\"" << tmp << "\"));" << endl; - } - continue; - } - } - s << endl; - - s << TAB << "return TARS_SUCCESS;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - - s << TAB << sStructName << " * " << sStructName << "_new()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "Int32 ret=0;" << endl; - s << TAB << sStructName << " *this = (" << sStructName << " *)TarsMalloc(sizeof("<< sStructName << "));" << endl; - s << TAB << "if(!this) return NULL;" << endl; - s << TAB << "ret = " << sStructName << "_init(this);" << endl; - s << TAB << "if(TARS_SUCCESS != ret) { return NULL; }" << endl; - s << TAB << "return this;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - return s.str(); -} - - -/*******************************ContainerPtr********************************/ -string Tars2C::generateH(const ContainerPtr &pPtr) const -{ - ostringstream s; - for (size_t i = 0; i < pPtr->getAllNamespacePtr().size(); i++) - { - s << generateH(pPtr->getAllNamespacePtr()[i]) << endl; - s << endl; - } - return s.str(); -} - -/******************************EnumPtr***************************************/ - -string Tars2C::generateH(const EnumPtr &pPtr, const string& namespaceId) const -{ - ostringstream s; - s << TAB << "enum " << namespaceId << "_" << pPtr->getId() << endl; - s << TAB << "{" << endl; - INC_TAB; - vector& member = pPtr->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << member[i]->getId(); - if (member[i]->hasDefault()) - { - s << " = " << member[i]->def(); - } - s << (i==member.size()-1 ? "" : ",") << endl; - } - DEL_TAB; - s << TAB << "};" << endl; - - return s.str(); -} - -/******************************ConstPtr***************************************/ -string Tars2C::generateH(const ConstPtr &pPtr) const -{ - ostringstream s; - - if (pPtr->getConstGrammarPtr()->t == ConstGrammar::STRING) - { - string tmp = tars::TC_Common::replace(pPtr->getConstGrammarPtr()->v, "\"", "\\\""); - s << TAB << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " " << pPtr->getTypeIdPtr()->getId() << " = \"" << tmp << "\";"<< endl; - } - else - { - s << TAB << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " " << pPtr->getTypeIdPtr()->getId() << " = " << pPtr->getConstGrammarPtr()->v - << ((tostr(pPtr->getTypeIdPtr()->getTypePtr()) == "tars::Int64") ? "LL;" : ";" )<< endl; - } - - return s.str(); -} -/******************************NamespacePtr***************************************/ - -string Tars2C::generateH(const NamespacePtr &pPtr) const -{ - ostringstream s; - vector &ss = pPtr->getAllStructPtr(); - vector &es = pPtr->getAllEnumPtr(); - - s << endl; - - for (size_t i = 0; i < es.size(); i++) - { - s << generateH(es[i], pPtr->getId()) << endl; - } - - for (size_t i = 0; i < ss.size(); i++) - { - s << generateH(ss[i], pPtr->getId()) << endl; - } - - s << endl; - - return s.str(); -} - - -string Tars2C::generateC(const NamespacePtr &pPtr) const -{ - ostringstream s; - vector &ss = pPtr->getAllStructPtr(); - s << endl; - for (size_t i = 0; i < ss.size(); i++) - { - s << generateC(ss[i], pPtr->getId()) << endl; - } - s << endl; - return s.str(); -} - - -/******************************Tars2C***************************************/ - -void Tars2C::generateH(const ContextPtr &pPtr) const -{ - string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())); - - string fileH = m_sBaseDir + "/" + n + ".h"; - - string define = tars::TC_Common::upper("__" + n + "_h_"); - - ostringstream s; - - s << g_parse->printHeaderRemark(); - - s << "#ifndef " << define << endl; - s << "#define " << define << endl; - s << endl; - s << "#include \"Tars_c.h\"" << endl; - - vector include = pPtr->getIncludes(); - for (size_t i = 0; i < include.size(); i++) - { - s << "#include \"" << g_parse->getHeader() << tars::TC_File::extractFileName(include[i]) << "\"" << endl; - } - - vector namespaces = pPtr->getNamespaces(); - - s << endl; - - for (size_t i = 0; i < namespaces.size(); i++) - { - s << generateH(namespaces[i]) << endl; - } - - s << endl; - s << "#endif" << endl; - - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); - tars::TC_File::save2file(fileH, s.str()); -} - -void Tars2C::generateC(const ContextPtr &pPtr) const -{ - string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())); - string fileC = m_sBaseDir + "/" + n + ".c"; - - string define = tars::TC_Common::upper("__" + n + "_h_"); - - ostringstream s; - - s << g_parse->printHeaderRemark(); - - vector namespaces = pPtr->getNamespaces(); - - s << "#include \"" << g_parse->getHeader() << n << ".h\"" << endl; - s << endl; - - for (size_t i = 0; i < namespaces.size(); i++) - { - s << generateC(namespaces[i]) << endl; - } - - s << endl; - - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); - tars::TC_File::save2file(fileC, s.str()); - -} - - -void Tars2C::createFile(const string &file) -{ - std::vector contexts = g_parse->getContexts(); - for (size_t i = 0; i < contexts.size(); i++) - { - if (file == contexts[i]->getFileName()) - { - generateH(contexts[i]); - generateC(contexts[i]); - } - } -} - -StructPtr Tars2C::findStruct(const ContextPtr &pPtr,const string &id) -{ - string sid = id; - - //ÔÚµ±Ç°namespaceÖвéÕÒ - vector namespaces = pPtr->getNamespaces(); - for (size_t i = 0; i < namespaces.size(); i++) - { - NamespacePtr np = namespaces[i]; - vector structs = np->getAllStructPtr(); - - for (size_t i = 0; i < structs.size(); i++) - { - if (structs[i]->getSid() == sid) - { - return structs[i]; - } - } - } - - return NULL; -} - - diff --git a/cpp/tools/tars2c/tars2c.h b/cpp/tools/tars2c/tars2c.h deleted file mode 100644 index 1a42457b5..000000000 --- a/cpp/tools/tars2c/tars2c.h +++ /dev/null @@ -1,348 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef _TARS2C_H -#define _TARS2C_H - -#include "parse.h" - -#include -#include - -using namespace tars; - -/** - * ¸ù¾ÝtarsÉú³ÉcÎļþ - * °üÀ¨½á¹¹µÄ±à½âÂëÒÔ¼°Éú³ÉProxyºÍServant - */ -class Tars2C -{ -public: - - /** - * Éú³É - * @param file - */ - void createFile(const string &file); - - - /** - * ÉèÖÃÉú³ÉÎļþµÄĿ¼ - * - */ - void setBaseDir(const std::string & sPath) { m_sBaseDir = sPath; } - - - /** - * ÉèÖÃÊÇ·ñ¼ì²éĬÈÏÖµ - * - * @author kevintian (2010-10-8) - * - * @param bCheck - */ - void setCheckDefault(const bool bCheck) { m_bCheckDefault = bCheck; } - - //ÏÂÃæÊDZà½âÂëµÄÔ´ÂëÉú³É -protected: - - /** - * Éú³ÉijÀàÐ͵ĽâÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string writeTo(const TypeIdPtr &pPtr,const string& namespaceId = "") const; - - /** - * Éú³ÉijÀàÐ͵ıàÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string readFrom(const TypeIdPtr &pPtr, bool bIsRequire = true,const string& namespaceId="") const; - - /** - * - * @param pPtr - * - * @return string - */ - string display(const TypeIdPtr &pPtr) const; - - /** - * - * @param pPtr - * - * @return string - */ - string displaySimple(const TypeIdPtr &pPtr, bool bSep) const; - - //ÏÂÃæÊÇÀàÐÍÃèÊöµÄÔ´ÂëÉú³É -protected: - /** - * Éú³ÉijÀàÐ͵Ä×Ö·û´®ÃèÊöÔ´Âë - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr &pPtr) const; - - /** - * Éú³ÉÄÚ½¨ÀàÐ͵Ä×Ö·û´®Ô´Âë - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr &pPtr) const; - /** - * Éú³ÉvectorµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr &pPtr) const; - - /** - * Éú³ÉmapµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr &pPtr) const; - - /** - * Éú³ÉijÖֽṹµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr &pPtr) const; - - - /** - * Éú³ÉijÖÖö¾ÙµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrEnum(const EnumPtr &pPtr) const; - - /** - * Éú³É½Ó¿ÚÃû³Æ - * @param pPtr - * - * @return string - */ - - string toFuncName(const TypePtr &pPtr) const; - - string toClassName(const TypePtr &pPtr) const; - /** - * Éú³ÉÀàÐͱäÁ¿µÄ½âÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string decode(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉÀàÐͱäÁ¿µÄ±àÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string encode(const TypeIdPtr &pPtr) const; - - //ÒÔÏÂÊÇhºÍcÎļþµÄ¾ßÌåÉú³É -protected: - /** - * ½á¹¹µÄmd5 - * @param pPtr - * - * @return string - */ - string MD5(const StructPtr &pPtr) const; - - /** - * Éú³É½á¹¹µÄÍ·ÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateH(const StructPtr &pPtr, const string& namespaceId) const; - - /** - * Éú³É½á¹¹µÄcppÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateC(const StructPtr &pPtr, const string& namespaceId) const; - - /** - * Éú³ÉÈÝÆ÷µÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const ContainerPtr &pPtr) const; - - /** - * Éú³ÉÈÝÆ÷µÄcÔ´Âë - * @param pPtr - * - * @return string - */ - string generateCpp(const ContainerPtr &pPtr) const; - - /** - * Éú³É²ÎÊýÉùÃ÷µÄÍ·ÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateH(const ParamDeclPtr &pPtr) const; - - /** - * - * @param pPtr - * - * @return string - */ - string generateOutH(const ParamDeclPtr &pPtr) const; - - /** - * - * @param pPtr - * @param cn - * - * @return string - */ - //string generateAsyncResponseCpp(const OperationPtr &pPtr, const string &cn) const; - - /** - * Éú³É²ÎÊýÉùÃ÷µÄcppÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateCpp(const ParamDeclPtr &pPtr) const; - - /** - * Éú³É×÷²Ù×÷µÄproxyµÄcppÎļþÄÚÈÝ - * @param pPtr - * @param cn - * - * @return string - */ - string generateCpp(const OperationPtr &pPtr, const string &cn) const; - - /** - * Éú³É²Ù×÷µÄservantµÄÍ·ÎļþÔ´Âë - * @param pPtr - * @param bVirtual - * - * @return string - */ - string generateH(const OperationPtr &pPtr, bool bVirtual, const string& interfaceId) const; - - /** - * Éú³É½Ó¿ÚµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const InterfacePtr &pPtr) const; - - /** - * ÉúµÄ½Ó¿ÚµÄcppÎļþµÄÔ´Âë - * @param pPtr - * - * @return string - */ - string generateCpp(const InterfacePtr &pPtr, const NamespacePtr &nPtr) const; - - /** - * Éú³Éö¾ÙµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const EnumPtr &pPtr, const string& namespaceId) const; - - /** - * Éú³É³£Á¿Í·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const ConstPtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼äµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const NamespacePtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼äcppÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateC(const NamespacePtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼äµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generateH(const ContextPtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼äcppÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generateC(const ContextPtr &pPtr) const; - - /** - * - * ¸ù¾ÝÃû×Ö²éÕҽṹ - * @param pPtr - * @param id - * - * @return StructPtr - */ - StructPtr findStruct(const ContextPtr &pPtr,const string &id); - - std::string m_sBaseDir; - - - /** - * ÊÇ·ñ¼ì²éĬÈÏÖµ - * - * @author kevintian (2010-10-8) - */ - bool m_bCheckDefault; -}; - -#endif - - diff --git a/cpp/tools/tars2cpp/CMakeLists.txt b/cpp/tools/tars2cpp/CMakeLists.txt deleted file mode 100644 index e57b3f39b..000000000 --- a/cpp/tools/tars2cpp/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -tars2language("tars2cpp") diff --git a/cpp/tools/tars2cpp/main.cpp b/cpp/tools/tars2cpp/main.cpp deleted file mode 100644 index 09e5aaa45..000000000 --- a/cpp/tools/tars2cpp/main.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_common.h" -#include "tars2cpp.h" - - -void usage() -{ - cout << "Usage : tars2cpp [OPTION] tarsfile" << endl; - cout << " --coder=Demo::interface1;Demo::interface2 create interface encode and decode api" << endl; - cout << " --dir=DIRECTORY generate source file to DIRECTORY(生æˆæ–‡ä»¶åˆ°ç›®å½•DIRECTORY,默认为当å‰ç›®å½•)" << endl; - cout << " --check-default= 如果optional字段值为默认值ä¸æ‰“包(默认打包)" << endl; - cout << " --os åªç”Ÿæˆtars文件中结构体编解ç çš„程åºæ®µ" << endl; - cout << " --include=\"dir1;dir2;dir3\" 设置tars文件æœç´¢è·¯å¾„" << endl; - cout << " --unknown 生æˆå¤„ç†tarsæ•°æ®æµä¸­çš„unkown field的代ç " << endl; - cout << " --tarsMaster 生æˆèŽ·å–主调信æ¯çš„选项" << endl; - cout << " --currentPriority use current path first." << endl; - cout << " tars2cpp support type: bool byte short int long float double vector map" << endl; - exit(0); -} - -void check(vector &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - bool bCoder = option.hasParam("coder"); - vector vCoder; - if(bCoder) - { - vCoder = tars::TC_Common::sepstr(option.getValue("coder"), ";", false); - if(vCoder.size() == 0) - { - usage(); - return 0; - } - } - - Tars2Cpp t2c; - - if (option.hasParam("dir")) - { - t2c.setBaseDir(option.getValue("dir")); - } - else - { - t2c.setBaseDir("."); - } - - t2c.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true); - - t2c.setOnlyStruct(option.hasParam("os")); - - t2c.setTarsMaster(option.hasParam("tarsMaster")); - - try - { - //增加includeæœç´¢è·¯å¾„ - g_parse->addIncludePath(option.getValue("include")); - - //是å¦å¯ä»¥ä»¥tars开头 - g_parse->setTars(option.hasParam("with-tars")); - g_parse->setHeader(option.getValue("header")); - g_parse->setCurrentPriority(option.hasParam("currentPriority")); - - t2c.setUnknownField(option.hasParam("unknown")); - for(size_t i = 0; i < vTars.size(); i++) - { - - g_parse->parse(vTars[i]); - t2c.createFile(vTars[i], vCoder); - } - } - catch(exception& e) - { - cerr< - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -////////////////////////////////////////////////////////////////////////////////// -// -Tars2Cpp::Tars2Cpp() -: _checkDefault(false) -, _onlyStruct(false) -, _namespace("tars") -, _unknownField(false) -, _tarsMaster(false) -{ - -} - -string Tars2Cpp::writeTo(const TypeIdPtr& pPtr) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "_os.write((" + _namespace + "::Int32)" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else if (pPtr->getTypePtr()->isArray()) - { - s << TAB << "_os.write((const " << tostr(pPtr->getTypePtr()) << " *)" << pPtr->getId() << ", " << pPtr->getId() << "Len" << ", " << pPtr->getTag() << ");" << endl; - } - else if (pPtr->getTypePtr()->isPointer()) - { - s << TAB << "_os.write((const " << tostr(pPtr->getTypePtr()) << ")" << pPtr->getId() << ", " << pPtr->getId() << "Len" << ", " << pPtr->getTag() << ");" << endl; - } - else - { - MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - - if (!_checkDefault || pPtr->isRequire() || (!pPtr->hasDefault() && !mPtr && !vPtr)) - { - s << TAB << "_os.write(" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - } - else - { - string sDefault = pPtr->def(); - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr && bPtr->kind() == Builtin::KindString) - { - sDefault = "\"" + tars::TC_Common::replace(pPtr->def(), "\"", "\\\"") + "\""; - } - - if (mPtr || vPtr) - { - s << TAB << "if (" << pPtr->getId() << ".size() > 0)" << endl; - } - else - { - s << TAB << "if (" << pPtr->getId() << " != " << sDefault << ")" << endl; - } - - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "_os.write(" << pPtr->getId() << ", " << pPtr->getTag() << ");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - } - - return s.str(); -} - -string Tars2Cpp::readFrom(const TypeIdPtr& pPtr, bool bIsRequire) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - //枚举强制类型转æ¢åœ¨O2编译选项情况下会告警 - string tmp = _namespace + "::Int32 eTemp" + TC_Common::tostr(pPtr->getTag()) + generateInitValue(pPtr); - - s << TAB << tmp << endl; - s << TAB << "_is.read(eTemp" << TC_Common::tostr(pPtr->getTag()); - } - else if (pPtr->getTypePtr()->isArray()) - { - s << TAB << "_is.read(" << pPtr->getId() << ", " << getSuffix(pPtr) << ", " << pPtr->getId() << "Len"; - } - else if (pPtr->getTypePtr()->isPointer()) - { - s << TAB << pPtr->getId() << " = (" << tostr(pPtr->getTypePtr()) << ")_is.cur();" << endl; - s << TAB << "_is.read(" << pPtr->getId() << ", _is.left(), " << pPtr->getId() << "Len"; - } - else - { - s << TAB << "_is.read(" << pPtr->getId(); - } - - s << ", " << pPtr->getTag() << ", " << ((pPtr->isRequire() && bIsRequire) ? "true" : "false") << ");" << endl; - - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << pPtr->getId() << " = (" << tostr(pPtr->getTypePtr()) << ")eTemp" << TC_Common::tostr(pPtr->getTag()) << ";" << endl; - } - - if(pPtr->getTypePtr()->isPointer()) - s << TAB <<"_is.mapBufferSkip("<getId() << "Len);"<getTag() << ");" << endl; - return s.str(); -} -string Tars2Cpp::writeUnknown() const -{ - ostringstream s; - s << TAB << "_os.writeUnknown(sUnknownField);" << endl; - return s.str(); -} - -string Tars2Cpp::display(const TypeIdPtr& pPtr) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "_ds.display((" + _namespace + "::Int32)" << pPtr->getId() << ",\"" << pPtr->getId() << "\");" << endl;; - } - else if (pPtr->getTypePtr()->isArray() || pPtr->getTypePtr()->isPointer()) - { - s << TAB << "_ds.display(" << pPtr->getId() << ", " << pPtr->getId() << "Len" << ",\"" << pPtr->getId() << "\");" << endl; - } - else - { - s << TAB << "_ds.display(" << pPtr->getId() << ",\"" << pPtr->getId() << "\");" << endl;; - } - - return s.str(); -} - -string Tars2Cpp::displaySimple(const TypeIdPtr& pPtr, bool bSep) const -{ - ostringstream s; - if (EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "_ds.displaySimple((" + _namespace + "::Int32)" << pPtr->getId() << ", " - << (bSep ? "true" : "false") << ");" << endl; - } - else if (pPtr->getTypePtr()->isArray()) - { - s << TAB << "_ds.displaySimple(" << pPtr->getId() << ", " << pPtr->getId() << "Len" << "," - << (bSep ? "true" : "false") << ");" << endl; - } - else if (pPtr->getTypePtr()->isPointer()) - { - s << TAB << "_ds.displaySimple("; - s << (bSep ? "" : ("(const " + tostr(pPtr->getTypePtr()) + ")")); - s << pPtr->getId() << ", " << pPtr->getId() << "Len" << "," << (bSep ? "true" : "false") << ");" << endl; - } - else - { - s << TAB << "_ds.displaySimple(" << pPtr->getId() << ", " - << (bSep ? "true" : "false") << ");" << endl; - } - - return s.str(); -} - - -string Tars2Cpp::generateCollection(const TypeIdPtr& pPtr, bool bSep) const -{ - ostringstream s; - if (pPtr->getTypePtr()->isArray()) - { - s << TAB << "_jj.generateCollection(" << "\"" << pPtr->getId() << "\"" << ", " << pPtr->getId() << ", " << pPtr->getId() << "Len" - << ");" << "\n" << (bSep ? TAB + "_jj.append(\",\", false);" : "") << endl; - } - else - { - s << TAB << "_jj.generateCollection(" << "\"" << pPtr->getId() << "\"" << ", " << pPtr->getId() - << ");" << "\n" << (bSep ? TAB + "_jj.append(\",\", false);" : "") << endl; - } - - return s.str(); -} - - -/*******************************获å–定长数组åæ ‡********************************/ -int Tars2Cpp::getSuffix(const TypeIdPtr& pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr && bPtr->kind() == Builtin::KindString && bPtr->isArray()) - { - return bPtr->getSize(); - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if (vPtr && vPtr->isArray()) - { - return vPtr->getSize(); - } - - return -1; -} - -/*******************************定长数组åæ ‡********************************/ - -string Tars2Cpp::toStrSuffix(const TypeIdPtr& pPtr) const -{ - ostringstream s; - - int i = getSuffix(pPtr); - - if (i >= 0) - { - s << "[" << i << "]"; - } - return s.str(); -} -/*******************************BuiltinPtr********************************/ - -string Tars2Cpp::tostr(const TypePtr& pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return tostrStruct(sPtr); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - -string Tars2Cpp::tostrBuiltin(const BuiltinPtr& pPtr) const -{ - string s; - - switch (pPtr->kind()) - { - case Builtin::KindBool: - s = _namespace + "::Bool"; - break; - case Builtin::KindByte: - s = _namespace + "::Char"; - break; - case Builtin::KindShort: - //为了兼容javaæ— unsigned, 编结ç æ—¶æŠŠtars问件中 unsigned char 对应到short - //c++中需è¦è¿˜åŽŸå›žæ¥ - s = (pPtr->isUnsigned() ? _namespace + "::UInt8" : _namespace + "::Short"); - break; - case Builtin::KindInt: - s = (pPtr->isUnsigned() ? _namespace + "::UInt16" : _namespace + "::Int32"); - break; - case Builtin::KindLong: - s = (pPtr->isUnsigned() ? _namespace + "::" + "UInt32" : _namespace + "::Int64"); - break; - case Builtin::KindFloat: - s = _namespace + "::Float"; - break; - case Builtin::KindDouble: - s = _namespace + "::Double"; - break; - case Builtin::KindString: - if(pPtr->isArray()) - s = _namespace+ "::Char"; //char a [8]; - else - s = "std::string";//string a; - break; - case Builtin::KindVector: - s = "std::vector"; - break; - case Builtin::KindMap: - s = "std::map"; - break; - default: - assert(false); - break; - } - - return s; -} -/*******************************VectorPtr********************************/ -string Tars2Cpp::tostrVector(const VectorPtr& pPtr) const -{ - //数组类型 - if (pPtr->isArray()) - { - return tostr(pPtr->getTypePtr()); - } - - //指针类型 - if (pPtr->isPointer()) - { - return tostr(pPtr->getTypePtr()) + " *"; - } - - string s = Builtin::builtinTable[Builtin::KindVector] + string("<") + tostr(pPtr->getTypePtr()); - - if (MapPtr::dynamicCast(pPtr->getTypePtr()) || VectorPtr::dynamicCast(pPtr->getTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} -/*******************************MapPtr********************************/ -string Tars2Cpp::tostrMap(const MapPtr& pPtr) const -{ - string s = Builtin::builtinTable[Builtin::KindMap] + string("<") + tostr(pPtr->getLeftTypePtr()) + ", " + tostr(pPtr->getRightTypePtr()); - if (MapPtr::dynamicCast(pPtr->getRightTypePtr()) || VectorPtr::dynamicCast(pPtr->getRightTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} - -/*******************************StructPtr********************************/ -string Tars2Cpp::tostrStruct(const StructPtr& pPtr) const -{ - return pPtr->getSid(); -} - -string Tars2Cpp::MD5(const StructPtr& pPtr) const -{ - string s; - vector& member = pPtr->getAllMemberPtr(); - for (size_t j = 0; j < member.size(); j++) - { - s += "_" + tostr(member[j]->getTypePtr()); - } - - return "\"" + tars::TC_MD5::md5str(s) + "\""; -} - -///////////////////////////////////////////////////////////////////// -string Tars2Cpp::tostrEnum(const EnumPtr& pPtr) const -{ - return pPtr->getSid(); -} -/////////////////////////////////////////////////////////////////////// -string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) const -{ - ostringstream s; - - s << TAB << "struct " << pPtr->getId() << " : public " + _namespace + "::TarsStructBase" << endl; - s << TAB << "{" << endl; - - s << TAB << "public:" << endl; - - INC_TAB; - - s << TAB << "static string className()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return " << "\"" << namespaceId << "." << pPtr->getId() << "\"" << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "static string MD5()" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return " << MD5(pPtr) << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - //////////////////////////////////////////////////////////// - //定义缺çœæž„造函数 - s << TAB << pPtr->getId() << "()" << endl; - - vector& member = pPtr->getAllMemberPtr(); - bool b = false; - //定义åˆå§‹åŒ–列表 - for (size_t j = 0; j < member.size(); j++) - { - - if (member[j]->getTypePtr()->isArray()) - { - if (!b) s << TAB << ":"; - else s << ","; - s << member[j]->getId() << "Len(0)"; - b = true; - continue; - } - - if (member[j]->getTypePtr()->isPointer()) - { - if (!b) s << TAB << ":"; - else s << ","; - s << member[j]->getId() << "Len(0)," << member[j]->getId() << "(NULL)"; - b = true; - continue; - } - - if (member[j]->hasDefault()) - { - if (!b) s << TAB << ":"; - else s << ","; - - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[j]->getTypePtr()); - //string值è¦è½¬ä¹‰ - if (bPtr && bPtr->kind() == Builtin::KindString) - { - string tmp = tars::TC_Common::replace(member[j]->def(), "\"", "\\\""); - s << member[j]->getId() << "(\"" << tmp << "\")"; - } - else - { - s << member[j]->getId() << "(" << member[j]->def() << ")"; - } - b = true; - } - else - { //没有æä¾›åˆå§‹å€¼æ‰ä¼šèµ°åˆ°è¿™é‡Œ,æ供枚举类型åˆå§‹åŒ–值 - EnumPtr ePtr = EnumPtr::dynamicCast(member[j]->getTypePtr()); - if (ePtr) - { - vector& eMember = ePtr->getAllMemberPtr(); - if (eMember.size() > 0) - { - if (!b) s << TAB << ":"; - else s << ","; - string sid = ePtr->getSid(); - s << member[j]->getId() << "(" << sid.substr(0, sid.find_first_of("::")) << "::" << eMember[0]->getId() << ")"; - b = true; - } - } - } - } - - if (b) - { - if (_unknownField) - { - s << ",sUnknownField(\"\")"; - } - s << endl; - } - - s << TAB << "{" << endl; - INC_TAB; - for (size_t j = 0; j < member.size(); j++) - { - VectorPtr vPtr = VectorPtr::dynamicCast(member[j]->getTypePtr()); - if (vPtr) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (!bPtr || (bPtr && bPtr->kind() == Builtin::KindString)) //éžå†…建类型或者string 类型ä¸èƒ½memset - { - continue; - } - } - if (!member[j]->getTypePtr()->isArray()) - { - continue; - } - s << TAB << "memset(" << member[j]->getId() << ", 0, " << "sizeof(" << member[j]->getId() << "));" << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - - //resetDefault()函数 - s << TAB << "void resetDefautlt()" << endl; - s << TAB << "{" << endl; - INC_TAB; - - member = pPtr->getAllMemberPtr(); - for (size_t j = 0; j < member.size(); j++) - { - if (member[j]->getTypePtr()->isArray()) - { - s << TAB << member[j]->getId() << "Len = 0;" << endl; - VectorPtr vPtr = VectorPtr::dynamicCast(member[j]->getTypePtr()); - if (vPtr) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (bPtr && bPtr->kind() != Builtin::KindString) //éžå†…建类型或者string 类型ä¸èƒ½memset - { - s << TAB << "memset(" << member[j]->getId() << ", 0, " << "sizeof(" << member[j]->getId() << "));" << endl; - } - } - continue; - } - - if (member[j]->getTypePtr()->isPointer()) - { - s << TAB << member[j]->getId() << "Len = 0;" << endl; - s << TAB << member[j]->getId() << " = NULL;" << endl; - continue; - } - - if (member[j]->hasDefault()) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[j]->getTypePtr()); - //string值è¦è½¬ä¹‰ - if (bPtr && bPtr->kind() == Builtin::KindString) - { - string tmp = tars::TC_Common::replace(member[j]->def(), "\"", "\\\""); - s << TAB << member[j]->getId() << " = \"" << tmp << "\";" << endl; - } - else - { - s << TAB << member[j]->getId() << " = " << member[j]->def() << ";" << endl; - } - } - } - - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "template" << endl; - s << TAB << "void writeTo(" + _namespace + "::TarsOutputStream& _os) const" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t j = 0; j < member.size(); j++) - { - s << writeTo(member[j]); - } - if (_unknownField) - { - s << writeUnknown(); - } - DEL_TAB; - s << TAB << "}" << endl; - - /////////////////////////////////////////////////////////// - s << TAB << "template" << endl; - s << TAB << "void readFrom(" + _namespace + "::TarsInputStream& _is)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "resetDefautlt();" << endl; - for (size_t j = 0; j < member.size(); j++) - { - s << readFrom(member[j]); - } - if (_unknownField) - { - s << readUnknown(member[member.size() - 1]); - } - - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "ostream& display(ostream& _os, int _level=0) const" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << _namespace + "::TarsDisplayer _ds(_os, _level);" << endl; - - for (size_t j = 0; j < member.size(); j++) - { - s << display(member[j]); - } - s << TAB << "return _os;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - - s << TAB << "ostream& displaySimple(ostream& _os, int _level=0) const" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << _namespace + "::TarsDisplayer _ds(_os, _level);" << endl; - - for (size_t j = 0; j < member.size(); j++) - { - s << displaySimple(member[j], (j != member.size() - 1 ? true : false)); - } - s << TAB << "return _os;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - DEL_TAB; - s << TAB << "public:" << endl; - INC_TAB; - - //定义æˆå‘˜å˜é‡ - for (size_t j = 0; j < member.size(); j++) - { - if (member[j]->getTypePtr()->isArray() || member[j]->getTypePtr()->isPointer()) //数组类型ã€æŒ‡é’ˆç±»åž‹éœ€è¦å®šä¹‰é•¿åº¦ - { - s << TAB << _namespace + "::" << "UInt32 " << member[j]->getId() << "Len" << ";" << endl; - } - s << TAB << tostr(member[j]->getTypePtr()) << " " << member[j]->getId() << toStrSuffix(member[j]) << ";" << endl; - - } - if (_unknownField) - { - s << TAB << "std::string sUnknownField; //¹¤¾ß´ø--unknown²ÎÊý×Ô¶¯Éú³É×Ö¶Î,´æ·Åδ֪tagÊý¾Ã." << endl; - } - DEL_TAB; - s << TAB << "};" << endl; - - //定义==æ“作 - s << TAB << "inline bool operator==(const " << pPtr->getId() << "&l, const " << pPtr->getId() << "&r)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return "; - for (size_t j = 0; j < member.size(); j++) - { - if (member[j]->getTypePtr()->isArray() || member[j]->getTypePtr()->isPointer()) //数组类型ã€æŒ‡é’ˆç±»åž‹ - { - s << "!memcmp(l." << member[j]->getId() << ",r." << member[j]->getId() << ",l." << member[j]->getId() << "Len)"; - } - else - { - s << "l." << member[j]->getId() << " == r." << member[j]->getId(); - } - if (j != member.size() - 1) - { - s << " && "; - } - } - s << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - //定义!= - s << TAB << "inline bool operator!=(const " << pPtr->getId() << "&l, const " << pPtr->getId() << "&r)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return !(l == r);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - vector key = pPtr->getKey(); - //定义< - if (key.size() > 0) - { - s << TAB << "inline bool operator<(const " << pPtr->getId() << "&l, const " << pPtr->getId() << "&r)" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t i = 0; i < key.size(); i++) - { - s << TAB << "if(l." << key[i] << " != r." << key[i] << ") "; - for (size_t z = 0; z < member.size(); z++) - { - if (key[i] == member[z]->getId() && (member[z]->getTypePtr()->isArray() || member[z]->getTypePtr()->isPointer())) //数组类型ã€æŒ‡é’ˆç±»åž‹ - { - s << "memcmp(l." << key[i] << ",r." << key[i] << ",l." << key[i] << "Len)< 0"; - } - } - s << " return (l." << key[i] << " < r." << key[i] << ");" << endl; - - } - - s << TAB << "return false;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - //定义<= - s << TAB << "inline bool operator<=(const " << pPtr->getId() << "&l, const " << pPtr->getId() << "&r)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return !(r < l);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - //定义> - s << TAB << "inline bool operator>(const " << pPtr->getId() << "&l, const " << pPtr->getId() << "&r)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return r < l;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - //定义>= - s << TAB << "inline bool operator>=(const " << pPtr->getId() << "&l, const " << pPtr->getId() << "&r)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return !(l < r);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - - return s.str(); -} - -/*******************************ContainerPtr********************************/ -string Tars2Cpp::generateH(const ContainerPtr& pPtr) const -{ - ostringstream s; - for (size_t i = 0; i < pPtr->getAllNamespacePtr().size(); i++) - { - s << generateH(pPtr->getAllNamespacePtr()[i]) << endl; - s << endl; - } - return s.str(); -} - -/******************************ParamDeclPtr***************************************/ -string Tars2Cpp::generateH(const ParamDeclPtr& pPtr) const -{ - ostringstream s; - - //输出å‚æ•°, 或简å•ç±»åž‹ - if (pPtr->isOut() || pPtr->getTypeIdPtr()->getTypePtr()->isSimple()) - { - s << tostr(pPtr->getTypeIdPtr()->getTypePtr()); - } - else - { - //结构, map, vector, string - s << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " &"; - } - - if (pPtr->isOut()) - { - s << " &"; - } - else - { - s << " "; - } - s << pPtr->getTypeIdPtr()->getId(); - - return s.str(); -} - -string Tars2Cpp::generateOutH(const ParamDeclPtr& pPtr) const -{ - if (!pPtr->isOut()) return ""; - - ostringstream s; - - //输出å‚æ•°, 或简å•ç±»åž‹ - if (pPtr->getTypeIdPtr()->getTypePtr()->isSimple()) - { - s << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " "; - } - else - { - //结构, map, vector, string - s << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " &"; - } - s << pPtr->getTypeIdPtr()->getId(); - - return s.str(); -} - -string Tars2Cpp::generateParamDecl(const ParamDeclPtr& pPtr) const -{ - ostringstream s; - - if (pPtr->isOut() || pPtr->getTypeIdPtr()->getTypePtr()->isSimple()) - { - s << tostr(pPtr->getTypeIdPtr()->getTypePtr()); - - if (pPtr->isOut()) s << " &"; - else s << " "; - } - else - { - //输入å‚æ•° - s << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " &"; - } - - s << pPtr->getTypeIdPtr()->getId(); - - return s.str(); -} - -string Tars2Cpp::generateDispatchAsync(const OperationPtr& pPtr, const string& cn) const -{ - ostringstream s; - s << TAB << "if (msg->response.iRet != tars::TARSSERVERSUCCESS)" << endl - << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(msg->response.iRet);" << endl; - s << endl; - - s << TAB << "return msg->response.iRet;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> _is;" << endl; - s << endl; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - s << TAB << "_is.setBuffer(msg->response.sBuffer);" << endl; - - //对输出å‚æ•°ç¼–ç  - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << tostr(pPtr->getReturnPtr()->getTypePtr()) << " " << pPtr->getReturnPtr()->getId() << generateInitValue(pPtr->getReturnPtr()) << ";" << endl; - s << readFrom(pPtr->getReturnPtr()) << endl; - } - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - s << TAB << tostr(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << " " - << vParamDecl[i]->getTypeIdPtr()->getId() << generateInitValue(vParamDecl[i]->getTypeIdPtr()) << ";" << endl; - s << readFrom(vParamDecl[i]->getTypeIdPtr()); - } - } - - //处ç†çº¿ç¨‹ç§æœ‰æ•°æ® - s << TAB << "CallbackThreadData * pCbtd = CallbackThreadData::getData();" << endl; - s << TAB << "assert(pCbtd != NULL);" << endl; - s << endl; - s << TAB << "pCbtd->setResponseContext(msg->response.context);" << endl; - s << endl; - - //异步回调都无返回值 - s << TAB << "callback_" << pPtr->getId() << "("; - string sParams; - if (pPtr->getReturnPtr()->getTypePtr()) - { - sParams = pPtr->getReturnPtr()->getId() + ", "; - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - sParams += vParamDecl[i]->getTypeIdPtr()->getId() + ", "; - } - } - s << tars::TC_Common::trimright(sParams, ", ", false) << ");" << endl; - - s << endl; - s << TAB << "pCbtd->delResponseContext();" << endl; - s << endl; - - - s << TAB << "return tars::TARSSERVERSUCCESS;" << endl; - - return s.str(); -} - -/////////////////////////////////////////////////////////////////////////////////////////////// -string Tars2Cpp::generateDispatchCoroAsync(const OperationPtr& pPtr, const string& cn) const -{ - ostringstream s; - s << TAB << "if (msg->response.iRet != tars::TARSSERVERSUCCESS)" << endl - << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(msg->response.iRet);" << endl; - s << endl; - - s << TAB << "return msg->response.iRet;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> _is;" << endl; - s << endl; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - s << TAB << "_is.setBuffer(msg->response.sBuffer);" << endl; - - if(pPtr->getReturnPtr()->getTypePtr() || vParamDecl.size() >0) - { - s << TAB << "try" << endl; - s << TAB << "{" << endl; - - INC_TAB; - } - - //对输出å‚æ•°ç¼–ç  - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << tostr(pPtr->getReturnPtr()->getTypePtr()) << " " << pPtr->getReturnPtr()->getId() << generateInitValue(pPtr->getReturnPtr()) << ";" << endl; - s << readFrom(pPtr->getReturnPtr()) << endl; - } - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - s << TAB << tostr(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << " " - << vParamDecl[i]->getTypeIdPtr()->getId() << generateInitValue(vParamDecl[i]->getTypeIdPtr()) << ";" << endl; - s << readFrom(vParamDecl[i]->getTypeIdPtr()); - } - } - - s << TAB << "setResponseContext(msg->response.context);" << endl; - s << endl; - - //异步回调都无返回值 - s << TAB << "callback_" << pPtr->getId() << "("; - string sParams; - if (pPtr->getReturnPtr()->getTypePtr()) - { - sParams = pPtr->getReturnPtr()->getId() + ", "; - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - sParams += vParamDecl[i]->getTypeIdPtr()->getId() + ", "; - } - } - s << tars::TC_Common::trimright(sParams, ", ", false) << ");" << endl; - - s << endl; - - if(pPtr->getReturnPtr()->getTypePtr() || vParamDecl.size() >0) - { - DEL_TAB; - - s << TAB << "}" << endl; - s << TAB << "catch(std::exception &ex)" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(tars::TARSCLIENTDECODEERR);" << endl; - s << endl; - s << TAB << "return tars::TARSCLIENTDECODEERR;" << endl; - DEL_TAB; - - s << TAB << "}" << endl; - s << TAB << "catch(...)" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(tars::TARSCLIENTDECODEERR);" << endl; - s << endl; - s << TAB << "return tars::TARSCLIENTDECODEERR;" << endl; - DEL_TAB; - - s << TAB << "}" << endl; - s << endl; - } - - s << TAB << "return tars::TARSSERVERSUCCESS;" << endl; - - return s.str(); -} -/////////////////////////////////////////////////////////////////////////////////////////////////////// -string Tars2Cpp::generateHAsync(const OperationPtr& pPtr) const -{ - ostringstream s; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - s << TAB << "virtual void " << "callback_" << pPtr->getId() << "("; - - string sParams; - if (pPtr->getReturnPtr()->getTypePtr()) - { - if (pPtr->getReturnPtr()->getTypePtr()->isSimple()) - { - sParams = tostr(pPtr->getReturnPtr()->getTypePtr()) + " ret, "; - } - else - { - //结构, map, vector, string - sParams = "const " + tostr(pPtr->getReturnPtr()->getTypePtr()) + "& ret, "; - } - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - ParamDeclPtr& pPtr = vParamDecl[i]; - if (pPtr->isOut()) - { - //输出å‚æ•°, 或简å•ç±»åž‹ - if (pPtr->getTypeIdPtr()->getTypePtr()->isSimple()) - { - sParams += tostr(pPtr->getTypeIdPtr()->getTypePtr()); - } - else - { - //结构, map, vector, string - sParams += " const " + tostr(pPtr->getTypeIdPtr()->getTypePtr()) + "&"; - } - sParams += " " + pPtr->getTypeIdPtr()->getId() + ", "; - } - } - s << tars::TC_Common::trimright(sParams, ", ", false) << ")" << endl; - - s << TAB << "{ throw std::runtime_error(\"callback_" << pPtr->getId() << "() override incorrect.\"); }" << endl; - s << TAB << "virtual void " << "callback_" << pPtr->getId() << "_exception(" + _namespace + "::Int32 ret)" << endl; - s << TAB << "{ throw std::runtime_error(\"callback_" << pPtr->getId() << "_exception() override incorrect.\"); }"; - s << endl; - - return s.str(); -} - -string Tars2Cpp::generateInitValue(const TypeIdPtr& pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - string init = ""; - if (bPtr && Builtin::KindBool == bPtr->kind()) - { - init = " = false"; - } - - //枚举强制类型转æ¢åœ¨O2编译选项情况下会告警 - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - if (pPtr->hasDefault()) - { - string sid = ePtr->getSid(); - init = " = " + pPtr->def() + ";"; - } - else - { - vector& eMember = ePtr->getAllMemberPtr(); - if (eMember.size() > 0) - { - string sid = ePtr->getSid(); - init = " = " + sid.substr(0, sid.find_first_of("::")) + "::" + eMember[0]->getId() + ";"; - } - } - } - return init; -} - -////////////////////////////////////////////////////////////////////////////////////////////////////// -string Tars2Cpp::generateServantDispatch(const OperationPtr& pPtr, const string& cn) const -{ - ostringstream s; - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> _is;" << endl; - s << TAB << "_is.setBuffer(_current->getRequestBuffer());" << endl; - - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - string routekey; - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - s << TAB << tostr(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << " "<< vParamDecl[i]->getTypeIdPtr()->getId() - << generateInitValue(vParamDecl[i]->getTypeIdPtr()) << ";" << endl; - - if (routekey.empty() && vParamDecl[i]->isRouteKey()) - { - routekey = vParamDecl[i]->getTypeIdPtr()->getId(); - } - } - - - s << TAB << "if (_current->getRequestVersion() == TUPVERSION)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "UniAttribute<" + _namespace + "::BufferWriter, " + _namespace + "::BufferReader> tarsAttr;" << endl; - s << TAB << "tarsAttr.setVersion(_current->getRequestVersion());" << endl; - s << TAB << "tarsAttr.decode(_current->getRequestBuffer());" << endl; - for(size_t i = 0; i < vParamDecl.size(); i++) - { - string sParamName = vParamDecl[i]->getTypeIdPtr()->getId(); - string sEnum2Int = (EnumPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) ? "(" + _namespace + "::Int32)" : ""; - if (!vParamDecl[i]->isOut()) - { - //枚举类型转æˆint - if (EnumPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) - { - s << TAB << sParamName << " = (" << tostr(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) - << ") tarsAttr.get<" + _namespace + "::Int32>(\"" << sParamName << "\");" << endl; - } - else - { - s << TAB << "tarsAttr.get(\"" << sParamName << "\", " << sParamName << ");" << endl; - } - } - else - { - //枚举类型转æˆint - if (EnumPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) - { - s << TAB << sParamName << " = (" << tostr(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) - << ") tarsAttr.getByDefault<" + _namespace + "::Int32>(\"" << sParamName << "\", " << sEnum2Int << sParamName << ");" << endl; - } - else - { - s << TAB << "tarsAttr.getByDefault(\"" << sParamName << "\", " << sEnum2Int << sParamName << ", " - << sEnum2Int << sParamName << ");" << endl; - } - } - } - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "else" << endl; - s << TAB << "{" << endl; - INC_TAB; - - //普通tars请求 - for (size_t i = 0; i < vParamDecl.size(); i++) - { - s << readFrom(vParamDecl[i]->getTypeIdPtr(), (!vParamDecl[i]->isOut())); - } - - DEL_TAB; - s << TAB << "}" << endl; - - if(pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << tostr(pPtr->getReturnPtr()->getTypePtr()) << " " << pPtr->getReturnPtr()->getId() << " = " << pPtr->getId() << "("; - } - else - { - s << TAB << pPtr->getId() << "("; - } - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - s << vParamDecl[i]->getTypeIdPtr()->getId(); - if(i != vParamDecl.size() - 1) - s << ","; - else - s << ", _current);" << endl; - } - if (vParamDecl.size() == 0) - { - s << "_current);" << endl; - } - s << TAB << "if(_current->isResponse())" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "if (_current->getRequestVersion() == TUPVERSION )" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "UniAttribute<" + _namespace + "::BufferWriter, " + _namespace + "::BufferReader> tarsAttr;" << endl; - s << TAB << "tarsAttr.setVersion(_current->getRequestVersion());" << endl; - if(pPtr->getReturnPtr()->getTypePtr()) - { - string sEnum2Int = (EnumPtr::dynamicCast(pPtr->getReturnPtr()->getTypePtr())) ? "(" + _namespace + "::Int32)" : ""; - s << TAB << "tarsAttr.put(\"\", " << sEnum2Int << "_ret);" << endl; - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - string sParamName = vParamDecl[i]->getTypeIdPtr()->getId(); - string sEnum2Int = (EnumPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) ? "(" + _namespace + "::Int32)" : ""; - if (vParamDecl[i]->isOut()) - { - s << TAB << "tarsAttr.put(\"" << sParamName << "\", " << sEnum2Int << sParamName << ");" << endl; - } - } - s << TAB << "tarsAttr.encode(_sResponseBuffer);" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "else" << endl; - - //普通tars调用输出å‚æ•° - s << TAB << "{" << endl; - INC_TAB; - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << writeTo(pPtr->getReturnPtr()); - } - //解ç è¾“出å‚æ•° - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - s << writeTo(vParamDecl[i]->getTypeIdPtr()); - } - } - s << TAB << "_os.swap(_sResponseBuffer);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "return tars::TARSSERVERSUCCESS;" << endl; - - return s.str(); -} - -string Tars2Cpp::promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire) const -{ - ostringstream s; - if(EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - //枚举强制类型转æ¢åœ¨O2编译选项情况下会告警 - string tmp = _namespace + "::Int32 eTemp" + TC_Common::tostr(pPtr->getTag()) + generateInitValue(pPtr); - - s << TAB << tmp <getTag()); - } - else if(pPtr->getTypePtr()->isArray()) - { - s << TAB << "_is.read(ptr->"<< pPtr->getId()<<", "<getId() << "Len"; - } - else if(pPtr->getTypePtr()->isPointer()) - { - s << TAB << pPtr->getId() <<" = ("<getTypePtr())<<")_is.cur();"<"<< pPtr->getId()<<", _is.left(), "<< pPtr->getId() << "Len"; - } - else - { - s << TAB << "_is.read(ptr->"<< pPtr->getId(); - } - - s << ", " << pPtr->getTag() << ", " << ((pPtr->isRequire() && bIsRequire)?"true":"false") << ");" << endl; - - if(EnumPtr::dynamicCast(pPtr->getTypePtr())) - { - s << TAB << "ptr->" << pPtr->getId() << " = (" <getTypePtr()) <<")eTemp"<getTag())<<";"<getTypePtr()->isPointer()) - s << TAB <<"_is.mapBufferSkip("<getId() << "Len);"<getTypePtr()); - string init = ""; - if(bPtr && Builtin::KindBool == bPtr->kind()) - { - return true; - } - - //枚举强制类型转æ¢åœ¨O2编译选项情况下会告警 - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if(ePtr) - { - if(pPtr->hasDefault()) - { - return true; - } - else - { - vector& eMember = ePtr->getAllMemberPtr(); - if(eMember.size() > 0) - { - return true; - } - } - } - return false; -} -string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) const -{ - ostringstream s; - //生æˆå‡½æ•°å£°æ˜Ž - s << TAB << "void async_" << pPtr->getId() << "("; - s << cn << "PrxCallbackPtr callback,"; - - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - string routekey = ""; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut() ) - { - s << generateParamDecl(vParamDecl[i]) << ","; - } - - if (routekey.empty() && vParamDecl[i]->isRouteKey()) - { - routekey = vParamDecl[i]->getTypeIdPtr()->getId(); - } - } - s << "const map& context = TARS_CONTEXT())"; - s << endl; - - s << TAB << "{" << endl; - INC_TAB; - - if (_tarsMaster) - { - s << TAB << "this->tars_setMasterFlag(true);" << endl; - } - - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - continue; - } - s << writeTo(vParamDecl[i]->getTypeIdPtr()); - } - - s << TAB << "std::map _mStatus;" << endl; - - if (!routekey.empty()) - { - ostringstream os; - - os << routekey; - - s << TAB << "_mStatus.insert(std::make_pair(ServantProxy::STATUS_GRID_KEY, " << os.str() << "));" << endl; - } - - s << TAB << "tars_invoke_async(tars::TARSNORMAL,\"" << pPtr->getId() << "\", _os.getByteBuffer(), context, _mStatus, callback);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << endl; - - //promise异步的函数声明 - string sStruct = pPtr->getId(); - - s << TAB << "promise::Future< " << cn <<"PrxCallbackPromise::Promise" << sStruct << "Ptr > promise_async_" << pPtr->getId() << "("; - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) - { - s << generateParamDecl(vParamDecl[i]) << ","; - } - } - s << "const map& context)" << endl; - - s << TAB << "{" << endl; - INC_TAB; - - if (_tarsMaster) - { - s << TAB << "this->tars_setMasterFlag(true);" << endl; - } - - s << TAB << "promise::Promise< " << cn <<"PrxCallbackPromise::Promise" << sStruct << "Ptr > promise;" << endl; - s << TAB << cn << "PrxCallbackPromisePtr callback = new " << cn << "PrxCallbackPromise(promise);" << endl; - s << endl; - - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - if(vParamDecl[i]->isOut()) - { - continue; - } - s << writeTo(vParamDecl[i]->getTypeIdPtr()); - } - - s << TAB << "std::map _mStatus;" << endl; - - if (!routekey.empty()) - { - ostringstream os; - - os << routekey; - - s << TAB << "_mStatus.insert(std::make_pair(ServantProxy::STATUS_GRID_KEY, " << os.str() << "));" << endl; - } - - s << TAB << "tars_invoke_async(tars::TARSNORMAL,\"" << pPtr->getId() << "\", _os.getByteBuffer(), context, _mStatus, callback);" << endl; - - s << endl; - s << TAB << "return promise.getFuture();" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //å程并行异步的函数声明 - s << TAB << "void coro_" << pPtr->getId() << "("; - s << cn << "CoroPrxCallbackPtr callback,"; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) - { - s << generateParamDecl(vParamDecl[i]) << ","; - } - } - s << "const map& context = TARS_CONTEXT())"; - s << endl; - - s << TAB << "{" << endl; - INC_TAB; - - if (_tarsMaster) - { - s << TAB << "this->tars_setMasterFlag(true);" << endl; - } - - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - continue; - } - s << writeTo(vParamDecl[i]->getTypeIdPtr()); - } - - s << TAB << "std::map _mStatus;" << endl; - - if (!routekey.empty()) - { - ostringstream os; - - os << routekey; - - s << TAB << "_mStatus.insert(std::make_pair(ServantProxy::STATUS_GRID_KEY, " << os.str() << "));" << endl; - } - - s << TAB << "tars_invoke_async(tars::TARSNORMAL,\"" << pPtr->getId() << "\", _os.getByteBuffer(), context, _mStatus, callback, true);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - return s.str(); -} - -///////////////////////////////////////////////////////////////////////////////////////////////// -string Tars2Cpp::generateH(const OperationPtr& pPtr, bool bVirtual, const string& interfaceId) const -{ - ostringstream s; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - s << TAB; - - if (bVirtual) s << "virtual "; - - s << tostr(pPtr->getReturnPtr()->getTypePtr()) << " " << pPtr->getId() << "("; - - string routekey = ""; - for (size_t i = 0; i < vParamDecl.size(); i++) - { - s << generateH(vParamDecl[i]) << ","; - - if (routekey.empty() && vParamDecl[i]->isRouteKey()) - { - routekey = vParamDecl[i]->getTypeIdPtr()->getId(); - } - } - - if (bVirtual) - { - s << "tars::TarsCurrentPtr current) = 0;"; - } - else - { - s << "const map &context = TARS_CONTEXT(),map * pResponseContext = NULL)"; - - s << endl; - - s << TAB << "{" << endl; - - INC_TAB; - - if (_tarsMaster) - { - s << TAB << "this->tars_setMasterFlag(true);" << endl; - } - - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - //if(vParamDecl[i]->isOut()) continue; - s << writeTo(vParamDecl[i]->getTypeIdPtr()); - } - - s << TAB << "" + _namespace + "::ResponsePacket rep;" << endl; - - s << TAB << "std::map _mStatus;" << endl; - - if (!routekey.empty()) - { - ostringstream os; - - os << routekey; - - s << TAB << "_mStatus.insert(std::make_pair(ServantProxy::STATUS_GRID_KEY, " << os.str() << "));" << endl; - } - - s << TAB << "tars_invoke(tars::TARSNORMAL,\"" << pPtr->getId() << "\", _os.getByteBuffer(), context, _mStatus, rep);" << endl; - s << TAB << "if(pResponseContext)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "*pResponseContext = rep.context;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - if (vParamDecl.size() > 0 || pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> _is;" << endl; - s << TAB << "_is.setBuffer(rep.sBuffer);" << endl; - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << tostr(pPtr->getReturnPtr()->getTypePtr()) << " " << pPtr->getReturnPtr()->getId() << generateInitValue(pPtr->getReturnPtr()) << ";" << endl; - s << readFrom(pPtr->getReturnPtr()); - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - s << readFrom(vParamDecl[i]->getTypeIdPtr()); - } - } - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << "return " << pPtr->getReturnPtr()->getId() << ";" << endl; - } - } - DEL_TAB; - s << TAB << "}" << endl; - } - - s << endl; - - if (bVirtual) - { - //异步回调 - s << TAB << "static void async_response_" << pPtr->getId() << "(tars::TarsCurrentPtr current"; - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << ", "; - if (pPtr->getReturnPtr()->getTypePtr()->isSimple()) - { - s << tostr(pPtr->getReturnPtr()->getTypePtr()) << " "; - } - else - { - //结构, map, vector, string - s << "const " << tostr(pPtr->getReturnPtr()->getTypePtr()) << " &"; - } - s << pPtr->getReturnPtr()->getId(); - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if(!vParamDecl[i]->isOut()) - continue; - - s << ", "; - s << generateOutH(vParamDecl[i]); - } - s << ")" << endl; - - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "if (current->getRequestVersion() == TUPVERSION )" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "UniAttribute<" + _namespace + "::BufferWriter, " + _namespace + "::BufferReader> tarsAttr;" << endl; - s << TAB << "tarsAttr.setVersion(current->getRequestVersion());" << endl; - if(pPtr->getReturnPtr()->getTypePtr()) - { - string sEnum2Int = (EnumPtr::dynamicCast(pPtr->getReturnPtr()->getTypePtr())) ? "(" + _namespace + "::Int32)" : ""; - s << TAB << "tarsAttr.put(\"\", " << sEnum2Int << "_ret);" << endl; - } - for(size_t i = 0; i < vParamDecl.size(); i++) - { - string sParamName = vParamDecl[i]->getTypeIdPtr()->getId(); - string sEnum2Int = (EnumPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) ? "(" + _namespace + "::Int32)" : ""; - if(vParamDecl[i]->isOut()) - { - s << TAB << "tarsAttr.put(\"" << sParamName << "\", " << sEnum2Int << sParamName << ");" << endl; - } - } - s << endl; - s << TAB << "vector sTupResponseBuffer;" << endl; - s << TAB << "tarsAttr.encode(sTupResponseBuffer);"<< endl; - s << TAB << "current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "else" << endl; - s << TAB << "{" << endl; - - INC_TAB; - - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - if(pPtr->getReturnPtr()->getTypePtr()) - { - s << writeTo(pPtr->getReturnPtr()) << endl; - } - for(size_t i = 0; i < vParamDecl.size(); i++) - { - if(!vParamDecl[i]->isOut()) - continue; - - s << writeTo(vParamDecl[i]->getTypeIdPtr()) << endl; - } - - - //s << TAB << "current->sendResponse(tars::TARSSERVERSUCCESS, string(_os.getBuffer(), _os.getLength()));" << endl; - s << TAB << "current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - } - return s.str(); -} - -/** - * 对接å£åæŽ’åº - */ -struct SortOperation { - bool operator ()(const OperationPtr& o1, const OperationPtr& o2) - { - return o1->getId() < o2->getId(); - } -}; - -string Tars2Cpp::generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const -{ - ostringstream s; - - string sStruct = pPtr->getId(); - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - //////// - DEL_TAB; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "struct Promise" << sStruct << ": virtual public TC_HandleBase" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl; - INC_TAB; - - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << TAB << tostr(pPtr->getReturnPtr()->getTypePtr()) << " _ret;" << endl; - } - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - ParamDeclPtr& pPtr = vParamDecl[i]; - if (pPtr->isOut()) - { - s << TAB << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " " << pPtr->getTypeIdPtr()->getId() << ";" << endl; - } - } - - s << TAB << "map _mRspContext;" << endl; - - DEL_TAB; - s << TAB << "};" << endl; - s << TAB << endl; - s << TAB << "typedef tars::TC_AutoPtr< " << pInter->getId() << "PrxCallbackPromise::Promise" << sStruct << " > Promise" << sStruct << "Ptr;" << endl; - s << endl; - - s << TAB << pInter->getId() << "PrxCallbackPromise(const promise::Promise< " << pInter->getId() << "PrxCallbackPromise::Promise" << sStruct << "Ptr > &promise)" << endl; - s << TAB << ": _promise_" << sStruct << "(promise)" << endl; - s << TAB << "{}" << endl; - s << TAB << endl; - - s << TAB << "virtual void " << "callback_" << pPtr->getId() << "(const " << pInter->getId() << "PrxCallbackPromise::Promise" << sStruct << "Ptr &ptr)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "_promise_" << sStruct << ".setValue(ptr);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "virtual void " << "callback_" << pPtr->getId() << "_exception(" + _namespace + "::Int32 ret)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "std::string str(\"\");" << endl; - s << TAB << "str += \"Function:" << pPtr->getId() << "_exception|Ret:\";" << endl; - s << TAB << "str += TC_Common::tostr(ret);" << endl; - s << TAB << "_promise_" << sStruct << ".setException(promise::copyException(str, ret));" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - DEL_TAB; - s << TAB << "protected:" << endl; - INC_TAB; - s << TAB << "promise::Promise< " << pInter->getId() << "PrxCallbackPromise::Promise" << sStruct << "Ptr > _promise_" << sStruct << ";" << endl; - - return s.str(); -} - -string Tars2Cpp::generateDispatchPromiseAsync(const OperationPtr &pPtr, const string &cn) const -{ - ostringstream s; - s << TAB << "if (msg->response.iRet != tars::TARSSERVERSUCCESS)" << endl - << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(msg->response.iRet);" << endl; - s << endl; - - s << TAB << "return msg->response.iRet;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> _is;" << endl; - s << endl; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - s << TAB << "_is.setBuffer(msg->response.sBuffer);" << endl; - s << endl; - - string sStruct = pPtr->getId(); - - s << TAB << cn << "PrxCallbackPromise::Promise" << sStruct << "Ptr ptr = new "<< cn << "PrxCallbackPromise::Promise" << sStruct << "();" << endl; - s << endl; - - if(pPtr->getReturnPtr()->getTypePtr() || vParamDecl.size() >0) - { - s << TAB << "try" << endl; - s << TAB << "{" << endl; - - INC_TAB; - } - - //对输出å‚æ•°ç¼–ç  - if(pPtr->getReturnPtr()->getTypePtr()) - { - if(isPromiseDispatchInitValue(pPtr->getReturnPtr())) - s << TAB << "ptr->_ret " << generateInitValue(pPtr->getReturnPtr())<< ";"<< endl; - s << promiseReadFrom(pPtr->getReturnPtr()) << endl; - } - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - if(vParamDecl[i]->isOut()) - { - if(isPromiseDispatchInitValue(vParamDecl[i]->getTypeIdPtr())) - s << TAB << "ptr->" << vParamDecl[i]->getTypeIdPtr()->getId() << generateInitValue(vParamDecl[i]->getTypeIdPtr())<< ";" << endl; - s << promiseReadFrom(vParamDecl[i]->getTypeIdPtr()); - } - } - - if(pPtr->getReturnPtr()->getTypePtr() || vParamDecl.size() >0) - { - DEL_TAB; - - s << TAB << "}" << endl; - s << TAB << "catch(std::exception &ex)" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(tars::TARSCLIENTDECODEERR);" << endl; - s << endl; - s << TAB << "return tars::TARSCLIENTDECODEERR;" << endl; - DEL_TAB; - - s << TAB << "}" << endl; - s << TAB << "catch(...)" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "callback_" << pPtr->getId() << "_exception(tars::TARSCLIENTDECODEERR);" << endl; - s << endl; - s << TAB << "return tars::TARSCLIENTDECODEERR;" << endl; - DEL_TAB; - - s << TAB << "}" << endl; - s << endl; - } - - s << TAB << "ptr->_mRspContext = msg->response.context;" << endl; - s << endl; - - s << TAB << "callback_" << pPtr->getId() << "(ptr);" << endl; - s << endl; - - - s << TAB << "return tars::TARSSERVERSUCCESS;" << endl; - - return s.str(); -} -/******************************InterfacePtr***************************************/ -string Tars2Cpp::generateH(const InterfacePtr &pPtr, const NamespacePtr &nPtr) const -{ - ostringstream s; - vector& vOperation = pPtr->getAllOperationPtr(); - - std::sort(vOperation.begin(), vOperation.end(), SortOperation()); - - //生æˆå¼‚步回调Proxy - s << TAB << "/* callback of async proxy for client */" << endl; - s << TAB << "class " << pPtr->getId() << "PrxCallback: public tars::ServantProxyCallback" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "virtual ~" << pPtr->getId() << "PrxCallback(){}" << endl; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << generateHAsync(vOperation[i]) << endl; - } - - DEL_TAB; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "virtual const map & getResponseContext() const" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "CallbackThreadData * pCbtd = CallbackThreadData::getData();" << endl; - s << TAB << "assert(pCbtd != NULL);" << endl; - s << endl; - s << TAB << "if(!pCbtd->getContextValid())" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "throw TC_Exception(\"cann't get response context\");" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "return pCbtd->getResponseContext();" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - DEL_TAB; - s << TAB << "public:" << endl; - INC_TAB; - - s << TAB << "virtual int onDispatch(tars::ReqMessagePtr msg)" << endl; - - //生æˆå¼‚æ­¥å›žè°ƒæŽ¥å£ - s << TAB << "{" << endl; - INC_TAB; - string dname = "__" + pPtr->getId() + "_all"; - string dispatch = "static ::std::string " + dname; - s << TAB << dispatch << "[]=" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "\"" << vOperation[i]->getId() << "\""; - if (i != vOperation.size() - 1) - { - s << ","; - } - s << endl; - - } - DEL_TAB; - s << TAB << "};" << endl; - - s << TAB << "pair r = equal_range(" << dname << ", " << dname << "+" << vOperation.size() << ", string(msg->request.sFuncName));" << endl; - - s << TAB << "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;" << endl; - - s << TAB << "switch(r.first - " << dname << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "case " << i << ":" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << generateDispatchAsync(vOperation[i], pPtr->getId()) << endl; - - DEL_TAB; - s << TAB << "}" << endl; - } - - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "return tars::TARSSERVERNOFUNCERR;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - DEL_TAB; - s << TAB << "};" << endl; - - s << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "PrxCallback> " << pPtr->getId() << "PrxCallbackPtr;" << endl; - s << endl; - - //生æˆpromise异步回调Proxy - s << TAB << "/* callback of promise async proxy for client */" << endl; - s << TAB << "class " << pPtr->getId() << "PrxCallbackPromise: public tars::ServantProxyCallback" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "virtual ~" << pPtr->getId() << "PrxCallbackPromise(){}" << endl; - - for(size_t i = 0; i < vOperation.size(); i++) - { - s << generateHPromiseAsync(pPtr, vOperation[i]) << endl; - } - - DEL_TAB; - - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "virtual int onDispatch(tars::ReqMessagePtr msg)" << endl; - - s << TAB << "{" << endl; - INC_TAB; - - dname = "__" + pPtr->getId() + "_all"; - dispatch = "static ::std::string " + dname; - s << TAB << dispatch << "[]=" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "\"" << vOperation[i]->getId() << "\""; - if (i != vOperation.size() - 1) - { - s << ","; - } - s << endl; - } - - DEL_TAB; - s << TAB << "};" << endl; - - s << endl; - - s << TAB << "pair r = equal_range(" << dname << ", " << dname << "+" << vOperation.size() << ", string(msg->request.sFuncName));" << endl; - - s << TAB << "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;" << endl; - - s << TAB << "switch(r.first - " << dname << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for(size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "case " << i << ":" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << generateDispatchPromiseAsync(vOperation[i], pPtr->getId()) << endl; - - DEL_TAB; - s << TAB << "}" << endl; - } - - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "return tars::TARSSERVERNOFUNCERR;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - DEL_TAB; - - s << TAB << "};" << endl; - - s << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "PrxCallbackPromise> " << pPtr->getId() << "PrxCallbackPromisePtr;" << endl; - s << endl; - - //生æˆå程异步回调类,用于并å‘请求 - s << TAB << "/* callback of coroutine async proxy for client */" << endl; - s << TAB << "class " << pPtr->getId() << "CoroPrxCallback: public " << pPtr->getId() << "PrxCallback" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "virtual ~" << pPtr->getId() << "CoroPrxCallback(){}" << endl; - DEL_TAB; - - s << TAB << "public:" << endl; - - INC_TAB; - s << TAB << "virtual const map & getResponseContext() const { return _mRspContext; }" << endl; - s << endl; - s << TAB << "virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; }" << endl; - s << endl; - DEL_TAB; - - s << TAB << "public:" << endl; - INC_TAB; - //生æˆåç¨‹å¼‚æ­¥å›žè°ƒæŽ¥å£ - s << TAB << "int onDispatch(tars::ReqMessagePtr msg)" << endl; - s << TAB << "{" << endl; - INC_TAB; - dname = "__" + pPtr->getId() + "_all"; - dispatch = "static ::std::string " + dname; - s << TAB << dispatch << "[]=" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "\"" << vOperation[i]->getId() << "\""; - if (i != vOperation.size() - 1) - { - s << ","; - } - s << endl; - } - - DEL_TAB; - s << TAB << "};" << endl; - - s << endl; - - s << TAB << "pair r = equal_range(" << dname << ", " << dname << "+" << vOperation.size() << ", string(msg->request.sFuncName));" << endl; - - s << TAB << "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;" << endl; - - s << TAB << "switch(r.first - " << dname << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "case " << i << ":" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << generateDispatchCoroAsync(vOperation[i], pPtr->getId()) << endl; - - DEL_TAB; - s << TAB << "}" << endl; - } - - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "return tars::TARSSERVERNOFUNCERR;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - - //////////////////////////////////////////////////////////////////// - DEL_TAB; - s << TAB << "protected:" << endl; - - INC_TAB; - s << TAB << "map _mRspContext;" << endl; - //s << TAB << "tars::ParallelSharedBasePtr _pPtr;" << endl; - DEL_TAB; - - s << TAB << "};" << endl; - - s << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "CoroPrxCallback> " << pPtr->getId() << "CoroPrxCallbackPtr;" << endl; - s << endl; - - //生æˆå®¢æˆ·ç«¯ä»£ç† - s << TAB << "/* proxy for client */" << endl; - s << TAB << "class " << pPtr->getId() << "Proxy : public tars::ServantProxy" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "typedef map TARS_CONTEXT;" << endl; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << generateH(vOperation[i], false, pPtr->getId()); // << endl; - s << generateHAsync(vOperation[i], pPtr->getId()) << endl; - } - - s << TAB << pPtr->getId() << "Proxy* tars_hash(int64_t key)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return (" << pPtr->getId() + "Proxy*)ServantProxy::tars_hash(key);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - s << TAB << pPtr->getId() << "Proxy* tars_consistent_hash(int64_t key)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return (" << pPtr->getId() + "Proxy*)ServantProxy::tars_consistent_hash(key);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - s << TAB << pPtr->getId() << "Proxy* tars_set_timeout(int msecond)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return (" << pPtr->getId() + "Proxy*)ServantProxy::tars_set_timeout(msecond);" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - DEL_TAB; - s << TAB << "};" << endl; - - s << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "Proxy> " << pPtr->getId() << "Prx;" << endl; - s << endl; - - //生æˆæœåŠ¡ç«¯Servant - s << TAB << "/* servant for server */" << endl; - s << TAB << "class " << pPtr->getId() << " : public tars::Servant" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl; - INC_TAB; - s << TAB << "virtual ~" << pPtr->getId() << "(){}" << endl; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << generateH(vOperation[i], true, pPtr->getId()) << endl; - } - - DEL_TAB; - s << TAB << "public:" << endl; - INC_TAB; - - s << TAB << "int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer)" << endl; - - s << TAB << "{" << endl; - INC_TAB; - dname = "__" + nPtr->getId() + "__" + pPtr->getId() + "_all"; - dispatch = "static ::std::string " + dname; - s << TAB << dispatch << "[]=" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "\"" << vOperation[i]->getId() << "\""; - if (i != vOperation.size() - 1) - { - s << ","; - } - s << endl; - } - - DEL_TAB; - s << TAB << "};" << endl; - - s << endl; - - s << TAB << "pair r = equal_range(" << dname << ", " << dname << "+" << vOperation.size() << ", _current->getFuncName());" << endl; - - s << TAB << "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;" << endl; - - s << TAB << "switch(r.first - " << dname << ")" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << TAB << "case " << i << ":" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << generateServantDispatch(vOperation[i], pPtr->getId()) << endl; - - DEL_TAB; - s << TAB << "}" << endl; - } - - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "return tars::TARSSERVERNOFUNCERR;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - DEL_TAB; - s << TAB << "};" << endl; - - return s.str(); -} - - -//struct SortOperation -//{ -// bool operator()(const OperationPtr &o1, const OperationPtr &o2) -// { -// return o1->getId() < o2->getId(); -// } -//}; - - -/******************************EnumPtr***************************************/ - -string Tars2Cpp::generateH(const EnumPtr& pPtr) const -{ - ostringstream s; - s << TAB << "enum " << pPtr->getId() << endl; - s << TAB << "{" << endl; - INC_TAB; - vector& member = pPtr->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << member[i]->getId(); - if (member[i]->hasDefault()) - { - s << " = " << member[i]->def(); - } - s << "," << endl; - } - DEL_TAB; - s << TAB << "};" << endl; - - //生æˆæžšä¸¾è½¬å­—符串函数 - s << TAB << "inline string etos" << "(const " << pPtr->getId() << " & e)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "switch(e)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "case " << member[i]->getId() << ": return " - << "\"" << member[i]->getId() << "\";" << endl; - } - s << TAB << "default: return \"\";" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - //s << TAB << "return \"\";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - //生æˆå­—符串转枚举函数 - s << TAB << "inline int stoe" << "(const string & s, " << pPtr->getId() << " & e)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "if(s == \"" << member[i]->getId() << "\") { e=" << member[i]->getId() << "; return 0;}" << endl; - } - s << endl; - s << TAB << "return -1;" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - - return s.str(); -} - -/******************************ConstPtr***************************************/ -string Tars2Cpp::generateH(const ConstPtr& pPtr) const -{ - ostringstream s; - - if (pPtr->getConstGrammarPtr()->t == ConstGrammar::STRING) - { - string tmp = tars::TC_Common::replace(pPtr->getConstGrammarPtr()->v, "\"", "\\\""); - s << TAB << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " " << pPtr->getTypeIdPtr()->getId() << " = \"" << tmp << "\";" << endl; - } - else - { - s << TAB << "const " << tostr(pPtr->getTypeIdPtr()->getTypePtr()) << " " << pPtr->getTypeIdPtr()->getId() << " = " << pPtr->getConstGrammarPtr()->v - << ((tostr(pPtr->getTypeIdPtr()->getTypePtr()) == _namespace + "::Int64") ? "LL;" : ";") << endl; - } - - return s.str(); -} -/******************************NamespacePtr***************************************/ - -string Tars2Cpp::generateH(const NamespacePtr& pPtr) const -{ - ostringstream s; - vector& is = pPtr->getAllInterfacePtr(); - vector& ss = pPtr->getAllStructPtr(); - vector& es = pPtr->getAllEnumPtr(); - vector& cs = pPtr->getAllConstPtr(); - - s << endl; - s << TAB << "namespace " << pPtr->getId() << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < cs.size(); i++) - { - s << generateH(cs[i]) << endl; - } - - for (size_t i = 0; i < es.size(); i++) - { - s << generateH(es[i]) << endl; - } - - for (size_t i = 0; i < ss.size(); i++) - { - s << generateH(ss[i], pPtr->getId()) << endl; - } - - s << endl; - - for (size_t i = 0; i < is.size() && _onlyStruct == false; i++) - { - s << generateH(is[i], pPtr) << endl; - s << endl; - } - - DEL_TAB; - s << "}"; - - s << endl << endl; - - return s.str(); -} - - -void Tars2Cpp::generateH(const ContextPtr &pPtr) const -{ - string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())); - - string fileH = _baseDir + "/" + n + ".h"; - - string define = tars::TC_Common::upper("__" + n + "_h_"); - - ostringstream s; - - s << g_parse->printHeaderRemark(); - - s << "#ifndef " << define << endl; - s << "#define " << define << endl; - s << endl; - s << "#include " << endl; - s << "#include " << endl; - s << "#include " << endl; - s << "#include \"tup/Tars.h\"" << endl; - - s << "using namespace std;" << endl; - - vector include = pPtr->getIncludes(); - for (size_t i = 0; i < include.size(); i++) - { - s << "#include \"" << g_parse->getHeader() << tars::TC_File::extractFileName(include[i]) << "\"" << endl; - } - - vector namespaces = pPtr->getNamespaces(); - - //åå­—ç©ºé—´æœ‰æŽ¥å£ - for (size_t i = 0; i < namespaces.size() && _onlyStruct == false; i++) - { - if (namespaces[i]->hasInterface()) - { - s << "#include \"servant/ServantProxy.h\"" << endl; - s << "#include \"servant/Servant.h\"" << endl; - s << "#include \"promise/promise.h\"" << endl; - break; - } - } - - s << endl; - - for (size_t i = 0; i < namespaces.size(); i++) - { - s << generateH(namespaces[i]) << endl; - } - - s << endl; - s << "#endif" << endl; - - tars::TC_File::makeDirRecursive(_baseDir, 0755); - tars::TC_File::save2file(fileH, s.str()); -} - -void Tars2Cpp::createFile(const string& file, const vector& vsCoder) -{ - std::vector contexts = g_parse->getContexts(); - for (size_t i = 0; i < contexts.size(); i++) - { - if (file == contexts[i]->getFileName()) - { - if (vsCoder.size() == 0) - { - generateH(contexts[i]); - } - else - { - for (size_t j = 0; j < vsCoder.size(); j++) - { - generateCoder(contexts[i], vsCoder[j]); - } - } - } - } -} - -StructPtr Tars2Cpp::findStruct(const ContextPtr& pPtr, const string& id) -{ - string sid = id; - - //在当å‰namespace中查找 - vector namespaces = pPtr->getNamespaces(); - for (size_t i = 0; i < namespaces.size(); i++) - { - NamespacePtr np = namespaces[i]; - vector structs = np->getAllStructPtr(); - - for (size_t i = 0; i < structs.size(); i++) - { - if (structs[i]->getSid() == sid) - { - return structs[i]; - } - } - } - - return NULL; -} - -//////////////////////////////// -//for coder generating -//////////////////////////////// - -string Tars2Cpp::generateCoder(const NamespacePtr& pPtr, const string& sInterface) const -{ - ostringstream s; - vector& is = pPtr->getAllInterfacePtr(); - vector& ss = pPtr->getAllStructPtr(); - vector& es = pPtr->getAllEnumPtr(); - vector& cs = pPtr->getAllConstPtr(); - - s << endl; - s << TAB << "namespace " << pPtr->getId() << endl; - s << TAB << "{" << endl; - INC_TAB; - - for (size_t i = 0; i < cs.size(); i++) - { - s << generateH(cs[i]) << endl; - } - - for (size_t i = 0; i < es.size(); i++) - { - s << generateH(es[i]) << endl; - } - - for (size_t i = 0; i < ss.size(); i++) - { - s << generateH(ss[i], pPtr->getId()) << endl; - } - - s << endl; - - for (size_t i = 0; i < is.size(); i++) - { - if (pPtr->getId() + "::" + is[i]->getId() == sInterface) - { - s << generateCoder(is[i]) << endl; - s << endl; - } - } - - DEL_TAB; - s << "}"; - - s << endl << endl; - - return s.str(); -} - -string Tars2Cpp::generateCoder(const InterfacePtr& pPtr) const -{ - ostringstream s; - - vector& vOperation = pPtr->getAllOperationPtr(); - - //生æˆç¼–解ç ç±» - s << TAB << "// encode and decode for client" << endl; - s << TAB << "class " << pPtr->getId() << "Coder" << endl; - s << TAB << "{" << endl; - s << TAB << "public:" << endl << endl; - INC_TAB; - s << TAB << "typedef map TARS_CONTEXT;" << endl << endl; - - s << TAB << "enum enumResult" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << "eTarsServerSuccess = 0," << endl; - s << TAB << "eTarsPacketLess = 1," << endl; - s << TAB << "eTarsPacketErr = 2," << endl; - s << TAB << "eTarsServerDecodeErr = -1," << endl; - s << TAB << "eTarsServerEncodeErr = -2," << endl; - s << TAB << "eTarsServerNoFuncErr = -3," << endl; - s << TAB << "eTarsServerNoServantErr = -4," << endl; - s << TAB << "eTarsServerQueueTimeout = -6," << endl; - s << TAB << "eTarsAsyncCallTimeout = -7," << endl; - s << TAB << "eTarsProxyConnectErr = -8," << endl; - s << TAB << "eTarsServerUnknownErr = -99," << endl; - - DEL_TAB; - s << TAB << "};" << endl << endl; - - for (size_t i = 0; i < vOperation.size(); i++) - { - s << generateCoder(vOperation[i]) << endl; - } - - DEL_TAB; - s << TAB << "protected:" << endl << endl; - INC_TAB; - s << TAB << "static " + _namespace + "::Int32 fetchPacket(const string & in, string & out)" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "if(in.length() < sizeof(" + _namespace + "::Int32)) return eTarsPacketLess;" << endl; - - s << TAB << "" + _namespace + "::Int32 iHeaderLen;" << endl; - s << TAB << "memcpy(&iHeaderLen, in.c_str(), sizeof(" + _namespace + "::Int32));" << endl; - - s << TAB << "iHeaderLen = ntohl(iHeaderLen);" << endl; - s << TAB << "if(iHeaderLen < (" + _namespace + "::Int32)sizeof(" + _namespace + "::Int32) || iHeaderLen > 100000000) return eTarsPacketErr;" << endl; - s << TAB << "if((" + _namespace + "::Int32)in.length() < iHeaderLen) return eTarsPacketLess;" << endl; - - s << TAB << "out = in.substr(sizeof(" + _namespace + "::Int32), iHeaderLen - sizeof(" + _namespace + "::Int32)); " << endl; - s << TAB << "return 0;" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - s << TAB << "static string encodeBasePacket(const string & sServantName, const string & sFuncName, const vector & buffer, " - << "const map& context = TARS_CONTEXT())" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> os;" << endl; - s << TAB << "os.write(1, 1);" << endl; - s << TAB << "os.write(0, 2);" << endl; - s << TAB << "os.write(0, 3);" << endl; - s << TAB << "os.write(0, 4);" << endl; - s << TAB << "os.write(sServantName, 5);" << endl; - s << TAB << "os.write(sFuncName, 6);" << endl; - s << TAB << "os.write(buffer, 7);" << endl; - s << TAB << "os.write(60, 8);" << endl; - s << TAB << "os.write(context, 9);" << endl; - s << TAB << "os.write(map(), 10);" << endl; - - s << TAB << _namespace + "::Int32 iHeaderLen;" << endl; - s << TAB << "iHeaderLen = htonl(sizeof(" + _namespace + "::Int32) + os.getLength());" << endl; - s << TAB << "string s;" << endl; - s << TAB << "s.append((const char*)&iHeaderLen, sizeof(" + _namespace + "::Int32));" << endl; - s << TAB << "s.append(os.getBuffer(), os.getLength());" << endl; - - s << TAB << "return s;" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - s << TAB << "static " + _namespace + "::Int32 decodeBasePacket(const string & in, " + _namespace + "::Int32 & iServerRet, vector & buffer)" << endl; - s << TAB << "{" << endl; - INC_TAB; - - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> is;" << endl; - s << TAB << "is.setBuffer(in.c_str(), in.length());" << endl; - s << TAB << "is.read(iServerRet, 5, true);" << endl; - s << TAB << "is.read(buffer, 6, true);" << endl; - - s << TAB << "return 0;" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - DEL_TAB; - s << TAB << "};" << endl; - - return s.str(); -} - -string Tars2Cpp::generateCoder(const OperationPtr& pPtr) const -{ - ostringstream s; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - //ç¼–ç å‡½æ•° - s << TAB << "//encode & decode function for '" << pPtr->getId() << "()'" << endl << endl; - s << TAB << "static string encode_" << pPtr->getId() << "(const string & sServantName, "; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) - { - s << generateH(vParamDecl[i]) << ","; - } - } - s << endl; - s << TAB << " const map& context = TARS_CONTEXT())" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "try" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << _namespace + "::TarsOutputStream<" + _namespace + "::BufferWriter> _os;" << endl; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) continue; - s << writeTo(vParamDecl[i]->getTypeIdPtr()); - } - - s << TAB << "return encodeBasePacket(sServantName, \"" << pPtr->getId() << "\", _os.getByteBuffer(), context);" << endl; - - DEL_TAB; - - s << TAB << "}" << endl; - s << TAB << "catch (" + _namespace + "::TarsException & ex)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return \"\";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - //解ç å‡½æ•° - - s << TAB << "static " + _namespace + "::Int32 decode_" << pPtr->getId() << "(const string & in "; - - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << ", " << tostr(pPtr->getReturnPtr()->getTypePtr()) << " & _ret "; - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if(!vParamDecl[i]->isOut()) - continue; - - s << ", " << generateH(vParamDecl[i]); - } - s << ")" << endl; - - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "try" << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "string out;" << endl; - s << TAB << _namespace + "::Int32 iRet = 0;" << endl; - s << TAB << "if((iRet = fetchPacket(in, out)) != 0) return iRet;" << endl; - - s << TAB << _namespace + "::TarsInputStream<" + _namespace + "::BufferReader> _is;" << endl; - s << TAB << _namespace + "::Int32 iServerRet=0;" << endl; - s << TAB << "vector buffer;" << endl; - s << TAB << "decodeBasePacket(out, iServerRet, buffer);" << endl; - s << TAB << "if(iServerRet != 0) return iServerRet;" << endl; - - s << TAB << "_is.setBuffer(buffer);" << endl; - - if (pPtr->getReturnPtr()->getTypePtr()) - { - s << readFrom(pPtr->getReturnPtr()); - } - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - s << readFrom(vParamDecl[i]->getTypeIdPtr()); - } - } - - - s << TAB << "return 0;" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "catch (" + _namespace + "::TarsException & ex)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "return eTarsPacketErr;" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - - - s << endl; - - return s.str(); -} - -void Tars2Cpp::generateCoder(const ContextPtr& pPtr, const string& sInterface) const -{ - cout << "Interface:" << sInterface << endl; - string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())) + "Coder"; - - string fileH = _baseDir + "/" + n + ".h"; - - string define = tars::TC_Common::upper("__" + n + "_h_"); - - ostringstream s; - - s << g_parse->printHeaderRemark(); - - s << "#ifndef " << define << endl; - s << "#define " << define << endl; - s << endl; - s << "#include " << endl; - s << "#include " << endl; - s << "#include " << endl; - s << "#include \"tup/Tars.h\"" << endl; - - s << "using namespace std;" << endl; - - vector include = pPtr->getIncludes(); - for (size_t i = 0; i < include.size(); i++) - { - s << "#include \"" << g_parse->getHeader() - << tars::TC_Common::replace(tars::TC_File::extractFileName(include[i]), ".h", "Coder.h") << "\"" << endl; - } - - vector namespaces = pPtr->getNamespaces(); - - s << endl; - - for (size_t i = 0; i < namespaces.size(); i++) - { - s << generateCoder(namespaces[i], sInterface) << endl; - } - - s << endl; - s << "#endif" << endl; - - tars::TC_File::makeDirRecursive(_baseDir, 0755); - tars::TC_File::save2file(fileH, s.str()); - - return; -} diff --git a/cpp/tools/tars2cpp/tars2cpp.h b/cpp/tools/tars2cpp/tars2cpp.h deleted file mode 100644 index 25bae6631..000000000 --- a/cpp/tools/tars2cpp/tars2cpp.h +++ /dev/null @@ -1,440 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS2CPP_H_ -#define __TARS2CPP_H_ - -#include "parse.h" - -#include -#include - -using namespace tars; - -/** - * æ ¹æ®tars生æˆc++文件 - * 包括结构的编解ç ä»¥åŠç”ŸæˆProxyå’ŒServant - */ -class Tars2Cpp -{ -public: - - Tars2Cpp(); - - /** - * ç”Ÿæˆ - * @param file - */ - void createFile(const string &file, const vector &vsCoder); - - /** - * 设置生æˆæ–‡ä»¶çš„目录 - * - */ - void setBaseDir(const std::string & sPath) { _baseDir = sPath; } - - /** - * 设置是å¦éœ€è¦æ‰“包默认值 - */ - void setCheckDefault(bool bCheck) { _checkDefault = bCheck; } - - /** - * 设置是å¦åªç”Ÿæˆstruct - */ - void setOnlyStruct(bool bOnlyStruct) { _onlyStruct = bOnlyStruct; } - - /** - * setUnknownField for UnknownField support - */ - void setUnknownField(bool bUnkownField) { _unknownField = bUnkownField; } - - /** - * added for master info - */ - void setTarsMaster(bool bTarsMaster) { _tarsMaster = bTarsMaster; } - - - //下é¢æ˜¯ç¼–解ç çš„æºç ç”Ÿæˆ -protected: - - /** - * 生æˆæŸç±»åž‹çš„解ç æºç  - * @param pPtr - * - * @return string - */ - string writeTo(const TypeIdPtr &pPtr) const; - - /** - * 生æˆæŸç±»åž‹çš„ç¼–ç æºç  - * @param pPtr - * - * @return string - */ - string readFrom(const TypeIdPtr &pPtr, bool bIsRequire = true) const; - - /** - * 生æˆæŸç±»åž‹çš„ç¼–ç æºç  - * @param pPtr - * - * @return string - */ - string promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire = true) const; - - /** - * - * @param pPtr - * - * @return string - */ - string readUnknown(const TypeIdPtr &pPtr) const; - /** - * - * @param pPtr - * - * @return string - */ - string writeUnknown()const; - /** - * - * @param pPtr - * - * @return string - */ - string display(const TypeIdPtr &pPtr) const; - - /** - * - * @param pPtr - * - * @return string - */ - string displaySimple(const TypeIdPtr &pPtr, bool bSep) const; - - /** - * - * @param pPtr - * - * @return string - */ - string generateCollection(const TypeIdPtr &pPtr, bool bSep) const; - - //下é¢æ˜¯ç±»åž‹æè¿°çš„æºç ç”Ÿæˆ -protected: - /** - * 生æˆæŸç±»åž‹çš„字符串æè¿°æºç  - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr &pPtr) const; - - /** - * 生æˆå†…建类型的字符串æºç  - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr &pPtr) const; - /** - * 生æˆvector的字符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr &pPtr) const; - - /** - * 生æˆmap的字符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr &pPtr) const; - - /** - * 生æˆæŸç§ç»“构的符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr &pPtr) const; - - /** - * 生æˆæŸç§æžšä¸¾çš„符串æè¿° - * @param pPtr - * - * @return string - */ - string tostrEnum(const EnumPtr &pPtr) const; - - /** - * 获å–定长数组的åæ ‡ - * @param pPtr - * - * @return string - */ - - string toStrSuffix(const TypeIdPtr &pPtr) const; - - /** - * 获å–定长数组的åæ ‡ - * @param pPtr - * - * @return int - */ - int getSuffix(const TypeIdPtr &pPtr) const; - - /** - * 生æˆunsigned转æ¢ä¸ºsigned的符串æè¿° - * @param pPtr - * - * @return string - */ - //string unsigned2signedStr(const BuiltinPtr &pPtr) const; - - /** - * 生æˆç±»åž‹å˜é‡çš„解ç æºç  - * @param pPtr - * - * @return string - */ - string decode(const TypeIdPtr &pPtr) const; - - /** - * 生æˆç±»åž‹å˜é‡çš„ç¼–ç æºç  - * @param pPtr - * - * @return string - */ - string encode(const TypeIdPtr &pPtr) const; - - //以下是hæ–‡ä»¶çš„å…·ä½“ç”Ÿæˆ -protected: - /** - * 结构的md5 - * @param pPtr - * - * @return string - */ - string MD5(const StructPtr &pPtr) const; - - /** - * 生æˆç»“构的头文件内容 - * @param pPtr - * - * @return string - */ - string generateH(const StructPtr &pPtr, const string& namespaceId) const; - - /** - * 生æˆå®¹å™¨çš„头文件æºç  - * @param pPtr - * - * @return string - */ - string generateH(const ContainerPtr &pPtr) const; - - /** - * 生æˆå‚数声明的头文件内容 - * @param pPtr - * - * @return string - */ - string generateH(const ParamDeclPtr &pPtr) const; - - /** - * - * @param pPtr - * - * @return string - */ - string generateOutH(const ParamDeclPtr &pPtr) const; - - /** - * - * @param pPtr - * @param cn - * - * @return string - */ - string generateAsyncResponseCpp(const OperationPtr &pPtr, const string &cn) const; - - /** - * 生æˆå‚数声明的内容 - * @param pPtr - * - * @return string - */ - string generateParamDecl(const ParamDeclPtr &pPtr) const; - - /** - * 生æˆæ“作函数调用分å‘çš„æºç  - * @param pPtr - * @param cn - * - * @return string - */ - string generateDispatchAsync(const OperationPtr &pPtr, const string &cn) const; - - /** - * 生æˆæ“作函数调用分å‘çš„æºç  - * @param pPtr - * @param cn - * - * @return string - */ - string generateDispatchCoroAsync(const OperationPtr &pPtr, const string &cn) const; - - /** - * 生æˆservantæ“作函数调用的æºç  - * @param pPtr - * @param cn - * - * @return string - */ - string generateServantDispatch(const OperationPtr& pPtr, const string& cn) const; - - /** - * 生æˆæ“作的servant的头文件æºç  - * @param pPtr - * @param bVirtual - * - * @return string - */ - string generateHAsync(const OperationPtr &pPtr) const; - - /** - * 生æˆæ“作的servant的头文件æºç  - * @param pPtr - * @param bVirtual - * - * @return string - */ - string generateHAsync(const OperationPtr &pPtr, const string& interfaceId) const; - - /** - * 生æˆæ“作的servant的头文件æºç  - * @param pPtr - * @param bVirtual - * - * @return string - */ - string generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const; - - /** - * 生æˆæ“作函数调用分å‘çš„æºç  - * @param pPtr - * @param cn - * - * @return string - */ - string generateDispatchPromiseAsync(const OperationPtr &pPtr, const string &cn) const; - - /** - * 生æˆæ“作的servant的头文件æºç  - * @param pPtr - * @param bVirtual - * - * @return string - */ - string generateH(const OperationPtr &pPtr, bool bVirtual, const string& interfaceId) const; - - /** - * 生æˆæŽ¥å£çš„头文件æºç  - * @param pPtr - * - * @return string - */ - string generateH(const InterfacePtr &pPtr, const NamespacePtr &nPtr) const; - - /** - * 生æˆæžšä¸¾çš„头文件æºç  - * @param pPtr - * - * @return string - */ - string generateH(const EnumPtr &pPtr) const; - - /** - * 生æˆå¸¸é‡å¤´æ–‡ä»¶æºç  - * @param pPtr - * - * @return string - */ - string generateH(const ConstPtr &pPtr) const; - - /** - * 生æˆå字空间的头文件æºç  - * @param pPtr - * - * @return string - */ - string generateH(const NamespacePtr &pPtr) const; - - /** - * 生æˆå字空间的头文件æºç  - * @param pPtr - * - * @return string - */ - void generateH(const ContextPtr &pPtr) const; - - /** - * - * æ ¹æ®å字查找结构 - * @param pPtr - * @param id - * - * @return StructPtr - */ - StructPtr findStruct(const ContextPtr &pPtr,const string &id); - - /** - * - * 生æˆæŽ¥å£ç¼–解ç ä»£ç  - * @param pPtr - * @param interface - */ - void generateCoder(const ContextPtr &pPtr,const string &interface) const; - - string generateCoder(const NamespacePtr &pPtr,const string & sInterface) const; - - string generateCoder(const InterfacePtr &pPtr) const; - - string generateCoder(const OperationPtr &pPtr) const; - - string generateInitValue(const TypeIdPtr &pPtr) const; - - bool isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const; - -private: - std::string _baseDir; - - bool _checkDefault; - - bool _onlyStruct; - - std::string _namespace ; - - bool _unknownField; - - bool _tarsMaster; -}; - -#endif - - diff --git a/cpp/tools/tars2cs/CMakeLists.txt b/cpp/tools/tars2cs/CMakeLists.txt deleted file mode 100644 index 2f4b2ad53..000000000 --- a/cpp/tools/tars2cs/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -tars2language("tars2cs") diff --git a/cpp/tools/tars2cs/main.cpp b/cpp/tools/tars2cs/main.cpp deleted file mode 100644 index 489aa067c..000000000 --- a/cpp/tools/tars2cs/main.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "tars2cs.h" - -void usage() -{ - cout << "Usage : tars2cs [OPTION] tarsfile" << endl; - cout << " tars2cs support type: bool byte short int long float double vector map" << endl; - cout << "supported [OPTION]:" << endl; - cout << " --help help,print this(°ïÖú)" << endl; - cout << " --dir=DIRECTORY generate java file to DIRECTORY(Éú³ÉÎļþµ½Ä¿Â¼DIRECTORY,ĬÈÏΪµ±Ç°Ä¿Â¼)" << endl; - cout << " --base-package=NAME package prefix, default 'com.qq.'(packageǰ׺£¬Î´Ö¸¶¨ÔòĬÈÏΪcom.qq.)" << endl; - cout << " --with-servant also generate servant class(Ò»²¢Éú³É·þÎñ¶Ë´úÂ룬δָ¶¨ÔòĬÈϲ»Éú³É)" << endl; - cout << endl; - exit(0); -} - -void check(vector &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - Tars2Cs t2cs; - - - //ÉèÖÃÉú³ÉÎļþµÄ¸ùĿ¼ - if(option.getValue("dir") != "") - { - t2cs.setBaseDir(option.getValue("dir")); - } - else - { - t2cs.setBaseDir("."); - } - - //°üÃûǰ׺ - if(option.hasParam("base-package")) - { - t2cs.setBasePackage(option.getValue("base-package")); - } - else - { - t2cs.setBasePackage("Com.QQ."); - } - - //ÊÇ·ñÉú³É·þÎñ¶ËÀà,ĬÈϲ»Éú³É - if(option.hasParam("with-servant")) - { - t2cs.setWithServant(true); - } - else - { - t2cs.setWithServant(false); - } - try - { - //ÊÇ·ñ¿ÉÒÔÒÔtars¿ªÍ· - g_parse->setTars(option.hasParam("with-tars")); - - for(size_t i = 0; i < vTars.size(); i++) - { - g_parse->parse(vTars[i]); - t2cs.createFile(vTars[i]); - } - }catch(exception& e) - { - cerr< -#include - -#define TARS_PACKAGE "Wup.Tars" - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -////////////////////////////////////////////////////////////////////////////////// -string Tars2Cs::toTypeInit(const TypePtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool: return "false;"; - case Builtin::KindByte: return "0;"; - case Builtin::KindShort: return "0;"; - case Builtin::KindInt: return "0;"; - case Builtin::KindLong: return "0L;"; - case Builtin::KindFloat: return "0.0f;"; - case Builtin::KindDouble: return "0.0;"; - case Builtin::KindString: return "\"\";"; - default: return ""; - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - //Êý×éÌØÊâ´¦Àí - string sType; - size_t iPosBegin, iPosEnd; - sType = tostr(vPtr->getTypePtr()); - if ((iPosBegin = sType.find("<")) != string::npos && (iPosEnd = sType.rfind(">")) != string::npos) - { - sType = sType.substr(0, iPosBegin) + sType.substr(iPosEnd+1); - } - //[] (Êý×é)µÄÊý×é±äΪ[1] - sType = tars::TC_Common::replace(sType, "[]" , "[1]"); - return "(" + tostr(vPtr->getTypePtr()) + "[]) new " + sType + "[1];";; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return "new " + tostrMap(mPtr, true) + "();"; - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return "new " + tostrStruct(sPtr) + "();"; - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return "0;"; - - return ""; -} - -string Tars2Cs::toObjStr(const TypePtr &pPtr) const -{ - string sType = tostr(pPtr); - - if (sType == "bool") return "bool"; - if (sType == "byte") return "byte"; - if (sType == "short" ) return "short"; - if (sType == "int" ) return "int"; - if (sType == "long" ) return "long"; - if (sType == "float" ) return "float"; - if (sType == "double" ) return "double"; - - return sType; -} - -string Tars2Cs::tostr(const TypePtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return tostrStruct(sPtr); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - -/*******************************BuiltinPtr********************************/ -string Tars2Cs::tostrBuiltin(const BuiltinPtr &pPtr) const -{ - string s; - - switch (pPtr->kind()) - { - case Builtin::KindBool: s = "bool"; break; - case Builtin::KindByte: s = "byte"; break; - case Builtin::KindShort: s = "short"; break; - case Builtin::KindInt: s = "int"; break; - case Builtin::KindLong: s = "long"; break; - case Builtin::KindFloat: s = "float"; break; - case Builtin::KindDouble: s = "double"; break; - case Builtin::KindString: s = "string"; break; - case Builtin::KindVector: s = "System.Collections.Generic.List"; break; - case Builtin::KindMap: s = "System.Collections.Generic.Dictionary"; break; - default: assert(false); break; - } - - return s; -} - -string Tars2Cs::tostrVector(const VectorPtr &pPtr) const -{ - string s = "System.Collections.Generic.List" + string("<") + toObjStr(pPtr->getTypePtr()) + ">"; - return s; -} - -string Tars2Cs::tostrMap(const MapPtr &pPtr, bool bNew) const -{ - string s; - if (!bNew) - { - s = "System.Collections.Generic.Dictionary"; - } - else - { - s = "System.Collections.Generic.Dictionary"; - } - s += string("<") + toObjStr(pPtr->getLeftTypePtr()) + ", " + toObjStr(pPtr->getRightTypePtr()) + ">"; - - return s; -} - -string Tars2Cs::tostrStruct(const StructPtr &pPtr) const -{ - return tars::TC_Common::replace(pPtr->getSid(), "::", ".").substr(tars::TC_Common::replace(pPtr->getSid(), "::", ".").find(".")+1); -} - -string Tars2Cs::tostrEnum(const EnumPtr &pPtr) const -{ - return "int"; -} - -/******************************StructPtr***************************************/ -string Tars2Cs::generateCs(const StructPtr &pPtr, const NamespacePtr &nPtr) const -{ - ostringstream s; - s << g_parse->printHeaderRemark(); - - vector key = pPtr->getKey(); - vector& member = pPtr->getAllMemberPtr(); - - s << TAB << "using System;"<getId()<< endl; - s << "{"<getId() << " : " TARS_PACKAGE ".TarsStruct"<< endl; - s << TAB << "{" << endl; - INC_TAB; - - //¶¨Òå³ÉÔ±±äÁ¿set;getº¯Êý - for (size_t i = 0; i < member.size(); i++) - { - string sDefault; - if (member[i]->hasDefault()) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[i]->getTypePtr()); - - if (bPtr && bPtr->kind() == Builtin::KindString) - { - sDefault = tars::TC_Common::replace(member[i]->def(), "\"", "\\\""); - sDefault = " = \"" + sDefault + "\""; - } - else - { - sDefault = " = " + member[i]->def(); - } - - s << TAB << tostr(member[i]->getTypePtr()) << " _" << member[i]->getId() << sDefault << ";" << endl; - s << TAB << "public " << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() << endl; - s << TAB << "{" << endl; - - INC_TAB; - s << TAB << "get" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << " return _" << member[i]->getId() << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "set" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << "_" << member[i]->getId() << " = value; " << endl; - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - - s << TAB << "}" << endl; - } - else - { - sDefault = " {get; set;} "; - s << TAB << "public " << tostr(member[i]->getTypePtr()) << " " << member[i]->getId() << sDefault << endl; - } - - s << endl; - } - - //writeTo() - s << TAB << "public override void WriteTo(" TARS_PACKAGE ".TarsOutputStream _os)" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "_os.Write(" << member[i]->getId() - << ", " << member[i]->getTag() << ");" << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //readFrom() - s << TAB << "public override void ReadFrom(" TARS_PACKAGE ".TarsInputStream _is)" << endl; - s << TAB << "{" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << member[i]->getId() << " = (" + tostr(member[i]->getTypePtr()) + ")" - << " _is.Read(" << member[i]->getId() << ", " << member[i]->getTag() << ", " - << (member[i]->isRequire() ? "true" : "false") << ");" << endl << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - //display() - s << TAB << "public override void Display(System.Text.StringBuilder _os, int _level)" << endl; - s << TAB << "{" << endl; - INC_TAB; - s << TAB << TARS_PACKAGE ".TarsDisplayer _ds = new " TARS_PACKAGE ".TarsDisplayer(_os, _level);" << endl; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "_ds.Display(" << member[i]->getId() << ", \"" << member[i]->getId() << "\");" << endl; - } - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << endl; - - string fileCs = getFilePath(nPtr->getId()) + pPtr->getId() + ".cs"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); - tars::TC_File::save2file(fileCs, s.str()); - - return s.str(); -} - -/******************************ConstPtr***************************************/ -void Tars2Cs::generateCs(const vector &es,const vector &cs,const NamespacePtr &nPtr) const -{ - if (es.size()==0 && cs.size()==0) - { - return; - } - ostringstream s; - s << g_parse->printHeaderRemark(); - - s << TAB << "using System;"<getId()<< endl; - s << "{"<0) - { - s << TAB << "class Const " << endl; - s << TAB << "{" << endl; - INC_TAB; - //-----------------constÀàÐÍ¿ªÊ¼------------------------------------ - for (size_t i = 0; i < cs.size(); i++) - { - if (cs[i]->getConstGrammarPtr()->t == ConstGrammar::STRING) - { - string tmp = tars::TC_Common::replace(cs[i]->getConstGrammarPtr()->v, "\"", "\\\""); - s << TAB << "public const "<< tostr(cs[i]->getTypeIdPtr()->getTypePtr()) << " "<getTypeIdPtr()->getId()<< " = \"" - << tmp << "\";"<< endl; - } - else if (tostr(cs[i]->getTypeIdPtr()->getTypePtr()) == "long") - { - s << TAB << "public const "<< tostr(cs[i]->getTypeIdPtr()->getTypePtr()) << " "<getTypeIdPtr()->getId()<< " = " - << cs[i]->getConstGrammarPtr()->v << "L;" << endl; - } - else - { - s << TAB << "public const "<< tostr(cs[i]->getTypeIdPtr()->getTypePtr()) <<" "<getTypeIdPtr()->getId()<< " = " - << cs[i]->getConstGrammarPtr()->v << ";" << endl; - } - } - DEL_TAB; - s << TAB << "}" << endl; - } - //-----------------constÀàÐͽáÊø-------------------------------- - if (es.size()>0) - { - //-----------------ö¾ÙÀàÐÍ¿ªÊ¼--------------------------------- - for (size_t i = 0; i < es.size(); i++) - { - s << TAB << "public enum "<getId()<& member = es[i]->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << member[i]->getId(); - if(member[i]->hasDefault()) - { - s << " = " << member[i]->def(); - } - - s <<","<getId()) + nPtr->getId()+"_const.cs"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); - tars::TC_File::save2file(fileCs, s.str()); - - return; -} - -/******************************NamespacePtr***************************************/ -void Tars2Cs::generateCs(const NamespacePtr &pPtr) const -{ - vector &ss = pPtr->getAllStructPtr(); - vector &es = pPtr->getAllEnumPtr(); - vector &cs = pPtr->getAllConstPtr(); - - for (size_t i = 0; i < ss.size(); i++) - { - generateCs(ss[i], pPtr); - } - - generateCs(es,cs,pPtr);//c#ÀïÃæµÄö¾Ù¡¢const¶¼·Åµ½Ò»Æð¡£ - - return; -} - -void Tars2Cs::generateCs(const ContextPtr &pPtr) const -{ - ostringstream s; - - vector namespaces = pPtr->getNamespaces(); - - for (size_t i = 0; i < namespaces.size(); i++) - { - generateCs(namespaces[i]); - } - -} - -void Tars2Cs::createFile(const string &file) -{ - std::vector contexts = g_parse->getContexts(); - for (size_t i = 0; i < contexts.size(); i++) - { - if (file == contexts[i]->getFileName()) - { - generateCs(contexts[i]); - } - } -} - -void Tars2Cs::setBaseDir(const string &dir) -{ - _baseDir = dir; -} - -void Tars2Cs::setBasePackage(const string &prefix) -{ - _packagePrefix = prefix; - if (_packagePrefix.length() != 0 && _packagePrefix.substr(_packagePrefix.length()-1, 1) != ".") - { - _packagePrefix += "."; - } -} - -string Tars2Cs::getFilePath(const string &ns) const -{ - return _baseDir + "/" + tars::TC_Common::replace(_packagePrefix, ".", "/") + "/" + ns + "/"; -} - diff --git a/cpp/tools/tars2cs/tars2cs.h b/cpp/tools/tars2cs/tars2cs.h deleted file mode 100644 index d1b5a717e..000000000 --- a/cpp/tools/tars2cs/tars2cs.h +++ /dev/null @@ -1,245 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef _TARS2CS_H -#define _TARS2CS_H - -#include "parse.h" - -#include -#include - -/** - * ¸ù¾ÝtarsÉú³ÉjavaÎļþ - * °üÀ¨½á¹¹µÄ±à½âÂëÒÔ¼°Éú³ÉProxyºÍServant - */ -class Tars2Cs -{ -public: - /** - * ÉèÖôúÂëÉú³ÉµÄ¸ùĿ¼ - * @param dir - */ - void setBaseDir(const string &dir); - - /** - * ÉèÖðüǰ׺ - * @param prefix - */ - void setBasePackage(const string &prefix); - - /** - * ÉèÖÃÊÇ·ñÐèÒª·þÎñ¶Ë´úÂë - */ - void setWithServant(bool bWithServant) { _bWithServant = bWithServant;} - - /** - * Éú³É - * @param file - * @param isFramework ÊÇ·ñÊÇ¿ò¼Ü - */ - void createFile(const string &file); - -protected: - /** - * ¸ù¾ÝÃüÃû¿Õ¼ä»ñÈ¡Îļþ·¾¶ - * @param ns ÃüÃû¿Õ¼ä - * - * @return string - */ - string getFilePath(const string &ns) const; - - string _packagePrefix; - string _baseDir; - bool _bWithServant; - - //ÏÂÃæÊDZà½âÂëµÄÔ´ÂëÉú³É -protected: - - /** - * Éú³ÉijÀàÐ͵ĽâÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string writeTo(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉijÀàÐ͵ıàÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string readFrom(const TypeIdPtr &pPtr) const; - - /** - * - * @param pPtr - * - * @return string - */ - string display(const TypeIdPtr &pPtr) const; - - //ÏÂÃæÊÇÀàÐÍÃèÊöµÄÔ´ÂëÉú³É -protected: - - /* - * Éú³ÉijÀàÐ͵ijõʼ»¯×Ö·û´® - * @param pPtr - * - * @return string - */ - string toTypeInit(const TypePtr &pPtr) const; - - /** - * Éú³ÉijÀàÐ͵ĶÔÓ¦¶ÔÏóµÄ×Ö·û´®ÃèÊöÔ´Âë - * @param pPtr - * - * @return string - */ - string toObjStr(const TypePtr &pPtr) const; - - /** - * ÅжÏÊÇ·ñÊǶÔÏóÀàÐÍ - */ - bool isObjType(const TypePtr &pPtr) const; - - /** - * Éú³ÉijÀàÐ͵Ä×Ö·û´®ÃèÊöÔ´Âë - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr &pPtr) const; - - /** - * Éú³ÉÄÚ½¨ÀàÐ͵Ä×Ö·û´®Ô´Âë - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr &pPtr) const; - /** - * Éú³ÉvectorµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr &pPtr) const; - - /** - * Éú³ÉmapµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr &pPtr, bool bNew = false) const; - - /** - * Éú³ÉijÖֽṹµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr &pPtr) const; - - /** - * Éú³ÉijÖÖö¾ÙµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrEnum(const EnumPtr &pPtr) const; - - /** - * Éú³ÉÀàÐͱäÁ¿µÄ½âÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string decode(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉÀàÐͱäÁ¿µÄ±àÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string encode(const TypeIdPtr &pPtr) const; - - //ÒÔÏÂÊÇhºÍjavaÎļþµÄ¾ßÌåÉú³É -protected: - /** - * Éú³É½á¹¹µÄjavaÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateCs(const StructPtr &pPtr, const NamespacePtr &nPtr) const; - - /** - * Éú³ÉÈÝÆ÷µÄjavaÔ´Âë - * @param pPtr - * - * @return string - */ - string generateCs(const ContainerPtr &pPtr) const; - - /** - * Éú³É²ÎÊýÉùÃ÷µÄjavaÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateCs(const ParamDeclPtr &pPtr) const; - - /** - * Éú³Éö¾ÙµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateCs(const EnumPtr &pPtr, const NamespacePtr &nPtr) const; - - /** - * Éú³É³£Á¿javaÔ´Âë - * @param pPtr - * - * @return string - */ - void generateCs(const ConstPtr &pPtr, const NamespacePtr &nPtr) const; - - void generateCs(const vector &es,const vector &cs,const NamespacePtr &nPtr) const; - /** - * Éú³ÉÃû×Ö¿Õ¼äjavaÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generateCs(const NamespacePtr &pPtr) const; - - /** - * Éú³Éÿ¸ötarsÎļþµÄjavaÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generateCs(const ContextPtr &pPtr) const; - -}; - -#endif - - diff --git a/cpp/tools/tars2node/CMakeLists.txt b/cpp/tools/tars2node/CMakeLists.txt deleted file mode 100644 index 9ef42174d..000000000 --- a/cpp/tools/tars2node/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# version -set(PARSER_VERSION "${TARS_VERSION}") -set(GENERATOR_VERSION "20180620") - -# namespace -set(IDL_NAMESPACE Tars) -string(TOLOWER ${IDL_NAMESPACE} TC_NAMESPACE) -set(GRAMMAR_NAME Grammar) - -# idl -set(RPC_MODULE_PATH "@tars/rpc") -set(STREAM_MODULE_PATH "@tars/stream") -set(IDL_TYPE "Tars") -set(PROTOCOL_NAME "Tup") - -# flag -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -Wall") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Wall") - -# define -add_definitions(-DPARSER_VERSION="${PARSER_VERSION}") -add_definitions(-DGENERATOR_VERSION="${GENERATOR_VERSION}") -add_definitions(-DTC_NAMESPACE=${TC_NAMESPACE}) -add_definitions(-DIDL_NAMESPACE=${IDL_NAMESPACE}) -add_definitions(-DGRAMMAR_NAME=${GRAMMAR_NAME}) -add_definitions(-DRPC_MODULE_PATH="${RPC_MODULE_PATH}") -add_definitions(-DSTREAM_MODULE_PATH="${STREAM_MODULE_PATH}") -add_definitions(-DEXECUTE_FILENAME="${PROJECT_NAME}") -add_definitions(-DIDL_TYPE="${IDL_TYPE}") -add_definitions(-DPROTOCOL_NAME="${PROTOCOL_NAME}") - -tars2language("tars2node") diff --git a/cpp/tools/tars2node/code_generator.cpp b/cpp/tools/tars2node/code_generator.cpp deleted file mode 100644 index ad80f031d..000000000 --- a/cpp/tools/tars2node/code_generator.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::printHeaderRemark(const string &sTypeName) -{ - ostringstream s; - s << "// **********************************************************************" << endl; - s << "// Parsed By " << IDL_NAMESPACE_STR << "Parser(" << PARSER_VERSION << "), Generated By " << EXECUTE_FILENAME << "(" << GENERATOR_VERSION << ")" << endl; - s << "// " << IDL_NAMESPACE_STR << "Parser Maintained By and " << EXECUTE_FILENAME << " Maintained By " << endl; - s << "// Generated from \"" << TC_File::extractFileName(_sIdlFile) << "\" by " << - (_bEntry ? sTypeName : (_bMinimalMembers ? "Minimal" : "Relation")) << " Mode" << endl; - s << "// **********************************************************************" << endl; - s << endl; - - return s.str(); -} - -void CodeGenerator::createFile(const string &file, const bool bEntry) -{ - _sIdlFile = getRealFileInfo(file); - _bEntry = bEntry; - - g_parse->parse(_sIdlFile); - - vector contexts = g_parse->getContexts(); - - for(size_t i = 0; i < contexts.size(); i++) - { - if (_sIdlFile == contexts[i]->getFileName()) - { - scan(_sIdlFile, true); //分æžæžšä¸¾å€¼ã€ç»“构体所在的文件 - - if (!_bClient && !_bServer) - { - generateJS(contexts[i]); //生æˆå½“å‰æ–‡ä»¶çš„编解ç æ–‡ä»¶ - if(_bDTS) generateDTS(contexts[i]); //ç”Ÿæˆ typescript æ述文件 - } - - if (_bClient) - { - if(!generateJSProxy(contexts[i])) return; //生æˆå½“å‰æ–‡ä»¶çš„客户端代ç†ç±»æ–‡ä»¶ - if(_bDTS) generateDTSProxy(contexts[i]); //生æˆå®¢æˆ·ç«¯ typescript æ述文件 - } - - if (_bServer) - { - if(!generateJSServer(contexts[i])) return; //生æˆå½“å‰æ–‡ä»¶çš„æœåŠ¡ç«¯ä»£ç†ç±»æ–‡ä»¶ - if(_bDTS) generateDTSServer(contexts[i]); //生æˆæœåŠ¡ç«¯ typescript æ述文件 - - generateJSServerImp(contexts[i]); //生æˆå½“å‰æ–‡ä»¶çš„æœåŠ¡ç«¯å®žçŽ°ç±»æ–‡ä»¶ - } - - vector files = contexts[i]->getIncludes(); - for (size_t ii = 0; _bRecursive && ii < files.size(); ii++) - { - CodeGenerator node; - node.setRpcPath(_sRpcPath); - node.setStreamPath(_sStreamPath); - node.setTargetPath(_sToPath); - node.setRecursive(_bRecursive); - node.setUseSpecialPath(_bUseSpecialPath); - node.setUseStringRepresent(_bUseStringRepresent); - node.setStringBinaryEncoding(_bStringBinaryEncoding); - node.setMinimalMembers(_bMinimalMembers); - node.setDependent(_depMembers); - node.setEnableDTS(_bDTS); - - node.createFile(files[ii], false); - } - } - } -} \ No newline at end of file diff --git a/cpp/tools/tars2node/code_generator.h b/cpp/tools/tars2node/code_generator.h deleted file mode 100644 index a1f958ba6..000000000 --- a/cpp/tools/tars2node/code_generator.h +++ /dev/null @@ -1,261 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _CODEGENERATOR_H -#define _CODEGENERATOR_H - -#include "parse.h" -#include "util/tc_file.h" -#include "util/tc_encoder.h" -#include - -#ifndef TAB - #define TAB g_parse->getTab() -#endif - -#ifndef INC_TAB - #define INC_TAB g_parse->incTab() -#endif - -#ifndef DEL_TAB - #define DEL_TAB g_parse->delTab() -#endif - -#define TO_LOWER_STRING(str) TC_Common::lower(str) - -#define DEFINE_STRING(str) string(CSTR(str)) -#define CSTR(str) #str -#define IDL_NAMESPACE_STR DEFINE_STRING(IDL_NAMESPACE) - -#define GET_CONST_GRAMMAR_PTR_V(name, ptr) \ - ptr->getConst##name##Ptr() -#define GET_CONST_GRAMMAR_PTR_BASE(name, ptr) \ - GET_CONST_GRAMMAR_PTR_V(name, ptr) -#define GET_CONST_GRAMMAR_PTR(ptr) \ - GET_CONST_GRAMMAR_PTR_BASE(GRAMMAR_NAME, ptr) - -#define CONST_GRAMMAR_V(name, val) \ - Const##name::val -#define CONST_GRAMMAR_BASE(name, val) \ - CONST_GRAMMAR_V(name, val) -#define CONST_GRAMMAR(val) \ - CONST_GRAMMAR_BASE(GRAMMAR_NAME, val) - -#define PROTOCOL_V(space, protocol, type) \ - space + "Stream." + protocol + "." + TC_Common::upper(protocol) + "_" + type -#define PROTOCOL_SIMPLE PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "SIMPLE") -#define PROTOCOL_COMPLEX PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "COMPLEX") -#define PROTOCOL_VAR TO_LOWER_STRING(PROTOCOL_NAME) - -using namespace TC_NAMESPACE; - -class CodeGenerator -{ -public: - CodeGenerator() - : uiNameIndex(0), - _sRpcPath(RPC_MODULE_PATH), - _sStreamPath(STREAM_MODULE_PATH), - _sToPath("./"), - _bClient(false), - _bServer(false), - _bRecursive(false), - _bUseSpecialPath(false), - _bUseStringRepresent(false), - _bStringBinaryEncoding(false), - _bMinimalMembers(false), - _bDTS(false) {} - - void createFile(const string &file, const bool bEntry = true); - - void setRpcPath(const string & sPath) { _sRpcPath = sPath; } - - void setStreamPath(const string & sPath) { _sStreamPath = sPath; } - - void setEnableClient(bool bEnable) { _bClient = bEnable; } - - void setEnableServer(bool bEnable) { _bServer = bEnable; } - - void setTargetPath(const string & sPath) { _sToPath = sPath + "/"; } - - void setRecursive(bool bEnable) { _bRecursive = bEnable; } - - void setUseSpecialPath(bool bEnable) { _bUseSpecialPath = bEnable; } - - void setUseStringRepresent(bool bEnable) { _bUseStringRepresent = bEnable; } - - void setStringBinaryEncoding(bool bEnable) { _bStringBinaryEncoding = bEnable; } - - void setMinimalMembers(bool bEnable) { _bMinimalMembers = bEnable; } - - void setDependent(set & deps) { _depMembers = deps; } - - void setEnableDTS(bool bEnable) { _bDTS = bEnable; } - -private: - struct ImportFileType - { - enum TYPE_T {EN_ENUM = 10000, EN_ENUM_VALUE, EN_STRUCT}; - int iType; - string sNamespace; - string sTypeName; - string sName; - }; - - struct ImportFile - { - string sFile; - string sModule; - map mapVars; - }; - - uint32_t uiNameIndex; - - map _mapFiles; - - set _depMembers; - - void scan(const string & sFile, bool bNotPrefix); - - string makeName(); - - string findName(const string & sNamespace, const string & sName); - -private: - string toFunctionName(const TypeIdPtr & pPtr, const string &sAction); - - string getDataType(const TypePtr & pPtr); - - string getDtsType(const TypePtr &pPtr, const bool bStream = true); - - string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace); - - string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool bGlobal); - - string generateJS(const StructPtr & pPtr, const string &sNamespace, bool &bNeedAssert); - - string generateJS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream); - - string generateJS(const EnumPtr &pPtr, const string &sNamespace); - - string generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert); - - bool generateJS(const ContextPtr &pPtr); - -private: - string generateJSProxy(const NamespacePtr &nPtr, bool &bNeedRpc, bool &bNeedStream); - - string generateJSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr); - - string generateJSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr); - - bool generateJSProxy(const ContextPtr &pPtr); - -private: - string generateJSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc, bool &bNeedAssert); - - string generatePing(const NamespacePtr &nPtr, const InterfacePtr &pPtr); - - string generateAsync(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr); - - string generateDispatch(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr); - - string generateJSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr); - - string generateJSServer(const InterfacePtr &pPtr, const NamespacePtr &nPtr); - - bool generateJSServer(const ContextPtr &pPtr); - -private: - string generateJSServerImp(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr); - - string generateJSServerImp(const NamespacePtr &nPtr, const InterfacePtr &pPtr); - - string generateJSServerImp(const ContextPtr &cPtr, const NamespacePtr &nPtr); - - void generateJSServerImp(const ContextPtr &cPtr); - -private: - string generateDTS(const StructPtr &pPtr, const string &sNamespace); - - string generateDTS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream); - - string generateDTS(const EnumPtr &pPtr, const string &sNamespace); - - string generateDTS(const NamespacePtr &pPtr, bool &bNeedStream); - - string generateDTS(const NamespacePtr &pPtr, const string &sContent); - - void generateDTS(const ContextPtr &cPtr); - -private: - string generateDTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc); - - string generateDTSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr); - - void generateDTSServer(const ContextPtr &cPtr); - -private: - string generateDTSProxy(const InterfacePtr &pPtr); - - string generateDTSProxy(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc); - - void generateDTSProxy(const ContextPtr &pPtr); - -private: - void makeUTF8File(const string & sFileName, const string & sFileContent); - - bool isSimple(const TypePtr & pPtr) const; - - bool isBinBuffer(const TypePtr & pPtr) const; - - bool isRawOrString(const TypePtr & pPtr) const; - - bool isDependent(const string & sNamespace, const string & sName) const; - - string getRealFileInfo(const string & sPath); - - string printHeaderRemark(const string & sTypeName); - -private: - string _sRpcPath; - - string _sStreamPath; - - string _sToPath; - - bool _bClient; - - bool _bServer; - - bool _bRecursive; - - bool _bUseSpecialPath; - - bool _bUseStringRepresent; - - bool _bStringBinaryEncoding; - - bool _bMinimalMembers; - - bool _bEntry; - - string _sIdlFile; - - bool _bDTS; -}; - -#endif \ No newline at end of file diff --git a/cpp/tools/tars2node/file_util.cpp b/cpp/tools/tars2node/file_util.cpp deleted file mode 100644 index 9f7fe9e23..000000000 --- a/cpp/tools/tars2node/file_util.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -void CodeGenerator::makeUTF8File(const string& sFileName, const string& sFileContent) -{ - char header[3] = {(char)(0xef), (char)(0xbb), (char)(0xbf)}; - - string sData(header, 3); - - sData += TC_Encoder::gbk2utf8(sFileContent); - - TC_File::save2file(sFileName, sData.c_str()); -} - -string CodeGenerator::getRealFileInfo(const string& sPath) -{ - return TC_File::extractFilePath(sPath) + TC_File::excludeFileExt(TC_File::extractFileName(sPath)) + "." + TC_Common::lower(IDL_TYPE); -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_js.cpp b/cpp/tools/tars2node/gen_js.cpp deleted file mode 100644 index b4b073928..000000000 --- a/cpp/tools/tars2node/gen_js.cpp +++ /dev/null @@ -1,388 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::generateJS(const EnumPtr &pPtr, const string &sNamespace) -{ - ostringstream s; - s << TAB << sNamespace << "." << pPtr->getId() << " = {" << endl; - INC_TAB; - - //æˆå‘˜å˜é‡ - int nenum = -1; - bool bDependent = false; - vector& member = pPtr->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - bDependent |= isDependent(sNamespace, member[i]->getId()); - - if (member[i]->hasDefault()) - { - nenum = TC_Common::strto(member[i]->def()); - } - else - { - nenum++; - } - - s << TAB << "\"" << member[i]->getId() << "\" : " << TC_Common::tostr(nenum) << "," << endl; - } - s << TAB << "\"_classname\" : \"" << sNamespace << "." << pPtr->getId() << "\"" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - //函数 - s << TAB << sNamespace << "." << pPtr->getId() << "._write = function(os, tag, val) { return os.writeInt32(tag, val); };" << endl; - s << TAB << sNamespace << "." << pPtr->getId() << "._read = function(is, tag, def) { return is.readInt32(tag, true, def); };" << endl; - - if (!_bMinimalMembers || _bEntry || bDependent || isDependent(sNamespace, pPtr->getId())) { - return s.str(); - } else { - return ""; - } -} - -string CodeGenerator::generateJS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream) -{ - if (_bMinimalMembers && !_bEntry && !isDependent(sNamespace, pPtr->getTypeIdPtr()->getId())) - { - return ""; - } - - ostringstream s; - - if (_bStringBinaryEncoding && GET_CONST_GRAMMAR_PTR(pPtr)->t == CONST_GRAMMAR(STRING)) - { - bNeedStream = true; - } - - s << TAB << sNamespace << "." << pPtr->getTypeIdPtr()->getId() << " = " << getDefault(pPtr->getTypeIdPtr(), GET_CONST_GRAMMAR_PTR(pPtr)->v, sNamespace, false) << ";" << endl; - - return s.str(); -} - -string CodeGenerator::generateJS(const StructPtr & pPtr, const string &sNamespace, bool &bNeedAssert) -{ - if (_bMinimalMembers && !_bEntry && !isDependent(sNamespace, pPtr->getId())) - { - return ""; - } - - ostringstream s; - vector& member = pPtr->getAllMemberPtr(); - - s << TAB << sNamespace << "." << pPtr->getId() << " = function() {"<< endl; - - INC_TAB; - - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "this." << member[i]->getId() << " = " << getDefault(member[i], member[i]->def(), sNamespace) << ";" << endl; - } - - { - s << TAB << "this._classname = \"" << sNamespace << "." << pPtr->getId() << "\";" << endl; - } - - DEL_TAB; - - s << TAB << "};" << endl; - s << TAB << sNamespace << "." << pPtr->getId() << "._classname = \"" << sNamespace << "." << pPtr->getId() << "\";" << endl; - - string sProto = TC_Common::replace(pPtr->getSid(), "::", "."); - s << TAB << sProto << "._write = function (os, tag, value) { os.writeStruct(tag, value); };" << endl; - s << TAB << sProto << "._read = function (is, tag, def) { return is.readStruct(tag, true, def); };" << endl; - - //_readFrom - s << TAB << sNamespace << "." << pPtr->getId() << "._readFrom = function (is) {" << endl; - INC_TAB; - s << TAB << "var tmp = new " << sNamespace << "." << pPtr->getId() << "();" << endl; - for (size_t i = 0; i < member.size(); i++) - { - string sFuncName = toFunctionName(member[i], "read"); - s << TAB << "tmp." << member[i]->getId() << " = is." << sFuncName << "(" << member[i]->getTag() - << ", " << (member[i]->isRequire()?"true":"false") << ", "; - - if (isSimple(member[i]->getTypePtr())) - { - s << getDefault(member[i], member[i]->def(), sNamespace) - << (isRawOrString(member[i]->getTypePtr()) ? ", 1" : ""); - } - else - { - s << getDataType(member[i]->getTypePtr()); - } - - s << ");" << endl; - } - s << TAB << "return tmp;" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - //_writeTo - s << TAB << sNamespace << "." << pPtr->getId() << ".prototype._writeTo = function (os) {" << endl; - - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - string sFuncName = toFunctionName(member[i], "write"); - - s << TAB << "os." << sFuncName << "(" << member[i]->getTag() << ", this." << member[i]->getId() - << (isRawOrString(member[i]->getTypePtr()) ? ", 1" : "") << ");" << endl; - } - DEL_TAB; - s << TAB << "};" << endl; - - //_equal - vector key = pPtr->getKey(); - - s << TAB << sNamespace << "." << pPtr->getId() << ".prototype._equal = function (" << (key.size() > 0 ? "anItem" : "") << ") {" << endl; - - INC_TAB; - - if (key.size() > 0) - { - s << TAB << "return "; - - for (size_t i = 0; i < key.size(); i++) - { - for (size_t ii =0; ii < member.size(); ii++) - { - if (key[i] != member[ii]->getId()) - { - continue; - } - - if (isSimple(member[i]->getTypePtr())) - { - s << (i==0?"":TAB + TAB) << "this." << key[i] << " === " << "anItem." << key[i]; - } - else - { - s << (i==0?"":TAB + TAB) << "this._equal(" << "anItem)"; - } - } - - if (i != key.size() - 1) - { - s << " && " << endl; - } - } - - s << ";" << endl; - - } - else - { - bNeedAssert = true; - s << TAB << "assert.fail(\"this structure not define key operation\");" << endl; - } - - DEL_TAB; - s << TAB << "};" << endl; - - //_genKey - s << TAB << sNamespace << "." << pPtr->getId() << ".prototype._genKey = function () {" << endl; - INC_TAB; - s << TAB << "if (!this._proto_struct_name_) {" << endl; - INC_TAB; - s << TAB << "this._proto_struct_name_ = \"STRUCT\" + Math.random();" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s << TAB << "return this._proto_struct_name_;" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - //toObject - s << TAB << sNamespace << "." << pPtr->getId() << ".prototype.toObject = function() { "<< endl; - INC_TAB; - s << TAB << "return {" << endl; - - for (size_t i = 0; i < member.size(); i++) - { - INC_TAB; - if (i > 0 && i < member.size()) { - s << "," << endl; - } - - if (isSimple(member[i]->getTypePtr())) { - s << TAB << "\"" << member[i]->getId() << "\" : this." << member[i]->getId(); - } - else { - s << TAB << "\"" << member[i]->getId() << "\" : this." << member[i]->getId() << ".toObject()"; - } - DEL_TAB; - } - - s << endl; - s << TAB << "};" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - //readFromJson - s << TAB << sNamespace << "." << pPtr->getId() << ".prototype.readFromObject = function(json) { "<< endl; - INC_TAB; - - for (size_t i = 0; i < member.size(); i++) - { - if (isSimple(member[i]->getTypePtr())) { - s << TAB << "json.hasOwnProperty(\"" << member[i]->getId() << "\") && (this." << member[i]->getId() << " = json." << member[i]->getId() << ");" << endl; - } else { - s << TAB << "json.hasOwnProperty(\"" << member[i]->getId() << "\") && (this." << member[i]->getId() << ".readFromObject(json." << member[i]->getId() << "));" << endl; - } - } - - DEL_TAB; - s << TAB << "};" << endl; - - //toBinBuffer - s << TAB << sNamespace << "." << pPtr->getId() << ".prototype.toBinBuffer = function () {" << endl; - INC_TAB; - s << TAB << "var os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl; - s << TAB << "this._writeTo(os);" << endl; - s << TAB << "return os.getBinBuffer();" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - //new - s << TAB << sNamespace << "." << pPtr->getId() << ".new = function () {" << endl; - INC_TAB; - s << TAB << "return new " << sNamespace << "." << pPtr->getId() << "();" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - //create - s << TAB << sNamespace << "." << pPtr->getId() << ".create = function (is) {" << endl; - INC_TAB; - s << TAB << "return " << sNamespace << "." << pPtr->getId() << "._readFrom(is);" << endl; - DEL_TAB; - s << TAB << "};" << endl; - - return s.str(); -} - -string CodeGenerator::generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert) -{ - ostringstream sstr; - vector ss(pPtr->getAllStructPtr()); - for (size_t last = 0; last != ss.size() && ss.size() != 0;) - { - last = ss.size(); - - for (vector::iterator iter=ss.begin(); iter!=ss.end();) - { - string str = generateJS(*iter, pPtr->getId(), bNeedAssert); - - if (!str.empty()) { - sstr << str << endl; - iter = ss.erase(iter); - - } else { - iter++; - } - } - } - - ostringstream cstr; - vector &cs = pPtr->getAllConstPtr(); - for (size_t i = 0; i < cs.size(); i++) - { - cstr << generateJS(cs[i], pPtr->getId(), bNeedStream); - } - - ostringstream estr; - vector & es = pPtr->getAllEnumPtr(); - for (size_t i = 0; i < es.size(); i++) - { - estr << generateJS(es[i], pPtr->getId()); - } - - ostringstream str; - if (!estr.str().empty()) str << estr.str() << endl; - if (!cstr.str().empty()) str << cstr.str() << endl; - if (!sstr.str().empty()) - { - bNeedStream = true; - str << sstr.str() << endl; - } - - return str.str(); -} - -bool CodeGenerator::generateJS(const ContextPtr &pPtr) -{ - vector namespaces = pPtr->getNamespaces(); - - ostringstream istr; - set setNamespace; - for(size_t i = 0; i < namespaces.size(); i++) - { - if (setNamespace.count(namespaces[i]->getId()) == 0) - { - istr << TAB << "var " << namespaces[i]->getId() << " = " << namespaces[i]->getId() << " || {};" << endl; - istr << TAB << "module.exports." << namespaces[i]->getId() << " = " << namespaces[i]->getId() << ";" << endl << endl; - - setNamespace.insert(namespaces[i]->getId()); - } - } - - //先生æˆç¼–解ç ä½“ - ostringstream estr; - bool bNeedAssert = false; - bool bNeedStream = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - estr << generateJS(namespaces[i], bNeedStream, bNeedAssert); - } - if (estr.str().empty()) - { - return false; - } - - //å†ç”Ÿæˆå¯¼å…¥æ¨¡å— - ostringstream ostr; - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - if (it->second.sModule.empty()) continue; - - if (estr.str().find(it->second.sModule + ".") == string::npos) continue; - - ostr << "var " << it->second.sModule << " = require(\"" << it->second.sFile << "\");" << endl; - } - - //生æˆæ–‡ä»¶å†…容 - ostringstream sstr; - sstr << printHeaderRemark("Structure"); - sstr << "\"use strict\";" << endl << endl; - if (bNeedAssert) - { - sstr << "var assert = require(\"assert\");" << endl; - } - if (bNeedStream) - { - sstr << "var " << IDL_NAMESPACE_STR << "Stream = require(\"" << _sStreamPath << "\");" << endl; - } - sstr << ostr.str() << endl; - sstr << istr.str(); - sstr << estr.str() << endl; - - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + IDL_TYPE + ".js"; - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, sstr.str()); - - return true; -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_js_dts.cpp b/cpp/tools/tars2node/gen_js_dts.cpp deleted file mode 100644 index 5a29f9683..000000000 --- a/cpp/tools/tars2node/gen_js_dts.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::generateDTS(const EnumPtr &pPtr, const string &sNamespace) -{ - ostringstream s; - - INC_TAB; - s << TAB << "enum " << pPtr->getId() << " {" << endl; - - INC_TAB; - //æˆå‘˜å˜é‡ - int nenum = -1; - bool bDependent = false; - vector& member = pPtr->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - bDependent |= isDependent(sNamespace, member[i]->getId()); - if (member[i]->hasDefault()) - { - nenum = TC_Common::strto(member[i]->def()); - } - else - { - nenum++; - } - s << TAB << "\"" << member[i]->getId() << "\" = " << TC_Common::tostr(nenum) << ((i < member.size() - 1) ? "," : "") << endl; - } - DEL_TAB; - - s << TAB << "}" << endl; - DEL_TAB; - - if (!_bMinimalMembers || _bEntry || bDependent || isDependent(sNamespace, pPtr->getId())) { - return s.str(); - } else { - return ""; - } -} - -string CodeGenerator::generateDTS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream) -{ - if (_bMinimalMembers && !_bEntry && !isDependent(sNamespace, pPtr->getTypeIdPtr()->getId())) - { - return ""; - } - - ostringstream s; - if (_bStringBinaryEncoding && GET_CONST_GRAMMAR_PTR(pPtr)->t == CONST_GRAMMAR(STRING)) - { - bNeedStream = true; - } - - INC_TAB; - s << TAB << "const " << pPtr->getTypeIdPtr()->getId() << ":" - << getDtsType(pPtr->getTypeIdPtr()->getTypePtr()) << ";" - << endl; - DEL_TAB; - return s.str(); -} - -string CodeGenerator::generateDTS(const StructPtr &pPtr, const string &sNamespace) -{ - if (_bMinimalMembers && !_bEntry && !isDependent(sNamespace, pPtr->getId())) - { - return ""; - } - - string sStructName = pPtr->getId() + "$OBJ"; - vector &member = pPtr->getAllMemberPtr(); - ostringstream s; - - INC_TAB; - s << TAB << "class " << pPtr->getId() << " {" << endl; - INC_TAB; - - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << (member[i]->getId()) << (member[i]->isRequire()?": ":"?: ") << getDtsType(member[i]->getTypePtr()) << ";" << endl; - } - if (member.size() > 0) - { - s << endl; - } - - s << TAB << "toObject(): " << sStructName << ";" << endl; - s << TAB << "readFromObject(json: " << sStructName << "): void;" << endl; - s << TAB << "toBinBuffer(): " << IDL_NAMESPACE_STR << "Stream.BinBuffer;" << endl; - s << TAB << "static new(): " << pPtr->getId() << ";" << endl; - s << TAB << "static create(is: " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "InputStream): " << pPtr->getId() << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << TAB << "interface " << sStructName << " {" << endl; - INC_TAB; - - for (size_t i = 0; i < member.size(); i++) - { - const string &sType = getDtsType(member[i]->getTypePtr(), false); - if (!sType.empty()) - { - s << TAB << (member[i]->getId()) << (member[i]->isRequire()?": ":"?: ") << sType << ";" << endl; - } - } - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - return s.str(); -} - -string CodeGenerator::generateDTS(const NamespacePtr &pPtr, bool &bNeedStream) -{ - //结构 - ostringstream sstr; - vector ss(pPtr->getAllStructPtr()); - for (size_t last = 0; last != ss.size() && ss.size() != 0;) - { - last = ss.size(); - for (vector::iterator iter=ss.begin(); iter!=ss.end();) - { - string str = generateDTS(*iter, pPtr->getId()); - if (!str.empty()) - { - sstr << str << endl; - iter = ss.erase(iter); - } - else - { - iter++; - } - } - } - - //å¸¸é‡ - ostringstream cstr; - vector &cs = pPtr->getAllConstPtr(); - for (size_t i = 0; i < cs.size(); i++) - { - cstr << generateDTS(cs[i], pPtr->getId(), bNeedStream); - } - - //枚举 - ostringstream estr; - vector &es = pPtr->getAllEnumPtr(); - for (size_t i = 0; i < es.size(); i++) - { - estr << generateDTS(es[i], pPtr->getId()); - } - - ostringstream kstr; - if (!estr.str().empty()) kstr << estr.str() << endl; - if (!cstr.str().empty()) kstr << cstr.str() << endl; - if (!sstr.str().empty()) - { - bNeedStream = true; - kstr << sstr.str(); - } - - return kstr.str(); -} - -string CodeGenerator::generateDTS(const NamespacePtr &pPtr, const string &sContent) -{ - ostringstream str; - if (!sContent.empty()) - { - str << "export declare namespace " << pPtr->getId() << " {" << endl; - str << sContent; - str << "}" << endl << endl; - } - return str.str(); -} - -void CodeGenerator::generateDTS(const ContextPtr &pPtr) -{ - vector namespaces = pPtr->getNamespaces(); - - //先生æˆç¼–解ç ä½“ - ostringstream estr; - bool bNeedStream = false; - for (size_t i = 0; i < namespaces.size(); i++) - { - estr << generateDTS(namespaces[i], generateDTS(namespaces[i], bNeedStream)); - } - if (estr.str().empty()) - { - return; - } - - //å†ç”Ÿæˆå¯¼å…¥æ¨¡å— - ostringstream ostr; - if (bNeedStream) - { - ostr << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl; - } - - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - if (it->second.sModule.empty()) continue; - - if (estr.str().find(it->second.sModule + ".") == string::npos) continue; - - ostr << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl; - } - - //生æˆæ–‡ä»¶å†…容 - ostringstream sstr; - sstr << printHeaderRemark("Structure"); - sstr << ostr.str() << endl; - sstr << estr.str() << endl; - - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + IDL_TYPE + ".d.ts"; - - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, sstr.str()); -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_proxy.cpp b/cpp/tools/tars2node/gen_proxy.cpp deleted file mode 100644 index 2454b8609..000000000 --- a/cpp/tools/tars2node/gen_proxy.cpp +++ /dev/null @@ -1,383 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -struct SortOperation -{ - bool operator()(const OperationPtr &o1, const OperationPtr &o2) - { - return o1->getId() < o2->getId(); - } -}; - -string CodeGenerator::generateJSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr) -{ - ostringstream str; - - //SETP01 生æˆç¼–ç æŽ¥å£ - str << TAB << "var __" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$EN = function ("; - vector & vParamDecl = oPtr->getAllParamDeclPtr(); - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - continue; - } - - str << (i == 0?"":", ") << vParamDecl[i]->getTypeIdPtr()->getId(); - } - str << ") {" << endl; - - INC_TAB; - - str << TAB << "var os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) continue; - - str << TAB << "os." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(" - << (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId() - << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : "") << ");" << endl; - - // 写入 Dependent 列表 - getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()); - } - - str << TAB << "return os.getBinBuffer();" << endl; - - DEL_TAB; - - str << TAB << "};" << endl << endl; - - - //STEP02 生æˆè§£ç å‡½æ•° - str << TAB << "var __" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$DE = function (data) {" << endl; - INC_TAB; - str << TAB << "try {" << endl; - INC_TAB; - - bool bHasParamOut = false; - if (vParamDecl.size() > 0) - { - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) { - bHasParamOut = true; - break; - } - } - } - - if (oPtr->getReturnPtr()->getTypePtr() || bHasParamOut) - { - str << TAB << "var is = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "InputStream(data.response.sBuffer);" << endl; - } - - str << TAB << "return {" << endl; - INC_TAB; - str << TAB << "\"request\" : data.request," << endl; - str << TAB << "\"response\" : {" << endl; - INC_TAB; - str << TAB << "\"costtime\" : data.request.costtime"; - - if (oPtr->getReturnPtr()->getTypePtr()) - { - str << "," << endl; - str << TAB << "\"return\"" - << " : is." << toFunctionName(oPtr->getReturnPtr(), "read") << "(0, true, "; - - if (isSimple(oPtr->getReturnPtr()->getTypePtr())) - { - str << getDefault(oPtr->getReturnPtr(), oPtr->getReturnPtr()->def(), nPtr->getId()) - << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : ""); - } - else - { - str << getDataType(oPtr->getReturnPtr()->getTypePtr()); - } - - str << ")"; - } - - if (bHasParamOut) - { - str << "," << endl; - str << TAB << "\"arguments\" : {" << endl; - - INC_TAB; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) continue; - - str << TAB << "\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\"" - << " : is." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "read") << "(" << (i + 1) << ", true, "; - - if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) - { - str << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId()) - << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); - } - else - { - str << getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()); - } - - str << ")"; - - if (i == vParamDecl.size() - 1) - { - str << endl; - } - else - { - str << "," << endl; - } - - } - - DEL_TAB; - str << TAB << "}"; - } - - str << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "};" << endl; - DEL_TAB; - str << TAB << "} catch (e) {" << endl; - INC_TAB; - str << TAB << "throw {" << endl; - INC_TAB; - str << TAB << "\"request\" : data.request," << endl; - str << TAB << "\"response\" : {" << endl; - INC_TAB; - str << TAB << "\"costtime\" : data.request.costtime," << endl; - str << TAB << "\"error\" : {" << endl; - INC_TAB; - str << TAB << "\"code\" : " << IDL_NAMESPACE_STR << "Error.CLIENT.DECODE_ERROR," << endl; - str << TAB << "\"message\" : e.message" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "};" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "};" << endl << endl; - - - //STEP03 生æˆæ¡†æž¶è°ƒç”¨é”™è¯¯å¤„ç†å‡½æ•° - str << TAB << "var __" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$ER = function (data) {" << endl; - INC_TAB; - str << TAB << "throw {" << endl; - INC_TAB; - str << TAB << "\"request\" : data.request," << endl; - str << TAB << "\"response\" : {" << endl; - INC_TAB; - str << TAB << "\"costtime\" : data.request.costtime," << endl; - str << TAB << "\"error\" : data.error" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "};" << endl << endl; - - - //SETP04 生æˆå‡½æ•°æŽ¥å£ - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Proxy.prototype." << oPtr->getId() << " = function ("; - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - continue; - } - - str << (i == 0?"":", ") << vParamDecl[i]->getTypeIdPtr()->getId(); - } - str << ") {" << endl; - - INC_TAB; - - str << TAB << "return this._worker." << TC_Common::lower(IDL_NAMESPACE_STR) << "_invoke(\"" << oPtr->getId() << "\", "; - str << "__" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$EN("; - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) - { - continue; - } - - str << (i == 0?"":", ") << vParamDecl[i]->getTypeIdPtr()->getId(); - } - str << "), arguments[arguments.length - 1]).then("; - str << "__" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$DE, "; - str << "__" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$ER);" << endl; - - DEL_TAB; - - str << TAB << "};" << endl; - - return str.str(); -} - -string CodeGenerator::generateJSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr) -{ - ostringstream str; - - vector & vOperation = pPtr->getAllOperationPtr(); - sort(vOperation.begin(), vOperation.end(), SortOperation()); - for (size_t i = 0; i < vOperation.size(); i++) - { - str << generateJSProxy(nPtr, pPtr, vOperation[i]) << endl; - } - - return str.str(); -} - -string CodeGenerator::generateJSProxy(const NamespacePtr &nPtr, bool &bNeedRpc, bool &bNeedStream) -{ - ostringstream str; - - vector & is = nPtr->getAllInterfacePtr(); - for (size_t i = 0; i < is.size(); i++) - { - str << generateJSProxy(nPtr, is[i]) << endl; - } - if (is.size() != 0) - { - bNeedRpc = true; - bNeedStream = true; - } - - return str.str(); -} - -bool CodeGenerator::generateJSProxy(const ContextPtr &cPtr) -{ - vector namespaces = cPtr->getNamespaces(); - - ostringstream istr; - set setNamespace; - for(size_t i = 0; i < namespaces.size(); i++) - { - if (setNamespace.count(namespaces[i]->getId()) == 0) - { - istr << TAB << "var " << namespaces[i]->getId() << " = " << namespaces[i]->getId() << " || {};" << endl; - istr << TAB << "module.exports." << namespaces[i]->getId() << " = " << namespaces[i]->getId() << ";" << endl << endl; - - setNamespace.insert(namespaces[i]->getId()); - } - } - - set setInterface; - for(size_t i = 0; i < namespaces.size(); i++) - { - vector & is = namespaces[i]->getAllInterfacePtr(); - for (size_t ii = 0; ii < is.size(); ii++) - { - if (setInterface.count(namespaces[i]->getId() + "::" + is[ii]->getId()) != 0) - { - continue; - } - setInterface.insert(namespaces[i]->getId() + "::" + is[ii]->getId()); - - istr << TAB << namespaces[i]->getId() << "." << is[ii]->getId() << "Proxy = function () {" << endl; - INC_TAB; - istr << TAB << "this._name = undefined;" << endl; - istr << TAB << "this._worker = undefined;" << endl; - DEL_TAB; - istr << TAB << "};" << endl << endl; - - istr << TAB << namespaces[i]->getId() << "." << is[ii]->getId() << "Proxy.prototype.setTimeout = function (iTimeout) {" << endl; - INC_TAB; - istr << TAB << "this._worker.timeout = iTimeout;" << endl; - DEL_TAB; - istr << TAB << "};" << endl << endl; - - istr << TAB << namespaces[i]->getId() << "." << is[ii]->getId() << "Proxy.prototype.getTimeout = function () {" << endl; - INC_TAB; - istr << TAB << "return this._worker.timeout;" << endl; - DEL_TAB; - istr << TAB << "};" << endl << endl; - } - } - - //先生æˆç¼–è§£ç  + 代ç†ç±» - ostringstream estr; - bool bNeedAssert = false; - bool bNeedStream = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - estr << generateJS(namespaces[i], bNeedStream, bNeedAssert); - } - - bool bNeedRpc = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - estr << generateJSProxy(namespaces[i], bNeedRpc, bNeedStream); - } - - if (estr.str().empty()) - { - return false; - } - - //å†ç”Ÿæˆå¯¼å…¥æ¨¡å— - ostringstream ostr; - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - if (it->second.sModule.empty()) continue; - - if (estr.str().find(it->second.sModule + ".") == string::npos) continue; - - ostr << "var " << it->second.sModule << " = require(\"" << it->second.sFile << "\");" << endl; - } - - //生æˆæ–‡ä»¶å†…容 - ostringstream sstr; - sstr << printHeaderRemark("Client"); - sstr << "\"use strict\";" << endl << endl; - if (bNeedAssert) - { - sstr << "var assert = require(\"assert\");" << endl; - } - if (bNeedStream) - { - sstr << "var " << IDL_NAMESPACE_STR << "Stream = require(\"" << _sStreamPath << "\");" << endl; - } - if (bNeedRpc) - { - sstr << "var " << IDL_NAMESPACE_STR << "Error = require(\"" << _sRpcPath << "\").error;" << endl; - } - - sstr << ostr.str() << endl; - sstr << istr.str() << endl; - sstr << estr.str() << endl; - - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(cPtr->getFileName())) + "Proxy.js"; - - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, sstr.str()); - - return true; -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_proxy_dts.cpp b/cpp/tools/tars2node/gen_proxy_dts.cpp deleted file mode 100644 index 72fef55df..000000000 --- a/cpp/tools/tars2node/gen_proxy_dts.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::generateDTSProxy(const InterfacePtr &pPtr) -{ - vector &vOperation = pPtr->getAllOperationPtr(); - ostringstream str; - - //class - INC_TAB; - str << TAB << "class " << pPtr->getId() << "Proxy {" << endl; - INC_TAB; - str << TAB << "setTimeout(timeout: number): void;" << endl; - str << TAB << "getTimeout(): number;" << endl; - for (size_t i = 0; i < vOperation.size(); i++) - { - OperationPtr &oPtr = vOperation[i]; - - str << TAB << oPtr->getId() << "("; - - vector &vParamDecl = oPtr->getAllParamDeclPtr(); - for (size_t j = 0; j < vParamDecl.size(); j++) - { - if(vParamDecl[j]->isOut()) - { - continue; - } - str << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getDtsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()); - str << ", "; - } - str << "property?: " << IDL_NAMESPACE_STR << "Rpc.InvokeProperty): Promise<" << pPtr->getId() << "$" << oPtr->getId() << "$DE>;" << endl; - } - DEL_TAB; - str << TAB << "}" << endl; - - //interface - for (size_t i = 0; i < vOperation.size(); i++) - { - OperationPtr &oPtr = vOperation[i]; - - str << TAB << "interface " << pPtr->getId() << "$" << oPtr->getId() << "$DE {" << endl; - INC_TAB; - str << TAB << "request: object;" << endl; - str << TAB << "response: {" << endl; - INC_TAB; - str << TAB << "costtime: number;" << endl; - if (oPtr->getReturnPtr()->getTypePtr()) - { - str << TAB << "return: " << getDtsType(oPtr->getReturnPtr()->getTypePtr()) << ";" << endl; - } - else - { - str << TAB << "return: void;" << endl; - } - - vector &vParamDecl = oPtr->getAllParamDeclPtr(); - bool hasArgs = false; - for (size_t j = 0; j < vParamDecl.size(); j++) - { - if(vParamDecl[j]->isOut()) { - hasArgs = true; - break; - } - } - - if(hasArgs) - { - str << TAB << "arguments: {" << endl; - INC_TAB; - for (size_t j = 0; j < vParamDecl.size(); j++) - { - if(!vParamDecl[j]->isOut()) { - continue; - } - str << TAB << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getDtsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()) << ";" << endl; - } - DEL_TAB; - str << TAB << "}" << endl; - } - - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "}" << endl; - } - DEL_TAB; - - return str.str(); -} - -string CodeGenerator::generateDTSProxy(const NamespacePtr &nPtr, bool &bNeedStream, bool &bNeedRpc) -{ - ostringstream str; - vector &is = nPtr->getAllInterfacePtr(); - for (size_t i = 0; i < is.size(); i++) - { - str << generateDTSProxy(is[i]) << endl; - } - if (is.size() != 0) - { - bNeedStream = true; - bNeedRpc = true; - } - return str.str(); -} - -void CodeGenerator::generateDTSProxy(const ContextPtr &cPtr) -{ - vector namespaces = cPtr->getNamespaces(); - - //先生æˆç¼–è§£ç  + 代ç†ç±» - ostringstream estr; - bool bNeedStream = false; - bool bNeedRpc = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - ostringstream kstr; - - kstr << generateDTS(namespaces[i], bNeedStream); - kstr << generateDTSProxy(namespaces[i], bNeedStream, bNeedRpc); - - estr << generateDTS(namespaces[i], kstr.str()); - } - if (estr.str().empty()) - { - return; - } - - //å†ç”Ÿæˆå¯¼å…¥æ¨¡å— - ostringstream ostr; - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - if (it->second.sModule.empty()) continue; - - if (estr.str().find(it->second.sModule + ".") == string::npos) continue; - - ostr << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl; - } - - //生æˆæ–‡ä»¶å†…容 - ostringstream sstr; - sstr << printHeaderRemark("Client"); - if (bNeedStream) - { - sstr << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl; - } - if (bNeedRpc) - { - sstr << "import * as " << IDL_NAMESPACE_STR << "Rpc from \"" << _sRpcPath << "\";" << endl; - } - - sstr << ostr.str() << endl; - sstr << estr.str() << endl; - - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(cPtr->getFileName())) + "Proxy.d.ts"; - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, sstr.str()); -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_server.cpp b/cpp/tools/tars2node/gen_server.cpp deleted file mode 100644 index 4501df43b..000000000 --- a/cpp/tools/tars2node/gen_server.cpp +++ /dev/null @@ -1,381 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::generatePing(const NamespacePtr &nPtr, const InterfacePtr &pPtr) -{ - ostringstream str; - - str << TAB << "var __" << nPtr->getId() << "_" << pPtr->getId() << "$" << TC_Common::lower(IDL_NAMESPACE_STR) << "_ping$RE = function (_ret) {" << endl; - INC_TAB; - str << TAB << "if (this.getRequestVersion() === " << PROTOCOL_SIMPLE << " || this.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl; - INC_TAB; - str << TAB << "var " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl; - str << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = this.getRequestVersion();" << endl; - str << TAB << PROTOCOL_VAR << ".writeInt32(\"\", _ret);" << endl << endl; - str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl; - DEL_TAB; - str << TAB << "} else {" << endl; - INC_TAB; - str << TAB << "var os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl; - str << TAB << "os.writeInt32(0, _ret);" << endl << endl; - str << TAB << "this.doResponse(os.getBinBuffer());" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "};" << endl << endl; - - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Imp.prototype.__" << TC_Common::lower(IDL_NAMESPACE_STR) << "_ping = function (current) {" << endl; - INC_TAB; - str << TAB << "__" << nPtr->getId() << "_" << pPtr->getId() << "$" << TC_Common::lower(IDL_NAMESPACE_STR) << "_ping$RE.call(current, 0);" << endl << endl; - str << TAB << "return " << IDL_NAMESPACE_STR << "Error.SUCCESS;" << endl; - DEL_TAB; - str << TAB << "};" << endl; - - return str.str(); -} - -string CodeGenerator::generateAsync(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr) -{ - ostringstream str; - - string sParams = ""; - if (oPtr->getReturnPtr()->getTypePtr()) - { - sParams += "_ret"; - - // 写入 Dependent 列表 - getDataType(oPtr->getReturnPtr()->getTypePtr()); - } - - vector & vParamDecl = oPtr->getAllParamDeclPtr(); - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) continue; - - sParams += (sParams.empty()?"":", ") + vParamDecl[i]->getTypeIdPtr()->getId(); - } - - str << TAB << "var __" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$RE = function (" << sParams << ") {" << endl; - - INC_TAB; - - if (sParams.empty()) - { - str << TAB << "this.doResponse(new " << IDL_NAMESPACE_STR << "Stream.BinBuffer());" << endl; - DEL_TAB; - str << TAB << "};" << endl; - - return str.str(); - } - - str << TAB << "if (this.getRequestVersion() === " << PROTOCOL_SIMPLE << " || this.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl; - INC_TAB; - str << TAB << "var " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl; - str << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = this.getRequestVersion();" << endl; - if (oPtr->getReturnPtr()->getTypePtr()) - { - str << TAB << PROTOCOL_VAR << "." << toFunctionName(oPtr->getReturnPtr(), "write") << "(\"\", _ret" - << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : "") << ");" << endl; - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) continue; - - str << TAB << PROTOCOL_VAR << "." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(\"" - << vParamDecl[i]->getTypeIdPtr()->getId() << "\", " << vParamDecl[i]->getTypeIdPtr()->getId() - << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : "") << ");" << endl; - } - str << endl; - str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl; - DEL_TAB; - str << TAB << "} else {" << endl; - - INC_TAB; - str << TAB << "var os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl; - if (oPtr->getReturnPtr()->getTypePtr()) - { - str << TAB << "os." << toFunctionName(oPtr->getReturnPtr(), "write") << "(0, _ret" - << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : "") << ");" << endl; - } - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (!vParamDecl[i]->isOut()) continue; - - str << TAB << "os." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(" - << (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId() - << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : "") << ");" << endl; - } - str << endl; - str << TAB << "this.doResponse(os.getBinBuffer());" << endl; - DEL_TAB; - str << TAB << "}" << endl; - - DEL_TAB; - - str << TAB << "};" << endl; - - return str.str(); -} - -string CodeGenerator::generateDispatch(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr) -{ - ostringstream str; - vector & vParamDecl = oPtr->getAllParamDeclPtr(); - - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Imp.prototype.__" << oPtr->getId() << " = function (current" << (vParamDecl.size() != 0 ? ", binBuffer" : "") << ") {" << endl; - - INC_TAB; - - ostringstream dstr; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - dstr << TAB << "var " << vParamDecl[i]->getTypeIdPtr()->getId() << " = null;" << endl; - } - if (vParamDecl.size() != 0) - { - dstr << endl; - } - - dstr << TAB << "if (current.getRequestVersion() === " << PROTOCOL_SIMPLE << " || current.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl; - INC_TAB; - dstr << TAB << "var " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl; - dstr << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = current.getRequestVersion();" << endl; - dstr << TAB << PROTOCOL_VAR << ".decode(binBuffer);" << endl; - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() - << " = " << PROTOCOL_VAR << "." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "read") - << "(\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\""; - - if (!isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) && !isBinBuffer(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) - { - dstr << ", " << getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()); - } - - if (vParamDecl[i]->isOut()) - { - dstr << ", " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true) - << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); - } - - dstr << ");" << endl; - } - DEL_TAB; - dstr << TAB << "} else {" << endl; - - INC_TAB; - dstr << TAB << "var is = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "InputStream(binBuffer);" << endl; - string sParams = ""; - for (size_t i = 0; i < vParamDecl.size(); i++) - { - sParams += ", " + vParamDecl[i]->getTypeIdPtr()->getId(); - - dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() - << " = is." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "read") << "(" - << (i + 1) << ", " << (vParamDecl[i]->isOut() ? "false" : "true") << ", "; - - if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) - { - dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId()) - << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); - } - else - { - dstr << getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()); - } - - dstr << ");" << endl; - } - DEL_TAB; - dstr << TAB << "}" << endl << endl; - - if (!sParams.empty()) - { - str << dstr.str(); - } - - str << TAB << "current.sendResponse = __" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$RE;" << endl << endl; - - str << TAB << "this." << oPtr->getId() << "(current" << sParams << ");" << endl << endl; - - str << TAB << "return " << IDL_NAMESPACE_STR << "Error.SUCCESS;" << endl; - - DEL_TAB; - - str << TAB << "};" << endl; - - return str.str(); -} - -string CodeGenerator::generateJSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr) -{ - ostringstream str; - - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Imp.prototype." << oPtr->getId() << " = function () {" << endl; - - INC_TAB; - str << TAB << "assert.fail(\"" << oPtr->getId() << " function not implemented\");" << endl; - DEL_TAB; - str << TAB << "};" << endl; - - return str.str(); -} - -string CodeGenerator::generateJSServer(const InterfacePtr &pPtr, const NamespacePtr &nPtr) -{ - ostringstream str; - vector & vOperation = pPtr->getAllOperationPtr(); - - //生æˆç±» - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Imp = function () { " << endl; - INC_TAB; - str << TAB << "this._name = undefined;" << endl; - str << TAB << "this._worker = undefined;" << endl; - DEL_TAB; - str << TAB << "};" << endl << endl; - - //生æˆåˆå§‹åŒ–函数 - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Imp.prototype.initialize = function () {};" << endl << endl; - - //生æˆåˆ†å‘函数 - str << TAB << nPtr->getId() << "." << pPtr->getId() << "Imp.prototype.onDispatch = function (current, funcName, binBuffer) { " << endl; - INC_TAB; - str << TAB << "if (\"__\" + funcName in this) {" << endl; - INC_TAB; - str << TAB << "return this[\"__\" + funcName](current, binBuffer);" << endl; - DEL_TAB; - str << TAB << "} else {" << endl; - INC_TAB; - str << TAB << "return " << IDL_NAMESPACE_STR << "Error.SERVER.FUNC_NOT_FOUND;" << endl; - DEL_TAB; - str << TAB << "}" << endl; - DEL_TAB; - str << TAB << "};" << endl << endl; - - //ç”Ÿæˆ PING 方法 - str << generatePing(nPtr, pPtr) << endl; - - //生æˆæŽ¥å£å‡½æ•° - for (size_t i = 0; i < vOperation.size(); i++) - { - str << generateJSServer(nPtr, pPtr, vOperation[i]) << endl; - str << generateAsync(nPtr, pPtr, vOperation[i]) << endl; - str << generateDispatch(nPtr, pPtr, vOperation[i]) << endl; - } - - str << endl; - - return str.str(); -} - -string CodeGenerator::generateJSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc, bool &bNeedAssert) -{ - ostringstream str; - - vector & is = pPtr->getAllInterfacePtr(); - for (size_t i = 0; i < is.size(); i++) - { - str << generateJSServer(is[i], pPtr) << endl; - } - if (is.size() != 0) - { - bNeedRpc = true; - bNeedStream = true; - bNeedAssert = true; - } - - return str.str(); -} - -bool CodeGenerator::generateJSServer(const ContextPtr &pPtr) -{ - vector namespaces = pPtr->getNamespaces(); - - ostringstream istr; - set setNamespace; - for(size_t i = 0; i < namespaces.size(); i++) - { - if (setNamespace.count(namespaces[i]->getId()) == 0) - { - istr << TAB << "var " << namespaces[i]->getId() << " = " << namespaces[i]->getId() << " || {};" << endl; - istr << TAB << "module.exports." << namespaces[i]->getId() << " = " << namespaces[i]->getId() << ";" << endl << endl; - - setNamespace.insert(namespaces[i]->getId()); - } - } - - //生æˆç¼–è§£ç  + æœåŠ¡ç±» - ostringstream estr; - bool bNeedAssert = false; - bool bNeedStream = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - estr << generateJS(namespaces[i], bNeedStream, bNeedAssert); - } - - bool bNeedRpc = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - estr << generateJSServer(namespaces[i], bNeedStream, bNeedRpc, bNeedAssert) << endl; - } - - if (estr.str().empty()) - { - return false; - } - - //å†ç”Ÿæˆå¯¼å…¥æ¨¡å— - ostringstream ostr; - if (bNeedAssert) - { - ostr << TAB << "var assert = require(\"assert\");" << endl; - } - if (bNeedStream) - { - ostr << TAB << "var " << IDL_NAMESPACE_STR << "Stream = require(\"" << _sStreamPath << "\");" << endl; - } - if (bNeedRpc) - { - ostr << TAB << "var " << IDL_NAMESPACE_STR << "Error = require(\"" << _sRpcPath << "\").error;" << endl; - } - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - if (it->second.sModule.empty()) continue; - - if (estr.str().find(it->second.sModule + ".") == string::npos) continue; - - ostr << TAB << "var " << it->second.sModule << " = require(\"" << it->second.sFile << "\");" << endl; - } - - ostringstream str; - - str << printHeaderRemark("Server"); - str << "\"use strict\";" << endl << endl; - str << ostr.str() << endl; - str << istr.str(); - str << estr.str() << endl; - - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".js"; - - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, str.str()); - - return true; -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_server_dts.cpp b/cpp/tools/tars2node/gen_server_dts.cpp deleted file mode 100644 index 49803cbda..000000000 --- a/cpp/tools/tars2node/gen_server_dts.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::generateDTSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr) -{ - vector &vOperation = pPtr->getAllOperationPtr(); - ostringstream str; - - //class - INC_TAB; - str << TAB << "class " << pPtr->getId() << "Imp {" << endl; - INC_TAB; - str << TAB << "initialize(): Promise | void;" << endl; - str << TAB << "protected onDispatch(current: " << IDL_NAMESPACE_STR << "Rpc." << IDL_TYPE << "Current, funcName: string, binBuffer: " << IDL_NAMESPACE_STR << "Stream.BinBuffer): number" << endl; - for (size_t i = 0; i < vOperation.size(); i++) - { - OperationPtr &oPtr = vOperation[i]; - str << TAB << oPtr->getId() << "(current: " << pPtr->getId() << "$" << oPtr->getId() << "$CUR"; - - vector &vParamDecl = oPtr->getAllParamDeclPtr(); - for (size_t j = 0; j < vParamDecl.size(); j++) - { - str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getDtsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()); - } - str << "): void;" << endl; - } - DEL_TAB; - str << TAB << "}" << endl; - - //interface - for (size_t i = 0; i < vOperation.size(); i++) - { - OperationPtr &oPtr = vOperation[i]; - - str << TAB << "interface " << pPtr->getId() << "$" << oPtr->getId() << "$CUR extends " << IDL_NAMESPACE_STR << "Rpc." << IDL_TYPE << "Current {" <getReturnPtr()->getTypePtr()) - { - str << "sendResponse(ret: " << getDtsType(oPtr->getReturnPtr()->getTypePtr()); - - vector &vParamDecl = oPtr->getAllParamDeclPtr(); - for (size_t j = 0; j < vParamDecl.size(); j++) - { - if(!vParamDecl[j]->isOut()) { - continue; - } - str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getDtsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()) ; - } - str << "): void;" << endl; - } - else - { - str << "sendResponse(): void;" << endl; - } - - DEL_TAB; - str << TAB << "}" << endl; - } - DEL_TAB; - - return str.str(); -} - -string CodeGenerator::generateDTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc) -{ - vector &is = pPtr->getAllInterfacePtr(); - ostringstream str; - - for (size_t i = 0; i < is.size(); i++) - { - str << generateDTSServer(pPtr, is[i]) << endl; - } - - if (is.size() != 0) - { - bNeedRpc = true; - bNeedStream = true; - } - - return str.str(); -} - -void CodeGenerator::generateDTSServer(const ContextPtr &pPtr) -{ - vector namespaces = pPtr->getNamespaces(); - - //生æˆç¼–è§£ç  + æœåŠ¡ç±» - ostringstream estr; - bool bNeedStream = false; - bool bNeedRpc = false; - for(size_t i = 0; i < namespaces.size(); i++) - { - ostringstream kstr; - - kstr << generateDTS(namespaces[i], bNeedStream); - kstr << generateDTSServer(namespaces[i], bNeedStream, bNeedRpc); - - estr << generateDTS(namespaces[i], kstr.str()); - } - if(estr.str().empty()) - { - return; - } - - //å†ç”Ÿæˆå¯¼å…¥æ¨¡å— - ostringstream ostr; - if (bNeedStream) - { - ostr << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl; - } - if (bNeedRpc) - { - ostr << "import * as " << IDL_NAMESPACE_STR << "Rpc from \"" << _sRpcPath << "\";" << endl; - } - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - if (it->second.sModule.empty()) continue; - - if (estr.str().find(it->second.sModule + ".") == string::npos) continue; - - ostr << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl; - } - - ostringstream str; - str << printHeaderRemark("Server"); - str << ostr.str() << endl; - str << estr.str() << endl; - - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".d.ts"; - - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, str.str()); -} \ No newline at end of file diff --git a/cpp/tools/tars2node/gen_server_imp.cpp b/cpp/tools/tars2node/gen_server_imp.cpp deleted file mode 100644 index a73b61bfa..000000000 --- a/cpp/tools/tars2node/gen_server_imp.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::generateJSServerImp(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr) -{ - ostringstream str; - str << nPtr->getId() << "." << pPtr->getId() << "Imp.prototype." << oPtr->getId() << " = function (current"; - - vector & vParamDecl = oPtr->getAllParamDeclPtr(); - for (size_t i = 0; i < vParamDecl.size(); i++) - { - str << ", " << vParamDecl[i]->getTypeIdPtr()->getId(); - } - - str << ") {" << endl; - INC_TAB; - str << TAB << "//TODO::" << endl; - DEL_TAB; - str << "};" << endl << endl; - - return str.str(); -} - -string CodeGenerator::generateJSServerImp(const NamespacePtr &nPtr, const InterfacePtr &pPtr) -{ - ostringstream str; - - - vector & vOperation = pPtr->getAllOperationPtr(); - for (size_t i = 0; i < vOperation.size(); i++) - { - str << generateJSServerImp(nPtr, pPtr, vOperation[i]); - } - - return str.str(); -} - -string CodeGenerator::generateJSServerImp(const ContextPtr &cPtr, const NamespacePtr &nPtr) -{ - ostringstream str; - - vector & is = nPtr->getAllInterfacePtr(); - for (size_t i = 0; i < is.size(); i++) - { - str << generateJSServerImp(nPtr, is[i]) << endl; - } - - return str.str(); -} - -void CodeGenerator::generateJSServerImp(const ContextPtr &cPtr) -{ - string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(cPtr->getFileName())) + "Imp.js"; - if (TC_File::isFileExist(sFileName)) - { - return ; - } - - ostringstream str; - str << printHeaderRemark("Imp"); - str << "\"use strict\";" << endl << endl; - - vector namespaces = cPtr->getNamespaces(); - set setNamespace; - for(size_t i = 0; i < namespaces.size(); i++) - { - if (setNamespace.count(namespaces[i]->getId()) != 0) - { - continue; - } - setNamespace.insert(namespaces[i]->getId()); - - str << "var " << namespaces[i]->getId() << " = require(\"./" - << TC_File::excludeFileExt(TC_File::extractFileName(cPtr->getFileName())) << ".js\")." - << namespaces[i]->getId() << ";" << endl; - - str << "module.exports." << namespaces[i]->getId() << " = " << namespaces[i]->getId() << ";" << endl; - } - str << endl; - - set setInterface; - for(size_t i = 0; i < namespaces.size(); i++) - { - vector & is = namespaces[i]->getAllInterfacePtr(); - for (size_t ii = 0; ii < is.size(); ii++) - { - if (setInterface.count(namespaces[i]->getId() + "::" + is[ii]->getId()) != 0) - { - continue; - } - setInterface.insert(namespaces[i]->getId() + "::" + is[ii]->getId()); - - str << namespaces[i]->getId() << "." << is[ii]->getId() << "Imp.prototype.initialize = function () {" << endl; - INC_TAB; - str << TAB << "//TODO::" << endl; - DEL_TAB; - str << "};" << endl << endl; - } - } - - for(size_t i = 0; i < namespaces.size(); i++) - { - str << generateJSServerImp(cPtr, namespaces[i]); - } - - TC_File::makeDirRecursive(_sToPath, 0755); - makeUTF8File(sFileName, str.str()); -} \ No newline at end of file diff --git a/cpp/tools/tars2node/idl_scan.cpp b/cpp/tools/tars2node/idl_scan.cpp deleted file mode 100644 index 94770f3db..000000000 --- a/cpp/tools/tars2node/idl_scan.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::makeName() -{ - int iHigh = uiNameIndex/26; - int iLow = uiNameIndex%26; - - uiNameIndex++; - - ostringstream s; - - s << "_" << TC_Common::upper(IDL_NAMESPACE_STR) << "_MODULE_"; - - if (iHigh != 0) - { - s << string(1, (char)(iHigh + 65)) << string(1, (char)(iLow + 65)); - } - else - { - s << string(1, (char)(iLow + 65)); - } - - s << "_"; - - return s.str(); -} - -bool CodeGenerator::isDependent(const string& sNamespace, const string& sName) const -{ - return _depMembers.find(sNamespace + "::" + sName) != _depMembers.end(); -} - -string CodeGenerator::findName(const string& sNamespace, const string& sName) -{ -#ifdef DUMP_FIND_NAME - cout << "FINDNAME BEGIN:" << sNamespace << "|" << sName << endl; -#endif - - for (map::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++) - { - #ifdef DUMP_FIND_NAME - for (map::iterator demo = it->second.mapVars.begin(); demo != it->second.mapVars.end(); demo++) - { - cout << "FINDNAME:" << it->second.sModule << "|" << demo->first << "|" << demo->second.sNamespace << "|" << demo->second.sName << endl; - } - #endif - - map::iterator inIter = it->second.mapVars.find(sNamespace + "::" + sName); - if (inIter == it->second.mapVars.end()) - { - continue; - } - - #ifdef DUMP_FIND_NAME - cout << "DEPMEMBER:" << it->first << "|" << inIter->second.sNamespace << "::" << inIter->second.sName << endl; - #endif - _depMembers.insert(inIter->second.sNamespace + "::" + inIter->second.sName); - - switch (inIter->second.iType) - { - case ImportFileType::EN_ENUM : // [[fallthrough]] - case ImportFileType::EN_STRUCT : - { - return it->second.sModule + (it->second.sModule.empty()?"":".") + inIter->second.sNamespace + "." + inIter->second.sName; - } - case ImportFileType::EN_ENUM_VALUE : - { - return it->second.sModule + (it->second.sModule.empty()?"":".") + inIter->second.sNamespace + "." + inIter->second.sTypeName + "." + inIter->second.sName; - } - default : - { - return it->second.sModule; - } - } - } - - return ""; -} - -void CodeGenerator::scan(const string& sFile, bool bNotPrefix) -{ - if (_mapFiles.find(sFile) != _mapFiles.end()) - { - return ; - } - - string sIdlFile = getRealFileInfo(sFile); - g_parse->parse(sIdlFile); - - vector contexts = g_parse->getContexts(); - - for(size_t i = 0; i < contexts.size(); i++) - { - if (sIdlFile == contexts[i]->getFileName()) - { - ImportFile item; - item.sFile = "./" + TC_File::excludeFileExt(TC_File::extractFileName(sFile)) + IDL_TYPE + ".js"; - item.sModule = bNotPrefix?"":makeName(); - - vector namespaces = contexts[i]->getNamespaces(); - for (size_t ii = 0; ii < namespaces.size(); ii++) - { - string sNamespace = namespaces[ii]->getId(); - - vector & ss = namespaces[ii]->getAllStructPtr(); - for (size_t iii = 0; iii < ss.size(); iii++) - { - vector vecNames = TC_Common::sepstr(ss[iii]->getSid(), "::"); - - ImportFileType temp; - temp.iType = ImportFileType::EN_STRUCT; - temp.sNamespace = sNamespace; - temp.sName = vecNames[1]; - - item.mapVars.insert(make_pair(temp.sNamespace + "::" + temp.sName, temp)); - } - - vector & es = namespaces[ii]->getAllEnumPtr(); - for (size_t iii = 0; iii < es.size(); iii++) - { - vector vecNames = TC_Common::sepstr(es[iii]->getSid(), "::"); - - ImportFileType temp; - temp.iType = ImportFileType::EN_ENUM; - temp.sNamespace = sNamespace; - temp.sName = vecNames[1]; - - item.mapVars.insert(make_pair(temp.sNamespace + "::" + temp.sName, temp)); - - vector & eMember = es[iii]->getAllMemberPtr(); - for (size_t iiii = 0; iiii < eMember.size(); iiii++) - { - ImportFileType temp; - temp.iType = ImportFileType::EN_ENUM_VALUE; - temp.sNamespace = sNamespace; - temp.sTypeName = vecNames[1]; - temp.sName = eMember[iiii]->getId(); - - #ifdef DUMP_FIND_NAME - cout << "GET::::" << temp.sNamespace << "|" << temp.sName << endl; - #endif - - item.mapVars.insert(make_pair(temp.sNamespace + "::" + temp.sName, temp)); - } - } - } - - _mapFiles.insert(make_pair(sFile, item)); - - vector vecFiles = contexts[i]->getIncludes(); - for (size_t ii = 0; ii < vecFiles.size(); ii++) - { - string sFileName = TC_File::extractFilePath(vecFiles[ii]) + TC_File::excludeFileExt(TC_File::extractFileName(vecFiles[ii])) + "." + TC_Common::lower(IDL_TYPE); - - scan(sFileName, false); - } - } - } -} \ No newline at end of file diff --git a/cpp/tools/tars2node/idl_util.cpp b/cpp/tools/tars2node/idl_util.cpp deleted file mode 100644 index 06ba786da..000000000 --- a/cpp/tools/tars2node/idl_util.cpp +++ /dev/null @@ -1,391 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "code_generator.h" - -string CodeGenerator::toFunctionName(const TypeIdPtr& pPtr, const string& sAction) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return sAction + "Boolean"; - case Builtin::KindString : return sAction + "String"; - case Builtin::KindByte : return sAction + "Int8"; - case Builtin::KindShort : return sAction + (bPtr->isUnsigned() ? "UInt8" : "Int16"); - case Builtin::KindInt : return sAction + (bPtr->isUnsigned() ? "UInt16" : "Int32"); - case Builtin::KindLong : return sAction + (bPtr->isUnsigned() ? "UInt32" : "Int64"); - case Builtin::KindFloat : return sAction + "Float"; - case Builtin::KindDouble : return sAction + "Double"; - default : assert(false); - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return sAction + "Bytes"; - } - - return sAction + "List"; - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - if (sPtr) - { - return sAction + "Struct"; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - return sAction + "Int32"; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); - if (mPtr) - { - return sAction + "Map"; - } - - return ""; -} - -bool CodeGenerator::isRawOrString(const TypePtr& pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - if (_bUseStringRepresent && bPtr->kind() == Builtin::KindLong) - { - return true; - } - - if (_bStringBinaryEncoding && bPtr->kind() == Builtin::KindString) - { - return true; - } - } - - return false; -} - -string CodeGenerator::getDataType(const TypePtr & pPtr) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return IDL_NAMESPACE_STR + "Stream.Boolean"; - case Builtin::KindString : return IDL_NAMESPACE_STR + "Stream.String"; - case Builtin::KindByte : return IDL_NAMESPACE_STR + "Stream.Int8"; - case Builtin::KindShort : return IDL_NAMESPACE_STR + "Stream.Int16"; - case Builtin::KindInt : return IDL_NAMESPACE_STR + "Stream.Int32"; - case Builtin::KindLong : return IDL_NAMESPACE_STR + "Stream.Int64"; - case Builtin::KindFloat : return IDL_NAMESPACE_STR + "Stream.Float"; - case Builtin::KindDouble : return IDL_NAMESPACE_STR + "Stream.Double"; - default : assert(false); - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return IDL_NAMESPACE_STR + "Stream.BinBuffer"; - } - - return IDL_NAMESPACE_STR + "Stream.List(" + getDataType(vPtr->getTypePtr()) + (isRawOrString(vPtr->getTypePtr()) ? ", 1" : "") + ")"; - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) - { - vector vecNames = TC_Common::sepstr(sPtr->getSid(), "::"); - assert(vecNames.size() == 2); - - return findName(vecNames[0], vecNames[1]); - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) - { - bool bLeft = isRawOrString(mPtr->getLeftTypePtr()); - bool bRight = isRawOrString(mPtr->getRightTypePtr()); - - if (!bRight && !bLeft) { - return IDL_NAMESPACE_STR + "Stream.Map(" + getDataType(mPtr->getLeftTypePtr()) + ", " + getDataType(mPtr->getRightTypePtr()) + ")"; - } else if (bRight && bLeft) { - return IDL_NAMESPACE_STR + "Stream.Map(" + getDataType(mPtr->getLeftTypePtr()) + ", " + getDataType(mPtr->getRightTypePtr()) + ", 1, 1)"; - } else if (bRight) { - return IDL_NAMESPACE_STR + "Stream.Map(" + getDataType(mPtr->getLeftTypePtr()) + ", " + getDataType(mPtr->getRightTypePtr()) + ", 0, 1)"; - } else if (bLeft) { - return IDL_NAMESPACE_STR + "Stream.Map(" + getDataType(mPtr->getLeftTypePtr()) + ", " + getDataType(mPtr->getRightTypePtr()) + ", 1)"; - } else { - assert(false); - } - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - vector vecNames = TC_Common::sepstr(ePtr->getSid(), "::"); - assert(vecNames.size() == 2); - - return findName(vecNames[0], vecNames[1]); - } - - assert(false); - return ""; -} - -string CodeGenerator::getDtsType(const TypePtr &pPtr, const bool bStream) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return "boolean"; - case Builtin::KindString : return _bStringBinaryEncoding ? (bStream ? (IDL_NAMESPACE_STR + "Stream.BinBuffer") : "Buffer") : "string"; - case Builtin::KindByte : return "number"; - case Builtin::KindShort : return "number"; - case Builtin::KindInt : return "number"; - case Builtin::KindLong : return _bUseStringRepresent ? "string" : "number"; - case Builtin::KindFloat : return "number"; - case Builtin::KindDouble : return "number"; - default : assert(false); - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return bStream ? (IDL_NAMESPACE_STR + "Stream.BinBuffer") : "Buffer"; - } - return (bStream ? (IDL_NAMESPACE_STR + "Stream.List") : "Array") + string("<") + getDtsType(vPtr->getTypePtr(), bStream) + string(">"); - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) - { - vector vecNames = TC_Common::sepstr(sPtr->getSid(), "::"); - assert(vecNames.size() == 2); - - return findName(vecNames[0], vecNames[1]) + (bStream ? "" : "$OBJ"); - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) - { - if (bStream) - { - return IDL_NAMESPACE_STR + "Stream.Map<" + getDtsType(mPtr->getLeftTypePtr(), bStream) + ", " + getDtsType(mPtr->getRightTypePtr(), bStream) + ">"; - } - else - { - const string& sLeftType = getDtsType(mPtr->getLeftTypePtr(), bStream); - if (sLeftType == "number" || sLeftType == "string") - { - return "{[key: " + getDtsType(mPtr->getLeftTypePtr(), bStream) + "]: " + getDtsType(mPtr->getRightTypePtr(), bStream) + "}"; - } - else if (isSimple(mPtr->getLeftTypePtr()) && sLeftType != "Buffer") - { - return "object"; - } - else - { - return ""; - } - } - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - vector vecNames = TC_Common::sepstr(ePtr->getSid(), "::"); - assert(vecNames.size() == 2); - - return findName(vecNames[0], vecNames[1]); - } - - assert(false); - return ""; -} - -bool CodeGenerator::isSimple(const TypePtr & pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - return true; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - return true; - } - - return false; -} - -bool CodeGenerator::isBinBuffer(const TypePtr & pPtr) const -{ - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return true; - } - - } - - return false; -} - -string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace) -{ - return getDefault(pPtr, sDefault, sNamespace, true); -} - -string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace, const bool bGlobal) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : - return sDefault.empty() ? "false" : sDefault; - case Builtin::KindString : - { - if (_bStringBinaryEncoding) - { - return IDL_NAMESPACE_STR + "Stream.BinBuffer.from(\"" + TC_Common::replace(sDefault, "\"", "\\\"") + "\").toNodeBufferUnSafe()"; - } - else - { - return "\"" + TC_Common::replace(sDefault, "\"", "\\\"") + "\""; - } - } - case Builtin::KindByte : // [[fallthrough]] - case Builtin::KindShort : // [[fallthrough]] - case Builtin::KindInt : // [[fallthrough]] - case Builtin::KindLong : - { - string sTemp = TC_Common::trim(sDefault); - if (sTemp.empty()) - { - sTemp = "0"; - } - else - { - if (TC_Common::tostr(TC_Common::strto(sTemp)) != sTemp) - { - //有å¯èƒ½æ˜¯æžšä¸¾å€¼ï¼Œåœ¨æžšä¸¾å€¼ä¸­æŸ¥æ‰¾ - vector vecNames = TC_Common::sepstr(sDefault, "::"); - if (vecNames.size() == 2) - { - sTemp = findName(vecNames[0], vecNames[1]); - } - else - { - sTemp = findName(sNamespace, sDefault); - } - } - } - - if (sTemp.empty()) - { - sTemp = "0"; - } - - if (_bUseStringRepresent) - { - if (bPtr->kind() == Builtin::KindLong) - { - sTemp = "\"" + sTemp + "\""; - } - } - - return sTemp; - } - case Builtin::KindFloat : // [[fallthrough]] - case Builtin::KindDouble : - return sDefault.empty()?"0.0":sDefault; - default : - assert(false); - } - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - if (sDefault.empty()) - { - vector& eMember = ePtr->getAllMemberPtr(); - if (eMember.size() > 0) - { - vector vecNames = TC_Common::sepstr(ePtr->getSid(), "::"); - - string sModule = findName(vecNames[0], eMember[0]->getId()); - - return sModule; - } - } - - if (!TC_Common::isdigit(sDefault)) - { - string s1 = sNamespace; - string s2 = sDefault; - - string::size_type index = sDefault.find("::"); - if (index != string::npos) - { - s1 = sDefault.substr(0, index); - s2 = sDefault.substr(index + 2); - } - - string sModule = findName(s1, s2); - - assert(!sModule.empty()); - - return sModule; - } - - return sDefault; - } - - if (bGlobal) - { - return "new " + getDataType(pPtr->getTypePtr()); - } - - return sDefault; -} \ No newline at end of file diff --git a/cpp/tools/tars2node/main.cpp b/cpp/tools/tars2node/main.cpp deleted file mode 100644 index 8eda13282..000000000 --- a/cpp/tools/tars2node/main.cpp +++ /dev/null @@ -1,184 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "util/tc_option.h" -#include "code_generator.h" - -void version() -{ - cout << "v" << GENERATOR_VERSION << endl; -} - -void usage() -{ - cout << "Version : " << GENERATOR_VERSION << endl; - cout << "Usage : " << EXECUTE_FILENAME << " [OPTION] " << "*." << TC_Common::lower(IDL_TYPE) << " file" << endl; - cout << " --version print " << EXECUTE_FILENAME << " version" << endl; - cout << " --rpc-path=DIRECTORY specify the path of rpc module." << endl; - cout << " --stream-path=DIRECTORY specify the path of stream module." << endl; - cout << " --allow-reserved-namespace allow you to use reserved word as a namespace." << endl; - cout << " --dir=DIRECTORY generate source file to DIRECTORY." << endl; - cout << " --relative use current path." << endl; - cout << " --base=DIRECTORY where to search " << IDL_TYPE << " files." << endl; - cout << " --r generate source all " << IDL_TYPE << " files." << endl; - cout << " --r-minimal minimize the dependent members." << endl; - cout << " --r-reserved list of names(split by \",\") that should be keeped." << endl; - cout << " --client just for client side source file." << endl; - cout << " --server just for server side source file." << endl; - cout << " --dts generate d.ts file." << endl; - cout << " --use-string-represent use string represent long type." << endl; - cout << " --string-binary-encoding get string raw bytes ." << endl; - cout << endl; - cout << EXECUTE_FILENAME << " support type: boolean char short int long float double list map" << endl; - - exit(0); -} - -void check(vector &vFiles) -{ - for(size_t i = 0; i < vFiles.size(); i++) - { - string ext = TC_File::extractFileExt(vFiles[i]); - if(ext == TC_Common::lower(IDL_TYPE)) - { - if(!TC_File::isFileExist(vFiles[i])) - { - cerr << "file '" << vFiles[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support "<< TC_Common::lower(IDL_TYPE) << " file." << endl; - usage(); - exit(0); - } - } -} - - - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - try - { - TC_Option option; - option.decode(argc, argv); - vector vFiles = option.getSingle(); - check(vFiles); - - if (option.hasParam("version")) - { - version(); - return 0; - } - - if(option.hasParam("help")) - { - usage(); - return 0; - } - - if(option.hasParam("base")) - { - if (::chdir(option.getValue("base").c_str()) != 0) { - return -1; - } - } - - #define ALLOW_USE_RESERVED_NAMESPACE_V(name, keeped) \ - g_parse->set##name(keeped); - #define ALLOW_USE_RESERVED_NAMESPACE_BASE(name, keeped) \ - ALLOW_USE_RESERVED_NAMESPACE_V(name, keeped) - #define ALLOW_USE_RESERVED_NAMESPACE(keeped) \ - ALLOW_USE_RESERVED_NAMESPACE_BASE(IDL_NAMESPACE, keeped) - - ALLOW_USE_RESERVED_NAMESPACE(option.hasParam("allow-reserved-namespace")); - - #undef ALLOW_USE_RESERVED_NAMESPACE - #undef ALLOW_USE_RESERVED_NAMESPACE_BASE - #undef ALLOW_USE_RESERVED_NAMESPACE_V - - g_parse->setUseCurrentPath(option.hasParam("relative")); - - CodeGenerator generator; - generator.setRpcPath(option.hasParam("rpc-path")?option.getValue("rpc-path"):RPC_MODULE_PATH); - generator.setStreamPath(option.hasParam("stream-path")?option.getValue("stream-path"):STREAM_MODULE_PATH); - generator.setEnableClient(option.hasParam("client")); - generator.setEnableServer(option.hasParam("server")); - generator.setTargetPath(option.hasParam("dir")?option.getValue("dir"):"./"); - generator.setUseSpecialPath(option.hasParam("relative")); - generator.setUseStringRepresent(option.hasParam("use-string-represent")); - generator.setStringBinaryEncoding(option.hasParam("string-binary-encoding")); - generator.setEnableDTS(option.hasParam("dts")); - - bool _bRecursive = option.hasParam("r"); - bool _bMinimalMembers = option.hasParam("r-minimal"); - - if (option.hasParam("r-reserved")) - { - set vMembers; - vector vReserved = TC_Common::sepstr(option.getValue("r-reserved"), ","); - for (size_t i = 0; i < vReserved.size(); i++) - { - if (vReserved[i].empty()) - { - continue; - } - - if (TC_Common::sepstr(vReserved[i], "::").size() == 2) - { - vMembers.insert(TC_Common::trim(vReserved[i])); - } else { - cerr << "reserved member must be match :: pattern" << endl; - return 0; - } - } - - if (vMembers.size() > 0) - { - _bMinimalMembers = true; - generator.setDependent(vMembers); - } - } - - if (!_bRecursive && _bMinimalMembers) - { - cerr << "Missing --r flag" << endl; - return 0; - } - - generator.setRecursive(_bRecursive); - generator.setMinimalMembers(_bMinimalMembers); - - for (size_t i = 0; i < vFiles.size(); i++) - { - generator.createFile(vFiles[i], true); - } - } - catch(exception& e) - { - cerr< &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - //ÊÇ·ñ¿ÉÒÔÒÔtars¿ªÍ· - g_parse->setTars(option.hasParam("with-tars")); - g_parse->setHeader(option.getValue("header")); - - Tars2OC j2c; - if (option.hasParam("dir")) - { - j2c.setBaseDir(option.getValue("dir")); - } - else - { - j2c.setBaseDir("."); - } - - if (option.hasParam("arc")) - { - j2c.setArc(true); - } - else - { - j2c.setArc(false); - } - - if(option.hasParam("nonamespace")) - { - j2c.setNeedNS(false); - - } - else - { - j2c.setNeedNS(true); - j2c.setNS(option.getValue("with-namespace")); - } - - try - { - for(size_t i = 0; i < vTars.size(); i++) - { - - g_parse->parse(vTars[i]); - j2c.createFile(vTars[i]); - } - }catch(exception& e) - { - cerr< - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -////////////////////////////////////////////////////////////////////////////////// - -string Tars2OC::toClassName(const TypePtr &pPtr) const -{ - string s; - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool: s = "bool"; break; - case Builtin::KindByte: s = "char"; break; - case Builtin::KindShort: s = "short"; break; - case Builtin::KindInt: s = "int32"; break; - case Builtin::KindLong: s = "int64"; break; - case Builtin::KindFloat: s = "float"; break; - case Builtin::KindDouble: s = "double"; break; - case Builtin::KindString: s = "string"; break; - default: assert(false); break; - } - - return s; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return tostrStruct(sPtr); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - -string Tars2OC::toAddtionalClassName(const TypePtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - string s = ""; - switch (bPtr->kind()) - { - case Builtin::KindBool: - case Builtin::KindByte: - case Builtin::KindShort: - case Builtin::KindInt: - case Builtin::KindLong: - case Builtin::KindFloat: - case Builtin::KindDouble: - { - s = "ONSNumber"; - break; - } - case Builtin::KindString: - { - s = "ONSString"; - break; - } - default: assert(false); break; - } - - return s; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - string s = ""; - //vector²»ÓÃÌí¼Ó¸½¼ÓÐÅÏ¢±àÂë - if (toClassName(vPtr) == "list") - { - s = "ONSData"; - } - else - { - s = "V" + toAddtionalClassName(vPtr->getTypePtr()); - - } - return s; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) - { - string sKey = toAddtionalClassName(mPtr->getLeftTypePtr()); - string sValue = toAddtionalClassName(mPtr->getRightTypePtr()); - - ostringstream osKeyLen; - { - int iLen = sKey.length(); - //¹æ¶¨²»Äܳ¬¹ý99 - assert(iLen <= 99 && iLen >=0); - char sLen[3]; - snprintf(&sLen[0],3,"%.2d",iLen); - osKeyLen <getSid(), "::", ""); - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - //Enumµ±×öObject´¦Àí - return "ONSNumber"; - } - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - - -/*******************************BuiltinPtr********************************/ -string Tars2OC::tostr(const TypePtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - if (toClassName(vPtr) == "list") - { - return "NSData*"; - } - else - { - return "NSArray*"; - } - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) return "NSDictionary*"; - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) return tostrStruct(sPtr)+ "*"; - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) return tostrEnum(ePtr); - - if (!pPtr) return "void"; - - assert(false); - return ""; -} - -string Tars2OC::tostrBuiltin(const BuiltinPtr &pPtr) const -{ - string s; - - switch (pPtr->kind()) - { - case Builtin::KindBool: - s = "TarsBool"; - break; - case Builtin::KindByte: - s = "TarsInt8"; - break; - case Builtin::KindShort: - s = (pPtr->isUnsigned()?"TarsUInt8":"TarsInt16"); - break; - case Builtin::KindInt: - s = (pPtr->isUnsigned()?"TarsUInt16":"TarsInt32"); - break; - case Builtin::KindLong: - s = (pPtr->isUnsigned()?"TarsUInt32":"TarsInt64"); - break; - case Builtin::KindFloat: - s = "TarsFloat"; - break; - case Builtin::KindDouble: - s = "TarsDouble"; - break; - case Builtin::KindString: - s = "NSString*"; - break; - default: - assert(false); - break; - } - - return s; -} -/*******************************VectorPtr********************************/ -string Tars2OC::tostrVector(const VectorPtr &pPtr) const -{ - string s = string("list<") + toClassName(pPtr->getTypePtr()); - - if (MapPtr::dynamicCast(pPtr->getTypePtr()) || VectorPtr::dynamicCast(pPtr->getTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} -/*******************************MapPtr********************************/ -string Tars2OC::tostrMap(const MapPtr &pPtr) const -{ - string s = string("map<") + toClassName(pPtr->getLeftTypePtr()) + "," + toClassName(pPtr->getRightTypePtr()); - if (MapPtr::dynamicCast(pPtr->getRightTypePtr()) || VectorPtr::dynamicCast(pPtr->getRightTypePtr())) - { - s += " >"; - } - else - { - s += ">"; - } - return s; -} -string Tars2OC::getReqOptionType(const TypeIdPtr &pPtr) const -{ - return (pPtr->isRequire())?"r":"o"; -} - -string Tars2OC::toTarsV2Procstr(const TypeIdPtr &pPtr) const -{ - ostringstream s; - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - - if (bPtr) - { - //ÆÕͨÀàÐÍʹÓÃJV2_PROP_NM - s << "JV2_PROP_NM(" << getReqOptionType(pPtr) << "," << pPtr->getTag() <<","<< pPtr->getId() << ")"; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if (vPtr) - { - //vector²»ÓÃÌí¼Ó¸½¼ÓÐÅÏ¢±àÂë - if (toClassName(vPtr) == "list") - { - s << "JV2_PROP_NM(" << getReqOptionType(pPtr) << "," << pPtr->getTag() << "," << pPtr->getId() << ")"; - } - else - { - s << "JV2_PROP_EX(" << getReqOptionType(pPtr) << "," << pPtr->getTag()<< "," <getId()<< "," <getTypePtr()); - if (mPtr) - { - s << "JV2_PROP_EX(" << getReqOptionType(pPtr) << "," << pPtr->getTag()<< "," <getId()<< "," << toAddtionalClassName(mPtr) << ")"; - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - if (sPtr) - { - s << "JV2_PROP_NM(" << getReqOptionType(pPtr) << "," << pPtr->getTag() <<","<< pPtr->getId() << ")"; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - s << "JV2_PROP_NM(" << getReqOptionType(pPtr) << "," << pPtr->getTag() <<","<< pPtr->getId() << ")"; - } - - return s.str(); -} - -/*******************************StructPtr********************************/ -string Tars2OC::tostrStruct(const StructPtr &pPtr) const -{ - - vector vStr = TC_Common::sepstr(pPtr->getSid(), "::"); - if(vStr.size() >= 2) - { - string sNamespace = getNamePrix(vStr[0]); - return sNamespace + vStr[1]; - } - - return TC_Common::replace(pPtr->getSid(), "::", ""); -} - -///////////////////////////////////////////////////////////////////// -string Tars2OC::tostrEnum(const EnumPtr &pPtr) const -{ - vector vStr = TC_Common::sepstr(pPtr->getSid(), "::"); - if(vStr.size() >= 2) - { - string sNamespace = getNamePrix(vStr[0]); - return sNamespace + vStr[1]; - } - return TC_Common::replace(pPtr->getSid(), "::", ""); -} - -void Tars2OC::toIncludeName(const TypePtr &pPtr,map& mReference) const -{ - //»ù±¾ÀàÐÍûÓÐÒÀÀµÆäËü½á¹¹ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - return ; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (!bPtr) - { - toIncludeName(vPtr->getTypePtr(),mReference); - } - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(mPtr->getLeftTypePtr()); - if (!bPtr)//not builin type - { - toIncludeName(mPtr->getLeftTypePtr(),mReference); - } - - BuiltinPtr _bPtr = BuiltinPtr::dynamicCast(mPtr->getRightTypePtr()); - if (!_bPtr) - { - toIncludeName(mPtr->getRightTypePtr(),mReference); - } - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) - { - mReference[tostrStruct(sPtr)] = EM_STRUCT_TYPE; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if (ePtr) - { - mReference[tostrEnum(ePtr)] = EM_ENUM_TYPE; - } -} - -map Tars2OC::getReferences(const StructPtr &pPtr, const string& namespaceId) const -{ - map mTemp; - vector& vMember = pPtr->getAllMemberPtr(); - - //²é¿´³ÉÔ±±äÁ¿ÊÇ·ñÒÀÀµÆäËüµÄ½á¹¹Ìå - for (size_t j = 0; j < vMember.size(); j++) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vMember[j]->getTypePtr()); - if (bPtr) - { - continue; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(vMember[j]->getTypePtr()); - if (vPtr) - { - //vector²»ÓÃÌí¼Ó¸½¼ÓÐÅÏ¢±àÂë - if (toClassName(vPtr) == "list") - { - continue; - } - else - { - toIncludeName(vPtr,mTemp); - } - } - - MapPtr mPtr = MapPtr::dynamicCast(vMember[j]->getTypePtr()); - if (mPtr) - { - toIncludeName(vPtr,mTemp); - } - - StructPtr sPtr = StructPtr::dynamicCast(vMember[j]->getTypePtr()); - if (sPtr) - { - mTemp[tostrStruct(sPtr)] = EM_STRUCT_TYPE; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(vMember[j]->getTypePtr()); - if (ePtr) - { - mTemp[tostrEnum(ePtr)] = EM_ENUM_TYPE; - } - - } - - return mTemp; -} -bool Tars2OC::IsRetainType(const TypePtr &pPtr) const -{ - //¶ÔÏóÀàÐÍÓÃretain,enum is NSInteger - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if((bPtr && bPtr->kind() != Builtin::KindString) || ePtr) - { - return false; - } - else - { - return true; - } - -} -string Tars2OC::getNamePrix(const string& sTarsNS) const -{ - string sNameSpace = ""; - if(m_bNeedNS) - { - if(!m_sNamespace.empty()) - { - return m_sNamespace; - } - else - { - return sTarsNS; - } - } - else - { - return sNameSpace; - } -} - -string Tars2OC::getPropertyName(const string& sId) const -{ - return "JV2_PROP(" + sId + ")"; -} - -string Tars2OC::getSetterName(const string& sId) const -{ - if(!sId.empty()) - { - return "set" + TC_Common::upper(sId.substr(0,1)) + sId.substr(1) + ":"; - } - else - { - return sId; - } -} - -/////////////////////////////////////////////////////////////////////// -string Tars2OC::generateH(const StructPtr &pPtr, const string& namespaceId) const -{ - ostringstream s; - - string sStructName = getNamePrix(namespaceId) + pPtr->getId(); - string fileH = m_sBaseDir + "/" + sStructName + ".h"; - - s << g_parse->printHeaderRemark(); - - s << "#import \"TarsObjectV2.h\"" << endl; - - map mTemp = getReferences(pPtr); - for(map::iterator it = mTemp.begin();it != mTemp.end();it++) - { - s << "#import \"" << it->first <<".h\""<& vMember = pPtr->getAllMemberPtr(); - for (size_t j = 0; j < vMember.size(); j++) - { - string sPropType = IsRetainType(vMember[j]->getTypePtr())?"retain":"assign"; - - s << "@property (nonatomic, "<getId() << "," << getSetterName(vMember[j]->getId()) <<")) "; - - s << tostr(vMember[j]->getTypePtr()) << " " << toTarsV2Procstr(vMember[j]) <<";"<& vMember) const -{ - ostringstream s; - - s << "- (id)init" <getTypePtr()); - if(bPtr) - { - if(bPtr->kind() == Builtin::KindBool) - { - string sBool = (vMember[j]->def() == "true")?"YES":"NO"; - s <getId()) << " = " << sBool << ";" << endl; - } - else if(bPtr->kind() == Builtin::KindString) - { - string tmp = tars::TC_Common::replace(vMember[j]->def(), "\"", "\\\""); - if(!tmp.empty()) - { - s <getId()) << " = @\"" << tmp << "\";" << endl; - } - else - { - s <getId()) << " = DefaultTarsString;" << endl; - } - } - else if((vMember[j]->def() != "0")) - { - s <getId()) << " = " << vMember[j]->def() << ";" << endl; - } - //ÆäËüÇé¿öµÄ»ù±¾ÀàÐͲ»Óóõʼ»¯£¬ÏµÍ³ÌṩĬÈÏÖµ - } - - //ÊÇ·ñrequire×Ö¶Î,ÇÒÊÇretainÊôÐÔ - if(vMember[j]->isRequire() && IsRetainType(vMember[j]->getTypePtr())) - { - VectorPtr vPtr = VectorPtr::dynamicCast(vMember[j]->getTypePtr()); - if (vPtr) - { - if (toClassName(vPtr) == "list") - { - s <getId()) << " = DefaultTarsData;" << endl; - } - else - { - s <getId()) << " = DefaultTarsArray;" << endl; - } - } - - MapPtr mPtr = MapPtr::dynamicCast(vMember[j]->getTypePtr()); - if (mPtr) - { - s <getId()) << " = DefaultTarsDictionary;" << endl; - } - - StructPtr sPtr = StructPtr::dynamicCast(vMember[j]->getTypePtr()); - if (sPtr) - { - s <getId()) << " = ["<& vMember) const -{ - ostringstream s; - - s << "- (void)dealloc" <getTypePtr())) - { - s <getId()) << " = nil;" <& vMember) const -{ - ostringstream s; - s <getSid(), "::", ".")<< "\";" <getSid(), "::", ""); - - s << TAB << "if (self == [" << sStructName << " class]) {" << endl; - - INC_TAB; - map mTemp = getReferences(pPtr); - for(map::iterator it = mTemp.begin();it != mTemp.end();it++) - { - if(it->second == EM_STRUCT_TYPE) - { - s << TAB << "[" << it->first <<" initialize];"<< endl; - } - } - - s << TAB <<"[super initialize];" <getId(); - - string fileH = m_sBaseDir + "/" + sEnumPrefix + ".m"; - - s << g_parse->printHeaderRemark(); - - s << "#import \"TarsObjectV2.h\"" << endl; - - s << "#import \"" <& member = pPtr->getAllMemberPtr(); - for(size_t i = 0; i < member.size(); i++) - { - string sMem = sEnumPrefix + "_" + member[i]->getId(); - - s << TAB << "case " << sMem << ": return "<< "@\"" << sMem << "\";" << endl; - } - s << TAB << "default: return @\"\";" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - DEL_TAB; - s << TAB << "}" << endl; - - s << endl; - - //Éú³É×Ö·û´®×ªÃ¶¾Ùº¯Êý - s << "+ (" << sEnumPrefix << ")stoe:(NSString *)s" << endl; - s << "{" << endl; - - INC_TAB; - for(size_t i = 0; i < member.size(); i++) - { - string sMem = sEnumPrefix + "_" + member[i]->getId(); - - s << TAB << "if(isTarsEnumStringEqual(s, @\"" << sMem << "\")) return " << sMem <<";"<getId(); - - ostringstream s; - - string fileM = m_sBaseDir + "/" + sStructName + ".m"; - - s << g_parse->printHeaderRemark(); - //¶¨Òå³ÉÔ±±äÁ¿ - vector& member = pPtr->getAllMemberPtr(); - - //////////////////////////////////////////////////////////// - s << "#import \"" <getAllNamespacePtr().size(); i++) - { - s << generateH(pPtr->getAllNamespacePtr()[i]) << endl; - s << endl; - } - return s.str(); -} - -/******************************EnumPtr***************************************/ - -string Tars2OC::generateH(const EnumPtr &pPtr, const string& namespaceId) const -{ - ostringstream s; - - string sEnumPrefix = getNamePrix(namespaceId) + pPtr->getId(); - - string fileH = m_sBaseDir + "/" + sEnumPrefix + ".h"; - - s << g_parse->printHeaderRemark(); - - s << "#import \"TarsObjectV2.h\"" << endl; - - s << endl; - s << "enum {"<& member = pPtr->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << sEnumPrefix << "_" << member[i]->getId(); - if (member[i]->hasDefault()) - { - s << " = " << member[i]->def(); - } - s << (i==member.size()-1 ? "" : ",") << endl; - } - DEL_TAB; - s <<"};" << endl; - - s <<"#define "<&ss = pPtr->getAllStructPtr(); - vector&es = pPtr->getAllEnumPtr(); - - //ÏȱéÀúö¾ÙÀàÐÍ,µ¥¶ÀÉú³ÉÎļþ - for (size_t i = 0; i < es.size(); i++) - { - generateH(es[i], pPtr->getId()); - generateM(es[i], pPtr->getId()); - } - - //±éÀú½á¹¹ÌåÀàÐÍ,µ¥¶ÀÉú³ÉÎļþ - for (size_t i = 0; i < ss.size(); i++) - { - generateH(ss[i], pPtr->getId()); - generateM(ss[i], pPtr->getId()); - } - - return ""; -} - - - -/******************************Tars2OC***************************************/ - -void Tars2OC::generate(const ContextPtr &pPtr) const -{ - vector namespaces = pPtr->getNamespaces(); - for (size_t i = 0; i < namespaces.size(); i++) - { - generate(namespaces[i]); - } -} - - - -void Tars2OC::createFile(const string &file) -{ - std::vector contexts = g_parse->getContexts(); - for (size_t i = 0; i < contexts.size(); i++) - { - if (file == contexts[i]->getFileName()) - { - generate(contexts[i]); - //generateM(contexts[i]); - } - } -} - diff --git a/cpp/tools/tars2oc/tars2oc.h b/cpp/tools/tars2oc/tars2oc.h deleted file mode 100644 index a632e2f5b..000000000 --- a/cpp/tools/tars2oc/tars2oc.h +++ /dev/null @@ -1,324 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef _TARS2OC_H -#define _TARS2OC_H - -#include "parse.h" - -#include -#include - -using namespace tars; - -/** - * ¸ù¾ÝtarsÉú³ÉhºÍmÎļþ - */ -class Tars2OC -{ - -public: - - /** - * Éú³É - * @param file - */ - void createFile(const string &file); - - - /** - * ÉèÖÃÉú³ÉÎļþµÄĿ¼ - * - */ - void setBaseDir(const std::string & sPath) { m_sBaseDir = sPath; } - - - /** - * ÉèÖÃÊÇ·ñ¼ì²éĬÈÏÖµ - * - * @param bCheck - */ - void setArc(const bool bArc) { m_bARC = bArc; } - /** - * ÉèÖø÷¸ö³ÉÔ±ÊôÐÔÊÇ·ñ¼ÓÉÏÃû×Ö¿Õ¼äǰ׺ - */ - void setNeedNS(bool bNeed) { m_bNeedNS = bNeed;} - /** - * ÉèÖÃÓû§×Ô¶¨ÒåÃû×Ö¿Õ¼ä - */ - void setNS(const string& sNS) {m_sNamespace = sNS;} - //ÏÂÃæÊÇÀàÐÍÃèÊöµÄÔ´ÂëÉú³É -protected: - /** - * »ñȡö¾Ù»òÕ߽ṹÌåÃû×Öǰ׺×Ö·û´® - * @param sTarsNS tarsÎļþÖж¨ÒåµÄÃû×Ö¿Õ¼ä - * @param bEnum ÊÇ·ñÊÇö¾ÙÀàÐÍ - * - * @return ´¦ÀíºóµÄǰ׺×Ö·û´® - */ - string getNamePrix(const string& sTarsNS) const; - - /** - * »ñÈ¡·ûºÏ¹æ·¶µÄ³ÉÔ±Ãû³Æ - */ - string getPropertyName(const string& sId) const; - - /** - * - * »ñÈ¡×Ô¶¨ÒåsetterµÄÃû×Ö - */ - string getSetterName(const string& sId) const; - /** - * Éú³ÉijÀàÐ͵Ä×Ö·û´®ÃèÊöÔ´Âë - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr &pPtr) const; - - /** - * Éú³ÉÄÚ½¨ÀàÐ͵Ä×Ö·û´®Ô´Âë - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr &pPtr) const; - /** - * Éú³ÉvectorµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr &pPtr) const; - - /** - * Éú³ÉmapµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr &pPtr) const; - - /** - * Éú³ÉijÖֽṹµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr &pPtr) const; - - - /** - * Éú³ÉijÖÖö¾ÙµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrEnum(const EnumPtr &pPtr) const; - - /** - * Éú³ÉOBJC °æ±¾2µÄÊôÐÔ¶¨Òå - * @param pPtr - * - * @return string - */ - string toTarsV2Procstr(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉOBJC°æ±¾2µÄ¸½¼ÓÐÅÏ¢±àÂë - * @param pPtr - * - * @return string - */ - string toAddtionalClassName(const TypePtr &pPtr) const; - - /** - * Éú³É½á¹¹ÌåÊôÐÔµÄsynthesize¶¨Òå - * @param vMember - * - * @return string - */ - string writesynthesize(const vector& vMember) const; - - /** - * Éú³ÉInitializeº¯Êý - * @param pPtr - * - * @return string - */ - string writeInitialize(const StructPtr &pPtr) const; - - /** - * Éú³É³õʼ»¯¶¨Ò庯Êýinit - * @param vMember - * - * @return string - */ - string writeInit(const vector& vMember) const; - - /** - * Éú³ÉÎö¹¹¶¨Òå - * @param vMember - * - * @return string - */ - string writedealloc(const vector& vMember) const; - - /** - * Éú³É½á¹¹Ãû³Æ¶¨Òå - * @param pPtr - * - * @return string - */ - string writeTarsType(const StructPtr &pPtr) const; - - /** - * Éú³ÉÀàÐͱêʾ·û - * @param pPtr - * - * @return string - */ - string toClassName(const TypePtr &pPtr) const; -protected: - /** - * »ñÈ¡tarsÖж¨ÒåµÄÐÞÊÎÀàÐÍ£¬requireºÍoptional - * @param pPtr - * - */ - string getReqOptionType(const TypeIdPtr &pPtr) const; - /** - * ÅжÏÀàÐÍÊÇ·ñÊôÓÚretainÀàÐÍ£¬stringºÍ¶ÔÏóÀàÐͶ¼ÊôÓÚretain - * @param pPtr - * - */ - bool IsRetainType(const TypePtr &pPtr) const; - /** - * »ñÈ¡½á¹¹ÌåÖÐÒÀÀµÆäËüÀàÐ͵ıêʶ·ûÃû³ÆºÍÀàÐÍ£¬Ä¿Ç°ÊǽṹÌåºÍö¾ÙÁ½ÖÖÀàÐÍ - * - * @param pPtr - * @param namespaceId - * - * @return - */ - map getReferences(const StructPtr &pPtr, const string& namespaceId="") const; - - /** - * »ñÈ¡³ÉÔ±¶ÔÏóÒÀÀµµÄÀàÐͱêʶ·ûÃû³Æ, - * ÀýÈç, stHello hello;stHelloΪÀàÐͱêʶ·ûÃû³Æ,ÀàÐÍΪ½á¹¹Ìå - * - * @param pPtr - * @param mRefs - */ - void toIncludeName(const TypePtr &pPtr,map& mRefs) const; - - /** - * Éú³É½á¹¹µÄÍ·ÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateH(const StructPtr &pPtr, const string& namespaceId) const; - - /** - * Éú³É½á¹¹µÄmÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateM(const StructPtr &pPtr, const string& namespaceId) const; - - /** - * Éú³Éö¾ÙµÄmÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generateM(const EnumPtr &pPtr, const string& namespaceId) const; - /** - * Éú³ÉÈÝÆ÷µÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const ContainerPtr &pPtr) const; - - /** - * Éú³Éö¾ÙµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateH(const EnumPtr &pPtr, const string& namespaceId) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼äµÄÍ·ÎļþºÍʵÏÖÔ´Âë - * @param pPtr - * - * @return string - */ - string generate(const NamespacePtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼ämÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generateM(const NamespacePtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼äµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generate(const ContextPtr &pPtr) const; - - /** - * Éú³ÉÃû×Ö¿Õ¼ämÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generateM(const ContextPtr &pPtr) const; - -private: - enum EM_TYPE - { - EM_STRUCT_TYPE = 1, - EM_ENUM_TYPE = 2 - }; - std::string m_sBaseDir; - - //, 2:enum,1,stuct; - mutable map m_mIncludes; - /** - * ÊÇ·ñÖ§³Öarc°æ±¾ - * - */ - bool m_bARC; - /** - * ÊÇ·ñÖ§³ÖÌí¼ÓÃû×Ö¿Õ¼äǰ׺ - */ - bool m_bNeedNS; - /** - *Óû§×Ô¶¨ÒåµÄÃû×Ö¿Õ¼äǰ׺£¬Èç¹û¸ÃֵΪ¿Õ£¬Ä¬ÈÏÊÇÈ¡tarsÎļþÖж¨ÒåµÄÖµ - */ - string m_sNamespace; -}; - -#endif - - diff --git a/cpp/tools/tars2php/CMakeLists.txt b/cpp/tools/tars2php/CMakeLists.txt deleted file mode 100644 index 5e50e6b19..000000000 --- a/cpp/tools/tars2php/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -tars2language("tars2php") diff --git a/cpp/tools/tars2php/main.cpp b/cpp/tools/tars2php/main.cpp deleted file mode 100644 index 92a99c4e7..000000000 --- a/cpp/tools/tars2php/main.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_common.h" -#include "tars2php.h" - - -void usage() -{ - cout << "Usage : tars2php [OPTION] tarsfile" << endl; - cout << " --coder=Demo::interface1;Demo::interface2 create interface encode and decode api" << endl; - cout << " --dir=DIRECTORY generate source file to DIRECTORY(Éú³ÉÎļþµ½Ä¿Â¼DIRECTORY,ĬÈÏΪµ±Ç°Ä¿Â¼)" << endl; - cout << " --check-default= Èç¹ûoptional×Ö¶ÎֵΪĬÈÏÖµ²»´ò°ü(ĬÈϲ»´ò°ü)" << endl; - cout << " tars2php support type: bool byte short int long float double vector map" << endl; - exit(0); -} - -void check(vector &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - bool bCoder = option.hasParam("coder"); - vector vCoder; - if(bCoder) - { - vCoder = tars::TC_Common::sepstr(option.getValue("coder"), ";", false); - if(vCoder.size() == 0) - { - usage(); - return 0; - } - } - - Tars2Php j2p; - - if (option.hasParam("dir")) - { - j2p.setBaseDir(option.getValue("dir")); - } - else - { - j2p.setBaseDir("."); - } - - j2p.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true); - try - { - //ÊÇ·ñ¿ÉÒÔÒÔtars¿ªÍ· - g_parse->setTars(option.hasParam("with-tars")); - g_parse->setHeader(option.getValue("header")); - - for(size_t i = 0; i < vTars.size(); i++) - { - - g_parse->parse(vTars[i]); - j2p.createFile(vTars[i], vCoder); - } - }catch(exception& e) - { - cerr< - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -////////////////////////////////////////////////////////////////////////////////// -// -Tars2Php::Tars2Php(): m_bCheckDefault(false) -{ - -} - -string Tars2Php::writeTo(const TypeIdPtr &pPtr) const -{ - ostringstream s; - s << TAB << "$this->" << pPtr->getId() << "->write($_out," << pPtr->getTag() << ");" << endl; - return s.str(); -} -string Tars2Php::readFrom(const TypeIdPtr &pPtr, bool bIsRequire) const -{ - ostringstream s; - s<"<getId()<<"->read($_in "; - s << ", " << pPtr->getTag() << ", " << ((pPtr->isRequire() && bIsRequire)?"true":"false") << ");" << endl; - return s.str(); -} - - -/*******************************»ñÈ¡¶¨³¤Êý×é×ø±ê********************************/ -int Tars2Php::getSuffix(const TypeIdPtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if(bPtr && bPtr->kind() == Builtin::KindString && bPtr->isArray()) - { - return bPtr->getSize(); - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if(vPtr && vPtr->isArray()) - { - return vPtr->getSize(); - } - - return -1; -} - -/*******************************¶¨³¤Êý×é×ø±ê********************************/ - -string Tars2Php::toStrSuffix(const TypeIdPtr &pPtr) const -{ - ostringstream s; - - int i = getSuffix(pPtr); - - if(i >= 0) - { - s << "[" << i << "]"; - } - return s.str(); -} -/*******************************BuiltinPtr********************************/ - -string Tars2Php::tostr(const TypePtr &pPtr) const -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if(bPtr) return tostrBuiltin(bPtr); - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if(vPtr) return tostrVector(vPtr); - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if(mPtr) return tostrMap(mPtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if(sPtr) return tostrStruct(sPtr); - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if(ePtr) return tostrEnum(ePtr); - - if(!pPtr) return "void"; - - assert(false); - return ""; -} - -string Tars2Php::tostrBuiltin(const BuiltinPtr &pPtr) const -{ - string s; - - switch(pPtr->kind()) - { - case Builtin::KindBool: - s = "c_char"; - break; - case Builtin::KindByte: - s = (pPtr->isUnsigned()?"c_short":"c_char"); - break; - case Builtin::KindShort: - s = (pPtr->isUnsigned()?"c_short":"c_short"); - break; - case Builtin::KindInt: - s = (pPtr->isUnsigned()?"c_int":"c_int"); - break; - case Builtin::KindLong: - s = "c_int64"; - break; - case Builtin::KindFloat: - s = "c_float"; - break; - case Builtin::KindDouble: - s = "c_double"; - break; - case Builtin::KindString: - s = "c_string"; - break; - case Builtin::KindVector: - s = "c_vector"; - break; - case Builtin::KindMap: - s = "c_map"; - break; - default: - assert(false); - break; - } - - return s; -} -/*******************************VectorPtr********************************/ -string Tars2Php::tostrVector(const VectorPtr &pPtr) const -{ - string s = string("c_vector (new ") + tostr(pPtr->getTypePtr()) + string(")"); - return s; -} -/*******************************MapPtr********************************/ -string Tars2Php::tostrMap(const MapPtr &pPtr) const -{ - string s = string("c_map (new ") + tostr(pPtr->getLeftTypePtr()) + ",new " + tostr(pPtr->getRightTypePtr()) + string(")"); - return s; -} - -/*******************************StructPtr********************************/ -string Tars2Php::tostrStruct(const StructPtr &pPtr) const -{ - return pPtr->getId(); -} - - -///////////////////////////////////////////////////////////////////// -string Tars2Php::tostrEnum(const EnumPtr &pPtr) const -{ - return "c_int";//pPtr->getId(); -} -/////////////////////////////////////////////////////////////////////// -string Tars2Php::generatePHP(const StructPtr &pPtr, const string& namespaceId) const -{ - ostringstream s; - - vector& member = pPtr->getAllMemberPtr(); - - s << TAB << "class " << pPtr->getId() << " extends c_struct" << endl; - s << TAB << "{" << endl; - INC_TAB; - //±äÁ¿ÉùÃ÷ - for(size_t k = 0;k < member.size();k++) - { - s<< TAB << "public $" <getId()<<";"<" <getId()<<" = clone $this->"<getId()<<";"<" <getId()<<" = new "<< tostr(member[k]->getTypePtr())<<";"<getId() << "\"" << ";" << endl; - DEL_TAB; - s << TAB << "}" << endl; - s<_skip_struct($_in);" << endl; - - DEL_TAB; - s << TAB << "}" << endl; - //read end - - DEL_TAB; - s << TAB << "}" << endl; - return s.str(); -} - - -/******************************NamespacePtr***************************************/ -string Tars2Php::generatePHP(const NamespacePtr &pPtr) const -{ - ostringstream s; - - vector &ss = pPtr->getAllStructPtr(); - - for(size_t i = 0; i < ss.size(); i++) - { - s << generatePHP(ss[i], pPtr->getId()) << endl; - } - - return s.str(); -} - -void Tars2Php::generatePHP(const ContextPtr &pPtr) const -{ - string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())); - string fileH = m_sBaseDir + "/" + n + "_wup.php"; - - string define = "printHeaderRemark(); - s << endl; - - s << "require_once('tars.php');" < namespaces = pPtr->getNamespaces(); - - for(size_t i = 0; i < namespaces.size(); i++) - { - s << generatePHP(namespaces[i]) << endl; - } - - s << "?>"<getFileName())); - string fileH = m_sBaseDir + "/" + n + "_pdu.php"; - - string define = "printHeaderRemark(); - s << endl; - s << "require_once('tars.php');" < namespaces = pPtr->getNamespaces(); - - for(size_t i = 0; i < namespaces.size(); i++) - { - s << generatePHP(namespaces[i]) << endl; - } - s << "?>"; - - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); - tars::TC_File::save2file(fileH, s.str()); -} - -void Tars2Php::createFile(const string &file, const vector &vsCoder) -{ - std::vector contexts = g_parse->getContexts(); - for(size_t i = 0; i < contexts.size(); i++) - { - if(file == contexts[i]->getFileName()) - { - //tup°æ±¾µÄ - generatePHP(contexts[i]); - } - } -} - - - diff --git a/cpp/tools/tars2php/tars2php.h b/cpp/tools/tars2php/tars2php.h deleted file mode 100644 index eed89e1ea..000000000 --- a/cpp/tools/tars2php/tars2php.h +++ /dev/null @@ -1,193 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#ifndef _TARS2PHP_H -#define _TARS2PHP_H - -#include "parse.h" - -#include -#include - -using namespace tars; - -/** - * ¸ù¾ÝtarsÉú³Éc++Îļþ - * °üÀ¨½á¹¹µÄ±à½âÂëÒÔ¼°Éú³ÉProxyºÍServant - */ -class Tars2Php -{ -public: - Tars2Php(); - - /** - * Éú³É - * @param file - */ - void createFile(const string &file, const vector &vsCoder); - - - /** - * ÉèÖÃÉú³ÉÎļþµÄĿ¼ - * - */ - void setBaseDir(const std::string & sPath) { m_sBaseDir = sPath; } - - /** - * ÉèÖÃÊÇ·ñÐèÒª´ò°üĬÈÏÖµ - */ - void setCheckDefault(bool bCheck) { m_bCheckDefault = bCheck; } - - //ÏÂÃæÊDZà½âÂëµÄÔ´ÂëÉú³É -protected: - - /** - * Éú³ÉijÀàÐ͵ĽâÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string writeTo(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉijÀàÐ͵ıàÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string readFrom(const TypeIdPtr &pPtr, bool bIsRequire = true) const; - - - //ÏÂÃæÊÇÀàÐÍÃèÊöµÄÔ´ÂëÉú³É -protected: - /** - * Éú³ÉijÀàÐ͵Ä×Ö·û´®ÃèÊöÔ´Âë - * @param pPtr - * - * @return string - */ - string tostr(const TypePtr &pPtr) const; - - /** - * Éú³ÉÄÚ½¨ÀàÐ͵Ä×Ö·û´®Ô´Âë - * @param pPtr - * - * @return string - */ - string tostrBuiltin(const BuiltinPtr &pPtr) const; - /** - * Éú³ÉvectorµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrVector(const VectorPtr &pPtr) const; - - /** - * Éú³ÉmapµÄ×Ö·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrMap(const MapPtr &pPtr) const; - - /** - * Éú³ÉijÖֽṹµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrStruct(const StructPtr &pPtr) const; - - /** - * Éú³ÉijÖÖö¾ÙµÄ·û´®ÃèÊö - * @param pPtr - * - * @return string - */ - string tostrEnum(const EnumPtr &pPtr) const; - - /** - * »ñÈ¡¶¨³¤Êý×éµÄ×ø±ê - * @param pPtr - * - * @return string - */ - - string toStrSuffix(const TypeIdPtr &pPtr) const; - - /** - * »ñÈ¡¶¨³¤Êý×éµÄ×ø±ê - * @param pPtr - * - * @return int - */ - int getSuffix(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉÀàÐͱäÁ¿µÄ½âÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string decode(const TypeIdPtr &pPtr) const; - - /** - * Éú³ÉÀàÐͱäÁ¿µÄ±àÂëÔ´Âë - * @param pPtr - * - * @return string - */ - string encode(const TypeIdPtr &pPtr) const; - - //ÒÔÏÂÊÇhºÍcppÎļþµÄ¾ßÌåÉú³É -protected: - /** - * Éú³É½á¹¹µÄÍ·ÎļþÄÚÈÝ - * @param pPtr - * - * @return string - */ - string generatePHP(const StructPtr &pPtr, const string& namespaceId) const; - - - /** - * Éú³ÉÃû×Ö¿Õ¼äµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - string generatePHP(const NamespacePtr &pPtr) const; - - - /** - * Éú³ÉÃû×Ö¿Õ¼äµÄÍ·ÎļþÔ´Âë - * @param pPtr - * - * @return string - */ - void generatePHP(const ContextPtr &pPtr) const; - - void generatePHP_Pdu(const ContextPtr &pPtr) const; - -private: - std::string m_sBaseDir; - - bool m_bCheckDefault; -}; - -#endif - - diff --git a/cpp/tools/tars2python/CMakeLists.txt b/cpp/tools/tars2python/CMakeLists.txt deleted file mode 100644 index 5c453eebd..000000000 --- a/cpp/tools/tars2python/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -tars2language("tars2python") diff --git a/cpp/tools/tars2python/main.cpp b/cpp/tools/tars2python/main.cpp deleted file mode 100644 index 03cae9234..000000000 --- a/cpp/tools/tars2python/main.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -#include "util/tc_option.h" -#include "util/tc_file.h" -#include "util/tc_common.h" -#include "tars2python.h" - - -void usage() -{ - cout << "Usage : tars2python [OPTION] tarsfile" << endl; - cout << " --dir=DIRECTORY generate source file to DIRECTORY(Éú³ÉÎļþµ½Ä¿Â¼DIRECTORY,ĬÈÏΪµ±Ç°Ä¿Â¼)" << endl; - cout << " --base-package=NAME package prefix, default 'com.qq.'(packageǰ׺£¬Î´Ö¸¶¨ÔòĬÈÏΪcom.qq.)" << endl; - cout << " tars2python support type: bool byte short int long float double vector map" << endl; - - exit(0); -} - -void check(vector &vTars) -{ - for(size_t i = 0; i < vTars.size(); i++) - { - string ext = tars::TC_File::extractFileExt(vTars[i]); - if(ext == "tars") - { - if(!tars::TC_File::isFileExist(vTars[i])) - { - cerr << "file '" << vTars[i] << "' not exists" << endl; - usage(); - exit(0); - } - } - else - { - cerr << "only support tars file." << endl; - usage(); - exit(0); - } - } -} - -int main(int argc, char* argv[]) -{ - if(argc < 2) - { - usage(); - } - - tars::TC_Option option; - option.decode(argc, argv); - vector vTars = option.getSingle(); - check(vTars); - - if(option.hasParam("help")) - { - usage(); - } - - Tars2Python j2python; - if (option.hasParam("dir")) - { - j2python.setBaseDir(option.getValue("dir")); - } - else - { - j2python.setBaseDir("."); - } - - if(option.hasParam("base-package")) - { - j2python.setBasePackage(option.getValue("base-package")); - } - else - { - j2python.setBasePackage("com.qq."); - } - - try - { - for (size_t i = 0; i < vTars.size(); i++) - { - g_parse->parse(vTars[i]); - j2python.createFile(vTars[i]); - } - } - catch(exception & e) - { - cerr< -// end modify - -#define TAB g_parse->getTab() -#define INC_TAB g_parse->incTab() -#define DEL_TAB g_parse->delTab() - -const string MAP_PREFIX = "mapcls_"; -const string VEC_PREFIX = "vctcls_"; - -string Tars2Python::generatePython(const EnumPtr &pPtr, const std::string &sNamespace) -{ - std::ostringstream s; - s << TAB << "class " << pPtr->getId() << ":" << endl; - INC_TAB; - - //³ÉÔ±±äÁ¿ - int nenum = -1; - vector& member = pPtr->getAllMemberPtr(); - for (size_t i = 0; i < member.size(); i++) - { - if (member[i]->hasDefault()) - { - nenum = tars::TC_Common::strto(member[i]->def()); - } - else - { - nenum++; - } - - s << TAB << member[i]->getId()<< " = " << tars::TC_Common::tostr(nenum) << ";" << endl; - } - - DEL_TAB; - - return s.str(); -} - -std::string Tars2Python::toFunctionName(const TypeIdPtr & pPtr, const string & sName) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return "tarscore.boolean"; - case Builtin::KindString: return "tarscore.string"; - case Builtin::KindByte: return "tarscore.int8"; - case Builtin::KindShort: return "tarscore." + string((bPtr->isUnsigned()?"uint8":"int16")); - case Builtin::KindInt: return "tarscore." + string((bPtr->isUnsigned()?"uint16":"int32")); - case Builtin::KindLong: return "tarscore." + string((bPtr->isUnsigned()?"uint32":"int64")); - case Builtin::KindFloat: return "tarscore.float"; - case Builtin::KindDouble: return "tarscore.double"; - default: assert(false); - } - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - if (sPtr) - { - return sPtr->getId(); - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - return "tarscore.int32"; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return "tarscore.bytes"; - } - - return "value." + VEC_PREFIX + sName; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); - if (mPtr) - { - return "value." + MAP_PREFIX + sName; - } - - assert(false); - return ""; -} - - - - - - - -string Tars2Python::getDefault(const TypeIdPtr & pPtr, const std::string &sDefault, const std::string & sCurStruct) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return sDefault.empty()?"True": (sDefault == "true"?"True":"False"); - case Builtin::KindString : return "\"" + tars::TC_Common::replace(sDefault, "\"", "\\\"") + "\""; - case Builtin::KindByte : - case Builtin::KindShort : - case Builtin::KindInt : - case Builtin::KindLong : return sDefault.empty()?"0":sDefault; - case Builtin::KindFloat : - case Builtin::KindDouble : return sDefault.empty()?"0.0":sDefault; - default : assert(false); - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return "bytes()"; - } - - return sCurStruct + "." + VEC_PREFIX + pPtr->getId() + "()"; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - string sid = ePtr->getSid(); - size_t pos = sid.find_last_of(":"); - return sDefault.empty()?"0":(sid.substr((pos == string::npos)?0:pos + 1) + "." + sDefault); - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - if (sPtr) - { - return sPtr->getId() + "()"; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); - if (mPtr) - { - return sCurStruct + "." + MAP_PREFIX + pPtr->getId() + "()"; - } - - assert(false); - return sDefault; -} - -string Tars2Python::tostrEnum(const EnumPtr &pPtr) -{ - return "tarscore.int8"; -} -string Tars2Python::makeDataType(const TypePtr & pPtr) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return "tarscore.boolean"; - case Builtin::KindString: return "tarscore.string"; - case Builtin::KindByte: return "tarscore.int8"; - case Builtin::KindShort: return "tarscore." + string((bPtr->isUnsigned()?"uint8":"int16")); - case Builtin::KindInt: return "tarscore." + string((bPtr->isUnsigned()?"uint16":"int32")); - case Builtin::KindLong: return "tarscore." + string((bPtr->isUnsigned()?"uint32":"int64")); - case Builtin::KindFloat: return "tarscore.float"; - case Builtin::KindDouble: return "tarscore.double"; - default: assert(false); - } - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return "tarscore.bytes"; - } - - return "tarscore.vctclass(" + makeDataType(vPtr->getTypePtr()) + ")"; - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); - if(ePtr) return tostrEnum(ePtr); - - StructPtr sPtr = StructPtr::dynamicCast(pPtr); - if (sPtr) - { - return sPtr->getId(); - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr); - if (mPtr) - { - return "tarscore.mapclass(" + makeDataType(mPtr->getLeftTypePtr()) + ", " + makeDataType(mPtr->getRightTypePtr()) + ")"; - } - - assert(false); - return ""; -} - -string Tars2Python::generatePython(const StructPtr & pPtr, const NamespacePtr & nPtr) -{ - std::ostringstream s; - vector key = pPtr->getKey(); - vector & member = pPtr->getAllMemberPtr(); - - s << TAB << "class " << pPtr->getId() << "(tarscore.struct):"<< endl; - INC_TAB; - - //STEP01 ¿ªÊ¼¶¨ÒåÀàÐèÒªµÄÀàÐÍ - s << TAB << "__tars_class__ = \"" << nPtr->getId() << "." << pPtr->getId() << "\";" << endl; - for (size_t i = 0, len = member.size(); i < len; i++) - { - VectorPtr vPtr = VectorPtr::dynamicCast(member[i]->getTypePtr()); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) continue; - - string st = makeDataType(member[i]->getTypePtr()); - s << TAB << VEC_PREFIX << member[i]->getId() << " = " << st << ";" << endl; - } - - MapPtr mPtr = MapPtr::dynamicCast(member[i]->getTypePtr()); - if (mPtr) - { - string st = makeDataType(member[i]->getTypePtr()); - s << TAB << MAP_PREFIX << member[i]->getId() << " = " << st << ";" << endl; - } - } - - //STEP02 ¶¨ÒåÊý¾Ý³ÉÔ± - s << TAB << endl; - s << TAB << "def __init__(self):" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - s << TAB << "self." << member[i]->getId() << " = " << getDefault(member[i], member[i]->def(), pPtr->getId()) << ";" << endl; - } - DEL_TAB; - - //STEP03 ¿ªÊ¼Éú³ÉwriteToº¯Êý - s << TAB << endl; - s << TAB << "@staticmethod" << endl; - s << TAB << "def writeTo(oos, value):" << endl; - INC_TAB; - for (size_t i = 0; i < member.size(); i++) - { - std::string sFuncName = toFunctionName(member[i], member[i]->getId()); - s << TAB << "oos.write(" << sFuncName << ", " << member[i]->getTag() << ", value." << member[i]->getId() << ");" << endl; - } - DEL_TAB; - - //STEP04 ¿ªÊ¼Éú³ÉreadFromº¯Êý - s << TAB << endl; - s << TAB << "@staticmethod" << endl; - s << TAB << "def readFrom(ios):" << endl; - INC_TAB; - s << TAB << "value = " << pPtr->getId() << "();" << endl; - for (size_t i = 0; i < member.size(); i++) - { - std::string sFuncName = toFunctionName(member[i], member[i]->getId()); - s << TAB << "value." << member[i]->getId() << "= ios.read(" << sFuncName << ", " << member[i]->getTag() - << ", " << (member[i]->isRequire()?"True":"False") << ", value." << member[i]->getId() << ");" << endl; - } - s << TAB << "return value;" << endl; - DEL_TAB; - - DEL_TAB; - return s.str(); -} - -string Tars2Python::makeParams(const OperationPtr &pPtr) -{ - ostringstream str; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - for (size_t i = 0; i < vParamDecl.size(); i++) - { - if (vParamDecl[i]->isOut()) continue; - - str << vParamDecl[i]->getTypeIdPtr()->getId() << ", "; - } - - str << "context = ServantProxy.mapcls_context()"; - - return str.str(); -} - -#if 0 -std::string Tars2Python::innerDataType(const TypeIdPtr & pPtr) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); - if (bPtr) - { - switch (bPtr->kind()) - { - case Builtin::KindBool : return "tarscore.boolean"; - case Builtin::KindString: return "tarscore.string"; - case Builtin::KindByte: return "tarscore.int8"; - case Builtin::KindShort: return "tarscore." + string((bPtr->isUnsigned()?"uint8":"int16")); - case Builtin::KindInt: return "tarscore." + string((bPtr->isUnsigned()?"uint16":"int32")); - case Builtin::KindLong: return "tarscore." + string((bPtr->isUnsigned()?"uint32":"int64")); - case Builtin::KindFloat: return "tarscore.float"; - case Builtin::KindDouble: return "tarscore.double"; - default: assert(false); - } - } - - StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); - if (sPtr) - { - return sPtr->getId(); - } - - EnumPtr ePtr = EnumPtr::dynamicCast(pPtr->getTypePtr()); - if (ePtr) - { - return "tarscore.int32"; - } - - VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); - if (vPtr) - { - BuiltinPtr vbPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if (vbPtr && vbPtr->kind() == Builtin::KindByte) - { - return "tarscore.bytes"; - } - - return "tarscore.vctclass(" + innerDataType(vPtr->getTypePtr()) + ")"; - } - - MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); - if (mPtr) - { - return "tarscore.mapclass(" + innerDataType() + ")"; - } - - assert(false); - return ""; -} - -#endif - -string Tars2Python::makeOperations(const OperationPtr &pPtr) -{ - ostringstream str; - vector& vParamDecl = pPtr->getAllParamDeclPtr(); - - str << TAB << "oos = tarscore.TarsOutputStream();" << endl; - for(size_t i = 0; i < vParamDecl.size(); i++) - { - if(vParamDecl[i]->isOut()) continue; - str << TAB << "oos.write(" << makeDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << ", " << (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId() << ");" << endl; - } - str << endl; - - str << TAB << "rsp = self.tars_invoke(ServantProxy.TARSNORMAL, \"" << pPtr->getId() << "\", oos.getBuffer(), context, None);" << endl << endl; - - if(vParamDecl.size() > 0 || pPtr->getReturnPtr()->getTypePtr()) - { - str << TAB << "ios = tarscore.TarsInputStream(rsp.sBuffer);" << endl; - - string ret; - if (pPtr->getReturnPtr()->getTypePtr()) - { - str << TAB << "ret = ios.read(" << makeDataType(pPtr->getReturnPtr()->getTypePtr()) << ", 0, True);" << endl; - ret += "ret, "; - } - - for(size_t i = 0; i < vParamDecl.size(); i++) - { - if(!vParamDecl[i]->isOut()) continue; - str << TAB << vParamDecl[i]->getTypeIdPtr()->getId() << " = ios.read(" << makeDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << ", " << (i + 1) << ", True);" << endl; - ret += vParamDecl[i]->getTypeIdPtr()->getId() + ", "; - } - - str << endl; - - if (!ret.empty()) - { - str << TAB << "return (" << ret.substr(0, ret.find_last_of(",")) << ");" << endl; - } - } - - return str.str(); -} - -string Tars2Python::generatePython(const InterfacePtr &pPtr) -{ - std::ostringstream str; - str << "#proxy for client" << endl; - - str << TAB << "class " << pPtr->getId() << "Proxy(ServantProxy):" << endl; - INC_TAB; - - vector& vOperation = pPtr->getAllOperationPtr(); - for(size_t i = 0; i < vOperation.size(); i++) - { - //Éú³Éÿ¸ö½Ó¿ÚµÄ±à½âÂë - str << TAB << "def " << vOperation[i]->getId() << "(self, " << makeParams(vOperation[i]) << "):" << endl; - INC_TAB; - str << makeOperations(vOperation[i]) << endl; - DEL_TAB; - } - - DEL_TAB; - return str.str(); -} - - -string Tars2Python::generatePython(const NamespacePtr &pPtr) -{ - std::ostringstream str; - - /* - //STEP01 ¿ªÊ¼Éú³É³£Á¿ - vector &cs = pPtr->getAllConstPtr(); - for (size_t i = 0; i < cs.size(); i++) - { - str << generatePython(cs[i], pPtr->getId()) << endl; - } - str << endl; - */ - - //STEP02 ¿ªÊ¼Éú³Éö¾ÙÖµ - vector & es = pPtr->getAllEnumPtr(); - for (size_t i = 0; i < es.size(); i++) - { - str << generatePython(es[i], pPtr->getId()) << endl; - } - - //STEP03 ¿ªÊ¼Éú³É½á¹¹Ìå±à½âÂ뺯Êý - vector & ss = pPtr->getAllStructPtr(); - for (size_t i = 0; i < ss.size(); i++) - { - str << generatePython(ss[i], pPtr) << endl; - } - - //STEP04 ¿ªÊ¼Éú³ÉProxyº¯Êý - vector & is = pPtr->getAllInterfacePtr(); - for(size_t i = 0; i < is.size(); i++) - { - str << generatePython(is[i]) << endl << endl; - } - - return str.str(); -} - -void Tars2Python::generatePython(const ContextPtr &pPtr) -{ - vector namespaces = pPtr->getNamespaces(); - for(size_t i = 0; i < namespaces.size(); i++) - { - std::ostringstream str; - str << "from tars.core import tarscore;" << endl; - str << "from tars.__rpc import ServantProxy;" << endl; - - vector include = pPtr->getIncludes(); - for(size_t j = 0; j < include.size(); j++) - { - set vec; - getNamespace(tars::TC_File::excludeFileExt(include[j]) + ".tars", vec); - - for (set::iterator it = vec.begin(); it != vec.end(); it++) - { - str << "from " << _packagePrefix << *it << "." << tars::TC_File::excludeFileExt(include[j]) << " import *;" << endl; - } - } - str << endl << endl; - - str << generatePython(namespaces[i]) << endl; - - string sPath = getFilePath() + "/" + namespaces[i]->getId() + "/"; - string sFileName = sPath + tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())) + ".py"; - tars::TC_File::makeDirRecursive(sPath, 0755); - tars::TC_File::save2file(sFileName, str.str()); - } - - makePackages(); -} - -void Tars2Python::getNamespace(const string & sFile, set & vec) -{ - g_parse->parse(sFile); - vector contexts = g_parse->getContexts(); - for (size_t i = 0; i < contexts.size(); i++) - { - if(sFile != contexts[i]->getFileName()) continue; - - vector namespaces = contexts[i]->getNamespaces(); - for (size_t j = 0; j < namespaces.size(); j++) - vec.insert(namespaces[j]->getId()); - } -} - -void Tars2Python::createFile(const string &file) -{ - std::vector contexts = g_parse->getContexts(); - for(size_t i = 0; i < contexts.size(); i++) - { - if(file == contexts[i]->getFileName()) - { - generatePython(contexts[i]); - } - } -} - -void Tars2Python::makePackages() -{ - string root = _packagePrefix.substr(0, _packagePrefix.find(".")); - vector files; - tars::TC_File::listDirectory(_baseDir + "/" + root, files, true); - files.push_back(_baseDir + "/" + root); - - for (size_t i = 0; i < files.size(); i++) - { - if (tars::TC_File::isFileExist(files[i], S_IFDIR) == true) - { - tars::TC_File::save2file(files[i] + "/__init__.py", ""); - } - } -} - -string Tars2Python::getFilePath() const -{ - return _baseDir + "/" + tars::TC_Common::replace(_packagePrefix, ".", "/"); -} diff --git a/cpp/tools/tars2python/tars2python.h b/cpp/tools/tars2python/tars2python.h deleted file mode 100644 index a88fd1b8b..000000000 --- a/cpp/tools/tars2python/tars2python.h +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _TARS2PYTHON_H -#define _TARS2PYTHON_H - -#include "parse.h" - -#include -#include -#include - -using namespace tars; - -#if 0 -#include "util/tc_md5.h" -#include "util/tc_file.h" -#include "util/tc_common.h" -#endif - -class Tars2Python -{ -public: - void setBaseDir(const string &dir) - { - _baseDir = dir; - } - - void setBasePackage(const string &prefix) - { - _packagePrefix = prefix; - if (_packagePrefix.length() != 0 && _packagePrefix.substr(_packagePrefix.length()-1, 1) != ".") - { - _packagePrefix += "."; - } - } - - void createFile(const string &file); - -private: - string generatePython(const ConstPtr &pPtr, const std::string &sNamespace); - - string generatePython(const EnumPtr &pPtr, const std::string &sNamespace); - - string generatePython(const StructPtr & pPtr, const NamespacePtr &nPtr); - - string generatePython(const NamespacePtr &pPtr); - - string generatePython(const InterfacePtr &pPtr); - - string toFunctionName(const TypeIdPtr & pPtr, const string & sName); - - string getDataType(const TypePtr & pPtr); - - string getDefault(const TypeIdPtr & pPtr, const std::string &sDefault, const std::string & sCurStruct); - - string makeDataType(const TypePtr & pPtr); - - string getFilePath() const; - - void getNamespace(const string & sFile, set & vec); - - void makePackages(); - - void generatePython(const ContextPtr &pPtr) ; - - string makeParams(const OperationPtr &pPtr); - - string makeOperations(const OperationPtr &pPtr); - string tostrEnum(const EnumPtr &pPtr); -private: - string _packagePrefix; - string _baseDir; -}; - -#endif - diff --git a/cpp/tools/tarsgrammar/CMakeLists.txt b/cpp/tools/tarsgrammar/CMakeLists.txt deleted file mode 100644 index ebfda5351..000000000 --- a/cpp/tools/tarsgrammar/CMakeLists.txt +++ /dev/null @@ -1,68 +0,0 @@ - -########################################################################################## -#生æˆ.lå’Œ.y文件生æˆçš„代ç æ–‡ä»¶copy到tarsparse目录下 - -set(OUT_DEPENDS_LEX_LIST) - -complice_lex(OUT_DEPENDS_LEX_LIST) - -FILE(GLOB SRC_LEX_LIST "*.l") - -set(CLEAN_LIST) - -#将生æˆä»£ç æ–‡ä»¶copy到对应的目录 -foreach (FILE ${SRC_LEX_LIST}) - - get_filename_component(NAME_WE ${FILE} NAME_WE) - set(LEX_CPP ${NAME_WE}.lex.cpp) - set(CP_SOURCE_CPP_DIR "${PROJECT_SOURCE_DIR}/tarsparse") - - add_custom_command( - OUTPUT ${CP_SOURCE_CPP_DIR}/${LEX_CPP} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - COMMAND cp -rf ${LEX_CPP} ${CP_SOURCE_CPP_DIR} - COMMENT "cp -rf ${LEX_CPP} ${CP_SOURCE_CPP_DIR}") - - #设置ä¾èµ– - list(APPEND OUT_DEPENDS_LEX_LIST ${CP_SOURCE_CPP_DIR}/${LEX_CPP}) - - #设置需è¦æ¸…除的.h文件 - list(APPEND CLEAN_LIST ${CP_SOURCE_CPP_DIR}/${LEX_CPP}) - -endforeach () - -set(OUT_DEPENDS_YACC_LIST) - -complice_yacc(OUT_DEPENDS_YACC_LIST) - -FILE(GLOB SRC_YACC_LIST "*.y") - -#将生æˆä»£ç æ–‡ä»¶copy到对应的目录 -foreach (FILE ${SRC_YACC_LIST}) - - get_filename_component(NAME_WE ${FILE} NAME_WE) - set(YACC_H ${NAME_WE}.tab.hpp) - set(YACC_CPP ${NAME_WE}.tab.cpp) - set(CP_SOURCE_H_DIR "${PROJECT_SOURCE_DIR}/tarsparse") - - add_custom_command( - OUTPUT ${CP_SOURCE_H_DIR}/${YACC_H} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - COMMAND cp -rf ${YACC_H} ${YACC_CPP} ${CP_SOURCE_H_DIR} - COMMENT "cp -rf ${YACC_H} ${YACC_CPP} ${CP_SOURCE_H_DIR}") - - #设置ä¾èµ– - list(APPEND OUT_DEPENDS_YACC_LIST ${CP_SOURCE_H_DIR}/${YACC_H}) - - #设置需è¦æ¸…除的.h文件 - list(APPEND CLEAN_LIST ${CP_SOURCE_H_DIR}/${YACC_H}) - list(APPEND CLEAN_LIST ${CP_SOURCE_H_DIR}/${YACC_CPP}) - -endforeach () - -#设置需è¦æ¸…除的文件 -set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") - -#add_custom_target(COPY-LEX-YACC ALL DEPENDS ${OUT_DEPENDS_LEX_LIST} ${OUT_DEPENDS_YACC_LIST}) - -add_custom_target(COPY-LEX-YACC ALL DEPENDS ${OUT_DEPENDS_LEX_LIST} ${CP_SOURCE_CPP_DIR}/${LEX_CPP} ${OUT_DEPENDS_YACC_LIST} ${CP_SOURCE_H_DIR}/${YACC_H}) diff --git a/cpp/tools/tarsgrammar/tars.l b/cpp/tools/tarsgrammar/tars.l deleted file mode 100644 index e4d321a74..000000000 --- a/cpp/tools/tarsgrammar/tars.l +++ /dev/null @@ -1,390 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -%option outfile="tars.lex.cpp" -%option yylineno -%{ -#include -#include -#include -#include -#include -#include - -#define YYSTYPE GrammarBasePtr - -#include "parse.h" -#include "tars.tab.hpp" - -using namespace std; - -extern "C" -{ - int yywrap() - { - return 1; - } -} - -struct include_file_state -{ - YY_BUFFER_STATE state; - string file; -}; - -#define MAX_INCLUDE_DEPTH 200 -include_file_state include_file_stack[MAX_INCLUDE_DEPTH]; -int include_file_stack_ptr = 0; - -%} - -tars_identifiers [[:alpha:]_][[:alnum:]_]* -tars_anychar . -tars_const_int (\+|-)?((0[0-7]+)|(0x[[:xdigit:]]+)|([[:digit:]]+)) -tars_float_literal (\+|-)?(([[:digit:]]*\.[[:digit:]]+)|([[:digit:]]+\.)) -tars_float_exp (e|E)(\+|-)?[[:digit:]]+ -tars_const_float (({tars_float_literal}{tars_float_exp}?)|((\+|-)?[[:digit:]]+{tars_float_exp}))[fF]? - -%x INCL - -%% - -"#include"[ \t]* { BEGIN(INCL); } - -"\"".*"\"" { - if ( include_file_stack_ptr >= MAX_INCLUDE_DEPTH ) - { - g_parse->error("Includes nested too deeply" ); - } - - string file; - bool b = g_parse->getFilePath( yytext, file); - g_parse->currentContextPtr()->addInclude(file); - - //该文件没解æžè¿‡ - if(b) - { - include_file_stack[include_file_stack_ptr].state = YY_CURRENT_BUFFER; - include_file_stack[include_file_stack_ptr].file = file; - include_file_stack_ptr++; - - yyin = fopen( file.c_str(), "r" ); - if ( !yyin ) - { - g_parse->error("can't open file:" + file); - } - - yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) ); - - g_parse->pushFile(file); - } - BEGIN(INITIAL); -} - -<> { - --include_file_stack_ptr; - if ( include_file_stack_ptr < 0 ) - { - include_file_stack_ptr = 0; - yyterminate(); - } - else - { - yy_delete_buffer( YY_CURRENT_BUFFER ); - fclose(yyin); - yy_switch_to_buffer(include_file_stack[include_file_stack_ptr].state ); - g_parse->popFile(); - } -} - -"::" { - return TARS_SCOPE_DELIMITER; -} - -"//" { - // C++的注释风格 - bool end = false; - while(!end) - { - int input = yyinput(); - if(input == '\n') - { - g_parse->nextLine(); - } - - if(input == '\n' || input == EOF) - { - end = true; - } - } -} - -"/*" { - // C的注释风格 - bool end = false; - string comment = yytext + 2; - while(!end) - { - int input = yyinput(); - switch(input) - { - case '*': - { - int nextInput = yyinput(); - if(nextInput == '/') - { - end = true; - } - else - { - comment += static_cast(input); - unput(nextInput); - } - break; - } - case '\n': - { - comment += static_cast(input); - g_parse->nextLine(); - break; - } - case EOF: - { - //到文件尾部, 但是没有出现*/ - g_parse->error("EOF in comment"); - end = true; - break; - } - default: - { - comment += static_cast(input); - break; - } - } - } - if(comment[0] == '*') - { - //æ³¨é‡Šå†…å®¹æš‚æ—¶æœªå¤„ç† - } -} - -{tars_identifiers} { - StringGrammarPtr ident = new StringGrammar; - ident->v = yytext; - yylval = ident; - return g_parse->checkKeyword(ident->v); -} - -{tars_identifiers}[[:space:]]*"(" { - StringGrammarPtr ident = new StringGrammar; - ident->v = yytext; - ident->v.erase(ident->v.find_first_of(" \t\v\n\r\f(")); - - yylval = ident; - - return TARS_OP; -} - -\" { - StringGrammarPtr str = new StringGrammar; - bool end = false; - while(!end) - { - int input = yyinput(); - switch(input) - { - case '"': - { - end = true; - break; - } - case '\n': - { - g_parse->error("newline in string"); - break; - } - case EOF: - { - g_parse->error("EOF in string"); - break; - } - case '\\': - { - static string specialChars = "nrtvfab?"; - static string octalChars = "0123"; - - char nextInput = static_cast(yyinput()); - if(nextInput == '\\' || nextInput == '"' || nextInput == '\'') - { - str->v += nextInput; - } - else if(specialChars.find(nextInput) != string::npos) - { - str->v += '\\'; - str->v += nextInput; - } - else if(octalChars.find(nextInput) != string::npos) - { - static string octalDigits = "01234567"; - - unsigned short us = nextInput - '0'; - if(octalDigits.find_first_of(nextInput = static_cast(yyinput())) != string::npos) - { - us = us * 8 + nextInput - '0'; - if(octalDigits.find_first_of(nextInput = static_cast(yyinput())) != string::npos) - { - us = us * 8 + nextInput - '0'; - } - else - { - unput(nextInput); - } - } - else - { - unput(nextInput); - } - - if(us == 0) - { - g_parse->error("illegal NUL character in string constant"); - } - str->v += static_cast(us); - } - else if(nextInput == 'x') - { - long long ull = 0; - while(isxdigit(nextInput = static_cast(yyinput()))) - { - ull *= 16; - if(isdigit(nextInput)) - { - ull += nextInput - '0'; - } - else if(islower(nextInput)) - { - ull += nextInput - 'a' + 10; - } - else - { - ull += nextInput - 'A' + 10; - } - } - - unput(nextInput); - - if(ull == 0) - { - g_parse->error("illegal NUL character in string constant"); - } - str->v += static_cast(ull); - } - else - { - str->v += static_cast(input); - } - - break; - } - default: - { - str->v += static_cast(input); - break; - } - } - } - yylval = str; - return TARS_STRING_LITERAL; -} - -{tars_const_int} { - errno = 0; - IntergerGrammarPtr ptr = new IntergerGrammar; - yylval = ptr; - - string value = yytext; - const char* beg = value.c_str(); - char* end = 0; - - ptr->v = strtoll(beg, &end, 0); - if(!(errno == 0 && beg != end)) - { - assert(ptr->v != 0); - string err = "integer constant `"; - err += value; - err += "' out of range"; - g_parse->error(err); - } - - return TARS_CONST_INTEGER; -} - -{tars_const_float} { - errno = 0; - FloatGrammarPtr ptr = new FloatGrammar; - yylval = ptr; - - string value(yytext); - - char lastChar = value[value.size() - 1]; - if(lastChar == 'f' || lastChar == 'F') - { - value = value.substr(0, value.size() - 1); - } - - ptr->v = strtod(value.c_str(), 0); - if((errno == ERANGE) && (ptr->v == HUGE_VAL || ptr->v == -HUGE_VAL)) - { - string err = "float point constant `"; - err += value; - err += "' too large (overflow)"; - g_parse->error(err); - } - else if(errno == ERANGE && ptr->v == 0) - { - string err = "float point constant `"; - err += value; - err += "' too small (underflow)"; - g_parse->error(err); - } - return TARS_CONST_FLOAT; -} - -[[:space:]] { - if(yytext[0] == '\n') - { - g_parse->nextLine(); - } -} - -{tars_anychar} { - if(yytext[0] < 32 || yytext[0] > 126) - { - stringstream s; - s << "illegal input character: '\\"; - s.width(3); - s.fill('0'); - s << oct << static_cast(static_cast(yytext[0])); - s << "'"; - - g_parse->error(s.str()); - return BAD_CHAR; - } - return yytext[0]; -} - -%% - diff --git a/cpp/tools/tarsgrammar/tars.y b/cpp/tools/tarsgrammar/tars.y deleted file mode 100644 index 9936222f5..000000000 --- a/cpp/tools/tarsgrammar/tars.y +++ /dev/null @@ -1,1110 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -%{ -#include -#include -#include - -using namespace std; - -#define YYSTYPE GrammarBasePtr - -#include "parse.h" -#define YYDEBUG 1 -#define YYINITDEPTH 10000 -%} - -%defines -%debug - -//keyword token -%token TARS_VOID -%token TARS_STRUCT -%token TARS_BOOL -%token TARS_BYTE -%token TARS_SHORT -%token TARS_INT -%token TARS_DOUBLE -%token TARS_FLOAT -%token TARS_LONG -%token TARS_STRING -%token TARS_VECTOR -%token TARS_MAP -%token TARS_NAMESPACE -%token TARS_INTERFACE -%token TARS_IDENTIFIER -%token TARS_OUT -%token TARS_OP -%token TARS_KEY -%token TARS_ROUTE_KEY -%token TARS_REQUIRE -%token TARS_OPTIONAL -%token TARS_CONST_INTEGER -%token TARS_CONST_FLOAT -%token TARS_FALSE -%token TARS_TRUE -%token TARS_STRING_LITERAL -%token TARS_SCOPE_DELIMITER -%token TARS_CONST -%token TARS_ENUM -%token TARS_UNSIGNED -%token BAD_CHAR - -%% -start: definitions -; - -// ---------------------------------------------------------------------- -definitions -// ---------------------------------------------------------------------- -: definition -{ -} -';' definitions -| error ';' -{ - yyerrok; -} -definitions -| definition -{ - g_parse->error("`;' missing after definition"); -} -| -{ -} -; - -// ---------------------------------------------------------------------- -definition -// ---------------------------------------------------------------------- -: namespace_def -{ - assert($1 == 0 || NamespacePtr::dynamicCast($1)); -} -| interface_def -{ - assert($1 == 0 || InterfacePtr::dynamicCast($1)); -} -| struct_def -{ - assert($1 == 0 || StructPtr::dynamicCast($1)); -} -| key_def -{ -} -| enum_def -{ - assert($1 == 0 || EnumPtr::dynamicCast($1)); -} -| const_def -{ - assert($1 == 0 || ConstPtr::dynamicCast($1)); -} -; - -// ---------------------------------------------------------------------- -enum_def -// ---------------------------------------------------------------------- -: enum_id -{ - $$ = $1; -} -'{' enumerator_list '}' -{ - if($3) - { - g_parse->popContainer(); - $$ = $3; - } - else - { - $$ = 0; - } - - $$ = $2; -} -; - -// ---------------------------------------------------------------------- -enum_id -// ---------------------------------------------------------------------- -: TARS_ENUM TARS_IDENTIFIER -{ - NamespacePtr c = NamespacePtr::dynamicCast(g_parse->currentContainer()); - if(!c) - { - g_parse->error("enum must define in namespace"); - } - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - EnumPtr e = c->createEnum(ident->v); - g_parse->pushContainer(e); - - $$ = e; -} -| TARS_ENUM keyword -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - g_parse->error("keyword `" + ident->v + "' cannot be used as enumeration name"); - $$ = $2; -} -; - -// ---------------------------------------------------------------------- -enumerator_list -// ---------------------------------------------------------------------- -: enumerator ',' enumerator_list -{ - $$ = $2; -} -| enumerator -{ -} -; - -// ---------------------------------------------------------------------- -enumerator -// ---------------------------------------------------------------------- -: TARS_IDENTIFIER -{ - TypePtr type = TypePtr::dynamicCast(g_parse->createBuiltin(Builtin::KindLong)); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - TypeIdPtr tPtr = new TypeId(type, ident->v); - tPtr->disableDefault(); - EnumPtr e = EnumPtr::dynamicCast(g_parse->currentContainer()); - assert(e); - e->addMember(tPtr); - $$ = e; -} -| keyword -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - g_parse->error("keyword `" + ident->v + "' cannot be used as enumerator"); -} -| TARS_IDENTIFIER '=' const_initializer -{ - TypePtr type = TypePtr::dynamicCast(g_parse->createBuiltin(Builtin::KindLong)); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - TypeIdPtr tPtr = new TypeId(type, ident->v); - ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($3); - g_parse->checkConstValue(tPtr, sPtr->t); - tPtr->setDefault(sPtr->v); - EnumPtr e = EnumPtr::dynamicCast(g_parse->currentContainer()); - assert(e); - e->addMember(tPtr); - $$ = e; -} -| -{ -} -; - -// ---------------------------------------------------------------------- -namespace_def -// ---------------------------------------------------------------------- -: TARS_NAMESPACE TARS_IDENTIFIER -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - ContainerPtr c = g_parse->currentContainer(); - NamespacePtr n = c->createNamespace(ident->v); - if(n) - { - g_parse->pushContainer(n); - $$ = GrammarBasePtr::dynamicCast(n); - } - else - { - $$ = 0; - } -} -'{' definitions '}' -{ - if($3) - { - g_parse->popContainer(); - $$ = $3; - } - else - { - $$ = 0; - } -} -; - - -//结构key------------------------------------------------------ -key_def -// ---------------------------------------------------------------------- -: TARS_KEY '[' scoped_name ',' -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3); - StructPtr sp = StructPtr::dynamicCast(g_parse->findUserType(ident->v)); - if(!sp) - { - g_parse->error("struct '" + ident->v + "' undefined!"); - } - - g_parse->setKeyStruct(sp); -} -key_members ']' -{ -} -; - -// 结构key------------------------------------------------------ -key_members -// ---------------------------------------------------------------------- -: TARS_IDENTIFIER -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - StructPtr np = g_parse->getKeyStruct(); - if(np) - { - np->addKey(ident->v); - } - else - { - $$ = 0; - } -} -| key_members ',' TARS_IDENTIFIER -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3); - StructPtr np = g_parse->getKeyStruct(); - if(np) - { - np->addKey(ident->v); - } - else - { - $$ = 0; - } -} -; - - -// ---------------------------------------------------------------------- -interface_def -// ---------------------------------------------------------------------- -: interface_id -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - - NamespacePtr c = NamespacePtr::dynamicCast(g_parse->currentContainer()); - - InterfacePtr cl = c->createInterface(ident->v); - if(cl) - { - g_parse->pushContainer(cl); - $$ = GrammarBasePtr::dynamicCast(cl); - } - else - { - $$ = 0; - } -} -'{' interface_exports '}' -{ - if($2) - { - g_parse->popContainer(); - $$ = GrammarBasePtr::dynamicCast($2); - } - else - { - $$ = 0; - } -} -; - -// ---------------------------------------------------------------------- -interface_id -// ---------------------------------------------------------------------- -: TARS_INTERFACE TARS_IDENTIFIER -{ - $$ = $2; -} -| TARS_INTERFACE keyword -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - g_parse->error("keyword `" + ident->v + "' cannot be used as interface name"); - $$ = $2; -} -; - -// ---------------------------------------------------------------------- -interface_exports -// ---------------------------------------------------------------------- -: interface_export ';' interface_exports -{ -} -| error ';' interface_exports -{ -} -| interface_export -{ - g_parse->error("`;' missing after definition"); -} -| -{ -} -; - -// ---------------------------------------------------------------------- -interface_export -// ---------------------------------------------------------------------- -: operation -; - -// ---------------------------------------------------------------------- -operation -// ---------------------------------------------------------------------- -: operation_preamble parameters ')' -{ - if($1) - { - g_parse->popContainer(); - $$ = GrammarBasePtr::dynamicCast($1); - } - else - { - $$ = 0; - } -} -; - -// ---------------------------------------------------------------------- -operation_preamble -// ---------------------------------------------------------------------- -: return_type TARS_OP -{ - TypePtr returnType = TypePtr::dynamicCast($1); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - string name = ident->v; - InterfacePtr cl = InterfacePtr::dynamicCast(g_parse->currentContainer()); - if(cl) - { - OperationPtr op = cl->createOperation(name, returnType); - if(op) - { - g_parse->pushContainer(op); - $$ = GrammarBasePtr::dynamicCast(op); - } - else - { - $$ = 0; - } - } - else - { - $$ = 0; - } -} -; - -// ---------------------------------------------------------------------- -return_type -// ---------------------------------------------------------------------- -: type -| TARS_VOID -{ - $$ = 0; -} -; - - -// ---------------------------------------------------------------------- -parameters -// ---------------------------------------------------------------------- -: // empty -{ -} -| type_id -{ - TypeIdPtr tsp = TypeIdPtr::dynamicCast($1); - - OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer()); - assert(op); - if(op) - { - op->createParamDecl(tsp, false, false); - } -} -| parameters ',' type_id -{ - TypeIdPtr tsp = TypeIdPtr::dynamicCast($3); - - OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer()); - assert(op); - if(op) - { - op->createParamDecl(tsp, false, false); - } -} -| out_qualifier type_id -{ - BoolGrammarPtr isOutParam = BoolGrammarPtr::dynamicCast($1); - TypeIdPtr tsp = TypeIdPtr::dynamicCast($2); - - OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer()); - assert(op); - if(op) - { - op->createParamDecl(tsp, isOutParam->v, false); - } -} -| parameters ',' out_qualifier type_id -{ - BoolGrammarPtr isOutParam = BoolGrammarPtr::dynamicCast($3); - TypeIdPtr tsp = TypeIdPtr::dynamicCast($4); - - OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer()); - assert(op); - if(op) - { - op->createParamDecl(tsp, isOutParam->v, false); - } -} -| routekey_qualifier type_id -{ - BoolGrammarPtr isRouteKeyParam = BoolGrammarPtr::dynamicCast($1); - TypeIdPtr tsp = TypeIdPtr::dynamicCast($2); - - OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer()); - assert(op); - if(op) - { - op->createParamDecl(tsp, false, isRouteKeyParam->v); - } -} -| parameters ',' routekey_qualifier type_id -{ - BoolGrammarPtr isRouteKeyParam = BoolGrammarPtr::dynamicCast($3); - TypeIdPtr tsp = TypeIdPtr::dynamicCast($4); - - OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer()); - assert(op); - if(op) - { - op->createParamDecl(tsp, false, isRouteKeyParam->v); - } -} -| out_qualifier -{ - g_parse->error("'out' must be defined with a type"); -} -| routekey_qualifier -{ - g_parse->error("'routekey' must be defined with a type"); -} -; - -// ---------------------------------------------------------------------- -routekey_qualifier -// ---------------------------------------------------------------------- -: TARS_ROUTE_KEY -{ - BoolGrammarPtr routekey = new BoolGrammar; - routekey->v = true; - $$ = GrammarBasePtr::dynamicCast(routekey); -} -; - -// ---------------------------------------------------------------------- -out_qualifier -// ---------------------------------------------------------------------- -: TARS_OUT -{ - BoolGrammarPtr out = new BoolGrammar; - out->v = true; - $$ = GrammarBasePtr::dynamicCast(out); -} -; - -// 结构定义-------------------------------------------------------------- -struct_def -// ---------------------------------------------------------------------- -: struct_id -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - NamespacePtr np = NamespacePtr::dynamicCast(g_parse->currentContainer()); - if(np) - { - StructPtr sp = np->createStruct(ident->v); - if(sp) - { - g_parse->pushContainer(sp); - $$ = GrammarBasePtr::dynamicCast(sp); - } - else - { - $$ = 0; - } - } - else - { - g_parse->error("struct '" + ident->v + "' must definition in namespace"); - } -} -'{' struct_exports '}' -{ - if($2) - { - g_parse->popContainer(); - } - $$ = $2; - - //ä¸èƒ½æœ‰ç©ºç»“æž„ - StructPtr st = StructPtr::dynamicCast($$); - assert(st); - if(st->getAllMemberPtr().size() == 0) - { - g_parse->error("struct `" + st->getSid() + "' must have at least one member"); - } -} -; - -// 结构å称定义---------------------------------------------------------- -struct_id -// ---------------------------------------------------------------------- -: TARS_STRUCT TARS_IDENTIFIER -{ - $$ = $2; -} -| TARS_STRUCT keyword -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - - g_parse->error("keyword `" + ident->v + "' cannot be used as struct name"); -} -| TARS_STRUCT error -{ - g_parse->error("abstract declarator '' used as declaration"); -} -; - -// 结构æˆå‘˜å˜é‡éƒ¨åˆ†------------------------------------------------------ -struct_exports -// ---------------------------------------------------------------------- -: data_member ';' struct_exports -{ - -} -| data_member -{ - g_parse->error("';' missing after definition"); -} -| -{ -} -; - -// -// -// æ•°æ®æˆå‘˜-------------------------------------------------------------- -data_member -// ---------------------------------------------------------------------- -: struct_type_id -{ - $$ = GrammarBasePtr::dynamicCast($1); -} -; - -// 结构的数æ®æˆå‘˜-------------------------------------------------------------- -struct_type_id -// ---------------------------------------------------------------------- -: TARS_CONST_INTEGER TARS_REQUIRE type_id -{ - StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer()); - if(np) - { - IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1); - g_parse->checkTag(iPtr->v); - - TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3); - tPtr->setRequire(iPtr->v); - np->addTypeId(tPtr); - $$ = GrammarBasePtr::dynamicCast($3); - } - else - { - $$ = 0; - } -} -| TARS_CONST_INTEGER TARS_REQUIRE type_id '=' const_initializer -{ - StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer()); - if(np) - { - IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1); - g_parse->checkTag(iPtr->v); - - //判断类型和数值类型是å¦ä¸€è‡´ - TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3); - ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($5); - g_parse->checkConstValue(tPtr, sPtr->t); - - tPtr->setRequire(iPtr->v); - tPtr->setDefault(sPtr->v); - np->addTypeId(tPtr); - $$ = GrammarBasePtr::dynamicCast($3); - } - else - { - $$ = 0; - } -} -| TARS_CONST_INTEGER TARS_OPTIONAL type_id '=' const_initializer -{ - StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer()); - if(np) - { - IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1); - g_parse->checkTag(iPtr->v); - - //判断类型和数值类型是å¦ä¸€è‡´ - TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3); - ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($5); - g_parse->checkConstValue(tPtr, sPtr->t); - - tPtr->setOptional(iPtr->v); - tPtr->setDefault(sPtr->v); - np->addTypeId(tPtr); - $$ = GrammarBasePtr::dynamicCast($3); - } - else - { - $$ = 0; - } -} -| TARS_CONST_INTEGER TARS_OPTIONAL type_id -{ - StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer()); - if(np) - { - IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1); - g_parse->checkTag(iPtr->v); - TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3); - tPtr->setOptional(iPtr->v); - np->addTypeId(tPtr); - $$ = GrammarBasePtr::dynamicCast($3); - } - else - { - $$ = 0; - } -} -| TARS_REQUIRE type_id -{ - g_parse->error("struct member need 'tag'"); -} -| TARS_OPTIONAL type_id -{ - g_parse->error("struct member need 'tag'"); -} -| TARS_CONST_INTEGER type_id -{ - g_parse->error("struct member need 'require' or 'optional'"); -} -| type_id -{ - g_parse->error("struct member need 'tag' or 'require' or 'optional'"); -} -; - -// ---------------------------------------------------------------------- -const_initializer -// ---------------------------------------------------------------------- -: TARS_CONST_INTEGER -{ - IntergerGrammarPtr intVal = IntergerGrammarPtr::dynamicCast($1); - ostringstream sstr; - sstr << intVal->v; - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::VALUE; - c->v = sstr.str(); - $$ = c; -} -| TARS_CONST_FLOAT -{ - FloatGrammarPtr floatVal = FloatGrammarPtr::dynamicCast($1); - ostringstream sstr; - sstr << floatVal->v; - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::VALUE; - c->v = sstr.str(); - $$ = c; -} -| TARS_STRING_LITERAL -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::STRING; - c->v = ident->v; - $$ = c; -} -| TARS_FALSE -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::BOOL; - c->v = ident->v; - $$ = c; -} -| TARS_TRUE -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::BOOL; - c->v = ident->v; - $$ = c; -} -| TARS_IDENTIFIER -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - - if (g_parse->checkEnum(ident->v) == false) - { - g_parse->error("error enum default value, not defined yet"); - } - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::ENUM; - c->v = ident->v; - $$ = c; -} -| scoped_name TARS_SCOPE_DELIMITER TARS_IDENTIFIER -{ - - StringGrammarPtr scoped = StringGrammarPtr::dynamicCast($1); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3); - - if (g_parse->checkEnum(ident->v) == false) - { - g_parse->error("error enum default value, not defined yet"); - } - ConstGrammarPtr c = new ConstGrammar(); - c->t = ConstGrammar::ENUM; - c->v = scoped->v + "::" + ident->v; - $$ = c; -} -; - -// 常é‡å®šä¹‰-------------------------------------------------------------- -const_def -// ---------------------------------------------------------------------- -: TARS_CONST type_id '=' const_initializer -{ - NamespacePtr np = NamespacePtr::dynamicCast(g_parse->currentContainer()); - if(!np) - { - g_parse->error("const type must define in namespace"); - } - - TypeIdPtr t = TypeIdPtr::dynamicCast($2); - ConstGrammarPtr c = ConstGrammarPtr::dynamicCast($4); - ConstPtr cPtr = np->createConst(t, c); - $$ = cPtr; -} -; - -// å˜é‡å®šä¹‰-------------------------------------------------------------- -type_id -// ---------------------------------------------------------------------- -: type TARS_IDENTIFIER -{ - TypePtr type = TypePtr::dynamicCast($1); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - - TypeIdPtr typeIdPtr = new TypeId(type, ident->v); - - $$ = GrammarBasePtr::dynamicCast(typeIdPtr); -} -|type TARS_IDENTIFIER '[' TARS_CONST_INTEGER ']' -{ - TypePtr type = g_parse->createVector(TypePtr::dynamicCast($1)); - IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($4); - g_parse->checkArrayVaid(type,iPtrSize->v); - type->setArray(iPtrSize->v); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - TypeIdPtr typeIdPtr = new TypeId(type, ident->v); - $$ = GrammarBasePtr::dynamicCast(typeIdPtr); -} -|type '*' TARS_IDENTIFIER -{ - TypePtr type = g_parse->createVector(TypePtr::dynamicCast($1)); - //IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($4); - g_parse->checkPointerVaid(type); - type->setPointer(true); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3); - TypeIdPtr typeIdPtr = new TypeId(type, ident->v); - $$ = GrammarBasePtr::dynamicCast(typeIdPtr); -} -|type TARS_IDENTIFIER ':' TARS_CONST_INTEGER -{ - TypePtr type = TypePtr::dynamicCast($1); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - TypeIdPtr typeIdPtr = new TypeId(type, ident->v); - IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($3); - g_parse->checkArrayVaid(type,iPtrSize->v); - $$ = GrammarBasePtr::dynamicCast(typeIdPtr); -} -| type keyword -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - g_parse->error("keyword `" + ident->v + "' cannot be used as data member name"); -} -| type -{ - g_parse->error("missing data member name"); -} -| error -{ - g_parse->error("unkown type"); -} -; - -// 类型------------------------------------------------------------------ -type -// ---------------------------------------------------------------------- -: type_no ':' TARS_CONST_INTEGER -{ - - TypePtr type = TypePtr::dynamicCast($1); - IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($3); - g_parse->checkArrayVaid(type,iPtrSize->v); - type->setArray(iPtrSize->v); - $$ = type; -} -| type_no -{ - $$ = $1; -} -| type_no ':' error -{ - g_parse->error("array missing size"); -} -; - -// 没有默认大å°çš„类型------------------------------------------------------------------ -type_no -// ---------------------------------------------------------------------- -: TARS_BOOL -{ - $$ = g_parse->createBuiltin(Builtin::KindBool); -} -| TARS_BYTE -{ - $$ = g_parse->createBuiltin(Builtin::KindByte); -} -| TARS_UNSIGNED TARS_BYTE //unsigned char 对应 short -{ - $$ = g_parse->createBuiltin(Builtin::KindShort,true); -} -| TARS_SHORT -{ - $$ = g_parse->createBuiltin(Builtin::KindShort); -} -| TARS_UNSIGNED TARS_SHORT -{ - $$ = g_parse->createBuiltin(Builtin::KindInt,true); -} -| TARS_INT -{ - $$ = g_parse->createBuiltin(Builtin::KindInt); -} -| TARS_UNSIGNED TARS_INT -{ - $$ = g_parse->createBuiltin(Builtin::KindLong,true); -} -| TARS_LONG -{ - $$ = g_parse->createBuiltin(Builtin::KindLong); -} -| TARS_FLOAT -{ - $$ = g_parse->createBuiltin(Builtin::KindFloat); -} -| TARS_DOUBLE -{ - $$ = g_parse->createBuiltin(Builtin::KindDouble); -} -| TARS_STRING -{ - $$ = g_parse->createBuiltin(Builtin::KindString); -} -| vector -{ - $$ = GrammarBasePtr::dynamicCast($1); -} -| map -{ - $$ = GrammarBasePtr::dynamicCast($1); -} -| scoped_name -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1); - TypePtr sp = g_parse->findUserType(ident->v); - if(sp) - { - $$ = GrammarBasePtr::dynamicCast(sp); - } - else - { - g_parse->error("'" + ident->v + "' undefined!"); - } -} -; - -// 关键字---------------------------------------------------------------- -vector -// ---------------------------------------------------------------------- -: TARS_VECTOR '<' type '>' -{ - $$ = GrammarBasePtr::dynamicCast(g_parse->createVector(TypePtr::dynamicCast($3))); -} -| TARS_VECTOR '<' error -{ - g_parse->error("vector error"); -} -| TARS_VECTOR '<' type error -{ - g_parse->error("vector missing '>'"); -} -| TARS_VECTOR error -{ - g_parse->error("vector missing type"); -} -; - -// 关键字---------------------------------------------------------------- -map -// ---------------------------------------------------------------------- -: TARS_MAP '<' type ',' type '>' -{ - $$ = GrammarBasePtr::dynamicCast(g_parse->createMap(TypePtr::dynamicCast($3), TypePtr::dynamicCast($5))); -} -| TARS_MAP '<' error -{ - g_parse->error("map error"); -} -; - -// ---------------------------------------------------------------------- -scoped_name -// ---------------------------------------------------------------------- -: TARS_IDENTIFIER -{ -} -| TARS_SCOPE_DELIMITER TARS_IDENTIFIER -{ - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2); - ident->v = "::" + ident->v; - $$ = GrammarBasePtr::dynamicCast(ident); -} -| scoped_name TARS_SCOPE_DELIMITER TARS_IDENTIFIER -{ - StringGrammarPtr scoped = StringGrammarPtr::dynamicCast($1); - StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3); - scoped->v += "::"; - scoped->v += ident->v; - $$ = GrammarBasePtr::dynamicCast(scoped); -} -; - -// 关键字---------------------------------------------------------------- -keyword -// ---------------------------------------------------------------------- -: TARS_STRUCT -{ -} -| TARS_VOID -{ -} -| TARS_BOOL -{ -} -| TARS_BYTE -{ -} -| TARS_SHORT -{ -} -| TARS_INT -{ -} -| TARS_FLOAT -{ -} -| TARS_DOUBLE -{ -} -| TARS_STRING -{ -} -| TARS_VECTOR -{ -} -| TARS_KEY -{ -} -| TARS_MAP -{ -} -| TARS_NAMESPACE -{ -} -| TARS_INTERFACE -{ -} -| TARS_OUT -{ -} -| TARS_REQUIRE -{ -} -| TARS_OPTIONAL -{ -} -| TARS_CONST_INTEGER -{ -} -| TARS_CONST_FLOAT -{ -} -| TARS_FALSE -{ -} -| TARS_TRUE -{ -} -| TARS_STRING_LITERAL -{ -} -| TARS_CONST -{ -} -| TARS_ENUM -{ -} -| TARS_UNSIGNED -{ -} -; - -%% - - diff --git a/cpp/tools/tarsparse/CMakeLists.txt b/cpp/tools/tarsparse/CMakeLists.txt deleted file mode 100644 index 8ac92ff31..000000000 --- a/cpp/tools/tarsparse/CMakeLists.txt +++ /dev/null @@ -1,37 +0,0 @@ - -set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) - -set(TARGETNAME "tarsparse") - -aux_source_directory(. DIR_SRCS) - -set(DEPENDS_SRC_LIST) - -set(DEPENDS_LEC_YACC_SRC_LIST) -list(APPEND DEPENDS_LEC_YACC_SRC_LIST "tars.lex.cpp") -list(APPEND DEPENDS_LEC_YACC_SRC_LIST "tars.tab.hpp") -list(APPEND DEPENDS_LEC_YACC_SRC_LIST "tars.tab.cpp") - -set(DEPENDS_LEC_YACC_SRC_DIR "${PROJECT_SOURCE_DIR}/tarsGrammar/") - -set(CURRENT_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}") - -foreach(LEC_YACC_SRC ${DEPENDS_LEC_YACC_SRC_LIST}) - - list(APPEND DEPENDS_SRC_LIST ${CURRENT_SRC_DIR}/${LEC_YACC_SRC}) - - add_custom_command( - OUTPUT ${CURRENT_SRC_DIR}/${LEC_YACC_SRC} - WORKING_DIRECTORY ${CURRENT_SRC_DIR}) -# COMMAND cp ${DEPENDS_LEC_YACC_SRC_DIR}/${LEC_YACC_SRC} ${CURRENT_SRC_DIR} -# COMMENT "cp ${DEPENDS_LEC_YACC_SRC_DIR}/${LEC_YACC_SRC} ${CURRENT_SRC_DIR}") - -endforeach() - -add_library(${TARGETNAME} ${DIR_SRCS} ${DEPENDS_SRC_LIST}) - -add_dependencies(${TARGETNAME} COPY-LEX-YACC) - -install(TARGETS tarsparse - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) diff --git a/cpp/tools/tarsparse/element.cpp b/cpp/tools/tarsparse/element.cpp deleted file mode 100644 index fdd902553..000000000 --- a/cpp/tools/tarsparse/element.cpp +++ /dev/null @@ -1,339 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "element.h" -#include -#include -#include "parse.h" -#include "util/tc_file.h" -#include "util/tc_common.h" - -/*************************************************************************************************/ -const char* Builtin::builtinTable[] = - { - "void", - "bool", - "byte", - "short", - "int", - "long", - "float", - "double", - "string", - "vector", - "map" - }; - -Builtin::Builtin(Kind kind,bool isUnsigned = false) : _kind(kind),_isUnsigned(isUnsigned) -{ -} - -Builtin::Kind Builtin::kind() const -{ - return _kind; -} - -bool Builtin::isSimple() const -{ - switch(_kind) - { - case Builtin::KindBool: - case Builtin::KindByte: - case Builtin::KindShort: - case Builtin::KindInt: - case Builtin::KindLong: - case Builtin::KindFloat: - case Builtin::KindDouble: - return true; - default: - return false; - } - - return true; -} - -string Builtin::def() const -{ - switch(_kind) - { - case Builtin::KindBool: - return "true"; - case Builtin::KindByte: - case Builtin::KindShort: - case Builtin::KindInt: - case Builtin::KindLong: - case Builtin::KindFloat: - case Builtin::KindDouble: - return "0"; - case Builtin::KindString: - return ""; - default: - assert(false); - } - - return ""; -} -/*************************************************************************************************/ -Vector::Vector(const TypePtr& ptr) : _ptr(ptr) -{ - -} - -/*************************************************************************************************/ -Map::Map(const TypePtr& pleft, const TypePtr& pright): _pleft(pleft), _pright(pright) -{ - -} - - -/*************************************************************************************************/ - -TypeId::TypeId(const TypePtr& ptr, const string&id) -: _ptr(ptr) -, _id(id) -, _bRequire(true) -, _tag(0) -, _bHasDefault(false) -, _size(0) -, _array(false) - -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(_ptr); - if(bPtr) - { - _bHasDefault = true; - _default = bPtr->def(); - } -} - -void TypeId::setRequire(int tag) -{ - _bRequire = true; - _tag = tag; -} - -void TypeId::setDefault(const string &def) -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(_ptr); - EnumPtr ePtr = EnumPtr::dynamicCast(_ptr); - assert(bPtr || ePtr); - - - _bHasDefault = true; - _default = def; -} - -void TypeId::disableDefault() -{ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(_ptr); - assert(bPtr); - _bHasDefault = false; -} - -void TypeId::setOptional(int tag) -{ - _bRequire = false; - _tag = tag; -} - -/*************************************************************************************************/ -NamespacePtr Container::createNamespace(const string &id) -{ - NamespacePtr np = NamespacePtr::dynamicCast(this); - if(np) - { - g_parse->error("namespace can't be nested!"); - } - - _ns.push_back(new Namespace(id)); - - g_parse->currentContextPtr()->addNamespacePtr(_ns.back()); - return _ns.back(); -} - -/*************************************************************************************************/ -ParamDeclPtr Operation::createParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k) -{ - _itag++; - if(_itag > 255) - { - g_parse->error("operation '" + _id + "' can't have more than 255 paramters!"); - } - - if(typeIdPtr->getId() == getId()) - { - g_parse->error("paramters name '" + _id + "' conflicts with operation name!"); - } - - typeIdPtr->setRequire(_itag); - _ps.push_back(new ParamDecl(typeIdPtr, v, k)); - return _ps.back(); -} - -/*************************************************************************************************/ -OperationPtr Interface::createOperation(const string &id, const TypePtr &typePtr) -{ - for(size_t i = 0; i < _ops.size(); i++) - { - if(_ops[i]->getId() == id) - { - g_parse->error("operation '" + id + "' exists in interface '" + _id + "'"); - return NULL; - } - } - - _ops.push_back(new Operation(id, typePtr)); - return _ops.back(); -} - -/*************************************************************************************************/ - -InterfacePtr Namespace::createInterface(const string &id) -{ - for(size_t i = 0; i < _is.size(); i++) - { - if(_is[i]->getId() == id) - { - g_parse->error("interface '" + id + "' exists in namespace '" + _id + "'"); - return NULL; - } - } - - if(id == _id) - { - g_parse->error("interface '" + id + "' must not be same with namespace '" + _id + "'"); - return NULL; - } - - _is.push_back(new Interface(id)); - return _is.back(); -} - -StructPtr Namespace::createStruct(const string& id) -{ - g_parse->checkConflict(_id + "::" + id); - - _ss.push_back(new Struct(id, _id + "::" + id)); - g_parse->addStructPtr(_ss.back()); - - return _ss.back(); -} - -EnumPtr Namespace::createEnum(const string &id) -{ - g_parse->checkConflict(_id + "::" + id); - - _es.push_back(new Enum(id, _id + "::" + id)); - g_parse->addEnumPtr(_es.back()); - - return _es.back(); -} - -ConstPtr Namespace::createConst(TypeIdPtr &pPtr, ConstGrammarPtr &cPtr) -{ - for(size_t i = 0; i < _cs.size(); i++) - { - if(_cs[i]->getTypeIdPtr()->getId() == pPtr->getId()) - { - g_parse->error("const type '" + pPtr->getId() + "' exists in namespace '" + _id + "'"); - return NULL; - } - } - - g_parse->checkConstValue(pPtr, cPtr->t); - _cs.push_back(new Const(pPtr, cPtr)); - - return _cs.back(); -} - -/*************************************************************************************************/ - -void Struct::addTypeId(const TypeIdPtr &typeIdPtr) -{ - StructPtr sp = StructPtr::dynamicCast(typeIdPtr->getTypePtr()); - if(sp) - { - if(sp->getSid() == getSid()) - { - g_parse->error("struct can't take self as member data"); - } - } - - for(size_t i = 0; i < _members.size(); i++) - { - if(_members[i]->getId() == typeIdPtr->getId()) - { - g_parse->error("data member '" + typeIdPtr->getId() + "' duplicate definition"); - } - if(_members[i]->getTag() == typeIdPtr->getTag()) - { - g_parse->error("data member '" + typeIdPtr->getId() + "' has equal tag with '" + _members[i]->getId() + "'"); - } - - if(_members[i]->getTag() > typeIdPtr->getTag()) - { - _members.insert(_members.begin() + i, typeIdPtr); - return; - } - } - - _members.push_back(typeIdPtr); -} - -void Struct::addKey(const string &member) -{ - size_t i; - for(i = 0; i < _members.size(); i++) - { - if(_members[i]->getId() == member) - { - break; - } - } - - if(i == _members.size()) - { - g_parse->error("key member '" + member + "' is not struct member"); - } - - _key.push_back(member); -} -/***********************************************************************************/ - -void Enum::addMember(const TypeIdPtr &typeIdPtr) -{ - for(size_t i = 0; i < _members.size(); i++) - { - if(_members[i]->getId() == typeIdPtr->getId()) - { - g_parse->error("enum member '" + typeIdPtr->getId() + "' duplicate definition"); - } - } - _members.push_back(typeIdPtr); -} - -/***********************************************************************************/ - -void Context::addInclude(const string &incl) -{ - if(incl == _filename) - { - g_parse->error("can't include self"); - } - _includes.push_back(tars::TC_File::excludeFileExt(incl) + ".h"); -} - - diff --git a/cpp/tools/tarsparse/element.h b/cpp/tools/tarsparse/element.h deleted file mode 100644 index 815c3afa3..000000000 --- a/cpp/tools/tarsparse/element.h +++ /dev/null @@ -1,861 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _ELEMENT_H -#define _ELEMENT_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include "errno.h" -#include "util/tc_autoptr.h" - -using namespace std; - - -void yyerror(char const *msg); - -/** - * 语法元素基类 - */ -class GrammarBase : virtual public tars::TC_HandleBase -{ -public: - virtual ~GrammarBase(){}; -}; - -typedef tars::TC_AutoPtr GrammarBasePtr; - -/** - * 解æžè¿‡ç¨‹ä¸­çš„字符串 - */ -class StringGrammar : public GrammarBase -{ -public: - - StringGrammar() { } - string v; -}; - -typedef tars::TC_AutoPtr StringGrammarPtr; - -/** - * 解æžè¿‡ç¨‹ä¸­çš„整形数 - */ -class IntergerGrammar : public GrammarBase -{ -public: - - IntergerGrammar():v(0) { } - long long v; -}; - -typedef tars::TC_AutoPtr IntergerGrammarPtr; - -/** - * 解æžè¿‡ç¨‹ä¸­çš„浮点数 - */ -class FloatGrammar : public GrammarBase -{ -public: - - FloatGrammar():v(0.0f) { } - double v; -}; - -typedef tars::TC_AutoPtr FloatGrammarPtr; - -/** - * 解æžè¿‡ç¨‹ä¸­çš„bool类型 - */ -class BoolGrammar : public GrammarBase -{ -public: - - BoolGrammar():v(false) { } - bool v; -}; - -typedef tars::TC_AutoPtr BoolGrammarPtr; - -/** - * 解æžè¿‡ç¨‹ä¸­çš„常类型 - */ -class ConstGrammar : public GrammarBase -{ -public: - - ConstGrammar():t(VALUE) { } - - enum - { - VALUE, - STRING, - BOOL, - ENUM - }; - - int t; - string v; -}; - -typedef tars::TC_AutoPtr ConstGrammarPtr; - -/////////////////////////////////////////////// - -/** - * 类型基类 - */ -class Type : virtual public GrammarBase -{ -public: - /** - * 构造函数 - * @param Type - */ - Type():_size(0),_array(false),_pointer(false){}; - - //是å¦ç®€å•ç±»åž‹ - virtual bool isSimple() const { return false;} - - //是å¦æ•°ç»„类型 - virtual bool isArray() const { return _array;} - virtual void setArray(int size) {_array = true;_size = size;} - virtual int getSize() const {return _size;}; - - //是å¦æŒ‡é’ˆç±»åž‹ - virtual bool isPointer() const { return _pointer;} - virtual void setPointer(bool b) {_pointer = b;} -public: - int _size; - bool _array; - bool _pointer; -}; - -typedef tars::TC_AutoPtr TypePtr; - -/** - * 内建类型 - */ -class Builtin : public Type -{ -public: - - enum Kind - { - KindVoid, - KindBool, - KindByte, - KindShort, - KindInt, - KindLong, - KindFloat, - KindDouble, - KindString, - KindVector, - KindMap - }; - - /** - * 构造函数 - * @param kind - */ - Builtin(Kind kind,bool isUnsigned); - - /** - * 类型 - * - * @return Kind - */ - Kind kind() const; - - /** - * 是å¦æ˜¯ç®€å•ç±»åž‹ - * - * @return bool - */ - bool isSimple() const; - - /** - * 是å¦æ˜¯unsined类型 - * - * @return bool - */ - bool isUnsigned() const {return _isUnsigned;} - - /** - * 设置是unsined类型 - * - * @return bool - */ - void setUnsigned(bool isUnsigned = false) {_isUnsigned = isUnsigned;} - - /** - * 缺çœå€¼ - * - * @return string - */ - string def() const; - - /** - * 字符串标示 - */ - static const char* builtinTable[]; - -protected: - - Kind _kind; - bool _isUnsigned; -}; - -typedef tars::TC_AutoPtr BuiltinPtr; - -/** - * Vector类型 - */ -class Vector : public Type -{ -public: - /** - * 构造函数 - * @param ptr - */ - Vector(const TypePtr& ptr); - - /** - * 获å–类型 - * - * @return TypePtr& - */ - TypePtr& getTypePtr() {return _ptr;} -protected: - TypePtr _ptr; -}; - -typedef tars::TC_AutoPtr VectorPtr; - -/** - * Map类型 - */ -class Map : public Type -{ -public: - /** - * 构造函数 - * @param pleft - * @param pright - */ - Map(const TypePtr& pleft, const TypePtr& pright); - - /** - * 获å–左类型 - * - * @return TypePtr& - */ - TypePtr& getLeftTypePtr() {return _pleft;} - - /** - * 获å–å³ç±»åž‹ - * - * @return TypePtr& - */ - TypePtr& getRightTypePtr() {return _pright;} - -protected: - TypePtr _pleft; - TypePtr _pright; -}; - -typedef tars::TC_AutoPtr MapPtr; - -/** - * å˜é‡å®šä¹‰ - */ -class TypeId : public GrammarBase -{ -public: - /** - * 构造函数 - * @param ptr - * @param id - */ - TypeId(const TypePtr& ptr, const string& id); - - /** - * å˜é‡å称 - * - * @return string - */ - string getId() const { return _id; } - - /** - * å˜é‡ç±»åž‹ - * - * @return Type* - */ - TypePtr& getTypePtr() { return _ptr;} - - /** - * 是å¦éœ€è¦è¯¥å‚æ•° - * - * @return bool - */ - bool isRequire() const { return _bRequire; } - - /** - * 设置必选字段 - * @param tag - */ - void setRequire(int tag); - - /** - * 设置å¯é€‰å­—段 - * åªæœ‰åŸºæœ¬ç±»åž‹æ‰æœ‰ç¼ºçœå€¼ - */ - void setDefault(const string &def); - - /** - * - * åªæœ‰åŸºæœ¬ç±»åž‹æ‰æœ‰ç¼ºçœå€¼ - */ - void disableDefault(); - - /** - * 设置å¯é€‰å­—段 - * @param tag - */ - void setOptional(int tag); - - /** - * tag标识 - */ - int getTag() const { return _tag; } - - /** - * 缺çœå€¼ - * - * @return string - */ - string def() const { return _default; } - - /** - * 是å¦æœ‰ç¼ºçœå€¼ - * - * @return bool - */ - bool hasDefault() const { return _bHasDefault; } - -protected: - TypePtr _ptr; - string _id; - bool _bRequire; - int _tag; - bool _bHasDefault; - string _default; - -public: - int _size; - bool _array; -}; - -typedef tars::TC_AutoPtr TypeIdPtr; - - -//////////////////////////////////////////////////// -// -class Namespace; -typedef tars::TC_AutoPtr NamespacePtr; -/** - * 容器基类, 所有å¯ä»¥åŒ…å«å…¶ä»–元素的元素都从该基类继承 - */ -class Container : virtual public GrammarBase -{ -public: - /** - * 构造函数 - * @param id - */ - Container(const string &id) : _id(id) - { - } - - /** - * 生æˆå字空间 - * @param id - * - * @return NamespacePtr - */ - NamespacePtr createNamespace(const string &id); - - /** - * 获å–ID - * - * @return string - */ - string getId() const { return _id;} - - /** - * 获å–所有的å字空间 - * - * @return vector& - */ - vector &getAllNamespacePtr() { return _ns; } -protected: - string _id; - vector _ns; -}; - -typedef tars::TC_AutoPtr ContainerPtr; - -//////////////////////////////////////////////////// -// -class Const : public GrammarBase -{ -public: - /** - * - * @param tPtr - * @param cPtr - */ - Const(TypeIdPtr &tPtr, ConstGrammarPtr &cPtr) : _tPtr(tPtr), _cPtr(cPtr) - { - } - - /** - * - * - * @return TypePtr& - */ - TypeIdPtr &getTypeIdPtr() { return _tPtr; } - - /** - * - * - * @return ConstGrammarPtr - */ - ConstGrammarPtr getConstGrammarPtr() { return _cPtr; } - -protected: - TypeIdPtr _tPtr; - ConstGrammarPtr _cPtr; -}; - -typedef tars::TC_AutoPtr ConstPtr; - -///////////////////////////////////////////////////////// -// -class Enum : virtual public Container, virtual public Type -{ -public: - Enum(const string &id, const string &sid) : Container(id), _sid(sid) - { - } - - /** - * 添加æˆå‘˜å˜é‡ - * @param ptid - */ - void addMember(const TypeIdPtr &ptr); - - /** - * 获å–所有的æˆå‘˜å˜é‡ - * - * @return vector& - */ - vector& getAllMemberPtr() {return _members;} - - /** - * 获å–结构的å称 - * - * @return string - */ - string getSid() const { return _sid; } - - /** - * 是å¦æ˜¯ç®€å•ç±»åž‹ - * - * @return bool - */ - virtual bool isSimple() const { return true; } - -protected: - - /** - * æ¯ä¸ªå˜é‡å称 - */ - vector _members; - - /** - * 包å«å字的空间的å称 - */ - string _sid; -}; - -typedef tars::TC_AutoPtr EnumPtr; - -//////////////////////////////////////////////////// -// -/** - * 结构 - */ -class Struct : virtual public Container, virtual public Type -{ -public: - /** - * 构造函数 - * @param id - */ - Struct(const string& id, const string &sid) : Container(id), _sid(sid) - { - } - - /** - * 添加æˆå‘˜å˜é‡ - * @param ptid - */ - void addTypeId(const TypeIdPtr &ptr); - - /** - * 获å–所有的æˆå‘˜å˜é‡ - * - * @return vector& - */ - vector& getAllMemberPtr() {return _members;} - - /** - * 获å–结构的å称 - * - * @return string - */ - string getSid() const { return _sid; } - - /** - * 增加å°äºŽmemeber - * @param member - */ - void addKey(const string &member); - - vector getKey() { return _key; } -protected: - vector _members; - vector _key; - string _sid; -}; - -typedef tars::TC_AutoPtr StructPtr; - -//////////////////////////////////////////////////// -// -/** - * å‚æ•°æè¿° - */ -class ParamDecl : public GrammarBase -{ -public: - /** - * 构造 - * @param typeIdPtr - * @param v - */ - ParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k) - : _typeIdPtr(typeIdPtr), _v(v), _k(k) - { - } - - /** - * å˜é‡å£°æ˜Ž - * - * @return TypeIdPtr& - */ - TypeIdPtr& getTypeIdPtr() { return _typeIdPtr; } - - /** - * 是å¦æ˜¯è¾“出å‚æ•° - * - * @return bool - */ - bool isOut() const { return _v; } - - /** - * 是å¦æ˜¯éœ€è¦è·¯ç”±çš„字段 - * - * @return bool - */ - bool isRouteKey() const { return _k; } - -protected: - TypeIdPtr _typeIdPtr; - bool _v; - bool _k; -}; -typedef tars::TC_AutoPtr ParamDeclPtr; - -/////////////////////////////////////////////////////// -// -/** - * æ“作类 - */ -class Operation : public Container -{ -public: - /** - * 构造函数 - * @param id - * @param typePtr - */ - Operation(const string &id, const TypePtr &typePtr) : Container(id), _itag(0) - { - _retPtr = new TypeId(typePtr, "_ret"); - _retPtr->setRequire(_itag); - } - - /** - * 生æˆä¸€ä¸ªå‚æ•° - * @param typeIdPtr - * @param v - * @param k - * - * @return ParamDeclPtr - */ - ParamDeclPtr createParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k); - - /** - * 获å–返回值类型 - * - * @return TypePtr& - */ - TypeIdPtr &getReturnPtr() { return _retPtr; } - - /** - * 获å–所有å‚æ•° - * - * @return vector& - */ - vector &getAllParamDeclPtr() { return _ps; } -protected: - int _itag; - TypeIdPtr _retPtr; - vector _ps; -}; - -typedef tars::TC_AutoPtr OperationPtr; -/////////////////////////////////////////////////////// -// -/** - * 接å£æè¿° - */ -class Interface : public Container -{ -public: - /** - * 构造 - * @param id - */ - Interface(const string &id) : Container(id) - { - } - - /** - * 生æˆä¸€ä¸ªæ“作 - * @param id - * @param typePtr - * - * @return OperationPtr - */ - OperationPtr createOperation(const string &id, const TypePtr &typePtr); - - /** - * 获å–所有æ“作 - * - * @return vector& - */ - vector &getAllOperationPtr() { return _ops; } -protected: - vector _ops; -}; - -typedef tars::TC_AutoPtr InterfacePtr; - -///////////////////////////////////////////////////////// -// -/** - * å字空间 - */ -class Namespace : public Container -{ -public: - /** - * 构造函数 - * @param id - */ - Namespace(const string &id) : Container(id) - { - } - - /** - * 生æˆæŽ¥å£ - * @param id - * - * @return InterfacePtr - */ - InterfacePtr createInterface(const string &id); - - /** - * 生æˆç»“æž„ - * @param id - * - * @return StructPtr - */ - StructPtr createStruct(const string& id); - - /** - * 生æˆæžšä¸¾ç±»åž‹ - * @param id - * - * @return EnumPtr - */ - EnumPtr createEnum(const string &id); - - /** - * - * @param pPtr - * @param cPtr - * - * @return ConstPtr - */ - ConstPtr createConst(TypeIdPtr &pPtr, ConstGrammarPtr &cPtr); - - /** - * 是å¦æœ‰æŽ¥å£ - * - * @return bool - */ - bool hasInterface() const { return !_is.empty(); } - - /** - * 获å–æ‰€æœ‰çš„æŽ¥å£ - * - * @return vector& - */ - vector &getAllInterfacePtr() { return _is; } - - /** - * 获å–所有的结构 - * - * @return vector& - */ - vector &getAllStructPtr() { return _ss; } - - /** - * 生æˆæžšä¸¾ç±»åž‹ - * - * @return vector& - */ - vector &getAllEnumPtr() { return _es; } - - /** - * 常é‡ç±»åž‹ - * - * @return vector& - */ - vector &getAllConstPtr() { return _cs; } - -protected: - vector _is; - vector _ss; - vector _es; - vector _cs; -}; - -///////////////////////////////////////////////////////////////// -/** - * 上下文 - */ -class Context : public tars::TC_HandleBase -{ -public: - /** - * 构造函数 - */ - Context(const string &file) : _currline(1), _filename(file) - { - } - - /** - * 下一行 - */ - void nextLine() { _currline++; } - - /** - * ç›®å‰çš„è¡Œ - * - * @return size_t - */ - size_t getCurrLine() { return _currline; } - - /** - * 当å‰æ–‡ä»¶å - * - * @return string - */ - string getFileName() { return _filename; } - - /** - * 添加include的文件 - * @param incl - */ - void addInclude(const string &incl); - - /** - * 添加属于这个文件的å字空间 - * @param c - */ - void addNamespacePtr(const NamespacePtr &c) - { - _namespaces.push_back(c); - } - - /** - * 获å–includes的文件 - * - * @return vector - */ - vector getIncludes() { return _includes; } - - /** - * 获å–å字空间 - * - * @return vector - */ - vector getNamespaces() { return _namespaces; } - -protected: - size_t _currline; - string _filename; - vector _includes; - vector _namespaces; -}; - -typedef tars::TC_AutoPtr ContextPtr; - -#endif - - diff --git a/cpp/tools/tarsparse/parse.cpp b/cpp/tools/tarsparse/parse.cpp deleted file mode 100644 index d39ecea74..000000000 --- a/cpp/tools/tarsparse/parse.cpp +++ /dev/null @@ -1,603 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "parse.h" -#include "tars.tab.hpp" -#include -#include -#include -#include "util/tc_common.h" -#include "util/tc_file.h" - -TarsParsePtr g_parse = new TarsParse(); - -void yyerror(char const *msg) -{ - g_parse->error(msg); -} - -TarsParse::TarsParse() -{ - _bWithTars = false; - _bUseCurrentPath = false; - _itab = 0; - _bUseCurrentPathFirst = false; - initScanner(); -} - -void TarsParse::setTars(bool bWithTars) -{ - _bWithTars = bWithTars; -} - -void TarsParse::setHeader(const string &sHeader) -{ - _sHeader = sHeader; -} - -void TarsParse::setCurrentPriority(bool bFlag) -{ - _bUseCurrentPathFirst = bFlag; -} - -string TarsParse::getHeader() -{ - if(_sHeader.empty()) - return _sHeader; - return _sHeader + "/"; -} - -void TarsParse::clear() -{ - while(!_contexts.empty()) _contexts.pop(); - while(!_contains.empty()) _contains.pop(); - _vcontexts.clear(); - _structs.clear(); - _enums.clear(); - _namespaces.clear(); -} - -void TarsParse::parse(const string& sFileName) -{ - if (_bUseCurrentPath) - { - std::string sTemp = sFileName; - bool isExist = tars::TC_File::isFileExist(sFileName); - - if (!isExist) - { - sTemp = tars::TC_File::extractFileName(sFileName); - } - - clear(); - - _contains.push(new Container("")); - if(!(yyin = fopen(sTemp.c_str(), "r"))) - { - error("open file '" + sFileName + "(" + sTemp + ")" + "' error :" + string(strerror(errno))); - } - - pushFile(sFileName); - - yyparse(); - - return ; - } - - if (_bUseCurrentPathFirst) - { - std::string sTemp = tars::TC_File::extractFileName(sFileName); - bool isExist = tars::TC_File::isFileExist(sTemp); - - if (!isExist) - { - sTemp = sFileName; - } - - clear(); - - _contains.push(new Container("")); - if(!(yyin = fopen(sTemp.c_str(), "r"))) - { - error("open file '" + sFileName + "(" + sTemp + ")" + "' error :" + string(strerror(errno))); - } - - pushFile(sFileName); - - yyparse(); - - return ; - } - - { - clear(); - - _contains.push(new Container("")); - if(!(yyin = fopen(sFileName.c_str(), "r"))) - { - error("open file '" + sFileName + "' error :" + string(strerror(errno))); - } - - pushFile(sFileName); - - yyparse(); - } -} - -void TarsParse::pushFile(const string &file) -{ - ContextPtr c = new Context(file); - _contexts.push(c); - _vcontexts.push_back(c); -} - -ContextPtr TarsParse::popFile() -{ - ContextPtr c = _contexts.top(); - _contexts.pop(); - return c; -} - -bool TarsParse::getFilePath(const string &s, string &file) -{ - if (_bUseCurrentPath) - { - if(s.length() < 2 || s[0] != '\"' || s[s.length()-1] != '\"') - { - error("#include need \"FILENAME\""); - } - - file = s.substr(1, s.length() - 2); - - std::string sTemp = file; - bool isExist = tars::TC_File::isFileExist(file); - - if (!isExist) - { - file = tars::TC_File::extractFileName(file); - } - - for(size_t i = 0; i < _vcontexts.size(); i++) - { - if(_vcontexts[i]->getFileName() == file) - { - return false; - } - } - - return true; - } - - if(_bUseCurrentPathFirst) - { - if(s.length() < 2 || s[0] != '\"' || s[s.length()-1] != '\"') - { - error("#include need \"FILENAME\""); - } - - file = s.substr(1, s.length() - 2); - - std::string sTemp = tars::TC_File::extractFileName(file); - bool isExist = tars::TC_File::isFileExist(sTemp); - - if (isExist) - { - file = sTemp; - } - - for(size_t i = 0; i < _vcontexts.size(); i++) - { - if(_vcontexts[i]->getFileName() == file) - { - return false; - } - } - - return true; - } - - if(s.length() < 2 || s[0] != '\"' || s[s.length()-1] != '\"') - { - error("#include need \"FILENAME\""); - } - - file = s.substr(1, s.length() - 2); - - if (!tars::TC_File::isFileExist(file)) - { - for (size_t i = 0; i < _vIncludePath.size(); i++) - { - if (tars::TC_File::isFileExist(_vIncludePath[i] + "/" + file)) - { - file = _vIncludePath[i] + "/" + file; - break; - } - } - } - - for(size_t i = 0; i < _vcontexts.size(); i++) - { - if(_vcontexts[i]->getFileName() == file) - { - return false; - } - } - - return true; -} - -string TarsParse::getCurrFileName() -{ - return _contexts.top()->getFileName(); -} - -void TarsParse::nextLine() -{ - _contexts.top()->nextLine(); -} - -ContextPtr TarsParse::currentContextPtr() -{ - return _contexts.top(); -} - -void TarsParse::error(const string &msg) -{ - cerr << _contexts.top()->getFileName() << ": " << _contexts.top()->getCurrLine() << ": error: " << msg << endl; - exit(-1); -} - -int TarsParse::checkKeyword(const string& s) -{ - std::map::const_iterator it = _keywordMap.find(s); - if(it != _keywordMap.end()) - { - return it->second; - } - - if(!_bWithTars) - { - string sPrefix = "tars"; - //ä¸èƒ½ä»¥tars开头 - if((s.length() >= sPrefix.length()) && (s.substr(0, sPrefix.length()) == sPrefix)) - { - error("identifier can't start with 'tars'"); - } - } - - return TARS_IDENTIFIER; -} - -void TarsParse::initScanner() -{ - _keywordMap["void"] = TARS_VOID; - _keywordMap["struct"] = TARS_STRUCT; - _keywordMap["bool"] = TARS_BOOL; - _keywordMap["byte"] = TARS_BYTE; - _keywordMap["short"] = TARS_SHORT; - _keywordMap["int"] = TARS_INT; - _keywordMap["double"] = TARS_DOUBLE; - _keywordMap["float"] = TARS_FLOAT; - _keywordMap["long"] = TARS_LONG; - _keywordMap["string"] = TARS_STRING; - _keywordMap["vector"] = TARS_VECTOR; - _keywordMap["map"] = TARS_MAP; - _keywordMap["key"] = TARS_KEY; - _keywordMap["routekey"] = TARS_ROUTE_KEY; - _keywordMap["module"] = TARS_NAMESPACE; - _keywordMap["interface"]= TARS_INTERFACE; - _keywordMap["out"] = TARS_OUT; - _keywordMap["require"] = TARS_REQUIRE; - _keywordMap["optional"] = TARS_OPTIONAL; - _keywordMap["false"] = TARS_FALSE; - _keywordMap["true"] = TARS_TRUE; - _keywordMap["enum"] = TARS_ENUM; - _keywordMap["const"] = TARS_CONST; - _keywordMap["unsigned"] = TARS_UNSIGNED; -} - -string TarsParse::getTab() -{ - ostringstream s; - for(int i = 0; i < _itab; i++) - { - s << " "; - } - - return s.str(); -} - -BuiltinPtr TarsParse::createBuiltin(Builtin::Kind kind,bool isUnsigned) -{ - return new Builtin(kind,isUnsigned); -} - -VectorPtr TarsParse::createVector(const TypePtr &ptr) -{ - return new Vector(ptr); -} - -MapPtr TarsParse::createMap(const TypePtr &pleft, const TypePtr &pright) -{ - return new Map(pleft, pright); -} - -void TarsParse::addNamespacePtr(const NamespacePtr &nPtr) -{ - _namespaces.push_back(nPtr); -} - -NamespacePtr TarsParse::findNamespace(const string &id) -{ - for(size_t i = 0; i < _namespaces.size(); i++) - { - if(_namespaces[i]->getId() == id) - { - return _namespaces[i]; - } - } - - return NULL; -} - -NamespacePtr TarsParse::currentNamespace() -{ - return _namespaces.back(); -} - -void TarsParse::addStructPtr(const StructPtr &sPtr) -{ - _structs.push_back(sPtr); -} - -void TarsParse::addEnumPtr(const EnumPtr &ePtr) -{ - _enums.push_back(ePtr); -} - -StructPtr TarsParse::findStruct(const string &sid) -{ - string ssid = sid; - - //在当å‰namespace中查找 - NamespacePtr np = currentNamespace(); - if(ssid.find("::") == string::npos) - { - ssid = np->getId() + "::" + ssid; - } - - for(size_t i = 0; i < _structs.size(); i++) - { - if(_structs[i]->getSid() == ssid) - { - return _structs[i]; - } - } - - return NULL; -} - -EnumPtr TarsParse::findEnum(const string &sid) -{ - string ssid = sid; - - //在当å‰namespace中查找 - NamespacePtr np = currentNamespace(); - if(ssid.find("::") == string::npos) - { - ssid = np->getId() + "::" + sid; - } - - for(size_t i = 0; i < _enums.size(); i++) - { - if(_enums[i]->getSid() == ssid) - { - return _enums[i]; - } - } - - return NULL; -} - -bool TarsParse::checkEnum(const string &idName) -{ - for(size_t i = 0; i < _enums.size(); i++) - { - vector & list = _enums[i]->getAllMemberPtr(); - - for (size_t j = 0; j < list.size(); j++) - { - if (list[j]->getId() == idName) - { - return true; - } - } - } - - return false; -} -void TarsParse::checkConflict(const string &sid) -{ - //是å¦å’Œæžšä¸¾é‡å - if(findEnum(sid)) - { - error("conflicts with enum '" + sid + "'"); - } - - //是å¦å’Œç»“æž„é‡å - if(findStruct(sid)) - { - error("conflicts with struct '" + sid + "'"); - } -} - -TypePtr TarsParse::findUserType(const string &sid) -{ - StructPtr sPtr = findStruct(sid); - if(sPtr) return sPtr; - - EnumPtr ePtr = findEnum(sid); - if(ePtr) return ePtr; - - return NULL; -} - -ContainerPtr TarsParse::currentContainer() -{ - return _contains.top(); -} - -void TarsParse::pushContainer(const ContainerPtr &c) -{ - _contains.push(c); - NamespacePtr np = NamespacePtr::dynamicCast(c); - if(np) - { - addNamespacePtr(np); - } -} - -ContainerPtr TarsParse::popContainer() -{ - ContainerPtr c = _contains.top(); - _contains.pop(); - - return c; -} - -void TarsParse::checkTag(int i) -{ - if(i >= 256) - { - error("struct memeber tag can't beyond 256"); - } - if(i < 0) - { - error("struct memeber tag can't less then 0"); - } -} - -void TarsParse::checkSize(int i) -{ - if(i >= 1024*1024) - { - error("struct memeber size can't beyond 1M"); - } - if(i < 1) - { - error("struct memeber size can't less than 1"); - } -} - -void TarsParse::checkArrayVaid(TypePtr &tPtr,int size) -{ - checkSize(size); - //åªæœ‰string/vectorå¯ä»¥ä¸ºarray类型 - //vectorä¸å¯ä»¥åµŒå¥—array类型 例如ä¸å…许vector:2; - - VectorPtr vPtr = VectorPtr::dynamicCast(tPtr); - if(vPtr ) - { - if(vPtr->getTypePtr()->isArray()) - { - error("fixed array type can not be nested"); - } - return; - } - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(tPtr); - if(bPtr && bPtr->kind()== Builtin::KindString) - { - return; - } - error("only string or vector can use fix array"); -} - -void TarsParse::checkPointerVaid(TypePtr &tPtr) -{ - //必须为vector类型 - - VectorPtr vPtr = VectorPtr::dynamicCast(tPtr); - if(vPtr) - { - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vPtr->getTypePtr()); - if( bPtr && bPtr->kind()== Builtin::KindByte) - return; - } - error("only 'byte *' can be pointer type"); -} - -void TarsParse::checkConstValue(TypeIdPtr &tPtr, int c) -{ - //åªæœ‰å†…建类型æ‰èƒ½æœ‰ç¼ºçœå€¼ - BuiltinPtr bPtr = BuiltinPtr::dynamicCast(tPtr->getTypePtr()); - EnumPtr ePtr = EnumPtr::dynamicCast(tPtr->getTypePtr()); - if(!bPtr && !ePtr) - { - error("only base/enum type can have default value"); - } - - if (ePtr) - { - if (c != ConstGrammar::VALUE && c != ConstGrammar::ENUM) - { - error("default value of enum only be int or enum_type"); - } - - return ; - } - - int b = bPtr->kind(); - - if(c == ConstGrammar::VALUE) - { - if(b == Builtin::KindBool) - { - error("default value of bool can only be true or false"); - } - if(b == Builtin::KindString) - { - error("default value of string can only be \"string\""); - } - } - else if(c == ConstGrammar::BOOL) - { - if(b != Builtin::KindBool) - { - error("only bool type can be true or false"); - } - } - else if(c == ConstGrammar::STRING) - { - if(b != Builtin::KindString) - { - error("only string type can be \"string\""); - } - } -} - -string TarsParse::printHeaderRemark() -{ - ostringstream s; - s << "// **********************************************************************" << endl; - s << "// This file was generated by a TARS parser!" << endl; - s << "// TARS version " << TARS_VERSION << "." << endl; - s << "// **********************************************************************" << endl; - s << endl; - - return s.str(); -} - - diff --git a/cpp/tools/tarsparse/parse.h b/cpp/tools/tarsparse/parse.h deleted file mode 100644 index f47863c4e..000000000 --- a/cpp/tools/tarsparse/parse.h +++ /dev/null @@ -1,368 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_PARSE_H_ -#define __TARS_PARSE_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "element.h" -#include "util/tc_common.h" -#include "util/tc_autoptr.h" - -using namespace std; - -/** - * Tars文件解æžç±» - * - */ -class TarsParse : public tars::TC_HandleBase -{ -public: - /** - * 构造函数 - */ - TarsParse(); - - /** - * 是å¦æ”¯æŒtars开头的标识 - * @param bWithTars - */ - void setTars(bool bWithTars); - - /** - * 头文件路径 - * @param sHeader - */ - void setHeader(const string &sHeader); - - /** - * 是å¦æ”¯æŒä¼˜å…ˆä½¿ç”¨å½“å‰tars文件 - * @param bWithTars - */ - void setCurrentPriority(bool bFlag); - - /** - * 获å–头文件路径 - */ - string getHeader(); - - /** - * 解æžæŸä¸€ä¸ªæ–‡ä»¶ - * @param sFileName - */ - void parse(const string &sFileName); - - /** - * 错误æ示 - * @param msg - */ - void error(const string &msg); - - /** - * 检查关键字 - * @param s - * - * @return int - */ - int checkKeyword(const string &s); - - /** - * 下一行 - */ - void nextLine(); - - /** - * ç›®å‰è§£æžçš„文件å称 - * - * @return string - */ - string getCurrFileName(); - - /** - * tab - * - * @return string - */ - string getTab(); - - /** - * 增加tabæ•° - */ - void incTab() { _itab++; } - - /** - * å‡å°‘tabæ•° - */ - void delTab() { _itab--; } - - /** - * 解æžæ–‡ä»¶ - * @param file - */ - void pushFile(const string &file); - - /** - * 弹出解æžæ–‡ä»¶ - */ - ContextPtr popFile(); - - /** - * 获å–所有的上下文 - * - * @return std::vector - */ - std::vector getContexts() { return _vcontexts; } - - /** - * 获å–ç›®å‰çš„容器 - * - * @return ContainerPtr - */ - ContainerPtr currentContainer(); - - /** - * push容器 - * @param c - */ - void pushContainer(const ContainerPtr &c); - - /** - * ç›®å‰çš„上下文 - * - * @return ContextPtr - */ - ContextPtr currentContextPtr(); - - /** - * 弹出容器 - * - * @return ContainerPtr - */ - ContainerPtr popContainer(); - - /** - * 生æˆBuiltin元素 - * @param kind - * - * @return BuiltinPtr - */ - BuiltinPtr createBuiltin(Builtin::Kind kind,bool isUnsigned = false); - - /** - * 生æˆVector元素 - * @param ptr - * - * @return VectorPtr - */ - VectorPtr createVector(const TypePtr &ptr); - - /** - * 生æˆMap元素 - * @param pleft - * @param pright - * - * @return MapPtr - */ - MapPtr createMap(const TypePtr &pleft, const TypePtr &pright); - - /** - * 添加结构元素 - * @param sPtr - */ - void addStructPtr(const StructPtr &sPtr); - - /** - * 查找结构 - * @param id - * - * @return StructPtr - */ - StructPtr findStruct(const string &sid); - - /** - * 添加枚举元素 - * @param ePtr - */ - void addEnumPtr(const EnumPtr &ePtr); - - /** - * 查找结构 - * @param id - * - * @return EnumPtr - */ - EnumPtr findEnum(const string &sid); - - /** - * æ£€æŸ¥å†²çª - * @param id - */ - void checkConflict(const string &sid); - - /** - * 查找自定义类型 - * @param sid - * - * @return TypePtr - */ - TypePtr findUserType(const string &sid); - - /** - * 查找å字空间 - * @param id - * - * @return NamespacePtr - */ - NamespacePtr findNamespace(const string &id); - - /** - * ç›®å‰çš„å字空间 - * - * @return NamespacePtr - */ - NamespacePtr currentNamespace(); - - /** - * 检查tagçš„åˆæ³•æ€§ - * @param i - */ - void checkTag(int i); - - - /** - * 检查szieçš„åˆæ³•æ€§ - * @param i - */ - void checkSize(int i); - - /** - * 检查arrayçš„åˆæ³•æ€§ - * @param i - */ - void checkArrayVaid(TypePtr &tPtr,int size); - - /** - * 检查pointerçš„åˆæ³•æ€§ - * @param i - */ - void checkPointerVaid(TypePtr &tPtr); - - /** - * 检查常é‡ç±»åž‹å’Œå€¼æ˜¯å¦ä¸€è‡´ - * @param c - * @param b - */ - void checkConstValue(TypeIdPtr &tPtr, int b); - - /** - * 获å–文件å - * @param s - * - * @return string - */ - bool getFilePath(const string &s, string &file); - - void setKeyStruct(const StructPtr& key) - { - _key = key; - } - - StructPtr getKeyStruct() - { - return _key; - } - - /** - * 打å°æ–‡ä»¶å¼€å¤´æ³¨é‡Š - */ - string printHeaderRemark(); - - - /** - * 判断当å‰çš„标识符是å¦æ˜¯æžšä¸¾å˜é‡ - */ - bool checkEnum(const string &idName); - - /** - - * 设置includeçš„tars文件全部从当å‰æ–‡ä»¶æœå¯» - */ - void setUseCurrentPath(const bool & bEnable) { _bUseCurrentPath = bEnable; } - - /** - * 查找tars文件时,设置include路径 - */ - void addIncludePath(const string &include) - { - vector v = tars::TC_Common::sepstr(include, "; ,", false); - - _vIncludePath.insert(_vIncludePath.end(), v.begin(), v.end()); - } - -protected: - /** - * 添加å字空间 - * @param nPtr - */ - void addNamespacePtr(const NamespacePtr &nPtr); - - /** - * åˆå§‹åŒ– - */ - void initScanner(); - - /** - * 清除 - */ - void clear(); - -protected: - bool _bWithTars; - std::map _keywordMap; - int _itab; - StructPtr _key; - std::stack _contexts; - std::stack _contains; - std::vector _vcontexts; - std::vector _structs; - std::vector _enums; - std::vector _namespaces; - string _sHeader; - bool _bUseCurrentPath; - bool _bUseCurrentPathFirst; - std::vector _vIncludePath; -}; - -extern int yyparse(); -extern int yylex(); -extern FILE *yyin, *yyout; - -typedef tars::TC_AutoPtr TarsParsePtr; - -extern TarsParsePtr g_parse; - -#endif - - diff --git a/cpp/util/CMakeLists.txt b/cpp/util/CMakeLists.txt deleted file mode 100644 index ecad534d4..000000000 --- a/cpp/util/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -cmake_minimum_required(VERSION 2.8) -project(util) - -include_directories(${MYSQL_DIR_INC}) -link_directories(${MYSQL_DIR_LIB}) - -enable_language(ASM) - -add_subdirectory(src) -add_subdirectory(include) - - diff --git a/cpp/util/README.md b/cpp/util/README.md deleted file mode 100644 index 8801059b8..000000000 --- a/cpp/util/README.md +++ /dev/null @@ -1 +0,0 @@ -该工程是Tars基础工具库。 \ No newline at end of file diff --git a/cpp/util/include/CMakeLists.txt b/cpp/util/include/CMakeLists.txt deleted file mode 100644 index 4128e88d5..000000000 --- a/cpp/util/include/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ - -install(DIRECTORY util DESTINATION include) - diff --git a/cpp/util/include/util/detail/tc_assert.h b/cpp/util/include/util/detail/tc_assert.h deleted file mode 100644 index 2359c5858..000000000 --- a/cpp/util/include/util/detail/tc_assert.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_ASSERT_H__ -#define __TC_ASSERT_H__ - -#include -#include - -namespace tars -{ - namespace detail - { - inline void tc_assertion_failed(const char *expr, const char *function, const char *file, int line) - { - std::cerr << "at " << file << ':' << line << ": in " << function - << "(): Assertion `" << expr << "' failed" << std::endl; - std::abort(); - } - - template struct TC_CompileTimeError; - template<> struct TC_CompileTimeError {}; - - } - -} - -#define TC_ASSERT(expr) \ - ((expr) ? ((void)0) : ::tars::detail::tc_assertion_failed(#expr, __func__, __FILE__, __LINE__)) - -#define TC_STATIC_ASSERT(exp, msg) \ - { tars::detail::TC_CompileTimeError<((exp) != 0)> ERROR_##msg; (void) ERROR_##msg; } - -#endif diff --git a/cpp/util/include/util/detail/tc_checked_delete.h b/cpp/util/include/util/detail/tc_checked_delete.h deleted file mode 100644 index df2bed64c..000000000 --- a/cpp/util/include/util/detail/tc_checked_delete.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_CHECKED_DELETE_H__ -#define __TC_CHECKED_DELETE_H__ - -namespace tars -{ - template - inline void tc_checked_delete(T *p) - { - typedef char type_must_be_complete[sizeof(T) ? 1 : -1]; - (void) sizeof(type_must_be_complete); - delete p; - } - - template - inline void tc_checked_array_delete(T *p) - { - typedef char type_must_be_complete[sizeof(T) ? 1 : -1]; - (void) sizeof(type_must_be_complete); - delete[] p; - } - -} - -#endif diff --git a/cpp/util/include/util/detail/tc_shared_count_base.h b/cpp/util/include/util/detail/tc_shared_count_base.h deleted file mode 100644 index 4b36d8a04..000000000 --- a/cpp/util/include/util/detail/tc_shared_count_base.h +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_SHARED_COUNT_BASE_H__ -#define __TC_SHARED_COUNT_BASE_H__ - -#include "../tc_atomic.h" - -namespace tars -{ - namespace detail - { - class tc_shared_count_base - { - public: - - tc_shared_count_base() - : m_use_count(1) - {} - - virtual ~tc_shared_count_base() - {} - - int use_count() const - { - return m_use_count.get(); - } - - void increment() - { - ++m_use_count; - } - - void release() - { - if (m_use_count.dec_and_test()) - { - dispose(); - delete this; - } - } - - virtual void dispose() = 0; - - private: - tc_shared_count_base(const tc_shared_count_base&); - void operator=(const tc_shared_count_base&); - - tars::TC_Atomic m_use_count; - }; - - } -} - -#endif diff --git a/cpp/util/include/util/detail/tc_shared_count_impl.h b/cpp/util/include/util/detail/tc_shared_count_impl.h deleted file mode 100644 index 922abf6fb..000000000 --- a/cpp/util/include/util/detail/tc_shared_count_impl.h +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_SHARED_COUNT_IMPL_H__ -#define __TC_SHARED_COUNT_IMPL_H__ - -#include "tc_shared_count_base.h" -#include "tc_checked_delete.h" - -namespace tars -{ - namespace detail - { - template - class tc_shared_count_impl_p : public tc_shared_count_base - { - public: - - template - tc_shared_count_impl_p(U *p) - : m_px(p) - {} - - virtual void dispose() - { - tc_checked_delete(m_px); - } - - private: - - T *m_px; - }; - - template - class tc_shared_count_impl_pd : public tc_shared_count_base - { - public: - - tc_shared_count_impl_pd(T *p, D& d) - : m_px(p) - , m_deleter(d) - {} - - virtual void dispose() // no throw - { - m_deleter(m_px); - } - - private: - - T *m_px; - D m_deleter; - }; - - } -} - -#endif diff --git a/cpp/util/include/util/detail/tc_template_util.h b/cpp/util/include/util/detail/tc_template_util.h deleted file mode 100644 index 9d5609323..000000000 --- a/cpp/util/include/util/detail/tc_template_util.h +++ /dev/null @@ -1,164 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_TEMPLATE_UTIL_H__ -#define __TC_TEMPLATE_UTIL_H__ - -namespace tars -{ - template - struct tc_integral_constant - { - typedef T value_type; - typedef tc_integral_constant type; - static const T value = v; - }; - - template const T tc_integral_constant::value; - - typedef tc_integral_constant true_type; - typedef tc_integral_constant false_type; - - template struct tc_is_void : false_type {}; - template <> struct tc_is_void : true_type {}; - - template struct tc_is_non_const_reference : false_type {}; - template struct tc_is_non_const_reference : true_type {}; - template struct tc_is_non_const_reference : false_type {}; - - template struct tc_is_raw_pointer : false_type {}; - template struct tc_is_raw_pointer : true_type {}; - - template - struct tc_is_same_type : tc_integral_constant {}; - - template - struct tc_is_same_type : tc_integral_constant {}; - - template - struct tc_select_type_c; - - template - struct tc_select_type_c - { - typedef type1 type; - }; - - template - struct tc_select_type_c - { - typedef type2 type; - }; - - template - struct tc_select_type : tc_select_type_c {}; - - namespace detail - { - typedef char (&Yes)[1]; - typedef char (&No)[2]; - - struct TC_ConvertHelper - { - template - static Yes Test(To); - - template - static No Test(...); - - template - static From Make(); - }; - - struct TC_IsClassHelper - { - template - static Yes Test(void (C::*)(void)); - - template - static No Test(...); - }; - - } - - template - struct tc_is_convertible : tc_integral_constant(detail::TC_ConvertHelper::Make())) == sizeof(detail::Yes)> - { - }; - - template - struct tc_is_class : tc_integral_constant(0)) == sizeof(detail::Yes)> - { - }; - - template - struct tc_is_derived_from - { - enum { value = tc_is_class::value && tc_is_class::value && tc_is_convertible::value }; - }; - - template - class tc_has_method_foo - { - struct BasMixin - { - void foo(); - }; - - struct Base : public T, public BasMixin {}; - - template struct Helper {}; - - template - static detail::No Test(Helper<&C::foo>*); - - template - static detail::Yes Test(...); - - public: - - enum { value = sizeof(Test(0)) == sizeof(detail::Yes) }; - }; - - template - struct tc_enable_if_c - { - typedef T type; - }; - - template - struct tc_enable_if_c {}; - - template - struct tc_enable_if : public tc_enable_if_c {}; - - template - struct tc_disable_if_c - { - typedef T type; - }; - - template - struct tc_disable_if_c {}; - - template - struct tc_disable_if : public tc_disable_if_c {}; - -} - -#endif diff --git a/cpp/util/include/util/tc_atomic.h b/cpp/util/include/util/tc_atomic.h deleted file mode 100644 index 97339808b..000000000 --- a/cpp/util/include/util/tc_atomic.h +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_ATOMIC_H -#define __TC_ATOMIC_H - -#include - -namespace tars -{ - -///////////////////////////////////////////////// -/** - * @file tc_atomic.h - * @brief 原å­è®¡æ•°ç±». - */ - -__BEGIN_DECLS - -#define TARS_LOCK "lock ; " - -typedef struct { volatile int counter; } tars_atomic_t; - -#define tars_atomic_read(v) ((v)->counter) - -#define tars_atomic_set(v,i) (((v)->counter) = (i)) - -__END_DECLS - -/** - * @brief 原å­æ“作类,对intåšåŽŸå­æ“作 - */ -class TC_Atomic -{ -public: - - /** - * 原å­ç±»åž‹ - */ - typedef int atomic_type; - - /** - * @brief 构造函数,åˆå§‹åŒ–为0 - */ - TC_Atomic(atomic_type at = 0) - { - set(at); - } - - TC_Atomic& operator++() - { - inc(); - return *this; - } - - TC_Atomic& operator--() - { - dec(); - return *this; - } - - operator atomic_type() const - { - return get(); - } - - TC_Atomic& operator+=(atomic_type n) - { - add(n); - return *this; - } - - TC_Atomic& operator-=(atomic_type n) - { - sub(n); - return *this; - } - - TC_Atomic& operator=(atomic_type n) - { - set(n); - return *this; - } - - /** - * @brief 获å–值 - * - * @return int - */ - atomic_type get() const { return _value.counter; } - - /** - * @brief 添加 - * @param i - * - * @return int - */ - atomic_type add(atomic_type i) { return add_and_return(i); } - - /** - * @brief å‡å°‘ - * @param i - * - * @return int - */ - atomic_type sub(atomic_type i) { return add_and_return(-i); } - - /** - * @brief 自加1 - * - * @return int - */ - atomic_type inc() { return add(1); } - - /** - * @brief 自å‡1 - */ - atomic_type dec() { return sub(1); } - - /** - * @brief 自加1 - * - * @return void - */ - void inc_fast() - { - __asm__ __volatile__( - TARS_LOCK "incl %0" - :"=m" (_value.counter) - :"m" (_value.counter)); - } - - /** - * @brief 自å‡1 - * Atomically decrements @_value by 1 and returns true if the - * result is 0, or false for all other - */ - bool dec_and_test() - { - unsigned char c; - - __asm__ __volatile__( - TARS_LOCK "decl %0; sete %1" - :"=m" (_value.counter), "=qm" (c) - :"m" (_value.counter) : "memory"); - - return c != 0; - } - - /** - * @brief 设置值 - */ - atomic_type set(atomic_type i) - { - _value.counter = i; - - return i; - } - -protected: - - /** - * @brief 增加并返回值 - */ - int add_and_return(int i) - { - /* Modern 486+ processor */ - int __i = i; - __asm__ __volatile__( - TARS_LOCK "xaddl %0, %1;" - :"=r"(i) - :"m"(_value.counter), "0"(i)); - return i + __i; - } - -protected: - - /** - * 值 - */ - tars_atomic_t _value; -}; - -} - -#endif diff --git a/cpp/util/include/util/tc_autoptr.h b/cpp/util/include/util/tc_autoptr.h deleted file mode 100644 index f10dfd671..000000000 --- a/cpp/util/include/util/tc_autoptr.h +++ /dev/null @@ -1,508 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_AUTOPTR_H -#define __TC_AUTOPTR_H - -#include "util/tc_atomic.h" -#include "util/tc_ex.h" - -namespace tars -{ -/////////////////////////////////////////////////////// -/** -* @file tc_autoptr.h -* @brief 智能指针类(智能指针ä¸èƒ½ç›¸äº’引用, å¦åˆ™å†…存泄æ¼). -*/ -////////////////////////////////////////////////////// - -/** -* @brief 空指针异常 -*/ -struct TC_AutoPtrNull_Exception : public TC_Exception -{ - TC_AutoPtrNull_Exception(const string &buffer) : TC_Exception(buffer){}; - ~TC_AutoPtrNull_Exception() throw(){}; -}; - -/** - * @brief 智能指针基类. - * - * 所有需è¦æ™ºèƒ½æŒ‡é’ˆæ”¯æŒçš„类都需è¦ä»Žè¯¥å¯¹è±¡ç»§æ‰¿ï¼Œ - * - * 内部采用引用计数TC_Atomic实现,对象å¯ä»¥æ”¾åœ¨å®¹å™¨ä¸­ï¼› - */ -template -class TC_HandleBaseT -{ -public: - - /** 原å­è®¡æ•°ç±»åž‹*/ - typedef T atomic_type; - - /** - * @brief å¤åˆ¶. - * - * @return TC_HandleBase& - */ - TC_HandleBaseT& operator=(const TC_HandleBaseT&) - { - return *this; - } - - /** - * @brief 增加计数 - */ - void incRef() { _atomic.inc_fast(); } - - /** - * @brief å‡å°‘计数, 当计数==0æ—¶, 且需è¦åˆ é™¤æ•°æ®æ—¶, 释放对象 - */ - void decRef() - { - if(_atomic.dec_and_test() && !_bNoDelete) - { - _bNoDelete = true; - delete this; - } - } - - /** - * @brief 获å–计数. - * - * @return int 计数值 - */ - int getRef() const { return _atomic.get(); } - - /** - * @brief 设置ä¸è‡ªåŠ¨é‡Šæ”¾. - * - * @param b 是å¦è‡ªåŠ¨åˆ é™¤,true or false - */ - void setNoDelete(bool b) { _bNoDelete = b; } - -protected: - - /** - * @brief 构造函数 - */ - TC_HandleBaseT() : _atomic(0), _bNoDelete(false) - { - } - - /** - * @brief æ‹·è´æž„造 - */ - TC_HandleBaseT(const TC_HandleBaseT&) : _atomic(0), _bNoDelete(false) - { - } - - /** - * @brief æžå¤Ÿ - */ - virtual ~TC_HandleBaseT() - { - } - -protected: - - /** - * 计数 - */ - atomic_type _atomic; - - /** - * 是å¦è‡ªåŠ¨åˆ é™¤ - */ - bool _bNoDelete; -}; - -template<> -inline void TC_HandleBaseT::incRef() -{ - //__sync_fetch_and_add(&_atomic,1); - ++_atomic; -} - -template<> -inline void TC_HandleBaseT::decRef() -{ - //int c = __sync_fetch_and_sub(&_atomic, 1); - //if(c == 1 && !_bNoDelete) - if(--_atomic == 0 && !_bNoDelete) - { - _bNoDelete = true; - delete this; - } -} - -template<> -inline int TC_HandleBaseT::getRef() const -{ - //return __sync_fetch_and_sub(const_cast(&_atomic), 0); - return _atomic; -} - -typedef TC_HandleBaseT TC_HandleBase; - -/** - * @brief 智能指针模æ¿ç±». - * - * å¯ä»¥æ”¾åœ¨å®¹å™¨ä¸­,且线程安全的智能指针. - * - * 通过它定义智能指针,该智能指针通过引用计数实现, - * - * å¯ä»¥æ”¾åœ¨å®¹å™¨ä¸­ä¼ é€’. - * - * template T必须继承于TC_HandleBase - */ -template -class TC_AutoPtr -{ -public: - - /** - * 元素类型 - */ - typedef T element_type; - - /** - * @brief 用原生指针åˆå§‹åŒ–, 计数+1. - * - * @param p - */ - TC_AutoPtr(T* p = 0) - { - _ptr = p; - - if(_ptr) - { - _ptr->incRef(); - } - } - - /** - * @brief 用其他智能指针r的原生指针åˆå§‹åŒ–, 计数+1. - * - * @param Y - * @param r - */ - template - TC_AutoPtr(const TC_AutoPtr& r) - { - _ptr = r._ptr; - - if(_ptr) - { - _ptr->incRef(); - } - } - - /** - * @brief æ‹·è´æž„造, 计数+1. - * - * @param r - */ - TC_AutoPtr(const TC_AutoPtr& r) - { - _ptr = r._ptr; - - if(_ptr) - { - _ptr->incRef(); - } - } - - /** - * @brief æžæž„ - */ - ~TC_AutoPtr() - { - if(_ptr) - { - _ptr->decRef(); - } - } - - /** - * @brief 赋值, 普通指针. - * - * @param p - * @return TC_AutoPtr& - */ - TC_AutoPtr& operator=(T* p) - { - if(_ptr != p) - { - if(p) - { - p->incRef(); - } - - T* ptr = _ptr; - _ptr = p; - - if(ptr) - { - ptr->decRef(); - } - } - return *this; - } - - /** - * @brief 赋值, 其他类型智能指针. - * - * @param Y - * @param r - * @return TC_AutoPtr& - */ - template - TC_AutoPtr& operator=(const TC_AutoPtr& r) - { - if(_ptr != r._ptr) - { - if(r._ptr) - { - r._ptr->incRef(); - } - - T* ptr = _ptr; - _ptr = r._ptr; - - if(ptr) - { - ptr->decRef(); - } - } - return *this; - } - - /** - * @brief 赋值, 该类型其他执政指针. - * - * @param r - * @return TC_AutoPtr& - */ - TC_AutoPtr& operator=(const TC_AutoPtr& r) - { - if(_ptr != r._ptr) - { - if(r._ptr) - { - r._ptr->incRef(); - } - - T* ptr = _ptr; - _ptr = r._ptr; - - if(ptr) - { - ptr->decRef(); - } - } - return *this; - } - - /** - * @brief 将其他类型的智能指针æ¢æˆå½“å‰ç±»åž‹çš„智能指针. - * - * @param Y - * @param r - * @return TC_AutoPtr - */ - template - static TC_AutoPtr dynamicCast(const TC_AutoPtr& r) - { - return TC_AutoPtr(dynamic_cast(r._ptr)); - } - - /** - * @brief 将其他原生类型的指针转æ¢æˆå½“å‰ç±»åž‹çš„智能指针. - * - * @param Y - * @param p - * @return TC_AutoPtr - */ - template - static TC_AutoPtr dynamicCast(Y* p) - { - return TC_AutoPtr(dynamic_cast(p)); - } - - /** - * @brief 获å–原生指针. - * - * @return T* - */ - T* get() const - { - return _ptr; - } - - /** - * @brief 调用. - * - * @return T* - */ - T* operator->() const - { - if(!_ptr) - { - throwNullHandleException(); - } - - return _ptr; - } - - /** - * @brief 引用. - * - * @return T& - */ - T& operator*() const - { - if(!_ptr) - { - throwNullHandleException(); - } - - return *_ptr; - } - - /** - * @brief 是å¦æœ‰æ•ˆ. - * - * @return bool - */ - operator bool() const - { - return _ptr ? true : false; - } - - /** - * @brief 交æ¢æŒ‡é’ˆ. - * - * @param other - */ - void swap(TC_AutoPtr& other) - { - std::swap(_ptr, other._ptr); - } - -protected: - - /** - * @brief 抛出异常 - */ - void throwNullHandleException() const; - -public: - T* _ptr; - -}; - -/** - * @brief 抛出异常. - * - * @param T - * @param file - * @param line - */ -template inline void -TC_AutoPtr::throwNullHandleException() const -{ - throw TC_AutoPtrNull_Exception("autoptr null handle error"); -} - -/** - * @brief ==判断. - * - * @param T - * @param U - * @param lhs - * @param rhs - * - * @return bool - */ -template -inline bool operator==(const TC_AutoPtr& lhs, const TC_AutoPtr& rhs) -{ - T* l = lhs.get(); - U* r = rhs.get(); - if(l && r) - { - return *l == *r; - } - else - { - return !l && !r; - } -} - -/** - * @brief ä¸ç­‰äºŽåˆ¤æ–­. - * - * @param T - * @param U - * @param lhs - * @param rhs - * - * @return bool - */ -template -inline bool operator!=(const TC_AutoPtr& lhs, const TC_AutoPtr& rhs) -{ - T* l = lhs.get(); - U* r = rhs.get(); - if(l && r) - { - return *l != *r; - } - else - { - return l || r; - } -} - -/** - * @brief å°äºŽåˆ¤æ–­, 用于放在map等容器中. - * - * @param T - * @param U - * @param lhs - * @param rhs - * - * @return bool - */ -template -inline bool operator<(const TC_AutoPtr& lhs, const TC_AutoPtr& rhs) -{ - T* l = lhs.get(); - U* r = rhs.get(); - if(l && r) - { - return *l < *r; - } - else - { - return !l && r; - } -} - -} - -#endif diff --git a/cpp/util/include/util/tc_base64.h b/cpp/util/include/util/tc_base64.h deleted file mode 100644 index 18f2887cc..000000000 --- a/cpp/util/include/util/tc_base64.h +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_BASE64_H -#define __TC_BASE64_H - -#include - -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_base64.h -* @brief base64编解ç ç±». -*/ - -///////////////////////////////////////////////// - -/** -* @brief 该类æ供标准的Base64çš„ç¼–ç è§£ç  -*/ -class TC_Base64 -{ -public: - /** - * @brief 对字符串进行base64ç¼–ç . - * - * @param data 需è¦ç¼–ç çš„æ•°æ® - * @param bChangeLine 是å¦éœ€è¦åœ¨æœ€ç»ˆç¼–ç æ•°æ®åŠ å…¥æ¢è¡Œç¬¦ , - * (RFC中建议æ¯76个字符åŽåŠ å…¥å›žè½¦æ¢è¡Œï¼Œé»˜è®¤ä¸ºä¸æ·»åŠ æ¢è¡Œ - * @return string ç¼–ç åŽçš„æ•°æ® - */ - static string encode(const string &data, bool bChangeLine = false); - - /** - * @brief 对字符串进行base64解ç . - * - * @param data 需è¦è§£ç çš„æ•°æ® - * @return string 解ç åŽçš„æ•°æ® - */ - static string decode(const string &data); - - /** - * @brief 对字符串进行base64ç¼–ç  . - * - * @param pSrc 需è¦ç¼–ç çš„æ•°æ® - * @param nSrcLen 需è¦ç¼–ç çš„æ•°æ®é•¿åº¦ - * @param pDst ç¼–ç åŽçš„æ•°æ® - * @param bChangeLine 是å¦éœ€è¦åœ¨æœ€ç»ˆç¼–ç æ•°æ®åŠ å…¥æ¢è¡Œç¬¦ï¼Œ - * RFC中建议æ¯76个字符åŽåŠ å…¥å›žè½¦æ¢è¡Œï¼Œé»˜è®¤ä¸ºä¸æ·»åŠ æ¢è¡Œ - * @return ç¼–ç åŽçš„字符串的长度 - */ - static int encode(const unsigned char* pSrc, int nSrcLen, char* pDst, bool bChangeLine = false); - - /** - * @brief 对字符串进行base64解ç . - * - * @param pSrc 需è¦è§£ç çš„æ•°æ® - * @param nSrcLe 需è¦è§£ç çš„æ•°æ®é•¿åº¦ - * @param pDst 解ç åŽçš„æ•°æ® - * @return 解ç åŽçš„字符串的长度 - */ - static int decode(const char* pSrc, int nSrcLen, unsigned char* pDst); - -protected: - - /** - * base64ç¼–ç è¡¨ - */ - static const char EnBase64Tab[]; - /** - * base64解ç è¡¨ - */ - static const char DeBase64Tab[]; -}; - -} -#endif diff --git a/cpp/util/include/util/tc_bind.h b/cpp/util/include/util/tc_bind.h deleted file mode 100644 index 49f6276d0..000000000 --- a/cpp/util/include/util/tc_bind.h +++ /dev/null @@ -1,409 +0,0 @@ -#ifndef __TC_BIND_H__ -#define __TC_BIND_H__ - -#include "tc_callback.h" -#include "tc_bind_detail.h" -#include "detail/tc_assert.h" -#include "detail/tc_template_util.h" - -namespace tars -{ - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void ()>::UnboundRunType> - TC_Bind(Functor functor) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - typedef detail::TC_BindState TC_BindState; - - return TC_Callback(new TC_BindState(FunctorType(functor))); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value, do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType) - > TC_BindState; - - return TC_Callback(new TC_BindState(FunctorType(functor), a1)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback(new TC_BindState(FunctorType(functor), a1, a2)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback(new TC_BindState(FunctorType(functor), a1, a2, a3)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value - && !tc_is_non_const_reference::A4Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback(new TC_BindState(FunctorType(functor), a1, a2, a3, a4)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value - && !tc_is_non_const_reference::A4Type>::value - && !tc_is_non_const_reference::A5Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback(new TC_BindState(FunctorType(functor), a1, a2, a3, a4, a5)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, - const A5& a5, const A6& a6) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value - && !tc_is_non_const_reference::A4Type>::value - && !tc_is_non_const_reference::A5Type>::value - && !tc_is_non_const_reference::A6Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback( - new TC_BindState(FunctorType(functor), a1, a2, a3, a4, a5, a6)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, - const A5& a5, const A6& a6, const A7& a7) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value - && !tc_is_non_const_reference::A4Type>::value - && !tc_is_non_const_reference::A5Type>::value - && !tc_is_non_const_reference::A6Type>::value - && !tc_is_non_const_reference::A7Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback( - new TC_BindState(FunctorType(functor), a1, a2, a3, a4, a5, a6, a7)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, - const A5& a5, const A6& a6, const A7& a7, const A8& a8) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value - && !tc_is_non_const_reference::A4Type>::value - && !tc_is_non_const_reference::A5Type>::value - && !tc_is_non_const_reference::A6Type>::value - && !tc_is_non_const_reference::A7Type>::value - && !tc_is_non_const_reference::A8Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback( - new TC_BindState(FunctorType(functor), a1, a2, a3, a4, a5, a6, a7, a8)); - } - - template - TC_Callback::FunctorType, - typename detail::TC_FunctorTraits::RunType, - void (typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - >::UnboundRunType> - TC_Bind(Functor functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9) - { - typedef typename detail::TC_FunctorTraits::RunType RunType; - typedef typename detail::TC_FunctorTraits::FunctorType FunctorType; - - TC_STATIC_ASSERT(!tc_is_non_const_reference::A1Type>::value - && !tc_is_non_const_reference::A2Type>::value - && !tc_is_non_const_reference::A3Type>::value - && !tc_is_non_const_reference::A4Type>::value - && !tc_is_non_const_reference::A5Type>::value - && !tc_is_non_const_reference::A6Type>::value - && !tc_is_non_const_reference::A7Type>::value - && !tc_is_non_const_reference::A8Type>::value - && !tc_is_non_const_reference::A9Type>::value, - do_not_bind_functions_with_non_const_ref); - - TC_STATIC_ASSERT(!tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value - && !tc_is_raw_pointer::value, - do_not_pass_raw_pointers_as_bound_argument); - - typedef detail::TC_BindState::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType, - typename detail::TC_CallbackParamTraits::StorageType) - > TC_BindState; - - return TC_Callback( - new TC_BindState(FunctorType(functor), a1, a2, a3, a4, a5, a6, a7, a8, a9)); - } - - template - detail::TC_UnretainedWrapper tc_unretained(T *p) - { return detail::TC_UnretainedWrapper(p); } - - template - detail::TC_OwnedWrapper tc_owned(T *p) - { return detail::TC_OwnedWrapper(p); } - - template - detail::TC_SharedWrapper tc_shared(const TC_SharedPtr& p) - { return detail::TC_SharedWrapper(p); } - -} - -#endif diff --git a/cpp/util/include/util/tc_bind_detail.h b/cpp/util/include/util/tc_bind_detail.h deleted file mode 100644 index 51f3cc387..000000000 --- a/cpp/util/include/util/tc_bind_detail.h +++ /dev/null @@ -1,2357 +0,0 @@ -#ifndef __TC_BIND_DETAIL_H__ -#define __TC_BIND_DETAIL_H__ - -#include "tc_callback_detail.h" - -namespace tars -{ - namespace detail - { - template - class TC_FunctorAdapter; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(); - - explicit TC_FunctorAdapter(R (*f)()) - : m_pf(f) - { } - - R operator()() - { return (*m_pf)(); } - - private: - R (*m_pf)(); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*); - - explicit TC_FunctorAdapter(R (T::*pmf)()) - : m_pmf(pmf) - { } - - R operator()(T *pobj) - { return (pobj->*m_pmf)(); } - - private: - R (T::*m_pmf)(); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*); - - explicit TC_FunctorAdapter(R (T::*pmf)() const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj) - { return (pobj->*m_pmf)(); } - - private: - R (T::*m_pmf)() const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1); - - TC_FunctorAdapter(R (*pf)(A1)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1) - { return (*m_pf)(a1); } - - private: - R (*m_pf)(A1); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1); - - TC_FunctorAdapter(R (T::*pmf)(A1)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, typename TC_CallbackParamTraits::ForwardType a1) - { return (pobj->*m_pmf)(a1); } - - private: - R (T::*m_pmf)(A1); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1); - - TC_FunctorAdapter(R (T::*pmf)(A1) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, typename TC_CallbackParamTraits::ForwardType a1) - { return (pobj->*m_pmf)(a1); } - - private: - R (T::*m_pmf)(A1) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2); - - TC_FunctorAdapter(R (*pf)(A1, A2)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2) - { return (*m_pf)(a1, a2); } - - private: - R (*m_pf)(A1, A2); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2) - { return (pobj->*m_pmf)(a1, a2); } - - private: - R (T::*m_pmf)(A1, A2); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2) - { return (pobj->*m_pmf)(a1, a2); } - - private: - R (T::*m_pmf)(A1, A2) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3) - { return (*m_pf)(a1, a2, a3); } - - private: - R (*m_pf)(A1, A2, A3); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3) - { return (pobj->*m_pmf)(a1, a2, a3); } - - private: - R (T::*m_pmf)(A1, A2, A3); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3) - { return (pobj->*m_pmf)(a1, a2, a3); } - - private: - R (T::*m_pmf)(A1, A2, A3) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3, A4); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3, A4)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4) - { return (*m_pf)(a1, a2, a3, a4); } - - private: - R (*m_pf)(A1, A2, A3, A4); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3, A4); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4) - { return (pobj->*m_pmf)(a1, a2, a3, a4); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3, A4); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4) - { return (pobj->*m_pmf)(a1, a2, a3, a4); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3, A4, A5); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3, A4, A5)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { return (*m_pf)(a1, a2, a3, a4, a5); } - - private: - R (*m_pf)(A1, A2, A3, A4, A5); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3, A4, A5); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3, A4, A5); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3, A4, A5, A6); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3, A4, A5, A6)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { return (*m_pf)(a1, a2, a3, a4, a5, a6); } - - private: - R (*m_pf)(A1, A2, A3, A4, A5, A6); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3, A4, A5, A6); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3, A4, A5, A6); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3, A4, A5, A6, A7); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3, A4, A5, A6, A7)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { return (*m_pf)(a1, a2, a3, a4, a5, a6, a7); } - - private: - R (*m_pf)(A1, A2, A3, A4, A5, A6, A7); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3, A4, A5, A6, A7); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6, A7)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6, a7); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6, A7); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3, A4, A5, A6, A7); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6, A7) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6, a7); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6, A7) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3, A4, A5, A6, A7, A8); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3, A4, A5, A6, A7, A8)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { return (*m_pf)(a1, a2, a3, a4, a5, a6, a7, a8); } - - private: - R (*m_pf)(A1, A2, A3, A4, A5, A6, A7, A8); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3, A4, A5, A6, A7, A8); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6, A7, A8)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6, a7, a8); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6, A7, A8); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3, A4, A5, A6, A7, A8); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6, A7, A8) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6, a7, a8); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6, A7, A8) const; - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(A1, A2, A3, A4, A5, A6, A7, A8, A9); - - TC_FunctorAdapter(R (*pf)(A1, A2, A3, A4, A5, A6, A7, A8, A9)) - : m_pf(pf) - { } - - R operator()(typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { return (*m_pf)(a1, a2, a3, a4, a5, a6, a7, a8, a9); } - - private: - R (*m_pf)(A1, A2, A3, A4, A5, A6, A7, A8, A9); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(T*, A1, A2, A3, A4, A5, A6, A7, A8, A9); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6, A7, A8, A9)) - : m_pmf(pmf) - { } - - R operator()(T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6, a7, a8, a9); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6, A7, A8, A9); - }; - - template - class TC_FunctorAdapter { - public: - typedef R (RunType)(const T*, A1, A2, A3, A4, A5, A6, A7, A8, A9); - - TC_FunctorAdapter(R (T::*pmf)(A1, A2, A3, A4, A5, A6, A7, A8, A9) const) - : m_pmf(pmf) - { } - - R operator()(const T *pobj, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { return (pobj->*m_pmf)(a1, a2, a3, a4, a5, a6, a7, a8, a9); } - - private: - R (T::*m_pmf)(A1, A2, A3, A4, A5, A6, A7, A8, A9) const; - }; - - template - class TC_UnretainedWrapper { - public: - explicit TC_UnretainedWrapper(T *p) : m_ptr(p) { } - T *get() const { return m_ptr; } - private: - T *m_ptr; - }; - - template - class TC_OwnedWrapper { - public: - explicit TC_OwnedWrapper(T *p) : m_ptr(p) { } - ~TC_OwnedWrapper() - { - if(m_ptr != NULL) - { - delete m_ptr; - m_ptr = NULL; - } - } - TC_OwnedWrapper(const TC_OwnedWrapper& o) - { - m_ptr = o.m_ptr; - o.m_ptr = NULL; - } - TC_OwnedWrapper& operator=(const TC_OwnedWrapper& o) - { - if(m_ptr != NULL) - { - delete m_ptr; - m_ptr = NULL; - } - m_ptr = o.m_ptr; - o.m_ptr = NULL; - } - T *get() const { return m_ptr; } - - private: - mutable T *m_ptr; - }; - - template - struct TC_SharedWrapper { - public: - explicit TC_SharedWrapper(const TC_SharedPtr& p) - : m_ptr(p) - { } - T *get() const { return m_ptr.get(); } - private: - TC_SharedPtr m_ptr; - }; - - template - struct TC_UnwrapTraits { - typedef const T& ForwardType; - static ForwardType unwrap(const T& o) { return o; } - }; - - template - struct TC_UnwrapTraits > { - typedef T* ForwardType; - static ForwardType unwrap(const TC_UnretainedWrapper& unretained) - { return unretained.get(); } - }; - - template - struct TC_UnwrapTraits > { - typedef T* ForwardType; - static ForwardType unwrap(const TC_OwnedWrapper& owned) - { return owned.get(); } - }; - - template - struct TC_UnwrapTraits > { - typedef T* ForwardType; - static ForwardType unwrap(const TC_SharedWrapper& shared) - { return shared.get(); } - }; - - template - class TC_Invoker; - - template - class TC_Invoker { - public: - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - return (bind_state->m_functor)(); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1) - { - TC_BindState *bind_state = static_cast(base); - return (bind_state->m_functor)(a1); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2) - { - TC_BindState *bind_state = static_cast(base); - return (bind_state->m_functor)(a1, a2); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2) - { - TC_BindState *bind_state = static_cast(base); - - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3, A4); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3, a4); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3, A4); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3, a4); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3, A4); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3, a4); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A4); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a4) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3, a4); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - return (bind_state->m_functor)(a1, a2, a3, a4); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3, A4, A5); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3, a4, a5); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3, A4, A5); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3, a4, a5); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3, A4, A5); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3, a4, a5); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A4, A5); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3, a4, a5); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A5); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a5) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - return (bind_state->m_functor)(a1, a2, a3, a4, a5); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - return (bind_state->m_functor)(a1, a2, a3, a4, a5); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3, A4, A5, A6); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3, A4, A5, A6); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3, A4, A5, A6); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A4, A5, A6); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A5, A6); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A6); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a6) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3, A4, A5, A6, A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3, A4, A5, A6, A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3, A4, A5, A6, A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A4, A5, A6, A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A5, A6, A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A6, A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A7); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a7) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typedef typename TC_BindState::Bound7UnwrapTraits Bound7UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - typename Bound7UnwrapTraits::ForwardType a7 = Bound7UnwrapTraits::unwrap(bind_state->m_a7); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3, A4, A5, A6, A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3, A4, A5, A6, A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3, A4, A5, A6, A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A4, A5, A6, A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A5, A6, A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A6, A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A7, A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A8); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a8) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typedef typename TC_BindState::Bound7UnwrapTraits Bound7UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - typename Bound7UnwrapTraits::ForwardType a7 = Bound7UnwrapTraits::unwrap(bind_state->m_a7); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typedef typename TC_BindState::Bound7UnwrapTraits Bound7UnwrapTraits; - typedef typename TC_BindState::Bound8UnwrapTraits Bound8UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - typename Bound7UnwrapTraits::ForwardType a7 = Bound7UnwrapTraits::unwrap(bind_state->m_a7); - typename Bound8UnwrapTraits::ForwardType a8 = Bound8UnwrapTraits::unwrap(bind_state->m_a8); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A1, A2, A3, A4, A5, A6, A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a1, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A2, A3, A4, A5, A6, A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a2, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A3, A4, A5, A6, A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a3, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A4, A5, A6, A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a4, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A5, A6, A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a5, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A6, A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a6, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A7, A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a7, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A8, A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a8, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typedef typename TC_BindState::Bound7UnwrapTraits Bound7UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - typename Bound7UnwrapTraits::ForwardType a7 = Bound7UnwrapTraits::unwrap(bind_state->m_a7); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(A9); - - static R invoke(TC_BindStateBase *base, - typename TC_CallbackParamTraits::ForwardType a9) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typedef typename TC_BindState::Bound7UnwrapTraits Bound7UnwrapTraits; - typedef typename TC_BindState::Bound8UnwrapTraits Bound8UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - typename Bound7UnwrapTraits::ForwardType a7 = Bound7UnwrapTraits::unwrap(bind_state->m_a7); - typename Bound8UnwrapTraits::ForwardType a8 = Bound8UnwrapTraits::unwrap(bind_state->m_a8); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - class TC_Invoker { - public: - - typedef R (UnboundRunType)(); - - static R invoke(TC_BindStateBase *base) - { - TC_BindState *bind_state = static_cast(base); - typedef typename TC_BindState::Bound1UnwrapTraits Bound1UnwrapTraits; - typedef typename TC_BindState::Bound2UnwrapTraits Bound2UnwrapTraits; - typedef typename TC_BindState::Bound3UnwrapTraits Bound3UnwrapTraits; - typedef typename TC_BindState::Bound4UnwrapTraits Bound4UnwrapTraits; - typedef typename TC_BindState::Bound5UnwrapTraits Bound5UnwrapTraits; - typedef typename TC_BindState::Bound6UnwrapTraits Bound6UnwrapTraits; - typedef typename TC_BindState::Bound7UnwrapTraits Bound7UnwrapTraits; - typedef typename TC_BindState::Bound8UnwrapTraits Bound8UnwrapTraits; - typedef typename TC_BindState::Bound9UnwrapTraits Bound9UnwrapTraits; - typename Bound1UnwrapTraits::ForwardType a1 = Bound1UnwrapTraits::unwrap(bind_state->m_a1); - typename Bound2UnwrapTraits::ForwardType a2 = Bound2UnwrapTraits::unwrap(bind_state->m_a2); - typename Bound3UnwrapTraits::ForwardType a3 = Bound3UnwrapTraits::unwrap(bind_state->m_a3); - typename Bound4UnwrapTraits::ForwardType a4 = Bound4UnwrapTraits::unwrap(bind_state->m_a4); - typename Bound5UnwrapTraits::ForwardType a5 = Bound5UnwrapTraits::unwrap(bind_state->m_a5); - typename Bound6UnwrapTraits::ForwardType a6 = Bound6UnwrapTraits::unwrap(bind_state->m_a6); - typename Bound7UnwrapTraits::ForwardType a7 = Bound7UnwrapTraits::unwrap(bind_state->m_a7); - typename Bound8UnwrapTraits::ForwardType a8 = Bound8UnwrapTraits::unwrap(bind_state->m_a8); - typename Bound9UnwrapTraits::ForwardType a9 = Bound9UnwrapTraits::unwrap(bind_state->m_a9); - return (bind_state->m_functor)(a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - }; - - template - struct TC_BindState; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - - explicit TC_BindState(const FunctorType& functor) - : m_functor(functor) - { } - - FunctorType m_functor; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1) - : m_functor(functor), - m_a1(a1) - { } - - FunctorType m_functor; - A1 m_a1; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2) - : m_functor(functor), - m_a1(a1), - m_a2(a2) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - typedef TC_UnwrapTraits Bound4UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3), - m_a4(a4) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - A4 m_a4; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - typedef TC_UnwrapTraits Bound4UnwrapTraits; - typedef TC_UnwrapTraits Bound5UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3, - const A4& a4, const A5& a5) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3), - m_a4(a4), - m_a5(a5) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - A4 m_a4; - A5 m_a5; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - typedef TC_UnwrapTraits Bound4UnwrapTraits; - typedef TC_UnwrapTraits Bound5UnwrapTraits; - typedef TC_UnwrapTraits Bound6UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3, - const A4& a4, const A5& a5, const A6& a6) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3), - m_a4(a4), - m_a5(a5), - m_a6(a6) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - A4 m_a4; - A5 m_a5; - A6 m_a6; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - typedef TC_UnwrapTraits Bound4UnwrapTraits; - typedef TC_UnwrapTraits Bound5UnwrapTraits; - typedef TC_UnwrapTraits Bound6UnwrapTraits; - typedef TC_UnwrapTraits Bound7UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, - const A5& a5, const A6& a6, const A7& a7) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3), - m_a4(a4), - m_a5(a5), - m_a6(a6), - m_a7(a7) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - A4 m_a4; - A5 m_a5; - A6 m_a6; - A7 m_a7; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - typedef TC_UnwrapTraits Bound4UnwrapTraits; - typedef TC_UnwrapTraits Bound5UnwrapTraits; - typedef TC_UnwrapTraits Bound6UnwrapTraits; - typedef TC_UnwrapTraits Bound7UnwrapTraits; - typedef TC_UnwrapTraits Bound8UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, - const A5& a5, const A6& a6, const A7& a7, const A8& a8) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3), - m_a4(a4), - m_a5(a5), - m_a6(a6), - m_a7(a7), - m_a8(a8) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - A4 m_a4; - A5 m_a5; - A6 m_a6; - A7 m_a7; - A8 m_a8; - }; - - template - class TC_BindState : public TC_BindStateBase { - public: - typedef TC_Invoker InvokerType; - typedef typename InvokerType::UnboundRunType UnboundRunType; - typedef TC_UnwrapTraits Bound1UnwrapTraits; - typedef TC_UnwrapTraits Bound2UnwrapTraits; - typedef TC_UnwrapTraits Bound3UnwrapTraits; - typedef TC_UnwrapTraits Bound4UnwrapTraits; - typedef TC_UnwrapTraits Bound5UnwrapTraits; - typedef TC_UnwrapTraits Bound6UnwrapTraits; - typedef TC_UnwrapTraits Bound7UnwrapTraits; - typedef TC_UnwrapTraits Bound8UnwrapTraits; - typedef TC_UnwrapTraits Bound9UnwrapTraits; - - TC_BindState(const FunctorType& functor, const A1& a1, const A2& a2, const A3& a3, const A4& a4, - const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9) - : m_functor(functor), - m_a1(a1), - m_a2(a2), - m_a3(a3), - m_a4(a4), - m_a5(a5), - m_a6(a6), - m_a7(a7), - m_a8(a8), - m_a9(a9) - { } - - FunctorType m_functor; - A1 m_a1; - A2 m_a2; - A3 m_a3; - A4 m_a4; - A5 m_a5; - A6 m_a6; - A7 m_a7; - A8 m_a8; - A9 m_a9; - }; - - template - struct TC_FunctorTraits { - typedef TC_FunctorAdapter FunctorType; - typedef typename FunctorType::RunType RunType; - }; - - template - struct TC_FunctorTraits > { - typedef TC_Callback FunctorType; - typedef typename TC_Callback::RunType RunType; - }; - - template - struct TC_FunctionTraits; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - typedef A4 A4Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - typedef A4 A4Type; - typedef A5 A5Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - typedef A4 A4Type; - typedef A5 A5Type; - typedef A6 A6Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - typedef A4 A4Type; - typedef A5 A5Type; - typedef A6 A6Type; - typedef A7 A7Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - typedef A4 A4Type; - typedef A5 A5Type; - typedef A6 A6Type; - typedef A7 A7Type; - typedef A8 A8Type; - }; - - template - struct TC_FunctionTraits { - typedef R ReturnType; - typedef A1 A1Type; - typedef A2 A2Type; - typedef A3 A3Type; - typedef A4 A4Type; - typedef A5 A5Type; - typedef A6 A6Type; - typedef A7 A7Type; - typedef A8 A8Type; - typedef A9 A9Type; - }; - - } - -} - -#endif diff --git a/cpp/util/include/util/tc_bitmap.h b/cpp/util/include/util/tc_bitmap.h deleted file mode 100644 index fb2ccc9a5..000000000 --- a/cpp/util/include/util/tc_bitmap.h +++ /dev/null @@ -1,250 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_BIT_MAP_H__ -#define __TC_BIT_MAP_H__ - -#include -#include -#include -#include "util/tc_ex.h" - -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_bitmap.h - * @brief 多ä½bitmapç±». - */ -///////////////////////////////////////////////// -/** - * @brief 异常 - */ -struct TC_BitMap_Exception : public TC_Exception -{ - TC_BitMap_Exception(const string &buffer) : TC_Exception(buffer){}; - ~TC_BitMap_Exception() throw(){}; -}; - - -/** - * @brief 内存bitmap,æ¯ä¸ªæ•´æ•°1ä½ï¼Œå¯ä»¥æ”¯æŒå¤šä½ï¼Œå³å‡ ä¸ªæ•´æ•°å¤šä½. - * - * æ“作过程ä¸åŠ é”,如果有需è¦åœ¨å¤–é¢è°ƒç”¨çš„时候加,通常采用群é”ç­–ç•¥. - * - * 注æ„群é”策略应该/8,然åŽæŒ‰ç…§å°¾å·åˆ†ç¾¤é” - */ -class TC_BitMap -{ -public: - /** - * @brief 内存的bitmap,æ¯ä¸ªæ•´æ•°ä¿æŒ1ä½ - * - */ - class BitMap - { - public: - - static const int _magic_bits[8]; - - #define _set_bit(n,m) (n|_magic_bits[m]) - #define _clear_bit(n,m) (n&(~_magic_bits[m])) - #define _get_bit(n,m) (n&_magic_bits[m]) - - /**共享内存版本*/ - #define BM_VERSION 1 - - /** - * @brief æ ¹æ®å…ƒç´ ä¸ªæ•°è®¡ç®—需è¦å†…å­˜çš„å¤§å° - * @param iElementCount, 需è¦ä¿å­˜çš„元素个数(元素从0开始记) - * - * @return size_t - */ - static size_t calcMemSize(size_t iElementCount); - - /** - * @brief åˆå§‹åŒ– - * @param pAddr ç»å¯¹åœ°å€ - * @param iSize 大å°, 采用(calcMemSize)è®¡ç®—å‡ºæ¥ - * @return 0: æˆåŠŸ, -1:内存ä¸å¤Ÿ - */ - void create(void *pAddr, size_t iSize); - - /** - * @brief é“¾æŽ¥åˆ°å†…å­˜å— - * @param pAddr 地å€, 采用(calcMemSize)è®¡ç®—å‡ºæ¥ - * @return 0, æˆåŠŸ, -1,版本ä¸å¯¹, -2:大å°ä¸å¯¹ - */ - int connect(void *pAddr, size_t iSize); - - /** - * @brief 是å¦æœ‰æ ‡è¯† - * @param i - * @return int, >0:有标识, =0:无标识, <0:超过范围 - */ - int get(size_t i); - - /** - * @brief 设置标识 - * @param i - * @return int, >0:有标识, =0:无标识, <0:超过范围 - */ - int set(size_t i); - - /** - * @brief 清除标识 - * @param i - * - * @return int, >0:有标识, =0:无标识, <0:超过范围 - */ - int clear(size_t i); - - /** - * @brief æ¸…é™¤æ‰€æœ‰çš„æ•°æ® - * - * @return int - */ - int clear4all(); - - /** - * @brief dump到文件 - * @param sFile - * - * @return int - */ - int dump2file(const string &sFile); - - /** - * @brief 从文件load - * @param sFile - * - * @return int - */ - int load5file(const string &sFile); - - /**共享内存头部*/ - struct tagBitMapHead - { - char _cVersion; /**版本, 当å‰ç‰ˆæœ¬ä¸º1*/ - size_t _iMemSize; /**共享内存大å°*/ - }__attribute__((packed)); - - /** - * @brief 获å–å¤´éƒ¨åœ°å€ - * @return tagBitMapHead* 共享内存头部 - */ - BitMap::tagBitMapHead *getAddr() const { return _pHead; } - - /** - * @brief 获å–å†…å­˜å¤§å° - * @return å†…å­˜å¤§å° - */ - size_t getMemSize() const { return _pHead->_iMemSize; } - - protected: - - /** - * 共享内存头部 - */ - tagBitMapHead *_pHead; - - /** - * æ•°æ®å—指针 - */ - unsigned char * _pData; - }; - - /** - * @brief æ ¹æ®å…ƒç´ ä¸ªæ•°è®¡ç®—需è¦å†…å­˜çš„å¤§å° - * @param iElementCount 需è¦ä¿å­˜çš„元素个数(元素从0开始记) - * @param iBitCount æ¯ä¸ªå…ƒç´ æ”¯æŒå‡ ä½(默认1ä½) (ä½æ•°>=1) - * @return æ‰€éœ€å†…å­˜çš„å¤§å° - */ - static size_t calcMemSize(size_t iElementCount, unsigned iBitCount = 1); - - /** - * @brief åˆå§‹åŒ– - * @param pAddr ç»å¯¹åœ°å€ - * @param iSize 大å°, 采用(calcMemSize)è®¡ç®—å‡ºæ¥ - * @return 0: æˆåŠŸ, -1:内存ä¸å¤Ÿ - */ - void create(void *pAddr, size_t iSize, unsigned iBitCount = 1); - - /** - * @brief é“¾æŽ¥åˆ°å†…å­˜å— - * @param pAddr 地å€ï¼Œé‡‡ç”¨(calcMemSize)è®¡ç®—å‡ºæ¥ - * @return 0:æˆåŠŸ, -1:版本ä¸å¯¹, -2:大å°ä¸å¯¹ - */ - int connect(void *pAddr, size_t iSize, unsigned iBitCount = 1); - - /** - * @brief 是å¦æœ‰æ ‡è¯† - * @param i 元素值 - * @param iBit ç¬¬å‡ ä½ - * @return int, >0:有标识, =0:无标识, <0:超过范围 - */ - int get(size_t i, unsigned iBit = 1); - - /** - * @brief 设置标识 - * @param i 元素值 - * @param iBit ç¬¬å‡ ä½ - * @return int, >0:有标识, =0:无标识, <0:超过范围 - */ - int set(size_t i, unsigned iBit = 1); - - /** - * @brief 清除标识 - * @param i 元素值 - * @param iBit ç¬¬å‡ ä½ - * @return int, >0:有标识, =0:无标识, <0:超过范围 - */ - int clear(size_t i, unsigned iBit = 1); - - /** - * @brief 清除所有的标识 - * - * @param iBit ç¬¬å‡ ä½ - * @return int - */ - int clear4all(unsigned iBit = (unsigned)(-1)); - - /** - * @brief dump到文件 - * @param sFile - * - * @return int - */ - int dump2file(const string &sFile); - - /** - * @brief 从文件load - * @param sFile - * - * @return int - */ - int load5file(const string &sFile); - -protected: - vector _bitmaps; -}; - -} - -#endif - diff --git a/cpp/util/include/util/tc_buffer.h b/cpp/util/include/util/tc_buffer.h deleted file mode 100644 index 095f270d7..000000000 --- a/cpp/util/include/util/tc_buffer.h +++ /dev/null @@ -1,229 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_BUFFER_H -#define __TC_BUFFER_H - -#include - -namespace tars -{ - -///////////////////////////////////////////////// -/** - *@file tc_buffer.h - *@brief 字节æµç¼“冲区å°è£…ç±». - * - */ -///////////////////////////////////////////////// - -/** - *@brief 字节æµç¼“冲区å°è£…ç±». - */ -class TC_Buffer -{ -public: - /** - * @brief 构造函数. - * - */ - TC_Buffer() : - _readPos(0), - _writePos(0), - _capacity(0), - _buffer(NULL), - _highWaterPercent(50) - { - } - - /** - * @brief æžæž„函数. - */ - ~TC_Buffer() - { - delete[] _buffer; - } - -private: - /** - * @brief ç¦æ­¢å¤åˆ¶ - */ - TC_Buffer(const TC_Buffer& ); - void operator=(const TC_Buffer& ); - -public: - /** - * @brief 将数æ®æ”¾å…¥ç¼“冲 - * - * @param data è¦å†™å…¥çš„æ•°æ®èµ·å§‹åœ°å€ - * @param size è¦å†™å…¥çš„æ•°æ®å­—节数 - * @return 写入的字节数 - */ - std::size_t PushData(const void* data, std::size_t size); - - /** - * @brief 调整缓冲区写游标 - * - * @param bytes è¦è°ƒæ•´çš„字节数 - */ - void Produce(std::size_t bytes) { _writePos += bytes; } - - /** - * @brief 从缓冲å–出数æ®,深拷è´,调整读游标 - * - * @param buf 接收数æ®çš„èµ·å§‹åœ°å€ - * @param size buf的字节数 - * @return 实际写入的字节数,å¯èƒ½å°äºŽsize - */ - std::size_t PopData(void* buf, std::size_t size); - - /** - * @brief 从缓冲å–出数æ®,æµ…æ‹·è´,ä¸è°ƒæ•´è¯»æ¸¸æ ‡ - * - * @param buf 接收数æ®çš„指针 - * @param size 接收数æ®çš„å¤§å° - */ - void PeekData(void*& buf, std::size_t& size); - - /** - * @brief 调整缓冲区读游标 - * - * @param bytes è¦è°ƒæ•´çš„字节数 - */ - void Consume(std::size_t bytes); - - /** - * @brief 缓冲区å¯è¯»æ•°æ®èµ·å§‹åœ°å€ - * - * @return 缓冲区å¯è¯»æ•°æ®èµ·å§‹åœ°å€ - */ - char* ReadAddr() { return &_buffer[_readPos]; } - - /** - * @brief 缓冲区å¯å†™èµ·å§‹åœ°å€ - * - * @return 缓冲区å¯å†™èµ·å§‹åœ°å€ - */ - char* WriteAddr() { return &_buffer[_writePos]; } - - /** - * @brief 缓冲区是å¦æœ‰æ•°æ® - * - * @return True:ç¼“å†²åŒºæ²¡æœ‰æ•°æ® - */ - bool IsEmpty() const { return ReadableSize() == 0; } - - /** - * @brief 缓冲区数æ®å¤§å° - * - * @return 缓冲区数æ®å­—节数 - */ - std::size_t ReadableSize() const { return _writePos - _readPos; } - - /** - * @brief 缓冲区å¯å†™ç©ºé—´å¤§å° - * - * @return 缓冲区å¯å†™ç©ºé—´å­—节数 - */ - std::size_t WritableSize() const { return _capacity - _writePos; } - - /** - * @brief 缓冲区å ç”¨å†…å­˜å¤§å° - * - * @return 缓冲区å ç”¨å†…存字节数 - */ - std::size_t Capacity() const { return _capacity; } - - /** - * @brief 当内存å ç”¨åœ¨æ•°æ®å¤§å°ä¸¤å€ä»¥ä¸Šï¼Œå°è¯•é‡Šæ”¾å†…å­˜(并ä¸æ˜¯æ¸…除数æ®) - * - */ - void Shrink(); - - /** - * @brief 清除数æ®,并ä¸é‡Šæ”¾å†…å­˜ - * - */ - void Clear(); - - /** - * @brief 交æ¢ä¸¤ä¸ªç¼“冲 - * - * @param buf è¦äº¤æ¢çš„缓冲 - */ - void Swap(TC_Buffer& buf); - - /** - * @brief ç¡®ä¿ç¼“冲有足够大å°å®¹çº³size字节的数æ®å†™å…¥ - * - * @param size å°†è¦å†™å…¥çš„æ•°æ®å­—节数 - */ - void AssureSpace(std::size_t size); - - /** - * @brief 设置负载率,大于此则Shrink什么都ä¸åš - * - * @param percents 负载率[10,100) - */ - void SetHighWaterPercent(size_t percents); - /** - * 缓冲区大å°ä¸Šé™ - */ - static const std::size_t kMaxBufferSize; - - /** - * ç¼“å†²åŒºé»˜è®¤å¤§å° - */ - static const std::size_t kDefaultSize; - -private: - /** - * 读游标 - */ - std::size_t _readPos; - - /** - * 写游标 - */ - std::size_t _writePos; - - /** - * 缓冲所å ç”¨çš„内存字节数 - */ - std::size_t _capacity; - - /** - * 缓冲区 - */ - char* _buffer; - - /** - * 高水ä½ç™¾åˆ†æ¯”,影å“Shrink() - */ - size_t _highWaterPercent; - - /** - * @brief é‡è®¾ç¼“冲区指针 - * - * @param ptr å°†è¦è®¾ç½®çš„指针 - */ - void ResetBuffer(void* ptr = NULL); -}; - -} // end namespace tars - -#endif - diff --git a/cpp/util/include/util/tc_buffer_pool.h b/cpp/util/include/util/tc_buffer_pool.h deleted file mode 100644 index bae500471..000000000 --- a/cpp/util/include/util/tc_buffer_pool.h +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_BUFFERPOOL_H -#define __TC_BUFFERPOOL_H - -#include -#include -#include - - -namespace tars -{ - -///////////////////////////////////////////////// -/** - *@file tc_buffer_pool.h - *@brief 缓冲池å°è£…ç±». - */ -///////////////////////////////////////////////// - - -struct TC_Slice -{ - explicit TC_Slice(void* d = NULL , size_t ds = 0, size_t l = 0); - void* data; - size_t dataLen; - size_t len; - -}; - -/** - *@brief 缓冲池å°è£…ç±». - */ -class TC_BufferPool -{ -public: - /** - * @brief 构造函数. - * @param minBlock 该缓冲池所负责分é…的最å°å†…å­˜å—å¤§å° - * @param maxBlock 该缓冲池所负责分é…的最大内存å—å¤§å° - */ - TC_BufferPool(size_t minBlock, size_t maxBlock); - - /** - * @brief æžæž„函数. - */ - ~TC_BufferPool(); - - /** - * @brief å†…å­˜åˆ†é… - * @param size 请求分é…的内存å—å¤§å° - * @return 分é…的内存地å€åŠå¤§å° - */ - TC_Slice Allocate(size_t size); - - /** - * @brief 内存释放 - * @param s 请求释放的内存å—å¤§å° - */ - void Deallocate(TC_Slice s); - - /** - * @brief 设置pool中内存å ç”¨ä¸Šé™ - * @param bytes ä¸Šé™ - */ - void SetMaxBytes(size_t bytes); - - /** - * @brief 获å–pool中内存å ç”¨ä¸Šé™ - * reutrn ä¸Šé™ - */ - size_t GetMaxBytes() const; - - /** - * @brief è°ƒè¯•æ‰“å° - * reutrn 打å°çš„字符串 - */ - std::string DebugPrint() const; - -private: - typedef std::list BufferList; - - /** - * @brief 从指定的buffer链表获å–内存 - * @param size 请求分é…的内存å—å¤§å° - * @param blist 请求的buffer链表 - * @return 分é…的内存地å€åŠå¤§å° - */ - TC_Slice _Allocate(size_t size, BufferList& blist); - - /** - * @brief æ ¹æ®æŒ‡å®šçš„分é…尺寸,寻找åˆé€‚çš„buffer链表 - * @param s 请求分é…的内存å—å¤§å° - * @return buffer链表 - */ - BufferList& _GetBufferList(size_t s); - - /** - * @brief æ ¹æ®æŒ‡å®šçš„分é…尺寸,寻找åˆé€‚çš„buffer链表 - * @param s 请求分é…的内存å—å¤§å° - * @return buffer链表 - */ - const BufferList& _GetBufferList(size_t s) const; - - /** - * buffer链表池 - */ - std::vector _buffers; - - /** - * buffer链表池负责分é…的最å°å†…å­˜å— - */ - const size_t _minBlock; - - /** - * buffer链表池负责分é…çš„æœ€å¤§å†…å­˜å— - */ - const size_t _maxBlock; - - /** - * buffer链表池å ç”¨çš„å†…å­˜ä¸Šé™ - */ - size_t _maxBytes; - - /** - * buffer链表池目å‰å ç”¨çš„内存 - */ - size_t _totalBytes; -}; - -} //end namespace tars - -#endif - diff --git a/cpp/util/include/util/tc_callback.h b/cpp/util/include/util/tc_callback.h deleted file mode 100644 index 2c8a82136..000000000 --- a/cpp/util/include/util/tc_callback.h +++ /dev/null @@ -1,512 +0,0 @@ -#ifndef __TC_CALLBACK_H__ -#define __TC_CALLBACK_H__ - -#include "tc_shared_ptr.h" -#include "tc_callback_detail.h" - -namespace tars -{ - - namespace detail - { - template class TC_BindState; - } - - template - class TC_Callback; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(void); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()() const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get()); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2) - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType, - typename detail::TC_CallbackParamTraits::ForwardType); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3, A4); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3, a4); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3, A4, A5); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3, a4, a5); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3, A4, A5, A6); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3, a4, a5, a6); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3, A4, A5, A6, A7); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6, - typename detail::TC_CallbackParamTraits::ForwardType a7) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3, a4, a5, a6, a7); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6, - typename detail::TC_CallbackParamTraits::ForwardType a7); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3, A4, A5, A6, A7, A8); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6, - typename detail::TC_CallbackParamTraits::ForwardType a7, - typename detail::TC_CallbackParamTraits::ForwardType a8) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3, a4, a5, a6, a7, a8); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6, - typename detail::TC_CallbackParamTraits::ForwardType a7, - typename detail::TC_CallbackParamTraits::ForwardType a8); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - - template - class TC_Callback { - private: - typedef void (*GenFuncPointer)(void); - typedef GenFuncPointer unspecified_bool_type; - - public: - - typedef R (RunType)(A1, A2, A3, A4, A5, A6, A7, A8, A9); - - TC_Callback() - : m_invoke(NULL) - { } - - template - TC_Callback(detail::TC_BindState* bind_state) - : m_bind_state_ptr(bind_state) - { - PolymorphicInvoke invoke = &detail::TC_BindState::InvokerType::invoke; - m_invoke = reinterpret_cast(invoke); - } - - R operator()(typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6, - typename detail::TC_CallbackParamTraits::ForwardType a7, - typename detail::TC_CallbackParamTraits::ForwardType a8, - typename detail::TC_CallbackParamTraits::ForwardType a9) const - { - PolymorphicInvoke f = reinterpret_cast(m_invoke); - return f(m_bind_state_ptr.get(), a1, a2, a3, a4, a5, a6, a7, a8, a9); - } - - operator unspecified_bool_type() const - { - return m_invoke; - } - - private: - - typedef R (*PolymorphicInvoke)(detail::TC_BindStateBase*, - typename detail::TC_CallbackParamTraits::ForwardType a1, - typename detail::TC_CallbackParamTraits::ForwardType a2, - typename detail::TC_CallbackParamTraits::ForwardType a3, - typename detail::TC_CallbackParamTraits::ForwardType a4, - typename detail::TC_CallbackParamTraits::ForwardType a5, - typename detail::TC_CallbackParamTraits::ForwardType a6, - typename detail::TC_CallbackParamTraits::ForwardType a7, - typename detail::TC_CallbackParamTraits::ForwardType a8, - typename detail::TC_CallbackParamTraits::ForwardType a9); - - TC_SharedPtr m_bind_state_ptr; - GenFuncPointer m_invoke; - }; - -} - -#endif diff --git a/cpp/util/include/util/tc_callback_detail.h b/cpp/util/include/util/tc_callback_detail.h deleted file mode 100644 index a8f605d14..000000000 --- a/cpp/util/include/util/tc_callback_detail.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef __TC_CALLBACK_DETAIL_H__ -#define __TC_CALLBACK_DETAIL_H__ - -#include "tc_scoped_ptr.h" - -namespace tars -{ - - namespace detail - { - - class TC_BindStateBase - { - public: - virtual ~TC_BindStateBase() { } - }; - - - template - struct TC_CallbackParamTraits - { - typedef const T& ForwardType; - typedef T StorageType; - }; - - template - struct TC_CallbackParamTraits - { - typedef T& ForwardType; - typedef T StorageType; - }; - - template - struct TC_CallbackParamTraits - { - typedef const T& ForwardType; - typedef T StorageType; - }; - - template - struct TC_CallbackParamTraits - { - typedef const T* ForwardType; - typedef const T* StorageType; - }; - - template - struct TC_CallbackParamTraits - { - typedef const T* ForwardType; - typedef const T* StorageType; - }; - - - template - struct TC_CallbackParamTraits > - { - typedef TC_ScopedPtr ForwardType; - typedef TC_ScopedPtr StorageType; - }; - - } - -} - -#endif diff --git a/cpp/util/include/util/tc_cgi.h b/cpp/util/include/util/tc_cgi.h deleted file mode 100644 index 5abf0602a..000000000 --- a/cpp/util/include/util/tc_cgi.h +++ /dev/null @@ -1,595 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_CGI_H -#define __TC_CGI_H - -#include -#include -#include -#include -#include "util/tc_ex.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_cgi.h -* @brief CGI处ç†ç±» -*/ -///////////////////////////////////////////////// -class TC_Cgi; -class TC_Cgi_Upload; -class TC_HttpRequest; - -/** -* @brief é…置文件异常类 -*/ -struct TC_Cgi_Exception : public TC_Exception -{ - TC_Cgi_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_Cgi_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_Cgi_Exception() throw(){}; -}; - -/** -* @brief 全局的å‹å…ƒå‡½æ•°ï¼Œå®šä¹‰è¯¥å‡½æ•°, -* 则å¯ä»¥ç”¨TC_Common::tostr对 vector进行输出æ“作 -*/ -ostream &operator<<(ostream &os, const TC_Cgi_Upload &tcCgiUpload); - -/** -* @brief cgi上传文件æ“作,通过该类获å–cgiä¸Šä¼ çš„æ–‡ä»¶ä¿¡æ¯ -*/ -class TC_Cgi_Upload -{ -public: - friend ostream &operator<<(ostream &os, const TC_Cgi_Upload &tcCgiUpload); - - /** - * @brief 构造函数 - */ - TC_Cgi_Upload() - :_sFileName("") - , _sRealFileName("") - , _sServerFileName("") - , _iSize(0) - , _bOverSize(false) - { - } - - /** - * @brief æ‹·è´æž„造函数. - */ - - TC_Cgi_Upload(const TC_Cgi_Upload &tcCgiUpload); - - /** - * @brief 赋值构造函数 - */ - TC_Cgi_Upload & operator=(const TC_Cgi_Upload &tcCgiUpload); - - /** - * @brief 获å–上传的信æ¯. - * - * return ä¸Šä¼ æ–‡ä»¶çš„ä¿¡æ¯ - */ - string tostr() const; - - /** - * @brief 获å–客户端IE INPUT上传控件的å称. - * - * return INPUT上传控件å称 - */ - string getFormFileName() const - { - return _sFileName; - } - - /** - * @brief 返回INPUT控件用户输入的å称,åŠå®¢æˆ·ç«¯çœŸå®žçš„文件å称. - * - * return 客户端真实的文件å称 - */ - string retRealFileName() const - { - return _sRealFileName; - } - - /** - * @brief 上传到æœåŠ¡å™¨åŽ,æœåŠ¡å™¨ç«¯æ–‡ä»¶å称. - * - * return æœåŠ¡å™¨ç«¯æ–‡ä»¶å称 - */ - string getSeverFileName() const - { - return _sServerFileName; - } - - /** - * @brief 获å–上传的文件大å°. - * - * return size_tç±»åž‹ï¼Œä¸Šä¼ çš„æ–‡ä»¶å¤§å° - */ - size_t getFileSize() const - { - return _iSize; - } - - /** - * @brief 上传的文件是å¦è¶…过大å°. - * - * return 超过大å°è¿”回true,å¦åˆ™è¿”回false - */ - bool isOverSize() const - { - return _bOverSize; - } - -protected: - - /** - * 上传文件,æµè§ˆå™¨file控件å称 - */ - string _sFileName; - - /** - * 上传文件真实å称,客户端的文件å称 - */ - string _sRealFileName; - - /** - * 上传文件æœåŠ¡å™¨ç«¯å称 - */ - string _sServerFileName; - - /** - * 上传文件大å°,字节数 - */ - size_t _iSize; - - /** - * 上传文件是å¦è¶…è¿‡å¤§å° - */ - bool _bOverSize; - - friend class TC_Cgi; -}; - -/** -* @brief cgiæ“作相关类. -* -* 主è¦æ“作包括: -* -* 1 支æŒå‚æ•°è§£æž -* -* 2 支æŒcookiesè§£æž -* -* 3 支æŒæ–‡ä»¶ä¸Šä¼ ,设置上传文件的最大个数,æ–‡ä»¶çš„æœ€å¤§å¤§å° -* -* 4 上传文件时, 需è¦æ£€æŸ¥æ–‡ä»¶æ˜¯å¦è¶…è¿‡æœ€å¤§å¤§å° -* -* 5 上传文件时, 需è¦æ£€æŸ¥ä¸Šä¼ æ–‡ä»¶ä¸ªæ•°æ˜¯å¦é™åˆ¶ -* -* 说明:多个文件åŒæ—¶ä¸Šä¼ æ—¶,æµè§ˆå™¨çš„file控件必须å–ä¸åŒname,å¦åˆ™å°†æ— æ³•æ­£ç¡®çš„上传文件 -* -* 注æ„:调用parseCgi解æžæ ‡å‡†è¾“å…¥, -* -* 如果有文件上传需è¦è°ƒç”¨setUpload, 并且需è¦åœ¨parseCgi之å‰è°ƒç”¨ -* -*/ -class TC_Cgi -{ -public: - - /** - * @brief TC_Cgi构造函数 - */ - TC_Cgi(); - - /** - * @brief æžæž„函数 - */ - virtual ~TC_Cgi(); - - /** - * @brief 定义环境å˜é‡ - */ - enum - { - ENM_SERVER_SOFTWARE, - ENM_SERVER_NAME, - ENM_GATEWAY_INTERFACE, - ENM_SERVER_PROTOCOL, - ENM_SERVER_PORT, - ENM_REQUEST_METHOD, - ENM_PATH_INFO, - ENM_PATH_TRANSLATED, - ENM_SCRIPT_NAME, - ENM_HTTP_COOKIE, - ENM_QUERY_STRING, - ENM_REMOTE_HOST, - ENM_REMOTE_ADDR, - ENM_AUTH_TYPE, - ENM_REMOTE_USER, - ENM_REMOTE_IDENT, - ENM_CONTENT_TYPE, - ENM_CONTENT_LENGTH, - ENM_HTTP_USER_AGENT - }; - - /** - * @brief 设置上传文件. - * - * @param sUploadFilePrefix, 文件å‰ç¼€(包å«è·¯å¾„), 如果有文件上传,则文件ä¿å­˜åœ¨ä»¥è¯¥å‰ç¼€ä¸ºå称的路径下 - * 如果有多个文件上传,则文件å称以次在åŽé¢åŠ "_åºå·" - * @param iMaxUploadFiles 最多上传文件个数,<0:没有é™åˆ¶ - * @param iUploadMaxSize æ¯ä¸ªæ–‡ä»¶ä¸Šä¼ çš„最大大å°(字节) - */ - void setUpload(const string &sUploadFilePrefix, int iMaxUploadFiles = 5, size_t iUploadMaxSize = 1024*1024*10, size_t iMaxContentLength = 1024*1024*10); - - /** - * @brief 从标准输入解æžcgi. - */ - void parseCgi(); - - /** - * @brief 直接从http请求解æž. - * - * @param request http请求 - */ - void parseCgi(const TC_HttpRequest &request); - - /** - * @brief 获å–cgiçš„urlå‚æ•°multimap. - * - * @return multimapcgiçš„urlå‚æ•° - */ - const multimap &getParamMap() const; - - /** - * @brief 获å–cgi环境å˜é‡map. - * - * @return mapcgi的环境å˜é‡ - */ - map getEnvMap() const { return _env; } - - /** - * @brief 获å–cgiçš„å‚æ•°map, å°†multimap转æ¢æˆmap返回 - * , 对于一个å‚æ•°å称对应多个å‚数值的情况, åªå–其中一个值. - * - * @return map - */ - map getParamMapEx() const; - - /** - * @brief 获å–cookiesçš„å‚æ•°map. - * - * @return map - */ - const map &getCookiesMap() const; - - /** - * @brief 获å–cgiçš„æŸä¸ªå‚æ•°. - * - * @param sName å‚æ•°å称 - * @return - */ - string &operator[](const string &sName); - - /** - * @brief 获å–cgiçš„æŸä¸ªå‚æ•°. - * - * @param sName å‚æ•°å称 - * @return å‚数的值 - */ - string getValue(const string& sName) const; - - /** - * @brief 获å–æŸä¸€å称的å‚数的多个值. - * - * 用于解æžcheckbox这类控件的值( 一个å‚æ•°å,多个å‚数值) - * @param sName å‚æ•°å称 - * @param vtValue 该å称的å‚数值组æˆçš„vector - * @return vector, 该å称的å‚数值组æˆçš„vector - */ - const vector &getMultiValue(const string& sName, vector &vtValue) const; - - /** - * @brief 获å–cookie值. - * - * @param sName cookieå称 - * @return string类型的cookie值 - */ - string getCookie(const string& sName) const; - - /** - * @brief 设置cookie值. - * - * @param sName cookieå称 - * @param sValue cookie值 - * @param sExpires 过期日期 - * @param sPath cookie有效路径 - * @param sDomain cookie有效域 - * @param bSecure 是å¦å®‰å…¨(ssl时有效) - * @return 返回字符串,代表cookie值 - */ - string setCookie(const string &sName, const string &sValue, const string &sExpires="", const string &sPath="/", const string &sDomain = "", bool bSecure = false); - - /** - * @brief å‚数链表是å¦ä¸ºç©º. - * - * @return å‚数链表为空返回true,å¦åˆ™è¿”回false - */ - bool isParamEmpty() const; - - /** - * @brief å‚数是å¦å­˜åœ¨. - * - * @param sName å‚æ•°å称 - * @return 存在返回true,å¦åˆ™è¿”回false - */ - bool isParamExist(const string& sName) const; - - /** - * @brief 上传文件是å¦è¶…过大å°ï¼Œå¤šä¸ªæ–‡ä»¶ä¸Šä¼ æ—¶, - * åªè¦æœ‰ä¸€ä¸ªæ–‡ä»¶è¶…过大å°,则超过 - * @return 按照以上标准超过大å°çš„返回true,å¦åˆ™è¿”回false - */ - bool isUploadOverSize() const; - - /** - * @brief 上传文件是å¦è¶…过大å°,多个文件上传时, - * åªè¦æœ‰ä¸€ä¸ªæ–‡ä»¶è¶…过大å°,则超过 - * @param vtUploads 返回超过大å°çš„文件å称(æµè§ˆå™¨file控件的å称) - * @return 按照以上标准超过大å°çš„返回true,å¦åˆ™è¿”回false - */ - bool isUploadOverSize(vector &vtUploads) const; - - /** - * @brief 是å¦è¶…过上传文件个数. - * - * @return 超过上传个数返回true,å¦åˆ™è¿”回false - */ - bool isOverUploadFiles() const { return _bOverUploadFiles; } - - /** - * @brief 获å–上传文件个数. - * - * @return size_t上传文件的个数 - */ - size_t getUploadFilesCount() const; - - /** - * @brief 获å–ä¸Šä¼ æ–‡ä»¶çš„ç›¸å…³ä¿¡æ¯ - * - * @return map结构中, - * ä¿å­˜æ–‡ä»¶å和文件相关信æ¯çš„map - */ - const map &getUploadFilesMap() const; - - /** - * @brief 获å–环境å˜é‡. - * - * @param iEnv 枚举å˜é‡ - * @return 环境å˜é‡ - */ - string getCgiEnv(int iEnv); - - /** - * @brief 获å–环境å˜é‡. - * - * @param sEnv 环境å˜é‡å称 - * @return 环境å˜é‡çš„值 - */ - string getCgiEnv(const string& sEnv); - - /** - * @brief 设置环境å˜é‡. - * - * @param sName 环境å˜é‡å称 - * @param sValue 环境å˜é‡çš„值 - */ - void setCgiEnv(const string &sName, const string &sValue); - - /** - * @brief 返回html头,content-type . - * - * @param sHeader 缺çœå€¼ä¸º"text/html" - * @return - */ - static string htmlHeader(const string &sHeader = "text/html"); - - /** - * @brief http请求的url解ç , %åŽé¢çš„æ¢æˆå­—符. - * - * @param sUrl http请求url - * @return 解ç åŽçš„字符串 - */ - static string decodeURL(const string &sUrl); - - /** - * @brief 对url进行编ç , éžæ•°å­—和字æ¯ç”¨%XX代替. - * - * @param sUrl http请求url - * @return ç¼–ç åŽçš„url - */ - static string encodeURL(const string &sUrl); - - /** - * @brief 对æºå­—符串进行HTMLç¼–ç (<>"&) - * - * @param src æºå­—符串 - * @param blankEncode 是å¦å¯¹ç©ºæ ¼ä¹Ÿç¼–ç (空格, \t, \r\n, \n) - * @return HTMLç¼–ç åŽçš„字符串 - */ - static string encodeHTML(const string &src, bool blankEncode = false); - - /** - * @brief 对æºå­—符串进行XMLç¼–ç (<>"&'). - * - * @param src æºå­—符串 - * @return XMLç¼–ç åŽçš„字符串 - */ - static string encodeXML(const string &src); - -protected: - - /** - * @brief 申明,但是ä¸å®šä¹‰,ä¿è¯è¿™ä¸ªå‡½æ•°ä¸ä¼šè¢«ä½¿ç”¨ - */ - TC_Cgi &operator=(const TC_Cgi &tcCgi); - - /** - * @brief GET method. - * - * @param sBuffer GETçš„QueryString - * return - */ - void getGET(string &sBuffer); - - /** - * @brief POST method. - * - * @param sBuffer POSTçš„QueryString - * return - */ - void getPOST(string &sBuffer); - - /** - * @brief 解æžæ–‡ä»¶ä¸Šä¼ . - * - * @param mmpParams [out]输出å‚æ•°multimap - * return - */ - void parseUpload(multimap &mmpParams); - - /** - * @brief 解æžformæ•°æ® - */ - void parseFormData(multimap &mmpParams, const string &sBoundary); - - /** - * @brief 忽略空行 - */ - void ignoreLine(); - - /** - * @brief 写文件. - * - * @param sFileName 文件å称 - * @param sBuffer è¦å†™å…¥çš„内容 - */ - bool writeFile(FILE*fp, const string &sFileName, const string &sBuffer, size_t &iTotalWrite); - - /** - * @brief éžä¸Šä¼ æ¨¡å¼ä¸‹è§£æž. - * - * @param mmpParams [out]输出å‚æ•°multimap - * @param sBuffer [in]输入QueryString - * return - */ - void parseNormal(multimap &mmpParams, const string& sBuffer); - - /** - * @brief 解æžcookies. - * - * @param mpCooies [out]输出cookiesmap - * @param sBuffer [in]输入Cookies字符串 - * return - */ - void parseCookies(map &mpCooies, const string& sBuffer); - - /** - * @brief 控制解æžcgi input的基本æµç¨‹. - * - * @param mmpParams [out]输出å‚æ•° multimap - * @param mpCooies [out]输出cookies - * return - */ - void readCgiInput(multimap &mmpParams, map &mpCooies); - -protected: - - /** - * buffer - */ - string _buffer; - - /** - * æµ - */ - istringstream _iss; - - /** - * 读入 - */ - istream *_is; - - /** - * 环境å˜é‡ - */ - map _env; - - /** - * cgiå‚æ•° - */ - multimap _mmpParams; - - /** - * cookies - */ - map _mpCookies; - - /** - * 上传文件å称å‰ç¼€ - */ - string _sUploadFilePrefix; - - /** - * 上传文件的最大个数,<0:æ— é™åˆ¶ - */ - int _iMaxUploadFiles; - - /** - * ä¸Šä¼ æ–‡ä»¶çš„æœ€å¤§å¤§å° - */ - size_t _iUploadMaxSize; - - /** - * 是å¦è¶…过上传文件个数 - */ - bool _bOverUploadFiles; - - /** - * 最大的content-length - */ - size_t _iMaxContentLength; - - /** - * 是å¦è¶…过大å°,有一个文件超过则超过 - */ - bool _bUploadFileOverSize; - - /** - * 上传文件相关信æ¯ä¿å­˜åœ¨è¯¥map中 - */ - map _mpUpload; -}; - -} - -#endif diff --git a/cpp/util/include/util/tc_clientsocket.h b/cpp/util/include/util/tc_clientsocket.h deleted file mode 100644 index 1b5476ab7..000000000 --- a/cpp/util/include/util/tc_clientsocket.h +++ /dev/null @@ -1,662 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _TC_CLIENTSOCKET_H__ -#define _TC_CLIENTSOCKET_H__ - -#include "util/tc_socket.h" -#include -#include "util/tc_http.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_clientsocket.h - * @brief 客户端å‘包收包类. - */ -///////////////////////////////////////////////// -/** -* @brief 解æžendpoint异常类 -*/ -struct TC_EndpointParse_Exception : public TC_Exception -{ - TC_EndpointParse_Exception(const string &buffer) : TC_Exception(buffer){}; - ~TC_EndpointParse_Exception() throw() {}; -}; - -/** - * @brief 表示一个网络端å£,支æŒä»¥ä¸‹æ ¼å¼: - * - * 1:tcp -h 127.0.0.1 -p 2345 -t 10000 - * - * 2:tcp -h /tmp/sock.sock -p 0 -t 10000 - * - * 3:udp -h 127.0.0.1 -p 2345 -t 10000 - * - * -p 0:表示本地套接字 - * - * -q 0:表示qosçš„dscp值 - * - * 此时-h表示的文件路径 - */ -class TC_Endpoint -{ -public: - enum EType { UDP = 0, TCP = 1, SSL = 2 }; - /** - * - */ - TC_Endpoint(); - - /** - * @brief 构造函数 - * @param host - * @param port - * @param timeout, 超时时间, 毫秒 - * @param type, SOCK_STREAM或SOCK_DGRAM - */ - TC_Endpoint(const string& host, int port, int timeout, int type = 1, int grid = 0, int qos = 0, int weight = -1, unsigned int weighttype = 0, int authType = 0) - { - init(host, port, timeout, type, grid, qos, weight, weighttype, authType); - } - - /** - * @brief 用字符串æè¿°æ¥æž„造 - * @param desc - */ - TC_Endpoint(const string& desc) - { - parse(desc); - } - - /** - * @brief æ‹·è´æž„造 - * @param l - */ - TC_Endpoint(const TC_Endpoint& l) - { - _host = l._host; - _port = l._port; - _timeout= l._timeout; - _type = l._type; - _grid = l._grid; - _qos = l._qos; - _weight = l._weight; - _weighttype = l._weighttype; - _authType = l._authType; - } - - /** - * @brief 赋值函数 - * @param l - * - * @return TC_Endpoint& - */ - TC_Endpoint& operator = (const TC_Endpoint& l) - { - if(this != &l) - { - _host = l._host; - _port = l._port; - _timeout= l._timeout; - _type = l._type; - _grid = l._grid; - _qos = l._qos; - _weight = l._weight; - _weighttype = l._weighttype; - _authType = l._authType; - } - - return *this; - } - - /** - * == - * @param l - * - * @return bool - */ - bool operator == (const TC_Endpoint& l) - { - return (_host == l._host && _port == l._port && _timeout == l._timeout && _type == l._type && _grid == l._grid && _qos == l._qos && _weight == l._weight && _weighttype == l._weighttype && _authType == l._authType); - } - - /** - * @brief 设置ip - * @param str - */ - void setHost(const string& host) { _host = host; } - - /** - * @brief 获å–ip - * - * @return const string& - */ - string getHost() const { return _host; } - - /** - * @brief è®¾ç½®ç«¯å£ - * @param port - */ - void setPort(int port) { _port = port; } - - /** - * @brief 获å–ç«¯å£ - * - * @return int - */ - int getPort() const { return _port; } - - /** - * @brief 设置超时时间 - * @param timeout - */ - void setTimeout(int timeout) { _timeout = timeout; } - - /** - * @brief 获å–超时时间 - * - * @return int - */ - int getTimeout() const { return _timeout; } - - /** - * @brief 是å¦æ˜¯TCP, å¦åˆ™åˆ™ä¸ºUDP - * - * @return bool - */ - int isTcp() const { return _type == TCP || _type == SSL; } - /** - * @brief 是å¦æ˜¯SSL - * - * @return int - */ - int isSSL() const { return _type == SSL; } - - /** - * @brief 设置为TCP或UDP - * @param bTcp - */ - void setTcp(bool bTcp) { _type = bTcp; } - - /** - * @brief 设置为TCP/UDP/SSL - * @param type - */ - void setType(int type) { _type = type; } - /** - * @brief 获å–å议类型 - */ - int getType() const { return _type; } - /** - * @brief 获å–è·¯ç”±çŠ¶æ€ - * @param grid - */ - int getGrid() const { return _grid; } - - /** - * @brief è®¾ç½®è·¯ç”±çŠ¶æ€ - * @param grid - */ - void setGrid(int grid) { _grid = grid; } - - /** - * @brief 获å–è·¯ç”±çŠ¶æ€ - * @param grid - */ - int getQos() const { return _qos; } - - /** - * @brief è®¾ç½®è·¯ç”±çŠ¶æ€ - * @param grid - */ - void setQos(int qos) { _qos = qos; } - - /** - * @brief 获å–节点的é™æ€æƒé‡å€¼ - */ - int getWeight() const { return _weight; } - - /** - * @brief 设置节点的é™æ€æƒé‡å€¼ - * @param weight - */ - void setWeight(int weight) { _weight = weight; } - - /** - * @brief 获å–节点的æƒé‡ä½¿ç”¨æ–¹å¼ - */ - unsigned int getWeightType() const { return _weighttype; } - - /** - * @brief 设置节点的æƒé‡ä½¿ç”¨æ–¹å¼ - * @param weightway - */ - void setWeightType(unsigned int weighttype) { _weighttype = weighttype; } - - /** - * @brief 是å¦æ˜¯æœ¬åœ°å¥—接字 - * - * @return bool - */ - bool isUnixLocal() const { return _port == 0; } - - /** - * @brief 获å–认è¯ç±»åž‹ - */ - int getAuthType() const { return _authType; } - - /** - * @brief 设置认è¯ç±»åž‹ - */ - void setAuthType(int type) { _authType = type; } - - /** - * @brief 字符串æè¿° - * - * @return string - */ - string toString() - { - ostringstream os; - if (_type == TCP) - os << "tcp"; - else if (_type == UDP) - os << "udp"; - else - os << "ssl"; - - os << " -h " << _host << " -p " << _port << " -t " << _timeout; - if (_grid != 0) os << " -g " << _grid; - if (_qos != 0) os << " -q " << _qos; - if (_weight != -1) os << " -w " << _weight; - if (_weighttype != 0) os << " -v " << _weighttype; - if (_authType != 0) os << " -e " << _authType; - return os.str(); - } - - /** - * @brief 字符串形å¼çš„ç«¯å£ - * tcp:SOCK_STREAM - * - * udp:SOCK_DGRAM - * - * -h: ip - * - * -p: ç«¯å£ - * - * -t: 超时时间, 毫秒 - * - * -p å’Œ -tå¯ä»¥çœç•¥, -t默认10s - * - * tcp -h 127.0.0.1 -p 2345 -t 10000 - * - * @param desc - */ - void parse(const string &desc); - -private: - void init(const string& host, int port, int timeout, int istcp, int grid, int qos, int weight, unsigned int weighttype, int authType); - -protected: - /** - * ip - */ - std::string _host; - - /** - * ç«¯å£ - */ - int _port; - - /** - * 超时时间 - */ - int _timeout; - - /** - * 类型 - */ - int _type; - - /** - * è·¯ç”±çŠ¶æ€ - */ - int _grid; - - /** - * 网络Qosçš„dscp值 - */ - int _qos; - - /** - * 节点的é™æ€æƒé‡å€¼ - */ - int _weight; - - /** - * 节点的æƒé‡ä½¿ç”¨æ–¹å¼ - */ - unsigned int _weighttype; - /** - * 鉴æƒç±»åž‹ - */ - int _authType; -}; - -/*************************************TC_ClientSocket**************************************/ - -/** -* @brief 客户端socket相关æ“作基类 -*/ -class TC_ClientSocket -{ -public: - - /** - * @brief 构造函数 - */ - TC_ClientSocket() : _port(0),_timeout(3000) {} - - /** - * @brief æžå¤Ÿå‡½æ•° - */ - virtual ~TC_ClientSocket(){} - - /** - * @brief 构造函数 - * @param sIP æœåŠ¡å™¨IP - * @param iPort 端å£, port为0æ—¶:表示本地套接字此时ip为文件路径 - * @param iTimeout 超时时间, 毫秒 - */ - TC_ClientSocket(const string &sIp, int iPort, int iTimeout) { init(sIp, iPort, iTimeout); } - - /** - * @brief åˆå§‹åŒ–函数 - * @param sIP æœåŠ¡å™¨IP - * @param iPort 端å£, port为0æ—¶:表示本地套接字此时ip为文件路径 - * @param iTimeout 超时时间, 毫秒 - */ - void init(const string &sIp, int iPort, int iTimeout) - { - _socket.close(); - _ip = sIp; - _port = iPort; - _timeout = iTimeout; - } - - /** - * @brief å‘é€åˆ°æœåŠ¡å™¨ - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @return int 0 æˆåŠŸ,<0 失败 - */ - virtual int send(const char *sSendBuffer, size_t iSendLen) = 0; - - /** - * @brief 从æœåŠ¡å™¨è¿”回ä¸è¶…过iRecvLen的字节 - * @param sRecvBuffer 接收buffer - * @param iRecvLen 指定接收多少个字符æ‰è¿”回,输出接收数æ®çš„长度 - * @return int 0 æˆåŠŸ,<0 失败 - */ - virtual int recv(char *sRecvBuffer, size_t &iRecvLen) = 0; - - /** - * @brief 定义å‘é€çš„错误 - */ - enum - { - EM_SUCCESS = 0, /** EM_SUCCESS:å‘é€æˆåŠŸ*/ - EM_SEND = -1, /** EM_SEND:å‘é€é”™è¯¯*/ - EM_SELECT = -2, /** EM_SELECT:select 错误*/ - EM_TIMEOUT = -3, /** EM_TIMEOUT:select超时*/ - EM_RECV = -4, /** EM_RECV: 接å—错误*/ - EM_CLOSE = -5, /**EM_CLOSE: æœåŠ¡å™¨ä¸»åŠ¨å…³é—­*/ - EM_CONNECT = -6, /** EM_CONNECT : æœåŠ¡å™¨è¿žæŽ¥å¤±è´¥*/ - EM_SOCKET = -7 /**EM_SOCKET : SOCKETåˆå§‹åŒ–失败*/ - }; - -protected: - /** - * 套接字å¥æŸ„ - */ - TC_Socket _socket; - - /** - * ip或文件路径 - */ - string _ip; - - /** - * 端å£æˆ–-1:标示是本地套接字 - */ - int _port; - - /** - * 超时时间, 毫秒 - */ - int _timeout; -}; - -/** - * @brief TCP客户端Socket - * 多线程使用的时候,ä¸ç”¨å¤šçº¿ç¨‹åŒæ—¶send/recv,å°å¿ƒä¸²åŒ…ï¼› - */ -class TC_TCPClient : public TC_ClientSocket -{ -public: - /** - * @brief 构造函数 - */ - TC_TCPClient(){} - - /** - * @brief 构造函数 - * @param sIp æœåŠ¡å™¨Ip - * @param iPort ç«¯å£ - * @param iTimeout 超时时间, 毫秒 - */ - TC_TCPClient(const string &sIp, int iPort, int iTimeout) : TC_ClientSocket(sIp, iPort, iTimeout) - { - } - - /** - * @brief å‘é€åˆ°æœåŠ¡å™¨ - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int send(const char *sSendBuffer, size_t iSendLen); - - /** - * @brief 从æœåŠ¡å™¨è¿”回ä¸è¶…过iRecvLen的字节 - * @param sRecvBuffer 接收buffer - * @param iRecvLen 指定接收多少个字符æ‰è¿”回,输出接收数æ®çš„长度 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int recv(char *sRecvBuffer, size_t &iRecvLen); - - /** - * @brief 从æœåŠ¡å™¨ç›´åˆ°ç»“æŸç¬¦(注æ„必须是æœåŠ¡å™¨è¿”回的结æŸç¬¦, - * 而ä¸æ˜¯ä¸­é—´çš„ç¬¦å· ) åªèƒ½æ˜¯åŒæ­¥è°ƒç”¨ - * @param sRecvBuffer 接收buffer, 包å«åˆ†éš”符 - * @param sSep 分隔符 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int recvBySep(string &sRecvBuffer, const string &sSep); - - /** - * @brief 接收倒æœåŠ¡å™¨å…³é—­è¿žæŽ¥ä¸ºæ­¢ - * @param recvBuffer - * - * @return int 0 æˆåŠŸ,<0 失败 - */ - int recvAll(string &sRecvBuffer); - - /** - * @brief 从æœåŠ¡å™¨è¿”回iRecvLen的字节 - * @param sRecvBuffer, sRecvBufferçš„buffer长度必须大于等于iRecvLen - * @param iRecvLen - * @return int 0 æˆåŠŸ,<0 失败 - */ - int recvLength(char *sRecvBuffer, size_t iRecvLen); - - /** - * @brief å‘é€åˆ°æœåŠ¡å™¨, 从æœåŠ¡å™¨è¿”回ä¸è¶…过iRecvLen的字节 - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @param sRecvBuffer 接收buffer - * @param iRecvLen 接收buffer的长度指针[in/out], - * 输入时表示接收buffer的大å°,返回时表示接收了多少个字符 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int sendRecv(const char* sSendBuffer, size_t iSendLen, char *sRecvBuffer, size_t &iRecvLen); - - /** - * @brief å‘é€å€’æœåŠ¡å™¨, 并等待æœåŠ¡å™¨ç›´åˆ°ç»“尾字符, 包å«ç»“尾字符 - * sSep必须是æœåŠ¡å™¨è¿”回的结æŸç¬¦,而ä¸æ˜¯ä¸­é—´çš„符å·ï¼Œåªèƒ½æ˜¯åŒæ­¥è°ƒç”¨ - * (一次接收一定长度的buffer,如果末尾是sSep则返回, - * å¦åˆ™ç»§ç»­ç­‰å¾…接收) - * - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @param sRecvBuffer 接收buffer - * @param sSep 结尾字符 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int sendRecvBySep(const char* sSendBuffer, size_t iSendLen, string &sRecvBuffer, const string &sSep); - - /** - * @brief å‘é€å€’æœåŠ¡å™¨, 并等待æœåŠ¡å™¨ç›´åˆ°ç»“尾字符(\r\n), 包å«\r\n - * 注æ„必须是æœåŠ¡å™¨è¿”回的结æŸç¬¦,而ä¸æ˜¯ä¸­é—´çš„ç¬¦å· - * åªèƒ½æ˜¯åŒæ­¥è°ƒç”¨ - * (一次接收一定长度的buffer,如果末尾是\r\n则返回,å¦åˆ™ç»§ç»­ç­‰å¾…接收) - * - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @param sRecvBuffer 接收buffer - * @param sSep 结尾字符 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int sendRecvLine(const char* sSendBuffer, size_t iSendLen, string &sRecvBuffer); - - /** - * @brief å‘é€åˆ°æœåŠ¡å™¨, 接收直到æœåŠ¡å™¨å…³é—­è¿žæŽ¥ä¸ºæ­¢ - * 此时æœåŠ¡å™¨å…³é—­è¿žæŽ¥ä¸ä½œä¸ºé”™è¯¯ - * @param sSendBuffer - * @param iSendLen - * @param sRecvBuffer - * - * @return int - */ - int sendRecvAll(const char* sSendBuffer, size_t iSendLen, string &sRecvBuffer); - -protected: - /** - * @brief 获å–socket - * - * @return int - */ - int checkSocket(); -}; - -/*************************************TC_TCPClient**************************************/ - /** - * @brief 多线程使用的时候,ä¸ç”¨å¤šçº¿ç¨‹åŒæ—¶send/recv,å°å¿ƒä¸²åŒ… - */ -class TC_UDPClient : public TC_ClientSocket -{ -public: - /** - * @brief 构造函数 - */ - TC_UDPClient(){}; - - /** - * @brief 构造函数 - * @param sIp æœåŠ¡å™¨IP - * @param iPort ç«¯å£ - * @param iTimeout 超时时间, 毫秒 - */ - TC_UDPClient(const string &sIp, int iPort, int iTimeout) : TC_ClientSocket(sIp, iPort, iTimeout) - { - } - - /** - * @brief å‘é€æ•°æ® - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * - * @return int 0 æˆåŠŸ,<0 失败 - */ - int send(const char *sSendBuffer, size_t iSendLen); - - /** - * @brief æŽ¥æ”¶æ•°æ® - * @param sRecvBuffer 接收buffer - * @param iRecvLen 输入/输出字段 - * @return int 0 æˆåŠŸ,<0 失败 - */ - int recv(char *sRecvBuffer, size_t &iRecvLen); - - /** - * @brief 接收数æ®, 并返回远程的端å£å’Œip - * @param sRecvBuffer 接收buffer - * @param iRecvLen 输入/输出字段 - * @param sRemoteIp 输出字段, 远程的ip - * @param iRemotePort 输出字段, è¿œç¨‹ç«¯å£ - * - * @return int 0 æˆåŠŸ,<0 失败 - */ - int recv(char *sRecvBuffer, size_t &iRecvLen, string &sRemoteIp, uint16_t &iRemotePort); - - /** - * @brief å‘é€å¹¶æŽ¥æ”¶æ•°æ® - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @param sRecvBuffer 输入/输出字段 - * @param iRecvLen 输入/输出字段 - * - * @return int 0 æˆåŠŸ,<0 失败 - */ - int sendRecv(const char *sSendBuffer, size_t iSendLen, char *sRecvBuffer, size_t &iRecvLen); - - /** - * @brief å‘é€å¹¶æŽ¥æ”¶æ•°æ®, åŒæ—¶èŽ·å–远程的ipå’Œç«¯å£ - * @param sSendBuffer å‘é€buffer - * @param iSendLen å‘é€buffer的长度 - * @param sRecvBuffer 输入/输出字段 - * @param iRecvLen 输入/输出字段 - * @param sRemoteIp 输出字段, 远程的ip - * @param iRemotePort 输出字段, è¿œç¨‹ç«¯å£ - * - * @return int 0 æˆåŠŸ,<0 失败 - */ - int sendRecv(const char *sSendBuffer, size_t iSendLen, char *sRecvBuffer, size_t &iRecvLen, string &sRemoteIp, uint16_t &iRemotePort); - -protected: - /** - * @brief 获å–socket - * - * @return TC_Socket& - */ - int checkSocket(); -}; - -} - -#endif diff --git a/cpp/util/include/util/tc_common.h b/cpp/util/include/util/tc_common.h deleted file mode 100644 index 29375c918..000000000 --- a/cpp/util/include/util/tc_common.h +++ /dev/null @@ -1,785 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_COMMON_H -#define __TC_COMMON_H - -#ifndef __USE_XOPEN -#define __USE_XOPEN -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_common.h -* @brief 帮助类,都是é™æ€å‡½æ•°,æ供一些常用的函数 . -* -*/ -///////////////////////////////////////////////// - - /** - * @brief 基础工具类,æ供了一些éžå¸¸åŸºæœ¬çš„函数使用. - * - * 这些函数都是以é™æ€å‡½æ•°æ供。 包括以下几ç§å‡½æ•°: - * - * Trim类函数,大å°å†™è½¬æ¢å‡½æ•°,分隔字符串函数(直接分隔字符串, - * - * 数字等),时间相关函数,字符串转æ¢å‡½æ•°,二进制字符串互转函数, - * - * 替æ¢å­—符串函数,Ip匹é…函数,判断一个数是å¦æ˜¯ç´ æ•°ç­‰ - */ -class TC_Common -{ -public: - - /** - * @brief 去掉头部以åŠå°¾éƒ¨çš„字符或字符串. - * - * @param sStr 输入字符串 - * @param s 需è¦åŽ»æŽ‰çš„字符 - * @param bChar 如果为true, 则去掉s中æ¯ä¸ªå­—符; 如果为false, 则去掉s字符串 - * @return string 返回去掉åŽçš„字符串 - */ - static string trim(const string &sStr, const string &s = " \r\n\t", bool bChar = true); - - /** - * @brief 去掉左边的字符或字符串. - * - * @param sStr 输入字符串 - * @param s 需è¦åŽ»æŽ‰çš„字符 - * @param bChar 如果为true, 则去掉s中æ¯ä¸ªå­—符; 如果为false, 则去掉s字符串 - * @return string 返回去掉åŽçš„字符串 - */ - static string trimleft(const string &sStr, const string &s = " \r\n\t", bool bChar = true); - - /** - * @brief 去掉å³è¾¹çš„字符或字符串. - * - * @param sStr 输入字符串 - * @param s 需è¦åŽ»æŽ‰çš„字符 - * @param bChar 如果为true, 则去掉s中æ¯ä¸ªå­—符; 如果为false, 则去掉s字符串 - * @return string 返回去掉åŽçš„字符串 - */ - static string trimright(const string &sStr, const string &s = " \r\n\t", bool bChar = true); - - /** - * @brief 字符串转æ¢æˆå°å†™. - * - * @param sString 字符串 - * @return string 转æ¢åŽçš„字符串 - */ - static string lower(const string &sString); - - /** - * @brief 字符串转æ¢æˆå¤§å†™. - * - * @param sString 字符串 - * @return string 转æ¢åŽçš„大写的字符串 - */ - static string upper(const string &sString); - - /** - * @brief 字符串是å¦éƒ½æ˜¯æ•°å­—çš„. - * - * @param sString 字符串 - * @return bool 是å¦æ˜¯æ•°å­— - */ - static bool isdigit(const string &sInput); - - /** - * @brief 字符串转æ¢æˆæ—¶é—´ç»“æž„. - * - * @param sString å­—ç¬¦ä¸²æ—¶é—´æ ¼å¼ - * @param sFormat æ ¼å¼ - * @param stTm 时间结构 - * @return 0: æˆåŠŸ, -1:失败 - */ - static int str2tm(const string &sString, const string &sFormat, struct tm &stTm); - - /** - * @brief GMTæ ¼å¼çš„时间转化为时间结构 - * - * eg.Sat, 06 Feb 2010 09:29:29 GMT, %a, %d %b %Y %H:%M:%S GMT - * - * å¯ä»¥ç”¨mktimeæ¢æˆtime_t, 但是注æ„时区 å¯ä»¥ç”¨mktime(&stTm) - * - * - timezoneæ¢æˆæœ¬åœ°çš„秒(time(NULL)值相åŒ) timezone是系统的 , - * - * 需è¦extern long timezone; - * - * @param sString GMTæ ¼å¼çš„时间 - * @param stTm 转æ¢åŽçš„时间结构 - * @return 0: æˆåŠŸ, -1:失败 - */ - static int strgmt2tm(const string &sString, struct tm &stTm); - - /** - * @brief 时间转æ¢æˆå­—符串. - * - * @param stTm 时间结构 - * @param sFormat 需è¦è½¬æ¢çš„目标格å¼ï¼Œé»˜è®¤ä¸ºç´§å‡‘æ ¼å¼ - * @return string 转æ¢åŽçš„时间字符串 - */ - static string tm2str(const struct tm &stTm, const string &sFormat = "%Y%m%d%H%M%S"); - - /** - * @brief 时间转æ¢æˆå­—符串. - * - * @param t 时间结构 - * @param sFormat 需è¦è½¬æ¢çš„目标格å¼ï¼Œé»˜è®¤ä¸ºç´§å‡‘æ ¼å¼ - * @return string 转æ¢åŽçš„时间字符串 - */ - static string tm2str(const time_t &t, const string &sFormat = "%Y%m%d%H%M%S"); - - /** - * @brief 当å‰æ—¶é—´è½¬æ¢æˆç´§å‡‘æ ¼å¼å­—符串 - * @param sFormat æ ¼å¼ï¼Œé»˜è®¤ä¸ºç´§å‡‘æ ¼å¼ - * @return string 转æ¢åŽçš„时间字符串 - */ - static string now2str(const string &sFormat = "%Y%m%d%H%M%S"); - - /** - * @brief 时间转æ¢æˆGMT字符串,GMTæ ¼å¼:Fri, 12 Jan 2001 18:18:18 GMT - * @param stTm 时间结构 - * @return string GMTæ ¼å¼çš„时间字符串 - */ - static string tm2GMTstr(const struct tm &stTm); - - /** - * @brief 时间转æ¢æˆGMT字符串,GMTæ ¼å¼:Fri, 12 Jan 2001 18:18:18 GMT - * @param stTm 时间结构 - * @return string GMTæ ¼å¼çš„时间字符串 - */ - static string tm2GMTstr(const time_t &t); - - /** - * @brief 当å‰æ—¶é—´è½¬æ¢æˆGMT字符串,GMTæ ¼å¼:Fri, 12 Jan 2001 18:18:18 GMT - * @return string GMTæ ¼å¼çš„时间字符串 - */ - static string now2GMTstr(); - - /** - * @brief 当å‰çš„日期(年月日)转æ¢æˆå­—符串(%Y%m%d). - * - * @return string (%Y%m%d)æ ¼å¼çš„时间字符串 - */ - static string nowdate2str(); - - /** - * @brief 当å‰çš„时间(时分秒)转æ¢æˆå­—符串(%H%M%S). - * - * @return string (%H%M%S)æ ¼å¼çš„时间字符串 - */ - static string nowtime2str(); - - /** - * @brief 获å–当å‰æ—¶é—´çš„的毫秒数. - * - * @return int64_t 当å‰æ—¶é—´çš„的毫秒数 - */ - static int64_t now2ms(); - - /** - * @brief å–出当å‰æ—¶é—´çš„微秒. - * - * @return int64_t å–出当å‰æ—¶é—´çš„微秒 - */ - static int64_t now2us(); - - /** - * @brief 字符串转化æˆT型,如果T是数值类型, 如果str为空,则T为0. - * - * @param sStr è¦è½¬æ¢çš„字符串 - * @return T T型类型 - */ - template - static T strto(const string &sStr); - - /** - * @brief 字符串转化æˆTåž‹. - * - * @param sStr è¦è½¬æ¢çš„字符串 - * @param sDefault 缺çœå€¼ - * @return T 转æ¢åŽçš„T类型 - */ - template - static T strto(const string &sStr, const string &sDefault); - - /** - * @brief 解æžå­—符串,用分隔符å·åˆ†éš”,ä¿å­˜åœ¨vector里 - * - * 例å­: |a|b||c| - * - * 如果withEmpty=trueæ—¶, 采用|分隔为:"","a", "b", "", "c", "" - * - * 如果withEmpty=falseæ—¶, 采用|分隔为:"a", "b", "c" - * - * 如果T类型为int等数值类型, 则分隔的字符串为"", 则强制转化为0 - * - * @param sStr 输入字符串 - * @param sSep 分隔字符串(æ¯ä¸ªå­—符都算为分隔符) - * @param withEmpty true代表空的也算一个元素, false时空的过滤 - * @return 解æžåŽçš„字符vector - */ - template - static vector sepstr(const string &sStr, const string &sSep, bool withEmpty = false); - - /** - * @brief T型转æ¢æˆå­—符串,åªè¦T能够使用ostream对象用<<é‡è½½,å³å¯ä»¥è¢«è¯¥å‡½æ•°æ”¯æŒ - * @param t è¦è½¬æ¢çš„æ•°æ® - * @return 转æ¢åŽçš„字符串 - */ - template - static string tostr(const T &t); - - /** - * @brief vector转æ¢æˆstring. - * - * @param t è¦è½¬æ¢çš„vectoråž‹çš„æ•°æ® - * @return 转æ¢åŽçš„字符串 - */ - template - static string tostr(const vector &t); - - /** - * @brief 把map输出为字符串. - * - * @param map è¦è½¬æ¢çš„map对象 - * @return string 输出的字符串 - */ - template - static string tostr(const map &t); - - /** - * @brief map输出为字符串. - * - * @param multimap map对象 - * @return 输出的字符串 - */ - template - static string tostr(const multimap &t); - - /** - * @brief pair 转化为字符串,ä¿è¯map等关系容器å¯ä»¥ç›´æŽ¥ç”¨tostræ¥è¾“出 - * @param pair pair对象 - * @return 输出的字符串 - */ - template - static string tostr(const pair &itPair); - - /** - * @brief container 转æ¢æˆå­—符串. - * - * @param iFirst 迭代器 - * @param iLast 迭代器 - * @param sSep 两个元素之间的分隔符 - * @return 转æ¢åŽçš„字符串 - */ - template - static string tostr(InputIter iFirst, InputIter iLast, const string &sSep = "|"); - - /** - * @brief 二进制数æ®è½¬æ¢æˆå­—符串. - * - * @param buf 二进制buffer - * @param len buffer长度 - * @param sSep 分隔符 - * @param lines 多少个字节æ¢ä¸€è¡Œ, 默认0表示ä¸æ¢è¡Œ - * @return 转æ¢åŽçš„字符串 - */ - static string bin2str(const void *buf, size_t len, const string &sSep = "", size_t lines = 0); - - /** - * @brief 二进制数æ®è½¬æ¢æˆå­—符串. - * - * @param sBinData äºŒè¿›åˆ¶æ•°æ® - * @param sSep 分隔符 - * @param lines 多少个字节æ¢ä¸€è¡Œ, 默认0表示ä¸æ¢è¡Œ - * @return 转æ¢åŽçš„字符串 - */ - static string bin2str(const string &sBinData, const string &sSep = "", size_t lines = 0); - - /** - * @brief 字符串转æ¢æˆäºŒè¿›åˆ¶. - * - * @param psAsciiData 字符串 - * @param sBinData äºŒè¿›åˆ¶æ•°æ® - * @param iBinSize 需è¦è½¬æ¢çš„字符串长度 - * @return 转æ¢é•¿åº¦ï¼Œå°äºŽç­‰äºŽ0则表示失败 - */ - static int str2bin(const char *psAsciiData, unsigned char *sBinData, int iBinSize); - - /** - * @brief 字符串转æ¢æˆäºŒè¿›åˆ¶. - * - * @param sBinData è¦è½¬æ¢çš„字符串 - * @param sSep 分隔符 - * @param lines 多少个字节æ¢ä¸€è¡Œ, 默认0表示ä¸æ¢è¡Œ - * @return 转æ¢åŽçš„äºŒè¿›åˆ¶æ•°æ® - */ - static string str2bin(const string &sBinData, const string &sSep = "", size_t lines = 0); - - /** - * @brief 替æ¢å­—符串. - * - * @param sString 输入字符串 - * @param sSrc 原字符串 - * @param sDest 目的字符串 - * @return string 替æ¢åŽçš„字符串 - */ - static string replace(const string &sString, const string &sSrc, const string &sDest); - - /** - * @brief 批é‡æ›¿æ¢å­—符串. - * - * @param sString 输入字符串 - * @param mSrcDest map<原字符串,目的字符串> - * @return string 替æ¢åŽçš„字符串 - */ - static string replace(const string &sString, const map& mSrcDest); - - /** - * @brief 匹é…以.分隔的字符串,pat中*则代表通é…符,代表éžç©ºçš„任何字符串 - * s为空, 返回false ,pat为空, 返回true - * @param s 普通字符串 - * @param pat 带*则被匹é…的字符串,用æ¥åŒ¹é…ipåœ°å€ - * @return 是å¦åŒ¹é…æˆåŠŸ - */ - static bool matchPeriod(const string& s, const string& pat); - - /** - * @brief 匹é…以.分隔的字符串. - * - * @param s 普通字符串 - * @param pat vector中的æ¯ä¸ªå…ƒç´ éƒ½æ˜¯å¸¦*则被匹é…的字符串,用æ¥åŒ¹é…ipåœ°å€ - * @return 是å¦åŒ¹é…æˆåŠŸ - */ - static bool matchPeriod(const string& s, const vector& pat); - - /** - * @brief 判断一个数是å¦ä¸ºç´ æ•°. - * - * @param n 需è¦è¢«åˆ¤æ–­çš„æ•°æ® - * @return true代表是素数,false表示éžç´ æ•° - */ - static bool isPrimeNumber(size_t n); - - /** - * @brief daemon - */ - static void daemon(); - - /** - * @brief 忽略管é“异常 - */ - static void ignorePipe(); - - /** - * @brief 将一个string类型转æˆä¸€ä¸ªå­—节 . - * - * @param sWhat è¦è½¬æ¢çš„字符串 - * @return char 转æ¢åŽçš„字节 - */ - static char x2c(const string &sWhat); - - /** - * @brief 大å°å­—符串æ¢æˆå­—节数,支æŒK, M, Gä¸¤ç§ ä¾‹å¦‚: 1K, 3M, 4G, 4.5M, 2.3G - * @param s è¦è½¬æ¢çš„字符串 - * @param iDefaultSize æ ¼å¼é”™è¯¯æ—¶, 缺çœçš„å¤§å° - * @return 字节数 - */ - static size_t toSize(const string &s, size_t iDefaultSize); - - /** - * @brief 生æˆåŸºäºŽ16进制字符的éšæœºä¸² - * @param p 存储éšæœºå­—符串 - * @param len å­—ç¬¦ä¸²å¤§å° - */ - static void getRandomHexChars(char* p, unsigned int len); -}; - -namespace p -{ - template - struct strto1 - { - D operator()(const string &sStr) - { - string s = "0"; - - if(!sStr.empty()) - { - s = sStr; - } - - istringstream sBuffer(s); - - D t; - sBuffer >> t; - - return t; - } - }; - - template<> - struct strto1 - { - char operator()(const string &sStr) - { - if(!sStr.empty()) - { - return sStr[0]; - } - return 0; - } - }; - - template<> - struct strto1 - { - short operator()(const string &sStr) - { - if(!sStr.empty()) - { - return atoi(sStr.c_str()); - } - return 0; - } - }; - - template<> - struct strto1 - { - unsigned short operator()(const string &sStr) - { - if(!sStr.empty()) - { - return strtoul(sStr.c_str(), NULL, 10); - } - return 0; - } - }; - - template<> - struct strto1 - { - int operator()(const string &sStr) - { - if(!sStr.empty()) - { - return atoi(sStr.c_str()); - } - return 0; - } - }; - - template<> - struct strto1 - { - unsigned int operator()(const string &sStr) - { - if(!sStr.empty()) - { - return strtoul(sStr.c_str(), NULL, 10); - } - return 0; - } - }; - - template<> - struct strto1 - { - long operator()(const string &sStr) - { - if(!sStr.empty()) - { - return atol(sStr.c_str()); - } - return 0; - } - }; - - template<> - struct strto1 - { - long long operator()(const string &sStr) - { - if(!sStr.empty()) - { - return atoll(sStr.c_str()); - } - return 0; - } - }; - - template<> - struct strto1 - { - unsigned long operator()(const string &sStr) - { - if(!sStr.empty()) - { - return strtoul(sStr.c_str(), NULL, 10); - } - return 0; - } - }; - - template<> - struct strto1 - { - float operator()(const string &sStr) - { - if(!sStr.empty()) - { - return atof(sStr.c_str()); - } - return 0; - } - }; - - template<> - struct strto1 - { - double operator()(const string &sStr) - { - if(!sStr.empty()) - { - return atof(sStr.c_str()); - } - return 0; - } - }; - - template - struct strto2 - { - D operator()(const string &sStr) - { - istringstream sBuffer(sStr); - - D t; - sBuffer >> t; - - return t; - } - }; - - template<> - struct strto2 - { - string operator()(const string &sStr) - { - return sStr; - } - }; - -} - -template -T TC_Common::strto(const string &sStr) -{ - using strto_type = typename std::conditional::value, p::strto1, p::strto2>::type; - - return strto_type()(sStr); -} - -template -T TC_Common::strto(const string &sStr, const string &sDefault) -{ - string s; - - if(!sStr.empty()) - { - s = sStr; - } - else - { - s = sDefault; - } - - return strto(s); -} - - -template -vector TC_Common::sepstr(const string &sStr, const string &sSep, bool withEmpty) -{ - vector vt; - - string::size_type pos = 0; - string::size_type pos1 = 0; - - while(true) - { - string s; - pos1 = sStr.find_first_of(sSep, pos); - if(pos1 == string::npos) - { - if(pos + 1 <= sStr.length()) - { - s = sStr.substr(pos); - } - } - else if(pos1 == pos) - { - s = ""; - } - else - { - s = sStr.substr(pos, pos1 - pos); - pos = pos1; - } - - if(withEmpty) - { - vt.push_back(strto(s)); - } - else - { - if(!s.empty()) - { - T tmp = strto(s); - vt.push_back(tmp); - } - } - - if(pos1 == string::npos) - { - break; - } - - pos++; - } - - return vt; -} -template -string TC_Common::tostr(const T &t) -{ - ostringstream sBuffer; - sBuffer << t; - return sBuffer.str(); -} - -template -string TC_Common::tostr(const vector &t) -{ - string s; - for(size_t i = 0; i < t.size(); i++) - { - s += tostr(t[i]); - s += " "; - } - return s; -} - -template -string TC_Common::tostr(const map &t) -{ - string sBuffer; - typename map::const_iterator it = t.begin(); - while(it != t.end()) - { - sBuffer += " ["; - sBuffer += tostr(it->first); - sBuffer += "]=["; - sBuffer += tostr(it->second); - sBuffer += "] "; - ++it; - } - return sBuffer; -} - -template -string TC_Common::tostr(const multimap &t) -{ - string sBuffer; - typename multimap::const_iterator it = t.begin(); - while(it != t.end()) - { - sBuffer += " ["; - sBuffer += tostr(it->first); - sBuffer += "]=["; - sBuffer += tostr(it->second); - sBuffer += "] "; - ++it; - } - return sBuffer; -} - -template -string TC_Common::tostr(const pair &itPair) -{ - string sBuffer; - sBuffer += "["; - sBuffer += tostr(itPair.first); - sBuffer += "]=["; - sBuffer += tostr(itPair.second); - sBuffer += "]"; - return sBuffer; -} - -template -string TC_Common::tostr(InputIter iFirst, InputIter iLast, const string &sSep) -{ - string sBuffer; - InputIter it = iFirst; - - while(it != iLast) - { - sBuffer += tostr(*it); - ++it; - - if(it != iLast) - { - sBuffer += sSep; - } - else - { - break; - } - } - - return sBuffer; -} - -} -#endif - diff --git a/cpp/util/include/util/tc_config.h b/cpp/util/include/util/tc_config.h deleted file mode 100644 index 44a8f7872..000000000 --- a/cpp/util/include/util/tc_config.h +++ /dev/null @@ -1,534 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_CONFIG_H_ -#define __TC_CONFIG_H_ - -#include -#include -#include -#include -#include "util/tc_ex.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_config.h - * @brief é…置文件读å–ç±». - */ -///////////////////////////////////////////////// - -/** -* 域分隔符 -*/ -const char TC_CONFIG_DOMAIN_SEP = '/'; - -/** -* å‚数开始符 -*/ -const char TC_CONFIG_PARAM_BEGIN = '<'; - -/** -* å‚数结æŸç¬¦ -*/ -const char TC_CONFIG_PARAM_END = '>'; - -/** -* @brief é…置文件异常类. -*/ -struct TC_Config_Exception : public TC_Exception -{ - TC_Config_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_Config_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_Config_Exception() throw(){}; -}; - -/** - * @brief é…置没有文件错误 - */ -struct TC_ConfigNoParam_Exception : public TC_Exception -{ - TC_ConfigNoParam_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_ConfigNoParam_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_ConfigNoParam_Exception() throw(){}; -}; - - -/** -* @brief 定义é…置文件中的域的类. -*/ -class TC_ConfigDomain -{ -public: - friend class TC_Config; - - /** - * @brief 构造函数. - * - * @param sLine 指é…ç½®æ–‡ä»¶ä¸­çš„ä¸€è¡Œï¼ŒæŒ‰è¡Œè¯»å– - */ - TC_ConfigDomain(const string &sLine); - - /** - * @brief æžå¤Ÿå‡½æ•°. - */ - ~TC_ConfigDomain(); - - /** - * @brief æ‹·è´å¤åˆ¶. - * - * @param tcd 一个TC_ConfigDomain对象,指é…置文件的一个域 - */ - TC_ConfigDomain(const TC_ConfigDomain &tcd); - - /** - * @brief 赋值. - * - * @param tcd 一个TC_ConfigDomain对象,指é…置文件的一个域 - */ - TC_ConfigDomain& operator=(const TC_ConfigDomain &tcd); - - - struct DomainPath - { - vector _domains; - string _param; - }; - - /** - * @brief 解æžä¸€ä¸ªdomain - * 对形如"/Main/Domain"çš„path进行解æžï¼Œè§£æžçš„结果为一个 - * DomainPath 类型,包括路径_domains和路径中对应的é…置项_param. - * - * @param path 一个ç»è¿‡å¤„ç†çš„字符串,必须符åˆä¸€å®šçš„è¦æ±‚ - * @param bWithParam "/Main/Domain"æ—¶bWithParam为ture - * "/Main/Domain"æ—¶bWithParam为false - * @return DomainPath 一个DomainPath对象,解æžå‡ºåŸŸä¸­çš„域åå’Œå‚æ•° - */ - static DomainPath parseDomainName(const string& path, bool bWithParam); - - /** - * @brief 增加å­åŸŸå称 - * 增加一个å­åŸŸï¼Œname是å­åŸŸçš„å字,比如"/Main/Domain" - * å†å¢žåŠ ä¸€ä¸ªname为subDomainçš„å­åŸŸ,å˜æˆ"/Main/Domain/subDomain" - * - * @param name å­åŸŸçš„å称 - * @return TC_ConfigDomain* 指å‘å­åŸŸçš„指针 - * @throws TC_Config_Exception - */ - TC_ConfigDomain* addSubDomain(const string& name); - - /** - * @brief 递归æœç´¢å­åŸŸ Sub Domain. - * - * @param itBegin 迭代器 ,指å‘_domains的顶部 - * @param itEnd 迭代器 ,指å‘_domains的底部 - * @return TC_ConfigDomain* 指å‘目标å­åŸŸçš„指针 - */ - TC_ConfigDomain *getSubTcConfigDomain(vector::const_iterator itBegin, vector::const_iterator itEnd); - const TC_ConfigDomain *getSubTcConfigDomain(vector::const_iterator itBegin, vector::const_iterator itEnd) const; - - /** - * @brief Get Param Value 获å–å‚æ•°/值对. - * - * @param sName é…置项å称 - * @return é…置项对应的值 - */ - string getParamValue(const string &sName) const; - - /** - * @brief Get Param Map 获å–map - * map是å‚æ•°/值对,是é…置项和é…置项的值对应的键值对, - * eg. SyncThreadNum = 2 - * @return 一个域中的å‚数和其对应的值的map - */ - const map& getParamMap() const { return _param; } - - /** - * @brief Get Domain Map 获å–map. - * - * @return map - */ - const map& getDomainMap() const { return _subdomain; } - - /** - * @brief Set Param Value 设置å‚æ•°/值对. - * - * @param sLine è¡Œ - * @return - */ - void setParamValue(const string &sLine); - - /** - * @brief æ’å…¥å‚数,把key里没有的é…ç½®é¡¹æ·»åŠ åˆ°æœ€åŽ - * - * @param å­˜é…置项和对应é…置项值的map - */ - void insertParamValue(const map &m); - - /** - * @brief Destroy 释放. - * - * @return æ—  - */ - void destroy(); - - /** - * @brief Get Name 获å–域å称 - * @return 域å称 - */ - string getName() const; - - /** - * @brief 设置域å称 - * - * @param 域å称 - */ - void setName(const string& name); - - /** - * @brief 按照文件中的顺åºèŽ·å–key - * - * @return vector存放keyçš„vector - */ - vector getKey() const; - - /** - * @brief 按照文件中的顺åºèŽ·å–line - * - * @return vector存放keyçš„vector - */ - vector getLine() const; - - /** - * @brief 按照文件中的顺åºèŽ·å–å­Domain - * - * @return vector存放域åå­—çš„vector - */ - vector getSubDomain() const; - - /** - * @brief 转æ¢æˆé…置文件的字符串格å¼. - * - * @param i tab的层数 - * @return string类型é…置字符串 - */ - string tostr(int i) const; - - /** - * @brief 克隆. - * - * @return TC_ConfigDomain* - */ - TC_ConfigDomain* clone() const - { - return new TC_ConfigDomain(*this); - } - -protected: - /** - * @brief 转义. - * - * @param name - * @return string - */ - static string parse(const string& s); - - /** - * @brief æ–¹å‘转义. - * - * @param s - * @return string - */ - static string reverse_parse(const string &s); - - /** - * @brief 设置å‚æ•°/值对 - * - * @param sName é…置项å称 - * @param sValue é…置项的值 - * @return æ—  - */ - void setParamValue(const string &sName, const string &sValue); - -protected: - - /** - * 域å称 - */ - string _name; - - /** - * name/value对,é…置项的å称和é…置项的值 - */ - map _param; - - /** - * key也就是é…置项的æ’å…¥é¡ºåº - */ - vector _key; - - /** - * å­åŸŸ - */ - map _subdomain; - - /** - * 域的æ’å…¥é¡ºåº - */ - vector _domain; - - /** - * 整行的é…置列表 - */ - vector _line; -}; - -/** - * @brief 定义é…置文件的类(兼容wbl模å¼ï¼‰. - - * 支æŒä»Žstring中解æžé…置文件; - - * 支æŒç”Ÿæˆé…置文件; - - * 解æžå‡ºé”™æŠ›å‡ºå¼‚常; - - * 采用[]获å–é…置,如果无é…置则抛出异常; - - * 采用get获å–é…置,ä¸å­˜åœ¨åˆ™è¿”回空; - - * 读å–é…置文件是线程安全的,insert域等函数éžçº¿ç¨‹å®‰å…¨ï¼› - - * 例如: - - *
- - * - - * Name = Value - - * - - * - - * Name = Value - - * - - *
- - * 获å–å‚æ•°:conf["/Main/Domain"] 获å–域Map: - - * getDomainMap("/Main/Domain", m); m得到Name/Value对 - - * 获å–域Vector: getDomainVector("/Main", v); v得到Domain列表 - - å¯ä»¥å¢žåŠ åŸŸæˆ–域下é¢çš„值对 - - */ -class TC_Config -{ -public: - - /** - * @brief 构造函数 - */ - TC_Config(); - - /** - * @brief æ‹·è´å¤åˆ¶. - * - * @param tc为TC_Config类型 - */ - TC_Config(const TC_Config &tc); - - /** - * @brief 赋值. - * - * @param tcd - * @return TC_Config& - */ - TC_Config& operator=(const TC_Config &tc); - - /** - * @brief 解æžæ–‡ä»¶. - * - * 把fileName对应的文件转化为输入æµåŽè°ƒç”¨parseå¯¹å…¶è¿›è¡Œè§£æž - * @param sFileName : 文件å称 - * @return æ—  - * @throws TC_Config_Exception - */ - void parseFile(const string& sFileName); - - /** - * @brief 解æžå­—符串. - * - * 把string 类型先转化为输入æµåŽè°ƒç”¨parseå¯¹å…¶è¿›è¡Œè§£æž - * @return void - * @throws TC_Config_Exception - */ - void parseString(const string& buffer); - - /** - * @brief 获å–值, 如果没有则抛出异常, - * 对于型如/Main/Domain的字符串,获å–域/Main/Domain下的é…置项å字为Param的值 - * @param sName å‚æ•°å称,例如: /Main/Domain - * @return é…置项对应的值 - * @throws TC_Config_Exception - */ - string operator[](const string &sName); - - /** - * @brief 获å–值, 注æ„如果没有ä¸æŠ›å‡ºå¼‚常,返回空字符串. - * 对于型如/Main/Domain的字符串,获å–域/Main/Domain下的é…置项å字为Param的值 - * @param sName å‚æ•°å称, 例如: /Main/Domain - * @return é…置项对应的值 - */ - string get(const string &sName, const string &sDefault="") const; - - /** - * @brief GetDomainParamMap获å–域下é¢çš„å‚数值对. - * - * @param path 域å称, 域标识, 例如: /Main/Domain - * @param m map类型,域内的map列表 - * @return bool, 返回域下é¢çš„å‚数值对 - */ - bool getDomainMap(const string &path, map &m) const; - - /** - * @brief 获å–域下é¢çš„å‚数值对,ä¸å­˜åˆ™è¿”回空map. - * - * @param path 域å称, 域标识, 例如: /Main/Domain - * @return map,对应域下é¢çš„所有å‚数值对 - */ - map getDomainMap(const string &path) const; - - /** - * @brief 获å–域下é¢çš„所有key, 按照文件的行顺åºè¿”回 - * @param path 域å称, 域标识, 例如: /Main/Domain - * @return vector,æŸä¸ªæ ‡è¯†ä¸‹çš„所有key - */ - vector getDomainKey(const string &path) const; - - /** - * @brief 获å–域下é¢çš„所有éžåŸŸè¡Œ, 按照文件的行顺åºè¿”回 - * 区别于getDomainKeyåªèƒ½èŽ·å–到key,该接å£å¯¹å¸¦"="çš„é…置也整行返回 - * @param path 域å称, 域标识, 例如: /Main/Domain - * @return vector,æŸä¸ªæ ‡è¯†ä¸‹çš„所有éžåŸŸè¡Œ - */ - vector getDomainLine(const string &path) const; - - /** - * @brief getDomainMap 获å–域下é¢çš„å­åŸŸ. - * - * @param path 域å称, 域标识, 例如: /Main/Domain - * @param v è¦èŽ·å–çš„å­åŸŸåå­— - * @param vector域下é¢çš„域å称 - * @return æˆåŠŸèŽ·å–返回true,å¦åˆ™è¿”回false - */ - bool getDomainVector(const string &path, vector &v) const; - - /** - * @brief 获å–域下é¢çš„å­åŸŸ, - * ä¸å­˜åœ¨åˆ™è¿”回空vector按照在文件中的顺åºè¿”回 - * @param path 域å称, 域标识, 例如: /Main/Domain - * @return vector目标域下é¢çš„å­åŸŸ - */ - vector getDomainVector(const string &path) const; - - /** - * @brief 是å¦å­˜åœ¨åŸŸ. - * - * @param path 域å称, 域标识, 例如: /Main/Domain - * @return 存在返回true,å¦åˆ™è¿”回false - */ - bool hasDomainVector(const string &path) const; - - /** - * @brief 增加域,在当å‰åŸŸä¸‹é¢å¢žåŠ åŸŸ, 如果已ç»å­˜åœ¨sAddDomain域, - * 则认为æˆåŠŸ - * @param sCurDomain 域标识符, 例如:/Main/Domain - * @param sAddDomain 需è¦å¢žåŠ çš„域å称: 例如: sCurDomain - * @param bCreate sCurDomain域ä¸å­˜åœ¨çš„情况下, 是å¦è‡ªåŠ¨åˆ›å»º - * @return 0-æˆåŠŸå¢žåŠ , 1-sCurDomainä¸å­˜åœ¨ - */ - int insertDomain(const string &sCurDomain, const string &sAddDomain, bool bCreate); - - - /** - * @brief 增加å‚数,å³é…置项,当å‰åŸŸä¸‹é¢å¢žåŠ é…置项å‚æ•°, - * 如果已ç»æœ‰ç›¸å…³å‚æ•°, 则忽略(ä¸æ›¿æ¢) - * @param sCurDomain 域标识符, 例如:/Main/Domain - * @param m map类型,存å‚数值对 - * @param bCreate sCurDomain域ä¸å­˜åœ¨çš„情况下, 是å¦è‡ªåŠ¨åˆ›å»º - * @return 0: æˆåŠŸ, 1:sCurDomainä¸å­˜åœ¨ - */ - int insertDomainParam(const string &sCurDomain, const map &m, bool bCreate); - - /** - * @brief åˆå¹¶é…置文件到当å‰é…置文件. - * - * @param cf - * @param bUpdate true-冲çªé¡¹æ›´æ–°æœ¬é…ç½®, false-冲çªé¡¹ä¸æ›´æ–° - */ - void joinConfig(const TC_Config &cf, bool bUpdate); - - /** - * @brief 转æ¢æˆé…置文件的字符串格å¼. - * - * @return é…置字符串 - */ - string tostr() const; - -protected: - /** - * @brief Parse输入æµï¼Œ - * 最终把输入æµè§£æžæˆä¸ºä¸€ä¸ªTC_ConfigDomain装入stack中 - * @param è¦è§£æžçš„输入æµï¼ŒæŒ‰è¡Œè§£æž - * @throws TC_Config_Exception - * @return - */ - void parse(istream &is); - - /** - * @brief create New Domain 生æˆå­åŸŸ. - * - * @param sName 域å称 - * @return 指å‘新生æˆçš„å­åŸŸçš„指针 - * @throws TC_Config_Exception - */ - TC_ConfigDomain *newTcConfigDomain(const string& sName); - - /** - * @brief Search Domain æœç´¢åŸŸ. - * - * @param sDomainName å‚æ•°å称,支æŒå­åŸŸæœç´¢ - * @return value - */ - TC_ConfigDomain *searchTcConfigDomain(const vector& domains); - const TC_ConfigDomain *searchTcConfigDomain(const vector& domains) const; - -protected: - - /** - * æ ¹domain - */ - TC_ConfigDomain _root; -}; - -} -#endif //_TC_CONFIG_H_ diff --git a/cpp/util/include/util/tc_consistent_hash.h b/cpp/util/include/util/tc_consistent_hash.h deleted file mode 100644 index 765724508..000000000 --- a/cpp/util/include/util/tc_consistent_hash.h +++ /dev/null @@ -1,174 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __CONSISTENT_HASH__ -#define __CONSISTENT_HASH__ - -#include "util/tc_md5.h" - -using namespace std; - -namespace tars -{ - -///////////////////////////////////////////////// -/** - * @file tc_consistent_hash.h - * @brief 一致性hash算法类. - */ -///////////////////////////////////////////////// - -struct node_T -{ - /** - *节点hash值 - */ - unsigned int iHashCode; - - /** - *节点下标 - */ - unsigned int iIndex; -}; - - - -/** - * @brief 一致性hash算法类 - */ -class TC_ConsistentHash -{ - public: - - /** - * @brief 构造函数 - */ - TC_ConsistentHash() - { - } - - /** - * @brief 节点比较. - * - * @param m1 node_T类型的对象,比较节点之一 - * @param m2 node_T类型的对象,比较节点之一 - * @return less or not 比较结果,less返回ture,å¦åˆ™è¿”回false - */ - static bool less_hash(const node_T & m1, const node_T & m2) - { - return m1.iHashCode < m2.iHashCode; - } - - /** - * @brief 增加节点. - * - * @param node 节点å称 - * @param index 节点的下标值 - * @return 节点的hash值 - */ - unsigned addNode(const string & node, unsigned int index) - { - node_T stItem; - stItem.iHashCode = hash_md5(TC_MD5::md5bin(node)); - stItem.iIndex = index; - vHashList.push_back(stItem); - - sort(vHashList.begin(), vHashList.end(), less_hash); - - return stItem.iHashCode; - } - - /** - * @brief 删除节点. - * - * @param node 节点å称 - * @return 0 : 删除æˆåŠŸ -1 : 没有对应节点 - */ - int removeNode(const string & node) - { - unsigned iHashCode = hash_md5(TC_MD5::md5bin(node)); - vector::iterator it; - for(it=vHashList.begin() ; it!=vHashList.end(); it++) - { - if(it->iHashCode == iHashCode) - { - vHashList.erase(it); - return 0; - } - } - return -1; - } - - /** - * @brief 获å–æŸkey对应到的节点node的下标. - * - * @param key keyå称 - * @param iIndex 对应到的节点下标 - * @return 0:获å–æˆåŠŸ -1:没有被添加的节点 - */ - int getIndex(const string & key, unsigned int & iIndex) - { - unsigned iCode = hash_md5(TC_MD5::md5bin(key)); - if(vHashList.size() == 0) - { - iIndex = 0; - return -1; - } - - unsigned low = 0; - unsigned high = vHashList.size(); - - if(iCode <= vHashList[0].iHashCode || iCode > vHashList[high-1].iHashCode) - { - iIndex = vHashList[0].iIndex; - return 0; - } - - while (low < high - 1) - { - unsigned mid = (low + high) / 2; - if (vHashList[mid].iHashCode > iCode) - { - high = mid; - } - else - { - low = mid; - } - } - iIndex = vHashList[low+1].iIndex; - return 0; - } - - protected: - /** - * @brief 计算md5值的hash,分布范围在 0 -- 2^32-1. - * - * @param sMd5 md5值 - * @return hash值 - */ - unsigned int hash_md5(const string & sMd5) - { - char *p = (char *) sMd5.c_str(); - return (*(int*)(p)) ^ (*(int*)(p+4)) ^ (*(int*)(p+8)) ^ (*(int*)(p+12)); - } - - vector vHashList; - -}; - -} -#endif diff --git a/cpp/util/include/util/tc_consistent_hash_new.h b/cpp/util/include/util/tc_consistent_hash_new.h deleted file mode 100644 index a15e2aac8..000000000 --- a/cpp/util/include/util/tc_consistent_hash_new.h +++ /dev/null @@ -1,383 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_CONSISTENT_HASH_NEW_H_ -#define __TC_CONSISTENT_HASH_NEW_H_ - -#include "util/tc_md5.h" -#include "util/tc_autoptr.h" -#include "util/tc_hash_fun.h" - -using namespace tars; - -namespace tars -{ - -struct node_T_new -{ - /** - *节点hash值 - */ - long iHashCode; - - /** - *节点下标 - */ - unsigned int iIndex; -}; - -enum TC_HashAlgorithmType -{ - E_TC_CONHASH_KETAMAHASH = 0, - E_TC_CONHASH_DEFAULTHASH = 1 -}; - -/** - * @brief hash 算法虚基类 - */ -class TC_HashAlgorithm : public TC_HandleBase -{ -public: - virtual long hash(const string & sKey) = 0; - virtual TC_HashAlgorithmType getHashType() = 0; - -protected: - long subTo32Bit(long hash) - { - return (hash & 0xFFFFFFFFL); - } - -}; - -typedef TC_AutoPtr TC_HashAlgorithmPtr; - -/** - * @brief ketama hash 算法 - */ -class TC_KetamaHashAlg : public TC_HashAlgorithm -{ -public: - virtual long hash(const string & sKey) - { - string sMd5 = TC_MD5::md5bin(sKey); - const char *p = (const char *) sMd5.c_str(); - - long hash = ((long)(p[3] & 0xFF) << 24) - | ((long)(p[2] & 0xFF) << 16) - | ((long)(p[1] & 0xFF) << 8) - | ((long)(p[0] & 0xFF)); - - return subTo32Bit(hash); - } - - virtual TC_HashAlgorithmType getHashType() - { - return E_TC_CONHASH_KETAMAHASH; - } -}; - -/** - * @brief 默认的 hash 算法 - */ -class TC_DefaultHashAlg : public TC_HashAlgorithm -{ -public: - virtual long hash(const string & sKey) - { - string sMd5 = TC_MD5::md5bin(sKey); - const char *p = (const char *) sMd5.c_str(); - - long hash = (*(int*)(p)) ^ (*(int*)(p+4)) ^ (*(int*)(p+8)) ^ (*(int*)(p+12)); - - return subTo32Bit(hash); - } - - virtual TC_HashAlgorithmType getHashType() - { - return E_TC_CONHASH_DEFAULTHASH; - } -}; - -/** - * @brief hash alg 工厂 - */ -class TC_HashAlgFactory -{ -public: - static TC_HashAlgorithm *getHashAlg() - { - TC_HashAlgorithm *ptrHashAlg = new TC_DefaultHashAlg(); - - return ptrHashAlg; - } - - static TC_HashAlgorithm *getHashAlg(TC_HashAlgorithmType hashType) - { - TC_HashAlgorithm *ptrHashAlg = NULL; - - switch(hashType) - { - case E_TC_CONHASH_KETAMAHASH: - { - ptrHashAlg = new TC_KetamaHashAlg(); - break; - } - case E_TC_CONHASH_DEFAULTHASH: - default: - { - ptrHashAlg = new TC_DefaultHashAlg(); - break; - } - } - - return ptrHashAlg; - } -}; - -/** - * @brief 一致性hash算法类 - */ -class TC_ConsistentHashNew -{ -public: - - /** - * @brief 构造函数 - */ - TC_ConsistentHashNew() - { - _ptrHashAlg = TC_HashAlgFactory::getHashAlg(); - } - - /** - * @brief 构造函数 - */ - TC_ConsistentHashNew(TC_HashAlgorithmType hashType) - { - _ptrHashAlg = TC_HashAlgFactory::getHashAlg(hashType); - } - - /** - * @brief 节点比较. - * - * @param m1 node_T_new类型的对象,比较节点之一 - * @param m2 node_T_new类型的对象,比较节点之一 - * @return less or not 比较结果,less返回ture,å¦åˆ™è¿”回false - */ - static bool less_hash(const node_T_new & m1, const node_T_new & m2) - { - return m1.iHashCode < m2.iHashCode; - } - - /** - * @brief 增加节点. - * - * @param node 节点å称 - * @param index 节点的下标值 - * @return 节点的hash值 - */ - int sortNode() - { - sort(_vHashList.begin(), _vHashList.end(), less_hash); - - return 0; - } - - /** - * @brief 打å°èŠ‚ç‚¹ä¿¡æ¯ - * - */ - void printNode() - { - map mapNode; - size_t size = _vHashList.size(); - - for (size_t i = 0; i < size; i++) - { - if (i == 0) - { - unsigned int value = 0xFFFFFFFF - _vHashList[size - 1].iHashCode + _vHashList[0].iHashCode; - mapNode[_vHashList[0].iIndex] = value; - } - else - { - unsigned int value = _vHashList[i].iHashCode - _vHashList[i - 1].iHashCode; - - if (mapNode.find(_vHashList[i].iIndex) != mapNode.end()) - { - value += mapNode[_vHashList[i].iIndex]; - } - - mapNode[_vHashList[i].iIndex] = value; - } - - cout << "printNode: " << _vHashList[i].iHashCode << "|" << _vHashList[i].iIndex << "|" << mapNode[_vHashList[i].iIndex] << endl; - } - - map::iterator it = mapNode.begin(); - double avg = 100; - double sum = 0; - - while (it != mapNode.end()) - { - double tmp = it->second; - cerr << "result: " << it->first << "|" << it->second << "|" << (tmp * 100 * mapNode.size() / 0xFFFFFFFF - avg) << endl; - sum += (tmp * 100 * mapNode.size() / 0xFFFFFFFF - avg) * (tmp * 100 * mapNode.size() / 0xFFFFFFFF - avg); - it++; - } - - cerr << "variance: " << sum / mapNode.size() << ", size: " << _vHashList.size() << endl; - } - - /** - * @brief 增加节点. - * - * @param node 节点å称 - * @param index 节点的下标值 - * @param weight 节点的æƒé‡ï¼Œé»˜è®¤ä¸º1 - * @return 是å¦æˆåŠŸ - */ - int addNode(const string & node, unsigned int index, int weight = 1) - { - if (_ptrHashAlg.get() == NULL) - { - return -1; - } - - node_T_new stItem; - stItem.iIndex = index; - - for (int j = 0; j < weight; j++) - { - string virtualNode = node + "_" + TC_Common::tostr(j); - - // TODO: ç›®å‰å†™äº†2 ç§hash 算法,å¯ä»¥æ ¹æ®éœ€è¦é€‰æ‹©ä¸€ç§ï¼Œ - // TODO: 其中KEMATA 为å‚考memcached client çš„hash 算法,default 为原有的hash 算法,测试结论在表格里有 - if (_ptrHashAlg->getHashType() == E_TC_CONHASH_KETAMAHASH) - { - string sMd5 = TC_MD5::md5bin(virtualNode); - char *p = (char *) sMd5.c_str(); - - for (int i = 0; i < 4; i++) - { - stItem.iHashCode = ((long)(p[i * 4 + 3] & 0xFF) << 24) - | ((long)(p[i * 4 + 2] & 0xFF) << 16) - | ((long)(p[i * 4 + 1] & 0xFF) << 8) - | ((long)(p[i * 4 + 0] & 0xFF)); - stItem.iIndex = index; - _vHashList.push_back(stItem); - } - } - else - { - stItem.iHashCode = _ptrHashAlg->hash(virtualNode); - _vHashList.push_back(stItem); - } - } - - return 0; - } - - /** - * @brief 获å–æŸkey对应到的节点node的下标. - * - * @param key keyå称 - * @param iIndex 对应到的节点下标 - * @return 0:获å–æˆåŠŸ -1:没有被添加的节点 - */ - int getIndex(const string & key, unsigned int & iIndex) - { - if(_ptrHashAlg.get() == NULL || _vHashList.size() == 0) - { - iIndex = 0; - return -1; - } - - long iCode = _ptrHashAlg->hash(TC_MD5::md5bin(key)); - - return getIndex(iCode, iIndex); - } - - /** - * @brief 获å–æŸhashcode对应到的节点node的下标. - * - * @param hashcode hashcode - * @param iIndex 对应到的节点下标 - * @return 0:获å–æˆåŠŸ -1:没有被添加的节点 - */ - int getIndex(long hashcode, unsigned int & iIndex) - { - if(_ptrHashAlg.get() == NULL || _vHashList.size() == 0) - { - iIndex = 0; - return -1; - } - - // åªä¿ç•™32ä½ - long iCode = (hashcode & 0xFFFFFFFFL); - - int low = 0; - int high = _vHashList.size(); - - if(iCode <= _vHashList[0].iHashCode || iCode > _vHashList[high-1].iHashCode) - { - iIndex = _vHashList[0].iIndex; - return 0; - } - - while (low < high - 1) - { - int mid = (low + high) / 2; - if (_vHashList[mid].iHashCode > iCode) - { - high = mid; - } - else - { - low = mid; - } - } - iIndex = _vHashList[low+1].iIndex; - return 0; - } - - /** - * @brief 获å–当å‰hash列表的长度. - * - * @return 长度值 - */ - size_t size() - { - return _vHashList.size(); - } - - /** - * @brief 清空当å‰çš„hash列表. - * - */ - void clear() - { - _vHashList.clear(); - } - -protected: - vector _vHashList; - TC_HashAlgorithmPtr _ptrHashAlg; - -}; - -} -#endif diff --git a/cpp/util/include/util/tc_dyn_object.h b/cpp/util/include/util/tc_dyn_object.h deleted file mode 100644 index cf2887480..000000000 --- a/cpp/util/include/util/tc_dyn_object.h +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_DYN_OBJECT_H -#define __TC_DYN_OBJECT_H - -#include - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_dyn_object.h - * @brief 动æ€ç”Ÿæˆç±». -*/ -///////////////////////////////////////////////// - - -/** - * @brief 动æ€ç”Ÿæˆç±» - */ -class TC_DYN_Object; -class TC_DYN_RuntimeClass; - -struct TC_DYN_RuntimeClass -{ - const char* m_lpszClassName; - int m_nObjectSize; - TC_DYN_Object* (* m_pfnCreateObject)(); - TC_DYN_RuntimeClass* m_pBaseClass; - TC_DYN_RuntimeClass* m_pNextClass; - - static TC_DYN_RuntimeClass* pFirstClass; - - TC_DYN_Object* createObject(); - - static TC_DYN_RuntimeClass* load(const char *szClassName); -}; - -struct TC_DYN_Init -{ - TC_DYN_Init(TC_DYN_RuntimeClass* pNewClass) - { - pNewClass->m_pNextClass = TC_DYN_RuntimeClass::pFirstClass; - TC_DYN_RuntimeClass::pFirstClass = pNewClass; - } -}; - -class TC_DYN_Object -{ -public: - TC_DYN_Object(){}; - virtual ~TC_DYN_Object(){}; - - virtual TC_DYN_RuntimeClass* GetRuntimeClass() const; - - bool isKindOf(const TC_DYN_RuntimeClass* pClass) const; -private: - - TC_DYN_Object(const TC_DYN_Object& objectSrc); - void operator=(const TC_DYN_Object& objectSrc); - -public: - static TC_DYN_RuntimeClass classTC_DYN_Object; -}; - -#define RUNTIME_CLASS(class_name) ((TC_DYN_RuntimeClass*)(&class_name::class##class_name)) - -#define DECLARE_DYNCREATE(class_name) \ -public: \ - static TC_DYN_RuntimeClass class##class_name; \ - virtual TC_DYN_RuntimeClass* GetRuntimeClass() const; \ - static TC_DYN_Object* createObject(); - -#define IMPLEMENT_DYNCREATE(class_name, base_class_name) \ - TC_DYN_Object* class_name::createObject() \ - { return new class_name; } \ - TC_DYN_RuntimeClass class_name::class##class_name = { \ - #class_name, \ - sizeof(class_name), \ - &class_name::createObject, \ - RUNTIME_CLASS(base_class_name), \ - NULL }; \ - static TC_DYN_Init _init_##class_name(&class_name::class##class_name); \ - TC_DYN_RuntimeClass* class_name::GetRuntimeClass() const \ - { return RUNTIME_CLASS(class_name); } - -#define TC_DYN_CreateObject(class_name) \ -(TC_DYN_RuntimeClass::load(class_name) == NULL ? NULL : TC_DYN_RuntimeClass::load(class_name)->createObject()) - -} - -#endif - diff --git a/cpp/util/include/util/tc_enable_shared_from_this.h b/cpp/util/include/util/tc_enable_shared_from_this.h deleted file mode 100644 index 187f0f1ba..000000000 --- a/cpp/util/include/util/tc_enable_shared_from_this.h +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_ENABLE_SHARED_FROM_THIS_H__ -#define __TC_ENABLE_SHARED_FROM_THIS_H__ - -#include "tc_shared_ptr.h" - -namespace tars -{ - - template - class TC_EnableSharedFromThis - { - public: - - TC_SharedPtr sharedFromThis() - { - TC_SharedPtr p(m_this, m_owner_use_count); - return p; - } - - TC_SharedPtr sharedFromThis() const - { - TC_SharedPtr p(m_this, m_owner_use_count); - return p; - } - - protected: - TC_EnableSharedFromThis() - : m_this(NULL) - , m_owner_use_count(NULL) - { } - - TC_EnableSharedFromThis(const TC_EnableSharedFromThis&) - { } - - TC_EnableSharedFromThis& operator=(const TC_EnableSharedFromThis&) - { - return *this; - } - - ~TC_EnableSharedFromThis() - { } - - private: - - template - void acceptOwner(const TC_SharedPtr& p) const - { - if (m_owner_use_count == NULL) - { - m_owner_use_count = p.m_pn; - m_this = p.get(); - } - } - - mutable T *m_this; - mutable detail::tc_shared_count_base *m_owner_use_count; - - template - friend void detail::tc_sp_enable_shared_from_this(const TC_SharedPtr *pp, const TC_EnableSharedFromThis *px); - }; - -} - -#endif diff --git a/cpp/util/include/util/tc_encoder.h b/cpp/util/include/util/tc_encoder.h deleted file mode 100644 index a365790ca..000000000 --- a/cpp/util/include/util/tc_encoder.h +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_ENCODER_H_ -#define __TC_ENCODER_H_ - -#include - -#include "util/tc_ex.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_encoder.h -* @brief 转ç ç±» -* -* -*/ -///////////////////////////////////////////////// - -/** -* @brief 转ç å¼‚常类 -*/ -struct TC_Encoder_Exception : public TC_Exception -{ - TC_Encoder_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_Encoder_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_Encoder_Exception() throw(){}; -}; - -/** -* @brief 该类æ供基本常用编ç çš„转æ¢. -* -* Gbk到utf8之间的相互转ç å‡½æ•°ï¼Œé€šè¿‡é™æ€å‡½æ•°æä¾›. -* -* 1:GBK ==> UTF8çš„è½¬æ¢ -* -* 2:UTF8 ==> GBKçš„è½¬æ¢ -*/ -class TC_Encoder -{ -public: - /** - * @brief gbk 转æ¢åˆ° utf8. - * - * @param sOut 输出buffer - * @param iMaxOutLen 输出buffer最大的长度/sOut的长度 - * @param sIn 输入buffer - * @param iInLen 输入buffer长度 - * @throws TC_Encoder_Exception - * @return - */ - static void gbk2utf8(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen); - - /** - * @brief gbk 转æ¢åˆ° utf8. - * - * @param sIn 输入buffer* - * @throws TC_Encoder_Exception - * @return 转æ¢åŽçš„utf8ç¼–ç  - */ - static string gbk2utf8(const string &sIn); - - /** - * @brief gbk 转æ¢åˆ° utf8. - * - * @param sIn 输入buffer - * @param vtStr 输出gbkçš„vector - * @throws TC_Encoder_Exception - * @return - */ - static void gbk2utf8(const string &sIn, vector &vtStr); - - /** - * @brief utf8 转æ¢åˆ° gbk. - * - * @param sOut 输出buffer - * @param iMaxOutLen 输出buffer最大的长度/sOut的长度 - * @param sIn 输入buffer - * @param iInLen 输入buffer长度 - * @throws TC_Encoder_Exception - * @return - */ - static void utf82gbk(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen); - - /** - * @brief utf8 转æ¢åˆ° gbk. - * - * @param sIn 输入buffer - * @throws TC_Encoder_Exception - * @return 转æ¢åŽçš„gbkç¼–ç  - */ - static string utf82gbk(const string &sIn); - - /** - * @brief å°†stringçš„\n替æ¢æŽ‰,转义字符串中的æŸä¸ªå­—符 - * - * 缺çœ:\n 转æ¢ä¸º \r\0; \r转æ¢ä¸º\, - * - * 主è¦ç”¨äºŽå°†string记录在一行,通常用于写bin-log的地方; - * @param str 待转æ¢å­—符串 - * @param f 需è¦è½¬ä¹‰çš„字符 - * @param t 转义åŽçš„字符 - * @param u 借用的转义符 - * @return str 转æ¢åŽçš„字符串 - */ - static string transTo(const string& str, char f = '\n', char t = '\r', char u = '\0'); - - /** - * @brief 从替æ¢çš„æ•°æ®æ¢å¤æºæ•°æ®,å°† transTo 的字符串还原, - * - * 缺çœ:\r\0 还原为\n,\r\r还原为, - * - * 主è¦ç”¨äºŽå°†string记录在一行,通常用于写bin-log的地方 - * @param str 待还原的字符串(必须是transToåŽå¾—到的字符串) - * @param f 被转义的字符 - * @param t 转义åŽçš„字符 - * @param u 借用的转义符 - * @return str 还原åŽçš„字符串 - */ - static string transFrom(const string& str, char f = '\n', char t = '\r', char u = '\0'); -}; - -} - - -#endif - - diff --git a/cpp/util/include/util/tc_epoll_server.h b/cpp/util/include/util/tc_epoll_server.h deleted file mode 100644 index c640e8487..000000000 --- a/cpp/util/include/util/tc_epoll_server.h +++ /dev/null @@ -1,1868 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TARS_TC_EPOLL_SERVER_H_ -#define __TARS_TC_EPOLL_SERVER_H_ - -#include -#include -#include -#include -#include -#include -#include -#include "util/tc_epoller.h" -#include "util/tc_thread.h" -#include "util/tc_clientsocket.h" -#include "util/tc_logger.h" -#include "util/tc_shm.h" -#include "util/tc_common.h" -#include "util/tc_mem_queue.h" -#include "util/tc_squeue.h" -#include "util/tc_mmap.h" -#include "util/tc_fifo.h" -#include "util/tc_buffer.h" -#include "util/tc_buffer_pool.h" - -using namespace std; - -namespace tars -{ - -#if TARS_SSL - class TC_OpenSSL; -#endif - -///////////////////////////////////////////////// -/** - * @file tc_epoll_server.h - * @brief EpollServerç±» - * - */ -///////////////////////////////////////////////// -/** - * Server基类 - * 注册å议解æžå™¨ - * 注册逻辑处ç†å™¨ - * 注册管ç†ç«¯å£å¤„ç†å™¨ - */ - -class PropertyReport; - -class TC_EpollServer : public TC_ThreadLock, public TC_HandleBase -{ -public: - //////////////////////////////////////////////////////////////////////////// - /** - * 定义å议解æžçš„返回值 - */ - enum - { - PACKET_LESS = 0, - PACKET_FULL = 1, - PACKET_ERR = -1, - }; - - enum EM_CLOSE_T - { - EM_CLIENT_CLOSE = 0, //客户端主动关闭 - EM_SERVER_CLOSE = 1, //æœåŠ¡ç«¯ä¸šåŠ¡ä¸»åŠ¨è°ƒç”¨close关闭连接,或者框架因æŸç§å¼‚常主动关闭连接 - EM_SERVER_TIMEOUT_CLOSE = 2 //连接超时了,æœåŠ¡ç«¯ä¸»åŠ¨å…³é—­ - }; - - /** - * 定义å议解æžæŽ¥å£çš„æ“作对象 - * 注æ„必须是线程安全的或是å¯ä»¥é‡å…¥çš„ - */ - using protocol_functor = std::function; - using header_filter_functor = std::function; - using conn_protocol_functor = std::function; - - class NetThread; - - class BindAdapter; - typedef TC_AutoPtr BindAdapterPtr; - - class Handle; - typedef TC_AutoPtr HandlePtr; - - class HandleGroup; - typedef TC_AutoPtr HandleGroupPtr; - - - //////////////////////////////////////////////////////////////////////////// - /**定义数æ®é˜Ÿåˆ—中的结构*/ - struct tagRecvData - { - uint32_t uid; /**连接标示*/ - string buffer; /**需è¦å‘é€çš„内容*/ - string ip; /**远程连接的ip*/ - uint16_t port; /**远程连接的端å£*/ - int64_t recvTimeStamp; /**接收到数æ®çš„时间*/ - bool isOverload; /**是å¦å·²è¿‡è½½ */ - bool isClosed; /**是å¦å·²å…³é—­*/ - int fd; /*ä¿å­˜äº§ç”Ÿè¯¥æ¶ˆæ¯çš„fd,用于回包时选择网络线程*/ - BindAdapterPtr adapter; /**标识哪一个adapter的消æ¯*/ - int closeType; /*如果是关闭消æ¯åŒ…,则标识关闭类型,0:表示客户端主动关闭;1:æœåŠ¡ç«¯ä¸»åŠ¨å…³é—­;2:连接超时æœåŠ¡ç«¯ä¸»åŠ¨å…³é—­*/ - }; - - struct tagSendData - { - char cmd; /**命令:'c',关闭fd; 's',有数æ®éœ€è¦å‘é€*/ - uint32_t uid; /**连接标示*/ - string buffer; /**需è¦å‘é€çš„内容*/ - string ip; /**远程连接的ip*/ - uint16_t port; /**远程连接的端å£*/ - }; - - typedef TC_ThreadQueue > recv_queue; - typedef TC_ThreadQueue > send_queue; - typedef recv_queue::queue_type recv_queue_type; - - //////////////////////////////////////////////////////////////////////////// - /** - * é“¾æŽ¥çŠ¶æ€ - */ - struct ConnStatus - { - string ip; - int32_t uid; - uint16_t port; - int timeout; - int iLastRefreshTime; - }; - //////////////////////////////////////////////////////////////////////////// - /** - * 按name对handle分组, - * æ¯ç»„handle处ç†ä¸€ä¸ªæˆ–多个Adapteræ¶ˆæ¯ - * æ¯ä¸ªhandle对象一个线程 - */ - struct HandleGroup : public TC_HandleBase - { - string name; - TC_ThreadLock monitor; - vector handles; - map adapters; - }; - //////////////////////////////////////////////////////////////////////////// - /** - * @brief 定义æœåŠ¡é€»è¾‘处ç†çš„æŽ¥å£ - * - */ - /** - * æœåŠ¡çš„逻辑处ç†ä»£ç  - */ - class Handle : public TC_Thread, public TC_ThreadLock, public TC_HandleBase - { - public: - /** - * 构造, 默认没有请求, 等待10s - */ - Handle(); - - /** - * æžæž„函数 - */ - virtual ~Handle(); - - /** - * 设置æœåŠ¡ - * @param pEpollServer - */ - void setEpollServer(TC_EpollServer *pEpollServer); - - /** - * 获å–æœåŠ¡ - * @return TC_EpollServer* - */ - TC_EpollServer* getEpollServer(); - - /** - * 设置所属的Group - * @param pHandleGroup - */ - void setHandleGroup(HandleGroupPtr& pHandleGroup); - - /** - * 获å–所属Group - * @return HandleGroup* - */ - HandleGroupPtr& getHandleGroup(); - - /** - * 线程处ç†æ–¹æ³• - */ - virtual void run(); - - public: - /** - * å‘é€æ•°æ® - * @param stRecvData - * @param sSendBuffer - */ - void sendResponse(unsigned int uid, const string &sSendBuffer, const string &ip, int port, int fd); - - /** - * 关闭链接 - * @param stRecvData - */ - void close(unsigned int uid, int fd); - - /** - * 设置等待时间 - * @param iWaitTime - */ - void setWaitTime(uint32_t iWaitTime); - - /** - * 对象åˆå§‹åŒ– - */ - virtual void initialize() {}; - - /** - * 唤醒HandleGroup中的handle线程 - */ - virtual void notifyFilter(); - - protected: - /** - * 具体的处ç†é€»è¾‘ - */ - virtual void handleImp(); - - /** - * 处ç†å‡½æ•° - * @param stRecvData: æŽ¥æ”¶åˆ°çš„æ•°æ® - */ - virtual void handle(const tagRecvData &stRecvData) = 0; - - /** - * 处ç†è¶…时数æ®, å³æ•°æ®åœ¨é˜Ÿåˆ—中的时间已ç»è¶…过 - * 默认直接关闭连接 - * @param stRecvData: æŽ¥æ”¶åˆ°çš„æ•°æ® - */ - virtual void handleTimeout(const tagRecvData &stRecvData); - - /** - * 处ç†è¿žæŽ¥å…³é—­é€šçŸ¥ï¼ŒåŒ…括 - * 1.close by peer - * 2.recv/send fail - * 3.close by timeout or overload - * @param stRecvData: - */ - virtual void handleClose(const tagRecvData &stRecvData); - - /** - * 处ç†overloadæ•°æ® å³æ•°æ®é˜Ÿåˆ—中长度已ç»è¶…过å…许值 - * 默认直接关闭连接 - * @param stRecvData: æŽ¥æ”¶åˆ°çš„æ•°æ® - */ - virtual void handleOverload(const tagRecvData &stRecvData); - - /** - * 处ç†Filterçš„æ¶ˆæ¯ - */ - //virtual void handleFilter() {}; - /** - * 处ç†å¼‚步回调队列 - */ - virtual void handleAsyncResponse() {} - /** - * handleFilter拆分的第二部分,处ç†ç”¨æˆ·è‡ªæœ‰æ•°æ® - * éžæ¸¸æˆé€»è¾‘å¯å¿½ç•¥bExpectIdleå‚æ•° - */ - virtual void handleCustomMessage(bool bExpectIdle = false) {} - - /** - * 心跳(æ¯å¤„ç†å®Œä¸€ä¸ªè¯·æ±‚或者等待请求超时都会调用一次) - */ - virtual void heartbeat() {} - - /** - * 线程已ç»å¯åŠ¨, 进入具体处ç†å‰è°ƒç”¨ - */ - virtual void startHandle() {} - - /** - * 线程马上è¦é€€å‡ºæ—¶è°ƒç”¨ - */ - virtual void stopHandle() {} - - /** - * 是å¦æ‰€æœ‰çš„Adpater队列都为空 - * @return bool - */ - virtual bool allAdapterIsEmpty(); - - /** - * 是å¦æ‰€æœ‰çš„servant都没有resp消æ¯å¾…å¤„ç† - * @return bool - */ - virtual bool allFilterIsEmpty(); - - /** - * å‹å…ƒç±» - */ - friend class BindAdapter; - protected: - /** - * æœåŠ¡ - */ - TC_EpollServer *_pEpollServer; - - /** - * 所属handle组 - */ - HandleGroupPtr _handleGroup; - - /** - * 等待时间 - */ - uint32_t _iWaitTime; - - }; - - - using close_functor = std::function; - using auth_process_wrapper_functor = std::function; - - //////////////////////////////////////////////////////////////////////////// - // æœåŠ¡ç«¯å£ç®¡ç†,监å¬socketä¿¡æ¯ - class BindAdapter : public TC_ThreadLock, public TC_HandleBase - { - public: - /** - * 缺çœçš„一些定义 - */ - enum - { - DEFAULT_QUEUE_CAP = 10*1024, /**æµé‡*/ - MIN_QUEUE_TIMEOUT = 3*1000, /**队列最å°è¶…时时间(ms)*/ - DEFAULT_MAX_CONN = 1024, /**缺çœæœ€å¤§è¿žæŽ¥æ•°*/ - DEFAULT_QUEUE_TIMEOUT = 60*1000, /**缺çœçš„队列超时时间(ms)*/ - }; - /** - * é¡ºåº - */ - enum EOrder - { - ALLOW_DENY, - DENY_ALLOW - }; - - - BindAdapter() - { - _pReportQueue = NULL; - _pReportConRate = NULL; - _pReportTimeoutNum = NULL; - } - - /** - * 构造函数 - */ - BindAdapter(TC_EpollServer *pEpollServer); - - /** - * æžå¤Ÿå‡½æ•° - */ - ~BindAdapter(); - - /** - * 设置adapter name - * @param name - */ - void setName(const string &name); - - /** - * 获å–adapter name - * @return string - */ - string getName() const; - - /** - * 获å–queue capacity - * @return int - */ - int getQueueCapacity() const; - - /** - * 设置queue capacity - * @param n - */ - void setQueueCapacity(int n); - - /** - * 设置åè®®å称 - * @param name - */ - void setProtocolName(const string& name); - - /** - * 获å–åè®®å称 - * @return const string& - */ - const string& getProtocolName(); - - /** - * 是å¦tarsåè®® - * @return bool - */ - bool isTarsProtocol(); - - /** - * 判断是å¦éœ€è¦è¿‡è½½ä¿æŠ¤ - * @return bool - */ - int isOverloadorDiscard(); - - /** - * 设置消æ¯åœ¨é˜Ÿåˆ—中的超时时间, t为毫秒 - * (超时时间精度åªèƒ½æ˜¯s) - * @param t - */ - void setQueueTimeout(int t); - - /** - * 获å–消æ¯åœ¨é˜Ÿåˆ—中的超时时间, 毫秒 - * @return int - */ - int getQueueTimeout() const; - - /** - * 设置endpoint - * @param str - */ - void setEndpoint(const string &str); - - /** - * 获å–ip - * @return const string& - */ - TC_Endpoint getEndpoint() const; - - /** - * 监å¬socket - * @return TC_Socket - */ - TC_Socket &getSocket(); - - /** - * 设置最大连接数 - * @param iMaxConns - */ - void setMaxConns(int iMaxConns); - - /** - * 获å–最大连接数 - * @return size_t - */ - size_t getMaxConns() const; - - /** - * 设置HeartBeat时间 - * @param n - */ - void setHeartBeatTime(time_t t); - - /** - * 获å–HeartBeat时间 - * @return size_t - */ - time_t getHeartBeatTime() const; - - /** - * 设置allow denyæ¬¡åº - * @param eOrder - */ - void setOrder(EOrder eOrder); - - /** - * 设置å…许ip - * @param vtAllow - */ - void setAllow(const vector &vtAllow); - - /** - * 设置ç¦æ­¢ip - * @param vtDeny - */ - void setDeny(const vector &vtDeny); - - /** - * 获å–å…许ip - * @return vector: ip列表 - */ - vector getAllow() const; - - /** - * 获å–ç¦æ­¢ip - * @return vector: ip列表 - */ - vector getDeny() const; - - /** - * 获å–allow denyæ¬¡åº - * @return EOrder - */ - EOrder getOrder() const; - - /** - * 是å¦Ip被å…许 - * @param ip - * @return bool - */ - bool isIpAllow(const string& ip) const; - - /** - * 是å¦è¶…过了最大连接数 - * @return bool - */ - bool isLimitMaxConnection() const; - - /** - * å‡å°‘当å‰è¿žæŽ¥æ•° - */ - void decreaseNowConnection(); - - /** - * å‡å°‘当å‰è¿žæŽ¥æ•° - */ - void increaseNowConnection(); - - /** - * 获å–æ‰€æœ‰é“¾æŽ¥çŠ¶æ€ - * @return ConnStatus - */ - vector getConnStatus(); - - /** - * 获å–当å‰è¿žæŽ¥æ•° - * @return size_t - */ - size_t getNowConnection() const; - - /** - * 获å–EpollServer - * @return TC_EpollServer* - */ - TC_EpollServer* getEpollServer(); - - /** - * 注册å议解æžå™¨ - * @param pp - */ - void setProtocol(const protocol_functor& pf, int iHeaderLen = 0, const header_filter_functor& hf = echo_header_filter); - - /** - * 获å–å议解æžå™¨ - * @return protocol_functor& - */ - protocol_functor &getProtocol(); - - /** - * 解æžåŒ…头处ç†å¯¹è±¡ - * @return protocol_functor& - */ - header_filter_functor &getHeaderFilterFunctor(); - - /** - * 增加数æ®åˆ°é˜Ÿåˆ—中 - * @param vtRecvData - * @param bPushBack åŽç«¯æ’å…¥ - * @param sBuffer - */ - void insertRecvQueue(const recv_queue::queue_type &vtRecvData,bool bPushBack = true); - - /** - * 通知等待在接收队列上é¢çš„çº¿ç¨‹é†’è¿‡æ¥ - */ - void notifyRecvQueue(); - - /** - * ç­‰å¾…æ•°æ® - * @return bool - */ - bool waitForRecvQueue(tagRecvData* &recv, uint32_t iWaitTime); - - /** - * æŽ¥æ”¶é˜Ÿåˆ—çš„å¤§å° - * @return size_t - */ - size_t getRecvBufferSize() const; - - /** - * 默认的å议解æžç±», 直接echo - * @param r - * @param o - * @return int - */ - static int echo_protocol(string &r, string &o); - - /** - * é»˜è®¤çš„åŒ…å¤´å¤„ç† - * @param i - * @param o - * @return int - */ - static int echo_header_filter(int i, string &o); - - /** - * 获å–需è¦è¿‡æ»¤çš„包头长度 - */ - int getHeaderFilterLen(); - - /** - * 设置所属的handle组å - * @param handleGroupName - */ - void setHandleGroupName(const string& handleGroupName); - - /** - * 获得所属的handle组å - * @return string - */ - string getHandleGroupName() const; - - /** - * 获得所属的handle - * @return HandleGroupPtr - */ - - HandleGroupPtr getHandleGroup() const - { - return _handleGroup; - } - - /** - * 设置ServantHandleæ•°ç›® - * @param n - */ - void setHandleNum(int n); - - /** - * 所属handle组的handleæ•°(æ¯ä¸ªhandle一个对象) - * @return int - */ - int getHandleNum(); - - /** - * 绑定两个Adapter到åŒä¸€ä¸ªGroup - * @param otherAdapter - */ - void setHandle(BindAdapterPtr& otherAdapter) - { - _pEpollServer->setHandleGroup(otherAdapter->getHandleGroupName(), this); - } - - /** - * åˆå§‹åŒ–处ç†çº¿ç¨‹,线程将会å¯åŠ¨ - */ - template void setHandle() - { - _pEpollServer->setHandleGroup(_handleGroupName, _iHandleNum, this); - } - - /** - * 设置æœåŠ¡ç«¯å›žåŒ…缓存的大å°é™åˆ¶ - */ - void setBackPacketBuffLimit(size_t iLimitSize); - - /** - * 获å–æœåŠ¡ç«¯å›žåŒ…缓存的大å°é™åˆ¶ - */ - size_t getBackPacketBuffLimit(); - /** - * 设置close回调函数 - */ - void setOnClose(const close_functor& f) { _closeFunc = f; } - /** - * 注册å议解æžå™¨ - */ - void setConnProtocol(const conn_protocol_functor& cpf, int iHeaderLen = 0, const header_filter_functor& hf = echo_header_filter); - /** - * 获å–å议解æžå™¨ - * @return conn_protocol_functor& - */ - conn_protocol_functor& getConnProtocol() { return _cpf; } - /** - * 注册鉴æƒåŒ…裹函数 - * @param apwf - */ - void setAuthProcessWrapper(const auth_process_wrapper_functor& apwf) { _authWrapper = apwf; } - - void setAkSk(const std::string& ak, const std::string& sk) { _accessKey = ak; _secretKey = sk; } - - bool checkAkSk(const std::string& ak, const std::string& sk) { return ak == _accessKey && sk == _secretKey; } - - std::string getSk(const std::string& ak) const { return (_accessKey == ak) ? _secretKey : ""; } - - public: - - //统计上报的对象 - PropertyReport * _pReportQueue; - PropertyReport * _pReportConRate; - PropertyReport * _pReportTimeoutNum; - - protected: - friend class TC_EpollServer; - friend class NetThread; - - /** - * æœåŠ¡ - */ - TC_EpollServer *_pEpollServer; - - /** - * Adapter所用的HandleGroup - */ - HandleGroupPtr _handleGroup; - - /** - * åè®®è§£æž - */ - protocol_functor _pf; - - /** - * 首个数æ®åŒ…包头过滤 - */ - header_filter_functor _hf; - - /** - * adapterçš„åå­— - */ - string _name; - - /** - * handle分组å称 - */ - string _handleGroupName; - - /** - * 监å¬fd - */ - TC_Socket _s; - - /** - * 绑定的IP - */ - TC_Endpoint _ep; - - /** - * 最大连接数 - */ - int _iMaxConns; - - /** - * 当å‰è¿žæŽ¥æ•° - */ - TC_Atomic _iCurConns; - - /** - * Handle个数 - */ - size_t _iHandleNum; - - /** - * å…许的Order - */ - volatile EOrder _eOrder; - - /** - * å…许的ip - */ - vector _vtAllow; - - /** - * ç¦æ­¢çš„ip - */ - vector _vtDeny; - - /** - * 接收的数æ®é˜Ÿåˆ— - */ - recv_queue _rbuffer; - - /** - * é˜Ÿåˆ—æœ€å¤§å®¹é‡ - */ - int _iQueueCapacity; - - /** - * 消æ¯è¶…时时间(从入队列到出队列间隔)(毫秒) - */ - int _iQueueTimeout; - - /** - * 首个数æ®åŒ…包头长度 - */ - int _iHeaderLen; - - /** - * 上次心跳å‘é€æ—¶é—´ - */ - volatile time_t _iHeartBeatTime; - - /** - * åè®®å称,缺çœä¸º"tars" - */ - string _protocolName; - - //回包缓存é™åˆ¶å¤§å° - size_t _iBackPacketBuffLimit; - - /** - * 包裹认è¯å‡½æ•°,ä¸èƒ½ä¸ºç©º - */ - auth_process_wrapper_functor _authWrapper; - - /** - * 该objçš„AK SK - */ - std::string _accessKey; - std::string _secretKey; - - //连接关闭的回调函数 - close_functor _closeFunc; - - // åè®®è§£æž - conn_protocol_functor _cpf; - }; - - //////////////////////////////////////////////////////////////////////////// - class NetThread : public TC_Thread, public TC_ThreadLock, public TC_HandleBase - { - public: - - //////////////////////////////////////////////////////////////////////////// - // æœåŠ¡è¿žæŽ¥ç®¡ç† - /** - * 建立连接的socketä¿¡æ¯ - */ - class Connection - { - public: - enum EnumConnectionType - { - EM_TCP = 0, - EM_UDP = 1, - }; - - /** - * 构造函数 - * @param lfd - * @param s - * @param ip - * @param port - */ - Connection(BindAdapter *pBindAdapter, int lfd, int timeout, int fd, const string& ip, uint16_t port); - - /** - * udp连接 - * @param fd - */ - Connection(BindAdapter *pBindAdapter, int fd); - /** - * 通讯组件åˆå§‹åŒ– - */ - Connection(BindAdapter *pBindAdapter); - - /** - * æžæž„函数 - */ - virtual ~Connection(); - - /** - * 链接所属的adapter - */ - BindAdapter* getBindAdapter() { return _pBindAdapter; } - - /** - * åˆå§‹åŒ– - * @param id, 连接的唯一id - */ - void init(unsigned int uid) { _uid = uid; } - - /** - * 获å–连接超时时间 - * - * @return int - */ - int getTimeout() const { return _timeout; } - - /** - * 获å–线程的惟一id - * - * @return unsigned int - */ - uint32_t getId() const { return _uid; } - - /** - * 获å–监å¬fd - * - * @return int - */ - int getListenfd() const { return _lfd; } - - /** - * 当å‰è¿žæŽ¥fd - * - * @return int - */ - virtual int getfd() const { return _sock.getfd(); } - - /** - * 是å¦æœ‰æ•ˆ - * - * @return bool - */ - bool isValid() const { return _sock.isValid();} - - /** - * 远程IP - * - * @return string - */ - string getIp() const { return _ip; } - - /** - * è¿œç¨‹ç«¯å£ - * - * @return uint16_t - */ - uint16_t getPort() const { return _port; } - - /** - * 设置首个数æ®åŒ…包头需è¦è¿‡æ»¤çš„字节数 - */ - void setHeaderFilterLen(int iHeaderLen) { _iHeaderLen = iHeaderLen; } - - /** - * 设置关闭,å‘é€å®Œå½“å‰æ•°æ®å°±å…³é—­è¿žæŽ¥ - */ - bool setClose();// { _bClose = true; return _sendbuffer.empty(); } - - /** - * 获å–连接类型 - */ - EnumConnectionType getType() const { return _enType; } - - bool IsEmptyConn() const {return _bEmptyConn;} - - /** - * Init Auth State; - */ - void tryInitAuthState(int initState); - - protected: - /** - * 关闭连接 - * @param fd - */ - void close(); - - /** - * 添加å‘é€buffer - * @param buffer - * @return int, -1:å‘é€å‡ºé”™, 0:æ— æ•°æ®, 1:å‘é€å®Œæ¯•, 2:è¿˜æœ‰æ•°æ® - */ - virtual int send(const string& buffer, const string &ip, uint16_t port, bool byEpollout = false); - - /** - * å‘é€æ•°æ® - * - * @return int - */ - virtual int send(); - - /** - * å‘é€buffer-slices - * @param slices - * @return int, -1:å‘é€å‡ºé”™, >= 0:å‘é€çš„字节数 - */ - int send(const std::vector& slices); - - - /** - * 读å–æ•°æ® - * @param fd - * @return int, -1:接收出错, 0:接收ä¸å…¨, 1:接收到一个完整包 - */ - virtual int recv(recv_queue::queue_type &o); - - /** - * 解æžåè®® - * @param o - * @return int: <0:å议错误, 0:没有一个完整的包, 1:收到至少一个包 - */ - int parseProtocol(recv_queue::queue_type &o); - - /** - * 增加数æ®åˆ°é˜Ÿåˆ—中 - * @param vtRecvData - */ - - void insertRecvQueue(recv_queue::queue_type &vRecvData); - - /** - * 对于udpæ–¹å¼çš„连接,分é…指定大å°çš„接收缓冲区 - *@param nSize - */ - bool setRecvBuffer(size_t nSize=DEFAULT_RECV_BUFFERSIZE); - - friend class NetThread; - - private: - /** - * tcpå‘é€æ•°æ® - */ - int tcpSend(const void* data, size_t len); - int tcpWriteV(const std::vector& buffers); - - /** - * 清空buffer-slices - * @param slices - */ - void clearSlices(std::vector& slices); - - /** - * æ•´ç†buffer-slices - * @param slices - * @param toSkippedBytes - */ - void adjustSlices(std::vector& slices, size_t toSkippedBytes); - - public: - /** - * 最åŽåˆ·æ–°æ—¶é—´ - */ - time_t _iLastRefreshTime; - - protected: - - /** - * 适é…器 - */ - BindAdapter *_pBindAdapter; - - /** - * TC_Socket - */ - TC_Socket _sock; - - /** - * è¿žæŽ¥çš„å”¯ä¸€ç¼–å· - */ - volatile uint32_t _uid; - - /** - * 监å¬çš„socket - */ - int _lfd; - - /** - * 超时时间 - */ - int _timeout; - - /** - * ip - */ - string _ip; - - /** - * ç«¯å£ - */ - uint16_t _port; - - /** - * 接收数æ®buffer - */ - string _recvbuffer; - - /** - * å‘é€æ•°æ®buffer - */ - std::vector _sendbuffer; - - /** - * 需è¦è¿‡æ»¤çš„头部字节数 - */ - int _iHeaderLen; - - /** - * å‘é€å®Œå½“å‰æ•°æ®å°±å…³é—­è¿žæŽ¥ - */ - bool _bClose; - - /** - * 临时队列的最大长度 - */ - int _iMaxTemQueueSize; - /** - * 连接类型 - */ - EnumConnectionType _enType; - - bool _bEmptyConn; - - /* - *接收数æ®çš„临时buffer,加这个目的是对udp接收数æ®åŒ…大å°è¿›è¡Œè®¾ç½® - */ - char *_pRecvBuffer; - - size_t _nRecvBufferSize; - public: - /* - *该连接的鉴æƒçŠ¶æ€ - */ - int _authState; - /* - *该连接的鉴æƒçŠ¶æ€æ˜¯å¦åˆå§‹åŒ–了 - */ - bool _authInit; -#if TARS_SSL - std::unique_ptr _openssl; -#endif - }; - //////////////////////////////////////////////////////////////////////////// - /** - * 带有时间链表的map - */ - class ConnectionList : public TC_ThreadLock - { - public: - /** - * 构造函数 - */ - ConnectionList(NetThread *pEpollServer); - - /** - * æžå¤Ÿå‡½æ•° - */ - ~ConnectionList() { if(_vConn) { delete[] _vConn; } } - - /** - * åˆå§‹åŒ–å¤§å° - * @param size - */ - void init(uint32_t size, uint32_t iIndex = 0); - - /** - * 获å–惟一ID - * - * @return unsigned int - */ - uint32_t getUniqId(); - - /** - * 添加连接 - * @param cPtr - * @param iTimeOutStamp - */ - void add(Connection *cPtr, time_t iTimeOutStamp); - - /** - * 刷新时间链 - * @param uid - * @param iTimeOutStamp, 超时时间点 - */ - void refresh(uint32_t uid, time_t iTimeOutStamp); - - /** - * æ£€æŸ¥è¶…æ—¶æ•°æ® - */ - void checkTimeout(time_t iCurTime); - - /** - * 获å–æŸä¸ªç›‘å¬ç«¯å£çš„连接 - * @param lfd - * @return vector - */ - vector getConnStatus(int lfd); - - /** - * 获å–æŸä¸€ä¸ªè¿žæŽ¥ - * @param p - * @return T - */ - Connection* get(uint32_t uid); - - /** - * 删除连接 - * @param uid - */ - void del(uint32_t uid); - - /** - * å¤§å° - * @return size_t - */ - size_t size(); - - protected: - typedef pair::iterator> list_data; - - /** - * 内部删除, ä¸åŠ é” - * @param uid - */ - void _del(uint32_t uid); - - protected: - /** - * æœåŠ¡ - */ - NetThread *_pEpollServer; - - /** - * 总计连接数 - */ - volatile uint32_t _total; - - /** - * 空闲链表 - */ - list _free; - - /** - * 空闲链元素个数 - */ - volatile size_t _free_size; - - /** - * 链接 - */ - list_data *_vConn; - - /** - * 超时链表 - */ - multimap _tl; - - /** - * 上次检查超时时间 - */ - time_t _lastTimeoutTime; - - /** - * 链接ID的魔数 - */ - uint32_t _iConnectionMagic; - }; - //////////////////////////////////////////////////////////////////////////// - public: - /** - * 构造函数 - */ - NetThread(TC_EpollServer *epollServer); - - /** - * æžæž„函数 - */ - virtual ~NetThread(); - - /** - * 绑定监å¬socket - * @param ls - */ - int bind(BindAdapterPtr &lsPtr); - - /** - * 网络线程执行函数 - */ - virtual void run(); - - /** - * åœæ­¢ç½‘络线程 - */ - void terminate(); - - /** - * 生æˆepoll - */ - void createEpoll(uint32_t iIndex = 0); - - /** - * åˆå§‹åŒ–udpç›‘å¬ - */ - void initUdp(); - - /** - * 是å¦æœåŠ¡ç»“æŸäº† - * - * @return bool - */ - bool isTerminate() const { return _bTerminate; } - - /** - * 获å–Epoller对象 - * @return TC_Epoller* - */ - TC_Epoller* getEpoller() { return &_epoller; } - - /** - * 获å–监å¬socketä¿¡æ¯ - * - * @return map - */ - map getListenSocketInfo(); - - /** - * æ ¹æ®å称获å–BindAdapter - * @param sName - * @return BindAdapterPtr - */ - BindAdapterPtr getBindAdapter(const string &sName); - - /** - * 关闭连接 - * @param uid - */ - void close(unsigned int uid); - - /** - * å‘é€æ•°æ® - * @param uid - * @param s - */ - void send(unsigned int uid, const string &s, const string &ip, uint16_t port); - - /** - * 获å–æŸä¸€ç›‘å¬ç«¯å£çš„连接数 - * @param lfd - * - * @return vector - */ - vector getConnStatus(int lfd); - - /** - * 获å–连接数 - * - * @return size_t - */ - size_t getConnectionCount() { return _list.size(); } - - /** - * 记录日志 - * @param s - */ - void debug(const string &s); - - /** - * INFO日志 - * @param s - */ - void info(const string &s); - - /** - * 记录错误日志 - * @param s - */ - void error(const string &s); - - /** - * 是å¦å¯ç”¨é˜²æ­¢ç©ºé“¾æŽ¥æ”»å‡»çš„机制 - * @param bEnable - */ - void EnAntiEmptyConnAttack(bool bEnable); - - /** - *设置空连接超时时间 - */ - void setEmptyConnTimeout(int timeout); - - /** - *设置udp的接收缓存区大å°ï¼Œå•ä½æ˜¯B,最å°å€¼ä¸º8192,最大值为DEFAULT_RECV_BUFFERSIZE - */ - void setUdpRecvBufferSize(size_t nSize=DEFAULT_RECV_BUFFERSIZE); - - /* - *当网络线程中listeners没有监å¬socket时,使用adapter中设置的最大连接数 - */ - void setListSize(size_t iSize) { _listSize += iSize; } - - /** - * å‘é€é˜Ÿåˆ—çš„å¤§å° - * @return size_t - */ - size_t getSendRspSize(); - - protected: - - /** - * 获å–连接 - * @param id - * - * @return ConnectionPtr - */ - Connection *getConnectionPtr(uint32_t uid) { return _list.get(uid); } - - /** - * 添加tcp链接 - * @param cPtr - * @param iIndex - */ - void addTcpConnection(Connection *cPtr); - - /** - * 添加udp连接 - * @param cPtr - * @param index - */ - void addUdpConnection(Connection *cPtr); - - /** - * 删除链接 - * @param cPtr - * @param bEraseList 是å¦æ˜¯è¶…时连接的删除 - * @param closeType 关闭类型,0:表示客户端主动关闭;1:æœåŠ¡ç«¯ä¸»åŠ¨å…³é—­;2:连接超时æœåŠ¡ç«¯ä¸»åŠ¨å…³é—­ - */ - void delConnection(Connection *cPtr, bool bEraseList = true,EM_CLOSE_T closeType=EM_CLIENT_CLOSE); - - /** - * å‘é€æ•°æ® - * @param cPtr - * @param buffer - */ - int sendBuffer(Connection *cPtr, const string &buffer, const string &ip, uint16_t port); - - /** - * å‘é€æ•°æ® - * @param cPtr - * @return int - */ - int sendBuffer(Connection *cPtr); - - /** - * 接收buffer - * @param cPtr - * @param buffer - * @return int - */ - int recvBuffer(Connection *cPtr, recv_queue::queue_type &v); - - /** - * 处ç†ç®¡é“æ¶ˆæ¯ - */ - void processPipe(); - - /** - * 处ç†ç½‘络请求 - */ - void processNet(const epoll_event &ev); - - /** - * åœæ­¢çº¿ç¨‹ - */ - void stopThread(); - - /** - * 新连接建立 - * @param fd - */ - bool accept(int fd); - - /** - * ç»‘å®šç«¯å£ - * @param ep - * @param s - */ - void bind(const TC_Endpoint &ep, TC_Socket &s); - - /** - * 空连接超时时间 - */ - int getEmptyConnTimeout() const; - - /** - *是å¦ç©ºè¿žæŽ¥æ£€æµ‹ - */ - bool IsEmptyConnCheck() const; - - //定义事件类型 - enum - { - ET_LISTEN = 1, - ET_CLOSE = 2, - ET_NOTIFY = 3, - ET_NET = 0, - }; - enum - { - MIN_EMPTY_CONN_TIMEOUT = 2*1000, /*空链接超时时间(ms)*/ - DEFAULT_RECV_BUFFERSIZE = 64*1024 /*缺çœæ•°æ®æŽ¥æ”¶buffer的大å°*/ - }; - //定义加入到网络线程的fd类别 - enum - { - TCP_CONNECTION = 0, - UDP_CONNECTION = 1, - }; - friend class BindAdapter; - friend class ConnectionList; - friend class TC_EpollServer; - - private: - /** - * æœåŠ¡ - */ - TC_EpollServer *_epollServer; - - /** - * 监å¬socket - */ - map _listeners; - - /** - * 没有监å¬socket的网络线程时,使用此å˜é‡ä¿å­˜adapterä¿¡æ¯ - */ - size_t _listSize; - - /** - * epoll - */ - TC_Epoller _epoller; - - /** - * åœæ­¢ - */ - bool _bTerminate; - - /** - * epoll是å¦å·²ç»åˆ›å»º - */ - bool _createEpoll; - - /** - * handle是å¦å·²ç»å¯åŠ¨ - */ - bool _handleStarted; - - /** - * 管é“(用于关闭æœåŠ¡) - */ - TC_Socket _shutdown; - - //管é“(用于通知有数æ®éœ€è¦å‘é€å°±) - TC_Socket _notify; - - /** - * 管ç†çš„连接链表 - */ - ConnectionList _list; - - /** - * å‘é€é˜Ÿåˆ— - */ - send_queue _sbuffer; - - /** - * BindAdapter是å¦æœ‰udpç›‘å¬ - */ - bool _hasUdp; - - /** - *空连接检测机制开关 - */ - bool _bEmptyConnAttackCheck; - - /** - * 空连接超时时间,å•ä½æ˜¯æ¯«ç§’,默认值2s, - * 该时间必须å°äºŽç­‰äºŽadapter自身的超时时间 - */ - int _iEmptyCheckTimeout; - - /** - * udp连接时接收包缓存大å°,针对所有udp接收缓存有效 - */ - size_t _nUdpRecvBufferSize; - - /** - * 属于该网络线程的内存池,ç›®å‰ä¸»è¦ç”¨äºŽå‘é€ä½¿ç”¨ - */ - TC_BufferPool* _bufferPool; - - /** - * 该网络线程的内存池所负责分é…的最å°å­—节和最大字节(2的幂å‘上å–æ•´) - */ - size_t _poolMinBlockSize; - size_t _poolMaxBlockSize; - - /** - * 该网络线程的内存池hold的最大字节 - */ - size_t _poolMaxBytes; - }; - //////////////////////////////////////////////////////////////////////////// -public: - /** - * 构造函数 - */ - TC_EpollServer(unsigned int iNetThreadNum = 1); - - /** - * æžæž„函数 - */ - ~TC_EpollServer(); - - /** - * 是å¦å¯ç”¨é˜²æ­¢ç©ºé“¾æŽ¥æ”»å‡»çš„机制 - * @param bEnable - */ - void EnAntiEmptyConnAttack(bool bEnable); - - /** - *设置空连接超时时间 - */ - void setEmptyConnTimeout(int timeout); - - /** - *设置NetThreadçš„å†…å­˜æ± ä¿¡æ¯ - */ - void setNetThreadBufferPoolInfo(size_t minBlock, size_t maxBlock, size_t maxBytes); - - /** - * 设置本地日志 - * @param plocalLogger - */ - void setLocalLogger(RollWrapperInterface *pLocalLogger) { _pLocalLogger = pLocalLogger; } - - /** - * 绑定到一个已ç»æœ‰çš„handle组上 - * @param groupName - * @param handleNum - * @param adapter - */ - void setHandleGroup(const string& groupName, BindAdapterPtr adapter) - { - map::iterator it = _handleGroups.find(groupName); - - if (it != _handleGroups.end()) - { - it->second->adapters[adapter->getName()] = adapter; - adapter->_handleGroup = it->second; - } - } - - /** - * 创建一个handle对象组,如果已ç»å­˜åœ¨åˆ™ç›´æŽ¥è¿”回 - * @param name - * @return HandlePtr - */ - template void setHandleGroup(const string& groupName, int32_t handleNum, BindAdapterPtr adapter) - { - map::iterator it = _handleGroups.find(groupName); - - if (it == _handleGroups.end()) - { - HandleGroupPtr hg = new HandleGroup(); - - hg->name = groupName; - - adapter->_handleGroup = hg; - - for (int32_t i = 0; i < handleNum; ++i) - { - HandlePtr handle = new T(); - - handle->setEpollServer(this); - - handle->setHandleGroup(hg); - - hg->handles.push_back(handle); - } - - _handleGroups[groupName] = hg; - - it = _handleGroups.find(groupName); - } - it->second->adapters[adapter->getName()] = adapter; - - adapter->_handleGroup = it->second; - } - - /** - * 选择网络线程 - * @param fd - */ - NetThread* getNetThreadOfFd(int fd) - { - return _netThreads[fd % _netThreads.size()]; - } - - /** - * 绑定监å¬socket - * @param ls - */ - int bind(BindAdapterPtr &lsPtr); - - /** - * å¯åŠ¨ä¸šåŠ¡å¤„ç†çº¿ç¨‹ - */ - void startHandle(); - - /** - * 生æˆepoll - */ - void createEpoll(); - - /** - * è¿è¡Œ - */ - void waitForShutdown(); - - /** - * åœæ­¢æœåŠ¡ - */ - void terminate(); - - /** - * 是å¦æœåŠ¡ç»“æŸäº† - * - * @return bool - */ - bool isTerminate() const { return _bTerminate; } - - /** - * æ ¹æ®å称获å–BindAdapter - * @param sName - * @return BindAdapterPtr - */ - BindAdapterPtr getBindAdapter(const string &sName); - - /** - * å‘网络线程添加连接 - */ - void addConnection(NetThread::Connection * cPtr, int fd, int iType); - - /** - * 关闭连接 - * @param uid - */ - void close(unsigned int uid, int fd); - - /** - * å‘é€æ•°æ® - * @param uid - * @param s - */ - void send(unsigned int uid, const string &s, const string &ip, uint16_t port, int fd); - - /** - * 获å–æŸä¸€ç›‘å¬ç«¯å£çš„连接数 - * @param lfd - * - * @return vector - */ - vector getConnStatus(int lfd); - - /** - * 获å–监å¬socketä¿¡æ¯ - * - * @return map - */ - map getListenSocketInfo(); - - /** - * 获å–所有连接的数目 - * - * @return size_t - */ - size_t getConnectionCount(); - - /** - * 记录日志 - * @param s - */ - void debug(const string &s); - - /** - * INFO日志 - * @param s - */ - void info(const string &s); - - /** - * 记录错误日志 - * @param s - */ - void error(const string &s); - - /** - * 获å–网络线程的数目 - */ - unsigned int getNetThreadNum() { return _netThreadNum; } - - /** - * 获å–ç½‘ç»œçº¿ç¨‹çš„æŒ‡é’ˆé›†åˆ - */ - vector getNetThread() { return _netThreads; } - - /** - * åœæ­¢çº¿ç¨‹ - */ - void stopThread(); - - /** - * 获å–所有业务线程的数目 - */ - unsigned int getLogicThreadNum(); - -protected: - - friend class BindAdapter; - -public: - - //统计æœåŠ¡ç«¯ç›¸åº”队列大å°çš„上报的对象 - PropertyReport * _pReportRspQueue; - -private: - /** - * 网络线程 - */ - std::vector _netThreads; - - /* - * 网络线程数目 - */ - unsigned int _netThreadNum; - - /* - * æœåŠ¡æ˜¯å¦åœæ­¢ - */ - bool _bTerminate; - - /* - * 业务线程是å¦å¯åŠ¨ - */ - bool _handleStarted; - - /** - * 本地循环日志 - */ - RollWrapperInterface *_pLocalLogger; - - /** - * 处ç†handle对象 - */ - map _handleGroups; -}; -typedef TC_AutoPtr TC_EpollServerPtr; -} - -#endif diff --git a/cpp/util/include/util/tc_epoller.h b/cpp/util/include/util/tc_epoller.h deleted file mode 100644 index 36e2963b2..000000000 --- a/cpp/util/include/util/tc_epoller.h +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_EPOLLER_H_ -#define __TC_EPOLLER_H_ - -#include -#include - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_epoller.h - * @brief epollæ“作å°è£…ç±» - */ -///////////////////////////////////////////////// - -/** - * @brief epolleræ“作类,已ç»é»˜è®¤é‡‡ç”¨äº†EPOLLETæ–¹å¼åšè§¦å‘ - */ -class TC_Epoller -{ -public: - - /** - * @brief 构造函数. - * - * @param bEt 默认是ET模å¼ï¼Œå½“状æ€å‘生å˜åŒ–的时候æ‰èŽ·å¾—通知 - */ - TC_Epoller(bool bEt = true); - - /** - * @brief æžå¤Ÿå‡½æ•°. - */ - ~TC_Epoller(); - - /** - * @brief 生æˆepollå¥æŸ„. - * - * @param max_connections epollæœåŠ¡éœ€è¦æ”¯æŒçš„最大连接数 - */ - void create(int max_connections); - - /** - * @brief 添加监å¬å¥æŸ„. - * - * @param fd å¥æŸ„ - * @param data 辅助的数æ®, å¯ä»¥åŽç»­åœ¨epoll_event中获å–到 - * @param event 需è¦ç›‘å¬çš„事件EPOLLIN|EPOLLOUT - * - */ - void add(int fd, long long data, __uint32_t event); - - /** - * @brief 修改å¥æŸ„事件. - * - * @param fd å¥æŸ„ - * @param data 辅助的数æ®, å¯ä»¥åŽç»­åœ¨epoll_event中获å–到 - * @param event 需è¦ç›‘å¬çš„事件EPOLLIN|EPOLLOUT - */ - void mod(int fd, long long data, __uint32_t event); - - /** - * @brief 删除å¥æŸ„事件. - * - * @param fd å¥æŸ„ - * @param data 辅助的数æ®, å¯ä»¥åŽç»­åœ¨epoll_event中获å–到 - * @param event 需è¦ç›‘å¬çš„事件EPOLLIN|EPOLLOUT - */ - void del(int fd, long long data, __uint32_t event); - - /** - * @brief 等待时间. - * - * @param millsecond 毫秒 - * @return int 有事件触å‘çš„å¥æŸ„æ•° - */ - int wait(int millsecond); - - /** - * @brief 获å–被触å‘的事件. - * - * @return struct epoll_event&被触å‘的事件 - */ - struct epoll_event& get(int i) { assert(_pevs != 0); return _pevs[i]; } - -protected: - - /** - * @brief 控制epoll,将EPOLL设为边缘触å‘EPOLLETæ¨¡å¼ - * @param fd å¥æŸ„,在create函数时被赋值 - * @param data 辅助的数æ®, å¯ä»¥åŽç»­åœ¨epoll_event中获å–到 - * @param event 需è¦ç›‘å¬çš„事件 - * @param op EPOLL_CTL_ADD: 注册新的fd到epfd中; - * EPOLL_CTL_MOD:修改已ç»æ³¨å†Œçš„fd的监å¬äº‹ä»¶ï¼› - * EPOLL_CTL_DEL:从epfd中删除一个fdï¼› - * - */ - void ctrl(int fd, long long data, __uint32_t events, int op); - -protected: - - /** - * epoll - */ - int _iEpollfd; - - /** - * 最大连接数 - */ - int _max_connections; - - /** - * 事件集 - */ - struct epoll_event *_pevs; - - /** - * 是å¦æ˜¯ETæ¨¡å¼ - */ - bool _et; -}; - -} -#endif - diff --git a/cpp/util/include/util/tc_eventfd.h b/cpp/util/include/util/tc_eventfd.h deleted file mode 100644 index 6596d4b3a..000000000 --- a/cpp/util/include/util/tc_eventfd.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_EVENT_FD_H_ -#define __TC_EVENT_FD_H_ - -#include -#include -#include -#include - -#ifdef SYS_eventfd -// since glibc 2.9 -#include -#else - -#if defined __x86_64 -#define SYS_eventfd 284 -#elif defined __i386 -#define SYS_eventfd 323 -#else -#error unknown architecture -#endif - -#ifndef O_CLOEXEC -#define O_CLOEXEC 02000000 /* set close_on_exec */ -#endif - -#define EFD_SEMAPHORE (1 << 0) -#define EFD_CLOEXEC O_CLOEXEC -#define EFD_NONBLOCK O_NONBLOCK - -inline int eventfd(unsigned int initval, int flags) -{ - return syscall(SYS_eventfd, initval, flags); -} - -typedef uint64_t eventfd_t; - -inline int eventfd_read(int fd, eventfd_t *value) -{ - return read(fd, value, sizeof(*value)) == sizeof(*value) ? 0 : -1; -} - -inline int eventfd_write(int fd, eventfd_t value) -{ - return write(fd, &value, sizeof(value)) == sizeof(value) ? 0 : -1; -} -#endif - -#endif - diff --git a/cpp/util/include/util/tc_ex.h b/cpp/util/include/util/tc_ex.h deleted file mode 100644 index ac88af5a2..000000000 --- a/cpp/util/include/util/tc_ex.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_EX_H -#define __TC_EX_H - -#include -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_ex.h -* @brief 异常类 -*/ -///////////////////////////////////////////////// - -/** -* @brief 异常类. -*/ -class TC_Exception : public exception -{ -public: - /** - * @brief 构造函数,æ供了一个å¯ä»¥ä¼ å…¥errno的构造函数, - * - * 异常抛出时直接获å–çš„é”™è¯¯ä¿¡æ¯ - * - * @param buffer å¼‚å¸¸çš„å‘Šè­¦ä¿¡æ¯ - */ - explicit TC_Exception(const string &buffer); - - /** - * @brief 构造函数,æ供了一个å¯ä»¥ä¼ å…¥errno的构造函数, - * - * 异常抛出时直接获å–çš„é”™è¯¯ä¿¡æ¯ - * - * @param buffer å¼‚å¸¸çš„å‘Šè­¦ä¿¡æ¯ - * @param err 错误ç , å¯ç”¨strerror获å–é”™è¯¯ä¿¡æ¯ - */ - TC_Exception(const string &buffer, int err); - - /** - * @brief æžå¤Ÿæ•°å‡½ - */ - virtual ~TC_Exception() throw(); - - /** - * @brief 错误信æ¯. - * - * @return const char* - */ - virtual const char* what() const throw(); - - /** - * @brief 获å–é”™è¯¯ç  - * - * @return æˆåŠŸèŽ·å–返回0 - */ - int getErrCode() { return _code; } - -private: - void getBacktrace(); - -private: - /** - * å¼‚å¸¸çš„ç›¸å…³ä¿¡æ¯ - */ - string _buffer; - - /** - * é”™è¯¯ç  - */ - int _code; - -}; - -} -#endif - diff --git a/cpp/util/include/util/tc_fcontext.h b/cpp/util/include/util/tc_fcontext.h deleted file mode 100644 index e6624c613..000000000 --- a/cpp/util/include/util/tc_fcontext.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef _TC_CONTEXT_FCONTEXT_H -#define _TC_CONTEXT_FCONTEXT_H - -// x86_64 -// test x86_64 before i386 because icc might -// define __i686__ for x86_64 too -#if defined(__x86_64__) || defined(__x86_64) \ - || defined(__amd64__) || defined(__amd64) \ - || defined(_M_X64) || defined(_M_AMD64) -# include "tc_fcontext_x86_64.h" -// i386 -#elif defined(i386) || defined(__i386__) || defined(__i386) \ - || defined(__i486__) || defined(__i586__) || defined(__i686__) \ - || defined(__X86__) || defined(_X86_) || defined(__THW_INTEL__) \ - || defined(__I86__) || defined(__INTEL__) || defined(__IA32__) \ - || defined(_M_IX86) || defined(_I86_) -# include "tc_fcontext_i386.h" -#else -# error "platform not supported" -#endif - -namespace tars -{ - -extern "C" intptr_t jump_fcontext( fcontext_t * ofc, fcontext_t const* nfc, intptr_t vp, bool preserve_fpu = true); -extern "C" fcontext_t * make_fcontext( void * sp, std::size_t size, void (* fn)( intptr_t) ); - -} - - -#endif diff --git a/cpp/util/include/util/tc_fcontext_i386.h b/cpp/util/include/util/tc_fcontext_i386.h deleted file mode 100644 index b4ffccf58..000000000 --- a/cpp/util/include/util/tc_fcontext_i386.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef _TC_CONTEXT_FCONTEXT_I386H -#define _TC_CONTEXT_FCONTEXT_I386H - -#include - -namespace tars -{ - -extern "C" -{ - -struct stack_t -{ - void * sp; - std::size_t size; - - stack_t() - : sp( 0) - , size( 0) - {} -}; - -struct fcontext_t -{ - uint32_t fc_greg[6]; - stack_t fc_stack; - uint32_t fc_freg[2]; - - fcontext_t() - : fc_greg() - , fc_stack() - , fc_freg() - {} -}; - -} - -} - -#endif diff --git a/cpp/util/include/util/tc_fcontext_x86_64.h b/cpp/util/include/util/tc_fcontext_x86_64.h deleted file mode 100644 index 5eaea510e..000000000 --- a/cpp/util/include/util/tc_fcontext_x86_64.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _TC_CONTEXT_FCONTEXT_X86_64_H -#define _TC_CONTEXT_FCONTEXT_X86_64_H - -#include - -namespace tars -{ - -extern "C" -{ - -struct stack_t -{ - void * sp; - std::size_t size; - - stack_t() - : sp(0) - , size(0) - {} -}; - -struct fp_t -{ - uint32_t fc_freg[2]; - - fp_t() - : fc_freg() - {} -}; - -struct fcontext_t -{ - uint64_t fc_greg[8]; - stack_t fc_stack; - fp_t fc_fp; - - fcontext_t() - : fc_greg() - , fc_stack() - , fc_fp() - {} -}; - -} - -} - -#endif diff --git a/cpp/util/include/util/tc_fifo.h b/cpp/util/include/util/tc_fifo.h deleted file mode 100644 index 7d922bab8..000000000 --- a/cpp/util/include/util/tc_fifo.h +++ /dev/null @@ -1,127 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_FIFO_H -#define __TC_FIFO_H -#include - - -namespace tars -{ - -///////////////////////////////////////////////// -/** - *@file tc_fifo.h - *@brief FIFOå°è£…ç±». - */ -///////////////////////////////////////////////// - -/** - *@brief 管é“æ“作类. - */ -class TC_Fifo -{ -public: - - /** - * @brief 管é“æ“作的枚举类型. - * 定义了对管é“çš„æ“作类型,EM_WRITE:写管é“,EM_READ ï¼šè¯»ç®¡é“ - */ - enum ENUM_RW_SET - { - EM_WRITE = 1, - EM_READ = 2 - }; - -public: - /** - * @brief 构造函数. - * - * @param bOwener : 是å¦æ‹¥æœ‰ç®¡é“,默认为ture - */ - TC_Fifo(bool bOwener = true); - - /** - * @brief æžæž„函数. - */ - ~TC_Fifo(); - -public: - /** - * @brief 打开FIFO. - * - * @param sPath è¦æ‰“开的FIFO文件的路径 - * @param enRW 管é“æ“作类型 - * @param mode 该FIFO文件的æƒé™ ,默认为å¯è¯»å¯å†™ - * @return 0-æˆåŠŸ,-1-失败 - */ - int open(const std::string & sPath, ENUM_RW_SET enRW, mode_t mode = 0777); - - /** - * @brief 关闭fifo - */ - void close(); - - /** - * @brief 获å–FIFO的文件æ述符. - * - * @return FIFO的文件æ述符 - */ - int fd() const { return _fd; } - - /** - * @brief 读数æ®, 当读å–æˆåŠŸæ—¶ï¼Œè¿”回实际读å–的字节数,如果返回的值是0,代表已ç»è¯»åˆ°æ–‡ä»¶çš„结æŸï¼›å°äºŽ0表示出现了错误 - * @param buffer 读å–的内容 - * @param max_size 读å–æ•°æ®çš„å¤§å° - * @return 读到的数æ®é•¿åº¦ ,å°äºŽ0则表示失败 - */ - int read(char * szBuff, const size_t sizeMax); - - /** - * @brief å‘管é“写数æ®. - * - * @param szBuff è¦å†™å…¥çš„æ•°æ® - * @param sizeBuffLen æ•°æ®çš„å¤§å° - * @return 大于0ï¼šè¡¨ç¤ºå†™äº†éƒ¨åˆ†æˆ–è€…å…¨éƒ¨æ•°æ® - * å°äºŽ0:表示出现错误 - */ - int write(const char * szBuff, const size_t sizeBuffLen); - -private: - /** - * FIFO文件的路径 - */ - std::string _sPathName; - - /** - * 是å¦æ‹¥æœ‰FIFO - */ - bool _bOwner; - - /** - * FIFO的文件的æ“作类型 - */ - ENUM_RW_SET _enRW; - - /** - * FIFO的文件æ述符 - */ - int _fd; -}; - -} -#endif - diff --git a/cpp/util/include/util/tc_file.h b/cpp/util/include/util/tc_file.h deleted file mode 100644 index 309f7ad6b..000000000 --- a/cpp/util/include/util/tc_file.h +++ /dev/null @@ -1,289 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_FILE_H_ -#define __TC_FILE_H_ - -#include -#include -#include -#include -#include -#include -#include -#include "util/tc_ex.h" -#include "util/tc_common.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_file.h - * @brief 文件处ç†ç±». - * - */ -///////////////////////////////////////////////// - - -/** -* @brief 文件异常类. -* -*/ -struct TC_File_Exception : public TC_Exception -{ - TC_File_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_File_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_File_Exception() throw(){}; -}; - -/** -* @brief 常用文件å处ç†å‡½æ•°. -* -*/ -class TC_File -{ -public: - - /** - * @brief 获å–文件大å°, 如果文件ä¸å­˜åœ¨, 则返回-1. - * - * @param sFullFileName 文件全路径(所在目录和文件å) - * @return ofstream::pos_typeç±»åž‹æ–‡ä»¶å¤§å° - */ - static ifstream::pos_type getFileSize(const string &sFullFileName); - - /** - * @brief 判断是å¦ä¸ºç»å¯¹è·¯å¾„, 忽略空格以'/'开头. - * - * @param sFullFileName 文件全路径(所在目录和文件å) - * @return ture是ç»å¯¹è·¯å¾„,false代表éžç»å¯¹è·¯å¾„ - */ - static bool isAbsolute(const string &sFullFileName); - - /** - * @brief 判断给定路径的文件是å¦å­˜åœ¨. - * 如果文件是符å·è¿žæŽ¥,则以符å·è¿žæŽ¥åˆ¤æ–­è€Œä¸æ˜¯ä»¥ç¬¦å·è¿žæŽ¥æŒ‡å‘的文件判断 - * @param sFullFileName 文件全路径 - * @param iFileType 文件类型, 缺çœS_IFREG - * @return true代表存在,fals代表ä¸å­˜åœ¨ - */ - static bool isFileExist(const string &sFullFileName, mode_t iFileType = S_IFREG); - - /** - * @brief 判断给定路径的文件是å¦å­˜åœ¨. - * 注æ„: 如果文件是符å·è¿žæŽ¥,则以符å·è¿žæŽ¥æŒ‡å‘的文件判断 - * @param sFullFileName 文件全路径 - * @param iFileType 文件类型, 缺çœS_IFREG - * @return true-存在,fals-ä¸å­˜åœ¨ - */ - static bool isFileExistEx(const string &sFullFileName, mode_t iFileType = S_IFREG); - - /** - * @brief 规则化目录å称, 把一些ä¸ç”¨çš„去掉, 例如./ç­‰. - * - * @param path 目录å称 - * @return 规范åŽçš„目录å称 - */ - static string simplifyDirectory(const string& path); - - /** - * @brief 创建目录, 如果目录已ç»å­˜åœ¨, 则也返回æˆåŠŸ. - * - * @param sFullPath è¦åˆ›å»ºçš„目录å称 - * @param iFlag æƒé™, 默认 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP| S_IXGRP | S_IROTH | S_IXOTH - * @return bool true-创建æˆåŠŸ ,false-创建失败 - */ - static bool makeDir(const string &sDirectoryPath, mode_t iFlag = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - - /** - *@brief 循环创建目录, 如果目录已ç»å­˜åœ¨, 则也返回æˆåŠŸ. - * - * @param sFullPath è¦åˆ›å»ºçš„目录å称 - * @param iFlag æƒé™, 默认 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH - * @return true-创建æˆåŠŸï¼Œfalse-创建失败 - */ - - static bool makeDirRecursive(const string &sDirectoryPath, mode_t iFlag = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - - /** - * @brief 设置文件是å¦å¯æ‰§è¡Œ. - * - * @param sFullFileName 文件全路径 - * @param canExecutable true表示å¯æ‰§è¡Œ, false代表ä¸å¯ä¹‹è¡Œ - * @return æˆåŠŸè¿”回0, 其他失败 - */ - static int setExecutable(const string &sFullFileName, bool canExecutable); - - /** - * @brief 判断文件是å¦å¯æ‰§è¡Œ. - * - * @param sFullFileName 文件全路径 - * @return true-å¯æ‰§è¡Œ, false-ä¸å¯æ‰§è¡Œ - */ - static bool canExecutable(const string &sFullFileName); - - /** - * @brief 删除一个文件或目录. - * - * @param sFullFileName 文件或者目录的全路径 - * @param bRecursive 如果是目录是å¦é€’归删除 - * @return 0-æˆåŠŸï¼Œå¤±è´¥å¯ä»¥é€šè¿‡errno查看失败的原因 - */ - static int removeFile(const string &sFullFileName, bool bRecursive); - - /** - * @brief 读å–文件到string - * 文件存在则返回文件数æ®ï¼Œä¸å­˜åœ¨æˆ–者读å–文件错误的时候, 返回为空 - * @param sFullFileName 文件å称 - * @return æ–‡ä»¶æ•°æ® - */ - static string load2str(const string &sFullFileName); - - /** - * @brief 读å–文件到vector - * 文件存在则返回文件数æ®ï¼Œä¸å­˜åœ¨æˆ–者读å–文件错误的时候, 返回为空 - * @param sFullFileName 文件å称 - */ - static void load2str(const string &sFullFileName, vector &buffer); - - /** - * @brief 写文件. - * - * @param sFullFileName 文件å称 - * @param sFileData 文件内容 - * @return - */ - static void save2file(const string &sFullFileName, const string &sFileData); - - /** - * @brief 写文件. - * - * @param sFullFileName 文件å - * @param sFileData æ•°æ®æŒ‡é’ˆ - * @param length 写入长度 - * @return 0-æˆåŠŸ,-1-失败 - */ - static int save2file(const string &sFullFileName, const char *sFileData, size_t length); - - /** - * @brief 获å–å‰å½“å¯æ‰§è¡Œæ–‡ä»¶è·¯å¾„. - * - * @return string å¯æ‰§è¡Œæ–‡ä»¶çš„路径全å称 - */ - static string getExePath(); - - /** - * @brief æå–文件å称 - *从一个完全文件å中去掉路径,例如:/usr/local/temp.gif获å–temp.gif - *@param sFullFileName 文件的完全å称 - *@return string æå–åŽçš„文件å称 - */ - static string extractFileName(const string &sFullFileName); - - /** - * @brief 从一个完全文件å中æå–文件的路径. - * - * 例如1: "/usr/local/temp.gif" 获å–"/usr/local/" - * 例如2: "temp.gif" èŽ·å– "./" - * @param sFullFileName 文件的完全å称 - * @return æå–åŽçš„文件路径 - */ - static string extractFilePath(const string &sFullFileName); - - /** - * @brief æå–文件扩展å. - * - * 例如1: "/usr/local/temp.gif" 获å–"gif" - * 例如2: "temp.gif" 获å–"gif" - *@param sFullFileName 文件å称 - *@return 文件扩展å - */ - static string extractFileExt(const string &sFullFileName); - - /** - * @brief æå–文件å称,去掉扩展å. - * 例如1: "/usr/local/temp.gif" 获å–"/usr/local/temp" - * 例如2: "temp.gif" 获å–"temp" - * @param sFullFileName 文件å称 - * @return 去掉扩展å的文件å称 - */ - static string excludeFileExt(const string &sFullFileName); - - /** - * @brief 替æ¢æ–‡ä»¶æ‰©å±•å - * - * 改å˜æ–‡ä»¶ç±»åž‹ï¼Œå¦‚果无扩展å,则加上扩展å =?1: - * 例如1:"/usr/temp.gif" 替 æ¢ "jpg" 得到"/usr/temp.jpg" - * 例如2: "/usr/local/temp" 替 æ¢ "jpg" 得到"/usr/local/temp.jpg" - * @param sFullFileName 文件å称 - * @param sExt 扩展å - * @return 替æ¢æ‰©å±•ååŽçš„文件å - */ - static string replaceFileExt(const string &sFullFileName, const string &sExt); - - /** - * @brief 从一个url中获å–完全文件å. - * - * 获å–除http://外,第一个'/'åŽé¢çš„所有字符 - * 例如1:http://www.qq.com/tmp/temp.gif 获å–tmp/temp.gif - * 例如2:www.qq.com/tmp/temp.gif 获å–tmp/temp.gif - * 例如3:/tmp/temp.gif 获å–tmp/temp.gif - * @param sUrl url字符串 - * @return 文件å称 - */ - static string extractUrlFilePath(const string &sUrl); - - /** - * @brief é历文件时确定是å¦é€‰æ‹©. - * - * @return 1-选择, 0-ä¸é€‰æ‹© - */ - typedef int (*FILE_SELECT)(const dirent *); - - /** - * @brief 扫æ一个目录. - * - * @param sFilePath 需è¦æ‰«æ的路径 - * @param vtMatchFiles 返回的文件å矢é‡è¡¨ - * @param f 匹é…函数,为NULLè¡¨ç¤ºæ‰€æœ‰æ–‡ä»¶éƒ½èŽ·å– - * @param iMaxSize 最大文件个数,iMaxSize <=0æ—¶,返回所有匹é…文件 - * @return 文件个数 - */ - static size_t scanDir(const string &sFilePath, vector &vtMatchFiles, FILE_SELECT f = NULL, int iMaxSize = 0); - - /** - * @brief é历目录, 获å–目录下é¢çš„所有文件和å­ç›®å½•. - * - * @param path 需è¦é历的路径 - * @param files 目标路径下é¢æ‰€æœ‰æ–‡ä»¶ - * @param bRecursive 是å¦é€’å½’å­ç›®å½• - * - **/ - static void listDirectory(const string &path, vector &files, bool bRecursive); - - /** - * @brief å¤åˆ¶æ–‡ä»¶æˆ–目录. - * 将文件或者目录从sExistFileå¤åˆ¶åˆ°sNewFile - * @param sExistFile å¤åˆ¶çš„文件或者目录æºè·¯å¾„ - * @param sNewFile å¤åˆ¶çš„文件或者目录目标路径 - * @param bRemove 是å¦å…ˆåˆ é™¤sNewFileå†copy ,防止Textfile busy导致å¤åˆ¶å¤±è´¥ - * @return - */ - static void copyFile(const string &sExistFile, const string &sNewFile,bool bRemove = false); -}; - -} -#endif // TC_FILE_H diff --git a/cpp/util/include/util/tc_file_mutex.h b/cpp/util/include/util/tc_file_mutex.h deleted file mode 100644 index 502e93576..000000000 --- a/cpp/util/include/util/tc_file_mutex.h +++ /dev/null @@ -1,162 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_FILE_MUTEX_H -#define __TC_FILE_MUTEX_H - -#include -#include "util/tc_lock.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_file_mutex.h -* @brief 文件é”ç±». -* -*/ -///////////////////////////////////////////////// - -/** -* @brief 异常类 -*/ -struct TC_FileMutex_Exception : public TC_Lock_Exception -{ - TC_FileMutex_Exception(const string &buffer) : TC_Lock_Exception(buffer){}; - TC_FileMutex_Exception(const string &buffer, int err) : TC_Lock_Exception(buffer, err){}; - ~TC_FileMutex_Exception() throw() {}; -}; - -/** - * @brief 文件é”, 注æ„:åªèƒ½åœ¨è¿›ç¨‹é—´åŠ é”. - */ -class TC_FileMutex -{ -public: - - /** - * @brief 构造函数. - */ - TC_FileMutex(); - - /** - * @brief æžå¤Ÿå‡½æ•°. - */ - virtual ~TC_FileMutex(); - - /** - * @brief åˆå§‹åŒ–文件é”. - * - * @param filename 欲æ“作的文件的åå­— - */ - void init(const std::string& filename); - - /** - * @brief 加读é”. - * - *@return 0-æˆåŠŸåŠ é”,-1-加é”失败 - */ - int rlock(); - - /** - * @brief 解读é”. - * - * @return 0-æˆåŠŸè§£é”,-1-解é”失败 - */ - int unrlock(); - - /** - * @brief å°è¯•è¯»é”. - * - * @throws TC_FileMutex_Exception - * @return 加é”æˆåŠŸåˆ™è¿”回false, å¦åˆ™è¿”回false - */ - bool tryrlock(); - - /** - * @brief 加写é”. - * - * @return int - */ - int wlock(); - - /** - * @brief 解写é”. - */ - int unwlock(); - - /** - * @brief å°è¯•å†™é”. - * - * @return bool,加é”æˆåŠŸåˆ™è¿”回false, å¦åˆ™è¿”回false - * @throws TC_FileMutex_Exception - */ - bool trywlock(); - - /** - * @brief 写é”. - * - * @return int, 0 正确 - */ - int lock(){return wlock();}; - - /** - * @brief 解写é”. - */ - int unlock(); - - /** - * @brief å°è¯•è§£é”. - * - * @throws TC_FileMutex_Exception - * @return int, 0 正确 - */ - bool trylock() {return trywlock();}; - -protected: - /** - * @brief 设置é”. - * - * @param fd 欲设置的文件æè¿°è¯ - * @param cmd 欲æ“作的指令 - * @param type 三ç§çŠ¶æ€ï¼Œåˆ†åˆ«ä¸ºF_RDLCK ,F_WRLCK ,F_UNLCK - * @param offset åç§»é‡ - * @param whence é”定的起始ä½ç½®ï¼Œä¸‰ç§æ–¹å¼ - * @param len é”å®šåŒºåŸŸçš„å¤§å° - * @return int:æˆåŠŸåˆ™è¿”回0,若有错误则返回-1. - */ - int lock(int fd, int cmd, int type, off_t offset, int whence, off_t len); - - /** - * @brief 是å¦è¢«å…¶ä»–进程é”了. - * - * @param fd 欲设置的文件æè¿°è¯ - * @param type 三ç§çŠ¶æ€ï¼Œåˆ†åˆ«ä¸ºF_RDLCK ,F_WRLCK ,F_UNLCK - * @param offset åç§»é‡ - * @param whence é”定的起始ä½ç½®ï¼Œä¸‰ç§æ–¹å¼ - * @param len é”å®šåŒºåŸŸçš„å¤§å° - * @return bool 有所返回true,无é”返回false. - */ - bool hasLock(int fd, int type, off_t offset, int whence, off_t len); - -private: - int _fd; -}; - -} - -#endif - diff --git a/cpp/util/include/util/tc_functor.h b/cpp/util/include/util/tc_functor.h deleted file mode 100644 index 4526b360e..000000000 --- a/cpp/util/include/util/tc_functor.h +++ /dev/null @@ -1,999 +0,0 @@ -#ifndef __TC_FUNCTOR_H -#define __TC_FUNCTOR_H - -#include -#include -#include "util/tc_loki.h" - -using namespace std; - -namespace tars -{ -/////////////////////////////////////////////////////////////////////////////////////////////////// -/** - * @file tc_functor.h - * @brief 通用仿函数类 - - * å‚考C++设计新æ€ç»´, loki库的设计 - * Functor对调用的å°è£…,å¯ä»¥å¯¹ä»¥ä¸‹å‡ ç§è°ƒç”¨å°è£…: - * C风格的函数调用 - * C风格的函数指针 - * C++风格的函数对象 - * 指å‘ç±»æˆå‘˜å‡½æ•°çš„指针调用 - * å°è£…以åŽçš„调用形å¼å¯ä»¥å˜æˆä¸¤ç§: - * A 仿函数对象调用方å¼, å³å¯¹ä¸Šè¿°çš„几ç§æ–¹å¼éƒ½å¯ä»¥åœ¨å³ä¾§æ·»åŠ ä¸€å¯¹åœ†æ‹¬å·,并在括å·å†…部放一组åˆé€‚çš„å‚æ•°æ¥è°ƒç”¨ - * B 把整个调用过程å°è£…一个函数对象, éšæ—¶è°ƒç”¨, 调用的时候ä¸ç”¨ä¼ å…¥å‚æ•°, 而在在对象建立时就传入了å‚æ•° - * C函数调用: - * void TestFunction3(const string &s, int i) - * - * { - * - * cout << "TestFunction3('" << s << "', '" << i << "')" << endl; - * - * } - * - * TC_Functor::Result> cmd3(TestFunction3); - * string s3("s3"); - * cmd3(s3, 10); - * C函数调用用TC_FunctorWrapperå°è£…:* - * TC_FunctorWrapper::Result> > fwrapper3(cmd3, s3, 10); - * fwrapper3(); - * 说明: - * void : 函数的返回值 - * C++函数对象调用: - * struct TestFunctor - * { - * - * void operator()(const string &s, int i) - * - * { - * - * cout << "TestFunctor::operator(" << s << "," << i << ") called" << endl; - * - * } - * - * } - * - * TestFunctor f; - * - * TC_Functor::Result> cmd3(f); - * - * string s3("s3"); - * - * cmd3(s3, 10); - * - * C++函数对象调用用TC_FunctorWrapperå°è£…: - * - * TC_FunctorWrapper::Result> > fwrapper3(cmd3, s3, 10); - * - * fwrapper3(); - * - * 指å‘ç±»æˆå‘˜å‡½æ•°çš„调用 - * struct TestMember - * { - * void mem3(const string &s, int i) - * { - * cout << "TestMember::mem3(" << s << "," << i << ") called" << endl; - * } - * } - * TC_Functor::Result> cmd3(&tm, &TestMember::mem3); - * cmd3("a", 33); - * 指å‘ç±»æˆå‘˜å‡½æ•°çš„调用用TC_FunctorWrapperå°è£…: - * TC_FunctorWrapper::Result> > fwrapper3(cmd3, "a", 10); - * fwrapper3(); - * 注æ„所有的TC_FunctorWrapper的调用, 在具体调用的时候是ä¸å¸¦å‚æ•°çš„, å‚数在构造的时候已ç»ä¼ è¿›åŽ»äº† - /////////////////////////////////////////////////////////////////////////////////////////////////// - * - * 对于C风格的函数调用, åŒå称的函数通常åªæœ‰ä¸€ä¸ª, 如果有多个相åŒå称的函数 - *ä¸åŒå‚数类型的函数, 则在构造TC_Functor的时候需è¦å¼ºåˆ¶ç±»åž‹è½¬æ¢, 如下: - * void TestFunction1() - * { - * cout << "TestFunction1()" << endl; - * } - * void TestFunction1(char c) - * { - * cout << "TestFunction1(" << c << ")" << endl; - * } - * typedef void (*TpFunc)(); - * TC_Functor cmd1(static_cast(TestFunction1)); - * cmd1(); - * typedef void (*TpFunc1)(char); - * TC_Functor::Result> - * cmd11(static_cast(TestFunction1)); cmd11('c'); - /////////////////////////////////////////////////////////////////////////////////////////////////// - * - * 对于类æˆå‘˜å‡½æ•°çš„指针调用, åŒå称的函数通常åªæœ‰ä¸€ä¸ª, 如果有多个相åŒå称的函数 - * ä¸åŒå‚数类型的函数, 则在构造TC_Functor的时候需è¦å¼ºåˆ¶ç±»åž‹è½¬æ¢, 如下: - * class TestMember - * { - * public: - * void mem1() - * { - * cout << "TestMember::mem1() called" << endl; - * } - * void mem1(char c) - * { - * cout << "TestMember::mem1('" << c << "') called" << endl; - * } - * }; - - * TestMember tm; - - * typedef void (TestMember::*TpMem)(); - - * typedef void (TestMember::*TpMem1)(char); - - * TC_Functor cmd1(&tm, static_cast(&TestMember::mem1)); - * cmd1(); - * - * typedef void (TestMember::*TpMem1)(char); - - * TC_Functor::Result> cmd11(&tm, - - * static_cast(&TestMember::mem1)); cmd11('c'); - * - * 具体示例请å‚è§demo/test_tc_functor.cpp. - */ - -template -class TC_FunctorImp -{ -public: - virtual R operator()() = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()() = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4, - typename TL::TypeTraits::ReferenceType p5) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4, - typename TL::TypeTraits::ReferenceType p5, - typename TL::TypeTraits::ReferenceType p6) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4, - typename TL::TypeTraits::ReferenceType p5, - typename TL::TypeTraits::ReferenceType p6, - typename TL::TypeTraits::ReferenceType p7) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4, - typename TL::TypeTraits::ReferenceType p5, - typename TL::TypeTraits::ReferenceType p6, - typename TL::TypeTraits::ReferenceType p7, - typename TL::TypeTraits::ReferenceType p8) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4, - typename TL::TypeTraits::ReferenceType p5, - typename TL::TypeTraits::ReferenceType p6, - typename TL::TypeTraits::ReferenceType p7, - typename TL::TypeTraits::ReferenceType p8, - typename TL::TypeTraits::ReferenceType p9) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -template -class TC_FunctorImp -{ -public: - virtual R operator()(typename TL::TypeTraits::ReferenceType p1, - typename TL::TypeTraits::ReferenceType p2, - typename TL::TypeTraits::ReferenceType p3, - typename TL::TypeTraits::ReferenceType p4, - typename TL::TypeTraits::ReferenceType p5, - typename TL::TypeTraits::ReferenceType p6, - typename TL::TypeTraits::ReferenceType p7, - typename TL::TypeTraits::ReferenceType p8, - typename TL::TypeTraits::ReferenceType p9, - typename TL::TypeTraits::ReferenceType p10) = 0; - virtual TC_FunctorImp* clone() const = 0; - virtual ~TC_FunctorImp() {}; -}; - -/** - * å°è£…对函数对象以åŠå‡½æ•°çš„调用 - */ -template -class TC_FunctorHandler : public TC_FunctorImp -{ -public: - typedef typename ParentFunctor::ResultType ResultType; - - TC_FunctorHandler(Fun fun) : _fun(fun) - { - - } - - TC_FunctorHandler *clone() const - { - return new TC_FunctorHandler(*this); - } - - ResultType operator()() - { return _fun(); } - - ResultType operator()(typename ParentFunctor::Reference1 p1) - { return _fun(p1); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2) - { return _fun(p1, p2); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3) - { return _fun(p1, p2, p3); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4) - { return _fun(p1, p2, p3, p4); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5) - { return _fun(p1, p2, p3, p4, p5); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6) - { return _fun(p1, p2, p3, p4, p5, p6); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7) - { return _fun(p1, p2, p3, p4, p5, p6, p7); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8) - { return _fun(p1, p2, p3, p4, p5, p6, p7, p8); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8, - typename ParentFunctor::Reference9 p9) - { return _fun(p1, p2, p3, p4, p5, p6, p7, p8, p9); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8, - typename ParentFunctor::Reference9 p9, - typename ParentFunctor::Reference10 p10) - { return _fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } - -private: - Fun _fun; -}; - -template -/** - * @brief å°è£…对æˆå‘˜å‡½æ•°çš„调用 - */ -class TC_MemFunHandler - : public TC_FunctorImp -{ -public: - typedef typename ParentFunctor::ResultType ResultType; - - TC_MemFunHandler(const PointerToObj &pObj, PointerToMemFun pMemFn) - : _pObj(pObj), _pMemFn(pMemFn) - { - } - - TC_MemFunHandler* clone() const - { - return new TC_MemFunHandler(*this); - } - - ResultType operator()() - { return (_pObj->*_pMemFn)(); } - - ResultType operator()(typename ParentFunctor::Reference1 p1) - { return (_pObj->*_pMemFn)(p1); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2) - { return (_pObj->*_pMemFn)(p1, p2); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3) - { return (_pObj->*_pMemFn)(p1, p2, p3); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4, p5); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4, p5, p6); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4, p5, p6, p7); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8, - typename ParentFunctor::Reference9 p9) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9); } - - ResultType operator()(typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8, - typename ParentFunctor::Reference9 p9, - typename ParentFunctor::Reference10 p10) - { return (_pObj->*_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } - -private: - PointerToObj _pObj; - PointerToMemFun _pMemFn; -}; - -template -class TC_FunctorWrapper; - - -template -/** - * @brief 函数对象类, å¯ä»¥å°è£…对: 函数对象, 函数, æˆå‘˜å‡½æ•° - * 的调用 - */ -class TC_Functor -{ -public: - typedef R ResultType; - typedef TList ParamList; - - /**定义å°è£…类型*/ - typedef TC_FunctorWrapper > wrapper_type; - - /**定义类型列表中æ¯ä¸ªå‚数的原类型*/ - typedef typename TL::TypeTraits::Result>::ParameterType Param1; - typedef typename TL::TypeTraits::Result>::ParameterType Param2; - typedef typename TL::TypeTraits::Result>::ParameterType Param3; - typedef typename TL::TypeTraits::Result>::ParameterType Param4; - typedef typename TL::TypeTraits::Result>::ParameterType Param5; - typedef typename TL::TypeTraits::Result>::ParameterType Param6; - typedef typename TL::TypeTraits::Result>::ParameterType Param7; - typedef typename TL::TypeTraits::Result>::ParameterType Param8; - typedef typename TL::TypeTraits::Result>::ParameterType Param9; - typedef typename TL::TypeTraits::Result>::ParameterType Param10; - - /**定义类型列表中æ¯ä¸ªå‚数的引用类型*/ - typedef typename TL::TypeTraits::ReferenceType Reference1; - typedef typename TL::TypeTraits::ReferenceType Reference2; - typedef typename TL::TypeTraits::ReferenceType Reference3; - typedef typename TL::TypeTraits::ReferenceType Reference4; - typedef typename TL::TypeTraits::ReferenceType Reference5; - typedef typename TL::TypeTraits::ReferenceType Reference6; - typedef typename TL::TypeTraits::ReferenceType Reference7; - typedef typename TL::TypeTraits::ReferenceType Reference8; - typedef typename TL::TypeTraits::ReferenceType Reference9; - typedef typename TL::TypeTraits::ReferenceType Reference10; - -public: - TC_Functor() - { - } - - TC_Functor(const TC_Functor &functor) - : _spImpl(functor._spImpl->clone()) - { - } - - TC_Functor& operator=(const TC_Functor &functor) - { - if(this != &functor) - { - _spImpl = std::auto_ptr(functor._spImpl->clone()); - } - - return (*this); - } - - template - TC_Functor(Fun fun) - : _spImpl(new TC_FunctorHandler(fun)) - { - } - - template - TC_Functor(const PointerToObj &pObj, PointerToMemFun pMemFn) - : _spImpl(new TC_MemFunHandler(pObj, pMemFn)) - { - } - - // 判断是å¦å·²ç»è®¾ç½®äº†functor - // å¯ä»¥è¿™æ ·ä½¿ç”¨if(functor) - operator bool() const - { - return _spImpl.get() ? true : false; - } - - R operator()() - { - return (*_spImpl)(); - } - - R operator()(Reference1 p1) - { - return (*_spImpl)(p1); - } - - R operator()(Reference1 p1, Reference2 p2) - { - return (*_spImpl)(p1, p2); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3) - { - return (*_spImpl)(p1, p2, p3); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4) - { - return (*_spImpl)(p1, p2, p3, p4); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4, Reference5 p5) - { - return (*_spImpl)(p1, p2, p3, p4, p5); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4, Reference5 p5, Reference6 p6) - { - return (*_spImpl)(p1, p2, p3, p4, p5, p6); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4, Reference5 p5, Reference6 p6, Reference7 p7) - { - return (*_spImpl)(p1, p2, p3, p4, p5, p6, p7); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4, Reference5 p5, Reference6 p6, Reference7 p7, Reference8 p8) - { - return (*_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4, Reference5 p5, Reference6 p6, Reference7 p7, Reference8 p8, Reference9 p9) - { - return (*_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9); - } - - R operator()(Reference1 p1, Reference2 p2, Reference3 p3, Reference4 p4, Reference5 p5, Reference6 p6, Reference7 p7, Reference8 p8, Reference9 p9, Reference10 p10) - { - return (*_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); - } - - -private: - template - friend class TC_FunctorWrapper; - - typedef TC_FunctorImp Impl; - - std::auto_ptr _spImpl; -}; - -/** - * @brief wapper基类 - */ -class TC_FunctorWrapperInterface -{ -public: - virtual void operator()() = 0; - virtual ~TC_FunctorWrapperInterface(){} -}; - -template - -/** - * @brief 对TC_Functor进行å°è£…, å¯ä»¥è®©TC_Functoråšåˆ°äº‹åŽè¢«è°ƒç”¨ - */ -class TC_FunctorWrapper : public TC_FunctorWrapperInterface -{ -public: - typedef typename ParentFunctor::ResultType R; - typedef typename ParentFunctor::ParamList TList; - - /** - * @brief æ‹·è´æž„造 - * @param fw - */ - TC_FunctorWrapper(const TC_FunctorWrapper &fw) - : _spImpl(fw._spImpl->clone()) - , _p1(fw._p1) - , _p2(fw._p2) - , _p3(fw._p3) - , _p4(fw._p4) - , _p5(fw._p5) - , _p6(fw._p6) - , _p7(fw._p7) - , _p8(fw._p8) - , _p9(fw._p9) - , _p10(fw._p10) - { - } - - /** - * @brief 构造函数 - * @param tf - */ - TC_FunctorWrapper(ParentFunctor &tf) - : _spImpl(tf._spImpl->clone()) - { - } - - /** - * @brief 一个å‚数调用的构造函数 - * @param tf - * @param p1 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1) - : _spImpl(tf._spImpl->clone()), _p1(p1) - { - } - - /** - * @brief 两个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2) - { - } - - /** - * @brief 三个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3) - { - } - - /** - * @brief 四个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4) - { - } - - /** - * @brief 五个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - * @param p5 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4), _p5(p5) - { - } - - /** - * @brief 六个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - * @param p5 - * @param p6 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4), _p5(p5), _p6(p6) - { - } - - /** - * @brief 七个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - * @param p5 - * @param p6 - * @param p7 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4), _p5(p5), _p6(p6), _p7(p7) - { - } - - /** - * @brief 八个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - * @param p5 - * @param p6 - * @param p7 - * @param p8 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4), _p5(p5), _p6(p6), _p7(p7), _p8(p8) - { - } - - /** - * @brief ä¹ä¸ªå‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - * @param p5 - * @param p6 - * @param p7 - * @param p8 - * @param p9 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8, - typename ParentFunctor::Reference9 p9) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4), _p5(p5), _p6(p6), _p7(p7), _p8(p8), _p9(p9) - { - } - - /** - * @brief å个å‚数调用的构造函数 - * @param tf - * @param p1 - * @param p2 - * @param p3 - * @param p4 - * @param p5 - * @param p6 - * @param p7 - * @param p8 - * @param p9 - * @param p10 - */ - TC_FunctorWrapper(ParentFunctor &tf, - typename ParentFunctor::Reference1 p1, - typename ParentFunctor::Reference2 p2, - typename ParentFunctor::Reference3 p3, - typename ParentFunctor::Reference4 p4, - typename ParentFunctor::Reference5 p5, - typename ParentFunctor::Reference6 p6, - typename ParentFunctor::Reference7 p7, - typename ParentFunctor::Reference8 p8, - typename ParentFunctor::Reference9 p9, - typename ParentFunctor::Reference10 p10) - : _spImpl(tf._spImpl->clone()), _p1(p1), _p2(p2), _p3(p3), _p4(p4), _p5(p5), _p6(p6), _p7(p7), _p8(p8), _p9(p9), _p10(p10) - { - } - - /** - * @brief 具体调用 - * @param tf - * @param p1 - */ - virtual R operator()() - { - return todo(TL::Int2Type::value>()); - } - - virtual ~TC_FunctorWrapper(){ } - -protected: - /** - * @brief 赋值函数ä¸å®šä¹‰, åªå£°æ˜Ž - * @param fw - * - * @return TC_FunctorWrapper& - */ - TC_FunctorWrapper& operator=(const TC_FunctorWrapper &fw); - - R todo(TL::Int2Type<0>) - { - return (*_spImpl)(); - } - - R todo(TL::Int2Type<1>) - { - return (*_spImpl)(_p1); - } - - R todo(TL::Int2Type<2>) - { - return (*_spImpl)(_p1, _p2); - } - - R todo(TL::Int2Type<3>) - { - return (*_spImpl)(_p1, _p2, _p3); - } - - R todo(TL::Int2Type<4>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4); - } - - R todo(TL::Int2Type<5>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4, _p5); - } - - R todo(TL::Int2Type<6>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4, _p5, _p6); - } - - R todo(TL::Int2Type<7>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4, _p5, _p6, _p7); - } - - R todo(TL::Int2Type<8>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8); - } - - R todo(TL::Int2Type<9>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9); - } - - R todo(TL::Int2Type<10>) - { - return (*_spImpl)(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10); - } - -protected: - typedef TC_FunctorImp Impl; - - std::auto_ptr _spImpl; - - /* - typename TL::TypeTraits::ReferenceTypeEx _p1; - typename TL::TypeTraits::ReferenceTypeEx _p2; - typename TL::TypeTraits::ReferenceTypeEx _p3; - typename TL::TypeTraits::ReferenceTypeEx _p4; - typename TL::TypeTraits::ReferenceTypeEx _p5; - typename TL::TypeTraits::ReferenceTypeEx _p6; - typename TL::TypeTraits::ReferenceTypeEx _p7; - typename TL::TypeTraits::ReferenceTypeEx _p8; - typename TL::TypeTraits::ReferenceTypeEx _p9; - typename TL::TypeTraits::ReferenceTypeEx _p10; - */ - - typename TL::TypeTraits::ParameterType _p1; - typename TL::TypeTraits::ParameterType _p2; - typename TL::TypeTraits::ParameterType _p3; - typename TL::TypeTraits::ParameterType _p4; - typename TL::TypeTraits::ParameterType _p5; - typename TL::TypeTraits::ParameterType _p6; - typename TL::TypeTraits::ParameterType _p7; - typename TL::TypeTraits::ParameterType _p8; - typename TL::TypeTraits::ParameterType _p9; - typename TL::TypeTraits::ParameterType _p10; -}; - -} - -#endif - diff --git a/cpp/util/include/util/tc_gzip.h b/cpp/util/include/util/tc_gzip.h deleted file mode 100644 index 2e590f9e4..000000000 --- a/cpp/util/include/util/tc_gzip.h +++ /dev/null @@ -1,185 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_GZIP_H -#define __TC_GZIP_H - -#include -#include -#include -#include -#include - -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_gzip.h -* @brief gzipç±», å°è£…çš„zlib库 -*/ - -///////////////////////////////////////////////// - -/** -* @brief 该类æ供标准GZIP压缩和解压算法 -*/ -class TC_GZip -{ -protected: - struct Output2Vector { - Output2Vector(vector& buffer) : _buffer(buffer) - { - _buffer.clear(); - } - void operator ()(char *begin, size_t length) - { - _buffer.insert(_buffer.end(), begin, begin + length); - } - - vector& _buffer; - }; - - struct Output2String { - Output2String(string& buffer) : _buffer(buffer) - { - _buffer.clear(); - } - void operator ()(char *begin, size_t length) - { - _buffer.append(begin, length); - } - - string& _buffer; - }; - -public: - /** - * @brief 对数æ®è¿›è¡ŒåŽ‹ç¼© - * - * @param src 需è¦åŽ‹ç¼©çš„æ•°æ® - * @param length æ•°æ®é•¿åº¦ - * @param buffer 输出buffer - * @return bool æˆåŠŸå¤±è´¥ - */ - static bool compress(const char *src, size_t length, vector& buffer); - - /** - * @brief 对数æ®è¿›è¡Œè§£åŽ‹ - * - * @param src 需è¦è§£åŽ‹çš„æ•°æ® - * @param length æ•°æ®é•¿åº¦ - * @param buffer 输出buffer - * @return bool æˆåŠŸå¤±è´¥ - */ - static bool uncompress(const char *src, size_t length, vector& buffer) - { - Output2Vector output(buffer); - - return uncompress(src, length, output); - } - - /** - * @brief 对数æ®è¿›è¡Œè§£åŽ‹ - * - * @param src 需è¦è§£åŽ‹çš„æ•°æ® - * @param length æ•°æ®é•¿åº¦ - * @param buffer 输出buffer - * @return bool æˆåŠŸå¤±è´¥ - */ - static bool uncompress(const char *src, size_t length, string& buffer) - { - Output2String output(buffer); - - return uncompress(src, length, output); - } - - /** - * @brief 对数æ®è¿›è¡Œåˆ†ç‰‡è§£åŽ‹, - * æ¯æ¬¡è§£åŽ‹çš„æ•°æ®è°ƒç”¨Output输出 - * - * @param src 需è¦è§£åŽ‹çš„æ•°æ® - * @param length æ•°æ®é•¿åº¦ - * @param o 输出buffer的函数对象 - * struct Output - * { - * void operator()(char *begin, size_t - * length); - * } - * @return bool æˆåŠŸå¤±è´¥ - */ - template - static bool uncompress(const char *src, size_t length, Output& o) - { -// buffer.clear(); - - z_stream strm; - - /* allocate inflate state */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = 0; - strm.next_in = Z_NULL; - - int ret = inflateInit2(&strm, 47); - - if (ret != Z_OK) - { - return false; - } - - strm.avail_in = length; - strm.next_in = (unsigned char *)src; - - static size_t CHUNK = 1024 * 256; - unsigned char *out = new unsigned char[CHUNK]; - - /* run inflate() on input until output buffer not full */ - do - { - strm.avail_out = CHUNK; - strm.next_out = out; - - ret = inflate(&strm, Z_NO_FLUSH); - - assert(ret != Z_STREAM_ERROR); /* state not clobbered */ - switch (ret) - { - case Z_NEED_DICT: - ret = Z_DATA_ERROR; /* and fall through */ - case Z_DATA_ERROR: - case Z_MEM_ERROR: - inflateEnd(&strm); - delete[] out; - return false; - } - o((char *)out, CHUNK - strm.avail_out); - // buffer.insert(buffer.end(), (char *)out, (char *)out + CHUNK - strm.avail_out); - } - while (strm.avail_out == 0); - - /* clean up and return */ - inflateEnd(&strm); - delete[] out; - - return (ret == Z_STREAM_END); - } -}; - -} -#endif diff --git a/cpp/util/include/util/tc_hash_fun.h b/cpp/util/include/util/tc_hash_fun.h deleted file mode 100644 index 95b16ede9..000000000 --- a/cpp/util/include/util/tc_hash_fun.h +++ /dev/null @@ -1,198 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _TC_HASH_FUN_H_ -#define _TC_HASH_FUN_H_ - -#include -#include - -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_hash_fun.h - * @brief hash算法. - *å¯ä»¥å¯¹è¾“入的字节æµè¿›è¡Œhash得到相当å‡åŒ€çš„hash值 - * - * - */ -///////////////////////////////////////////////// - -template struct hash { }; -template struct hash_new { }; - -inline size_t hash_string(const char* s) -{ - unsigned long h = 0; - for ( ; *s; ++s) - h = 5*h + *s; - - return size_t(h); -} - -////////////////////////////////////////////////////////// -/** - * @brief å°½é‡é‡‡ç”¨hash_new, æ›´å‡è¡¡ä¸€äº›. - * - *å¯ä»¥å¯¹è¾“入的字节æµè¿›è¡Œhash得到相当å‡åŒ€çš„hash值 - */ -////////////////////////////////////////////////////////// -template <> -struct hash -{ - size_t operator()(const string &s) const - { - size_t h = 0, g; - const char *arKey = s.c_str(); - size_t nKeyLength = s.length(); - const char *arEnd = arKey + nKeyLength; - while (arKey < arEnd) - { - h = (h << 4) + *arKey++; - if ((g = (h & 0xF0000000))) - { - h = h ^ (g >> 24); - h = h ^ g; - } - } - return h; - } -}; - -template <> -struct hash_new -{ - size_t operator()(const string &s) const - { - const char *ptr= s.c_str(); - size_t key_length = s.length(); - uint32_t value= 0; - - while (key_length--) - { - value += *ptr++; - value += (value << 10); - value ^= (value >> 6); - } - value += (value << 3); - value ^= (value >> 11); - value += (value << 15); - - return value == 0 ? 1 : value; - } -}; - -template <> -struct hash -{ - size_t operator()(const char* s) const { return hash_string(s); } -}; - -template <> -struct hash -{ - size_t operator()(const char* s) const { return hash_string(s); } -}; - -template <> -struct hash -{ - size_t operator()(char x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(unsigned char x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(unsigned char x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(short x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(unsigned short x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(int x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(unsigned int x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(long x) const { return x; } -}; - -template <> -struct hash -{ - size_t operator()(unsigned long x) const { return x; } -}; - -/** -* @brief 一个奇妙的hash算法. -* -*å¯ä»¥å¯¹è¾“入的字节æµè¿›è¡Œhash得到相当å‡åŒ€çš„hash值 -*/ -struct magic_string_hash -{ - size_t operator()(const string &s) const - { - const char *ptr= s.c_str(); - size_t key_length = s.length(); - uint32_t value= 0; - - while (key_length--) - { - value += *ptr++; - value += (value << 10); - value ^= (value >> 6); - } - value += (value << 3); - value ^= (value >> 11); - value += (value << 15); - - return value == 0 ? 1 : value; - } -}; - -//////////////////////////////////////////////////////////////////// -} - -#endif - diff --git a/cpp/util/include/util/tc_hashmap.h b/cpp/util/include/util/tc_hashmap.h deleted file mode 100644 index 5f9917054..000000000 --- a/cpp/util/include/util/tc_hashmap.h +++ /dev/null @@ -1,1894 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_HASHMAP_H__ -#define __TC_HASHMAP_H__ - -#include -#include -#include -#include -#include -#include "util/tc_ex.h" -#include "util/tc_mem_vector.h" -#include "util/tc_pack.h" -#include "util/tc_mem_chunk.h" -#include "util/tc_hash_fun.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** -* @file tc_hashmap.h -* @brief hashmapç±» -*/ -///////////////////////////////////////////////// -/** -* @brief Hash map异常类 -*/ -struct TC_HashMap_Exception : public TC_Exception -{ - TC_HashMap_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_HashMap_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_HashMap_Exception() throw(){}; -}; - -//////////////////////////////////////////////////////////////////////////////////// -/** - * @brief 基于内存的hashmap, 所有æ“作需è¦è‡ªå·±åŠ é” - * - *内存hashmap,ä¸è¦ç›´æŽ¥ä½¿ç”¨è¯¥ç±»ï¼Œé€šè¿‡jmem组件æ¥ä½¿ç”¨. - * - *该hashmap通过TC_MemMutilChunkAllocatoræ¥åˆ†é…空间,支æŒä¸åŒå¤§å°çš„内存å—的分é…ï¼› - * - *支æŒå†…存和共享内存,对接å£çš„所有æ“作都需è¦åŠ é”,*内部有è„æ•°æ®é“¾ï¼Œæ”¯æŒæ•°æ®ç¼“写; - * - *当数æ®è¶…过一个数æ®å—时,则会拼接多个数æ®å—ï¼› - * - *Set时当数æ®å—用完,自动淘汰最长时间没有访问的数æ®ï¼Œä¹Ÿå¯ä»¥ä¸æ·˜æ±°ï¼Œç›´æŽ¥è¿”回错误; - * - *支æŒdump到文件,或从文件loadï¼› - */ -class TC_HashMap -{ -public: - struct HashMapIterator; - struct HashMapLockIterator; - - /** - * @brief æ“ä½œæ•°æ® - */ - struct BlockData - { - string _key; /**æ•°æ®Key*/ - string _value; /**æ•°æ®value*/ - bool _dirty; /**是å¦æ˜¯è„æ•°æ®*/ - time_t _synct; /**sync time, ä¸ä¸€å®šæ˜¯çœŸæ­£çš„回写时间*/ - BlockData() - : _dirty(false) - , _synct(0) - { - } - }; - - /////////////////////////////////////////////////////////////////////////////////// - /** - * @brief 内存数æ®å—,读å–å’Œå­˜æ”¾æ•°æ® - */ - class Block - { - public: - - /** - * @brief blockæ•°æ®å¤´ - */ - struct tagBlockHead - { - uint32_t _iSize; /**block的容é‡å¤§å°*/ - uint32_t _iIndex; /**hash的索引*/ - size_t _iBlockNext; /**下一个Block,tagBlockHead, 没有则为0*/ - size_t _iBlockPrev; /**上一个Block,tagBlockHead, 没有则为0*/ - size_t _iSetNext; /**Set链上的上一个Block*/ - size_t _iSetPrev; /**Set链上的上一个Block*/ - size_t _iGetNext; /**Get链上的上一个Block*/ - size_t _iGetPrev; /**Get链上的上一个Block*/ - time_t _iSyncTime; /**上次缓写时间*/ - bool _bDirty; /**是å¦æ˜¯è„æ•°æ®*/ - bool _bOnlyKey; /**是å¦åªæœ‰key, 没有内容*/ - bool _bNextChunk; /**是å¦æœ‰ä¸‹ä¸€ä¸ªchunk*/ - union - { - size_t _iNextChunk; /**下一个Chunkå—, _bNextChunk=true时有效, tagChunkHead*/ - size_t _iDataLen; /**当å‰æ•°æ®å—中使用了的长度, _bNextChunk=false时有效*/ - }; - char _cData[0]; /**æ•°æ®å¼€å§‹éƒ¨åˆ†*/ - }__attribute__((packed)); - - /** - * @brief éžå¤´éƒ¨çš„block, 称为chunk - */ - struct tagChunkHead - { - uint32_t _iSize; /**block的容é‡å¤§å°*/ - bool _bNextChunk; /**是å¦è¿˜æœ‰ä¸‹ä¸€ä¸ªchunk*/ - union - { - size_t _iNextChunk; /**下一个数æ®å—, _bNextChunk=true时有效, tagChunkHead*/ - size_t _iDataLen; /**当å‰æ•°æ®å—中使用了的长度, _bNextChunk=false时有效*/ - }; - char _cData[0]; /**æ•°æ®å¼€å§‹éƒ¨åˆ†*/ - }__attribute__((packed)); - - /** - * @brief 构造函数 - * @param Map - * @param iAddr 当å‰MemBlockçš„åœ°å€ - */ - Block(TC_HashMap *pMap, size_t iAddr) - : _pMap(pMap) - , _iHead(iAddr) - { - } - - /** - * @brief copy - * @param mb - */ - Block(const Block &mb) - : _pMap(mb._pMap) - , _iHead(mb._iHead) - { - } - - /** - * - * @param mb - * - * @return Block& - */ - Block& operator=(const Block &mb) - { - _iHead = mb._iHead; - _pMap = mb._pMap; - return (*this); - } - - /** - * - * @param mb - * - * @return bool - */ - bool operator==(const Block &mb) const { return _iHead == mb._iHead && _pMap == mb._pMap; } - - /** - * - * @param mb - * - * @return bool - */ - bool operator!=(const Block &mb) const { return _iHead != mb._iHead || _pMap != mb._pMap; } - - /** - * 获å–block头ç»å¯¹åœ°å€ - * @param iAddr - * - * @return tagChunkHead* - */ - tagBlockHead *getBlockHead(size_t iAddr) { return ((tagBlockHead*)_pMap->getAbsolute(iAddr)); } - - /** - * 获å–MemBlockå¤´åœ°å€ - * - * @return void* - */ - tagBlockHead *getBlockHead() {return getBlockHead(_iHead);} - - /** - * 头部 - * - * @return size_t - */ - size_t getHead() { return _iHead;} - - /** - * @brief 当å‰æ¡¶é“¾è¡¨æœ€åŽä¸€ä¸ªblock的头部 - * - * @return size_t - */ - size_t getLastBlockHead(); - - /** - * @brief 最新Get时间 - * - * @return time_t - */ - time_t getSyncTime() { return getBlockHead()->_iSyncTime; } - - /** - * @brief 设置回写时间 - * @param iSyncTime - */ - void setSyncTime(time_t iSyncTime) { getBlockHead()->_iSyncTime = iSyncTime; } - - /** - * @brief 获å–Blockä¸­çš„æ•°æ® - * - * @return int - * TC_HashMap::RT_OK, 正常, 其他异常 - * TC_HashMap::RT_ONLY_KEY, åªæœ‰Key - * 其他异常 - */ - int getBlockData(TC_HashMap::BlockData &data); - - /** - * @brief 获å–æ•°æ® - * @param pData - * @param iDatalen - * @return int, - * TC_HashMap::RT_OK, 正常 - * 其他异常 - */ - int get(void *pData, size_t &iDataLen); - - /** - * @brief 获å–æ•°æ® - * @param s - * @return int - * TC_HashMap::RT_OK, 正常 - * 其他异常 - */ - int get(string &s); - - /** - * @brief è®¾ç½®æ•°æ® - * @param pData - * @param iDatalen - * @param vtData, æ·˜æ±°çš„æ•°æ® - */ - int set(const void *pData, size_t iDataLen, bool bOnlyKey, vector &vtData); - - /** - * @brief 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty() { return getBlockHead()->_bDirty; } - - /** - * @brief è®¾ç½®æ•°æ® - * @param b - */ - void setDirty(bool b); - - /** - * @brief 是å¦åªæœ‰key - * - * @return bool - */ - bool isOnlyKey() { return getBlockHead()->_bOnlyKey; } - - /** - * @brief 当å‰å…ƒç´ ç§»åŠ¨åˆ°ä¸‹ä¸€ä¸ªblock - * @return true, 移到下一个block了, false, 没有下一个block - * - */ - bool nextBlock(); - - /** - * @brief 当å‰å…ƒç´ ç§»åŠ¨åˆ°ä¸Šä¸€ä¸ªblock - * @return true, 移到下一个block了, false, 没有下一个block - * - */ - bool prevBlock(); - - /** - * @brief 释放block的所有空间 - */ - void deallocate(); - - /** - * @brief æ–°block时调用该函数,分é…一个新的block - * @param index, hash索引 - * @param iAllocSize, å†…å­˜å¤§å° - */ - void makeNew(size_t index, size_t iAllocSize); - - /** - * @brief 从Block链表中删除当å‰Block,åªå¯¹Block有效, - * 对Chunk是无效的 - * @return - */ - void erase(); - - /** - * @brief 刷新set链表, 放在Set链表头部 - */ - void refreshSetList(); - - /** - * @brief 刷新get链表, 放在Get链表头部 - */ - void refreshGetList(); - - protected: - - /** - * @brief 获å–Chunk头ç»å¯¹åœ°å€ - * - * @return tagChunkHead* - */ - tagChunkHead *getChunkHead() {return getChunkHead(_iHead);} - - /** - * @brief 获å–chunk头ç»å¯¹åœ°å€ - * @param iAddr - * - * @return tagChunkHead* - */ - tagChunkHead *getChunkHead(size_t iAddr) { return ((tagChunkHead*)_pMap->getAbsolute(iAddr)); } - - /** - * @brief 从当å‰çš„chunk开始释放 - * @param iChunk é‡Šæ”¾åœ°å€ - */ - void deallocate(size_t iChunk); - - /** - * @brief 如果数æ®å®¹é‡ä¸å¤Ÿ, 则新增加chunk, ä¸å½±å“åŽŸæœ‰æ•°æ® - * 使新增加的总容é‡å¤§äºŽiDataLen,释放多余的chunk - * @param iDataLen - * - * @return int, - */ - int allocate(size_t iDataLen, vector &vtData); - - /** - * @brief 挂接chunk, 如果core则挂接失败, ä¿è¯å†…å­˜å—还å¯ä»¥ç”¨ - * @param pChunk - * @param chunks - * - * @return int - */ - int joinChunk(tagChunkHead *pChunk, const vector chunks); - - /** - * @brief 分é…n个chunk地å€, - * 注æ„释放内存的时候ä¸èƒ½é‡Šæ”¾æ­£åœ¨åˆ†é…的对象 - * @param fn, 分é…ç©ºé—´å¤§å° - * @param chunks, 分é…æˆåŠŸè¿”回的chunks地å€åˆ—表 - * @param vtData, æ·˜æ±°çš„æ•°æ® - * @return int - */ - int allocateChunk(size_t fn, vector &chunks, vector &vtData); - - /** - * @brief 获å–æ•°æ®é•¿åº¦ - * - * @return size_t - */ - size_t getDataLen(); - - public: - - /** - * Map - */ - TC_HashMap *_pMap; - - /** - * block区å—首地å€, ç›¸å¯¹åœ°å€ - */ - size_t _iHead; - - }; - - //////////////////////////////////////////////////////////////////////// - /* - * 内存数æ®å—分é…器 - * - */ - class BlockAllocator - { - public: - - /** - * @brief 构造函数 - */ - BlockAllocator(TC_HashMap *pMap) - : _pMap(pMap) - , _pChunkAllocator(new TC_MemMultiChunkAllocator()) - { - } - - /** - * @brief æžå¤Ÿå‡½æ•° - */ - ~BlockAllocator() - { - if(_pChunkAllocator != NULL) - { - delete _pChunkAllocator; - } - _pChunkAllocator = NULL; - } - - - /** - * @brief åˆå§‹åŒ– - * @param pHeadAddr, 地å€, æ¢åˆ°åº”用程åºçš„ç»å¯¹åœ°å€ - * @param iSize, å†…å­˜å¤§å° - * @param iMinBlockSize, 最å°æ•°æ®å—å¤§å° - * @param iMaxBlockSize, 最大数æ®å—å¤§å° - * @param fFactor, å› å­ - */ - void create(void *pHeadAddr, size_t iSize, size_t iMinBlockSize, size_t iMaxBlockSize, float fFactor) - { - _pChunkAllocator->create(pHeadAddr, iSize, iMinBlockSize, iMaxBlockSize, fFactor); - } - - /** - * @brief 连接上 - * @param pAddr, 地å€, æ¢åˆ°åº”用程åºçš„ç»å¯¹åœ°å€ - */ - void connect(void *pHeadAddr) - { - _pChunkAllocator->connect(pHeadAddr); - } - - /** - * @brief 扩展空间 - * @param pAddr - * @param iSize - */ - void append(void *pAddr, size_t iSize) - { - _pChunkAllocator->append(pAddr, iSize); - } - - /** - * @brief é‡å»º - */ - void rebuild() - { - _pChunkAllocator->rebuild(); - } - - /** - * @brief 获å–æ¯ç§æ•°æ®å—å¤´éƒ¨ä¿¡æ¯ - * - * @return TC_MemChunk::tagChunkHead - */ - vector getBlockDetail() const { return _pChunkAllocator->getBlockDetail(); } - - /** - * @brief 获å–å†…å­˜å¤§å° - * - * @return size_t - */ - size_t getMemSize() const { return _pChunkAllocator->getMemSize(); } - - /** - * @brief 获å–真正的数æ®å®¹é‡ - * - * @return size_t - */ - size_t getCapacity() const { return _pChunkAllocator->getCapacity(); } - - /** - * @brief æ¯ç§block中的chunk个数(æ¯ç§block中的chunk个数相åŒ) - * - * @return vector - */ - vector singleBlockChunkCount() const { return _pChunkAllocator->singleBlockChunkCount(); } - - /** - * @brief 获å–所有blockçš„chunk个数 - * - * @return size_t - */ - size_t allBlockChunkCount() const { return _pChunkAllocator->allBlockChunkCount(); } - - /** - * @brief 在内存中分é…一个新的Block - * - * @param index, block hash索引 - * @param iAllocSize: in/需è¦åˆ†é…的大å°, out/分é…çš„å—å¤§å° - * @param vtData, 返回释放的内存å—æ•°æ® - * @return size_t, 相对地å€,0表示没有空间å¯ä»¥åˆ†é… - */ - size_t allocateMemBlock(size_t index, size_t &iAllocSize, vector &vtData); - - /** - * @brief 为地å€ä¸ºiAddrçš„Block分é…一个chunk - * - * @param iAddr,分é…çš„Blockçš„åœ°å€ - * @param iAllocSize, in/需è¦åˆ†é…的大å°, out/分é…çš„å—å¤§å° - * @param vtData 返回释放的内存å—æ•°æ® - * @return size_t, 相对地å€,0表示没有空间å¯ä»¥åˆ†é… - */ - size_t allocateChunk(size_t iAddr, size_t &iAllocSize, vector &vtData); - - /** - * @brief 释放Block - * @param v - */ - void deallocateMemBlock(const vector &v); - - /** - * @brief 释放Block - * @param v - */ - void deallocateMemBlock(size_t v); - - protected: - //ä¸å…许copy构造 - BlockAllocator(const BlockAllocator &); - //ä¸å…许赋值 - BlockAllocator& operator=(const BlockAllocator &); - bool operator==(const BlockAllocator &mba) const; - bool operator!=(const BlockAllocator &mba) const; - public: - /** - * map - */ - TC_HashMap *_pMap; - - /** - * chunk分é…器 - */ - TC_MemMultiChunkAllocator *_pChunkAllocator; - }; - - //////////////////////////////////////////////////////////////// - /** - * @brief mapçš„æ•°æ®é¡¹ - */ - class HashMapLockItem - { - public: - - /** - * - * @param pMap - * @param iAddr - */ - HashMapLockItem(TC_HashMap *pMap, size_t iAddr); - - /** - * - * @param mcmdi - */ - HashMapLockItem(const HashMapLockItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return HashMapLockItem& - */ - HashMapLockItem &operator=(const HashMapLockItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator==(const HashMapLockItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator!=(const HashMapLockItem &mcmdi); - - /** - * @brief 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty(); - - /** - * @brief 是å¦åªæœ‰Key - * - * @return bool - */ - bool isOnlyKey(); - - /** - * @brief 获å–最åŽSync时间 - * - * @return time_t - */ - time_t getSyncTime(); - - /** - * 获å–值, 如果åªæœ‰Key(isOnlyKey)的情况下, v为空 - * @return int - * RT_OK:æ•°æ®èŽ·å–OK - * RT_ONLY_KEY: key有效, v无效为空 - * 其他值, 异常 - * - */ - int get(string& k, string& v); - - /** - * @brief 获å–值 - * @return int - * RT_OK:æ•°æ®èŽ·å–OK - * 其他值, 异常 - */ - int get(string& k); - - /** - * @brief 获å–æ•°æ®å—ç›¸å¯¹åœ°å€ - * - * @return size_t - */ - size_t getAddr() const { return _iAddr; } - - protected: - - /** - * @brief è®¾ç½®æ•°æ® - * @param k - * @param v - * @param vtData, æ·˜æ±°çš„æ•°æ® - * @return int - */ - int set(const string& k, const string& v, vector &vtData); - - /** - * @brief 设置Key, æ— æ•°æ® - * @param k - * @param vtData - * - * @return int - */ - int set(const string& k, vector &vtData); - - /** - * - * @param pKey - * @param iKeyLen - * - * @return bool - */ - bool equal(const string &k, string &v, int &ret); - - /** - * - * @param pKey - * @param iKeyLen - * - * @return bool - */ - bool equal(const string& k, int &ret); - - /** - * @brief 下一个item - * - * @return HashMapLockItem - */ - void nextItem(int iType); - - /** - * 上一个item - * @param iType - */ - void prevItem(int iType); - - friend class TC_HashMap; - friend struct TC_HashMap::HashMapLockIterator; - - private: - /** - * map - */ - TC_HashMap *_pMap; - - /** - * blockçš„åœ°å€ - */ - size_t _iAddr; - }; - - ///////////////////////////////////////////////////////////////////////// - /** - * @brief 定义迭代器 - */ - struct HashMapLockIterator - { - public: - - /** - *@brief 定义éåŽ†æ–¹å¼ - */ - enum - { - IT_BLOCK = 0, /**普通的顺åº*/ - IT_SET = 1, /**Set时间顺åº*/ - IT_GET = 2, /**Get时间顺åº*/ - }; - - /** - * è¿­ä»£å™¨çš„é¡ºåº - */ - enum - { - IT_NEXT = 0, /**顺åº*/ - IT_PREV = 1, /**逆åº*/ - }; - - /** - * - */ - HashMapLockIterator(); - - /** - * @brief 构造函数 - * @param iAddr, åœ°å€ - * @param type - */ - HashMapLockIterator(TC_HashMap *pMap, size_t iAddr, int iType, int iOrder); - - /** - * @brief copy - * @param it - */ - HashMapLockIterator(const HashMapLockIterator &it); - - /** - * @brief å¤åˆ¶ - * @param it - * - * @return HashMapLockIterator& - */ - HashMapLockIterator& operator=(const HashMapLockIterator &it); - - /** - * - * @param mcmi - * - * @return bool - */ - bool operator==(const HashMapLockIterator& mcmi); - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const HashMapLockIterator& mcmi); - - /** - * @brief å‰ç½®++ - * - * @return HashMapLockIterator& - */ - HashMapLockIterator& operator++(); - - /** - * @brief åŽç½®++ - * - * @return HashMapLockIterator& - */ - HashMapLockIterator operator++(int); - - /** - * - * - * @return HashMapLockItem&i - */ - HashMapLockItem& operator*() { return _iItem; } - - /** - * - * - * @return HashMapLockItem* - */ - HashMapLockItem* operator->() { return &_iItem; } - - public: - /** - * - */ - TC_HashMap *_pMap; - - /** - * - */ - HashMapLockItem _iItem; - - /** - * è¿­ä»£å™¨çš„æ–¹å¼ - */ - int _iType; - - /** - * è¿­ä»£å™¨çš„é¡ºåº - */ - int _iOrder; - - }; - - //////////////////////////////////////////////////////////////// - /** - * @brief mapçš„HashItem项, 一个HashItem对应多个数æ®é¡¹ - */ - class HashMapItem - { - public: - - /** - * - * @param pMap - * @param iIndex - */ - HashMapItem(TC_HashMap *pMap, size_t iIndex); - - /** - * - * @param mcmdi - */ - HashMapItem(const HashMapItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return HashMapItem& - */ - HashMapItem &operator=(const HashMapItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator==(const HashMapItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator!=(const HashMapItem &mcmdi); - - /** - * @brief 获å–当å‰hash桶的所有数é‡, 注æ„åªèŽ·å–有key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * - * @return - */ - void get(vector &vtData); - - /** - * - * - * @return int - */ - int getIndex() const { return _iIndex; } - - /** - * @brief 下一个item - * - */ - void nextItem(); - - /** - * 设置当å‰hash桶下所有数æ®ä¸ºè„æ•°æ®ï¼Œæ³¨æ„åªè®¾ç½®æœ‰key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸è®¾ç½® - * @param - * @return int - */ - int setDirty(); - - friend class TC_HashMap; - friend struct TC_HashMap::HashMapIterator; - - private: - /** - * map - */ - TC_HashMap *_pMap; - - /** - * æ•°æ®å—åœ°å€ - */ - size_t _iIndex; - }; - - ///////////////////////////////////////////////////////////////////////// - /** - * @brief 定义迭代器 - */ - struct HashMapIterator - { - public: - - /** - * @brief 构造函数 - */ - HashMapIterator(); - - /** - * @brief 构造函数 - * @param iIndex, åœ°å€ - * @param type - */ - HashMapIterator(TC_HashMap *pMap, size_t iIndex); - - /** - * @brief copy - * @param it - */ - HashMapIterator(const HashMapIterator &it); - - /** - * @brief å¤åˆ¶ - * @param it - * - * @return HashMapLockIterator& - */ - HashMapIterator& operator=(const HashMapIterator &it); - - /** - * - * @param mcmi - * - * @return bool - */ - bool operator==(const HashMapIterator& mcmi); - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const HashMapIterator& mcmi); - - /** - * @brief å‰ç½®++ - * - * @return HashMapIterator& - */ - HashMapIterator& operator++(); - - /** - * @brief åŽç½®++ - * - * @return HashMapIterator& - */ - HashMapIterator operator++(int); - - /** - * - * - * @return HashMapItem&i - */ - HashMapItem& operator*() { return _iItem; } - - /** - * - * - * @return HashMapItem* - */ - HashMapItem* operator->() { return &_iItem; } - - public: - /** - * - */ - TC_HashMap *_pMap; - - /** - * - */ - HashMapItem _iItem; - }; - - ////////////////////////////////////////////////////////////////////////////////////////////////// - - /** - * @brief map头 - */ - struct tagMapHead - { - char _cMaxVersion; /**大版本*/ - char _cMinVersion; /**å°ç‰ˆæœ¬*/ - bool _bReadOnly; /**是å¦åªè¯»*/ - bool _bAutoErase; /**是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±°*/ - char _cEraseMode; /**淘汰方å¼:0x00:按照Get链淘汰, 0x01:按照Set链淘汰*/ - size_t _iMemSize; /**内存大å°*/ - size_t _iMinDataSize; /**最å°æ•°æ®å—大å°*/ - size_t _iMaxDataSize; /**最大数æ®å—大å°*/ - float _fFactor; /**å› å­*/ - float _fRadio; /**chunks个数/hash个数*/ - size_t _iElementCount; /**总元素个数*/ - size_t _iEraseCount; /**æ¯æ¬¡åˆ é™¤ä¸ªæ•°*/ - size_t _iDirtyCount; /**è„æ•°æ®ä¸ªæ•°*/ - size_t _iSetHead; /**Set时间链表头部*/ - size_t _iSetTail; /**Set时间链表尾部*/ - size_t _iGetHead; /**Get时间链表头部*/ - size_t _iGetTail; /**Get时间链表尾部*/ - size_t _iDirtyTail; /**è„æ•°æ®é“¾å°¾éƒ¨*/ - time_t _iSyncTime; /**回写时间*/ - size_t _iUsedChunk; /**å·²ç»ä½¿ç”¨çš„内存å—*/ - size_t _iGetCount; /**get次数*/ - size_t _iHitCount; /**命中次数*/ - size_t _iBackupTail; /**热备指针*/ - size_t _iSyncTail; /**回写链表*/ - size_t _iOnlyKeyCount; /** OnlyKey个数*/ - size_t _iReserve[4]; /**ä¿ç•™*/ - }__attribute__((packed)); - - /** - * @brief 需è¦ä¿®æ”¹çš„åœ°å€ - */ - struct tagModifyData - { - size_t _iModifyAddr; /**修改的地å€*/ - char _cBytes; /**字节数*/ - size_t _iModifyValue; /**值*/ - }__attribute__((packed)); - - /** - * 修改数æ®å—头部 - */ - struct tagModifyHead - { - char _cModifyStatus; /**修改状æ€: 0:ç›®å‰æ²¡æœ‰äººä¿®æ”¹, 1: 开始准备修改, 2:修改完毕, 没有copy到内存中*/ - size_t _iNowIndex; /**更新到目å‰çš„索引, ä¸èƒ½æ“作10个*/ - tagModifyData _stModifyData[20]; /**一次最多20次修改*/ - }__attribute__((packed)); - - /** - * HashItem - */ - struct tagHashItem - { - size_t _iBlockAddr; /**指å‘æ•°æ®é¡¹çš„å移地å€*/ - uint32_t _iListCount; /**链表个数*/ - }__attribute__((packed)); - - //64ä½æ“作系统用基数版本å·, 32ä½æ“作系统用64ä½ç‰ˆæœ¬å· -#if __WORDSIZE == 64 - - //å®šä¹‰ç‰ˆæœ¬å· - enum - { - MAX_VERSION = 0, /**当å‰map的大版本å·*/ - MIN_VERSION = 3, /**当å‰mapçš„å°ç‰ˆæœ¬å·*/ - }; - -#else - //å®šä¹‰ç‰ˆæœ¬å· - enum - { - MAX_VERSION = 0, /**当å‰map的大版本å·*/ - MIN_VERSION = 2, /**当å‰mapçš„å°ç‰ˆæœ¬å·*/ - }; - -#endif - - /** - *@brief å®šä¹‰æ·˜æ±°æ–¹å¼ - */ - enum - { - ERASEBYGET = 0x00, /**按照Get链表淘汰*/ - ERASEBYSET = 0x01, /**按照Set链表淘汰*/ - }; - - /** - * @brief get, setç­‰int返回值 - */ - enum - { - RT_OK = 0, /**æˆåŠŸ*/ - RT_DIRTY_DATA = 1, /**è„æ•°æ®*/ - RT_NO_DATA = 2, /**没有数æ®*/ - RT_NEED_SYNC = 3, /**需è¦å›žå†™*/ - RT_NONEED_SYNC = 4, /**ä¸éœ€è¦å›žå†™*/ - RT_ERASE_OK = 5, /**淘汰数æ®æˆåŠŸ*/ - RT_READONLY = 6, /**mapåªè¯»*/ - RT_NO_MEMORY = 7, /**内存ä¸å¤Ÿ*/ - RT_ONLY_KEY = 8, /**åªæœ‰Key, 没有Value*/ - RT_NEED_BACKUP = 9, /**需è¦å¤‡ä»½*/ - RT_NO_GET = 10, /**没有GET过*/ - RT_DECODE_ERR = -1, /**解æžé”™è¯¯*/ - RT_EXCEPTION_ERR = -2, /**异常*/ - RT_LOAD_DATA_ERR = -3, /**加载数æ®å¼‚常*/ - RT_VERSION_MISMATCH_ERR = -4, /**版本ä¸ä¸€è‡´*/ - RT_DUMP_FILE_ERR = -5, /**dump到文件失败*/ - RT_LOAL_FILE_ERR = -6, /**load文件到内存失败*/ - RT_NOTALL_ERR = -7, /**没有å¤åˆ¶å®Œå…¨*/ - }; - - /** - * @brief 定义迭代器 - */ - typedef HashMapIterator hash_iterator; - typedef HashMapLockIterator lock_iterator; - - - /** - * @brief 定义hash处ç†å™¨ - */ - using hash_functor = std::function; - - ////////////////////////////////////////////////////////////////////////////////////////////// - - /** - * @brief 构造函数 - */ - TC_HashMap() - : _iMinDataSize(0) - , _iMaxDataSize(0) - , _fFactor(1.0) - , _fRadio(2) - , _pDataAllocator(new BlockAllocator(this)) - , _lock_end(this, 0, 0, 0) - , _end(this, (size_t)(-1)) - , _hashf(hash()) - { - } - - /** - * @brief 定义hash处ç†å™¨åˆå§‹åŒ–æ•°æ®å—å¹³å‡å¤§å° - * 表示内存分é…的时候,会分é…n个最å°å—, n个(最å°å¿«*增长因å­ï¼‰, n个(最å°å¿«*增长因å­*增长因å­ï¼‰..., 直到nä¸ªæœ€å¤§å— - * n是hashmap自己计算出æ¥çš„ - * è¿™ç§åˆ†é…策略通常是数æ®å—记录å˜é•¿æ¯”较多的使用, 便于节约内存,如果数æ®è®°å½•åŸºæœ¬ä¸æ˜¯å˜é•¿çš„, 那最å°å—=最大快,增长因å­=1å°±å¯ä»¥äº† - * @param iMinDataSize 最å°æ•°æ®å—å¤§å° - * @param iMaxDataSize 最大数æ®å—å¤§å° - * @param fFactor å¢žé•¿å› å­ - */ - void initDataBlockSize(size_t iMinDataSize, size_t iMaxDataSize, float fFactor); - - /** - * @brief 始化chunkæ•°æ®å—/hash项比值, - * 默认是2,有需è¦æ›´æ”¹å¿…须在create之å‰è°ƒç”¨ - * - * @param fRadio - */ - void initHashRadio(float fRadio) { _fRadio = fRadio;} - - /** - * @brief åˆå§‹åŒ–, 之å‰éœ€è¦è°ƒç”¨:initDataAvgSizeå’ŒinitHashRadio - * @param pAddr ç»å¯¹åœ°å€ - * @param iSize å¤§å° - * @return 失败则抛出异常 - */ - void create(void *pAddr, size_t iSize); - - /** - * @brief é“¾æŽ¥åˆ°å†…å­˜å— - * @param pAddr, åœ°å€ - * @param iSize, å†…å­˜å¤§å° - * @return 失败则抛出异常 - */ - void connect(void *pAddr, size_t iSize); - - /** - * @brief 原æ¥çš„æ•°æ®å—基础上扩展内存,注æ„通常åªèƒ½å¯¹mmap文件生效 - * (如果iSize比本æ¥çš„内存就å°,则返回-1) - * @param pAddr, 扩展åŽçš„空间 - * @param iSize - * @return 0:æˆåŠŸ, -1:失败 - */ - int append(void *pAddr, size_t iSize); - - /** - * @brief 获å–æ¯ç§å¤§å°å†…å­˜å—çš„å¤´éƒ¨ä¿¡æ¯ - * - * @return vector: ä¸åŒå¤§å°å†…å­˜å—å¤´éƒ¨ä¿¡æ¯ - */ - vector getBlockDetail() { return _pDataAllocator->getBlockDetail(); } - - /** - * @brief 所有block中chunk的个数 - * - * @return size_t - */ - size_t allBlockChunkCount() { return _pDataAllocator->allBlockChunkCount(); } - - /** - * @brief æ¯ç§block中chunk的个数(ä¸åŒå¤§å°å†…å­˜å—的个数相åŒ) - * - * @return vector - */ - vector singleBlockChunkCount() { return _pDataAllocator->singleBlockChunkCount(); } - - /** - * @brief 获å–hash桶的个数 - * - * @return size_t - */ - size_t getHashCount() { return _hash.size(); } - - /** - * @brief 获å–元素的个数 - * - * @return size_t - */ - size_t size() { return _pHead->_iElementCount; } - - /** - * @brief è„æ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t dirtyCount() { return _pHead->_iDirtyCount;} - - /** - * @brief OnlyKeyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return size_t - */ - size_t onlyKeyCount() { return _pHead->_iOnlyKeyCount;} - - /** - * @brief 设置æ¯æ¬¡æ·˜æ±°æ•°é‡ - * @param n - */ - void setEraseCount(size_t n) { _pHead->_iEraseCount = n; } - - /** - * @brief 获å–æ¯æ¬¡æ·˜æ±°æ•°é‡ - * - * @return size_t - */ - size_t getEraseCount() { return _pHead->_iEraseCount; } - - /** - * @brief 设置åªè¯» - * @param bReadOnly - */ - void setReadOnly(bool bReadOnly) { _pHead->_bReadOnly = bReadOnly; } - - /** - * @brief 是å¦åªè¯» - * - * @return bool - */ - bool isReadOnly() { return _pHead->_bReadOnly; } - - /** - * @brief 设置是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * @param bAutoErase - */ - void setAutoErase(bool bAutoErase) { _pHead->_bAutoErase = bAutoErase; } - - /** - * @brief 是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * - * @return bool - */ - bool isAutoErase() { return _pHead->_bAutoErase; } - - /** - * @brief è®¾ç½®æ·˜æ±°æ–¹å¼ - * TC_HashMap::ERASEBYGET - * TC_HashMap::ERASEBYSET - * @param cEraseMode - */ - void setEraseMode(char cEraseMode) { _pHead->_cEraseMode = cEraseMode; } - - /** - * @brief 获å–æ·˜æ±°æ–¹å¼ - * - * @return bool - */ - char getEraseMode() { return _pHead->_cEraseMode; } - - /** - * @brief 设置回写时间(秒) - * @param iSyncTime - */ - void setSyncTime(time_t iSyncTime) { _pHead->_iSyncTime = iSyncTime; } - - /** - * @brief 获å–回写时间 - * - * @return time_t - */ - time_t getSyncTime() { return _pHead->_iSyncTime; } - - /** - * @brief 获å–头部数æ®ä¿¡æ¯ - * - * @return tagMapHead& - */ - tagMapHead& getMapHead() { return *_pHead; } - - /** - * @brief 设置hashæ–¹å¼ - * @param hash_of - */ - void setHashFunctor(hash_functor hashf) { _hashf = hashf; } - - /** - * @brief 返回hash处ç†å™¨ - * - * @return hash_functor& - */ - hash_functor &getHashFunctor() { return _hashf; } - - /** - * @brief hash item - * @param index - * - * @return tagHashItem& - */ - tagHashItem *item(size_t iIndex) { return &_hash[iIndex]; } - - /** - * @brief dump到文件 - * @param sFile - * - * @return int - * RT_DUMP_FILE_ERR: dump到文件出错 - * RT_OK: dump到文件æˆåŠŸ - */ - int dump2file(const string &sFile); - - /** - * @brief 从文件load - * @param sFile - * - * @return int - * RT_LOAL_FILE_ERR: load出错 - * RT_VERSION_MISMATCH_ERR: 版本ä¸ä¸€è‡´ - * RT_OK: loadæˆåŠŸ - */ - int load5file(const string &sFile); - - /** - * @brief ä¿®å¤hash索引为içš„hash链(iä¸èƒ½æ“作hashmap的索引值) - * @param i - * @param bRepair - * - * @return int - */ - int recover(size_t i, bool bRepair); - - /** - * @brief 清空hashmap - * 所有mapçš„æ•°æ®æ¢å¤åˆ°åˆå§‹çŠ¶æ€ - */ - void clear(); - - /** - * @brief 检查数æ®å¹²å‡€çŠ¶æ€ - * @param k - * - * @return int - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_DIRTY_DATA: 是è„æ•°æ® - * RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const string &k); - - /** - * @brief 设置为è„æ•°æ®, 修改SET时间链, 会导致数æ®å›žå†™ - * @param k - * - * @return int - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirty(const string& k); - - /** - * æ•°æ®å›žå†™å¤±è´¥åŽé‡æ–°è®¾ç½®ä¸ºè„æ•°æ® - * @param k - * - * @return int - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirtyAfterSync(const string& k); - - /** - * @brief 设置为干净数æ®, 修改SET链, 导致数æ®ä¸å›žå†™ - * @param k - * - * @return int - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setClean(const string& k); - - /** - * @brief 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * @param iSyncTime:æ•°æ®ä¸Šæ¬¡å›žå†™çš„时间 - * - * @return int: - * RT_NO_DATA: æ²¡æœ‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK:获å–æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int get(const string& k, string &v, time_t &iSyncTime); - - /** - * @brief 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * - * @return int: - * RT_NO_DATA: æ²¡æœ‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK:获å–æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int get(const string& k, string &v); - - /** - * @brief 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @param v: 值 - * @param bDirty: 是å¦æ˜¯è„æ•°æ® - * @param vtData: 被淘汰的记录 - * @return int: - * RT_READONLY: mapåªè¯» - * RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const string& k, const string& v, bool bDirty, vector &vtData); - - /** - * @brief 设置key, ä½†æ— æ•°æ® - * @param k - * @param vtData - * - * @return int - * RT_READONLY: mapåªè¯» - * RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const string& k, vector &vtData); - - /** - * @brief åˆ é™¤æ•°æ® - * @param k, 关键字 - * @param data, 被删除的记录 - * @return int: - * RT_READONLY: mapåªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key, 删除æˆåŠŸ - * RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int del(const string& k, BlockData &data); - - /** - * @brief 淘汰数æ®, æ¯æ¬¡åˆ é™¤ä¸€æ¡, æ ¹æ®Get时间淘汰 - * 外部循环调用该接å£æ·˜æ±°æ•°æ® - * 直到: 元素个数/chunks * 100 < radio, bCheckDirty 为true时,é‡åˆ°è„æ•°æ®åˆ™æ·˜æ±°ç»“æŸ - * @param radio: 共享内存chunks使用比例 0< radio < 100 - * @param data: 当å‰è¢«åˆ é™¤çš„一æ¡è®°å½• - * @return int: - * RT_READONLY: mapåªè¯» - * RT_OK: ä¸ç”¨å†ç»§ç»­æ·˜æ±°äº† - * RT_ONLY_KEY:åªæœ‰Key, 删除æˆåŠŸ - * RT_DIRTY_DATA:æ•°æ®æ˜¯è„æ•°æ®ï¼Œå½“bCheckDirty=true时会有å¯èƒ½äº§ç”Ÿè¿™ç§è¿”回值 - * RT_ERASE_OK:淘汰当å‰æ•°æ®æˆåŠŸ, 继续淘汰 - * 其他返回值: 错误, 通常忽略, 继续调用erase淘汰 - */ - int erase(int radio, BlockData &data, bool bCheckDirty = false); - - /** - * @brief 回写, æ¯æ¬¡è¿”回需è¦å›žå†™çš„ä¸€æ¡ - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime则需è¦å›žå†™ - * _pHead->_iSyncTimeç”±setSyncTime函数设定, 默认10分钟 - - * 外部循环调用该函数进行回写 - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * @param iNowTime: 当å‰æ—¶é—´ - * 回写时间与当å‰æ—¶é—´ç›¸å·®_pHead->_iSyncTime都需è¦å›žå†™ - * @param data : å›žå†™çš„æ•°æ® - * @return int: - * RT_OK: 到è„æ•°æ®é“¾è¡¨å¤´éƒ¨äº†, å¯ä»¥sleep一下å†å°è¯• - * RT_ONLY_KEY:åªæœ‰Key, 删除æˆåŠŸ, 当å‰æ•°æ®ä¸è¦ç¼“写,继续调用sync回写 - * RT_NEED_SYNC:当å‰è¿”回的dataæ•°æ®éœ€è¦å›žå†™ - * RT_NONEED_SYNC:当å‰è¿”回的dataæ•°æ®ä¸éœ€è¦å›žå†™ - * 其他返回值: 错误, 通常忽略, 继续调用sync回写 - */ - int sync(time_t iNowTime, BlockData &data); - - /** - * @brief 开始回写, 调整回写指针 - */ - void sync(); - - /** - * @brief 开始备份之å‰è°ƒç”¨è¯¥å‡½æ•° - * - * @param bForceFromBegin: 是å¦å¼ºåˆ¶é‡å¤´å¼€å§‹å¤‡ä»½ - * @return void - */ - void backup(bool bForceFromBegin = false); - - /** - * @brief 开始备份数æ®, æ¯æ¬¡è¿”回需è¦å¤‡ä»½çš„一æ¡æ•°æ® - * @param data - * - * @return int - * RT_OK: 备份完毕 - * RT_NEED_BACKUP:当å‰è¿”回的dataæ•°æ®éœ€è¦å¤‡ä»½ - * RT_ONLY_KEY:åªæœ‰Key, 当å‰æ•°æ®ä¸è¦å¤‡ä»½ - * 其他返回值: 错误, 通常忽略, 继续调用backup - */ - int backup(BlockData &data); - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, 需è¦å¯¹mapåŠ é” - - /** - * @brief ç»“æŸ - * - * @return - */ - lock_iterator end() { return _lock_end; } - - - /** - * @brief æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * @param string - */ - lock_iterator find(const string& k); - - /** - * @brief blockæ­£åº - * - * @return lock_iterator - */ - lock_iterator begin(); - - /** - * @brief blocké€†åº - * - * @return lock_iterator - */ - lock_iterator rbegin(); - - /** - * @brief 以Set时间排åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator beginSetTime(); - - /** - * @brief Set链逆åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator rbeginSetTime(); - - /** - * @brief 以Get时间排åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator beginGetTime(); - - /** - * @brief Get链逆åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator rbeginGetTime(); - - /** - * @brief 获å–è„链表尾部迭代器(最长时间没有æ“作的è„æ•°æ®) - * - * 返回的迭代器++表示按照时间顺åº==>(最短时间没有æ“作的è„æ•°æ®) - * - * @return lock_iterator - */ - lock_iterator beginDirty(); - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, ä¸éœ€è¦å¯¹mapåŠ é” - - /** - * @brief æ ¹æ®hash桶é历 - * - * @return hash_iterator - */ - hash_iterator hashBegin(); - - /** - * @brief ç»“æŸ - * - * @return - */ - hash_iterator hashEnd() { return _end; } - - /** - * 获å–指定下标的hash_iterator - * @param iIndex - * - * @return hash_iterator - */ - hash_iterator hashIndex(size_t iIndex); - - /** - * @brief æè¿° - * - * @return string - */ - string desc(); - - /** - * @brief 修改更新到内存中 - */ - void doUpdate(bool bUpdate = false); - -protected: - - friend class Block; - friend class BlockAllocator; - friend class HashMapIterator; - friend class HashMapItem; - friend class HashMapLockIterator; - friend class HashMapLockItem; - - //ç¦æ­¢copy构造 - TC_HashMap(const TC_HashMap &mcm); - //ç¦æ­¢å¤åˆ¶ - TC_HashMap &operator=(const TC_HashMap &mcm); - - /** - * @brief åˆå§‹åŒ– - * @param pAddr - */ - void init(void *pAddr); - - - /** - * @brief 增加è„æ•°æ®ä¸ªæ•° - */ - void incDirtyCount() { update(&_pHead->_iDirtyCount, _pHead->_iDirtyCount+1); } - - /** - * @brief å‡å°‘è„æ•°æ®ä¸ªæ•° - */ - void delDirtyCount() { update(&_pHead->_iDirtyCount, _pHead->_iDirtyCount-1); } - - /** - * @brief 增加数æ®ä¸ªæ•° - */ - void incElementCount() { update(&_pHead->_iElementCount, _pHead->_iElementCount+1); } - - /** - * @brief å‡å°‘æ•°æ®ä¸ªæ•° - */ - void delElementCount() { update(&_pHead->_iElementCount, _pHead->_iElementCount-1); } - - /** - * @brief 增加OnlyKeyæ•°æ®ä¸ªæ•° - */ - void incOnlyKeyCount() { update(&_pHead->_iOnlyKeyCount, _pHead->_iOnlyKeyCount+1); } - - /** - * å‡å°‘OnlyKeyæ•°æ®ä¸ªæ•° - */ - void delOnlyKeyCount() { update(&_pHead->_iOnlyKeyCount, _pHead->_iOnlyKeyCount-1); } - - /** - * @brief 增加Chunkæ•° - * 直接更新, 因为有å¯èƒ½ä¸€æ¬¡åˆ†é…çš„chunk个数 - * 多余更新区å—的内存空间, 导致越界错误 - */ - void incChunkCount() { _pHead->_iUsedChunk++; } - - /** - * @brief å‡å°‘Chunkæ•° - * 直接更新, 因为有å¯èƒ½ä¸€æ¬¡é‡Šæ”¾çš„chunk个数 - * 多余更新区å—的内存空间, 导致越界错误 - */ - void delChunkCount() { _pHead->_iUsedChunk--; } - - /** - * @brief 增加hit次数 - */ - void incGetCount() { update(&_pHead->_iGetCount, _pHead->_iGetCount+1); } - - /** - * @brief 增加命中次数 - */ - void incHitCount() { update(&_pHead->_iHitCount, _pHead->_iHitCount+1); } - - /** - * @brief æŸhash链表数æ®ä¸ªæ•°+1 - * @param index - */ - void incListCount(uint32_t index) { update(&item(index)->_iListCount, item(index)->_iListCount+1); } - - /** - * @brief æŸhash链表数æ®ä¸ªæ•°+1 - * @param index - */ - void delListCount(size_t index) { update(&item(index)->_iListCount, item(index)->_iListCount-1); } - - /** - * @brief 相对地å€æ¢æˆç»å¯¹åœ°å€ - * @param iAddr - * - * @return void* - */ - void *getAbsolute(size_t iAddr) { return (void *)((char*)_pHead + iAddr); } - - /** - * @brief ç»å¯¹åœ°å€æ¢æˆç›¸å¯¹åœ°å€ - * - * @return size_t - */ - size_t getRelative(void *pAddr) { return (char*)pAddr - (char*)_pHead; } - - /** - * @brief 淘汰iNowAddr之外的数æ®(æ ¹æ®æ·˜æ±°ç­–略淘汰) - * @param iNowAddr, 当å‰Blockä¸èƒ½æ­£åœ¨åˆ†é…空间, ä¸èƒ½è¢«æ·˜æ±° - * 0表示åšç›´æŽ¥æ ¹æ®æ·˜æ±°ç­–略淘汰 - * @param vector, è¢«æ·˜æ±°çš„æ•°æ® - * @return size_t,淘汰的数æ®ä¸ªæ•° - */ - size_t eraseExcept(size_t iNowAddr, vector &vtData); - - /** - * @brief æ ¹æ®Key计算hash值 - * @param pKey - * @param iKeyLen - * - * @return size_t - */ - size_t hashIndex(const string& k); - - /** - * @brief æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * - */ - lock_iterator find(const string& k, size_t index, string &v, int &ret); - - /** - * @brief æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * @param mb - */ - lock_iterator find(const string& k, size_t index, int &ret); - - /** - * @brief 分æžhashçš„æ•°æ® - * @param iMaxHash - * @param iMinHash - * @param fAvgHash - */ - void analyseHash(uint32_t &iMaxHash, uint32_t &iMinHash, float &fAvgHash); - - /** - * @brief 修改具体的值 - * @param iModifyAddr - * @param iModifyValue - */ - void update(void* iModifyAddr, size_t iModifyValue); - -#if __WORDSIZE == 64 - void update(void* iModifyAddr, uint32_t iModifyValue); -#endif - - /** - * - * @param iModifyAddr - * @param iModifyValue - */ - void update(void* iModifyAddr, bool bModifyValue); - - /** - * @brief 获å–大于n且离n最近的素数 - * @param n - * - * @return size_t - */ - size_t getMinPrimeNumber(size_t n); - -protected: - - /** - * 区å—指针 - */ - tagMapHead *_pHead; - - /** - * 最å°çš„æ•°æ®å—å¤§å° - */ - size_t _iMinDataSize; - - /** - * 最大的数æ®å—å¤§å° - */ - size_t _iMaxDataSize; - - /** - * å˜åŒ–å› å­ - */ - float _fFactor; - - /** - * 设置chunkæ•°æ®å—/hash项比值 - */ - float _fRadio; - - /** - * hash对象 - */ - TC_MemVector _hash; - - /** - * 修改数æ®å— - */ - tagModifyHead *_pstModifyHead; - - /** - * block分é…器对象 - */ - BlockAllocator *_pDataAllocator; - - /** - * 尾部 - */ - lock_iterator _lock_end; - - /** - * 尾部 - */ - hash_iterator _end; - - /** - * hashå€¼è®¡ç®—å…¬å¼ - */ - hash_functor _hashf; -}; - -} - -#endif - diff --git a/cpp/util/include/util/tc_hashmap_compact.h b/cpp/util/include/util/tc_hashmap_compact.h deleted file mode 100644 index 690db7249..000000000 --- a/cpp/util/include/util/tc_hashmap_compact.h +++ /dev/null @@ -1,2043 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_HASHMAP_COMPACT_H__ -#define __TC_HASHMAP_COMPACT_H__ - -#include -#include -#include -#include -#include -#include "util/tc_ex.h" -#include "util/tc_mem_vector.h" -#include "util/tc_pack.h" -#include "util/tc_mem_chunk.h" -#include "util/tc_hash_fun.h" -#include "util/tc_thread.h" - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_hashmap_compact.h - * @brief hashmapç±»(紧凑型, 64ä½os下, æ¯ä¸ªchunkçš„é¢å¤–内存å ç”¨æ¯”tc_hashmapè¦å°30个字节) - * - */ -///////////////////////////////////////////////// -/** -* @brief Hash map异常类 -*/ -struct TC_HashMapCompact_Exception : public TC_Exception -{ - TC_HashMapCompact_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_HashMapCompact_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_HashMapCompact_Exception() throw(){}; -}; - -//////////////////////////////////////////////////////////////////////////////////// -/** - * @brief 紧凑性hashmap. - * 基于内存的hashmap,所有æ“作需è¦è‡ªå·±åŠ é” , 使用方å¼åŒTC_Hashmap,但是64ä½ç³»ç»Ÿä¸‹æ›´èŠ‚约é¢å¤–的管ç†ç©ºé—´ - */ -class TC_HashMapCompact -{ -public: - struct HashMapIterator; - struct HashMapLockIterator; - - /** - * @brief æ“ä½œæ•°æ® - * string _key æ•°æ®Key - * string _value æ•°æ®value - * bool _dirty 是å¦æ˜¯è„æ•°æ® - * uint32_t _synct sync time ä¸ä¸€å®šæ˜¯çœŸæ­£çš„回写时间 - * uint32_t _expiret æ•°æ®è¿‡æœŸçš„ç»å¯¹æ—¶é—´ - * 由设置或更新数æ®æ—¶æ供,0表示ä¸å…³å¿ƒæ­¤æ—¶é—´ uint8_t - * uint8_t _ver æ•°æ®ç‰ˆæœ¬ï¼Œ1为åˆå§‹ç‰ˆæœ¬ï¼Œ0为ä¿ç•™ - */ - struct BlockData - { - string _key; /**æ•°æ®Key*/ - string _value; /**æ•°æ®value*/ - bool _dirty; /**是å¦æ˜¯è„æ•°æ®*/ - uint32_t _synct; /**sync time, ä¸ä¸€å®šæ˜¯çœŸæ­£çš„回写时间*/ - uint32_t _expiret; /**æ•°æ®è¿‡æœŸçš„ç»å¯¹æ—¶é—´ï¼Œç”±è®¾ç½®æˆ–æ›´æ–°æ•°æ®æ—¶æ供,0表示ä¸å…³å¿ƒæ­¤æ—¶é—´*/ - uint8_t _ver; /**æ•°æ®ç‰ˆæœ¬ï¼Œ1为åˆå§‹ç‰ˆæœ¬ï¼Œ0为ä¿ç•™*/ - BlockData() - : _dirty(false) - , _synct(0) - , _expiret(0) - , _ver(1) - { - } - }; - - /////////////////////////////////////////////////////////////////////////////////// - /** - * @brief 内存数æ®å—,读å–å’Œå­˜æ”¾æ•°æ® - */ - class Block - { - public: - - /** - * @brief blockæ•°æ®å¤´ - */ - struct tagBlockHead - { - uint16_t _iSize; /**block的容é‡å¤§å°*/ - uint32_t _iIndex; /**hash的索引*/ - uint32_t _iBlockNext; /**下一个Block,tagBlockHead, 没有则为0*/ - uint32_t _iBlockPrev; /**上一个Block,tagBlockHead, 没有则为0*/ - uint32_t _iSetNext; /**Set链上的上一个Block*/ - uint32_t _iSetPrev; /**Set链上的上一个Block*/ - uint32_t _iGetNext; /**Get链上的上一个Block*/ - uint32_t _iGetPrev; /**Get链上的上一个Block*/ - uint32_t _iSyncTime; /**上次缓写时间*/ - uint32_t _iExpireTime; /** æ•°æ®è¿‡æœŸçš„ç»å¯¹æ—¶é—´ï¼Œç”±è®¾ç½®æˆ–æ›´æ–°æ•°æ®æ—¶æ供,0表示ä¸å…³å¿ƒæ­¤æ—¶é—´*/ - uint8_t _iVersion; /** æ•°æ®ç‰ˆæœ¬ï¼Œ1为åˆå§‹ç‰ˆæœ¬ï¼Œ0为ä¿ç•™*/ - bool _bDirty; /**是å¦æ˜¯è„æ•°æ®*/ - bool _bOnlyKey; /**是å¦åªæœ‰key, 没有内容*/ - bool _bNextChunk; /**是å¦æœ‰ä¸‹ä¸€ä¸ªchunk*/ - union - { - uint32_t _iNextChunk; /**下一个Chunkå—, _bNextChunk=true时有效, tagChunkHead*/ - uint32_t _iDataLen; /**当å‰æ•°æ®å—中使用了的长度, _bNextChunk=false时有效*/ - }; - char _cData[0]; /**æ•°æ®å¼€å§‹éƒ¨åˆ†*/ - }__attribute__((packed)); - - /** - * @brief éžå¤´éƒ¨çš„block, 称为chunk - */ - struct tagChunkHead - { - uint16_t _iSize; /**block的容é‡å¤§å°*/ - bool _bNextChunk; /**是å¦è¿˜æœ‰ä¸‹ä¸€ä¸ªchunk*/ - union - { - uint32_t _iNextChunk; /**下一个数æ®å—, _bNextChunk=true时有效, tagChunkHead*/ - uint32_t _iDataLen; /**当å‰æ•°æ®å—中使用了的长度, _bNextChunk=false时有效*/ - }; - char _cData[0]; /**æ•°æ®å¼€å§‹éƒ¨åˆ†*/ - }__attribute__((packed)); - - /** - * @brief 构造函数 - * @param Map - * @param 当å‰MemBlockçš„åœ°å€ - * @param pAdd - */ - Block(TC_HashMapCompact *pMap, uint32_t iAddr) - : _pMap(pMap) - , _iHead(iAddr) - { - _pHead = getBlockHead(_iHead); - } - - /** - * @brief æ‹·è´æž„造 - * @param mb - */ - Block(const Block &mb) - : _pMap(mb._pMap) - , _iHead(mb._iHead) - { - _pHead = getBlockHead(_iHead); - } - - /** - * @brief 获å–block头ç»å¯¹åœ°å€ - * @param iAddr - * - * @return tagChunkHead* - */ - tagBlockHead *getBlockHead(uint32_t iAddr) { return ((tagBlockHead*)_pMap->getAbsolute(iAddr)); } - - /** - * @brief 获å–MemBlockå¤´åœ°å€ - * - * @return void* - */ - tagBlockHead *getBlockHead() {return _pHead;} - - /** - * @brief 头部 - * - * @return uint32_t - */ - uint32_t getHead() { return _iHead;} - - /** - * @brief 当å‰æ¡¶é“¾è¡¨æœ€åŽä¸€ä¸ªblock的头部 - * - * @return uint32_t - */ - uint32_t getLastBlockHead(); - - /** - * @brief 获å–æ•°æ®çš„过期时间 - * - * @return uint32_t,å•ä½ä¸ºç§’,返回0表示无过期时间 - */ - uint32_t getExpireTime() { return getBlockHead()->_iExpireTime; } - - /** - * @brief 设置数æ®çš„过期时间 - * @param iExpireTime,过期ç»å¯¹æ—¶é—´ï¼Œå•ä½ä¸ºç§’ - */ - void setExpireTime(uint32_t iExpireTime) - { - if(iExpireTime != 0) - { - getBlockHead()->_iExpireTime = iExpireTime; - } - } - - /** - * @brief 最新Get时间 - * - * @return uint32_t - */ - uint32_t getSyncTime() { return getBlockHead()->_iSyncTime; } - - /** - * @brief 设置回写时间 - * @param iSyncTime - */ - void setSyncTime(uint32_t iSyncTime) { getBlockHead()->_iSyncTime = iSyncTime; } - - /** - * @brief 获å–æ•°æ®ç‰ˆæœ¬ - */ - uint8_t getVersion() { return getBlockHead()->_iVersion; } - - /** - * @brief 设置数æ®ç‰ˆæœ¬ - */ - void setVersion(uint8_t iVersion) { getBlockHead()->_iVersion = iVersion; } - - /** - * @brief 获å–Blockä¸­çš„æ•°æ® - * - * @return int - * TC_HashMapCompact::RT_OK, 正常, 其他异常 - * TC_HashMapCompact::RT_ONLY_KEY, åªæœ‰Key - * 其他异常 - */ - int getBlockData(TC_HashMapCompact::BlockData &data); - - /** - * @brief 获å–æ•°æ® - * @param pData - * @param iDatalen - * @return int, - * TC_HashMapCompact::RT_OK, 正常 - * 其他异常 - */ - int get(void *pData, uint32_t &iDataLen); - - /** - * @brief 获å–æ•°æ® - * @param s - * @return int - * TC_HashMapCompact::RT_OK, 正常 - * 其他异常 - */ - int get(string &s); - - /** - * @brief è®¾ç½®æ•°æ® - * @param pData - * @param iDatalen - * @param vtData, æ·˜æ±°çš„æ•°æ® - */ - int set(const string& k, const string& v, uint32_t iExpireTime, uint8_t iVersion, bool bNewBlock, bool bOnlyKey, vector &vtData); - - /** - * @brief 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty() { return getBlockHead()->_bDirty; } - - /** - * @brief è®¾ç½®æ•°æ® - * @param b - */ - void setDirty(bool b); - - /** - * @brief 是å¦åªæœ‰key - * - * @return bool - */ - bool isOnlyKey() { return getBlockHead()->_bOnlyKey; } - - /** - * @brief 当å‰å…ƒç´ ç§»åŠ¨åˆ°ä¸‹ä¸€ä¸ªblock - * @return true, 移到下一个block了, false, 没有下一个block - * - */ - bool nextBlock(); - - /** - * @brief 当å‰å…ƒç´ ç§»åŠ¨åˆ°ä¸Šä¸€ä¸ªblock - * @return true, 移到下一个block了, false, 没有下一个block - * - */ - bool prevBlock(); - - /** - * @brief 释放block的所有空间 - */ - void deallocate(); - - /** - * @brief æ–°block时调用该函数,分é…一个新的block - * @param index, hash索引 - * @param iAllocSize, å†…å­˜å¤§å° - */ - void makeNew(uint32_t index, uint32_t iAllocSize); - - /** - * @brief 从Block链表中删除当å‰Block,åªå¯¹Block有效, - * 对Chunk是无效的 - * @return - */ - void erase(); - - /** - * @brief 刷新set链表, 放在Set链表头部 - */ - void refreshSetList(); - - /** - * @brief 刷新get链表, 放在Get链表头部 - */ - void refreshGetList(); - - protected: - - Block& operator=(const Block &mb); - bool operator==(const Block &mb) const; - bool operator!=(const Block &mb) const; - - /** - * @brief 获å–Chunk头ç»å¯¹åœ°å€ - * - * @return tagChunkHead* - */ - tagChunkHead *getChunkHead() {return getChunkHead(_iHead);} - - /** - * @brief 获å–chunk头ç»å¯¹åœ°å€ - * @param iAddr - * - * @return tagChunkHead* - */ - tagChunkHead *getChunkHead(uint32_t iAddr) { return ((tagChunkHead*)_pMap->getAbsolute(iAddr)); } - - /** - * @brief 从当å‰çš„chunk开始释放 - * @param iChunk é‡Šæ”¾åœ°å€ - */ - void deallocate(uint32_t iChunk); - - /** - * @brief æ’入到hashmap的链中 - */ - void insertHashMap(); - - /** - * @brief 如果数æ®å®¹é‡ä¸å¤Ÿ, 则新增加chunk, ä¸å½±å“åŽŸæœ‰æ•°æ® - * 使新增加的总容é‡å¤§äºŽiDataLen,释放多余的chunk - * @param iDataLen - * - * @return int, - */ - int allocate(uint32_t iDataLen, vector &vtData); - - /** - * @brief 挂接chunk, 如果core则挂接失败, ä¿è¯å†…å­˜å—还å¯ä»¥ç”¨ - * @param pChunk - * @param chunks - * - * @return int - */ - int joinChunk(tagChunkHead *pChunk, const vector chunks); - - /** - * @brief 分é…n个chunk地å€ï¼Œ - * 注æ„释放内存的时候ä¸èƒ½é‡Šæ”¾æ­£åœ¨åˆ†é…的对象 - * @param fn 分é…ç©ºé—´å¤§å° - * @param chunks 分é…æˆåŠŸè¿”回的chunks地å€åˆ—表 - * @param vtData æ·˜æ±°çš„æ•°æ® - * @return int - */ - int allocateChunk(uint32_t fn, vector &chunks, vector &vtData); - - /** - * @brief 获å–æ•°æ®é•¿åº¦ - * - * @return uint32_t - */ - uint32_t getDataLen(); - - private: - - /** - * Map - */ - TC_HashMapCompact *_pMap; - - /** - * block区å—首地å€, ç›¸å¯¹åœ°å€ - */ - uint32_t _iHead; - - /** - * åœ°å€ - */ - tagBlockHead * _pHead; - }; - - //////////////////////////////////////////////////////////////////////// - /** - * @brief 内存数æ®å—分é…器 - */ - class BlockAllocator - { - public: - - /** - * @brief 构造函数 - */ - BlockAllocator(TC_HashMapCompact *pMap) - : _pMap(pMap) - , _pChunkAllocator(new TC_MemMultiChunkAllocator()) - { - } - - /** - * @brief æžå¤Ÿå‡½æ•° - */ - ~BlockAllocator() - { - if(_pChunkAllocator != NULL) - { - delete _pChunkAllocator; - } - _pChunkAllocator = NULL; - } - - /** - * @brief åˆå§‹åŒ– - * @param pHeadAddr, 地å€, æ¢åˆ°åº”用程åºçš„ç»å¯¹åœ°å€ - * @param iSize, å†…å­˜å¤§å° - * @param iMinBlockSize, 最å°æ•°æ®å—å¤§å° - * @param iMaxBlockSize, 最大数æ®å—å¤§å° - * @param fFactor, å› å­ - */ - void create(void *pHeadAddr, size_t iSize, uint32_t iMinBlockSize, uint32_t iMaxBlockSize, float fFactor) - { - _pChunkAllocator->create(pHeadAddr, iSize, iMinBlockSize, iMaxBlockSize, fFactor); - } - - /** - * @brief 连接上 - * @param pAddr, 地å€, æ¢åˆ°åº”用程åºçš„ç»å¯¹åœ°å€ - */ - void connect(void *pHeadAddr) - { - _pChunkAllocator->connect(pHeadAddr); - } - - /** - * @brief 扩展空间 - * @param pAddr - * @param iSize - */ - void append(void *pAddr, size_t iSize) - { - _pChunkAllocator->append(pAddr, iSize); - } - - /** - * @brief é‡å»º - */ - void rebuild() - { - _pChunkAllocator->rebuild(); - } - - /** - * @brief 获å–æ¯ç§æ•°æ®å—å¤´éƒ¨ä¿¡æ¯ - * - * @return TC_MemChunk::tagChunkHead - */ - vector getBlockDetail() const { return _pChunkAllocator->getBlockDetail(); } - - /** - * @brief å†…å­˜å¤§å° - * - * @return size_t - */ - size_t getMemSize() const { return _pChunkAllocator->getMemSize(); } - - /** - * @brief 真正的数æ®å®¹é‡ - * - * @return size_t - */ - size_t getCapacity() const { return _pChunkAllocator->getCapacity(); } - - /** - * @brief æ¯ç§block中的chunk个数(æ¯ç§block中的chunk个数相åŒ) - * - * @return vector - */ - vector singleBlockChunkCount() const { return _pChunkAllocator->singleBlockChunkCount(); } - - /** - * @brief 所有blockçš„chunk个数 - * - * @return size_t - */ - size_t allBlockChunkCount() const { return _pChunkAllocator->allBlockChunkCount(); } - - /** - * @brief 在内存中分é…一个新的Block - * - * @param index, block hash索引 - * @param iAllocSize: in/需è¦åˆ†é…的大å°, out/分é…çš„å—å¤§å° - * @param vtData, 返回释放的内存å—æ•°æ® - * @return size_t, 相对地å€,0表示没有空间å¯ä»¥åˆ†é… - */ - uint32_t allocateMemBlock(uint32_t index, uint32_t &iAllocSize, vector &vtData); - - /** - * @brief 为地å€ä¸ºiAddrçš„Block分é…一个chunk - * - * @param iAddr,分é…çš„Blockçš„åœ°å€ - * @param iAllocSize, in/需è¦åˆ†é…的大å°, out/分é…çš„å—å¤§å° - * @param vtData 返回释放的内存å—æ•°æ® - * @return size_t, 相对地å€,0表示没有空间å¯ä»¥åˆ†é… - */ - uint32_t allocateChunk(uint32_t iAddr, uint32_t &iAllocSize, vector &vtData); - - /** - * @brief 释放Block - * @param v - */ - void deallocateMemBlock(const vector &v); - - /** - * @brief 释放Block - * @param v - */ - void deallocateMemBlock(uint32_t v); - - protected: - /** - *@brief ä¸å…许copy构造 - * */ - BlockAllocator(const BlockAllocator &); - //ä¸å…许赋值 - BlockAllocator& operator=(const BlockAllocator &); - bool operator==(const BlockAllocator &mba) const; - bool operator!=(const BlockAllocator &mba) const; - - public: - /** - * map - */ - TC_HashMapCompact *_pMap; - - /** - * chunk分é…器 - */ - TC_MemMultiChunkAllocator *_pChunkAllocator; - }; - - //////////////////////////////////////////////////////////////// - /** - *@brief mapçš„æ•°æ®é¡¹ - * */ - class HashMapLockItem - { - public: - - /** - * - * @param pMap - * @param iAddr - */ - HashMapLockItem(TC_HashMapCompact *pMap, uint32_t iAddr); - - /** - * - * @param mcmdi - */ - HashMapLockItem(const HashMapLockItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return HashMapLockItem& - */ - HashMapLockItem &operator=(const HashMapLockItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator==(const HashMapLockItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator!=(const HashMapLockItem &mcmdi); - - /** - * @brief 是å¦æ˜¯è„æ•°æ® - * - * @return bool - */ - bool isDirty(); - - /** - * @brief 是å¦åªæœ‰Key - * - * @return bool - */ - bool isOnlyKey(); - - /** - * @brief 最åŽSync时间 - * - * @return uint32_t - */ - uint32_t getSyncTime(); - - /** - * @brief 获å–值, 如果åªæœ‰Key(isOnlyKey)的情况下, v为空 - * @return int - * RT_OK:æ•°æ®èŽ·å–OK - * RT_ONLY_KEY: key有效, v无效为空 - * 其他值, 异常 - * - */ - int get(string& k, string& v); - - /** - * @brief 获å–值 - * @return int - * RT_OK:æ•°æ®èŽ·å–OK - * 其他值, 异常 - */ - int get(string& k); - - /** - * @brief æ•°æ®å—ç›¸å¯¹åœ°å€ - * - * @return uint32_t - */ - uint32_t getAddr() const { return _iAddr; } - - protected: - - /** - * @brief è®¾ç½®æ•°æ® - * @param k - * @param v - * @param iExpiretime, æ•°æ®è¿‡æœŸæ—¶é—´ï¼Œå•ä½ä¸ºç§’,0表示ä¸è®¾ç½®è¿‡æœŸæ—¶é—´ - * @param iVersion, æ•°æ®ç‰ˆæœ¬, 应该根æ®get出的数æ®ç‰ˆæœ¬å†™å›žï¼Œä¸º0表示ä¸å…³å¿ƒæ•°æ®ç‰ˆæœ¬ - * @param vtData, æ·˜æ±°çš„æ•°æ® - * @return int - */ - int set(const string& k, const string& v, uint32_t iExpireTime, uint8_t iVersion, bool bNewBlock, vector &vtData); - - /** - * @brief 设置Key, æ— æ•°æ® - * @param k - * @param vtData - * - * @return int - */ - int set(const string& k, uint8_t iVersion, bool bNewBlock, vector &vtData); - - /** - * - * @param pKey - * @param iKeyLen - * - * @return bool - */ - bool equal(const string &k, string &v, int &ret); - - /** - * - * @param pKey - * @param iKeyLen - * - * @return bool - */ - bool equal(const string& k, int &ret); - - /** - * @brief 下一个item - * - * @return HashMapLockItem - */ - void nextItem(int iType); - - /** - * @brief 上一个item - * @param iType - */ - void prevItem(int iType); - - friend class TC_HashMapCompact; - friend struct TC_HashMapCompact::HashMapLockIterator; - - private: - /** - * map - */ - TC_HashMapCompact *_pMap; - - /** - * blockçš„åœ°å€ - */ - uint32_t _iAddr; - }; - - ///////////////////////////////////////////////////////////////////////// - /** - * @brief 定义迭代器 - * */ - struct HashMapLockIterator - { - public: - - /** - *@brief 定义éåŽ†æ–¹å¼ - * */ - enum - { - IT_BLOCK = 0, /**普通的顺åº*/ - IT_SET = 1, /**Set时间顺åº*/ - IT_GET = 2, /**Get时间顺åº*/ - }; - - /** - * @brief è¿­ä»£å™¨çš„é¡ºåº - */ - enum - { - IT_NEXT = 0, /**顺åº*/ - IT_PREV = 1, /**逆åº*/ - }; - - /** - * - */ - HashMapLockIterator(); - - /** - * @brief 构造函数 - * @param iAddr, åœ°å€ - * @param type - */ - HashMapLockIterator(TC_HashMapCompact *pMap, uint32_t iAddr, int iType, int iOrder); - - /** - * @brief copy - * @param it - */ - HashMapLockIterator(const HashMapLockIterator &it); - - /** - * @brief å¤åˆ¶ - * @param it - * - * @return HashMapLockIterator& - */ - HashMapLockIterator& operator=(const HashMapLockIterator &it); - - /** - * - * @param mcmi - * - * @return bool - */ - bool operator==(const HashMapLockIterator& mcmi); - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const HashMapLockIterator& mcmi); - - /** - * @brief å‰ç½®++ - * - * @return HashMapLockIterator& - */ - HashMapLockIterator& operator++(); - - /** - * @brief åŽç½®++ - * - * @return HashMapLockIterator& - */ - HashMapLockIterator operator++(int); - - /** - * - * - * @return HashMapLockItem&i - */ - HashMapLockItem& operator*() { return _iItem; } - - /** - * - * - * @return HashMapLockItem* - */ - HashMapLockItem* operator->() { return &_iItem; } - - public: - /** - * - */ - TC_HashMapCompact *_pMap; - - /** - * - */ - HashMapLockItem _iItem; - - /** - * è¿­ä»£å™¨çš„æ–¹å¼ - */ - int _iType; - - /** - * è¿­ä»£å™¨çš„é¡ºåº - */ - int _iOrder; - - }; - - //////////////////////////////////////////////////////////////// - // mapçš„HashItem项, 一个HashItem对应多个数æ®é¡¹ - class HashMapItem - { - public: - - /** - * - * @param pMap - * @param iIndex - */ - HashMapItem(TC_HashMapCompact *pMap, uint32_t iIndex); - - /** - * - * @param mcmdi - */ - HashMapItem(const HashMapItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return HashMapItem& - */ - HashMapItem &operator=(const HashMapItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator==(const HashMapItem &mcmdi); - - /** - * - * @param mcmdi - * - * @return bool - */ - bool operator!=(const HashMapItem &mcmdi); - - /** - * @brief 获å–当å‰hash桶的所有数é‡, 注æ„åªèŽ·å–有key/valueçš„æ•°æ®, - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * - * @return - */ - void get(vector &vtData); - - /** - * @brief 获å–当å‰hash桶的过期数æ®, 注æ„åªèŽ·å–有key/valueçš„æ•°æ®, - * 对于åªæœ‰keyçš„æ•°æ®, ä¸èŽ·å– - * - * @return - */ - void getExpire(uint32_t t, vector &vtData); - - /** - * - * - * @return uint32_t - */ - uint32_t getIndex() const { return _iIndex; } - - /** - * @brief 下一个item - * - */ - void nextItem(); - - /** - * 设置当å‰hash桶下所有数æ®ä¸ºè„æ•°æ®ï¼Œæ³¨æ„åªè®¾ç½®æœ‰key/valueçš„æ•°æ® - * 对于åªæœ‰keyçš„æ•°æ®, ä¸è®¾ç½® - * @param - * @return int - */ - int setDirty(); - - friend class TC_HashMapCompact; - friend struct TC_HashMapCompact::HashMapIterator; - - private: - /** - * map - */ - TC_HashMapCompact *_pMap; - - /** - * æ•°æ®å—åœ°å€ - */ - uint32_t _iIndex; - }; - - ///////////////////////////////////////////////////////////////////////// - - /** - * @brief 定义迭代器 - */ - struct HashMapIterator - { - public: - - /** - * @brief 构造函数 - */ - HashMapIterator(); - - /** - * @brief 构造函数 - * @param iIndex, åœ°å€ - * @param type - */ - HashMapIterator(TC_HashMapCompact *pMap, uint32_t iIndex); - - /** - * @brief æ‹·è´æž„造 - * @param it - */ - HashMapIterator(const HashMapIterator &it); - - /** - * @brief å¤åˆ¶ - * @param it - * - * @return HashMapLockIterator& - */ - HashMapIterator& operator=(const HashMapIterator &it); - - /** - * - * @param mcmi - * - * @return bool - */ - bool operator==(const HashMapIterator& mcmi); - - /** - * - * @param mv - * - * @return bool - */ - bool operator!=(const HashMapIterator& mcmi); - - /** - * @brief å‰ç½®++ - * - * @return HashMapIterator& - */ - HashMapIterator& operator++(); - - /** - * @brief åŽç½®++ - * - * @return HashMapIterator& - */ - HashMapIterator operator++(int); - - /** - * - * - * @return HashMapItem&i - */ - HashMapItem& operator*() { return _iItem; } - - /** - * - * - * @return HashMapItem* - */ - HashMapItem* operator->() { return &_iItem; } - - public: - /** - * - */ - TC_HashMapCompact *_pMap; - - /** - * - */ - HashMapItem _iItem; - }; - - ////////////////////////////////////////////////////////////////////////////////////////////////// - /** - * map头 - */ - struct tagMapHead - { - char _cMaxVersion; //大版本 - char _cMinVersion; //å°ç‰ˆæœ¬ - bool _bReadOnly; //是å¦åªè¯» - bool _bAutoErase; //是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - char _cEraseMode; //淘汰方å¼:0x00:按照Get链淘汰, 0x01:按照Set链淘汰 - size_t _iMemSize; //å†…å­˜å¤§å° - uint32_t _iMinDataSize; //最å°æ•°æ®å—å¤§å° - uint32_t _iMaxDataSize; //最大数æ®å—å¤§å° - float _fFactor; //å› å­ - float _fRadio; //chunks个数/hash个数 - uint32_t _iElementCount; //总元素个数 - uint32_t _iEraseCount; //æ¯æ¬¡åˆ é™¤ä¸ªæ•° - uint32_t _iDirtyCount; //è„æ•°æ®ä¸ªæ•° - uint32_t _iSetHead; //Set时间链表头部 - uint32_t _iSetTail; //Set时间链表尾部 - uint32_t _iGetHead; //Get时间链表头部 - uint32_t _iGetTail; //Get时间链表尾部 - uint32_t _iDirtyTail; //è„æ•°æ®é“¾å°¾éƒ¨ - uint32_t _iSyncTime; //回写时间 - uint32_t _iUsedChunk; //å·²ç»ä½¿ç”¨çš„å†…å­˜å— - uint32_t _iGetCount; //get次数 - uint32_t _iHitCount; //命中次数 - uint32_t _iBackupTail; //热备指针 - uint32_t _iSyncTail; //回写链表 - uint32_t _iOnlyKeyCount; // OnlyKey个数 - bool _bInit; //是å¦å·²ç»å®Œæˆåˆå§‹åŒ– - char _cReserve[15]; //ä¿ç•™ - //uint32_t _iReserve[4]; //ä¿ç•™ - }__attribute__((packed)); - - /** - * 需è¦ä¿®æ”¹çš„åœ°å€ - */ - struct tagModifyData - { - size_t _iModifyAddr; /**修改的地å€*/ - char _cBytes; /**字节数*/ - size_t _iModifyValue; /**值*/ - }__attribute__((packed)); - - /** - * 修改数æ®å—头部 - */ - struct tagModifyHead - { - char _cModifyStatus; /**修改状æ€: 0:ç›®å‰æ²¡æœ‰äººä¿®æ”¹, 1: 开始准备修改, 2:修改完毕, 没有copy到内存中*/ - uint32_t _iNowIndex; /**更新到目å‰çš„索引, ä¸èƒ½æ“作10个*/ - tagModifyData _stModifyData[50000]; /**一次最多50000次修改*/ - }__attribute__((packed)); - - /** - * HashItem - */ - struct tagHashItem - { - uint32_t _iBlockAddr; /**指å‘æ•°æ®é¡¹çš„å移地å€*/ - uint32_t _iListCount; /**链表个数*/ - }__attribute__((packed)); - - /** - * @brief 64ä½æ“作系统用基数版本å·, 32ä½æ“作系统用64ä½ç‰ˆæœ¬å· - * */ - //64ä½æ“作系统用基数版本å·, 32ä½æ“作系统用å¶æ•°ç‰ˆæœ¬å· -#if __WORDSIZE == 64 - - /** - * @brief å®šä¹‰ç‰ˆæœ¬å· - */ - enum - { - MAX_VERSION = 4, //当å‰mapçš„å¤§ç‰ˆæœ¬å· - MIN_VERSION = 1, //当å‰mapçš„å°ç‰ˆæœ¬å· - }; - -#else - /** - * @brief å®šä¹‰ç‰ˆæœ¬å· - */ - enum - { - MAX_VERSION = 4, /**当å‰map的大版本å·*/ - MIN_VERSION = 0, /**当å‰mapçš„å°ç‰ˆæœ¬å·*/ - }; - -#endif - - /** - * @brief å®šä¹‰æ·˜æ±°æ–¹å¼ - */ - enum - { - ERASEBYGET = 0x00, /**按照Get链表淘汰*/ - ERASEBYSET = 0x01, /**按照Set链表淘汰*/ - }; - - /** - * @brief get, setç­‰int返回值 - */ - enum - { - RT_OK = 0, /**æˆåŠŸ*/ - RT_DIRTY_DATA = 1, /**è„æ•°æ®*/ - RT_NO_DATA = 2, /**没有数æ®*/ - RT_NEED_SYNC = 3, /**需è¦å›žå†™*/ - RT_NONEED_SYNC = 4, /**ä¸éœ€è¦å›žå†™*/ - RT_ERASE_OK = 5, /**淘汰数æ®æˆåŠŸ*/ - RT_READONLY = 6, /**mapåªè¯»*/ - RT_NO_MEMORY = 7, /**内存ä¸å¤Ÿ*/ - RT_ONLY_KEY = 8, /**åªæœ‰Key, 没有Value*/ - RT_NEED_BACKUP = 9, /**需è¦å¤‡ä»½*/ - RT_NO_GET = 10, /**没有GET过*/ - RT_DATA_VER_MISMATCH = 11, /**写入数æ®ç‰ˆæœ¬ä¸åŒ¹é…*/ - RT_DECODE_ERR = -1, /**解æžé”™è¯¯*/ - RT_EXCEPTION_ERR = -2, /**异常*/ - RT_LOAD_DATA_ERR = -3, /**加载数æ®å¼‚常*/ - RT_VERSION_MISMATCH_ERR = -4, /**版本ä¸ä¸€è‡´*/ - RT_DUMP_FILE_ERR = -5, /**dump到文件失败*/ - RT_LOAL_FILE_ERR = -6, /**load文件到内存失败*/ - RT_NOTALL_ERR = -7, /**没有å¤åˆ¶å®Œå…¨*/ - }; - - /** - * @brief 定义迭代器 - */ - typedef HashMapIterator hash_iterator; - typedef HashMapLockIterator lock_iterator; - - /** - * @brief 定义hash处ç†å™¨ - */ - using hash_functor = std::function; - - ////////////////////////////////////////////////////////////////////////////////////////////// - //map的接å£å®šä¹‰ - - /** - * @brief 构造函数 - */ - TC_HashMapCompact() - : _iMinDataSize(0) - , _iMaxDataSize(0) - , _fFactor(1.0) - , _fRadio(2) - , _pDataAllocator(new BlockAllocator(this)) - , _lock_end(this, 0, 0, 0) - , _end(this, (uint32_t)(-1)) - , _hashf(hash_new()) - { - } - - /** - * @brief åˆå§‹åŒ–æ•°æ®å—å¹³å‡å¤§å° - * 表示内存分é…的时候,会分é…n个最å°å—, n个(最å°å¿«*增长因å­ï¼‰, n个(最å°å¿«*增长因å­*增长因å­ï¼‰..., 直到nä¸ªæœ€å¤§å— - * n是hashmap自己计算出æ¥çš„ - * è¿™ç§åˆ†é…策略通常是数æ®å—记录å˜é•¿æ¯”较多的使用, 便于节约内存,如果数æ®è®°å½•åŸºæœ¬ä¸æ˜¯å˜é•¿çš„, 那最å°å—=最大快,增长因å­=1å°±å¯ä»¥äº† - * @param iMinDataSize: 最å°æ•°æ®å—å¤§å° - * @param iMaxDataSize: 最大数æ®å—å¤§å° - * @param fFactor: å¢žé•¿å› å­ - */ - void initDataBlockSize(uint32_t iMinDataSize, uint32_t iMaxDataSize, float fFactor); - - /** - * @brief 始化chunkæ•°æ®å—/hash项比值, 默认是2, - * 有需è¦æ›´æ”¹å¿…须在create之å‰è°ƒç”¨ - * - * @param fRadio - */ - void initHashRadio(float fRadio) { _fRadio = fRadio;} - - /** - * @brief åˆå§‹åŒ–, 之å‰éœ€è¦è°ƒç”¨:initDataAvgSizeå’ŒinitHashRadio - * @param pAddr ç»å¯¹åœ°å€ - * @param iSize å¤§å° - * @return 失败则抛出异常 - */ - void create(void *pAddr, size_t iSize); - - /** - * é“¾æŽ¥åˆ°å†…å­˜å— - * @param pAddr, åœ°å€ - * @param iSize, å†…å­˜å¤§å° - * @return 失败则抛出异常 - */ - void connect(void *pAddr, size_t iSize); - - /** - * @brief 原æ¥çš„æ•°æ®å—基础上扩展内存, 注æ„通常åªèƒ½å¯¹mmap文件生效 - * (如果iSize比本æ¥çš„内存就å°,则返回-1) - * @param pAddr, 扩展åŽçš„空间 - * @param iSize - * @return 0:æˆåŠŸ, -1:失败 - */ - int append(void *pAddr, size_t iSize); - - /** - * @brief 获å–æ¯ç§å¤§å°å†…å­˜å—çš„å¤´éƒ¨ä¿¡æ¯ - * - * @return vector: ä¸åŒå¤§å°å†…å­˜å—å¤´éƒ¨ä¿¡æ¯ - */ - vector getBlockDetail() { return _pDataAllocator->getBlockDetail(); } - - /** - * @brief 所有block中chunk的个数 - * - * @return size_t - */ - size_t allBlockChunkCount() { return _pDataAllocator->allBlockChunkCount(); } - - /** - * @brief æ¯ç§block中chunk的个数(ä¸åŒå¤§å°å†…å­˜å—的个数相åŒ) - * - * @return vector - */ - vector singleBlockChunkCount() { return _pDataAllocator->singleBlockChunkCount(); } - - /** - * @brief 获å–hash桶的个数 - * - * @return size_t - */ - uint32_t getHashCount() { return _hash.size(); } - - /** - * @brief 元素的个数 - * - * @return size_t - */ - uint32_t size() { return _pHead->_iElementCount; } - - /** - * @brief è„æ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return uint32_t - */ - uint32_t dirtyCount() { return _pHead->_iDirtyCount;} - - /** - * @brief OnlyKeyæ•°æ®å…ƒç´ ä¸ªæ•° - * - * @return uint32_t - */ - uint32_t onlyKeyCount() { return _pHead->_iOnlyKeyCount;} - - /** - * @brief 设置æ¯æ¬¡æ·˜æ±°æ•°é‡ - * @param n - */ - void setEraseCount(uint32_t n) { _pHead->_iEraseCount = n; } - - /** - * @brief 获å–æ¯æ¬¡æ·˜æ±°æ•°é‡ - * - * @return uint32_t - */ - uint32_t getEraseCount() { return _pHead->_iEraseCount; } - - /** - * @brief 设置åªè¯» - * @param bReadOnly - */ - void setReadOnly(bool bReadOnly) { _pHead->_bReadOnly = bReadOnly; } - - /** - * @brief 是å¦åªè¯» - * - * @return bool - */ - bool isReadOnly() { return _pHead->_bReadOnly; } - - /** - * @brief 设置是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * @param bAutoErase - */ - void setAutoErase(bool bAutoErase) { _pHead->_bAutoErase = bAutoErase; } - - /** - * @brief 是å¦å¯ä»¥è‡ªåŠ¨æ·˜æ±° - * - * @return bool - */ - bool isAutoErase() { return _pHead->_bAutoErase; } - - /** - * @brief è®¾ç½®æ·˜æ±°æ–¹å¼ - * TC_HashMapCompact::ERASEBYGET - * TC_HashMapCompact::ERASEBYSET - * @param cEraseMode - */ - void setEraseMode(char cEraseMode) { _pHead->_cEraseMode = cEraseMode; } - - /** - * @brief 获å–æ·˜æ±°æ–¹å¼ - * - * @return bool - */ - char getEraseMode() { return _pHead->_cEraseMode; } - - /** - * @brief 设置回写时间(秒) - * @param iSyncTime - */ - void setSyncTime(uint32_t iSyncTime) { _pHead->_iSyncTime = iSyncTime; } - - /** - * @brief 获å–回写时间 - * - * @return uint32_t - */ - uint32_t getSyncTime() { return _pHead->_iSyncTime; } - - /** - * @brief 获å–头部数æ®ä¿¡æ¯ - * - * @return tagMapHead& - */ - tagMapHead& getMapHead() { return *_pHead; } - - /** - * @brief 设置hashæ–¹å¼ - * @param hash_of - */ - void setHashFunctor(hash_functor hashf) { _hashf = hashf; } - - /** - * @brief 返回hash处ç†å™¨ - * - * @return hash_functor& - */ - hash_functor &getHashFunctor() { return _hashf; } - - /** - * @brief hash item - * @param index - * - * @return tagHashItem& - */ - tagHashItem *item(uint32_t iIndex) { return &_hash[iIndex]; } - - /** - * dump到文件 - * @param sFile - * - * @return int - * RT_DUMP_FILE_ERR: dump到文件出错 - * RT_OK: dump到文件æˆåŠŸ - */ - int dump2file(const string &sFile); - - /** - * @brief 从文件load - * @param sFile - * - * @return int - * RT_LOAL_FILE_ERR: load出错 - * RT_VERSION_MISMATCH_ERR: 版本ä¸ä¸€è‡´ - * RT_OK: loadæˆåŠŸ - */ - int load5file(const string &sFile); - - /** - * @brief ä¿®å¤hash索引为içš„hash链(iä¸èƒ½æ“作hashmap的索引值) - * @param i - * @param bRepair - * - * @return int - */ - int recover(size_t i, bool bRepair); - - /** - * @brief 清空hashmap,所有mapçš„æ•°æ®æ¢å¤åˆ°åˆå§‹çŠ¶æ€ - */ - void clear(); - - /** - * @brief 检查数æ®å¹²å‡€çŠ¶æ€ - * @param k - * - * @return int - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_DIRTY_DATA: 是è„æ•°æ® - * RT_OK: æ˜¯å¹²å‡€æ•°æ® - * 其他返回值: 错误 - */ - int checkDirty(const string &k); - - /** - * @brief 设置为è„æ•°æ®, 修改SET时间链, 会导致数æ®å›žå†™ - * @param k - * - * @return int - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirty(const string& k); - - /** - * æ•°æ®å›žå†™å¤±è´¥åŽé‡æ–°è®¾ç½®ä¸ºè„æ•°æ® - * @param k - * - * @return int - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK: 设置è„æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int setDirtyAfterSync(const string& k); - - /** - * @brief 设置为干净数æ®, 修改SET链, 导致数æ®ä¸å›žå†™ - * @param k - * - * @return int - * RT_READONLY: åªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int setClean(const string& k); - - /** - * @brief 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * @param iSyncTime:æ•°æ®ä¸Šæ¬¡å›žå†™çš„时间 - * @param iExpiretime: æ•°æ®è¿‡æœŸæ—¶é—´ï¼Œå•ä½ä¸ºç§’,0表示ä¸è®¾ç½®è¿‡æœŸæ—¶é—´ - * @param iVersion: æ•°æ®ç‰ˆæœ¬, 应该根æ®get出的数æ®ç‰ˆæœ¬å†™å›žï¼Œä¸º0表示ä¸å…³å¿ƒæ•°æ®ç‰ˆæœ¬ - * - * @return int: - * RT_NO_DATA: æ²¡æœ‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK:获å–æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int get(const string& k, string &v, uint32_t &iSyncTime, uint32_t& iExpireTime, uint8_t& iVersion); - - /** - * @brief 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * @param iSyncTime:æ•°æ®ä¸Šæ¬¡å›žå†™çš„时间 - * - * @return int: - * RT_NO_DATA: æ²¡æœ‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK:获å–æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int get(const string& k, string &v, uint32_t &iSyncTime); - - /** - * @brief 获å–æ•°æ®, 修改GET时间链 - * @param k - * @param v - * - * @return int: - * RT_NO_DATA: æ²¡æœ‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key - * RT_OK:获å–æ•°æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int get(const string& k, string &v); - - /** - * @brief 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k: 关键字 - * @param v: 值 - * @param bDirty: 是å¦æ˜¯è„æ•°æ® - * @param vtData: 被淘汰的记录 - * @return int: - * RT_READONLY: mapåªè¯» - * RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const string& k, const string& v, bool bDirty, vector &vtData); - - /** - * @brief 设置数æ®, 修改时间链, 内存ä¸å¤Ÿæ—¶ä¼šè‡ªåŠ¨æ·˜æ±°è€çš„æ•°æ® - * @param k 关键字 - * @param v 值 - * @param iExpiretime æ•°æ®è¿‡æœŸæ—¶é—´ï¼Œå•ä½ä¸ºç§’, - * 0表示ä¸è®¾ç½®è¿‡æœŸæ—¶é—´ - * @param iVersion æ•°æ®ç‰ˆæœ¬, - * 应该根æ®get出的数æ®ç‰ˆæœ¬å†™å›žï¼Œä¸º0表示ä¸å…³å¿ƒæ•°æ®ç‰ˆ 本 - * @param bDirty 是å¦æ˜¯è„æ•°æ® - * @param vtData 被淘汰的记录 - * @return RT_READONLY: mapåªè¯» - * RT_NO_MEMORY:没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * RT_OK: 设置æˆåŠŸ 其他返回值: 错误 - */ - int set(const string& k, const string& v, uint32_t iExpireTime, uint8_t iVersion, bool bDirty, vector &vtData); - - /** - * @brief 设置key, ä½†æ— æ•°æ® - * @param k - * @param vtData - * - * @return RT_READONLY: mapåªè¯» - * RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const string& k, vector &vtData); - - /** - * @brief 设置key, ä½†æ— æ•°æ® - * @param k - * @param iVersion: æ•°æ®ç‰ˆæœ¬, 应该根æ®get出的数æ®ç‰ˆæœ¬å†™å›žï¼Œä¸º0表示ä¸å…³å¿ƒæ•°æ®ç‰ˆæœ¬ - * @param vtData - * - * @return int - * RT_READONLY: mapåªè¯» - * RT_NO_MEMORY: 没有空间(ä¸æ·˜æ±°æ•°æ®æƒ…况下会出现) - * RT_OK: 设置æˆåŠŸ - * 其他返回值: 错误 - */ - int set(const string& k, uint8_t iVersion, vector& vtData); - - /** - * @brief åˆ é™¤æ•°æ® - * @param k, 关键字 - * @param data, 被删除的记录 - * @return int: - * RT_READONLY: mapåªè¯» - * RT_NO_DATA: 没有当å‰æ•°æ® - * RT_ONLY_KEY:åªæœ‰Key, 删除æˆåŠŸ - * RT_OK: 删除数æ®æˆåŠŸ - * 其他返回值: 错误 - */ - int del(const string& k, BlockData &data); - - /** - * @brief 淘汰数æ®, æ¯æ¬¡åˆ é™¤ä¸€æ¡, - * æ ¹æ®Get时间淘汰,外部循环调用该接å£æ·˜æ±°æ•°æ® 直到: - * 元素个数/chunks * 100 < radio, bCheckDirty - * 为true时,é‡åˆ°è„æ•°æ®åˆ™æ·˜æ±°ç»“æŸ - * @param radio 共享内存chunks使用比例 0< radio < 100 - * @param data 当å‰è¢«åˆ é™¤çš„一æ¡è®°å½• - * @return RT_READONLY: mapåªè¯» - * RT_OK: ä¸ç”¨å†ç»§ç»­æ·˜æ±°äº† - * RT_ONLY_KEY:åªæœ‰Key, 删除æˆåŠŸ - * RT_DIRTY_DATA:æ•°æ®æ˜¯è„æ•°æ®ï¼Œå½“bCheckDirty=true时会有å¯èƒ½äº§ç”Ÿè¿™ç§è¿”回值 - * RT_ERASE_OK:淘汰当å‰æ•°æ®æˆåŠŸ, 继续淘汰 - * 其他返回值: 错误, 通常忽略, 继续调用erase淘汰 - */ - int erase(int radio, BlockData &data, bool bCheckDirty = false); - - /** - * @brief 回写, æ¯æ¬¡è¿”回需è¦å›žå†™çš„ä¸€æ¡ - * æ•°æ®å›žå†™æ—¶é—´ä¸Žå½“å‰æ—¶é—´è¶…过_pHead->_iSyncTime则需è¦å›žå†™ - * _pHead->_iSyncTimeç”±setSyncTime函数设定, 默认10分钟 - * 外部循环调用该函数进行回写 - * mapåªè¯»æ—¶ä»ç„¶å¯ä»¥å›žå†™ - * @param iNowTime: 当å‰æ—¶é—´ - * 回写时间与当å‰æ—¶é—´ç›¸å·®_pHead->_iSyncTime都需è¦å›žå†™ - * @param data : å›žå†™çš„æ•°æ® - * @return int: - * RT_OK: 到è„æ•°æ®é“¾è¡¨å¤´éƒ¨äº†, å¯ä»¥sleep一下å†å°è¯• - * RT_ONLY_KEY:åªæœ‰Key, 删除æˆåŠŸ, 当å‰æ•°æ®ä¸è¦ç¼“写,继续调用sync回写 - * RT_NEED_SYNC:当å‰è¿”回的dataæ•°æ®éœ€è¦å›žå†™ - * RT_NONEED_SYNC:当å‰è¿”回的dataæ•°æ®ä¸éœ€è¦å›žå†™ - * 其他返回值: 错误, 通常忽略, 继续调用sync回写 - */ - int sync(uint32_t iNowTime, BlockData &data); - - /** - * @brief 开始回写, 调整回写指针 - */ - void sync(); - - /** - * @brief 开始备份之å‰è°ƒç”¨è¯¥å‡½æ•° - * - * @param bForceFromBegin: 是å¦å¼ºåˆ¶é‡å¤´å¼€å§‹å¤‡ä»½ - * @return void - */ - void backup(bool bForceFromBegin = false); - - /** - * @brief 开始备份数æ®, æ¯æ¬¡è¿”回需è¦å¤‡ä»½çš„一æ¡æ•°æ® - * @param data - * - * @return int - * RT_OK: 备份完毕 - * RT_NEED_BACKUP:当å‰è¿”回的dataæ•°æ®éœ€è¦å¤‡ä»½ - * RT_ONLY_KEY:åªæœ‰Key, 当å‰æ•°æ®ä¸è¦å¤‡ä»½ - * 其他返回值: 错误, 通常忽略, 继续调用backup - */ - int backup(BlockData &data); - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, 需è¦å¯¹mapåŠ é” - - /** - *@brief ç»“æŸ - * - * @return - */ - lock_iterator end() { return _lock_end; } - - - /** - * @brief æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * @param string - */ - lock_iterator find(const string& k); - - /** - * @brief blockæ­£åº - * - * @return lock_iterator - */ - lock_iterator begin(); - - /** - *@brief blocké€†åº - * - * @return lock_iterator - */ - lock_iterator rbegin(); - - /** - * @brief 以Set时间排åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator beginSetTime(); - - /** - * @brief Set链逆åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator rbeginSetTime(); - - /** - * @brief 以Get时间排åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator beginGetTime(); - - /** - * @brief Get链逆åºçš„迭代器 - * - * @return lock_iterator - */ - lock_iterator rbeginGetTime(); - - /** - * @brief 获å–è„链表尾部迭代器(最长时间没有æ“作的è„æ•°æ®), - * 返回的迭代器++表示按照时间顺åº==>(最短时间没有æ“作的è„æ•°æ®) - * - * @return lock_iterator - */ - lock_iterator beginDirty(); - - ///////////////////////////////////////////////////////////////////////////////////////// - // 以下是é历map函数, ä¸éœ€è¦å¯¹mapåŠ é” - - /** - * @brief æ ¹æ®hash桶é历 - * - * @return hash_iterator - */ - hash_iterator hashBegin(); - - /** - * @brief ç»“æŸ - * - * @return - */ - hash_iterator hashEnd() { return _end; } - - /** - * @brief æè¿° - * - * @return string - */ - string desc(); - -public: - - /** - * @brief 错误ä¿æŠ¤ - */ - struct FailureRecover - { - FailureRecover(TC_HashMapCompact *pMap) : _pMap(pMap) - { - _pMap->doRecover(); - } - - ~FailureRecover() - { - _pMap->doUpdate(); - } - - protected: - TC_HashMapCompact *_pMap; - }; - -protected: - - friend class Block; - friend class BlockAllocator; - friend class HashMapIterator; - friend class HashMapItem; - friend class HashMapLockIterator; - friend class HashMapLockItem; - - /** - * @brief ç¦æ­¢copy构造 - * */ - TC_HashMapCompact(const TC_HashMapCompact &mcm); - /** - * @brief ç¦æ­¢å¤åˆ¶ - * */ - TC_HashMapCompact &operator=(const TC_HashMapCompact &mcm); - - /** - * @brief åˆå§‹åŒ– - * @param pAddr - */ - void init(void *pAddr); - - - /** - * @brief 增加è„æ•°æ®ä¸ªæ•° - */ - void incDirtyCount() { saveValue(&_pHead->_iDirtyCount, _pHead->_iDirtyCount+1); } - - /** - * @brief å‡å°‘è„æ•°æ®ä¸ªæ•° - */ - void delDirtyCount() { saveValue(&_pHead->_iDirtyCount, _pHead->_iDirtyCount-1); } - - /** - * @brief 增加数æ®ä¸ªæ•° - */ - void incElementCount() { saveValue(&_pHead->_iElementCount, _pHead->_iElementCount+1); } - - /** - * @brief å‡å°‘æ•°æ®ä¸ªæ•° - */ - void delElementCount() { saveValue(&_pHead->_iElementCount, _pHead->_iElementCount-1); } - - /** - * @brief 增加OnlyKeyæ•°æ®ä¸ªæ•° - */ - void incOnlyKeyCount() { saveValue(&_pHead->_iOnlyKeyCount, _pHead->_iOnlyKeyCount+1); } - - /** - * @brief å‡å°‘OnlyKeyæ•°æ®ä¸ªæ•° - */ - void delOnlyKeyCount() { saveValue(&_pHead->_iOnlyKeyCount, _pHead->_iOnlyKeyCount-1); } - - /* - * @brief 增加Chunkæ•° - * 以选择是å¦å¯ç›´æŽ¥æ›´æ–°, 因为有å¯èƒ½ä¸€æ¬¡åˆ†é…çš„chunk个数 - * 多余更新区å—的内存空间, 导致越界错误 - */ - void incChunkCount(bool bUpdateDirect = true) - { - if(bUpdateDirect) - { - ++_pHead->_iUsedChunk; - } - else - saveValue(&_pHead->_iUsedChunk, _pHead->_iUsedChunk+1); - } - - /** - * @brief å‡å°‘Chunkæ•° - * å¯ä»¥é€‰æ‹©æ˜¯å¦ç›´æŽ¥æ›´æ–°, 因为有å¯èƒ½ä¸€æ¬¡é‡Šæ”¾çš„chunk个数 - * 多余更新区å—的内存空间, 导致越界错误 - */ - void delChunkCount(bool bUpdateDirect = true) - { - if(bUpdateDirect) - { - --_pHead->_iUsedChunk; - } - else - saveValue(&_pHead->_iUsedChunk, _pHead->_iUsedChunk-1); - } - - /** - * @brief 增加hit次数 - */ - void incGetCount() { saveValue(&_pHead->_iGetCount, _pHead->_iGetCount+1); } - - /** - * @brief 增加命中次数 - */ - void incHitCount() { saveValue(&_pHead->_iHitCount, _pHead->_iHitCount+1); } - - /** - * @brief æŸhash链表数æ®ä¸ªæ•°+1 - * @param index - */ - void incListCount(uint32_t index) { saveValue(&item(index)->_iListCount, item(index)->_iListCount+1); } - - /** - * @brief æŸhash链表数æ®ä¸ªæ•°+1 - * @param index - */ - void delListCount(uint32_t index) { saveValue(&item(index)->_iListCount, item(index)->_iListCount-1); } - - /** - * @brief 相对地å€æ¢æˆç»å¯¹åœ°å€ - * @param iAddr - * - * @return void* - */ - void *getAbsolute(uint32_t iAddr) { if(iAddr == 0) return NULL; return _pDataAllocator->_pChunkAllocator->getAbsolute(iAddr); } - - /** - * @brief 淘汰iNowAddr之外的数æ®(æ ¹æ®æ·˜æ±°ç­–略淘汰) - * @param iNowAddr, 当å‰Blockä¸èƒ½æ­£åœ¨åˆ†é…空间, ä¸èƒ½è¢«æ·˜æ±° - * 0表示åšç›´æŽ¥æ ¹æ®æ·˜æ±°ç­–略淘汰 - * @param vector, è¢«æ·˜æ±°çš„æ•°æ® - * @return size_t,淘汰的数æ®ä¸ªæ•° - */ - uint32_t eraseExcept(uint32_t iNowAddr, vector &vtData); - - /** - * @brief æ ¹æ®Key计算hash值 - * @param pKey - * @param iKeyLen - * - * @return size_t - */ - uint32_t hashIndex(const string& k); - - /** - * @brief æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * - */ - lock_iterator find(const string& k, uint32_t index, string &v, int &ret); - - /** - * @brief æ ¹æ®KeyæŸ¥æ‰¾æ•°æ® - * @param mb - */ - lock_iterator find(const string& k, uint32_t index, int &ret); - - /** - * @brief 分æžhashçš„æ•°æ® - * @param iMaxHash - * @param iMinHash - * @param fAvgHash - */ - void analyseHash(uint32_t &iMaxHash, uint32_t &iMinHash, float &fAvgHash); - - /** - * @brief 修改具体的值 - * @param iModifyAddr - * @param iModifyValue - */ - template - void saveValue(void* iModifyAddr, T iModifyValue, bool bModify = true) - { - //获å–原始值 - T tmp = *(T*)iModifyAddr; - - //ä¿å­˜åŽŸå§‹å€¼ - _pstModifyHead->_stModifyData[_pstModifyHead->_iNowIndex]._iModifyAddr = (char*)iModifyAddr - (char*)_pHead; - _pstModifyHead->_stModifyData[_pstModifyHead->_iNowIndex]._iModifyValue = tmp; - _pstModifyHead->_stModifyData[_pstModifyHead->_iNowIndex]._cBytes = sizeof(iModifyValue); - _pstModifyHead->_cModifyStatus = 1; - - _pstModifyHead->_iNowIndex++; - - if(bModify) - { - //修改具体值 - *(T*)iModifyAddr = iModifyValue; - } - - assert(_pstModifyHead->_iNowIndex < sizeof(_pstModifyHead->_stModifyData) / sizeof(tagModifyData)); - } - - void saveAddr(uint32_t iAddr, char cByte=0); - - /** - * @brief æ¢å¤æ•°æ® - */ - void doRecover(); - - /** - * @brief 确认处ç†å®Œæ¯• - */ - void doUpdate(); - - /** - * 确认处ç†å®Œæ¯• - */ - void doUpdate2(); - - /** - * 确认处ç†å®Œæ¯• - */ - void doUpdate3(); - - /** - * 确认处ç†å®Œæ¯• - */ - void doUpdate4(); - - /** - * @brief 获å–大于n且离n最近的素数 - * @param n - * - * @return size_t - */ - uint32_t getMinPrimeNumber(uint32_t n); - - /** - * 释放ä¿æŠ¤åŒºä¸­çš„chunk - */ - void deallocate(uint32_t iChunk, uint32_t iIndex); - - /** - * 释放ä¿æŠ¤åŒºä¸­çš„一个block - */ - void deallocate2(uint32_t iHead); - -protected: - - /** - * 区å—指针 - */ - tagMapHead *_pHead; - - /** - * 最å°çš„æ•°æ®å—å¤§å° - */ - uint32_t _iMinDataSize; - - /** - * 最大的数æ®å—å¤§å° - */ - uint32_t _iMaxDataSize; - - /** - * å˜åŒ–å› å­ - */ - float _fFactor; - - /** - * 设置chunkæ•°æ®å—/hash项比值 - */ - float _fRadio; - - /** - * hash对象 - */ - TC_MemVector _hash; - - /** - * 修改数æ®å— - */ - tagModifyHead *_pstModifyHead; - - /** - * block分é…器对象 - */ - BlockAllocator *_pDataAllocator; - - /** - * 尾部 - */ - lock_iterator _lock_end; - - /** - * 尾部 - */ - hash_iterator _end; - - /** - * hashå€¼è®¡ç®—å…¬å¼ - */ - hash_functor _hashf; -}; - -} - -#endif - diff --git a/cpp/util/include/util/tc_http.h b/cpp/util/include/util/tc_http.h deleted file mode 100644 index 35ed2e4c3..000000000 --- a/cpp/util/include/util/tc_http.h +++ /dev/null @@ -1,1280 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_HTTP_H_ -#define __TC_HTTP_H_ - -#include "util/tc_ex.h" -#include "util/tc_common.h" -#include "util/tc_autoptr.h" -#include "util/tc_thread.h" -#include "util/tc_socket.h" -#include "util/tc_epoller.h" -#include "util/tc_timeout_queue.h" -#include -#include -#include -#include - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_http.h - * @brief httpç±». - * - * 包括TC_HttpRequestã€TC_HttpResponse两个类; - * - * 支æŒGET HEAD POST OPTIONS,其他HTTP方法ä¸æ”¯æŒï¼› - * - * 通过TC_HttpRequest::checkRequest判断http请求是å¦æ”¶å®Œï¼› - * - * 与TC_ClientSocketé…åˆï¼Œæ”¯æŒåŒæ­¥å‘é€http请求,且支æŒhttpçš„chunkç¼–ç ï¼› - * - * å‘é€http请求时,éžçº¿ç¨‹å®‰å…¨ - * - * 1 支æŒhttpå议的解æž,支æŒGET/POSTæ¨¡å¼ - * - * 2 内嵌支æŒå¯¹chunkç¼–ç çš„è§£æž - * - * 3 支æŒåŒæ­¥http请求和å“应(åªæ”¯æŒçŸ­è¿žæŽ¥,ä¸æ”¯æŒkeep-alive) - * - * 4 支æŒhttpå“应包体的增é‡è§£æž - * - * 5 增加Cookie管ç†ç±» - * - */ -///////////////////////////////////////////////// - - -/** -* @brief httpå议解æžå¼‚常类 -*/ -struct TC_Http_Exception : public TC_Exception -{ - TC_Http_Exception(const string &sBuffer) : TC_Exception(sBuffer){}; - ~TC_Http_Exception() throw(){}; -}; - -/** - * @brief http应答å议解æžå¼‚常类 - */ -struct TC_HttpResponse_Exception : public TC_Http_Exception -{ - TC_HttpResponse_Exception(const string &sBuffer) : TC_Http_Exception(sBuffer){}; - ~TC_HttpResponse_Exception() throw(){}; -}; - -/** - * @brief http请求å议解æžå¼‚常类 - */ -struct TC_HttpRequest_Exception : public TC_Http_Exception -{ - TC_HttpRequest_Exception(const string &sBuffer) : TC_Http_Exception(sBuffer){}; - ~TC_HttpRequest_Exception() throw(){}; -}; - -/** - * @brief 简å•çš„URL解æžç±». - * - * eg: - * ftp://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref. - * - * scheme: ftp. - * - * user:user. - * - * pass:password. - * - * domain:www.qq.com. - * - * port:8080. - * - * path:/abc/dev/query. - * - * query:a=b&c=3. - * - * ref:ref. - * - * request:/abc/dev/query?a=b&c=3#ref. - * - * relativePath:/abc/dev/. - * - * rootPath:ftp://user:password@www.qq.com:8080/. - */ - -class TC_URL -{ -public: - - /** - * @brief URL类型 - */ - enum URL_TYPE - { - HTTP, - HTTPS, - FTP - }; - - /** - * @brief 构造函数 - */ - TC_URL() : _iURLType(HTTP) - { - } - - /** - * @brief 解æžURL, url必须是ç»å¯¹è·¯å¾„. - * - * @throws TC_URL_Exception - * @param sURL 具体的URL串 - * @return 解æžæˆåŠŸè¿”回true,å¦åˆ™è¿”回false - */ - bool parseURL(const string &sURL); - - /** - * @brief 清空 - */ - void clear(); - - /** - * @brief 获å–scheme. - * - * @return const string&类型的scheme - */ - string getScheme() const; - - /** - * @brief 获å–用户å. - * - * @return const string& :用户å - */ - string getUserName() const; - - /** - * @brief 获å–密ç . - * - * @return const string& ï¼šå¯†ç  - */ - string getPassword() const; - - /** - * @brief 获å–域å. - * - * @return const string& :域å - */ - string getDomain() const; - - /** - * @brief 获å–端å£. - * - * @return const string& ï¼šç«¯å£ - */ - string getPort() const; - - /** - * @brief 是å¦æ˜¯ç¼ºçœçš„端å£. - * - * @return bool型,类缺çœçš„端å£è¿”回true,å¦åˆ™è¿”回false - */ - bool isDefaultPort() const; - - /** - * @brief 获å–路径. - * - * @return const string& :路径值 - */ - string getPath() const; - - /** - * @brief 获å–查询部分. - * - * @return string :查询结果 - */ - string getQuery() const; - - /** - * @brief å–获Request,ä¸åŒ…括Host, - * 例如http://www.qq.com/abc?a=b#ref则为:/abc?a=b#ref - * @return 符åˆä¸Šè¿°æ¡ä»¶çš„request - */ - string getRequest() const; - - /** - * @brief 获å–Ref. - * - * @return Ref串 - */ - string getRef() const; - - /** - * @brief 是å¦æœ‰æ•ˆ. - * - * @return 有效返回true,å¦åˆ™è¿”回false - */ - bool isValid() const; - - /** - * @brief 获å–解æžåŽURL. - * - * @return 解æžåŽçš„URL - */ - string getURL() const; - - /** - * @brief 获å–URL类型. - * - * @return URL类型 - */ - int getType() const { return _iURLType; } - - /** - * @brief 获å–相对路径. - * - * @return 相对路径字符串 - */ - string getRelativePath() const; - - /** - * @brief 获å–根路径. - * - * @return 根路径字符串 - */ - string getRootPath() const; - - /** - * @brief 调整路径. - * - * 以当å‰URL调整路径. - * - * 1 http://www.qq.com/a/b?a=b, /test/abc => http://www.qq.com/test/abc - * - * 2 http://www.qq.com/a/b?a=b, test/abc => http://www.qq.com/a/b/test/abc - * - * 3 http://www.qq.com/a/b?a=b, ../test/abc => http://www.qq.com/a/test/abc - * - * @param sRelativeURL: 相对当å‰URLçš„åœ°å€ - * @return 返回调整åŽçš„URL - */ - TC_URL buildWithRelativePath(const string &sRelativeURL) const; - - /** - * @brief 规整化. - */ - void specialize(); - -protected: - /** - * @brief æ¢æˆURL. - * - * @return URL串 - */ - string toURL(); - - /** - * @brief 获å–段. - * - * @param frag - * @return string - */ - //string getFragment(const string& frag) const; - - /** - * @brief ç±»åž‹åˆ°å­—ç¬¦ä¸²çš„è½¬æ¢ - * - * @return string:转æ¢åŽçš„字符串 - */ - string type2String() const; - - /** - * @brief 获å–å议的缺çœç«¯å£. - * - * @return string:缺çœç«¯å£ - */ - string getDefaultPort() const; - - /** - * @brief 简化URL. - * - * @param sPath 原路径 - * @return 简化åŽçš„URL - */ - string simplePath(const string &sPath) const; - -protected: - - /** - * URL类型 - */ - int _iURLType; - - string _sScheme; - string _sUser; - string _sPass; - string _sDomain; - string _sPort; - string _sPath; - string _sQuery; - string _sRef; - /** - * 解æžåŽçš„URL - */ - string _sURL; -}; - -/** - * @brief httpå议解æžç±», 请求和å“åº”éƒ½åœ¨è¯¥ç±»ä¸­è§£æž - */ -class TC_Http -{ -public: - /** - * @brief 构造函数 - */ - TC_Http() - { - TC_Http::reset(); - setConnection("close"); //默认就用短连接 - } - - /** - * @brief ä¸åŒºåˆ†å¤´éƒ¨å¤§å°å†™ - */ - struct CmpCase - { - bool operator()(const string &s1, const string &s2) const - { - //return TC_Common::upper(s1) < TC_Common::upper(s2); - if(strcasecmp(s1.c_str(), s2.c_str()) < 0) - { - return true; - } - else - { - return false; - } - } - }; - - typedef multimap http_header_type; - - /** - * @brief 删除头部. - * - * @param sHeader:å¤´éƒ¨ä¿¡æ¯ - */ - void eraseHeader(const string &sHeader) { _headers.erase(sHeader); } - - /** - * @brief 设置 Cache-Control. - * - * @param sCacheControl - */ - void setCacheControl(const string &sCacheControl){setHeader("Cache-Control", sCacheControl);} - - /** - * @brief 设置 Connection. - * - * @param sConnection:Connectionä¿¡æ¯ - */ - void setConnection(const string &sConnection){setHeader("Connection", sConnection);} - - /** - * @brief 设置 Content-Type. - * - * @param sContentType:Contentä¿¡æ¯ - */ - void setContentType(const string &sContentType){setHeader("Content-Type", sContentType);} - - /** - * @brief 设置内容长度. - * - * @param iContentLength:è¦è®¾ç½®çš„长度值 - */ - void setContentLength(size_t iContentLength) - { - setHeader("Content-Length", TC_Common::tostr(iContentLength)); - } - - /** - * @brief 设置 Referer. - * - * @param sReferer:Refererä¿¡æ¯ - */ - void setReferer(const string &sReferer){setHeader("Referer", sReferer);} - - /** - * @brief 设置 Host. - * - * @param sHost 例如 www.qq.com:80 - */ - void setHost(const string &sHost){setHeader("Host", sHost);} - - /** - * @brief 设置 Accept-Encoding. - * - * @param sAcceptEncoding, gzip, compress, deflate, identity - */ - void setAcceptEncoding(const string &sAcceptEncoding){setHeader("Accept-Encoding", sAcceptEncoding);} - - /** - * @brief 设置 Accept-Language. - * - * @param sAcceptLanguage:Accept-Languageä¿¡æ¯ - */ - void setAcceptLanguage(const string &sAcceptLanguage){setHeader("Accept-Language", sAcceptLanguage);} - - /** - * @brief 设置 Accept. - * - * @param sAccept Acceptä¿¡æ¯ - */ - void setAccept(const string &sAccept){setHeader("Accept", sAccept);} - - /** - * @brief 设置 Transfer-Encoding. - * - * @param sTransferEncoding:Transfer-Encodingä¿¡æ¯ - */ - void setTransferEncoding(const string& sTransferEncoding) {setHeader("Transfer-Encoding", sTransferEncoding); } - - /** - * @brief 设置header, - * 常用的值请使用已ç»æœ‰çš„get/set方法设置 - * (除Set-Cookie&Cookie外,头部字段ä¸èƒ½é‡å¤) - * @param sHeadName headerçš„åå­— - * @param sHeadValue header的值 - */ - void setHeader(const string &sHeadName, const string &sHeadValue) - { - //Set-Cookieå’ŒCookieå¯ä»¥æœ‰å¤šä¸ªå¤´ - const char * pStr1 = "SET-COOKIE"; - const char * pStr2 = "COOKIE";//原则上COOKIEåªæœ‰ä¸€ä¸ªï¼Œæ‹…心有兼容性问题,ä¿ç•™ - if((strcasecmp(sHeadName.c_str(), pStr1) != 0) && (strcasecmp(sHeadName.c_str(), pStr2) != 0)) - { - _headers.erase(sHeadName); - } - /* if(TC_Common::upper(sHeadName) != "SET-COOKIE" && TC_Common::upper(sHeadName) != "COOKIE") - { - _headers.erase(sHeadName); - }*/ - _headers.insert(multimap::value_type(sHeadName, sHeadValue)); - } - - /** - * @brief 设置header,常用的值请使用已ç»æœ‰çš„get/set方法设 - * (头部字段å¯ä»¥é‡å¤) - * @param sHeadName headerçš„åå­— - * @param sHeadValue header的值 - */ - void setHeaderMulti(const string &sHeadName, const string &sHeadValue) - { - _headers.insert(multimap::value_type(sHeadName, sHeadValue)); - } - - /** - * @brief 获å–头(é‡å¤çš„也æå–). - * - * @param sHeadName headerçš„åå­— - * @return vectorheader的值 - */ - vector getHeaderMulti(const string &sHeadName) const; - - /** - * @brief 获å–http内容长度. - * - * @return http内容长度. - */ - size_t getContentLength() const; - - /** - * @brief 获å–http请求Host. - * - * @return hostè¯·æ±‚ä¿¡æ¯ - */ - string getHost() const; - - /** - * @brief 获å–http头部长度. - * - * @return 头部长度 - */ - size_t getHeadLength() const { return _headLength; } - - /** - * @brief 获å–http内容. - * - * @return http内容串 - */ - string getContent() const { return _content; } - - /** - * @brief 设置http body(默认ä¸ä¿®æ”¹content-length). - * - * @param content http body内容 - * @param bUpdateContentLength 是å¦æ›´æ–°ContentLength - */ - void setContent(const string &content, bool bUpdateContentLength = false) - { - _content = content; - - if(bUpdateContentLength) - { - eraseHeader("Content-Length"); - if(_content.length() > 0) - setContentLength(_content.length()); - } - } - - /** - * @brief 获å–内容类型. - * - * @return string - */ - string getContentType() const; - - /** - * @brief 获å–http头部å‚数,(Set-Cookieå’ŒCookieä¸è¦ç”¨è¿™ä¸ªèŽ·å–, - * å•ç‹¬æœ‰å‡½æ•°èŽ·å–) - * @param sHeader headeråå­— - * @return headerçš„ç›¸å…³ä¿¡æ¯ - */ - string getHeader(const string& sHeader) const; - - /** - * @brief 获å–http头部map. - * - * @return http_header_type& - */ - const http_header_type& getHeaders() const{return _headers;} - - /** - * @brief é‡ç½® - */ - void reset(); - - /** - * @brief 读å–一行. - * - * @param ppChar 读å–ä½ç½®æŒ‡é’ˆ - * @return string 读å–的内容 - */ - static string getLine(const char** ppChar); - - /** - * @brief 读å–一行. - * - * @param ppChar 读å–ä½ç½®æŒ‡é’ˆ - * @param iBufLen 长度 - * @return string 读å–的内容 - */ - static string getLine(const char** ppChar, int iBufLen); - - /** - * @brief 生æˆå¤´éƒ¨å­—符串(ä¸åŒ…å«ç¬¬ä¸€è¡Œ). - * - * @return string:头部字符串 - */ - string genHeader() const; - - /** - * @brief 该http原始数æ®åŒ…是å¦æ˜¯chunkç¼–ç æ ¼å¼. - * - * @return bool:包å«è¿”回true,å¦åˆ™è¿”回false - */ - bool isChunked() const { return _bIsChunked; } - - /** - * @brief 解æžè¯·æ±‚head,ä¸è§£æžç¬¬ä¸€è¡Œ, - * 第一行请求包和å“应包ä¸ä¸€æ ·ï¼Œ åŽé¢çš„æ•°æ®è§£æžä¸ºmapæ ¼å¼ - * @param szBuffer - * @return const char*, å移的指针 - */ - static const char* parseHeader(const char* szBuffer, http_header_type &sHeader); - -protected: - - /** - * http头部所有å˜é‡ - */ - http_header_type _headers; - - /** - * http头部长度 - */ - size_t _headLength; - - /** - * http头部内容 - */ - string _content; - - /** - * 是å¦æ˜¯chunkæ¨¡å¼ - */ - bool _bIsChunked; -}; - -/********************* TC_HttpCookie***********************/ - -/** - * @brief 简å•çš„Cookie管ç†ç±». - */ -class TC_HttpCookie -{ -public: - typedef map http_cookie_data; - - struct Cookie - { - http_cookie_data _data; - string _domain; - string _path; - time_t _expires; //等于0表示åªåœ¨å½“å‰å›žè¯æœ‰æ•ˆ - bool _isSecure; - }; - - /** - * @brief æ¸…ç©ºæ•°æ® - */ - void clear(); - - /** - * @brief 增加Cookie到管ç†å™¨ä¸­. - * - * @param sCookieRspURL 产生该Cookie HTTPå“应的URL - * @param vCookies set-Cookie字符串 - */ - void addCookie(const string &sCookieRspURL, const vector &vCookies); - - /** - * @brief 增加Cookie到管ç†å™¨. - * - * @param cookie - */ - void addCookie(const Cookie &cookie); - - /** - * @brief 增加Cookie到管ç†å™¨. - * - * @param cookie - */ - void addCookie(const list &cookie); - - /** - * @brief 获å–æŸä¸ªurlçš„cookie域值对,去掉了Domain,Path等字段 - * @param sReqURL 需è¦åŒ¹é…çš„URL - * @param cookies - */ - void getCookieForURL(const string &sReqURL, list &cookies); - - /** - * @brief 获å–æŸä¸ªurlçš„cookie域值对,去掉了Domain,Path等字段 - * @param sReqURL 需è¦åŒ¹é…çš„URL - * @param sCookie - * @param string - */ - void getCookieForURL(const string &sReqURL, string &sCookie); - - /** - * @brief 匹é…域å,sCookieDomain串有多个域 - * sCookieDomain未以.开头, 则sCookieDomain=sDomain - * sCookieDomain以.开头,则sDomainå³è¾¹åŒ¹é…sDomain,sDomain比sCookieDomain以最多多一个. - * (x.y.z.com åŒ¹é… .y.z.com, x.y.z.com ä¸åŒ¹é….z.com ) - * @param sCookieDomain - * @param sDomain 域å - * @return bool 匹é…æˆåŠŸè¿”回true,å¦åˆ™è¿”回false - */ - static bool matchDomain(const string &sCookieDomain, const string &sDomain); - - /** - * @brief 匹é…路径. - * - * @param sCookiePath sPathçš„å‰ç¼€ - * @param sPath 路径 - * @return 匹é…路径的长度 - */ - static size_t matchPath(const string &sCookiePath, const string &sPath); - - /** - * @brief 获å–所有的Cookie. - * - * @return list& - */ - list getAllCookie(); - - /** - * @brief 删除过期的Cookie,仅仅存在与当å‰å›žè¯çš„Cookieä¸åˆ é™¤ - * @param t 超过当å‰æ—¶é—´çš„cookie都删除 - * @param bErase true:存在于当å‰å›žè¯çš„Cookie也删除 - * false:存在于当å‰å›žè¯çš„Cookieä¸åˆ é™¤ - */ - void deleteExpires(time_t t, bool bErase = false); - - /** - * @brief 返回所有没有过期的Cookie且有Expires字段的Cookie, - * å³ä»…仅存在与当å‰å›žè¯çš„Cookie是ä¸è¿”回的通常用于永久åºåˆ—化 - * @param t 超过当å‰æ—¶é—´çš„cookie都删除 - * @return list:所有符åˆæ¡ä»¶çš„cookie - */ - list getSerializeCookie(time_t t); - -protected: - /** - * @brief 判断Cookie是有效. - * - * @param tURL - * @return size_t - */ - size_t isCookieMatch(const Cookie &cookie, const TC_URL &tURL) const; - - /** - * @brief 检查Cookie是å¦è¿‡æœŸï¼Œå½“å‰å›žè¯çš„ä¸ç®—过期(expires=0) - * @param cookie 被检查的cookie对象 - * @return bool 过期返回true,å¦åˆ™è¿”回false - */ - bool isCookieExpires(const Cookie &cookie) const; - - /** - * @brief 添加Cookie. - * - * @param cookie è¦æ·»åŠ çš„cookie对象 - * @param cookies è¦è¢«æ·»åŠ çš„list对象 - */ - void addCookie(const Cookie &cookie, list &cookies); - - /** - * @brief 修正Domain. - * - * @param sDomain 修正å‰çš„Domain - * @return string 修正åŽçš„Domain - */ - bool fixDomain(const string &sDomain, string &sFixDomain); - -protected: - - /** - * @brief ä¿å­˜Cookie. - * - * key:domain+path - */ - list _cookies; -}; - -/********************* TC_HttpResponse ***********************/ - -class TC_HttpResponse : public TC_Http -{ -public: - - /** - * @brief 构造 - */ - TC_HttpResponse() - { - TC_HttpResponse::reset(); - } - - /** - * @brief resetå“应包 - */ - void reset(); - - /** - * @brief 增é‡decode,输入的buffer会自动在解æžè¿‡ç¨‹ä¸­è¢«æ¸…除掉, - * 增é‡decode之å‰å¿…é¡»reset, - * (网络接收的buffer直接添加到sBuffer里é¢å³å¯, 然åŽå¢žé‡è§£æž) - * (能够解æžçš„æ•°æ®TC_HttpResponse会自动从sBuffer里é¢æ¶ˆé™¤ï¼Œç›´åˆ°ç½‘络接收完毕或者解æžè¿”回true) - * @param buffer - * @throws TC_HttpResponse_Exception, ä¸æ”¯æŒçš„httpåè®®, 抛出异常 - * @return true:解æžå‡ºä¸€ä¸ªå®Œæ•´çš„buffer - * false:还需è¦ç»§ç»­è§£æžï¼Œå¦‚æžœæœåŠ¡å™¨ä¸»åŠ¨å…³é—­è¿žæŽ¥çš„模å¼ä¸‹ - * , 也å¯èƒ½ä¸éœ€è¦å†è§£æžäº† - */ - bool incrementDecode(string &sBuffer); - - /** - * @brief 解æžhttp应答(采用stringæ–¹å¼) , - * 注æ„:如果http头部没有Content-Length且éžchunk模å¼, 则返回true - * 需è¦ç½‘络层判断(httpæœåŠ¡ç«¯ä¸»åŠ¨å…³é—­è¿žæŽ¥ä¹Ÿç®—是å‘é€httpå“应完整了) - * @param sBuffer - * @return bool, sBuffer是å¦æ˜¯å®Œæ•´çš„http请求 - */ - bool decode(const string &sBuffer); - - /** - * @brief 解æžhttp应答, - * 注æ„:如果http头部没有Content-Length且éžchunk模å¼, 则返回true - * 需è¦ç½‘络层判断(httpæœåŠ¡ç«¯ä¸»åŠ¨å…³é—­è¿žæŽ¥ä¹Ÿç®—是å‘é€httpå“应完整了) - * @param sBuffer - * @return bool, sBuffer是å¦æ˜¯å®Œæ•´çš„http请求 - */ - bool decode(const char *sBuffer, size_t iLength); - - /** - * @brief ç¼–ç åº”答包(采用stringæ–¹å¼). - * - * @param sBuffer - * @return string ç¼–ç åŽçš„应答包 - */ - string encode() const; - - /** - * @brief ç¼–ç åº”答包(采用vectoræ–¹å¼). - * - * @param sBuffer - * @return string ç¼–ç åŽçš„应答包(vectorå½¢å¼çš„) - */ - void encode(vector &sBuffer) const; - - /** - * @brief 获å–第一行(HTTP/1.1 200 OK). - * - * @return string 获å–的内容 - */ - string getResponseHeaderLine() const { return _headerLine; } - - /** - * @brief 获å–HTTPå“应状æ€(例如200). - * - * @return int - */ - int getStatus() const {return _status; } - - /** - * @brief 设置状æ€. - * - * @param status 状æ€å€¼ - */ - void setStatus(int status) { _status = status; } - - /** - * @brief 获å–æè¿°(例如OK). - * - * @return string æè¿°ä¿¡æ¯ - */ - string getAbout() const { return _about; } - - /** - * @brief 设置æè¿°. - * - * @param about æè¿°ä¿¡æ¯ - */ - void setAbout(const string &about) { _about = about; } - - /** - * @brief 获å–版本, 例如HTTP/1.1 . - * - * @return string ç‰ˆæœ¬ä¿¡æ¯ - */ - string getVersion() const { return _version; } - - /** - * @brief å¦ç§æ¨¡å¼:HTTP/1.1(默认)或者HTTP/1.0 . - * - * @param sVersion - */ - void setVersion(const string &sVersion) { _version = sVersion; } - - /** - * @brief 设置应答状æ€(采用stringæ–¹å¼). - * - * @param status 状æ€ç  - * @param about æè¿°ä¿¡æ¯ - * @param sBody poståè®®body的内容 - */ - void setResponse(int status = 200, const string& about = "OK", const string& sBody = ""); - - /** - * @brief 设置应答状æ€. - * - * @param status 状æ€ç  - * @param about æè¿°ä¿¡æ¯ - * @param sBuffer poståè®®body的内容 - * @param iLength sBuffer长度 - */ - void setResponse(int status, const string& about, const char *sBuffer, size_t iLength); - - /** - * @brief 设置应答, 缺çœstatus=200, about=OK. - * - * @param sBuffer 应答内容 - * @param iLength sBuffer长度 - */ - void setResponse(const char *sBuffer, size_t iLength); - - /** - * @brief 设置æœåŠ¡. - * - * @param sServer æœåŠ¡çš„ä¿¡æ¯ - */ - void setServer(const string &sServer){setHeader("Server", sServer);} - - /** - * @brief 设置Set-Cookie. - * - * @param sCookie Cookieä¿¡æ¯ - */ - void setSetCookie(const string &sCookie){setHeader("Set-Cookie", sCookie);} - - /** - * @brief 获å–Set-Cookie. - * - * @return vector - */ - vector getSetCookie() const; - - /** - * @brief 解æžåº”答头. - * - * @param szBuffer åº”ç­”å¤´ä¿¡æ¯ - * @return - */ - void parseResponseHeader(const char* szBuffer); - -protected: - - /** - * åº”ç­”çŠ¶æ€ - */ - int _status; - - /** - * 应答æè¿° - */ - string _about; - - /** - * 获å–版本 - */ - string _version; - - /** - * 获å–第一行 - */ - string _headerLine; - - /** - * 临时的content length - */ - size_t _iTmpContentLength; -}; - -/********************* TC_HttpRequest ***********************/ - -class TC_HttpRequest : public TC_Http -{ -public: - - /////////////////////////////////////////////////////////////////// - TC_HttpRequest() - { - TC_HttpRequest::reset(); - setUserAgent("TC_Http"); - } - - /** - * @brief 请求å议类型 - */ - enum - { - REQUEST_GET, - REQUEST_POST, - REQUEST_OPTIONS, - REQUEST_HEAD, - REQUEST_PUT, - REQUEST_DELETE, - REQUEST_PATCH, - }; - - /** - * @brief 检查http请求是å¦æ”¶å…¨. - * - * @param sBuffer http请求 - * @throws TC_HttpRequest_Exception, ä¸æ”¯æŒçš„httpåè®®, 抛出异常 - * @return true: 收全, false:ä¸å…¨ - */ - static bool checkRequest(const char* sBuffer, size_t len); - - /** - * @brief é‡ç½® - */ - void reset(); - - /** - * @brief 设置 User-Agent. - * - * @param sUserAgent - */ - void setUserAgent(const string &sUserAgent){setHeader("User-Agent", sUserAgent);} - - /** - * @brief 设置 Cookie. - * - * @param sCookie - */ - void setCookie(const string &sCookie){setHeader("Cookie", sCookie);} - - /** - * @brief 获å–原始Cookieè¡Œ. - * - * @return vector - */ - vector getCookie(); - - /** - * @brief 解æžhttp请求, 如果ä¸æ˜¯HTTP请求则抛出异常. - * - * @param sBuffer è¦è§£æžçš„http请求 - * @return sBuffer是å¦æ˜¯å®Œæ•´çš„http请求 - * @throw TC_HttpRequest_Exception - */ - bool decode(const string &sBuffer); - - /** - * @brief 解æžhttp请求, - * 如果ä¸æ˜¯HTTP请求则抛出异常(采用vectoræ–¹å¼). - * - * @param sBuffer http请求 - * @param iLength 长度 - * @throw TC_HttpRequest_Exception - * @return sBuffer是å¦æ˜¯å®Œæ•´çš„http请求 - */ - bool decode(const char *sBuffer, size_t iLength); - - /** - * @brief 生æˆè¯·æ±‚(采用stringæ–¹å¼). - * - * @param sUrl - * @return string - */ - string encode(); - - /** - * @brief 生æˆè¯·æ±‚(采用vectoræ–¹å¼). - * - * @param buffer请求内容 - */ - void encode(vector &buffer); - - /** - * @brief 设置请求包. - * - * @param sUrl 例如:http://www.qq.com/query?a=b&c=d - * @param bCreateHost 是å¦æ–°åˆ›å»ºå¤´éƒ¨çš„Hostä¿¡æ¯ - * (默认, 如果有Hostä¿¡æ¯äº†, å°±ä¸åˆ›å»º) - * (注æ„, 是在encode的时候创建的) - */ - void setRequest(const string& method, const string &sUrl, const std::string& body = "", bool bNewCreateHost = false); - /** - * @brief 设置Get请求包. - * - * @param sUrl 例如:http://www.qq.com/query?a=b&c=d - * @param bCreateHost 是å¦æ–°åˆ›å»ºå¤´éƒ¨çš„Hostä¿¡æ¯ - * (默认, 如果有Hostä¿¡æ¯äº†, å°±ä¸åˆ›å»º) - * (注æ„, 是在encode的时候创建的) - */ - void setGetRequest(const string &sUrl, bool bNewCreateHost = false); - - /** - * @brief 设置Head请求包. - * - * @param sUrl 例如:http://www.qq.com/query?a=b&c=d - * @param bCreateHost 是å¦æ–°åˆ›å»ºå¤´éƒ¨çš„Hostä¿¡æ¯ - * (默认, 如果有Hostä¿¡æ¯äº†, å°±ä¸åˆ›å»º) - * (注æ„, 是在encode的时候创建的) - */ - void setHeadRequest(const string &sUrl, bool bNewCreateHost = false); - - /** - * @brief 设置POST请求包(采用stringæ–¹å¼). - * - * @param sUrl 例如:http://www.qq.com/query - * @param sPostBody 如果requestType是GET, 则sPostBody无效 - * @param bCreateHost 是å¦æ–°åˆ›å»ºå¤´éƒ¨çš„Hostä¿¡æ¯ - * (默认, 如果有Hostä¿¡æ¯äº†, å°±ä¸åˆ›å»º) - * (注æ„, 是在encode的时候创建的) - */ - void setPostRequest(const string &sUrl, const string &sPostBody, bool bNewCreateHost = false); - - /** - * @brief 设置POST请求包(采用vectoræ–¹å¼). - * - * @param sUrl 例如:http://www.qq.com/query - * @param sPostBody 如果requestType是GET, 则sPostBody无效 - * @param bCreateHost 是å¦æ–°åˆ›å»ºå¤´éƒ¨çš„Hostä¿¡æ¯ - * (默认, 如果有Hostä¿¡æ¯äº†, å°±ä¸åˆ›å»º) - * (注æ„, 是在encode的时候创建的) - */ - void setPostRequest(const string &sUrl, const char *sBuffer, size_t iLength, bool bNewCreateHost = false); - - void setPutRequest(const string &sUrl, const string &sPostBody, bool bNewCreateHost = false); - void setPatchRequest(const string &sUrl, const string &sPostBody, bool bNewCreateHost = false); - - void setDeleteRequest(const string &sUrl, const string &sPostBody, bool bNewCreateHost = false); - - /** - * @brief 设置Get请求包. - * - * @param sUrl 例如:http://www.qq.com/query?a=b&c=d - * @param bCreateHost 是å¦æ–°åˆ›å»ºå¤´éƒ¨çš„Hostä¿¡æ¯ - * (默认, 如果有Hostä¿¡æ¯äº†, å°±ä¸åˆ›å»º) - * (注æ„, 是在encode的时候创建的) - */ - void setOptionsRequest(const string &sUrl, bool bNewCreateHost = false); - - /** - * @brief 获å–url里é¢çš„地å€å’Œç«¯å£. - * - * @param sHost - * @param iPort - */ - void getHostPort(string &sDomain, uint32_t &iPort); - - /** - * @brief å‘é€HTTP请求. - * - * @param iTimeout 毫秒 - * @return 0 æˆåŠŸï¼› - * <0失败, 具体值å‚è§TC_ClientSocket声明 - */ - int doRequest(TC_HttpResponse &stHttpRep, int iTimeout = 3000); - - /** - * @brief 请求类型. - */ - int requestType() const { return _requestType ; } - - /** - * @brief 是å¦æ˜¯GET请求. - * - * @return 是GET请求返回true,å¦åˆ™è¿”回false - */ - bool isGET() const { return _requestType == REQUEST_GET; } - - /** - * @brief 是å¦æ˜¯HEAD请求. - * - * @return 是HEAD请求返回true,å¦åˆ™è¿”回false - */ - bool isHEAD() const { return _requestType == REQUEST_HEAD; } - - /** - * @brief 是å¦æ˜¯POST请求. - * - * @return 是GET请求返回true,å¦åˆ™è¿”回false - */ - bool isPOST() const { return _requestType == REQUEST_POST; } - - /** - * @brief 是å¦æ˜¯OPTIONS请求. - * - * @return 是GET请求返回true,å¦åˆ™è¿”回false - */ - bool isOPTIONS() const { return _requestType == REQUEST_OPTIONS; } - - /** - * @brief 是å¦æ˜¯Delete请求. - * - * @return 是delete请求返回true,å¦åˆ™è¿”回false - */ - bool isDELETE() const { return _requestType == REQUEST_DELETE; } - - /** - * @brief 获å–请求的URL. - * - * @return const TC_URL& - */ - const TC_URL &getURL() const { return _httpURL; } - - /** - * @brief 获å–完整的http请求. - * - * @return http请求串 - */ - string getOriginRequest() const { return _httpURL.getURL(); } - - /** - * @brief 获å–http请求的url,ä¸åŒ…括Host, - * 例如http://www.qq.com/abc?a=b, 则为:/abc?a=b - * @return http请求的url - */ - string getRequest() const { return _httpURL.getRequest(); } - - /** - * @brief 获å–http请求的url部分, å³?å‰é¢ï¼Œä¸åŒ…括Host, - * 例如http://www.qq.com/abc?a=b#def, 则为:/abc - * @return http请求的url部分 - * */ - string getRequestUrl() const { return _httpURL.getPath(); } - - /** - * @brief 获å–http请求urlåŽçš„å‚数部分,å³?åŽé¢#å‰é¢éƒ¨åˆ†ä¸ - * 包括Host, 例如http://www.qq.com/abc?a=b#def, 则为:a=b - * @return http请求urlåŽçš„å‚数部分 - */ - string getRequestParam() const { return _httpURL.getQuery(); } - - /** - * @brief 解æžè¯·æ±‚头部. - * - * @param szBuffer 请求头部 - * @return size_t - */ - size_t parseRequestHeader(const char* szBuffer); - - /** - * @brief 请求类型到字符串. - * - * @param iRequestType 请求 - * @return 解æžåŽçš„字符串 - */ - string requestType2str(int iRequestType) const; - -protected: - - /** - * @brief 对http请求编ç . - * - * @param sUrl 需è¦è¿›è¡Œç¼–ç çš„http请求 - * @param iRequestType ç¼–ç åŽçš„è¾“å‡ºæµ - * @return void - */ - void encode(int iRequestType, ostream &os); - - /** - * @brief 解æžURL - */ - void parseURL(const string& sUrl); - -protected: - - /** - * 请求URL - */ - TC_URL _httpURL; - - /** - * 请求类型 - */ - int _requestType; -}; - - -} -#endif - diff --git a/cpp/util/include/util/tc_http2clientmgr.h b/cpp/util/include/util/tc_http2clientmgr.h deleted file mode 100644 index af8bb69fe..000000000 --- a/cpp/util/include/util/tc_http2clientmgr.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_HTTP2CLIENTMANAGER_H -#define __TC_HTTP2CLIENTMANAGER_H - -#if TARS_HTTP2 - -#include - -#include "util/tc_singleton.h" - -namespace tars -{ - -///////////////////////////////////////////////// -/** - *@file tc_http2clientmgr.h - *@brief http2客户端sessioné›†åˆ - */ -///////////////////////////////////////////////// - -class TC_NgHttp2; - -class Http2ClientSessionManager : public TC_Singleton -{ -public: - Http2ClientSessionManager(); - ~Http2ClientSessionManager(); - - TC_NgHttp2* getSession(int id, bool isServer = false); - - bool delSession(int id); - -private: - typedef std::map SESSION_MAP; - SESSION_MAP _sessions; -}; - -} // end namespace tars - -#endif // end #if TARS_SSL - -#endif - diff --git a/cpp/util/include/util/tc_http2session.h b/cpp/util/include/util/tc_http2session.h deleted file mode 100644 index 3e02f6a88..000000000 --- a/cpp/util/include/util/tc_http2session.h +++ /dev/null @@ -1,160 +0,0 @@ -#ifndef __TC_HTTP2_H__ -#define __TC_HTTP2_H__ - -#if TARS_HTTP2 - -#include "util/tc_thread.h" -#include "util/tc_autoptr.h" -#include "util/tc_http.h" -#include "nghttp2/nghttp2.h" - -namespace tars -{ - -typedef enum -{ - REQUEST_GET, - REQUEST_POST, - REQUEST_OPTIONS, - REQUEST_HEAD, - REQUEST_PUT, - REQUEST_DELETE -}Req_Type; - -typedef int (*ResponseFunc)(const Req_Type reqtype, - const string &requri, - const TC_Http::http_header_type &reqHeader, - const string &reqBody, - int &resopnseStatus, - string &resopnseAbout, - TC_Http::http_header_type &responseHeader, - string &responseBody); - - -class TC_Http2Session: public TC_HandleBase -{ -public: - - struct Http2Response - { - int status; - string about; - TC_Http::http_header_type header; - string body; - }; - - int parse(string &in, string &out); - - int getRequest(const vector &request, vector& vtReqid); - - int doResopnse(int32_t reqid, const Http2Response &response, vector& out); - - int getMethod(int32_t reqid, Req_Type &method) - { - TC_ThreadLock::Lock lock(reqLock_); - map::iterator it = mReq_.find(reqid); - if (it != mReq_.end()) - method = it->second.method; - else - return -1; - } - - int getUri(int32_t reqid, string &uri) - { - TC_ThreadLock::Lock lock(reqLock_); - map::iterator it = mReq_.find(reqid); - if (it != mReq_.end()) - uri = it->second.uri; - else - return -1; - } - - int getHeader(int32_t reqid, TC_Http::http_header_type &header) - { - TC_ThreadLock::Lock lock(reqLock_); - map::iterator it = mReq_.find(reqid); - if (it != mReq_.end()) - header = it->second.header; - else - return -1; - } - - int getBody(int32_t reqid, string &body) - { - TC_ThreadLock::Lock lock(reqLock_); - map::iterator it = mReq_.find(reqid); - if (it != mReq_.end()) - body = it->second.body; - else - return -1; - } - - int doRequest(const vector &request, vector& response); - - void setResponseFunc(ResponseFunc func) - { - responseFunc_ = func; - } - - struct RequestPack - { - RequestPack():streamId(0), bFinish(false){} - - Req_Type method; - string uri; - TC_Http::http_header_type header; - string body; - int32_t streamId; - bool bFinish; - }; - - struct DataPack - { - DataPack(){} - - DataPack(string &data, int pos):dataBuf(data), readPos(pos){} - - string dataBuf; - unsigned int readPos; - }; - - TC_Http2Session(); - - ~TC_Http2Session(); - - TC_ThreadLock responseBufLock_; - string responseBuf_; - - TC_ThreadLock reqLock_; - map mReq_; - - string reqout_; - -private: - - int (*responseFunc_)(const Req_Type reqtype, - const string &requri, - const TC_Http::http_header_type &reqHeader, - const string &reqBody, - int &resopnseStatus, - string &resopnseAbout, - TC_Http::http_header_type &responseHeader, - string &responseBody); - - nghttp2_session *session_; - - bool bNewCon_; - - TC_ThreadLock nghttpLock; - - bool bOldVersion_; - bool bUpgrade_; -}; - -typedef TC_AutoPtr TC_Http2SessionPtr; - -} - -#endif - -#endif diff --git a/cpp/util/include/util/tc_http_async.h b/cpp/util/include/util/tc_http_async.h deleted file mode 100644 index ebd9a53ef..000000000 --- a/cpp/util/include/util/tc_http_async.h +++ /dev/null @@ -1,475 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_HTTP_ASYNC_H_ -#define __TC_HTTP_ASYNC_H_ - -#include -#include "util/tc_thread_pool.h" -#include "util/tc_http.h" -#include "util/tc_autoptr.h" -#include "util/tc_socket.h" - -namespace tars -{ - -///////////////////////////////////////////////// -/** -* @file tc_http_async.h -* @brief http异步调用类. -* -* httpåŒæ­¥è°ƒç”¨ä½¿ç”¨TC_HttpRequest::doRequestå°±å¯ä»¥äº† -*/ -///////////////////////////////////////////////// - - -/** -* @brief 线程异常 -*/ -struct TC_HttpAsync_Exception : public TC_Exception -{ - TC_HttpAsync_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_HttpAsync_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_HttpAsync_Exception() throw(){}; -}; - -/** - * @brief 异步线程处ç†ç±». - * - * 异步HTTP请求 ,使用方å¼ç¤ºä¾‹å¦‚下: - * - * 实现异步回调对象 - * - * 异步对象回调执行的时候是在TC_HttpAsync中线程执行的 - * - * 用智能指针new出æ¥, ä¸ç”¨ç®¡ç”Ÿå‘½å‘¨æœŸ - * - * class AsyncHttpCallback : public - * - * TC_HttpAsync::RequestCallback - * - * { - * - * public: - * - * AsyncHttpCallback(const string &sUrl) : _sUrl(sUrl) - * - * { - * - * } - * - * virtual void onException(const string &ex) - * - * { - * - * cout << "onException:" << _sUrl << ":" << ex << endl; - * - * } - * - * //请求回æ¥çš„时候onResponse被调用 - * - * //bClose表示æœåŠ¡ç«¯å…³é—­äº†è¿žæŽ¥ ,从而认为收到了一个完整的httpå“应 - * - * virtual void onResponse(bool bClose, TC_HttpResponse - * - * &stHttpResponse) - * - * { - * - * cout << "onResponse:" << _sUrl << ":" << - * - * TC_Common::tostr(stHttpResponse.getHeaders()) << - * - * endl; - * } - * - * virtual void onTimeout() - * - * { - * - * cout << "onTimeout:" << _sUrl << endl; - * - * } - * - * - * //连接被关闭时调用 - * - * virtual void onClose() - * - * { - * - * cout << "onClose:" << _sUrl << endl; - * - * } - * - * protected: - * - * string _sUrl; - * - * }; - * - * //å°è£…一个函数, æ ¹æ®å®žé™…æƒ…å†µå¤„ç† - * - * int addAsyncRequest(TC_HttpAsync &ast, const string &sUrl) { - * - * TC_HttpRequest stHttpReq; stHttpReq.setGetRequest(sUrl); - * - * - * //new出æ¥ä¸€ä¸ªå¼‚步回调对象 - * - * TC_HttpAsync::RequestCallbackPtr p = new - * - * AsyncHttpCallback(sUrl); - * - * return ast.doAsyncRequest(stHttpReq, p); - * - * } - * - * //具体使用的示例代ç å¦‚下: - * - * TC_HttpAsync ast; - * - * ast.setTimeout(1000); //设置异步请求超时时间 - * - * ast.start(); - * - * //真正的代ç éœ€è¦åˆ¤æ–­è¿”回值,返回值=0æ‰è¡¨ç¤ºè¯·æ±‚å·²ç»å‘é€å‡ºåŽ»äº† - * - * int ret = addAsyncRequest(ast, "www.baidu.com"); - * - * addAsyncRequest(ast, "www.qq.com"); - * - * addAsyncRequest(ast, "www.google.com"); - * - * addAsyncRequest(ast, "http://news.qq.com/a/20100108/002269.htm"); - * - * addAsyncRequest(ast, "http://news.qq.com/zt/2010/mtjunshou/"); - * - * addAsyncRequest(ast,"http://news.qq.com/a/20100108/000884.htm"); - * - * addAsyncRequest(ast,"http://news.qq.com/a/20100108/000884.htm"); - * - * addAsyncRequest(ast,"http://tech.qq.com/zt/2009/renovate/index.htm"); - * - * ast.waitForAllDone(); - * - * ast.terminate(); - */ -class TC_HttpAsync : public TC_Thread, public TC_ThreadLock -{ -public: - /** - * @brief 异步请求回调对象 - */ - class RequestCallback : public TC_HandleBase - { - public: - /** - * @brief 完整的å“应回æ¥äº†. - * - * @param bClose 因为远程æœåŠ¡å™¨å…³é—­è¿žæŽ¥è®¤ä¸ºhttp完整了 - * @param stHttpResponse httpå“应包 - */ - virtual void onResponse(bool bClose, TC_HttpResponse &stHttpResponse) = 0; - - /** - * @brief æ¯æ¬¡æ”¶åˆ°æ•°æ®ä¸”http头收全了都会调用, - * stHttpResponseçš„æ•°æ®å¯èƒ½ä¸æ˜¯å®Œå…¨çš„httpå“åº”æ•°æ® ,åªæœ‰éƒ¨åˆ†bodyæ•°æ® - * @param stHttpResponse 收到的httpæ•°æ® - * @return true:继续收å–æ•°æ®, false:ä¸æ”¶å–æ•°æ®äº† - */ - virtual bool onReceive(TC_HttpResponse &stHttpResponse) { return true;}; - - /** - * @brief 异常. - * - * @param ex 异常原因 - */ - virtual void onException(const string &ex) = 0; - - /** - * @brief 超时没有å“应 - */ - virtual void onTimeout() = 0; - - /** - * @brief 连接被关闭 - */ - virtual void onClose() = 0; - }; - - typedef TC_AutoPtr RequestCallbackPtr; - -protected: - /** - * @brief 异步http请求(短连接) - */ - class AsyncRequest : public TC_HandleBase - { - public: - /** - * @brief 构造. - * - * @param stHttpRequest - * @param callbackPtr - */ - AsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr); - - /** - * @brief æžæž„ - */ - ~AsyncRequest(); - - /** - * @brief 获å–å¥æŸ„ - * - * @return int - */ - int getfd() { return _fd.getfd(); } - - /** - * @brief å‘起建立连接. - * - * @return int - */ - int doConnect(); - - /** - * @brief å‘起建立到addr的连接,ä¸ç”¨DNS解æž. - * - * @param addr å¯ç”¨äºŽç›´æŽ¥è¿žæŽ¥é€æ˜Žä»£ç†æˆ–者连接通过DNS解æžåŽçš„åœ°å€ - * @return int - */ - int doConnect(struct sockaddr* addr); - - /** - * @brief å‘生异常 - */ - void doException(); - - /** - * @brief å‘é€è¯·æ±‚ - */ - void doRequest(); - - /** - * @brief 接收å“应 - */ - void doReceive(); - - /** - * @brief 关闭连接 - */ - void doClose(); - - /** - * @brief 超时 - */ - void timeout(); - - /** - * @brief 设置唯一ID. - * - * @param uniqId - */ - void setUniqId(uint32_t uniqId) { _iUniqId = uniqId;} - - /** - * @brief 获å–唯一ID. - * - * @return uint32_t - */ - uint32_t getUniqId() const { return _iUniqId; } - - /** - * @brief 设置处ç†è¯·æ±‚çš„http异步线程. - * - * @param pHttpAsync :异步线程处ç†å¯¹è±¡ - */ - void setHttpAsync(TC_HttpAsync *pHttpAsync) { _pHttpAsync = pHttpAsync; } - - /** - * @brief 设置å‘网络请求时绑定的ip地å€. - * - * @param addr - */ - void setBindAddr(const struct sockaddr* addr); - - protected: - /** - * @brief 接收请求. - * - * @param buf - * @param len - * @param flag - * @return int - */ - int recv(void* buf, uint32_t len, uint32_t flag); - - /** - * @brief å‘é€è¯·æ±‚. - * - * @param buf å‘é€å†…容 - * @param len å‘é€é•¿åº¦ - * @param flag - * @return int - */ - int send(const void* buf, uint32_t len, uint32_t flag); - - protected: - TC_HttpAsync *_pHttpAsync; - TC_HttpResponse _stHttpResp; - TC_Socket _fd; - string _sHost; - uint32_t _iPort; - uint32_t _iUniqId; - string _sReq; - string _sRsp; - RequestCallbackPtr _callbackPtr; - bool _bindAddrSet; - struct sockaddr _bindAddr; - }; - - typedef TC_AutoPtr AsyncRequestPtr; - -public: - - typedef TC_TimeoutQueue http_queue_type; - - /** - * @brief 构造函数 - */ - TC_HttpAsync(); - - /** - * @brief æžæž„函数 - */ - ~TC_HttpAsync(); - - /** - * @brief 异步å‘起请求. - * - * @param stHttpRequest - * @param httpCallbackPtr - * @param bUseProxy,是å¦ä½¿ç”¨ä»£ç†æ–¹å¼è¿žæŽ¥ - * @param addr, bUseProxy为false ç›´æŽ¥è¿žæŽ¥æŒ‡å®šçš„åœ°å€ - * @return int, <0:å‘起连接失败, å¯ä»¥é€šè¿‡strerror(返回值) - * =0:æˆåŠŸ - */ - int doAsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr, bool bUseProxy=false, struct sockaddr* addr=NULL); - - /** - * @brief 设置proxyåœ°å€ - * - */ - int setProxyAddr(const char* Host,uint16_t Port); - - /** - * @brief 设置代ç†çš„地å€. - * - * ä¸é€šè¿‡åŸŸå解æžå‘é€,直接å‘é€åˆ°ä»£ç†æœåŠ¡å™¨çš„ip地å€) - * @param sProxyAddr æ ¼å¼ 192.168.1.2:2345 或者 sslproxy.qq.com:2345 - */ - int setProxyAddr(const char* sProxyAddr); - - /** - * @brief 设置绑定的地å€. - * - * @param sProxyAddr æ ¼å¼ 192.168.1.2 - */ - int setBindAddr(const char* sBindAddr); - - /** - * @brief 设置绑定的地å€. - * - * @param addr 直接用 addr 赋值 - */ - void setProxyAddr(const struct sockaddr* addr); - - /** - * @brief å¯åŠ¨å¼‚步处ç†. - * - * å‚æ•°å·²ç»æ— æ•ˆ(网络层有且åªæœ‰ä¸€ä¸ªçº¿ç¨‹) - * @param num, 异步处ç†çš„线程数 - */ - void start(int iThreadNum = 1); - - /** - * @brief 设置超时(所有请求都åªèƒ½ç”¨ä¸€ç§è¶…时时间). - * - * @param timeout: 毫秒, 但是具体的超时精度åªèƒ½åœ¨så·¦å³ - */ - void setTimeout(int millsecond) { _data->setTimeout(millsecond); } - - /** - * @brief 等待请求全部结æŸ(等待毫秒精度在100mså·¦å³). - * - * @param millsecond, 毫秒 -1表示永远等待 - */ - void waitForAllDone(int millsecond = -1); - - /** - * @brief 结æŸçº¿ç¨‹ - */ - void terminate(); - -protected: - - using async_process_type = std::function; - - /** - * @brief 超时处ç†. - * - * @param ptr - */ - static void timeout(AsyncRequestPtr& ptr); - - /** - * @brief å…·ä½“ç½‘ç»œå¤„ç† - */ - static void process(AsyncRequestPtr &p, int events); - - /** - * @brief 具体的网络处ç†é€»è¾‘ - */ - void run() ; - - /** - * @brief 删除异步请求对象 - */ - void erase(uint32_t uniqId); - - friend class AsyncRequest; - -protected: - TC_ThreadPool _tpool; - - http_queue_type *_data; - - TC_Epoller _epoller; - - bool _terminate; - - struct sockaddr _proxyAddr; - - struct sockaddr _bindAddr; - - bool _bindAddrSet; -}; - -} -#endif - diff --git a/cpp/util/include/util/tc_lock.h b/cpp/util/include/util/tc_lock.h deleted file mode 100644 index 84b9f6b2e..000000000 --- a/cpp/util/include/util/tc_lock.h +++ /dev/null @@ -1,281 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _TC_LOCK_H -#define _TC_LOCK_H - -#include -#include -#include -#include "util/tc_ex.h" - -using namespace std; - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_lock.h - * @brief é”ç±» - */ -///////////////////////////////////////////////// - - -/** -* @brief é”异常 -*/ -struct TC_Lock_Exception : public TC_Exception -{ - TC_Lock_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_Lock_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_Lock_Exception() throw() {}; -}; - -/** - * @brief é”模æ¿ç±»å…¶ä»–具体é”é…åˆä½¿ç”¨ï¼Œ - * 构造时候加é”,æžå¤Ÿçš„æ—¶å€™è§£é” - */ -template -class TC_LockT -{ -public: - - /** - * @brief æž„é€ å‡½æ•°ï¼Œæž„é€ æ—¶æž·é” - * - * @param mutex é”对象 - */ - TC_LockT(const T& mutex) : _mutex(mutex) - { - _mutex.lock(); - _acquired = true; - } - - /** - * @brief æžæž„,æžæž„æ—¶è§£é” - */ - virtual ~TC_LockT() - { - if (_acquired) - { - _mutex.unlock(); - } - } - - /** - * @brief 上é”, 如果已ç»ä¸Šé”,则抛出异常 - */ - void acquire() const - { - if (_acquired) - { - throw TC_Lock_Exception("thread has locked!"); - } - _mutex.lock(); - _acquired = true; - } - - /** - * @brief å°è¯•ä¸Šé”. - * - * @return æˆåŠŸè¿”回true,å¦åˆ™è¿”回false - */ - bool tryAcquire() const - { - _acquired = _mutex.tryLock(); - return _acquired; - } - - /** - * @brief 释放é”, 如果没有上过é”, 则抛出异常 - */ - void release() const - { - if (!_acquired) - { - throw TC_Lock_Exception("thread hasn't been locked!"); - } - _mutex.unlock(); - _acquired = false; - } - - /** - * @brief 是å¦å·²ç»ä¸Šé”. - * - * @return 返回trueå·²ç»ä¸Šé”,å¦åˆ™è¿”回false - */ - bool acquired() const - { - return _acquired; - } - -protected: - - /** - * @brief 构造函数 - * 用于é”å°è¯•æ“作,与TC_LockT相似 - * - */ - TC_LockT(const T& mutex, bool) : _mutex(mutex) - { - _acquired = _mutex.tryLock(); - } - -private: - - // Not implemented; prevents accidental use. - TC_LockT(const TC_LockT&); - TC_LockT& operator=(const TC_LockT&); - -protected: - /** - * é”对象 - */ - const T& _mutex; - - /** - * 是å¦å·²ç»ä¸Šé” - */ - mutable bool _acquired; -}; - -/** - * @brief å°è¯•ä¸Šé” - */ -template -class TC_TryLockT : public TC_LockT -{ -public: - - TC_TryLockT(const T& mutex) : TC_LockT(mutex, true) - { - } -}; - -/** - * @brief 空é”, ä¸åšä»»ä½•é”动作 - */ -class TC_EmptyMutex -{ -public: - /** - * @brief 写é”. - * - * @return int, 0 正确 - */ - int lock() const {return 0;} - - /** - * @brief è§£å†™é” - */ - int unlock() const {return 0;} - - /** - * @brief å°è¯•è§£é”. - * - * @return int, 0 正确 - */ - bool trylock() const {return true;} -}; - -/** - * @brief 读写é”读é”模æ¿ç±» - * 构造时候加é”,æžå¤Ÿçš„æ—¶å€™è§£é” - */ - -template -class TC_RW_RLockT -{ -public: - /** - * @brief æž„é€ å‡½æ•°ï¼Œæž„é€ æ—¶æž·é” - * - * @param lock é”对象 - */ - TC_RW_RLockT(T& lock) - : _rwLock(lock),_acquired(false) - { - _rwLock.ReadLock(); - _acquired = true; - } - - /** - * @brief æžæž„æ—¶è§£é” - */ - ~TC_RW_RLockT() - { - if (_acquired) - { - _rwLock.Unlock(); - } - } -private: - /** - *é”对象 - */ - const T& _rwLock; - - /** - * 是å¦å·²ç»ä¸Šé” - */ - mutable bool _acquired; - - TC_RW_RLockT(const TC_RW_RLockT&); - TC_RW_RLockT& operator=(const TC_RW_RLockT&); -}; - -template -class TC_RW_WLockT -{ -public: - /** - * @brief æž„é€ å‡½æ•°ï¼Œæž„é€ æ—¶æž·é” - * - * @param lock é”对象 - */ - TC_RW_WLockT(T& lock) - : _rwLock(lock),_acquired(false) - { - _rwLock.WriteLock(); - _acquired = true; - } - /** - * @brief æžæž„æ—¶è§£é” - */ - ~TC_RW_WLockT() - { - if(_acquired) - { - _rwLock.Unlock(); - } - } -private: - /** - *é”对象 - */ - const T& _rwLock; - /** - * 是å¦å·²ç»ä¸Šé” - */ - mutable bool _acquired; - - TC_RW_WLockT(const TC_RW_WLockT&); - TC_RW_WLockT& operator=(const TC_RW_WLockT&); -}; - -}; -#endif - diff --git a/cpp/util/include/util/tc_logger.h b/cpp/util/include/util/tc_logger.h deleted file mode 100644 index 339bc6c4d..000000000 --- a/cpp/util/include/util/tc_logger.h +++ /dev/null @@ -1,1646 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __TC_LOGGER_H -#define __TC_LOGGER_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "util/tc_ex.h" -#include "util/tc_autoptr.h" -#include "util/tc_common.h" -#include "util/tc_file.h" -#include "util/tc_thread.h" -#include "util/tc_monitor.h" -#include "util/tc_thread_pool.h" -#include "util/tc_timeprovider.h" - -using namespace std; -using namespace __gnu_cxx; - -namespace tars -{ -///////////////////////////////////////////////// -/** - * @file tc_logger.h - * @brief 日志类. - * - * 说明:å¯æ‰©å±•çš„日志类,å¯ä»¥è‡ªå®šä¹‰æ»šåŠ¨ç­–略和写方法. - * - * æ供了两ç§æ–¹å¼çš„滚动方å¼: - * - * 1 按文件大å°,文件数目滚动; - * - * 2 按时间滚动(按天/å°æ—¶/分钟等) - * - * 上述两ç§é»˜è®¤çš„策略中,都是默认写入文件的,当没有åˆå§‹åŒ–,则默认写入到cout - * - * 自定义滚动方å¼çš„步骤如下: - * - * 1 实现自定义模æ¿ç±»,继承于TC_LoggerRoll,实现roll函数,该函数实现写逻辑, - * - * 例如:template - * - * class MyRoll : public TC_LoggerRoll, public TC_ThreadMutex - * - * 2 模æ¿ä¸­çš„WriteT是具体的写入逻辑,æ供缺çœçš„:TC_DefaultWriteT - * - * 3 å¯ä»¥è‡ªå®šä¹‰å®žçŽ°WriteT, 例如:实现å‘é€åˆ°ç½‘络的逻辑. - * - * 4 在MyRoll中实现内部类RollWrapperI, 并继承RollWrapperBase > - * - * 5 RollWrapperI主è¦å°±æ˜¯æä¾›MyRollçš„åˆå§‹åŒ–接å£, 这些接å£æœ€åŽä¼šç›´æŽ¥å应到TC_Logger中. - * - * 6 RollWrapperI构造时, 构造MyRoll对象(采用智能指针),并且接å£çš„æ“作直接转å‘到MyRoll对象中. - * - * 实现这些逻辑åŽ, 采用模æ¿æ–¹å¼å®šä¹‰å‡ºç±»åž‹: - * - * typedef TC_Logger MyLogger; - * - * - */ - - -/** -* @brief 日志异常类 -*/ - struct TC_Logger_Exception : public TC_Exception - { - TC_Logger_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_Logger_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_Logger_Exception() throw(){}; - }; - -/** - * @brief 写æ“作 - */ - class TC_DefaultWriteT - { - public: - void operator()(ostream &of, const deque > &ds) - { - deque >::const_iterator it = ds.begin(); - while (it != ds.end()) - { - of << it->second; - ++it; - } - of.flush(); - } - }; - - class TC_LoggerThreadGroup; - -////////////////////////////////////////////////////////////////////////////// -/** - * @brief 具体写日志基类 - */ - class TC_LoggerRoll : public TC_HandleBase - { - public: - /** - * @brief 构造函数 - */ - TC_LoggerRoll() : _pThreadGroup(NULL) - { - } - - /** - * @brief 实时记日志, 并且滚动. - * - * ä¸åŒçš„策略日志类,实现ä¸åŒçš„逻辑 - * @param buffer - */ - virtual void roll(const deque > &ds) = 0; - - /** - * @brief 安装线程. - * - * @param ltg - */ - void setupThread(TC_LoggerThreadGroup *pThreadGroup); - - /** - * @brief å–消线程. - * - */ - void unSetupThread(); - - /** - * @brief 写到日志. - * - * @param 日志内容 - */ - void write(const pair &buffer); - - /** - * @brief 刷新缓存到文件 - */ - void flush(); - - /** - * @brief 设置染色是å¦ç”Ÿæ•ˆ. - * - * @param bEnable - */ - void enableDyeing(bool bEnable, const string& sKey) - { - TC_LockT lock(_mutexDyeing); - - if (bEnable) - { - _mapThreadID.insert(std::make_pair(pthread_self(), sKey)); - //_setThreadID.insert(pthread_self()); - } - else - { - _mapThreadID.erase(pthread_self()); - //_setThreadID.erase(pthread_self()); - } - - _bDyeingFlag = (_mapThreadID.size() > 0); - //_bDyeingFlag = (_setThreadID.size() > 0); - } - - protected: - - /** - * buffer - */ - TC_ThreadQueue > _buffer; - - /** - * é” - */ - TC_ThreadMutex _mutex; - - /** - * 线程组 - */ - TC_LoggerThreadGroup *_pThreadGroup; - - /** - * 是å¦å·²ç»æŸ“色的标志 - */ - static bool _bDyeingFlag; - - /** - * æŸ“è‰²ç”¨é” - * - */ - static TC_ThreadMutex _mutexDyeing; - - /** - * 染色的线程IDé›†åˆ - * - */ - static hash_map _mapThreadID; - }; - - typedef TC_AutoPtr TC_LoggerRollPtr; - -////////////////////////////////////////////////////////////////////////////// -// -/** - * @brief 写日志线程组. - * - * 关键点:注册日志åŽ,会ä¿å­˜èŒèƒ½æŒ‡é’ˆ,ä¿è¯æ—¥å¿—对象一直存在 - */ - class TC_LoggerThreadGroup : public TC_ThreadLock - { - public: - /** - * @brief 构造函数 - */ - TC_LoggerThreadGroup(); - - /** - * @brief æžå¤Ÿå‡½æ•° - */ - ~TC_LoggerThreadGroup(); - - /** - * @brief å¯åŠ¨çº¿ç¨‹. - * - * @param iThreadNum线程数 - */ - void start(size_t iThreadNum); - - /** - * @brief 注册logger对象. - * - * @param l TC_LoggerRollPtr对象 - */ - void registerLogger(TC_LoggerRollPtr &l); - - /** - * @brief å¸è½½logger对象. - * - * @param l TC_LoggerRollPtr对象 - */ - void unRegisterLogger(TC_LoggerRollPtr &l); - - /** - * @brief åˆ·æ–°æ‰€æœ‰çš„æ•°æ® - */ - void flush(); - - protected: - - /** - * @brief 写日志 - */ - void run(); - - /** - * @brief 指针比较 - */ - struct KeyComp - { - bool operator()(const TC_LoggerRollPtr& p1, const TC_LoggerRollPtr& p2) const - { - return p1.get() < p2.get(); - } - }; - - typedef set logger_set; - - protected: - - /** - * ç»“æŸ - */ - bool _bTerminate; - - /** - * 写线程 - */ - TC_ThreadPool _tpool; - - /** - * logger对象 - */ - logger_set _logger; - - }; - -/** - * @brief 自定义logger buffer - */ - class LoggerBuffer : public std::basic_streambuf - { - public: - /** - * @brief 定义最大的buffer大å°(10M) - */ - enum - { - MAX_BUFFER_LENGTH = 1024*1024*10, - }; - - /** - * @brief 构造函数 - */ - LoggerBuffer(); - - /** - * @brief 构造函数. - * - * @param roll TC_LoggerRollPtr对象 - * @param buffer_len bufferå¤§å° - */ - LoggerBuffer(TC_LoggerRollPtr roll, size_t buffer_len); - - /** - * @brief æžæž„函数 - */ - ~LoggerBuffer(); - - protected: - - /** - * @brief 分é…空间. - * - * @param n - */ - void reserve(std::streamsize n); - - /** - * @brief 放数æ®. - * - * @param s - * @param n - * - * @return streamsize - */ - virtual streamsize xsputn(const char_type* s, streamsize n); - - /** - * @brief buffer满了, 具体写数æ®. - * - * @param c - * @return int_type - */ - virtual int_type overflow(int_type c); - - /** - * @brief 读空间empty了(ä¸å®žçŽ°). - * - * @return int_type - */ - virtual int_type underflow() { return std::char_traits::eof();} - - /** - * @brief 具体写逻辑. - * - * @return int - */ - virtual int sync(); - - protected: - LoggerBuffer(const LoggerBuffer&); - LoggerBuffer& operator=(const LoggerBuffer&); - - protected: - /** - * @brief 写日志 - */ - TC_LoggerRollPtr _roll; - - /** - * 缓冲区 - */ - char* _buffer; - - /** - * ç¼“å†²åŒºå¤§å° - */ - std::streamsize _buffer_len; - }; - -/** - * @brief 临时类, æžå¤Ÿçš„时候写日志 - */ - class LoggerStream - { - public: - /** - * @brief 构造. - * - * @param stream - * @param mutex - */ - LoggerStream(const char *header, ostream *stream, ostream *estream, TC_ThreadRecMutex &mutex) : _stream(stream), _estream(estream), _mutex(mutex) - { - _buffer << header; - } - - /** - * @brief æžæž„ - */ - ~LoggerStream() - { - if (_stream) - { - TC_LockT lock(_mutex); - _stream->clear(); - (*_stream) << _buffer.str(); - - _stream->flush(); - } - } - - /** - * @brief é‡è½½<< - */ - template - LoggerStream& operator << (const P &t) { if (_stream) _buffer << t;return *this;} - - /** - * @brief endl,flush等函数 - */ - typedef ostream& (*F)(ostream& os); - LoggerStream& operator << (F f) { if (_stream) (f)(_buffer);return *this;} - - /** - * @brief hex等系列函数 - */ - typedef ios_base& (*I)(ios_base& os); - LoggerStream& operator << (I f) { if (_stream) (f)(_buffer);return *this;} - - /** - * @brief 字段转æ¢æˆostream类型. - * - * @return ostream& - */ - operator ostream&() - { - if (_stream) - { - return _buffer; - } - - return *_estream; - } - - //ä¸å®žçŽ° - LoggerStream(const LoggerStream& lt); - LoggerStream& operator=(const LoggerStream& lt); - - protected: - - /** - * 缓冲区 - */ - std::stringstream _buffer; - - /** - * è¾“å‡ºæµ - */ - std::ostream *_stream; - - /** - * - */ - std::ostream *_estream; - - /** - * é” - */ - TC_ThreadRecMutex &_mutex; - }; - -/** - * @brief 日志基类 - */ - template class RollPolicy> - class TC_Logger : public RollPolicy::RollWrapperI - { - public: - - /** - * @brief 设置显示标题 - */ - enum - { - HAS_TIME = 0x01, /**秒级别时间*/ - HAS_PID = 0x02, /**进程ID*/ - HAS_LEVEL = 0x04, /**日志等级*/ - HAS_MTIME = 0x08 /**毫秒级别时间(ä¸å»ºè®®ä½¿ç”¨, 会影å“性能)*/ - }; - - /** - * @brief 枚举类型,定义日志的四ç§ç­‰çº§ . - */ - enum - { - NONE_LOG = 1, /**所有的log都ä¸å†™*/ - ERROR_LOG = 2, /**写错误log*/ - WARN_LOG = 3, /**写错误,警告log*/ - DEBUG_LOG = 4, /**写错误,警告,调试log*/ - INFO_LOG = 5 /**写错误,警告,调试,Info log*/ - }; - - /** - * @brief 日志级别å称 - */ - static const string LN[6]; - - /** - * @brief 构造函数 - */ - TC_Logger() - : _flag(HAS_TIME) - , _level(DEBUG_LOG) - , _buffer(TC_LoggerRollPtr::dynamicCast(this->_roll), 1024) - , _stream(&_buffer) - , _ebuffer(NULL, 0) - , _estream(&_ebuffer) - ,_sSepar("|") - ,_bHasSquareBracket(false) - { - } - - /** - * @brief æžå¤Ÿå‡½æ•° - */ - ~TC_Logger() - { - } - - /** - * @brief 修改标题. - * - * @param flag - * @param add - */ - void modFlag(int flag, bool add = true) - { - if (add) - { - _flag |= flag; - } - else - { - _flag &= ~flag; - } - } - - /** - * @brief 是å¦æœ‰æŸæ ‡ç¤º. - * - * @return bool - */ - bool hasFlag(int flag) const { return _flag & flag;} - - /** - * @brief 获å–flag - * @return flag的值 - */ - int getFlag() const { return _flag;} - - /** - * @brief 获å–日志等级. - * - * @return int 等级 - */ - int getLogLevel() const { return _level;} - - /** - * @brief 设置日志等级. - * - * @param level 等级 - * @return æˆåŠŸè®¾ç½®è¿”回0,å¦åˆ™è¿”回-1 - */ - int setLogLevel(int level) - { - if (!isLogLevelValid(level)) - { - return -1; - } - - _level = level; - return 0; - } - - /** - * @brief 设置等级. - * - * @param level - * @param int - */ - int setLogLevel(const string &level) - { - if (level == "ERROR") - { - return setLogLevel(ERROR_LOG); - } - else if (level == "WARN") - { - return setLogLevel(WARN_LOG); - } - else if (level == "DEBUG") - { - return setLogLevel(DEBUG_LOG); - } - else if (level == "NONE") - { - return setLogLevel(NONE_LOG); - } - else if (level == "INFO") - { - return setLogLevel(INFO_LOG); - } - return -1; - } - - bool IsNeedLog(const string &level) - { - if (level == "ERROR") - { - return IsNeedLog(ERROR_LOG); - } - else if (level == "WARN") - { - return IsNeedLog(WARN_LOG); - } - else if (level == "DEBUG") - { - return IsNeedLog(DEBUG_LOG); - } - else if (level == "NONE") - { - return IsNeedLog(NONE_LOG); - } - else if (level == "INFO") - { - return IsNeedLog(INFO_LOG); - } - else - { - return true; - } - } - bool IsNeedLog(int level) - { - return level <= _level; - } - /** - * @brief 框架中增加的日志内容之间的分割符,默认是"|" - * @param str - */ - void setSeparator(const string& str) {_sSepar = str;} - - /** - * @brief 框架中日期和时间之间是å¦éœ€è¦åŠ ä¸­æ‹¬å·[],有些统计有特殊需求;默认ä¸åŠ  - * @param bEnable - */ - void enableSqareWrapper(bool bEnable) {_bHasSquareBracket = bEnable;} - - /** - * @brief DEBUG记日志 - */ - LoggerStream info() { return stream(INFO_LOG);} - - /** - * @brief DEBUG记日志 - */ - LoggerStream debug() { return stream(DEBUG_LOG);} - - /** - * @brief WARNING记日志 - */ - LoggerStream warn() { return stream(WARN_LOG);} - - /** - * @brief ERROR记日志 - */ - LoggerStream error() { return stream(ERROR_LOG);} - - /** - * @brief 记所有日志, 与等级无关 - */ - LoggerStream any() { return stream(0);} - - LoggerStream log(int level) { return stream(level);} - protected: - - /** - * @brief 获å–头部信æ¯. - * - * @param c - * @param len - * @param level - */ - void head(char *c, int len, int level) - { - size_t n = 0; - - if (hasFlag(TC_Logger::HAS_MTIME)) - { - struct timeval t; - TC_TimeProvider::getInstance()->getNow(&t); - //gettimeofday(&t, NULL); - - tm tt; - localtime_r(&t.tv_sec, &tt); - const char *szFormat = (_bHasSquareBracket)?("[%04d-%02d-%02d %02d:%02d:%02d.%03ld]%s"):("%04d-%02d-%02d %02d:%02d:%02d.%03ld%s"); - n += snprintf(c + n, len-n, szFormat, - tt.tm_year + 1900, tt.tm_mon+1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, t.tv_usec/1000,_sSepar.c_str()); - } - else if (hasFlag(TC_Logger::HAS_TIME)) - { - time_t t = TNOW; - tm tt; - localtime_r(&t, &tt); - const char *szFormat = (_bHasSquareBracket)?("[%04d-%02d-%02d %02d:%02d:%02d]%s"):("%04d-%02d-%02d %02d:%02d:%02d%s"); - n += snprintf(c + n, len-n, szFormat, - tt.tm_year + 1900, tt.tm_mon+1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec,_sSepar.c_str()); - } - - if (hasFlag(TC_Logger::HAS_PID)) - { - n += snprintf(c + n, len - n, "%ld%s", syscall(SYS_gettid), _sSepar.c_str()); - } - - if (hasFlag(TC_Logger::HAS_LEVEL)) - { - n += snprintf(c + n, len - n, "%s%s", TC_Logger::LN[level].c_str(), _sSepar.c_str()); - } - } - - /** - * @brief æµ. - * - * @param level - * @return LoggerStream - */ - LoggerStream stream(int level) - { - ostream *ost = NULL; - - if (level <= _level) - { - char c[128] = "\0"; - head(c, sizeof(c) - 1, level); - - ost = &_stream; - - return LoggerStream(c, ost, &_estream, _mutex); - } - - return LoggerStream(NULL, ost, &_estream, _mutex); - } - - /** - * @brief 进程等级是å¦æœ‰æ•ˆ. - * - * @param level : 进程等级 - * @return bool,等级是å¦æœ‰æ•ˆ - */ - bool isLogLevelValid(int level) - { - switch (level) - { - case NONE_LOG: - case ERROR_LOG: - case WARN_LOG: - case DEBUG_LOG: - case INFO_LOG: - return true; - break; - } - - return false; - } - - protected: - /** - * 显示头 - */ - int _flag; - - /** - * 日志最高等级 - */ - int _level; - - /** - * buffer - */ - LoggerBuffer _buffer; - - /** - * loggerä¸´æ—¶æµ - */ - std::ostream _stream; - - /** - * 空buffer - */ - LoggerBuffer _ebuffer; - - /** - * ç©ºæµ - */ - std::ostream _estream; - - /** - * é” - */ - TC_ThreadRecMutex _mutex; - /** - * 分隔符 - */ - string _sSepar; - /** - * 日期部分是å¦åŠ ä¸Š[] - */ - bool _bHasSquareBracket; - - }; - - template class RollPolicy> - const string TC_Logger::LN[6] = {"ANY", "NONE_LOG", "ERROR", "WARN", "DEBUG", "INFO"}; - -//////////////////////////////////////////////////////////////////////////////// - - class RollWrapperInterface - { - public: - - virtual ~RollWrapperInterface() {} - - /** - * @brief DEBUG记日志 - */ - virtual LoggerStream info() = 0; - - /** - * @brief DEBUG记日志 - */ - virtual LoggerStream debug() = 0; - - /** - * @brief WARNING记日志 - */ - virtual LoggerStream warn() = 0; - - /** - * @brief ERROR记日志 - */ - virtual LoggerStream error() = 0; - - /** - * @brief 记所有日志, 与等级无关 - */ - virtual LoggerStream any() = 0; - - /** - *@brief 按照等级æ¥è¾“出日志 - */ - virtual LoggerStream log(int level)=0; - /** - * @brief 如果是异步调用,则马上进行刷新 - */ - virtual void flush() = 0; - }; - - - template - class RollWrapperBase : public RollWrapperInterface - { - public: - - typedef TC_AutoPtr RollPolicyWriteTPtr; - - /** - * @brief 构造 - */ - RollWrapperBase() - { - _roll = new RollPolicyWriteT; - } - - /** - * @brief 安装线程. - * - * @param ltg - */ - void setupThread(TC_LoggerThreadGroup *ltg) { _roll->setupThread(ltg);} - - /** - * @brief å–消线程 - */ - void unSetupThread() { _roll->unSetupThread();} - - /** - * @brief 获å–写对象. - * - * @return WriteT& - */ - typename RollPolicyWriteT::T &getWriteT() { return _roll->getWriteT();} - - /** - * @brief 写日志. - * - * @param buffer - */ - void roll(const pair &buffer) { _roll->write(buffer);} - - /** - * @brief 获å–roll实例. - * - * @return RollPolicyWriteTPtr& - */ - RollPolicyWriteTPtr & getRoll() {return _roll;} - - /** - * @brief 异步刷新 - */ - void flush() { _roll->flush(); } - - protected: - - /** - * @brief 具体写日志æ“作类 - */ - RollPolicyWriteTPtr _roll; - - }; - -//////////////////////////////////////////////////////////////////////////////// -/** - * @brief 日志滚动方法, æ ¹æ®æ—¥å¿—大å°æ»šåŠ¨ - */ - template - class TC_RollBySize : public TC_LoggerRoll, public TC_ThreadMutex - { - public: - typedef WriteT T; - - /** - * @brief å°è£…ç±»(æ供接å£) - */ - class RollWrapperI : public RollWrapperBase > - { - public: - /** - * @brief åˆå§‹åŒ–. - * - * @param path - * @param iMaxSize, 字节 - * @param iMaxNum - */ - void init(const string &path, int iMaxSize = 5000000, int iMaxNum = 10) - { - this->_roll->init(path, iMaxSize, iMaxNum); - } - - /** - * @brief 获å–日志路径. - * - * @return string - */ - string getPath() { return this->_roll->getPath();} - - /** - * @brief 设置文件路径 - */ - void setPath(const string &path) { this->_roll->setPath(path);} - - /** - * @brief 获å–最大大å°. - * - * @return int - */ - int getMaxSize() { return this->_roll->getMaxSize();} - - /** - * @brief 设置最大大å°. - * - * @param maxSize - */ - void setMaxSize(int maxSize) { this->_roll->setMaxSize(maxSize);} - - /** - * @brief 获å–最大个数. - * - * @return int - */ - int getMaxNum() { return this->_roll->getMaxNum();} - - /** - * @brief 设置文件个数. - * - * @param maxNum - */ - void setMaxNum(int maxNum) { this->_roll->setMaxNum(maxNum);} - }; - - /** - * @brief 构造函数 - */ - TC_RollBySize() : _maxSize(5000000),_maxNum(10),_iUpdateCount(0), _lt(time(NULL)) - { - } - - /** - * @brief æžå¤Ÿ - */ - ~TC_RollBySize() - { - if (_of.is_open()) - { - _of.close(); - } - } - - /** - * @brief åˆå§‹åŒ–. - * - * @param path, 文件å - * @param iMaxSize, æœ€å¤§å¤§å° - * @param iMaxNum, 最大个数 - */ - void init(const string &path, int iMaxSize = 5000000, int iMaxNum = 10) - { - TC_LockT lock(*this); - - _path = path; - _maxSize= iMaxSize; - _maxNum = iMaxNum; - } - - /** - * @brief 获å–日志路径. - * - * @return string - */ - string getPath() { TC_LockT lock(*this); return _path;} - - /** - * @brief 设置路径. - */ - void setPath(const string &path) { TC_LockT lock(*this); _path = path;} - - /** - * @brief 获å–最大大å°. - * - * @return int - */ - int getMaxSize() { TC_LockT lock(*this); return _maxSize;} - - /** - * @brief 设置最大大å°. - * - * @param maxSize - * @return void - */ - void setMaxSize(int maxSize) { TC_LockT lock(*this); return _maxSize = maxSize;} - - /** - * @brief 获å–最大个数. - * - * @return int - */ - int getMaxNum() { TC_LockT lock(*this); return _maxNum;} - - /** - * @brief 设置最大个数. - * - * @param maxNum - */ - void setMaxNum(int maxNum) { TC_LockT lock(*this); return _maxNum = maxNum;} - - /** - * @brief 获å–写示例. - * - * @return T& - */ - WriteT &getWriteT() { return _t;} - - /** - * 函数对象 - * - * @param string - */ - void roll(const deque > &buffer) - { - TC_LockT lock(*this); - - if (_path.empty()) - { - _t(cout, buffer); - return; - } - - time_t t = TNOW; - time_t tt= t - _lt; - //æ¯éš”5, é‡æ–°æ‰“开一次文件, é¿å…文件被删除åŽå¥æŸ„ä¸é‡Šæ”¾ - if (tt > 5 || tt < 0) - { - _lt = t; - _of.close(); - } - - //检查日志文件是å¦æ‰“å¼€ - if (!_of.is_open()) - { - string sLogFileName = _path + ".log"; - _of.open(sLogFileName.c_str(), ios::app); - - string sLogFilePath = TC_File::extractFilePath(_path); - - if (!TC_File::isFileExist(sLogFilePath,S_IFDIR)) - { - TC_File::makeDirRecursive(sLogFilePath); - } - - if (!_of) - { - //抛异常å‰ç»§ç»­è¿›å…¥_t 以便打远程日志 - _t(_of, buffer); - - throw TC_Logger_Exception("[TC_RollBySize::roll]:fopen fail: " + sLogFileName, errno); - } - - } - - _t(_of, buffer); - - if (tt <= 5) - { - return; - } - - //文件大å°å°äºŽé™åˆ¶, 直接返回 - if (_of.tellp() < _maxSize) - { - return; - } - - //文件大å°è¶…出é™åˆ¶,删除最åŽä¸€ä¸ªæ–‡ä»¶ - string sLogFileName = _path + TC_Common::tostr(_maxNum - 1) + ".log"; - if (access(sLogFileName.c_str(), F_OK) == 0) - { - if (remove(sLogFileName.c_str()) < 0 ) - { - return; - } - } - - //å°†log文件命åshift, xxx1.log=>xxx2.log,第一文件还是为xxx.log - for (int i = _maxNum-2; i >= 0; i--) - { - if (i == 0) - { - sLogFileName = _path + ".log"; - } - else - { - sLogFileName = _path + TC_Common::tostr(i) + ".log"; - } - - if (access(sLogFileName.c_str(), F_OK) == 0) - { - string sNewLogFileName = _path + TC_Common::tostr(i + 1) + ".log"; - rename(sLogFileName.c_str(), sNewLogFileName.c_str()); - } - } - - _of.close(); - _of.open(sLogFileName.c_str(), ios::app); - if (!_of) - { - throw TC_Logger_Exception("[TC_RollBySize::roll]:fopen fail: " + sLogFileName, errno); - } - } - - protected: - - /** - * 文件路径 - */ - string _path; - - /** - * æ—¥å¿—æ–‡ä»¶çš„æœ€å¤§å¤§å° - */ - int _maxSize; - - /** - * log文件最大数log文件命å为:xxx.log xxx1.log ... - * xxx[_maxNum-1].log,_maxNum<=1 ?xxx.log - */ - int _maxNum; - - /** - * 日志文件 - */ - ofstream _of; - - /** - * 具体写æ“作 - */ - WriteT _t; - - /** - * å¤šé•¿æ—¶é—´æ£€æŸ¥ä¸€æ¬¡æ–‡ä»¶å¤§å° - */ - short _iUpdateCount; - - /** - * å¤šé•¿æ—¶é—´æ£€æŸ¥ä¸€æ¬¡æ–‡ä»¶å¤§å° - */ - time_t _lt; - }; - - typedef TC_Logger TC_RollLogger; - - /** - * @brief æ ¹æ®æ—¶é—´æ»šåŠ¨æ—¥å¿—分隔类型 - */ - class TarsLogType:public TC_HandleBase - { - public: - TarsLogType():_next_time_t(TNOW),_format("%Y%m%d"),_next_cut_time(TC_Common::tm2str(TNOW, "%Y%m%d")),_frequency(1),_des("day"){} - virtual ~TarsLogType(){} - //频率值 - virtual size_t frequence()=0; - - virtual std::string get_init_time() - { - time_t tmp = _next_time_t - frequence(); - return TC_Common::tm2str(tmp, _format); - } - - //返回空表示没有到下一个记录点 - virtual std::string get_new_time(time_t t) - { - if(t >= _next_time_t && TC_Common::tm2str(t,_format) >= _next_cut_time) - { - //时间规整 - time_t new_nt = _next_time_t + ((t - _next_time_t) / frequence()) * frequence(); - _next_time_t = new_nt + frequence(); - _next_cut_time = TC_Common::tm2str(_next_time_t,_format); - return TC_Common::tm2str(new_nt,_format); - } - else - { - return ""; - } - } - - //转æ¢æˆç›¸åº”的字符串形å¼:1day,1hour,2minute - inline std::string& toString() - { - return _des; - } - protected: - //计算下一个时间点 - void init(const string &format = "%Y%m%d",size_t frequency=1) - { - _frequency = (frequency == 0?1:frequency); - _format = format; - - _next_time_t = TNOW / frequence() + 1; - _next_time_t *= frequence(); - - _next_cut_time = TC_Common::tm2str(_next_time_t, _format); - } - protected: - time_t _next_time_t; - string _format; - string _next_cut_time; - size_t _frequency; - string _des; - }; - typedef TC_AutoPtr TarsLogTypePtr; - - class TarsLogByDay:public TarsLogType - { - public: - static const string FORMAT; - public: - explicit TarsLogByDay(const string &format = "%Y%m%d",size_t frequency=1) - { - init(format,frequency); - _des = TC_Common::tostr(_frequency) + "day"; - } - size_t frequence() - { - const size_t DAY_IN_SECOND = 60 * 60 * 24; - return _frequency * DAY_IN_SECOND; - } - }; - - class TarsLogByHour:public TarsLogType - { - public: - static const string FORMAT; - public: - explicit TarsLogByHour(const string &format = "%Y%m%d%H",size_t frequency=1) - { - init(format,frequency); - _des = TC_Common::tostr(_frequency) + "hour"; - } - - size_t frequence() - { - const size_t HOUR_IN_SECOND = 60 * 60; - return _frequency * HOUR_IN_SECOND; - } - }; - - - class TarsLogByMinute:public TarsLogType - { - public: - static const string FORMAT; - public: - explicit TarsLogByMinute(const string &format = "%Y%m%d%H%M",size_t frequency=1) - { - init(format,frequency); - _des = TC_Common::tostr(_frequency) + "minute"; - } - size_t frequence() - { - const size_t MINUTE_IN_SECOND = 60; - return _frequency * MINUTE_IN_SECOND; - } - }; - - - /** - * @brief æ ¹æ®æ—¶é—´æ»šåŠ¨æ—¥å¿— - */ - template - class TC_RollByTime : public TC_LoggerRoll, public TC_ThreadMutex - { - public: - - typedef WriteT T; - - /** - * @brief å°è£…ç±»(接å£ç±») - */ - class RollWrapperI : public RollWrapperBase > - { - public: - - /** - * @brief åˆå§‹åŒ–. - * - * @param path,日志路径 - * @param format,日志文件记录格å¼ï¼ŒæŒ‰å¤©ï¼Œå°æ—¶ï¼Œåˆ†é’Ÿ - * @param bHasSufix,日志文件是å¦æ·»åŠ ".log"åŽç¼€ - * @param sConcatstr,日志路径和时间字串之间的连接符,例如:app_log/test_20121210.log - * @param logTypePtr,日志记录类型,详è§TarsLogType - * @param bIsRemote,是å¦æ˜¯è¿œç¨‹æ—¥å¿—实例 - */ - void init(const string &path, const string &format = "%Y%m%d",bool bHasSufix=true,const string&sConcatstr="_",const TarsLogTypePtr& logTypePtr=NULL,bool bIsRemote=false) - { - this->_roll->init(path, format,bHasSufix,sConcatstr,logTypePtr,bIsRemote); - } - - /** - * @brief 获å–日志路径. - * - * @return string - */ - string getPath() { return this->_roll->getPath();} - - /** - * @brief 设置文件路径 - */ - void setPath(const string &path) { this->_roll->setPath(path);} - - /** - * @brief 获å–æ ¼å¼. - * - * @return string - */ - string getFormat() { return this->_roll->getFormat();} - - /** - * @brief 设置格å¼. - * - * @param format,支æŒæŒ‰å¤©ï¼ŒæŒ‰å°æ—¶ï¼ŒæŒ‰åˆ†é’Ÿæ ¼å¼ - */ - void setFormat(const string &format) { this->_roll->setFormat(format);} - - void setRemote(bool en) { this->_roll->setRemote(en);} - }; - - /** - * @brief 构造 - */ - TC_RollByTime() : _lt(TNOW),_logTypePtr(NULL),_bRemoteType(false) - { - _bHasSufix = true; - _sConcatStr = "_"; - } - - /** - * @brief æžå¤Ÿ - */ - ~TC_RollByTime() - { - if (_of.is_open()) - { - _of.close(); - } - } - - /** - * @brief åˆå§‹åŒ–. - * - * @param path, 文件路径 - * @param format, æ ¼å¼ - * @param bHasSufix - * @param sConcatstr - * @param logTypePtr - * @param bIsRemote - */ - - void init(const string &path, const string &format = "%Y%m%d",bool bHasSufix=true,const string &sConcatstr="_",const TarsLogTypePtr& logTypePtr=NULL,bool bIsRemote=false) - { - TC_LockT lock(*this); - - _logTypePtr = logTypePtr; - _path = path; - _format = format; - _bHasSufix = bHasSufix; - _sConcatStr = sConcatstr; - _bRemoteType = bIsRemote; - - if(_logTypePtr) - { - _currentTime = logTypePtr->get_init_time(); - } - else - { - _currentTime = TC_Common::tm2str(TNOW, _format); - } - } - - /** - * @brief 获å–日志路径. - * - * @return string - */ - string getPath() { TC_LockT lock(*this); return _path;} - - /** - * @brief 设置文件路径 - */ - void setPath(const string &path) { TC_LockT lock(*this); _path = path;} - - /** - * @brief 获å–æ ¼å¼. - * - * @return string - */ - string getFormat() { TC_LockT lock(*this); return _format;} - - /** - * @brief 设置格å¼. - * - * @param format,支æŒæŒ‰å¤©ï¼ŒæŒ‰å°æ—¶ï¼ŒæŒ‰åˆ†é’Ÿæ ¼å¼ - */ - void setFormat(const string &format){ TC_LockT lock(*this); _format = format;} - - /** - * - * 如果是关闭了本地日志,则ä¸ç”Ÿæˆæ–‡ä»¶ - * 如果是远程日志实例,本地ä¸ç”Ÿæˆæ–‡ä»¶ - */ - void setRemote(bool en) { TC_LockT lock(*this); _bRemoteType = en;} - /** - * @brief 获å–写示例. - * - * @return WriteT& - */ - WriteT &getWriteT() { return _t;} - - /** - * @brief 函数对象. - * - * @param path - * @param of - * @return string - */ - void roll(const deque > &buffer) - { - TC_LockT lock(*this); - - if (_path.empty()) - { - _t(cout, buffer); - return; - } - //远程日志在本地ä¸ç”¨æ‰“开文件 - if(_bRemoteType) - { - _t(_of, buffer); - return; - } - - time_t t = TNOW; - - string nowTime = ""; - //如果设置了记录类型,则使用记录类型æ¥è®¡ç®—时间 - if(_logTypePtr) - { - nowTime = _logTypePtr->get_new_time(t); - //时间跨越了 - if(nowTime != "") - { - _currentTime = nowTime; - _of.close(); - } - else - { - nowTime = _currentTime; - } - } - else - { - nowTime = TC_Common::tm2str(t, _format); - //检查时间是å¦è·¨æ—¶é—´äº†, 跨时间åŽå…³é—­æ–‡ä»¶ - if (_currentTime != nowTime) - { - _currentTime = nowTime; - _of.close(); - } - } - - //æ¯éš”10s, é‡æ–°æ‰“开一次文件, é¿å…文件被删除åŽå¥æŸ„ä¸é‡Šæ”¾ - if (t - _lt > 10 || t - _lt < 0) - { - _lt = t; - _of.close(); - } - - if (!_of.is_open()) - { - string sLogFileName = _path + _sConcatStr + nowTime + (_bHasSufix?".log":""); - string sLogFilePath = TC_File::extractFilePath(_path); - if (!TC_File::isFileExist(sLogFilePath,S_IFDIR)) - { - TC_File::makeDirRecursive(sLogFilePath); - } - - _of.open(sLogFileName.c_str(), ios::app); - - if (!_of) - { - //抛异常å‰ç»§ç»­è¿›å…¥_t 以便打远程日志 - _t(_of, buffer); - throw TC_Logger_Exception("[TC_RollByTime::roll]:fopen fail: " + sLogFileName, errno); - } - } - - //写 - _t(_of, buffer); - } - - protected: - /** - * 文件路径 - */ - string _path; - - /** - * æ—¶é—´æ ¼å¼ - */ - string _format; - - /** - * 上次roll时的时间 - */ - string _currentTime; - - /** - * 日志文件 - */ - ofstream _of; - - /** - * 具体写æ“作 - */ - WriteT _t; - - /** - * å¤šé•¿æ—¶é—´æ£€æŸ¥ä¸€æ¬¡æ–‡ä»¶å¤§å° - */ - time_t _lt; - /** - * 日志文件å是å¦å¸¦.logåŽç¼€ - */ - bool _bHasSufix; - /** - * 日志文件å中用户自定义字符与日期字符间的连接符,默认是"_" - */ - string _sConcatStr; - - - /** - * 按天/å°æ—¶/分钟输出日志时的记录类型 - */ - - TarsLogTypePtr _logTypePtr; - /** - * 是å¦æ˜¯è¿œç¨‹æ—¥å¿—实例 - */ - bool _bRemoteType; - }; - - typedef TC_Logger TC_DayLogger; - -} - -#endif - diff --git a/cpp/util/include/util/tc_loki.h b/cpp/util/include/util/tc_loki.h deleted file mode 100644 index 5440aa391..000000000 --- a/cpp/util/include/util/tc_loki.h +++ /dev/null @@ -1,637 +0,0 @@ -#ifndef __TC_TYPETRAITS_H -#define __TC_TYPETRAITS_H - -#include - -namespace tars -{ -///////////////////////////////////////////////// -// 说明: loki -///////////////////////////////////////////////// - -namespace TL -{ - //åªå£°æ˜Ž, ä¸å®šä¹‰çš„ç±», 作为TYPELIST的末端类型 - class NullType; - - //空类型 - struct EmptyType { }; - - /** - * 数值到类型的映射 - */ - template - struct Int2Type - { - enum { value = v }; - }; - - /** - * 类型到类型的映射 - */ - template - struct Type2Type - { - typedef T OriginalType; - }; - - /////////////////////////////////////////////////////////////////////////// - // 以下是TypeList的定义(ç›®å‰åªæ”¯æŒ10个å‚æ•°) - /** - * 定义类型链表 - */ - template - struct TypeList - { - typedef Head H; - typedef Tail T; - }; - - #define TYPELIST_1(T1) TypeList - #define TYPELIST_2(T1, T2) TypeList - #define TYPELIST_3(T1, T2, T3) TypeList - #define TYPELIST_4(T1, T2, T3, T4) TypeList - #define TYPELIST_5(T1, T2, T3, T4, T5) TypeList - #define TYPELIST_6(T1, T2, T3, T4, T5, T6) TypeList - #define TYPELIST_7(T1, T2, T3, T4, T5, T6, T7) TypeList - #define TYPELIST_8(T1, T2, T3, T4, T5, T6, T7, T8) TypeList - #define TYPELIST_9(T1, T2, T3, T4, T5, T6, T7, T8, T9) TypeList - #define TYPELIST_10(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) TypeList - #define TYPELIST_11(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) TypeList - #define TYPELIST_12(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) TypeList - #define TYPELIST_13(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) TypeList - #define TYPELIST_14(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) TypeList - #define TYPELIST_15(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) TypeList - #define TYPELIST_16(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) TypeList - #define TYPELIST_17(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) TypeList - #define TYPELIST_18(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) TypeList - #define TYPELIST_19(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) TypeList - #define TYPELIST_20(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) TypeList - - - ////////////////////////////////////////////////////////////////////////////// - // 以下定义TypeList的编译期的æ“作函数(通过å特化实现) - /** - * Length: å–TypeList的长度 - */ - template struct Length; - template<> struct Length - { - enum { value = 0 }; - }; - template struct Length > - { - enum { value = 1 + Length::value }; - }; - - /** - * TypeAt, å–链表在iä½ç½®ä¸Šçš„类型 - */ - template struct TypeAt; - template struct TypeAt, 0> - { - typedef Head Result; - }; - template struct TypeAt, i> - { - typedef typename TypeAt::Result Result; - }; - - /** - * TypeAt, å–链表在iä½ç½®ä¸Šçš„类型, i超出了返回, 则返回DefaultType - */ - template struct TypeAtNonStrict - { - typedef DefaultType Result; - }; - template struct TypeAtNonStrict, 0, DefaultType> - { - typedef Head Result; - }; - template struct TypeAtNonStrict, i, DefaultType> - { - typedef typename TypeAtNonStrict::Result Result; - }; - - /** - * å–链表上类型为Tçš„åºå·, 没有则返回-1 - */ - template struct IndexOf; - template struct IndexOf - { - enum { value = -1 }; - }; - template struct IndexOf, T> - { - enum { value = 0 }; - }; - template struct IndexOf, T> - { - private: - enum { temp = IndexOf::value }; - public: - enum { value = temp == -1 ? -1 : 1 + temp }; - }; - - /** - * Append, 添加到链表尾部 - */ - template struct Append; - template<> struct Append - { - typedef NullType Result; - }; - template struct Append - { - typedef TYPELIST_1(T) Result; - }; - template struct Append > - { - typedef TypeList Result; - }; - template struct Append, T> - { - typedef TypeList::Result> Result; - }; - - /** - * Erase 删除 - */ - template struct Erase; - template struct Erase - { - typedef NullType Result; - }; - template struct Erase, T> - { - typedef Tail Result; - }; - template struct Erase, T> - { - typedef TypeList::Result> Result; - }; - - /** - * EraseAll 删除 - */ - template struct EraseAll; - template struct EraseAll - { - typedef NullType Result; - }; - template struct EraseAll, T> - { - typedef typename EraseAll::Result Result; - }; - template struct EraseAll, T> - { - typedef TypeList::Result> Result; - }; - - /** - * 生æˆTypeList类型 - */ - template - struct TLMaker - { - private: - typedef TYPELIST_20(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, - T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) TmpList; - public: - typedef typename EraseAll::Result Result; - }; - - ///////////////////////////////////////////////////////////////////////////////////// - //判断类型T是å¦å¯ä»¥è½¬æ¢æˆç±»åž‹U(å‚考了wbl库, 直接采用loki, 编译时会有警告 - //关键:如果能够转æ¢, 则接收U的函数, 也能够接收T - template - class Conversion - { - protected: - typedef char YES; - struct NO {char dummy[2];}; - struct any_conversion - { - template any_conversion(const volatile P&); - template any_conversion(P&); - }; - - template struct conversion_checker - { - static NO _m_check(any_conversion ...); - static YES _m_check(P, int); - }; - - static T _m_from; - public: - enum - { - //是å¦å¯ä»¥è½¬æ¢(如果Test(makeT())匹é…到了static Small Test(U), 则å¯ä»¥è½¬æ¢) - exists = (sizeof(conversion_checker::_m_check(_m_from, 0)) == sizeof(YES)), - //是å¦å¯ä»¥åŒå‘è½¬æ¢ - exists2Way = exists && Conversion::exists, - //是å¦ç›¸åŒç±»åž‹ - sameType = false - }; - }; - - //å特化æ¥ç¡®å®šsameType - template - class Conversion - { - public: - enum - { - exists = true, - exists2Way = true, - sameType = true - }; - }; - - //判断两个类是å¦å¯ä»¥ç»§æ‰¿ - //关键:å­ç±»æŒ‡é’ˆå¯ä»¥è½¬æ¢æˆçˆ¶ç±»æŒ‡é’ˆ, 且ä¸æ˜¯void*类型 - //相åŒç±»åž‹, SUPERSUBCLASS判断为true - #define SUPERSUBCLASS(T, U) (TL::Conversion::exists && !TL::Conversion::sameType) - //相åŒç±»åž‹, SUPERSUBCLASS_STRICT判断为false - #define SUPERSUBCLASS_STRICT(T, U) (SUPERSUBCLASS(T, U) && !TL::Conversion::sameType) - - /////////////////////////////////////////////////////////////////////////////////////////////// - // 类型选择器 - template - struct TypeSelect - { - typedef U Result; - }; - - template - struct TypeSelect - { - typedef V Result; - }; - - /////////////////////////////////////////////////////////////////////////////////////// - /** - * 类型èƒå–器, copy至Loki库 - */ - template - class TypeTraits - { - private: - - /////////////////////////////////////////////////////// - //æå–引用的原始类型(å³åŽ»æŽ‰å¼•ç”¨ç±»åž‹) - template - struct ReferenceTraits - { - enum { result = false }; - typedef U Result; - }; - - template - struct ReferenceTraits - { - enum { result = true }; - typedef U Result; - }; - - /////////////////////////////////////////////////////// - //指针类型 - template - struct PointerTraits - { - enum { result = false }; - typedef TL::NullType Result; - }; - - template - struct PointerTraits - { - enum { result = true }; - typedef U Result; - }; - - template - struct PointerTraits - { - enum { result = true }; - typedef U Result; - }; - - /////////////////////////////////////////////////////// - //æˆå‘˜å‡½æ•°æŒ‡é’ˆ, gcc下é¢æ”¯æŒæœ‰é—®é¢˜, å±è”½ä¹‹ - template - struct PointerToMemberTraits - { - enum { result = false }; - }; - - template - struct PointerToMemberTraits - { - enum { result = true }; - }; - - template - struct PointerToMemberTraits - { - enum { result = true }; - }; - - /////////////////////////////////////////////////////// - // const - template - struct UnConstTraits - { - enum { result = false }; - typedef U Result; - }; - template - struct UnConstTraits - { - enum { result = true }; - typedef U Result; - }; - template - struct UnConstTraits - { - enum { result = true }; - typedef U& Result; - }; - - /////////////////////////////////////////////////////// - // volatile - template - struct UnVolatileTraits - { - enum { result = false }; - typedef U Result; - }; - template - struct UnVolatileTraits - { - enum { result = true }; - typedef U Result; - }; - template - struct UnVolatileTraits - { - enum { result = true }; - typedef U& Result; - }; - public: - //T是å¦æ˜¯æŒ‡é’ˆç±»åž‹ - enum { isPointer = PointerTraits::result }; - //T是å¦æ˜¯å¼•ç”¨ç±»åž‹ - enum { isReference = ReferenceTraits::result }; - //T是å¦æŒ‡å‘æˆå‘˜å‡½æ•°çš„指针 - enum { isMemberPointer = PointerToMemberTraits::result }; - - //T是å¦æ˜¯Const类型 - enum { isConst = UnConstTraits::result }; - //T是å¦æ˜¯Volatile类型 - enum { isVolatile = UnVolatileTraits::result }; - - //如果T是指针类型,则获å–T的原类型, å³åŽ»æŽ‰æŒ‡é’ˆç±»åž‹ - typedef typename PointerTraits::Result PointeeType; - //如果T是引用类型,则获å–T的原类型, å³åŽ»æŽ‰å¼•ç”¨ç±»åž‹ - typedef typename ReferenceTraits::Result ReferencedType; - //如果T是Const类型,则获å–T的原类型, å³åŽ»æŽ‰Const类型 - typedef typename UnConstTraits::Result NonConstType; - //如果T是volatile类型,则获å–T的原类型, å³åŽ»æŽ‰volatile类型 - typedef typename UnVolatileTraits::Result NonVolatileType; - //去掉const volatile类型 - typedef typename UnVolatileTraits::Result>::Result UnqualifiedType; - - public: - - ////////////////////////////////////////////////////// - // - typedef TL::TLMaker::Result UnsignedInts; - typedef TL::TLMaker::Result SignedInts; - typedef TL::TLMaker::Result OtherInts; - typedef TL::TLMaker::Result Floats; - typedef TL::TYPELIST_2(TL::EmptyType, TL::NullType) NullTypes; - - //无符å·æ•´å½¢ - enum { isStdUnsignedInt = TL::IndexOf::value >= 0 }; - //有符å·æ•´å½¢ - enum { isStdSignedInt = TL::IndexOf::value >= 0 }; - //æ•´å½¢ - enum { isStdInt = isStdUnsignedInt || isStdSignedInt || TL::IndexOf::value >= 0 }; - //浮点类型 - enum { isStdFloat = TL::IndexOf::value >= 0 }; - //数值类型 - enum { isStdArith = isStdInt || isStdFloat }; - //基础类型(包括void) - enum { isStdFundamental = isStdArith || TL::IndexOf::value >= 0}; - //空类型 - enum { isNullType = TL::IndexOf::value >= 0 }; - //简å•ç±»åž‹ - enum { isBaseType = isStdArith || isPointer || isMemberPointer }; - - //对于å¤æ‚类型, 获å–æ•°æ®çš„引用类型, å³åŠ ä¸Šå¼•ç”¨ç±»åž‹ - typedef typename TypeSelect::Result ReferenceType; - - //对于å¤æ‚类型且éžç©ºç±»åž‹, 获å–æ•°æ®çš„引用类型, å³åŠ ä¸Šå¼•ç”¨ç±»åž‹ - //typedef typename TypeSelect::Result ReferenceTypeEx; - - //获å–æ•°æ®çš„原类型, 消除引用的引用这ç§æƒ…况 - typedef typename TypeSelect::Result ParameterType; - }; - - //////////////////////////////////////////////////////////////////////////////////////////////////// - //下é¢çš„使用开始展示TypeListçš„å¨åŠ›, 用于自动生æˆclass - - //散乱的继承体系 - template class Unit> - class ScatterHierarchy; - - /* - namespace p - { - //注释copy至loki库 - // The following type helps to overcome subtle flaw in the original - // implementation of GenScatterHierarchy. - // The flaw is revealed when the input type list of GenScatterHierarchy - // contains more then tars element of the same type (e.g. LOKI_TYPELIST_2(int, int)). - // In this case GenScatterHierarchy will contain multiple bases of the same - // type and some of them will not be reachable (per 10.3). - // For example before the fix the first element of Tuple - // is not reachable in any way! - template - struct ScatterHierarchyTag; - } - - template class Unit> - class ScatterHierarchy, Unit> : public ScatterHierarchy, Unit> - , public ScatterHierarchy - { - public: - typedef TypeList TList; - typedef ScatterHierarchy, Unit> LeftBase; - typedef ScatterHierarchy RightBase; - - template struct Rebind - { - typedef Unit Result; - }; - }; - - // In the middle *unique* class that resolve possible ambiguity - template class Unit> - class ScatterHierarchy, Unit> - : public ScatterHierarchy - { - }; - */ - - //具现化继承体系 - template class Unit> - class ScatterHierarchy, Unit> - : public ScatterHierarchy - , public ScatterHierarchy - { - public: - typedef TypeList TList; - typedef ScatterHierarchy LeftBase; - typedef ScatterHierarchy RightBase; - template struct Rebind - { - typedef Unit Result; - }; - }; - - template class Unit> - class ScatterHierarchy : public Unit - { - public: - typedef Unit LeftBase; - - template struct Rebind - { - typedef Unit Result; - }; - }; - - template