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

Solution

parent 959e4385
Branches
No related merge requests found
......@@ -35,6 +35,17 @@ 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 +67,26 @@ public class IndexReader {
}
private byte[] getData(FileChannel fc, int index) throws IOException {
// 0 .. 3 - number of images
// 4 .. 7 - position of image data file
// 9 .. 11 - number of bytes of image data
// position_in_index := 4 + index * 8
ByteBuffer bb = ByteBuffer.allocate(8);
fc.position(4 + index * 8);
while (bb.hasRemaining()) {
fc.read(bb);
}
bb.flip();
int positionOfData = bb.getInt();
int legthOfData = bb.getInt();
// position_of_data := readInt(position_in_index)
// length_of_data := readInt(position_in_index + 4)
// data := readData(position_of_data, length_of_data)
// return data
// bb.array()
return new byte[0];
}
}
package koz01.java2.lab08;
import java.io.IOException;
import java.net.InetSocketAddress;
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 java.util.Set;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class RemoteServer {
public static void main(String[] args) throws IOException
{
int port = Integer.parseInt(args[0]);
Selector selector = Selector.open();
// open server socket channel on given port
ServerSocketChannel sc = ServerSocketChannel.open();
sc.bind(new InetSocketAddress(port));
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_ACCEPT);
log.info("Server started and waiting connection on port {}", port);
while (true) {
selector.select();
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey sk = iterator.next();
if (sk.channel() == sc) {
SocketChannel channel = sc.accept();
log.info("client connected {}", channel.getRemoteAddress());
// register channel to selector
}
else {
SocketChannel channel = (SocketChannel) sk.channel();
// read index of image (int)
int index = 0;
byte[] data = new IndexReader().readData(index);
// write(send) size of data
// write(send) data
}
iterator.remove();
}
}
}
}
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