Home

Documentation

Project Support

Changes in Version 1 of GraspExtrasSoftwareFitspipeSocketProtocol

Author:
crae
Timestamp:
Tue Sep 10 10:39:16 2019
Comment:
Initial version copied over from camera wiki. Will likely have to fix a bunch of things related to the newer trac there, and replace the plantuml diagrams with images

Legend:

Unmodified
Added
Removed
Modified
  • GraspExtrasSoftwareFitspipeSocketProtocol

    v0 v1
      1  = Grasp Software Extras: Fitspipe Network Protocol = 
      2   
      3  [[PageOutline(2-5,Contents,pullout)]] 
      4   
      5  We're being forced to document how fitspipe works below the stargrasp toolchain level for DL-NIRSP, so before it to the point where I'm happy to commit it to some more permanent format, I'm writing things up here (the plantuml stuff will likely be helpful for ladder diagrams, etc.). 
      6   
      7  == Description == 
      8   
      9  ''"fitspipe"'' is a network server process that is used to transmit streams of image data from various instruments. Due to its background in astronomy, it is primarily focused on handling data that conforms to the [https://fits.gsfc.nasa.gov/fits_documentation.html FITS] format. As such, some of the conventions used are somewhat FITS-centric. 
      10   
      11  While it is capable of managing frames from still cameras, its original intent was distribution of video. 
      12   
      13  To summarise its overall purpose, fitspipe exists to: 
      14    * Allow producer clients to distribute image data without needing knowledge of consumers. 
      15    * Allow multiple consumer clients to receive image data independently from one another. 
      16    * Act as a frame buffer to lessen the requirement that clients always keep up with video frame rates. 
      17    * Keep potentially high-frame-rate, high-bandwidth streams away from disk storage. fitspipe predates the existence of fast SSDs, and not relying on disk storage avoids problems with access times, transfer rates, file management, ssd flash wear, etc. 
      18    * Decouple the operation of the actual instrument producing image data (protocols, etc.) from the transport of data. 
      19   
      20  For example, the IR cameras for DL-NIRSP transport data via UDP jumbo frames with no facility to retransmit missed packets. Since attempting to distribute data that way would be a nightmare, the camera server publishes each frame as it's received to fitspipe, translating to TCP, big buffers and normal MTUs. 
      21   
      22  This document is intended for those that need to interface with fitspipe at the socket level. For users that simply want to quickly use fitspipe in a system, the suggested first step is to use the command-line tools available in the STARGRASP software tarball at https://www.stargrasp.org/wiki/GraspSw . 
      23   
      24  == General Operational Concepts == 
      25   
      26  fitspipe allows multiple independent streams of data to exist simultaneously. In fitspipe parlance, these are termed ''"feeds"''. Each feed represents one or more buffered frames of image data. Each frame received is assigned an incrementing sequence number, which is used by clients to retrieve data. 
      27   
      28  The buffer size for a stream is a function of image dimensions and ''"depth"'' in fitspipe parlance.  The ''"depth"'' here refers to number of buffered frames that will be kept in memory (not image format or bits per pixel, of which only 16bpp is currently supported by fitspipe.)  The depth can be set on the server command line. 
      29   
      30  Clients sending frames of image data to the fitspipe server (''"put"'') specify which feed the data should be published to. New feeds are created on demand  
      31   
      32  Clients retrieving frames of image data from the server (''"get"'') are responsible for keeping track of which frames are available in the server's buffer versus which frames they have yet to receive. To assist data retrieval without polling, a blocking mechanism exists to allow a client to wait for the next frame to arrive. 
      33   
      34  All image data transferred from fitspipe is sent in network byte order, i.e. big-endian. 
      35   
      36  == Protocol Stack == 
      37   
      38  '''Basically lifted straight from the DLCam command interface doc...'''  Which lifted its content from the [https://svn.ifa.hawaii.edu/gpc/archive/psdc/gpc-conductor-interface/ PS1 OTIS-CAM interface document.] 
      39   
      40  Layers of network communications are often classified using the OSI Reference Model. Because the camera 
      41  system is not just a concept, but a physical server with hardwired connection to the observatory, it is possible to describe all layers 
      42  now: 
      43   
      44    ||=Layer=||=Purpose=||=Commands=||=Data=|| 
      45    ||Layer 1    ||Physical     || 10GBase-T    || 10GBase-T || 
      46    ||Layer 2    ||Data Link    || Ethernet     || Ethernet  || 
      47    ||Layer 3    ||Network      || IP           || IP        || 
      48    ||Layer 4    ||Transport    || TCP          || TCP       || 
      49    ||Layer 5    ||Session      || Socket       || Socket    || 
      50    ||Layer 6    ||Presentation || custom       || custom    || 
      51    ||Layer 7    ||Application  || ''cash:'' Camera Shell || Raw/FITS data || 
      52    ||||||||   '''Table 1: Protocol Stack Summary'''   || 
      53   
      54  In so many words, a “socket connection over TCP/IP on Ethernet” could describe interface layers 1-5, but to be explicit it is worth 
      55  describing each layer with some detail to avoid mistaken assumptions (e.g. Will we support IPv6? Jumbo packets?) 
      56   
      57  === Physical Layer === 
      58   
      59  Between the camera server running fitspipe and the rest of an observatory (CSS in the case of DKIST) will be a series of switches, fibers and/or copper wiring connecting the camera server to the facility network. 
      60   
      61  === Data Link Layer === 
      62   
      63  The camera server expects to operate over 10-Gigabit Ethernet with normal (1500 byte) max. transmission units (MTU). Operation over lower-bandwidth links is possible, but will eventually result in missing data as the aggregate data rate of the camera at the required frame rates is above 1 Gigabit/sec. Operation over higher-bandwidth links is permissible. 
      64   
      65  === Network Layer === 
      66  Internet Protocol v4 will be used. The camera server running fitspipe will be on a private IP subnet (not Internet). If necessary, switches will route communications with the facility. 
      67   
      68  === Transport Layer === 
      69  TCP will be used on top of IPv4. 
      70   
      71  By default, the command server will listen for new connections on TCP port 9999. However, clients should have the flexibility to 
      72  connect on a different port. 
      73   
      74  === Session Layer === 
      75   
      76  Clients of the camera command interface establish a session by opening a socket. The number of active sessions is limited only by 
      77  the operating system and CPU resources; the latter becomes increasingly important for high-resolution, high-frame-rate applications. 
      78   
      79  It is possible for any given client to use a new socket for every frame retrieved, but it is also acceptable to re-use a single socket connection. 
      80   
      81  === Presentation Layer === 
      82   
      83  A client need only be concerned with managing the application layer (described next). While sockets, TCP/IP and ethernet are all 
      84  standard, the presentation layer is the first custom layer so a description is necessary. 
      85   
      86  Something something line-based messages. 
      87   
      88  First, a “line” is defined as any string of up to 32767 ASCII characters. Within the communications stream, lines are terminated by `\r` or `\n `. Note that this does not imply that the application layer supports command line lengths of 32767. Line type prefixes and encoding of control characters and non-7-bit ASCII characters within the message itself may occupy extra room. 
      89   
      90  Image data is presented as binary data instead of ASCII with no encoding, FITS header included. 
      91   
      92  ==== Input direction (command) ==== 
      93  Command input is only valid when previous command response has been received completely. or a new socket has just been opened. Input consists of a maximum of 32767 characters between ASCII 32 and 127 inclusive followed by a single \r or \n (ASCII 13 or 10).  
      94   
      95  TBD More details on encoding. 
      96   
      97  ==== Output direction (response) ==== 
      98   
      99  Each command will return one or more lines in response. 
      100   
      101  After the initial command-response transaction, both the `put` and `get` commands transfer binary data in the relevant direction. 
      102   
      103  Each response line begins with one of the following: 
      104   
      105    ||=String=||=Description=|| 
      106    || `'> ’`   ||Followed by echo of command, acknowledges that command was received and processing has started. || 
      107    || `'+ '`   ||Followed any message line (except the last) of output generated by the command. || 
      108    || `'. '`   ||Followed by final message from command completing successfully. || 
      109    || `'! '`   ||Followed by error description from command which has failed.    || 
      110    ||`'# '`   ||Followed by 1-line description of image frame, then binary data. Only used in response to `get`. || 
      111    ||||  '''Table 2: Response line prefixes'''  || 
      112   
      113  The client must not feed a new line of input until ’.’ or ’!’ has been received. The client is also not expected to close the session until 
      114  such complete response has been received. 
      115   
      116  The prefix ’* ’ followed by an asynchronous notification is reserved for urgent/out of band messages not associated with any specific 
      117  command. For example, 
      118    {{{ 
      119  ’* warning: system shutting down in 5 minutes’ 
      120  }}} 
      121   
      122  could appear at any time (including in the midst of a response to a command, but never in a way that breaks another line.) 
      123   
      124  In addition to the Conductor Camera Shell namespace, the camera server command interface and the low-level interface to the FPGA boards also uses this presentation layer. 
      125   
      126  === Application Layer === 
      127   
      128  If desired, C Language bindings will be provided to interface to the presentation layer. The status server application programming 
      129  interface (API) is described in detail in the Status Server Client C API document. 
      130  TBD: A separate C library for accessing the command interface will allow the user to: 
      131  • Set callback to receive response message strings. 
      132  • Set callback to receive asynchronous messages. 
      133  • Check for new messages. 
      134  • Send an ASCII command string, wait for it to complete, and return binary (PASS or FAIL) status of the request. 
      135  • Interrupt the system (from another session.) 
      136   
      137  Summary of command string input syntax: 
      138  • Command names are lower case letters, digits, and underscores only. 
      139  • Command parameters are typically specified as a series of `name=value` pairs. 
      140  • Command parameters follow the command name, separated by whitespace. 
      141  • Parameter names are case insensitive letters, digits, and underscores only. 
      142  • Parameter names may be defined containing a single ’ * ’ star character indicating that the parameter name may be abbreviated beyond that point. 
      143    * The root portion of the parameter name (up to any ’ * ’ star character given in the definition) is required. Additional characters, up to the full name, are optional but may not be mistyped. 
      144    * Abbreviations are intended as convenience for manual operation modes. ''Automated clients and scripts should use the full name.'' 
      145  • Quotation: ’...’ and "..." are accepted. 
      146  • Any characters after ’ # ’ outside of quotation taken as comments. 
      147  • Command parameters are not sensitive to white space outside of quotation. 
      148  • Commands may define their parameters as positional and non-positional. 
      149    * Positional parameters may be supplied only as a value without the name= portion of the parameter pair. 
      150    * Positional parameters are defined by their order in the set of command parameters. 
      151    * If the first character in a parameter argument is not a legal parameter name character, or if the first non-legal character is not ’=’ (equal sign) then the entire argument is interpreted as a value for a positional parameter. 
      152    * Non-positional parameters are fully specified by a name=value pair. 
      153    * Non-positional parameters are order-independent; each pair fully specifies the name of the parameter, so the parameter may appear anywhere in the set. 
      154    * Positional rules are a convenience for manual operation. Automated clients and scripts should always generate parameters with the full option name and an ’=’.  
      155   
      156  The actual commands intended for the application layer are described in the following section. 
      157   
      158  == Command Set == 
      159   
      160  Fitspipe supports a very limited command set. There are single commands for feed discovery/interrogation, to put a frame to the server, and to get one. These are performed with `ls`, `put` and `get` accordingly. 
      161   
      162  === ls === 
      163   
      164  `ls` lists all of the available feeds, and provides some information about each one. The command serves two purposes: 
      165    * Discovery of available feeds, 
      166    * Retrieving information about each feed prior to retrieving a frame of image data. 
      167   
      168  The `ls` command takes no parameters. 
      169   
      170  If one or more feeds exist, it receives one or more lines in response, each representing a single feed. Each per-feed line begins with the continuation string documented above, followed by the following `name=value` parameter pairs: 
      171   
      172    ||=Name=||=Type=||=Description=|| 
      173    ||`'feed='`  ||String    ||Name of feed.    || 
      174    ||`'naxis1='` ||Integer   ||Width of image frame.|| 
      175    ||`'naxis2='` ||Integer   ||Height of image frame. || 
      176    ||`'depth='` ||Integer   ||Number of buffered frames.|| 
      177    ||`'oldest='` ||Integer   ||Sequence number of oldest frame currently held in buffer.|| 
      178    ||`'newest='` ||Integer   ||Sequence number of most recent frame currently held in buffer.|| 
      179   
      180   
      181  This is then followed by a final line indicating successful completion of the command. 
      182   
      183  If no feeds have been created, then there will be no lines of data ahead if the completion indication, but the command will indicate that it was processed successfully. 
      184   
      185  For example: 
      186    {{{ 
      187  ls 
      188  + feed=default naxis1=2048 naxis2=2048 depth=300 oldest=3330 newest=3629 
      189  . OK 
      190  }}} 
      191   
      192  The above response indicates that there is a single feed named "default" available, representing 300 buffered frames of 2048x2048-pixel image data. The oldest frame can be retrieved with sequence number 3330, and the most recent by requesting frame 3629. Frames with sequence numbers below that range have been dropped from the buffer and no longer exist, and frames with higher sequence numbers have yet to be received. 
      193   
      194  === put === 
      195   
      196  The `put` command publishes a single frame of FITS image data to the server. Given the name of a feed, the server then expects a binary transfer of a full simple FITS image, including a complete header and padding as required in a FITS file if it were on disk. 
      197   
      198  Because the transfer includes the header, the client doesn't need to specify any other information about the image being transferred other than the feed name; the FITS header defines the dimensions of the data, etc. This can be useful for general-purpose clients that don't need to know anything about the data they're uploading. 
      199   
      200  The put command takes the following parameters: 
      201   
      202    ||=Name=||=Type=||=Required?=||=Description=|| 
      203    ||`'feed='` ||String||Required||Name of the feed to publish to. If the feed doesn't already exist, it is created.|| 
      204   
      205  The server replies with a single line indicating success or failure. Upon success, the client then writes the entire FITS image, header included, as binary data. 
      206   
      207  === get === 
      208   
      209    {{{ 
      210  %%% Should we sort out the server response so that it indicates the size of the header?  
      211  It would make the client's job a fair bit simpler... 
      212   
      213  %%% SI: I don't know, but I suspect if you go try to make this improvement you'll find out why it is a challenge.  Probably some of the info needed at the time of the response is not always there yet or something.  (I.e., the server may not always know the size yet, but it has to write something into the buffer and respond right away because of the way sockio events are processed.) 
      214  }}} 
      215   
      216  The `get` command retrieves a single frame of image data from the server. The client passes the name of the feed and a sequence number for the frame to be retrieved, along with an indication of whether the FITS header should also be downloaded. 
      217   
      218  The get command takes the following parameters: 
      219   
      220    ||=Name=||=Type=||=Required?=||=Description=|| 
      221    ||`'feed='`  ||String||Rerquired||Name of the feed to fetch a frame from.|| 
      222    ||`'frame='` ||Integer||Optional||Sequence number of the frame to be retrieved. If not given, default behaviour is to fetch the most recent frame.|| 
      223    ||`'fullheader='` ||Boolean||Optional||Indicates whether FITS header should be transferred. '1' to send FITS header, '0' to send only the image data. Default value is 0.|| 
      224   
      225  The response to the get command is a little different from all other commands in a couple of ways: 
      226    * If the command was parsed successfully and the requested image is available, the response will be a single line beginning with `# ` as indicated above, followed by a minimal amount of data about the image. The intent is to signify that the first line is not part of the actual data, but is more like a comment providing more information about the following image data. 
      227    * If the command was parsed successfully, the feed exists, but the frame number is for a future frame that does not yet exist, the response line will be started with `# `, but further output to that client will block until that frame becomes available. Once the frame has been uploaded to the server, the rest of the metadata comment line will be sent, followed by the image data. 
      228    * The complete response line is always a fixed 40 bytes in length, including the trailing newline. 
      229   
      230  The fixed-length response takes the following format: 
      231    {{{ 
      232  '# frame width x height   \n' 
      233  }}} 
      234   
      235  where the fields occupy fixed positions and have fixed widths: 
      236    frame:: 
      237    The frame sequence number, as recorded by the server (integer, 10 digits wide, chars 2-11). 
      238    width:: 
      239    The width of the image in pixels (integer, 10 digits wide, chars 13-22). 
      240    height:: 
      241    The height of the image in pixels (integer, 10 digits wide, chars 26-35). 
      242   
      243  There is a guaranteed space between the leading `#` and the first fields and also between each of the fields. The fields are always arranged in the fixed offsets indicated above. The line ends with three spaces and a newline to fill out the remaining 40 bytes. 
      244   
      245  Note that if the client requests a frame number that has been discarded (i.e. is lower than the oldest buffered frame), ''the server currently responds by sending the latest frame''. ''It is up to the client to check that the frame number received is the same as that requested, and to deal with the case where it is not.'' 
      246   
      247  Both this behaviour and the fixed 40-byte format stems from a previous system involving small images transferred at high rates, keeping the overhead down as far as possible in terms of bytes transferred (and thus the number of packets per frame) and latency. The fixed-width/fixed-position fields also reduce the complexity of parsing the received data, further reducing latency. 
      248   
      249  Only 16-bit-per-pixel image data is supported, so at this point, the size of the image data in bytes is known. 
      250   
      251  After the initial one-line response, the server sends the frame as binary data. 
      252   
      253  If a header was requested, then the client is expected to detect and handle the additional data by following the standard for FITS headers. The client will not know ahead of time the size of the header, but the rules for FITS headers make it possible to determine the extent of the header. While it is outside the scope of this document to fully describe the FITS header format, the relevant rules can be summarised as follows: 
      254    * FITS headers consist of one or more blocks of exactly 2880 bytes. 
      255    * Each block consists of 36 80-byte 'cards'. 
      256    * Each card begins with an 8-byte text keyword that names the card. 
      257    * If any keyword in a block is `END     ` ("END" followed by 5 ASCII spaces), that indicates the block is the last in the header. 
      258      * Any keyword in a block may indicate the end of the header. 
      259      * No cards containing data will be found after the `END` keyword. 
      260    * Alternatively, if no `END` keyword is present in a header block, the header continues, and the current block will be followed by at least one more 2880-byte block. 
      261   
      262  After the last 2880-byte block is received for the header, the server sends the image data. The image data is contiguous with the last header block, and consists of 16 bit-per-bixel values of the dimensions indicated in the first line of the response. The first pixel sent is (0,0), followed by the rest of the first row of pixels, followed by the next row, etc. 
      263   
      264  If no header is requested, then the header is skipped and the image data is sent  immediately after the one-line text response. 
      265   
      266  Note that it is strongly recommended that clients always take the header for each image. The overhead of receiving a few header blocks per frame versus the full image frame is usually minimal, and the headers may contain metadata that could be useful to be passed on from the camera system. 
      267   
      268  == Common FITS Pitfalls == 
      269   
      270  While they are not strictly related to the fitspipe protocol, the FITS standard does lay a few traps for the unwary. A brief recap is therefore probably worthwhile: 
      271   
      272  As mentioned, image data is sent in network byte order, and handily the FITS standard requires big-endian image data. It may be tempting to think that little-endian systems need to do a lot of byte-swapping to "fix" the 16-bit pixels, but this is not the case so long as the data remains in FITS format. However, the client must consider endianness if it translates the image data to some other format. 
      273   
      274  The FITS standard does not allow for unsigned integer values for pixels; all stored values are signed. However, raw image data is typically a series of unsigned A/D converter measurements. To get around this, a standard FITS header will include two keywords, `BZERO` and `BSCALE` which are used (as per the [https://docs.astropy.org/en/stable/io/fits/usage/image.html#scaled-data astropy docs]) to transform stored values to the original physical values as follows: 
      275    {{{ 
      276  physical value = BSCALE * (storage value) + BZERO 
      277  }}} 
      278   
      279  Because the client that published the data to fitspipe followed the FITS standard, the reverse transform was applied to the raw data before uploading, and all of the pixel values were shifted and scaled. ''If the client intends to write data out to a non-FITS format, it must transform every one of the pixels received before passing the data along.'' Failure to do so will result in odd value wrapping problems, etc. 
      280   
      281  In general, for most 16-bit-per-pixel systems it is somewhat safe to assume that BZERO is 32768 and BSCALE is 1.0. However, it is more correct to transfer the header and retrieve the values directly from the relevant keyword values. 
      282   
      283  If the client intends to write a FITS image using the supplied header and image data, please note that the FITS standard requires the image data to be padded out to the next 2880-byte boundary with zeroes. Since it is trivial to generate the required padding within the client (and probably far less expensive than transferring trailing zeroes across the network), the image data from the server does '''not''' include this padding. 
      284   
      285  == Examples == 
      286   
      287  Some examples of client-server interactions to transfer frames from fitspipe. 
      288   
      289  Note: trailing newline characters on each message are omitted for clarity. 
      290   
      291  === Client retrieves latest frame ===  #ExampleLatestFrame 
      292   
      293  The client grabs one frame (the latest in the server's buffer) from the server with no header. 
      294   
      295    {{{#!PlantUml 
      296  @startuml 
      297  title Fitspipe client gets latest frame 
      298   
      299  participant Client 
      300  participant Server 
      301   
      302  Client->Server: 'ls' 
      303  activate Server #FEFECE 
      304  Server->Client: <size:9>'+ feed=default naxis1=2048 naxis2=2048 depth=300 oldest=3330 newest=3629\n. OK'</size> 
      305   
      306  deactivate Server 
      307  note left of Client  
      308  Newest frame is 3629 
      309  end note 
      310   
      311  Client -> Server: 'get feed=default framenum=3629 fullheader=0' 
      312  activate Server #FEFECE 
      313  Server -> Client: '#       3629       2048 x       2048   ' 
      314   
      315  note left of Client 
      316  Client knows to expect  
      317  2048x2048 2-byte pixels, 
      318  allocates  8388608 bytes  
      319  for image data. 
      320  end note 
      321   
      322  Server->Client: **Binary image transfer** 
      323   
      324  deactivate Server 
      325  @enduml 
      326  }}} 
      327   
      328  === Client retrieves latest frame (including header) === 
      329   
      330  The client grabs one frame (the latest in the server's buffer) from the server, preceded by a header. 
      331   
      332    {{{#!PlantUml 
      333  @startuml 
      334  title Fitspipe client gets latest frame 
      335   
      336  participant Client 
      337  participant Server 
      338   
      339  Client->Server: 'ls' 
      340  activate Server #FEFECE 
      341  Server->Client: <size:9>'+ feed=default naxis1=2048 naxis2=2048 depth=300 oldest=3330 newest=33629\n. OK'</size> 
      342   
      343  deactivate Server 
      344  note left of Client  
      345  Newest frame is 3629 
      346  end note 
      347   
      348  Client -> Server: 'get feed=default framenum=3629 fullheader=1' 
      349  activate Server #FEFECE 
      350  Server -> Client: '#       3629       2048 x       2048   ' 
      351   
      352  loop 1 or more times 
      353  Server -> Client: **2880-byte header block** 
      354  note left of Client 
      355  Client repeatedly  
      356  reads 2880-byte  
      357  header blocks,  
      358  looking for END 
      359  end note 
      360  end 
      361   
      362  note left of Client 
      363  Client knows to expect  
      364  2048x2048 2-byte pixels, 
      365  allocates  8388608 bytes  
      366  for image data. 
      367  end note 
      368   
      369  Server->Client: **Binary image transfer** 
      370   
      371  deactivate Server 
      372  @enduml 
      373  }}} 
      374   
      375  === Client Gets Multiple Frames === 
      376   
      377  A client that wishes to get multiple frames has a couple of different strategies it can use, all of which are similar to the [#ExampleLatestFrame latest frame example] above. After the initial `ls` to get a sense of the current frame sequence number, the client can attack this in a couple of ways: 
      378    * The client queries the server with 'ls' ahead of each and every frame. 
      379    * The client can maintain its own running sequence number without repeated queries of the server. 
      380   
      381  In either case, ''it is the client's responsibility'' to ensure that the sequence number in the initial `# ` response line matches that which was requested. 
      382   
      383  It is also the client's choice as to how to react to a mismatch. For example, the `fitspipe-get` example tool in the STARGRASP tarball doesn't attempt to do anything intelligent - if it falls so far behind that the server returns the latest frame instead of a frame it no longer has, `fitspipe-get` simply declares the missing frames lost forever while printing a warning, instead of trying to do something more dynamic to attempt to drop as few frames as possible. 
      384   
      385  ==== Client Queries Server on Each Frame ==== 
      386   
      387  n this example, the client makes no assumptions about which is the "current" frame, so it queries the server ahead of each transfer. 
      388   
      389  Note: if the client relies solely on the server's view of the latest frame without maintaining any sort of state for itself, it may be vulnerable to skipping frames if the client momentarily takes longer than a single frame period to between requests. For example, if it requests and gets frame 300, then some sort of garbage collection mechanism kicks in (or similar) and the camera keeps producing new data in the meantime, it may come back and find on the next query that the current frame is 302. If it blindly requests that frame, then it never gets frame 301 from the server, and drops that frame. 
      390   
      391  For that reason, it is strongly recommended that ''so long as latency is not a concern'', even if it queries the server each time the client should maintain its own counter and request sequential frames even if it falls behind. For a given application, fitspipe's buffer should have been sized appropriately to allow the client to lag for a short time and then catch up again, dropping no data. 
      392   
      393  On the other hand, if latency is more important than receiving and processing each and every frame (e.g. for guide video), it may be preferable to assume older frames are stale and should be skipped. As such, it is probably preferable in this case to always request the latest frame regardless of however many other frames are buffered; applications such as this might also consider running fitspipe with a very small frame buffer. 
      394   
      395    {{{#!PlantUml 
      396  @startuml 
      397  title Fitspipe client waits for new data 
      398   
      399  participant Client 
      400  participant Server 
      401   
      402  loop multiple times 
      403  Client->Server: 'ls' 
      404  activate Server #FEFECE 
      405  Server->Client: <size:9>'+ feed=default naxis1=2048 naxis2=2048 depth=300 oldest=n-299 newest=n</size>\n.<size:9>OK'</size> 
      406   
      407  deactivate Server 
      408  note left of Client  
      409  Newest frame is //n//, 
      410  client will wait for //n+1// 
      411  end note 
      412   
      413  Client -> Server: 'get feed=default framenum=n+1 fullheader=0' 
      414  activate Server #FEFECE 
      415  Server -> Client: '# ' **(Note: remaining 38 bytes still outstanding) ** 
      416    
      417  ... Camera eventually produces a new frame and uploads to fitspipe ... 
      418   
      419  Server -> Client: '     n+1       2048 x       2048   ' **(Note: line continuation) ** 
      420   
      421  note left of Client 
      422  Client knows to expect  
      423  2048x2048 2-byte pixels, 
      424  allocates  8388608 bytes  
      425  for image data. 
      426  end note 
      427   
      428  Server->Client: **Binary image transfer** 
      429  end 
      430   
      431  deactivate Server 
      432  @enduml 
      433  }}} 
      434   
      435  ==== Client Maintains its own Frame Count ==== 
      436   
      437  Alternatively, the client, after initially querying the server, can maintain its own frame counter and request sequential frames without checking with the server each time. 
      438   
      439  The client may request and receive headers on each frame; this is omitted for brevity. 
      440   
      441    {{{#!PlantUml 
      442  @startuml 
      443  title Fitspipe client waits for new data 
      444   
      445  participant Client 
      446  participant Server 
      447   
      448  Client->Server: 'ls' 
      449  activate Server #FEFECE 
      450  Server->Client: <size:9>'+ feed=default naxis1=2048 naxis2=2048 depth=300 oldest=n-299 newest=n</size>\n.<size:9>OK'</size> 
      451   
      452  deactivate Server 
      453  note left of Client  
      454  Newest frame is //n//, 
      455  client will wait for //n+1// 
      456  end note 
      457   
      458  Client -> Server: 'get feed=default framenum=n+1 fullheader=0' 
      459  activate Server #FEFECE 
      460  Server -> Client: '# ' **(Note: remaining 38 bytes still outstanding) ** 
      461    
      462  ... Camera eventually produces a new frame and uploads to fitspipe ... 
      463   
      464  Server -> Client: '     n+1       2048 x       2048   ' **(Note: line continuation)** 
      465   
      466  note left of Client 
      467  Client knows to expect  
      468  2048x2048 2-byte pixels, 
      469  allocates  8388608 bytes  
      470  for image data. 
      471  end note 
      472   
      473  Server->Client: **Binary image transfer** 
      474   
      475  note left of Client  
      476  Previous frame was //n+1//, 
      477  request //n+2// 
      478  end note 
      479   
      480  Client -> Server: 'get feed=default framenum=n+2 fullheader=0' 
      481  Server -> Client: '# ' **(Note: remaining 38 bytes still outstanding) ** 
      482    
      483  ... Camera eventually produces a new frame and uploads to fitspipe ... 
      484   
      485  Server -> Client: '     n+2       2048 x       2048   ' 
      486  Server->Client: **Binary image transfer** 
      487   
      488   
      489  note left of Client  
      490  Previous frame was //n+2//, 
      491  request //n+3// 
      492  end note 
      493   
      494  Client -> Server: 'get feed=default framenum=n+3 fullheader=0' 
      495  Server -> Client: '# ' 
      496    
      497  ... Camera eventually produces a new frame and uploads to fitspipe ... 
      498   
      499  Server -> Client: '     n+3       2048 x       2048   ' (Note: line continuation) 
      500  Server -> Client: **Binary image transfer** 
      501   
      502  == etc. == 
      503   
      504  deactivate Server 
      505  @enduml 
      506  }}}