esoe 2 years ago
parent
commit
5722409920
  1. 41
      src/main/java/ru/molokoin/Connection.java
  2. 3
      src/main/java/ru/molokoin/ExchangeServer.java
  3. 3
      src/main/java/ru/molokoin/ServerLauncher.java
  4. BIN
      target/classes/ru/molokoin/Connection.class
  5. BIN
      target/classes/ru/molokoin/ExchangeServer.class
  6. BIN
      target/classes/ru/molokoin/ServerLauncher.class

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

@ -1,21 +1,58 @@
package ru.molokoin; package ru.molokoin;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Connection { public class Connection {
public SocketChannel accepted = null; public SocketChannel accepted = null;
public Connection(){} public Connection(){}
public Connection(SocketChannel accepted){ public Connection(SocketChannel accepted){
this.accepted = 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();
}
} }
} }

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

@ -1,7 +1,8 @@
package ru.molokoin; package ru.molokoin;
public class ExchangeServer { 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){ public void enable(ExchangeServer server){

3
src/main/java/ru/molokoin/ServerLauncher.java

@ -11,9 +11,10 @@ import java.util.Queue;
* Подключения ставятся в очередь * Подключения ставятся в очередь
*/ */
public class ServerLauncher implements Runnable{ public class ServerLauncher implements Runnable{
private int tcpPort = 0; private int tcpPort = 8081;
private ServerSocketChannel channel = null; private ServerSocketChannel channel = null;
private Queue<Connection> connectionQueue = null; private Queue<Connection> connectionQueue = null;
private boolean isActive = false;//флаг проверки, запущен ли сервер
@Override @Override
public void run() { public void run() {

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/ServerLauncher.class

Binary file not shown.
Loading…
Cancel
Save