package de.dhbwstuttgart.server.packet;

import com.fasterxml.jackson.annotation.JsonIgnore;
import de.dhbwstuttgart.server.SocketClient;
import de.dhbwstuttgart.server.SocketFuture;
import de.dhbwstuttgart.server.SocketServer;
import org.java_websocket.WebSocket;

/**
 * Normally, a connection stays open until either the client or the server process ends.
 * Send this packet to inform the server that the connection can be closed once all tasks are done
 */
public class SetAutoclosePacket implements IClientToServerPacket.Void {

  public int dummyProperty = 1;

  @JsonIgnore
  public static SetAutoclosePacket create() {
    return new SetAutoclosePacket();
  }

  @JsonIgnore
  public void onHandle(WebSocket webSocket, SocketServer socketServer) {
    webSocket.<SocketServer.SocketData>getAttachment().closeIfNoTasksLeft = true;
    socketServer.log(webSocket, "Marked connection as autoclose");
  }

  @JsonIgnore
  public SocketFuture<IServerToClientPacket> getFuture() {
    return SocketFuture.completedFuture();
  }
}
