<commit_before>#!/usr/bin/env scheme-script

;; Copyright (C) 2016 <NAME>
;; 
;; This file is licensed under the Apache License, Version 2.0 (the
;; "License"); you may not use this file except in compliance with the
;; License.  You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
;; implied.  See the License for the specific language governing
;; permissions and limitations under the License.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; This is an example file for an asynchronous socket server.  It is
;; just an echo-bot - it will echo back whatever is sent to it.
;;
;; You can send connect to it with: 'telnet ::1 8000'.  Any number of
;; telnet sessions (up to the number of file descriptors permitted by
;; the system) can be run concurrently.  When the last telnet client
;; has disconnected, the server will finish.
;;
;; This file uses the chez-a-sync library from
;; https://github.com/ChrisVine/chez-a-sync

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(import (a-sync coroutines)
	(a-sync event-loop)
	(simple-sockets basic)
	(simple-sockets a-sync)
	(chezscheme))

(set-default-event-loop!)

(define count 0)
(define server-sock #f)

(define (handle-conversation port)
  (a-sync (lambda (await resume)
	    (let next ([line (await-getline! await resume port)])
	      (if (not (string=? line ""))
		  (begin
		    (await-put-string! await resume port (string-append "echo: " line "\n"))
		    (next (await-getline! await resume port)))
		  (begin
		    ;; we must call clear-input-port before applying
		    ;; close-port
		    (clear-input-port port)
		    (close-port port)
		    (set! count (- count 1))
		    (when (zero? count)
		      ;; this a-sync block can bring the execution of
		      ;; the a-sync block in start-server to an end by
		      ;; removing the watch established by
		      ;; await-accept!
		      (event-loop-remove-read-watch! server-sock))))))))

(define (start-server)
  (set-ignore-sigpipe)
  (a-sync (lambda (await resume)
	    (set! server-sock (listen-on-ipv6-socket #t 8000 5))
	    (let loop ()
	      (let* ([vec (make-bytevector 16)]
		     [accept (await-accept-ipv6-connection! await resume server-sock vec)]
		     [port (open-fd-input/output-port accept
						      (buffer-mode block)
						      (make-transcoder (utf-8-codec) 'crlf))])
		(set! count (+ count 1))<commit_msg>Correct comments in example server file<commit_after>#!/usr/bin/env scheme-script

;; Copyright (C) 2016 <NAME>
;; 
;; This file is licensed under the Apache License, Version 2.0 (the
;; "License"); you may not use this file except in compliance with the
;; License.  You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
;; implied.  See the License for the specific language governing
;; permissions and limitations under the License.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; This is an example file for an asynchronous socket server.  It is
;; just an echo-bot - it will echo back whatever is sent to it.
;;
;; You can connect to it with: 'telnet ::1 8000'.  Any number of
;; telnet sessions (up to the number of file descriptors permitted by
;; the system) can be run concurrently.  When the last telnet client
;; has disconnected, the server will finish.
;;
;; This file uses the chez-a-sync library from
;; https://github.com/ChrisVine/chez-a-sync

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(import (a-sync coroutines)
	(a-sync event-loop)
	(simple-sockets basic)
	(simple-sockets a-sync)
	(chezscheme))

(set-default-event-loop!)

(define count 0)
(define server-sock #f)

(define (handle-conversation port)
  (a-sync (lambda (await resume)
	    (let next ([line (await-getline! await resume port)])
	      (if (not (string=? line ""))
		  (begin
		    (await-put-string! await resume port (string-append "echo: " line "\n"))
		    (next (await-getline! await resume port)))
		  (begin
		    ;; we must call clear-input-port before applying
		    ;; close-port
		    (clear-input-port port)
		    (close-port port)
		    (set! count (- count 1))
		    (when (zero? count)
		      ;; this a-sync block can bring the execution of
		      ;; the a-sync block in start-server to an end by
		      ;; removing the watch established by
		      ;; await-accept-ipv6-connection!
		      (event-loop-remove-read-watch! server-sock))))))))

(define (start-server)
  (set-ignore-sigpipe)
  (a-sync (lambda (await resume)
	    (set! server-sock (listen-on-ipv6-socket #t 8000 5))
	    (let loop ()
	      (let* ([vec (make-bytevector 16)]
		     [accept (await-accept-ipv6-connection! await resume server-sock vec)]
		     [port (open-fd-input/output-port accept
						      (buffer-mode block)
						      (make-transcoder (utf-8-codec) 'crlf))])
		(set! count (+ count 1))
{'fraction_non_alphanumeric': 0.1690775878264137, 'fraction_numerical': 0.009956791283111027, 'mean_word_length': 3.381893004115226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4708266', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1580, 'n_words': 583}