by MarcoBoschi » Tue Sep 01, 2009 7:35 am
Antonio,
do you think that a program like this may translated into xharbour/Fivewin?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using INNOVAPHONE.test.Cs.pbx_wsdl;
namespace INNOVAPHONE.test.Cs
{
//
// this form will merely show a window with cumulating PBX events
// its purpose is to demonstrate the use of async events in VB.Net
//
public partial class FrmMain : Form
{
// the forms PBX link
myPBX pbx;
// PBX access data
// assuming the controling user is "_TAPI_" which has a user-password "access"
const string httpUser = "admin";
const string httpPw = "ip800";
const string pbxUser = "_TAPI_";
const string pbxMonitor = "Marco Boschi";
const string pbxUrl = "http://172.28.2.240/PBX0/user.soap";
// PBX runtime data
public int pbxKey;
public int pbxSession;
public int pbxUserId;
public FrmMain()
{
InitializeComponent();
}
// initalize pbx link on load
private void FrmMain_Load(object sender, System.EventArgs e)
{
FrmMain frm = (FrmMain)sender;
try
{
// create the link
pbx = new myPBX(ref frm, addEvent, showEchoResult, callStarted);
// set the URL
pbx.Url = pbxUrl;
// set the HTTP level credentials
pbx.Credentials = new System.Net.NetworkCredential(httpUser, httpPw, "");
// initialize the session, remember the session-id and -key
pbxSession = pbx.Initialize(pbxUser, "VBNet SOAP Test", true, true, out pbxKey);
// monitor a user
pbxUserId = pbx.UserInitialize(pbxSession, pbxMonitor, false);
// start async retrieval of events from pbx
pbx.startPolling();
}
catch (Exception err)
{
string msg1;
string msg2;
msg1 = err.Message;
if (err.InnerException != null)
{
msg2 = err.InnerException.Message;
}
else
{
msg2 = "";
}
MessageBox.Show("pbx link initialization failed: " + msg1 + " / " + msg2);
}
}
public void addEvent(string ev)
{
events.Items.Add(ev);
}
private void btnEcho_Click(object sender, EventArgs e)
{
try
{
FrmMain main = this;
myPBX _m = new myPBX(ref main, addEvent, showEchoResult, callStarted);
// set the URL
_m.Url = pbxUrl;
// set the HTTP level credentials
_m.Credentials = new System.Net.NetworkCredential(httpUser, httpPw, "");
// initialize the session, remember the session-id and -key
int _mKey;
int _mSession = _m.Initialize(pbxUser, "VBNet SOAP Test", true, true, out _mKey);
// monitor a user
//int _mUserId = _m.UserInitialize(pbxSession, pbxMonitor, false);
_m.startEcho(pbxSession, pbxKey);
}
catch (Exception ex)
{
MessageBox.Show("Eccezione in esecuzione richiesta.");
}
}
public void showEchoResult(string res)
{
MessageBox.Show(res);
}
public void callStarted(string res)
{
MessageBox.Show(res);
}
private void btnCall_Click(object sender, EventArgs e)
{
try
{
pbx.createCall();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
// the PBX link
// we need a derived class to be able to handle the async events
public class myPBX : pbx_wsdl.pbx
{
// derive from the auto-generated soap class
// link to form
private FrmMain form;
public delegate void AddEventDelegate(string ev);
private AddEventDelegate _addEventDelegate;
public delegate void ShowEchoResultDelegate(string res);
private ShowEchoResultDelegate _showEchoResultDelegate;
public delegate void CallStartedDelegate(string res);
private CallStartedDelegate _callStartedDelegate;
// constructor to save the form link
public myPBX(ref FrmMain form, AddEventDelegate eventD, ShowEchoResultDelegate echoD, CallStartedDelegate callD)
{
//preparo i metodi esterni per la gestione asincrona dei risultati dei vari metodi
_addEventDelegate = eventD;
_showEchoResultDelegate = echoD;
_callStartedDelegate = callD;
//prendo il form per poter invocare il metodo corretto
this.form = form;
//collego i miei metodi di gestione dei risultati a quelli del webService
this.PollCompleted += new pbx_wsdl.PollCompletedEventHandler(pollCB);
this.EchoCompleted += new pbx_wsdl.EchoCompletedEventHandler(echoCB);
this.UserCallCompleted += new pbx_wsdl.UserCallCompletedEventHandler(userCallCB);
}
// setup an async Poll
public void startPolling()
{
try
{
this.PollAsync(form.pbxSession);
}
catch (Exception e)
{
MessageBox.Show("start poll failed: " + e.Message + " / " + e.InnerException.ToString());
}
}
// setup an async call request
public void createCall()
{
try
{
int key;
int session_h = this.Initialize("Marco Boschi", "demo", true, true, out key);
int user_h = this.UserInitialize(session_h, "Marco Boschi", true);
this.UserCallAsync(user_h, "", "267", "", 0, null);
}
catch (Exception e)
{
MessageBox.Show("start call failed: " + e.Message + " / " + e.InnerException.ToString());
}
}
//setup an async Exho (Nicola 14/11/2008
public void startEcho(int _session, int _key)
{
try
{
this.EchoAsync(_session, _key);
}
catch (Exception ex)
{
MessageBox.Show("start echo failed: " + ex.Message + " / " + ex.InnerException.ToString());
}
}
// handle an async Poll result
private void pollCB(object sender, pbx_wsdl.PollCompletedEventArgs e)
{
// scan user and call events
foreach (pbx_wsdl.UserInfo ui in e.Result.user)
{
string description = "user ";
if (!String.IsNullOrEmpty(ui.cn))
{
description = description + "(" + ui.type + ") " + ui.cn;
}
// Me.form.events.Items.Add("user " + ui.cn) -- dangerous
this.form.Invoke(_addEventDelegate, new object[] { description });
}
foreach (pbx_wsdl.CallInfo ci in e.Result.call)
{
// Me.form.events.Items.Add("call -> " + ci.msg) -- dangerous
this.form.Invoke(_addEventDelegate, new object[] { "call -> " + ci.msg });
}
// schedule next async Poll
this.startPolling();
}
//handle an async Echo result
private void echoCB(object sender, pbx_wsdl.EchoCompletedEventArgs e)
{
this.form.Invoke(_showEchoResultDelegate, new object[] { String.Concat("il controllo di sessione ha risposto: ", e.Result) });
}
//handle an async userCall result
private void userCallCB(object sender, pbx_wsdl.UserCallCompletedEventArgs e)
{
string error = "";
if (e.Error != null)
{
error = e.Error.Message;
}
string message = String.Format("Creazione cancellata ({0}). Errore: {1}.\r\nRisultato: {2}.\r\nUserState: {3}.", e.Cancelled, error, e.Result, e.UserState.ToString());
this.form.Invoke(_callStartedDelegate, new object[] { message });
}
}
}