%
% $Id: reliability2.tex,v 1.10 2014/10/31 08:53:24 carzanig Exp carzanig $
%
\documentclass[t]{beamer}
\IfFileExists{lucidabr.sty}{%
  \usepackage[T1]{fontenc}
  \usepackage{textcomp} % to get the right copyright, etc.
  \usepackage[altbullet]{lucidabr}     % get larger bullet
  \DeclareEncodingSubset{TS1}{hlh}{1}}{}% including \oldstylenums
%
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{pstricks}
\usepackage{pst-node}
\usepackage{multido}
\usepackage{clrscode3e}

%======================== PsTricks settings and styles ================
\psset{linecolor=black,unit=1cm,dimen=middle,gridcolor=gray,subgridcolor=lightgray}
\newhsbcolor{Lightgray}{0 0 .95}
\newpsstyle{client}{linewidth=2pt,linecolor=gray,fillstyle=solid,fillcolor=white,framearc=.3}
\newpsstyle{server}{linewidth=2pt,linecolor=black,fillstyle=solid,fillcolor=white}
\newpsstyle{message}{linewidth=1pt,linecolor=gray,fillstyle=solid,fillcolor=white,shadow=true}
\newpsstyle{router}{linewidth=1pt,linecolor=black,fillstyle=solid,fillcolor=lightgray}
\newpsstyle{link}{linewidth=1pt,linecolor=black,fillstyle=none}
%
\SpecialCoor
%
\mode<presentation>
%======================== my Beamer preferences =======================
\setbeamertemplate{frametitle}{%
  \vbox to \baselineskip{\vfill%
    \hbox to \textwidth{\hfill\textbf{\insertframetitle}\strut}}}
\setbeamertemplate{sidebar left}{}
%
% comment this out to get a navigationbar at the bottom
%
\setbeamertemplate{sidebar right}{}
\setbeamertemplate{itemize item}{\vbox{\hrule width1ex height1ex depth0pt}}
\setbeamerfont{title}{family=\rmfamily,size=\LARGE,series=\bfseries}
\setbeamerfont{frametitle}{family=\rmfamily,size=\LARGE,series=\bfseries}
\setbeamertemplate{footline}{%
  \mode<handout>{\strut\hspace{.5em}\hbox{\insertcopyright}\hfill\insertpagenumber\hbox to 1ex{\hspace{.5em}\hss}}%
  \mode<beamer>{\strut\hfill\insertcopyright\hbox to 1ex{\hss}}}
