⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.cpp

📁 三汇CTI示例程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "StdAfx.h"
#include "App.h"

#include "MainFrame.h"

IMPLEMENT_DYNAMIC(MainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(MainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_TIMER() 
	ON_WM_DESTROY()
END_MESSAGE_MAP()

// Channel list
Channel *channels;

BOOL IsStartCti;

// Conference_id - master_channel pair
map<int, int> cm_pair;

int total_ch;

// Master Channel Configure

// eserved channels
int master[] = {0, 1, 2, 3};
const int master_num = sizeof(master) / sizeof(int);


// Add files to playlist by num. 
//	Example: if num = 12, it will add 1.wav and 2.wav to the playlist of ch. 
void add_list(int ch, long num)
{
	int a = log10(num);
	int d;
	char s[2];
	char file[10];

	while (a >= 0)
	{
		// Get first bit (10-based)
		d = num / pow(10, a);
		num -= d * pow(10, a--);
		itoa(d, s, 10);

		// Cat a filename
		strcpy(file, "wav\\");
		strcat(file, s);
		strcat(file, ".wav");

		// Add to the playlist
		CHECK_RETURN SsmAddToFileList(ch, file, -1, 0, -1);
	}
}

// Get its master channel of the parameter 'conf'
int get_master(int conf)
{
	return cm_pair.find(conf)->second;
}

// Get master channel of the conf that parameter 'channel' joined.
int get_chs_master(int channel)
{
	int GrpId, MmbrId;
	DWORD SilenceTime;
	WORD JoinMode, IsSpeaking;
	CHECK_RETURN SsmGetConfChInfo(channel, &GrpId, &MmbrId, &JoinMode, &IsSpeaking, &SilenceTime);

	return get_master(GrpId);
}

// get an idle channel for the master from the array master
int get_idle()
{
	for (int i = 0; i < total_ch; i++)
	{ 
		if (channels[i].master == MASTER_IDLE)
			return i;
	}
	return -1;
}

// check whether the two channel are in the same conference
bool is_same_group(int ch1, int ch2)
{
	int group1, group2;
	int tmp;
	WORD tmp2;
	DWORD tmp3;
	if (SsmGetConfChInfo(ch1, &group1, &tmp, &tmp2, &tmp2, &tmp3) == -1)
		return false;
	if (SsmGetConfChInfo(ch2, &group2, &tmp, &tmp2, &tmp2, &tmp3) == -1)
		return false;

	return (group1 == group2);

}

// UI and something unimportant
int MainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	CFrameWnd::OnCreate(lpCreateStruct);
	EVENT_SET_INFO EventSet;
	int i;

	// CTI Board initalizes
	IsStartCti = FALSE;
	if (SsmStartCti("ini\\shconfig.ini", "ini\\shindex.ini") != 0)
	{
		MessageBox(SsmGetLastErrMsgA(), _T("Conference Demo"), MB_OK | MB_ICONWARNING);
		DestroyWindow();
	}
	else if (SsmGetMaxUsableBoard() != SsmGetMaxCfgBoard())
	{
		MessageBox(SsmGetLastErrMsgA(), _T("Conference Demo"), MB_OK | MB_ICONWARNING);
		DestroyWindow();
	}
	else
	{
		IsStartCti = TRUE;

		EventSet.dwWorkMode = EVENT_MESSAGE;
		EventSet.lpHandlerParam = this->GetSafeHwnd();
		CHECK_RETURN SsmSetEvent(-1, -1, TRUE, &EventSet);

		total_ch = SsmGetMaxCh();
		if (total_ch >= master_num)
		{
			channels = new Channel[total_ch];
			for (i = 0; i < total_ch; i++)
			{
				channels[i].state_channel = CH_IDLE;
				channels[i].state_conference = CO_IDLE;
				channels[i].type = SsmGetChType(i);
				channels[i].master = NOT;
				channels[i].BgSoundEnabled = true;
				channels[i].IsPlayingBgSound = false;
				channels[i].BgSoundVolume = 0;
				channels[i].ch_to_talk_securely = -1;
			}
			for(i = 0; i < master_num; i++)
			{
				if (master[i] < total_ch)
				{
					channels[master[i]].master = MASTER_IDLE;
				}
			}
		}
		else
		{
			MessageBox("Can not get enough channels.", _T("Conference Demo"), MB_OK | MB_ICONWARNING);
			DestroyWindow();
		}
		SetTimer(0, 100, NULL);

	}

	return 0;
}

BOOL MainFrame::DestroyWindow()
{
	CFrameWnd::DestroyWindow();
	return TRUE;
}

void MainFrame::OnDestroy()
{
	if (IsStartCti)
	{
		delete[] channels;
		SsmCloseCti();
	}

	return;
}

BOOL MainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	wndSplitter.CreateStatic(this, 1, 2);
	wndSplitter.CreateView(0, 0, RUNTIME_CLASS(LeftView), CSize(400, 100), pContext);
	wndSplitter.CreateView(0, 1, RUNTIME_CLASS(ChildView), CSize(400, 100), pContext);

	wndStatusBar.Create(this);

	return TRUE;
}

