Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/elsys/oop
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioBenov committed Jul 28, 2024
2 parents c54026b + d8e475e commit 37cb650
Show file tree
Hide file tree
Showing 48 changed files with 996 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions materials/2023-2024/11b/2024-04-15-routers/routers/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions materials/2023-2024/11b/2024-04-15-routers/routers/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>routers</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import java.util.*;

public class Router implements Runnable {
private List<Router> other = new LinkedList<>();
private List<Object> hosts = new LinkedList<>();

private Queue<Object> packets = new LinkedList<>();
private Queue<Object> waitingPackets = new LinkedList<>();

private Map<String, Integer> arpTable = new TreeMap<>();

// synchronized public List<Router> getOther() {
// return other;
// }

@Override
public void run() {
try {
while(true) {
while(!packets.isEmpty()) {
Object packet = packets.remove();
processPacket(packet);
Thread.sleep(500);
}

synchronized (this) {
wait();
}
}
} catch (InterruptedException e) {
System.out.println("Router shutting down");
}
}

private void processPacket(Object packet) {
String packetType = "data";

switch (packetType) {
case "data":
processDataPacket(packet);
break;
case "query":
processQueryPacket(packet);
break;
}
}

private void processQueryPacket(Object packet) {
String destination = "H3";
String srcRouter = "R1";

Object host = findHost(destination);
if(host != null) {
Router r1; // = ???
r1.receivePacket("type=found, destination=H3");
return;
}
}

private Object findHost(String destination) {
for(Object host : hosts) {
String hostName = "H3";
if(hostName.equals(destination)) {
return host;
}
}

return null;
}

private void processDataPacket(Object packet) {
String destination = "H3";

for(Object host : hosts) {
String hostName = "H3";
if(hostName.equals(destination)) {
System.out.println("host.receivePacket(packet);");
return;
}
}

if(arpTable.containsKey(destination)) {
Integer neighborIndex = arpTable.get(destination);
other.get(neighborIndex).receivePacket(packet);
return;
}

for(Router neighbor : other) {
neighbor.receivePacket("type: query, destination: H3");
}

waitingPackets.add(packet);
}

private void receivePacket(Object packet) {

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>executor-service-2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;

public class Main {
public static class MyTask implements Callable {
private int task;

public MyTask(int task) {
this.task = task;
}

// @Override
// public void run() {
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }

@Override
public Integer call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

return task;
}
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
// ExecutorService ex = Executors.newFixedThreadPool(2);
// ExecutorService ex = Executors.newCachedThreadPool();
ExecutorService ex = Executors.newWorkStealingPool()

List<Future<Integer>> results = new LinkedList<>();
for(int i = 0; i < 10; i++) {
// ex.execute(new MyTask(i));
// Future<Integer> res = ex.submit(new MyTask(i));
// Integer finalRes = res.get();
// System.out.println(finalRes);
results.add(ex.submit(new MyTask(i)));
}

results.stream().map(future -> {
try {
return future.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}).forEach(System.out::println);

ex.shutdown();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 37cb650

Please sign in to comment.