\setbeamersize{text margin left=.5cm,text margin right=.5cm,sidebar width left=0pt,sidebar width right=0pt}
\setbeamercolor{footline}{fg=black!20}
\setbeamercolor{background canvas}{bg=}
\setbeamerfont{alerted text}{shape=\itshape}
\setbeamercolor{alerted text}{fg=beamer@blendedblue}
\setbeamercolor{code}{fg=black!90,bg=black!10}
\setbeamercolor{table}{fg=black!90,bg=black!10}
\setbeamercolor{block body example}{fg=black!90,bg=black!10}
\setbeamercolor{block title example}{fg=black!10,bg=blue!90!green!50}
\setbeamertemplate{blocks}[rounded][shadow=true]
\setbeamertemplate{block title example}{\phantom{}}
%======================== Network Objects =======================
\def\Router{\pspicture(-.3,-.3)(.3,.3)\pscircle[style=router](0,0){.3}\endpspicture}
\def\Arouter(#1)#2{\cnode[style=router](#1){.3}{#2}}
\def\Host{\pspicture(-.3,-.3)(.3,.3)\psframe[style=client](-.3,-.3)(.3,.3)\endpspicture}
\def\Ahost(#1)#2{\rput(#1){\rnode{#2}{\Host}}}
\def\Aserver(#1)#2{\rput(#1){\rnode{#2}{\pspicture(-.3,-.3)(.3,.3)\psframe[style=client](-.3,-.3)(.3,.3)}}}
%======================== Document Info =======================
\title{Reliable Data Transfer II}
\author{Antonio Carzaniga}
\institute{Faculty of Informatics\\University of Lugano}
\date{October 1, 2014}
\newcommand{\insertcopyright}{\copyright\ 2005--2007\quad Antonio Carzaniga}
%======================================================================
\begin{document}
\begin{frame}% Titleslide
  \maketitle
\end{frame}

\begin{frame}\frametitle{Outline}
  \begin{itemize}
  \item Performance of the stop-and-wait protocol

    \bigskip
  \item Go-Back-N

    \bigskip
  \item Selective repeat

  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Back to Reliable Data Tranfer}
  \pause
  \begin{center}
    \begin{pspicture}(-5.5,-4)(4.5,2)
      \psset{labelsep=0pt}
      % 
      \footnotesize
      \alt<3,10-11>{\rput(-2,0){\ovalnode[doubleline=true,fillstyle=solid,fillcolor=yellow]{S0}{S0}}}{%
        \rput(-2,0){\ovalnode[doubleline=true]{S0}{S0}}}%
%
      \alt<4-5>{\rput(1,0){\ovalnode[fillstyle=solid,fillcolor=yellow]{ACK0}{ACK0}}}{%
        \rput(1,0){\ovalnode{ACK0}{ACK0}}}%
      \alt<6-7>{\rput(1,-3){\ovalnode[fillstyle=solid,fillcolor=yellow]{S1}{S1}}}{%
        \rput(1,-3){\ovalnode{S1}{S1}}}%
      \alt<8-9>{\rput(-2,-3){\ovalnode[fillstyle=solid,fillcolor=yellow]{ACK1}{ACK1}}}{%
        \rput(-2,-3){\ovalnode{ACK1}{ACK1}}}%
      \footnotesize
      % 
      \ncdiag[linewidth=2pt,arrows={<-},armA=.8,armB=.8,
              angleA=150,angleB=-150,linearc=0.23]{S0}{S0}
      \nbput{\begin{tabular}{@{}l@{}}
          u\_recv($pkt$)\\
          \hline
        \end{tabular}}
              % 
      \only<4>{\ncarc[arcangleA=30,arcangleB=30,linewidth=4pt,linecolor=yellow]{->}{S0}{ACK0}}
      \ncarc[arcangleA=30,arcangleB=30,linewidth=2pt]{->}{S0}{ACK0}
      \naput[npos=.35,labelsep=-.2]{\begin{tabular}{@{}l@{}}
          r\_send($data$)\\
          \hline
          $data\_pkt=[0,data]^*$\\
          u\_send($data\_pkt$)\\
          start\_timer()
        \end{tabular}}
      \ncdiag[linewidth=2pt,arrows={->},armA=.8,armB=.8,
              angleA=120,angleB=60,linearc=0.23]{ACK0}{ACK0}
      \ncput[ref=bl,npos=1.5]{\begin{tabular}{@{}l@{}}
          timeout\\
          \hline 
          u\_send($data\_pkt$)\\
          start\_timer()
        \end{tabular}}
%
      \ncdiag[linewidth=2pt,arrows={->},armA=.8,armB=.8,
              angleA=30,angleB=-30,linearc=0.23]{ACK0}{ACK0}
      \naput{\begin{tabular}{@{}l@{}}
          u\_recv($pkt$)\\
          \textbf{and} ($pkt$ is corrupted\\
          \textbf{or} $pkt=(\text{ACK},1)$)\\
          \hline 
          u\_send($data\_pkt$)\\
          start\_timer()
        \end{tabular}}
      \only<6>{\ncarc[arcangleA=30,arcangleB=30,linewidth=4pt,linecolor=yellow]{->}{ACK0}{S1}}
      \ncarc[arcangleA=30,arcangleB=30,linewidth=2pt]{->}{ACK0}{S1}
      \naput[npos=.55]{\begin{tabular}{@{}l@{}}
          u\_recv($pkt$)\\
          \textbf{and} $pkt$ is good\\
          \textbf{and} $pkt=(\text{ACK},0)$\\
          \hline 
        \end{tabular}}
%
      \ncdiag[linewidth=2pt,arrows={->},armA=.8,armB=.8,
              angleA=30,angleB=-30,linearc=0.23]{S1}{S1}
      \naput{\begin{tabular}{@{}l@{}}
          u\_recv($pkt$)\\
          \hline
        \end{tabular}}
%
      \only<8>{\ncarc[arcangleA=30,arcangleB=30,linewidth=4pt,linecolor=yellow]{->}{S1}{ACK1}}
      \ncarc[arcangleA=30,arcangleB=30,linewidth=2pt]{->}{S1}{ACK1}
      \naput[npos=.15,labelsep=-.2]{\begin{tabular}{@{}l@{}}
          r\_send($data$)\\
          \hline
          $data\_pkt=[1,data]^*$\\
          u\_send($data\_pkt$)\\
          start\_timer()
        \end{tabular}}
%
      \ncdiag[linewidth=2pt,arrows={<-},armA=.8,armB=.8,
              angleA=-120,angleB=-60,linearc=0.23]{ACK1}{ACK1}
      \nbput{\begin{tabular}{@{}l@{}}
          timeout\\
          \hline 
          u\_send($data\_pkt$)\\
          start\_timer()
        \end{tabular}}
%
      \ncdiag[linewidth=2pt,arrows={<-},armA=.8,armB=.8,
              angleA=150,angleB=-150,linearc=0.23]{ACK1}{ACK1}
      \nbput{\begin{tabular}{@{}l@{}}
          u\_recv($pkt$)\\
          \textbf{and} ($pkt$ is corrupted\\
          \textbf{or} $pkt=(\text{ACK},0)$)\\
          \hline 
          u\_send($data\_pkt$)\\
          start\_timer()
        \end{tabular}}
%
      \only<10>{\ncarc[arcangleA=30,arcangleB=30,linewidth=4pt,linecolor=yellow]{->}{ACK1}{S0}}
      \ncarc[arcangleA=30,arcangleB=30,linewidth=2pt]{->}{ACK1}{S0}
      \naput[npos=.6]{\begin{tabular}{@{}l@{}}
          u\_recv($pkt$)\\
          \textbf{and} $pkt$ is good\\
          \textbf{and} $pkt=(\text{ACK},1)$\\
          \hline 
        \end{tabular}}

    \end{pspicture}
  \end{center}
\end{frame}

\begin{frame}\frametitle{Network Usage}
  \begin{center}
    \begin{pspicture}(-5,-3.5)(5,3)%\psgrid(-5,-3)(5,3)
      \rput[B](-2,2.7){sender}
      \psline[linewidth=2pt]{->}(-2,2.5)(-2,-3.5)
%
      \rput[B](2,2.7){receiver}
      \psline[linewidth=2pt]{->}(2,2.5)(2,-3.5)
%
      \footnotesize
      \psset{labelsep=1pt,arrowsize=6pt}
      \rput[r](-3.5,2.5){r\_send($pkt_1$)}
      \pause
      \rput[r](-2.1,2){u\_send([$pkt_1$,0])}
      \pause
      \pspolygon[linewidth=0pt,fillcolor=lightgray,
                 fillstyle=solid](-2,2)(2,1)(2,.5)(-2,1.5)
      \pcline{->}(-2,2)(2,1)\aput{:U}{[$pkt_1$,0]}
      \psline{->}(-2,1.5)(2,.5)
      \pause
      \rput[l](2.1,.5){u\_send([ACK,0])}
      \rput[l](3.5,0){r\_recv($pkt_1$)}
      \pause
      \pcline{->}(2,.5)(-2,-.5)\bput{:D}{[ACK,0]}
      \pause
      \rput[r](-3.5,0){r\_send($pkt_2$)}
      \pause
      \rput[r](-2.1,-.5){u\_send([$pkt_2$,1])}
      \pause
      \pspolygon[linewidth=0pt,fillcolor=lightgray,
                 fillstyle=solid](-2,-.5)(2,-1.5)(2,-2)(-2,-1)
      \pcline{->}(-2,-.5)(2,-1.5)\aput{:U}{[$pkt_2$,1]}
      \psline{->}(-2,-1)(2,-2)
      \pause
      \rput[l](2.1,-2){u\_send([ACK,1])}
      \rput[l](3.5,-2.5){r\_recv($pkt_2$)}
      \pause
      \pcline{->}(2,-2)(-2,-3)\bput{:D}{[ACK,1]}
    \end{pspicture}
  \end{center}
\end{frame}

\begin{frame}\frametitle{Network Usage}
  \begin{center}
    \begin{pspicture}(-5,-3.5)(5,3)%\psgrid(-5,-3)(5,3)
      \rput[B](-2,2.7){sender}
      \psline[linewidth=2pt]{->}(-2,2.5)(-2,-3.5)
%
      \rput[B](2,2.7){receiver}
      \psline[linewidth=2pt]{->}(2,2.5)(2,-3.5)
%
      \footnotesize
      \psset{labelsep=1pt,arrowsize=6pt}
      \pspolygon[linewidth=0pt,fillcolor=lightgray,
                 fillstyle=solid](-2,2)(2,1)(2,.5)(-2,1.5)
      \pcline{->}(-2,2)(2,1)\aput{:U}{[$pkt_1$,0]}
      \psline{->}(-2,1.5)(2,.5)
      \pcline{->}(2,.5)(-2,-.5)\bput{:D}{[ACK,0]}
      \pspolygon[linewidth=0pt,fillcolor=lightgray,
                 fillstyle=solid](-2,-.5)(2,-1.5)(2,-2)(-2,-1)
      \pcline{->}(-2,-.5)(2,-1.5)\aput{:U}{[$pkt_2$,1]}
      \psline{->}(-2,-1)(2,-2)
      \pcline{->}(2,-2)(-2,-3)\bput{:D}{[ACK,1]}
      \pause
      \psline[linecolor=blue,linewidth=1pt](-2,2)(2.2,2)
      \psline[linecolor=blue,linewidth=1pt](-2,-.5)(2.2,-.5)
      \pause
      \pcline[linecolor=blue,linewidth=1pt]{|*-|*}(2.5,2)(2.5,1)
      \naput{$d$}
      \pause
      \pcline[linecolor=blue,linewidth=1pt]{|*-|*}(2.5,1)(2.5,.5)
      \naput{$\ell_{\mathit{pkt}_1}/R$}
      \pause
      \pcline[linecolor=blue,linewidth=1pt]{|*-|*}(2.5,.5)(2.5,-.5)
      \naput{$d$}
      \pause
      \rput(-4,.75){\normalsize%
        \begin{tabular}[m]{c}
          \emph{utilization}\\
          \emph{factor}\\[1em]
          $U=\frac{\ell_{\mathit{pkt}}/R}{2d + \ell_{\mathit{pkt}}/R}$\\
        \end{tabular}}
    \end{pspicture}
  \end{center}
\end{frame}

\begin{frame}\frametitle{Improving Network Usage}
  \begin{itemize}
  \item How do we achieve a better \emph{utilization factor}?

    \pause
    \bigskip
    \begin{center}
      \begin{pspicture}(-5,-3)(5,3)%\psgrid(-5,-3)(5,3)
        \rput[B](-2,2.7){sender}
        \psline[linewidth=2pt]{->}(-2,2.5)(-2,-3)
        \rput[B](2,2.7){receiver}
        \psline[linewidth=2pt]{->}(2,2.5)(2,-3)
        % 
        \footnotesize
        \psset{labelsep=1pt,arrowsize=6pt}
        \pspolygon[linewidth=0pt,fillcolor=lightgray,
        fillstyle=solid](-2,2)(2,1)(2,.5)(-2,1.5)
        \pcline{->}(-2,2)(2,1)\aput{:U}{[$pkt_1$,$0$]}
        \pcline{->}(2,.5)(-2,-.5)\lput*[l]{:D}(.9){[ACK,$0$]}
        \pause
        \pcline{->}(2,0)(-2,-1)\lput*[l]{:D}(.9){[ACK,$1$]}
        \pspolygon[linewidth=0pt,fillcolor=lightgray,
        fillstyle=solid](-2,1.5)(2,.5)(2,0)(-2,1)
        \pcline{->}(-2,1.5)(2,.5)\aput{:U}{[$pkt_2$,$1$]}
        \pause
        \pcline{->}(2,-.5)(-2,-1.5)\lput*[l]{:D}(.9){$\cdots$}
        \pspolygon[linewidth=0pt,fillcolor=lightgray,
        fillstyle=solid](-2,1)(2,0)(2,-.5)(-2,.5)
        \pcline{->}(-2,1)(2,0)\aput{:U}{$\cdots$}
        \pause
        \pcline{->}(2,-1)(-2,-2)\lput*[l]{:D}(.9){[ACK,$w-1$]}
        \pspolygon[linewidth=0pt,fillcolor=lightgray,
        fillstyle=solid](-2,.5)(2,-.5)(2,-1)(-2,0)
        \pcline{->}(-2,.5)(2,-.5)\aput{:U}{[$pkt_w$,$w-1$]}
        % \psline{->}(-2,0)(2,-1)
      \end{pspicture}
    \end{center}
  \end{itemize}
\end{frame}



\begin{frame}\frametitle{Go-Back-N}
  \begin{itemize}
  \item \textbf{Idea:} the sender transmits multiple packets without
    waiting for an acknowledgement

    \pause
    \medskip
  \item Sender has up to $W$ unacknowledged packets in the pipeline

    \begin{itemize}
    \item the sender's state machine gets very complex
    \item we represent the sender's state with its queue of
      acknowledgements
    \end{itemize}

    \smallskip
    \pause
    \begin{center}
      \begin{pspicture}(-5,-1)(5,1)\small
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-3,.5)
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=lightgray](-3,-.5)(1,.5)
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](3,-.5)(5,.5)
        \psframe(-5,-.5)(5,.5)
        \multido{\n=-5+.5}{20}{%
          \psline[linecolor=black,linewidth=1pt](\n,-.5)(\n,.5)}	
        \pause
        \rput[B](-4,.7){acknowledged}
        \rput[B](-1,.7){pending}
        \rput[B](2,.7){available}
        \rput[B](4,.7){unavailable}
        \pause
        \psline[arrowsize=6pt]{->}(-2.75,-1)(-2.75,-.5)
        \rput[t](-2.75,-1){%
          \begin{tabular}{c}
            first pending\\
            acknowledgement\\
            \emph{(base)}
          \end{tabular}}
        \pause
        \psline[arrowsize=6pt]{->}(1.25,-1)(1.25,-.5)
        \rput[t](1.25,-1){%
          \begin{tabular}{c}
            next available\\
            sequence number\\
            \emph{(next\_seq\_num)}
          \end{tabular}}
        \pause
        \pcline[linecolor=gray]{|*-|*}(-3,-2.5)(3,-2.5)\nbput{%
          window size ($W$\/)} 
      \end{pspicture}
    \end{center}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Sliding Window Protocol: Sender}
  \begin{center}
    \begin{pspicture}(-5,-1.5)(5,1)\small
      \only<-5>{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-3,.5)
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](3,-.5)(5,.5)
      }
      \only<6->{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-2,.5)
      }
      \only<-3>{%
        \psframe[linewidth=0pt,
                 fillstyle=solid,fillcolor=lightgray](-3,-.5)(1,.5)
      }
      \only<4-5>{%
        \psframe[linewidth=0pt,
                 fillstyle=solid,fillcolor=lightgray](-3,-.5)(1.5,.5)
      }
      \only<6->{%
        \psframe[linewidth=0pt,
                 fillstyle=solid,fillcolor=lightgray](-2,-.5)(1.5,.5)
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](4,-.5)(5,.5)
      }
      \psframe(-5,-.5)(5,.5)
      \multido{\n=-5+.5}{20}{\psline[linecolor=black,linewidth=1pt](\n,-.5)(\n,.5)}	
      \only<-5>{%
        \psline[arrowsize=6pt]{->}(-2.75,-1)(-2.75,-.5)
        \rput[t](-2.75,-1){
          \begin{tabular}{c}\emph{base}\end{tabular}}
        \pcline[linecolor=gray]{|*-|*}(-3,-1.5)(3,-1.5)\nbput{sliding window}
      }
      \only<6->{%
        \psline[arrowsize=6pt]{->}(-1.75,-1)(-1.75,-.5)
        \rput[t](-1.75,-1){
          \begin{tabular}{c}\emph{base}\end{tabular}}
        \pcline[linecolor=gray]{|*-|*}(-2,-1.5)(4,-1.5)\nbput{sliding window}
      }
      \only<-3>{%
        \psline[arrowsize=6pt]{->}(1.25,-1)(1.25,-.5)
        \rput[t](1.25,-1){%
          \begin{tabular}{c}\emph{next\_seq\_num}\end{tabular}}
      }
      \only<4->{%
        \psline[arrowsize=6pt]{->}(1.75,-1)(1.75,-.5)
        \rput[t](1.75,-1){%
          \begin{tabular}{c}\emph{next\_seq\_num}\end{tabular}}
      }
      \only<5->{%
        \psline[arrowsize=6pt]{->}(-2.25,1)(-2.25,.5)
        \rput[b](-2.25,1){%
          \begin{tabular}{c}\emph{A}\end{tabular}}
      }
    \end{pspicture}
  \end{center}

  \begin{itemize}
    \pause
  \item r\_send($pkt_1$)
    \begin{itemize}
      \pause
      \smallskip
    \item u\_send([$pkt_1$,$next\_seq\_num$])
      \pause
      \smallskip
    \item $next\_seq\_num \gets next\_seq\_num + 1$ 
    \end{itemize}

    \pause
    \bigskip
  \item u\_recv([ACK,$A$])
    \begin{itemize}
      \pause
      \smallskip
    \item $\id{base} \gets A + 1$

      \pause
      \smallskip
    \item notice that acknewledgements are ``cumulative''
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Sliding Window Protocol: Sender}
  \pause
  \begin{itemize}
  \item The sender remembers the first sequence number that has not
    yet been acknowledged

    \begin{itemize}
      \smallskip
    \item or the highest acknowledged sequence number
    \end{itemize}

    \bigskip
  \item The sender remembers the first available sequence number

    \begin{itemize}
      \smallskip
    \item or the highest used sequence number (i.e., sent to the receiver)
    \end{itemize}

    \bigskip
  \item The sender responds to three types of events
    \pause
    \begin{itemize}
      \smallskip
    \item \emph{r\_send():} invocation from the application layer:
      send more data if a sequence number is available

      \smallskip
      \pause
    \item \emph{ACK:} receipt of an acknowledgement: shift the window
      (it's a ``cumulative'' ACK)

      \smallskip
      \pause
    \item \emph{timeout:} ``Go-Back-N.''  I.e., resend all the packets
      that have been sent but not acknowledged
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Sliding Window Protocol: Sender}

  \begin{itemize}
  \item 
    \begin{tabular}[t]{@{}l@{}}
      $init$\\
      \hline
      % \begin{codebox}
      %   \zi $\id{base}\gets 1$
      %   \zi $\id{next\_seq\_num}\gets 1$
      % \end{codebox}
      $\id{base}\gets 1$\\
      $\id{next\_seq\_num}\gets 1$\\
    \end{tabular}

    \pause
    \bigskip
  \item
    \begin{tabular}[t]{@{}l@{}}
      r\_send($data$)\\
      \hline
      \textbf{if} $\id{next\_seq\_num} < \id{base} + W$:\\
      \hspace{2em}$pkt[\id{next\_seq\_num]\gets} [\id{next\_seq\_num},\id{data}]^*$\\
      \hspace{2em}u\_send($pkt[next\_seq\_num]$)\\
      \hspace{2em}\textbf{if} $next\_seq\_num\isequal base$:\\
      \hspace{4em}start\_timer()\\
      \hspace{2em}$next\_seq\_num\gets next\_seq\_num + 1$\\
      \textbf{else}:\\
      \hspace{2em}refuse\_data(data)\hspace{2em}\emph{// block the sender}\\
    \end{tabular}
\end{itemize}
  
\end{frame}

\begin{frame}\frametitle{Sliding Window Protocol: Sender}
  \begin{itemize}
  \item 
    \begin{tabular}[t]{@{}l@{}}
      u\_recv($pkt$) \textbf{and} $pkt$ is corrupted\\
      \hline
    \end{tabular}

    \pause
    \smallskip
  \item
    \begin{tabular}[t]{@{}l@{}}
      u\_recv(ACK,$ack\_num$)\\
      \hline
      $base\gets ack\_num + 1$\hspace{2em}\emph{// resume the sender}\\
      \textbf{if} $next\_seq\_num\isequal base$:\\
      \hspace{2em}stop\_timer()\\
      \textbf{else}:\\
      \hspace{2em}start\_timer()\\
    \end{tabular}

    \pause
    \smallskip
  \item
    \begin{tabular}[t]{@{}l@{}}
      timeout\\
      \hline
      start\_timer()\\
      \textbf{foreach} $i$ \textbf{in} $base\ldots next\_seq\_num - 1$:\\
      \hspace{2em}u\_send($pkt[i]$)\\
    \end{tabular}
\end{itemize}
  
\end{frame}

\begin{frame}\frametitle{Sliding Window Protocol: Receiver}
  \pause
  \begin{itemize}
  \item Simple: as in the stop-and-wait case, the receiver maintains a
    counter representing the \emph{expected sequence number}

    \pause
    \bigskip
  \item The receiver waits for a (good) data packet with the expected
    sequence number

    \pause
    \smallskip
    \begin{itemize}
    \item acknowledges the expected sequence number

    \pause
    \smallskip
    \item delivers the data to the application
    \end{itemize}
\end{itemize}
\end{frame}

\begin{frame}\frametitle{Sliding Window Protocol: Receiver}
  \begin{itemize}
  \item 
    \begin{tabular}[t]{@{}l@{}}
      \emph{init}\\
      \hline
      $expected\_seq\_num\gets 1$\\
      $ackpkt\gets[ACK,0]^*$\\
    \end{tabular}

    \pause
    \bigskip
  \item
    \begin{tabular}[t]{@{}l@{}}
      u\_recv($[data,seq\_num]$) \textbf{and} good\\
      \textbf{and} $seq\_num = expected\_seq\_num$\\
      \hline
      r\_recv($data$)\\
      $ackpkt\gets[ACK,expected\_seq\_num]^*$\\
      $expected\_seq\_num\gets expected\_seq\_num + 1$\\
      u\_send($ackpkt$)\\
    \end{tabular}

    \pause
    \bigskip
  \item
    \begin{tabular}[t]{@{}l@{}}
      u\_recv($[data,seq\_num]$)\\
      \textbf{and} (corrupted \textbf{or} $seq\_num \ne expected\_seq\_num$)\\
      \hline
      u\_send($ackpkt$)\\
    \end{tabular}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Comments}
  \begin{itemize}
  \item Concepts

  \pause
  \smallskip
    \begin{itemize}
    \item \emph{sequence numbers}
      \smallskip
      \pause
    \item \emph{sliding window}

      \pause
      \smallskip
    \item \emph{cumulative acknowledgements}

      \pause
      \smallskip
    \item \emph{checksums}, \emph{timeouts}, and
      \emph{sender-initiated retransmission}
    \end{itemize}


    \pause
    \medskip
  \item Advantages: \emph{simple}

    \pause
    \begin{itemize}
      \smallskip
    \item the sender maintains \alert{two counters} and a \alert{one
        timer}

      \smallskip
    \item the receiver maintains \alert{one counter}
    \end{itemize}

    \pause
    \medskip
  \item Disadvantages: \emph{not optimal}, \emph{not adaptive}
    \pause
    \smallskip
    \begin{itemize}
    \item the sender can fill the window without filling the pipeline

      \pause
      \smallskip
    \item the receiver may buffer out-of-order packets\ldots
    \end{itemize}
\end{itemize}
\end{frame}

\begin{frame}\frametitle{Performance Analysis}
  \begin{itemize}
  \item What is a good value for $W$?

    \pause
    \begin{itemize}
      \smallskip
    \item $W$ that achieves the \emph{maximum utilization} of the
      connection
    \end{itemize}

    \pause
    \bigskip
    \begin{tabular}{@{}rcl}
      $\ell$&$=$&$stream$\\
      $d$&$=$&$500ms$\\
      $R$&$=$&$1Mb/s$\\
      $W$&$=$&$?$\\
    \end{tabular}
      
    \pause
    \bigskip
  \item The problem may seem a bit underspecified.  What is the
    (average) packet size?

    \bigskip
    \begin{tabular}{@{}rcl}
      $\ell_{\mathit{pkt}}$&$=$&$1Kb$\\
      $d$&$=$&$500ms$\\
      $R$&$=$&$1Mb/s$\\
      $W$&$=$&\only<3>{?}\uncover<4->{$\frac{2d\times R}{\ell_{\mathit{pkt}}}=1000$}\\
    \end{tabular}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Performance Analysis}
  \begin{itemize}
  \item The RTT--throughput product $(2d\times R)$ is the crucial
    factor 

    \pause 
    \medskip
    \begin{itemize}
    \item $W\times \ell_{\mathit{pkt}} \le 2d\times R$ 

      \begin{itemize}
        \smallskip
      \item why $W\times \ell_{\mathit{pkt}} > 2d\times R$ doesn't make much sense?
      \end{itemize}

      \pause
    \medskip
    \item maximum channel utilization when $W\times \ell_{\mathit{pkt}} = 2d\times R$

    \medskip
    \item $2d\times R$ can be thought of as the \emph{capacity} of a
      connection 
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Problems with Go-Back-N}
  \begin{itemize}
  \item Let's consider a fully utilized connection

    \pause
    \medskip
    \begin{tabular}{@{}rcl}
      $\ell_{\mathit{pkt}}$&$=$&$1Kb$\\
      $d$&$=$&$500ms$\\
      $R$&$=$&$1Mb/s$\\
      $W$&$=$&$\frac{R\times d}{\ell_{\mathit{pkt}}}=1000$\\
    \end{tabular}

      \pause
    \medskip
  \item What happens if the first packet (or acknowledgement) is lost?

    \pause
    \medskip
  \item Sender retransmits the entire content of its buffers  

    \begin{itemize}
      \pause
      \smallskip
    \item $W\times \ell_{\mathit{pkt}} = 2d\times R = 1Mb$

      \smallskip
    \item retransmitting 1Mb to recover 1Kb worth of data isn't
      exactly the best solution.  Not to mention conjestions\ldots

    \end{itemize}

    \pause
    \medskip
  \item Is there a better way to deal with retransmissions?
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Selective Repeat}
  \begin{itemize}
  \item \textbf{Idea:} have the sender retransmit only those packets that it
    suspects were lost or corrupted

    \pause
    \medskip
    \begin{itemize}
    \item sender maintains a vector of acknowledgement flags

      \medskip
      \pause
    \item receiver maintains a vector of acknowledged falgs

      \medskip
      \pause
    \item in fact, receiver maintains a buffer of out-of-order packets

      \medskip
      \pause
    \item sender maintains a timer for each pending packet

      \medskip
      \pause
    \item sender resends a packet when its timer expires

      \medskip
      \pause
    \item sender slides the window when the lowest pending sequence
      number is acknowledged
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}\frametitle{Selective Repeat: Sender}
  \begin{center}
    \begin{pspicture}(-5,-1.5)(5,1)\small
      \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-3,.5)
      \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](3,-.5)(5,.5)
      \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-2,.5)
      \only<-3>{%
        \psframe[linewidth=0pt,
                 fillstyle=solid,fillcolor=lightgray](-3,-.5)(1,.5)
      }
      \only<4->{%
        \psframe[linewidth=0pt,
                 fillstyle=solid,fillcolor=lightgray](-3,-.5)(1.5,.5)
      }
      \only<6->{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-2.5,-.5)(-2,.5)
      }
      \psframe(-5,-.5)(5,.5)
      \multido{\n=-5+.5}{20}{\psline[linecolor=black,linewidth=1pt](\n,-.5)(\n,.5)}	
      \psline[arrowsize=6pt]{->}(-2.75,-1)(-2.75,-.5)
      \rput[t](-2.75,-1){
        \begin{tabular}{c}\emph{base}\end{tabular}}
      \pcline[linecolor=gray]{|*-|*}(-3,-1.5)(3,-1.5)\nbput{sliding window}
      \only<-3>{%
        \psline[arrowsize=6pt]{->}(1.25,-1)(1.25,-.5)
        \rput[t](1.25,-1){%
          \begin{tabular}{c}\emph{next\_seq\_num}\end{tabular}}
      }
      \only<4->{%
        \psline[arrowsize=6pt]{->}(1.75,-1)(1.75,-.5)
        \rput[t](1.75,-1){%
          \begin{tabular}{c}\emph{next\_seq\_num}\end{tabular}}
      }
      \only<5->{%
        \psline[arrowsize=6pt]{->}(-2.25,1)(-2.25,.5)
        \rput[b](-2.25,1){%
          \begin{tabular}{c}\emph{A}\end{tabular}}
      }
    \end{pspicture}
  \end{center}

  \begin{itemize}
    \pause
  \item r\_send($pkt_1$)
    \pause
    \begin{itemize}
      \smallskip
    \item u\_send([$pkt_1$,$next\_seq\_num$])

      \smallskip
    \item start\_timer($next\_seq\_num$)

      \pause
      \smallskip
    \item $next\_seq\_num \gets next\_seq\_num + 1$ 

    \end{itemize}

    \pause
    \bigskip
  \item u\_recv([ACK,$A$])

    \pause
    \begin{itemize}
      \smallskip
    \item $acks[A] \gets 1$\hspace{4em}\emph{// remember that A was ACK'd}
      \pause

      \smallskip
    \item acknewledgements are no longer ``cumulative''
    \end{itemize}
  \end{itemize}

\end{frame}

\begin{frame}\frametitle{Selective Repeat: Receiver}
  \pause
  \begin{center}
    \begin{pspicture}(-5,-2)(5,1)\small
      \alt<1-8>{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-3,.5)
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](3,-.5)(5,.5)
      }{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-2,.5)
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](4,-.5)(5,.5)
      }%
      \only<4-8>{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=lightgray](-2.5,-.5)(-2,.5)
      }%
      \only<6-8>{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=lightgray](-3,-.5)(-2.5,.5)
      }%
      \psframe(-5,-.5)(5,.5)
      \multido{\n=-5+.5}{20}{\psline[linecolor=black,linewidth=1pt](\n,-.5)(\n,.5)}	
      \alt<1-8>{%
        \rput[B](-4,.7){received}
        \rput[B](0,.7){acceptable}
        \rput[B](4,.7){not usable}
      }{%
        \rput[B](-3,.7){received}
        \rput[B](1,.7){acceptable}
        \rput[B](4.5,.7){not usable}
      }%
