From 0b09e0ab60bd030cfc537c12ac826be18a5fc514 Mon Sep 17 00:00:00 2001 From: zongor Date: Tue, 10 Oct 2023 23:49:28 -0400 Subject: [PATCH] add json module --- .gitignore | 1 - common/sql/test.db3 | Bin 8192 -> 8192 bytes fortran/client/fpm.toml | 3 +- fortran/client/src/player.f90 | 70 ++++++++++++++++++++----- fortran/server/.test.sh.swp | Bin 12288 -> 0 bytes fortran/server/app/main.f90 | 54 ++++++++++++++----- fortran/server/fpm.toml | 3 +- fortran/server/src/db.f90 | 78 +++++++++++----------------- fortran/server/test/json_test.sh | 2 + fortran/server/test/player.json | Bin 0 -> 87 bytes fortran/server/test/server_test.f90 | 7 --- 11 files changed, 134 insertions(+), 84 deletions(-) delete mode 100644 fortran/server/.test.sh.swp create mode 100755 fortran/server/test/json_test.sh create mode 100644 fortran/server/test/player.json delete mode 100644 fortran/server/test/server_test.f90 diff --git a/.gitignore b/.gitignore index 9fa917e..a260995 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,3 @@ a.out *.*~ build/ -fortran/client/client diff --git a/common/sql/test.db3 b/common/sql/test.db3 index 5d766a5d12b2982e3f06f2d9968fc48d490e7aca..8272975cbb2769743271b9343f023c68c08073df 100644 GIT binary patch delta 59 zcmZp0XmFSy&B!)U#+jduK`)t+mw|zSkzbC1Uv9IYzy`j}a{OlnL^)ZKGZM3l42_IU POp_QEFuY3n=fMF0c2f>X delta 59 zcmZp0XmFSy&B!`Y#+jd$K`-k9F9QPuBflI2zuaa)fen0{<@nDEh;pzbXC!7985$Xz Pm?kkSV0e}C&w~R1nbZ#) diff --git a/fortran/client/fpm.toml b/fortran/client/fpm.toml index 508f25a..8f289a3 100644 --- a/fortran/client/fpm.toml +++ b/fortran/client/fpm.toml @@ -16,4 +16,5 @@ library = false implicit-typing = false implicit-external = false source-form = "free" -[dependencies] \ No newline at end of file +[dependencies] +json-fortran = { git = "https://github.com/jacobwilliams/json-fortran.git" } \ No newline at end of file diff --git a/fortran/client/src/player.f90 b/fortran/client/src/player.f90 index d479e2f..0853b45 100644 --- a/fortran/client/src/player.f90 +++ b/fortran/client/src/player.f90 @@ -2,6 +2,7 @@ module player_mod use iso_fortran_env use iso_c_binding use raylib + use json_module use mod_dill, only: ipaddr, ipaddr_remote, IPADDR_IPV4, mrecv, msend, tcp_connect, & suffix_attach implicit none @@ -90,28 +91,59 @@ contains character(len=256) :: f_message character(len=*), parameter :: TCP_SUFFIX = c_carriage_return//c_new_line//c_null_char + type(json_value), pointer :: root, user + type(json_file) :: json + character(len=:), allocatable :: str + character(len=255) :: jsn_string + character(len=10) :: i_str + logical :: found + rc = ipaddr_remote(addr, '127.0.0.1'//c_null_char, 5555_c_int, IPADDR_IPV4, -1_c_int64_t) connection = tcp_connect(addr, -1_c_int64_t) connection = suffix_attach(connection, TCP_SUFFIX, 2_c_size_t) - write (f_message, "(f8.2, f8.2, i3, a24)") & - this%position%x, this%position%y, request_type, this%username + call json%initialize() + call json%create_object(root, '') + call json%create_object(user, 'user') - print *, f_c_string(f_message, .true.) + call json%add(user, 'username', this%username) + call json%add(user, 'x_pos', this%position%x) + call json%add(user, 'y_pos', this%position%y) + call json%add(user, 'command', request_type) - rc = msend(connection, f_c_string(f_message, .true.), & - transfer(Len_Trim(f_c_string(f_message, .true.)), 0_c_size_t), -1_c_int64_t) + call json%add(root, user) + call json%serialize(root, str) + rc = msend(connection, f_c_string(str, .true.), & + transfer(Len_Trim(f_c_string(str, .true.)), 0_c_size_t), -1_c_int64_t) + + call json%destroy() + + call json%initialize() message_size = mrecv(connection, message, msglen, -1_c_int64_t) - print *, 'recv message: ', message(:message_size) - read (message(:message_size), '(i3)') count + print *, 'recv message: ', message + + call c_f_string(message, jsn_string) + call json%deserialize(jsn_string(:Len_Trim(jsn_string))) + + call json%info('/users', n_children=count) print *, count - do i = 0, count, 1 - message_size = mrecv(connection, message, msglen, -1_c_int64_t) - - read (message(:message_size), "(i3, i3, i3, f8.2, f8.2, a24)") & - apperance_r, apperance_g, apperance_b, x_pos, y_pos, username + do i = 0, count - 1 + write (i_str, fmt='(I10)') i + i_str = adjustl(i_str) + call json%get("/users/"//istr//"/apperance_r", apperance_r, found) + if (.not. found) stop 1 + call json%get("/users/"//istr//"/apperance_g", apperance_g, found) + if (.not. found) stop 1 + call json%get("/users/"//istr//"/apperance_b", apperance_b, found) + if (.not. found) stop 1 + call json%get("/users/"//istr//"/x_pos", x_pos, found) + if (.not. found) stop 1 + call json%get("/users/"//istr//"/y_pos", y_pos, found) + if (.not. found) stop 1 + call json%get("/users/"//istr//"/username", username, found) + if (.not. found) stop 1 if (allocated(players)) then players = [players, player(username, vector3(x_pos, 1.0_c_float, y_pos), & @@ -121,6 +153,8 @@ contains color(apperance_r, apperance_g, apperance_b, 255))] end if end do + + call json%destroy() end function send_packet ! from ivanpribec `https://fortran-lang.discourse.group/t/best-practices-for-passing-c-strings/104/12` @@ -146,4 +180,16 @@ contains end block end function + subroutine c_f_string(c_string, f_string) + character(kind=c_char), dimension(:), intent(in) :: c_string + character(len=*), intent(out) :: f_string + integer :: i + i = 1 + do while (c_string(i) /= c_null_char .and. i <= len(f_string) - 1) + f_string(i:i) = c_string(i) + i = i + 1 + end do + if (i < len(f_string)) f_string(i:) = ' ' + end subroutine c_f_string + end module player_mod diff --git a/fortran/server/.test.sh.swp b/fortran/server/.test.sh.swp deleted file mode 100644 index f528278e48adb7bcbfd9dab38776cfe499e8da89..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&Jx;?g6bJCvG9U&7f(tlwsDzZbZ4?AYU|=j_O&pgNk~lanLx&=`3n$_L+=3C| zG|9pcsa>i_e=E_GKVtd)qzqABG`hdNV>jN2Y&#`7eE5yOCVe{kAX=&HQR#A`y*l`svk-v576MB%bFYu5 z{$#v))dv?2J3pIlu>qDs00Izz00bZa0SG_<0$mp{d5_*~68$=ngF5#9#kz|C0SG_< z0uX=z1Rwwb2tWV=5P(1j1;Uu<{eCQUC&_(&E%2B`bw$E>18rFEKY2$W=