Skip to content
Snippets Groups Projects
Commit 1aa24c3b authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Solution

parent 959e4385
No related merge requests found
Pipeline #119 failed with stages
in 0 seconds
......@@ -35,6 +35,18 @@ public class IndexReader {
}
public byte[] readData(int index) {
try (FileChannel fc = FileChannel.open(getFile())) {
return getData(fc, index);
}
catch (IOException exc) {
log.error("read", exc);
return null;
}
}
private Path getFile() throws IOException {
Path LocalFile = Files.createTempFile(null, null).getParent().resolve(
IMAGE_NAME);
......@@ -56,6 +68,25 @@ public class IndexReader {
}
private byte[] getData(FileChannel fc, int index) throws IOException {
return new byte[0];
//position := readInt(index*8 + 4)
//length := readInt(index*8 + 8)
//data := readData(position, length)
//return data;
//go to specific position ... FileChannel.position(long)
//read data ... FileChannel.read(ByteBuffer)
//ByteBuffer.getInt()
ByteBuffer bb = ByteBuffer.allocate(8);
fc.position(index*8 + 4);
fc.read(bb);
bb.flip();
int positon = bb.getInt();
int legth = bb.getInt();
bb = ByteBuffer.allocate(legth);
fc.position(positon);
fc.read(bb);
return bb.array();
}
}
package koz01.java2.lab08;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class RemoteImageServer {
public static void main(String[] args) throws IOException {
IndexReader indexReader = new IndexReader();
int port = Integer.parseInt(args[0]);
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(port));
log.info("Waiting connection on port {}", port);
ssc.configureBlocking(false);
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey sk = keys.next();
if (sk.channel() == ssc) {
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
log.info("Connected: {}", sc.getRemoteAddress());
sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_CONNECT);
// register sc in selector for Read Operation
}
else {
// get SocketChannel from selection key
// readInt from socketChannel
// writeInt to socketChannel ... length of image
// writeData to socketChannel
ByteChannel sc = (ByteChannel) sk.channel();
ByteBuffer bb = ByteBuffer.allocate(4);
if (sc.read(bb) < 0) {
// connection is closed so remove selector
sk.cancel();
}
bb.flip();
int index = bb.getInt();
byte[] data = indexReader.readData(index);
bb.clear();
bb.putInt(data.length);
bb.flip();
sc.write(bb);
bb = ByteBuffer.wrap(data);
bb.position(bb.limit());
bb.flip();
sc.write(bb);
}
keys.remove();
}
}
}
}
......@@ -13,8 +13,8 @@
<Label layoutX="81.0" layoutY="592.0" prefHeight="26.0" prefWidth="103.0" text="Index obrázku" />
<Button fx:id="load" disable="true" layoutX="306.0" layoutY="592.0" mnemonicParsing="false" onAction="#readButton" text="Načti" />
<Label layoutX="81.0" layoutY="531.0" text="Adresa" />
<TextField fx:id="adress" layoutX="149.0" layoutY="526.0" prefHeight="26.0" prefWidth="131.0" />
<TextField fx:id="port" layoutX="207.0" layoutY="560.0" prefHeight="26.0" prefWidth="73.0" />
<TextField fx:id="adress" layoutX="149.0" layoutY="526.0" prefHeight="26.0" prefWidth="131.0" text="127.0.0.1" />
<TextField fx:id="port" layoutX="207.0" layoutY="560.0" prefHeight="26.0" prefWidth="73.0" text="10000" />
<Label layoutX="81.0" layoutY="565.0" prefHeight="16.0" prefWidth="46.0" text="Port" />
<Button fx:id="connect" layoutX="306.0" layoutY="526.0" mnemonicParsing="false" onAction="#connect" text="Připojit" />
</children>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment