Skip to content

Commit

Permalink
[BE2] use Scala 3 compatible idioms
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Bettens committed Jun 18, 2022
1 parent 70f91c0 commit c29d897
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 57 deletions.
2 changes: 1 addition & 1 deletion be2-scala/src/main/scala/ch/epfl/pop/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.actor.{ActorRef, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{RequestContext, RouteResult}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.util.Timeout
import ch.epfl.pop.config.{RuntimeEnvironment, ServerConf}
import ch.epfl.pop.pubsub.{MessageRegistry, PubSubMediator, PublishSubscribe}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ object HighLevelProtocol extends DefaultJsonProtocol {
Broadcast(params.channel, params.message)
}

override def write(obj: Broadcast): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Broadcast): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object CatchupFormat extends RootJsonFormat[Catchup] {
override def read(json: JsValue): Catchup = Catchup(json.convertTo[Params].channel)

override def write(obj: Catchup): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Catchup): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object PublishFormat extends RootJsonFormat[Publish] {
Expand All @@ -99,19 +99,19 @@ object HighLevelProtocol extends DefaultJsonProtocol {
Publish(params.channel, params.message)
}

override def write(obj: Publish): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Publish): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object SubscribeFormat extends RootJsonFormat[Subscribe] {
override def read(json: JsValue): Subscribe = Subscribe(json.convertTo[Params].channel)

override def write(obj: Subscribe): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Subscribe): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object UnsubscribeFormat extends RootJsonFormat[Unsubscribe] {
override def read(json: JsValue): Unsubscribe = Unsubscribe(json.convertTo[Params].channel)

override def write(obj: Unsubscribe): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Unsubscribe): JsValue = obj.toJson(ParamsFormat.write _)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object ElectionChannel {
* @tparam T type of data to extract from the election channel
* @return Future of a list of tuple containing the message and the data extracted
*/
def extractMessages[T: Manifest](dbActor: AskableActorRef = DbActor.getInstance): Future[List[(Message, T)]] = {
def extractMessages[T: reflect.ClassTag](dbActor: AskableActorRef = DbActor.getInstance): Future[List[(Message, T)]] = {
for {
DbActor.DbActorCatchupAck(messages) <- dbActor ? DbActor.Catchup(channel)
result <- Future.traverse(messages.flatMap(message =>
Expand Down
4 changes: 2 additions & 2 deletions be2-scala/src/main/scala/ch/epfl/pop/pubsub/ClientActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.epfl.pop.pubsub

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import akka.event.LoggingReceive
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import ch.epfl.pop.model.objects.Channel
import ch.epfl.pop.pubsub.ClientActor._
import ch.epfl.pop.pubsub.PubSubMediator._
Expand Down Expand Up @@ -93,7 +93,7 @@ object ClientActor {
final case class ConnectWsHandle(wsClient: ActorRef) extends Event

// unsubscribe from all channels
final case object DisconnectWsHandle extends Event
case object DisconnectWsHandle extends Event

// subscribe to a particular channel
final case class SubscribeTo(channel: Channel) extends Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Answerer {
// Send the ClientAnswer to clientActorRef. Whenever the stream between the client
// actor and the actual client (front-end) is broken, the message DisconnectWsHandle
// is sent to clientActorRef
.to(Sink.actorRef(clientActorRef, DisconnectWsHandle, { t: Throwable => println(t); DisconnectWsHandle }))
.to(Sink.actorRef(clientActorRef, DisconnectWsHandle, { (t: Throwable) => println(t); DisconnectWsHandle }))

// Integration point between Akka Streams and above actor
val source: Source[TextMessage, NotUsed] = Source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ case object LaoHandler extends MessageHandler {
_ <- dbActor ? DbActor.WriteLaoData(laoChannel, message, address)
//after creating the lao, we need to send a lao#greet message to the frontend
greet: GreetLao = GreetLao(data.id, params.get.sender, address.get, List.empty)
broadcastGreet: Base64Data = Base64Data.encode(GreetLaoFormat.write(greet).toString())
broadcastGreet: Base64Data = Base64Data.encode(GreetLaoFormat.write(greet).toString)
_ <- dbBroadcast(rpcMessage, laoChannel, broadcastGreet, laoChannel)
} yield ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.epfl.pop.pubsub.graph.handlers

import akka.NotUsed
import akka.actor.ActorRef
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.stream.FlowShape
import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Partition}
import ch.epfl.pop.model.network.method.{Catchup, Subscribe, Unsubscribe}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class RollCallHandler(dbRef: => AskableActorRef) extends MessageHandler {
))
}
case error@Right(_) => error
case _ => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswer, rpcRequest.id))
case null => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswer, rpcRequest.id))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SocialMediaHandler(dbRef: => AskableActorRef) extends MessageHandler {
case None => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, "Server failed to extract LAO id for the broadcast", rpcMessage.id))
}
case error@Right(_) => error
case _ => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
case null => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
}
}

