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 { @@ -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 { @@ -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 { @@ -52,7 +64,5 @@ public class Connection {
catch (IOException e) {
e.printStackTrace();
}
}
}

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

@ -4,8 +4,9 @@ import java.util.Queue; @@ -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<Connection> connectionQueue;
//Запуск сервера
public void enable(ExchangeServer server){
@ -16,7 +17,7 @@ public class ExchangeServer { @@ -16,7 +17,7 @@ public class ExchangeServer {
//
}
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);
server = new Thread(launch);
server.setName("ServerLauncherThread");

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

@ -1,27 +1,29 @@ @@ -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{ @@ -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<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 {
btn.setText("START");
try {
server.finish();
//server.finish();
} catch (Exception 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