Context in Co-array Fortran

Here’s the translation of the Go code to Co-array Fortran, with explanations in Markdown format suitable for Hugo:

Our example demonstrates how to create a simple HTTP server and handle context cancellation. Co-array Fortran doesn’t have built-in HTTP server capabilities, so we’ll simulate this behavior using coarrays and image control statements.

program context_example
  use iso_fortran_env
  implicit none

  integer :: this_image, num_images
  logical :: server_running[*] = .false.
  character(len=100) :: message[*]

  this_image = this_image()
  num_images = num_images()

  if (this_image == 1) then
    call start_server()
  else
    call simulate_client()
  end if

contains

  subroutine start_server()
    integer :: client_image
    logical :: request_complete

    print *, "Image 1: Server started"
    server_running = .true.
    sync all

    do while (server_running)
      if (message(this_image) /= '') then
        print *, "Image 1: Request received from client"
        
        ! Simulate work
        call sleep(10)
        
        ! Check for cancellation
        if (.not. server_running) then
          print *, "Image 1: Request cancelled"
          message(this_image) = "cancelled"
        else
          message(this_image) = "hello"
        end if
        
        print *, "Image 1: Request completed"
      end if
    end do

    print *, "Image 1: Server stopped"
  end subroutine start_server

  subroutine simulate_client()
    integer :: server_image = 1

    sync all
    if (server_running(server_image)) then
      print *, "Image", this_image, ": Sending request to server"
      message(server_image) = "request"
      
      ! Simulate cancellation after 2 seconds
      call sleep(2)
      server_running(server_image) = .false.
      
      sync all
      
      if (message(server_image) == "cancelled") then
        print *, "Image", this_image, ": Request was cancelled"
      else
        print *, "Image", this_image, ": Received response:", trim(message(server_image))
      end if
    else
      print *, "Image", this_image, ": Server not running"
    end if
  end subroutine simulate_client

end program context_example

In this Co-array Fortran example, we simulate a server-client scenario using multiple images. Image 1 acts as the server, while other images act as clients.

The server runs in a loop, waiting for requests. When a request is received, it simulates work by sleeping for 10 seconds. During this time, it checks if a cancellation signal has been received.

Clients send requests to the server and then simulate a cancellation after 2 seconds. This is analogous to the context cancellation in the original example.

To run this program, you would compile it with a Co-array Fortran compiler and run it with multiple images:

$ caf context_example.f90 -o context_example
$ cafrun -np 4 ./context_example

This will run the program with 4 images: one server and three clients.

Note that this is a simplified simulation and doesn’t include actual HTTP functionality or error handling. In a real-world scenario, you would need to use additional libraries or implement network communication explicitly.