Skip to content
Snippets Groups Projects
Select Git revision
  • aadc86900db143e54b6c2812e659a35112c49287
  • master default protected
  • release/202005
  • release/202001
  • release/201912
  • release/201911
  • release/releaseWindowsTestOne
  • release/windowsReleaseTest
  • release/releaseTest
  • release/releaseWindowsTest
  • release/201910
  • release/qt/201910
  • release/windows-test/201910
  • release/201908
  • release/201906
  • release/201905
  • release/201904
  • release/201903
  • release/201902
  • release/201901
  • release/201812
  • 4.0.0
  • 2.2.0
  • 2.1.0
  • 2.0.1
  • 2.0.0
  • 1.4.1
  • 1.4.0
  • 1.3.0
  • 1.2.0
  • 1.1.0
31 results

active-agent.scm

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    active-agent.scm 2.59 KiB
    #!/usr/bin/env -S ./agent.exe -s
    !#
    
    ;;; Commentary:
    ;;;
    ;;; This is an example of an active agent.
    ;;;
    ;;; The active agent ensure that an account is created and then call its peer
    ;;; every minute.
    ;;;
    ;;; Code:
    
    ;;; We import here Jami's primitives.
    (use-modules
     (ice-9 threads)
     (agent)
     ((jami account) #:prefix account:)
     ((jami signal) #:prefix jami:)
     ((jami call) #:prefix call:)
     ((jami logger) #:prefix jami:))
    
    (define* (make-a-call from to #:key (timeout 30) (media-flow 10))
      "Make a call from account id FROM to peer id TO.
    If call is not in state CURRENT before TIMEOUT, returns #f, otherwise the call is
    hang up after MEDIA-FLOW seconds and #t is returned.
    "
      (jami:info "Placing call from:~a to:~a" from to)
    
      (let ([mtx (make-recursive-mutex)]
            [cnd (make-condition-variable)]
            [this-call-id ""]
            [continue #t])
    
        (with-mutex mtx
          (jami:on-signal 'state-changed
                          (lambda (account-id call-id state code)
                            (with-mutex mtx
                              (when (and continue
                                         (string= account-id from)
                                         (string= call-id this-call-id)
                                         (string= "CURRENT" state))
                                (signal-condition-variable cnd))
                              continue)))
    
          (set! this-call-id (call:place-call/media from to))
    
          (let ([success (wait-condition-variable cnd mtx
                                                  (+ (current-time) timeout))])
            (when success
              (sleep media-flow) ; Wait for media to flow between peers.
              (call:hang-up from this-call-id))
            (set! continue #f)
            success))))
    
    ;;; Change FIXME for the peer id you want to contact.
    (define peer "FIXME")
    
    ;;; This will create your agent and wait for its announcement on the DHT (see
    ;;; (agent)).
    (define agent (make-agent "bfbfbfbfbfbfbfbf"))
    
    ;;; This will make you friend with the other peer.  You need to accept the trust
    ;;; request within a certain amount of time (see (agent)).
    (make-friend agent peer)
    
    ;;; You can change the value of MEDIA-FLOW and GRACE-PERIOD.
    (let loop ([account (account-id agent)]
               [media-flow 7]
               [grace-period 30])
      (make-a-call account peer #:media-flow media-flow)
    
      ;; Disabling our account for GRACE-PERIOD.
      (jami:info "Disabling account")
      (account:send-register account #f)
      (sleep grace-period)
    
      ;; Renabling our account and wait GRACE-PERIOD.
      (jami:info "Enabling account")
      (account:send-register account #t)
      (sleep grace-period)
    
      (loop account media-flow grace-period))