1 /*
2 * Created on Oct 6, 2003
3 * Copyright (c) 2003. All rights reserved.
4 */
5 package portaview.server;
6
7 import java.util.Hashtable;
8 import java.util.List;
9 import java.util.Vector;
10
11 import org.apache.xmlrpc.AsyncCallback;
12 import org.apache.xmlrpc.XmlRpcClient;
13
14 import portaview.Consts;
15 import portaview.Log;
16 import portaview.Registry;
17 import portaview.model.Album;
18 import portaview.model.AlbumCollection;
19 import portaview.model.AlbumModel;
20 import portaview.model.PhotoModel;
21 import portaview.model.PortaViewModel;
22 import portaview.model.SlideSettings;
23 import portaview.util.ImageUtils;
24
25 /***
26 * Provides the API for client calls to remote PortaViews.
27 *
28 * @author <a href="mailto:wwlee1@uiuc.edu">William Lee</a>
29 * @version $Id: PortaViewClient.java,v 1.10 2003/12/10 06:18:57 wlee Exp $
30 */
31 public class PortaViewClient
32 {
33 /***
34 * The viewer server that contains all data about the connections. Note that the client
35 * depends on the server instance recorded in the registry. You need to call the
36 * Registry.setSever() first
37 */
38 public PortaViewClient()
39 {
40 }
41
42 /***
43 * Updates the remote PortaView model. We usually do this when the model has changed
44 * and we want to notify the remote PortaView about this change.
45 * @param model the model to update.
46 */
47 public void updatePortaViewModel(PortaViewModel model)
48 {
49 Log.debug("c: updating portaview model: " + model.getName());
50 try
51 {
52 Hashtable rtn =
53 (Hashtable) (getClientToHost(model
54 .getInetAddress()
55 .getHostAddress())
56 .execute("main.getStatus", new Vector()));
57 SlideSettings ss = new SlideSettings(rtn);
58 if (model.getSlideSettings().equals(ss))
59 {
60 return;
61 }
62 else
63 {
64 model.setSlideSettings(ss);
65 }
66 }
67 catch (Exception e)
68 {
69 e.printStackTrace();
70 }
71 }
72
73 /***
74 * Register this PortaView with the master.
75 * @return true if registration is successful, false otherwise.
76 */
77 public boolean registerWithMaster()
78 {
79 Log.debug("c: register current portaview with master");
80 try
81 {
82 Vector p = new Vector();
83 p.add(Registry.getServer().getName());
84 p.add(Registry.getServer().getInetAddress().getHostAddress());
85 Boolean rtn =
86 (Boolean) getClientToMaster().execute("main.register", p);
87 return rtn.booleanValue();
88 }
89 catch (Exception e)
90 {
91 e.printStackTrace();
92 return false;
93 }
94 }
95
96 /***
97 * Unregister this PortaView with the master.
98 * @return true if successfully unregistered from master, false otherwise.
99 */
100 public boolean unregisterWithMaster()
101 {
102 Log.debug("c: unregister current portaview with master");
103 try
104 {
105 Vector p = new Vector();
106 p.add(Registry.getServer().getName());
107 getClientToMaster().execute("main.unregister", p);
108 return true;
109 }
110 catch (Exception e)
111 {
112 e.printStackTrace();
113 }
114 return true;
115 }
116
117 /***
118 * Retrieves the photo with a given name.
119 * @param album album name
120 * @param name photo name
121 * @param callback async callback that handles the return of the XML-RPC call.
122 */
123 public void getPhotoWithName(
124 String album,
125 String name,
126 AsyncCallback callback)
127 {
128 Log.debug("c: Getting photo with name " + album + ":" + name);
129 try
130 {
131 Vector p = new Vector();
132 p.add(album);
133 p.add(name);
134 getClientToMaster().executeAsync(
135 "main.getPhotoWithName",
136 p,
137 callback);
138 }
139 catch (Exception e)
140 {
141 // error
142 e.printStackTrace();
143 }
144 }
145
146 /***
147 * Returns a list of AlbumModels from the master.
148 */
149 public AlbumCollection getAlbumsFromMaster()
150 {
151 Log.debug("c: Getting albums from master.");
152 Vector p = new Vector();
153 try
154 {
155 Vector albums =
156 (Vector) getClientToMaster().execute("main.getAlbums", p);
157 // the return is a vector of hashes of string. The second level
158 // of vectors are the photos associated with it.
159 AlbumCollection rtn = new AlbumCollection();
160 for (int i = 0; i < albums.size(); i++)
161 {
162 Hashtable ht = (Hashtable) albums.get(i);
163 String name = (String) ht.get(Album.XT_NAME);
164 AlbumModel am = new AlbumModel(name);
165 Vector photos = (Vector) ht.get(Album.XT_PHOTOS);
166 List rtnp = new Vector();
167 for (int j = 0; j < photos.size(); j++)
168 {
169 rtnp.add(new PhotoModel(am, (String) photos.get(j)));
170 }
171 am.setPhotos(rtnp);
172 am.setImage(
173 ImageUtils.getImage((byte[]) ht.get(Album.XT_ICON)));
174 rtn.add(am);
175 }
176 return rtn;
177
178 }
179 catch (Exception e)
180 {
181 // error
182 e.printStackTrace();
183 }
184 return null;
185
186 }
187
188 /***
189 * Updates the remote portaview with the portaview's settings.
190 * @param pvm PortaViewModel that is used to update the remote PortaView.
191 */
192 public void updateRemotePortaView(PortaViewModel pvm)
193 {
194 Log.debug(
195 "c: Updating remote PortaView: "
196 + pvm.getName()
197 + "@"
198 + pvm.getInetAddress());
199 Vector p = new Vector();
200 p.add(pvm.getSlideSettings().toHashtable());
201 try
202 {
203 getClientToHost(
204 pvm.getInetAddress().getHostAddress()).execute(
205 "main.update",
206 p);
207 }
208 catch (Exception e)
209 {
210 e.printStackTrace();
211 }
212 }
213
214 /***
215 * Tries to retrieve the full image from the master. This uses an asynchronous XML-RPC call
216 * to retrieve the image, since potentially it can be big and take a long time.
217 */
218 public void getPhotoFromMaster(SlideSettings ss, AsyncCallback callback)
219 {
220 Log.debug("c: Getting photo from master:" + ss);
221 String slide = ss.getSlide();
222 int index = ss.getIndex();
223 Vector p = new Vector();
224 synchronized (this)
225 {
226 p.add(slide);
227 p.add(new Integer(index));
228 }
229 try
230 {
231 getClientToMaster().executeAsync("main.getPhoto", p, callback);
232 }
233 catch (Exception e)
234 {
235 // error
236 e.printStackTrace();
237 }
238 }
239
240 /***
241 * Returns the client that can talk to the particular portaview host.
242 * @param host the host name/IP of the remote host.
243 * @return the XML-RPC client object.
244 * @throws Exception when the URL generated is not valid for the given host.
245 */
246 private XmlRpcClient getClientToHost(String host) throws Exception
247 {
248 XmlRpcClient xmlrpc =
249 new XmlRpcClient(
250 PortaViewServer.XRPC_PROTO
251 + "://"
252 + host
253 + ":"
254 + Consts.XML_RPC_PORT
255 + "/RPC2");
256 //Log.debug("Client Executing call to " + xmlrpc.getURL());
257 return xmlrpc;
258 }
259
260 /***
261 * Returns the XML-RPC client to the master server.
262 */
263 private XmlRpcClient getClientToMaster() throws Exception
264 {
265 if (Registry.getServer().getMasterIP() == null)
266 throw new Exception("Master IP not set");
267 return getClientToHost(
268 Registry.getServer().getMasterIP().getHostAddress());
269
270 }
271 }
This page was automatically generated by Maven