-
Notifications
You must be signed in to change notification settings - Fork 0
/
MucajMorales.java
189 lines (147 loc) · 4.62 KB
/
MucajMorales.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
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ak.lab;
import net.jini.space.JavaSpace;
/**
*
* @author Rodrigo Morales Jugen Mucaj
*/
public class Worker {
public static Answer makeWorker(Task task) {
return new Answer(task.index, task.aValue, task.bValue);
}
public static void main(String[] args) {
Lookup finder = new Lookup(JavaSpace.class);
JavaSpace space = (JavaSpace) finder.getService();
Task temp = new Task();
try{
System.out.println("Rading incoming task from space");
int counter = 0;
while(true){
Task task = (Task) space.take(temp, null, Long.MAX_VALUE);
System.out.println("Task received. Task: " + task.toString());
counter++;
if(task.index.equals(-1)){
System.out.println("Death pill");
space.write(task, null, 10000);
break;
}
else{
Answer answer = new Answer();
answer.index = task.index;
answer.result = task.aValue + task.bValue;
System.out.println(task.aValue.toString() + " + " + task.bValue.toString() + " = " + answer.result.toString());
space.write(answer, null, Long.MAX_VALUE);
System.out.println("Answer obj sent to space");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ak.lab;
import net.jini.core.entry.Entry;
/**
*
* @author Rodrigo Morales Jugen Mucaj
*/
public class Task implements Entry {
public Integer aValue;
public Integer bValue;
public Integer index;
public Task(){
this.aValue = null;
this.bValue = null;
this.index = null;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ak.lab;
import net.jini.core.entry.Entry;
/**
*
* @author Rodrigo Morales Jugen Mucaj
*/
public class Answer implements Entry{
public Integer index;
public Integer result;
public Answer(){
this.index = null;
this.result = null;
}
public Answer(int index, int a, int b) {
this.index = index;
this.result = a + b;
}
/**
* Turns our Answer obj into a nicely formatted string
* @return
*/
public String toString() {
return "Answer: index " + this.index + " res: " + this.result;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ak.lab;
import java.util.Random;
import net.jini.space.JavaSpace;
/**
*
* @author Rodrigo Morales Jugen Mucaj
*/
public class Master {
public static void main(String[] args) {
System.out.println("witam");
int size = 1000;
Integer[] a = new Integer[size];
Integer[] b = new Integer[size];
Integer[] c = new Integer[size];
Random random = new Random();
int maxValue = 100;
Lookup finder = new Lookup(JavaSpace.class);
JavaSpace space = (JavaSpace) finder.getService();
System.out.println("znalazlem space");
try{
for(
int i = 0; i< size; i++){
a[i] = random.nextInt(maxValue);
b[i] = random.nextInt(maxValue);
Task task = new Task();
task.aValue = a[i];
task.bValue = b[i];
task.index = i;
space.write(task, null, Long.MAX_VALUE);
System.out.println("tasks sent");
}
for(int i = 0; i < size; i++){
Answer temp = new Answer();
Answer answer = (Answer) space.take(temp, null, 100000);
c[answer.index] = answer.result;
System.out.println("index: "+answer.index.toString() + " value: " + answer.result.toString());
}
// System.out.println("Results matrix: \n");
// for (int i = 0; i < ; i++) {
// System.out.println(c[i]);
// }
Task pill = new Task();
pill.index = -1;
space.write(pill,null, 10000);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}