$darkmode
DENOPTIM
Py4JGetawayServer.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.integration.python;
20import java.net.BindException;
21
22import denoptim.exception.DENOPTIMException;
23import denoptim.io.DenoptimIO;
24import py4j.GatewayServer;
25import py4j.Py4JNetworkException;
26
36{
40 private GatewayServer server;
41
45 private String pathname = null;
46
50 private Object data = null;
51
52//------------------------------------------------------------------------------
53
60 public static void launch() throws DENOPTIMException
61 {
62 Py4JGetawayServer launcher = new Py4JGetawayServer();
63 GatewayServer gatewayServer = new GatewayServer(launcher);
64 try
65 {
66 gatewayServer.start();
67 } catch (Py4JNetworkException e)
68 {
69 if (e.getCause() instanceof BindException
70 && e.getCause().getMessage().contains("already in use"))
71 throw new DENOPTIMException("DENOPTIM Py4J server already "
72 + "running!",e);
73 }
74 launcher.setServer(gatewayServer);
75 }
76
77//------------------------------------------------------------------------------
78
85 public Object loadData(String pathname) throws Exception
86 {
87 this.pathname = pathname;
89 return data;
90 }
91
92//------------------------------------------------------------------------------
93
97 public Object getData()
98 {
99 return data;
100 }
101
102//------------------------------------------------------------------------------
103
108 public String getSourcePathName()
109 {
110 return pathname;
111 }
112
113//------------------------------------------------------------------------------
114
118 public void shutdown()
119 {
120 if (server != null)
121 server.shutdown();
122 }
123
124//------------------------------------------------------------------------------
125
130 private void setServer(GatewayServer server)
131 {
132 this.server = server;
133 }
134
135//------------------------------------------------------------------------------
136
137}
A tool that start a Py4J gateway server that can listens to calls from Python and translate JAVA obje...
void setServer(GatewayServer server)
Sets the reference to the launched server.
Object loadData(String pathname)
Reads any DENOPTIM-kind of data from the given pathname.
String pathname
Pathname from which data was loaded, or null if no data has been loaded.
Object data
Any DENOPTIM-related data loaded from file, if any.
static void launch()
Starts a gateway server using this class as entry point, which then becomes the interpreter of any da...
GatewayServer server
The actual server we launch, or null if not launched yet.
Utility methods for input/output.
static Object readDENOPTIMData(String pathname)
Reads any content of a given pathname and tries to read DENOPTIM data from it.