Skip to content

Commit

Permalink
Separated topic and service into separate managers, implemented IService
Browse files Browse the repository at this point in the history
  • Loading branch information
mtbrobotanist committed Feb 14, 2018
1 parent ac2d60b commit a26eab8
Show file tree
Hide file tree
Showing 9 changed files with 657 additions and 290 deletions.
25 changes: 25 additions & 0 deletions Core/IService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SimpleJSON;

namespace ROSBridgeLib.Core
{
public delegate void ServiceResponseCallback<in T>(T response) where T : IServiceResponse;

public interface IServiceRequest
{
/// <summary>
/// Must serialize as a list of variables.
/// <para/> https://github.com/RobotWebTools/rosbridge_suite/blob/master/ROSBRIDGE_PROTOCOL.md
/// </summary>
/// <returns>a json formatted list of this request's variables</returns>
string ToJSONList();
}

public interface IServiceResponse
{
/// <summary>
/// Deserialize the message to the values implemented by this response.
/// </summary>
/// <param name="msg"></param>
void Deserialize(JSONNode msg);
}
}
87 changes: 87 additions & 0 deletions Core/ROSBridgeServiceClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using SimpleJSON;
using UnityEngine;
using WebSocketSharp;

namespace ROSBridgeLib.Core
{
public class ROSBridgeServiceClient
{
protected WebSocket socket;
protected string service;
protected Type serviceResponseType;

public event ServiceResponseCallback<IServiceResponse> ResponseReceived;

public string Service
{
get { return service; }
}

public Type ServiceResponseType
{
get { return serviceResponseType; }
}

public ROSBridgeServiceClient(WebSocket socket, string service, Type serviceResponseType)
{
this.socket = socket;
this.service = service;
this.serviceResponseType = serviceResponseType;
}

public void CallService(IServiceRequest request)
{
string req = ROSBridgeMsg.CallService(service, request.ToJSONList());
System.Diagnostics.Debug.Print($"Sending: {req}");
socket.Send(req);
}

public void ProcessRawResponse(JSONNode rawResponse)
{
IServiceResponse response = (IServiceResponse)Activator.CreateInstance(serviceResponseType);
response.Deserialize(rawResponse);
ProcessResponse(response);
}

protected virtual void ProcessResponse(IServiceResponse response)
{
ResponseReceived?.Invoke(response);
}
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public class ROSBridgeServiceClient<T_REQ, U_RES> : ROSBridgeServiceClient
where T_REQ : IServiceRequest where U_RES : IServiceResponse
{
public new event ServiceResponseCallback<U_RES> ResponseReceived;

public ROSBridgeServiceClient(WebSocket socket, string service)
: base(socket, service, typeof(U_RES))
{ }

protected override void ProcessResponse(IServiceResponse response)
{
base.ProcessResponse(response);
ResponseReceived?.Invoke((U_RES)response);
}

public void CallService(T_REQ request)
{
base.CallService(request);
}

public void AddServiceResponse(ServiceResponseCallback<U_RES> callback)
{
ResponseReceived += callback;
}

public void RemoveServiceResponse(ServiceResponseCallback<U_RES> callback)
{
ResponseReceived -= callback;
}
}
}
Loading

0 comments on commit a26eab8

Please sign in to comment.