From e666993ba8d7f7bf49d8139b109bb3ab21bd55a8 Mon Sep 17 00:00:00 2001 From: neal Date: Sun, 5 Mar 2023 14:24:48 +0100 Subject: [PATCH 01/75] Add TransactionCommunity --- .../trustchain/app/TrustChainApplication.kt | 12 +++++++- .../trustchain/detoks/DeToksMainActivity.kt | 16 ++++++++++ .../nl/tudelft/trustchain/detoks/MyMessage.kt | 13 ++++++++ .../trustchain/detoks/TransactionCommunity.kt | 30 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 detoks/src/main/java/nl/tudelft/trustchain/detoks/MyMessage.kt create mode 100644 detoks/src/main/java/nl/tudelft/trustchain/detoks/TransactionCommunity.kt diff --git a/app/src/main/java/nl/tudelft/trustchain/app/TrustChainApplication.kt b/app/src/main/java/nl/tudelft/trustchain/app/TrustChainApplication.kt index edec87c89..2d9d67d93 100644 --- a/app/src/main/java/nl/tudelft/trustchain/app/TrustChainApplication.kt +++ b/app/src/main/java/nl/tudelft/trustchain/app/TrustChainApplication.kt @@ -62,6 +62,7 @@ import nl.tudelft.trustchain.common.bitcoin.WalletService import nl.tudelft.trustchain.common.eurotoken.GatewayStore import nl.tudelft.trustchain.common.eurotoken.TransactionRepository import nl.tudelft.trustchain.currencyii.CoinCommunity +import nl.tudelft.trustchain.detoks.TransactionCommunity import nl.tudelft.trustchain.eurotoken.community.EuroTokenCommunity import nl.tudelft.trustchain.eurotoken.db.TrustStore import nl.tudelft.trustchain.gossipML.RecommenderCommunity @@ -117,7 +118,8 @@ class TrustChainApplication : Application() { createLiteratureCommunity(), createRecommenderCommunity(), createIdentityCommunity(), - createFOCCommunity() + createFOCCommunity(), + createTransactionCommunity() ), walkerInterval = 5.0 ) @@ -281,6 +283,14 @@ class TrustChainApplication : Application() { ) } + private fun createTransactionCommunity(): OverlayConfiguration { + val randomWalk = RandomWalk.Factory() + return OverlayConfiguration( + Overlay.Factory(TransactionCommunity::class.java), + listOf(randomWalk) + ) + } + private fun createDiscoveryCommunity(): OverlayConfiguration { val randomWalk = RandomWalk.Factory() val randomChurn = RandomChurn.Factory() diff --git a/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksMainActivity.kt b/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksMainActivity.kt index 68acab9b6..344b2f3dc 100644 --- a/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksMainActivity.kt +++ b/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksMainActivity.kt @@ -1,6 +1,11 @@ package nl.tudelft.trustchain.detoks import android.os.Bundle +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import nl.tudelft.ipv8.android.IPv8Android import nl.tudelft.trustchain.common.BaseActivity class DeToksActivity : BaseActivity() { @@ -10,5 +15,16 @@ class DeToksActivity : BaseActivity() { super.onCreate(savedInstanceState) val actionBar = supportActionBar actionBar!!.hide() + broadcast() + } + + private fun broadcast() { + val community = IPv8Android.getInstance().getOverlay()!! + lifecycleScope.launch { + while (isActive) { + community.broadcastGreeting() + delay(1000) + } + } } } diff --git a/detoks/src/main/java/nl/tudelft/trustchain/detoks/MyMessage.kt b/detoks/src/main/java/nl/tudelft/trustchain/detoks/MyMessage.kt new file mode 100644 index 000000000..ca2f78d8e --- /dev/null +++ b/detoks/src/main/java/nl/tudelft/trustchain/detoks/MyMessage.kt @@ -0,0 +1,13 @@ +import nl.tudelft.ipv8.messaging.Deserializable + +class MyMessage(val message: String) : nl.tudelft.ipv8.messaging.Serializable { + override fun serialize(): ByteArray { + return message.toByteArray() + } + + companion object Deserializer : Deserializable { + override fun deserialize(buffer: ByteArray, offset: Int): Pair { + return Pair(MyMessage(buffer.toString(Charsets.UTF_8)), buffer.size) + } + } +} diff --git a/detoks/src/main/java/nl/tudelft/trustchain/detoks/TransactionCommunity.kt b/detoks/src/main/java/nl/tudelft/trustchain/detoks/TransactionCommunity.kt new file mode 100644 index 000000000..a401c5860 --- /dev/null +++ b/detoks/src/main/java/nl/tudelft/trustchain/detoks/TransactionCommunity.kt @@ -0,0 +1,30 @@ +package nl.tudelft.trustchain.detoks + +import MyMessage +import mu.KotlinLogging +import nl.tudelft.ipv8.Community +import nl.tudelft.ipv8.messaging.Packet + +private const val MESSAGE_ID = 1 +private val logger = KotlinLogging.logger {} + +class TransactionCommunity: Community() { + override val serviceId = "02313685c1912a144279f8248fc3db5899c5df8c" + + + init { + messageHandlers[MESSAGE_ID] = ::onMessage + } + + private fun onMessage(packet: Packet) { + val (peer, payload) = packet.getAuthPayload(MyMessage.Deserializer) + logger.debug("DemoCommunity", peer.mid + ": " + payload.message) + } + + fun broadcastGreeting() { + for (peer in getPeers()) { + val packet = serializePacket(MESSAGE_ID, MyMessage("Hello!")) + send(peer.address, packet) + } + } +} From 58826bb674a1ea7e0a7c022d751039a3ea20c505 Mon Sep 17 00:00:00 2001 From: Bob Brockbernd Date: Mon, 6 Mar 2023 09:17:55 +0100 Subject: [PATCH 02/75] Text + button --- .../trustchain/detoks/DeToksFragment.kt | 8 ++++++++ .../src/main/res/layout/fragment_detoks.xml | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksFragment.kt b/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksFragment.kt index 3c3633d75..d2fdd99c6 100644 --- a/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksFragment.kt +++ b/detoks/src/main/java/nl/tudelft/trustchain/detoks/DeToksFragment.kt @@ -3,6 +3,8 @@ package nl.tudelft.trustchain.detoks import android.os.Bundle import android.util.Log import android.view.View +import android.widget.Button +import android.widget.Toast import androidx.viewpager2.widget.ViewPager2 import kotlinx.android.synthetic.main.fragment_detoks.* import mu.KotlinLogging @@ -58,6 +60,12 @@ class DeToksFragment : BaseFragment(R.layout.fragment_detoks) { viewPagerVideos.adapter = VideosAdapter(torrentManager) viewPagerVideos.currentItem = 0 + + val button = view.findViewById