📄 sendsm.cpp
字号:
/* --------------------------------------------------------------------------
Name: SendSM.cpp
Title: CMPP API of ISMG for CMPP 1.1
Package: Send 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 <sys/types.h>
#include <sys/stat.h>
#include "CMPPAPI.hpp"
#define SEND_MODE_SINGLE 0
#define SEND_MODE_BATCH 1
typedef struct
{
int nNeedReply;
int nMsgLevel;
char sServiceID[10+1];
int nMsgFormat;
char sFeeType[2+1];
char sFeeCode[6+1];
char sValidTime[17+1];
char sAtTime[17+1];
char sSrcTermID[21+1];
char sDestTermID[21+1];
char sDestTermIDFile[100];
char sMsgFile[100];
char sMsgIDFile[100];
} SMArgument;
void Usage(char *sProgram);
int ReadArgumentFile(char *sFile, SMArgument *pSMArgument, int nOutput);
int main(int argc, char ** argv)
{
int nSendMode = SEND_MODE_SINGLE, nOutput = 1;
char sArgFile[100] = "";
SMArgument theSMArgument;
char sMsgID[21+1];
int nErrorCode;
FILE *pFile;
int nMsgLen, n;
char sMsgContent[10240];
char sFeeTerminalID[22] = "";
int nFeeUserType = 0;
char *sDestTermIDs = NULL;
char *sContent = NULL;
// get argument
for(n=1; n<argc; ++n)
{
if(strcmp(argv[n], "-B") == 0)
{
nSendMode = SEND_MODE_BATCH;
}
else if(strcmp(argv[n], "-S") == 0)
{
nSendMode = SEND_MODE_SINGLE;
}
else if(strcmp(argv[n], "-n") == 0)
{
nOutput = 0;
}
else if(strcmp(argv[n], "-a") == 0)
{
n++;
strcpy(sArgFile, argv[n]);
}
else if(strcmp(argv[n], "-u") == 0)
{
n++;
nFeeUserType = atoi(argv[n]);
}
else if(strcmp(argv[n], "-f") == 0)
{
n++;
strcpy(sFeeTerminalID, argv[n]);
}
else if(strcmp(argv[n], "-d") == 0)
{
n++;
sDestTermIDs = argv[n];
}
else if(strcmp(argv[n], "-m") == 0)
{
n++;
sContent = argv[n];
}
else
{
if(nOutput == 1)
{
Usage(argv[0]);
}
exit(1);
}
}
if(strlen(sArgFile) == 0)
{
if(nOutput == 1)
{
printf("No argument file!\n");
}
exit(1);
}
// read arguments from file
if(ReadArgumentFile(sArgFile, &theSMArgument, nOutput) != 0)
{
if(nOutput == 1)
{
printf("Fail to read argument from file %s!\n", sArgFile);
}
exit(1);
}
else
{
if(nOutput == 1)
{
printf("need_reply=%d!\n", theSMArgument.nNeedReply);
printf("msg_level=%d!\n", theSMArgument.nMsgLevel);
printf("serviceid=%s!\n", theSMArgument.sServiceID);
printf("msg_format=%d!\n", theSMArgument.nMsgFormat);
printf("fee_type=%s!\n", theSMArgument.sFeeType);
printf("fee_code=%s!\n", theSMArgument.sFeeCode);
printf("valid_time=%s!\n", theSMArgument.sValidTime);
printf("at_time=%s!\n", theSMArgument.sAtTime);
printf("src_termid=%s!\n", theSMArgument.sSrcTermID);
printf("dest_termid=%s!\n", theSMArgument.sDestTermID);
printf("dest_termid_file=%s!\n", theSMArgument.sDestTermIDFile);
printf("msg_file=%s!\n", theSMArgument.sMsgFile);
printf("msgid_file=%s!\n", theSMArgument.sMsgIDFile);
}
}
// initiate API
if(InitCMPPAPI() != 0)
{
if(nOutput == 1)
{
printf("Fail to call InitCMPPAPI!\n");
}
exit(1);
}
if(nSendMode == SEND_MODE_SINGLE)
{
// get message from file
struct stat sbuf;
if(stat(theSMArgument.sMsgFile, &sbuf) != 0 )
{
nMsgLen = -1;
}
else
{
nMsgLen = sbuf.st_size;
}
if(nMsgLen <= 0)
{
if(nOutput == 1)
{
printf("Fail to read message file %s!\n", theSMArgument.sMsgFile);
}
exit(1);
}
pFile = fopen(theSMArgument.sMsgFile, "rb");
fread(sMsgContent, nMsgLen, 1, pFile);
fclose(pFile);
// remove unseen character at the end of the sMsgContent, no binary format
if(theSMArgument.nMsgFormat != 4 && theSMArgument.nMsgFormat != 8)
{
sMsgContent[nMsgLen] = 0;
for(n=nMsgLen-1; n>0; --n)
{
if((unsigned char)sMsgContent[n] <= '\r')
{
sMsgContent[n] = 0;
}
else
{
break;
}
}
nMsgLen = strlen(sMsgContent);
}
if(nOutput == 1)
{
printf("msg_len=%d!\n", nMsgLen);
}
if(CMPPSendSingle(theSMArgument.nNeedReply, theSMArgument.nMsgLevel,
theSMArgument.sServiceID, theSMArgument.nMsgFormat,
theSMArgument.sFeeType, theSMArgument.sFeeCode,
theSMArgument.sValidTime, theSMArgument.sAtTime,
theSMArgument.sSrcTermID, theSMArgument.sDestTermID,
nMsgLen, sMsgContent,
sMsgID, &nErrorCode,
(char)nFeeUserType, sFeeTerminalID, 0, 0) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPSendSingle, error=%d!\n", nErrorCode);
}
exit(1);
}
// write msgid and error code to file
else
{
pFile = fopen(theSMArgument.sMsgIDFile, "w");
if(pFile != NULL)
{
fprintf(pFile, "%s\t%d\n", sMsgID, nErrorCode);
fclose(pFile);
}
else
{
if(nOutput == 1)
{
printf("Fail to open file %s!\n", theSMArgument.sMsgIDFile);
}
}
}
}
else
{
if(sDestTermIDs != NULL && sContent != NULL)
{
if(CMPPSendBatch2(theSMArgument.nNeedReply, theSMArgument.nMsgLevel,
theSMArgument.sServiceID, theSMArgument.nMsgFormat,
theSMArgument.sFeeType, theSMArgument.sFeeCode,
theSMArgument.sValidTime, theSMArgument.sAtTime,
theSMArgument.sSrcTermID, sDestTermIDs,
strlen(sContent), sContent, theSMArgument.sMsgIDFile,
(char)nFeeUserType, sFeeTerminalID, 0, 0) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPSendBatch!\n");
}
exit(1);
}
}
else
{
if(CMPPSendBatch(theSMArgument.nNeedReply, theSMArgument.nMsgLevel,
theSMArgument.sServiceID, theSMArgument.nMsgFormat,
theSMArgument.sFeeType, theSMArgument.sFeeCode,
theSMArgument.sValidTime, theSMArgument.sAtTime,
theSMArgument.sSrcTermID, theSMArgument.sDestTermIDFile,
theSMArgument.sMsgFile, theSMArgument.sMsgIDFile,
(char)nFeeUserType, sFeeTerminalID, 0, 0) != 0)
{
if(nOutput == 1)
{
printf("Fail to call CMPPSendBatch!\n");
}
exit(1);
}
}
}
exit(0);
return(0);
}
void Usage(char *sProgram)
{
printf("Usage: %s [-B]|[-S][-n] <-a argument_file> [-u fee_user_type] [-f fee_term_id] [-d dest_term_ids] [-m content]\n", sProgram);
printf(" -B: send one short message to multiple users\n");
printf(" -S: send one short message to single user\n");
printf(" -a: argument file\n");
printf(" -n: no message output\n");
printf(" -u: fee_user_type, default is 0\n");
printf(" -f: fee_terminal_id, default is \"\"\n");
printf(" -d: dest_terminal_ids, e.g. \"13922000001,13922000002\"\n");
printf(" -m: message content\n");
printf("e.g.: %s -S -a sm.arg # send single short message defined in file sm.arg\n", sProgram);
printf("e.g.: %s -B -a sm.arg # send batch short message defined in file sm.arg\n", sProgram);
printf("e.g.: %s -B -a sm.arg -d \"13922000001,13922000002\" -m \"test\" # send batch short message defined in file sm.arg\n", sProgram);
}
int ReadArgumentFile(char *sFile, SMArgument *pSMArgument, int nOutput)
{
FILE *pFile;
char sLine[101];
int n, nLen, m;
pFile = fopen(sFile, "r");
if(pFile == NULL)
return(1);
for(n=0; n<13; ++n)
{
if(fgets(sLine, 100, pFile) == NULL)
{
if(nOutput == 1)
{
printf("Fail to read no.%d parameter in file %s!\n", n+1, sFile);
}
fclose(pFile);
return(1);
}
// remove '\n' and '\r'
nLen = strlen(sLine);
for(m=nLen-1; m>=0; --m)
{
if(sLine[m] == '\n' || sLine[m] == '\r')
{
sLine[m] = 0;
}
break;
}
switch(n)
{
case 0:
pSMArgument->nNeedReply = atoi(sLine);
break;
case 1:
pSMArgument->nMsgLevel = atoi(sLine);
break;
case 2:
if(strlen(sLine) > 10)
sLine[10] = 0;
strcpy(pSMArgument->sServiceID, sLine);
break;
case 3:
pSMArgument->nMsgFormat = atoi(sLine);
break;
case 4:
if(strlen(sLine) > 2)
sLine[2] = 0;
strcpy(pSMArgument->sFeeType, sLine);
break;
case 5:
if(strlen(sLine) > 6)
sLine[6] = 0;
strcpy(pSMArgument->sFeeCode, sLine);
break;
case 6:
if(strlen(sLine) > 17)
sLine[17] = 0;
strcpy(pSMArgument->sValidTime, sLine);
break;
case 7:
if(strlen(sLine) > 17)
sLine[17] = 0;
strcpy(pSMArgument->sAtTime, sLine);
break;
case 8:
if(strlen(sLine) > 21)
sLine[21] = 0;
strcpy(pSMArgument->sSrcTermID, sLine);
break;
case 9:
if(strlen(sLine) > 21)
sLine[21] = 0;
strcpy(pSMArgument->sDestTermID, sLine);
break;
case 10:
if(strlen(sLine) > 100)
sLine[100] = 0;
strcpy(pSMArgument->sDestTermIDFile, sLine);
break;
case 11:
if(strlen(sLine) > 100)
sLine[100] = 0;
strcpy(pSMArgument->sMsgFile, sLine);
break;
case 12:
if(strlen(sLine) > 100)
sLine[100] = 0;
strcpy(pSMArgument->sMsgIDFile, sLine);
break;
}
}
fclose(pFile);
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -