Added full chain of P&L notifications

This commit is contained in:
Falkvinge
2017-05-16 08:13:16 +02:00
parent c61bff3866
commit cc40100824
7 changed files with 204 additions and 18 deletions
+13 -1
View File
@@ -13,6 +13,7 @@ using Swarmops.Common.Enums;
using Swarmops.Logic.Communications;
using Swarmops.Logic.Communications.Payload;
using Swarmops.Common.ExtensionMethods;
using Swarmops.Logic;
using Swarmops.Logic.Security;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
@@ -343,7 +344,7 @@ namespace Swarmops.Frontend.Socket
{
Console.WriteLine(" - Backend socket error: " + args.Message);
// TODO: Try reconnecting
// TODO: Try reconnecting if closed or never opened
}
@@ -408,6 +409,17 @@ namespace Swarmops.Frontend.Socket
return false;
}
public static void SendMessageUpstream(SocketMessage message)
{
JObject json = new JObject();
json["ServerRequest"] = "Metapackage";
json["XmlData"] = message.ToXml();
_backendSocket.Send(json.ToString());
}
private static Dictionary<FrontendMalfunctions,bool> _activeAlarms;
internal static WebSocketServer SocketServer { get { return _socketServer; } }
+34 -14
View File
@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Swarmops.Frontend.Socket;
using Swarmops.Logic;
using Swarmops.Logic.Security;
using WebSocketSharp;
using WebSocketSharp.Server;
@@ -39,6 +40,9 @@ namespace Swarmops.Frontend.Socket
case "AddBitcoinAddress":
FrontendLoop.AddBitcoinAddress((string) json["Address"]);
break;
case "Metapackage":
ProcessMetapackage((string) json["XmlData"]);
break;
case "Ping":
// TODO: Request heartbeat from backend
// Sessions.Broadcast("{\"messageType\":\"Heartbeat\"}");
@@ -49,30 +53,46 @@ namespace Swarmops.Frontend.Socket
}
}
private void ProcessMetapackage(string xmlData)
{
SocketMessage message = SocketMessage.FromXml(xmlData);
// Most should just be sent straight to backend. Some could possibly be processed already here.
switch (message.MessageType)
{
case "...":
// some future to-be-defined processing
break;
default:
// we're not handling here, send to backend
FrontendLoop.SendMessageUpstream(message);
break;
}
}
protected override void OnOpen()
{
Console.WriteLine(" * Attempted socket connection");
string authBase64 = Context.QueryString["Auth"];
// TODO: Check for valid system authority token
authBase64 = Uri.UnescapeDataString (authBase64); // Defensive programming - % sign does not exist in base64 so this won't ever collapse a working encoding
_authority = Authority.FromEncryptedXml (authBase64);
if (Authority.IsSystemAuthorityTokenValid(authBase64))
{
_authority = null; // System authority
}
else
{
_authority = Authority.FromEncryptedXml(authBase64);
this.Sessions.RegisterSession(this, this._authority); // Only register non-system sessions
Console.WriteLine(" - - authenticated: " + this._authority.Person.Canonical);
}
Console.WriteLine(" - - authenticated: " + this._authority.Person.Canonical);
this.Sessions.RegisterSession(this, this._authority);
base.OnOpen();
JObject json = new JObject();
json ["MessageType"] = json["messageType"] = "AnnualProfitLossCents";
json["ProfitLossCents"] = this._authority.Organization.GetProfitLossCents().ToString(CultureInfo.InvariantCulture);
json["OrganizationId"] = this._authority.Organization.Identity;
this.Send(json.ToString());
}
protected override void OnClose (CloseEventArgs e)