BOOL MainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	CFrameWnd::PreCreateWindow(cs);

	return TRUE;
}

BOOL MainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	if (wndSplitter.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;
	else
		return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

//		Deal with Synway messages.
//		It is important for the demo and you may refer here.
LRESULT MainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	int new_state;
	int ch;
	char dtmf;

	if (message >= WM_USER)
	{
		ch = wParam;
		switch (message - WM_USER)
		{
			// Here is about the channel state, it will send unitive messages to the conference.
			//	For more details, please see Channel flowchart.
			case E_CHG_ChState:
				new_state = lParam & 0x00FF;
					// Trunk and digital
				if (channels[ch].type == 0 ||
					channels[ch].type == 4 ||
					channels[ch].type == 6 ||
					channels[ch].type == 7 ||
					channels[ch].type == 8 ||
					channels[ch].type == 11)
				{
					if (new_state == 2 && channels[ch].state_channel == CH_IDLE)
					{
						CHECK_RETURN SsmPickup(ch);
						channels[ch].state_channel = CH_WAIT_TALK;
					}
					else if (new_state == 3 && channels[ch].state_channel == CH_WAIT_TALK)
					{
						PostMessage(TO_START, ch);
						channels[ch].state_channel = CH_TALK;
					}
					else if (channels[ch].state_channel == CH_WAIT_TALK)
					{
						CHECK_RETURN SsmHangup(ch);
						channels[ch].state_channel = CH_IDLE;
					}
					else if (channels[ch].state_channel == CH_TALK)
					{
						CHECK_RETURN SsmHangup(ch);
						PostMessage(TO_END, ch);
						channels[ch].state_channel = CH_IDLE;
					}
				}
				// Station
				else if (channels[ch].type == 2) 
				{
					if (new_state == 1 && channels[ch].state_channel == CH_IDLE)
					{
						PostMessage(TO_START, ch);
						channels[ch].state_channel = CH_TALK;
					}
					else if (new_state == 0 && channels[ch].state_channel == CH_TALK)
					{
						PostMessage(TO_END, ch);
						channels[ch].state_channel = CH_IDLE;
					}
				}
				break; 

			// Here is about dtmf for each choice.
			case E_CHG_RcvDTMF:
				// Arc 4
				if (channels[ch].state_conference == CO_CREATE_OR_JOIN)
				{
					dtmf = lParam & 0x00FF;
					if (dtmf == '#')
					{
						// To create
						if (channels[ch].dtmf_str.length() == 0)
						{
							// Create a conference.
							int conf_id = SsmCreateConfGroup(-1, -1, -1, -1);
							channels[ch].creater = true;
							if (conf_id >= 0)
							{
								channels[ch].conf_to_join = conf_id;
								int master_ch = get_idle();
								if (master_ch >= 0)
								{
									// Add master
									channels[master_ch].master = MASTER_BUSY;
									cm_pair.insert(make_pair(conf_id, master_ch));
									CHECK_RETURN SsmJoinConfGroup(conf_id, master_ch, 0, 0, FALSE, FALSE);
									// Record
									time_t a_clock;
									char *file_name;
									char *file_namex;
									time(&a_clock);
									file_name = asctime(localtime(&a_clock));
									file_namex = (char *)malloc(strlen(file_name) + 10);
									strcpy(file_namex, "rec\\");
									strcat(file_namex, file_name);
									strcat(file_namex, ".wav");
									for (int i = 0; i < strlen(file_namex); i++)
									{
										if ( file_namex[i] == ':')
											file_namex[i] = '_';
										else if ( file_namex[i] == '\x0a')
											file_namex[i] = ' ';
									}
									CHECK_RETURN SsmRecToFile(master_ch, file_namex, -1, 0, -1, 0, 0);
									// Play "The id of the conference created is ..." .
									CHECK_RETURN SsmClearFileList(ch);
									CHECK_RETURN SsmAddToFileList(ch, "wav\\d.wav", -1, 0, -1);
									add_list(ch, conf_id);
									// Play "Choose join-mode ...".
									CHECK_RETURN SsmAddToFileList(ch, "wav\\c.wav", -1, 0, -1);
									CHECK_RETURN SsmPlayFileList(ch);
									// Change state.
									channels[ch].state_conference = CO_HOW_TO_JOIN;
								}
								else // No more master channels
								{
									CHECK_RETURN SsmFreeConfGroup(conf_id);
									CHECK_RETURN SsmClearFileList(ch);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -