1 package be
.nikiroo
.jvcard
.remote
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileNotFoundException
;
8 import java
.io
.FileOutputStream
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStreamReader
;
11 import java
.io
.OutputStreamWriter
;
12 import java
.net
.Socket
;
13 import java
.net
.UnknownHostException
;
14 import java
.security
.InvalidParameterException
;
15 import java
.util
.HashMap
;
16 import java
.util
.LinkedList
;
17 import java
.util
.List
;
19 import java
.util
.MissingResourceException
;
20 import java
.util
.ResourceBundle
;
22 import be
.nikiroo
.jvcard
.Card
;
23 import be
.nikiroo
.jvcard
.Contact
;
24 import be
.nikiroo
.jvcard
.Data
;
25 import be
.nikiroo
.jvcard
.parsers
.Format
;
26 import be
.nikiroo
.jvcard
.parsers
.Vcard21Parser
;
27 import be
.nikiroo
.jvcard
.resources
.Bundles
;
28 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
31 * This class will synchronise {@link Card}s between a local instance an a
32 * remote jVCard server.
38 /** The time in ms after which we declare that 2 timestamps are different */
39 static private final int GRACE_TIME
= 2000;
41 /** Directory where to store local cache of remote {@link Card}s. */
42 static private File cacheDir
;
45 * Directory where to store cache of remote {@link Card}s without
46 * modifications since the last synchronisation.
48 static private File cacheDirOrig
;
49 /** Directory where to store timestamps for files in cacheDirOrig */
50 static private File cacheDirOrigTS
;
52 static private boolean autoSync
;
56 /** Resource name on the remote server. */
60 * Create a new {@link Sync} object, ready to operate for the given resource
61 * on the given server.
64 * Note that the format used is the standard "host:port_number/file", with
65 * an optional <tt>jvcard://</tt> prefix.
69 * E.g.: <tt>jvcard://localhost:4444/family.vcf</tt>
73 * the server and port to contact, optionally prefixed with
76 * @throws InvalidParameterException
77 * if the remote configuration file <tt>remote.properties</tt>
78 * cannot be accessed or if the cache directory cannot be used
80 public Sync(String url
) {
81 if (cacheDir
== null) {
86 url
= url
.replace("jvcard://", "");
87 int indexSl
= url
.indexOf('/');
88 this.name
= url
.substring(indexSl
+ 1);
89 url
= url
.substring(0, indexSl
);
90 this.host
= url
.split("\\:")[0];
91 this.port
= Integer
.parseInt(url
.split("\\:")[1]);
92 } catch (Exception e
) {
93 throw new InvalidParameterException(
94 "the given parameter was not a valid HOST:PORT value: "
100 * Create a new {@link Sync} object, ready to operate on the given server.
104 * the server to contact
108 * the resource name to synchronise to
110 public Sync(String host
, int port
, String name
) {
117 * Check if the synchronisation is available for this resource.
119 * @return TRUE if it is possible to contact the remote server and that this
120 * server has the resource available
122 public boolean isAvailable() {
124 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
125 "check avail client");
127 s
.sendCommand(Command
.LIST_CARD
);
128 List
<String
> timestampedFiles
= s
.receiveBlock();
131 for (String timestampedFile
: timestampedFiles
) {
132 String file
= timestampedFile
.substring(StringUtils
.fromTime(0)
134 if (file
.equals(name
)) {
138 } catch (Exception e
) {
144 // return: synced or not
146 public Card
sync(boolean force
) throws UnknownHostException
, IOException
{
148 long tsOriginal
= getLastModified();
150 Card local
= new Card(getCache(cacheDir
), Format
.VCard21
);
151 local
.setRemote(true);
153 // do NOT update unless we are in autoSync or forced mode or we don't
154 // have the file on cache
155 if (!autoSync
&& !force
&& tsOriginal
!= -1) {
159 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
), "sync client");
161 // get the server time stamp
165 s
.sendCommand(Command
.LIST_CARD
);
166 List
<String
> timestampedFiles
= s
.receiveBlock();
168 for (String timestampedFile
: timestampedFiles
) {
169 String file
= timestampedFile
.substring(StringUtils
.fromTime(0)
171 if (file
.equals(name
)) {
172 tsServer
= StringUtils
.toTime(timestampedFile
.substring(0,
173 StringUtils
.fromTime(0).length()));
179 // - file not preset neither in cache nor on server
180 // - remote < previous
181 if ((tsServer
== -1 && tsOriginal
== -1)
182 || (tsServer
!= -1 && tsOriginal
!= -1 && ((tsOriginal
- tsServer
) > GRACE_TIME
))) {
183 throw new IOException(
184 "The timestamps between server and client are invalid");
188 boolean serverChanges
= (tsServer
- tsOriginal
) > GRACE_TIME
;
189 boolean localChanges
= false;
190 Card original
= null;
191 if (tsOriginal
!= -1) {
192 original
= new Card(getCache(cacheDirOrig
), Format
.VCard21
);
193 localChanges
= !local
.isEquals(original
, true);
196 Command action
= null;
198 // Sync to server if:
200 action
= Command
.PUT_CARD
;
203 // Sync from server if:
205 action
= Command
.HASH_CONTACT
;
208 // Sync from/to server if
209 if (serverChanges
&& localChanges
) {
211 action
= Command
.HELP
;
214 // PUT the whole file if:
215 if (tsServer
== -1) {
216 action
= Command
.POST_CARD
;
219 // GET the whole file if:
220 if (tsOriginal
== -1) {
221 action
= Command
.GET_CARD
;
224 System
.err
.println("remote: " + (tsServer
/ 1000) % 1000 + " ("
226 System
.err
.println("previous: " + (tsOriginal
/ 1000) % 1000 + " ("
228 System
.err
.println("local changes: " + localChanges
);
229 System
.err
.println("server changes: " + serverChanges
);
230 System
.err
.println("choosen action: " + action
);
232 if (action
!= null) {
233 s
.sendCommand(Command
.SELECT
, name
);
234 if (tsServer
!= StringUtils
.toTime(s
.receiveLine())) {
235 System
.err
.println("DEBUG: it changed. retry.");
236 s
.sendCommand(Command
.SELECT
);
243 s
.sendCommand(Command
.GET_CARD
);
244 List
<String
> data
= s
.receiveBlock();
245 setLastModified(data
.remove(0));
246 Card server
= new Card(Vcard21Parser
.parseContact(data
));
247 local
.replaceListContent(server
);
251 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
254 s
.sendCommand(Command
.POST_CARD
);
255 s
.sendBlock(Vcard21Parser
.toStrings(local
));
256 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
257 setLastModified(s
.receiveLine());
260 List
<Contact
> added
= new LinkedList
<Contact
>();
261 List
<Contact
> removed
= new LinkedList
<Contact
>();
262 List
<Contact
> from
= new LinkedList
<Contact
>();
263 List
<Contact
> to
= new LinkedList
<Contact
>();
264 original
.compare(local
, added
, removed
, from
, to
);
266 s
.sendCommand(Command
.PUT_CARD
);
268 for (Contact c
: removed
) {
269 s
.sendCommand(Command
.DELETE_CONTACT
, c
.getId());
271 for (Contact c
: added
) {
272 s
.sendCommand(Command
.POST_CONTACT
, c
.getId());
273 s
.sendBlock(Vcard21Parser
.toStrings(c
, -1));
275 if (from
.size() > 0) {
276 for (int index
= 0; index
< from
.size(); index
++) {
277 Contact f
= from
.get(index
);
278 Contact t
= to
.get(index
);
280 List
<Data
> subadded
= new LinkedList
<Data
>();
281 List
<Data
> subremoved
= new LinkedList
<Data
>();
282 f
.compare(t
, subadded
, subremoved
, subremoved
,
284 s
.sendCommand(Command
.PUT_CONTACT
, name
);
285 for (Data d
: subremoved
) {
286 s
.sendCommand(Command
.DELETE_DATA
,
287 d
.getContentState());
289 for (Data d
: subadded
) {
290 s
.sendCommand(Command
.POST_DATA
,
291 d
.getContentState());
292 s
.sendBlock(Vcard21Parser
.toStrings(d
));
297 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
298 s
.sendCommand(Command
.PUT_CARD
);
299 setLastModified(s
.receiveLine());
304 s
.sendCommand(Command
.PUT_CARD
);
306 s
.sendCommand(Command
.LIST_CONTACT
);
307 Map
<String
, String
> remote
= new HashMap
<String
, String
>();
308 for (String line
: s
.receiveBlock()) {
309 int indexSp
= line
.indexOf(" ");
310 String hash
= line
.substring(0, indexSp
);
311 String uid
= line
.substring(indexSp
+ 1);
313 remote
.put(uid
, hash
);
316 List
<Contact
> deleted
= new LinkedList
<Contact
>();
317 List
<Contact
> changed
= new LinkedList
<Contact
>();
318 List
<String
> added
= new LinkedList
<String
>();
320 for (Contact c
: local
) {
321 String hash
= remote
.get(c
.getId());
324 } else if (!hash
.equals(c
.getContentState())) {
329 for (String uid
: remote
.keySet()) {
330 if (local
.getById(uid
) == null)
336 for (Contact c
: deleted
) {
340 for (String uid
: added
) {
341 s
.sendCommand(Command
.GET_CONTACT
, uid
);
342 for (Contact cc
: Vcard21Parser
.parseContact(s
348 for (Contact c
: changed
) {
350 s
.sendCommand(Command
.GET_CONTACT
, c
.getId());
351 for (Contact cc
: Vcard21Parser
.parseContact(s
358 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
359 s
.sendCommand(Command
.PUT_CARD
);
360 setLastModified(s
.receiveLine());
365 throw new IOException(action
366 + " operation not supported yet :(");
369 s
.sendCommand(Command
.SELECT
);
371 } catch (IOException e
) {
373 } catch (Exception e
) {
384 * Return the requested cache for the current resource.
389 * @return the cached {@link File}
391 private File
getCache(File dir
) {
392 return new File(dir
.getPath() + File
.separator
+ name
);
396 * Return the cached {@link File} corresponding to the current resource.
398 * @return the cached {@link File}
400 public File
getCache() {
401 return new File(cacheDir
.getPath() + File
.separator
+ name
);
405 * Get the last modified date of the current resource's original cached
406 * file, that is, the time the server reported as the "last modified time"
407 * when this resource was transfered.
409 * @return the last modified time from the server back when this resource
412 public long getLastModified() {
414 BufferedReader in
= new BufferedReader(new InputStreamReader(
415 new FileInputStream(cacheDirOrigTS
.getPath()
416 + File
.separator
+ name
)));
417 String line
= in
.readLine();
420 return StringUtils
.toTime(line
);
421 } catch (FileNotFoundException e
) {
423 } catch (Exception e
) {
429 * Set the last modified date of the current resource's original cached
430 * file, that is, the time the server reported as the "last modified time"
431 * when this resource was transfered.
434 * the last modified time from the server back when this resource
437 public void setLastModified(String time
) {
439 BufferedWriter out
= new BufferedWriter(new OutputStreamWriter(
440 new FileOutputStream(cacheDirOrigTS
.getPath()
441 + File
.separator
+ name
)));
445 } catch (FileNotFoundException e
) {
447 } catch (IOException e
) {
453 * Configure the synchronisation mechanism (cache, auto update...).
455 * @throws InvalidParameterException
456 * if the remote configuration file <tt>remote.properties</tt>
457 * cannot be accessed or if the cache directory cannot be used
459 static private void config() {
461 ResourceBundle bundle
= Bundles
.getBundle("remote");
464 dir
= bundle
.getString("CLIENT_CACHE_DIR").trim();
466 cacheDir
= new File(dir
+ File
.separator
+ "current");
468 cacheDirOrig
= new File(dir
+ File
.separator
+ "original");
469 cacheDirOrig
.mkdir();
470 cacheDirOrigTS
= new File(dir
+ File
.separator
+ "timestamps");
471 cacheDirOrigTS
.mkdir();
473 if (!cacheDir
.exists() || !cacheDirOrig
.exists()) {
474 throw new IOException("Cannot open or create cache store at: "
478 String autoStr
= bundle
.getString("CLIENT_AUTO_SYNC");
479 if (autoStr
!= null && autoStr
.trim().equalsIgnoreCase("true")) {
483 } catch (MissingResourceException e
) {
484 throw new InvalidParameterException(
485 "Cannot access remote.properties configuration file");
486 } catch (Exception e
) {
487 throw new InvalidParameterException(
488 "Cannot open or create cache store at: " + dir
);