[chronojump] Added missing file from last commit
- From: Xavier de Blas <xaviblas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] Added missing file from last commit
- Date: Tue, 18 Jun 2019 12:11:22 +0000 (UTC)
commit 36a71fad464a6c12978d428346c03a88da775ce9
Author: Xavier de Blas <xaviblas gmail com>
Date: Tue Jun 18 14:11:06 2019 +0200
Added missing file from last commit
src/json/json.cs | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 333 insertions(+)
---
diff --git a/src/json/json.cs b/src/json/json.cs
new file mode 100644
index 00000000..0811596c
--- /dev/null
+++ b/src/json/json.cs
@@ -0,0 +1,333 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * ChronoJump is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Copyright (C) 2016-2017 Carles Pina & Xavier de Blas
+ */
+
+using System;
+using System.Net;
+using System.Web;
+using System.IO;
+using System.Json;
+using System.Text;
+using System.Collections;
+using System.Collections.Generic; //Dictionary
+using Mono.Unix;
+
+
+public class Json
+{
+ public string ResultMessage;
+
+ protected bool connected; //know if server is connected. Do it when there's a change on RFID (pulse)
+ protected WebRequest request; //generic request (for all methods except ping)
+ protected static string serverUrl = "http://api.chronojump.org:8080";
+ //string serverUrl = "http://192.168.200.1:8080";
+
+ public static void ChangeServerUrl(string url)
+ {
+ serverUrl = url;
+ }
+
+ public Json()
+ {
+ ResultMessage = "";
+ }
+
+ public bool PostCrashLog(string email, string comments)
+ {
+ string filePath = UtilAll.GetLogFileOld();
+
+ if(! File.Exists(filePath)) {
+ this.ResultMessage = Catalog.GetString("Could not send file.\nIt does not exist.");
+ return false;
+ }
+
+ if(comments != null && comments != "")
+ Util.InsertTextBeginningOfFile(
+ "----------\nUser comments:\n" + comments + "\n----------\n",
filePath);
+
+ // Create a request using a URL that can receive a post.
+ if (! createWebRequest(requestType.GENERIC, "/backtrace/" +
UtilAll.ReadVersionFromBuildInfo() + "-" + email))
+ return false;
+
+ // Set the Method property of the request to POST.
+ request.Method = "POST";
+
+ // Create POST data and convert it to a byte array.
+ byte[] byteArray = readFile(filePath);
+
+ // Set the ContentType property of the WebRequest.
+ request.ContentType = "application/x-www-form-urlencoded";
+
+ // Set the ContentLength property of the WebRequest.
+ request.ContentLength = byteArray.Length;
+
+ // Get the request stream.
+ Stream dataStream;
+ if(! getWebRequestStream (request, out dataStream, Catalog.GetString("Could not send file.")))
+ return false;
+
+ // Write the data to the request stream.
+ dataStream.Write (byteArray, 0, byteArray.Length);
+
+ // Close the Stream object.
+ dataStream.Close ();
+
+ // Get the response.
+ WebResponse response;
+ if(! getWebResponse (request, out response, Catalog.GetString("Could not send file.")))
+ return false;
+
+
+ // Display the status.
+ LogB.Information(((HttpWebResponse)response).StatusDescription);
+
+ // Get the stream containing content returned by the server.
+ dataStream = response.GetResponseStream ();
+
+ // Open the stream using a StreamReader for easy access.
+ StreamReader reader = new StreamReader (dataStream);
+
+ // Read the content.
+ string responseFromServer = reader.ReadToEnd ();
+
+ // Display the content.
+ LogB.Information(responseFromServer);
+
+ // Clean up the streams.
+ reader.Close ();
+ dataStream.Close ();
+ response.Close ();
+
+
+ JsonValue result = JsonValue.Parse(responseFromServer);
+ string crash_id = result["crash_id"];
+ LogB.Information("crash_id: ", crash_id);
+
+ this.ResultMessage = Catalog.GetString("Log sent. Thank you.");
+ return true;
+ }
+
+ private byte[] readFile(string filePath)
+ {
+ return System.IO.File.ReadAllBytes(filePath);
+ }
+
+
+ //public bool ChronojumpUpdated = true;
+ public bool GetLastVersion(string currentVersion)
+ {
+ // Create a request using a URL that can receive a post.
+ if (! createWebRequest(requestType.GENERIC, "/version"))
+ return false;
+
+ // Set the Method property of the request to GET.
+ request.Method = "GET";
+
+ // Set the ContentType property of the WebRequest.
+ //request.ContentType = "application/x-www-form-urlencoded";
+
+ HttpWebResponse response;
+ if(! getHttpWebResponse (request, out response, Catalog.GetString("Could not get last
version.")))
+ return false;
+
+ string responseFromServer;
+ using (var sr = new StreamReader(response.GetResponseStream()))
+ {
+ responseFromServer = sr.ReadToEnd();
+ }
+
+ //this prints:
+ // {"stable": "1.4.9"}
+ //this.ResultMessage = "Last version published: " + responseFromServer;
+
+ string [] strFull = responseFromServer.Split(new char[] {':'});
+ int startPos = strFull[1].IndexOf('"') +1;
+ int endPos = strFull[1].LastIndexOf('"') -2;
+
+ string lastVersionPublished = strFull[1].Substring(startPos,endPos); //1.4.9
+
+ /*
+ string updateStr = "";
+ if(currentVersion != lastVersionPublished)
+ updateStr = "\n\n" + Catalog.GetString("Update software at ") + "www.chronojump.org";
+ */
+
+ string str =
+ Catalog.GetString("Installed version is: ") + currentVersion + "\n" +
+ Catalog.GetString("Last version published: ") + lastVersionPublished;
+
+ VersionCompare vCompare = new VersionCompare(
+ new Version31(currentVersion),
+ new Version31(lastVersionPublished));
+ str += "\n\n" + vCompare.ResultStr;
+
+ this.ResultMessage = str;
+
+ //ChronojumpUpdated = (currentVersion == ResultMessage);
+
+ return true;
+ }
+
+ /*
+ * if software just started, ping gets stuck by network problems, and user try to exit software,
+ * thread.Abort doesn't kill the thread properly
+ * just kill the webRequest
+ */
+ WebRequest requestPing;
+ bool requestPingAborting;
+
+ public void PingAbort()
+ {
+ requestPingAborting = true;
+ if(requestPing != null)
+ requestPing.Abort(); //cancel an asynchronous request
+ }
+ public bool Ping(string osVersion, string cjVersion, string machineID)
+ {
+ requestPingAborting = false;
+
+ if (! createWebRequest(requestType.PING, "/ping"))
+ return false;
+
+ // Set the Method property of the request to POST.
+ requestPing.Method = "POST";
+
+ // Set the ContentType property of the WebRequest.
+ requestPing.ContentType = "application/json";
+
+ // Creates the json object
+ JsonObject json = new JsonObject();
+ json.Add("os_version", osVersion);
+ json.Add("cj_version", cjVersion);
+ json.Add("machine_id", machineID);
+
+ // Converts it to a String
+ String js = json.ToString();
+
+ // Writes the json object into the request dataStream
+ Stream dataStream;
+ if(! getWebRequestStream (requestPing, out dataStream, "Could not send ping (A)."))
+ return false;
+
+ if(requestPingAborting) {
+ LogB.Information("Aborted from PingAbort");
+ return false;
+ }
+
+ dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
+
+ dataStream.Close ();
+
+ // Get the response.
+ WebResponse response;
+ if(! getWebResponse (requestPing, out response, "Could not send ping (B)."))
+ return false;
+
+ if(requestPingAborting) {
+ LogB.Information("Aborted from PingAbort");
+ return false;
+ }
+
+ // Display the status (will be 201, CREATED)
+ Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+
+ // Clean up the streams.
+ dataStream.Close ();
+ response.Close ();
+
+ this.ResultMessage = "Ping sent.";
+ return true;
+ }
+
+ protected enum requestType { GENERIC, PING };
+
+ protected bool createWebRequest(Json.requestType rt, string webService)
+ {
+ try {
+ if(rt == Json.requestType.GENERIC)
+ request = WebRequest.Create (serverUrl + webService);
+ else //(rt == Json.requestType.PING)
+ requestPing = WebRequest.Create (serverUrl + webService);
+
+ } catch {
+ this.ResultMessage =
+ string.Format(Catalog.GetString("You are not connected to the Internet\nor
{0} server is down."),
+ serverUrl) + "\n\nMaybe proxy is not configured";
+ //System.Net.WebRequest.GetSystemWebProxy System.NullReferenceException: Object
reference not set to an instance of an object
+ return false;
+ }
+
+ return true;
+ }
+
+ protected bool getWebRequestStream(WebRequest req, out Stream dataStream, string errMessage)
+ {
+ dataStream = null;
+
+ try {
+ dataStream = req.GetRequestStream ();
+ } catch {
+ this.ResultMessage = errMessage + "\n" +
+ string.Format(Catalog.GetString("You are not connected to the Internet\nor
{0} server is down."),
+ serverUrl);
+ return false;
+ }
+
+ return true;
+ }
+
+ protected bool getWebResponse(WebRequest req, out WebResponse response, string errMessage)
+ {
+ response = null;
+
+ try {
+ response = req.GetResponse ();
+ } catch {
+ this.ResultMessage = errMessage + "\n" +
+ string.Format(Catalog.GetString("You are not connected to the Internet\nor
{0} server is down."),
+ serverUrl);
+ return false;
+ }
+
+ return true;
+ }
+
+ protected bool getHttpWebResponse(WebRequest req, out HttpWebResponse response, string errMessage)
+ {
+ response = null;
+
+ try {
+ response = (HttpWebResponse) req.GetResponse ();
+ } catch {
+ this.ResultMessage = errMessage + "\n" +
+ string.Format(Catalog.GetString("You are not connected to the Internet\nor
{0} server is down."),
+ serverUrl);
+ return false;
+ }
+
+ return true;
+ }
+
+ public bool Connected {
+ get { return connected; }
+ }
+
+ ~Json() {}
+}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]