提交 6fa0202e authored 作者: Jonas Gauffin's avatar Jonas Gauffin

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/scripts/contrib@12881 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 29916c4d
...@@ -13,7 +13,7 @@ namespace FreeSwitch.EventSocket.Commands ...@@ -13,7 +13,7 @@ namespace FreeSwitch.EventSocket.Commands
public override string Command public override string Command
{ {
get { return "hold"; } get { return "uuid_hold"; }
} }
public override string Arguments public override string Arguments
......
...@@ -22,7 +22,7 @@ namespace FreeSwitch.EventSocket.Commands ...@@ -22,7 +22,7 @@ namespace FreeSwitch.EventSocket.Commands
{ {
private SofiaSipAddress _caller; private SofiaSipAddress _caller;
private Address _destination; private Address _destination;
private readonly IList<ChannelVariable> _variables = new List<ChannelVariable>(); private readonly IList<CallVariable> _variables = new List<CallVariable>();
private string _callerIdName; private string _callerIdName;
private string _callerIdNumber; private string _callerIdNumber;
private bool _varsAdded; private bool _varsAdded;
...@@ -49,7 +49,7 @@ namespace FreeSwitch.EventSocket.Commands ...@@ -49,7 +49,7 @@ namespace FreeSwitch.EventSocket.Commands
set { _destination = value; } set { _destination = value; }
} }
public IList<ChannelVariable> Variables public IList<CallVariable> Variables
{ {
get { return _variables; } get { return _variables; }
} }
...@@ -77,22 +77,35 @@ namespace FreeSwitch.EventSocket.Commands ...@@ -77,22 +77,35 @@ namespace FreeSwitch.EventSocket.Commands
{ {
if (!_varsAdded) if (!_varsAdded)
{ {
_variables.Add(new ChannelVariable("origination_caller_id_name", CallerIdName ?? _caller.Extension)); string name = CallerIdName ?? _caller.Extension;
_variables.Add(new CallVariable("origination_caller_id_name", "'" + name + "'"));
if (!string.IsNullOrEmpty(_callerIdNumber)) if (!string.IsNullOrEmpty(_callerIdNumber))
_variables.Add(new ChannelVariable("origination_caller_id_number", _callerIdNumber)); _variables.Add(new CallVariable("origination_caller_id_number", _callerIdNumber));
_varsAdded = true; _varsAdded = true;
if (_autoAnswer) if (_autoAnswer)
_variables.Add(new ChannelVariable("sip_auto_answer", "true")); {
_variables.Add(new CallLegVariable("sip_invite_params", "intercom=true"));
_variables.Add(new CallLegVariable("sip_h_Call-Info", "<sip:$${domain}>;answer-after=0"));
_variables.Add(new CallLegVariable("sip_auto_answer", "true"));
}
} }
string variables = string.Empty; string variables = string.Empty;
foreach (ChannelVariable var in _variables) string legVariables = string.Empty;
foreach (CallVariable var in _variables)
{
if (var is CallLegVariable)
legVariables += var + ",";
else
variables += var + ","; variables += var + ",";
}
if (variables.Length > 0) if (variables.Length > 0)
variables = "{" + variables.Remove(variables.Length - 1, 1) + "}"; variables = "{" + variables.Remove(variables.Length - 1, 1) + "}";
if (legVariables.Length > 0)
legVariables = "[" + legVariables.Remove(legVariables.Length - 1, 1) + "]";
return variables + Caller + " " + Destination; return variables + legVariables + Caller + " " + Destination;
} }
} }
......
...@@ -73,8 +73,8 @@ namespace FreeSwitch.EventSocket ...@@ -73,8 +73,8 @@ namespace FreeSwitch.EventSocket
public void Start(string hostname) public void Start(string hostname)
{ {
_socket.MessageReceived += OnMessage; _socket.MessageReceived += OnMessage;
_socket.Connect(hostname);
_socket.DataReceived += OnData; _socket.DataReceived += OnData;
_socket.Connect(hostname);
} }
private void OnData(string data) private void OnData(string data)
......
...@@ -141,6 +141,9 @@ namespace FreeSwitch.EventSocket ...@@ -141,6 +141,9 @@ namespace FreeSwitch.EventSocket
_text.CopyTo(i, chars, 0, plainEvent.ContentLength); _text.CopyTo(i, chars, 0, plainEvent.ContentLength);
plainEvent.Body = new string(chars); plainEvent.Body = new string(chars);
// api/response is buggy for originate, no \n\n are appended at the end.
if (plainEvent.ContentType != "api/response")
{
// check for errors. // check for errors.
int pos = plainEvent.Body.IndexOf("\n\n"); int pos = plainEvent.Body.IndexOf("\n\n");
if (pos < plainEvent.Body.Length - 2) if (pos < plainEvent.Body.Length - 2)
...@@ -150,10 +153,11 @@ namespace FreeSwitch.EventSocket ...@@ -150,10 +153,11 @@ namespace FreeSwitch.EventSocket
Console.WriteLine(headers); Console.WriteLine(headers);
Console.WriteLine("Body"); Console.WriteLine("Body");
Console.WriteLine(plainEvent.Body); Console.WriteLine(plainEvent.Body);
Console.WriteLine("=========================== EVERYTHING in _text =============================="); Console.WriteLine("=========================== EVERYTHING unparsed ==============================");
Console.WriteLine(_text); Console.WriteLine(_text);
throw new InvalidDataException("Invalid event: " + _text); throw new InvalidDataException("Invalid event: " + _text);
} }
}
if (plainEvent.Body.Length < plainEvent.ContentLength) if (plainEvent.Body.Length < plainEvent.ContentLength)
throw new InvalidDataException("Body contents are too small!"); throw new InvalidDataException("Body contents are too small!");
......
...@@ -198,12 +198,16 @@ namespace FreeSwitch.EventSocket ...@@ -198,12 +198,16 @@ namespace FreeSwitch.EventSocket
} }
catch (InvalidDataException err) catch (InvalidDataException err)
{ {
LogWriter(LogPrio.Warning, "Failed to parse event message (" + err.Message + "): " + Environment.NewLine + _parser.Text); LogWriter(LogPrio.Warning,
"Failed to parse event message." + Environment.NewLine + "Exception: " + err +
Environment.NewLine + _parser.Text);
HandleDisconnect(); HandleDisconnect();
} }
catch (ArgumentException err) catch (ArgumentException err)
{ {
LogWriter(LogPrio.Warning, "Failed to parse event message ("+err.Message+"): " + Environment.NewLine + _parser.Text); LogWriter(LogPrio.Warning,
"Failed to parse event message." + Environment.NewLine + "Exception: " + err +
Environment.NewLine + _parser.Text);
HandleDisconnect(); HandleDisconnect();
} }
} }
...@@ -260,9 +264,9 @@ namespace FreeSwitch.EventSocket ...@@ -260,9 +264,9 @@ namespace FreeSwitch.EventSocket
try try
{ {
PlainEventMsg msg = _parser.ParseOne(); PlainEventMsg msg = _parser.ParseOne();
LogWriter(LogPrio.Trace, "MessageType: " + msg.ContentType);
while (msg != null) while (msg != null)
{ {
LogWriter(LogPrio.Trace, "MessageType: " + msg.ContentType);
switch (msg.ContentType) switch (msg.ContentType)
{ {
case "auth/request": case "auth/request":
......
...@@ -70,6 +70,7 @@ ...@@ -70,6 +70,7 @@
<Compile Include="Commands\SleepCmd.cs" /> <Compile Include="Commands\SleepCmd.cs" />
<Compile Include="EventManager.cs" /> <Compile Include="EventManager.cs" />
<Compile Include="ChannelEvents\EventDtmfStatus.cs" /> <Compile Include="ChannelEvents\EventDtmfStatus.cs" />
<Compile Include="General\CallVariable.cs" />
<Compile Include="Ivr\DtmfEventArgs.cs" /> <Compile Include="Ivr\DtmfEventArgs.cs" />
<Compile Include="Ivr\IvrInterface.cs" /> <Compile Include="Ivr\IvrInterface.cs" />
<Compile Include="Ivr\IvrQueue.cs" /> <Compile Include="Ivr\IvrQueue.cs" />
...@@ -90,7 +91,7 @@ ...@@ -90,7 +91,7 @@
<Compile Include="Events\EventHeartbeat.cs" /> <Compile Include="Events\EventHeartbeat.cs" />
<Compile Include="Events\EventReSchedule.cs" /> <Compile Include="Events\EventReSchedule.cs" />
<Compile Include="General\Address.cs" /> <Compile Include="General\Address.cs" />
<Compile Include="General\ChannelVariable.cs" /> <Compile Include="General\CallLegVariable.cs" />
<Compile Include="General\SipAddress.cs" /> <Compile Include="General\SipAddress.cs" />
<Compile Include="General\SofiaSipAddress.cs" /> <Compile Include="General\SofiaSipAddress.cs" />
<Compile Include="PartyInfo.cs" /> <Compile Include="PartyInfo.cs" />
...@@ -103,6 +104,7 @@ ...@@ -103,6 +104,7 @@
<Compile Include="SipEvents\EventPresenceProbe.cs" /> <Compile Include="SipEvents\EventPresenceProbe.cs" />
<Compile Include="SipEvents\EventRoster.cs" /> <Compile Include="SipEvents\EventRoster.cs" />
<Compile Include="Events\EventMessageWaiting.cs" /> <Compile Include="Events\EventMessageWaiting.cs" />
<Compile Include="SipEvents\EventSofiaUnregister.cs" />
<Compile Include="SipEvents\SipEvent.cs" /> <Compile Include="SipEvents\SipEvent.cs" />
<Compile Include="SipEvents\EventPresence.cs" /> <Compile Include="SipEvents\EventPresence.cs" />
<Compile Include="SipEvents\EventSofiaExpire.cs" /> <Compile Include="SipEvents\EventSofiaExpire.cs" />
......
namespace FreeSwitch.EventSocket.General
{
/// <summary>
/// Variable stored only for a specific leg.
/// </summary>
/// <remarks>
/// Channel variables are used in FreeSWITCH to store information
/// that can be used by JavaScript programs.
/// </remarks>
public class CallLegVariable : CallVariable
{
/// <summary>
/// Initializes a new instance of the <see cref="CallLegVariable"/> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public CallLegVariable(string name, string value) : base(name, value)
{
}
}
}
namespace FreeSwitch.EventSocket.General
{
/// <summary>
/// Variable that applies to both legs in a call.
/// </summary>
/// <remarks>
/// Variables can be access from FreeSWITCH with the name
/// "variable_[name]"
/// </remarks>
public class CallVariable
{
private readonly string _name;
private readonly string _value;
/// <summary>
/// Initializes a new instance of the <see cref="CallVariable"/> class.
/// </summary>
/// <param name="name">Name of variable.</param>
/// <param name="value">Value.</param>
public CallVariable(string name, string value)
{
_name = name;
_value = value;
}
public string Name
{
get { return _name; }
}
public string Value
{
get { return _value; }
}
public override string ToString()
{
return _name + "=" + _value;
}
}
}
namespace FreeSwitch.EventSocket.General namespace FreeSwitch.EventSocket.General
{ {
/// <summary>
/// Destination as a FreeSWITCH Sofia sip address.
/// </summary>
public class SofiaSipAddress : Address public class SofiaSipAddress : Address
{ {
private readonly string _domain; private readonly string _profileName;
public SofiaSipAddress(string domain, string userName) /// <summary>
/// Initializes a new instance of the <see cref="SofiaSipAddress"/> class.
/// </summary>
/// <param name="profileName">Profile (context) name.</param>
/// <param name="userName">User name (and if needed, IP address / domain name).</param>
public SofiaSipAddress(string profileName, string userName)
{ {
Extension = userName; Extension = userName;
_domain = domain; _profileName = profileName;
} }
/// <summary>
/// Initializes a new instance of the <see cref="SofiaSipAddress"/> class.
/// </summary>
/// <param name="address">Another address.</param>
public SofiaSipAddress(SipAddress address) public SofiaSipAddress(SipAddress address)
{ {
_domain = address.Domain; _profileName = address.Domain;
Extension = address.Extension; Extension = address.Extension;
} }
public override string ToString() public override string ToString()
{ {
return "sofia/mydomain.com/" + Extension + "%" + _domain; return "sofia/" + _profileName + "/" + Extension;
} }
public static SofiaSipAddress Parse(string fullAddress) public static SofiaSipAddress Parse(string fullAddress)
{ {
string[] parts = fullAddress.Split('@'); string[] parts = fullAddress.Split('@');
if (parts.Length == 2) if (parts.Length == 2)
{
return new SofiaSipAddress(parts[1], parts[0]); return new SofiaSipAddress(parts[1], parts[0]);
}
else
{
parts = fullAddress.Split('/');
if (parts.Length == 3)
{
return new SofiaSipAddress(parts[1], parts[2]);
}
}
return null;
parts = fullAddress.Split('/');
return parts.Length == 3 ? new SofiaSipAddress(parts[1], parts[2]) : null;
} }
} }
} }
...@@ -95,14 +95,8 @@ namespace FreeSwitch.EventSocket ...@@ -95,14 +95,8 @@ namespace FreeSwitch.EventSocket
Caller = new PartyInfo(); Caller = new PartyInfo();
return Caller.Parse(name.Substring(7), value); return Caller.Parse(name.Substring(7), value);
} }
else
{
if (base.ParseCommand(name, value))
return true;
else
return res;
}
return base.ParseCommand(name, value) || res;
} }
return true; return true;
} }
......
using System;
namespace FreeSwitch.EventSocket namespace FreeSwitch.EventSocket
{ {
public class EventSofiaExpire : EventPresence public class EventSofiaExpire : EventPresence
{ {
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
private string _profileName; private string _profileName;
private string _userName; private string _userName;
private string _domain; private string _domain;
private string _userAgent; private string _userAgent;
private DateTime _expires;
public string ProfileName public string ProfileName
{ {
...@@ -31,6 +35,12 @@ namespace FreeSwitch.EventSocket ...@@ -31,6 +35,12 @@ namespace FreeSwitch.EventSocket
set { _userAgent = value; } set { _userAgent = value; }
} }
public DateTime Expires
{
get { return _expires; }
set { _expires = value; }
}
/* /*
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "profile-name", "%s", argv[6]); switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "profile-name", "%s", argv[6]);
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "user", "%s", argv[1]); switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "user", "%s", argv[1]);
...@@ -50,6 +60,11 @@ namespace FreeSwitch.EventSocket ...@@ -50,6 +60,11 @@ namespace FreeSwitch.EventSocket
case "host": case "host":
_domain = value; _domain = value;
break; break;
case "expires":
int seconds;
if (int.TryParse(value, out seconds))
_expires = UnixEpoch.AddSeconds(seconds);
break;
case "user-agent": case "user-agent":
_userAgent = value; _userAgent = value;
break; break;
......
namespace FreeSwitch.EventSocket.SipEvents
{
public class EventSofiaUnregister : EventBase
{
private string _domain;
private string _profileName;
private string _user;
private string _contact;
private string _callId;
private int _expires;
public string ProfileName
{
get { return _profileName; }
set { _profileName = value; }
}
public string UserName
{
get { return _user; }
set { _user = value; }
}
public string Domain
{
get { return _domain; }
set { _domain = value; }
}
public string Contact
{
get { return _contact; }
set { _contact = value; }
}
public string CallId
{
get { return _callId; }
set { _callId = value; }
}
public int Expires
{
get { return _expires; }
set { _expires = value; }
}
/*
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "profile-name", "%s", profile->name);
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from-user", "%s", to_user);
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from-host", "%s", to_host);
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "rpid", "%s", rpid);
*/
public override bool ParseCommand(string name, string value)
{
switch (name)
{
case "profile-name":
_profileName = value;
break;
case "from-user":
_user = value;
break;
case "from-host":
_domain = value;
break;
case "contact":
_contact = value;
break;
case "call-id":
_callId = value;
break;
case "expires":
int.TryParse(value, out _expires);
break;
default:
return base.ParseCommand(name, value);
}
return true;
}
public override string ToString()
{
return "SofiaUnregister(" + _user + "@" + _domain + ", " + _expires + ")." + base.ToString();
}
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论