Skip to content

Commit 0968514

Browse files
author
Valeriu
committed
lab 89
1 parent 2d2a06c commit 0968514

71 files changed

Lines changed: 7604 additions & 22 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

_8/src/main/java/Main.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@
55
public class Main {
66
public static void main(String args[]) {
77
Game game = new Game();
8-
game.setBoard(new Board(10));
9-
game.addPlayer(new Player("Player 1"));
10-
game.addPlayer(new Player("Player 2"));
11-
game.addPlayer(new Player("Player 3"));
8+
game.setBoard(new Board(3));
9+
game.addPlayer(new Player("Player 1",game));
10+
//game.addPlayer(new Player("Player 2",game));
11+
// game.addPlayer(new Player("Player 3",game));
12+
// game.addPlayer(new Player("Player 4",game));
13+
// game.addPlayer(new Player("Player 5",game));
14+
// game.addPlayer(new Player("Player 6",game));
15+
//
16+
// for(int i=10;i<100000;i++)
17+
// game.addPlayer(new Player("Player "+i,game));
18+
1219
game.start();
20+
21+
Player winner = game.getWinner();
22+
if (winner == null)
23+
System.out.println("No one won");
24+
else
25+
System.out.println(game.getWinner().toString() + " is the winner");
1326
}
1427
}

_8/src/main/java/model/Board.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
package model;
22

3-
import java.util.Collection;
43
import java.util.Collections;
4+
import java.util.LinkedList;
55
import java.util.List;
66

