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
Branches test
No related tags found
No related merge requests found
......@@ -16,16 +16,14 @@ public class Client {
.defaultHelp(true)
.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("?")
.help("Path of the config file containing list of host and its port");
/*
Options of grep command
*/
parser.addArgument("-w").nargs("*")
.help("This prints lines that match a pattern");
parser.addArgument("--command").nargs("*")
.help("Put command of grep: <grep_options> <string_to_match>");
/* Read input arguments */
Namespace ns = null;
try {
ns = parser.parseArgs(args);
......@@ -34,18 +32,12 @@ public class Client {
System.exit(1);
}
/* Read config file */
FileReader file = new FileReader(ns.getString("configFile"));
BufferedReader reader = new BufferedReader(file);
/*
Read server hosts, ports, and log files
*/
// ArrayList<String> servers = new ArrayList<>();
// ArrayList<Integer> ports = new ArrayList<>();
// ArrayList<String> logFiles = new ArrayList<>();
/* Read server hosts, ports, and log files */
ArrayList<ServerConf> servers = new ArrayList<>();
String line;
while (true) {
line = reader.readLine();
......@@ -62,8 +54,9 @@ public class Client {
}
}
String pattern = (String) ns.getList("w").get(0);
pattern = "-w " + pattern;
/* Read grep command */
String pattern = String.join(" ", ns.getList("command"));
pattern = "-" + pattern;
/*
Creating threads based on number of servers
......@@ -76,9 +69,7 @@ public class Client {
t.start();
}
/*
End threads
*/
/* End threads */
for (GrepThread t: threads) {
try {
t.join();
......
......@@ -7,15 +7,9 @@ import java.util.ArrayList;
import java.util.List;
public class Grep {
List<String> cmd = new ArrayList<>();
public Grep(String pattern) {
this.cmd.add("/bin/sh");
this.cmd.add("-c");
this.cmd.add("grep --color=always " + pattern);
}
public List<String> run_cmd() throws IOException {
System.out.println(cmd);
public static List<String> run_cmd(String pattern) throws IOException {
String[] cmd = {"/bin/sh", "-c", "grep --color=always " + pattern};
// System.out.println(cmd);
ProcessBuilder prc_builder = new ProcessBuilder(cmd);
Process process = prc_builder.start();
BufferedReader input = new BufferedReader
......
......@@ -24,10 +24,16 @@ public class GrepThread extends Thread{
Socket socket = new Socket(host, port);
socket.setSoTimeout(5000);
/*
Creat output stream to send to servers
*/
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(pattern + " " + logFile);
System.out.println("Connected to server. Running grep job...");
/*
Get input from servers and display results of query
*/
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
String output = (String) inputStream.readObject();
System.out.println("-----------------------------");
......
......@@ -8,6 +8,9 @@ import net.sourceforge.argparse4j.inf.Namespace;
public class Main {
public static void main(String[] args) {
/*
Server side only need input port number
*/
ArgumentParser parser = ArgumentParsers.newArgumentParser("Main")
.defaultHelp(true)
.description("Run grep server.");
......
package cs425.mp1;
import java.io.*;
import java.net.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private int port;
......@@ -11,16 +14,19 @@ public class Server {
public Server(int port) {
this.port = port;
try {
/* Establish socket to client */
server = new ServerSocket(port);
System.out.println("Server start listening on port "+ port);
socket = server.accept();
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());
try {
String mess = (String) inputStream.readObject();
Grep grep = new Grep(mess);
String res = String.join("\n", grep.run_cmd());
String res = String.join("\n", Grep.run_cmd(mess));
System.out.println(res);
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
......
package cs425.mp1;
/**
* This class is specified configurations for Server
*/
public class ServerConf {
private final String host;
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