RemoteControllerCnn.cpp 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364
  1. #if (defined _WIN32 || defined _WIN64)
  2. #include <io.h>
  3. #include <winsock2.h>
  4. #include <ws2tcpip.h>
  5. #include <Psapi.h>
  6. #include "fileutil.h"
  7. #pragma comment(lib,"psapi.lib")
  8. #endif
  9. #include "RemoteControllerCnn.h"
  10. #include "RemoteControllerFSM.h"
  11. #include "Event.h"
  12. #include "cJSON.h"
  13. #include "base64.h"
  14. #include "digital_conv.h"
  15. #include <fstream>
  16. #include "path.h"
  17. #include "SpUtility.h"
  18. #include "../mod_healthmanager/HealthManager_client_g.h"
  19. using namespace HealthManager;
  20. #define __int64 long int
  21. #include "../mod_UpgradeMgr/UpgradeManager_client_g.h"
  22. using namespace UpgradeManager;
  23. #include "../mod_guiconsole/GUIConsole_client_g.h"
  24. using namespace GUIConsole;
  25. #include "../mod_ResourceWatcher/ResourceWatcher_client_g.h"
  26. using namespace ResourceWatcher;
  27. #include "remoteBase.h"
  28. #include "MyZip.h"
  29. namespace fs = boost::filesystem;
  30. #include <functional>
  31. #if (defined _WIN32 || defined _WIN64)
  32. #define generateFilesize(fileHandle, filePath) long long nFileSize = _filelengthi64(_fileno(fileHandle))
  33. #include "XZip.h"
  34. #else
  35. #define generateFilesize(fileHandle, filePath) long long nFileSize = fs::file_size(filePath.GetData())
  36. #include "zip.h"
  37. #include <stdlib.h>
  38. #include "XZipZilb.h"
  39. #define ZeroMemory(object, size) memset(object, 0, size)
  40. #define WritePrivateProfileStringA(a, b, c, d) Base_writeIni(std::string(a), std::string(b), std::string(c), std::string(d))
  41. #include <sstream>
  42. void _ui64toa(unsigned long long large_unit, char *str)
  43. {
  44. auto largeNumStr = std::to_string(large_unit);
  45. strcpy(str, largeNumStr.c_str());
  46. /*
  47. std::stringstream ss;
  48. ss << large_unit;
  49. std::string src = ss.str();
  50. memset(str, 0, strsize);
  51. strncpy(str, src.c_str(), strsize);
  52. */
  53. }
  54. int strncpy_s(char* _Destination, size_t _SizeInBytes, char const* _Source, size_t _MaxCount)
  55. {
  56. if (_MaxCount > _SizeInBytes)
  57. return nullptr == strncpy(_Destination, _Source, _SizeInBytes);
  58. else
  59. return nullptr == strncpy(_Destination, _Source, _MaxCount);
  60. }
  61. int strncpy_s(char* _Destination, char const* _Source, size_t _MaxCount)
  62. {
  63. return nullptr == strncpy(_Destination, _Source, _MaxCount);
  64. }
  65. #endif
  66. CRemoteControllerCnn::CRemoteControllerCnn(CEntityBase *pEntity, FSMBase *pFSM)
  67. :SpSecureClient(pEntity), m_pFSM(pFSM),m_chRemoteMode('N')
  68. {
  69. }
  70. CRemoteControllerCnn::~CRemoteControllerCnn(void)
  71. {
  72. }
  73. void CRemoteControllerCnn::OnDisconnect()
  74. {
  75. Dbg("connection disconnected");
  76. // 连接中断,重置远程维护状态,释放所有资源
  77. m_chRemoteMode = 'N';
  78. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_ReleaseChannel));
  79. }
  80. void CRemoteControllerCnn::OnPkgAnswer(const CSmartPointer<IPackage> &pRecvPkg)
  81. {
  82. string serviceCode = pRecvPkg->GetServiceCode();
  83. if (serviceCode == "StRemote")
  84. {
  85. HandleCreateRemoteChannelRet(pRecvPkg);
  86. }
  87. else if (serviceCode == "EdRemote")
  88. {
  89. HandleEndRemoteModeReq(pRecvPkg);
  90. }
  91. else if (serviceCode == "DelRun")
  92. {
  93. HandleDelRunInfoLogReq(pRecvPkg);
  94. }
  95. else if (serviceCode == "RestartS")
  96. {
  97. HandleRestartShellReq(pRecvPkg);
  98. }
  99. else if (serviceCode == "RestartO")
  100. {
  101. HandleRestartMachineReq(pRecvPkg);
  102. }
  103. else if (serviceCode == "ViewEnti")
  104. {
  105. HandleViewEntityStateReq(pRecvPkg);
  106. }
  107. else if (serviceCode == "ViewConf")
  108. {
  109. HandleViewEntityConfigReq(pRecvPkg);
  110. }
  111. else if (serviceCode == "ViewDbg")
  112. {
  113. HandleViewDebugLogReq(pRecvPkg);
  114. }
  115. else if (serviceCode == "OprDisk")
  116. {
  117. HandleOperateDiskReq(pRecvPkg);
  118. }
  119. else if (serviceCode == "EvtLog")
  120. {
  121. HandleUploadEventLogReq(pRecvPkg);
  122. }
  123. else if(serviceCode == "ULogBD")
  124. {
  125. HandleUploadLogByDateReq(pRecvPkg);
  126. }
  127. else if(serviceCode == "ULogsBD")
  128. {
  129. HandleBatchUploadLogsByDateReq(pRecvPkg);
  130. }
  131. else if (serviceCode == "EnterMM")
  132. {
  133. HandleEnterMaintainModeReq(pRecvPkg);
  134. }
  135. else if (serviceCode == "ExitMM")
  136. {
  137. HandleExitMaintainModeReq(pRecvPkg);
  138. }
  139. else if (serviceCode == "UpgPack")
  140. {
  141. HandleUpgradePackReq(pRecvPkg);
  142. }
  143. else if (serviceCode == "Rollback")
  144. {
  145. HandleRollbackUpgradeReq(pRecvPkg);
  146. }
  147. else if (serviceCode == "GetUser")
  148. {
  149. HandleGetCurMaintainerReq(pRecvPkg);
  150. }
  151. else if (serviceCode == "Enpower")
  152. {
  153. HandleEnpowerMaintainerReq(pRecvPkg);
  154. }
  155. else if (serviceCode == "TakePwr")
  156. {
  157. HandleTakeoverPowerReq(pRecvPkg);
  158. }
  159. else if (serviceCode == "ForceQt")
  160. {
  161. HandleForceQuitMaintainReq(pRecvPkg);
  162. }
  163. else if (serviceCode == "EditCfg")
  164. {
  165. HandleEditConfigReq(pRecvPkg);
  166. }
  167. else if (serviceCode == "ViewVar")
  168. {
  169. HandleViewSysVarReq(pRecvPkg);
  170. }
  171. else if (serviceCode == "GetVer")
  172. {
  173. HandleGetVersionReq(pRecvPkg);
  174. }
  175. else if (serviceCode == "VRunCfg")
  176. {
  177. HandleViewRunCfgReq(pRecvPkg);
  178. }
  179. else if (serviceCode == "VPkgLog")
  180. {
  181. HandleViewInstallLogReq(pRecvPkg);
  182. }
  183. else if (serviceCode == "UpldLog")
  184. {
  185. HandleUploadLogReq(pRecvPkg);
  186. }
  187. else if (serviceCode == "UpldLogs")
  188. {
  189. HandleBatchUploadLogsReq(pRecvPkg);
  190. }
  191. else if (serviceCode == "ViewEnEx")
  192. {
  193. HandleViewEntityStateExReq(pRecvPkg);
  194. }
  195. else if (serviceCode == "CancUpg")
  196. {
  197. HandleCancelUpgradeReq(pRecvPkg);
  198. }
  199. else
  200. {
  201. Dbg("unknown service code: %s", serviceCode.c_str());
  202. }
  203. }
  204. bool CRemoteControllerCnn::GetHostIPAddr(BYTE addr[4])
  205. {
  206. memset(addr, 0, 4);
  207. #if (defined _WIN32 || defined _WIN64)
  208. char szHostName[64] = {};
  209. int nRet = gethostname(szHostName, sizeof(szHostName));
  210. if (nRet != 0)
  211. {
  212. Dbg("gethostname fail: %d", nRet);
  213. return false;
  214. }
  215. addrinfo hints = {};
  216. hints.ai_family = AF_INET;
  217. PADDRINFOA pResult = NULL;
  218. nRet = getaddrinfo(szHostName, NULL, &hints, &pResult);
  219. if (nRet != 0)
  220. {
  221. Dbg("getaddrinfo fail: %d", nRet);
  222. return false;
  223. }
  224. for(auto ptr=pResult; ptr != NULL ;ptr=ptr->ai_next)
  225. {
  226. assert (ptr->ai_family == AF_INET);
  227. struct in_addr *in = (struct in_addr*)ptr->ai_addr;
  228. if (in->S_un.S_un_b.s_b1 != 192 || ptr->ai_next != NULL)
  229. {
  230. memcpy(&addr[0], in, 4);
  231. break;
  232. }
  233. }
  234. freeaddrinfo(pResult);
  235. return true;
  236. #else
  237. return Base_GetHostIPAddr(addr);
  238. #endif
  239. }
  240. ErrorCodeEnum CRemoteControllerCnn::SendCreateRemoteChannelReq()
  241. {
  242. auto pEntity = m_pEntity->GetFunction();
  243. CSystemStaticInfo info;
  244. auto rc = pEntity->GetSystemStaticInfo(info);
  245. assert(rc == Error_Succeed);
  246. RvcCreateChannelReq req = {};
  247. strncpy_s(req.TerminalNo, sizeof(req.TerminalNo), info.strTerminalID, _TRUNCATE);
  248. // get ip addr
  249. assert(GetHostIPAddr(req.IPAddr));
  250. // get life id
  251. CSystemRunInfo info2 = {};
  252. rc = pEntity->GetSystemRunInfo(info2);
  253. req.LifeID = info2.tmStart;
  254. // send package
  255. auto pReqPkg = CreateNewPackage("StRemote");
  256. pReqPkg->AddStruct("RemoteR", false, false, (BYTE*)&req, sizeof(req));
  257. auto strPkgID = SendPackage(pReqPkg);
  258. assert(strPkgID != "");
  259. return Error_Succeed;
  260. }
  261. ErrorCodeEnum CRemoteControllerCnn::HandleCreateRemoteChannelRet(const CSmartPointer<IPackage> &pRecvPkg)
  262. {
  263. DWORD dwSysCode, dwUserCode;
  264. string strErrMsg;
  265. ErrorCodeEnum rc = Error_Succeed;
  266. if (pRecvPkg->GetErrMsg(dwSysCode, dwUserCode, strErrMsg))
  267. {
  268. rc = (ErrorCodeEnum)dwSysCode;
  269. LogError(Severity_Middle, rc, dwUserCode,
  270. CSimpleStringA::Format("create remote channel error: %s", strErrMsg.c_str()));
  271. return rc;
  272. }
  273. RvcCreateChannelRet ret = {};
  274. int nLen = sizeof(ret);
  275. int nArrayLen(0);
  276. if (!pRecvPkg->GetStructData("RemoteA", (BYTE*)&ret, &nLen, &nArrayLen))
  277. {
  278. LogError(Severity_Low, Error_Param, 0, "get return struct [RemoteA] fail");
  279. return Error_Param;
  280. }
  281. Dbg("create remote channel succeed, remote user: [%s], channel id: [%s]", ret.AgentID, ret.ChannelID);
  282. return EnterMonitorMode();
  283. }
  284. ErrorCodeEnum CRemoteControllerCnn::HandleEndRemoteModeReq(const CSmartPointer<IPackage> &pRecvPkg)
  285. {
  286. Dbg("exit remote mode now");
  287. m_chRemoteMode = 'N';
  288. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_ReleaseChannel));
  289. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  290. SendPackage(pReplyPkg);
  291. return Error_Succeed;
  292. }
  293. ErrorCodeEnum CRemoteControllerCnn::HandleDelRunInfoLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  294. {
  295. Dbg("Del run info log req");
  296. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  297. int nLen = pRecvPkg->GetStructLen("DelRunR");
  298. if (nLen <= 0)
  299. {
  300. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [DelRunR] fail").c_str());
  301. SendPackage(pReplyPkg);
  302. return Error_Param;
  303. }
  304. assert(nLen == sizeof(RvcDelRunInfoLogReq));
  305. RvcDelRunInfoLogReq req = {};
  306. ZeroMemory(&req, sizeof(RvcDelRunInfoLogReq));
  307. int nArrayLen(0);
  308. pRecvPkg->GetStructData("DelRunR", (BYTE*)&req, &nLen, &nArrayLen);
  309. CSimpleString strLogName = req.logName;
  310. RvcDelRunInfoLogRet ret = {};
  311. ret.TaskID = req.TaskID;
  312. pReplyPkg->AddStruct("DelRunA", false, false, (BYTE*)&ret, sizeof(ret));
  313. auto pFunc = m_pEntity->GetFunction();
  314. CSystemStaticInfo sysInfo;
  315. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  316. CSimpleString runInfoPath;
  317. pFunc->GetPath("RunInfo", runInfoPath);
  318. CSimpleString delFilePaht = runInfoPath;
  319. delFilePaht.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(strLogName).Append(".ini");
  320. Dbg("curTaskId:%d ,try to delete %s, strLogName = %s", req.TaskID, delFilePaht, strLogName);
  321. if (Base_Exist(delFilePaht.GetData()))
  322. {
  323. if (!Base_DeleteFile(delFilePaht.GetData()))
  324. {
  325. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件删除失败", delFilePaht).GetData()).c_str());
  326. SendPackage(pReplyPkg);
  327. return Error_Cancel;
  328. }
  329. }
  330. else
  331. {
  332. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件不存在", delFilePaht).GetData()).c_str());
  333. SendPackage(pReplyPkg);
  334. return Error_Cancel;
  335. }
  336. SendPackage(pReplyPkg);
  337. return Error_Succeed;
  338. }
  339. ErrorCodeEnum CRemoteControllerCnn::HandleTest(const CSmartPointer<IPackage> &pRecvPkg)
  340. {
  341. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  342. auto pFunc = m_pEntity->GetFunction();
  343. CSystemStaticInfo sysInfo;
  344. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  345. CSimpleString runInfoPath;
  346. pFunc->GetPath("RunInfo", runInfoPath);
  347. CSimpleString delFilePaht = runInfoPath;
  348. delFilePaht.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("MediaController").Append(".ini");
  349. Dbg("try to delete %s", delFilePaht);
  350. if (Base_Exist(delFilePaht.GetData()))
  351. {
  352. if (!Base_DeleteFile(delFilePaht.GetData()))
  353. {
  354. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件删除失败", delFilePaht).GetData()).c_str());
  355. SendPackage(pReplyPkg);
  356. return Error_Cancel;
  357. }
  358. }
  359. else
  360. {
  361. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件不存在", delFilePaht).GetData()).c_str());
  362. SendPackage(pReplyPkg);
  363. return Error_Cancel;
  364. }
  365. SendPackage(pReplyPkg);
  366. return Error_Succeed;
  367. }
  368. ErrorCodeEnum CRemoteControllerCnn::HandleRestartShellReq(const CSmartPointer<IPackage> &pRecvPkg)
  369. {
  370. Dbg("remote restart shell req");
  371. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  372. int nLen = pRecvPkg->GetStructLen("RestSR");
  373. if (nLen <= 0)
  374. {
  375. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [RestSR] fail").c_str());
  376. SendPackage(pReplyPkg);
  377. return Error_Param;
  378. }
  379. assert(nLen == sizeof(RvcRestartShellReq));
  380. RvcRestartShellReq req = {};
  381. int nArrayLen(0);
  382. pRecvPkg->GetStructData("RestSR", (BYTE*)&req, &nLen, &nArrayLen);
  383. RvcRestartShellRet ret = {};
  384. ret.TaskID = req.TaskID;
  385. pReplyPkg->AddStruct("RestSA", false, false, (BYTE*)&ret, sizeof(ret));
  386. // 检查当前状态,是否可以重启Shell
  387. auto pFunc = m_pEntity->GetFunction();
  388. CSimpleStringA strCustomerHandleVal;
  389. pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
  390. CSimpleStringA strCustomerBeingVal;
  391. pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
  392. if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
  393. {
  394. Dbg("remote restart shell fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
  395. , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
  396. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法重启").c_str());
  397. SendPackage(pReplyPkg);
  398. return Error_Cancel;
  399. }
  400. CSimpleStringA strValue = "";
  401. pFunc->GetSysVar("LocalMaintain", strValue);
  402. if (strValue.Compare("N") != 0)
  403. {
  404. Dbg("remote restart shell fail for var [LocalMaintain] == '%s'", (const char*)strValue);
  405. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法重启").c_str());
  406. SendPackage(pReplyPkg);
  407. return Error_Cancel;
  408. }
  409. // 释放重启事件给健康实体
  410. LogEvent(Severity_Middle, EVENT_RESTART_SHELL, "remote restart shell");
  411. SendPackage(pReplyPkg);
  412. return Error_Succeed;
  413. }
  414. ErrorCodeEnum CRemoteControllerCnn::HandleRestartMachineReq(const CSmartPointer<IPackage> &pRecvPkg)
  415. {
  416. Dbg("remote restart machine req");
  417. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  418. int nLen = pRecvPkg->GetStructLen("RestOR");
  419. if (nLen <= 0)
  420. {
  421. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [RestOR] fail").c_str());
  422. SendPackage(pReplyPkg);
  423. return Error_Param;
  424. }
  425. assert(nLen == sizeof(RvcRestartMachineReq));
  426. RvcRestartMachineReq req = {};
  427. int nArrayLen(0);
  428. pRecvPkg->GetStructData("RestOR", (BYTE*)&req, &nLen, &nArrayLen);
  429. RvcRestartMachineRet ret = {};
  430. ret.TaskID = req.TaskID;
  431. pReplyPkg->AddStruct("RestOA", false, false, (BYTE*)&ret, sizeof(ret));
  432. // 检查当前状态,是否可以重启OS
  433. auto pFunc = m_pEntity->GetFunction();
  434. CSimpleStringA strCustomerHandleVal;
  435. pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
  436. CSimpleStringA strCustomerBeingVal;
  437. pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
  438. if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
  439. {
  440. Dbg("remote restart machine fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
  441. , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
  442. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法重启").c_str());
  443. SendPackage(pReplyPkg);
  444. return Error_Cancel;
  445. }
  446. CSimpleStringA strValue = "";
  447. pFunc->GetSysVar("LocalMaintain", strValue);
  448. if (strValue.Compare("N") != 0)
  449. {
  450. Dbg("remote restart machine fail for var [LocalMaintain] == '%s'", (const char*)strValue);
  451. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法重启").c_str());
  452. SendPackage(pReplyPkg);
  453. return Error_Cancel;
  454. }
  455. // 释放重启事件给健康实体
  456. LogEvent(Severity_Middle, EVENT_RESTART_MACHINE, "remote restart machine");
  457. SendPackage(pReplyPkg);
  458. return Error_Succeed;
  459. }
  460. ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityStateReq(const CSmartPointer<IPackage> &pRecvPkg)
  461. {
  462. Dbg("remote view entity state");
  463. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  464. int nLen = pRecvPkg->GetStructLen("ViewEntR");
  465. if (nLen <=0)
  466. {
  467. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewEntR] fail").c_str());
  468. SendPackage(pReplyPkg);
  469. return Error_Param;
  470. }
  471. assert(nLen == sizeof(RvcViewEntityReq));
  472. RvcViewEntityReq req = {};
  473. int nArrayLen(0);
  474. pRecvPkg->GetStructData("ViewEntR", (BYTE*)&req, &nLen, &nArrayLen);
  475. RvcViewEntityRet1 ret1 = {};
  476. ret1.TaskID = req.TaskID;
  477. pReplyPkg->AddStruct("ViewEnA1", false, false, (BYTE*)&ret1, sizeof(ret1));
  478. auto pFunc = m_pEntity->GetFunction();
  479. CAutoArray<CSimpleStringA> strEntityNames;
  480. CAutoArray<WORD> dwEntityIDs;
  481. pFunc->GetAllRegistedEntity(strEntityNames, dwEntityIDs);
  482. int nCount = strEntityNames.GetCount();
  483. if (nCount >0)
  484. {
  485. nCount += 8; // 包括root\shell\silverlight3个虚拟实体
  486. RvcViewEntityRet2 *pRet2 = new RvcViewEntityRet2[nCount];
  487. memset(pRet2, 0, sizeof(RvcViewEntityRet2) * nCount);
  488. for(int i=0; i<nCount - 8; i++)
  489. {
  490. CEntityRunInfo info = {};
  491. CSimpleStringA strEntityName = strEntityNames[i];
  492. pFunc->GetEntityRunInfo(strEntityName, info);
  493. strncpy_s(pRet2[i].EntityName, sizeof(pRet2[i].EntityName), (const char*)strEntityName, _TRUNCATE);
  494. pRet2[i].EntityID = dwEntityIDs[i];
  495. pRet2[i].EntityState = info.eState;
  496. pRet2[i].ProcessID = info.dwProcessID;
  497. if (info.dwProcessID > 0)
  498. {
  499. #if (defined _WIN32 || defined _WIN64)
  500. auto hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, info.dwProcessID );
  501. if (hProcess != NULL)
  502. {
  503. PROCESS_MEMORY_COUNTERS pmc = {};
  504. GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
  505. pRet2[i].UsedMemory = pmc.WorkingSetSize / 1024 / 1024;
  506. }
  507. #else
  508. pRet2[i].UsedMemory = get_proc_mem(info.dwProcessID) / 1024 / 1024;
  509. #endif
  510. }
  511. }
  512. strncpy_s(pRet2[nCount - 8].EntityName, sizeof(pRet2[nCount - 8].EntityName), "guardian", _TRUNCATE);
  513. strncpy_s(pRet2[nCount - 7].EntityName, sizeof(pRet2[nCount - 7].EntityName), "DualActive", _TRUNCATE);
  514. strncpy_s(pRet2[nCount - 6].EntityName, sizeof(pRet2[nCount - 6].EntityName), "SpHost", _TRUNCATE);
  515. strncpy_s(pRet2[nCount - 5].EntityName, sizeof(pRet2[nCount - 5].EntityName), "SysInit", _TRUNCATE);
  516. strncpy_s(pRet2[nCount - 4].EntityName, sizeof(pRet2[nCount - 4].EntityName), "SpBase", _TRUNCATE);
  517. strncpy_s(pRet2[nCount - 3].EntityName, sizeof(pRet2[nCount - 3].EntityName), "Root", _TRUNCATE);
  518. strncpy_s(pRet2[nCount - 2].EntityName, sizeof(pRet2[nCount - 2].EntityName), "Shell", _TRUNCATE);
  519. strncpy_s(pRet2[nCount - 1].EntityName, sizeof(pRet2[nCount - 1].EntityName), "Silverlight", _TRUNCATE);
  520. pReplyPkg->AddStruct("ViewEnA2", false, false, (BYTE*)pRet2, sizeof(RvcViewEntityRet2) * nCount, nCount);
  521. }
  522. SendPackage(pReplyPkg);
  523. return Error_Succeed;
  524. }
  525. ErrorCodeEnum CRemoteControllerCnn::HandleViewSysVarReq(const CSmartPointer<IPackage> &pRecvPkg)
  526. {
  527. Dbg("remote view system vars");
  528. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  529. int nLen = pRecvPkg->GetStructLen("ViewVarR");
  530. if (nLen <= 0)
  531. {
  532. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewVarR] len fail").c_str());
  533. SendPackage(pReplyPkg);
  534. return Error_Param;
  535. }
  536. assert(nLen == sizeof(RvcViewSysVarReq));
  537. RvcViewSysVarReq req = {};
  538. int nArrayLen(0);
  539. if (!pRecvPkg->GetStructData("ViewVarR", (BYTE*)&req, &nLen, &nArrayLen))
  540. {
  541. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewVarR] data fail").c_str());
  542. SendPackage(pReplyPkg);
  543. return Error_Param;
  544. }
  545. auto pFunc = m_pEntity->GetFunction();
  546. RvcViewSysVarRet arrRet[5] = {};
  547. char *arrVarNames[] = { "RunState", "CustomerHandle", "CallState", "EntryPermit", "LocalMaintain" };
  548. for (int i = 0; i < 5; i++)
  549. {
  550. CSimpleStringA strValue;
  551. pFunc->GetSysVar(arrVarNames[i], strValue);
  552. arrRet[i].TaskID = req.TaskID;
  553. strcpy(arrRet[i].VarName, arrVarNames[i]);
  554. arrRet[i].VarValue = strValue[0];
  555. }
  556. pRecvPkg->AddStruct("ViewVarA", false, false, (BYTE*)arrRet, sizeof(arrRet), 5);
  557. SendPackage(pRecvPkg);
  558. return Error_Succeed;
  559. }
  560. ErrorCodeEnum CRemoteControllerCnn::HandleEditConfigReq(const CSmartPointer<IPackage> &pRecvPkg)
  561. {
  562. Dbg("remote edit config");
  563. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  564. int nLen = pRecvPkg->GetStructLen("EditCfgR");
  565. if (nLen <= 0)
  566. {
  567. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EditCfgR] len fail").c_str());
  568. SendPackage(pReplyPkg);
  569. return Error_Param;
  570. }
  571. char *pBuf = new char[nLen];
  572. memset(pBuf, 0, sizeof(pBuf));
  573. int nArrayLen(0);
  574. if (!pRecvPkg->GetStructData("EditCfgR", (BYTE*)pBuf, &nLen, &nArrayLen) || nArrayLen <=0)
  575. {
  576. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EditCfgR] data fail").c_str());
  577. SendPackage(pReplyPkg);
  578. return Error_Param;
  579. }
  580. auto pFunc = m_pEntity->GetFunction();
  581. assert(nLen == nArrayLen * sizeof(RvcEditConfigReq));
  582. RvcEditConfigReq *pReq = (RvcEditConfigReq*)pBuf;
  583. RvcEditConfigRet ret = { pReq[0].TaskID };
  584. for (int i = 0; i < nArrayLen; i++)
  585. {
  586. CSimpleStringA strEntityName = pReq[i].EntityName;
  587. CSimpleStringA strCfgPath;
  588. if (iequals(strEntityName.GetData(), "Silverlight") || iequals(strEntityName.GetData(), "DualActive") || iequals(strEntityName.GetData(), "SpHost")
  589. || iequals(strEntityName.GetData(), "SysInit") || iequals(strEntityName.GetData(), "SpBase") || iequals(strEntityName.GetData(), "guardian"))
  590. {
  591. Dbg("entity \"%s\" has no config", strEntityName);
  592. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("实体没有配置文件").c_str());
  593. SendPackage(pReplyPkg);
  594. delete[] pBuf;
  595. return Error_NotExist;
  596. }
  597. if (iequals(strEntityName.GetData(), "Root"))
  598. {
  599. auto rc = pFunc->GetPath("HardwareCfg", strCfgPath);
  600. assert(rc == Error_Succeed);
  601. strCfgPath.Append(SPLIT_SLASH_STR).Append("Root.ini");
  602. }
  603. else
  604. {
  605. auto rc = pFunc->GetPath("Cfg", strCfgPath);
  606. assert(rc == Error_Succeed);
  607. strCfgPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(".ini");
  608. }
  609. char cEditType = pReq[i].EditType;
  610. Dbg("edit config: entity = %s, type = %c, section = %s, key = %s, value = %s", (const char*)strEntityName, cEditType, pReq[i].Section, pReq[i].Key, pReq[i].Value);
  611. if (cEditType == 'A' || cEditType == 'U')
  612. WritePrivateProfileStringA(pReq[i].Section, pReq[i].Key, pReq[i].Value, strCfgPath.GetData());
  613. else if (cEditType == 'D')
  614. WritePrivateProfileStringA(pReq[i].Section, pReq[i].Key, "", strCfgPath.GetData());
  615. }
  616. delete[] pBuf;
  617. pReplyPkg->AddStruct("EditCfgA", false, false, (BYTE*)&ret, sizeof(ret));
  618. SendPackage(pRecvPkg);
  619. return Error_Succeed;
  620. }
  621. ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityConfigReq(const CSmartPointer<IPackage> &pRecvPkg)
  622. {
  623. Dbg("remote view entity config");
  624. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  625. int nLen = pRecvPkg->GetStructLen("ViewCfgR");
  626. if (nLen <=0)
  627. {
  628. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewCfgR] fail").c_str());
  629. SendPackage(pReplyPkg);
  630. return Error_Param;
  631. }
  632. assert(nLen == sizeof(RvcViewConfigReq));
  633. RvcViewConfigReq req = {};
  634. int nArrayLen(0);
  635. pRecvPkg->GetStructData("ViewCfgR", (BYTE*)&req, &nLen, &nArrayLen);
  636. // 读取实体配置文件
  637. CSimpleStringA strEntityName = req.EntityName;
  638. auto pFunc = m_pEntity->GetFunction();
  639. CSimpleStringA strCfgPath;
  640. if (strEntityName == "Root")
  641. {
  642. auto rc = pFunc->GetPath("HardwareCfg", strCfgPath);
  643. assert(rc == Error_Succeed);
  644. strCfgPath.Append(SPLIT_SLASH_STR).Append("Root.ini");
  645. }
  646. else
  647. {
  648. auto rc = pFunc->GetPath("Cfg", strCfgPath);
  649. assert(rc == Error_Succeed);
  650. strCfgPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(".ini");
  651. }
  652. if (!Base_Exist(strCfgPath.GetData()))
  653. {
  654. Dbg("file [%s] not exist", (const char*)strCfgPath);
  655. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置文件不存在").c_str());
  656. SendPackage(pReplyPkg);
  657. return Error_NotExist;
  658. }
  659. auto cfgSize = Base_filesize(strCfgPath.GetData());
  660. char* tmpBuf = new char[cfgSize];
  661. memset(tmpBuf, 0, cfgSize);
  662. auto readRet = Base_readFile(strCfgPath.GetData(), 0, tmpBuf, cfgSize);
  663. if(!readRet.first)
  664. {
  665. pReplyPkg->SetErrMsg(0, 0, SP::Utility::UTF8ToGBK("open entity config file fail").c_str());
  666. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open config [%s] fail", (const char*)strCfgPath));
  667. }
  668. else
  669. {
  670. int nRetLen = sizeof(RvcViewConfigRet) + cfgSize * 2; //需自行转换\n和\r\n
  671. char* pBuf = new char[nRetLen];
  672. memset(pBuf, 0, nRetLen);
  673. RvcViewConfigRet* pRet = (RvcViewConfigRet*)pBuf;
  674. int dstPos = 0;
  675. for (auto i = 0; i < cfgSize; i++)
  676. {
  677. if ((i == 0 && tmpBuf[i] == '\n') || (tmpBuf[i] == '\n' && tmpBuf[i - 1] != '\r'))
  678. pRet->ConfigData[dstPos++] = '\r';
  679. pRet->ConfigData[dstPos++] = tmpBuf[i];
  680. }
  681. pReplyPkg->AddStruct("ViewCfgA", false, false, (BYTE*)pBuf, dstPos + sizeof(RvcViewConfigRet));
  682. }
  683. SendPackage(pReplyPkg);
  684. return Error_Succeed;
  685. }
  686. ErrorCodeEnum CRemoteControllerCnn::HandleViewDebugLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  687. {
  688. Dbg("remote view entity debug log");
  689. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  690. int nLen = pRecvPkg->GetStructLen("ViewDbgR");
  691. if (nLen <=0)
  692. {
  693. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewDbgR] fail").c_str());
  694. SendPackage(pReplyPkg);
  695. return Error_Param;
  696. }
  697. assert(nLen == sizeof(RvcViewDbgReq));
  698. RvcViewDbgReq req = {};
  699. int nArrayLen(0);
  700. pRecvPkg->GetStructData("ViewDbgR", (BYTE*)&req, &nLen, &nArrayLen);
  701. auto pFunc = m_pEntity->GetFunction();
  702. CSimpleStringA strEntityName = req.EntityName;
  703. if (iequals(strEntityName.GetData(), "Root"))
  704. {
  705. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("Root实体没有日志").c_str());
  706. Dbg("entity \"Root\" has no dbg log");
  707. SendPackage(pReplyPkg);
  708. return Error_NotExist;
  709. }
  710. CSimpleStringA strViewDate = req.ViewDate;
  711. if (strViewDate.IsNullOrEmpty())
  712. {
  713. strViewDate = getCurData().c_str();
  714. }
  715. ErrorCodeEnum rc(Error_Succeed);
  716. CSimpleStringA strLogPath;
  717. if (iequals(strEntityName.GetData(), "Shell"))
  718. {
  719. auto rc = pFunc->GetPath("Dbg", strLogPath);
  720. assert(rc == Error_Succeed);
  721. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  722. }
  723. else if (iequals(strEntityName.GetData(), "Silverlight"))
  724. {
  725. auto rc = pFunc->GetPath("Slv", strLogPath);
  726. assert(rc == Error_Succeed);
  727. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  728. }
  729. else if (iequals(strEntityName.GetData(), "DualActive")|| iequals(strEntityName.GetData(), "SpHost")
  730. || iequals(strEntityName.GetData(), "SysInit") || iequals(strEntityName.GetData(), "SpBase") || iequals(strEntityName.GetData(), "guardian"))
  731. {
  732. auto rc = pFunc->GetPath("Dbg", strLogPath);
  733. assert(rc == Error_Succeed);
  734. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  735. }
  736. else
  737. {
  738. CEntityStaticInfo info = {};
  739. auto rc = pFunc->GetEntityStaticInfo(strEntityName, info);
  740. if (rc != Error_Succeed)
  741. {
  742. Dbg("invalid entity name [%s]", strEntityName);
  743. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体名称\"%s\"无效", strEntityName).GetData()).c_str());
  744. SendPackage(pReplyPkg);
  745. return rc;
  746. }
  747. rc = pFunc->GetPath("Dbg", strLogPath);
  748. assert(rc == Error_Succeed);
  749. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  750. }
  751. if (!Base_Exist(strLogPath.GetData()))
  752. {
  753. Dbg("log [%s] not exist", (const char*)strLogPath);
  754. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  755. SendPackage(pReplyPkg);
  756. return Error_NotExist;
  757. }
  758. long long nStartPos = req.StartPos;
  759. if (nStartPos <0)
  760. nStartPos = 0;
  761. long long nMaxBytes = req.MaxRetBytes;
  762. if (nMaxBytes <= 0)
  763. nMaxBytes = 8 * 1024;
  764. auto file = fopen(strLogPath, "rb");
  765. if (file == NULL)
  766. {
  767. auto nError = errno;
  768. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strLogPath, nError).GetData()).c_str());
  769. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strLogPath, nError));
  770. SendPackage(pReplyPkg);
  771. return rc;
  772. }
  773. generateFilesize(file, strLogPath);
  774. if (nStartPos >0 && nStartPos < nFileSize)
  775. _fseeki64(file, nStartPos, SEEK_SET);
  776. else
  777. nStartPos = 0;
  778. long long nNextPos = nStartPos + nMaxBytes;
  779. if (nMaxBytes + nStartPos > nFileSize)
  780. {
  781. nMaxBytes = nFileSize - nStartPos;
  782. nNextPos = 0;
  783. }
  784. long long nRetLen = sizeof(RvcViewDbgRet) +nMaxBytes;
  785. char *pBuf = new char[nRetLen];
  786. memset(pBuf, 0, nRetLen);
  787. RvcViewDbgRet *pRet = (RvcViewDbgRet*)pBuf;
  788. pRet->TaskID = req.TaskID;
  789. strncpy_s(pRet->EntityName, sizeof(pRet->EntityName), strEntityName, _TRUNCATE);
  790. strncpy(pRet->ViewDate, strViewDate, 8);
  791. pRet->NextPos = nNextPos;
  792. pRet->FileLength = nFileSize;
  793. char *pLogData = (char*)pRet->LogData;
  794. long long nHasRead(0);
  795. DWORD nReadLen(0);
  796. BOOL bRet = TRUE;
  797. do
  798. {
  799. nReadLen = fread(pLogData+nHasRead, 1, nMaxBytes-nHasRead, file);
  800. if (nReadLen >0)
  801. nHasRead += nReadLen;
  802. }
  803. while (nReadLen >0 && nHasRead < nMaxBytes);
  804. fclose(file);
  805. if (!bRet)
  806. {
  807. delete[] pBuf;
  808. auto nError = GetLastError();
  809. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strLogPath, nError).GetData()).c_str());
  810. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strLogPath, nError));
  811. SendPackage(pReplyPkg);
  812. return rc;
  813. }
  814. if (pRet->NextPos > 0)
  815. {
  816. // 整行切分处理
  817. while (nReadLen > sizeof(RvcViewDbgRet) &&
  818. (pBuf[nRetLen - 1] != '\r' && pBuf[nRetLen - 1] != '\n'))
  819. {
  820. nRetLen--;
  821. pRet->NextPos--;
  822. }
  823. }
  824. pReplyPkg->AddStruct("ViewDbgA", false, false, (BYTE*)pBuf, nRetLen);
  825. delete[] pBuf;
  826. SendPackage(pReplyPkg);
  827. return Error_Succeed;
  828. }
  829. ErrorCodeEnum CRemoteControllerCnn::HandleUploadEventLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  830. {
  831. Dbg("remote upload event log");
  832. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  833. int nLen = pRecvPkg->GetStructLen("EvtLogR");
  834. if (nLen <= 0)
  835. {
  836. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EvtLogR] fail").c_str());
  837. SendPackage(pReplyPkg);
  838. return Error_Param;
  839. }
  840. if (nLen != sizeof(RvcUploadEventLogReq))
  841. Dbg("nLen:%d, RvcUploadEventLogReq Size:%d", nLen, sizeof(RvcUploadEventLogReq));
  842. assert(nLen == sizeof(RvcUploadEventLogReq));
  843. RvcUploadEventLogReq req;
  844. int nArrayLen(0);
  845. pRecvPkg->GetStructData("EvtLogR", (BYTE*)&req, &nLen, &nArrayLen);
  846. auto pFunc = m_pEntity->GetFunction();
  847. CSimpleStringA strZipFileName;
  848. CSimpleStringA strLogPath;
  849. CSimpleStringA strZipPath, strTodayLogName;
  850. if (req.StartPos <= 0)
  851. {
  852. ResourceWatcherService_ExtractEventLog_Req rvcReq;
  853. ZeroMemory(&rvcReq, sizeof(ResourceWatcherService_ExtractEventLog_Req));
  854. {//初始化rvcReq
  855. rvcReq.evtName = req.evtName;
  856. rvcReq.evtLevel = req.evtLevel;
  857. rvcReq.duration = req.duration;
  858. rvcReq.startTime = req.startTime;
  859. rvcReq.endTime = req.endTime;
  860. rvcReq.cusEvtFileName = CSimpleStringA(req.cusEvtFileName);
  861. rvcReq.evtSrcEventName = CSimpleStringA(req.evtSrcEventName);
  862. rvcReq.reversed1 = req.reversed1;
  863. rvcReq.reversed2 = CSimpleStringA(req.reversed2);
  864. }
  865. ResourceWatcherService_ExtractEventLog_Ans rvcAns;
  866. ZeroMemory(&rvcAns, sizeof(ResourceWatcherService_ExtractEventLog_Ans));
  867. ResourceWatcherService_ClientBase *pClient = new ResourceWatcherService_ClientBase(m_pEntity);
  868. auto rc = pClient->Connect();
  869. if (rc == Error_Succeed)
  870. {
  871. Dbg("evtName:%d, evtLevel:%d, duration:%d, startTime:%d, endTime:%d, cusEvtFileName:%s,evtSrcEventName:%s, reversed1:%d, reversed2:%s, startPos:%d, MaxRet:%d, UploadFile:%s",
  872. req.evtName, req.evtLevel, req.duration, req.startTime, req.endTime,
  873. req.cusEvtFileName, req.evtSrcEventName, req.reversed1, req.reversed2, req.StartPos, req.MaxRetBytes, req.UploadFile);
  874. rc = pClient->ExtractEventLog(rvcReq, rvcAns, 60000 * 5);
  875. Dbg("entries:%d, information:%s, evtLogFileName:%s", rvcAns.entries, rvcAns.information.GetData(), rvcAns.evtLogFileName.GetData());
  876. pClient->GetFunction()->CloseSession();
  877. }
  878. if (rc != Error_Succeed)
  879. {
  880. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[ResourceWatcher]实体失败").c_str());
  881. LogError(Severity_Low, rc, 0, "调用[ResourceWatcher]实体失败");
  882. }
  883. else
  884. {
  885. Dbg("获取系统事件日志成功");
  886. //压缩位置
  887. strTodayLogName = getCurData().c_str();
  888. CSystemStaticInfo sysInfo;
  889. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  890. strZipFileName = CSimpleStringA::Format("%s_event_%s_%02X_%02X.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName, rvcAns.entries,
  891. req.evtLevel + req.duration + req.startTime + req.endTime);
  892. pFunc->GetPath("Temp", strZipPath);
  893. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  894. strLogPath = rvcAns.evtLogFileName;
  895. if (0 == rvcAns.entries)
  896. {
  897. Dbg("log [%s] not exist", (const char*)strLogPath);
  898. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("未能获取到相关日志").GetData()).c_str());
  899. SendPackage(pReplyPkg);
  900. return Error_NotExist;
  901. }
  902. if (!Base_Exist(strLogPath.GetData()))
  903. {
  904. Dbg("log [%s] not exist", (const char*)strLogPath);
  905. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  906. SendPackage(pReplyPkg);
  907. return Error_NotExist;
  908. }
  909. // 生成ZIP
  910. rc = ZipFile(strZipPath, strLogPath);
  911. if (rc != Error_Succeed)
  912. {
  913. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  914. SendPackage(pReplyPkg);
  915. return rc;
  916. }
  917. else
  918. Base_DeleteFile(strLogPath.GetData());
  919. }
  920. }
  921. else
  922. {
  923. strZipFileName = req.UploadFile;
  924. pFunc->GetPath("Temp", strZipPath);
  925. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  926. }
  927. if (!Base_Exist(strZipPath.GetData()))
  928. {
  929. Dbg("zip log [%s] not exist", (const char*)strZipPath);
  930. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  931. SendPackage(pReplyPkg);
  932. return Error_NotExist;
  933. }
  934. // 开始上传
  935. long long nStartPos = req.StartPos;
  936. if (nStartPos <0)
  937. nStartPos = 0;
  938. long long nMaxBytes = req.MaxRetBytes;
  939. if (nMaxBytes <= 0)
  940. nMaxBytes = 32 * 1024;
  941. auto file = fopen(strZipPath, "rb");
  942. if (file == NULL)
  943. {
  944. auto nError = errno;
  945. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  946. Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  947. SendPackage(pReplyPkg);
  948. return Error_Unexpect;
  949. }
  950. generateFilesize(file, strZipPath);
  951. if (nStartPos >0 && nStartPos < nFileSize)
  952. {
  953. _fseeki64(file, nStartPos, SEEK_SET);
  954. }
  955. else
  956. nStartPos = 0;
  957. long long nNextPos = nStartPos + nMaxBytes;
  958. if (nMaxBytes + nStartPos > nFileSize)
  959. {
  960. nMaxBytes = nFileSize - nStartPos;
  961. nNextPos = 0;
  962. }
  963. long long nRetLen = sizeof(RvcUploadEventLogRet)+nMaxBytes;
  964. char *pBuf = new char[nRetLen];
  965. memset(pBuf, 0, nRetLen);
  966. RvcUploadEventLogRet *pRet = (RvcUploadEventLogRet*)pBuf;
  967. pRet->TaskID = req.TaskID;
  968. strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
  969. pRet->ZipFileLen = nFileSize;
  970. pRet->StartPos = req.StartPos;
  971. pRet->NextPos = nNextPos;
  972. char *pLogData = (char*)pRet->LogData;
  973. long long nHasRead(0);
  974. DWORD nReadLen(0);
  975. BOOL bRet = TRUE;
  976. do
  977. {
  978. //Dbg("ftell: %d", ftell(file));
  979. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  980. //Dbg("ftell: %d", ftell(file));
  981. if (nReadLen >0)
  982. nHasRead += nReadLen;
  983. } while (nReadLen >0 && nHasRead < nMaxBytes);
  984. fclose(file);
  985. if (nHasRead < nMaxBytes)
  986. {
  987. delete[] pBuf;
  988. auto nError = GetLastError();
  989. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  990. Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  991. SendPackage(pReplyPkg);
  992. return Error_Unexpect;
  993. }
  994. pReplyPkg->AddStruct("EvtLogA", false, false, (BYTE*)pBuf, nRetLen);
  995. delete[] pBuf;
  996. SendPackage(pReplyPkg);
  997. if (nNextPos <= 0)
  998. {
  999. // 删除压缩文件
  1000. Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  1001. Base_DeleteFile(strZipPath.GetData());
  1002. }
  1003. return Error_Succeed;
  1004. }
  1005. char *makeOperateDiskJson(RvcOperateDiskRet1 *ret1, RvcOperateDiskRet2 *ret2, int ret2Num)
  1006. {
  1007. cJSON * pJsonRoot = NULL;
  1008. pJsonRoot = cJSON_CreateObject();
  1009. if(NULL == pJsonRoot || NULL == ret1 || NULL == ret2 || ret2Num <= 0)
  1010. return NULL;
  1011. cJSON_AddNumberToObject(pJsonRoot, "TaskID", ret1->TaskID);
  1012. cJSON_AddNumberToObject(pJsonRoot, "result", ret1->result);
  1013. cJSON_AddStringToObject(pJsonRoot, "header", ret1->header);
  1014. cJSON_AddNumberToObject(pJsonRoot, "attachment1", ret1->attachment1);
  1015. cJSON *arrFileName = cJSON_CreateArray(), *arrFileSize = cJSON_CreateArray(), *arrFtCreate = cJSON_CreateArray(), *arrftModified = cJSON_CreateArray(),
  1016. *arrftAccess = cJSON_CreateArray(), *arrFileAttribute = cJSON_CreateArray(), *arrForbidAttributes = cJSON_CreateArray(),
  1017. *arrReserved1 = cJSON_CreateArray(), *arrReserved2 = cJSON_CreateArray();
  1018. if (NULL == arrFileName || NULL == arrFileSize || NULL == arrFtCreate || NULL == arrftModified || NULL == arrftAccess || NULL == arrFileAttribute || NULL == arrForbidAttributes
  1019. || NULL == arrReserved1 || NULL ==arrReserved2)
  1020. return NULL;
  1021. for (int i = 0; i < ret2Num; i++)
  1022. {
  1023. CDigitalConv62 d62;
  1024. char temp[30] = "";
  1025. string str62;
  1026. cJSON_AddStringToObject(arrFileName, "fileName", ret2[i].fileName);
  1027. _ui64toa(ret2[i].fileSize, temp);
  1028. cJSON_AddStringToObject(arrFileSize, "fileSize", d62.to_x(temp, 1).data());
  1029. _ui64toa(ret2[i].ftCreate, temp);
  1030. cJSON_AddStringToObject(arrFtCreate, "ftCreate", d62.to_x(temp, 1).data());
  1031. _ui64toa(ret2[i].ftModified, temp);
  1032. cJSON_AddStringToObject(arrftModified, "ftModified", d62.to_x(temp, 1).data());
  1033. _ui64toa(ret2[i].ftAccess, temp);
  1034. cJSON_AddStringToObject(arrftAccess, "ftAccess", d62.to_x(temp, 1).data());
  1035. cJSON_AddNumberToObject(arrFileAttribute, "fileAttribute", ret2[i].fileAttribute);
  1036. cJSON_AddNumberToObject(arrForbidAttributes, "forbidAttributes", ret2[i].forbidAttributes);
  1037. if (0 != ret2[i].reserved1)
  1038. cJSON_AddNumberToObject(arrReserved1, "reserved1", ret2[i].reserved1);
  1039. //cJSON_AddStringToObject(arrReserved2, "reserved2", ret2[i].reserved2);
  1040. }
  1041. cJSON_AddItemToObject(pJsonRoot, "arrFileName", arrFileName);
  1042. cJSON_AddItemToObject(pJsonRoot, "arrFileSize", arrFileSize);
  1043. cJSON_AddItemToObject(pJsonRoot, "arrFtCreate", arrFtCreate);
  1044. cJSON_AddItemToObject(pJsonRoot, "arrftModified", arrftModified);
  1045. cJSON_AddItemToObject(pJsonRoot, "arrftAccess", arrftAccess);
  1046. cJSON_AddItemToObject(pJsonRoot, "arrFileAttribute", arrFileAttribute);
  1047. cJSON_AddItemToObject(pJsonRoot, "arrForbidAttributes", arrForbidAttributes);
  1048. cJSON_AddItemToObject(pJsonRoot, "arrReserved1", arrReserved1);
  1049. //cJSON_AddItemToObject(pJsonRoot, "arrReserved2", arrReserved2);
  1050. char *out = cJSON_Print(pJsonRoot);
  1051. cJSON_Delete(pJsonRoot);
  1052. return out;
  1053. }
  1054. ErrorCodeEnum CRemoteControllerCnn::HandleOperateDiskReq(const CSmartPointer<IPackage> &pRecvPkg)
  1055. {
  1056. Dbg("request operate disk");
  1057. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1058. int nLen = pRecvPkg->GetStructLen("OprDiskR");
  1059. if (nLen <= 0)
  1060. {
  1061. Dbg("Failed:[OprDiskR]");
  1062. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [OprDiskR] fail").c_str());
  1063. SendPackage(pReplyPkg);
  1064. return Error_Param;
  1065. }
  1066. assert(nLen == sizeof(RvcOperateDiskReq));
  1067. RvcOperateDiskReq req = {};
  1068. int nArrayLen(0);
  1069. pRecvPkg->GetStructData("OprDiskR", (BYTE*)&req, &nLen, &nArrayLen);
  1070. // 调用本地维护实体取控制信息
  1071. ResourceWatcherService_OperateFile_Req rvcReq;
  1072. ZeroMemory(&rvcReq, sizeof(ResourceWatcherService_OperateFile_Req));
  1073. {//初始化rvcReq
  1074. CSimpleStringA curTmp(req.current);
  1075. #if (defined _WIN32 || defined _WIN64)
  1076. rvcReq.current = curTmp;
  1077. #else
  1078. if (curTmp[0] == '/')
  1079. rvcReq.current = curTmp;
  1080. else
  1081. {
  1082. std::string tmpStr = curTmp.GetData();
  1083. std::replace(tmpStr.begin(), tmpStr.end(), '\\', '/');
  1084. rvcReq.current = CSimpleStringA("/") + tmpStr.c_str();
  1085. }
  1086. #endif
  1087. rvcReq.mode = req.mode;
  1088. rvcReq.attribute = req.attribute;
  1089. rvcReq.content = CSimpleStringA(req.content);
  1090. rvcReq.filter1 = req.filter1;
  1091. rvcReq.filter2 = CSimpleStringA(req.filter2);
  1092. rvcReq.attachment1 = req.attachment1;
  1093. rvcReq.attachment2 = CSimpleStringA(req.attachment2);
  1094. }
  1095. Dbg("current:%s, reqMode:%d, mode:%d, attribute:%d, content:%s", rvcReq.current.GetData(), req.mode, rvcReq.mode, rvcReq.attribute, rvcReq.content.GetData());
  1096. ResourceWatcherService_OperateFile_Ans rvcAns;
  1097. ZeroMemory(&rvcAns, sizeof(ResourceWatcherService_OperateFile_Ans));
  1098. ResourceWatcherService_ClientBase *pClient = new ResourceWatcherService_ClientBase(m_pEntity);
  1099. RvcOperateDiskRet2 *pRet2 = NULL;
  1100. auto rc = pClient->Connect();
  1101. if (rc == Error_Succeed)
  1102. {
  1103. rc = pClient->OperateFile(rvcReq, rvcAns, 60000);
  1104. pClient->GetFunction()->CloseSession();
  1105. }
  1106. if (rc != Error_Succeed)
  1107. {
  1108. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[ResourceWatcher]实体失败").c_str());
  1109. LogError(Severity_Low, rc, 0, "调用[ResourceWatcher]实体失败");
  1110. }
  1111. else
  1112. {
  1113. Dbg("获取磁盘目录信息成功");
  1114. //添加首层目录信息
  1115. RvcOperateDiskRet1 ret1(req.TaskID, rvcAns.result, rvcAns.header, rvcAns.attachment1);
  1116. RvcOperateDiskRet2 current(rvcAns.attachment2, rvcAns.fileSize, rvcAns.ftCreate, rvcAns.ftModified,
  1117. rvcAns.ftAccess, rvcAns.fileAttribute, rvcAns.forbidAttribute, rvcAns.reversed1, rvcAns.reversed2);
  1118. //添加目录具体文件信息
  1119. pRet2 = new RvcOperateDiskRet2[rvcAns.count + 1];
  1120. ZeroMemory(pRet2, sizeof(RvcOperateDiskRet2) * (rvcAns.count + 1));
  1121. memcpy(&(pRet2[0]), &current, sizeof(RvcOperateDiskRet2));
  1122. //Dbg("size:%d", rvcAns.count);
  1123. for (int i = 0; i < rvcAns.count; i++)
  1124. {
  1125. RvcOperateDiskRet2 tempRet(rvcAns.fileNames[i], rvcAns.fileSizes[i], rvcAns.ftCreates[i], rvcAns.ftModifieds[i],
  1126. rvcAns.ftAccesses[i], rvcAns.fileAttributes[i], rvcAns.forbidAttributes[i],
  1127. (rvcAns.reserved1.GetCount() == rvcAns.count ? rvcAns.reserved1[i] : 0), (rvcAns.reserved2.GetCount() == rvcAns.count ? rvcAns.reserved2[i] : ""));
  1128. memcpy(&(pRet2[i + 1]), &tempRet, sizeof(RvcOperateDiskRet2));
  1129. }
  1130. char *outStr = makeOperateDiskJson(&ret1, pRet2, rvcAns.count + 1);
  1131. char *slimJsonStr = new char[strlen(outStr)];
  1132. ZeroMemory(slimJsonStr, strlen(outStr));
  1133. int pos = 0;
  1134. for (int i = 0; i < strlen(outStr); i++)
  1135. {
  1136. if ('\t' != outStr[i] && '\n' != outStr[i])
  1137. {
  1138. slimJsonStr[pos] = outStr[i];
  1139. pos++;
  1140. }
  1141. }
  1142. slimJsonStr[pos] = '\0';
  1143. long long nRetLen = sizeof(RvcOperateDiskJsonRet) + 60 * 1024;
  1144. char *pBuf = new char[nRetLen];
  1145. ZeroMemory(pBuf, nRetLen);
  1146. RvcOperateDiskJsonRet *pRet = (RvcOperateDiskJsonRet*)pBuf;
  1147. pRet->TaskID = req.TaskID;
  1148. auto pFunc = m_pEntity->GetFunction();
  1149. CSimpleStringA jsonFileName, tempDir, zipJsonName;
  1150. pFunc->GetPath("Temp", tempDir);
  1151. jsonFileName.Append(tempDir).Append(SPLIT_SLASH_STR).Append("transfer.txt");
  1152. zipJsonName.Append(tempDir).Append(SPLIT_SLASH_STR).Append("zipJson.zip");
  1153. ofstream jsonFile(jsonFileName.GetData());
  1154. if (jsonFile.is_open())
  1155. {
  1156. Dbg("jsonLength:%d", pRet->jsonLength);
  1157. auto gbkStr = SP::Utility::UTF8ToGBK(slimJsonStr);
  1158. jsonFile << gbkStr << endl;
  1159. jsonFile.close();
  1160. }
  1161. if (Error_Succeed != (rc = ZipFile(zipJsonName, jsonFileName)))
  1162. {
  1163. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)zipJsonName).GetData()).c_str());
  1164. SendPackage(pReplyPkg);
  1165. return rc;
  1166. }
  1167. std::ifstream t;
  1168. t.open(zipJsonName, std::ios_base::binary | std::ios_base::in | std::ios_base::out); // open input file
  1169. t.seekg(0, std::ios::end); // go to the end
  1170. pRet->jsonLength = t.tellg(); // report location (this is the length)
  1171. t.seekg(0, std::ios::beg); // go back to the beginning
  1172. t.read(pRet->jsonData, pRet->jsonLength); // read the whole file into the buffer
  1173. t.close(); // close file handle
  1174. pReplyPkg->AddStruct("OprDkJA", false, false, (BYTE*)pRet, nRetLen);
  1175. delete []slimJsonStr;
  1176. Base_DeleteFile(jsonFileName.GetData());
  1177. Base_DeleteFile(zipJsonName.GetData());
  1178. }
  1179. SendPackage(pReplyPkg);
  1180. return Error_Succeed;
  1181. }
  1182. ErrorCodeEnum CRemoteControllerCnn::HandleEnterMaintainModeReq(const CSmartPointer<IPackage> &pRecvPkg)
  1183. {
  1184. Dbg("request enter maintain mode");
  1185. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1186. int nLen = pRecvPkg->GetStructLen("EnterMMR");
  1187. if (nLen <=0)
  1188. {
  1189. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EnterMMR] fail").c_str());
  1190. SendPackage(pReplyPkg);
  1191. return Error_Param;
  1192. }
  1193. assert(nLen == sizeof(RvcEnterMaintainModeReq));
  1194. RvcEnterMaintainModeReq req = {};
  1195. int nArrayLen(0);
  1196. pRecvPkg->GetStructData("EnterMMR", (BYTE*)&req, &nLen, &nArrayLen);
  1197. RvcEnterMaintainModeRet ret = {};
  1198. ret.TaskID = req.TaskID;
  1199. pReplyPkg->AddStruct("EnterMMA", false, false, (BYTE*)&ret, sizeof(ret));
  1200. // 检查当前状态
  1201. auto pFunc = m_pEntity->GetFunction();
  1202. CSimpleStringA strCustomerHandleVal;
  1203. pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
  1204. CSimpleStringA strCustomerBeingVal;
  1205. pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
  1206. if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
  1207. {
  1208. Dbg("remote enter maintain mode fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
  1209. , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
  1210. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法暂停服务").c_str());
  1211. SendPackage(pReplyPkg);
  1212. return Error_Cancel;
  1213. }
  1214. CSimpleStringA strValue = "";
  1215. pFunc->GetSysVar("LocalMaintain", strValue);
  1216. if (strValue.Compare("N") != 0)
  1217. {
  1218. Dbg("remote enter maintain mode fail for var [LocalMaintain] == '%s'", (const char*)strValue);
  1219. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法暂停服务").c_str());
  1220. SendPackage(pReplyPkg);
  1221. return Error_Cancel;
  1222. }
  1223. // 调用健康实体进入维护模式
  1224. HealthManagerService_ClientBase *pClient = new HealthManagerService_ClientBase(m_pEntity);
  1225. auto rc = pClient->Connect();
  1226. if (rc == Error_Succeed)
  1227. {
  1228. HealthManagerService_EnterState_Req req = {};
  1229. req.state = "M";
  1230. HealthManagerService_EnterState_Ans ans = {};
  1231. rc = pClient->EnterState(req, ans, 10000);
  1232. pClient->GetFunction()->CloseSession();
  1233. }
  1234. else {
  1235. pClient->SafeDelete();
  1236. }
  1237. if (rc != Error_Succeed)
  1238. {
  1239. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用健康实体进入维护模式失败").c_str());
  1240. LogError(Severity_Low, rc, 0, "调用健康失败进入维护模式失败");
  1241. }
  1242. SendPackage(pReplyPkg);
  1243. return Error_Succeed;
  1244. }
  1245. ErrorCodeEnum CRemoteControllerCnn::HandleExitMaintainModeReq(const CSmartPointer<IPackage> &pRecvPkg)
  1246. {
  1247. Dbg("request exit maintain mode");
  1248. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1249. int nLen = pRecvPkg->GetStructLen("ExitMMR");
  1250. if (nLen <=0)
  1251. {
  1252. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ExitMMR] fail").c_str());
  1253. SendPackage(pReplyPkg);
  1254. return Error_Param;
  1255. }
  1256. assert(nLen == sizeof(RvcExitMaintainModeReq));
  1257. RvcExitMaintainModeReq req = {};
  1258. int nArrayLen(0);
  1259. pRecvPkg->GetStructData("ExitMMR", (BYTE*)&req, &nLen, &nArrayLen);
  1260. RvcExitMaintainModeRet ret = {};
  1261. ret.TaskID = req.TaskID;
  1262. pReplyPkg->AddStruct("ExitMMA", false, false, (BYTE*)&ret, sizeof(ret));
  1263. HealthManagerService_ClientBase *pClient = new HealthManagerService_ClientBase(m_pEntity);
  1264. auto rc = pClient->Connect();
  1265. if (rc == Error_Succeed)
  1266. {
  1267. HealthManagerService_ExitState_Req req = {};
  1268. req.state = "M";
  1269. HealthManagerService_ExitState_Ans ans = {};
  1270. rc = pClient->ExitState(req, ans, 10000);
  1271. pClient->GetFunction()->CloseSession();
  1272. } else {
  1273. pClient->SafeDelete();
  1274. }
  1275. if (rc != Error_Succeed)
  1276. {
  1277. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用健康实体退出维护模式失败").c_str());
  1278. LogError(Severity_Low, rc, 0, "调用健康失败退出维护模式失败");
  1279. }
  1280. SendPackage(pReplyPkg);
  1281. return Error_Succeed;
  1282. }
  1283. ErrorCodeEnum CRemoteControllerCnn::HandleUpgradePackReq(const CSmartPointer<IPackage> &pRecvPkg)
  1284. {
  1285. Dbg("request remote upgarde pack");
  1286. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1287. int nLen = pRecvPkg->GetStructLen("UpgPackR");
  1288. if (nLen <=0)
  1289. {
  1290. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [UpgPackR] fail").c_str());
  1291. SendPackage(pReplyPkg);
  1292. return Error_Param;
  1293. }
  1294. assert(nLen == sizeof(RvcUpgradePackReq));
  1295. RvcUpgradePackReq req = {};
  1296. int nArrayLen(0);
  1297. pRecvPkg->GetStructData("UpgPackR", (BYTE*)&req, &nLen, &nArrayLen);
  1298. RvcUpgradePackRet ret = {};
  1299. ret.TaskID = req.TaskID;
  1300. pReplyPkg->AddStruct("UpgPackA", false, false, (BYTE*)&ret, sizeof(ret));
  1301. CSimpleStringA strPack = req.PackName;
  1302. Dbg("begin upgrade pack: %s", (const char*)strPack);
  1303. // 调用升级实体接口升级
  1304. UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
  1305. auto rc = pClient->Connect();
  1306. if (rc == Error_Succeed)
  1307. {
  1308. UpgradeMgrService_RegistLocalPack_Req req = {};
  1309. req.strPackFile = strPack;
  1310. UpgradeMgrService_RegistLocalPack_Ans ans = {};
  1311. rc = pClient->RegistLocalPack(req, ans, 10000);
  1312. pClient->GetFunction()->CloseSession();
  1313. } else {
  1314. pClient->SafeDelete();
  1315. }
  1316. if (rc != Error_Succeed)
  1317. {
  1318. if (rc == Error_NotExist)
  1319. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包文件不存在").c_str());
  1320. else if (rc == Error_AlreadyExist)
  1321. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包已安装").c_str());
  1322. else if (rc == Error_Cancel)
  1323. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包已被取消安装").c_str());
  1324. else
  1325. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("远程执行升级失败").c_str());
  1326. Dbg("remote regist upgrade pack fail: 0x%X", rc);
  1327. }
  1328. else
  1329. {
  1330. Dbg("remote regist upgrade pack succeed!");
  1331. }
  1332. SendPackage(pReplyPkg);
  1333. return Error_Succeed;
  1334. }
  1335. ErrorCodeEnum CRemoteControllerCnn::HandleRollbackUpgradeReq(const CSmartPointer<IPackage> &pRecvPkg)
  1336. {
  1337. Dbg("request remote rollback upgarde");
  1338. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1339. int nLen = pRecvPkg->GetStructLen("RollbakR");
  1340. if (nLen <= 0)
  1341. {
  1342. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [RollbakR] fail").c_str());
  1343. SendPackage(pReplyPkg);
  1344. return Error_Param;
  1345. }
  1346. assert(nLen == sizeof(RvcRollbackReq));
  1347. RvcRollbackReq req = {};
  1348. int nArrayLen(0);
  1349. pRecvPkg->GetStructData("RollbakR", (BYTE*)&req, &nLen, &nArrayLen);
  1350. RvcRollbackRet ret = {};
  1351. ret.TaskID = req.TaskID;
  1352. pReplyPkg->AddStruct("RollbakA", false, false, (BYTE*)&ret, sizeof(ret));
  1353. // 调用升级实体接口回滚
  1354. UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
  1355. auto rc = pClient->Connect();
  1356. if (rc == Error_Succeed)
  1357. {
  1358. UpgradeMgrService_RollbackUpdate_Req req = {};
  1359. UpgradeMgrService_RollbackUpdate_Ans ans = {};
  1360. rc = pClient->RollbackUpdate(req, ans, 10000);
  1361. pClient->GetFunction()->CloseSession();
  1362. } else {
  1363. pClient->SafeDelete();
  1364. }
  1365. if (rc != Error_Succeed)
  1366. {
  1367. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用升级实体回滚升级失败").c_str());
  1368. LogError(Severity_Low, rc, 0, "调用升级实体回滚升级失败");
  1369. }
  1370. else
  1371. {
  1372. Dbg("remote rollback upgrade succeed!");
  1373. }
  1374. SendPackage(pReplyPkg);
  1375. return Error_Succeed;
  1376. }
  1377. ErrorCodeEnum CRemoteControllerCnn::HandleGetCurMaintainerReq(const CSmartPointer<IPackage> &pRecvPkg)
  1378. {
  1379. Dbg("request get maintainer info");
  1380. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1381. int nLen = pRecvPkg->GetStructLen("GetUserR");
  1382. if (nLen <=0)
  1383. {
  1384. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [GetUserR] fail").c_str());
  1385. SendPackage(pReplyPkg);
  1386. return Error_Param;
  1387. }
  1388. assert(nLen == sizeof(RvcGetUserReq));
  1389. RvcGetUserReq req = {};
  1390. int nArrayLen(0);
  1391. pRecvPkg->GetStructData("GetUserR", (BYTE*)&req, &nLen, &nArrayLen);
  1392. RvcGetUserRet ret = {};
  1393. ret.TaskID = req.TaskID;
  1394. // 调用本地维护实体取控制信息
  1395. GUIConsoleService_GetCurrentMaintainer_Req req1 = {};
  1396. GUIConsoleService_GetCurrentMaintainer_Ans ans1 = {};
  1397. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1398. auto rc = pClient->Connect();
  1399. if (rc == Error_Succeed)
  1400. {
  1401. rc = pClient->GetCurrentMaintainer(req1, ans1, 10000);
  1402. pClient->GetFunction()->CloseSession();
  1403. } else {
  1404. pClient->SafeDelete();
  1405. }
  1406. if (rc != Error_Succeed)
  1407. {
  1408. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1409. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1410. }
  1411. else
  1412. {
  1413. Dbg("获取运维人员信息成功");
  1414. strncpy_s(ret.UserID, sizeof(ret.UserID), ans1.strUserID, _TRUNCATE);
  1415. strncpy_s(ret.UserName, sizeof(ret.UserName), ans1.strUserName, _TRUNCATE);
  1416. strncpy_s(ret.CurRight, sizeof(ret.CurRight), ans1.strCurRight, _TRUNCATE);
  1417. strncpy_s(ret.Authorizer, sizeof(ret.Authorizer), ans1.strAuthorizer, _TRUNCATE);
  1418. ret.BeginTime = ans1.dwBeginTime;
  1419. pReplyPkg->AddStruct("GetUserA", false, false, (BYTE*)&ret, sizeof(ret));
  1420. }
  1421. SendPackage(pReplyPkg);
  1422. return Error_Succeed;
  1423. }
  1424. ErrorCodeEnum CRemoteControllerCnn::HandleEnpowerMaintainerReq(const CSmartPointer<IPackage> &pRecvPkg)
  1425. {
  1426. Dbg("request enpower maintainer");
  1427. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1428. int nLen = pRecvPkg->GetStructLen("EnpowerR");
  1429. if (nLen <=0)
  1430. {
  1431. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EnpowerR] fail").c_str());
  1432. SendPackage(pReplyPkg);
  1433. return Error_Param;
  1434. }
  1435. assert(nLen == sizeof(RvcEnpowerUserReq));
  1436. RvcEnpowerUserReq req = {};
  1437. int nArrayLen(0);
  1438. pRecvPkg->GetStructData("EnpowerR", (BYTE*)&req, &nLen, &nArrayLen);
  1439. RvcEnpowerUserRet ret = {};
  1440. ret.TaskID = req.TaskID;
  1441. // 调用本地维护实体取控制信息
  1442. GUIConsoleService_Empower_Req req1 = {};
  1443. GUIConsoleService_Empower_Ans ans1 = {};
  1444. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1445. auto rc = pClient->Connect();
  1446. if (rc == Error_Succeed)
  1447. {
  1448. req1.strUserID = req.UserID;
  1449. rc = pClient->Empower(req1, ans1, 10000);
  1450. pClient->GetFunction()->CloseSession();
  1451. } else {
  1452. pClient->SafeDelete();
  1453. }
  1454. if (rc != Error_Succeed)
  1455. {
  1456. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1457. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1458. }
  1459. else
  1460. {
  1461. Dbg("远程维护授权成功");
  1462. pReplyPkg->AddStruct("EnpowerA", false, false, (BYTE*)&ret, sizeof(ret));
  1463. }
  1464. SendPackage(pReplyPkg);
  1465. return Error_Succeed;
  1466. }
  1467. ErrorCodeEnum CRemoteControllerCnn::HandleTakeoverPowerReq(const CSmartPointer<IPackage> &pRecvPkg)
  1468. {
  1469. Dbg("request takeover maintainer power");
  1470. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1471. int nLen = pRecvPkg->GetStructLen("TakeovrR");
  1472. if (nLen <=0)
  1473. {
  1474. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [TakeovrR] fail").c_str());
  1475. SendPackage(pReplyPkg);
  1476. return Error_Param;
  1477. }
  1478. assert(nLen == sizeof(RvcTakeoverPowerReq));
  1479. RvcTakeoverPowerReq req = {};
  1480. int nArrayLen(0);
  1481. pRecvPkg->GetStructData("TakeovrR", (BYTE*)&req, &nLen, &nArrayLen);
  1482. RvcTakeoverPowerRet ret = {};
  1483. ret.TaskID = req.TaskID;
  1484. // 调用本地维护实体取控制信息
  1485. GUIConsoleService_Takeover_Req req1 = {};
  1486. GUIConsoleService_Takeover_Ans ans1 = {};
  1487. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1488. auto rc = pClient->Connect();
  1489. if (rc == Error_Succeed)
  1490. {
  1491. req1.strUserID = req.UserID;
  1492. rc = pClient->Takeover(req1, ans1, 10000);
  1493. pClient->GetFunction()->CloseSession();
  1494. } else {
  1495. pClient->SafeDelete();
  1496. }
  1497. if (rc != Error_Succeed)
  1498. {
  1499. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1500. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1501. }
  1502. else
  1503. {
  1504. Dbg("远程解除高级维护权限成功");
  1505. pReplyPkg->AddStruct("TakeovrA", false, false, (BYTE*)&ret, sizeof(ret));
  1506. }
  1507. SendPackage(pReplyPkg);
  1508. return Error_Succeed;
  1509. }
  1510. ErrorCodeEnum CRemoteControllerCnn::HandleForceQuitMaintainReq(const CSmartPointer<IPackage> &pRecvPkg)
  1511. {
  1512. Dbg("request force quit maintain");
  1513. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1514. int nLen = pRecvPkg->GetStructLen("ForceQtR");
  1515. if (nLen <=0)
  1516. {
  1517. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ForceQtR] fail").c_str());
  1518. SendPackage(pReplyPkg);
  1519. return Error_Param;
  1520. }
  1521. assert(nLen == sizeof(RvcForceQuitReq));
  1522. RvcForceQuitReq req = {};
  1523. int nArrayLen(0);
  1524. pRecvPkg->GetStructData("ForceQtR", (BYTE*)&req, &nLen, &nArrayLen);
  1525. RvcForceQuitRet ret = {};
  1526. ret.TaskID = req.TaskID;
  1527. // 调用本地维护实体
  1528. GUIConsoleService_ForceQuit_Req req1 = {};
  1529. GUIConsoleService_ForceQuit_Ans ans1 = {};
  1530. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1531. auto rc = pClient->Connect();
  1532. if (rc == Error_Succeed)
  1533. {
  1534. rc = pClient->ForceQuit(req1, ans1, 10000);
  1535. pClient->GetFunction()->CloseSession();
  1536. } else {
  1537. pClient->SafeDelete();
  1538. }
  1539. if (rc != Error_Succeed)
  1540. {
  1541. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1542. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1543. }
  1544. else
  1545. {
  1546. Dbg("远程强制退出维护状态成功");
  1547. pReplyPkg->AddStruct("ForceQtA", false, false, (BYTE*)&ret, sizeof(ret));
  1548. }
  1549. SendPackage(pReplyPkg);
  1550. return Error_Succeed;
  1551. }
  1552. ErrorCodeEnum CRemoteControllerCnn::EnterMonitorMode()
  1553. {
  1554. m_chRemoteMode = 'O';
  1555. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterMonitor));
  1556. Dbg("enter monitor mode succeed");
  1557. return Error_Succeed;
  1558. }
  1559. ErrorCodeEnum CRemoteControllerCnn::EnterMaintainMode(bool bLocalMaintain)
  1560. {
  1561. // 是否远程维护
  1562. if (bLocalMaintain)
  1563. {
  1564. // 弹出本地控制台
  1565. Dbg("show local maintain console");
  1566. }
  1567. else
  1568. {
  1569. // 远程维护 显示维护图片 遮挡前端操作界面
  1570. Dbg("show remote maintain picture");
  1571. }
  1572. m_chRemoteMode = bLocalMaintain ? 'L' : 'R';
  1573. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterMaintain));
  1574. Dbg("enter maintain mode succeed");
  1575. return Error_Succeed;
  1576. }
  1577. ErrorCodeEnum CRemoteControllerCnn::EnterTerminalMode(int nRemotePort)
  1578. {
  1579. // 检测本机远程桌面是否打开
  1580. // 检测远程用户是否创建
  1581. // 连接分行桥接服务端口
  1582. // 重定向此连接至3389端口
  1583. m_chRemoteMode = 'T';
  1584. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterTerminal));
  1585. Dbg("enter terminal mode succeed");
  1586. return Error_Succeed;
  1587. }
  1588. ErrorCodeEnum CRemoteControllerCnn::HandleGetVersionReq(const CSmartPointer<IPackage> &pRecvPkg)
  1589. {
  1590. Dbg("request get active version");
  1591. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1592. int nLen = pRecvPkg->GetStructLen("GetVerR");
  1593. if (nLen <= 0)
  1594. {
  1595. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [GetVerR] fail").c_str());
  1596. SendPackage(pReplyPkg);
  1597. return Error_Param;
  1598. }
  1599. assert(nLen == sizeof(RvcGetActiveVerReq));
  1600. RvcGetActiveVerReq req = {};
  1601. int nArrayLen(0);
  1602. pRecvPkg->GetStructData("GetVerR", (BYTE*)&req, &nLen, &nArrayLen);
  1603. RvcGetActiveVerRet ret = {};
  1604. ret.TaskID = req.TaskID;
  1605. CSimpleStringA strPath;
  1606. auto rc = m_pEntity->GetFunction()->GetPath("RootVer", strPath);
  1607. if (rc == Error_Succeed)
  1608. {
  1609. strPath.Append(SPLIT_SLASH_STR).Append("active.txt");
  1610. FILE *pFile = fopen(strPath, "r");
  1611. if (pFile != NULL)
  1612. {
  1613. char szTemp[256] = {};
  1614. int n = fread(szTemp, 1, 256, pFile);
  1615. strncpy_s(ret.Version, sizeof(ret.Version), szTemp, _TRUNCATE);
  1616. fclose(pFile);
  1617. pReplyPkg->AddStruct("GetVerA", false, false, (BYTE*)&ret, sizeof(ret));
  1618. }
  1619. else
  1620. {
  1621. Dbg("open active.txt fail");
  1622. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("open active.txt fail").c_str());
  1623. }
  1624. }
  1625. else
  1626. {
  1627. Dbg("get root ver path fail");
  1628. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK("get root ver path fail").c_str());
  1629. }
  1630. SendPackage(pReplyPkg);
  1631. return Error_Succeed;
  1632. }
  1633. ErrorCodeEnum CRemoteControllerCnn::HandleViewRunCfgReq(const CSmartPointer<IPackage> &pRecvPkg)
  1634. {
  1635. Dbg("remote view runcfg");
  1636. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1637. int nLen = pRecvPkg->GetStructLen("VRunCfgR");
  1638. if (nLen <= 0)
  1639. {
  1640. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [VRunCfgR] fail").c_str());
  1641. SendPackage(pReplyPkg);
  1642. return Error_Param;
  1643. }
  1644. assert(nLen == sizeof(RvcViewRunCfgReq));
  1645. RvcViewRunCfgReq req = {};
  1646. int nArrayLen(0);
  1647. pRecvPkg->GetStructData("VRunCfgR", (BYTE*)&req, &nLen, &nArrayLen);
  1648. // 读取实体配置文件
  1649. CSimpleStringA strConfigName = req.ConfigName;
  1650. CSimpleStringA strCfgPath;
  1651. if (Error_Succeed != m_pEntity->GetFunction()->GetPath("RunInfo", strCfgPath)) {
  1652. Dbg("Get RunInfo path failed");
  1653. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置不存在").c_str());
  1654. SendPackage(pReplyPkg);
  1655. return Error_NotExist;
  1656. }
  1657. strCfgPath.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(strConfigName);
  1658. if (!Base_Exist(strCfgPath.GetData()))
  1659. {
  1660. Dbg("runcfg [%s] not exist", (const char*)strCfgPath);
  1661. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置不存在").c_str());
  1662. SendPackage(pReplyPkg);
  1663. return Error_NotExist;
  1664. }
  1665. auto cfgSize = Base_filesize(strCfgPath.GetData());
  1666. char* tmpBuf = new char[cfgSize];
  1667. memset(tmpBuf, 0, cfgSize);
  1668. auto readRet = Base_readFile(strCfgPath.GetData(), 0, tmpBuf, cfgSize);
  1669. if (!readRet.first)
  1670. {
  1671. pReplyPkg->SetErrMsg(0, 0, SP::Utility::UTF8ToGBK("open runcfg fail").c_str());
  1672. Dbg("open file [%s] fail", (const char*)strCfgPath);
  1673. }
  1674. else
  1675. {
  1676. int nRetLen = sizeof(RvcViewRunCfgRet) + cfgSize * 2;
  1677. char* pBuf = new char[nRetLen];
  1678. memset(pBuf, 0, nRetLen);
  1679. RvcViewRunCfgRet* pRet = (RvcViewRunCfgRet*)pBuf;
  1680. int dstPos = 0;
  1681. for (auto i = 0; i < cfgSize; i++)
  1682. {
  1683. if ((i == 0 && tmpBuf[i] == '\n') || (tmpBuf[i] == '\n' && tmpBuf[i - 1] != '\r'))
  1684. pRet->ConfigData[dstPos++] = '\r';
  1685. pRet->ConfigData[dstPos++] = tmpBuf[i];
  1686. }
  1687. pReplyPkg->AddStruct("VRunCfgA", false, false, (BYTE*)pBuf, dstPos + sizeof(RvcViewConfigRet));
  1688. }
  1689. SendPackage(pReplyPkg);
  1690. return Error_Succeed;
  1691. }
  1692. ErrorCodeEnum CRemoteControllerCnn::HandleViewInstallLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  1693. {
  1694. Dbg("remote view install log");
  1695. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1696. int nLen = pRecvPkg->GetStructLen("VPkgLogR");
  1697. if (nLen <= 0)
  1698. {
  1699. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [VPkgLogR] fail").c_str());
  1700. SendPackage(pReplyPkg);
  1701. return Error_Param;
  1702. }
  1703. assert(nLen == sizeof(RvcViewInstallLogReq));
  1704. RvcViewInstallLogReq req = {};
  1705. int nArrayLen(0);
  1706. pRecvPkg->GetStructData("VPkgLogR", (BYTE*)&req, &nLen, &nArrayLen);
  1707. CSimpleStringA strEntityName = req.InstallLog;
  1708. // 读取升级日志文件
  1709. CSimpleStringA strLogFile = req.InstallLog;
  1710. CSimpleStringA strLogPath;
  1711. auto rc = m_pEntity->GetFunction()->GetPath("RunInfo", strLogPath);
  1712. assert(rc == Error_Succeed);
  1713. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strLogFile);
  1714. if (!Base_Exist(strLogPath.GetData()))
  1715. {
  1716. Dbg("install log [%s] not exist", (const char*)strLogPath);
  1717. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
  1718. SendPackage(pReplyPkg);
  1719. return Error_NotExist;
  1720. }
  1721. auto file = fopen(strLogPath, "rb");
  1722. if (file == NULL)
  1723. {
  1724. auto nError = errno;
  1725. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("open install log [%s] fail(%d)", (const char*)strLogPath, nError).GetData()).c_str());
  1726. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open install log [%s] fail(%d)", (const char*)strLogPath, nError));
  1727. SendPackage(pReplyPkg);
  1728. return rc;
  1729. }
  1730. generateFilesize(file, strLogPath);
  1731. long long nRetLen = sizeof(RvcViewInstallLogRet)+nFileSize;
  1732. char *pBuf = new char[nRetLen];
  1733. memset(pBuf, 0, nRetLen);
  1734. RvcViewInstallLogRet *pRet = (RvcViewInstallLogRet*)pBuf;
  1735. pRet->TaskID = req.TaskID;
  1736. char *pLogData = (char*)pRet->LogData;
  1737. long long nHasRead(0);
  1738. DWORD nReadLen(0);
  1739. do
  1740. {
  1741. //Dbg("ftell: %d", ftell(file));
  1742. nReadLen = fread(pLogData + nHasRead, 1, nFileSize - nHasRead, file);
  1743. //Dbg("ftell: %d", ftell(file));
  1744. if (nReadLen >0)
  1745. nHasRead += nReadLen;
  1746. } while (nReadLen >0 && nHasRead < nFileSize);
  1747. fclose(file);
  1748. std::string gbkLogStr = SP::Utility::UTF8ToGBK(pLogData);
  1749. int gbkLen = sizeof(RvcViewInstallLogRet) + gbkLogStr.length();
  1750. char* gbkBuf = new char[gbkLen];
  1751. memset(gbkBuf, 0, gbkLen);
  1752. RvcViewInstallLogRet* gbkRet = (RvcViewInstallLogRet*)gbkBuf;
  1753. gbkRet->TaskID = req.TaskID;
  1754. memcpy(gbkRet->LogData, gbkLogStr.c_str(), gbkLogStr.length());
  1755. pReplyPkg->AddStruct("VPkgLogA", false, false, (BYTE*)gbkRet, gbkLen);
  1756. delete[] pBuf;
  1757. delete[] gbkBuf;
  1758. SendPackage(pReplyPkg);
  1759. return Error_Succeed;
  1760. }
  1761. ErrorCodeEnum CRemoteControllerCnn::ZipFile(LPCTSTR lpszZipArchive, LPCTSTR lpszSrcFile)
  1762. {
  1763. BOOL bResult = TRUE;
  1764. assert(lpszZipArchive);
  1765. assert(lpszZipArchive[0] != ('\0'));
  1766. if (!lpszZipArchive || lpszZipArchive[0] == ('\0'))
  1767. return Error_Param;
  1768. assert(lpszSrcFile);
  1769. assert(lpszSrcFile[0] != ('\0'));
  1770. if (!lpszSrcFile || lpszSrcFile[0] == ('\0'))
  1771. return Error_Param;
  1772. // does zip source file exist?
  1773. if (!Base_Exist(lpszSrcFile))
  1774. {
  1775. Dbg("source file '%s' cannot be found", lpszSrcFile);
  1776. return Error_NotExist;
  1777. }
  1778. // does zip file exist?
  1779. if (Base_Exist(lpszZipArchive))
  1780. {
  1781. Dbg("dest zip file '%s' exists, delete it!", lpszZipArchive);
  1782. Base_DeleteFile(lpszZipArchive);
  1783. }
  1784. // 先拷贝源文件(防止文件被占用导致压缩失败)
  1785. CSimpleStringA strCopyFile = lpszSrcFile;
  1786. strCopyFile += ".bak";
  1787. Base_CopyFile(lpszSrcFile, strCopyFile.GetData());
  1788. // use only the file name for zip file entry
  1789. TCHAR * cp = (TCHAR *)strrchr(lpszSrcFile, SPLIT_SLASH);
  1790. if (cp == NULL)
  1791. cp = (TCHAR *)lpszSrcFile;
  1792. else
  1793. cp++;
  1794. #if (defined _WIN32 || defined _WIN64)
  1795. HZIP hz = CreateZip((void *)lpszZipArchive, 0, ZIP_FILENAME);
  1796. #else
  1797. zipFile hz = zipOpen(lpszZipArchive, APPEND_STATUS_CREATE); //创建zip文件
  1798. #endif
  1799. if (hz)
  1800. {
  1801. #if (defined _WIN32 || defined _WIN64)
  1802. ZRESULT zr = ZipAdd(hz, cp, (void *)(const char*)strCopyFile, 0, ZIP_FILENAME);
  1803. CloseZip(hz);
  1804. if (zr == ZR_OK)
  1805. #else
  1806. auto zr = AddFileToZip(hz, cp, strCopyFile.GetData());
  1807. zipClose(hz, NULL);
  1808. if (zr)
  1809. #endif
  1810. {
  1811. Dbg("added '%s' to zip file '%s' succeed", lpszSrcFile, lpszZipArchive);
  1812. bResult = TRUE;
  1813. }
  1814. else
  1815. {
  1816. Dbg("failed to add zip source file '%s'", lpszSrcFile);
  1817. bResult = FALSE;
  1818. }
  1819. }
  1820. else
  1821. {
  1822. Dbg("failed to create zip file '%s'", lpszZipArchive);
  1823. bResult = FALSE;
  1824. }
  1825. if (Base_Exist(strCopyFile.GetData()))
  1826. Base_DeleteFile(strCopyFile.GetData());
  1827. if (!bResult && Base_Exist(lpszZipArchive))
  1828. Base_DeleteFile(lpszZipArchive);
  1829. return bResult ? Error_Succeed : Error_Unexpect;
  1830. }
  1831. ErrorCodeEnum CRemoteControllerCnn::BatchZipFiles(LPCTSTR lpszZipArchive, const list<string> &listSrcFiles)
  1832. {
  1833. BOOL bResult = TRUE;
  1834. if (!lpszZipArchive || lpszZipArchive[0] == ('\0'))
  1835. return Error_Param;
  1836. // does zip file exist?
  1837. if (Base_Exist(lpszZipArchive))
  1838. {
  1839. Dbg("dest zip file '%s' exists, delete it!", lpszZipArchive);
  1840. Base_DeleteFile(lpszZipArchive);
  1841. }
  1842. #if (defined _WIN32 || defined _WIN64)
  1843. HZIP hz = CreateZip((void*)lpszZipArchive, 0, ZIP_FILENAME);
  1844. #else
  1845. zipFile hz = zipOpen(lpszZipArchive, APPEND_STATUS_CREATE); //创建zip文件
  1846. #endif
  1847. if (!hz)
  1848. {
  1849. Dbg("failed to create zip file '%s'", lpszZipArchive);
  1850. return Error_Unexpect;
  1851. }
  1852. // does zip source file exist?
  1853. for (auto it = listSrcFiles.begin(); it != listSrcFiles.end(); it++)
  1854. {
  1855. if (!Base_Exist(it->c_str()))
  1856. {
  1857. Dbg("source file '%s' cannot be found", it->c_str());
  1858. bResult = FALSE;
  1859. break;
  1860. }
  1861. // 先拷贝源文件(防止文件被占用导致压缩失败)
  1862. CSimpleStringA strSrcFile = it->c_str();
  1863. CSimpleStringA strCopyFile = strSrcFile;
  1864. strCopyFile += ".bak";
  1865. Base_CopyFile(strSrcFile.GetData(), strCopyFile.GetData());
  1866. // use only the file name for zip file entry
  1867. CSimpleStringA strTmp = strSrcFile;
  1868. const TCHAR * cp = strrchr(strTmp, SPLIT_SLASH);
  1869. if (cp != NULL)
  1870. {
  1871. *((char*)cp) = '_';
  1872. cp = (TCHAR *)strrchr(strTmp, SPLIT_SLASH);
  1873. }
  1874. if (cp != NULL)
  1875. cp++;
  1876. else
  1877. cp = strTmp;
  1878. #if (defined _WIN32 || defined _WIN64)
  1879. ZRESULT zr = ZipAdd(hz, cp, (void*)(const char*)strCopyFile, 0, ZIP_FILENAME);
  1880. CloseZip(hz);
  1881. #else
  1882. auto zr = AddFileToZip(hz, cp, strCopyFile.GetData());
  1883. #endif
  1884. // delete .bak file
  1885. if (Base_Exist(strCopyFile.GetData()))
  1886. Base_DeleteFile(strCopyFile.GetData());
  1887. // did add work?
  1888. #if (defined _WIN32 || defined _WIN64)
  1889. if (zr == ZR_OK)
  1890. #else
  1891. if(zr)
  1892. #endif
  1893. {
  1894. Dbg("added '%s' to zip file '%s' succeed", cp, lpszZipArchive);
  1895. }
  1896. else
  1897. {
  1898. Dbg("failed to add zip source file '%s'", cp);
  1899. bResult = FALSE;
  1900. break;
  1901. }
  1902. }
  1903. if (!bResult && Base_Exist(lpszZipArchive))
  1904. {
  1905. Base_DeleteFile(lpszZipArchive);
  1906. }
  1907. zipClose(hz, NULL);
  1908. return bResult ? Error_Succeed : Error_Unexpect;
  1909. }
  1910. ErrorCodeEnum CRemoteControllerCnn::HandleUploadLogByDateReq(const CSmartPointer<IPackage> &pRecvPkg)
  1911. {
  1912. Dbg("remote upload log");
  1913. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1914. int nLen = pRecvPkg->GetStructLen("ULogBDR");
  1915. if (nLen <= 0)
  1916. {
  1917. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ULogBDR] fail").c_str());
  1918. SendPackage(pReplyPkg);
  1919. return Error_Param;
  1920. }
  1921. assert(nLen == sizeof(RvcUploadLogByDateReq));
  1922. RvcUploadLogByDateReq req = {};
  1923. int nArrayLen(0);
  1924. pRecvPkg->GetStructData("ULogBDR", (BYTE*)&req, &nLen, &nArrayLen);
  1925. auto pFunc = m_pEntity->GetFunction();
  1926. CSimpleStringA strZipFileName;
  1927. CSimpleStringA strLogPath;
  1928. CSimpleStringA strZipPath;
  1929. if (req.StartPos <= 0)
  1930. {
  1931. auto strTodayLogName = 0 == strlen(req.ViewDate) ? CSimpleStringA(getCurData().c_str()) : CSimpleStringA(req.ViewDate);
  1932. CSystemStaticInfo sysInfo;
  1933. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  1934. assert(rc == Error_Succeed);
  1935. CSimpleStringA strReqLogName = req.UploadFile;
  1936. // 第一次上传,需生成对应ZIP文件
  1937. if (strReqLogName.Compare("Silverlight", true) == 0) // slv日志
  1938. {
  1939. strZipFileName = CSimpleStringA::Format("%s_slv_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  1940. pFunc->GetPath("Slv", strLogPath);
  1941. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1942. }
  1943. else if (strReqLogName.IsEndWith(".zip", true)) // 安装包日志
  1944. {
  1945. strZipFileName = CSimpleStringA::Format("%s_pkg_%s", (const char*)sysInfo.strTerminalID, (const char*)strReqLogName);
  1946. pFunc->GetPath("RunInfo", strLogPath);
  1947. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strReqLogName).Append(".log");
  1948. }
  1949. else
  1950. {
  1951. // 实体Dbg日志
  1952. strZipFileName = CSimpleStringA::Format("%s_dbg_%s_%s.zip",
  1953. (const char*)sysInfo.strTerminalID, (const char*)strReqLogName, (const char*)strTodayLogName);
  1954. pFunc->GetPath("Dbg", strLogPath);
  1955. if (strReqLogName.Compare("Shell", true) == 0)
  1956. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1957. else if (strReqLogName.Compare("DualActive", true) == 0)
  1958. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1959. else if (strReqLogName.Compare("guardian", true) == 0)
  1960. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1961. else if (strReqLogName.Compare("SpHost", true) == 0)
  1962. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1963. else if (strReqLogName.Compare("SysInit", true) == 0)
  1964. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1965. else if (strReqLogName.Compare("SpBase", true) == 0)
  1966. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1967. else
  1968. {
  1969. CEntityStaticInfo info = {};
  1970. auto nRet = pFunc->GetEntityStaticInfo(strReqLogName, info);
  1971. if (nRet != Error_Succeed)
  1972. {
  1973. Dbg("entity [%s] not exist", (const char*)strReqLogName);
  1974. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)strReqLogName).GetData()).c_str());
  1975. SendPackage(pReplyPkg);
  1976. return Error_NotExist;
  1977. }
  1978. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1979. }
  1980. }
  1981. pFunc->GetPath("Temp", strZipPath);
  1982. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  1983. if (!Base_Exist(strLogPath.GetData()))
  1984. {
  1985. Dbg("log [%s] not exist", (const char*)strLogPath);
  1986. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  1987. SendPackage(pReplyPkg);
  1988. return Error_NotExist;
  1989. }
  1990. // 生成ZIP
  1991. rc = ZipFile(strZipPath, strLogPath);
  1992. if (rc != Error_Succeed)
  1993. {
  1994. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  1995. SendPackage(pReplyPkg);
  1996. return rc;
  1997. }
  1998. }
  1999. else
  2000. {
  2001. strZipFileName = req.UploadFile;
  2002. pFunc->GetPath("Temp", strZipPath);
  2003. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2004. }
  2005. if (!Base_Exist(strZipPath.GetData()))
  2006. {
  2007. Dbg("zip log [%s] not exist", (const char*)strZipPath);
  2008. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2009. SendPackage(pReplyPkg);
  2010. return Error_NotExist;
  2011. }
  2012. // 开始上传
  2013. long long nStartPos = req.StartPos;
  2014. if (nStartPos <0)
  2015. nStartPos = 0;
  2016. long long nMaxBytes = req.MaxRetBytes;
  2017. if (nMaxBytes <= 0)
  2018. nMaxBytes = 32 * 1024;
  2019. auto file = fopen(strZipPath, "rb");
  2020. if (file == NULL)
  2021. {
  2022. auto nError = errno;
  2023. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2024. Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2025. SendPackage(pReplyPkg);
  2026. return Error_Unexpect;
  2027. }
  2028. generateFilesize(file, strZipPath);
  2029. if (nStartPos >0 && nStartPos < nFileSize)
  2030. {
  2031. _fseeki64(file, nStartPos, SEEK_SET);
  2032. }
  2033. else
  2034. nStartPos = 0;
  2035. long long nNextPos = nStartPos + nMaxBytes;
  2036. if (nMaxBytes + nStartPos > nFileSize)
  2037. {
  2038. nMaxBytes = nFileSize - nStartPos;
  2039. nNextPos = 0;
  2040. }
  2041. long long nRetLen = sizeof(RvcUploadLogByDateRet)+nMaxBytes;
  2042. char *pBuf = new char[nRetLen];
  2043. memset(pBuf, 0, nRetLen);
  2044. RvcUploadLogByDateRet *pRet = (RvcUploadLogByDateRet*)pBuf;
  2045. pRet->TaskID = req.TaskID;
  2046. strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
  2047. pRet->ZipFileLen = nFileSize;
  2048. pRet->StartPos = req.StartPos;
  2049. pRet->NextPos = nNextPos;
  2050. char *pLogData = (char*)pRet->LogData;
  2051. long long nHasRead(0);
  2052. DWORD nReadLen(0);
  2053. do
  2054. {
  2055. //Dbg("ftell: %d", ftell(file));
  2056. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2057. //Dbg("ftell: %d", ftell(file));
  2058. if (nReadLen >0)
  2059. nHasRead += nReadLen;
  2060. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2061. fclose(file);
  2062. if (nHasRead < nMaxBytes)
  2063. {
  2064. delete[] pBuf;
  2065. auto nError = GetLastError();
  2066. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2067. Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2068. SendPackage(pReplyPkg);
  2069. return Error_Unexpect;
  2070. }
  2071. pReplyPkg->AddStruct("ULogBDA", false, false, (BYTE*)pBuf, nRetLen);
  2072. delete[] pBuf;
  2073. SendPackage(pReplyPkg);
  2074. if (nNextPos <= 0)
  2075. {
  2076. // 删除压缩文件
  2077. Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2078. Base_DeleteFile(strZipPath.GetData());
  2079. }
  2080. return Error_Succeed;
  2081. }
  2082. ErrorCodeEnum CRemoteControllerCnn::HandleUploadLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  2083. {
  2084. Dbg("remote upload log");
  2085. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2086. int nLen = pRecvPkg->GetStructLen("UpldLogR");
  2087. if (nLen <= 0)
  2088. {
  2089. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [UpldLogR] fail").c_str());
  2090. SendPackage(pReplyPkg);
  2091. return Error_Param;
  2092. }
  2093. assert(nLen == sizeof(RvcUploadLogReq));
  2094. RvcUploadLogReq req = {};
  2095. int nArrayLen(0);
  2096. pRecvPkg->GetStructData("UpldLogR", (BYTE*)&req, &nLen, &nArrayLen);
  2097. auto pFunc = m_pEntity->GetFunction();
  2098. CSimpleStringA strZipFileName;
  2099. CSimpleStringA strLogPath;
  2100. CSimpleStringA strZipPath;
  2101. if (req.StartPos <= 0)
  2102. {
  2103. auto strTodayLogName = getCurData().c_str();
  2104. CSystemStaticInfo sysInfo;
  2105. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  2106. assert(rc == Error_Succeed);
  2107. CSimpleStringA strReqLogName = req.UploadFile;
  2108. // 第一次上传,需生成对应ZIP文件
  2109. if (strReqLogName.Compare("Silverlight", true) == 0) // slv日志
  2110. {
  2111. strZipFileName = CSimpleStringA::Format("%s_slv_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  2112. pFunc->GetPath("Slv", strLogPath);
  2113. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2114. }
  2115. else if (strReqLogName.Compare("AllDbg", true) == 0)
  2116. {
  2117. strZipFileName = CSimpleStringA::Format("%s_AllDbg_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  2118. pFunc->GetPath("Dbg", strLogPath);
  2119. pFunc->GetPath("Temp", strZipPath);
  2120. strZipPath.Append(SPLIT_SLASH_STR).Append((const char*)strZipFileName);
  2121. try
  2122. {
  2123. std::pair<bool, std::string> result = MyZip::ZipDir(strZipPath.GetData(), strLogPath.GetData());
  2124. if (result.first)
  2125. goto BeginUpload;
  2126. else
  2127. {
  2128. Dbg("%s zip failed", strZipFileName);
  2129. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("%s zip failed", strZipFileName).GetData()).c_str());
  2130. SendPackage(pReplyPkg);
  2131. return Error_NotExist;
  2132. }
  2133. }
  2134. catch (exception &e)
  2135. {
  2136. Dbg("error:%s", e.what());
  2137. }
  2138. }
  2139. else if (strReqLogName.Compare("RunInfomation", true) == 0)
  2140. {
  2141. strZipFileName = CSimpleStringA::Format("%s_runInformation_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  2142. pFunc->GetPath("Temp", strZipPath);
  2143. strZipPath.Append(SPLIT_SLASH_STR).Append((const char*)strZipFileName);
  2144. vector<std::string> zipArr;
  2145. pFunc->GetPath("RunInfo", strLogPath);
  2146. zipArr.push_back(strLogPath.GetData());
  2147. pFunc->GetPath("HardwareCfg", strLogPath);
  2148. strLogPath.Append(SPLIT_SLASH_STR).Append("root.ini");
  2149. zipArr.push_back(strLogPath.GetData());
  2150. Dbg("%s,%s,%s", zipArr[0].c_str(), zipArr[1].c_str(), strZipPath);
  2151. std::pair<bool, std::string> result = MyZip::ZipVector(strZipPath.GetData(), zipArr);
  2152. if (result.first)
  2153. goto BeginUpload;
  2154. else
  2155. {
  2156. Dbg("%s zip failed", strZipFileName);
  2157. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("%s zip failed", strZipFileName).GetData()).c_str());
  2158. SendPackage(pReplyPkg);
  2159. return Error_NotExist;
  2160. }
  2161. }
  2162. else if (strReqLogName.IsEndWith(".zip", true)) // 安装包日志
  2163. {
  2164. strZipFileName = CSimpleStringA::Format("%s_pkg_%s", (const char*)sysInfo.strTerminalID, (const char*)strReqLogName);
  2165. pFunc->GetPath("RunInfo", strLogPath);
  2166. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strReqLogName).Append(".log");
  2167. }
  2168. else
  2169. {
  2170. // 实体Dbg日志
  2171. strZipFileName = CSimpleStringA::Format("%s_dbg_%s_%s.zip",
  2172. (const char*)sysInfo.strTerminalID, (const char*)strReqLogName, (const char*)strTodayLogName);
  2173. pFunc->GetPath("Dbg", strLogPath);
  2174. if (strReqLogName.Compare("Shell", true) == 0)
  2175. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2176. else if (strReqLogName.Compare("DualActive", true) == 0)
  2177. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2178. else if (strReqLogName.Compare("guardian", true) == 0)
  2179. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2180. else if (strReqLogName.Compare("SpHost", true) == 0)
  2181. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2182. else if (strReqLogName.Compare("SysInit", true) == 0)
  2183. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2184. else if (strReqLogName.Compare("SpBase", true) == 0)
  2185. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2186. else
  2187. {
  2188. CEntityStaticInfo info = {};
  2189. auto nRet = pFunc->GetEntityStaticInfo(strReqLogName, info);
  2190. if (nRet != Error_Succeed)
  2191. {
  2192. Dbg("entity [%s] not exist", (const char*)strReqLogName);
  2193. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)strReqLogName).GetData()).c_str());
  2194. SendPackage(pReplyPkg);
  2195. return Error_NotExist;
  2196. }
  2197. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2198. }
  2199. }
  2200. pFunc->GetPath("Temp", strZipPath);
  2201. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2202. if (!Base_Exist(strLogPath.GetData()))
  2203. {
  2204. Dbg("log [%s] not exist", (const char*)strLogPath);
  2205. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  2206. SendPackage(pReplyPkg);
  2207. return Error_NotExist;
  2208. }
  2209. // 生成ZIP
  2210. rc = ZipFile(strZipPath, strLogPath);
  2211. if (rc != Error_Succeed)
  2212. {
  2213. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2214. SendPackage(pReplyPkg);
  2215. return rc;
  2216. }
  2217. }
  2218. else
  2219. {
  2220. strZipFileName = req.UploadFile;
  2221. pFunc->GetPath("Temp", strZipPath);
  2222. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2223. }
  2224. if (!Base_Exist(strZipPath.GetData()))
  2225. {
  2226. Dbg("zip log [%s] not exist", (const char*)strZipPath);
  2227. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2228. SendPackage(pReplyPkg);
  2229. return Error_NotExist;
  2230. }
  2231. BeginUpload:
  2232. // 开始上传
  2233. long long nStartPos = req.StartPos;
  2234. if (nStartPos <0)
  2235. nStartPos = 0;
  2236. long long nMaxBytes = req.MaxRetBytes;
  2237. if (nMaxBytes <= 0)
  2238. nMaxBytes = 32 * 1024;
  2239. auto file = fopen(strZipPath, "rb");
  2240. if (file == NULL)
  2241. {
  2242. auto nError = errno;
  2243. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2244. Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2245. SendPackage(pReplyPkg);
  2246. return Error_Unexpect;
  2247. }
  2248. generateFilesize(file, strZipPath);
  2249. if (nStartPos >0 && nStartPos < nFileSize)
  2250. {
  2251. _fseeki64(file, nStartPos, SEEK_SET);
  2252. }
  2253. else
  2254. nStartPos = 0;
  2255. long long nNextPos = nStartPos + nMaxBytes;
  2256. if (nMaxBytes + nStartPos > nFileSize)
  2257. {
  2258. nMaxBytes = nFileSize - nStartPos;
  2259. nNextPos = 0;
  2260. }
  2261. long long nRetLen = sizeof(RvcUploadLogRet)+nMaxBytes;
  2262. char *pBuf = new char[nRetLen];
  2263. memset(pBuf, 0, nRetLen);
  2264. RvcUploadLogRet *pRet = (RvcUploadLogRet*)pBuf;
  2265. pRet->TaskID = req.TaskID;
  2266. strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
  2267. pRet->ZipFileLen = nFileSize;
  2268. pRet->StartPos = req.StartPos;
  2269. pRet->NextPos = nNextPos;
  2270. char *pLogData = (char*)pRet->LogData;
  2271. long long nHasRead(0);
  2272. DWORD nReadLen(0);
  2273. do
  2274. {
  2275. //Dbg("ftell: %d", ftell(file));
  2276. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2277. //Dbg("ftell: %d", ftell(file));
  2278. if (nReadLen >0)
  2279. nHasRead += nReadLen;
  2280. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2281. fclose(file);
  2282. if (nHasRead < nMaxBytes)
  2283. {
  2284. delete[] pBuf;
  2285. auto nError = GetLastError();
  2286. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2287. Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2288. SendPackage(pReplyPkg);
  2289. return Error_Unexpect;
  2290. }
  2291. pReplyPkg->AddStruct("UpldLogA", false, false, (BYTE*)pBuf, nRetLen);
  2292. delete[] pBuf;
  2293. SendPackage(pReplyPkg);
  2294. if (nNextPos <= 0)
  2295. {
  2296. // 删除压缩文件
  2297. Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2298. Base_DeleteFile(strZipPath.GetData());
  2299. }
  2300. return Error_Succeed;
  2301. }
  2302. ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityStateExReq(const CSmartPointer<IPackage> &pRecvPkg)
  2303. {
  2304. Dbg("remote view entity state ex");
  2305. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2306. int nLen = pRecvPkg->GetStructLen("ViEnExR");
  2307. if (nLen <= 0)
  2308. {
  2309. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViEnExR] fail").c_str());
  2310. SendPackage(pReplyPkg);
  2311. return Error_Param;
  2312. }
  2313. assert(nLen == sizeof(RvcViewEntityExReq));
  2314. RvcViewEntityExReq req = {};
  2315. int nArrayLen(0);
  2316. pRecvPkg->GetStructData("ViEnExR", (BYTE*)&req, &nLen, &nArrayLen);
  2317. RvcViewEntityExRet1 ret1 = {};
  2318. ret1.TaskID = req.TaskID;
  2319. pReplyPkg->AddStruct("ViEnExA1", false, false, (BYTE*)&ret1, sizeof(ret1));
  2320. auto pFunc = m_pEntity->GetFunction();
  2321. CAutoArray<CSimpleStringA> strEntityNames;
  2322. CAutoArray<WORD> dwEntityIDs;
  2323. pFunc->GetAllRegistedEntity(strEntityNames, dwEntityIDs);
  2324. int nCount = strEntityNames.GetCount();
  2325. if (nCount >0)
  2326. {
  2327. nCount += 8; // 包括root\shell\silverlight3个虚拟实体
  2328. RvcViewEntityExRet2 *pRet2 = new RvcViewEntityExRet2[nCount];
  2329. memset(pRet2, 0, sizeof(RvcViewEntityExRet2)* nCount);
  2330. CSimpleStringA strEntCfgDir;
  2331. CSimpleStringA strRunCfgDir;
  2332. pFunc->GetPath("Cfg", strEntCfgDir);
  2333. strEntCfgDir.Append(SPLIT_SLASH_STR);
  2334. pFunc->GetPath("RunInfo", strRunCfgDir);
  2335. strRunCfgDir.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR);
  2336. for (int i = 0; i<nCount - 8; i++)
  2337. {
  2338. CEntityRunInfo info = {};
  2339. CSimpleStringA strEntityName = strEntityNames[i];
  2340. pFunc->GetEntityRunInfo(strEntityName, info);
  2341. strncpy_s(pRet2[i].EntityName, sizeof(pRet2[i].EntityName), (const char*)strEntityName, _TRUNCATE);
  2342. pRet2[i].EntityID = dwEntityIDs[i];
  2343. pRet2[i].EntityState = info.eState;
  2344. pRet2[i].ProcessID = info.dwProcessID;
  2345. if (info.dwProcessID > 0)
  2346. {
  2347. #if (defined _WIN32 || defined _WIN64)
  2348. auto hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, info.dwProcessID);
  2349. if (hProcess != NULL)
  2350. {
  2351. PROCESS_MEMORY_COUNTERS pmc = {};
  2352. GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
  2353. pRet2[i].UsedMemory = pmc.WorkingSetSize / 1024 / 1024;
  2354. }
  2355. #else
  2356. pRet2[i].UsedMemory = get_proc_mem(info.dwProcessID) / 1024 / 1024;
  2357. #endif
  2358. }
  2359. CSimpleStringA strEntCfgPath = strEntCfgDir + strEntityName + ".ini";
  2360. CSimpleStringA strRunCfgPath = strRunCfgDir + strEntityName + ".ini";
  2361. pRet2[i].HasConfig = Base_Exist(strEntCfgPath.GetData());
  2362. pRet2[i].HasRunCfg = Base_Exist(strRunCfgPath.GetData());
  2363. }
  2364. strncpy_s(pRet2[nCount - 8].EntityName, sizeof(pRet2[nCount - 8].EntityName), "guardian", _TRUNCATE);
  2365. pRet2[nCount - 8].HasConfig = false;
  2366. strncpy_s(pRet2[nCount - 7].EntityName, sizeof(pRet2[nCount - 7].EntityName), "DualActive", _TRUNCATE);
  2367. pRet2[nCount - 7].HasConfig = false;
  2368. strncpy_s(pRet2[nCount - 6].EntityName, sizeof(pRet2[nCount - 6].EntityName), "SpHost", _TRUNCATE);
  2369. pRet2[nCount - 6].HasConfig = false;
  2370. strncpy_s(pRet2[nCount - 5].EntityName, sizeof(pRet2[nCount - 5].EntityName), "SysInit", _TRUNCATE);
  2371. pRet2[nCount - 5].HasConfig = false;
  2372. strncpy_s(pRet2[nCount - 4].EntityName, sizeof(pRet2[nCount - 4].EntityName), "SpBase", _TRUNCATE);
  2373. pRet2[nCount - 4].HasConfig = false;
  2374. // root\shell\silverlight3个虚拟实体
  2375. strncpy_s(pRet2[nCount - 3].EntityName, sizeof(pRet2[nCount - 3].EntityName), "Root", _TRUNCATE);
  2376. pRet2[nCount - 3].HasConfig = true;
  2377. strncpy_s(pRet2[nCount - 2].EntityName, sizeof(pRet2[nCount - 2].EntityName), "Shell", _TRUNCATE);
  2378. pRet2[nCount - 2].HasConfig = true;
  2379. strncpy_s(pRet2[nCount - 1].EntityName, sizeof(pRet2[nCount - 1].EntityName), "Silverlight", _TRUNCATE);
  2380. pReplyPkg->AddStruct("ViEnExA2", false, false, (BYTE*)pRet2, sizeof(RvcViewEntityExRet2)* nCount, nCount);
  2381. }
  2382. SendPackage(pReplyPkg);
  2383. return Error_Succeed;
  2384. }
  2385. ErrorCodeEnum CRemoteControllerCnn::HandleBatchUploadLogsByDateReq(const CSmartPointer<IPackage> &pRecvPkg)
  2386. {
  2387. Dbg("remote batch upload logs by date");
  2388. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2389. int nLen = pRecvPkg->GetStructLen("ULogsBDR");
  2390. if (nLen <= 0)
  2391. {
  2392. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ULogsBDR] fail").c_str());
  2393. SendPackage(pReplyPkg);
  2394. return Error_Param;
  2395. }
  2396. assert(nLen == sizeof(RvcBatchUploadLogsByDateReq) || nLen == sizeof(RvcBatchUploadLogsByDateReq2));
  2397. RvcBatchUploadLogsByDateReq req = {};
  2398. RvcBatchUploadLogsByDateReq2 req2 = {};
  2399. int nArrayLen(0);
  2400. if (nLen == sizeof(RvcBatchUploadLogsByDateReq))
  2401. pRecvPkg->GetStructData("ULogsBDR", (BYTE*)&req, &nLen, &nArrayLen);
  2402. else
  2403. {
  2404. Dbg("RvcBatchUploadLogsByDateReq2");
  2405. pRecvPkg->GetStructData("ULogsBDR", (BYTE*)&req2, &nLen, &nArrayLen);
  2406. req.TaskID = req2.TaskID;
  2407. strncpy_s(req.ViewDate, req2.ViewDate, sizeof(req2.ViewDate));
  2408. strncpy_s(req.UploadFile, req2.UploadFile, sizeof(req2.UploadFile));
  2409. strncpy_s(req.ZipFileName, req2.ZipFileName, sizeof(req2.ZipFileName));
  2410. req.StartPos = req2.StartPos;
  2411. req.MaxRetBytes = req2.MaxRetBytes;
  2412. }
  2413. Dbg("UploadFile:%s, ZipFileName:%s", req.UploadFile, req.ZipFileName);
  2414. CSimpleStringA strZipPath;
  2415. auto pFunc = m_pEntity->GetFunction();
  2416. pFunc->GetPath("Temp", strZipPath);
  2417. strZipPath.Append(SPLIT_SLASH_STR);
  2418. CSimpleStringA strZipFileName = req.ZipFileName;
  2419. strZipPath += strZipFileName;
  2420. if (req.StartPos <= 0)
  2421. {
  2422. CSimpleStringA strLogPath;
  2423. auto strTodayLogName = (0 == strlen(req.ViewDate) ? CSimpleStringA(getCurData().c_str()) : CSimpleStringA(req.ViewDate));
  2424. CSimpleStringA strReqLogNames = req.UploadFile;
  2425. auto arrFiles = strReqLogNames.Split(';');
  2426. list<string> listSrcFiles;
  2427. for (int i = 0; i < arrFiles.GetCount(); i++)
  2428. {
  2429. // 第一次上传,需生成对应ZIP文件
  2430. if (arrFiles[i].Compare("Silverlight", true) == 0) // slv日志
  2431. {
  2432. pFunc->GetPath("Slv", strLogPath);
  2433. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2434. }
  2435. else if (arrFiles[i].IsEndWith(".zip", true)) // 安装包日志
  2436. {
  2437. pFunc->GetPath("RunInfo", strLogPath);
  2438. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)arrFiles[i]).Append(".log");
  2439. }
  2440. else
  2441. {
  2442. // 实体Dbg日志
  2443. pFunc->GetPath("Dbg", strLogPath);
  2444. if (arrFiles[i].Compare("Shell", true) == 0)
  2445. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2446. else if (arrFiles[i].Compare("DualActive", true) == 0)
  2447. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2448. else if (arrFiles[i].Compare("guardian", true) == 0)
  2449. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2450. else if (arrFiles[i].Compare("SpHost", true) == 0)
  2451. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2452. else if (arrFiles[i].Compare("SysInit", true) == 0)
  2453. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2454. else if (arrFiles[i].Compare("SpBase", true) == 0)
  2455. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2456. else if (arrFiles[i].Compare("Root", true) == 0)
  2457. strLogPath.Append(SPLIT_SLASH_STR).Append("Root").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2458. else
  2459. {
  2460. CEntityStaticInfo info = {};
  2461. auto nRet = pFunc->GetEntityStaticInfo(arrFiles[i], info);
  2462. if (nRet != Error_Succeed)
  2463. {
  2464. Dbg("entity [%s] not exist", (const char*)arrFiles[i]);
  2465. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)arrFiles[i]).GetData()).c_str());
  2466. SendPackage(pReplyPkg);
  2467. return Error_NotExist;
  2468. }
  2469. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2470. }
  2471. }
  2472. if (!Base_Exist(strLogPath.GetData()))
  2473. {
  2474. Dbg("log [%s] not exist", (const char*)strLogPath);
  2475. continue;
  2476. }
  2477. listSrcFiles.push_back((const char*)strLogPath);
  2478. }
  2479. if (listSrcFiles.size() == 0)
  2480. {
  2481. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
  2482. SendPackage(pReplyPkg);
  2483. return Error_NotExist;
  2484. }
  2485. Dbg("strZipPath:%s", strZipPath.GetData());
  2486. // 生成ZIP
  2487. auto rc = BatchZipFiles(strZipPath, listSrcFiles);
  2488. if (rc != Error_Succeed)
  2489. {
  2490. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2491. SendPackage(pReplyPkg);
  2492. return rc;
  2493. }
  2494. }
  2495. if (!Base_Exist(strZipPath.GetData()))
  2496. {
  2497. Dbg("zip log [%s] not exist", (const char*)strZipPath);
  2498. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2499. SendPackage(pReplyPkg);
  2500. return Error_NotExist;
  2501. }
  2502. // 开始上传
  2503. long long nStartPos = req.StartPos;
  2504. if (nStartPos <0)
  2505. nStartPos = 0;
  2506. long long nMaxBytes = req.MaxRetBytes;
  2507. if (nMaxBytes <= 0)
  2508. nMaxBytes = 32 * 1024;
  2509. auto file = fopen(strZipPath, "rb");
  2510. if (file == NULL)
  2511. {
  2512. auto nError = errno;
  2513. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2514. Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2515. SendPackage(pReplyPkg);
  2516. return Error_Unexpect;
  2517. }
  2518. generateFilesize(file, strZipPath);
  2519. if (nStartPos >0 && nStartPos < nFileSize)
  2520. {
  2521. _fseeki64(file, nStartPos, SEEK_SET);
  2522. }
  2523. else
  2524. nStartPos = 0;
  2525. long long nNextPos = nStartPos + nMaxBytes;
  2526. if (nMaxBytes + nStartPos > nFileSize)
  2527. {
  2528. nMaxBytes = nFileSize - nStartPos;
  2529. nNextPos = 0;
  2530. }
  2531. long long nRetLen = sizeof(RvcBatchUploadLogsByDateRet) + nMaxBytes;
  2532. char *pBuf = new char[nRetLen];
  2533. memset(pBuf, 0, nRetLen);
  2534. RvcBatchUploadLogsByDateRet *pRet = (RvcBatchUploadLogsByDateRet*)pBuf;
  2535. pRet->TaskID = req.TaskID;
  2536. strncpy_s(pRet->ZipFileName, sizeof(pRet->ZipFileName), strZipFileName, _TRUNCATE);
  2537. pRet->ZipFileLen = nFileSize;
  2538. pRet->StartPos = req.StartPos;
  2539. pRet->NextPos = nNextPos;
  2540. char *pLogData = (char*)pRet->LogData;
  2541. long long nHasRead(0);
  2542. DWORD nReadLen(0);
  2543. do
  2544. {
  2545. //Dbg("ftell: %d", ftell(file));
  2546. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2547. //Dbg("ftell: %d", ftell(file));
  2548. if (nReadLen >0)
  2549. nHasRead += nReadLen;
  2550. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2551. fclose(file);
  2552. if (nHasRead < nMaxBytes)
  2553. {
  2554. delete[] pBuf;
  2555. auto nError = GetLastError();
  2556. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2557. Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2558. SendPackage(pReplyPkg);
  2559. return Error_Unexpect;
  2560. }
  2561. pReplyPkg->AddStruct("ULogsBDA", false, false, (BYTE*)pBuf, nRetLen);
  2562. delete[] pBuf;
  2563. SendPackage(pReplyPkg);
  2564. if (nNextPos <= 0)
  2565. {
  2566. // 删除压缩文件
  2567. Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2568. Base_DeleteFile(strZipPath.GetData());
  2569. }
  2570. return Error_Succeed;
  2571. }
  2572. ErrorCodeEnum CRemoteControllerCnn::HandleBatchUploadLogsReq(const CSmartPointer<IPackage> &pRecvPkg)
  2573. {
  2574. Dbg("remote batch upload logs");
  2575. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2576. int nLen = pRecvPkg->GetStructLen("UpdLogsR");
  2577. if (nLen <= 0)
  2578. {
  2579. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [UpdLogsR] fail").c_str());
  2580. SendPackage(pReplyPkg);
  2581. return Error_Param;
  2582. }
  2583. assert(nLen == sizeof(RvcBatchUploadLogsReq) || nLen == sizeof(RvcBatchUploadLogsReq2));
  2584. RvcBatchUploadLogsReq req = {};
  2585. RvcBatchUploadLogsReq2 req2 = {};
  2586. int nArrayLen(0);
  2587. if (nLen == sizeof(RvcBatchUploadLogsReq))
  2588. pRecvPkg->GetStructData("UpdLogsR", (BYTE*)&req, &nLen, &nArrayLen);
  2589. else
  2590. {
  2591. pRecvPkg->GetStructData("UpdLogsR", (BYTE*)&req2, &nLen, &nArrayLen);
  2592. req.TaskID = req2.TaskID;
  2593. req.StartPos = req2.StartPos;
  2594. req.MaxRetBytes = req2.MaxRetBytes;
  2595. strncpy_s(req.UploadFile, req2.UploadFile, sizeof(req2.UploadFile));
  2596. strncpy_s(req.ZipFileName, req2.ZipFileName, sizeof(req2.ZipFileName));
  2597. }
  2598. CSimpleStringA strZipPath;
  2599. auto pFunc = m_pEntity->GetFunction();
  2600. pFunc->GetPath("Temp", strZipPath);
  2601. strZipPath.Append(SPLIT_SLASH_STR);
  2602. CSimpleStringA strZipFileName = req.ZipFileName;
  2603. strZipPath += strZipFileName;
  2604. if (req.StartPos <= 0)
  2605. {
  2606. CSimpleStringA strLogPath;
  2607. auto strTodayLogName = CSimpleStringA(getCurData().c_str());
  2608. CSimpleStringA strReqLogNames = req.UploadFile;
  2609. auto arrFiles = strReqLogNames.Split(';');
  2610. list<string> listSrcFiles;
  2611. for (int i = 0; i < arrFiles.GetCount(); i++)
  2612. {
  2613. // 第一次上传,需生成对应ZIP文件
  2614. if (arrFiles[i].Compare("Silverlight", true) == 0) // slv日志
  2615. {
  2616. pFunc->GetPath("Slv", strLogPath);
  2617. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2618. }
  2619. else if (arrFiles[i].IsEndWith(".zip", true)) // 安装包日志
  2620. {
  2621. pFunc->GetPath("RunInfo", strLogPath);
  2622. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)arrFiles[i]).Append(".log");
  2623. }
  2624. else
  2625. {
  2626. // 实体Dbg日志
  2627. pFunc->GetPath("Dbg", strLogPath);
  2628. if (arrFiles[i].Compare("Shell", true) == 0)
  2629. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2630. else if (arrFiles[i].Compare("DualActive", true) == 0)
  2631. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2632. else if (arrFiles[i].Compare("guardian", true) == 0)
  2633. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2634. else if (arrFiles[i].Compare("SpHost", true) == 0)
  2635. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2636. else if (arrFiles[i].Compare("SysInit", true) == 0)
  2637. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2638. else if (arrFiles[i].Compare("SpBase", true) == 0)
  2639. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2640. else if (arrFiles[i].Compare("Root", true) == 0)
  2641. strLogPath.Append(SPLIT_SLASH_STR).Append("Root").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2642. else
  2643. {
  2644. CEntityStaticInfo info = {};
  2645. auto nRet = pFunc->GetEntityStaticInfo(arrFiles[i], info);
  2646. if (nRet != Error_Succeed)
  2647. {
  2648. Dbg("entity [%s] not exist", (const char*)arrFiles[i]);
  2649. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)arrFiles[i]).GetData()).c_str());
  2650. SendPackage(pReplyPkg);
  2651. return Error_NotExist;
  2652. }
  2653. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2654. }
  2655. }
  2656. if (!Base_Exist(strLogPath.GetData()))
  2657. {
  2658. Dbg("log [%s] not exist", (const char*)strLogPath);
  2659. continue;
  2660. }
  2661. listSrcFiles.push_back((const char*)strLogPath);
  2662. }
  2663. if (listSrcFiles.size() == 0)
  2664. {
  2665. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
  2666. SendPackage(pReplyPkg);
  2667. return Error_NotExist;
  2668. }
  2669. // 生成ZIP
  2670. auto rc = BatchZipFiles(strZipPath, listSrcFiles);
  2671. if (rc != Error_Succeed)
  2672. {
  2673. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2674. SendPackage(pReplyPkg);
  2675. return rc;
  2676. }
  2677. }
  2678. if (!Base_Exist(strZipPath.GetData()))
  2679. {
  2680. Dbg("zip log [%s] not exist", (const char*)strZipPath);
  2681. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2682. SendPackage(pReplyPkg);
  2683. return Error_NotExist;
  2684. }
  2685. // 开始上传
  2686. long long nStartPos = req.StartPos;
  2687. if (nStartPos <0)
  2688. nStartPos = 0;
  2689. long long nMaxBytes = req.MaxRetBytes;
  2690. if (nMaxBytes <= 0)
  2691. nMaxBytes = 32 * 1024;
  2692. auto file = fopen(strZipPath, "rb");
  2693. if (file == NULL)
  2694. {
  2695. auto nError = errno;
  2696. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2697. Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2698. SendPackage(pReplyPkg);
  2699. return Error_Unexpect;
  2700. }
  2701. generateFilesize(file, strZipPath);
  2702. if (nStartPos >0 && nStartPos < nFileSize)
  2703. {
  2704. _fseeki64(file, nStartPos, SEEK_SET);
  2705. }
  2706. else
  2707. nStartPos = 0;
  2708. long long nNextPos = nStartPos + nMaxBytes;
  2709. if (nMaxBytes + nStartPos > nFileSize)
  2710. {
  2711. nMaxBytes = nFileSize - nStartPos;
  2712. nNextPos = 0;
  2713. }
  2714. long long nRetLen = sizeof(RvcBatchUploadLogsRet) + nMaxBytes;
  2715. char *pBuf = new char[nRetLen];
  2716. memset(pBuf, 0, nRetLen);
  2717. RvcBatchUploadLogsRet *pRet = (RvcBatchUploadLogsRet*)pBuf;
  2718. pRet->TaskID = req.TaskID;
  2719. strncpy_s(pRet->ZipFileName, sizeof(pRet->ZipFileName), strZipFileName, _TRUNCATE);
  2720. pRet->ZipFileLen = nFileSize;
  2721. pRet->StartPos = req.StartPos;
  2722. pRet->NextPos = nNextPos;
  2723. char *pLogData = (char*)pRet->LogData;
  2724. long long nHasRead(0);
  2725. DWORD nReadLen(0);
  2726. do
  2727. {
  2728. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2729. if (nReadLen >0)
  2730. nHasRead += nReadLen;
  2731. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2732. fclose(file);
  2733. if (nHasRead < nMaxBytes)
  2734. {
  2735. delete[] pBuf;
  2736. auto nError = GetLastError();
  2737. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2738. Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2739. SendPackage(pReplyPkg);
  2740. return Error_Unexpect;
  2741. }
  2742. pReplyPkg->AddStruct("UpdLogsA", false, false, (BYTE*)pBuf, nRetLen);
  2743. delete[] pBuf;
  2744. SendPackage(pReplyPkg);
  2745. if (nNextPos <= 0)
  2746. {
  2747. // 删除压缩文件
  2748. Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2749. Base_DeleteFile(strZipPath.GetData());
  2750. }
  2751. return Error_Succeed;
  2752. }
  2753. ErrorCodeEnum CRemoteControllerCnn::HandleCancelUpgradeReq(const CSmartPointer<IPackage> &pRecvPkg)
  2754. {
  2755. Dbg("remote cancel upgarde");
  2756. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2757. int nLen = pRecvPkg->GetStructLen("CancUpgR");
  2758. if (nLen <= 0)
  2759. {
  2760. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [CancUpgR] fail").c_str());
  2761. SendPackage(pReplyPkg);
  2762. return Error_Param;
  2763. }
  2764. assert(nLen == sizeof(RvcCancelUpgradeReq));
  2765. RvcCancelUpgradeReq req = {};
  2766. int nArrayLen(0);
  2767. pRecvPkg->GetStructData("CancUpgR", (BYTE*)&req, &nLen, &nArrayLen);
  2768. RvcCancelUpgradeRet ret = {};
  2769. ret.TaskID = req.TaskID;
  2770. pReplyPkg->AddStruct("CancUpgA", false, false, (BYTE*)&ret, sizeof(ret));
  2771. CSimpleStringA strPack = req.PackName;
  2772. Dbg("begin cancel upgrade: %s", (const char*)strPack);
  2773. // 调用升级实体接口升级
  2774. UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
  2775. auto rc = pClient->Connect();
  2776. if (rc == Error_Succeed)
  2777. {
  2778. UpgradeMgrService_CancelUpdate_Req req = {};
  2779. req.strPackFile = strPack;
  2780. UpgradeMgrService_CancelUpdate_Ans ans = {};
  2781. rc = pClient->CancelUpdate(req, ans, 10000);
  2782. pClient->GetFunction()->CloseSession();
  2783. } else {
  2784. pClient->SafeDelete();
  2785. }
  2786. if (rc != Error_Succeed)
  2787. {
  2788. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("远程取消升级失败").c_str());
  2789. Dbg("remote cancel upgrade fail: 0x%X", rc);
  2790. }
  2791. else
  2792. {
  2793. Dbg("remote cancel upgrade succeed!");
  2794. }
  2795. SendPackage(pReplyPkg);
  2796. return Error_Succeed;
  2797. }