mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Added code to submit statistics.
This commit is contained in:
@@ -35,13 +35,65 @@
|
|||||||
// Copyright (C) 2011-2015 Claunia.com
|
// Copyright (C) 2011-2015 Claunia.com
|
||||||
// ****************************************************************************/
|
// ****************************************************************************/
|
||||||
// //$Id$
|
// //$Id$
|
||||||
|
using System.Threading;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using DiscImageChef.Console;
|
||||||
|
|
||||||
namespace DiscImageChef.Core
|
namespace DiscImageChef.Core
|
||||||
{
|
{
|
||||||
public static class Remote
|
public static class Remote
|
||||||
{
|
{
|
||||||
public static void SubmitReport(System.Xml.Serialization.XmlSerializer xmlSer)
|
public static void SubmitReport(Metadata.DeviceReport report)
|
||||||
{
|
{
|
||||||
// TODO: Implement this
|
Thread submitThread = new Thread(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
System.Console.WriteLine("Uploading device report");
|
||||||
|
#else
|
||||||
|
DicConsole.DebugWriteLine("Submit stats", "Uploading device report");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
MemoryStream xmlStream = new MemoryStream();
|
||||||
|
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Metadata.DeviceReport));
|
||||||
|
xmlSer.Serialize(xmlStream, report);
|
||||||
|
xmlStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
WebRequest request = WebRequest.Create("http://discimagechef.claunia.com/api/uploadreport");
|
||||||
|
((HttpWebRequest)request).UserAgent = string.Format("DiscImageChef {0}", typeof(Version).Assembly.GetName().Version);
|
||||||
|
request.Method = "POST";
|
||||||
|
request.ContentLength = xmlStream.Length;
|
||||||
|
request.ContentType = "application/xml";
|
||||||
|
Stream reqStream = request.GetRequestStream();
|
||||||
|
xmlStream.CopyTo(reqStream);
|
||||||
|
reqStream.Close();
|
||||||
|
WebResponse response = request.GetResponse();
|
||||||
|
|
||||||
|
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Stream data = response.GetResponseStream();
|
||||||
|
StreamReader reader = new StreamReader(data);
|
||||||
|
|
||||||
|
string responseFromServer = reader.ReadToEnd();
|
||||||
|
data.Close();
|
||||||
|
response.Close();
|
||||||
|
xmlStream.Close();
|
||||||
|
}
|
||||||
|
catch(WebException)
|
||||||
|
{
|
||||||
|
// Can't connect to the server, do nothing
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
throw;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
});
|
||||||
|
submitThread.Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,7 +130,77 @@ namespace DiscImageChef.Core
|
|||||||
|
|
||||||
public static void SubmitStats()
|
public static void SubmitStats()
|
||||||
{
|
{
|
||||||
// TODO: Implement it
|
Thread submitThread = new Thread(() =>
|
||||||
|
{
|
||||||
|
if(submitStatsLock)
|
||||||
|
return;
|
||||||
|
submitStatsLock = true;
|
||||||
|
|
||||||
|
var statsFiles = Directory.EnumerateFiles(Settings.Settings.StatsPath, "PartialStats_*.xml", SearchOption.TopDirectoryOnly);
|
||||||
|
|
||||||
|
foreach(string statsFile in statsFiles)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if(!File.Exists(statsFile))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Stats stats = new Stats();
|
||||||
|
|
||||||
|
// This can execute before debug console has been inited
|
||||||
|
#if DEBUG
|
||||||
|
System.Console.WriteLine("Uploading partial statistics file {0}", statsFile);
|
||||||
|
#else
|
||||||
|
DicConsole.DebugWriteLine("Submit stats", "Uploading partial statistics file {0}", statsFile);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
FileStream fs = new FileStream(statsFile, FileMode.Open, FileAccess.Read);
|
||||||
|
XmlSerializer xs = new XmlSerializer(stats.GetType());
|
||||||
|
stats = (Stats)xs.Deserialize(fs);
|
||||||
|
fs.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
WebRequest request = WebRequest.Create("http://discimagechef.claunia.com/api/uploadstats");
|
||||||
|
((HttpWebRequest)request).UserAgent = string.Format("DiscImageChef {0}", typeof(Version).Assembly.GetName().Version);
|
||||||
|
request.Method = "POST";
|
||||||
|
request.ContentLength = fs.Length;
|
||||||
|
request.ContentType = "application/xml";
|
||||||
|
Stream reqStream = request.GetRequestStream();
|
||||||
|
fs.CopyTo(reqStream);
|
||||||
|
reqStream.Close();
|
||||||
|
WebResponse response = request.GetResponse();
|
||||||
|
|
||||||
|
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Stream data = response.GetResponseStream();
|
||||||
|
StreamReader reader = new StreamReader(data);
|
||||||
|
|
||||||
|
string responseFromServer = reader.ReadToEnd();
|
||||||
|
data.Close();
|
||||||
|
response.Close();
|
||||||
|
fs.Close();
|
||||||
|
if(responseFromServer == "ok")
|
||||||
|
File.Delete(statsFile);
|
||||||
|
}
|
||||||
|
catch(WebException)
|
||||||
|
{
|
||||||
|
// Can't connect to the server, postpone til next try
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
submitStatsLock = false;
|
||||||
|
throw;
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
submitStatsLock = false;
|
||||||
|
});
|
||||||
|
submitThread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddCommand(string command)
|
public static void AddCommand(string command)
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
* Commands/DeviceReport.cs:
|
||||||
|
Added code to submit statistics.
|
||||||
|
|
||||||
* ChangeLog:
|
* ChangeLog:
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ namespace DiscImageChef.Commands
|
|||||||
|
|
||||||
if(Settings.Settings.Current.ShareReports)
|
if(Settings.Settings.Current.ShareReports)
|
||||||
{
|
{
|
||||||
Remote.SubmitReport(xmlSer);
|
Remote.SubmitReport(report);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user