Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.64 KB | None | 0 0
  1. import akka.actor.{Actor, ActorRef, ActorSystem, Props}
  2.  
  3. //Using Akka Actor library model following YouTube functionality: (Scala)
  4. //
  5. //There are two types of actors - "Channel" and "User".
  6. //User can subscribe to a channel and revert the subscription.
  7. //When a video is uploaded to a channel all of the subscribers are
  8. //notified about it.
  9. //
  10. //A video is identified by its title. Do not store videos on the channel - just notify about them.
  11. //Videos should be uploaded from the main thread - do not create user-owner.
  12. //
  13. //Both types of actors have to print some messages to the console to inform about actions being performed.
  14. //Define both classes and companion objects, later should define message types and the "props" methods.
  15. //
  16. //Present the functionality. Show that it works for multiple channels and users. (10 pts)
  17.  
  18.  
  19. class Channel extends Actor{
  20.   private var subscribers = List[ActorRef]()
  21.  
  22.   override def receive: Receive = {
  23.     case Channel.NewMovie(title) => {
  24.       println(s"~~Movie created $title")
  25.       subscribers.foreach(f => f ! User.Notification(title)) // make it foreach
  26.     }
  27.     case Channel.Sub(user) => {
  28.       println(s"Sub Received $user")
  29.       subscribers = user :: subscribers
  30.     }
  31.     case Channel.UnSub(user) => {
  32.       subscribers = subscribers.filter(_ != user)
  33.       println(s"UNSUBSCRIBED $user")
  34.     }
  35.   }
  36.  
  37. }
  38. object Channel {
  39.   case class NewMovie(title: String)
  40.   case class Sub(user: ActorRef)
  41.   case class UnSub(user: ActorRef)
  42.  
  43.   def props: Props = Props[Channel]
  44. }
  45.  
  46. class User(private val id: Int) extends Actor{
  47.  
  48.   override def receive: Receive = {
  49.     case User.Notification(title) => {
  50.       println(s"User $id received notification:   $title")
  51.     }
  52.   }
  53. }
  54. object User{
  55.   var id: Int = 0
  56.   case class Notification(title: String)
  57.  
  58.   def props() =
  59.   {
  60.     id += 1
  61.     Props(classOf[User], id - 1)
  62.   }
  63. }
  64.  
  65.  
  66. object MainYouTube {
  67.  
  68.   def main(args: Array[String]): Unit =
  69.   {
  70.     println("start")
  71.    
  72.     val actSys = ActorSystem()
  73.     val user1: ActorRef = actSys.actorOf(User.props)
  74.     val user2: ActorRef = actSys.actorOf(User.props)
  75.     val user3: ActorRef = actSys.actorOf(User.props)
  76.     val channel: ActorRef = actSys.actorOf(Channel.props)
  77.  
  78.     channel ! Channel.Sub(user1)
  79.     channel ! Channel.Sub(user2)
  80.     channel ! Channel.NewMovie("Kapitan Bomba odc. 140")
  81.  
  82.     channel ! Channel.Sub(user3)
  83.     channel ! Channel.NewMovie("Piesek leszek odc. 999")
  84.  
  85.     channel ! Channel.UnSub(user3)
  86.  
  87.     channel ! Channel.NewMovie("Laserowy gniew dzidy odc. 12")
  88.  
  89.    
  90.    
  91.     Thread.sleep(5000)
  92.     println("after sleep")
  93.     actSys.terminate()
  94.  
  95.  
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement