1 |
package appl.parallel.data; |
2 |
|
3 |
import java.awt.Rectangle; |
4 |
import java.io.IOException; |
5 |
import java.io.Serializable; |
6 |
import java.net.InetAddress; |
7 |
import java.net.UnknownHostException; |
8 |
import java.rmi.Naming; |
9 |
|
10 |
import org.apache.log4j.LogManager; |
11 |
import org.apache.log4j.Logger; |
12 |
|
13 |
import appl.data.LoadingException; |
14 |
import appl.parallel.client.ClientDataServer; |
15 |
import appl.parallel.client.DataServer; |
16 |
import appl.parallel.spmd.split.DataPartition; |
17 |
|
18 |
/** |
19 |
* Loads partitioned data from a {@link ClientDataServer}. And unloads the data |
20 |
* |
21 |
* @author Dominik Appl |
22 |
*/ |
23 |
public class XuluClientLoader extends AbstractDataHandler implements |
24 |
PartitionDataHandler { |
25 |
|
26 |
protected InetAddress address; |
27 |
|
28 |
protected String namingString; |
29 |
|
30 |
private String unloadTarget = null; |
31 |
|
32 |
/** |
33 |
* constructs a new {@link XuluClientLoader}. The local IP adress at the |
34 |
* time of construction is later used on serverside for communication (e.g. |
35 |
* unloading). |
36 |
* |
37 |
* @param rootID |
38 |
* the id of the data |
39 |
* @param partitionBounds |
40 |
* the bounds of the partition to be retrieved on server side |
41 |
* @param unloadBounds |
42 |
* the bounds of the partition which is to be uploaded to the |
43 |
* client after calculation |
44 |
*/ |
45 |
public XuluClientLoader(int rootID, ClientDataServer client, |
46 |
Rectangle partitionBounds, Rectangle unloadBounds) { |
47 |
super(rootID, client, partitionBounds, unloadBounds); |
48 |
address = null; |
49 |
namingString = null; |
50 |
try { |
51 |
this.address = InetAddress.getLocalHost(); |
52 |
unloadTarget = address.getHostAddress(); |
53 |
namingString = "rmi://" + unloadTarget + "/DataServer"; |
54 |
} catch (UnknownHostException e) { |
55 |
LOG.error("The client IP could not be determined!!", e); |
56 |
e.printStackTrace(); |
57 |
} |
58 |
} |
59 |
|
60 |
/** |
61 |
* constructs a new {@link XuluClientLoader}. The unloading is done to the |
62 |
* {@link DataServer} at the machine given with the parameter unloadTarget |
63 |
* (IP/Adress or Hostname). |
64 |
* |
65 |
* @param rootID |
66 |
* the id of the data |
67 |
* @param partitionBounds |
68 |
* the bounds of the partition to be retrieved on server side |
69 |
* @param unloadBounds |
70 |
* the bounds of the partition which is to be uploaded to the |
71 |
* client after calculation |
72 |
*/ |
73 |
public XuluClientLoader(int rootID, ClientDataServer client, |
74 |
Rectangle partitionBounds, Rectangle unloadBounds, |
75 |
String unloadTarget) { |
76 |
spmdClient = client; |
77 |
this.unloadTarget = unloadTarget; |
78 |
address = null; |
79 |
namingString = null; |
80 |
try { |
81 |
this.address = InetAddress.getLocalHost(); |
82 |
namingString = "rmi://" + unloadTarget + "/DataServer"; |
83 |
} catch (UnknownHostException e) { |
84 |
LOG.error("The client IP could not be determined!!", e); |
85 |
e.printStackTrace(); |
86 |
} |
87 |
this.rootID = rootID; |
88 |
this.partitionBounds = partitionBounds; |
89 |
this.unloadBounds = unloadBounds; |
90 |
} |
91 |
|
92 |
public XuluClientLoader newInstance(int rootID, ClientDataServer client, |
93 |
Rectangle partitionBounds, Rectangle unloadBounds) { |
94 |
return new XuluClientLoader(rootID, client, partitionBounds, |
95 |
unloadBounds); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Used for deserialization. Do not use for other purposes |
100 |
*/ |
101 |
public XuluClientLoader() { |
102 |
super(); |
103 |
} |
104 |
|
105 |
/* |
106 |
* (non-Javadoc) |
107 |
* |
108 |
* @see appl.data.DataLoader#getLoadInfo() |
109 |
*/ |
110 |
public String getLoadInfo() { |
111 |
return "XuluClientLoader tries to load data with rootID " + rootID |
112 |
+ " from IP: " + address.getHostAddress() + " with naming: " |
113 |
+ namingString; |
114 |
} |
115 |
|
116 |
/* |
117 |
* (non-Javadoc) |
118 |
* |
119 |
* @see appl.data.DataLoader#getLoadInfo() |
120 |
*/ |
121 |
public String getUnloadInfo() { |
122 |
return "XuluClientLoader tries to unload data with rootID " + rootID |
123 |
+ " to IP: " + address.getHostAddress() + " with naming: " |
124 |
+ namingString; |
125 |
} |
126 |
|
127 |
/* |
128 |
* (non-Javadoc) |
129 |
* |
130 |
* @see appl.data.DataLoader#load() |
131 |
*/ |
132 |
public DataPartition load() throws LoadingException { |
133 |
try { |
134 |
LOG.debug(getLoadInfo()); |
135 |
DataServer client = (DataServer) Naming.lookup(namingString); |
136 |
data = client.getPartition(rootID, partitionBounds); |
137 |
LOG.debug("Partition with id " + rootID |
138 |
+ " successfully retrieved from client"); |
139 |
return data; |
140 |
} catch (Exception e) { |
141 |
LOG.error("Partition retrieval failed: " + e.getMessage()); |
142 |
} |
143 |
return null; |
144 |
} |
145 |
|
146 |
/** |
147 |
* Initializes the {@link Logger} after deserialization (used by |
148 |
* {@link java.io.Serializable} |
149 |
* |
150 |
* @author Dominik Appl |
151 |
* @param s |
152 |
* @throws IOException |
153 |
* @see Serializable |
154 |
*/ |
155 |
private synchronized void readObject(java.io.ObjectInputStream s) |
156 |
throws IOException, ClassNotFoundException { |
157 |
// reading standard fields |
158 |
s.defaultReadObject(); |
159 |
// initialize Logger |
160 |
LOG = LogManager.getLogger(this.getClass().getName()); |
161 |
} |
162 |
|
163 |
/* |
164 |
* (non-Javadoc) |
165 |
* |
166 |
* @see appl.data.DataUnloader#unload() |
167 |
*/ |
168 |
public void unload() { |
169 |
try { |
170 |
if (LOG.isDebugEnabled()) |
171 |
LOG.debug(getUnloadInfo()); |
172 |
if (data == null) { |
173 |
if (LOG.isDebugEnabled()) |
174 |
LOG.debug("No data loaded -> nothing to unload"); |
175 |
return; |
176 |
} |
177 |
// construct return grid only if necessary |
178 |
DataPartition returnPartition = null; |
179 |
if (unloadBounds.equals(data.getPartitionBounds())) |
180 |
returnPartition = data; |
181 |
else |
182 |
returnPartition = data.getPartition(unloadBounds); |
183 |
// if there is a local spmdClient try to use it |
184 |
if (spmdClient != null) |
185 |
spmdClient.updatePartition(rootID, returnPartition, |
186 |
unloadBounds); |
187 |
// else connect remotely to client |
188 |
else { |
189 |
DataServer client = (DataServer) Naming.lookup(namingString); |
190 |
client.updatePartition(rootID, returnPartition, unloadBounds); |
191 |
} |
192 |
|
193 |
LOG.debug("Partition with id " + rootID |
194 |
+ " was transfered back to client!"); |
195 |
} catch (Exception e) { |
196 |
LOG.error("Backtransfer of data to client failed!", e); |
197 |
} |
198 |
} |
199 |
|
200 |
} |