Commit 6df28e26 authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- fix package rate

- update version
- fix concurrent write issue in manager
parent ee25ad83
......@@ -5,7 +5,7 @@
<GradleProjectSettings>
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.10" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.14.1" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
......@@ -13,13 +13,7 @@
<option value="$PROJECT_DIR$/ncmultipeerconnectivity" />
</set>
</option>
<option name="myModules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
<option value="$PROJECT_DIR$/ncmultipeerconnectivity" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings>
</option>
</component>
......
......@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.nclab.ncmultipeerchat"
......
......@@ -224,7 +224,7 @@ public class LobbyActivity extends Activity {
private void goToChat() {
Intent intent = new Intent();
intent.setClass(LobbyActivity.this, ChatActivity.class);
intent.setClass(LobbyActivity.this, PackageRateActivity.class);
LobbyActivity.this.startActivity(intent);
LobbyActivity.this.finish();
}
......
......@@ -20,20 +20,23 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;
public class PackageRateActivity extends Activity {
public static final String TAG = "PackageRateActivity";
public static final int MaxPingCount = 30;
public static final int MaxPingCount = 1;
public static final int MessageSize = 496;
private Button m_pintBtn;
private TextView m_txtCurrentPing;
private TextView m_txtReceivedCount;
private TextView m_txtTotalCount;
private TextView m_txtToken;
private boolean m_isPing;
private boolean m_isPingEnabled;
private boolean m_isLogEnabled = false;
private boolean m_isLogEnabled = true;
private int m_messageSize;
private int m_packageRate; // how many package per second
private long m_lastServerBroadcastTime;
private int m_totalCount;
......@@ -98,8 +101,6 @@ public class PackageRateActivity extends Activity {
}
});
m_messageSize = 1;
updatePackageSize();
updatePackageRate();
updateBandwidth();
......@@ -107,6 +108,7 @@ public class PackageRateActivity extends Activity {
m_txtCurrentPing = ((TextView)findViewById(R.id.lbprCurrentPing));
m_txtReceivedCount = ((TextView)findViewById(R.id.lbprReceivedCount));
m_txtTotalCount = ((TextView)findViewById(R.id.lbprTotalCount));
m_txtToken = (TextView)findViewById(R.id.lbprToken);
updateStatus();
......@@ -150,12 +152,12 @@ public class PackageRateActivity extends Activity {
}
private int getPackageSize() {
int size = m_messageSize + 10;
if (m_messageSize >= 128) {
int size = MessageSize + 10;
if (MessageSize >= 128) {
size += 1;
}
if (m_messageSize >= 499) {
if (MessageSize >= 499) {
size += 2;
}
......@@ -207,7 +209,7 @@ public class PackageRateActivity extends Activity {
m_pingDict.put(token, info);
m_receivedCount += 1;
if (m_isPing) {
if (true) {
updatePackageSize();
updatePackageRate();
updateBandwidth();
......@@ -216,10 +218,13 @@ public class PackageRateActivity extends Activity {
m_txtTotalCount.setText(String.valueOf(m_totalCount));
if (info.m_totalCount == info.m_currentCount) {
m_txtToken.setText(String.valueOf(token));
calculateResultBytoken(token);
/*
if (m_totalCount >= MaxPingCount && m_receivedCount >= MaxPingCount) {
m_isPingEnabled = false;
calculateResult();
}
}*/
}
}
......@@ -258,8 +263,7 @@ public class PackageRateActivity extends Activity {
m_receivedCount = 0;
m_isPingEnabled = true;
m_messageSize = 1;
m_packageRate = 1;
m_packageRate = 60;
m_lastServerBroadcastTime = 0;
if (m_isLogEnabled) {
......@@ -309,11 +313,11 @@ public class PackageRateActivity extends Activity {
Message.PingMessage.Builder mb = Message.PingMessage.newBuilder();
String tmp = "";
for (int i=0; i<m_messageSize; ++i) {
for (int i=0; i<MessageSize; ++i) {
tmp += "a";
}
int token = m_totalCount+1;
int token = ++m_totalCount;//+1;
mb.setMessage(tmp);
......@@ -325,22 +329,23 @@ public class PackageRateActivity extends Activity {
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.NCMCSessionSendDataReliable);
Log.d(TAG, "doPing: message size: " + MessageSize + ", total size: " + msg.toByteArray().length);
MultiplayerController.getInstance().sendDataToAllPeer(msg.toByteArray(), NCMCSession.NCMCSessionSendDataUnreliable);
PingInfo info = new PingInfo();
info.m_startTime = startTime;
info.m_token = token;
info.m_totalCount = MultiplayerController.getInstance().getCurrentSession().getConnectedPeers().size();
info.m_currentCount = 0;
info.m_number = m_totalCount + 1;
m_totalCount += info.m_totalCount;
//info.m_number = m_totalCount + 1;
//m_totalCount += info.m_totalCount;
m_pingDict.put(token, info);
/*
if (m_totalCount >= MaxPingCount) {
m_isPingEnabled = false;
}
}*/
}
private void calculateResult() {
......@@ -409,4 +414,40 @@ public class PackageRateActivity extends Activity {
return rt;
}
private void calculateResultBytoken(int token) {
double totalTime = 0.0;
double min = 10000.0;
double max = 0.0;
List<Double> allTimes = new ArrayList<>();
PingInfo info = m_pingDict.get(token);
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) {
int connectedCnt = MultiplayerController.getInstance().getCurrentSession().getConnectedPeers().size();
//token, packagesize, packagerate, bandwidth, client count, ping, std
m_logger.write(token + "," + getPackageSize() + "," + m_packageRate + "," + getBandwidth() + "," + connectedCnt + "," + average + "," + std, true);
}
//m_pingDict.remove(token);
}
}
......@@ -31,7 +31,7 @@ public class PackageSizeActivity extends Activity {
private boolean m_isPing;
private boolean m_isPingEnabled;
private boolean m_isLogEnabled = false;
private boolean m_isLogEnabled = true;
private int m_messageSize;
private int m_totalCount;
......@@ -283,14 +283,14 @@ public class PackageSizeActivity extends Activity {
long startTime = System.nanoTime();
Log.d(TAG, "doPing: message size: " + m_messageSize + ", total size: " + msg.toByteArray().length + ", packageSize: " + getPackageSize());
MultiplayerController.getInstance().sendDataToAllPeer(msg.toByteArray(), NCMCSession.NCMCSessionSendDataReliable);
MultiplayerController.getInstance().sendDataToAllPeer(msg.toByteArray(), NCMCSession.NCMCSessionSendDataUnreliable);
PingInfo info = new PingInfo();
info.m_startTime = startTime;
info.m_token = token;
info.m_totalCount = MultiplayerController.getInstance().getCurrentSession().getConnectedPeers().size();
info.m_currentCount = 0;
info.m_number = m_totalCount + 1;
//info.m_number = m_totalCount + 1;
m_totalCount += info.m_totalCount;
m_pingDict.put(token, info);
......
......@@ -12,7 +12,7 @@ public class PingInfo {
public List<Double> m_timeIntervals;
public int m_totalCount;
public int m_currentCount;
public int m_number;
//public int m_number;
PingInfo() {
m_timeIntervals = new ArrayList<>();
......
......@@ -145,13 +145,22 @@
android:layout_toEndOf="@+id/lbprTotalCount" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/btnprStart"
android:text="Token : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:text="Bandwidth : "
android:id="@+id/textView10"
android:layout_below="@+id/textViewpackagerate"
android:layout_alignParentStart="true" />
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
......@@ -159,7 +168,17 @@
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/lbprBandwith"
android:layout_below="@+id/lbprPackageRate"
android:layout_toStartOf="@+id/btnprStart" />
<TextView
android:id="@+id/lbprToken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/textViewprpackagesize" />
android:layout_marginStart="35dp"
android:layout_toEndOf="@+id/textView10"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
......@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......
#Mon Dec 28 10:00:20 PST 2015
#Tue Mar 21 14:57:05 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
......@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.0'
defaultConfig {
minSdkVersion 21
......
......@@ -73,7 +73,7 @@ import java.util.concurrent.ConcurrentHashMap;
private ScanCallback mScanCallback;
private BluetoothGattCallback mGattCallback;
private ConcurrentHashMap<String, NCMCPeripheralInfo> mDiscoveredPeripherals = null; // key:device address/identifier
private ConcurrentHashMap<String, List<NCMCMessageData>> mMessageSendMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, List<NCMCMessageData>> mMessageSendMap = new ConcurrentHashMap<>();
//endregion
//region PERIPHERAL VARS
......@@ -443,7 +443,7 @@ import java.util.concurrent.ConcurrentHashMap;
mCentralService.notifyFoundPeer(peerID);
}
// stop scan
stopBrowsing();
//stopBrowsing();
}
} catch (NullPointerException ex) {
ex.printStackTrace();
......@@ -577,14 +577,16 @@ import java.util.concurrent.ConcurrentHashMap;
Log.d(TAG, "onCharacteristicWrite: " + characteristic.getUuid().toString() + " device = " + gatt.getDevice().getAddress() + " status = " + status);
try {
String deviceAddress = gatt.getDevice().getAddress();
if (mMessageSendMap.containsKey(deviceAddress)) {
List<NCMCMessageData> messageQueue = mMessageSendMap.get(deviceAddress);
if (messageQueue != null) {
Log.d(TAG, "onCharacteristicWrite: "+ deviceAddress + " current queue size:" + messageQueue.size());
messageQueue.remove(0);
if (mMessageSendMap.get(deviceAddress).size() != 0) {
executeSendCentralData(deviceAddress);
synchronized (mMessageSendMap) {
if (mMessageSendMap.containsKey(deviceAddress)) {
List<NCMCMessageData> messageQueue = mMessageSendMap.get(deviceAddress);
if (messageQueue != null) {
Log.d(TAG, "onCharacteristicWrite: " + deviceAddress + " current queue size:" + messageQueue.size());
messageQueue.remove(0);
if (mMessageSendMap.get(deviceAddress).size() != 0) {
executeSendCentralData(deviceAddress);
}
}
}
}
......@@ -770,7 +772,7 @@ import java.util.concurrent.ConcurrentHashMap;
mMessageSendMap.put(address, new LinkedList<NCMCMessageData>());
}
List<byte[]> msgs = makeMsg(message, MAX_MTU);
List<byte[]> msgs = makeMsg(message, info.mtu);
List<NCMCMessageData> tmpMessageQueue = new LinkedList<>();
for (byte[] msg : msgs) {
......@@ -779,13 +781,15 @@ import java.util.concurrent.ConcurrentHashMap;
tmpMessageQueue.add(msgData);
}
List<NCMCMessageData> messageQueue = this.mMessageSendMap.get(address);
Log.d(TAG, "sendCentralDataToPeripheral: " + address +" current queue size:"+messageQueue.size() + " tmpQueue size:" + tmpMessageQueue.size());
messageQueue.addAll(tmpMessageQueue);
this.mMessageSendMap.put(address, messageQueue);
synchronized (mMessageSendMap) {
List<NCMCMessageData> messageQueue = this.mMessageSendMap.get(address);
messageQueue.addAll(tmpMessageQueue);
this.mMessageSendMap.put(address, messageQueue);
Log.d(TAG, "sendCentralDataToPeripheral: " + address + " current queue size:" + messageQueue.size() + " tmpQueue size:" + tmpMessageQueue.size());
if (this.mMessageSendMap.get(address).size() == tmpMessageQueue.size()) {
executeSendCentralData(address); // trigger execute write when this is the first message in the queue.
if (this.mMessageSendMap.get(address).size() == tmpMessageQueue.size()) {
executeSendCentralData(address); // trigger execute write when this is the first message in the queue.
}
}
}
}
......@@ -802,7 +806,7 @@ import java.util.concurrent.ConcurrentHashMap;
byte[] dataToSend = msgInfo.getFullData();
if (msgInfo.isReliable && targetInfo.writeWithResponseCharacteristic != null) {
targetInfo.writeWithoutResponseCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
targetInfo.writeWithResponseCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
targetInfo.writeWithResponseCharacteristic.setValue(dataToSend);
targetInfo.bluetoothGatt.writeCharacteristic(targetInfo.writeWithResponseCharacteristic);
Log.d(TAG, "sendDataToPeripheral: "+targetInfo.device.getAddress()+" WithResponse: byteMsg size :" + dataToSend.length);
......
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