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.
 
 
 
 
 
 

140 rivejä
5.0 KiB

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