From 3063e9db31f5724a8ab1d6b530aed9819b65b0cd Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 7 Jul 2013 04:35:04 +0000 Subject: [PATCH] Moved mail transmission to backend's actual loop. Fingers crossed... --- Backend/Backend.csproj | 1 + Backend/CommsTransmitter.cs | 54 +++++++++++++++++++ Backend/Program.cs | 2 + Database/Database-OutboundCommRecipients.cs | 34 ++++++++++++ Database/Database-OutboundComms.cs | 32 +++++++++++ Logic/Communications/OutboundComm.cs | 22 ++++++++ Logic/Communications/OutboundCommRecipient.cs | 17 ++++++ 7 files changed, 162 insertions(+) create mode 100644 Backend/CommsTransmitter.cs diff --git a/Backend/Backend.csproj b/Backend/Backend.csproj index fc28deb6..8f08f6b6 100644 --- a/Backend/Backend.csproj +++ b/Backend/Backend.csproj @@ -64,6 +64,7 @@ + diff --git a/Backend/CommsTransmitter.cs b/Backend/CommsTransmitter.cs new file mode 100644 index 00000000..a8c1a87c --- /dev/null +++ b/Backend/CommsTransmitter.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Swarmops.Logic.Communications; +using Swarmops.Logic.Communications.Transmission; + +namespace Swarmops.Backend +{ + internal class CommsTransmitter + { + static internal void Run() + { + OutboundComms comms = OutboundComms.GetOpen(); + + foreach (OutboundComm comm in comms) + { + // TODO: RESOLVE RECIPIENTS + + if (!comm.Resolved) + { + continue; + } + + if (comm.TransmitterClass != "Swarmops.Utility.Communications.CommsTransmitterMail") + { + throw new NotImplementedException(); + } + + ICommsTransmitter transmitter = new Swarmops.Utility.Communications.CommsTransmitterMail(); + + OutboundCommRecipients recipients = comm.Recipients; + PayloadEnvelope envelope = PayloadEnvelope.FromXml(comm.PayloadXml); + + comm.StartTransmission(); + + foreach (OutboundCommRecipient recipient in recipients) + { + try + { + transmitter.Transmit(envelope, recipient.Person); + recipient.CloseSuccess(); + } + catch (OutboundCommTransmitException e) + { + recipient.CloseFailed(e.Description); + } + } + + comm.Open = false; + } + } + } +} diff --git a/Backend/Program.cs b/Backend/Program.cs index 0fb46f36..494911b0 100644 --- a/Backend/Program.cs +++ b/Backend/Program.cs @@ -271,6 +271,8 @@ namespace Swarmops.Backend { BotLog.Write(0, "MainCycle", "Ten-second entry"); + CommsTransmitter.Run(); + try { /*TestTrace("Running EventProcessor.Run()..."); diff --git a/Database/Database-OutboundCommRecipients.cs b/Database/Database-OutboundCommRecipients.cs index f5abbf6b..3076bad6 100644 --- a/Database/Database-OutboundCommRecipients.cs +++ b/Database/Database-OutboundCommRecipients.cs @@ -157,6 +157,40 @@ namespace Swarmops.Database } } + public void SetOutboundCommRecipientClosed (int outboundCommRecipientId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetOutboundCommRecipientClosed", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "outboundCommRecipientId", outboundCommRecipientId); + AddParameterWithName(command, "dateTime", DateTime.UtcNow); + + command.ExecuteNonQuery(); + } + } + + public int SetOutboundCommRecipientFailed(int outboundCommRecipientId, string failReason) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetOutboundCommRecipientFailed", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "outboundCommId", outboundCommRecipientId); + AddParameterWithName(command, "dateTime", DateTime.UtcNow); + AddParameterWithName(command, "failReason", failReason); + + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + #endregion diff --git a/Database/Database-OutboundComms.cs b/Database/Database-OutboundComms.cs index e8d0d2d6..ae97fe08 100644 --- a/Database/Database-OutboundComms.cs +++ b/Database/Database-OutboundComms.cs @@ -241,6 +241,38 @@ namespace Swarmops.Database } } + public void SetOutboundCommTransmissionStart(int outboundCommId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetOutboundCommTransmissionStart", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "outboundCommId", outboundCommId); + AddParameterWithName(command, "dateTime", DateTime.UtcNow); + + command.ExecuteNonQuery(); + } + } + + public void SetOutboundCommClosed(int outboundCommId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetOutboundCommClosed", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "outboundCommId", outboundCommId); + AddParameterWithName(command, "dateTime", DateTime.UtcNow); + + command.ExecuteNonQuery(); + } + } + #endregion diff --git a/Logic/Communications/OutboundComm.cs b/Logic/Communications/OutboundComm.cs index 5a736a57..52267693 100644 --- a/Logic/Communications/OutboundComm.cs +++ b/Logic/Communications/OutboundComm.cs @@ -128,6 +128,28 @@ namespace Swarmops.Logic.Communications get { return OutboundCommRecipients.ForOutboundComm(this); } } + public new bool Open + { + get { return base.Open; } + set + { + if (base.Open && value == false) + { + SwarmDb.GetDatabaseForWriting().SetOutboundCommClosed(this.Identity); + base.Open = false; + } + else + { + throw new InvalidOperationException("Can only change OutboundComms.Open from true to false"); + } + } + } + + public void StartTransmission() + { + SwarmDb.GetDatabaseForWriting().SetOutboundCommTransmissionStart(this.Identity); + } + } public enum CommResolverClass diff --git a/Logic/Communications/OutboundCommRecipient.cs b/Logic/Communications/OutboundCommRecipient.cs index 1b313ef3..1b01f47a 100644 --- a/Logic/Communications/OutboundCommRecipient.cs +++ b/Logic/Communications/OutboundCommRecipient.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Swarmops.Basic.Types.Communications; +using Swarmops.Database; using Swarmops.Logic.Swarm; namespace Swarmops.Logic.Communications @@ -24,6 +25,22 @@ namespace Swarmops.Logic.Communications get { return Person.FromIdentity(base.PersonId); } } + public void CloseSuccess() + { + SwarmDb.GetDatabaseForWriting().SetOutboundCommRecipientClosed(this.Identity); + + base.Success = true; + base.Open = false; + } + + public void CloseFailed(string failReason) + { + SwarmDb.GetDatabaseForWriting().SetOutboundCommRecipientFailed(this.Identity, failReason); + + base.Success = false; + base.Open = false; + } + // TODO: CloseSuccess, CloseFail } }