-
Notifications
You must be signed in to change notification settings - Fork 8
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
Federation Data Exchange for FE2 #1943
Changes from 15 commits
8d576ec
e80f557
1677c00
bbc7b5e
71e63ad
05c3de9
62f22bb
05c5dcb
7bf43cb
65dc966
ac25784
4a32b59
4b52dbf
07913e0
147f0be
20998ca
9550e1e
5008da9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.github.dedis.popstellar.model.network.method.message.data.federation | ||
|
||
import com.github.dedis.popstellar.model.network.method.message.MessageGeneral | ||
import com.github.dedis.popstellar.model.network.method.message.data.Action | ||
import com.github.dedis.popstellar.model.network.method.message.data.Data | ||
import com.github.dedis.popstellar.model.network.method.message.data.Objects | ||
import com.google.gson.annotations.SerializedName | ||
|
||
/** Informs about the result of the authentication procedure */ | ||
class FederationResult | ||
/** | ||
* Constructor for a data Federation Result | ||
* | ||
* @param status status of the result (either success or failure) | ||
* @param reason reason of the failure | ||
* @param publicKey public key of the other LAO organizer | ||
* @param challenge challenge used to connect the LAOs | ||
*/ | ||
( | ||
val status: String, | ||
val reason: String? = null, | ||
@SerializedName("public_key") val publicKey: String? = null, | ||
val challenge: MessageGeneral, | ||
) : Data { | ||
|
||
init { | ||
when (status) { | ||
FAILURE -> { | ||
require(reason != null) { "Reason must be provided for $FAILURE status." } | ||
require(publicKey == null) { "Public key must be null for $FAILURE status." } | ||
} | ||
SUCCESS -> { | ||
require(publicKey != null) { "Public key must be provided for $SUCCESS status." } | ||
require(reason == null) { "Reason must be null for $SUCCESS status." } | ||
} | ||
else -> throw IllegalArgumentException("Status must be either '$FAILURE' or '$SUCCESS'.") | ||
} | ||
} | ||
|
||
override val `object`: String | ||
get() = Objects.FEDERATION.`object` | ||
|
||
override val action: String | ||
get() = Action.RESULT.action | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) { | ||
return true | ||
} | ||
if (other == null || javaClass != other.javaClass) { | ||
return false | ||
} | ||
val that = other as FederationResult | ||
return status == that.status && | ||
reason == that.reason && | ||
publicKey == that.publicKey && | ||
challenge == that.challenge | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return java.util.Objects.hash(status, reason, publicKey, challenge) | ||
} | ||
|
||
override fun toString(): String { | ||
if (status == FAILURE) { | ||
return "FederationResult{status='$status', reason='$reason', challenge='$challenge'}" | ||
} else if (status == SUCCESS) { | ||
return "FederationResult{status='$status', public_key='$publicKey', " + | ||
"challenge='$challenge'}" | ||
} | ||
return "FederationResult{ERROR}" | ||
} | ||
|
||
fun isSuccess(): Boolean { | ||
return status == SUCCESS | ||
} | ||
|
||
companion object { | ||
private const val SUCCESS = "success" | ||
private const val FAILURE = "failure" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.dedis.popstellar.model.network.method.message.data.federation | ||
|
||
import com.github.dedis.popstellar.model.network.method.message.data.Action | ||
import com.github.dedis.popstellar.model.network.method.message.data.Data | ||
import com.github.dedis.popstellar.model.network.method.message.data.Objects | ||
import com.google.gson.annotations.SerializedName | ||
|
||
/** Token exchange to be broadcast in the LAO */ | ||
class TokensExchange | ||
/** | ||
* Constructor for a data TokenExchange | ||
* | ||
* @param laoId ID of the remote LAO | ||
* @param rollCallId ID of the rollCall of the remote LAO | ||
* @param tokens array of tokens contained in the rollCall | ||
* @param timestamp timestamp of the message | ||
*/ | ||
( | ||
@SerializedName("lao_id") val laoId: String, | ||
@SerializedName("roll_call_id") val rollCallId: String, | ||
val tokens: Array<String>, | ||
val timestamp: Long | ||
) : Data { | ||
override val `object`: String | ||
get() = Objects.FEDERATION.`object` | ||
|
||
override val action: String | ||
get() = Action.TOKENS_EXCHANGE.action | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) { | ||
return true | ||
} | ||
if (other == null || javaClass != other.javaClass) { | ||
return false | ||
} | ||
val that = other as TokensExchange | ||
return laoId == that.laoId && | ||
rollCallId == that.rollCallId && | ||
tokens.contentEquals(that.tokens) && | ||
timestamp == that.timestamp | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return java.util.Objects.hash(laoId, rollCallId, tokens, timestamp) | ||
} | ||
|
||
override fun toString(): String { | ||
return "TokensExchange{lao_id='$laoId', roll_call_id='$rollCallId', " + | ||
"tokens='$tokens', timestamp='$timestamp'}" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,14 @@ import javax.inject.Singleton | |
class LinkedOrganizationsRepository @Inject constructor() { | ||
private var challenge: Challenge? = null | ||
private var onChallengeUpdatedCallback: ((Challenge) -> Unit)? = null | ||
private var linkedLaos: MutableMap<String, MutableMap<String, Array<String>>> = mutableMapOf() | ||
private var onLinkedLaosUpdatedCallback: ((String, MutableMap<String, Array<String>>) -> Unit)? = | ||
null | ||
private var newTokensNotifyFunction: ((String, String, String, Array<String>) -> Unit)? = null | ||
var otherLaoId: String? = null | ||
var otherServerAddr: String? = null | ||
var otherPublicKey: String? = null | ||
|
||
/** | ||
* Updates the challenge | ||
* | ||
* @param challenge the new Challenge | ||
*/ | ||
fun updateChallenge(challenge: Challenge) { | ||
this.challenge = challenge | ||
onChallengeUpdatedCallback?.invoke(challenge) | ||
|
@@ -35,6 +34,36 @@ class LinkedOrganizationsRepository @Inject constructor() { | |
return challenge | ||
} | ||
|
||
fun addLinkedLao(laoId: String, otherLaoId: String, tokens: Array<String>) { | ||
val laoMap = linkedLaos.getOrPut(laoId) { mutableMapOf() } | ||
laoMap[otherLaoId] = tokens | ||
onLinkedLaosUpdatedCallback?.invoke(laoId, laoMap) | ||
} | ||
|
||
fun updateAndNotifyLinkedLao( | ||
laoId: String, | ||
otherLaoId: String, | ||
tokens: Array<String>, | ||
rollCallId: String | ||
) { | ||
addLinkedLao(laoId, otherLaoId, tokens) | ||
newTokensNotifyFunction?.invoke(laoId, otherLaoId, rollCallId, tokens) | ||
Comment on lines
+65
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you take a look at other repos (any one, e.g. rollcallrepo) you'd see that in general this is not the preferred way to notify modifications on a given data structure, but that we use java rx constructs (subject, observables and so on) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well the thing is that I tried using observables and for some reason I had many problems with the UI not updating or weird other issues... That's why in the end I changed to using callbacks, I know it's a bit more complex but at least it's working There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can keep it as is then |
||
} | ||
|
||
fun setOnLinkedLaosUpdatedCallback( | ||
callback: (String, MutableMap<String, Array<String>>) -> Unit | ||
) { | ||
onLinkedLaosUpdatedCallback = callback | ||
} | ||
|
||
fun setNewTokensNotifyFunction(function: (String, String, String, Array<String>) -> Unit) { | ||
newTokensNotifyFunction = function | ||
} | ||
|
||
fun getLinkedLaos(laoId: String): MutableMap<String, Array<String>> { | ||
return linkedLaos.getOrDefault(laoId, mutableMapOf()) | ||
} | ||
|
||
fun flush() { | ||
otherLaoId = null | ||
otherServerAddr = null | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ import com.github.dedis.popstellar.ui.lao.event.LaoDetailAnimation.showIn | |
import com.github.dedis.popstellar.ui.lao.event.LaoDetailAnimation.showOut | ||
import com.github.dedis.popstellar.ui.qrcode.QrScannerFragment | ||
import com.github.dedis.popstellar.ui.qrcode.ScanningAction | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* A simple [Fragment] subclass. Use the [LinkedOrganizationsFragment.newInstance] factory method to | ||
|
@@ -41,9 +44,6 @@ class LinkedOrganizationsFragment : Fragment() { | |
linkedOrganizationsViewModel = | ||
obtainLinkedOrganizationsViewModel(requireActivity(), laoViewModel.laoId) | ||
|
||
// Starts from a clean repository | ||
linkedOrganizationsViewModel.flushRepository() | ||
|
||
// Sets the text and the button depending on the user's role | ||
laoViewModel.role.observe(viewLifecycleOwner) { role: Role -> | ||
if (role == Role.ORGANIZER) { | ||
|
@@ -59,6 +59,19 @@ class LinkedOrganizationsFragment : Fragment() { | |
binding.inviteOtherOrganization.setOnClickListener(invitationPage) | ||
binding.joinOtherOrganizationInvitation.setOnClickListener(joinButton) | ||
|
||
// Displaying the linked organizations | ||
val laos = linkedOrganizationsViewModel.getLinkedLaosMap().keys | ||
displayLinkedOrganizations(laos) | ||
linkedOrganizationsViewModel.doWhenLinkedLaosIsUpdated { laoId, laoMap -> | ||
if (laoId == laoViewModel.laoId) { | ||
CoroutineScope(Dispatchers.Main).launch { | ||
val currentLaos = laoMap.keys | ||
displayLinkedOrganizations(currentLaos) | ||
} | ||
} | ||
} | ||
|
||
linkedOrganizationsViewModel.setLinkedLaosNotifyFunction() | ||
Comment on lines
+62
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why calling display first with the current list and then putting a custom and complex observer that uses coroutines and not directly using a simple observer? This relates to my previous comment on how to deal with modifications using observables and subjects |
||
handleBackNav() | ||
|
||
return binding.root | ||
|
@@ -97,13 +110,27 @@ class LinkedOrganizationsFragment : Fragment() { | |
|
||
private var joinButton = | ||
View.OnClickListener { | ||
linkedOrganizationsViewModel.flushRepository() | ||
laoViewModel.setIsTab(false) | ||
linkedOrganizationsViewModel.manager = parentFragmentManager | ||
LaoActivity.setCurrentFragment(parentFragmentManager, R.id.fragment_qr_scanner) { | ||
QrScannerFragment.newInstance(ScanningAction.FEDERATION_JOIN) | ||
} | ||
} | ||
|
||
private fun displayLinkedOrganizations(laos: Set<String>) { | ||
if (laos.isNotEmpty()) { | ||
val laosText = laos.joinToString(separator = "\n\n") | ||
val textToDisplay = context?.getString(R.string.list_organizations, laosText) | ||
binding.noOrganizationsText.visibility = View.GONE | ||
binding.listOrganizationsText.visibility = View.VISIBLE | ||
binding.listOrganizationsText.text = textToDisplay | ||
} else { | ||
binding.listOrganizationsText.visibility = View.GONE | ||
binding.noOrganizationsText.visibility = View.VISIBLE | ||
} | ||
} | ||
|
||
private fun handleBackNav() { | ||
LaoActivity.addBackNavigationCallbackToEvents(requireActivity(), viewLifecycleOwner, TAG) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like input validation, maybe you should complete MessageValidator class to check a federation status? Validation would then just be a clear oneliner 👍
You also probably want to check the public key to be non empty B64