Skip to content

Commit f07fbeb

Browse files
authored
Merge pull request Astn#75 from Astn/NamingAndUsability
Moved a few functions, and fixed some names for better clarity. Astn#50
2 parents ee298a8 + 7e942d9 commit f07fbeb

5 files changed

Lines changed: 65 additions & 42 deletions

File tree

AustinHarris.JsonRpcTestN/Test.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void TestCanCreateMultipleServicesOfSameTypeInTheirOwnSessions()
4747

4848
for (int i = 0; i < 100; i++)
4949
{
50-
ServiceBinder.bindService(i.ToString(), () => Poco.WithOffset(i));
50+
ServiceBinder.BindService(i.ToString(), Poco.WithOffset(i));
5151
}
5252

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

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

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

18281828
string request = @"{method:'workie',params:{'sooper':'good'},id:1}";
18291829
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"workie ... good\",\"id\":1}";

Json-Rpc/Handler.cs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,6 @@ public static void RpcSetException(JsonRpcException exception)
125125
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.");
126126
}
127127

128-
private void RemoveRpcException()
129-
{
130-
if (Task.CurrentId != null)
131-
{
132-
var id = Task.CurrentId.Value;
133-
RpcExceptions[id] = null;
134-
JsonRpcException va;
135-
RpcExceptions.TryRemove(id, out va);
136-
}
137-
}
138-
139128
private AustinHarris.JsonRpc.PreProcessHandler externalPreProcessingHandler;
140129
private AustinHarris.JsonRpc.PostProcessHandler externalPostProcessingHandler;
141130
private Func<JsonRequest, JsonRpcException, JsonRpcException> externalErrorHandler;
@@ -151,9 +140,42 @@ private void RemoveRpcException()
151140

152141
#region Public Methods
153142

154-
public void UnRegister(string key)
143+
/// <summary>
144+
/// Allows you to register all the functions on a Pojo Type that have been attributed as [JsonRpcMethod] to the specified sessionId
145+
/// </summary>
146+
/// <param name="sessionID">The session to register against</param>
147+
/// <param name="instance">The instance containing JsonRpcMethods to register</param>
148+
public static void RegisterInstance(string sessionID, object instance)
149+
{
150+
ServiceBinder.BindService(sessionID, instance);
151+
}
152+
153+
/// <summary>
154+
/// Allows you to register any function, lambda, etc even when not attributed with JsonRpcMethod.
155+
/// Requires you to specify all types and defaults
156+
/// </summary>
157+
/// <param name="methodName">The method name that will map to the registered function</param>
158+
/// <param name="parameterNameTypeMapping">The parameter names and types that will be positionally bound to the function</param>
159+
/// <param name="parameterNameDefaultValueMapping">Optional default values for parameters</param>
160+
/// <param name="implementation">A reference to the Function</param>
161+
public void RegisterFuction(string methodName, Dictionary<string, Type> parameterNameTypeMapping, Dictionary<string, object> parameterNameDefaultValueMapping, Delegate implementation)
162+
{
163+
MetaData.AddService(methodName, parameterNameTypeMapping, parameterNameDefaultValueMapping, implementation);
164+
}
165+
166+
public void UnRegisterFunction(string methodName)
167+
{
168+
MetaData.Services.Remove(methodName);
169+
}
170+
171+
public void SetPreProcessHandler(AustinHarris.JsonRpc.PreProcessHandler handler)
172+
{
173+
externalPreProcessingHandler = handler;
174+
}
175+
176+
public void SetPostProcessHandler(AustinHarris.JsonRpc.PostProcessHandler handler)
155177
{
156-
MetaData.Services.Remove(key);
178+
externalPostProcessingHandler = handler;
157179
}
158180

159181
/// <summary>
@@ -370,7 +392,7 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
370392
RemoveRpcContext();
371393
}
372394
}
373-
395+
#endregion
374396
/// <summary>
375397
/// Method returns the actual callback set to this thread in Handle() method.
376398
/// If callback is not set, then empty callback is returned.
@@ -428,7 +450,18 @@ internal void SetParseErrorHandler(Func<string, JsonRpcException, JsonRpcExcepti
428450
parseErrorHandler = handler;
429451
}
430452

431-
#endregion
453+
private void RemoveRpcException()
454+
{
455+
if (Task.CurrentId != null)
456+
{
457+
var id = Task.CurrentId.Value;
458+
RpcExceptions[id] = null;
459+
JsonRpcException va;
460+
RpcExceptions.TryRemove(id, out va);
461+
}
462+
}
463+
464+
432465
private object CleanUpParameter(object p, SMDAdditionalParameters metaData)
433466
{
434467
var bob = p as JValue;
@@ -522,15 +555,6 @@ private JsonResponse PostProcess(Action<JsonResponse> callback, JsonRequest requ
522555
return response;
523556
}
524557

525-
public void SetPreProcessHandler(AustinHarris.JsonRpc.PreProcessHandler handler)
526-
{
527-
externalPreProcessingHandler = handler;
528-
}
529-
530-
public void SetPostProcessHandler(AustinHarris.JsonRpc.PostProcessHandler handler)
531-
{
532-
externalPostProcessingHandler = handler;
533-
}
534558
}
535559

536560
}

Json-Rpc/JsonRpcService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public abstract class JsonRpcService
1010
{
1111
protected JsonRpcService()
1212
{
13-
ServiceBinder.bindService(Handler.DefaultSessionId(), () => this);
13+
ServiceBinder.BindService(Handler.DefaultSessionId(), this);
1414
}
1515

1616
protected JsonRpcService(string sessionID)
1717
{
18-
ServiceBinder.bindService(sessionID, () => this);
18+
ServiceBinder.BindService(sessionID, this);
1919
}
2020
}
2121
}

Json-Rpc/SMDService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public SMD ()
3434
TypeHashes = new List<string>();
3535
}
3636

37-
public void AddService(string method, Dictionary<string,Type> parameters, Dictionary<string, object> defaultValues, Delegate dele)
37+
internal void AddService(string method, Dictionary<string,Type> parameters, Dictionary<string, object> defaultValues, Delegate dele)
3838
{
3939
var newService = new SMDService(transport,"JSON-RPC-2.0",parameters, defaultValues, dele);
4040
Services.Add(method,newService);

Json-Rpc/ServiceBinder.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88

99
public static class ServiceBinder
1010
{
11-
public static void bindService<T>(string sessionID, Func<T> serviceFactory)
11+
public static void BindService<T>() where T : new()
12+
{
13+
BindService<T>(Handler.DefaultSessionId());
14+
}
15+
public static void BindService<T>(string sessionID) where T : new()
16+
{
17+
BindService(sessionID, new T());
18+
}
19+
20+
public static void BindService(string sessionID, Object instance)
1221
{
13-
var instance = serviceFactory();
1422
var item = instance.GetType(); // var item = typeof(T);
1523

1624
var methods = item.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(m => m.GetCustomAttributes(typeof(JsonRpcMethodAttribute), false).Length > 0);
@@ -58,15 +66,6 @@ public static void bindService<T>(string sessionID, Func<T> serviceFactory)
5866
handlerSession.MetaData.AddService(methodName, paras, defaultValues, newDel);
5967
}
6068
}
61-
62-
}
63-
public static void bindService<T>(string sessionID) where T : new()
64-
{
65-
bindService(sessionID, () => new T());
66-
}
67-
public static void bindService<T>() where T : new()
68-
{
69-
bindService<T>(Handler.DefaultSessionId());
7069
}
7170
}
7271
}

0 commit comments

Comments
 (0)