Esempio n. 1
0
        /// <summary>
        /// Syncs one replication file.
        /// </summary>
        /// <returns></returns>
        private bool SyncOne()
        {
            if (_current_source == null)
            {
                int next = _sequenceNumber + 1;

                // try to download the new changes.
                int next_temp = next;
                string folder1 = (next / 1000000).ToString("000");
                next_temp = next_temp - ((next / 1000000) * 1000000);
                string folder = (next_temp / 1000).ToString("000");
                string file = (next_temp - ((next_temp / 1000) * 1000)).ToString("000");

                string url_change = _url + string.Format("/{0}/{1}/{2}.osc.gz", folder1, folder, file);
                string url_state = _url + string.Format("/{0}/{1}/{2}.state.txt", folder1, folder, file);

                try
                {
                    // get changeset file.
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
                        url_state);
                    request.Timeout = 10000;

                     WebResponse myResp = request.GetResponse();

                    Stream stream = myResp.GetResponseStream();
                    string state_file = (new StreamReader(stream)).ReadToEnd();

                    stream.Close();
                    stream.Dispose();

                    // state file was found!
                    string[] state_array = state_file.Split(new string[]{"\n"},StringSplitOptions.None);

                    // download changeset
                    request = (HttpWebRequest)HttpWebRequest.Create(
                        url_change);
                    request.Timeout = 10000;
                    myResp = request.GetResponse();
                    Stream change_stream = myResp.GetResponseStream();
                    GZipStream unzipped_stream = new GZipStream(change_stream, CompressionMode.Decompress);

                    // stream all the data into a byte array.
                    //byte[] changeset_data = unzipped_stream.
                    MemoryStream memory_stream = new MemoryStream();
                    unzipped_stream.CopyTo(memory_stream);
                    unzipped_stream.Close();
                    change_stream.Close();
                    memory_stream.Seek(0, SeekOrigin.Begin);

                    // used the memory stream.
                    XmlDataProcessorChangeSetSource current_source = new XmlDataProcessorChangeSetSource(memory_stream);
                    current_source.Initialize();
                    _sequenceNumber = next;

                    OsmSharp.Tools.Output.OutputStreamHost.WriteLine("");
                    OsmSharp.Tools.Output.OutputStreamHost.WriteLine("Started applying changeset {0}:{1}!", _sequenceNumber,state_array[0]);

                    _current_source = current_source;

                    return true;
                }
                catch (WebException)
                {

                }
            }
            return false;
        }
Esempio n. 2
0
        /// <summary>
        /// Moves to the next changeset.
        /// </summary>
        /// <returns></returns>
        public override bool MoveNext()
        {
            // start downloading changes.
            if (!_started)
            {
                _started = true;
                Thread thr = new Thread(new ThreadStart(Start));
                thr.Start();
            }

            // wait for new data.
            while (_current_source == null)
            {
                Thread.Sleep(50);
            }

            // get the next data.
            if (!_current_source.MoveNext())
            {
                // reset the current source; it's finished!
                _current_source.Close();
                _current_source = null;

                // if the max is passed return false
                if (_sequenceNumber + 1 > _max)
                {
                    this.Stop();

                    return false;
                }

                // wait for any next data to exit.
                this.MoveNext();
            }
            return true;
        }