esoe 2 years ago
parent
commit
8bf8b18d50
  1. 38
      src/main/java/ru/molokoin/Connection.java
  2. 5
      src/main/java/ru/molokoin/ExchangeServer.java
  3. 18
      src/main/java/ru/molokoin/MainFrameController.java
  4. BIN
      target/classes/ru/molokoin/Connection.class
  5. BIN
      target/classes/ru/molokoin/ExchangeServer.class
  6. BIN
      target/classes/ru/molokoin/MainFrameController.class

38
src/main/java/ru/molokoin/Connection.java

@ -13,20 +13,32 @@ public class Connection {
public Connection(){} public Connection(){}
public Connection(SocketChannel accepted){ public Connection(SocketChannel accepted){
this.accepted = accepted; this.accepted = accepted;
this.put(); this.write();
this.read();
} }
//читаем из подключения //читаем из подключения
public void read(ByteBuffer byteBuffer) throws IOException{ public void read(){
// try{
accepted.read(byteBuffer); 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);
} }
//пишем в подключение
public void write(ByteBuffer byteBuffer) throws IOException{
//
accepted.write(byteBuffer);
} }
//пишем страничку по умолчанию catch (IOException e) {
public void put(){ e.printStackTrace();
}
}
//пишем в подключение
public void write(){
//accepted.write(null); //accepted.write(null);
RandomAccessFile aFile; RandomAccessFile aFile;
try { try {
@ -34,11 +46,11 @@ public class Connection {
Path path = Paths.get(stringPath); Path path = Paths.get(stringPath);
aFile = new RandomAccessFile(path.toAbsolutePath().toString(), "rw"); aFile = new RandomAccessFile(path.toAbsolutePath().toString(), "rw");
FileChannel inChannel = aFile.getChannel(); FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(24); ByteBuffer buf = ByteBuffer.allocate(1024);
int bytesRead; int bytesRead;
bytesRead = inChannel.read(buf); bytesRead = inChannel.read(buf);
while (bytesRead != -1){ while (bytesRead != -1){
System.out.println("Read " + bytesRead); System.out.println("Write " + bytesRead);
buf.flip(); buf.flip();
while(buf.hasRemaining()){ while(buf.hasRemaining()){
//System.out.print((char) buf.get()); //System.out.print((char) buf.get());
@ -52,7 +64,5 @@ public class Connection {
catch (IOException e) { catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

5
src/main/java/ru/molokoin/ExchangeServer.java

@ -4,8 +4,9 @@ import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
public class ExchangeServer { public class ExchangeServer {
private static ServerLauncher launch; static ServerLauncher launch;
public static Thread server; public static Thread server;
static Queue<Connection> connectionQueue;
//Запуск сервера //Запуск сервера
public void enable(ExchangeServer server){ public void enable(ExchangeServer server){
@ -16,7 +17,7 @@ public class ExchangeServer {
// //
} }
public static void main(String[] args) { public static void main(String[] args) {
Queue<Connection> connectionQueue = (Queue<Connection>) new ArrayBlockingQueue(1024); connectionQueue = (Queue<Connection>) new ArrayBlockingQueue(1024);
launch = new ServerLauncher(8081, connectionQueue); launch = new ServerLauncher(8081, connectionQueue);
server = new Thread(launch); server = new Thread(launch);
server.setName("ServerLauncherThread"); server.setName("ServerLauncherThread");

18
src/main/java/ru/molokoin/MainFrameController.java

@ -1,27 +1,29 @@
package ru.molokoin; package ru.molokoin;
import java.net.URL; import java.net.URL;
import java.util.Queue;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.concurrent.ArrayBlockingQueue;
import javafx.event.Event; import javafx.event.Event;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import ru.molokoin.example.ExchangeServer; import ru.molokoin.ExchangeServer;
public class MainFrameController implements Initializable{ public class MainFrameController implements Initializable{
@FXML @FXML
public Button btn; public Button btn;
@FXML @FXML
public TextField portField; public TextField portField;
public ExchangeServer server; public ExchangeServer es;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
System.out.println("Инициализация компонентов интерфейса ..."); System.out.println("Инициализация компонентов интерфейса ...");
try { try {
server = new ExchangeServer(); es = new ExchangeServer();
} catch (Exception ex) { } catch (Exception ex) {
System.out.println(ex); System.out.println(ex);
} }
@ -31,13 +33,19 @@ public class MainFrameController implements Initializable{
public void buttonClicked(Event e){ public void buttonClicked(Event e){
if (btn.getText().equals("START")){ if (btn.getText().equals("START")){
btn.setText("STOP"); btn.setText("STOP");
server.start(); es = new ExchangeServer();
//
es.connectionQueue = (Queue<Connection>) new ArrayBlockingQueue(1024);
es.launch = new ServerLauncher(8081, es.connectionQueue);
es.server = new Thread(es.launch);
es.server.setName("ServerLauncherThread");
es.server.start();
}else { }else {
btn.setText("START"); btn.setText("START");
try { try {
server.finish(); //server.finish();
} catch (Exception ex) { } catch (Exception ex) {
System.out.println(ex); System.out.println(ex);
} }

BIN
target/classes/ru/molokoin/Connection.class

Binary file not shown.

BIN
target/classes/ru/molokoin/ExchangeServer.class

Binary file not shown.

BIN
target/classes/ru/molokoin/MainFrameController.class

Binary file not shown.
Loading…
Cancel
Save