-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadBalancer.java
More file actions
238 lines (218 loc) · 7.63 KB
/
LoadBalancer.java
File metadata and controls
238 lines (218 loc) · 7.63 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.Remote;
import java.util.*;
// Creating Remote interface for our application
interface Lb_interface extends Remote {
void grade_assignment() throws RemoteException;
void view_assignment() throws RemoteException;
void post_announcement(String annouce) throws RemoteException;
boolean enter_marks(int resource_key, String message) throws RemoteException;
String get_marks(int id) throws RemoteException;
String displayMarks(int id) throws RemoteException;
void postDoubt(String message) throws RemoteException;
void uploadAssignment(byte[] buffer,int length,String fname) throws RemoteException;
boolean acquire(int resource_key) throws RemoteException;
boolean release(int resource_key) throws RemoteException;
}
public class LoadBalancer implements Lb_interface {
public int counter=0;
public int queue[]=new int[100];
public HashMap<Integer, Boolean> mutex_locks_marks = new HashMap<Integer, Boolean>();
public HashMap<Integer, Integer> readers_count_marks = new HashMap<Integer, Integer>();
public boolean mutex_locks_announce=false;
public int readers_count_announce=0;
public String studentServerList[]={"Server1S","Server2S","Server3S"};
public String teacherServerList[]={"Server1T","Server2T","Server3T"};
public int server_weights[]={5,3,2};
public int server_Counters[]={0,0,0};
public LoadBalancer() {
mutex_locks_marks.put(1,false);
mutex_locks_marks.put(2,false);
mutex_locks_marks.put(3,false);
mutex_locks_marks.put(4,false);
readers_count_marks.put(1,0);
readers_count_marks.put(2,0);
readers_count_marks.put(3,0);
readers_count_marks.put(4,0);
}
public static void main(String args[]) {
try
{
LoadBalancer lb = new LoadBalancer();
Lb_interface stub = (Lb_interface) UnicastRemoteObject.exportObject(lb, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("LoadBalancer", stub);
System.err.println("Load balancer running ...");
System.out.println("The servers working are :");
for(int i=0;i<3;i++)
{System.out.println(lb.studentServerList[i]);
}
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
public String displayMarks(int id) {
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=studentServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
counter+=1;
counter%=3;
Student stub =(Student) registry.lookup(server);
return stub.displayMarks(id);
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e);
}
return "Not found";
}
public void postDoubt(String message) {
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=studentServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
counter+=1;
counter%=3;
System.out.println(message);
Student stub =(Student) registry.lookup(server);
stub.postDoubt(message);
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e);
}
}
public void uploadAssignment(byte[] buffer,int length,String fname)
{
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=studentServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
counter+=1;
counter%=3;
Student stub =(Student) registry.lookup(server);
stub.uploadAssignment(buffer,length,fname);
}
catch(Exception e)
{
System.out.println("ERROR");
System.out.println(e);
}
}
public void grade_assignment() {
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=teacherServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
System.out.println("Request : Upload ");
counter+=1;
counter%=3;
Teacher stub =(Teacher) registry.lookup(server);
stub.grade_assignment();
}
catch(Exception e)
{
}
}
public void view_assignment() {
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=teacherServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
System.out.println("Request : View Assignment ");
counter+=1;
counter%=3;
Teacher stub =(Teacher) registry.lookup(server);
stub.view_assignment();
}
catch(Exception e)
{
}
}
public void post_announcement(String announce) {
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=teacherServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
System.out.println("Request : Announcment ");
counter+=1;
counter%=3;
Teacher stub =(Teacher) registry.lookup(server);
stub.post_announcement(announce);
}
catch(Exception e)
{
}
}
public boolean enter_marks(int resource_key,String message) {
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=studentServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
counter+=1;
counter%=3;
Teacher stub =(Teacher) registry.lookup(server);
stub.enter_marks(message);
return true;
}
catch(Exception e)
{
return false;
}
}
public String get_marks(int id)
{
try
{
Registry registry = LocateRegistry.getRegistry(null);
String server=teacherServerList[counter];
System.out.println("Server allocated is "+server.charAt(6));
counter+=1;
counter%=3;
Teacher stub =(Teacher) registry.lookup(server);
return stub.get_marks(id);
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e);
}
return "Not found";
}
public boolean acquire(int resource_key)
{
if(mutex_locks_marks.get(resource_key))
{
// queue[queue.length]=queue.length;
System.out.println("Locked");
return false;
}
else
{
mutex_locks_marks.replace(resource_key,true);
System.out.println("granted");
System.out.println(mutex_locks_marks);
return true;
}
}
public boolean release(int resource_key)
{
mutex_locks_marks.replace(resource_key,false);
System.out.println("Released");
return true;
}
}