crossfire/crossfire/protocol.rkt

73 lines
2.3 KiB
Racket

#lang racket/base
;; crossfire: distributed brute force infrastructure
;;
;; Copyright (C) 2020 haskal
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(require "comms.rkt" "not-crypto.rkt"
syntax/parse/define
(for-syntax racket/base racket/syntax))
;; utility functions and macros for defining rpcs
;; id generation helpers
(define-for-syntax (rpc-type-id what)
(format-id what "rpc-type-~a" (syntax-e what)))
(define-for-syntax (rpc-impl-id what)
(format-id what "rpc-impl-~a" (syntax-e what)))
;; parameters for comms, tm, and targeted node
(define current-comms (make-parameter #f))
(define current-tm (make-parameter #f))
(define current-to-node (make-parameter #f))
;; defines a class of rpcs
(define-simple-macro (define-rpc-type type:id)
#:with def-id (rpc-type-id #'type)
(define def-id (make-hash)))
;; defines an rpc implementation, registers it with a given class of rpcs and makes a wrapper to
;; call it
(define-simple-macro (define-rpc type:id (name:id args:id ...) body:expr ...)
#:with def-id (rpc-type-id #'type)
#:with impl-id (rpc-impl-id #'name)
(begin
(define (impl-id args ...) body ...)
(define (name args ...)
(tm-transact (current-tm) (current-to-node) (quote name) (list args ...)))
(hash-set! def-id (quote name) impl-id)))
;; installs all rpcs of a given rpc class into the transaction manager
(define-simple-macro (install-rpc-type type:id)
#:with def-id (rpc-type-id #'type)
(for ([(k v) (in-hash def-id)])
(tm-register-rpc (current-tm) k v)))
;; ok now we get to the real stuff
;; server rpc functions
(define-rpc-type server)
(define-rpc server (test-rpc a b)
(displayln "test rpc")
(displayln b)
(add1 a))
;; agent rpc functions
;; TODO ...