diff --git a/src/main/java/ru/molokoin/Connection.java b/src/main/java/ru/molokoin/Connection.java index c630449..d15232f 100644 --- a/src/main/java/ru/molokoin/Connection.java +++ b/src/main/java/ru/molokoin/Connection.java @@ -1,21 +1,58 @@ package ru.molokoin; +import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; import java.nio.channels.SocketChannel; +import java.nio.file.Path; +import java.nio.file.Paths; public class Connection { public SocketChannel accepted = null; public Connection(){} public Connection(SocketChannel accepted){ this.accepted = accepted; + this.put(); } //читаем из подключения - public void read(ByteBuffer byteBuffer){ + public void read(ByteBuffer byteBuffer) throws IOException{ // + accepted.read(byteBuffer); } //пишем в подключение - public void write(ByteBuffer byteBuffer){ + public void write(ByteBuffer byteBuffer) throws IOException{ // + accepted.write(byteBuffer); + } + //пишем страничку по умолчанию + public void put(){ + //accepted.write(null); + RandomAccessFile aFile; + try { + String stringPath = "src/main/webapp/upload.http"; + Path path = Paths.get(stringPath); + aFile = new RandomAccessFile(path.toAbsolutePath().toString(), "rw"); + FileChannel inChannel = aFile.getChannel(); + ByteBuffer buf = ByteBuffer.allocate(24); + int bytesRead; + bytesRead = inChannel.read(buf); + while (bytesRead != -1){ + System.out.println("Read " + bytesRead); + buf.flip(); + while(buf.hasRemaining()){ + //System.out.print((char) buf.get()); + accepted.write(buf); + } + buf.clear(); + bytesRead = inChannel.read(buf); + } + aFile.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/ru/molokoin/ExchangeServer.java b/src/main/java/ru/molokoin/ExchangeServer.java index 65ee20c..84a6a55 100644 --- a/src/main/java/ru/molokoin/ExchangeServer.java +++ b/src/main/java/ru/molokoin/ExchangeServer.java @@ -1,7 +1,8 @@ package ru.molokoin; public class ExchangeServer { - public static Thread server = new Thread(new ServerLauncher()); + private static ServerLauncher launch = new ServerLauncher(); + public static Thread server = new Thread(launch); //Запуск сервера public void enable(ExchangeServer server){ diff --git a/src/main/java/ru/molokoin/ServerLauncher.java b/src/main/java/ru/molokoin/ServerLauncher.java index d9a550f..cd4d9f0 100644 --- a/src/main/java/ru/molokoin/ServerLauncher.java +++ b/src/main/java/ru/molokoin/ServerLauncher.java @@ -11,9 +11,10 @@ import java.util.Queue; * Подключения ставятся в очередь */ public class ServerLauncher implements Runnable{ - private int tcpPort = 0; + private int tcpPort = 8081; private ServerSocketChannel channel = null; private Queue connectionQueue = null; + private boolean isActive = false;//флаг проверки, запущен ли сервер @Override public void run() { diff --git a/target/classes/ru/molokoin/Connection.class b/target/classes/ru/molokoin/Connection.class index 33c8a54..a167da8 100644 Binary files a/target/classes/ru/molokoin/Connection.class and b/target/classes/ru/molokoin/Connection.class differ diff --git a/target/classes/ru/molokoin/ExchangeServer.class b/target/classes/ru/molokoin/ExchangeServer.class index d71d0c3..7479344 100644 Binary files a/target/classes/ru/molokoin/ExchangeServer.class and b/target/classes/ru/molokoin/ExchangeServer.class differ diff --git a/target/classes/ru/molokoin/ServerLauncher.class b/target/classes/ru/molokoin/ServerLauncher.class index 92ad0c1..db3190a 100644 Binary files a/target/classes/ru/molokoin/ServerLauncher.class and b/target/classes/ru/molokoin/ServerLauncher.class differ