Hello : environment : Linux Manjaro 20.1.2 XFCE GCC-LIBGCC-NIM

I would like to share, I was helped I spent more than 48 hours in a row ....

To relate the Linux C system development book to Nim. So a lot of reading, to 
match the programs correctly, as well as going from array to char * to cstring 
etc in a pipe that does what it wants;) ... I also discovered the book-nim well 
done ...

I unraveled .... my tests were done in C / C ++ via NIM identically VS-Code for 
Nim and Geany for C / C ++

The comments are in the program .... the program could be optimized but it is 
intended for study.

open & send

qmsend.nim 
    
    
    import posix
    import strformat
    import unicode
    
    let QUEUE_NAME  = "/QMname"   # slash required
    
    let MAX_NMSG    = 50          # nbr max de message  attention 10 de base  
voir kernel
    
    
    let MSGLEN:int   = 2048       # msg text length --> Linux default 8192
    
    var mqd_t : Mqd               # code retour mq_open       -1 error
    var mqd_a : MqAttr            # attribut mqreceive
    
    var bytes_send :cstring       # variable mq_receive
    
    var mq_priority: int          # variable mqreceive priority
    
    var send:cint                 # code retour mq_receive    -1 error
    
    var ctrl : int                # code retour mq_getattr    -1 error
    
    
    
    
    
    
    proc main() =
      
      # initialize the queue attributes
      
      mqd_a.mq_flags = 0          # 0 Normal O_NONBLOCK no block
      
      mqd_a.mq_maxmsg = 10        # max message next wait
      
      mqd_a.mq_msgsize = MSGLEN   # len mq_msgsize global général
      
      mqd_a.mq_curmsgs = 0        # nbr message dans la mq
      
      
      discard mq_unlink(QUEUE_NAME) # détache MQ
      discard mq_close(mqd_t)       # destroy MQ
      
      # create the message
      mqd_t =  mq_open(QUEUE_NAME,O_RDWR or O_CREAT,0o640,mqd_a.addr ) # umask 
640 Linux manuel
      echo fmt"mqd_t--> {mqd_t}"
      
      
      # send message for test
      
      bytes_send = ""
      bytes_send = fmt"ôêèéîà123456"
      send = mq_send(mqd_t, bytes_send,len($bytes_send), 12);
      echo fmt"{bytes_send}   {runeLen($bytes_send)}   {len($bytes_send)}"
      
      
      bytes_send = fmt"Bonjour"
      send = mq_send(mqd_t, bytes_send,runeLen($bytes_send),20)
      echo fmt"{bytes_send}   {runeLen($bytes_send)}"
      
      for i in 1..4:
        bytes_send = ""
        bytes_send = fmt"message num:{$i}"
        send = mq_send(mqd_t, bytes_send,runeLen($bytes_send), 15)
        echo fmt"{bytes_send}   {runeLen($bytes_send)}"
      
      bytes_send = ""
      bytes_send = fmt"exit"
      send = mq_send(mqd_t, bytes_send,runeLen($bytes_send), 10);
      echo fmt"{bytes_send}   {runeLen($bytes_send)}"
      
      ctrl = mq_getattr(mqd_t, mqd_a.addr)
      echo fmt"ctrl--> {ctrl}"
      echo fmt"getattr-->  mqd_a.mq_flag={mqd_a.mq_flags} -- 
mqd_a.mq_maxmsg={mqd_a.mq_maxmsg} -- mqd_a.mq_msgsize={mqd_a.mq_msgsize} -- 
mqd_a.mq_curmsgs={ mqd_a.mq_curmsgs}"
    
    main()
    
    
    Run

\--------------------------------------------------------------------- receive 
& close mq

qmreceive.nim 
    
    
    import posix
    import strformat
    
    let QUEUE_NAME  = "/QMname"
    
    let MSGTXTLEN:int   = 2048    # msg text length --> Linux default 8192
    
    var mqd_t : Mqd               # key mqreceive
    
    var mqd_a : MqAttr            # attribut mqreceive
    
    var tab: array[0..128, char]  # variable mqreceive buffer spécifique
    
    var mq_priority: int          # variable mqreceive priority
    
    var rcv : int                 # code retour mq_receive    nbr byte
    
    var ctrl : int                # code retour mq_getattr    -1 error
    
    var rlen* : int               # variable mqreceive len
    
    var rcv_val: string           # zone de travail
    
    
    # détricotte buffer mqueue
    func mqBuffer(a1: array[0..128, char]) : string =
      var i = 0
      while a1[i] != '\x00':
          result.add( $a1[i] )
          inc(i)
    
    
    proc main() =
      
      # create the message
      mqd_t =  mq_open(QUEUE_NAME,O_RDWR)
      echo fmt"mqd_t--> {mqd_t}"
      
      ctrl = mq_getattr(mqd_t, mqd_a.addr)
      
      while mqd_a.mq_curmsgs > 0:
        
        ctrl = mq_getattr(mqd_t, mqd_a.addr)
        for i in 0..127 :  tab[i] ='\x00'
        rlen = mqd_a.mq_msgsize
        mq_priority= 999
        rcv = mq_receive(mqd_t, tab.addr, rlen,mq_priority)
        rcv_val = mqBuffer(tab)
        
        ctrl = mq_getattr(mqd_t, mqd_a.addr)
        #echo fmt"tab--> {$tab}"
        echo fmt"rcv--> {rcv}   bytes_read={$rcv_val}<-- len={len(rcv_val)}  
q_priority={mq_priority}"
        #echo fmt"getattr-->  mqd_a.mq_flag={mqd_a.mq_flags} -- 
mqd_a.mq_maxmsg={mqd_a.mq_maxmsg} -- mqd_a.mq_msgsize={mqd_a.mq_msgsize} -- 
mqd_a.mq_curmsgs={ mqd_a.mq_curmsgs}"
      
      
      discard mq_unlink(QUEUE_NAME) # détache MQ
      discard mq_close(mqd_t)       # destroy MQ
    
    main()
    
    
    Run

interest: for example to have independent "view / list / update" tables and can 
communicate with the master programs, we can also a message server which will 
take care of launching the jobs and correctly parameterizing, etc ...

My next thing would be to do real parallel processing or rather to be able to 
open interactive programs independently of the parent who starts it and vice 
versa.

Reply via email to