source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java@ 1523

Last change on this file since 1523 was 1523, checked in by framm, 15 years ago
  • Major redesign of how JOSM talks to the OSM server. Connections now all go through a new OsmApi class that finds out which version the server uses. JOSM should now be able to handle 0.5 and 0.6 without configuration change. Config options osm-server.version and osm-server.additional-versions now obsolete. Handling of error and cancel situations might still need some improvement.
  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.LinkedList;
8
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
14
15/**
16 * Class that uploads all changes to the osm server.
17 *
18 * This is done like this: - All objects with id = 0 are uploaded as new, except
19 * those in deleted, which are ignored - All objects in deleted list are
20 * deleted. - All remaining objects with modified flag set are updated.
21 */
22public class OsmServerWriter {
23
24 /**
25 * This list contains all successfully processed objects. The caller of
26 * upload* has to check this after the call and update its dataset.
27 *
28 * If a server connection error occurs, this may contain fewer entries
29 * than where passed in the list to upload*.
30 */
31 public Collection<OsmPrimitive> processed;
32
33 private OsmApi api = new OsmApi();
34
35 private static final int MSECS_PER_SECOND = 1000;
36 private static final int SECONDS_PER_MINUTE = 60;
37 private static final int MSECS_PER_MINUTE = MSECS_PER_SECOND * SECONDS_PER_MINUTE;
38
39 long uploadStartTime;
40
41 public String timeLeft(int progress, int list_size) {
42 long now = System.currentTimeMillis();
43 long elapsed = now - uploadStartTime;
44 if (elapsed == 0)
45 elapsed = 1;
46 float uploads_per_ms = (float)progress / elapsed;
47 float uploads_left = list_size - progress;
48 int ms_left = (int)(uploads_left / uploads_per_ms);
49 int minutes_left = ms_left / MSECS_PER_MINUTE;
50 int seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE ;
51 String time_left_str = Integer.toString(minutes_left) + ":";
52 if (seconds_left < 10)
53 time_left_str += "0";
54 time_left_str += Integer.toString(seconds_left);
55 return time_left_str;
56 }
57
58 /**
59 * Send the dataset to the server.
60 * @param the_version version of the data set
61 * @param list list of objects to send
62 */
63 public void uploadOsm(String the_version, Collection<OsmPrimitive> list) {
64 processed = new LinkedList<OsmPrimitive>();
65 api.initialize();
66
67 Main.pleaseWaitDlg.progress.setMaximum(list.size());
68 Main.pleaseWaitDlg.progress.setValue(0);
69
70 boolean useChangesets = api.hasChangesetSupport();
71
72 // controls whether or not we try and upload the whole bunch in one go
73 boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload",
74 "0.6".equals(api.getVersion()));
75
76 // solicit commit comment from user
77 String comment = null;
78 while (useChangesets && comment == null) {
79 comment = JOptionPane.showInputDialog(Main.parent,
80 tr("Provide a brief comment for the changes you are uploading:"),
81 tr("Commit comment"), JOptionPane.QUESTION_MESSAGE);
82 if (comment == null)
83 return;
84 // Don't let people just hit enter
85 if (comment.trim().length() >= 3)
86 break;
87 comment = null;
88 }
89
90 // create changeset if required
91 try {
92 if (useChangesets) api.createChangeset(comment);
93 } catch (OsmTransferException ex) {
94 dealWithTransferException(ex);
95 return;
96 }
97
98 try {
99 if (useDiffUploads) {
100 // all in one go
101 processed.addAll(api.uploadDiff(list));
102 } else {
103 // upload changes individually (90% of code is for the status display...)
104 NameVisitor v = new NameVisitor();
105 uploadStartTime = System.currentTimeMillis();
106 for (OsmPrimitive osm : list) {
107 osm.visit(v);
108 int progress = Main.pleaseWaitDlg.progress.getValue();
109 String time_left_str = timeLeft(progress, list.size());
110 Main.pleaseWaitDlg.currentAction.setText(
111 tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
112 Math.round(100.0*progress/list.size()), progress,
113 list.size(), time_left_str, tr(v.className), v.name, osm.id));
114 makeApiRequest(osm);
115 processed.add(osm);
116 Main.pleaseWaitDlg.progress.setValue(progress+1);
117 }
118 }
119 if (useChangesets) api.stopChangeset();
120 } catch (OsmTransferException e) {
121 try {
122 if (useChangesets) api.stopChangeset();
123 } catch (Exception ee) {
124 // ignore nested exception
125 }
126 dealWithTransferException(e);
127 }
128 }
129
130 void makeApiRequest(OsmPrimitive osm) throws OsmTransferException {
131 if (osm.deleted) {
132 api.deletePrimitive(osm);
133 } else if (osm.id == 0) {
134 api.createPrimitive(osm);
135 } else {
136 api.modifyPrimitive(osm);
137 }
138 }
139
140 private void dealWithTransferException (OsmTransferException e) {
141 Main.pleaseWaitDlg.currentAction.setText(tr("Transfer aborted due to error (will wait for 5 seconds):") + e.getMessage());
142 try {
143 Thread.sleep(5000);
144 }
145 catch (InterruptedException ex) {}
146 }
147}
Note: See TracBrowser for help on using the repository browser.