Action dispatcher

This commit is contained in:
danielgrabowski 2019-11-05 17:00:36 +01:00
parent ff77cbbe1d
commit 2e624d9b7a
13 changed files with 120 additions and 42 deletions

View File

@ -19,7 +19,7 @@ namespace Squirrowse.Client
services.AddHostedService<Worker>();
services.AddSingleton<IConnectionManager>(x=> new ConnectionManager("http://localhost", 5000)); //keep as transient for now
services.AddSingleton< ICameraService, CameraService >(x=>new CameraService(new Camera()));
services.AddTransient<IStreamService, StreamService>();
services.AddTransient<IActionDispatcher, ActionDispatcher>();
});
}

View File

@ -6,18 +6,23 @@ using Squirrowse.Core.Models;
namespace Squirrowse.Client.Service
{
public class StreamService : IStreamService
public class ActionDispatcher : IActionDispatcher
{
private readonly IConnectionManager connectionManager;
private readonly ILogger<StreamService> logger;
private readonly ILogger<ActionDispatcher> logger;
private readonly HubConnection session;
public StreamService(ILogger<StreamService> logger, IConnectionManager connectionManager)
public ActionDispatcher(ILogger<ActionDispatcher> logger, IConnectionManager connectionManager)
{
this.connectionManager = connectionManager;
this.logger = logger;
session = connectionManager.Connect();
session.StartAsync();
session.On("Start",StartStream);
session.On("Stop",StopStream);
}
public Task StopStream()
{
throw new System.NotImplementedException();
}
public async Task SayHello()
@ -32,5 +37,10 @@ namespace Squirrowse.Client.Service
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
}
public Task StartStream()
{
throw new System.NotImplementedException();
}
}
}

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
namespace Squirrowse.Client.Service
@ -6,6 +8,7 @@ namespace Squirrowse.Client.Service
public class ConnectionManager : IConnectionManager
{
private readonly HubConnection _connection;
private bool Connected;
public ConnectionManager(string url, int port)
{
@ -14,11 +17,25 @@ namespace Squirrowse.Client.Service
.AddMessagePackProtocol()
.WithAutomaticReconnect()
.Build();
}
public HubConnection Connect()
public async Task<HubConnection> GetConnection()
{
if (Connected)
{
return _connection;
}
await _connection.StartAsync();
await RegisterOnHub();
Connected = true;
return _connection;
}
private async Task RegisterOnHub()
{
await _connection.SendAsync("AddUser", Environment.UserName);
}
}
}

View File

@ -3,9 +3,11 @@ using System.Threading.Tasks;
namespace Squirrowse.Client.Service
{
public interface IStreamService
public interface IActionDispatcher
{
Task SendStreamAsync(IAsyncEnumerable<byte[]> asb);
Task StartStream();
Task StopStream();
Task SayHello();
}
}

View File

@ -1,9 +1,10 @@
using Microsoft.AspNetCore.SignalR.Client;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
namespace Squirrowse.Client.Service
{
public interface IConnectionManager
{
HubConnection Connect();
Task<HubConnection> GetConnection();
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
@ -11,13 +12,13 @@ namespace Squirrowse.Client
public class Worker : BackgroundService
{
private readonly ILogger<Worker> logger;
private readonly IStreamService streamService;
private readonly IConnectionManager _connectionManager;
private readonly ICameraService camera;
public Worker(ILogger<Worker> logger, IStreamService streamService, ICameraService camera)
public Worker(ILogger<Worker> logger, IConnectionManager connectionManager, ICameraService camera)
{
this.logger = logger;
this.streamService = streamService;
this._connectionManager = connectionManager;
this.camera = camera;
}
@ -28,10 +29,10 @@ namespace Squirrowse.Client
{
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await _connectionManager.GetConnection();
//await _connectionManager.SendStreamAsync(camera.GetFramesAsyncEnumerator());
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator());
// await Task.Delay(50, stoppingToken);
// await Task.Delay(50, stoppingToken);
}
}
}

View File

@ -0,0 +1,14 @@
namespace Squirrowse.Core.Models
{
public class User
{
public string ConnectionId { get; set; }
public string AgentName { get; set; }
public User(string connectionId, string agentName)
{
ConnectionId = connectionId;
AgentName = agentName;
}
}
}

View File

@ -6,7 +6,7 @@ namespace Squirrowse.Service.Hubs
{
public interface IStreamHub
{
Task AddClient();
Task AddUser(string username);
Task UploadByteStream(IAsyncEnumerable<byte[]> stream);
Task Startstream(string userId);
Task StopStream(string userId);

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Squirrowse.Service.Hubs
{
public interface IStreamManager
{
Task AddUser(string connectionId, string agentName);
Task RemoveUserbyConnectionId(string connectionId);
Task RemoveUserByUserName(string agentName);
bool CheckUser(string agentName);
}
}

View File

@ -12,15 +12,16 @@ namespace Squirrowse.Service.Hubs
public class StreamHub : Hub, IStreamHub //fujka
{
private readonly ILogger<StreamHub> logger;
public StreamHub(ILogger<StreamHub> logger)
private readonly IStreamManager manager;
public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
{
this.logger = logger;
this.manager = manager;
}
public async Task AddClient()
public async Task AddUser(string UserName)
{
throw new NotImplementedException();
await manager.AddUser(Context.ConnectionId, UserName);
}
public async Task AddToGroup(Groups group, string user = "")

View File

@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Squirrowse.Core.Models;
namespace Squirrowse.Service.Hubs
{
public class StreamManager : IStreamManager
{
private readonly List<User> _users = new List<User>(); //temporary
public Task AddUser(string connectionId, string userName)
{
_users.Add(new User(connectionId, userName));
return Task.CompletedTask;
}
public Task RemoveUserbyConnectionId(string connectionId)
{
_users.Remove(_users.First(user => user.ConnectionId == connectionId));
return Task.CompletedTask;
}
public Task RemoveUserByUserName(string userName)
{
_users.RemoveAll(user => user.AgentName == userName);
return Task.CompletedTask;
}
public bool CheckUser(string userName)
{
return _users.Any(user => user.AgentName == userName);
}
}
}

View File

@ -1,7 +0,0 @@
namespace Squirrowse.Service
{
public interface IUserProvider
{
string GetUserId();
}
}

View File

@ -1,12 +0,0 @@
using System;
namespace Squirrowse.Service
{
public class UserProvider : IUserProvider
{
public string GetUserId()
{
throw new NotImplementedException();
}
}
}