summaryrefslogtreecommitdiff
path: root/platform/remote-servers/impl/src/com/intellij/remoteServer/impl/runtime/ServerConnectionManagerImpl.java
blob: f12e646a72b9b332d2007d71890d14560d90bf08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.intellij.remoteServer.impl.runtime;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.remoteServer.configuration.RemoteServer;
import com.intellij.remoteServer.configuration.ServerConfiguration;
import com.intellij.remoteServer.runtime.ServerConnection;
import com.intellij.remoteServer.runtime.ServerConnectionManager;
import com.intellij.util.Alarm;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author nik
 */
public class ServerConnectionManagerImpl extends ServerConnectionManager {

  private static final int POLL_DEPLOYMENTS_DELAY = 2000;

  private final Map<RemoteServer<?>, ServerConnection> myConnections = new HashMap<RemoteServer<?>, ServerConnection>();
  private final ServerConnectionEventDispatcher myEventDispatcher = new ServerConnectionEventDispatcher();

  @NotNull
  @Override
  public <C extends ServerConfiguration> ServerConnection getOrCreateConnection(@NotNull RemoteServer<C> server) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    ServerConnection connection = myConnections.get(server);
    if (connection == null) {
      connection = doCreateConnection(server, this);
      myConnections.put(server, connection);
      myEventDispatcher.fireConnectionCreated(connection);
      pollDeployments(connection);
    }
    return connection;
  }

  private void pollDeployments(final ServerConnection connection) {
    connection.computeDeployments(new Runnable() {

      @Override
      public void run() {
        new Alarm().addRequest(new Runnable() {

          @Override
          public void run() {
            if (connection == getConnection(connection.getServer())) {
              pollDeployments(connection);
            }
          }
        }, POLL_DEPLOYMENTS_DELAY, ModalityState.any());
      }
    });
  }

  @NotNull
  @Override
  public <C extends ServerConfiguration> ServerConnection createTemporaryConnection(@NotNull RemoteServer<C> server) {
    return doCreateConnection(server, null);
  }

  private <C extends ServerConfiguration> ServerConnection doCreateConnection(@NotNull RemoteServer<C> server,
                                                                              ServerConnectionManagerImpl manager) {
    ServerTaskExecutorImpl executor = new ServerTaskExecutorImpl();
    return new ServerConnectionImpl(server, server.getType().createConnector(server, executor), manager, getEventDispatcher());
  }

  @Nullable
  @Override
  public <C extends ServerConfiguration> ServerConnection getConnection(@NotNull RemoteServer<C> server) {
    return myConnections.get(server);
  }

  public void removeConnection(RemoteServer<?> server) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    myConnections.remove(server);
  }

  public ServerConnectionEventDispatcher getEventDispatcher() {
    return myEventDispatcher;
  }

  @NotNull
  @Override
  public Collection<ServerConnection> getConnections() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    return Collections.unmodifiableCollection(myConnections.values());
  }
}