Esempio n. 1
0
		public string BuildAction(ManagerAction action, string internalActionId)
		{
			MethodInfo getter;
			object value;
			StringBuilder sb = new StringBuilder();
			string valueAsString = string.Empty;

			if (typeof(Action.ProxyAction).IsAssignableFrom(action.GetType()))
				sb.Append(string.Concat("ProxyAction: ", action.Action, Common.LINE_SEPARATOR));
			else
				sb.Append(string.Concat("Action: ", action.Action, Common.LINE_SEPARATOR));

			if (string.IsNullOrEmpty(internalActionId))
				valueAsString = action.ActionId;
			else
				valueAsString = string.Concat(internalActionId, Common.INTERNAL_ACTION_ID_DELIMITER, action.ActionId);

			if (!string.IsNullOrEmpty(valueAsString))
				sb.Append(string.Concat("ActionID: ", valueAsString, Common.LINE_SEPARATOR));

			Dictionary<string, MethodInfo> getters = Helper.GetGetters(action.GetType());

			foreach (string name in getters.Keys)
			{
				string nameLower = name.ToLower(Helper.CultureInfo);
				if (nameLower == "class" || nameLower == "action" || nameLower == "actionid")
					continue;

				getter = getters[name];
				Type propType = getter.ReturnType;
				if (!(propType == typeof(string)
					|| propType == typeof(bool)
					|| propType == typeof(double)
					|| propType == typeof(DateTime)
					|| propType == typeof(int)
					|| propType == typeof(long)
					|| propType == typeof(Dictionary<string, string>)
					)
					)
					continue;

				try
				{
					value = getter.Invoke(action, new object[] { });
				}
				catch (UnauthorizedAccessException ex)
				{
#if LOGGER
					logger.Error("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
					continue;
#else
					throw new ManagerException("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
#endif
				}
				catch (TargetInvocationException ex)
				{
#if LOGGER
					logger.Error("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
					continue;
#else
					throw new ManagerException("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
#endif
				}

				if (value == null)
					continue;
				if (value is string)
				{
					valueAsString = (string)value;
					if (valueAsString.Length == 0)
						continue;
				}
				else if (value is bool)
					valueAsString = ((bool)value ? "true" : "false");
				else if (value is DateTime)
					valueAsString = value.ToString();
				else if (value is IDictionary)
				{
					valueAsString = Helper.JoinVariables((IDictionary)value, Common.LINE_SEPARATOR, ": ");
					if (valueAsString.Length == 0)
						continue;
					sb.Append(valueAsString);
					sb.Append(Common.LINE_SEPARATOR);
					continue;
				}
				else
					valueAsString = value.ToString();

				sb.Append(string.Concat(name, ": ", valueAsString, Common.LINE_SEPARATOR));
			}

            IActionVariable actionVar = action as IActionVariable;
            if(actionVar != null && actionVar.GetVariables().Count > 0)
            {
                sb.Append(string.Concat("Variable: ", Helper.JoinVariables(actionVar.GetVariables(), VAR_DELIMITER, "="), Common.LINE_SEPARATOR));
            }

			sb.Append(Common.LINE_SEPARATOR);
			return sb.ToString();
		}
Esempio n. 2
0
		internal void SendToAsterisk(ManagerAction action, string internalActionId)
		{
			if (mrSocket == null)
				throw new SystemException("Unable to send action: socket is null");

			string buffer = BuildAction(action, internalActionId);
#if LOGGER
			logger.Debug("Sent action : '{0}' : {1}", internalActionId, action);
#endif
			if (sa == null)
				sa = new SendToAsteriskDelegate(sendToAsterisk);
			sa.Invoke(buffer);
		}
Esempio n. 3
0
		public int SendAction(ManagerAction action, ResponseHandler responseHandler)
		{
			if (action == null)
				throw new ArgumentException("Unable to send action: action is null.");

			if (mrSocket == null)
				throw new SystemException("Unable to send " + action.Action + " action: not connected.");

			// if the responseHandler is null the user is obviously not interested in the response, thats fine.
			string internalActionId = string.Empty;
			if (responseHandler != null)
			{
				internalActionId = createInternalActionId();
				responseHandler.Hash = internalActionId.GetHashCode();
				AddResponseHandler(responseHandler);
			}

			SendToAsterisk(action, internalActionId);

			return responseHandler != null ? responseHandler.Hash : 0;
		}
Esempio n. 4
0
		/// <summary>
		/// Send action ans with timeout (milliseconds)
		/// </summary>
		/// <param name="action">action to send</param>
		/// <param name="timeout">timeout in milliseconds</param>
		/// <returns></returns>
		public Response.ManagerResponse SendAction(ManagerAction action, int timeOut)
		{
			AutoResetEvent autoEvent = new AutoResetEvent(false);
			ResponseHandler handler = new ResponseHandler(action, autoEvent);

			int hash = SendAction(action, handler);
			bool result = autoEvent.WaitOne(timeOut <= 0 ? -1 : timeOut, true);

			RemoveResponseHandler(handler);

			if (result)
				return handler.Response;
			throw new TimeoutException("Timeout waiting for response to " + action.Action);
		}
 public void Free()
 {
     autoEvent = null;
     action = null;
     response = null;
 }
 /// <summary>
 ///     Creates a new instance.
 /// </summary>
 /// <param name="result">the result to store the response in</param>
 /// <param name="thread">the thread to interrupt when the response has been received</param>
 public ResponseHandler(ManagerAction action, AutoResetEvent autoEvent)
 {
     response = null;
     this.action = action;
     this.autoEvent = autoEvent;
 }