Guest User

Untitled

a guest
Sep 13th, 2014
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. void __fastcall TForm1::dataUpload2ServerServe(TCustomIpClient *ClientSocket)
  2. {
  3.     NextPacketSize npk;
  4.     TXMLDocument *xml;
  5.     UnicodeString filename;
  6.     String routeName;
  7.     String uname = map.find(ClientSocket->RemoteHost)->second.username;
  8.     int lastRouteId;
  9.  
  10.     ThreadLock->Enter();
  11.  
  12.     ClientSocket->ReceiveBuf(&npk, sizeof(NextPacketSize));
  13.  
  14.     Memo1->Lines->Add(String("Client ")+uname+String(" is trying to upload a route to the server."));
  15.  
  16.     char *buffer = (char*)malloc( sizeof(char)*(npk.size+1));
  17.     //char *buffer = new char[npk.size+1];
  18.  
  19.     memset(buffer, NULL, (npk.size+1)*sizeof(char));
  20.  
  21.     ClientSocket->ReceiveBuf(buffer, (npk.size)*sizeof(char), MSG_WAITALL);
  22.  
  23.     std::string final_xml(buffer, npk.size);
  24.  
  25.     xml = new TXMLDocument(NULL);
  26.  
  27.  
  28.     std::vector<TForm1::Pair> pair = findXMLTag(buffer, "<Image>", npk.size+1);
  29.  
  30.     for(int i = pair.size() - 1; i >= 0 ; i--)
  31.     {
  32.         int new_size;
  33.         char* tmp = base64(&buffer[pair[i].start], pair[i].end - pair[i].start, &new_size);
  34.  
  35.         final_xml = binary_replace(final_xml, pair[i].start, pair[i].end, tmp);
  36.  
  37.         free(tmp);
  38.     }
  39.  
  40.     pair = findXMLTag(const_cast<char*>(final_xml.c_str()), "<Video>", final_xml.size());
  41.  
  42.     for(int i = pair.size() - 1; i >= 0; i--)
  43.     {
  44.         int new_size;
  45.         char* tmp = base64(&buffer[pair[i].start], pair[i].end - pair[i].start, &new_size);
  46.  
  47.         final_xml = binary_replace(final_xml, pair[i].start, pair[i].end, tmp);
  48.  
  49.         free(tmp);
  50.     }
  51.  
  52.     pair = findXMLTag(const_cast<char*>(final_xml.c_str()), "<Text>", final_xml.size());
  53.  
  54.     for(int i = pair.size() - 1; i >= 0; i--)
  55.     {
  56.         int new_size;
  57.         char* tmp = base64(&buffer[pair[i].start], pair[i].end - pair[i].start, &new_size);
  58.  
  59.         final_xml = binary_replace(final_xml, pair[i].start, pair[i].end, tmp);
  60.  
  61.         free(tmp);
  62.     }
  63.  
  64.     xml->LoadFromXML(AnsiString(final_xml.c_str()));
  65.  
  66.     _di_IXMLNodeList root = xml->ChildNodes->GetNode("root")->GetChildNodes();
  67.  
  68.     filename = xml->ChildNodes->GetNode("root")->GetAttribute("filename");
  69.     routeName = filename;
  70.  
  71.     filename = path + UnicodeString("uploadedRoutes\\") + filename;
  72.     filename += ".xml";
  73.     //Add the route into the database.
  74.     try
  75.     {
  76.         int userId;
  77.  
  78.         String SQLQuery = "SELECT idUsers FROM users WHERE Username = :uname";
  79.         TSQLQuery *SQL = new TSQLQuery(NULL);
  80.  
  81.         SQL->SQLConnection = SQLConnection;
  82.         SQL->SQL->Text = SQLQuery;
  83.  
  84.  
  85.         SQL->ParamByName("uname")->AsAnsiString =  map.find(ClientSocket->RemoteHost)->second.username;
  86.  
  87.         SQL->Open();
  88.  
  89.         SQL->First();
  90.  
  91.         userId = SQL->FieldByName("idUsers")->AsInteger;
  92.  
  93.         SQL->Close();
  94.  
  95.         SQLQuery = "INSERT INTO routes(routeName, userID) VALUES(:rtname, :userid)";
  96.         SQL->SQL->Text = SQLQuery;
  97.         SQL->ParamByName("rtname")->AsAnsiString = routeName;
  98.         SQL->ParamByName("userid")->AsInteger = userId;
  99.  
  100.         SQL->ExecSQL();
  101.  
  102.         SQLQuery = "SELECT MAX(idRoutes) as lastID FROM routes ";
  103.         SQL->SQL->Text = SQLQuery;
  104.         SQL->Open();
  105.         SQL->First();
  106.         lastRouteId = SQL->FieldByName("lastID")->AsInteger;
  107.         SQL->Close();
  108.  
  109.         delete SQL;
  110.     }
  111.     catch(Exception &e)
  112.     {
  113.         Memo2->Lines->Add(e.ToString());
  114.     }
  115.     //The route has been added into the database.
  116.  
  117.     //extract the info about each point(Lon, Lat), Image, Video, Text
  118.     //place those info in the float like class
  119.     //And then perform a douglas-peckeur run on those points.
  120.     //At last, reconstruct the XML file and store it, while updating the sql server.
  121.  
  122.     int child_count = root->GetCount();
  123.     for(int i = 0; i < child_count; i++)
  124.     {
  125.          int point_count;
  126.  
  127.          _di_IXMLNodeList point = root->GetNode(i)->GetChildNodes();
  128.  
  129.          point_count = point->GetCount();
  130.  
  131.          _di_IXMLNode node = point->GetNode("Coords");
  132.          String Lat, Lon, DateTime;
  133.  
  134.          Lat = node->GetChildNodes()->GetNode("Lat")->GetText();
  135.          Lon = node->GetChildNodes()->GetNode("Lon")->GetText();
  136.          DateTime = point->GetNode("DateTime")->GetText();
  137.  
  138.          try
  139.          {
  140.             String SQLQuery = "INSERT INTO waypoints(routeID, Lon, Lat, DateTime) VALUES(:rtid, :lon, :lat, :dt)";
  141.             TSQLQuery *SQL = new TSQLQuery(NULL);
  142.  
  143.             SQL->SQLConnection = SQLConnection;
  144.             SQL->SQL->Text = SQLQuery;
  145.  
  146.  
  147.             SQL->ParamByName("rtid")->AsInteger = lastRouteId;
  148.             SQL->ParamByName("lon")->AsAnsiString = Lon;
  149.             SQL->ParamByName("lat")->AsAnsiString = Lat;
  150.             SQL->ParamByName("dt")->AsAnsiString = DateTime;
  151.  
  152.             SQL->ExecSQL();
  153.  
  154.             delete SQL;
  155.          }
  156.          catch (Exception &e)
  157.          {
  158.             PageControl1->Canvas->Brush->Color = clRed;
  159.             Memo2->Lines->Add(e.ToString());
  160.          }
  161.  
  162.          String Data = point->GetNode("Text")->GetText();
  163.          handleData(Data, TypeOfMedia::Text);
  164.  
  165.          Data = point->GetNode("Image")->GetText();
  166.          handleData(Data, TypeOfMedia::Image);
  167.  
  168.          Data = point->GetNode("Video")->GetText();
  169.          handleData(Data, TypeOfMedia::Video);
  170.     }
  171.  
  172.     xml->SaveToFile(filename);
  173.  
  174.     xml->Active = false;
  175.  
  176.  
  177.     free(buffer);
  178.     delete xml;
  179.  
  180.     buildXML(lastRouteId);
  181.  
  182.     Memo1->Lines->Add(String("Client ")+uname+String(" has uploaded the route successfully."));
  183.  
  184.     //Finally since we got a new route we will add it to the routes xml.
  185.     buildRouteList(lastRouteId);
  186.  
  187.     ThreadLock->Leave();
  188. }
Advertisement
Add Comment
Please, Sign In to add comment