- val userId = event.userID
- // FIXME: Implement
-// self reply GetUserStateResponse(userId, Right(this._userState))
- }
-
- def onProcessResourceEvent(event: ProcessResourceEvent): Unit = {
- val rcEvent = event.rcEvent
-
- logger.info("Got\n{}", rcEvent.toJsonString)
- }
-
-
-// private[this] def D_userID = {
-// if(this._newUserState eq null)
-// if(this._imState eq null)
-// "<NOT INITIALIZED>"
-// else
-// this._imState.latestIMEvent.userID
-// else
-// this._newUserState.userID
-// }
-//
-// private[this] def DEBUG(fmt: String, args: Any*) =
-// logger.debug("UserActor[%s]: %s".format(D_userID, fmt.format(args: _*)))
-//
-// private[this] def INFO(fmt: String, args: Any*) =
-// logger.info("UserActor[%s]: %s".format(D_userID, fmt.format(args: _*)))
-//
-// private[this] def WARN(fmt: String, args: Any*) =
-// logger.warn("UserActor[%s]: %s".format(D_userID, fmt.format(args: _*)))
-//
-// private[this] def ERROR(fmt: String, args: Any*) =
-// logger.error("UserActor[%s]: %s".format(D_userID, fmt.format(args: _*)))
-//
-// private[this] def ERROR(t: Throwable, fmt: String, args: Any*) =
-// logger.error("UserActor[%s]: %s".format(D_userID, fmt.format(args: _*)), t)
+ checkInitial()
+
+ haveUserState match {
+ case true ⇒
+ val realtimeMillis = TimeHelpers.nowMillis()
+ chargingService.calculateRealtimeUserState(
+ this._userStateModel,
+ aquarium.currentResourceMapping,
+ realtimeMillis
+ )
+
+ sender ! GetUserStateResponse(Right(this._userStateModel.userStateMsg))
+
+ case false ⇒
+ sender ! GetUserStateResponse(Left("No state for user %s [AQU-STA-0006]".format(event.userID)), 404)
+ }
+ }
+
+ def onGetUserWalletRequest(event: GetUserWalletRequest): Unit = {
+ checkInitial()
+
+ haveUserState match {
+ case true ⇒
+ DEBUG("haveWorkingUserState: %s", event)
+ val realtimeMillis = TimeHelpers.nowMillis()
+ chargingService.calculateRealtimeUserState(
+ this._userStateModel,
+ aquarium.currentResourceMapping,
+ realtimeMillis
+ )
+
+ sender ! GetUserWalletResponse(
+ Right(
+ GetUserWalletResponseData(
+ this.userID,
+ this._userStateModel.totalCredits,
+ MessageFactory.newWalletEntriesMsg(this._userStateModel.userStateMsg.getWalletEntries)
+ )))
+
+ case false ⇒
+ DEBUG("!haveWorkingUserState: %s", event)
+ haveAgreements match {
+ case true ⇒
+ DEBUG("haveAgreements: %s", event)
+ sender ! GetUserWalletResponse(
+ Right(
+ GetUserWalletResponseData(
+ this.userID,
+ Real.toMsgField(aquarium.initialUserBalance(this.unsafeUserCreationIMEventMsg.getRole, this.unsafeUserCreationIMEventMsg.getOccurredMillis)),
+ MessageFactory.newWalletEntriesMsg()
+ )))
+
+ case false ⇒
+ DEBUG("!haveUserCreationIMEvent: %s", event)
+ sender ! GetUserWalletResponse(Left("No wallet for user %s [AQU-WAL-00 8]".format(event.userID)), 404)
+ }
+ }
+ }
+
+ /**
+ * Initializes the actor's internal state.
+ *
+ * @param userID
+ */
+ def onSetUserActorUserID(userID: String) {
+ // Create the full agreement history from the original sources (IMEvents)
+ this._userStateModel = ModelFactory.newInitialUserStateModel(
+ userID,
+ Real(0),
+ TimeHelpers.nowMillis()
+ )
+
+ require(this._userStateModel.isInitial, "this._userStateModel.isInitial")
+ }
+
+ private[this] def D_userID = {
+ if(haveUserState) userID else "???"
+ }
+
+ private[this] def DEBUG(fmt: String, args: Any*) =
+ logger.debug("[%s] - %s".format(D_userID, fmt.format(args: _*)))
+
+ private[this] def INFO(fmt: String, args: Any*) =
+ logger.info("[%s] - %s".format(D_userID, fmt.format(args: _*)))
+
+ private[this] def WARN(fmt: String, args: Any*) =
+ logger.warn("[%s] - %s".format(D_userID, fmt.format(args: _*)))
+
+ private[this] def ERROR(fmt: String, args: Any*) =
+ logger.error("[%s] - %s".format(D_userID, fmt.format(args: _*)))
+
+ private[this] def ERROR(t: Throwable, fmt: String, args: Any*) =
+ logger.error("[%s] - %s".format(D_userID, fmt.format(args: _*)), t)