Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions AustinHarris.JsonRpcTestN/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void TestCanCreateMultipleServicesOfSameTypeInTheirOwnSessions()

for (int i = 0; i < 100; i++)
{
ServiceBinder.bindService(i.ToString(), () => Poco.WithOffset(i));
ServiceBinder.BindService(i.ToString(), Poco.WithOffset(i));
}

for (int i = 0; i < 100; i++)
Expand All @@ -68,7 +68,7 @@ public void TestCanCreateAndRemoveSession()
Tuple.Create ("sooper", typeof(string)),
Tuple.Create ("returns", typeof(string))
}.ToDictionary(x => x.Item1, x => x.Item2);
h.MetaData.AddService("workie", metadata, new System.Collections.Generic.Dictionary<string, object>(),new Func<string, string>(x => "workie ... " + x));
h.RegisterFuction("workie", metadata, new System.Collections.Generic.Dictionary<string, object>(),new Func<string, string>(x => "workie ... " + x));

string request = @"{method:'workie',params:{'sooper':'good'},id:1}";
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"workie ... good\",\"id\":1}";
Expand Down Expand Up @@ -1583,7 +1583,7 @@ public void TestPreProcessOnSession()
Tuple.Create ("sooper", typeof(string)),
Tuple.Create ("returns", typeof(string))
}.ToDictionary(x => x.Item1, x => x.Item2);
h.MetaData.AddService("workie", metadata, new System.Collections.Generic.Dictionary<string, object>(),new Func<string, string>(x => "workie ... " + x));
h.RegisterFuction("workie", metadata, new System.Collections.Generic.Dictionary<string, object>(),new Func<string, string>(x => "workie ... " + x));

string request = @"{method:'workie',params:{'sooper':'good'},id:1}";
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"workie ... good\",\"id\":1}";
Expand Down Expand Up @@ -1823,7 +1823,7 @@ public void TestPostProcessOnSession()
Tuple.Create ("sooper", typeof(string)),
Tuple.Create ("returns", typeof(string))
}.ToDictionary(x => x.Item1, x => x.Item2);
h.MetaData.AddService("workie", metadata, new System.Collections.Generic.Dictionary<string, object>(), new Func<string, string>(x => "workie ... " + x));
h.RegisterFuction("workie", metadata, new System.Collections.Generic.Dictionary<string, object>(), new Func<string, string>(x => "workie ... " + x));

string request = @"{method:'workie',params:{'sooper':'good'},id:1}";
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"workie ... good\",\"id\":1}";
Expand Down
72 changes: 48 additions & 24 deletions Json-Rpc/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,6 @@ public static void RpcSetException(JsonRpcException exception)
throw new InvalidOperationException("This method is only valid when used within the context of a method marked as a JsonRpcMethod, and that method must of been invoked by the JsonRpc Handler.");
}

private void RemoveRpcException()
{
if (Task.CurrentId != null)
{
var id = Task.CurrentId.Value;
RpcExceptions[id] = null;
JsonRpcException va;
RpcExceptions.TryRemove(id, out va);
}
}

private AustinHarris.JsonRpc.PreProcessHandler externalPreProcessingHandler;
private AustinHarris.JsonRpc.PostProcessHandler externalPostProcessingHandler;
private Func<JsonRequest, JsonRpcException, JsonRpcException> externalErrorHandler;
Expand All @@ -151,9 +140,42 @@ private void RemoveRpcException()

#region Public Methods

public void UnRegister(string key)
/// <summary>
/// Allows you to register all the functions on a Pojo Type that have been attributed as [JsonRpcMethod] to the specified sessionId
/// </summary>
/// <param name="sessionID">The session to register against</param>
/// <param name="instance">The instance containing JsonRpcMethods to register</param>
public static void RegisterInstance(string sessionID, object instance)
{
ServiceBinder.BindService(sessionID, instance);
}

