forked from CSUN-SERL/ROSBridgeLib
-
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.
Separated topic and service into separate managers, implemented IService
- Loading branch information
1 parent
ac2d60b
commit a26eab8
Showing
9 changed files
with
657 additions
and
290 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
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); | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
Oops, something went wrong.