Skip to content

Commit

Permalink
add toast notifications for leader election
Browse files Browse the repository at this point in the history
  • Loading branch information
MozaTech committed Jun 10, 2024
1 parent 1afce7b commit 3408f4b
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.fragment.app.Fragment
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.Overlay
import nl.tudelft.ipv8.Peer
Expand Down Expand Up @@ -252,6 +253,12 @@ open class CoinCommunity constructor(
payload: ByteArray
) {
Log.i("CoinCommunitySending", "Sending payload to ${peer.address}")
Toast.makeText(
this.context,
"Sending payload to ${peer.address}",
Toast.LENGTH_SHORT
).show()

send(
peer,
payload
Expand Down Expand Up @@ -293,13 +300,24 @@ open class CoinCommunity constructor(

private fun onDaoJoinDataPacket(packet: Packet) {
Log.d("LEADER", "Received data from Peer wanting to join")
Toast.makeText(
this.context,
"Received data from Peer wanting to join",
Toast.LENGTH_SHORT
).show()

val (peer, payload) =
packet.getAuthPayload(
SignPayload.Deserializer
)
try {
Log.d("LEADER", "Peer wanting to join: ${peer.publicKey}")
Toast.makeText(
this.context,
"Peer wanting to join: ${peer.publicKey}",
Toast.LENGTH_SHORT
).show()

joinBitcoinWallet(
payload.mostRecentSWBlock.transaction,
payload.proposeBlockData,
Expand All @@ -309,8 +327,19 @@ open class CoinCommunity constructor(
// Add new nonceKey after joining a DAO
WalletManagerAndroid.getInstance()
.addNewNonceKey(payload.proposeBlockData.SW_UNIQUE_ID, this.context)

Toast.makeText(
this.context,
"Peer: ${peer.publicKey}, joined successfully",
Toast.LENGTH_SHORT
).show()
} catch (t: Throwable) {
Log.e("LEADER", "Joining failed. ${t.message ?: "No further information"}.")
Toast.makeText(
this.context,
"Peer: ${peer.publicKey}, failed to join",
Toast.LENGTH_SHORT
).show()
}
}

Expand Down Expand Up @@ -354,6 +383,12 @@ open class CoinCommunity constructor(
payload: ElectedPayload
) {
Log.d("LEADER", "Elected: " + peer.publicKey)
Toast.makeText(
this.context,
"Elected: ${peer.publicKey} as leader",
Toast.LENGTH_SHORT
).show()

getCurrentLeader()[payload.DAOid.decodeToString()] = peer
}

Expand All @@ -363,6 +398,12 @@ open class CoinCommunity constructor(
ElectionPayload.Deserializer
)
Log.d("Leader", "Election packet received.")

Toast.makeText(
this.context,
"Election packed received",
Toast.LENGTH_SHORT
).show()
getCandidates()[payload.DAOid.decodeToString()] = ArrayList()
onElectionRequest(peer, payload)
}
Expand Down Expand Up @@ -390,6 +431,11 @@ open class CoinCommunity constructor(
this.sendPayload(peer, aliveResponse)

Log.d("Leader", "Election started.")
Toast.makeText(
this.context,
"Leader election started",
Toast.LENGTH_SHORT
).show()

getCurrentLeader()[payload.DAOid.decodeToString()] = null

Expand All @@ -403,6 +449,11 @@ open class CoinCommunity constructor(

if (higherPeers.isEmpty()) {
Log.d("Leader", "Elected: " + this.myPeer.publicKey)
Toast.makeText(
this.context,
"Elected ${this.myPeer.publicKey} as leader",
Toast.LENGTH_SHORT
).show()

val electedPayload = this.createElectedResponse(payload.DAOid)
this.sendPayload(peer, electedPayload)
Expand Down Expand Up @@ -450,6 +501,12 @@ open class CoinCommunity constructor(
) {
Log.d("LEADER", "Leader doesn't exists.")
Log.d("LEADER", "Requesting election...")
Toast.makeText(
this.context,
"Requesting election...",
Toast.LENGTH_SHORT
).show()

val peers = this.getPeers()
val peerPK = getPeersPKInDao(publicKeyBlock)
for (peer in peers) {
Expand All @@ -466,6 +523,12 @@ open class CoinCommunity constructor(

val currentLeader = getCurrentLeader()[publicKeyBlock.decodeToString()]!!
Log.d("LEADER", "sending dao join transaction data to leader ${currentLeader.publicKey}")
Toast.makeText(
this.context,
"sending dao join transaction data to leader ${currentLeader.publicKey}",
Toast.LENGTH_SHORT
).show()

val payload =
SignPayload(
getServiceIdNew().toByteArray(),
Expand Down

0 comments on commit 3408f4b

Please sign in to comment.