Simulation Core
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Redis_Interface.f90 7.3 KiB

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