%
      \alt<1-8>{%
        \psline[arrowsize=6pt]{->}(-2.75,-1)(-2.75,-.5)
        \rput[t](-2.75,-1){
          \begin{tabular}{c}\emph{rcv\_base}\end{tabular}}
        \pcline[linecolor=gray]{|*-|*}(-3,-1.5)(3,-1.5)\nbput{sliding window}
      }{%
        \psline[arrowsize=6pt]{->}(-1.75,-1)(-1.75,-.5)
        \rput[t](-1.75,-1){
          \begin{tabular}{c}\emph{rcv\_base}\end{tabular}}
        \pcline[linecolor=gray]{|*-|*}(-2,-1.5)(4,-1.5)\nbput{sliding window}}%
      \only<3-4>{%
        \psline[arrowsize=6pt]{->}(-2.25,1)(-2.25,.5)
        \rput[b](-2.25,1){%
          \begin{tabular}{c}$X_1$\end{tabular}}}%
      \only<6-8>{%
        \psline[arrowsize=6pt]{->}(-2.75,1)(-2.75,.5)
        \rput[b](-2.75,1){%
          \begin{tabular}{c}$X_2$\end{tabular}}
      }
      \only<8>{%
        \psline[arrowsize=6pt]{->}(-1.75,1)(-1.75,.5)
        \rput[b](-1.75,1){%
          \begin{tabular}{c}$B$\end{tabular}}
      }
    \end{pspicture}
  \end{center}

    \only<3-4>{%
      \begin{itemize}
      \item u\_recv([$pkt_1$,$X_1$]) \textbf{and} $rcv\_base \le X_1 < rcv\_base + W$
        \begin{itemize}
          \smallskip
        \item<4> $buffer[X_1]\gets pkt_1$

          \smallskip
        \item<4> u\_send($[ACK,X_1]^*$)\hspace{4em}\emph{// no longer a ``cumulative'' ACK}\\
        \end{itemize}
      \end{itemize}
    }%
    \only<6->{%
      \begin{itemize}
      \item u\_recv([$pkt_2$,$X_2$]) \textbf{and} $rcv\_base \le X_2 < rcv\_base + W$ 
        \begin{itemize}
        \item $buffer[X_2]\gets pkt_2$

          \smallskip
        \item u\_send($[ACK,X_2]^*$)

          \smallskip
        \item<7->
          \begin{tabular}[t]{@{}l}
            \textbf{if} $X_2 \isequal rcv\_base$:\\
            \onslide<8-9>
            \hspace{2em}$B\gets first\_missing\_seq\_num()$\\
            \hspace{2em}\textbf{foreach} $i$ \textbf{in} $rcv\_base\ldots B - 1$:\\
            \hspace{4em}r\_recv($buffer[i]$)\\
            \onslide<9>
            \hspace{2em}$rcv\_base\gets B$\\
          \end{tabular}
        \end{itemize}
      \end{itemize}
    }%
\end{frame}

\begin{frame}\frametitle{Selective Repeat: Sender}
  \pause
  \begin{center}
    \begin{pspicture}(-5,-2)(5,1)\small
      \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-5,-.5)(-3,.5)
      \alt<-8>{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](3,-.5)(5,.5)
      }{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](4,-.5)(5,.5)
      }
      \psframe[linewidth=0pt,
               fillstyle=solid,fillcolor=lightgray](-3,-.5)(1.5,.5)
      \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-2.5,-.5)(-2,.5)
      \only<7->{%
        \psframe[linewidth=0pt,fillstyle=solid,fillcolor=gray](-3,-.5)(-2.5,.5)
      }
