diff --git a/src/main/java/ru/molokoin/Connection.java b/src/main/java/ru/molokoin/Connection.java index d15232f..118031b 100644 --- a/src/main/java/ru/molokoin/Connection.java +++ b/src/main/java/ru/molokoin/Connection.java @@ -13,20 +13,32 @@ public class Connection { public Connection(){} public Connection(SocketChannel accepted){ this.accepted = accepted; - this.put(); + this.write(); + this.read(); } //читаем из подключения - public void read(ByteBuffer byteBuffer) throws IOException{ - // - accepted.read(byteBuffer); + public void read(){ + try{ + RandomAccessFile aFile; + ByteBuffer buf = ByteBuffer.allocate(1024); + int bytesRead = accepted.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 = accepted.read(buf); + } + } + catch (IOException e) { + e.printStackTrace(); + } } //пишем в подключение - public void write(ByteBuffer byteBuffer) throws IOException{ - // - accepted.write(byteBuffer); - } - //пишем страничку по умолчанию - public void put(){ + public void write(){ //accepted.write(null); RandomAccessFile aFile; try { @@ -34,11 +46,11 @@ public class Connection { Path path = Paths.get(stringPath); aFile = new RandomAccessFile(path.toAbsolutePath().toString(), "rw"); FileChannel inChannel = aFile.getChannel(); - ByteBuffer buf = ByteBuffer.allocate(24); + ByteBuffer buf = ByteBuffer.allocate(1024); int bytesRead; bytesRead = inChannel.read(buf); while (bytesRead != -1){ - System.out.println("Read " + bytesRead); + System.out.println("Write " + bytesRead); buf.flip(); while(buf.hasRemaining()){ //System.out.print((char) buf.get()); @@ -52,7 +64,5 @@ public class Connection { catch (IOException e) { e.printStackTrace(); } - } - } diff --git a/src/main/java/ru/molokoin/ExchangeServer.java b/src/main/java/ru/molokoin/ExchangeServer.java index f4aa73a..fb9df17 100644 --- a/src/main/java/ru/molokoin/ExchangeServer.java +++ b/src/main/java/ru/molokoin/ExchangeServer.java @@ -4,8 +4,9 @@ import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; public class ExchangeServer { - private static ServerLauncher launch; + static ServerLauncher launch; public static Thread server; + static Queue connectionQueue; //Запуск сервера public void enable(ExchangeServer server){ @@ -16,7 +17,7 @@ public class ExchangeServer { // } public static void main(String[] args) { - Queue connectionQueue = (Queue) new ArrayBlockingQueue(1024); + connectionQueue = (Queue) new ArrayBlockingQueue(1024); launch = new ServerLauncher(8081, connectionQueue); server = new Thread(launch); server.setName("ServerLauncherThread"); diff --git a/src/main/java/ru/molokoin/MainFrameController.java b/src/main/java/ru/molokoin/MainFrameController.java index 77a8d7a..1ea9400 100644 --- a/src/main/java/ru/molokoin/MainFrameController.java +++ b/src/main/java/ru/molokoin/MainFrameController.java @@ -1,27 +1,29 @@ package ru.molokoin; import java.net.URL; +import java.util.Queue; import java.util.ResourceBundle; +import java.util.concurrent.ArrayBlockingQueue; import javafx.event.Event; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextField; -import ru.molokoin.example.ExchangeServer; +import ru.molokoin.ExchangeServer; public class MainFrameController implements Initializable{ @FXML public Button btn; @FXML public TextField portField; - public ExchangeServer server; + public ExchangeServer es; @Override public void initialize(URL location, ResourceBundle resources) { System.out.println("Инициализация компонентов интерфейса ..."); try { - server = new ExchangeServer(); + es = new ExchangeServer(); } catch (Exception ex) { System.out.println(ex); } @@ -31,13 +33,19 @@ public class MainFrameController implements Initializable{ public void buttonClicked(Event e){ if (btn.getText().equals("START")){ btn.setText("STOP"); - server.start(); + es = new ExchangeServer(); + // + es.connectionQueue = (Queue) new ArrayBlockingQueue(1024); + es.launch = new ServerLauncher(8081, es.connectionQueue); + es.server = new Thread(es.launch); + es.server.setName("ServerLauncherThread"); + es.server.start(); }else { btn.setText("START"); try { - server.finish(); + //server.finish(); } catch (Exception ex) { System.out.println(ex); } diff --git a/target/classes/ru/molokoin/Connection.class b/target/classes/ru/molokoin/Connection.class index 85c95e1..2a488a4 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 fc5c91e..a200d71 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/MainFrameController.class b/target/classes/ru/molokoin/MainFrameController.class index 087e324..7137ec4 100644 Binary files a/target/classes/ru/molokoin/MainFrameController.class and b/target/classes/ru/molokoin/MainFrameController.class differ