Skip to content

Commit

Permalink
Rename OicClient to OicDiscoveryService
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Aug 6, 2017
1 parent bfb1053 commit bf193db
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
4 changes: 2 additions & 2 deletions OICNet.Tests/OicTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void Setup()
[Test]
public void BroadcastAllInterfaces()
{
var service = new OicClient();
var service = new OicDiscoverService();

service.AddBroadcastInterface(_broadcaster.Object);
service.AddInterface(_broadcaster.Object);
service.Discover();

Mock.VerifyAll(_broadcaster);
Expand Down
14 changes: 14 additions & 0 deletions OICNet/OicConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace OICNet
{
public class OicConfiguration
{
public OicMessageSerialiser Serialiser { get; set; }

private static OicConfiguration _default;

public static OicConfiguration Default => _default ?? (_default = new OicConfiguration
{
Serialiser = new OicMessageSerialiser(new OicResolver())
});
}
}
17 changes: 16 additions & 1 deletion OICNet/OicDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ public class OicDevice

public IOicEndpoint RemoteEndpoint { get; }

private readonly OicConfiguration _configuration;

public OicDevice(IOicInterface localInterface, IOicEndpoint remoteEndpoint)
:this(OicConfiguration.Default, localInterface, remoteEndpoint)
{

}

public OicDevice(OicConfiguration configuration, IOicInterface localInterface, IOicEndpoint remoteEndpoint)
{
_configuration = configuration;
LocalInterface = localInterface;
RemoteEndpoint = remoteEndpoint;
}


internal void PassMessage(OicResponse response)
{
//var resource = _configuration.Serialiser.Deserialise(response.Payload, response.ContentType);
//if(response.Uri.Equals("/oic/res",StringComparison.OrdinalIgnoreCase))
// resource.
}
}
}
49 changes: 20 additions & 29 deletions OICNet/OicClient.cs → OICNet/OicDiscoverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,53 @@ public class OicNewDeviceEventArgs : EventArgs
public OicDevice Device { get; set; }
}

public class OicClient : IDisposable
public class OicDiscoverService : IDisposable
{
private readonly List<OicDevice> _devices = new List<OicDevice>();

private readonly List<IOicInterface> _broadcastInterfaces;
private readonly List<IOicInterface> _broadcastInterfaces = new List<IOicInterface>();

private readonly OicMessageSerialiser _serialiser;
private readonly OicConfiguration _configuration;

public event EventHandler<OicNewDeviceEventArgs> NewDevice;

public event EventHandler<OicDeviceReceivedMessageEventArgs> ReceivedMessage;
public OicDiscoverService()
: this(OicConfiguration.Default)
{

}

public OicClient()
public OicDiscoverService(OicConfiguration configuration)
{
_broadcastInterfaces = new List<IOicInterface>();

_serialiser = new OicMessageSerialiser(new OicResolver());
_configuration = configuration;
}

public void AddBroadcastInterface(IOicInterface provider)
public void AddInterface(IOicInterface provider)
{
if (provider is null)
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(provider));
provider.ReceivedMessage += OnReceivedMessage;
_broadcastInterfaces.Add(provider);
}

public async Task SendAsync(OicDevice device, OicRequest message)
{
await device.LocalInterface.SendMessageAsync(device.RemoteEndpoint, message);
}

private void OnReceivedMessage(object sender, OicReceivedMessageEventArgs e)
{
//TODO: match up sender with an interface
System.Diagnostics.Debug.WriteLine($"Got a message from {e.Endpoint.Authority}");

var device = _devices.FirstOrDefault(d => d.RemoteEndpoint.Authority == e.Endpoint.Authority);
if (device == null)
{
device = new OicDevice((sender as IOicInterface), e.Endpoint);
device = new OicDevice(_configuration, (sender as IOicInterface), e.Endpoint);
NewDevice?.Invoke(this, new OicNewDeviceEventArgs { Device = device });
}

System.Diagnostics.Debugger.Break();
if(e.Message.ContentType == OicMessageContentType.ApplicationJson)
{
var result = _serialiser.Deserialise(e.Message.Payload, OicMessageContentType.ApplicationJson);
}
device.PassMessage(e.Message);
//_serialiser.Deserialise(e.Message.Payload, e.Message.ContentType);

ReceivedMessage?.Invoke(this, new OicDeviceReceivedMessageEventArgs
{
Device = device,
Message = e.Message
});
//ReceivedMessage?.Invoke(this, new OicDeviceReceivedMessageEventArgs
//{
// Device = device,
// Message = e.Message
//});
}

public void Discover()
Expand All @@ -88,7 +79,7 @@ public void Discover()

public void Dispose()
{
//TODO: Dispose OICNet.OicClient properly
//TODO: Dispose OICNet.OicDiscoverService properly
}
}
}

0 comments on commit bf193db

Please sign in to comment.