-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
131 lines (116 loc) · 3.4 KB
/
Server.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
private int num;
public static void main(String[] args)
{
int j;
int i=0;
int n;
Scanner s = new Scanner(System.in);
String user[]=new String[40];
String memory[]=new String[40];
String ram;
long ra[]=new long[40];
long te;
String ip[]=new String[40];
String temp;
Socket socket[]= new Socket[40];
String CPU[]= new String[40];
try
{
try {
System.out.println("************ Modifed bully Algorithm **************");
System.out.print("Enter Number of Nodes : ");
n = s.nextInt();
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(i<n)
{
//Reading the message from the client
socket[i] = serverSocket.accept();
System.out.println(socket[i]);
InputStream is = socket[i].getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
user[i] = br.readLine();
memory[i] = br.readLine();
ram= br.readLine();
ip[i]= br.readLine();
String returnMessage;
//Sending the response back to the client.
OutputStream os = socket[i].getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
returnMessage = " We got your Configuration '"+user[i]+"'"+"\n";
bw.write(returnMessage);
System.out.println("\nNew Configuration found\n");
bw.flush();
ra[i]=Long.parseLong(ram);
i++;
}
System.out.print("\n--------------------\n");
for(j=0; j<n; j++)
{
System.out.println("User name "+user[j]);
System.out.println("Memory "+memory[j] );
System.out.println("Ram "+ra[j] );
System.out.println("IP4 Address "+ip[j]);
System.out.print("\n--------------------\n");
}
for( i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(ra[i]<ra[j])
{
te=ra[i];
ra[i]=ra[j];
ra[j]=te;
temp=user[i];
user[i]=user[j];
user[j]=temp;
temp=ip[i];
ip[i]=ip[j];
ip[j]=temp;
}
}
}
System.out.print("Coordinator is " + user[0]+"\n");
for(i=0; i<n; i++)
{
System.out.println(socket[i]);
OutputStream os = socket[i].getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String sendMessage = user[0] + "\n";
bw.write(sendMessage);
sendMessage = ip[0] + "\n";
bw.write(sendMessage);
bw.flush();
// System.out.println("Message sent to the server : "+sendMessage);
}
}
catch (InputMismatchException e) {
System.out.println("You have entered invalid data");
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("You have entered invalid data");
}
finally
{
try
{
}
catch(Exception e){
}
}
}
}