Skip to content
Snippets Groups Projects
Commit e6e83703 authored by Mai Pham's avatar Mai Pham
Browse files

Implement grep

parent 8878ac76
No related branches found
No related tags found
No related merge requests found
Showing with 171 additions and 45 deletions
.idea/ .idea/
*.iml *.iml
out/*
*.DS_Store
localhost 5001 /home/maipham/uiuc/cs425/mp1/grep/files/test.rtf
\ No newline at end of file
{\rtf1\ansi\ansicpg1252\cocoartf2638
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
Error: This is error 1 in log file
Error: This is error 2 in log file
Error: This is error 3 in log file
}
\ No newline at end of file
fa22-cs425-5202.cs.illinois.edu 5000 d.log
fa22-cs425-5203.cs.illinois.edu 5000 e.log
fa22-cs425-5204.cs.illinois.edu 5000 e.log
\ No newline at end of file
{\rtf1\ansi\ansicpg1252\cocoartf2638
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 Error: This is an error for multi test case 1\
}
\ No newline at end of file
{\rtf1\ansi\ansicpg1252\cocoartf2638
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 Error: This is a confirm for multi test case 2\
!}
\ No newline at end of file
...@@ -15,8 +15,16 @@ public class Client { ...@@ -15,8 +15,16 @@ public class Client {
ArgumentParser parser = ArgumentParsers.newArgumentParser("Client") ArgumentParser parser = ArgumentParsers.newArgumentParser("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>
*/
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");
/*
Options of grep command
*/
parser.addArgument("-w").nargs("*")
.help("This prints lines that match a pattern");
Namespace ns = null; Namespace ns = null;
try { try {
...@@ -29,30 +37,48 @@ public class Client { ...@@ -29,30 +37,48 @@ public class Client {
FileReader file = new FileReader(ns.getString("configFile")); FileReader file = new FileReader(ns.getString("configFile"));
BufferedReader reader = new BufferedReader(file); BufferedReader reader = new BufferedReader(file);
ArrayList<String> servers = new ArrayList<>(); /*
ArrayList<Integer> ports = new ArrayList<>(); Read server hosts, ports, and log files
ArrayList<String> logFiles = new ArrayList<>(); */
// ArrayList<String> servers = new ArrayList<>();
// ArrayList<Integer> ports = new ArrayList<>();
// ArrayList<String> logFiles = new ArrayList<>();
ArrayList<ServerConf> servers = new ArrayList<>();
String line; String line;
while (true) { while (true) {
line = reader.readLine(); line = reader.readLine();
if (line != null) { if (line != null) {
String[] s = line.split(" "); String[] s = line.split(" ");
servers.add(s[0].trim());
ports.add(Integer.parseInt(s[1].trim())); String server = s[0].trim();
logFiles.add(s[2].trim()); int port = Integer.parseInt(s[1].trim());
String logFile = s[2].trim();
servers.add(new ServerConf(server, port, logFile));
} else { } else {
break; break;
} }
} }
String pattern = (String) ns.getList("w").get(0);
pattern = "-w " + pattern;
/*
Creating threads based on number of servers
*/
int n = servers.size(); int n = servers.size();
ArrayList<GrepThread> threads = new ArrayList<>(n); ArrayList<GrepThread> threads = new ArrayList<>(n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
GrepThread t = new GrepThread(servers.get(i), ports.get(i), logFiles.get(i)); GrepThread t = new GrepThread(servers.get(i), pattern);
threads.add(t); threads.add(t);
t.start(); t.start();
} }
/*
End threads
*/
for (GrepThread t: threads) { for (GrepThread t: threads) {
try { try {
t.join(); t.join();
......
package cs425.mp1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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);
ProcessBuilder prc_builder = new ProcessBuilder(cmd);
Process process = prc_builder.start();
BufferedReader input = new BufferedReader
(new InputStreamReader(process.getInputStream()));
String line;
List<String> res = new ArrayList<>();
System.out.println("Start reading logs!");
//Fix this to return count of line matches
while ((line = input.readLine()) != null) {
res.add(line);
}
return res;
}
}
package cs425.mp1; package cs425.mp1;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket; import java.net.Socket;
public class GrepThread extends Thread{ public class GrepThread extends Thread{
private final String host; private final ServerConf server;
private final int port; private final String pattern;
private final String logFile;
public GrepThread(ServerConf server, String pattern){
public GrepThread(String host, int port, String logFile){ this.server = server;
this.host = host; this.pattern = pattern;
this.port = port;
this.logFile = logFile;
} }
@Override @Override
public void run() { public void run() {
try { try {
String host = server.getHost();
int port = server.getPort();
String logFile = server.getLogFile();
Socket socket = new Socket(host, port); Socket socket = new Socket(host, port);
System.out.println("Connected to server"); socket.setSoTimeout(5000);
} catch(IOException u) {
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(pattern + " " + logFile);
System.out.println("Connected to server. Running grep job...");
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
String output = (String) inputStream.readObject();
System.out.println("-----------------------------");
System.out.println(output);
System.out.println("End querying for host " + host + ":" + logFile);
System.out.println("-----------------------------");
} catch(IOException | ClassNotFoundException u) {
if (u instanceof ClassNotFoundException) {
System.err.println("Client fails to get output of the query.");
} else
System.err.println("Error happens when connecting to server.");
u.printStackTrace(); u.printStackTrace();
} }
} }
......
...@@ -16,34 +16,23 @@ public class Server { ...@@ -16,34 +16,23 @@ public class Server {
socket = server.accept(); socket = server.accept();
System.out.println("Client connected"); System.out.println("Client connected");
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());
System.out.println(res);
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(res);
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error reading message");
e.printStackTrace();
}
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// DataInputStream in = null;
// try {
// in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// String line = "";
//
// // reads message from client until "Over" is sent
// while (!line.equals("Over"))
// {
// try
// {
// line = in.readUTF();
// System.out.println(line);
//
// }
// catch(IOException i)
// {
// System.out.println(i);
// }
// }
// System.out.println("Closing connection");
} }
public void close() throws IOException { public void close() throws IOException {
......
package cs425.mp1;
public class ServerConf {
private final String host;
private final int port;
private final String logFile;
public ServerConf(String host, int port, String logFile) {
this.host = host;
this.port = port;
this.logFile = logFile;
}
public String getHost() {
return host;
}
public String getLogFile() {
return logFile;
}
public int getPort() {
return port;
}
}
No preview for this file type
File added
No preview for this file type
No preview for this file type
File added
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