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

Development #66

Merged
merged 5 commits into from
May 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,8 @@ class TrustChainApplication : Application() {
private fun createDeToksCommunity(): OverlayConfiguration<DeToksCommunity> {
val randomWalk = RandomWalk.Factory()

val settings = TrustChainSettings()
val driver = AndroidSqliteDriver(Database.Schema, this, "detoks.db")
val store = TrustChainSQLiteStore(Database(driver))

return OverlayConfiguration(
DeToksCommunity.Factory(this, settings, store),
DeToksCommunity.Factory(this),
listOf(randomWalk)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package nl.tudelft.trustchain.detoks

import android.content.Context
import android.util.Log
import com.frostwire.jlibtorrent.Sha1Hash
import nl.tudelft.ipv8.Overlay
import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.attestation.trustchain.TrustChainCommunity
import nl.tudelft.ipv8.attestation.trustchain.TrustChainCrawler
import nl.tudelft.ipv8.attestation.trustchain.TrustChainSettings
import nl.tudelft.ipv8.attestation.trustchain.store.TrustChainStore
import nl.tudelft.ipv8.messaging.Deserializable
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.ipv8.messaging.Serializable
import nl.tudelft.trustchain.detoks.gossiper.*
import kotlin.math.max
import nl.tudelft.trustchain.detoks.gossiper.BootGossiper
import nl.tudelft.trustchain.detoks.gossiper.BootMessage
import nl.tudelft.trustchain.detoks.gossiper.NetworkSizeGossiper
import nl.tudelft.trustchain.detoks.gossiper.NetworkSizeMessage
import nl.tudelft.trustchain.detoks.gossiper.TorrentGossiper
import nl.tudelft.trustchain.detoks.gossiper.TorrentMessage


class DeToksCommunity(
Expand All @@ -37,7 +35,6 @@ class DeToksCommunity(

companion object {
const val LOGGING_TAG = "DeToksCommunity"
const val BLOCK_TYPE = "detoks_transaction"
const val MESSAGE_TORRENT_ID = 1
const val MESSAGE_TRANSACTION_ID = 2
const val MESSAGE_NETWORK_SIZE_ID = 3
Expand Down Expand Up @@ -72,33 +69,14 @@ class DeToksCommunity(
send(peer.address, packet)
}
} else {
Log.d(LOGGING_TAG, "Insufficient funds!")
Log.e(LOGGING_TAG, "Insufficient funds!")
}

}
fun increaseTokens(amount: Float) {
val x = walletManager.getOrCreateWallet(myPeer.mid)
walletManager.setWalletBalance(myPeer.mid, x.balance + amount)
}
fun requestTokens(amount: Float, recipientMid: String) {
if (myPeer.mid == recipientMid) {
Log.d(LOGGING_TAG, "Cannot request tokens from yourself.")
return
}

Log.d(LOGGING_TAG, "Requesting $amount tokens from $recipientMid")

// Find the peer by its mid
val recipientPeer = getPeers().find { it.mid == recipientMid }

// If the peer is found, send a token request message
if (recipientPeer != null) {
val requestMessage = TokenRequestMessage(amount, myPeer.mid, recipientMid)
gossipWith(recipientPeer, requestMessage, MESSAGE_TOKEN_REQUEST_ID)
} else {
Log.d(LOGGING_TAG, "Peer not found: $recipientMid")
}
}

fun saveLibTorrentPort(port: String) {
libtorrentPort = port
Expand Down Expand Up @@ -233,10 +211,7 @@ class DeToksCommunity(
return walletManager.getOrCreateWallet(myPeer.mid).balance
}
class Factory(
private val context: Context,
private val settings: TrustChainSettings,
private val database: TrustChainStore,
private val crawler: TrustChainCrawler = TrustChainCrawler()
private val context: Context
) : Overlay.Factory<DeToksCommunity>(DeToksCommunity::class.java) {
override fun create(): DeToksCommunity {
return DeToksCommunity(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ class DeToksFragment : BaseFragment(R.layout.fragment_detoks) {
})
}

fun onVideoError() {
viewPagerVideos.scrollTo(100, 100)
}



companion object {
const val DEFAULT_CACHING_AMOUNT = 2
const val DEFAULT_TORRENT_FILE = "detoks.torrent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import nl.tudelft.trustchain.common.BaseActivity
import nl.tudelft.trustchain.detoks.gossiper.GossiperService
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.attestation.trustchain.BlockListener
import nl.tudelft.ipv8.attestation.trustchain.BlockSigner
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
import nl.tudelft.ipv8.attestation.trustchain.store.TrustChainStore
import nl.tudelft.ipv8.attestation.trustchain.validation.TransactionValidator
import nl.tudelft.ipv8.attestation.trustchain.validation.ValidationResult


class DeToksActivity : BaseActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.*
import com.frostwire.jlibtorrent.TorrentHandle
import android.widget.TextView
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.trustchain.detoks.gossiper.NetworkSizeGossiper
import nl.tudelft.trustchain.common.ui.BaseFragment

import nl.tudelft.trustchain.detoks.gossiper.NetworkSizeGossiper

class DetoksDebugFragment : BaseFragment(R.layout.fragment_detoks_debug) {

private val deToksCommunity = IPv8Android.getInstance().getOverlay<DeToksCommunity>()!!
private lateinit var torrentManager: TorrentManager
val strategyDescr = mapOf(0 to "Random (randomly chosen torrent)",
private val strategyDescr = mapOf(0 to "Random (randomly chosen torrent)",
1 to "Hot (watchtime in descending order with time cut-off)",
2 to "Rising (watchtime in descending order with lower time cut-off than hot)",
3 to "New (upload date from newest to oldest)",
Expand Down
8 changes: 1 addition & 7 deletions detoks/src/main/java/nl/tudelft/trustchain/detoks/Profile.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package nl.tudelft.trustchain.detoks

import android.util.Log
import com.frostwire.jlibtorrent.TorrentInfo
import nl.tudelft.trustchain.detoks.gossiper.NetworkSizeGossiper
import kotlin.math.min
import kotlin.math.max


class ProfileEntry(
Expand All @@ -14,7 +12,7 @@ class ProfileEntry(
var uploadDate: Long = 0, // This is the torrent creation date
var hopCount: Int = 0, // Amount of other nodes visited
var timesSeen: Int = 0, // Count of times we received it
var likes: Int = 0, // TODO: Dependent on other team
var likes: Int = 0,
)

class Profile(
Expand All @@ -27,10 +25,6 @@ class Profile(
return profiles[key]!!
}

fun hasWatched(key: String): Boolean {
return profiles.contains(key) && profiles[key]!!.watched
}

fun updateEntryDuration(key: String, duration: Long) {
addProfile(key)
profiles[key]!!.duration = duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SeedingDebugFragment : BaseFragment(R.layout.fragment_debug_seeding) {
handler.postDelayed(this, 2000)
}
}
handler.postDelayed(runnable,2000)
handler.post(runnable)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -43,8 +43,6 @@ class SeedingDebugFragment : BaseFragment(R.layout.fragment_debug_seeding) {
val strategyRecycleView = view.findViewById<RecyclerView>(R.id.debugView)
strategyRecycleView.adapter = seedingDebugAdapter
strategyRecycleView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)


}
}

Expand Down Expand Up @@ -82,15 +80,10 @@ class SeedingDebugAdapter(private val strategyData: List<TorrentHandle>) : Recyc
val bundle = Bundle()
bundle.putString("torrent_name", holder.hashTextView.text.toString())


holder.downloadTextView.text = status.state().name

holder.uploadTextView.text = status.isSeeding.toString()

holder.seederTextView.text = status.listSeeds().toString()

holder.leecherTextView.text = status.listPeers().toString()

}

override fun getItemCount(): Int = strategyData.size
Expand Down
20 changes: 6 additions & 14 deletions detoks/src/main/java/nl/tudelft/trustchain/detoks/Strategy.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nl.tudelft.trustchain.detoks

import android.util.Log
import nl.tudelft.trustchain.detoks.TorrentManager.TorrentHandler
import java.nio.channels.Selector
import kotlin.random.Random

class Strategy {
Expand All @@ -19,7 +17,6 @@ private val strategyComparators = mutableMapOf<Int, (Pair<TorrentHandler, Profil


companion object {

const val STRATEGY_RANDOM = 0
const val STRATEGY_HOT = 1
const val STRATEGY_RISING = 2
Expand Down Expand Up @@ -123,21 +120,16 @@ private val strategyComparators = mutableMapOf<Int, (Pair<TorrentHandler, Profil
if (id == STRATEGY_RANDOM) return handlers.shuffled().toMutableList()
if (!strategyComparators.contains(id)) return handlers

var handlerProfile = handlers.map {
val key = it.handle.infoHash().toString()
val handlerProfile = handlers.map {
val key = TorrentManager.createKey(it.handle.infoHash(), it.fileIndex)
if (!profiles.contains(key)) return@map Pair(it, ProfileEntry())
return@map Pair(it, profiles[key])
}

var sortedHandlerProfile: MutableList<Pair<TorrentHandler, ProfileEntry?>>

if (id == STRATEGY_RISING){
sortedHandlerProfile = filteredSort(handlerProfile, id, RISING_CUTOFF_SECONDS)
} else if (id == STRATEGY_HOT) {
sortedHandlerProfile = filteredSort(handlerProfile, id, HOT_CUTOFF_SECONDS)
} else {
sortedHandlerProfile =
handlerProfile.sortedWith(strategyComparators[id]!!).toMutableList()
val sortedHandlerProfile: MutableList<Pair<TorrentHandler, ProfileEntry?>> = when (id) {
STRATEGY_RISING -> filteredSort(handlerProfile, id, RISING_CUTOFF_SECONDS)
STRATEGY_HOT -> filteredSort(handlerProfile, id, HOT_CUTOFF_SECONDS)
else -> handlerProfile.sortedWith(strategyComparators[id]!!).toMutableList()
}

return sortedHandlerProfile.map { it.first }.toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.appcompat.widget.SwitchCompat
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.exoplayer2.util.Log
import kotlinx.android.synthetic.main.fragment_strategy.*
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.trustchain.common.ui.BaseFragment
Expand All @@ -38,8 +37,6 @@ class StrategyFragment : BaseFragment(R.layout.fragment_strategy) {
torrentManager = TorrentManager.getInstance(requireActivity())
strategyRecyclerViewAdapter = StrategyAdapter(torrentManager.seedingTorrents, torrentManager.profitMap)



val handler = Handler((Looper.getMainLooper()))
val runnable: Runnable = object : Runnable {
override fun run() {
Expand Down Expand Up @@ -176,8 +173,6 @@ class StrategyAdapter(private val strategyData: List<TorrentHandler>, private va
}
}

val community: DeToksCommunity = IPv8Android.getInstance().getOverlay()!!

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.strategy_recycle_view, parent, false)
Expand Down
Loading