Skip to content
Snippets Groups Projects
Commit 64272b3b authored by maitp2's avatar maitp2
Browse files

Merge branch 'modify' into 'main'

Modify parameters, add grep and clean code

See merge request trangtd2/cs425_mp1!3
parents 58c93410 68077b4f
No related branches found
No related tags found
No related merge requests found
...@@ -16,16 +16,14 @@ public class Client { ...@@ -16,16 +16,14 @@ public class Client {
.defaultHelp(true) .defaultHelp(true)
.description("Run distributed grep."); .description("Run distributed grep.");
/* /*
config file is a text file has format: <server_host> <server_port> <log_file> Config file is a text file has format: <server_host> <server_port> <log_file>
*/ */
parser.addArgument("-cf","--configFile").nargs("?") parser.addArgument("-cf","--configFile").nargs("?")
.help("Path of the config file containing list of host and its port"); .help("Path of the config file containing list of host and its port");
/* parser.addArgument("--command").nargs("*")
Options of grep command .help("Put command of grep: <grep_options> <string_to_match>");
*/
parser.addArgument("-w").nargs("*")
.help("This prints lines that match a pattern");
/* Read input arguments */
Namespace ns = null; Namespace ns = null;
try { try {
ns = parser.parseArgs(args); ns = parser.parseArgs(args);
...@@ -34,18 +32,12 @@ public class Client { ...@@ -34,18 +32,12 @@ public class Client {
System.exit(1); System.exit(1);
} }
/* Read config file */
FileReader file = new FileReader(ns.getString("configFile")); FileReader file = new FileReader(ns.getString("configFile"));
BufferedReader reader = new BufferedReader(file); BufferedReader reader = new BufferedReader(file);
/* /* Read server hosts, ports, and log files */
Read server hosts, ports, and log files
*/
// ArrayList<String> servers = new ArrayList<>();
// ArrayList<Integer> ports = new ArrayList<>();
// ArrayList<String> logFiles = new ArrayList<>();
ArrayList<ServerConf> servers = new ArrayList<>(); ArrayList<ServerConf> servers = new ArrayList<>();
String line; String line;
while (true) { while (true) {
line = reader.readLine(); line = reader.readLine();
...@@ -62,8 +54,9 @@ public class Client { ...@@ -62,8 +54,9 @@ public class Client {
} }
} }
String pattern = (String) ns.getList("w").get(0); /* Read grep command */
pattern = "-w " + pattern; String pattern = String.join(" ", ns.getList("command"));
pattern = "-" + pattern;
/* /*
Creating threads based on number of servers Creating threads based on number of servers
...@@ -76,9 +69,7 @@ public class Client { ...@@ -76,9 +69,7 @@ public class Client {
t.start(); t.start();
} }
/* /* End threads */
End threads
*/
for (GrepThread t: threads) { for (GrepThread t: threads) {
try { try {
t.join(); t.join();
......
...@@ -7,15 +7,9 @@ import java.util.ArrayList; ...@@ -7,15 +7,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Grep { public class Grep {
List<String> cmd = new ArrayList<>(); public static List<String> run_cmd(String pattern) throws IOException {
public Grep(String pattern) { String[] cmd = {"/bin/sh", "-c", "grep --color=always " + pattern};
this.cmd.add("/bin/sh"); // System.out.println(cmd);
this.cmd.add("-c");
this.cmd.add("grep --color=always " + pattern);
}
public List<String> run_cmd() throws IOException {
System.out.println(cmd);
ProcessBuilder prc_builder = new ProcessBuilder(cmd); ProcessBuilder prc_builder = new ProcessBuilder(cmd);
Process process = prc_builder.start(); Process process = prc_builder.start();
BufferedReader input = new BufferedReader BufferedReader input = new BufferedReader
......
...@@ -24,10 +24,16 @@ public class GrepThread extends Thread{ ...@@ -24,10 +24,16 @@ public class GrepThread extends Thread{
Socket socket = new Socket(host, port); Socket socket = new Socket(host, port);
socket.setSoTimeout(5000); socket.setSoTimeout(5000);
/*
Creat output stream to send to servers
*/
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream()); ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(pattern + " " + logFile); outputStream.writeObject(pattern + " " + logFile);
System.out.println("Connected to server. Running grep job..."); System.out.println("Connected to server. Running grep job...");
/*
Get input from servers and display results of query
*/
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
String output = (String) inputStream.readObject(); String output = (String) inputStream.readObject();
System.out.println("-----------------------------"); System.out.println("-----------------------------");
......
...@@ -8,6 +8,9 @@ import net.sourceforge.argparse4j.inf.Namespace; ...@@ -8,6 +8,9 @@ import net.sourceforge.argparse4j.inf.Namespace;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
/*
Server side only need input port number
*/
ArgumentParser parser = ArgumentParsers.newArgumentParser("Main") ArgumentParser parser = ArgumentParsers.newArgumentParser("Main")
.defaultHelp(true) .defaultHelp(true)
.description("Run grep server."); .description("Run grep server.");
......
package cs425.mp1; package cs425.mp1;
import java.io.*; import java.io.IOException;
import java.net.*; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server { public class Server {
private int port; private int port;
...@@ -11,16 +14,19 @@ public class Server { ...@@ -11,16 +14,19 @@ public class Server {
public Server(int port) { public Server(int port) {
this.port = port; this.port = port;
try { try {
/* Establish socket to client */
server = new ServerSocket(port); server = new ServerSocket(port);
System.out.println("Server start listening on port "+ port); System.out.println("Server start listening on port "+ port);
socket = server.accept(); socket = server.accept();
System.out.println("Client connected"); System.out.println("Client connected");
/*
Input command from client and call Grep class to execute, then return output to output stream
*/
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
try { try {
String mess = (String) inputStream.readObject(); String mess = (String) inputStream.readObject();
Grep grep = new Grep(mess); String res = String.join("\n", Grep.run_cmd(mess));
String res = String.join("\n", grep.run_cmd());
System.out.println(res); System.out.println(res);
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream()); ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
......
package cs425.mp1; package cs425.mp1;
/**
* This class is specified configurations for Server
*/
public class ServerConf { public class ServerConf {
private final String host; private final String host;
private final int port; private final int port;
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment