/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/
#include "MQTTPacket.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#if !defined(_WINDOWS)
#include <sys/time.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#define MAXHOSTNAMELEN 256
#define EAGAIN WSAEWOULDBLOCK
#define EINTR WSAEINTR
#define EINPROGRESS WSAEINPROGRESS
#define EWOULDBLOCK WSAEWOULDBLOCK
#define ENOTCONN WSAENOTCONN
#define ECONNRESET WSAECONNRESET
#endif
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
struct Options
{
char* connection; /**< connection to system under test. */
char** haconnections;
int hacount;
int verbose;
int test_no;
} options =
{
"tcp://m2m.eclipse.org:1883",
NULL,
0,
0,
0,
};
void usage()
{
}
void getopts(int argc, char** argv)
{
int count = 1;
while (count < argc)
{
if (strcmp(argv[count], "--test_no") == 0)
{
if (++count < argc)
options.test_no = atoi(argv[count]);
else
usage();
}
else if (strcmp(argv[count], "--connection") == 0)
{
if (++count < argc)
{
options.connection = argv[count];
printf("\nSetting connection to %s\n", options.connection);
}
else
usage();
}
else if (strcmp(argv[count], "--haconnections") == 0)
{
if (++count < argc)
{
char* tok = strtok(argv[count], " ");
options.hacount = 0;
options.haconnections = malloc(sizeof(char*) * 5);
while (tok)
{
options.haconnections[options.hacount] = malloc(strlen(tok) + 1);
strcpy(options.haconnections[options.hacount], tok);
options.hacount++;
tok = strtok(NULL, " ");
}
}
else
usage();
}
else if (strcmp(argv[count], "--verbose") == 0)
{
options.verbose = 1;
printf("\nSetting verbose on\n");
}
count++;
}
}
#define LOGA_DEBUG 0
#define LOGA_INFO 1
#include <stdarg.h>
#include <time.h>
#include <sys/timeb.h>
void MyLog(int LOGA_level, char* format, ...)
{
static char msg_buf[256];
va_list args;
struct timeb ts;
struct tm *timeinfo;
if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
return;
ftime(&ts);
timeinfo = localtime(&ts.time);
strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo);
sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
va_start(args, format);
vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
va_end(args);
printf("%s\n", msg_buf);
fflush(stdout);
}
#if defined(WIN32) || defined(_WINDOWS)
#define mqsleep(A) Sleep(1000*A)
#define START_TIME_TYPE DWORD
static DWORD start_time = 0;
START_TIME_TYPE start_clock(void)
{
return GetTickCount();
}
#elif defined(AIX)
#define mqsleep sleep
#define START_TIME_TYPE struct timespec
START_TIME_TYPE start_clock(void)
{
static struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
return start;
}
#else
#define mqsleep sleep
#define START_TIME_TYPE struct timeval
/* TODO - unused - remove? static struct timeval start_time; */
START_TIME_TYPE start_clock(void)
{
struct timeval start_time;
gettimeofday(&start_time, NULL);
return start_time;
}
#endif
#if defined(WIN32)
long elapsed(START_TIME_TYPE start_time)
{
return GetTickCount() - start_time;
}
#elif defined(AIX)
#define assert(a)
long elapsed(struct timespec start)
{
struct timespec now, res;
clock_gettime(CLOCK_REALTIME, &now);
ntimersub(now, start, res);
return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
}
#else
long elapsed(START_TIME_TYPE start_time)
{
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&now, &start_time, &res);
return (res.tv_sec)*1000 + (res.tv_usec)/1000;
}
#endif
#define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
#define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
int tests = 0;
int failures = 0;
FILE* xml;
START_TIME_TYPE global_start_time;
char output[3000];
char* cur_output = output;
void write_test_result()
{
long duration = elapsed(global_start_time);
fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
if (cur_output != output)
{
fprintf(xml, "%s", output);
cur_output = output;
}
fprintf(xml, "</testcase>\n");
}
void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
{
++tests;
if (!value)
{
va_list args;
++failures;
printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
va_start(args, format);
vprintf(format, args);
va_end(args);
cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
description, filename, lineno);
}
else
MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
}
#define min(a, b) ((a < b) ? a : b)
int checkMQTTStrings(MQTTString a, MQTTString b)
{
if (!a.lenstring.data)
{
a.lenstring.data = a.cstring;
if (a.cstring)
a.lenstring.len = strlen(a.cstring);
}
if (!b.lenstring.data)
{
b.lenstring.data = b.cstring;
if (b.cstring)
b.lenstring.len = strlen(b.cstring);
}
return memcmp(a.lenstring.data, b.lenstring.data, min(a.lenstring.len, b.lenstring.len)) == 0;
}
int checkConnectPackets(MQTTPacket_connectData* before, MQTTPacket_connectData* after)
{
int rc = 0;
int start_failures = failures;
assert("struct_ids should be the same",
memcmp(before->struct_id, after->struct_id, 4) == 0, "struct_ids were different %.4s\n", after->struct_id);
assert("struct_versions should be the same",
before->struct_version == after->struct_version, "struct_versions were different\n", rc);
assert("MQTT versions should be the same",
before->MQTTVersion == after->MQTTVersion, "MQTT versions were different\n", rc);
assert("ClientIDs should be the same",
checkMQTTStrings(before->clientID, after->clientID), "ClientIDs were different\n", rc);
assert("keepAliveIntervals should be the same",
before->keepAliveInterval == after->keepAliveInterval, "keepAliveIntervals were different %d\n", after->keepAliveInterval);
assert("cleansessions should be the same",
before->cleansession == after->cleansession, "cleansessions were different\n", rc);
assert("willFlags should be the same",
before->willFlag == after->willFlag, "willFlags were different\n", rc);
if (before->willFlag)
{
assert("will struct_ids should be the same",
memcmp(before->will.struct_id, after->will.struct_id, 4) == 0, "will struct_ids were different %.4s\n", after->struct_id);
assert("will struct_versions should be the same",
before->will.struct_version == after->will.struct_version, "will struct_versions were different\n", rc);
assert("topic names should be the same",
checkMQTTStrings(before->will.topicName, after->will.topicName), "topic names were different\n", rc);
assert("messages should be the same",
checkMQTTStrings(before->will.message, after->will.message), "messages were different\n", rc);
assert("retained flags should be the same
washinglee
- 粉丝: 3
- 资源: 8
最新资源
- 毕设和企业适用springboot人工智能客服系统类及大数据云平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能客服系统类及环保监控平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能客服系统类及电子商务优化平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车电商类及直播流媒体平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车电商类及智能图像识别系统源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及AI语音识别平台源码+论文+视频.zip
- 毕设和企业适用springboot区域电商平台类及产品体验管理系统源码+论文+视频.zip
- 毕设和企业适用springboot区域电商平台类及仓储管理平台源码+论文+视频.zip
- 毕设和企业适用springboot区块链交易平台类及自动化测试平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能客服系统类及健身管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能客服系统类及教学资源共享平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能客服系统类及教育资源共享平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及电力系统优化平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及机器人平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及IT资产管理平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及机器学习平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