77
public class Board {
88

99
private final Graph complete;
1010

11-
public Board(int n){
11+
public Board(int n) {
1212

13-
List<Edge> edges = null;
14-
int edgeCount = 0;
15-
for (int i = 0; i < n-1 ; i++) {
16-
for (int j = i+1; j < n ; j++) {
17-
edges.set(edgeCount, new Edge(i, j));
13+
List<Edge> edges = new LinkedList<>();
14+
for (int i = 0; i < n - 1; i++) {
15+
for (int j = i + 1; j < n; j++) {
16+
edges.add(new Edge(i, j));
1817
}
1918
}
2019

2120
Collections.shuffle(edges);
22-
complete = new Graph(edges);
21+
complete = new Graph(edges);
2322

2423
}
2524

26-
public synchronized Edge extract(){
25+
public synchronized Edge extract() {
2726

2827
Edge edge = complete.pullFirst();
2928
return edge;
3029
}
3130

32-
public boolean isEmpty(){
31+
public boolean isEmpty() {
3332

3433
return complete.isEmpty();
3534

_8/src/main/java/model/Game.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ public List<Player> getPlayers() {
2727
}
2828

2929

30-
//Create the method that will start the game: start one thread for each player
31-
public void startGame() {
32-
// TODO: code this
33-
}
3430

3531
public void setWinner(Player player) {
3632
this.winner = player;
3733
}
3834

3935
public void start() {
36+
players.forEach(player -> {
37+
Thread newThread = new Thread(player);
38+
newThread.start();
39+
});
40+
}
4041

42+
public Player getWinner() {
43+
return winner;
4144
}
4245
}

_8/src/main/java/model/Player.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package model;
22

3+
import java.util.LinkedList;
4+
import java.util.List;
5+
36
public class Player implements Runnable{
47
private String name;
58
private Game game;
69
private Graph graph;
710

8-
public Player(String name) {
11+
public Player(String name, Game game) {
912
this.name = name;
13+
this.graph = new Graph(new LinkedList<Edge>());
1014
}
1115

1216
public Player(String name, Game game, Graph graph) {
@@ -45,14 +49,19 @@ private boolean play() throws InterruptedException {
4549
graph.addEdge( board.extract() );
4650
Thread.sleep(THINKING_TIME); //declare this constant
4751
if (graph.isSpanningTree()) {
48-
game.setWinner(this);
52+
if (game.getWinner() != null)
53+
game.setWinner(this);
54+
else return true;
4955
}
5056
return true;
5157
}
5258

5359
public void run() {
54-
this.graph.extractEdge();
55-
System.out.println(this.toString() + " extracted an edge...");
60+
try {
61+
play();
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
}
5665
}
5766

5867

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.company;
2+
// alow pararel running
3+
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.io.PrintWriter;
9+
import java.net.Socket;
10+
import java.net.SocketException;
11+
import java.net.UnknownHostException;
12+
import java.util.Scanner;
13+
14+
public class SocialNetworkClient {
15+
private final static String SERVER_ADDRESS = "127.0.0.1";
16+
private final static int PORT = 8100;
17+
private Socket socket;
18+
19+
public static void main(String[] args) throws IOException {
20+
SocialNetworkClient client = new SocialNetworkClient();
21+
while (true) {
22+
String request = client.readFromKeyboard();
23+
if (request.equalsIgnoreCase("exit")) {
24+
break;
25+
} else {
26+
try {
27+
client.sendRequestToServer(request);
28+
}
29+
catch (SocketException se){
30+
System.out.println("Server shut down successfully!");
31+
System.exit(0);
32+
}
33+
}
34+
}
35+
}
36+
37+
//Implement the sendRequestToServer method
38+
private String sendRequestToServer(String request) throws IOException {
39+
socket = new Socket(SERVER_ADDRESS, PORT);
40+
String resp = "";
41+
// timeout after 10000 milliseconds
42+
int timeOut = 10000;
43+
this.socket.setSoTimeout(timeOut);
44+
try {
45+
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
46+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
47+
// send the request to the server:
48+
out.println(request);
49+
// read the response from the server:
50+
String response = in.readLine();
51+
System.out.println(response);
52+
} catch (UnknownHostException e) {
53+
System.err.println(e);
54+
}
55+
return resp;
56+
}
57+
58+
private String readFromKeyboard() {
59+
Scanner scanner = new Scanner(System.in);
60+
// citesc toata linia de la tastatura:
61+
return scanner.nextLine();
62+
}
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.company;
2+
import java.net.*;
3+
import java.io.*;
4+
5+
public class ClientThread extends Thread {
6+
private Socket socket = null;
7+
private SocialNetworkServer server;
8+
SocialNetwork net = new SocialNetwork(socket);
9+
10+
ClientThread(Socket socket) throws FileNotFoundException, UnsupportedEncodingException {
11+
this.socket = socket;
12+
// this.server = server;
13+
}
14+
15+
@Override
16+
public void run() {
17+
try {
18+
while (true) {
19+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //client -> server stream
20+
String request = in.readLine();
21+
System.out.println("\r\nMessage from " + Thread.currentThread().getName() + ": " + request);
22+
String response = execute(request);
23+
if (request.equalsIgnoreCase("stop")) {
24+
System.exit(0);
25+
}
26+
PrintWriter out = new PrintWriter(socket.getOutputStream()); //server -> client stream
27+
out.println(response);
28+
// gracefully stop!!!
29+
30+
out.flush();
31+
}
32+
} catch (IOException e) {
33+
System.err.println("Communication error... " + e);
34+
} finally {
35+
try {
36+
socket.close(); // or use try-with-resources
37+
} catch (IOException e) {
38+
System.err.println(e);
39+
}
40+
}
41+
}
42+
43+
// display the message: "Server received the request ... "
44+
private String execute(String request) throws IOException {
45+
String response;
46+
response = null;
47+
CommandBuilder commandBuilder = new CommandBuilder();
48+
Command command;
49+
if (request.equalsIgnoreCase("stop")) {
50+
response = "Goodbye!";
51+
} else {
52+
System.out.println("Server received the request " + request);
53+
command = commandBuilder.CommandBuilder(request);
54+
response = command.execute(net, socket);
55+
}
56+
return response;
57+
}
58+
}
59+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.company;
2+
3+
import java.io.IOException;
4+
import java.net.Socket;
5+
6+
public interface Command
7+
{
8+
String execute(SocialNetwork network, Socket socket) throws IOException;
9+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.company;
2+
3+
import java.util.Arrays;
4+
5+
public class CommandBuilder {
6+
public Command CommandBuilder(String request)
7+
{
8+
String command;
9+
String[] args;
10+
String[] parts = request.split(" ");
11+
command = parts[0];
12+
args = Arrays.copyOfRange(parts,1,parts.length);
13+
14+
if(command.equals("register"))
15+
{
16+
return new RegisterCommand(args);
17+
}
18+
19+
if(command.equals("login"))
20+
{
21+
return new LoginCommand(args);
22+
}
23+
24+
if(command.equals("friend"))
25+
{
26+
return new FriendCommand(args);
27+
}
28+
29+
if(command.equals("send"))
30+
{
31+
return new SendCommand(args);
32+
}
33+
// stop app
34+
if(command.equals("stop"))
35+
{
36+
return new StopCommand(args);
37+
}
38+
39+
if(command.equals("read"))
40+
{
41+
return new readCommand(args);
42+
}
43+
return null;
44+
45+
}
46+
}

0 commit comments

Comments
 (0)