Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

199 regels
6.9 KiB

  1. # 1 "/home/admin/SimulationCore2/Redis/Redis_Interface.f90"
  2. Module RedisInterface
  3. use iso_c_binding, only: c_char
  4. ! USE Kinds, ONLY: rk, ck
  5. TYPE FString
  6. CHARACTER(KIND=c_char,LEN=:),allocatable :: item
  7. END TYPE FString
  8. character(len=20000)::buffer
  9. Interface
  10. SUBROUTINE addnums(a, b) BIND(C,name='addnums')
  11. USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
  12. IMPLICIT NONE
  13. INTEGER(C_INT) :: a, b
  14. END SUBROUTINE addnums
  15. SUBROUTINE initConnection_C(address,port,password,datakey,status) BIND(C, name='initConnection')
  16. use iso_c_binding, only: c_char,c_int
  17. character(kind=c_char) :: address(*)
  18. integer(kind=c_int)::port,status
  19. character(kind=c_char) :: password(*),datakey(*)
  20. END SUBROUTINE initConnection_C
  21. SUBROUTINE setData_C(part,str) BIND(C,name='setData')
  22. use iso_c_binding, only: c_char
  23. character(kind=c_char) :: str(*)
  24. character(kind=c_char) :: part(*)
  25. END SUBROUTINE setData_C
  26. FUNCTION getData_C(len) BIND(C,name='getData')
  27. use iso_c_binding, only: c_ptr,c_int
  28. integer(kind=c_int)::len
  29. type(c_ptr) :: getData_C
  30. END FUNCTION getData_C
  31. SUBROUTINE getData_C_bystr(str) BIND(C,name='getData_bystr')
  32. use iso_c_binding, only: c_ptr,c_int,c_char
  33. type(c_ptr) :: str
  34. END SUBROUTINE getData_C_bystr
  35. SUBROUTINE getData_C_byfile() BIND(C,name='getData_byfile')
  36. END SUBROUTINE getData_C_byfile
  37. SUBROUTINE deallocateData() BIND(C, name='deallocData')
  38. END SUBROUTINE deallocateData
  39. SUBROUTINE listenToChannel_C() BIND(C,name='listenTochannel')
  40. END SUBROUTINE listenToChannel_C
  41. SUBROUTINE publishMessageToChannel_C(str) BIND(C,name='publishMessageToChannel')
  42. use iso_c_binding, only: c_char
  43. character(kind=c_char) :: str(*)
  44. END SUBROUTINE publishMessageToChannel_C
  45. ! Fortran subroutine to deallocate the memory
  46. SUBROUTINE deallocate_c_string(c_pointer) BIND(C, NAME='free')
  47. use iso_c_binding, only: c_char,c_ptr
  48. ! IMPORT :: C_PTR
  49. TYPE(c_ptr) :: c_pointer
  50. ! No need for additional code here; the BIND attribute takes care of calling the C free function
  51. END SUBROUTINE deallocate_c_string
  52. SUBROUTINE getData2_C(result,len) BIND(C, name='getData2')
  53. USE ISO_C_BINDING, ONLY: C_PTR, C_INT
  54. TYPE(C_PTR),VALUE :: result
  55. INTEGER(C_INT) :: len
  56. END SUBROUTINE getData2_C
  57. End Interface
  58. contains
  59. ! Fortran subroutine to deallocate the memory
  60. ! SUBROUTINE deallocate_c_string(c_ptr) BIND(C, NAME='free')
  61. ! USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_F_POINTER
  62. ! IMPORT :: C_PTR, C_FUNPTR, C_F_POINTER
  63. ! TYPE(C_PTR), INTENT(IN) :: c_ptr
  64. ! TYPE(C_FUNPTR) :: free_func
  65. ! INTERFACE
  66. ! SUBROUTINE c_free(ptr) BIND(C, NAME='free')
  67. ! IMPORT :: C_PTR
  68. ! TYPE(C_PTR), VALUE :: ptr
  69. ! END SUBROUTINE c_free
  70. ! END INTERFACE
  71. ! free_func = C_FUNLOC(c_free)
  72. ! CALL C_F_POINTER(c_ptr, free_func)
  73. ! END SUBROUTINE deallocate_c_string
  74. SUBROUTINE initConnection(address,port,password, datakey,status)
  75. use iso_c_binding, only: c_null_char,c_char
  76. ! use json_module
  77. character(len=*) :: password,address,datakey
  78. integer::port,status
  79. character(len=:),allocatable::c_address,c_password,c_datakey
  80. c_datakey = datakey//c_null_char
  81. c_password = password//c_null_char
  82. c_address = address//c_null_char
  83. call initConnection_C(c_address,port,c_password,c_datakey,status)
  84. ! print *,"returned to initConnection"
  85. END SUBROUTINE initConnection
  86. SUBROUTINE setState(str)
  87. use SimulationVariables
  88. use iso_c_binding, only: c_null_char
  89. character(len=*):: str
  90. character(len=len_trim(str)+1)::c_str
  91. character(len=6)::part
  92. part = "state"//c_null_char
  93. c_str = str//c_null_char
  94. if(log_level>4) print *,"setting Data: ",len_trim(str)
  95. call setData_C(part,c_str)
  96. END SUBROUTINE setState
  97. SUBROUTINE setData(str)
  98. use SimulationVariables
  99. use iso_c_binding, only: c_null_char
  100. character(len=*):: str
  101. character(len=len_trim(str)+1)::c_str
  102. character(len=4)::part
  103. part = "out"//c_null_char
  104. c_str = str//c_null_char
  105. if(log_level>4) print *,"setting Data: ",len_trim(str)
  106. call setData_C(part,c_str)
  107. END SUBROUTINE setData
  108. SUBROUTINE getData(string,len)
  109. use iso_c_binding, only: c_char,c_ptr,c_f_pointer,c_null_ptr,c_int
  110. ! character(:),allocatable :: getData2
  111. character(:),allocatable :: string
  112. integer :: string_shape(1)
  113. integer(c_int):: len
  114. type(c_ptr) :: c_string
  115. integer::l
  116. l = 30000
  117. string_shape(1) = l!int(l,kind=kind(Integer))
  118. ! print *,"reading data l=",l
  119. if(.not. allocated(string)) then
  120. print *,"allocate string"
  121. allocate(character(l) :: string)
  122. endif
  123. len = l
  124. c_string = getData_C(len)
  125. ! print *,"data read. l=",l
  126. ! len = int(c_len,kind=kind(len))
  127. call c_f_pointer(c_string, string, string_shape)
  128. ! call deallocateData()
  129. c_string=c_null_ptr
  130. ! call deallocate_c_string(c_string)
  131. ! string = trim(string)
  132. ! print *,len_trim(string), "chars read."
  133. ! print *,string(1:l)
  134. ! getData2=string
  135. END SUBROUTINE getData
  136. ! SUBROUTINE getData2(string, len)
  137. ! USE ISO_C_BINDING, ONLY: C_PTR, C_INT,c_f_pointer
  138. ! CHARACTER(LEN=:), ALLOCATABLE :: string
  139. ! INTEGER :: len
  140. ! TYPE(C_PTR) :: c_string
  141. ! ! Allocate the buffer in Fortran
  142. ! ALLOCATE(CHARACTER(LEN=len) :: string)
  143. ! ! Call the C function to get the data
  144. ! ! CALL getData2_C(c_string)
  145. ! ! Copy the data from the C pointer to the Fortran string
  146. ! CALL C_F_POINTER(c_string, string, [len])
  147. ! ! Deallocate the C pointer
  148. ! CALL deallocate_c_string(c_string)
  149. ! ! Trim the string to remove any padding
  150. ! string = TRIM(string)
  151. ! END SUBROUTINE getData2
  152. subroutine ListenToChannel()
  153. print *, "ListenToChannel"
  154. call listenToChannel_C()
  155. end subroutine ListenToChannel
  156. subroutine test() bind (C,name="test")
  157. print *, "test function in fortran."
  158. end subroutine test
  159. subroutine publishMessageToChannel(str)
  160. use SimulationVariables
  161. use iso_c_binding, only: c_null_char
  162. character(len=*):: str
  163. character(len=len_trim(str)+1)::c_str
  164. if(str .ne. 'ack') then
  165. print *, "publishMessageToChannel: ", str
  166. endif
  167. c_str = str//c_null_char
  168. ! if(log_level>4) print *,"Sending message: ",len_trim(str)
  169. call publishMessageToChannel_C(c_str)
  170. end subroutine publishMessageToChannel
  171. END Module RedisInterface