Home

Documentation

Project Support

Changes in Version 4 of GraspExtrasSoftwareFitspipeSocketProtocol

Author:
crae
Timestamp:
Tue Sep 10 11:26:36 2019
Comment:
Adding footnote about example python client

Legend:

Unmodified
Added
Removed
Modified
  • GraspExtrasSoftwareFitspipeSocketProtocol

    v3 v4
    1 1  = Grasp Software Extras: Fitspipe Network Protocol = 
    2 2  This page describes the protocol used by the 'fitspipe' server bundled as part of the [wiki:GraspExtrasSoftwareFitspipe 'extras'] package in the [wiki:GraspSw software tarball]. It gives a general overview of the server and documents the basic commands used by clients sending or receiving image frames to/from the server. 
    3 3  Emphasis is placed on clients retrieving data; it is expected that external entities will be more likely to be taking frames from a system supplied with GRASP controllers rather than putting data. 
    4 4  == Description == 
    5 5  ''"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. 
    6 6  While it is capable of managing frames from still cameras, its original intent was distribution of video. 
    7 7  To summarise its overall purpose, fitspipe exists to: 
    8 8    * Allow producer clients to distribute image data without needing knowledge of consumers. 
    9 9    * Allow multiple consumer clients to receive image data independently from one another. 
    10 10    * Act as a frame buffer to lessen the requirement that clients always keep up with video frame rates. 
    11 11    * 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. 
    12 12    * Decouple the operation of the actual instrument producing image data (protocols, etc.) from the transport of data. 
    13 13  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. 
    14 14  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 . 
    15 15  == General Operational Concepts == 
    16 16  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. 
    17 17  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. 
    18 18  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  
    19 19  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. 
    20 20  All image data transferred from fitspipe is sent in network byte order, i.e. big-endian. 
    21 21  == Protocol Stack == 
    22 22  Layers of network communications are often classified using the OSI Reference Model. Because the camera 
    23 23  system is not just a concept, but a physical server with hardwired connection to the observatory, it is possible to describe all layers 
    24 24  now: 
    25 25  ||'''Layer'''||'''Purpose'''||'''Commands'''||'''Data'''|| 
    26 26  ||Layer 1    ||Physical     || 10GBase-T    || 10GBase-T || 
    27 27  ||Layer 2    ||Data Link    || Ethernet     || Ethernet  || 
    28 28  ||Layer 3    ||Network      || IP           || IP        || 
    29 29  ||Layer 4    ||Transport    || TCP          || TCP       || 
    30 30  ||Layer 5    ||Session      || Socket       || Socket    || 
    31 31  ||Layer 6    ||Presentation || custom       || custom    || 
    32 32  ||Layer 7    ||Application  || ''cash:'' Camera Shell || Raw/FITS data || 
    33 33  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 
    34 34  describing each layer with some detail to avoid mistaken assumptions (e.g. Will we support IPv6? Jumbo packets?) 
    35 35  === Physical Layer === 
    36 36  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. 
    37 37  === Data Link Layer === 
    38 38  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. 
    39 39  === Network Layer === 
    40 40  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. 
    41 41  === Transport Layer === 
    42 42  TCP will be used on top of IPv4. 
    43 43  By default, the command server will listen for new connections on TCP port 9999. However, clients should have the flexibility to 
    44 44  connect on a different port. 
    45 45  === Session Layer === 
    46 46  Clients of the camera command interface establish a session by opening a socket. The number of active sessions is limited only by 
    47 47  the operating system and CPU resources; the latter becomes increasingly important for high-resolution, high-frame-rate applications. 
    48 48  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. 
    49 49  === Presentation Layer === 
    50 50  A client need only be concerned with managing the application layer (described next). While sockets, TCP/IP and ethernet are all 
    51 51  standard, the presentation layer is the first custom layer so a description is necessary. 
    52 52  Something something line-based messages. 
    53 53  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. 
    54 54  Image data is presented as binary data instead of ASCII with no encoding, FITS header included. 
    55 55  ==== Input direction (command) ==== 
    56 56  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).  
    57 57  TBD More details on encoding. 
    58 58  ==== Output direction (response) ==== 
    59 59  Each command will return one or more lines in response. 
    60 60  After the initial command-response transaction, both the `put` and `get` commands transfer binary data in the relevant direction. 
    61 61  Each response line begins with one of the following: 
    62 62  ||'''String'''||'''Description'''|| 
    63 63  || '> ’   ||Followed by echo of command, acknowledges that command was received and processing has started. || 
    64 64  || '+ '   ||Followed any message line (except the last) of output generated by the command. || 
    65 65  || '. '   ||Followed by final message from command completing successfully. || 
    66 66  || '! '   ||Followed by error description from command which has failed.    || 
    67 67  || '# '   ||Followed by 1-line description of image frame, then binary data. Only used in response to `get`. || 
    68 68  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 
    69 69  such complete response has been received. 
    70 70  The prefix ’* ’ followed by an asynchronous notification is reserved for urgent/out of band messages not associated with any specific 
    71 71  command. For example, 
    72 72    {{{ 
    73 73  ’* warning: system shutting down in 5 minutes’ 
    74 74  }}} 
    75 75  could appear at any time (including in the midst of a response to a command, but never in a way that breaks another line.) 
    76 76  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. 
    77 77  === Application Layer === 
    78 78  If desired, C Language bindings will be provided to interface to the presentation layer. The status server application programming 
    79 79  interface (API) is described in detail in the Status Server Client C API document. 
    80 80  TBD: A separate C library for accessing the command interface will allow the user to: 
    81 81  • Set callback to receive response message strings. 
    82 82  • Set callback to receive asynchronous messages. 
    83 83  • Check for new messages. 
    84 84  • Send an ASCII command string, wait for it to complete, and return binary (PASS or FAIL) status of the request. 
    85 85  • Interrupt the system (from another session.) 
    86 86  Summary of command string input syntax: 
    87 87  • Command names are lower case letters, digits, and underscores only. 
    88 88  • Command parameters are typically specified as a series of `name=value` pairs. 
    89 89  • Command parameters follow the command name, separated by whitespace. 
    90 90  • Parameter names are case insensitive letters, digits, and underscores only. 
    91 91  • Parameter names may be defined containing a single ’ * ’ star character indicating that the parameter name may be abbreviated beyond that point. 
    92 92    * 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. 
    93 93    * Abbreviations are intended as convenience for manual operation modes. ''Automated clients and scripts should use the full name.'' 
    94 94  • Quotation: ’...’ and "..." are accepted. 
    95 95  • Any characters after ’ # ’ outside of quotation taken as comments. 
    96 96  • Command parameters are not sensitive to white space outside of quotation. 
    97 97  • Commands may define their parameters as positional and non-positional. 
    98 98    * Positional parameters may be supplied only as a value without the name= portion of the parameter pair. 
    99 99    * Positional parameters are defined by their order in the set of command parameters. 
    100 100    * 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. 
    101 101    * Non-positional parameters are fully specified by a name=value pair. 
    102 102    * Non-positional parameters are order-independent; each pair fully specifies the name of the parameter, so the parameter may appear anywhere in the set. 
    103 103    * Positional rules are a convenience for manual operation. Automated clients and scripts should always generate parameters with the full option name and an ’=’.  
    104 104  The actual commands intended for the application layer are described in the following section. 
    105 105  == Command Set == 
    106 106  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. 
    107 107  === ls === 
    108 108  `ls` lists all of the available feeds, and provides some information about each one. The command serves two purposes: 
    109 109    * Discovery of available feeds, 
    110 110    * Retrieving information about each feed prior to retrieving a frame of image data. 
    111 111  The `ls` command takes no parameters. 
    112 112  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: 
    113 113  ||'''Name'''  ||'''Type'''||'''Description'''|| 
    114 114  ||'feed='   ||String    ||Name of feed.    || 
    115 115  ||'naxis1=' ||Integer   ||Width of image frame.|| 
    116 116  ||'naxis2=' ||Integer   ||Height of image frame. || 
    117 117  ||'depth='  ||Integer   ||Number of buffered frames.|| 
    118 118  ||'oldest=' ||Integer   ||Sequence number of oldest frame currently held in buffer.|| 
    119 119  ||'newest=' ||Integer   ||Sequence number of most recent frame currently held in buffer.|| 
    120 120  This is then followed by a final line indicating successful completion of the command. 
    121 121  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. 
    122 122  For example: 
    123 123    {{{ 
    124 124  ls 
    125 125  + feed=default naxis1=2048 naxis2=2048 depth=300 oldest=3330 newest=3629 
    126 126  . OK 
    127 127  }}} 
    128 128  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. 
    129 129  === put === 
    130 130  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. 
    131 131  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. 
    132 132  The put command takes the following parameters: 
    133 133  ||'''Name'''||'''Type'''||'''Required?'''||'''Description'''|| 
    134 134  ||'feed=' ||String||Required||Name of the feed to publish to. If the feed doesn't already exist, it is created.|| 
    135 135  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. 
    136 136  === get === 
    137 137  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. 
    138 138  The get command takes the following parameters: 
    139 139  ||'''Name''' ||'''Type'''||'''Required?'''||'''Description'''|| 
    140 140  ||'feed='  ||String ||Required||Name of the feed to fetch a frame from.|| 
    141 141  ||'frame=' ||Integer||Optional||Sequence number of the frame to be retrieved. If not given, default behaviour is to fetch the most recent frame.|| 
    142 142  ||'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.|| 
    143 143  The response to the get command is a little different from all other commands in a couple of ways: 
    144 144    * 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. 
    145 145    * 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. 
    146 146    * The complete response line is always a fixed 40 bytes in length, including the trailing newline. 
    147 147  The fixed-length response takes the following format: 
    148 148    {{{ 
    149 149  '# frame width x height   \n' 
    150 150  }}} 
    151 151  where the fields occupy fixed positions and have fixed widths: 
    152 152    frame:: 
    153 153    The frame sequence number, as recorded by the server (integer, 10 digits wide, chars 2-11). 
    154 154    width:: 
    155 155    The width of the image in pixels (integer, 10 digits wide, chars 13-22). 
    156 156    height:: 
    157 157    The height of the image in pixels (integer, 10 digits wide, chars 26-35). 
    158 158  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. 
    159 159  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.'' 
    160 160  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. 
    161 161  Only 16-bit-per-pixel image data is supported, so at this point, the size of the image data in bytes is known. 
    162 162  After the initial one-line response, the server sends the frame as binary data. 
    163 163  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: 
    164 164    * FITS headers consist of one or more blocks of exactly 2880 bytes. 
    165 165    * Each block consists of 36 80-byte 'cards'. 
    166 166    * Each card begins with an 8-byte text keyword that names the card. 
    167 167    * If any keyword in a block is `END     ` ("END" followed by 5 ASCII spaces), that indicates the block is the last in the header. 
    168 168      * Any keyword in a block may indicate the end of the header. 
    169 169      * No cards containing data will be found after the `END` keyword. 
    170 170    * 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. 
    171 171  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. 
    172 172  If no header is requested, then the header is skipped and the image data is sent  immediately after the one-line text response. 
    173 173  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. 
    174 174  == Common FITS Pitfalls == 
    175 175  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: 
    176 176  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. 
    177 177  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: 
    178 178    {{{ 
    179 179  physical value = BSCALE * (storage value) + BZERO 
    180 180  }}} 
    181 181  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. 
    182 182  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. 
    183 183  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. 
    184 184  == Examples == 
    185 185  Some examples of client-server interactions to transfer frames from fitspipe. 
    186 186  Note: trailing newline characters on each message are omitted for clarity. 
    187 187  === Client retrieves latest frame === 
    188 188  The client grabs one frame (the latest in the server's buffer) from the server with no header. 
    189 189  [[Image(example_client_latest_frame.png)]] 
    190 190  === Client retrieves latest frame (including header) === 
    191 191  The client grabs one frame (the latest in the server's buffer) from the server, preceded by a header. 
    192 192  [[Image(example_client_latest_frame_with_header.png)]] 
    193 193  === Client Gets Multiple Frames === 
    194 194  A client that wishes to get multiple frames has a couple of different strategies it can use, all of which are similar to the 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: 
    195 195    * The client queries the server with 'ls' ahead of each and every frame. 
    196 196    * The client can maintain its own running sequence number without repeated queries of the server. 
    197 197  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. 
    198 198  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. 
    199 199  ==== Client Queries Server on Each Frame ==== 
    200 200  In this example, the client makes no assumptions about which is the "current" frame, so it queries the server ahead of each transfer. 
    201 201  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. 
    202 202  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. 
    203 203  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. 
    204 204  [[Image(example_client_new_frame_ls.png)]] 
    205 205  ==== Client Maintains its own Frame Count ==== 
    206 206  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. 
    207 207  The client may request and receive headers on each frame; this is omitted for brevity. 
    208 208  [[Image(example_client_new_frame_counter.png)]] 
      209   
      210  === Example Client === 
      211   
      212  As an added bonus, an [https://www.stargrasp.org/attachment/wiki/GraspExtrasSoftwareFitspipeSocketProtocol/fitspipe-get.py example implementation of a simple fitspipe-get client in Python] was produced from reading this document to illustrate fetching images from a fitspipe server with full FITS headers, and then producing either concatenated frames on stdout to be piped to other fitspipe-style tools, or to a series individual simple FITS image files. 
      213   
      214  {{{ 
      215  #!html 
      216  <b><font style="color: red">This is NOT provided for production use.</font></b> 
      217  }}} 
      218   
      219  The example code does not make any serious attempts at error detection (never mind correction), does the bare minimum to get things up and running on the network, makes a whole bunch of assumptions that are easily exploitable, and is likely not a good guide to Python programming practices in general.