summaryrefslogtreecommitdiff
path: root/xml/impl/src/org/jetbrains/io/fastCgi/FastCgiService.java
blob: 54f13c6c8ea10e29a69cc5fcfba9eed510eb6bd7 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package org.jetbrains.io.fastCgi;

import com.intellij.concurrency.JobScheduler;
import com.intellij.execution.filters.TextConsoleBuilder;
import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.openapi.util.AsyncValueLoader;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.ContentFactory;
import com.intellij.util.Consumer;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.StripedLockIntObjectConcurrentHashMap;
import com.intellij.util.net.NetUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.io.ChannelExceptionHandler;
import org.jetbrains.io.NettyUtil;
import org.jetbrains.io.Responses;

import javax.swing.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

// todo send FCGI_ABORT_REQUEST if client channel disconnected
public abstract class FastCgiService implements Disposable {
  static final Logger LOG = Logger.getInstance(FastCgiService.class);

  protected final Project project;

  private final AtomicInteger requestIdCounter = new AtomicInteger();
  private final StripedLockIntObjectConcurrentHashMap<Channel> requests = new StripedLockIntObjectConcurrentHashMap<Channel>();

  private volatile Channel fastCgiChannel;

  protected final AsyncValueLoader<OSProcessHandler> processHandler = new AsyncValueLoader<OSProcessHandler>() {
    @Override
    protected boolean isCancelOnReject() {
      return true;
    }

    @Override
    protected void load(@NotNull final AsyncResult<OSProcessHandler> result) throws IOException {
      final int port = NetUtils.findAvailableSocketPort();
      final OSProcessHandler processHandler = createProcessHandler(project, port);
      if (processHandler == null) {
        result.setRejected();
        return;
      }

      result.doWhenRejected(new Runnable() {
        @Override
        public void run() {
          processHandler.destroyProcess();
        }
      });

      final MyProcessAdapter processListener = new MyProcessAdapter();
      processHandler.addProcessListener(processListener);
      processHandler.startNotify();

      if (result.isRejected()) {
        return;
      }

      JobScheduler.getScheduler().schedule(new Runnable() {
        @Override
        public void run() {
          if (result.isRejected()) {
            return;
          }

          ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
            @Override
            public void run() {
              if (!result.isRejected()) {
                try {
                  connectToProcess(result, port, processHandler, processListener);
                }
                catch (Throwable e) {
                  result.setRejected();
                  LOG.error(e);
                }
              }
            }
          });
        }
      }, NettyUtil.MIN_START_TIME, TimeUnit.MILLISECONDS);
    }

    @Override
    protected void disposeResult(@NotNull OSProcessHandler processHandler) {
      try {
        Channel currentFastCgiChannel = fastCgiChannel;
        if (currentFastCgiChannel != null) {
          fastCgiChannel = null;
          NettyUtil.closeAndReleaseFactory(currentFastCgiChannel);
        }
        processHandler.destroyProcess();
      }
      finally {
        requestIdCounter.set(0);
        if (!requests.isEmpty()) {
          List<Channel> waitingClients = ContainerUtil.toList(requests.elements());
          requests.clear();
          for (Channel channel : waitingClients) {
            try {
              if (channel.isActive()) {
                Responses.sendStatus(HttpResponseStatus.BAD_GATEWAY, channel);
              }
            }
            catch (Throwable e) {
              NettyUtil.log(e, LOG);
            }
          }
        }
      }
    }
  };

  private ConsoleView console;

  protected FastCgiService(Project project) {
    this.project = project;
  }

  protected abstract OSProcessHandler createProcessHandler(Project project, int port);

  private void connectToProcess(final AsyncResult<OSProcessHandler> asyncResult, final int port, final OSProcessHandler processHandler, final Consumer<String> errorOutputConsumer) {
    Bootstrap bootstrap = NettyUtil.oioClientBootstrap();
    final FastCgiChannelHandler fastCgiChannelHandler = new FastCgiChannelHandler(requests);
    bootstrap.handler(new ChannelInitializer() {
      @Override
      protected void initChannel(Channel channel) throws Exception {
        channel.pipeline().addLast(new FastCgiDecoder(errorOutputConsumer), fastCgiChannelHandler, ChannelExceptionHandler.getInstance());
      }
    });
    fastCgiChannel = NettyUtil.connectClient(bootstrap, new InetSocketAddress(NetUtils.getLoopbackAddress(), port), asyncResult);
    if (fastCgiChannel != null) {
      asyncResult.setDone(processHandler);
    }
  }

  public void send(final FastCgiRequest fastCgiRequest, final ByteBuf content) {
    content.retain();

    if (processHandler.has()) {
      fastCgiRequest.writeToServerChannel(content, fastCgiChannel);
    }
    else {
      processHandler.get().doWhenDone(new Runnable() {
        @Override
        public void run() {
          fastCgiRequest.writeToServerChannel(content, fastCgiChannel);
        }
      }).doWhenRejected(new Runnable() {
        @Override
        public void run() {
          content.release();
          Channel channel = requests.get(fastCgiRequest.requestId);
          if (channel != null && channel.isActive()) {
            Responses.sendStatus(HttpResponseStatus.BAD_GATEWAY, channel);
          }
        }
      });
    }
  }

  public int allocateRequestId(Channel channel) {
    int requestId = requestIdCounter.getAndIncrement();
    if (requestId >= Short.MAX_VALUE) {
      requestIdCounter.set(0);
      requestId = requestIdCounter.getAndDecrement();
    }
    requests.put(requestId, channel);
    return requestId;
  }

  @Override
  public void dispose() {
    processHandler.reset();
  }

  protected abstract void buildConsole(@NotNull TextConsoleBuilder consoleBuilder);

  @NotNull
  protected abstract String getConsoleToolWindowId();

  @NotNull
  protected abstract Icon getConsoleToolWindowIcon();

  private final class MyProcessAdapter extends ProcessAdapter implements Consumer<String> {
    private void createConsole() {
      TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
      buildConsole(consoleBuilder);
      console = consoleBuilder.getConsole();

      ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
          ToolWindow toolWindow = ToolWindowManager.getInstance(project).registerToolWindow(getConsoleToolWindowId(), false, ToolWindowAnchor.BOTTOM, project, true);
          toolWindow.setIcon(getConsoleToolWindowIcon());
          toolWindow.getContentManager().addContent(ContentFactory.SERVICE.getInstance().createContent(console.getComponent(), "", false));
        }
      }, project.getDisposed());
    }

    @Override
    public void onTextAvailable(ProcessEvent event, Key outputType) {
      print(event.getText(), ConsoleViewContentType.getConsoleViewType(outputType));
    }

    private void print(String text, ConsoleViewContentType contentType) {
      if (console == null) {
        createConsole();
      }
      console.print(text, contentType);
    }

    @Override
    public void processTerminated(ProcessEvent event) {
      processHandler.reset();
      print(getConsoleToolWindowId() + " terminated\n", ConsoleViewContentType.SYSTEM_OUTPUT);
    }

    @Override
    public void consume(String message) {
      print(message, ConsoleViewContentType.ERROR_OUTPUT);
    }
  }
}