%
      \psframe(-5,-.5)(5,.5)
      \multido{\n=-5+.5}{20}{\psline[linecolor=black,linewidth=1pt](\n,-.5)(\n,.5)}	
      \alt<-8>{%
        \psline[arrowsize=6pt]{->}(-2.75,-1)(-2.75,-.5)
        \rput[t](-2.75,-1){
          \begin{tabular}{c}\emph{base}\end{tabular}}
        \pcline[linecolor=gray]{|*-|*}(-3,-1.5)(3,-1.5)\nbput{sliding
          window}
      }{%
        \psline[arrowsize=6pt]{->}(-1.75,-1)(-1.75,-.5)
        \rput[t](-1.75,-1){
          \begin{tabular}{c}\emph{base}\end{tabular}}
        \pcline[linecolor=gray]{|*-|*}(-2,-1.5)(4,-1.5)\nbput{sliding
          window}
      }
      \psline[arrowsize=6pt]{->}(1.75,-1)(1.75,-.5)
      \rput[t](1.75,-1){%
        \begin{tabular}{c}\emph{next\_seq\_num}\end{tabular}}
      \only<3-4>{%
        \psline[arrowsize=6pt]{->}(-2.75,1)(-2.75,.5)
        \rput[b](-2.75,1){%
          \begin{tabular}{c}$T$\end{tabular}}
        \psframe[linecolor=red,linewidth=3pt](-3,-.5)(-2.5,.5)
      }
      \only<6-8>{%
        \psline[arrowsize=6pt]{->}(-2.75,1)(-2.75,.5)
        \rput[b](-2.75,1){%
          \begin{tabular}{c}$A$\end{tabular}}
      }
    \end{pspicture}
  \end{center}

  \begin{itemize}
    \onslide<3-4>
  \item Timeout for sequence number $T$
    \onslide<4>
    \begin{itemize}
    \item u\_send($[pkt[T],T]^*$)
    \end{itemize}

    \onslide<6->
  \item u\_recv([ACK,$A$])
    \begin{itemize}
      \onslide<7->
    \item $acks[A] \gets 1$

      \onslide<8->
    \item 
      \begin{tabular}[t]{@{}l}
        \textbf{if} $A \isequal base$:\\
        \onslide<9->
        \hspace{2em}$base\gets first\_missing\_ack\_num()$\\
      \end{tabular}
  \end{itemize}
\end{itemize}
\end{frame}

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% mode: outline-minor
%%% mode: TeX-fold
%%% TeX-fold-macro-spec-list: nil
%%% TeX-fold-env-spec-list: (("[PS-PICTURE]" ("pspicture")) 
%%%                          ("[CODEBOX]" ("codebox")))
%%% outline-regexp: "[\t ]*\\\\documentclass\\|[\t ]*\\\\begin *{frame}\\|[\t ]*\\\\begin *{document}\\|[\t ]*\\\\end *{document}\\|%===\\|[\t ]*\\\\part"
%%% End: 
