forked from centaurus-project/centaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76148e
commit 848c34f
Showing
26 changed files
with
436 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Centaurus.Domain | ||
{ | ||
public class ExtensionConfig | ||
{ | ||
public List<ExtensionConfigItem> Extensions { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Centaurus.Domain | ||
{ | ||
public class ExtensionConfigItem | ||
{ | ||
public bool IsDisabled { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public Dictionary<string, string> ExtensionConfig { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace Centaurus.Domain | ||
{ | ||
public class ExtensionItem | ||
{ | ||
private ExtensionItem(string name, Version version, Dictionary<string, string> config, IExtension extensionInstance) | ||
{ | ||
Name = name; | ||
Version = version; | ||
ExtensionInstance = extensionInstance; | ||
Config = config; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public Version Version { get; } | ||
|
||
public IExtension ExtensionInstance { get; } | ||
|
||
public Dictionary<string, string> Config { get; } | ||
|
||
public static ExtensionItem Load(ExtensionConfigItem extensionConfigItem) | ||
{ | ||
var extensionPath = Path.Combine(Path.GetDirectoryName(Global.Settings.ExtensionsConfigFilePath), extensionConfigItem.Name + ".dll"); | ||
if (!File.Exists(extensionPath)) | ||
throw new Exception($"Extension {extensionConfigItem.Name} is not found."); | ||
var extensionAssenmbly = Assembly.LoadFile(extensionPath); | ||
var extensionTypes = extensionAssenmbly.GetTypes().Where(t => typeof(IExtension).IsAssignableFrom(t)); | ||
if (extensionTypes.Count() < 1) | ||
throw new Exception($"Extension {extensionConfigItem.Name} doesn't contain types that implement IExtension interface."); | ||
else if (extensionTypes.Count() > 1) | ||
throw new Exception($"Extension {extensionConfigItem.Name} contains multiple types that implement IExtension interface."); | ||
|
||
return new ExtensionItem(extensionConfigItem.Name, extensionAssenmbly.GetName().Version, extensionConfigItem.ExtensionConfig, (IExtension)Activator.CreateInstance(extensionTypes.First())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
using Centaurus.Models; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Centaurus.Domain | ||
{ | ||
public class EnvelopeEventArgs | ||
{ | ||
public BaseWebSocketConnection Connection { get; set; } | ||
|
||
public MessageEnvelope Message { get; set; } | ||
|
||
} | ||
|
||
public class EnvelopeErrorEventArgs : EnvelopeEventArgs | ||
{ | ||
public Exception Exception { get; set; } | ||
} | ||
|
||
public class NotifyEventArgs | ||
{ | ||
public RawPubKey Account { get; set; } | ||
public MessageEnvelope Envelope { get; set; } | ||
} | ||
|
||
public class ExtensionsManager | ||
{ | ||
public async Task RegisterAllExtensions() | ||
{ | ||
if (string.IsNullOrWhiteSpace(Global.Settings.ExtensionsConfigFilePath)) | ||
return; | ||
|
||
var configFilePath = Path.GetFullPath(Global.Settings.ExtensionsConfigFilePath); | ||
if (!File.Exists(configFilePath)) | ||
throw new Exception("Extensions config file is not found."); | ||
|
||
var extensionConfig = JsonConvert.DeserializeObject<ExtensionConfig>(File.ReadAllText(configFilePath)); | ||
|
||
foreach (var configItem in extensionConfig.Extensions) | ||
{ | ||
var extension = ExtensionItem.Load(configItem); | ||
await extension.ExtensionInstance.Init(configItem.ExtensionConfig); | ||
|
||
extensions.Add(extension); | ||
} | ||
} | ||
|
||
private List<ExtensionItem> extensions = new List<ExtensionItem>(); | ||
|
||
public IEnumerable<ExtensionItem> Extensions => extensions; | ||
|
||
public event EventHandler<WebSocket> OnBeforeNewConnection; | ||
public event EventHandler<BaseWebSocketConnection> OnConnectionValidated; | ||
public void ConnectionValidated(BaseWebSocketConnection args) | ||
{ | ||
OnConnectionValidated?.Invoke(this, args); | ||
} | ||
public void BeforeNewConnection(WebSocket args) | ||
{ | ||
OnBeforeNewConnection?.Invoke(this, args); | ||
} | ||
|
||
public event EventHandler<EnvelopeErrorEventArgs> OnHandleMessageFailed; | ||
public void HandleMessageFailed(EnvelopeErrorEventArgs args) | ||
{ | ||
OnHandleMessageFailed?.Invoke(this, args); | ||
} | ||
|
||
public event EventHandler<EnvelopeEventArgs> OnBeforeSendMessage; | ||
public event EventHandler<EnvelopeEventArgs> OnAfterSendMessage; | ||
public event EventHandler<EnvelopeErrorEventArgs> OnSendMessageFailed; | ||
public void BeforeSendMessage(EnvelopeEventArgs args) | ||
{ | ||
OnBeforeSendMessage?.Invoke(this, args); | ||
} | ||
public void AfterSendMessage(EnvelopeEventArgs args) | ||
{ | ||
OnAfterSendMessage?.Invoke(this, args); | ||
} | ||
public void SendMessageFailed(EnvelopeErrorEventArgs args) | ||
{ | ||
OnSendMessageFailed?.Invoke(this, args); | ||
} | ||
|
||
public event EventHandler<BaseWebSocketConnection> OnBeforeConnectionClose; | ||
|
||
public void BeforeConnectionClose(BaseWebSocketConnection args) | ||
{ | ||
OnBeforeConnectionClose?.Invoke(this, args); | ||
} | ||
|
||
public event EventHandler<NotifyEventArgs> OnBeforeNotify; | ||
public event EventHandler<MessageEnvelope> OnBeforeNotifyAuditors; | ||
|
||
|
||
public void BeforeNotify(NotifyEventArgs args) | ||
{ | ||
OnBeforeNotify?.Invoke(this, args); | ||
} | ||
|
||
public void BeforeNotifyAuditors(MessageEnvelope args) | ||
{ | ||
OnBeforeNotifyAuditors?.Invoke(this, args); | ||
} | ||
|
||
public event EventHandler<EnvelopeEventArgs> OnBeforeValidateMessage; | ||
public event EventHandler<EnvelopeEventArgs> OnAfterValidateMessage; | ||
public event EventHandler<EnvelopeEventArgs> OnBeforeHandleMessage; | ||
public event EventHandler<EnvelopeEventArgs> OnAfterHandleMessage; | ||
|
||
public void BeforeValidateMessage(EnvelopeEventArgs args) | ||
{ | ||
OnBeforeValidateMessage?.Invoke(this, args); | ||
} | ||
|
||
public void AfterValidateMessage(EnvelopeEventArgs args) | ||
{ | ||
OnAfterValidateMessage?.Invoke(this, args); | ||
} | ||
|
||
public void BeforeHandleMessage(EnvelopeEventArgs args) | ||
{ | ||
OnBeforeHandleMessage?.Invoke(this, args); | ||
} | ||
|
||
public void AfterHandleMessage(EnvelopeEventArgs args) | ||
{ | ||
OnAfterHandleMessage?.Invoke(this, args); | ||
} | ||
|
||
public event EventHandler<MessageEnvelope> OnBeforeQuantumHandle; | ||
public event EventHandler<ResultMessage> OnAfterQuantumHandle; | ||
public void BeforeQuantumHandle(MessageEnvelope args) | ||
{ | ||
OnBeforeQuantumHandle?.Invoke(this, args); | ||
} | ||
|
||
public void AfterQuantumHandle(ResultMessage args) | ||
{ | ||
OnAfterQuantumHandle?.Invoke(this, args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Centaurus.Domain | ||
{ | ||
public interface IExtension | ||
{ | ||
Task Init(Dictionary<string, string> settings); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.