Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WAN log, manual NAT puncturing to debug #37

Merged
merged 5 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ android {

defaultConfig {
applicationId "nl.tudelft.trustchain"
minSdkVersion 24
minSdkVersion 22
targetSdkVersion 29
versionCode generateVersionCode()
versionName "0.1"
Expand Down Expand Up @@ -117,8 +117,8 @@ dependencies {

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

// Logging
Expand All @@ -135,6 +135,7 @@ dependencies {

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.freeCompilerArgs += [
"-Xuse-experimental=kotlin.Experimental,kotlin.ExperimentalUnsignedTypes"
"-Xuse-experimental=kotlin.Experimental,kotlin.ExperimentalUnsignedTypes",
"-Xopt-in=kotlin.RequiresOptIn"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nl.tudelft.trustchain.app

import android.app.Application
import android.bluetooth.BluetoothManager
import android.os.Build
import android.util.Log
import androidx.core.content.getSystemService
import androidx.preference.PreferenceManager
Expand Down Expand Up @@ -111,8 +112,8 @@ class TrustChainApplication : Application() {
val strategies = mutableListOf(
randomWalk, randomChurn, periodicSimilarity, nsd
)
if (bluetoothManager.adapter != null) {
val ble = BluetoothLeDiscovery.Factory(this, bluetoothManager)
if (bluetoothManager.adapter != null && Build.VERSION.SDK_INT >= 24) {
val ble = BluetoothLeDiscovery.Factory()
strategies += ble
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.tudelft.trustchain.app.ui.dashboard

import android.content.res.ColorStateList
import androidx.core.content.res.ResourcesCompat
import com.mattskala.itemadapter.BindingItemRenderer
import nl.tudelft.trustchain.app.databinding.ItemDashboardBinding

Expand All @@ -12,7 +13,7 @@ class DashboardItemRenderer(
) {
override fun bindView(item: DashboardItem, binding: ItemDashboardBinding) {
val context = binding.root.context
val color = context.getColor(item.app.color)
val color = ResourcesCompat.getColor(context.resources, item.app.color, null)
binding.imgIcon.setImageResource(item.app.icon)
binding.imgIcon.imageTintList = ColorStateList.valueOf(color)
binding.txtAppName.text = item.app.appName
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
buildscript {
ext.kotlin_version = '1.3.72'
ext.ktlint_version = '0.36.0'
ext.coroutines_version = '1.3.7'
ext.ktlint_gradle_version = '9.1.1'
ext.sqldelight_version = '1.2.2'
ext.nav_version = "2.2.1"
Expand Down
9 changes: 8 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 24
minSdkVersion 22
targetSdkVersion 29

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -91,3 +91,10 @@ dependencies {
testImplementation "com.squareup.sqldelight:sqlite-driver:$sqldelight_version"
testImplementation "com.goterl.lazycode:lazysodium-java:4.2.4"
}


tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.freeCompilerArgs += [
"-Xopt-in=kotlin.RequiresOptIn"
]
}
20 changes: 20 additions & 0 deletions common/src/main/java/nl/tudelft/trustchain/common/DemoCommunity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package nl.tudelft.trustchain.common

import android.util.Log
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.BroadcastChannel
import nl.tudelft.ipv8.IPv4Address
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.attestation.trustchain.TrustChainCommunity
import nl.tudelft.ipv8.messaging.*
import nl.tudelft.ipv8.messaging.payload.IntroductionResponsePayload
import nl.tudelft.ipv8.messaging.payload.PuncturePayload
import java.util.*

class DemoCommunity : Community() {
Expand All @@ -17,6 +20,9 @@ class DemoCommunity : Community() {

val lastTrackerResponses = mutableMapOf<IPv4Address, Date>()

@ExperimentalCoroutinesApi
val punctureChannel = BroadcastChannel<Pair<Address, PuncturePayload>>(10000)

// Retrieve the trustchain community
private fun getTrustChainCommunity(): TrustChainCommunity {
return IPv8Android.getInstance().getOverlay()
Expand Down Expand Up @@ -46,6 +52,7 @@ class DemoCommunity : Community() {
object MessageId {
const val THALIS_MESSAGE = 222
const val TORRENT_MESSAGE = 223
const val PUNCTURE_TEST = 251
}

// SEND MESSAGE
Expand All @@ -69,10 +76,17 @@ class DemoCommunity : Community() {
}
}

fun sendPuncture(address: IPv4Address, id: Int) {
val payload = PuncturePayload(myEstimatedLan, myEstimatedWan, id)
val packet = serializePacket(MessageId.PUNCTURE_TEST, payload, sign = false)
endpoint.send(address, packet)
}

// RECEIVE MESSAGE
init {
messageHandlers[MessageId.THALIS_MESSAGE] = ::onMessage
messageHandlers[MessageId.TORRENT_MESSAGE] = ::onTorrentMessage
messageHandlers[MessageId.PUNCTURE_TEST] = ::onPunctureTest
}

private fun onMessage(packet: Packet) {
Expand All @@ -85,6 +99,12 @@ class DemoCommunity : Community() {
val (peer, payload) = packet.getAuthPayload(MyMessage.Deserializer)
Log.i("personal", peer.mid + ": " + payload.message)
}

@OptIn(ExperimentalCoroutinesApi::class)
private fun onPunctureTest(packet: Packet) {
val payload = packet.getPayload(PuncturePayload.Deserializer)
punctureChannel.offer(Pair(packet.source, payload))
}
}

// THE MESSAGE (CLASS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package nl.tudelft.trustchain.common

import android.util.Log
Expand Down Expand Up @@ -47,7 +48,7 @@ class MarketCommunity : Community() {
private fun onTradePacket(packet: Packet) {
val payload = packet.getAuthPayload(TradePayload.Deserializer).second
notifyListeners(payload)
Log.d("MarketCommunity::onTradePacket", "Received packet:{${payload.primaryCurrency}, ${payload.secondaryCurrency}, ${payload.amount}, ${payload.price}, ${payload.type}}")
Log.d("MarketCommunity", "Received packet:{${payload.primaryCurrency}, ${payload.secondaryCurrency}, ${payload.amount}, ${payload.price}, ${payload.type}}")
}

fun addListener(type: TradePayload.Type?, listener: (TradePayload) -> Unit) {
Expand Down
2 changes: 1 addition & 1 deletion currencyii/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 24
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
12 changes: 9 additions & 3 deletions debug/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 24
minSdkVersion 22
targetSdkVersion 29

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -57,8 +57,8 @@ dependencies {

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

// Logging
Expand All @@ -72,3 +72,9 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.freeCompilerArgs += [
"-Xopt-in=kotlin.RequiresOptIn"
]
}
50 changes: 44 additions & 6 deletions debug/src/main/java/nl/tudelft/trustchain/debug/DebugFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,56 @@ import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.text.style.ForegroundColorSpan
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import kotlinx.coroutines.*
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.peerdiscovery.DiscoveryCommunity
import nl.tudelft.ipv8.util.toHex
import nl.tudelft.trustchain.common.ui.BaseFragment
import nl.tudelft.trustchain.common.util.viewBinding
import nl.tudelft.trustchain.debug.databinding.FragmentDebugBinding
import java.text.SimpleDateFormat
import java.util.*


class DebugFragment : BaseFragment(R.layout.fragment_debug) {
private val binding by viewBinding(FragmentDebugBinding::bind)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.debug_options, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.wanLog -> {
findNavController().navigate(R.id.wanLogFragment)
true
}
R.id.multiPunch -> {
findNavController().navigate(R.id.punctureFragment)
true
}
else -> super.onOptionsItemSelected(item)
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand Down Expand Up @@ -56,7 +87,7 @@ class DebugFragment : BaseFragment(R.layout.fragment_debug) {
append(overlay.javaClass.simpleName)
append(" (")
val textColorResId = if (overlay.getPeers().isNotEmpty()) R.color.green else R.color.red
val textColor = resources.getColor(textColorResId, null)
val textColor = ResourcesCompat.getColor(resources, textColorResId, null)
inSpans(ForegroundColorSpan(textColor)) {
val peers = overlay.getPeers()
val peersCountStr = resources.getQuantityString(
Expand All @@ -74,28 +105,35 @@ class DebugFragment : BaseFragment(R.layout.fragment_debug) {
}
val totalPeersCount = ipv8.network.verifiedPeers.size
val textColorResId = if (totalPeersCount > 0) R.color.green else R.color.red
val textColor = resources.getColor(textColorResId, null)
val textColor = ResourcesCompat.getColor(resources, textColorResId, null)
inSpans(ForegroundColorSpan(textColor)) {
append(resources.getQuantityString(R.plurals.x_peers, totalPeersCount, totalPeersCount))
}
}

updateBootstrapList()

lifecycleScope.launch {
lifecycleScope.launchWhenCreated {
val blockCount = withContext(Dispatchers.IO) {
getTrustChainCommunity().database.getBlockCount(null)
}
binding.txtBlockCount.text = blockCount.toString()
if (view != null) {
binding.txtBlockCount.text = blockCount.toString()
}
}

lifecycleScope.launch {
lifecycleScope.launchWhenCreated {
val chainLength = withContext(Dispatchers.IO) {
getTrustChainCommunity().getChainLength()
}
binding.txtChainLength.text = chainLength.toString()
if (view != null) {
binding.txtChainLength.text = chainLength.toString()
}
}

binding.txtConnectionType.text = getDemoCommunity().network.wanLog
.estimateConnectionType().value

try {
val pInfo: PackageInfo =
requireContext().packageManager.getPackageInfo(requireContext().packageName, 0)
Expand Down
Loading