main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include<stdio.h>
  2. #include <curl/curl.h>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <string>
  6. #include <memory>
  7. #include "7zFile.h"
  8. #include "LzmaDec.h"
  9. #include "7ZItf.h"
  10. #include <jsoncpp/json/json.h>
  11. #include <jsoncpp/json/writer.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "../common/comm.h"
  18. CURLcode Download(const char * url,const char * filename);
  19. bool LoadConfig();
  20. void WriteConfig(int32_t version);
  21. EgoType Type;
  22. int32_t Version;
  23. std::string Url;
  24. std::string getpath()
  25. {
  26. char exec_name [BUFSIZ];
  27. readlink ("/proc/self/exe", exec_name, BUFSIZ);
  28. int32_t len=strlen(exec_name);
  29. for(int i=len;i>=0;i--)
  30. {
  31. if(exec_name[i]=='/')
  32. {
  33. exec_name[i]=0;
  34. break;
  35. }
  36. }
  37. return std::string(exec_name);
  38. }
  39. int main()
  40. {
  41. int32_t version=-1;
  42. if(!LoadConfig()) return 0;
  43. char szurl[256];
  44. sprintf(szurl,"%s/iego.json",Url.c_str());
  45. if(Download(szurl,"iego.json")==CURLcode::CURLE_OK)
  46. {
  47. Json::Value root;
  48. Json::Reader jsonReader;
  49. std::ifstream ifile("./iego.json");
  50. if(jsonReader.parse(ifile,root))
  51. {
  52. version=root["car_ver"].asInt();
  53. }
  54. if(Version<version)
  55. {
  56. std::string file=root["car_file"].asString();
  57. char szurl[256];
  58. sprintf(szurl,"%s/%s",Url.c_str(),file.c_str());
  59. Download(szurl,file.c_str());
  60. Decompress(file.c_str());
  61. WriteConfig(version);
  62. }
  63. system("./EgoController");
  64. }
  65. printf("hello,world");
  66. return 0;
  67. }
  68. size_t DownloadCallback(void * pBuffer,size_t nSize,size_t nMemBytes,FILE * fp);
  69. CURLcode Download(const char * url,const char * filename)
  70. {
  71. FILE * fp=fopen(filename,"wb+");
  72. CURL* curl = curl_easy_init();
  73. if(curl == NULL)
  74. {
  75. //抛出错误异常
  76. throw std::runtime_error("create easy_handle fail");
  77. }
  78. curl_easy_setopt(curl, CURLOPT_URL, url);
  79. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &DownloadCallback);
  80. curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
  81. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
  82. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  83. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
  84. //开始进行请求
  85. CURLcode retCode=curl_easy_perform(curl);
  86. //清理curl_easy
  87. curl_easy_cleanup(curl);
  88. fclose(fp);
  89. return retCode;
  90. }
  91. bool LoadConfig()
  92. {
  93. Json::Value root;
  94. Json::Reader jsonReader;
  95. std::ifstream ifile("./ego.json");
  96. if(jsonReader.parse(ifile,root))
  97. {
  98. Url=root["url"].asString();
  99. auto str=root["type"].asString();
  100. Type=EgoType::Car;
  101. Version=root["version"].asInt();
  102. return true;
  103. }
  104. return false;
  105. }
  106. void WriteConfig(int32_t version)
  107. {
  108. Json::Value root;
  109. Json::Reader jsonReader;
  110. std::ifstream ifile("./ego.json");
  111. if(jsonReader.parse(ifile,root))
  112. {
  113. root["version"]=version;
  114. Json::StreamWriterBuilder b;
  115. std::unique_ptr<Json::StreamWriter> sw(b.newStreamWriter());
  116. //sw.Write(root);
  117. std::ofstream ofile;
  118. ofile.open("./ego.json",std::ios::out);
  119. sw->write(root,&ofile);
  120. ofile.close();
  121. }
  122. }
  123. size_t DownloadCallback(void * pBuffer,size_t nSize,size_t nMemBytes,FILE * fp)
  124. {
  125. size_t nWrite=fwrite(pBuffer,nSize,nMemBytes,fp);
  126. return nWrite;
  127. }