Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GroovyServ - Technical Part

GroovyServ - Technical Part

Yasuharu Nakano

March 25, 2023
Tweet

More Decks by Yasuharu Nakano

Other Decks in Programming

Transcript

  1. groovyclient ClientConnectionRepository ݱࡏͷThreadGroupʹର͢Δ ClientConnectionΛฦ͢ ClientConnection OutputStream !script" println “Hello, GroovyServ!”

    System.out (StreamResponse OutputStream) ϓϩτίϧʹج͍ͮͯม׵ ͨ͠σʔλΛSocketͷ OutputStream#write() ʹॻ͖ग़͢ εΫϦϓτ͕System.out(err) Λ࢖ͬͯɺඪ४ग़ྗΛΫϥΠΞϯ τʹૹΔͷਤ Hello, GroovyServ! if Channelϔομ==”err” " ඪ४Τϥʔग़ྗ΁ʂ if Channelϔομ==”out” " ඪ४ग़ྗ΁ʂ
  2. ClientConnection Repository ݱࡏͷThreadGroupʹର͢Δ ClientConnectionΛฦ͢ ClientConnection PipedOutputStream PipedInputStream groovyclient StreamRequestHandler !script"

    System.in.eachLine { ... } ઀ଓ͞ΕͨύΠϓ Socket.inputStream.read()ͯ͠ɺ ϓϩτίϧʹج͍ͮͯύʔεͨ͠ϘσΟ෦Λ pipedOutputStream.write()ʹॻ͖ग़͢ System.in (StreamRequestInputStream) ClientConnection಺ͷ PipedInputStreamʹॲཧΛҕৡ εΫϦϓτ͕System.inΛ࢖ͬ ͯɺΫϥΠΞϯτ͔Βͷඪ४ೖྗ Λऔಘ͢Δͷਤ
  3. InvocationRequest 'Cwd:' <cwd> LF 'Arg:' <argn> LF 'Arg:' <arg1> LF

    'Arg:' <arg2> LF 'Cp:' <classpath> LF 'Cookie:' <cookie> LF LF where: <cwd> is current working directory. <arg1><arg2>.. are commandline arguments(optional). <classpath>.. is the value of environment variable CLASSPATH(optional). <cookie> is authentication value which certify client is the user who invoked the server. LF is line feed (0x0a, '\n').
  4. StreamRequest 'Size:' <size> LF LF <data from STDIN> where: <size>

    is the size of data to send to server. <size>==-1 means client exited. <data from STDIN> is byte sequence from standard input.
  5. StreamResponse 'Status:' <status> LF 'Channel:' <id> LF 'Size:' <size> LF

    LF <data for STDERR/STDOUT> where: <status> is exit status of invoked groovy script. <id> is 'out' or 'err', where 'out' means standard output of the program. 'err' means standard error of the program. <size> is the size of chunk. <data from STDERR/STDOUT> is byte sequence from standard output/error.
  6. Cookie / Loopback only ηΩϡϦςΟରࡦͷͨΊʹҎԼͷ2ͭͷػߏ͕ಋೖ͞Ε͍ͯΔ Only from Loopback address ʮݟ஌Β͵ผͷϚγϯ͔Β͍ͭͭͪΌͩΊʯ

    ୯७ʹɺInetAddress#isLoopbackAddress()Ͱ൑ఆ Cookie ʮLoopback addressܦ༝Ͱ΋ɺىಈͨ͠αʔόͱಉ͔ͦ͡ΕҎ্ͷΞΫηε ݖݶ͕ͳ͍Ϣʔβ͔ΒͷϦΫΤετ͸NGʯ HTTPͷCookieͱ͸શؔ͘܎ͳ͍ αʔόىಈ࣌ʹϥϯμϜจࣈྻΛΫοΩʔͱͯ͠ɺ~/.groovy/groovyserv/ cookieʹอଘ ΫϥΠΞϯτ͔ΒͷϦΫΤετ͝ͱʹɺϑΝΠϧ͔ΒಡΈऔͬͨΫοΩʔΛ Cookieϔομʹ෇͚ͯૹΓ͚ͭΔ αʔόଆͰϦΫΤετύʔε࣌ʹΫοΩʔτʔΫϯ͕Ұக͠ͳ͚Ε ͹ɺAuthentication failed
  7. ΧϨϯτσΟϨΫτϦͱ͍͏֓೦͕ཪଆʹӅṭ͞Ε͍ͯΔ JavaͰ͕͢ɺ͖ͪΜͱରॲ͠ͳ͍ͱࠔΔ৔໘͕͋Γ·͢ GroovyServͰ͸ɺαʔόىಈ࣌ͷCWDͱɺΫϥΠΞϯτ ࣮ߦ࣌ͷCWD͕ҟͳΔͨΊɺԿ΋ରॲΛ͠ͳ͍ͱɾɾɾ CWD(Current Working Directory) $ cd /tmp

    $ groovyserver -r $ cd /home/kobo $ cat > hoge.txt HOGE!! ^C $ groovyclient -e ‘println(new File(“hoge.txt”).text)’ Caught: java.io.FileNotFoundException: hoge.txt (No such file or directory) ...SNIP...
  8. ΑͬͯɺҎԼͷରॲ͕ඞཁ γεςϜϓϩύςΟ“user.dir”ʹηοτ File#getAbsolutePath()ʹӨڹ͢Δ “"user.dir", which is initialized during jvm startup,

    should be used as an informative/readonly system property”ʢposted 2008-08-18ʣɾɾɾͩͱɾɾɾʁʂ http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557 JNAΛར༻ͯ͠JVMϓϩηεࣗମͷCWDΛมߋ “user.dir”ʹΑΔઈରύεิ׬͕ͳ͔ͥ࿙Ε͍ͯΔ FileInputStreamΛؚΉଟ਺ͷΫϥεͷͨΊ ͋ͱɺCWDΛΫϥεύεʹ௥Ճ͓ͯ͘͠ GroovyͰ͸ɺ࣮ߦ͞ΕͨεΫϦϓτͱಉ͡σΟϨΫτϦʹ͋ΔεΫϦϓ τ͸ɺΫϥεύεʹೖͬͯΔ΋ͷͱͯ͠ѻΘΕΔͨΊ
  9. import com.sun.jna.Library import com.sun.jna.Native import com.sun.jna.Platform interface CLibrary extends Library

    { String libname = (Platform.isWindows() ? "msvcrt" : "c") CLibrary INSTANCE = Native.loadLibrary(libname, CLibrary.class) int chdir(String dir) int _chdir(String dir) } ͱɺґଘϥΠϒϥϦΛ௥Ճ͓͍ͯͯ͠#ͰOKʂศརʂ <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>3.2.2</version> </dependency> JNAͰcwdΛมߋ͢Δʹ͸ɺPOMϑΝΠϧͰ
  10. How to Build Maven2ͰϏϧυ $ cd groovyserv-<Version> $ mvn clean

    verify όΠφϦύοέʔδ: target/groovyserv-<Version>-<OS>-<arch>-bin.zip Integration-Test༻ͷ؀ڥʢύεΛ௨ͤ͹࣮ߦ͢Δ͜ͱ΋Մೳɻࢼߦࡨޡ࣌ʹศརʣ target/groovyserv-<Version>-<OS>-<arch>.dir/groovyserv-<Version>-<OS>-<arch> ςετͰࣦഊ͢Δ৔߹: EncodeIT͕ΤϥʔʹͳΔͳͲɺจࣈԽ͚ܥ͕ո͚͠Ε͹ɺจࣈΤϯίʔυΛUTF-8ʹઃఆ͢Δ $ export _JAVA_OPTIONS=-Dfile.encoding=UTF-8 ςετ؀ڥ͚ͩͷ໰୊Ͱ͋Ε͹͢΂ͯͷςετΛεΩοϓ͢Δํ๏΋͋Δ $ mvn -Dmaven.test.skip=true clean package Windows্ͰϏϧυ͢ΔͨΊʹ͸Cygwinͱgcc͕ඞཁ