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.
 
 
 
 
 
 

138 lines
5.2 KiB

  1. Module RedisInterface
  2. ! use iso_c_binding, only: c_char
  3. Interface
  4. SUBROUTINE addnums(a, b) BIND(C,name='addnums')
  5. USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
  6. IMPLICIT NONE
  7. INTEGER(C_INT) :: a, b
  8. END SUBROUTINE addnums
  9. SUBROUTINE initConnection_C(address,port,password,datakey,status) BIND(C, name='initConnection')
  10. use iso_c_binding, only: c_char,c_int
  11. character(kind=c_char) :: address(*)
  12. integer(kind=c_int)::port,status
  13. character(kind=c_char) :: password(*),datakey(*)
  14. END SUBROUTINE initConnection_C
  15. SUBROUTINE setData_C(part,str) BIND(C,name='setData')
  16. use iso_c_binding, only: c_char
  17. character(kind=c_char) :: str(*)
  18. character(kind=c_char) :: part(*)
  19. END SUBROUTINE setData_C
  20. FUNCTION getData_C(len) BIND(C,name='getData')
  21. use iso_c_binding, only: c_ptr,c_int
  22. integer(kind=c_int)::len
  23. type(c_ptr) :: getData_C
  24. END FUNCTION getData_C
  25. SUBROUTINE deallocateData() BIND(C, name='deallocData')
  26. END SUBROUTINE deallocateData
  27. End Interface
  28. contains
  29. SUBROUTINE initConnection(configFilename,status)
  30. use json_module
  31. use iso_c_binding, only: c_null_char,c_char
  32. character(len=*) :: configFilename
  33. integer::port,status
  34. type(json_file) :: jsonfile
  35. type(json_value),pointer :: jsonvalue,jsonvalue2
  36. type(json_core) :: jsoncore
  37. character(len=:),allocatable :: password,address,datakey
  38. character(len=:),allocatable::c_address,c_password,c_datakey
  39. call jsonfile%initialize()
  40. print *,"Initilized: simulation with ",configFilename
  41. call jsonfile%load_file(configFilename);
  42. if (jsonfile%failed()) then
  43. print *,"can not open config file: ",configFilename ;
  44. stop
  45. endif
  46. ! print *,"file read"
  47. call jsonfile%json_file_get_root(jsonvalue)
  48. call jsoncore%get(jsonvalue,'redis',jsonvalue2)
  49. call jsoncore%get(jsonvalue2,"address",address);
  50. call jsoncore%get(jsonvalue2,"port",port);
  51. call jsoncore%get(jsonvalue2,"password",password);
  52. call jsoncore%get(jsonvalue2,"datakey",datakey);
  53. c_datakey = datakey//c_null_char
  54. c_password = password//c_null_char
  55. c_address = address//c_null_char
  56. call initConnection_C(c_address,port,c_password,c_datakey,status)
  57. ! print *,"returned to initConnection"
  58. END SUBROUTINE initConnection
  59. SUBROUTINE setData(str)
  60. use SimulationVariables
  61. use iso_c_binding, only: c_null_char
  62. character(len=*):: str
  63. character(len=len_trim(str)+1)::c_str
  64. character(len=4)::part
  65. part = "out"//c_null_char
  66. c_str = str//c_null_char
  67. if(logging>4) print *,"setting Data: ",len_trim(str)
  68. call setData_C(part,c_str)
  69. END SUBROUTINE setData
  70. SUBROUTINE setInput(str)
  71. use SimulationVariables
  72. use iso_c_binding, only: c_null_char
  73. character(len=*):: str
  74. character(len=len_trim(str)+1)::c_str
  75. character(len=4)::part
  76. part = "in"//c_null_char
  77. c_str = str//c_null_char
  78. if(logging>4) print *,"setting Data: ",len_trim(str)
  79. call setData_C(part,c_str)
  80. END SUBROUTINE setInput
  81. SUBROUTINE getData(string)
  82. use iso_c_binding, only: c_char,c_ptr,c_f_pointer,c_null_ptr
  83. ! character(:),allocatable :: getData2
  84. character(:),allocatable :: string
  85. integer :: string_shape(1)
  86. type(c_ptr) :: c_string
  87. integer::l
  88. l = 30000
  89. ! print *,"reading data l=",l
  90. c_string = getData_C(l)
  91. ! print *,"data read. l=",l
  92. ! len = int(c_len,kind=kind(len))
  93. string_shape(1) = l!int(l,kind=kind(Integer))
  94. if(.not. allocated(string)) then
  95. print *,"allocate string"
  96. allocate(character(l) :: string)
  97. endif
  98. call c_f_pointer(c_string, string, string_shape)
  99. c_string=c_null_ptr
  100. string = trim(string)
  101. ! print *,len_trim(string), "chars read."
  102. ! print *,string(1:l)
  103. ! getData2=string
  104. END SUBROUTINE getData
  105. ! SUBROUTINE getData2(string) !result(string)
  106. ! use iso_c_binding, only: c_char,c_ptr,c_f_pointer,c_null_ptr
  107. ! ! character(:),allocatable :: getData2
  108. ! character(:),allocatable :: string
  109. ! integer :: string_shape(1)
  110. ! type(c_ptr) :: c_string
  111. ! integer::l
  112. ! ! print *,"reading data (getData2)"
  113. ! c_string = getData_C(l)
  114. ! ! string = c_str
  115. ! ! len = int(c_len,kind=kind(len))
  116. ! string_shape(1) = l! int(l,kind=kind(integer))
  117. ! if(.not. allocated(string)) allocate(character(l) :: string)
  118. ! call c_f_pointer(c_string, string, string_shape)
  119. ! c_string=c_null_ptr
  120. ! ! print *,len_trim(string), "chars read."
  121. ! ! print *,string(1:l)
  122. ! ! getData2=string
  123. ! END SUBROUTINE getData2
  124. END Module RedisInterface