/// <summary>
/// Allows you to register any function, lambda, etc even when not attributed with JsonRpcMethod.
/// Requires you to specify all types and defaults
/// </summary>
/// <param name="methodName">The method name that will map to the registered function</param>
/// <param name="parameterNameTypeMapping">The parameter names and types that will be positionally bound to the function</param>
/// <param name="parameterNameDefaultValueMapping">Optional default values for parameters</param>
/// <param name="implementation">A reference to the Function</param>
public void RegisterFuction(string methodName, Dictionary<string, Type> parameterNameTypeMapping, Dictionary<string, object> parameterNameDefaultValueMapping, Delegate implementation)
{
MetaData.AddService(methodName, parameterNameTypeMapping, parameterNameDefaultValueMapping, implementation);
}

public void UnRegisterFunction(string methodName)
{
MetaData.Services.Remove(methodName);
}

public void SetPreProcessHandler(AustinHarris.JsonRpc.PreProcessHandler handler)
{
externalPreProcessingHandler = handler;
}

public void SetPostProcessHandler(AustinHarris.JsonRpc.PostProcessHandler handler)
{
MetaData.Services.Remove(key);
externalPostProcessingHandler = handler;
}

/// <summary>
Expand Down Expand Up @@ -370,7 +392,7 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
RemoveRpcContext();
}
}

#endregion
/// <summary>
/// Method returns the actual callback set to this thread in Handle() method.
/// If callback is not set, then empty callback is returned.
Expand Down Expand Up @@ -428,7 +450,18 @@ internal void SetParseErrorHandler(Func<string, JsonRpcException, JsonRpcExcepti
parseErrorHandler = handler;
}

#endregion
private void RemoveRpcException()
{
if (Task.CurrentId != null)
{
var id = Task.CurrentId.Value;
RpcExceptions[id] = null;
JsonRpcException va;
RpcExceptions.TryRemove(id, out va);
}
}


private object CleanUpParameter(object p, SMDAdditionalParameters metaData)
{
var bob = p as JValue;
Expand Down Expand Up @@ -522,15 +555,6 @@ private JsonResponse PostProcess(Action<JsonResponse> callback, JsonRequest requ
return response;
}

public void SetPreProcessHandler(AustinHarris.JsonRpc.PreProcessHandler handler)
{
externalPreProcessingHandler = handler;
}

public void SetPostProcessHandler(AustinHarris.JsonRpc.PostProcessHandler handler)
{
externalPostProcessingHandler = handler;
}
}

}
Expand Down
4 changes: 2 additions & 2 deletions Json-Rpc/JsonRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public abstract class JsonRpcService
{
protected JsonRpcService()
{
ServiceBinder.bindService(Handler.DefaultSessionId(), () => this);
ServiceBinder.BindService(Handler.DefaultSessionId(), this);
}

protected JsonRpcService(string sessionID)
{
ServiceBinder.bindService(sessionID, () => this);
ServiceBinder.BindService(sessionID, this);
}
}
}
2 changes: 1 addition & 1 deletion Json-Rpc/SMDService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SMD ()
TypeHashes = new List<string>();
}

public void AddService(string method, Dictionary<string,Type> parameters, Dictionary<string, object> defaultValues, Delegate dele)
internal void AddService(string method, Dictionary<string,Type> parameters, Dictionary<string, object> defaultValues, Delegate dele)
{
var newService = new SMDService(transport,"JSON-RPC-2.0",parameters, defaultValues, dele);
Services.Add(method,newService);
Expand Down
21 changes: 10 additions & 11 deletions Json-Rpc/ServiceBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@

public static class ServiceBinder
{
public static void bindService<T>(string sessionID, Func<T> serviceFactory)
public static void BindService<T>() where T : new()
{
BindService<T>(Handler.DefaultSessionId());
}
public static void BindService<T>(string sessionID) where T : new()
{
BindService(sessionID, new T());
}

public static void BindService(string sessionID, Object instance)
{
var instance = serviceFactory();
var item = instance.GetType(); // var item = typeof(T);

var methods = item.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(m => m.GetCustomAttributes(typeof(JsonRpcMethodAttribute), false).Length > 0);
Expand Down Expand Up @@ -58,15 +66,6 @@ public static void bindService<T>(string sessionID, Func<T> serviceFactory)
handlerSession.MetaData.AddService(methodName, paras, defaultValues, newDel);
}
}

}
public static void bindService<T>(string sessionID) where T : new()
{
bindService(sessionID, () => new T());
}
public static void bindService<T>() where T : new()
{
bindService<T>(Handler.DefaultSessionId());
}
}
}