fortran66のブログ

fortran について書きます。

ZMQ を利用して Fortran で http server

ZMQ zmq-socket

ZMQ の zmq-socket 関数のサンプルに、http server の例題があったので、真似して Fortran で書いて見ました。

zmq_socket(3) - 0MQ Api

ZeroMQ: Messaging for Many Applications

ZeroMQ: Messaging for Many Applications

ZeroMQ

ZeroMQ

実行例

f:id:fortran66:20181031002555p:plain

ソース・プログラム

成り行きでいじっていたので、examples ディレクトリに作ってしまいましたw

O@HP8:~/f77_zmq-master/examples$ gfortran -g -O2 -Wall -Wno-unused -fopenmp test.f ../travis_ci/lib/libzmq.so libf77zmq.a -lzmq

include file のために、固定形式にしたので 72 桁制限があります。

      program server
        implicit none
        include 'f77_zmq.h'
        integer(ZMQ_PTR) :: context
        integer(ZMQ_PTR) :: socket
        character(len = :), allocatable :: id, raw, http_response
        integer :: rc, id_size, iraw_size
        character(len = *), parameter :: CRLF = achar(13) // achar(10)

        allocate (character(len = 256):: id, raw)
        http_response = 'HTTP/1.0 200 OK'    // CRLF //
     &                  'Content-Type: text/html' // CRLF //
     &                                                CRLF //
     &    '<!DOCTYPE html>'                        // CRLF //
     &    '<html>'                                 // CRLF //
     &    '<head>'                                 // CRLF //
     &    '<title>Fortran ZMQ http server</title>' // CRLF //
     &    '</head>'                                // CRLF //
     &    '<body>'                                 // CRLF //
     &    '<h1>Fortran ZMQ http server </h1>'      // CRLF //
     &    '<p>Make Fortran Great Again!</p>'       // CRLF //
     &    '</body>'                                // CRLF //
     &    '</html>'

        context   = f77_zmq_ctx_new()
        socket    = f77_zmq_socket(context, ZMQ_STREAM)
        rc        = f77_zmq_bind(socket, 'tcp://*:8080')
        if (rc /= 0) stop 'Initialization error!'

        do
          id_size = f77_zmq_recv(socket, id, 256, 0)
          if (id_size <= 0) stop 'id_size error!'
          do
            iraw_size = f77_zmq_recv(socket, raw, 256, 0)
            if (iraw_size < 0) stop 'raw_size error!'
            if (iraw_size == 256) cycle
            exit
          end do
          rc = f77_zmq_send(socket, id, id_size, ZMQ_SNDMORE)
          rc = f77_zmq_send(socket, http_response,
     &                                  len(http_response), 0)
          rc = f77_zmq_send (socket, id, id_size, ZMQ_SNDMORE)
          rc = f77_zmq_send (socket, 0, 0, 0)
        end do

        rc = f77_zmq_close(socket)
        rc = f77_zmq_ctx_destroy(context)
      end program server

Modern Fortran Explained: Incorporating Fortran 2018 (Numerical Mathematics and Scientific Computation)

Modern Fortran Explained: Incorporating Fortran 2018 (Numerical Mathematics and Scientific Computation)

Parallel Programming with Co-arrays (Chapman & Hall/CRC Computational Science)

Parallel Programming with Co-arrays (Chapman & Hall/CRC Computational Science)

Modern Missile Guidance

Modern Missile Guidance