Commit 8835171a authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- add package rate test code

parent bca81abb
......@@ -103,6 +103,9 @@
9BB10CF71D551ACC00A038FD /* PackageRateScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BB10CF61D551ACC00A038FD /* PackageRateScene.m */; };
9BB10CF81D551ACC00A038FD /* PackageRateScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BB10CF61D551ACC00A038FD /* PackageRateScene.m */; };
9BB10CF91D551ACC00A038FD /* PackageRateScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BB10CF61D551ACC00A038FD /* PackageRateScene.m */; };
9BD5F5791D58FEB600B1694D /* PingInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BD5F5781D58FEB600B1694D /* PingInfo.m */; };
9BD5F57A1D58FEB600B1694D /* PingInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BD5F5781D58FEB600B1694D /* PingInfo.m */; };
9BD5F57B1D58FEB600B1694D /* PingInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BD5F5781D58FEB600B1694D /* PingInfo.m */; };
B71F5911188484E7003F4D28 /* iPad-old.png in Resources */ = {isa = PBXBuildFile; fileRef = B71F5907188484E7003F4D28 /* iPad-old.png */; };
B71F5912188484E7003F4D28 /* iPad-old@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B71F5908188484E7003F4D28 /* iPad-old@2x.png */; };
B71F5913188484E7003F4D28 /* iPad.png in Resources */ = {isa = PBXBuildFile; fileRef = B71F5909188484E7003F4D28 /* iPad.png */; };
......@@ -280,6 +283,8 @@
9BB10CEF1D551AB700A038FD /* PackageSizeScene.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PackageSizeScene.m; path = Source/PackageSizeScene.m; sourceTree = "<group>"; };
9BB10CF51D551ACC00A038FD /* PackageRateScene.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PackageRateScene.h; path = Source/PackageRateScene.h; sourceTree = "<group>"; };
9BB10CF61D551ACC00A038FD /* PackageRateScene.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PackageRateScene.m; path = Source/PackageRateScene.m; sourceTree = "<group>"; };
9BD5F5771D58FEB600B1694D /* PingInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PingInfo.h; path = Source/PingInfo.h; sourceTree = "<group>"; };
9BD5F5781D58FEB600B1694D /* PingInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PingInfo.m; path = Source/PingInfo.m; sourceTree = "<group>"; };
B71F5907188484E7003F4D28 /* iPad-old.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "iPad-old.png"; path = "Icons/iPad-old.png"; sourceTree = "<group>"; };
B71F5908188484E7003F4D28 /* iPad-old@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "iPad-old@2x.png"; path = "Icons/iPad-old@2x.png"; sourceTree = "<group>"; };
B71F5909188484E7003F4D28 /* iPad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = iPad.png; path = Icons/iPad.png; sourceTree = "<group>"; };
......@@ -628,6 +633,8 @@
9BB10CEF1D551AB700A038FD /* PackageSizeScene.m */,
9BB10CF51D551ACC00A038FD /* PackageRateScene.h */,
9BB10CF61D551ACC00A038FD /* PackageRateScene.m */,
9BD5F5771D58FEB600B1694D /* PingInfo.h */,
9BD5F5781D58FEB600B1694D /* PingInfo.m */,
);
name = Scenes;
sourceTree = "<group>";
......@@ -1035,6 +1042,7 @@
7A59498E19E3941300F65F90 /* AppDelegate.m in Sources */,
9B2430801D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */,
9B6B1ED81D1895860008B45D /* SettingScene.m in Sources */,
9BD5F57B1D58FEB600B1694D /* PingInfo.m in Sources */,
9B1F83CF1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m in Sources */,
9B2A23631D2428E800C6EEC5 /* ChatRoomScene.m in Sources */,
9B1F83DE1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */,
......@@ -1059,6 +1067,7 @@
9B1F83DD1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */,
9B28F0271D219148008B35B3 /* NCMCAlertView.m in Sources */,
9B1F83CE1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m in Sources */,
9BD5F57A1D58FEB600B1694D /* PingInfo.m in Sources */,
927F619E196C771E000F43EF /* LobbyScene.m in Sources */,
5B121030197484FA004C1E1D /* NCMultipeerConnectivityActivity.m in Sources */,
);
......@@ -1081,6 +1090,7 @@
9B1F83CD1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m in Sources */,
9B24307E1D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */,
9B1F83C81D19AEEF00837DA1 /* NCMCPeripheralService.m in Sources */,
9BD5F5791D58FEB600B1694D /* PingInfo.m in Sources */,
9B3EC30E1D525C2C00EF4C0E /* Messages.pbobjc.m in Sources */,
5B121038197487F2004C1E1D /* AppDelegate.m in Sources */,
9B1F83C61D19AEEF00837DA1 /* NCMCCentralService.m in Sources */,
......
......@@ -7,7 +7,345 @@
//
#import "PackageRateScene.h"
#import "MultiplayerController.h"
#import "Parameters.h"
#import "NetworkLogger.h"
#import "Messages.pbobjc.h"
#import "PingInfo.h"
@implementation PackageRateScene
{
CCButton* btnPRStart;
CCLabelTTF* lbprNetworkStatus;
CCLabelTTF* lbprIsHost;
CCLabelTTF* lbprPackageSize;
CCLabelTTF* lbprCurrentPing;
CCLabelTTF* lbprReceviedCount;
CCLabelTTF* lbprTotalCount;
CCLabelTTF* lbprPackageRate;
CCLabelTTF* lbprBandwidth;
BOOL isPing;
BOOL isPingEnabled;
NetworkLogger *myLog;
NSMutableDictionary *pingDict;
unsigned int totalCount;
BOOL isLogEnabled;
int messageSize;
double lastServerBroadcastTime;
int packageRate;
unsigned int receivedCount;
}
-(void)onEnter
{
[super onEnter];
NSUInteger peerCount = [[[[MultiplayerController instance] currentSession] getConnectedPeers] count];
lbprNetworkStatus.string = [NSString stringWithFormat:@"%lu", (unsigned long)peerCount];
lbprIsHost.string = [[MultiplayerController instance]isHost] ? @"YES" : @"NO";
isPing = NO;
isPingEnabled = NO;
btnPRStart.title = @"Start";
messageSize = 0;
isLogEnabled = NO;
packageRate = 0;
lbprPackageSize.string = [NSString stringWithFormat:@"%d", [self getPackageSize]];
lbprPackageRate.string = [NSString stringWithFormat:@"%d", packageRate];
lbprBandwidth.string = [NSString stringWithFormat:@"%f", [self getBandwidth]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleReceivedMessageWithNotification:)
name:RECEIVED_MESSAGE_NOTIFICATION
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatePlayerlistWithNotification:)
name:UPDATE_PLAYERLIST_NOTIFICATION
object:nil];
}
-(void)onExit
{
[super onExit];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)handleUpdatePlayerlistWithNotification:(NSNotification *)notification
{
NSUInteger peerCount = [[[[MultiplayerController instance] currentSession] getConnectedPeers] count];
lbprNetworkStatus.string = [NSString stringWithFormat:@"%lu", (unsigned long)peerCount];
}
- (void)handleReceivedMessageWithNotification:(NSNotification *)notification
{
NSData* msgData = [[notification userInfo] objectForKey:@"data"];
//NSString* name = [[notification userInfo] objectForKey:@"name"];
PingMessage *message = [[PingMessage alloc] initWithData:msgData error:nil];
if (message == nil) {
CCLOG(@"Invalid data received!!!");
return;
}
if (message.messageType == PingMessage_MsgType_Response) {
NSString *token = [NSString stringWithFormat:@"%u", (message.token)];
PingInfo *info = pingDict[token];
if (info == nil) {
CCLOG(@"Invalid ping token received!!!");
return;
} else if(info.totalCount == info.currentCount) {
CCLOG(@"Token over received!!!");
return;
}
CFTimeInterval receiveTime = [[[notification userInfo] objectForKey:@"time"] doubleValue];
CFTimeInterval timeInterval = receiveTime - info.startTime - message.responseTime;
NSNumber *numTime = [[NSNumber alloc] initWithDouble:timeInterval];
[info.timeIntervals addObject:numTime];
info.currentCount += 1;
receivedCount += 1;
if (isPing) {
lbprPackageSize.string = [NSString stringWithFormat:@"%d", [self getPackageSize]];
lbprCurrentPing.string = [NSString stringWithFormat:@"%f", timeInterval];
lbprReceviedCount.string = [NSString stringWithFormat:@"%d", receivedCount];
lbprTotalCount.string = [NSString stringWithFormat:@"%d", totalCount];
lbprPackageRate.string = [NSString stringWithFormat:@"%d", packageRate];
lbprBandwidth.string = [NSString stringWithFormat:@"%f", [self getBandwidth]];
if (info.totalCount == info.currentCount) {
if (totalCount >= MaxPingCount && receivedCount >= MaxPingCount) {
isPingEnabled = NO;
[self calculateResult];
}
}
}
} else if (message.messageType == PingMessage_MsgType_Ping){
PingMessage *packet = [[PingMessage alloc]init];
packet.messageType = PingMessage_MsgType_Response;
packet.token = message.token;
packet.isReliable = message.isReliable;
packet.message = @"";
CFTimeInterval receiveTime = [[[notification userInfo] objectForKey:@"time"] doubleValue];
NSTimeInterval t2 = CACurrentMediaTime() * 1000; // s to ms
packet.responseTime = t2 - receiveTime;
NSData *sendData = [packet data];
NCMCSessionSendDataMode mode = message.isReliable ? NCMCSessionSendDataReliable : NCMCSessionSendDataUnreliable;
[[MultiplayerController instance] sendData:sendData toAllwithMode:mode];
CCLOG(@"send response to %@ with token : %u, length : %lu and local response time : %f", [[notification userInfo] objectForKey:@"peerName"], message.token, (unsigned long)sendData.length, packet.responseTime);
}
}
-(int)getPackageSize
{
int size = messageSize + 10;
if (messageSize >= 128) {
size += 1;
}
if (messageSize >= 502) {
size += 2;
}
return size;
}
-(BOOL)shouldBroadcast
{
BOOL ret = NO;
double currentTime = CACurrentMediaTime();
if(lastServerBroadcastTime == 0) {
// first time broadcast.
lastServerBroadcastTime = currentTime;
}
float broadcastInterval = 1.0f/packageRate;
float broadcastTimeElapsed = (currentTime - lastServerBroadcastTime);
//
if(broadcastTimeElapsed > broadcastInterval) {
lastServerBroadcastTime = currentTime;
ret = YES;
}
return ret;
}
-(float)getBandwidth
{
int packageSize = [self getPackageSize];
return packageRate * packageSize;
}
-(void)startLog
{
if (myLog == nil) {
myLog = [[NetworkLogger alloc]init];
}
[myLog newLogFile];
}
-(void)writeLog:(NSString *)log
{
if (myLog != nil) {
[myLog write:log];
}
}
-(void)onBtnPRStart
{
if (isPing) {
[self stopPing];
} else {
[self startPing];
}
}
-(void)startPing
{
isPing = YES;
btnPRStart.title = @"Stop";
if (pingDict == nil) {
pingDict = [[NSMutableDictionary alloc]init];
} else {
[pingDict removeAllObjects ];
}
totalCount = 0;
receivedCount = 0;
messageSize = 1;
if (isLogEnabled) {
[self startLog];
}
lastServerBroadcastTime = 0;
packageRate = 1;
isPingEnabled = YES;
}
-(void)stopPing
{
isPing = NO;
isPingEnabled = NO;
btnPRStart.title = @"Start";
}
-(void)update:(CCTime)delta
{
//CCLOG(@"update time %f", delta);
if (isPing && isPingEnabled && [self shouldBroadcast]) {
[self doPing];
}
}
-(void)doPing
{
PingMessage* bufMsg = [[PingMessage alloc] init];
NSMutableString *message = [[NSMutableString alloc]initWithCapacity:messageSize];
for (int i=0; i<messageSize; ++i) {
[message appendString:@"a"];
}
int token = totalCount + 1;
bufMsg.message = message;
bufMsg.token = token;
bufMsg.messageType = PingMessage_MsgType_Ping;
bufMsg.responseTime = 0.0;
bufMsg.isReliable = NO;
NSData* msg = [bufMsg data];
CFTimeInterval startTime = CACurrentMediaTime() * 1000;
CCLOG(@"DoPing : messageSize: %d, totalSize: %lu, packageSize: %d", messageSize, (unsigned long)[msg length], [self getPackageSize]);
[[MultiplayerController instance] sendData:msg toAllwithMode:NCMCSessionSendDataReliable];
PingInfo *info = [[PingInfo alloc]init];
info.startTime = startTime;
info.token = token;
info.totalCount = [[[[MultiplayerController instance] currentSession] getConnectedPeers] count];
info.currentCount = 0;
info.number = totalCount + 1;
totalCount += info.totalCount;
info.timeIntervals = [[NSMutableArray alloc]initWithCapacity:info.totalCount];
NSString *t = [NSString stringWithFormat:@"%d", token];
[pingDict setValue:info forKey:t];
if (totalCount >= MaxPingCount) {
isPingEnabled = NO;
}
}
- (NSNumber *)standardDeviationOf:(NSArray *)array mean:(double)mean
{
if(![array count]) return nil;
double sumOfSquaredDifferences = 0.0;
for(NSNumber *number in array)
{
double valueOfNumber = [number doubleValue];
double difference = valueOfNumber - mean;
sumOfSquaredDifferences += difference * difference;
}
return [NSNumber numberWithDouble:sqrt(sumOfSquaredDifferences / [array count])];
}
- (void)calculateResult
{
NSMutableArray *allTimes = [[NSMutableArray alloc]init];
for (id key in pingDict) {
PingInfo *info = pingDict[key];
for(NSNumber *num in info.timeIntervals) {
[allTimes addObject:num];
}
}
NSNumber *average = [allTimes valueForKeyPath:@"@avg.self"];
NSNumber *std = [self standardDeviationOf:allTimes mean:[average doubleValue]];
if (isLogEnabled) {
// log (packSize, packageRate, bandwidth, client count, avgPing, sd)
NSUInteger peerCount = [[[[MultiplayerController instance] currentSession] getConnectedPeers] count];
int realsize = [self getPackageSize];
NSString *log = [[NSString alloc]initWithFormat:@"%d, %d, %.8f, %lu, %.8f, %.8f\n", realsize, packageRate, [self getBandwidth], (unsigned long)peerCount, [average doubleValue], [std doubleValue]];
[self writeLog:log];
}
if (isPing) {
[pingDict removeAllObjects];
receivedCount = 0;
totalCount = 0;
packageRate += 1;
isPingEnabled = YES;
}
}
@end
......@@ -11,26 +11,7 @@
#import "Parameters.h"
#import "NetworkLogger.h"
#import "Messages.pbobjc.h"
@interface PingInfo : NSObject
@property(assign, nonatomic)int token;
@property(assign, nonatomic)CFTimeInterval startTime;
@property(strong, nonatomic)NSMutableArray *timeIntervals;
@property(assign, nonatomic)unsigned long totalCount;
@property(assign, nonatomic)unsigned long currentCount;
@property(assign, nonatomic)unsigned long number;
@end
@implementation PingInfo
@synthesize token;
@synthesize startTime;
@synthesize timeIntervals;
@synthesize totalCount;
@synthesize currentCount;
@synthesize number;
@end
#import "PingInfo.h"
......
//
// PingInfo.h
// NCMultipeerConnectivity
//
// Created by Chengzhao Li on 2016-08-08.
// Copyright © 2016 Apportable. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface PingInfo : NSObject
@property(assign, nonatomic)int token;
@property(assign, nonatomic)CFTimeInterval startTime;
@property(strong, nonatomic)NSMutableArray *timeIntervals;
@property(assign, nonatomic)unsigned long totalCount;
@property(assign, nonatomic)unsigned long currentCount;
@property(assign, nonatomic)unsigned long number;
@end
//
// PingInfo.m
// NCMultipeerConnectivity
//
// Created by Chengzhao Li on 2016-08-08.
// Copyright © 2016 Apportable. All rights reserved.
//
#import "PingInfo.h"
@implementation PingInfo
@synthesize token;
@synthesize startTime;
@synthesize timeIntervals;
@synthesize totalCount;
@synthesize currentCount;
@synthesize number;
@end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment