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

📄 tran.cc

📁 此源代码只用于学习,不得用于其他商业活动 .
💻 CC
📖 第 1 页 / 共 4 页
字号:
/*
* Copyright (c) 2002, 南京联创系统集成股份有限公司综合结算产品部
* All rights reserved.
*
* 文件名称:tran.cc
* 摘    要:传输客户端
*
* 当前版本:
* 作    者:冯亮(fengl@lianchuang.com)
* 完成日期:
*/
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "c++dcl.h"
#include "basefun.h"

#include "tran.h"

/*线程数*/
int gv_iThreadCount;

/*远程主机信息*/
int gv_iServerPort;
char gv_chServerIp[15 + 1];

/*客户端ip*/
char gv_chClientIp[15 + 1];

/*线程空闲时休眠时间*/
int gv_iSleepTime;

/*日志告警路径*/
char gv_chLogPath[STRING_LEN];
char gv_chAlertPath[STRING_LEN];
char gv_chInfoPath[STRING_LEN];
/*存放配置文件内容*/
S_ThreadConfig *gv_ThreadConfig = 0;

S_GobalMutex gv_GobalMutex;
C_Monitor gv_TranMonitor;

C_LogAlert gv_TranAlert;
C_LogAlert gv_TranLog;
C_LogAlert gv_OperInfo;

char gv_chBackupDir[STRING_LEN];

/*通道号*/
char gv_chChannelCode[CHANNEL_CODE_LEN + 1];

int gv_iTermFlag = 1;

/*用来线程间通信*/
volatile int g_iArrayFlag[MAX_THREAD_NUMS];

/*用来判断各个线程是否已经退出*/
volatile int g_iArrayExitFlag[MAX_THREAD_NUMS];

char g_ReMemoryMatrix[MAX_THREAD_NUMS][STRING_LEN];

int main(int argc, char **argv)
{
	ios::sync_with_stdio();

	pthread_t tid[MAX_THREAD_NUMS];

	#ifdef	_DEBUG_FOR_TRAN_
	time_t lv_startTime, lv_endTime;
	double lv_diffTime;

	time(&lv_startTime);
	#endif

	if(argc < 3)
	{
		printf("\nUSAGE: %s iChannel iPosition\n", argv[0]);
		printf("       \"iChannel\" is a serial number.\n");
		printf("       \"iPosition\" is the index in the share memory.\n");
		return -1;
	}

	char chAlertInfo[STRING_LEN];
	memset(chAlertInfo, 0, sizeof(chAlertInfo));

	/*判断程序是否已经运行*/
	if(gv_TranMonitor.ProcIsExist(argc, argv))
	{
		printf("Program '%s' is already running, use 'ps' to check!\n", argv[0]);
		return -1;
	}

	/*初始化共享内存*/
	if(!gv_TranMonitor.MonitorInit(argc, argv))
	{
		printf(	"%s: Init Monitor fail!\n",argv[0]);
		return -1;
	}

	gv_TranMonitor.ReportStatus("INIT", "进程正在初始化,请稍候...");

	if(!InitLogAlert(argv))
	{
		gv_TranMonitor.ReportStatus("ERROR", "进程出错,正在退出...");
		printf("InitLogAlert Error!\n");
		return -1;
	}

	if(!LoadConfig(argc, argv))
	{
		gv_TranMonitor.ReportStatus("ERROR", "进程出错,正在退出...");
		sprintf(chAlertInfo, "%s", "LoadConfig error!");
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(LOADCONFIG_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		delete[] gv_ThreadConfig;
		gv_OperInfo.Stop(OPER_CODE);
		return -1;
	}

	pthread_mutex_init(&gv_GobalMutex.LogMutex, 0);
	pthread_mutex_init(&gv_GobalMutex.AlertMutex, 0);
	pthread_mutex_init(&gv_GobalMutex.ConfigFileMutex, 0);
	pthread_mutex_init(&gv_GobalMutex.SearchFileMutex, 0);

	int i;

	for(i = 1; i <= gv_iThreadCount; ++i)
	{
		g_iArrayFlag[i] = 1;
		g_iArrayExitFlag[i] = 1;
	}

	InitSignal();

	for(i = 1; i <= gv_iThreadCount; ++i)
	{
		if(pthread_create(&tid[i], 0, ClientThread, (void *)i) != 0)
		{
			gv_TranMonitor.ReportStatus("ERROR", "进程出错,正在退出...");
			sprintf(chAlertInfo, "%s%d error!", "ThreadNo", i);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(CREATETHREAD_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			delete[] gv_ThreadConfig;
			gv_OperInfo.Stop(OPER_CODE);
			return -1;
		}
	}

	int iCommitTime = 0;
	char chStatus[STRING_LEN];
	char chObject[STRING_LEN];
	char chTemp[STRING_LEN];
	vector<int> vFlag;

	for(i = 0; i <= gv_iThreadCount; ++i)
	{
		vFlag.push_back(1);
	}

	bool bFlag;

	while(gv_iTermFlag == 1)
	{
		iCommitTime++;
		if(COMMIT_TIME == iCommitTime)
		{
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			gv_TranAlert.Commit();
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			pthread_mutex_lock(&gv_GobalMutex.LogMutex);
			gv_TranLog.Commit();
			pthread_mutex_unlock(&gv_GobalMutex.LogMutex);
			iCommitTime = 0;
		}

		sleep(1);

		memset(chStatus, 0, sizeof(chStatus));
		memset(chObject, 0, sizeof(chObject));
		for(i = 1; i <= gv_iThreadCount; ++i)
		{
			if(strlen(gv_ThreadConfig[i].FileName) != 0)
			{
				//传输并显示最前面的文件名和偏移量
				sprintf(chObject, "%sF", chObject);
				sprintf(
					chTemp,
					"%s(%ld)",
					gv_ThreadConfig[i].FileName,
					gv_ThreadConfig[i].FilePos
					);
				vFlag[i] = 1;
				break;
			}
			else
			{
				//空闲
				sprintf(chObject, "%sI", chObject);
				vFlag[i] = 0;
			}
		}
		i++;
		while(i <= gv_iThreadCount)
		{
			if(strlen(gv_ThreadConfig[i].FileName) != 0)
			{
				//传输,不显示文件信息
				sprintf(chObject, "%sR", chObject);
			}
			else
			{
				//空闲
				sprintf(chObject, "%sI", chObject);
			}
			i++;
		}
		int iTotal = 0;
		for(i = 1; i < vFlag.size(); ++i)
		{
			iTotal += vFlag[i];
		}
		if(0 == iTotal)
		{
			bFlag = true;
		}
		else
		{
			bFlag = false;
		}
		if(bFlag)
		{

			#ifdef	_DEBUG_FOR_TRAN_
			time(&lv_endTime);
			lv_diffTime = difftime(lv_endTime, lv_startTime);
			if(lv_diffTime == 0)
			{
				lv_diffTime = 1;
			}
			printf("\ntotal time is %ld\n", (long)lv_diffTime);
			#endif

			strcpy(chStatus, "IDLE");
			sprintf(chObject, "%s.%s", chObject, "没有需要传输的文件");
		}
		else
		{
			strcpy(chStatus, "RUN");
			sprintf(chObject, "%s.%s", chObject, chTemp);

		}
		gv_TranMonitor.ReportStatus(chStatus, chObject);
	}/*while*/

	gv_TranMonitor.ReportStatus("EXIT", "进程正常退出,请稍候...");

	JudgeFlag();

	pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
	gv_TranAlert.Commit();
	pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);

	pthread_mutex_lock(&gv_GobalMutex.LogMutex);
	gv_TranLog.Commit();
	pthread_mutex_unlock(&gv_GobalMutex.LogMutex);

	pthread_mutex_destroy(&gv_GobalMutex.LogMutex);
	pthread_mutex_destroy(&gv_GobalMutex.AlertMutex);
	pthread_mutex_destroy(&gv_GobalMutex.ConfigFileMutex);
	pthread_mutex_destroy(&gv_GobalMutex.SearchFileMutex);

	delete[] gv_ThreadConfig;
	gv_OperInfo.Stop(OPER_CODE);
	return 0;
}
/****************************************************************
**
**	Function: JudgeFlag
**	Purpose:
** 	Input Parammeters:

**	Return:
**	datetime:
**
*****************************************************************/
void JudgeFlag()
{
	int i;
	for(i = 1; i <= gv_iThreadCount; ++i)
	{
		g_iArrayFlag[i] = 0;
	}
	int iResult = 0;
	i = 1;
	while(true)
	{
		iResult += g_iArrayExitFlag[i];
		++i;
		if(i == gv_iThreadCount + 1)
		{
			if(0 == iResult)
			{
				break;
			}
			iResult = 0;
			i = 1;
		}
	}
	return;
}

/****************************************************************
**
**	Function: InitLogAlert
**	Purpose:  初始化日志告警
** 	Input Parammeters: char **argv

**	Return:
**	datetime:
**
*****************************************************************/
bool InitLogAlert(char **argv)
{
	C_Config Config;
	char chCommonConfigFile[STRING_LEN];
	char Section[STRING_LEN], Key[STRING_LEN], Value[STRING_LEN];
	char *pchAppPath = 0;
	char chAlertInfo[STRING_LEN];

	//根据环境变量获得配置文件的位置
	if((pchAppPath = getenv(SETTLE_HOME)) == 0)
	{
		printf("Not get the environment \"%s\" in InitLogAlert.", SETTLE_HOME);
		return false;
	}
	if(!IsDirectory(pchAppPath))
	{
		printf("The directory \"%s\" is not exist!", pchAppPath);
		return false;
	}
	FullPath(pchAppPath);

	memset(chCommonConfigFile, 0, sizeof(chCommonConfigFile));

	strcpy(chCommonConfigFile, pchAppPath);
	strcat(chCommonConfigFile, COMMON_CONFIG_FILE);

	strcpy(Section, "LogAlert");

	strcpy(Key,"LogPath");
	if (Config.GetParam(chCommonConfigFile,Section,Key,Value) == false)
	{
		printf(
			"Config file error at %s-->%s-->%s\n",
			chCommonConfigFile,
			Section,
			Key
			);
		return false;
	}
	strncpy(gv_chLogPath,Value,sizeof(gv_chLogPath) - 1);
	if(!IsDirectory(gv_chLogPath))
	{
		printf(
			"The directory \"%s\" is not exist!",
			gv_chLogPath
			);
		return false;
	}
	FullPath(gv_chLogPath);

	strcpy(Key,"AlertPath");
	if (Config.GetParam(chCommonConfigFile,Section,Key,Value) == false)
	{
		printf(
			"Config file error at %s-->%s-->%s\n",
			chCommonConfigFile,
			Section,
			Key
			);
		return false;
	}
	strncpy(gv_chAlertPath, Value, sizeof(gv_chAlertPath) - 1);
	if(!IsDirectory(gv_chAlertPath))
	{
		printf(
			"The directory \"%s\" is not exist!",
			gv_chAlertPath
			);
		return false;
	}
	FullPath(gv_chAlertPath);

	strcpy(Key,"InfoPath");
	if (Config.GetParam(chCommonConfigFile,Section,Key,Value) == false)
	{
		printf(
			"Config file error at %s-->%s-->%s\n",
			chCommonConfigFile,
			Section,
			Key
			);
		return false;
	}
	strncpy(gv_chInfoPath, Value, sizeof(gv_chInfoPath) - 1);
	if(!IsDirectory(gv_chAlertPath))
	{
		printf(
			"The directory \"%s\" is not exist!",
			gv_chInfoPath
			);
		return false;
	}
	FullPath(gv_chInfoPath);

	if(!gv_OperInfo.InitForInfo(gv_chInfoPath, MODEL_NAME, argv[1]))
	{
		printf("InitForInfo error!\n");
		return false;
	}
	gv_OperInfo.Start(OPER_CODE);
	char chLogAlertFileHead[STRING_LEN];
	memset(chLogAlertFileHead, 0, sizeof(chLogAlertFileHead));

	//此变量用来截取不带路径的执行文件名
	char *pchTemp = 0;
	pchTemp = strrchr(argv[0], '/');
	memset(chLogAlertFileHead, 0, sizeof(chLogAlertFileHead));

	if(0 == pchTemp)
	{
		sprintf(chLogAlertFileHead, "%s.%s", argv[0], argv[1]);
		Trim(chLogAlertFileHead);
	}
	else
	{
		pchTemp++;
		sprintf(chLogAlertFileHead, "%s.%s", pchTemp, argv[1]);
		Trim(chLogAlertFileHead);
	}

	#ifdef	_DEBUG_FOR_TRAN_
	printf("gv_chLogPath = %s\n", gv_chLogPath);
	printf("gv_chAlertPath = %s\n", gv_chAlertPath);
	printf("chLogAlertFileHead = %s\n", chLogAlertFileHead);
	#endif//_DEBUG_FOR_TRAN_

	if(!gv_TranLog.Initialize(gv_chLogPath, chLogAlertFileHead))
	{
		return false;
	}
	if(!gv_TranAlert.Initialize(gv_chAlertPath, chLogAlertFileHead))
	{
		return false;
	}
	return true;
}

/****************************************************************
**
**	Function: LoadConfig
**	Purpose:  读取配置文件数据
** 	Input Parammeters:

**	Return:	true -- 成功  false -- 失败
**	datetime:
**
*****************************************************************/
bool LoadConfig(int argc, char **argv)
{

⌨️ 快捷键说明

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