Contents

Why we need to know what's State Machine and why we need it

   Aug 13, 2023     3 min read

Hi I writing this document because I think all Developer needing to know what's state machine and use it.

maybe you ask why?
let me i telling a example for you

in my projects i must handle too many data and all of them coming from a server
maybe now you asking it’s not problem we can read it very easy like use of just function readAll in library QTcpSocket or i don’t know it’s\depended your language using or your library but it’s really easy because you don’t want use of sysCall kernels

all of theme until this is ok but a mistake happen and too many moving data to you
and you reading all of theme but now what’s must happen ?
thinking to it
it’s has many answers
maybe your program crashed or you lost many data or your parser and your algorithm can’t handle because you can’t decide which data is for use of your function
ok now you told me what’s the best way for solving this problem maybe you have many answers but The most basic way to use is use of state machine

Explain why must to know state machine

let me i explain it whit an example

when you are riding a machine and using it you don’t want to know anything about it
and you just need to know when time must use pedal
because all operation are happen other class and you just need to know how you must work with pedal
yes just this

but when you start write a class for handle pedal you must need to know many things
like write a class for know when time our class must upper speed machine or when time must down speed machine
and it’s so easy you just must write a function for input data and handle it and if to many data coming for you
you will not have much problems

What's state machine

it’s a category of states of happen in your program

like example problem network i told you
let me i explain better than problem network
in your server when data must send to you
it’s use a character in header for set this is start of message and it’s start from here
and using a ending character for set this is end of message
when can use a state machine for handle it very easy
because you can parse it and be make sure your data is right
and maybe you using a switch case in your function and handle it with a enum and whit it you have very beautiful code
and can reading it very easy

Example Code

while (DATA_STATUS == DATASTATUS::PHASE_1 && _byteArray.size() >= PACKET_DATA_HEADER)  {
        if (_byteArray[0] == 'H' && _byteArray[1] == 'i') {
            DATA_STATUS = DATASTATUS::PHASE_2;
            _byteArray.remove(0, PACKET_DATA_HEADER);
        }
        else {
            _byteArray.remove(0, PACKET_DATA_HEADER);
        }
    }

    if (DATA_STATUS == DATASTATUS::PHASE_2 && _byteArray.size() >= PACKET_DATA_SIZE) {
        memcpy(_dataRecive, _byteArray.constData(), PACKET_DATA_SIZE);
        _byteArray.remove(0, PACKET_DATA_SIZE);
        DATA_STATUS = DATASTATUS::PHASE_1;

        double* _dataConverted = reinterpret_cast<double*>(_dataRecive);
        m_list.append(*_dataConverted);
        m_pastList.append(*_dataConverted);

        sendNumbers();
    } 

Explain it

i use a while for check all message coming and ensure it’s bigger bigger than or equal my header file
because i must check header start message for find new messages

while (DATA_STATUS == DATASTATUS::PHASE_1 && _byteArray.size() >= PACKET_DATA_HEADER)

in an example server send me two character for start message:
i reading all message coming until find header and ensure new message start it

if (_byteArray[0] == 'H' && _byteArray[1] == 'i')


example i found new message and i set state machine two another phase for other works

DATA_STATUS = DATASTATUS::PHASE_2;


i remove header because i found new message

_byteArray.remove(0, PACKET_DATA_HEADER);


and if i can’t find it
i must just remove as much as the size of the start message and back two while for iterate again for find start message
and in phase two i parse message and again going back two while for find other new message for parsing

summary

state machine is just a collection of deciding for working.

thank you for reading this post I wish you to have a nice day , have a great time.