Commit cc528b8a authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- pack size test code

parent 7ca54706
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nclab.ncmultipeerchat">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:allowBackup="true"
......@@ -12,7 +12,8 @@
android:screenOrientation="landscape"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity"
<activity
android:name=".LoginActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -29,6 +30,8 @@
<activity
android:name=".ChatActivity"
android:screenOrientation="landscape" />
<activity android:name=".PackageSizeActivity"
android:screenOrientation="landscape" />
</application>
</manifest>
\ No newline at end of file
......@@ -36,9 +36,11 @@ public class MultiplayerController {
public final static String BLE_BROADCAST_SCAN_TIMEOUT = "ncchat.nclab.com.BLE_BROADCAST_SCAN_TIMEOUT";
public final static String BLE_BROADCAST_START_FAILED = "ncchat.nclab.com.BLE_BROADCAST_START_FAILED";
public final static String BLE_BROADCAST_UPDATE_PLAYERLIST = "ncchat.nclab.com.BLE_BROADCAST_UPDATE_PLAYERLIST_";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE_DATA = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_DATA";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE_TIME = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_TIME";
private NCMCSession currentSession;
private NCMCCentralService currentCentralService;
......@@ -320,6 +322,37 @@ public class MultiplayerController {
}
}
public void sendDataToAllPeer(byte[] msgData, int mode) {
for (NCMCPeerID peerID : this.currentSessionPlayerIDs) {
if (!peerID.getDisplayName().equalsIgnoreCase(this.currentSession.myPeerID.getDisplayName()))
{
Log.d(TAG, "sendDataToPeer: " + peerID.getDisplayName() + " msgData length: " + msgData.length);
byte[] data = packMessageWithType(MSG_CHAT_MSG, msgData);
List<NCMCPeerID> peerIDs = new LinkedList<>();
peerIDs.add(peerID);
this.currentSession.sendData(data, peerIDs, mode);
}
}
}
public void enableHighTraffic() {
for (NCMCPeerID peerID : this.currentSessionPlayerIDs) {
if (!peerID.getDisplayName().equalsIgnoreCase(this.currentSession.myPeerID.getDisplayName()))
{
this.currentSession.enableHighTraffic(peerID);
}
}
}
public void disableHighTraffic() {
for (NCMCPeerID peerID : this.currentSessionPlayerIDs) {
if (!peerID.getDisplayName().equalsIgnoreCase(this.currentSession.myPeerID.getDisplayName()))
{
this.currentSession.disableHighTraffic(peerID);
}
}
}
public void gotoChatRoom() {
if (this.currentCentralService != null) {
this.currentCentralService.stopBrowsingForPeers();
......@@ -391,6 +424,7 @@ public class MultiplayerController {
Intent i = new Intent(BLE_BROADCAST_RECEIVE_MESSAGE);
i.putExtra(BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME, stringForMCPeerDisplayName(fromPeer.getDisplayName()));
i.putExtra(BLE_BROADCAST_RECEIVE_MESSAGE_DATA, data);
i.putExtra(BLE_BROADCAST_RECEIVE_MESSAGE_TIME, System.nanoTime()); // 1 millisecond = 1000000 nanosecond
broadcastStatus(i);
break;
}
......
package com.nclab.ncmultipeerchat;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.test.suitebuilder.TestMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.nclab.ncmultipeerconnectivity.NCMCSession;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
public class PackageSizeActivity extends Activity {
public static final String TAG = "PackageSizeActivity";
public static final int MaxPingCount = 2;
private Button m_pintBtn;
private TextView m_txtCurrentPing;
private TextView m_txtReceivedCount;
private TextView m_txtTotalCount;
private boolean m_isPing;
private boolean m_isPingEnabled;
private boolean m_isLogEnabled = false;
private int m_messageSize;
private int m_totalCount;
private int m_receivedCount;
private Hashtable<Integer, PingInfo> m_pingDict = null;
private NetworkLogger m_logger;
private final IntentFilter m_intentFilter = new IntentFilter();
private BroadcastReceiver m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE: {
Bundle bundle = intent.getExtras();
if (bundle.containsKey(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME) &&
bundle.containsKey(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_DATA) &&
bundle.containsKey(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_TIME)) {
String fromName = bundle.getString(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME);
long receiveTime = bundle.getLong(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_TIME);
byte[] data = bundle.getByteArray(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_DATA);
handleMessage(data, fromName, receiveTime);
}
break;
}
case MultiplayerController.BLE_BROADCAST_UPDATE_PLAYERLIST: {
updateStatus();
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package_size);
String isHost = MultiplayerController.getInstance().isHost() ? "Yes" : "No";
((TextView)findViewById(R.id.lbpsIsHost)).setText(isHost);
m_isPing = false;
m_pintBtn = (Button)findViewById(R.id.btnpsStart);
m_pintBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (m_isPing) {
m_isPing = false;
m_pintBtn.setText(getResources().getString(R.string.start));
stopPing();
} else {
m_isPing = true;
m_pintBtn.setText(getResources().getString(R.string.stop));
startPing();
}
}
});
m_messageSize = 0;
updatePackageSize();
m_txtCurrentPing = ((TextView)findViewById(R.id.lbpsCurrentPing));
m_txtReceivedCount = ((TextView)findViewById(R.id.lbpsReceivedCount));
m_txtTotalCount = ((TextView)findViewById(R.id.lbpsTotalCount));
updateStatus();
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE);
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_UPDATE_PLAYERLIST);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
backToSettingActivity();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onResume() {
super.onResume();
MultiplayerController.getInstance().setContext(this);
registerReceiver(m_broadcastReceiver, m_intentFilter);
updateStatus();
}
@Override
protected void onPause() {
super.onPause();
MultiplayerController.getInstance().setContext(null);
unregisterReceiver(m_broadcastReceiver);
}
private void updateStatus() {
int connectedCnt = MultiplayerController.getInstance().getCurrentSession().getConnectedPeers().size();
((TextView)findViewById(R.id.lbpsNetworkStatus)).setText(String.valueOf(connectedCnt));
}
private void updatePackageSize() {
((TextView)findViewById(R.id.lbpsPackageSize)).setText(String.valueOf(getPackageSize()));
}
private int getPackageSize() {
int size = m_messageSize + 10;
if (m_messageSize >= 128) {
size += 1;
}
if (m_messageSize >= 499) {
size += 2;
}
return size;
}
private void backToSettingActivity() {
MultiplayerController.getInstance().disconnect();
Intent intent = new Intent();
intent.setClass(PackageSizeActivity.this, SettingActivity.class);
PackageSizeActivity.this.startActivity(intent);
PackageSizeActivity.this.finish();
}
private void handleMessage(byte[] data, String from, long receiveTime) {
if (data != null && data.length != 0) {
try {
Message.PingMessage message = Message.PingMessage.parseFrom(data);
if (message.getMessageType() == Message.PingMessage.MsgType.RESPONSE) {
int token = message.getToken();
PingInfo info = m_pingDict.get(token);
if (info.m_totalCount == info.m_currentCount) {
Log.d(TAG, "handleMessage: token over received");
return;
}
double timeInterval = ((double)receiveTime) / 1000000.0 - ((double)info.m_startTime)/1000000.0 - message.getResponseTime();
Log.d(TAG, "handleMessage token : " + token + ", timeInterval : " + timeInterval);
info.m_timeIntervals.add(timeInterval);
info.m_currentCount += 1;
m_pingDict.put(token, info);
m_receivedCount += 1;
if (m_isPing) {
updatePackageSize();
m_txtCurrentPing.setText(String.valueOf(timeInterval));
m_txtReceivedCount.setText(String.valueOf(m_receivedCount));
m_txtTotalCount.setText(String.valueOf(m_totalCount));
if (info.m_totalCount == info.m_currentCount) {
if (m_totalCount >= MaxPingCount && m_receivedCount >= MaxPingCount) {
m_isPingEnabled = false;
calculateResult();
} else {
doPing();
}
}
}
} else if (message.getMessageType() == Message.PingMessage.MsgType.PING) {
Message.PingMessage.Builder mb = Message.PingMessage.newBuilder();
mb.setToken(message.getToken());
mb.setMessageType(Message.PingMessage.MsgType.RESPONSE);
mb.setIsReliable(message.getIsReliable());
mb.setMessage("");
double responseTime = ((double)System.nanoTime())/1000000.0 - ((double)receiveTime) / 1000000.0;
mb.setResponseTime(responseTime);
Message.PingMessage msg = mb.build();
Log.d(TAG, "handleMessage: send response with token : " + msg.getToken() + " response time : " + responseTime);
int mode = message.getIsReliable() ? NCMCSession.NCMCSessionSendDataReliable : NCMCSession.NCMCSessionSendDataUnreliable;
MultiplayerController.getInstance().sendDataToPeer(msg.toByteArray(), from, mode);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void startPing() {
m_isPing = true;
if (m_pingDict == null) {
m_pingDict = new Hashtable<>();
} else {
m_pingDict.clear();
}
m_totalCount = 0;
m_receivedCount = 0;
m_isPingEnabled = true;
m_messageSize = 1;
if (m_isLogEnabled) {
m_logger = new NetworkLogger(this, "");
}
MultiplayerController.getInstance().enableHighTraffic();
doPing();
}
private void stopPing() {
m_isPing = false;
m_isPingEnabled = false;
if (m_isLogEnabled && m_logger != null) {
m_logger.flush();
m_logger.close();
m_logger = null;
}
MultiplayerController.getInstance().disableHighTraffic();
}
private void doPing() {
Message.PingMessage.Builder mb = Message.PingMessage.newBuilder();
String tmp = "";
for (int i=0; i<m_messageSize; ++i) {
tmp += "a";
}
mb.setMessage(tmp);
mb.setToken(m_totalCount+1);
mb.setMessageType(Message.PingMessage.MsgType.PING);
mb.setResponseTime(0.0);
mb.setIsReliable(false);
Message.PingMessage msg = mb.build();
long startTime = System.nanoTime();
Log.d(TAG, "doPing: message size: " + m_messageSize + ", total size: " + msg.toByteArray().length);
MultiplayerController.getInstance().sendDataToAllPeer(msg.toByteArray(), NCMCSession.NCMCSessionSendDataUnreliable);
PingInfo info = new PingInfo();
info.m_startTime = startTime;
info.m_token = m_totalCount+1;
info.m_totalCount = MultiplayerController.getInstance().getCurrentSession().getConnectedPeers().size();
info.m_currentCount = 0;
info.m_number = m_totalCount + 1;
m_totalCount += info.m_totalCount;
m_pingDict.put(m_totalCount, info);
if (m_totalCount >= MaxPingCount) {
m_isPingEnabled = false;
}
}
private void calculateResult() {
double totalTime = 0.0;
double min = 10000.0;
double max = 0.0;
List<Double> allTimes = new ArrayList<>();
Enumeration<PingInfo> values = m_pingDict.elements();
while (values.hasMoreElements()) {
PingInfo info = values.nextElement();
for (Double time : info.m_timeIntervals) {
totalTime += time;
if (time > max) {
max = time;
}
if (time < min) {
min = time;
}
allTimes.add(time);
}
}
double average = totalTime / allTimes.size();
double sumOfSquaredDifferences = 0.0;
for (Double time : allTimes) {
double difference = time - average;
sumOfSquaredDifferences += difference * difference;
}
double std = Math.sqrt(sumOfSquaredDifferences / allTimes.size());
if (m_isLogEnabled && m_logger != null) {
m_logger.write(getPackageSize() + "," + average + "," + std, true);
}
if (m_isPing) {
m_pingDict.clear();
m_receivedCount = 0;
m_totalCount = 0;
m_messageSize += 1;
m_isPingEnabled = true;
doPing();
}
}
}
package com.nclab.ncmultipeerchat;
import java.util.ArrayList;
import java.util.List;
/**
* ping information
*/
public class PingInfo {
public int m_token;
public long m_startTime;
public List<Double> m_timeIntervals;
public int m_totalCount;
public int m_currentCount;
public int m_number;
PingInfo() {
m_timeIntervals = new ArrayList<>();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.nclab.ncmultipeerchat.PackageSizeActivity"
android:background="@android:color/holo_blue_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Status : "
android:id="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Is Host : "
android:id="@+id/textView5"
android:layout_below="@+id/textView4"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/btnpsStart"
android:layout_below="@+id/textView5"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Connected"
android:id="@+id/lbpsNetworkStatus"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Yes"
android:id="@+id/lbpsIsHost"
android:layout_toEndOf="@+id/textView5"
android:layout_above="@+id/btnpsStart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Package Size : "
android:id="@+id/textView6"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/lbpsPackageSize"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/textView6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Current Ping : "
android:id="@+id/textView7"
android:layout_below="@+id/textView6"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/lbpsCurrentPing"
android:layout_below="@+id/lbpsPackageSize"
android:layout_alignStart="@+id/lbpsPackageSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Received Count : "
android:id="@+id/textView8"
android:layout_below="@+id/textView7"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/lbpsReceivedCount"
android:layout_below="@+id/lbpsCurrentPing"
android:layout_toEndOf="@+id/textView8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total Count : "
android:id="@+id/textView9"
android:layout_below="@+id/textView8"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"