protected virtual AbstractListener LoadListener(AbstractListener listenerWorker, string applicationType, string listenerName) { try { if (typeApplicationHasListener(listenerName)) { getLogger().Error("Listener " + listenerName + " repeated. Request discarded."); throw new RepeatedListenerNameException("Listener " + listenerName + " repeated. Request discarded."); } listenerWorker.init(main); listenerDefaultWorkerMap[listenerName] = listenerWorker; string[] datatype = listenerWorker.getListenerTypes(); for (int count = 0; count < datatype.Length; count++) { List <AbstractListener> existing; if (!listenerMapDataType.TryGetValue(datatype[count], out existing)) { existing = new List <AbstractListener>(); listenerMapDataType[datatype[count]] = existing; } // At this point we know that "existing" refers to the relevant list in the // dictionary, one way or another. existing.Add(listenerWorker); } } catch (Exception e) { getLogger().Error(e); } return(listenerWorker); }
public virtual object dispatchMessageToSingleListener(FunctionContext context, string functionName) { if (listenerMapDataType.ContainsKey(context.type)) { AbstractListener ablistworker = getListener(functionName); if (ablistworker != null) { #if DEBUG Stopwatch timeOperations = Stopwatch.StartNew(); #endif try { return(ablistworker.addContextToQueue(context)); } finally { #if DEBUG timeOperations.Stop(); getLogger().Debug("Dispatched message to " + ablistworker.getName() + " in " + timeOperations.ElapsedMilliseconds + "ms"); #endif } } else { getLogger().Warning("Not Found listener for function " + functionName); } } else { getLogger().Warning("Not Found listener for " + context.type); } return(null); }
public virtual Task <object> dispatchMessageToAsyncSingleListener(FunctionContext context, string functionName) { /*return Task.Run<T>(() => { * var result=dispatchMessageToSingleListener(context,functionName); * if (result!=null) return (T)result; * else return default(T); * }); */ if (listenerMapDataType.ContainsKey(context.type)) { AbstractListener ablistworker = getListener(functionName); if (ablistworker != null) { #if DEBUG Stopwatch timeOperations = Stopwatch.StartNew(); #endif try { Task <object> task; if (ablistworker.isAsyncMethod() || ablistworker.isYieldMethod()) { if (main.infoctx.DebugMode) { getLogger().Debug("Calling Async dispatch " + ablistworker.getName()); } task = ablistworker.addContextToGenericAsyncQueue(context); } else { if (main.infoctx.DebugMode) { getLogger().Debug("Calling Awaitable dispatch " + ablistworker.getName()); } task = Task.Run <object>(async() => { return(await ablistworker.addContextToGenericAsyncQueue(context)); }); } return(task); } finally { #if DEBUG timeOperations.Stop(); getLogger().Debug("Dispatched messageAsyc to " + ablistworker.getName() + " in " + timeOperations.ElapsedMilliseconds + "ms"); #endif } } else { getLogger().Warning("Not Found listener for function " + functionName); } } else { getLogger().Warning("Not Found listener for " + context.type); } return(null); }
public virtual Dictionary <string, object> dispatchMessageToListeners(FunctionContext context) { Dictionary <string, object> result = new Dictionary <string, object>(); if (listenerMapDataType.ContainsKey(context.type)) { for (int i = 0; i < listenerMapDataType[context.type].Count; i++) { #if DEBUG Stopwatch timeOperations = Stopwatch.StartNew(); #endif AbstractListener ablistworker = (AbstractListener)listenerMapDataType[context.type][i]; try { object ret = ablistworker.addContextToQueue(context); result.Add(ablistworker.getName(), ret); } finally { #if DEBUG timeOperations.Stop(); getLogger().Debug("Dispatched message to " + ablistworker.getName() + " in " + timeOperations.ElapsedMilliseconds + "ms"); #endif } } } else { getLogger().Warning("Not Found listener for " + context.type); } return(result); }
public virtual Dictionary <string, Task <object> > dispatchMessageToAsyncListenersGetTasks(FunctionContext context) { Dictionary <string, Task <object> > tasksDict = new Dictionary <string, Task <object> >(); if (listenerMapDataType.ContainsKey(context.type)) { for (int i = 0; i < listenerMapDataType[context.type].Count; i++) { #if DEBUG Stopwatch timeOperations = Stopwatch.StartNew(); #endif AbstractListener ablistworker = (AbstractListener)listenerMapDataType[context.type][i]; try { Task <object> task; if (ablistworker.isAsyncMethod() || ablistworker.isYieldMethod()) { if (main.infoctx.DebugMode) { getLogger().Debug("Calling Async dispatch " + ablistworker.getName()); } task = ablistworker.addContextToGenericAsyncQueue(context); } else { if (main.infoctx.DebugMode) { getLogger().Debug("Calling Awaitable dispatch " + ablistworker.getName()); } task = Task.Run <object>(async() => { return(await ablistworker.addContextToGenericAsyncQueue(context)); }); } tasksDict.Add(ablistworker.getName(), task); } finally { #if DEBUG timeOperations.Stop(); getLogger().Debug("Dispatched MessageAsync to " + ablistworker.getName() + " in " + timeOperations.ElapsedMilliseconds + "ms"); #endif } } } else { getLogger().Warning("Not Found listener for " + context.type); } return(tasksDict); }
protected virtual AbstractListener addListener(Type clazz, string applicationType, string listenerName) { AbstractListener _listenerWorker = Activator.CreateInstance(clazz) as AbstractListener; if (listenerName == null) { listenerName = _listenerWorker.getName(); } if (IsSubclassOfRawGeneric(clazz, "AbstractListenerAsync")) { _listenerWorker.setHasAsynMethod(IsAsyncMethod(clazz, "addContextToAsyncQueue")); } else { _listenerWorker.setHasAsynMethod(IsAsyncMethod(clazz, "addContextToGenericAsyncQueue")); } AbstractListener listenerloaded = LoadListener(_listenerWorker, applicationType, listenerName); return(listenerloaded); }
public virtual AbstractListener getListener(string FunctionName) { AbstractListener ablistworker = (AbstractListener)listenerDefaultWorkerMap[FunctionName]; return(ablistworker); }