src/lib/AbstractParser.h
branchv_0
changeset 1 68a281aefa76
equal deleted inserted replaced
0:28294b895e5e 1:68a281aefa76
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <cstring>
       
    20 #include <cstdint>
       
    21 
       
    22 namespace relpipe {
       
    23 namespace in {
       
    24 namespace asn1 {
       
    25 namespace lib {
       
    26 
       
    27 class AbstractParserImpl;
       
    28 
       
    29 /**
       
    30  * Event-driven parser that consumes sequence of byte buffers (chunked stream)
       
    31  * and produces some output (implementation specific).
       
    32  * 
       
    33  * The flow is controlled from outside: incomming data pieces are passed to the parser through the write() method as they come.
       
    34  * 
       
    35  * From the child class perspective (implementation of particular parser)
       
    36  * data pieces are obtained through the read() method from the internal buffer maintained by AbstractParser.
       
    37  */
       
    38 class AbstractParser {
       
    39 private:
       
    40 	friend AbstractParserImpl;
       
    41 	AbstractParserImpl* implementation;
       
    42 	void rollback();
       
    43 public:
       
    44 	virtual ~AbstractParser();
       
    45 
       
    46 	/**
       
    47 	 * Submit a part of incomming data to the parser.
       
    48 	 * When possible, such data are usually processed synchronously during this method call.
       
    49 	 * If not (incomplete TLV/AVP/PDU/token/etc.) this part is appended to the internal buffer maintained by AbstractParser
       
    50 	 * and processed in some next cycle or – in worst case – during the close() call.
       
    51 	 * 
       
    52 	 * @param buffer readable memory
       
    53 	 * @param length size of the buffer
       
    54 	 */
       
    55 	void write(const uint8_t* buffer, const size_t length);
       
    56 
       
    57 	/**
       
    58 	 * Finalize the parsing process.
       
    59 	 * After calling this method, all data from AbstractParser buffers should be consumed, parsed and results published.
       
    60 	 * No write() call is expected after close() and would fail.
       
    61 	 * However the parser object remains valid and may be used to get some auxiliary information (if supperted by given implementation).
       
    62 	 */
       
    63 	virtual void close();
       
    64 protected:
       
    65 	AbstractParser();
       
    66 
       
    67 	/**
       
    68 	 * May be thrown from the update() method in order to cancel currenty cycle and do explicit rollback.
       
    69 	 * Same data will be processed in the next cycle.
       
    70 	 */
       
    71 	class ExplicitRollbackException {
       
    72 		// TODO: common super-class for exceptions, hidden implementation
       
    73 	};
       
    74 
       
    75 	/**
       
    76 	 * May be called from the update() method in order to explicitly confirm that read data was successfully processed.
       
    77 	 * Such data will not be processed again during the next cycle (even if ReadBufferUnderflowException occurs later in this cycle).
       
    78 	 
       
    79 	 * Explicit commit() call is useful when we did some demanding work (so we are not willing to do it again in case of ReadBufferUnderflowException);
       
    80 	 * and is necessary when we already published some outputs or did other non-idempotent operation or caused some other significant side effects.
       
    81 	 
       
    82 	 * If there is no commit() called and update() just finishes, commit() is called implicitly.
       
    83 	 * 
       
    84 	 * Note: There is no accessible rollback() method – throw ExplicitRollbackException instead.
       
    85 	 */
       
    86 	void commit();
       
    87 
       
    88 	/**
       
    89 	 * Fill the buffer with incomming data of given length (exactly).
       
    90 	 * 
       
    91 	 * If there are not enough data available, ReadBufferUnderflowException is thrown.
       
    92 	 * This exception should not be caught in the child class – it should propagate back to the AbstractParser
       
    93 	 * where it causes rollback(). In such case, the same data will be available during next update() cycle.
       
    94 	 * 
       
    95 	 * @param buffer writable memory
       
    96 	 * @param length size of the buffer
       
    97 	 */
       
    98 	void read(uint8_t* buffer, const size_t length);
       
    99 
       
   100 	/**
       
   101 	 * Like read(), but does not update the marks (buffer positions), so it can be called again and again with same result.
       
   102 	 * 
       
   103 	 * @param buffer writable memory
       
   104 	 * @param length size of the buffer
       
   105 	 */
       
   106 	void peek(uint8_t* buffer, const size_t length);
       
   107 
       
   108 	/**
       
   109 	 * @return total number of bytes that were successfully read
       
   110 	 */
       
   111 	size_t getBytesRead();
       
   112 
       
   113 	/**
       
   114 	 * Reads input from buffers using read(), parses data and usually emits the result (e.g. to a handler/listener).
       
   115 	 * Is specific for particular format/parser.
       
   116 	 * 
       
   117 	 * This method is called at least once at the end of the stream (with whole stream content in read buffers).
       
   118 	 * Usually it is called more often, by default: after each write (with just already received data part in read buffers).
       
   119 	 */
       
   120 	virtual void update() = 0;
       
   121 };
       
   122 
       
   123 }
       
   124 }
       
   125 }
       
   126 }