Exemplo n.º 1
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);
		}
Exemplo n.º 2
0
		public string BuildAction(ManagerAction action, string internalActionId)
		{
			PropertyInfo getter;
			object[] attrs;
			object value;
			StringBuilder sb = new StringBuilder();
			string valueAsString = string.Empty;
			bool ignoreValue = false;

			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, PropertyInfo> getters = Helper.GetProperties(action.GetType(), Helper.PropertyMask.WithGet);

			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.PropertyType;
				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.GetValue(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;

				attrs = getter.GetCustomAttributes(typeof(ConditionIgnoreAttribute), true);

				foreach (var item in attrs)
				{
					var attr = item as ConditionIgnoreAttribute;

					if (attr.Value.Equals(value))
					{
						ignoreValue = true;
						break;
					}
				}

				if (ignoreValue)
				{
					ignoreValue = false;
					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));
			}

			sb.Append(Common.LINE_SEPARATOR);
			return sb.ToString();
		}
Exemplo 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;
		}
Exemplo 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);
		}
Exemplo n.º 5
0
 public ManagerResponse sendaction(ManagerAction action)
 {
     ManagerResponse response = null;
     try
     {
         if (action != null)
         {
             // sending action to manager
             manager.SendAction(action);
         }
         else
             throw new Exception("action is null");
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return response;
 }
Exemplo n.º 6
0
		public void Free()
		{
			autoEvent = null;
			action = null;
			response = null;
		}
Exemplo n.º 7
0
		/// <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)
		{
			this.response = null;
			this.action = action;
			this.autoEvent = autoEvent;
		}