implement Optional Eventing in InMemory Stores for Push and WebRTC Connections and also Groups ...

This commit is contained in:
2025-07-14 06:00:29 +03:30
parent 2518bf1077
commit 6e1fb930a5
11 changed files with 184 additions and 72 deletions
+6 -3
View File
@@ -261,7 +261,8 @@ namespace xPushService.DI
{ {
// //
Log($"there is no provided PushGroupStore, try to register XPushGroupInMemoryStore ..."); Log($"there is no provided PushGroupStore, try to register XPushGroupInMemoryStore ...");
groupStore = new XPushGroupInMemoryStore(); var storeEvents = services.GetRegisteredService<IXPushGroupStoreEvents>();
groupStore = new XPushGroupInMemoryStore(storeEvents);
services.AddSingleton<IXPushGroupStore>(groupStore); services.AddSingleton<IXPushGroupStore>(groupStore);
} }
@@ -271,7 +272,8 @@ namespace xPushService.DI
{ {
// //
Log($"there is no provided XPushConnectionStore, try to register XPushConnectionInMemoryStore ..."); Log($"there is no provided XPushConnectionStore, try to register XPushConnectionInMemoryStore ...");
connectionStore = new XPushConnectionInMemoryStore(); var storeEvents = services.GetRegisteredService<IXPushConnectionStoreEvents>();
connectionStore = new XPushConnectionInMemoryStore(storeEvents);
services.AddSingleton<IXPushConnectionStore>(connectionStore); services.AddSingleton<IXPushConnectionStore>(connectionStore);
} }
@@ -281,7 +283,8 @@ namespace xPushService.DI
{ {
// //
Log($"there is no provided XWebRTCConnectionStore, try to register XWebRTCConnectionInMemoryStore ..."); Log($"there is no provided XWebRTCConnectionStore, try to register XWebRTCConnectionInMemoryStore ...");
webRtcConnectionStore = new XWebRTCConnectionInMemoryStore(); var storeEvents = services.GetRegisteredService<IXWebRTCConnectionStoreEvents>();
webRtcConnectionStore = new XWebRTCConnectionInMemoryStore(storeEvents);
services.AddSingleton<IXWebRTCConnectionStore>(webRtcConnectionStore); services.AddSingleton<IXWebRTCConnectionStore>(webRtcConnectionStore);
} }
#endregion #endregion
+9
View File
@@ -0,0 +1,9 @@
using xModels.Base;
using xPushService.Interfaces;
using xPushService.Models;
namespace xPushService.Events
{
public class XPushConnectionStoreEvents : XBaseStoreEvent<XPushConnectionDto, string>, IXPushConnectionStoreEvents
{ }
}
+9
View File
@@ -0,0 +1,9 @@
using xModels.Base;
using xPushService.Interfaces;
using xPushService.Models;
namespace xPushService.Events
{
public class XPushGroupStoreEvents : XBaseStoreEvent<XPushGroupDto, string>, IXPushGroupStoreEvents
{ }
}
+9
View File
@@ -0,0 +1,9 @@
using xModels.Base;
using xPushService.Interfaces;
using xPushService.Models;
namespace xPushService.Events
{
public class XWebRTCConnectionStoreEvents : XBaseStoreEvent<XWebRTCConnectionDto, string>, IXWebRTCConnectionStoreEvents
{ }
}
@@ -0,0 +1,8 @@
using xModels.Interfaces;
using xPushService.Models;
namespace xPushService.Interfaces
{
public interface IXPushConnectionStoreEvents : IXBaseStoreEvents<XPushConnectionDto, string>
{ }
}
+4 -2
View File
@@ -1,6 +1,8 @@
using xModels.Interfaces; using xModels.Interfaces;
using xPushService.Models; using xPushService.Models;
namespace xPushService.Interfaces { namespace xPushService.Interfaces
public interface IXPushGroupStore : IXBaseStore<XPushGroupDto, string> { } {
public interface IXPushGroupStore : IXBaseStore<XPushGroupDto, string>
{ }
} }
+8
View File
@@ -0,0 +1,8 @@
using xModels.Interfaces;
using xPushService.Models;
namespace xPushService.Interfaces
{
public interface IXPushGroupStoreEvents : IXBaseStoreEvents<XPushGroupDto, string>
{}
}
@@ -0,0 +1,8 @@
using xModels.Interfaces;
using xPushService.Models;
namespace xPushService.Interfaces
{
public interface IXWebRTCConnectionStoreEvents : IXBaseStoreEvents<XWebRTCConnectionDto, string>
{ }
}
+99 -61
View File
@@ -10,8 +10,17 @@ using xModels.Providers;
using xPushService.Interfaces; using xPushService.Interfaces;
using xPushService.Models; using xPushService.Models;
namespace xPushService.Store { namespace xPushService.Store
public class XPushConnectionInMemoryStore : XBaseInMemoryStore<XPushConnectionDto, string>, IXPushConnectionStore { {
public class XPushConnectionInMemoryStore : XBaseInMemoryStore<XPushConnectionDto, string>, IXPushConnectionStore
{
//
#region Constructor ...
public XPushConnectionInMemoryStore(
IXPushConnectionStoreEvents events = null
) : base(events) {}
#endregion
// //
#region Custom ... #region Custom ...
/// <summary> /// <summary>
@@ -19,8 +28,9 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="connectionId"></param> /// <param name="connectionId"></param>
/// <returns></returns> /// <returns></returns>
public Task<XPushConnectionDto> GetByConnectionId (string connectionId) { public Task<XPushConnectionDto> GetByConnectionId(string connectionId)
return Get (connectionId); {
return Get(connectionId);
} }
/// <summary> /// <summary>
@@ -28,8 +38,9 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public Task<IEnumerable<XPushConnectionDto>> GetByUserName (string userName) { public Task<IEnumerable<XPushConnectionDto>> GetByUserName(string userName)
return FindMany (c => c.User.ToNormalString () == userName.ToNormalString ()); {
return FindMany(c => c.User.ToNormalString() == userName.ToNormalString());
} }
/// <summary> /// <summary>
@@ -38,10 +49,11 @@ namespace xPushService.Store {
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="device"></param> /// <param name="device"></param>
/// <returns></returns> /// <returns></returns>
public Task<XPushConnectionDto> GetByUserDevice (string userName, XDeviceDto device) { public Task<XPushConnectionDto> GetByUserDevice(string userName, XDeviceDto device)
return FindOne (c => {
c.User.ToNormalString () == userName.ToNormalString () && return FindOne(c =>
c.Device.IsSameContent ( c.User.ToNormalString() == userName.ToNormalString() &&
c.Device.IsSameContent(
device, null, null, null, false device, null, null, null, false
) )
); );
@@ -52,8 +64,9 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="connectionId"></param> /// <param name="connectionId"></param>
/// <returns></returns> /// <returns></returns>
public Task<bool> IsExistsConnectionId (string connectionId) { public Task<bool> IsExistsConnectionId(string connectionId)
return IsExistsByKey (connectionId); {
return IsExistsByKey(connectionId);
} }
/// <summary> /// <summary>
@@ -61,15 +74,17 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public Task<bool> IsExistsUser (string userName) { public Task<bool> IsExistsUser(string userName)
return GetByUserName (userName) {
.ContinueWith (findTask => { return GetByUserName(userName)
.ContinueWith(findTask =>
{
// //
var findResult = findTask var findResult = findTask
.RunTask(); .RunTask();
// //
var result = findResult.Any (); var result = findResult.Any();
return result; return result;
}); });
} }
@@ -80,15 +95,17 @@ namespace xPushService.Store {
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="device"></param> /// <param name="device"></param>
/// <returns></returns> /// <returns></returns>
public Task<IEnumerable<XDeviceDto>> GetUserDevices (string userName) { public Task<IEnumerable<XDeviceDto>> GetUserDevices(string userName)
return GetByUserName (userName) {
.ContinueWith (userConnectionsTask => { return GetByUserName(userName)
.ContinueWith(userConnectionsTask =>
{
// //
var userConnections = userConnectionsTask var userConnections = userConnectionsTask
.RunTask(); .RunTask();
// //
var result = userConnections.Select (c => c.Device); var result = userConnections.Select(c => c.Device);
return result; return result;
}); });
} }
@@ -98,8 +115,9 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="connectionId"></param> /// <param name="connectionId"></param>
/// <returns></returns> /// <returns></returns>
public Task<bool> RemoveByConnectionId (string connectionId) { public Task<bool> RemoveByConnectionId(string connectionId)
return RemoveByKey (connectionId); {
return RemoveByKey(connectionId);
} }
/// <summary> /// <summary>
@@ -108,23 +126,26 @@ namespace xPushService.Store {
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="device"></param> /// <param name="device"></param>
/// <returns></returns> /// <returns></returns>
public Task<bool> RemoveByUserDevice (string userName, XDeviceDto device) { public Task<bool> RemoveByUserDevice(string userName, XDeviceDto device)
return GetByUserDevice ( {
return GetByUserDevice(
device: device, device: device,
userName: userName userName: userName
) )
.ContinueWith (connectionTask => { .ContinueWith(connectionTask =>
{
// //
var connection = connectionTask var connection = connectionTask
.RunTask(); .RunTask();
// //
if (connection.IsNull ()) { if (connection.IsNull())
{
return false; return false;
} }
// //
var result = Remove (connection) var result = Remove(connection)
.RunTask(); .RunTask();
return result; return result;
}); });
@@ -135,20 +156,24 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public Task<bool> RemoveUser (string userName) { public Task<bool> RemoveUser(string userName)
return GetByUserName (userName) {
.ContinueWith (connectionsTask => { return GetByUserName(userName)
.ContinueWith(connectionsTask =>
{
// //
var connections = connectionsTask var connections = connectionsTask
.RunTask(); .RunTask();
// //
if (!connections.HasChild ()) { if (!connections.HasChild())
{
return false; return false;
} }
// //
var result = RemoveMany (new XBaseRangeRequest<XPushConnectionDto> { var result = RemoveMany(new XBaseRangeRequest<XPushConnectionDto>
{
Items = connections Items = connections
}) })
.RunTask(); .RunTask();
@@ -163,16 +188,19 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="connectionId"></param> /// <param name="connectionId"></param>
/// <returns></returns> /// <returns></returns>
public Task<DateTime> GetLastSeenByConnectionId (string connectionId) { public Task<DateTime> GetLastSeenByConnectionId(string connectionId)
return GetByConnectionId (connectionId) {
.ContinueWith (connectionTask => { return GetByConnectionId(connectionId)
.ContinueWith(connectionTask =>
{
// //
var connection = connectionTask var connection = connectionTask
.RunTask(); .RunTask();
// //
if (connection.IsNull ()) { if (connection.IsNull())
throw XException.NotFound.ToException (); {
throw XException.NotFound.ToException();
} }
// //
@@ -186,22 +214,25 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public Task<DateTime> GetLastSeenByUserName (string userName) { public Task<DateTime> GetLastSeenByUserName(string userName)
return GetByUserName (userName) {
.ContinueWith (connectionsTask => { return GetByUserName(userName)
.ContinueWith(connectionsTask =>
{
// //
var connections = connectionsTask var connections = connectionsTask
.RunTask (); .RunTask();
// //
if (!connections.HasChild ()) { if (!connections.HasChild())
throw XException.NotFound.ToException (); {
throw XException.NotFound.ToException();
} }
// //
var result = connections var result = connections
.Select (c => c.LastSeen) .Select(c => c.LastSeen)
.Max (); .Max();
// //
return result; return result;
@@ -214,19 +245,22 @@ namespace xPushService.Store {
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="device"></param> /// <param name="device"></param>
/// <returns></returns> /// <returns></returns>
public Task<DateTime> GetLastSeenByUserDevice (string userName, XDeviceDto device) { public Task<DateTime> GetLastSeenByUserDevice(string userName, XDeviceDto device)
return GetByUserDevice ( {
return GetByUserDevice(
device: device, device: device,
userName: userName userName: userName
) )
.ContinueWith (connectionTask => { .ContinueWith(connectionTask =>
{
// //
var connection = connectionTask var connection = connectionTask
.RunTask (); .RunTask();
// //
if (connection.IsNull ()) { if (connection.IsNull())
throw XException.NotFound.ToException (); {
throw XException.NotFound.ToException();
} }
// //
@@ -240,9 +274,11 @@ namespace xPushService.Store {
/// </summary> /// </summary>
/// <param name="connectionId"></param> /// <param name="connectionId"></param>
/// <returns></returns> /// <returns></returns>
public Task<bool> UpdateLastSeen (string connectionId) { public Task<bool> UpdateLastSeen(string connectionId)
return IsExistsConnectionId (connectionId) {
.ContinueWith (isExistsTask => { return IsExistsConnectionId(connectionId)
.ContinueWith(isExistsTask =>
{
// //
// define empty result ... // define empty result ...
var result = false; var result = false;
@@ -254,25 +290,27 @@ namespace xPushService.Store {
// //
// check result is false ... // check result is false ...
if (!result) { if (!result)
{
return result; return result;
} }
// //
// retreive connection ... // retreive connection ...
var connection = GetByConnectionId (connectionId) var connection = GetByConnectionId(connectionId)
.RunTask(); .RunTask();
result = !connection.IsNull (); result = !connection.IsNull();
if (!result) { if (!result)
{
return result; return result;
} }
// //
// Update Connection last seen ... // Update Connection last seen ...
connection.LastSeen = DateTime.UtcNow; connection.LastSeen = DateTime.UtcNow;
var updatedConnection = Update (connection) var updatedConnection = Update(connection)
.RunTask (); .RunTask();
result = !updatedConnection.IsNull (); result = !updatedConnection.IsNull();
// //
return result; return result;
+11 -2
View File
@@ -2,6 +2,15 @@ using xModels.Providers;
using xPushService.Interfaces; using xPushService.Interfaces;
using xPushService.Models; using xPushService.Models;
namespace xPushService.Store { namespace xPushService.Store
public class XPushGroupInMemoryStore : XBaseInMemoryStore<XPushGroupDto, string>, IXPushGroupStore { } {
public class XPushGroupInMemoryStore : XBaseInMemoryStore<XPushGroupDto, string>, IXPushGroupStore
{
//
#region Constructor ...
public XPushGroupInMemoryStore(
IXPushGroupStoreEvents events = null
) : base(events) { }
#endregion
}
} }
+11 -2
View File
@@ -2,6 +2,15 @@ using xModels.Providers;
using xPushService.Interfaces; using xPushService.Interfaces;
using xPushService.Models; using xPushService.Models;
namespace xPushService.Store { namespace xPushService.Store
public class XWebRTCConnectionInMemoryStore : XBaseInMemoryStore<XWebRTCConnectionDto, string>, IXWebRTCConnectionStore { } {
public class XWebRTCConnectionInMemoryStore : XBaseInMemoryStore<XWebRTCConnectionDto, string>, IXWebRTCConnectionStore
{
//
#region Constructor ...
public XWebRTCConnectionInMemoryStore(
IXWebRTCConnectionStoreEvents events = null
) : base(events) { }
#endregion
}
} }