RZ

001: // ControlRemote.cc # the actual synthesize algorithm
002:
003: #include <iostream>
004: #include <iterator>
005:
006: #include "RZError.hh"
007: #include "GetoptVerbose.hh"
008: #include "DataPackets.hh"
009: #include "ConReservation.hh"
010: #include "ControlRemote.hh"
011:
012: // ######## ######## initial header retrieval request ######## ########
013:
014: void RZ::ControlRemote::initial_mark()
015: {
016:     fwd_border = rz_wrapper.rz_at_begin;
017:
018:     // find highest block not wholely contained < $first_packet_from
019:     {
020:         unsigned int rzj = file_fwd_border - rz_wrapper.wrapper_size;
021:         while (rzj > rz_wrapper.rz_at[ fwd_border ]) ++fwd_border;
022:         while (rzj < rz_wrapper.rz_at[ fwd_border ]) --fwd_border;
023:     }
024:     mark_intervals.clear();
025:     mark_intervals.push_back(fwd_border);
026:
027:     ConReservation::writeremotetab(0, fwd_border);
028:
029:     if (fwd_border >= rz_wrapper.rz_at_end) throw Finished();
030: }
031:
032: // ######## ######## later retrieval requests ######## ########
033:
034: void RZ::ControlRemote::mark()
035: {
036:     mark_intervals.clear();
037:     mark_intervals.push_back(fwd_border);
038:
039:     fwdmultipleintervals(mark_intervals, packet_limit);
040:     fwd_border = mark_intervals.front();
041:     file_fwd_border = rz_wrapper.wrapper_size + rz_wrapper.rz_at[ fwd_border ];
042:
043:     if (fwd_border >= rz_wrapper.rz_at_end) throw Finished();
044: }
045:
046: // ######## ######## wait on need ######## ########
047:
048: //static const char bytes_equals[] = "bytes=";
049:
050: void RZ::ControlRemote::resolve(std::string &bytes)
051: {
052:     std::vector< int > g;
053:     g.push_back(fwd_border);
054:     ConReservation::fwdmultipleintervals(g, packet_limit);
055:     int fwdhorizon = g.back();
056:
057:     if (g.size() <= 1) throw Finished();
058:
059:     bytes.clear();
060:     //bytes.insert(bytes.end(), bytes_equals, bytes_equals + 6);
061:     bytes.append("bytes=", 6);
062:     std::vector< int >::iterator interval = g.begin(), intervalend = g.end();
063:     --intervalend;
064:     {
065:         int at = *interval++;
066:         int to = *interval++;
067:         ConReservation::writeremotetab(at, to);
068:
069:         unsigned int fileat = at > fwd_border
070:             ? rz_wrapper.wrapper_size + rz_wrapper.rz_at[ at ]
071:             : file_fwd_border;
072:         unsigned int fileto = rz_wrapper.wrapper_size + rz_wrapper.rz_at[ to ];
073:
074:         char *sbuf;
075:         int slen = asprintf(&sbuf, "%u-%u,", fileat, fileto - 1);
076:         //bytes.insert(bytes.end(), sbuf, sbuf + slen);
077:         bytes.append(sbuf, slen);
078:     }
079:     while (interval < intervalend) {
080:         int at = *interval++;
081:         int to = *interval++;
082:         ConReservation::writeremotetab(at, to);
083:
084:         unsigned int fileat = rz_wrapper.wrapper_size + rz_wrapper.rz_at[ at ];
085:         unsigned int fileto = rz_wrapper.wrapper_size + rz_wrapper.rz_at[ to ];
086:
087:         char *sbuf;
088:         int slen = asprintf(&sbuf, "%u-%u,", fileat, fileto - 1);
089:         //bytes.insert(bytes.end(), sbuf, sbuf + slen);
090:         bytes.append(sbuf, slen);
091:     }
092:     //bytes.pop_back();
093:     bytes.erase(--bytes.end());
094:
095:     if (RZ::debug) {
096:         std::ostream_iterator< int > show(std::cerr, " ");
097:         std::cerr << "[";
098:         std::copy(mark_intervals.begin(), mark_intervals.end(), show);
099:         std::cerr << "|";
100:         std::copy(g.begin(), g.end(), show);
101:         std::cerr << "]";
102:     }
103:     if (RZ::debug + RZ::verbose > 2) std::cerr << "[" << bytes << "]";
104:
105:     if (interval == g.end()) {
106:         packet_limit = int(packet_limit * DataPackets::PACKET_INFLATION);
107:         if (RZ::debug) std::cerr << "[INFLATE]";
108:     }
109:
110:     if (!ConReservation::equalintervals(mark_intervals, g)) {
111:         packet_limit = int(packet_limit / DataPackets::PACKET_DEFLATION);
112:         if (packet_limit < (int)DataPackets::FIRST_PACKET_SIZE)
113:             packet_limit = DataPackets::FIRST_PACKET_SIZE;
114:         if (RZ::debug) std::cerr << "[DEFLATE]";
115:     }
116:
117:     fwd_border = fwdhorizon;
118:     file_fwd_border = rz_wrapper.wrapper_size + rz_wrapper.rz_at[ fwd_border ];
119:
120:     bytes.push_back('\n');
121: }
122:
123: // ######## ######## ######## ######## ######## ######## ######## ########
124:
125: // ControlRemote.cc # the actual synthesize algorithm