From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM, NUMERIC_HTTP_ADDR,URIBL_BLOCKED,WEIRD_PORT autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [180.246.144.41]) by gnuweeb.org (Postfix) with ESMTPSA id 16A5580615; Sun, 21 Aug 2022 11:25:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1661081152; bh=uLHCyPuxm562c2VF9nwJ0hfgfxV64iyVX5xu2czgylM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MOkMZLgzawW8Bpmwyym070sZP06UXI+ZTimetfe8K9ZvFkm7NBo+D05KjJVvFd07K YQTn5/8a54ExwqnKei9NODKmrz+FQyOQDVK3HAMZ7lSaZ0UHgzbn91Y+s9bOI+c578 8QX3Uh/uOQHSa0lCEOI4mvVd2dkVzqQgUJiMYEvfm5WL/eRGYfc25AQpPytMJd9Hv6 Dd551qPzViyduZ8v+AfZJLqn6hEp4ZycWVVJ+L5ZbVgM3uNoslR3OZ+LIa1XlPbADc QkOWlLNFRSNdfXp7TYx4uoqG2St2HN2WmvNHKVHqAgPqLZYs9IGTauYhebb/ZsyNK2 1Iff0FRh1FuWA== From: Ammar Faizi To: Alviro Iskandar Setiawan Cc: Ammar Faizi , Muhammad Rizki , Kanna Scarlet , GNU/Weeb Mailing List Subject: [PATCH v1 16/22] tests/js/ring: Add JavaScript class wrapper example Date: Sun, 21 Aug 2022 18:24:47 +0700 Message-Id: <20220821112453.3026255-17-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220821112453.3026255-1-ammarfaizi2@gnuweeb.org> References: <20220821112453.3026255-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Just an example writing a wrapper to simplify the SQ/CQ mechanism. Signed-off-by: Ammar Faizi --- tests/js/ring.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/js/ring.js b/tests/js/ring.js index 6752fe1..372c6e6 100644 --- a/tests/js/ring.js +++ b/tests/js/ring.js @@ -164,6 +164,94 @@ function test_simple_http_post_set_header() assert(h.ch.read_ret() === ss.length); } +class CHNet { + constructor(cr, url, method) { + this.ch = chnet.create_net(); + this.ch.set_url(url); + this.ch.set_method(method); + this.cr = cr; + } + + prep_read(read_size) { + let ring = this.cr.ring; + let sqe = ring.get_sqe(); + if (!sqe) + return false; + sqe.prep_read(this.ch, read_size); + sqe.set_user_data(this); + return true; + } + + set_user_data(data) { + this.udata = data; + } + + get_user_data() { + return this.udata; + } + + set_header(key, val, overwrite = true) { + this.ch.set_request_header(key, val, overwrite); + } +}; + +class ChromiumNet { + constructor() { + this.ring = chnet.create_ring(4096); + } + + create(url, method = "GET") { + return new CHNet(this, url, method); + } + + for_each_cqe(cb = null) { + let ret = this.ring.for_each_cqe(function (cqe) { + + if (cb) + cb(cqe); + + let ch = cqe.user_data; + let ret = cqe.res; + let udata = ch.udata; + + if (ret < 0 && ch.onerror) + ch.onerror(ch, ret, udata); + else + ch.onreadcomplete(ch, ret, udata); + }); + this.ring.cq_advance(ret); + return ret; + } +}; + +function test_classes() +{ + const UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"; + const NR_REQ = 10; + + let cr = new ChromiumNet; + let ch_arr = []; + let i, j, ch; + + for (i = 0; i < NR_REQ; i++) { + ch = cr.create("http://127.0.0.1:8000/index.php?action=hello"); + ch.set_header("User-Agent", UA); + ch.prep_read(4096); + ch.set_user_data(i); + ch.onerror = function (cr, ret, udata) { + console.log(`Req: ${udata} returned error: ${cr.ch.get_error()}`); + }; + ch.onreadcomplete = function (cr, ret, udata) { + console.log(`Req: ${udata} returned ${ret} bytes: ${cr.ch.read_buf()}`); + }; + ch_arr[i] = ch; + } + + cr.ring.submit_sqe(); + cr.ring.wait_cqe(1); + cr.for_each_cqe(); +} + function main() { test_nop(); @@ -171,6 +259,7 @@ function main() test_simple_http(); test_simple_http_set_header(); test_simple_http_post_set_header(); + test_classes(); } main(); -- Ammar Faizi