Skip to content

Commit a92e84f

Browse files
committed
python: forward console ports to the machine of a remote interpreter
1 parent 3d4421f commit a92e84f

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

‎python-impl/src/main/java/com/jetbrains/python/impl/console/PydevConsoleRunnerImpl.java‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import consulo.localize.LocalizeValue;
6464
import consulo.logging.Logger;
6565
import consulo.module.Module;
66+
import consulo.platform.ForwardedPort;
67+
import consulo.platform.Platform;
6668
import consulo.platform.base.icon.PlatformIconGroup;
6769
import consulo.process.ExecutionException;
6870
import consulo.process.KillableProcessHandler;
@@ -108,6 +110,7 @@
108110
import java.util.Collections;
109111
import java.util.List;
110112
import java.util.Map;
113+
import java.util.concurrent.CopyOnWriteArrayList;
111114
import java.util.function.Consumer;
112115
import java.util.stream.Collectors;
113116

@@ -133,6 +136,8 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner {
133136
private Sdk mySdk;
134137
private GeneralCommandLine myGeneralCommandLine;
135138
protected int[] myPorts;
139+
/** The ports leading between the IDE and the machine of a remote interpreter - closed with the console's process. */
140+
private final List<ForwardedPort> myForwardedPorts = new CopyOnWriteArrayList<>();
136141
private PydevConsoleCommunication myPydevConsoleCommunication;
137142
private ProcessHandler myProcessHandler;
138143
protected PydevConsoleExecuteActionHandler myConsoleExecuteActionHandler;
@@ -372,13 +377,36 @@ protected GeneralCommandLine doCreateConsoleCmdLine(
372377
ParamsGroup group = cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
373378
helper.addToGroup(group, cmd);
374379

375-
for (int port : ports) {
380+
for (int port : portsOnMachine(sdk, ports)) {
376381
group.addParameter(String.valueOf(port));
377382
}
378383

379384
return cmd;
380385
}
381386

387+
/**
388+
* The ports the console process is given: its own server port, and the client port it calls the IDE back on. On
389+
* another machine the client port is one published there, leading to the IDE's port; the server port is the
390+
* machine's own - the IDE reaches it through a forwarded one, see {@link #createProcess()}.
391+
*/
392+
private int[] portsOnMachine(Sdk sdk, int[] ports) {
393+
Platform platform = sdk.getPlatform();
394+
if (platform.equals(Platform.current()) || ports.length < 2) {
395+
return ports;
396+
}
397+
try {
398+
ForwardedPort published = platform.net().publish(ports[1]);
399+
myForwardedPorts.add(published);
400+
int[] machinePorts = ports.clone();
401+
machinePorts[1] = published.port();
402+
return machinePorts;
403+
}
404+
catch (IOException e) {
405+
LOG.warn("Cannot publish the console port " + ports[1] + " on " + platform, e);
406+
return ports;
407+
}
408+
}
409+
382410
private PythonConsoleView createConsoleView() {
383411
PythonConsoleView consoleView = new PythonConsoleView(myProject, myTitle, mySdk);
384412
myPydevConsoleCommunication.setConsoleFile(consoleView.getVirtualFile());
@@ -392,15 +420,40 @@ private ProcessHandler createProcess() throws ExecutionException {
392420
EncodingEnvironmentUtil.setLocaleEnvironmentIfMac(envs, myGeneralCommandLine.getCharset());
393421

394422
ProcessHandler handler = PythonProcessHandler.createProcessHandler(myGeneralCommandLine);
423+
handler.addProcessListener(new ProcessAdapter() {
424+
@Override
425+
public void processTerminated(ProcessEvent event) {
426+
closeForwardedPorts();
427+
}
428+
});
395429
try {
396-
myPydevConsoleCommunication = new PydevConsoleCommunication(myProject, myPorts[0], handler, myPorts[1]);
430+
myPydevConsoleCommunication = new PydevConsoleCommunication(myProject, serverPortOnIde(), handler, myPorts[1]);
397431
}
398432
catch (Exception e) {
433+
closeForwardedPorts();
399434
throw new ExecutionException(e.getMessage());
400435
}
401436
return handler;
402437
}
403438

439+
/** The port the IDE reaches the console's server on - forwarded to the machine of a remote interpreter. */
440+
private int serverPortOnIde() throws IOException {
441+
Platform platform = mySdk.getPlatform();
442+
if (platform.equals(Platform.current())) {
443+
return myPorts[0];
444+
}
445+
ForwardedPort forwarded = platform.net().forward("127.0.0.1", myPorts[0]);
446+
myForwardedPorts.add(forwarded);
447+
return forwarded.port();
448+
}
449+
450+
private void closeForwardedPorts() {
451+
for (ForwardedPort port : myForwardedPorts) {
452+
port.close();
453+
}
454+
myForwardedPorts.clear();
455+
}
456+
404457
private ProcessHandler createProcessHandler(ProcessHandler process) {
405458
myProcessHandler = new PyConsoleProcessHandler(process, myConsoleView, myPydevConsoleCommunication);
406459
return myProcessHandler;

0 commit comments

Comments
 (0)