List<RecordedEvent> deserializeRecordedEvents(String path) { List<RecordedEvent> events = new ArrayList<>(); createInputEventClassMap(); JsonObject jsonObject; try (FileReader fileReader = new FileReader(path)) { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(fileReader); jsonObject = jsonElement.getAsJsonObject(); JsonArray jsonEvents = jsonObject.getAsJsonArray("events"); for (JsonElement element : jsonEvents) { jsonObject = element.getAsJsonObject(); String className = jsonObject.get("event_class").getAsString(); long refId = jsonObject.get("entityRef_ID").getAsLong(); long index = jsonObject.get("index").getAsLong(); long timestamp = jsonObject.get("timestamp").getAsLong(); Event event = deserializeSpecificEventData(jsonObject.get("event_data").getAsJsonObject(), className); RecordedEvent re = new RecordedEvent(refId, event, timestamp, index); events.add(re); } } catch (Exception e) { logger.error("Error while deserializing event:", e); } return events; }
/** * Try to process recorded events for 'maxDuration' miliseconds. Events are only processed if the time is right. * @param maxDuration the amount of time in which this method will try to process recorded events in one go. */ private void processRecordedEventsBatch(long maxDuration) { long beginTime = System.currentTimeMillis(); for (RecordedEvent re = recordedEvents.peek(); re != null; re = recordedEvents.peek()) { long passedTime = System.currentTimeMillis() - this.replayEventsLoadTime; //Waits until the time of reproduction is right or until 'maxDuration' miliseconds have already passed since this method was called while (passedTime < re.getTimestamp()) { passedTime = System.currentTimeMillis() - this.replayEventsLoadTime; if ((System.currentTimeMillis() - beginTime) >= maxDuration) { return; } } recordedEvents.poll(); EntityRef entity = getEntityRef(re); // Sends recorded event to be processed if (re.getComponent() != null) { originalSend(entity, re.getEvent(), re.getComponent()); } else { originalSend(entity, re.getEvent()); } this.lastRecordedEventIndex = re.getIndex(); // Check if time is up. if ((System.currentTimeMillis() - beginTime) >= maxDuration) { return; } } }
void serializeRecordedEvents(List<RecordedEvent> events, String filePath) { try { JsonWriter writer = new JsonWriter(new FileWriter(filePath)); writer.beginObject(); writer.name("events"); writer.beginArray(); for (RecordedEvent event : events) { writer.beginObject(); writer.name("entityRef_ID").value(event.getEntityId()); writer.name("timestamp").value(event.getTimestamp()); writer.name("index").value(event.getIndex()); writer.name("event_class").value(event.getEvent().getClass().getName()); writer.name("event_data"); writer.beginObject(); writeSpecificEventData(writer, event.getEvent()); writer.endObject(); writer.endObject(); } writer.endArray(); writer.endObject(); writer.close(); } catch (Exception e) { logger.error("Error while serializing events:", e); } }
/** * Since only the EntityRef's id is saved in the RecordedEvent, it is necessary to get the real EntityRef when * processing a RecordedEvent. * @param recordedEvent The recorded event containing the ID of the entity to be gotten. * @return the EntityRef with the id recorded in the RecordedEvent. */ private EntityRef getEntityRef(RecordedEvent recordedEvent) { return this.entityManager.getEntity(recordedEvent.getEntityId()); }
/** * Receives a PendingEvent and add it as a RecordedEvent in the RecordedEventStore if it is an event type that should be * recorded. * @param pendingEvent PendingEvent to be checked and added * @return If the event was added to the RecordedEventStore */ public boolean addEvent(PendingEvent pendingEvent) { if (shouldRecordEvent(pendingEvent)) { long timestamp = System.currentTimeMillis() - this.startTime; Event e = this.eventCopier.copyEvent(pendingEvent.getEvent()); PendingEvent newPendingEvent = new PendingEvent(pendingEvent.getEntity(), e); RecordedEvent recordedEvent; if (pendingEvent.getComponent() == null) { recordedEvent = new RecordedEvent(newPendingEvent.getEntity().getId(), newPendingEvent.getEvent(), timestamp, this.eventCounter); } else { recordedEvent = new RecordedEvent(newPendingEvent.getEntity().getId(), newPendingEvent.getEvent(), newPendingEvent.getComponent(), timestamp, eventCounter); } this.eventCounter++; return recordedEventStore.add(recordedEvent); } else { return false; } }
entity = entityManager.create(); Long id = entity.getId(); eventStore.add(new RecordedEvent(id, new AttackButton(), 1, 1)); eventStore.add(new RecordedEvent(id, new AttackButton(), 2, 2)); eventStore.add(new RecordedEvent(id, new AttackButton(), 3, 3));