Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting part of commit 08753ef6 that was major change #33

Merged
merged 1 commit into from
Aug 30, 2021
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
1 change: 1 addition & 0 deletions pitaya-sharp/ExampleEFCore/src/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static void Main(string[] args)
Environment.Exit(1);
});

PitayaCluster.RegisterHandler(new UserHandler());
PitayaCluster.SetSerializer(new JSONSerializer()); // Using json serializer for easier interop with pitaya-cli

try
Expand Down
2 changes: 1 addition & 1 deletion pitaya-sharp/NPitaya/src/Models/BaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NPitaya.Models
{
public class BaseHandler : IRemote
public class BaseHandler : IBaseRemote
{
public string GetName()
{
Expand Down
2 changes: 1 addition & 1 deletion pitaya-sharp/NPitaya/src/Models/BaseRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace NPitaya.Models
{
public class BaseRemote : IRemote
public class BaseRemote : IBaseRemote
{
public string GetName()
{
Expand Down
2 changes: 1 addition & 1 deletion pitaya-sharp/NPitaya/src/Models/IRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NPitaya.Models
{
public interface IRemote
public interface IBaseRemote
{
string GetName();
Dictionary<string, RemoteMethod> GetRemotesMap();
Expand Down
4 changes: 2 additions & 2 deletions pitaya-sharp/NPitaya/src/Models/RemoteMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace NPitaya.Models
{
public class RemoteMethod
{
public readonly IRemote Obj;
public readonly IBaseRemote Obj;
internal MethodBase Method { get; }
internal Type ReturnType { get; }
internal Type ArgType { get; }
public RemoteMethod(IRemote obj, MethodBase method, Type returnType, Type argType){
public RemoteMethod(IBaseRemote obj, MethodBase method, Type returnType, Type argType){
Obj = obj;
Method = method;
ReturnType = returnType;
Expand Down
38 changes: 15 additions & 23 deletions pitaya-sharp/NPitaya/src/PitayaCluster.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,8 @@ public static void Initialize(GrpcConfig grpcCfg,
ListenToIncomingRPCs();
}

private static List<Type> GetAllInheriting(Type type)
{
return AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
.Where(x => type.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract && x.FullName != type.FullName)
.Select(x => x).ToList();
}

private static void ListenToIncomingRPCs()
{
var handlers = GetAllInheriting(typeof(BaseHandler));
foreach (var handler in handlers){
RegisterHandler((BaseHandler)Activator.CreateInstance(handler));
}

var remotes = GetAllInheriting(typeof(BaseRemote));
foreach (var remote in remotes){
RegisterRemote((BaseRemote)Activator.CreateInstance(remote));
}

for (int i = 0; i < ProcessorsCount; i++)
{
var threadId = i + 1;
Expand Down Expand Up @@ -152,13 +135,18 @@ public static void Initialize(NatsConfig natsCfg,
ListenToIncomingRPCs();
}

private static void RegisterRemote(BaseRemote remote)
public static void RegisterRemote(BaseRemote remote)
{
string className = DefaultRemoteNameFunc(remote.GetName());
RegisterRemote(remote, className, DefaultRemoteNameFunc);
}

private static void RegisterRemote(BaseRemote remote, string name, RemoteNameFunc remoteNameFunc) // TODO remote function name func
public static void RegisterRemote(BaseRemote remote, string name)
{
RegisterRemote(remote, name, DefaultRemoteNameFunc);
}

public static void RegisterRemote(BaseRemote remote, string name, RemoteNameFunc remoteNameFunc) // TODO remote function name func
{
Dictionary<string, RemoteMethod> m = remote.GetRemotesMap();
foreach (KeyValuePair<string, RemoteMethod> kvp in m)
Expand All @@ -175,13 +163,17 @@ private static void RegisterRemote(BaseRemote remote, string name, RemoteNameFun
}
}

private static void RegisterHandler(BaseHandler handler)
public static void RegisterHandler(BaseHandler handler)
{
string className = DefaultRemoteNameFunc(handler.GetName());
RegisterHandler(handler, className, DefaultRemoteNameFunc);
}

// TODO create method to override defaultRemoteNameFunc
public static void RegisterHandler(BaseHandler handler, string name)
{
RegisterHandler(handler, name, DefaultRemoteNameFunc);
}

public static void RegisterHandler(BaseHandler handler, string name, RemoteNameFunc remoteNameFunc)
{
Dictionary<string, RemoteMethod> m = handler.GetRemotesMap();
Expand Down Expand Up @@ -329,11 +321,11 @@ public static unsafe Task<T> Rpc<T>(string serverId, Route route, object msg)

if (!ok) // error
{
if (retError.code == "PIT-504")
if (retError.code == "PIT-504")
{
throw new PitayaTimeoutException($"Timeout on RPC call: ({retError.code}: {retError.msg})");
}
if (retError.code == "PIT-404")
if (retError.code == "PIT-404")
{
throw new PitayaRouteNotFoundException($"Route not found on RPC call: ({retError.code}: {retError.msg})");
}
Expand Down
5 changes: 5 additions & 0 deletions pitaya-sharp/exampleapp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ static void Main(string[] args)

Logger.Info("pitaya lib initialized successfully :)");

var tr = new TestRemote();
PitayaCluster.RegisterRemote(tr);
var th = new TestHandler();
PitayaCluster.RegisterHandler(th);

Thread.Sleep(1000);

TrySendRpc();
Expand Down