forked from subham-71/Scalable-Ticketing-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.java
116 lines (102 loc) · 4.03 KB
/
client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.Scanner;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.concurrent.TimeUnit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException ;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
class sendQuery implements Runnable
{
int sockPort = 7005 ;
// public sendQuery(int arg) // constructor to get arguments from the main thread
// {
// // arg from main thread
// }
public void run()
{
try
{
//Creating a client socket to send query requests
Socket socketConnection = new Socket("localhost", sockPort) ;
// Files for input queries and responses
String inputfile = "./Input/" +Thread.currentThread().getName() + "_input.txt" ;
String outputfile = "./Output/" +Thread.currentThread().getName() + "_output.txt" ;
//-----Initialising the Input & ouput file-streams and buffers-------
OutputStreamWriter outputStream = new OutputStreamWriter(socketConnection
.getOutputStream());
BufferedWriter bufferedOutput = new BufferedWriter(outputStream);
InputStreamReader inputStream = new InputStreamReader(socketConnection
.getInputStream());
BufferedReader bufferedInput = new BufferedReader(inputStream);
PrintWriter printWriter = new PrintWriter(bufferedOutput,true);
File queries = new File(inputfile);
File output = new File(outputfile);
FileWriter filewriter = new FileWriter(output);
Scanner sc = new Scanner(queries);
String query = "";
//--------------------------------------------------------------------
// Read input queries
while(sc.hasNextLine())
{
query = sc.nextLine();
printWriter.println(query);
}
// Get query responses from the input end of the socket of client
char c;
while((c = (char) bufferedInput.read()) != (char)-1)
{
// System.out.print(i);
filewriter.write(c);
}
// close the buffers and socket
filewriter.close();
sc.close();
socketConnection.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
public class client
{
public static void main(String args[])throws IOException
{
// int numberOfusers = 1; // Indicate no of users
// // Creating a thread pool
// ExecutorService executorService = Executors.newFixedThreadPool(numberOfusers) ;
// for(int i = 0; i < numberOfusers; i++)
// {
// Runnable runnableTask = new sendQuery() ; // Pass arg if any as sendQuery(arg)
// executorService.submit(runnableTask) ;
// }
int firstLevelThreads = 3 ; // Indicate no of users
/**************************/
// Creating a thread pool
ExecutorService executorService = Executors.newFixedThreadPool(firstLevelThreads);
for(int i = 0; i < firstLevelThreads; i++)
{
Runnable runnableTask = new invokeWorkers(); // Pass arg, if any to constructor sendQuery(arg)
executorService.submit(runnableTask) ;
}
executorService.shutdown() ;
try
{
if (!executorService.awaitTermination(1001, TimeUnit.MILLISECONDS))
{
executorService.shutdownNow();
}
}
catch (InterruptedException e)
{
executorService.shutdownNow();
}
}
}