net.sourceforge.plantuml.ftp.FtpServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plantuml-mit Show documentation
Show all versions of plantuml-mit Show documentation
PlantUML is a component that allows to quickly write diagrams from text.
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
package net.sourceforge.plantuml.ftp;
// server
// FtpServer.java
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.log.Logme;
public class FtpServer {
private final Map datas = new TreeMap();
private final ExecutorService exeImage = Executors.newFixedThreadPool(2);
private final int listenPort;
private int portFree = 10042;
private String ip;
private final FileFormat defaultfileFormat;
public FtpServer(int listenPort, FileFormat defaultfileFormat) {
this.listenPort = listenPort;
this.defaultfileFormat = defaultfileFormat == null ? FileFormat.PNG : defaultfileFormat;
}
public synchronized int getFreePort() {
portFree++;
// Log.println("port=" + portFree);
return portFree;
}
public void go() throws IOException {
final ServerSocket s = new ServerSocket(listenPort);
final ExecutorService exe = Executors.newCachedThreadPool();
while (true) {
final Socket incoming = s.accept();
ip = incoming.getLocalAddress().getHostAddress();
System.out.println("New Client Connected from " + incoming.getInetAddress().getHostName() + "... ");
exe.submit(new FtpLoop(incoming, this));
}
}
public String getIpServer() {
return ip;
}
public synchronized FtpConnexion getFtpConnexion(String user) {
FtpConnexion data = datas.get(Objects.requireNonNull(user));
if (data == null) {
data = new FtpConnexion(user, defaultfileFormat);
datas.put(user, data);
}
return data;
}
public static void main(String[] args) throws IOException {
System.out.println("****************************** ************************************************** ");
System.out.println("****************************** FTP SERVER***********************************");
System.out.println("****************************** ************************************************** ");
System.out.println("Server Started...");
System.out.println("Waiting for connections...");
System.out.println(" ");
new FtpServer(4242, FileFormat.PNG).go();
}
public void processImage(final FtpConnexion connexion, final String name) {
exeImage.submit(new Runnable() {
public void run() {
try {
connexion.processImage(name);
} catch (IOException e) {
Logme.error(e);
}
}
});
}
public final String getCharset() {
return UTF_8.name();
}
}