Esempio n. 1
0
 def remove(user: User):
     if OnlineUsers.has(user):
         # Remove the user from OnlineUsers list first,
         # so that the server will not dispatch this event to
         # the logging out user whose SocketServer
         # has already shutdown.
         OnlineUsers.users.remove(user)
         dispatch(LogoutEvent(user), OnlineUsers.users)
     else:
         raise NotLoggedIn("This user has already logged out.")
Esempio n. 2
0
 def add(user: User):
     if not OnlineUsers.has(user):
         # The SocketServer which listens for events will start
         # only after the user has SUCCESSFULLY logged in!
         # Hence, the order of the following two lines of code
         # DOES matter. If we reverse their order, the dispatcher
         # will get a connection refused error
         # (since the SocketServer has not yet started) :)
         dispatch(LoginEvent(user), OnlineUsers.users)
         OnlineUsers.users.append(user)
     else:
         raise AlreadyLoggedIn("This user has already logged in.")
Esempio n. 3
0
    def remove(exam: Exam):
        if OngoingExams.has(exam):
            # Stop the timer of this exam.
            OngoingExams.exams[exam].timer.cancel()

            # Remove the element from the dict.
            del OngoingExams.exams[exam]

            # Notify all users that the exam has been halt.
            dispatch(HaltExamEvent(exam), OnlineUsers.users)
        else:
            raise NotLaunched(
                "This exam has not been launched or already ended.")
Esempio n. 4
0
 def remove(self, user: User):
     # Once a student left the exam, the student will not be able
     # to attend the exam again, unless the exam is re-launched.
     # A teacher can enter and leave as he/she wishes.
     if self.has(user):
         if not user.is_student:
             self.teachers.remove(user)
         else:
             # Notify all teachers that a student has left the exam,
             # and that they will not receive snapshot from that student any longer.
             dispatch(LeaveExamEvent(self.exam, user), self.teachers)
     else:
         raise NotAttended("This user has not attended to this exam.")
Esempio n. 5
0
    def add(self, user: User):
        if not self.has(user):
            if user.is_student:
                self.students.append(user)

                # Notify all teachers that a student has attend to the exam,
                # and that they should prepare for receiving snapshot from that student.
                dispatch(AttendExamEvent(self.exam, user), self.teachers)
            else:
                self.teachers.append(user)
        else:
            raise AlreadyAttended(
                "This user has already attended to this exam.")
Esempio n. 6
0
    def add(exam: Exam):
        if not OngoingExams.has(exam):
            # Create an empty student list for this exam.
            OngoingExams.exams[exam] = ExamData(exam)
            # Start the timer of this exam.
            # Also record the start_time of timer for evaluating remaining time later.
            OngoingExams.exams[exam].timer = Timer(exam.duration * 60,
                                                   OngoingExams.remove,
                                                   args=[exam])
            OngoingExams.exams[exam].timer.start()
            OngoingExams.exams[exam].start_time = time.time()

            # Notify all users that the exam has been launched.
            dispatch(LaunchExamEvent(exam), OnlineUsers.users)
        else:
            raise AlreadyLaunched("This exam has already been launched.")