📄 recvsm.cpp
字号:
/* --------------------------------------------------------------------------
Name: RecvSM.cpp
Title: CMPP API of ISMG for CMPP 1.1
Package: Receive short message sample code for Asiainfo CMPP 1.1 API
Written: 2000/12/20 Asiainfo
Revised:
Synopsis:
Editor: TAB=4
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CMPPAPI.hpp"
void Usage(char *sProgram);
int MOProcess(DeliverResp *pDeliverResp);
// public variables
char sParamFile[100] = "", sMsgFile[100] = "";
int nOutput = 1;
extern int CMPPDeliverError(const int nTimeout, DeliverResp *pDeliverResp, int nErrorType);
int main(int argc, char ** argv)
{
int n;
FILE *pFile;
int nTimeout = 0, nFunType = 0;
DeliverResp theDeliverResp;
char sTime[20], sSMCNo[20], sStat[8], sSubmitTime[11], sDoneTime[11], sDestTermID[21];
int nMsgID, nSMCSequence;
// get argument
for(n=1; n<argc; ++n)
{
if(strcmp(argv[n], "-n") == 0)
{
nOutput = 0;
}
else if(strcmp(argv[n], "-t") == 0)
{
n++;
nTimeout = atoi(argv[n]);
}
else if(strcmp(argv[n], "-p") == 0)
{
n++;
strcpy(sParamFile, argv[n]);
}
else if(strcmp(argv[n], "-m") == 0)
{
n++;
strcpy(sMsgFile, argv[n]);
}
else if(strcmp(argv[n], "-f") == 0)
{
n++;
nFunType = atoi(argv[n]);
}
else
{
if(nOutput == 1)
{
Usage(argv[0]);
}
exit(1);
}
}
if(strlen(sMsgFile) == 0 || strlen(sParamFile) == 0)
{
if(nOutput == 1)
{
printf("No parameter file or message file!\n");
}
exit(1);
}
// initiate API
if(InitCMPPAPI() != 0)
{
if(nOutput == 1)
{
printf("Fail to call InitCMPPAPI!\n");
}
exit(1);
}
if(nFunType == 1)
{
if(CMPPDeliverCallback(MOProcess) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPDeliverCallback!\n");
}
exit(1);
}
}
else
{
if(nFunType == 0)
{
if(CMPPDeliver(nTimeout, &theDeliverResp) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPDeliver!\n");
}
exit(1);
}
// write receiving short message into files
pFile = fopen(sParamFile, "w");
if(pFile == NULL)
{
if(nOutput == 1)
{
printf("Fail to open file %s!\n", sParamFile);
}
exit(1);
}
fprintf(pFile, "%s\n", theDeliverResp.sMsgID);
fprintf(pFile, "%d\n", theDeliverResp.nMsgLevel);
fprintf(pFile, "%s\n", theDeliverResp.sServiceID);
fprintf(pFile, "%d\n", theDeliverResp.nMsgFormat);
fprintf(pFile, "%s\n", theDeliverResp.sSrcTermID);
fprintf(pFile, "%d\n", theDeliverResp.nIsReply);
fprintf(pFile, "%d\n", theDeliverResp.nMsgLen);
fprintf(pFile, "%s\n", theDeliverResp.sDestTermID);
fclose(pFile);
pFile = fopen(sMsgFile, "wb");
if(pFile == NULL)
{
if(nOutput == 1)
{
printf("Fail to open file %s!\n", sMsgFile);
}
exit(1);
}
fwrite(theDeliverResp.sMsgContent, theDeliverResp.nMsgLen, 1, pFile);
fclose(pFile);
}
else if(nFunType == 2)
{
while(1)
{
if(CMPPDeliver(nTimeout, &theDeliverResp) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPDeliver!\n");
}
continue;
}
printf("\n");
printf("MsgID=%s\n", theDeliverResp.sMsgID);
printf("MsgLevel=%d\n", theDeliverResp.nMsgLevel);
printf("ServiceID=%s\n", theDeliverResp.sServiceID);
printf("MsgFormat=%d\n", theDeliverResp.nMsgFormat);
printf("SrcTermID=%s\n", theDeliverResp.sSrcTermID);
printf("IsReply=%d\n", theDeliverResp.nIsReply);
printf("MsgLen=%d\n", theDeliverResp.nMsgLen);
printf("DestTermID=%s\n", theDeliverResp.sDestTermID);
if(theDeliverResp.nIsReply == 1)
{
CMPPAnalyzeStatusReport(theDeliverResp.sMsgContent,
sTime, sSMCNo, &nMsgID,
sStat, sSubmitTime, sDoneTime,
sDestTermID, &nSMCSequence);
printf( "## status report ##\n"
"time=%s\n"
"smc_no=%s\n"
"msg_id=%010d%05d%05d\n"
"status=%s\n"
"submit_time=%s\n"
"done_time=%s\n"
"termid=%s\n"
"smc_seq=%d\n",
sTime, sSMCNo, atoi(sTime), atoi(sSMCNo), nMsgID,
sStat, sSubmitTime, sDoneTime,
sDestTermID, nSMCSequence);
}
else
{
printf("\n#%s#\n", theDeliverResp.sMsgContent);
}
}
}
else if(nFunType == 3 || nFunType == 4)
{
if(nFunType == 3)
{
n = 0;
}
else
{
n = 1;
}
if(CMPPDeliverError(nTimeout, &theDeliverResp, n) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPDeliverError!\n");
}
exit(1);
}
printf("$$ Call CMPPDeliverError\n");
printf("%s\n", theDeliverResp.sMsgID);
printf("%d\n", theDeliverResp.nMsgLevel);
printf("%s\n", theDeliverResp.sServiceID);
printf("%d\n", theDeliverResp.nMsgFormat);
printf("%s\n", theDeliverResp.sSrcTermID);
printf("%d\n", theDeliverResp.nIsReply);
printf("%d\n", theDeliverResp.nMsgLen);
printf("%s\n", theDeliverResp.sDestTermID);
printf("content=%s!\n", theDeliverResp.sMsgContent);
}
}
exit(0);
return(0);
}
void Usage(char *sProgram)
{
printf("Usage: %s [-n] [-f function] <-t timeout> <-p parameter_file> <-m message_file>\n", sProgram);
printf(" -n: no message output\n");
printf(" -t: waiting time, in second. 0 means wait forever.\n");
printf(" -p: receiving short message parameter file name.\n");
printf(" -m: receiving short message content file name.\n");
printf(" -f: 0=CMPPDeliver, 1=CMPPDeliverCallback, 2=Loop receive\n");
printf("If call CMPPDeliverCallback, %s will receive MO messages continuously until user break.\n", sProgram);
printf("e.g.: %s -t 10 -p sm.par -m sm.txt\n", sProgram);
}
int MOProcess(DeliverResp *pDeliverResp)
{
static int nCounter = 0;
char sFileName[100];
FILE *pFile;
sprintf(sFileName, "%s.%03d", sParamFile, nCounter);
pFile = fopen(sFileName, "w");
if(pFile == NULL)
{
if(nOutput == 1)
{
printf("Fail to open file %s!\n", sFileName);
}
return(1);
}
fprintf(pFile, "%s\n", pDeliverResp->sMsgID);
fprintf(pFile, "%d\n", pDeliverResp->nMsgLevel);
fprintf(pFile, "%s\n", pDeliverResp->sServiceID);
fprintf(pFile, "%d\n", pDeliverResp->nMsgFormat);
fprintf(pFile, "%s\n", pDeliverResp->sSrcTermID);
fprintf(pFile, "%d\n", pDeliverResp->nIsReply);
fprintf(pFile, "%d\n", pDeliverResp->nMsgLen);
fprintf(pFile, "%s\n", pDeliverResp->sDestTermID);
fclose(pFile);
sprintf(sFileName, "%s.%03d", sMsgFile, nCounter);
pFile = fopen(sFileName, "wb");
if(pFile == NULL)
{
if(nOutput == 1)
{
printf("Fail to open file %s!\n", sFileName);
}
return(1);
}
fwrite(pDeliverResp->sMsgContent, pDeliverResp->nMsgLen, 1, pFile);
fclose(pFile);
nCounter++;
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -