summaryrefslogtreecommitdiffstats
path: root/src/frontend/terminaloverlay.h
blob: e19601970746184a9b0ef8e9c60d01c119efcead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
    Mosh: the mobile shell
    Copyright 2012 Keith Winstein

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations including
    the two.

    You must obey the GNU General Public License in all respects for all
    of the code used other than OpenSSL. If you modify file(s) with this
    exception, you may extend this exception to your version of the
    file(s), but you are not obligated to do so. If you do not wish to do
    so, delete this exception statement from your version. If you delete
    this exception statement from all source files in the program, then
    also delete it here.
*/

#ifndef TERMINAL_OVERLAY_HPP
#define TERMINAL_OVERLAY_HPP

#include "terminalframebuffer.h"
#include "network.h"
#include "transportsender.h"
#include "parser.h"

#include <vector>
#include <limits.h>

namespace Overlay {
  using namespace Terminal;
  using namespace Network;
  using std::deque;
  using std::list;
  using std::vector;
  using std::wstring;

  enum Validity {
    Pending,
    Correct,
    CorrectNoCredit,
    IncorrectOrExpired,
    Inactive
  };

  class ConditionalOverlay {
  public:
    uint64_t expiration_frame;
    int col;
    bool active; /* represents a prediction at all */
    uint64_t tentative_until_epoch; /* when to show */
    uint64_t prediction_time; /* used to find long-pending predictions */

    ConditionalOverlay( uint64_t s_exp, int s_col, uint64_t s_tentative )
      : expiration_frame( s_exp ), col( s_col ),
	active( false ),
	tentative_until_epoch( s_tentative ), prediction_time( uint64_t( -1 ) )
    {}

    virtual ~ConditionalOverlay() {}

    bool tentative( uint64_t confirmed_epoch ) const { return tentative_until_epoch > confirmed_epoch; }
    void reset( void ) { expiration_frame = tentative_until_epoch = -1; active = false; }
    void expire( uint64_t s_exp, uint64_t now )
    {
      expiration_frame = s_exp; prediction_time = now;
    }
  };

  class ConditionalCursorMove : public ConditionalOverlay {
  public:
    int row;

    void apply( Framebuffer &fb, uint64_t confirmed_epoch ) const;

    Validity get_validity( const Framebuffer &fb, uint64_t early_ack, uint64_t late_ack ) const;

    ConditionalCursorMove( uint64_t s_exp, int s_row, int s_col, uint64_t s_tentative )
      : ConditionalOverlay( s_exp, s_col, s_tentative ), row( s_row )
    {}
  };

  class ConditionalOverlayCell : public ConditionalOverlay {
  public:
    Cell replacement;
    bool unknown;

    vector<Cell> original_contents; /* we don't give credit for correct predictions
				       that match the original contents */

    void apply( Framebuffer &fb, uint64_t confirmed_epoch, int row, bool flag ) const;
    Validity get_validity( const Framebuffer &fb, int row, uint64_t early_ack, uint64_t late_ack ) const;

    ConditionalOverlayCell( uint64_t s_exp, int s_col, uint64_t s_tentative )
      : ConditionalOverlay( s_exp, s_col, s_tentative ),
	replacement( 0 ),
	unknown( false ),
	original_contents()
    {}

    void reset( void ) { unknown = false; original_contents.clear(); ConditionalOverlay::reset(); }
    void reset_with_orig( void ) {
      if ( (!active) || unknown ) {
	reset();
	return;
      }

      original_contents.push_back( replacement );
      ConditionalOverlay::reset();
    }
  };

  class ConditionalOverlayRow {
  public:
    int row_num;

    typedef vector<ConditionalOverlayCell> overlay_cells_type;
    overlay_cells_type overlay_cells;

    void apply( Framebuffer &fb, uint64_t confirmed_epoch, bool flag ) const;

    /* For use with find_if */
    bool row_num_eq( int v ) const { return row_num == v; }

    ConditionalOverlayRow( int s_row_num ) : row_num( s_row_num ), overlay_cells() {}
  };

  /* the various overlays */
  class NotificationEngine {
  private:
    uint64_t last_word_from_server;
    uint64_t last_acked_state;
    wstring message;
    bool message_is_network_exception;
    uint64_t message_expiration;
    bool show_quit_keystroke;

    bool server_late( uint64_t ts ) const { return (ts - last_word_from_server) > 6500; }
    bool reply_late( uint64_t ts ) const { return (ts - last_acked_state) > 10000; }
    bool need_countup( uint64_t ts ) const { return server_late( ts ) || reply_late( ts ); }

  public:
    void adjust_message( void );
    void apply( Framebuffer &fb ) const;
    const wstring &get_notification_string( void ) const { return message; }
    void server_heard( uint64_t s_last_word ) { last_word_from_server = s_last_word; }
    void server_acked( uint64_t s_last_acked ) { last_acked_state = s_last_acked; }
    int wait_time( void ) const;

    void set_notification_string( const wstring &s_message, bool permanent = false, bool s_show_quit_keystroke = true )
    {
      message = s_message;
      if ( permanent ) {
        message_expiration = -1;
      } else {
        message_expiration = timestamp() + 1000;
      }
      message_is_network_exception = false;
      show_quit_keystroke = s_show_quit_keystroke;
    }

    void set_network_exception( const NetworkException &e )
    {
      wchar_t tmp[ 128 ];
      swprintf( tmp, 128, L"%s: %s", e.function.c_str(), strerror( e.the_errno ) );

      message = tmp;
      message_is_network_exception = true;
      message_expiration = timestamp() + Network::ACK_INTERVAL + 100;
    }

    void clear_network_exception()
    {
      if ( message_is_network_exception ) {
	message_expiration = std::min( message_expiration, timestamp() + 1000 );
      }
    }

    NotificationEngine();
  };

  class PredictionEngine {
  private:
    static const uint64_t SRTT_TRIGGER_LOW = 20; /* <= ms cures SRTT trigger to show predictions */
    static const uint64_t SRTT_TRIGGER_HIGH = 30; /* > ms starts SRTT trigger */

    static const uint64_t FLAG_TRIGGER_LOW = 50; /* <= ms cures flagging */
    static const uint64_t FLAG_TRIGGER_HIGH = 80; /* > ms starts flagging */

    static const uint64_t GLITCH_THRESHOLD = 250; /* prediction outstanding this long is glitch */
    static const uint64_t GLITCH_REPAIR_COUNT = 10; /* non-glitches required to cure glitch trigger */
    static const uint64_t GLITCH_REPAIR_MININTERVAL = 150; /* required time in between non-glitches */

    static const uint64_t GLITCH_FLAG_THRESHOLD = 5000; /* prediction outstanding this long => underline */

    char last_byte;
    Parser::UTF8Parser parser;

    typedef list<ConditionalOverlayRow> overlays_type;
    overlays_type overlays;

    typedef list<ConditionalCursorMove> cursors_type;
    cursors_type cursors;

    typedef ConditionalOverlayRow::overlay_cells_type overlay_cells_type;

    uint64_t local_frame_sent, local_frame_acked, local_frame_late_acked;

    ConditionalOverlayRow & get_or_make_row( int row_num, int num_cols );

    uint64_t prediction_epoch;
    uint64_t confirmed_epoch;

    void become_tentative( void );

    void newline_carriage_return( const Framebuffer &fb );

    bool flagging; /* whether we are underlining predictions */
    bool srtt_trigger; /* show predictions because of slow round trip time */
    unsigned int glitch_trigger; /* show predictions temporarily because of long-pending prediction */
    uint64_t last_quick_confirmation;

    ConditionalCursorMove & cursor( void ) { assert( !cursors.empty() ); return cursors.back(); }

    void kill_epoch( uint64_t epoch, const Framebuffer &fb );

    void init_cursor( const