Expand All @@ -81,7 +81,7 @@ class SocialMediaHandler(dbRef: => AskableActorRef) extends MessageHandler {
case None => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, "Server failed to extract LAO id for the broadcast", rpcMessage.id))
}
case error@Right(_) => error
case _ => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
case null => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class ChannelSuite extends AnyFunSuite with should.Matchers {
def channel = Channel("/root/full/pop")

val expected = Hash(Base64Data("pop"))
an[IllegalArgumentException] shouldNot be(thrownBy(channel))
channel.extractChildChannel should equal(expected)
}
test("Encoded LaoId extraction channel test") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.network.method.message.Message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.handlers

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.network.method.message.Message
Expand Down Expand Up @@ -36,12 +36,12 @@ class ElectionHandlerTest extends TestKit(ActorSystem("Election-DB-System")) wit
private val keyPair: KeyPair = KeyPair()
private val electionData: ElectionData = ElectionData(Hash(Base64Data.encode("election")), keyPair)

private final val sender: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION
private final val SENDER: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_OWNER: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)

private final val channelDataWithSetupAndOpenAndCastMessage: ChannelData = ChannelData(ObjectType.ELECTION, List(DATA_CAST_VOTE_MESSAGE, DATA_SET_UP_OPEN_BALLOT, DATA_OPEN_MESSAGE))
private final val messages: List[Message] = List(MESSAGE_CAST_VOTE_ELECTION_WORKING, MESSAGE_SETUPELECTION_OPEN_BALLOT_WORKING, MESSAGE_OPEN_ELECTION_WORKING, MESSAGE_END_ELECTION_WORKING)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.handlers

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.objects.DbActorNAckException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.handlers

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.objects.DbActorNAckException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down Expand Up @@ -46,12 +46,12 @@ class ElectionValidatorSuite extends TestKit(ActorSystem("electionValidatorTestA
deleteRecursively(directory)
}

private final val sender: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION
private final val SENDER: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_WRONG: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(PK_WRONG, List(PK_WRONG), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val channelDataRightSetup: ChannelData = ChannelData(ObjectType.LAO, List.empty)
private final val channelDataWrongSetup: ChannelData = ChannelData(ObjectType.ELECTION, List.empty)
Expand Down Expand Up @@ -433,7 +433,7 @@ class ElectionValidatorSuite extends TestKit(ActorSystem("electionValidatorTestA
test("Open up an election with invalid lao id fails") {
val dbActorRef = mockDbWorking
val message: GraphMessage = new ElectionValidator(dbActorRef).validateOpenElection(OPEN_ELECTION_WRONG_LAO_ID_RPC)
val messageStandardActor: GraphMessage = ElectionValidator.validateOpenElection(OPEN_ELECTION_WRONG_LAO_ID_RPC)
val messageStandardActor: GraphMessage = ElectionValidator.validateOpenElection(OPEN_ELECTION_WRONG_LAO_ID_RPC)
message shouldBe a[Right[_, PipelineError]]
messageStandardActor shouldBe a[Right[_, PipelineError]]
system.stop(dbActorRef.actorRef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
import ch.epfl.pop.model.network.method.message.data.ObjectType
import ch.epfl.pop.model.objects.{Base64Data, ChannelData, LaoData, PrivateKey, PublicKey}
import ch.epfl.pop.storage.{DbActor, InMemoryStorage}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.pubsub.graph.{GraphMessage, PipelineError}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down Expand Up @@ -46,12 +46,12 @@ class MertingValidatorSuite extends TestKit(ActorSystem("meetingValidatorTestAct
deleteRecursively(directory)
}

private final val sender: PublicKey = PublicKey(Base64Data("J9fBzJV70Jk5c-i3277Uq4CmeL4t53WDfUghaK0HpeM="))
private final val SENDER: PublicKey = PublicKey(Base64Data("J9fBzJV70Jk5c-i3277Uq4CmeL4t53WDfUghaK0HpeM="))

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_OWNER: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(PK_OWNER, List(PK_OWNER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val channelDataRightSetup: ChannelData = ChannelData(ObjectType.LAO, List.empty)
private final val channelDataWrongSetup: ChannelData = ChannelData(ObjectType.ELECTION, List.empty)
Expand Down Expand Up @@ -255,4 +255,4 @@ class MertingValidatorSuite extends TestKit(ActorSystem("meetingValidatorTestAct
messageStandardActor shouldBe a[Right[_, PipelineError]]
system.stop(dbActorRef.actorRef)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.network.method.message.data.ObjectType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down Expand Up @@ -46,13 +46,11 @@ class RollCallValidatorSuite extends TestKit(ActorSystem("rollcallValidatorTestA
deleteRecursively(directory)
}

private final val sender: PublicKey = SENDER

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_OWNER: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(sender, List(PK_OWNER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(SENDER, List(PK_OWNER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val channelDataWrong: ChannelData = ChannelData(ObjectType.INVALID, List.empty)
private final val channelDataRight: ChannelData = ChannelData(ObjectType.LAO, List.empty)
private final val rollcallDataCreate: RollCallData = RollCallData(CreateRollCallExamples.R_ID, ActionType.CREATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.File
import java.util.concurrent.TimeUnit

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.storage

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import ch.epfl.pop.model.network.method.message.Message
import ch.epfl.pop.model.network.method.message.data.ActionType.{ActionType, CREATE}
Expand Down
Loading

0 comments on commit c29d897

Please sign in to comment.